slub.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251
  1. /*
  2. * SLUB: A slab allocator that limits cache line use instead of queuing
  3. * objects in per cpu and per node lists.
  4. *
  5. * The allocator synchronizes using per slab locks and only
  6. * uses a centralized lock to manage a pool of partial slabs.
  7. *
  8. * (C) 2007 SGI, Christoph Lameter <clameter@sgi.com>
  9. */
  10. #include <linux/mm.h>
  11. #include <linux/module.h>
  12. #include <linux/bit_spinlock.h>
  13. #include <linux/interrupt.h>
  14. #include <linux/bitops.h>
  15. #include <linux/slab.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/cpu.h>
  18. #include <linux/cpuset.h>
  19. #include <linux/mempolicy.h>
  20. #include <linux/ctype.h>
  21. #include <linux/kallsyms.h>
  22. #include <linux/memory.h>
  23. /*
  24. * Lock order:
  25. * 1. slab_lock(page)
  26. * 2. slab->list_lock
  27. *
  28. * The slab_lock protects operations on the object of a particular
  29. * slab and its metadata in the page struct. If the slab lock
  30. * has been taken then no allocations nor frees can be performed
  31. * on the objects in the slab nor can the slab be added or removed
  32. * from the partial or full lists since this would mean modifying
  33. * the page_struct of the slab.
  34. *
  35. * The list_lock protects the partial and full list on each node and
  36. * the partial slab counter. If taken then no new slabs may be added or
  37. * removed from the lists nor make the number of partial slabs be modified.
  38. * (Note that the total number of slabs is an atomic value that may be
  39. * modified without taking the list lock).
  40. *
  41. * The list_lock is a centralized lock and thus we avoid taking it as
  42. * much as possible. As long as SLUB does not have to handle partial
  43. * slabs, operations can continue without any centralized lock. F.e.
  44. * allocating a long series of objects that fill up slabs does not require
  45. * the list lock.
  46. *
  47. * The lock order is sometimes inverted when we are trying to get a slab
  48. * off a list. We take the list_lock and then look for a page on the list
  49. * to use. While we do that objects in the slabs may be freed. We can
  50. * only operate on the slab if we have also taken the slab_lock. So we use
  51. * a slab_trylock() on the slab. If trylock was successful then no frees
  52. * can occur anymore and we can use the slab for allocations etc. If the
  53. * slab_trylock() does not succeed then frees are in progress in the slab and
  54. * we must stay away from it for a while since we may cause a bouncing
  55. * cacheline if we try to acquire the lock. So go onto the next slab.
  56. * If all pages are busy then we may allocate a new slab instead of reusing
  57. * a partial slab. A new slab has noone operating on it and thus there is
  58. * no danger of cacheline contention.
  59. *
  60. * Interrupts are disabled during allocation and deallocation in order to
  61. * make the slab allocator safe to use in the context of an irq. In addition
  62. * interrupts are disabled to ensure that the processor does not change
  63. * while handling per_cpu slabs, due to kernel preemption.
  64. *
  65. * SLUB assigns one slab for allocation to each processor.
  66. * Allocations only occur from these slabs called cpu slabs.
  67. *
  68. * Slabs with free elements are kept on a partial list and during regular
  69. * operations no list for full slabs is used. If an object in a full slab is
  70. * freed then the slab will show up again on the partial lists.
  71. * We track full slabs for debugging purposes though because otherwise we
  72. * cannot scan all objects.
  73. *
  74. * Slabs are freed when they become empty. Teardown and setup is
  75. * minimal so we rely on the page allocators per cpu caches for
  76. * fast frees and allocs.
  77. *
  78. * Overloading of page flags that are otherwise used for LRU management.
  79. *
  80. * PageActive The slab is frozen and exempt from list processing.
  81. * This means that the slab is dedicated to a purpose
  82. * such as satisfying allocations for a specific
  83. * processor. Objects may be freed in the slab while
  84. * it is frozen but slab_free will then skip the usual
  85. * list operations. It is up to the processor holding
  86. * the slab to integrate the slab into the slab lists
  87. * when the slab is no longer needed.
  88. *
  89. * One use of this flag is to mark slabs that are
  90. * used for allocations. Then such a slab becomes a cpu
  91. * slab. The cpu slab may be equipped with an additional
  92. * freelist that allows lockless access to
  93. * free objects in addition to the regular freelist
  94. * that requires the slab lock.
  95. *
  96. * PageError Slab requires special handling due to debug
  97. * options set. This moves slab handling out of
  98. * the fast path and disables lockless freelists.
  99. */
  100. #define FROZEN (1 << PG_active)
  101. #ifdef CONFIG_SLUB_DEBUG
  102. #define SLABDEBUG (1 << PG_error)
  103. #else
  104. #define SLABDEBUG 0
  105. #endif
  106. static inline int SlabFrozen(struct page *page)
  107. {
  108. return page->flags & FROZEN;
  109. }
  110. static inline void SetSlabFrozen(struct page *page)
  111. {
  112. page->flags |= FROZEN;
  113. }
  114. static inline void ClearSlabFrozen(struct page *page)
  115. {
  116. page->flags &= ~FROZEN;
  117. }
  118. static inline int SlabDebug(struct page *page)
  119. {
  120. return page->flags & SLABDEBUG;
  121. }
  122. static inline void SetSlabDebug(struct page *page)
  123. {
  124. page->flags |= SLABDEBUG;
  125. }
  126. static inline void ClearSlabDebug(struct page *page)
  127. {
  128. page->flags &= ~SLABDEBUG;
  129. }
  130. /*
  131. * Issues still to be resolved:
  132. *
  133. * - Support PAGE_ALLOC_DEBUG. Should be easy to do.
  134. *
  135. * - Variable sizing of the per node arrays
  136. */
  137. /* Enable to test recovery from slab corruption on boot */
  138. #undef SLUB_RESILIENCY_TEST
  139. #if PAGE_SHIFT <= 12
  140. /*
  141. * Small page size. Make sure that we do not fragment memory
  142. */
  143. #define DEFAULT_MAX_ORDER 1
  144. #define DEFAULT_MIN_OBJECTS 4
  145. #else
  146. /*
  147. * Large page machines are customarily able to handle larger
  148. * page orders.
  149. */
  150. #define DEFAULT_MAX_ORDER 2
  151. #define DEFAULT_MIN_OBJECTS 8
  152. #endif
  153. /*
  154. * Mininum number of partial slabs. These will be left on the partial
  155. * lists even if they are empty. kmem_cache_shrink may reclaim them.
  156. */
  157. #define MIN_PARTIAL 5
  158. /*
  159. * Maximum number of desirable partial slabs.
  160. * The existence of more partial slabs makes kmem_cache_shrink
  161. * sort the partial list by the number of objects in the.
  162. */
  163. #define MAX_PARTIAL 10
  164. #define DEBUG_DEFAULT_FLAGS (SLAB_DEBUG_FREE | SLAB_RED_ZONE | \
  165. SLAB_POISON | SLAB_STORE_USER)
  166. /*
  167. * Set of flags that will prevent slab merging
  168. */
  169. #define SLUB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
  170. SLAB_TRACE | SLAB_DESTROY_BY_RCU)
  171. #define SLUB_MERGE_SAME (SLAB_DEBUG_FREE | SLAB_RECLAIM_ACCOUNT | \
  172. SLAB_CACHE_DMA)
  173. #ifndef ARCH_KMALLOC_MINALIGN
  174. #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
  175. #endif
  176. #ifndef ARCH_SLAB_MINALIGN
  177. #define ARCH_SLAB_MINALIGN __alignof__(unsigned long long)
  178. #endif
  179. /* Internal SLUB flags */
  180. #define __OBJECT_POISON 0x80000000 /* Poison object */
  181. #define __SYSFS_ADD_DEFERRED 0x40000000 /* Not yet visible via sysfs */
  182. /* Not all arches define cache_line_size */
  183. #ifndef cache_line_size
  184. #define cache_line_size() L1_CACHE_BYTES
  185. #endif
  186. static int kmem_size = sizeof(struct kmem_cache);
  187. #ifdef CONFIG_SMP
  188. static struct notifier_block slab_notifier;
  189. #endif
  190. static enum {
  191. DOWN, /* No slab functionality available */
  192. PARTIAL, /* kmem_cache_open() works but kmalloc does not */
  193. UP, /* Everything works but does not show up in sysfs */
  194. SYSFS /* Sysfs up */
  195. } slab_state = DOWN;
  196. /* A list of all slab caches on the system */
  197. static DECLARE_RWSEM(slub_lock);
  198. static LIST_HEAD(slab_caches);
  199. /*
  200. * Tracking user of a slab.
  201. */
  202. struct track {
  203. void *addr; /* Called from address */
  204. int cpu; /* Was running on cpu */
  205. int pid; /* Pid context */
  206. unsigned long when; /* When did the operation occur */
  207. };
  208. enum track_item { TRACK_ALLOC, TRACK_FREE };
  209. #if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
  210. static int sysfs_slab_add(struct kmem_cache *);
  211. static int sysfs_slab_alias(struct kmem_cache *, const char *);
  212. static void sysfs_slab_remove(struct kmem_cache *);
  213. #else
  214. static inline int sysfs_slab_add(struct kmem_cache *s) { return 0; }
  215. static inline int sysfs_slab_alias(struct kmem_cache *s, const char *p)
  216. { return 0; }
  217. static inline void sysfs_slab_remove(struct kmem_cache *s)
  218. {
  219. kfree(s);
  220. }
  221. #endif
  222. /********************************************************************
  223. * Core slab cache functions
  224. *******************************************************************/
  225. int slab_is_available(void)
  226. {
  227. return slab_state >= UP;
  228. }
  229. static inline struct kmem_cache_node *get_node(struct kmem_cache *s, int node)
  230. {
  231. #ifdef CONFIG_NUMA
  232. return s->node[node];
  233. #else
  234. return &s->local_node;
  235. #endif
  236. }
  237. static inline struct kmem_cache_cpu *get_cpu_slab(struct kmem_cache *s, int cpu)
  238. {
  239. #ifdef CONFIG_SMP
  240. return s->cpu_slab[cpu];
  241. #else
  242. return &s->cpu_slab;
  243. #endif
  244. }
  245. /*
  246. * The end pointer in a slab is special. It points to the first object in the
  247. * slab but has bit 0 set to mark it.
  248. *
  249. * Note that SLUB relies on page_mapping returning NULL for pages with bit 0
  250. * in the mapping set.
  251. */
  252. static inline int is_end(void *addr)
  253. {
  254. return (unsigned long)addr & PAGE_MAPPING_ANON;
  255. }
  256. void *slab_address(struct page *page)
  257. {
  258. return page->end - PAGE_MAPPING_ANON;
  259. }
  260. static inline int check_valid_pointer(struct kmem_cache *s,
  261. struct page *page, const void *object)
  262. {
  263. void *base;
  264. if (object == page->end)
  265. return 1;
  266. base = slab_address(page);
  267. if (object < base || object >= base + s->objects * s->size ||
  268. (object - base) % s->size) {
  269. return 0;
  270. }
  271. return 1;
  272. }
  273. /*
  274. * Slow version of get and set free pointer.
  275. *
  276. * This version requires touching the cache lines of kmem_cache which
  277. * we avoid to do in the fast alloc free paths. There we obtain the offset
  278. * from the page struct.
  279. */
  280. static inline void *get_freepointer(struct kmem_cache *s, void *object)
  281. {
  282. return *(void **)(object + s->offset);
  283. }
  284. static inline void set_freepointer(struct kmem_cache *s, void *object, void *fp)
  285. {
  286. *(void **)(object + s->offset) = fp;
  287. }
  288. /* Loop over all objects in a slab */
  289. #define for_each_object(__p, __s, __addr) \
  290. for (__p = (__addr); __p < (__addr) + (__s)->objects * (__s)->size;\
  291. __p += (__s)->size)
  292. /* Scan freelist */
  293. #define for_each_free_object(__p, __s, __free) \
  294. for (__p = (__free); (__p) != page->end; __p = get_freepointer((__s),\
  295. __p))
  296. /* Determine object index from a given position */
  297. static inline int slab_index(void *p, struct kmem_cache *s, void *addr)
  298. {
  299. return (p - addr) / s->size;
  300. }
  301. #ifdef CONFIG_SLUB_DEBUG
  302. /*
  303. * Debug settings:
  304. */
  305. #ifdef CONFIG_SLUB_DEBUG_ON
  306. static int slub_debug = DEBUG_DEFAULT_FLAGS;
  307. #else
  308. static int slub_debug;
  309. #endif
  310. static char *slub_debug_slabs;
  311. /*
  312. * Object debugging
  313. */
  314. static void print_section(char *text, u8 *addr, unsigned int length)
  315. {
  316. int i, offset;
  317. int newline = 1;
  318. char ascii[17];
  319. ascii[16] = 0;
  320. for (i = 0; i < length; i++) {
  321. if (newline) {
  322. printk(KERN_ERR "%8s 0x%p: ", text, addr + i);
  323. newline = 0;
  324. }
  325. printk(KERN_CONT " %02x", addr[i]);
  326. offset = i % 16;
  327. ascii[offset] = isgraph(addr[i]) ? addr[i] : '.';
  328. if (offset == 15) {
  329. printk(KERN_CONT " %s\n", ascii);
  330. newline = 1;
  331. }
  332. }
  333. if (!newline) {
  334. i %= 16;
  335. while (i < 16) {
  336. printk(KERN_CONT " ");
  337. ascii[i] = ' ';
  338. i++;
  339. }
  340. printk(KERN_CONT " %s\n", ascii);
  341. }
  342. }
  343. static struct track *get_track(struct kmem_cache *s, void *object,
  344. enum track_item alloc)
  345. {
  346. struct track *p;
  347. if (s->offset)
  348. p = object + s->offset + sizeof(void *);
  349. else
  350. p = object + s->inuse;
  351. return p + alloc;
  352. }
  353. static void set_track(struct kmem_cache *s, void *object,
  354. enum track_item alloc, void *addr)
  355. {
  356. struct track *p;
  357. if (s->offset)
  358. p = object + s->offset + sizeof(void *);
  359. else
  360. p = object + s->inuse;
  361. p += alloc;
  362. if (addr) {
  363. p->addr = addr;
  364. p->cpu = smp_processor_id();
  365. p->pid = current ? current->pid : -1;
  366. p->when = jiffies;
  367. } else
  368. memset(p, 0, sizeof(struct track));
  369. }
  370. static void init_tracking(struct kmem_cache *s, void *object)
  371. {
  372. if (!(s->flags & SLAB_STORE_USER))
  373. return;
  374. set_track(s, object, TRACK_FREE, NULL);
  375. set_track(s, object, TRACK_ALLOC, NULL);
  376. }
  377. static void print_track(const char *s, struct track *t)
  378. {
  379. if (!t->addr)
  380. return;
  381. printk(KERN_ERR "INFO: %s in ", s);
  382. __print_symbol("%s", (unsigned long)t->addr);
  383. printk(" age=%lu cpu=%u pid=%d\n", jiffies - t->when, t->cpu, t->pid);
  384. }
  385. static void print_tracking(struct kmem_cache *s, void *object)
  386. {
  387. if (!(s->flags & SLAB_STORE_USER))
  388. return;
  389. print_track("Allocated", get_track(s, object, TRACK_ALLOC));
  390. print_track("Freed", get_track(s, object, TRACK_FREE));
  391. }
  392. static void print_page_info(struct page *page)
  393. {
  394. printk(KERN_ERR "INFO: Slab 0x%p used=%u fp=0x%p flags=0x%04lx\n",
  395. page, page->inuse, page->freelist, page->flags);
  396. }
  397. static void slab_bug(struct kmem_cache *s, char *fmt, ...)
  398. {
  399. va_list args;
  400. char buf[100];
  401. va_start(args, fmt);
  402. vsnprintf(buf, sizeof(buf), fmt, args);
  403. va_end(args);
  404. printk(KERN_ERR "========================================"
  405. "=====================================\n");
  406. printk(KERN_ERR "BUG %s: %s\n", s->name, buf);
  407. printk(KERN_ERR "----------------------------------------"
  408. "-------------------------------------\n\n");
  409. }
  410. static void slab_fix(struct kmem_cache *s, char *fmt, ...)
  411. {
  412. va_list args;
  413. char buf[100];
  414. va_start(args, fmt);
  415. vsnprintf(buf, sizeof(buf), fmt, args);
  416. va_end(args);
  417. printk(KERN_ERR "FIX %s: %s\n", s->name, buf);
  418. }
  419. static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p)
  420. {
  421. unsigned int off; /* Offset of last byte */
  422. u8 *addr = slab_address(page);
  423. print_tracking(s, p);
  424. print_page_info(page);
  425. printk(KERN_ERR "INFO: Object 0x%p @offset=%tu fp=0x%p\n\n",
  426. p, p - addr, get_freepointer(s, p));
  427. if (p > addr + 16)
  428. print_section("Bytes b4", p - 16, 16);
  429. print_section("Object", p, min(s->objsize, 128));
  430. if (s->flags & SLAB_RED_ZONE)
  431. print_section("Redzone", p + s->objsize,
  432. s->inuse - s->objsize);
  433. if (s->offset)
  434. off = s->offset + sizeof(void *);
  435. else
  436. off = s->inuse;
  437. if (s->flags & SLAB_STORE_USER)
  438. off += 2 * sizeof(struct track);
  439. if (off != s->size)
  440. /* Beginning of the filler is the free pointer */
  441. print_section("Padding", p + off, s->size - off);
  442. dump_stack();
  443. }
  444. static void object_err(struct kmem_cache *s, struct page *page,
  445. u8 *object, char *reason)
  446. {
  447. slab_bug(s, reason);
  448. print_trailer(s, page, object);
  449. }
  450. static void slab_err(struct kmem_cache *s, struct page *page, char *fmt, ...)
  451. {
  452. va_list args;
  453. char buf[100];
  454. va_start(args, fmt);
  455. vsnprintf(buf, sizeof(buf), fmt, args);
  456. va_end(args);
  457. slab_bug(s, fmt);
  458. print_page_info(page);
  459. dump_stack();
  460. }
  461. static void init_object(struct kmem_cache *s, void *object, int active)
  462. {
  463. u8 *p = object;
  464. if (s->flags & __OBJECT_POISON) {
  465. memset(p, POISON_FREE, s->objsize - 1);
  466. p[s->objsize - 1] = POISON_END;
  467. }
  468. if (s->flags & SLAB_RED_ZONE)
  469. memset(p + s->objsize,
  470. active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE,
  471. s->inuse - s->objsize);
  472. }
  473. static u8 *check_bytes(u8 *start, unsigned int value, unsigned int bytes)
  474. {
  475. while (bytes) {
  476. if (*start != (u8)value)
  477. return start;
  478. start++;
  479. bytes--;
  480. }
  481. return NULL;
  482. }
  483. static void restore_bytes(struct kmem_cache *s, char *message, u8 data,
  484. void *from, void *to)
  485. {
  486. slab_fix(s, "Restoring 0x%p-0x%p=0x%x\n", from, to - 1, data);
  487. memset(from, data, to - from);
  488. }
  489. static int check_bytes_and_report(struct kmem_cache *s, struct page *page,
  490. u8 *object, char *what,
  491. u8 *start, unsigned int value, unsigned int bytes)
  492. {
  493. u8 *fault;
  494. u8 *end;
  495. fault = check_bytes(start, value, bytes);
  496. if (!fault)
  497. return 1;
  498. end = start + bytes;
  499. while (end > fault && end[-1] == value)
  500. end--;
  501. slab_bug(s, "%s overwritten", what);
  502. printk(KERN_ERR "INFO: 0x%p-0x%p. First byte 0x%x instead of 0x%x\n",
  503. fault, end - 1, fault[0], value);
  504. print_trailer(s, page, object);
  505. restore_bytes(s, what, value, fault, end);
  506. return 0;
  507. }
  508. /*
  509. * Object layout:
  510. *
  511. * object address
  512. * Bytes of the object to be managed.
  513. * If the freepointer may overlay the object then the free
  514. * pointer is the first word of the object.
  515. *
  516. * Poisoning uses 0x6b (POISON_FREE) and the last byte is
  517. * 0xa5 (POISON_END)
  518. *
  519. * object + s->objsize
  520. * Padding to reach word boundary. This is also used for Redzoning.
  521. * Padding is extended by another word if Redzoning is enabled and
  522. * objsize == inuse.
  523. *
  524. * We fill with 0xbb (RED_INACTIVE) for inactive objects and with
  525. * 0xcc (RED_ACTIVE) for objects in use.
  526. *
  527. * object + s->inuse
  528. * Meta data starts here.
  529. *
  530. * A. Free pointer (if we cannot overwrite object on free)
  531. * B. Tracking data for SLAB_STORE_USER
  532. * C. Padding to reach required alignment boundary or at mininum
  533. * one word if debuggin is on to be able to detect writes
  534. * before the word boundary.
  535. *
  536. * Padding is done using 0x5a (POISON_INUSE)
  537. *
  538. * object + s->size
  539. * Nothing is used beyond s->size.
  540. *
  541. * If slabcaches are merged then the objsize and inuse boundaries are mostly
  542. * ignored. And therefore no slab options that rely on these boundaries
  543. * may be used with merged slabcaches.
  544. */
  545. static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p)
  546. {
  547. unsigned long off = s->inuse; /* The end of info */
  548. if (s->offset)
  549. /* Freepointer is placed after the object. */
  550. off += sizeof(void *);
  551. if (s->flags & SLAB_STORE_USER)
  552. /* We also have user information there */
  553. off += 2 * sizeof(struct track);
  554. if (s->size == off)
  555. return 1;
  556. return check_bytes_and_report(s, page, p, "Object padding",
  557. p + off, POISON_INUSE, s->size - off);
  558. }
  559. static int slab_pad_check(struct kmem_cache *s, struct page *page)
  560. {
  561. u8 *start;
  562. u8 *fault;
  563. u8 *end;
  564. int length;
  565. int remainder;
  566. if (!(s->flags & SLAB_POISON))
  567. return 1;
  568. start = slab_address(page);
  569. end = start + (PAGE_SIZE << s->order);
  570. length = s->objects * s->size;
  571. remainder = end - (start + length);
  572. if (!remainder)
  573. return 1;
  574. fault = check_bytes(start + length, POISON_INUSE, remainder);
  575. if (!fault)
  576. return 1;
  577. while (end > fault && end[-1] == POISON_INUSE)
  578. end--;
  579. slab_err(s, page, "Padding overwritten. 0x%p-0x%p", fault, end - 1);
  580. print_section("Padding", start, length);
  581. restore_bytes(s, "slab padding", POISON_INUSE, start, end);
  582. return 0;
  583. }
  584. static int check_object(struct kmem_cache *s, struct page *page,
  585. void *object, int active)
  586. {
  587. u8 *p = object;
  588. u8 *endobject = object + s->objsize;
  589. if (s->flags & SLAB_RED_ZONE) {
  590. unsigned int red =
  591. active ? SLUB_RED_ACTIVE : SLUB_RED_INACTIVE;
  592. if (!check_bytes_and_report(s, page, object, "Redzone",
  593. endobject, red, s->inuse - s->objsize))
  594. return 0;
  595. } else {
  596. if ((s->flags & SLAB_POISON) && s->objsize < s->inuse)
  597. check_bytes_and_report(s, page, p, "Alignment padding", endobject,
  598. POISON_INUSE, s->inuse - s->objsize);
  599. }
  600. if (s->flags & SLAB_POISON) {
  601. if (!active && (s->flags & __OBJECT_POISON) &&
  602. (!check_bytes_and_report(s, page, p, "Poison", p,
  603. POISON_FREE, s->objsize - 1) ||
  604. !check_bytes_and_report(s, page, p, "Poison",
  605. p + s->objsize - 1, POISON_END, 1)))
  606. return 0;
  607. /*
  608. * check_pad_bytes cleans up on its own.
  609. */
  610. check_pad_bytes(s, page, p);
  611. }
  612. if (!s->offset && active)
  613. /*
  614. * Object and freepointer overlap. Cannot check
  615. * freepointer while object is allocated.
  616. */
  617. return 1;
  618. /* Check free pointer validity */
  619. if (!check_valid_pointer(s, page, get_freepointer(s, p))) {
  620. object_err(s, page, p, "Freepointer corrupt");
  621. /*
  622. * No choice but to zap it and thus loose the remainder
  623. * of the free objects in this slab. May cause
  624. * another error because the object count is now wrong.
  625. */
  626. set_freepointer(s, p, page->end);
  627. return 0;
  628. }
  629. return 1;
  630. }
  631. static int check_slab(struct kmem_cache *s, struct page *page)
  632. {
  633. VM_BUG_ON(!irqs_disabled());
  634. if (!PageSlab(page)) {
  635. slab_err(s, page, "Not a valid slab page");
  636. return 0;
  637. }
  638. if (page->inuse > s->objects) {
  639. slab_err(s, page, "inuse %u > max %u",
  640. s->name, page->inuse, s->objects);
  641. return 0;
  642. }
  643. /* Slab_pad_check fixes things up after itself */
  644. slab_pad_check(s, page);
  645. return 1;
  646. }
  647. /*
  648. * Determine if a certain object on a page is on the freelist. Must hold the
  649. * slab lock to guarantee that the chains are in a consistent state.
  650. */
  651. static int on_freelist(struct kmem_cache *s, struct page *page, void *search)
  652. {
  653. int nr = 0;
  654. void *fp = page->freelist;
  655. void *object = NULL;
  656. while (fp != page->end && nr <= s->objects) {
  657. if (fp == search)
  658. return 1;
  659. if (!check_valid_pointer(s, page, fp)) {
  660. if (object) {
  661. object_err(s, page, object,
  662. "Freechain corrupt");
  663. set_freepointer(s, object, page->end);
  664. break;
  665. } else {
  666. slab_err(s, page, "Freepointer corrupt");
  667. page->freelist = page->end;
  668. page->inuse = s->objects;
  669. slab_fix(s, "Freelist cleared");
  670. return 0;
  671. }
  672. break;
  673. }
  674. object = fp;
  675. fp = get_freepointer(s, object);
  676. nr++;
  677. }
  678. if (page->inuse != s->objects - nr) {
  679. slab_err(s, page, "Wrong object count. Counter is %d but "
  680. "counted were %d", page->inuse, s->objects - nr);
  681. page->inuse = s->objects - nr;
  682. slab_fix(s, "Object count adjusted.");
  683. }
  684. return search == NULL;
  685. }
  686. static void trace(struct kmem_cache *s, struct page *page, void *object, int alloc)
  687. {
  688. if (s->flags & SLAB_TRACE) {
  689. printk(KERN_INFO "TRACE %s %s 0x%p inuse=%d fp=0x%p\n",
  690. s->name,
  691. alloc ? "alloc" : "free",
  692. object, page->inuse,
  693. page->freelist);
  694. if (!alloc)
  695. print_section("Object", (void *)object, s->objsize);
  696. dump_stack();
  697. }
  698. }
  699. /*
  700. * Tracking of fully allocated slabs for debugging purposes.
  701. */
  702. static void add_full(struct kmem_cache_node *n, struct page *page)
  703. {
  704. spin_lock(&n->list_lock);
  705. list_add(&page->lru, &n->full);
  706. spin_unlock(&n->list_lock);
  707. }
  708. static void remove_full(struct kmem_cache *s, struct page *page)
  709. {
  710. struct kmem_cache_node *n;
  711. if (!(s->flags & SLAB_STORE_USER))
  712. return;
  713. n = get_node(s, page_to_nid(page));
  714. spin_lock(&n->list_lock);
  715. list_del(&page->lru);
  716. spin_unlock(&n->list_lock);
  717. }
  718. static void setup_object_debug(struct kmem_cache *s, struct page *page,
  719. void *object)
  720. {
  721. if (!(s->flags & (SLAB_STORE_USER|SLAB_RED_ZONE|__OBJECT_POISON)))
  722. return;
  723. init_object(s, object, 0);
  724. init_tracking(s, object);
  725. }
  726. static int alloc_debug_processing(struct kmem_cache *s, struct page *page,
  727. void *object, void *addr)
  728. {
  729. if (!check_slab(s, page))
  730. goto bad;
  731. if (object && !on_freelist(s, page, object)) {
  732. object_err(s, page, object, "Object already allocated");
  733. goto bad;
  734. }
  735. if (!check_valid_pointer(s, page, object)) {
  736. object_err(s, page, object, "Freelist Pointer check fails");
  737. goto bad;
  738. }
  739. if (object && !check_object(s, page, object, 0))
  740. goto bad;
  741. /* Success perform special debug activities for allocs */
  742. if (s->flags & SLAB_STORE_USER)
  743. set_track(s, object, TRACK_ALLOC, addr);
  744. trace(s, page, object, 1);
  745. init_object(s, object, 1);
  746. return 1;
  747. bad:
  748. if (PageSlab(page)) {
  749. /*
  750. * If this is a slab page then lets do the best we can
  751. * to avoid issues in the future. Marking all objects
  752. * as used avoids touching the remaining objects.
  753. */
  754. slab_fix(s, "Marking all objects used");
  755. page->inuse = s->objects;
  756. page->freelist = page->end;
  757. }
  758. return 0;
  759. }
  760. static int free_debug_processing(struct kmem_cache *s, struct page *page,
  761. void *object, void *addr)
  762. {
  763. if (!check_slab(s, page))
  764. goto fail;
  765. if (!check_valid_pointer(s, page, object)) {
  766. slab_err(s, page, "Invalid object pointer 0x%p", object);
  767. goto fail;
  768. }
  769. if (on_freelist(s, page, object)) {
  770. object_err(s, page, object, "Object already free");
  771. goto fail;
  772. }
  773. if (!check_object(s, page, object, 1))
  774. return 0;
  775. if (unlikely(s != page->slab)) {
  776. if (!PageSlab(page))
  777. slab_err(s, page, "Attempt to free object(0x%p) "
  778. "outside of slab", object);
  779. else
  780. if (!page->slab) {
  781. printk(KERN_ERR
  782. "SLUB <none>: no slab for object 0x%p.\n",
  783. object);
  784. dump_stack();
  785. } else
  786. object_err(s, page, object,
  787. "page slab pointer corrupt.");
  788. goto fail;
  789. }
  790. /* Special debug activities for freeing objects */
  791. if (!SlabFrozen(page) && page->freelist == page->end)
  792. remove_full(s, page);
  793. if (s->flags & SLAB_STORE_USER)
  794. set_track(s, object, TRACK_FREE, addr);
  795. trace(s, page, object, 0);
  796. init_object(s, object, 0);
  797. return 1;
  798. fail:
  799. slab_fix(s, "Object at 0x%p not freed", object);
  800. return 0;
  801. }
  802. static int __init setup_slub_debug(char *str)
  803. {
  804. slub_debug = DEBUG_DEFAULT_FLAGS;
  805. if (*str++ != '=' || !*str)
  806. /*
  807. * No options specified. Switch on full debugging.
  808. */
  809. goto out;
  810. if (*str == ',')
  811. /*
  812. * No options but restriction on slabs. This means full
  813. * debugging for slabs matching a pattern.
  814. */
  815. goto check_slabs;
  816. slub_debug = 0;
  817. if (*str == '-')
  818. /*
  819. * Switch off all debugging measures.
  820. */
  821. goto out;
  822. /*
  823. * Determine which debug features should be switched on
  824. */
  825. for (; *str && *str != ','; str++) {
  826. switch (tolower(*str)) {
  827. case 'f':
  828. slub_debug |= SLAB_DEBUG_FREE;
  829. break;
  830. case 'z':
  831. slub_debug |= SLAB_RED_ZONE;
  832. break;
  833. case 'p':
  834. slub_debug |= SLAB_POISON;
  835. break;
  836. case 'u':
  837. slub_debug |= SLAB_STORE_USER;
  838. break;
  839. case 't':
  840. slub_debug |= SLAB_TRACE;
  841. break;
  842. default:
  843. printk(KERN_ERR "slub_debug option '%c' "
  844. "unknown. skipped\n", *str);
  845. }
  846. }
  847. check_slabs:
  848. if (*str == ',')
  849. slub_debug_slabs = str + 1;
  850. out:
  851. return 1;
  852. }
  853. __setup("slub_debug", setup_slub_debug);
  854. static unsigned long kmem_cache_flags(unsigned long objsize,
  855. unsigned long flags, const char *name,
  856. void (*ctor)(struct kmem_cache *, void *))
  857. {
  858. /*
  859. * The page->offset field is only 16 bit wide. This is an offset
  860. * in units of words from the beginning of an object. If the slab
  861. * size is bigger then we cannot move the free pointer behind the
  862. * object anymore.
  863. *
  864. * On 32 bit platforms the limit is 256k. On 64bit platforms
  865. * the limit is 512k.
  866. *
  867. * Debugging or ctor may create a need to move the free
  868. * pointer. Fail if this happens.
  869. */
  870. if (objsize >= 65535 * sizeof(void *)) {
  871. BUG_ON(flags & (SLAB_RED_ZONE | SLAB_POISON |
  872. SLAB_STORE_USER | SLAB_DESTROY_BY_RCU));
  873. BUG_ON(ctor);
  874. } else {
  875. /*
  876. * Enable debugging if selected on the kernel commandline.
  877. */
  878. if (slub_debug && (!slub_debug_slabs ||
  879. strncmp(slub_debug_slabs, name,
  880. strlen(slub_debug_slabs)) == 0))
  881. flags |= slub_debug;
  882. }
  883. return flags;
  884. }
  885. #else
  886. static inline void setup_object_debug(struct kmem_cache *s,
  887. struct page *page, void *object) {}
  888. static inline int alloc_debug_processing(struct kmem_cache *s,
  889. struct page *page, void *object, void *addr) { return 0; }
  890. static inline int free_debug_processing(struct kmem_cache *s,
  891. struct page *page, void *object, void *addr) { return 0; }
  892. static inline int slab_pad_check(struct kmem_cache *s, struct page *page)
  893. { return 1; }
  894. static inline int check_object(struct kmem_cache *s, struct page *page,
  895. void *object, int active) { return 1; }
  896. static inline void add_full(struct kmem_cache_node *n, struct page *page) {}
  897. static inline unsigned long kmem_cache_flags(unsigned long objsize,
  898. unsigned long flags, const char *name,
  899. void (*ctor)(struct kmem_cache *, void *))
  900. {
  901. return flags;
  902. }
  903. #define slub_debug 0
  904. #endif
  905. /*
  906. * Slab allocation and freeing
  907. */
  908. static struct page *allocate_slab(struct kmem_cache *s, gfp_t flags, int node)
  909. {
  910. struct page *page;
  911. int pages = 1 << s->order;
  912. if (s->order)
  913. flags |= __GFP_COMP;
  914. if (s->flags & SLAB_CACHE_DMA)
  915. flags |= SLUB_DMA;
  916. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  917. flags |= __GFP_RECLAIMABLE;
  918. if (node == -1)
  919. page = alloc_pages(flags, s->order);
  920. else
  921. page = alloc_pages_node(node, flags, s->order);
  922. if (!page)
  923. return NULL;
  924. mod_zone_page_state(page_zone(page),
  925. (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  926. NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
  927. pages);
  928. return page;
  929. }
  930. static void setup_object(struct kmem_cache *s, struct page *page,
  931. void *object)
  932. {
  933. setup_object_debug(s, page, object);
  934. if (unlikely(s->ctor))
  935. s->ctor(s, object);
  936. }
  937. static struct page *new_slab(struct kmem_cache *s, gfp_t flags, int node)
  938. {
  939. struct page *page;
  940. struct kmem_cache_node *n;
  941. void *start;
  942. void *last;
  943. void *p;
  944. BUG_ON(flags & GFP_SLAB_BUG_MASK);
  945. page = allocate_slab(s,
  946. flags & (GFP_RECLAIM_MASK | GFP_CONSTRAINT_MASK), node);
  947. if (!page)
  948. goto out;
  949. n = get_node(s, page_to_nid(page));
  950. if (n)
  951. atomic_long_inc(&n->nr_slabs);
  952. page->slab = s;
  953. page->flags |= 1 << PG_slab;
  954. if (s->flags & (SLAB_DEBUG_FREE | SLAB_RED_ZONE | SLAB_POISON |
  955. SLAB_STORE_USER | SLAB_TRACE))
  956. SetSlabDebug(page);
  957. start = page_address(page);
  958. page->end = start + 1;
  959. if (unlikely(s->flags & SLAB_POISON))
  960. memset(start, POISON_INUSE, PAGE_SIZE << s->order);
  961. last = start;
  962. for_each_object(p, s, start) {
  963. setup_object(s, page, last);
  964. set_freepointer(s, last, p);
  965. last = p;
  966. }
  967. setup_object(s, page, last);
  968. set_freepointer(s, last, page->end);
  969. page->freelist = start;
  970. page->inuse = 0;
  971. out:
  972. return page;
  973. }
  974. static void __free_slab(struct kmem_cache *s, struct page *page)
  975. {
  976. int pages = 1 << s->order;
  977. if (unlikely(SlabDebug(page))) {
  978. void *p;
  979. slab_pad_check(s, page);
  980. for_each_object(p, s, slab_address(page))
  981. check_object(s, page, p, 0);
  982. ClearSlabDebug(page);
  983. }
  984. mod_zone_page_state(page_zone(page),
  985. (s->flags & SLAB_RECLAIM_ACCOUNT) ?
  986. NR_SLAB_RECLAIMABLE : NR_SLAB_UNRECLAIMABLE,
  987. -pages);
  988. page->mapping = NULL;
  989. __free_pages(page, s->order);
  990. }
  991. static void rcu_free_slab(struct rcu_head *h)
  992. {
  993. struct page *page;
  994. page = container_of((struct list_head *)h, struct page, lru);
  995. __free_slab(page->slab, page);
  996. }
  997. static void free_slab(struct kmem_cache *s, struct page *page)
  998. {
  999. if (unlikely(s->flags & SLAB_DESTROY_BY_RCU)) {
  1000. /*
  1001. * RCU free overloads the RCU head over the LRU
  1002. */
  1003. struct rcu_head *head = (void *)&page->lru;
  1004. call_rcu(head, rcu_free_slab);
  1005. } else
  1006. __free_slab(s, page);
  1007. }
  1008. static void discard_slab(struct kmem_cache *s, struct page *page)
  1009. {
  1010. struct kmem_cache_node *n = get_node(s, page_to_nid(page));
  1011. atomic_long_dec(&n->nr_slabs);
  1012. reset_page_mapcount(page);
  1013. __ClearPageSlab(page);
  1014. free_slab(s, page);
  1015. }
  1016. /*
  1017. * Per slab locking using the pagelock
  1018. */
  1019. static __always_inline void slab_lock(struct page *page)
  1020. {
  1021. bit_spin_lock(PG_locked, &page->flags);
  1022. }
  1023. static __always_inline void slab_unlock(struct page *page)
  1024. {
  1025. bit_spin_unlock(PG_locked, &page->flags);
  1026. }
  1027. static __always_inline int slab_trylock(struct page *page)
  1028. {
  1029. int rc = 1;
  1030. rc = bit_spin_trylock(PG_locked, &page->flags);
  1031. return rc;
  1032. }
  1033. /*
  1034. * Management of partially allocated slabs
  1035. */
  1036. static void add_partial(struct kmem_cache_node *n,
  1037. struct page *page, int tail)
  1038. {
  1039. spin_lock(&n->list_lock);
  1040. n->nr_partial++;
  1041. if (tail)
  1042. list_add_tail(&page->lru, &n->partial);
  1043. else
  1044. list_add(&page->lru, &n->partial);
  1045. spin_unlock(&n->list_lock);
  1046. }
  1047. static void remove_partial(struct kmem_cache *s,
  1048. struct page *page)
  1049. {
  1050. struct kmem_cache_node *n = get_node(s, page_to_nid(page));
  1051. spin_lock(&n->list_lock);
  1052. list_del(&page->lru);
  1053. n->nr_partial--;
  1054. spin_unlock(&n->list_lock);
  1055. }
  1056. /*
  1057. * Lock slab and remove from the partial list.
  1058. *
  1059. * Must hold list_lock.
  1060. */
  1061. static inline int lock_and_freeze_slab(struct kmem_cache_node *n, struct page *page)
  1062. {
  1063. if (slab_trylock(page)) {
  1064. list_del(&page->lru);
  1065. n->nr_partial--;
  1066. SetSlabFrozen(page);
  1067. return 1;
  1068. }
  1069. return 0;
  1070. }
  1071. /*
  1072. * Try to allocate a partial slab from a specific node.
  1073. */
  1074. static struct page *get_partial_node(struct kmem_cache_node *n)
  1075. {
  1076. struct page *page;
  1077. /*
  1078. * Racy check. If we mistakenly see no partial slabs then we
  1079. * just allocate an empty slab. If we mistakenly try to get a
  1080. * partial slab and there is none available then get_partials()
  1081. * will return NULL.
  1082. */
  1083. if (!n || !n->nr_partial)
  1084. return NULL;
  1085. spin_lock(&n->list_lock);
  1086. list_for_each_entry(page, &n->partial, lru)
  1087. if (lock_and_freeze_slab(n, page))
  1088. goto out;
  1089. page = NULL;
  1090. out:
  1091. spin_unlock(&n->list_lock);
  1092. return page;
  1093. }
  1094. /*
  1095. * Get a page from somewhere. Search in increasing NUMA distances.
  1096. */
  1097. static struct page *get_any_partial(struct kmem_cache *s, gfp_t flags)
  1098. {
  1099. #ifdef CONFIG_NUMA
  1100. struct zonelist *zonelist;
  1101. struct zone **z;
  1102. struct page *page;
  1103. /*
  1104. * The defrag ratio allows a configuration of the tradeoffs between
  1105. * inter node defragmentation and node local allocations. A lower
  1106. * defrag_ratio increases the tendency to do local allocations
  1107. * instead of attempting to obtain partial slabs from other nodes.
  1108. *
  1109. * If the defrag_ratio is set to 0 then kmalloc() always
  1110. * returns node local objects. If the ratio is higher then kmalloc()
  1111. * may return off node objects because partial slabs are obtained
  1112. * from other nodes and filled up.
  1113. *
  1114. * If /sys/slab/xx/defrag_ratio is set to 100 (which makes
  1115. * defrag_ratio = 1000) then every (well almost) allocation will
  1116. * first attempt to defrag slab caches on other nodes. This means
  1117. * scanning over all nodes to look for partial slabs which may be
  1118. * expensive if we do it every time we are trying to find a slab
  1119. * with available objects.
  1120. */
  1121. if (!s->remote_node_defrag_ratio ||
  1122. get_cycles() % 1024 > s->remote_node_defrag_ratio)
  1123. return NULL;
  1124. zonelist = &NODE_DATA(slab_node(current->mempolicy))
  1125. ->node_zonelists[gfp_zone(flags)];
  1126. for (z = zonelist->zones; *z; z++) {
  1127. struct kmem_cache_node *n;
  1128. n = get_node(s, zone_to_nid(*z));
  1129. if (n && cpuset_zone_allowed_hardwall(*z, flags) &&
  1130. n->nr_partial > MIN_PARTIAL) {
  1131. page = get_partial_node(n);
  1132. if (page)
  1133. return page;
  1134. }
  1135. }
  1136. #endif
  1137. return NULL;
  1138. }
  1139. /*
  1140. * Get a partial page, lock it and return it.
  1141. */
  1142. static struct page *get_partial(struct kmem_cache *s, gfp_t flags, int node)
  1143. {
  1144. struct page *page;
  1145. int searchnode = (node == -1) ? numa_node_id() : node;
  1146. page = get_partial_node(get_node(s, searchnode));
  1147. if (page || (flags & __GFP_THISNODE))
  1148. return page;
  1149. return get_any_partial(s, flags);
  1150. }
  1151. /*
  1152. * Move a page back to the lists.
  1153. *
  1154. * Must be called with the slab lock held.
  1155. *
  1156. * On exit the slab lock will have been dropped.
  1157. */
  1158. static void unfreeze_slab(struct kmem_cache *s, struct page *page, int tail)
  1159. {
  1160. struct kmem_cache_node *n = get_node(s, page_to_nid(page));
  1161. ClearSlabFrozen(page);
  1162. if (page->inuse) {
  1163. if (page->freelist != page->end)
  1164. add_partial(n, page, tail);
  1165. else if (SlabDebug(page) && (s->flags & SLAB_STORE_USER))
  1166. add_full(n, page);
  1167. slab_unlock(page);
  1168. } else {
  1169. if (n->nr_partial < MIN_PARTIAL) {
  1170. /*
  1171. * Adding an empty slab to the partial slabs in order
  1172. * to avoid page allocator overhead. This slab needs
  1173. * to come after the other slabs with objects in
  1174. * order to fill them up. That way the size of the
  1175. * partial list stays small. kmem_cache_shrink can
  1176. * reclaim empty slabs from the partial list.
  1177. */
  1178. add_partial(n, page, 1);
  1179. slab_unlock(page);
  1180. } else {
  1181. slab_unlock(page);
  1182. discard_slab(s, page);
  1183. }
  1184. }
  1185. }
  1186. /*
  1187. * Remove the cpu slab
  1188. */
  1189. static void deactivate_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
  1190. {
  1191. struct page *page = c->page;
  1192. int tail = 1;
  1193. /*
  1194. * Merge cpu freelist into freelist. Typically we get here
  1195. * because both freelists are empty. So this is unlikely
  1196. * to occur.
  1197. *
  1198. * We need to use _is_end here because deactivate slab may
  1199. * be called for a debug slab. Then c->freelist may contain
  1200. * a dummy pointer.
  1201. */
  1202. while (unlikely(!is_end(c->freelist))) {
  1203. void **object;
  1204. tail = 0; /* Hot objects. Put the slab first */
  1205. /* Retrieve object from cpu_freelist */
  1206. object = c->freelist;
  1207. c->freelist = c->freelist[c->offset];
  1208. /* And put onto the regular freelist */
  1209. object[c->offset] = page->freelist;
  1210. page->freelist = object;
  1211. page->inuse--;
  1212. }
  1213. c->page = NULL;
  1214. unfreeze_slab(s, page, tail);
  1215. }
  1216. static inline void flush_slab(struct kmem_cache *s, struct kmem_cache_cpu *c)
  1217. {
  1218. slab_lock(c->page);
  1219. deactivate_slab(s, c);
  1220. }
  1221. /*
  1222. * Flush cpu slab.
  1223. * Called from IPI handler with interrupts disabled.
  1224. */
  1225. static inline void __flush_cpu_slab(struct kmem_cache *s, int cpu)
  1226. {
  1227. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  1228. if (likely(c && c->page))
  1229. flush_slab(s, c);
  1230. }
  1231. static void flush_cpu_slab(void *d)
  1232. {
  1233. struct kmem_cache *s = d;
  1234. __flush_cpu_slab(s, smp_processor_id());
  1235. }
  1236. static void flush_all(struct kmem_cache *s)
  1237. {
  1238. #ifdef CONFIG_SMP
  1239. on_each_cpu(flush_cpu_slab, s, 1, 1);
  1240. #else
  1241. unsigned long flags;
  1242. local_irq_save(flags);
  1243. flush_cpu_slab(s);
  1244. local_irq_restore(flags);
  1245. #endif
  1246. }
  1247. /*
  1248. * Check if the objects in a per cpu structure fit numa
  1249. * locality expectations.
  1250. */
  1251. static inline int node_match(struct kmem_cache_cpu *c, int node)
  1252. {
  1253. #ifdef CONFIG_NUMA
  1254. if (node != -1 && c->node != node)
  1255. return 0;
  1256. #endif
  1257. return 1;
  1258. }
  1259. /*
  1260. * Slow path. The lockless freelist is empty or we need to perform
  1261. * debugging duties.
  1262. *
  1263. * Interrupts are disabled.
  1264. *
  1265. * Processing is still very fast if new objects have been freed to the
  1266. * regular freelist. In that case we simply take over the regular freelist
  1267. * as the lockless freelist and zap the regular freelist.
  1268. *
  1269. * If that is not working then we fall back to the partial lists. We take the
  1270. * first element of the freelist as the object to allocate now and move the
  1271. * rest of the freelist to the lockless freelist.
  1272. *
  1273. * And if we were unable to get a new slab from the partial slab lists then
  1274. * we need to allocate a new slab. This is slowest path since we may sleep.
  1275. */
  1276. static void *__slab_alloc(struct kmem_cache *s,
  1277. gfp_t gfpflags, int node, void *addr, struct kmem_cache_cpu *c)
  1278. {
  1279. void **object;
  1280. struct page *new;
  1281. if (!c->page)
  1282. goto new_slab;
  1283. slab_lock(c->page);
  1284. if (unlikely(!node_match(c, node)))
  1285. goto another_slab;
  1286. load_freelist:
  1287. object = c->page->freelist;
  1288. if (unlikely(object == c->page->end))
  1289. goto another_slab;
  1290. if (unlikely(SlabDebug(c->page)))
  1291. goto debug;
  1292. object = c->page->freelist;
  1293. c->freelist = object[c->offset];
  1294. c->page->inuse = s->objects;
  1295. c->page->freelist = c->page->end;
  1296. c->node = page_to_nid(c->page);
  1297. slab_unlock(c->page);
  1298. return object;
  1299. another_slab:
  1300. deactivate_slab(s, c);
  1301. new_slab:
  1302. new = get_partial(s, gfpflags, node);
  1303. if (new) {
  1304. c->page = new;
  1305. goto load_freelist;
  1306. }
  1307. if (gfpflags & __GFP_WAIT)
  1308. local_irq_enable();
  1309. new = new_slab(s, gfpflags, node);
  1310. if (gfpflags & __GFP_WAIT)
  1311. local_irq_disable();
  1312. if (new) {
  1313. c = get_cpu_slab(s, smp_processor_id());
  1314. if (c->page)
  1315. flush_slab(s, c);
  1316. slab_lock(new);
  1317. SetSlabFrozen(new);
  1318. c->page = new;
  1319. goto load_freelist;
  1320. }
  1321. return NULL;
  1322. debug:
  1323. object = c->page->freelist;
  1324. if (!alloc_debug_processing(s, c->page, object, addr))
  1325. goto another_slab;
  1326. c->page->inuse++;
  1327. c->page->freelist = object[c->offset];
  1328. c->node = -1;
  1329. slab_unlock(c->page);
  1330. return object;
  1331. }
  1332. /*
  1333. * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
  1334. * have the fastpath folded into their functions. So no function call
  1335. * overhead for requests that can be satisfied on the fastpath.
  1336. *
  1337. * The fastpath works by first checking if the lockless freelist can be used.
  1338. * If not then __slab_alloc is called for slow processing.
  1339. *
  1340. * Otherwise we can simply pick the next object from the lockless free list.
  1341. */
  1342. static __always_inline void *slab_alloc(struct kmem_cache *s,
  1343. gfp_t gfpflags, int node, void *addr)
  1344. {
  1345. void **object;
  1346. unsigned long flags;
  1347. struct kmem_cache_cpu *c;
  1348. local_irq_save(flags);
  1349. c = get_cpu_slab(s, smp_processor_id());
  1350. if (unlikely(is_end(c->freelist) || !node_match(c, node)))
  1351. object = __slab_alloc(s, gfpflags, node, addr, c);
  1352. else {
  1353. object = c->freelist;
  1354. c->freelist = object[c->offset];
  1355. }
  1356. local_irq_restore(flags);
  1357. if (unlikely((gfpflags & __GFP_ZERO) && object))
  1358. memset(object, 0, c->objsize);
  1359. return object;
  1360. }
  1361. void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
  1362. {
  1363. return slab_alloc(s, gfpflags, -1, __builtin_return_address(0));
  1364. }
  1365. EXPORT_SYMBOL(kmem_cache_alloc);
  1366. #ifdef CONFIG_NUMA
  1367. void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
  1368. {
  1369. return slab_alloc(s, gfpflags, node, __builtin_return_address(0));
  1370. }
  1371. EXPORT_SYMBOL(kmem_cache_alloc_node);
  1372. #endif
  1373. /*
  1374. * Slow patch handling. This may still be called frequently since objects
  1375. * have a longer lifetime than the cpu slabs in most processing loads.
  1376. *
  1377. * So we still attempt to reduce cache line usage. Just take the slab
  1378. * lock and free the item. If there is no additional partial page
  1379. * handling required then we can return immediately.
  1380. */
  1381. static void __slab_free(struct kmem_cache *s, struct page *page,
  1382. void *x, void *addr, unsigned int offset)
  1383. {
  1384. void *prior;
  1385. void **object = (void *)x;
  1386. slab_lock(page);
  1387. if (unlikely(SlabDebug(page)))
  1388. goto debug;
  1389. checks_ok:
  1390. prior = object[offset] = page->freelist;
  1391. page->freelist = object;
  1392. page->inuse--;
  1393. if (unlikely(SlabFrozen(page)))
  1394. goto out_unlock;
  1395. if (unlikely(!page->inuse))
  1396. goto slab_empty;
  1397. /*
  1398. * Objects left in the slab. If it
  1399. * was not on the partial list before
  1400. * then add it.
  1401. */
  1402. if (unlikely(prior == page->end))
  1403. add_partial(get_node(s, page_to_nid(page)), page, 1);
  1404. out_unlock:
  1405. slab_unlock(page);
  1406. return;
  1407. slab_empty:
  1408. if (prior != page->end)
  1409. /*
  1410. * Slab still on the partial list.
  1411. */
  1412. remove_partial(s, page);
  1413. slab_unlock(page);
  1414. discard_slab(s, page);
  1415. return;
  1416. debug:
  1417. if (!free_debug_processing(s, page, x, addr))
  1418. goto out_unlock;
  1419. goto checks_ok;
  1420. }
  1421. /*
  1422. * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
  1423. * can perform fastpath freeing without additional function calls.
  1424. *
  1425. * The fastpath is only possible if we are freeing to the current cpu slab
  1426. * of this processor. This typically the case if we have just allocated
  1427. * the item before.
  1428. *
  1429. * If fastpath is not possible then fall back to __slab_free where we deal
  1430. * with all sorts of special processing.
  1431. */
  1432. static __always_inline void slab_free(struct kmem_cache *s,
  1433. struct page *page, void *x, void *addr)
  1434. {
  1435. void **object = (void *)x;
  1436. unsigned long flags;
  1437. struct kmem_cache_cpu *c;
  1438. local_irq_save(flags);
  1439. debug_check_no_locks_freed(object, s->objsize);
  1440. c = get_cpu_slab(s, smp_processor_id());
  1441. if (likely(page == c->page && c->node >= 0)) {
  1442. object[c->offset] = c->freelist;
  1443. c->freelist = object;
  1444. } else
  1445. __slab_free(s, page, x, addr, c->offset);
  1446. local_irq_restore(flags);
  1447. }
  1448. void kmem_cache_free(struct kmem_cache *s, void *x)
  1449. {
  1450. struct page *page;
  1451. page = virt_to_head_page(x);
  1452. slab_free(s, page, x, __builtin_return_address(0));
  1453. }
  1454. EXPORT_SYMBOL(kmem_cache_free);
  1455. /* Figure out on which slab object the object resides */
  1456. static struct page *get_object_page(const void *x)
  1457. {
  1458. struct page *page = virt_to_head_page(x);
  1459. if (!PageSlab(page))
  1460. return NULL;
  1461. return page;
  1462. }
  1463. /*
  1464. * Object placement in a slab is made very easy because we always start at
  1465. * offset 0. If we tune the size of the object to the alignment then we can
  1466. * get the required alignment by putting one properly sized object after
  1467. * another.
  1468. *
  1469. * Notice that the allocation order determines the sizes of the per cpu
  1470. * caches. Each processor has always one slab available for allocations.
  1471. * Increasing the allocation order reduces the number of times that slabs
  1472. * must be moved on and off the partial lists and is therefore a factor in
  1473. * locking overhead.
  1474. */
  1475. /*
  1476. * Mininum / Maximum order of slab pages. This influences locking overhead
  1477. * and slab fragmentation. A higher order reduces the number of partial slabs
  1478. * and increases the number of allocations possible without having to
  1479. * take the list_lock.
  1480. */
  1481. static int slub_min_order;
  1482. static int slub_max_order = DEFAULT_MAX_ORDER;
  1483. static int slub_min_objects = DEFAULT_MIN_OBJECTS;
  1484. /*
  1485. * Merge control. If this is set then no merging of slab caches will occur.
  1486. * (Could be removed. This was introduced to pacify the merge skeptics.)
  1487. */
  1488. static int slub_nomerge;
  1489. /*
  1490. * Calculate the order of allocation given an slab object size.
  1491. *
  1492. * The order of allocation has significant impact on performance and other
  1493. * system components. Generally order 0 allocations should be preferred since
  1494. * order 0 does not cause fragmentation in the page allocator. Larger objects
  1495. * be problematic to put into order 0 slabs because there may be too much
  1496. * unused space left. We go to a higher order if more than 1/8th of the slab
  1497. * would be wasted.
  1498. *
  1499. * In order to reach satisfactory performance we must ensure that a minimum
  1500. * number of objects is in one slab. Otherwise we may generate too much
  1501. * activity on the partial lists which requires taking the list_lock. This is
  1502. * less a concern for large slabs though which are rarely used.
  1503. *
  1504. * slub_max_order specifies the order where we begin to stop considering the
  1505. * number of objects in a slab as critical. If we reach slub_max_order then
  1506. * we try to keep the page order as low as possible. So we accept more waste
  1507. * of space in favor of a small page order.
  1508. *
  1509. * Higher order allocations also allow the placement of more objects in a
  1510. * slab and thereby reduce object handling overhead. If the user has
  1511. * requested a higher mininum order then we start with that one instead of
  1512. * the smallest order which will fit the object.
  1513. */
  1514. static inline int slab_order(int size, int min_objects,
  1515. int max_order, int fract_leftover)
  1516. {
  1517. int order;
  1518. int rem;
  1519. int min_order = slub_min_order;
  1520. for (order = max(min_order,
  1521. fls(min_objects * size - 1) - PAGE_SHIFT);
  1522. order <= max_order; order++) {
  1523. unsigned long slab_size = PAGE_SIZE << order;
  1524. if (slab_size < min_objects * size)
  1525. continue;
  1526. rem = slab_size % size;
  1527. if (rem <= slab_size / fract_leftover)
  1528. break;
  1529. }
  1530. return order;
  1531. }
  1532. static inline int calculate_order(int size)
  1533. {
  1534. int order;
  1535. int min_objects;
  1536. int fraction;
  1537. /*
  1538. * Attempt to find best configuration for a slab. This
  1539. * works by first attempting to generate a layout with
  1540. * the best configuration and backing off gradually.
  1541. *
  1542. * First we reduce the acceptable waste in a slab. Then
  1543. * we reduce the minimum objects required in a slab.
  1544. */
  1545. min_objects = slub_min_objects;
  1546. while (min_objects > 1) {
  1547. fraction = 8;
  1548. while (fraction >= 4) {
  1549. order = slab_order(size, min_objects,
  1550. slub_max_order, fraction);
  1551. if (order <= slub_max_order)
  1552. return order;
  1553. fraction /= 2;
  1554. }
  1555. min_objects /= 2;
  1556. }
  1557. /*
  1558. * We were unable to place multiple objects in a slab. Now
  1559. * lets see if we can place a single object there.
  1560. */
  1561. order = slab_order(size, 1, slub_max_order, 1);
  1562. if (order <= slub_max_order)
  1563. return order;
  1564. /*
  1565. * Doh this slab cannot be placed using slub_max_order.
  1566. */
  1567. order = slab_order(size, 1, MAX_ORDER, 1);
  1568. if (order <= MAX_ORDER)
  1569. return order;
  1570. return -ENOSYS;
  1571. }
  1572. /*
  1573. * Figure out what the alignment of the objects will be.
  1574. */
  1575. static unsigned long calculate_alignment(unsigned long flags,
  1576. unsigned long align, unsigned long size)
  1577. {
  1578. /*
  1579. * If the user wants hardware cache aligned objects then
  1580. * follow that suggestion if the object is sufficiently
  1581. * large.
  1582. *
  1583. * The hardware cache alignment cannot override the
  1584. * specified alignment though. If that is greater
  1585. * then use it.
  1586. */
  1587. if ((flags & SLAB_HWCACHE_ALIGN) &&
  1588. size > cache_line_size() / 2)
  1589. return max_t(unsigned long, align, cache_line_size());
  1590. if (align < ARCH_SLAB_MINALIGN)
  1591. return ARCH_SLAB_MINALIGN;
  1592. return ALIGN(align, sizeof(void *));
  1593. }
  1594. static void init_kmem_cache_cpu(struct kmem_cache *s,
  1595. struct kmem_cache_cpu *c)
  1596. {
  1597. c->page = NULL;
  1598. c->freelist = (void *)PAGE_MAPPING_ANON;
  1599. c->node = 0;
  1600. c->offset = s->offset / sizeof(void *);
  1601. c->objsize = s->objsize;
  1602. }
  1603. static void init_kmem_cache_node(struct kmem_cache_node *n)
  1604. {
  1605. n->nr_partial = 0;
  1606. atomic_long_set(&n->nr_slabs, 0);
  1607. spin_lock_init(&n->list_lock);
  1608. INIT_LIST_HEAD(&n->partial);
  1609. #ifdef CONFIG_SLUB_DEBUG
  1610. INIT_LIST_HEAD(&n->full);
  1611. #endif
  1612. }
  1613. #ifdef CONFIG_SMP
  1614. /*
  1615. * Per cpu array for per cpu structures.
  1616. *
  1617. * The per cpu array places all kmem_cache_cpu structures from one processor
  1618. * close together meaning that it becomes possible that multiple per cpu
  1619. * structures are contained in one cacheline. This may be particularly
  1620. * beneficial for the kmalloc caches.
  1621. *
  1622. * A desktop system typically has around 60-80 slabs. With 100 here we are
  1623. * likely able to get per cpu structures for all caches from the array defined
  1624. * here. We must be able to cover all kmalloc caches during bootstrap.
  1625. *
  1626. * If the per cpu array is exhausted then fall back to kmalloc
  1627. * of individual cachelines. No sharing is possible then.
  1628. */
  1629. #define NR_KMEM_CACHE_CPU 100
  1630. static DEFINE_PER_CPU(struct kmem_cache_cpu,
  1631. kmem_cache_cpu)[NR_KMEM_CACHE_CPU];
  1632. static DEFINE_PER_CPU(struct kmem_cache_cpu *, kmem_cache_cpu_free);
  1633. static cpumask_t kmem_cach_cpu_free_init_once = CPU_MASK_NONE;
  1634. static struct kmem_cache_cpu *alloc_kmem_cache_cpu(struct kmem_cache *s,
  1635. int cpu, gfp_t flags)
  1636. {
  1637. struct kmem_cache_cpu *c = per_cpu(kmem_cache_cpu_free, cpu);
  1638. if (c)
  1639. per_cpu(kmem_cache_cpu_free, cpu) =
  1640. (void *)c->freelist;
  1641. else {
  1642. /* Table overflow: So allocate ourselves */
  1643. c = kmalloc_node(
  1644. ALIGN(sizeof(struct kmem_cache_cpu), cache_line_size()),
  1645. flags, cpu_to_node(cpu));
  1646. if (!c)
  1647. return NULL;
  1648. }
  1649. init_kmem_cache_cpu(s, c);
  1650. return c;
  1651. }
  1652. static void free_kmem_cache_cpu(struct kmem_cache_cpu *c, int cpu)
  1653. {
  1654. if (c < per_cpu(kmem_cache_cpu, cpu) ||
  1655. c > per_cpu(kmem_cache_cpu, cpu) + NR_KMEM_CACHE_CPU) {
  1656. kfree(c);
  1657. return;
  1658. }
  1659. c->freelist = (void *)per_cpu(kmem_cache_cpu_free, cpu);
  1660. per_cpu(kmem_cache_cpu_free, cpu) = c;
  1661. }
  1662. static void free_kmem_cache_cpus(struct kmem_cache *s)
  1663. {
  1664. int cpu;
  1665. for_each_online_cpu(cpu) {
  1666. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  1667. if (c) {
  1668. s->cpu_slab[cpu] = NULL;
  1669. free_kmem_cache_cpu(c, cpu);
  1670. }
  1671. }
  1672. }
  1673. static int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
  1674. {
  1675. int cpu;
  1676. for_each_online_cpu(cpu) {
  1677. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  1678. if (c)
  1679. continue;
  1680. c = alloc_kmem_cache_cpu(s, cpu, flags);
  1681. if (!c) {
  1682. free_kmem_cache_cpus(s);
  1683. return 0;
  1684. }
  1685. s->cpu_slab[cpu] = c;
  1686. }
  1687. return 1;
  1688. }
  1689. /*
  1690. * Initialize the per cpu array.
  1691. */
  1692. static void init_alloc_cpu_cpu(int cpu)
  1693. {
  1694. int i;
  1695. if (cpu_isset(cpu, kmem_cach_cpu_free_init_once))
  1696. return;
  1697. for (i = NR_KMEM_CACHE_CPU - 1; i >= 0; i--)
  1698. free_kmem_cache_cpu(&per_cpu(kmem_cache_cpu, cpu)[i], cpu);
  1699. cpu_set(cpu, kmem_cach_cpu_free_init_once);
  1700. }
  1701. static void __init init_alloc_cpu(void)
  1702. {
  1703. int cpu;
  1704. for_each_online_cpu(cpu)
  1705. init_alloc_cpu_cpu(cpu);
  1706. }
  1707. #else
  1708. static inline void free_kmem_cache_cpus(struct kmem_cache *s) {}
  1709. static inline void init_alloc_cpu(void) {}
  1710. static inline int alloc_kmem_cache_cpus(struct kmem_cache *s, gfp_t flags)
  1711. {
  1712. init_kmem_cache_cpu(s, &s->cpu_slab);
  1713. return 1;
  1714. }
  1715. #endif
  1716. #ifdef CONFIG_NUMA
  1717. /*
  1718. * No kmalloc_node yet so do it by hand. We know that this is the first
  1719. * slab on the node for this slabcache. There are no concurrent accesses
  1720. * possible.
  1721. *
  1722. * Note that this function only works on the kmalloc_node_cache
  1723. * when allocating for the kmalloc_node_cache. This is used for bootstrapping
  1724. * memory on a fresh node that has no slab structures yet.
  1725. */
  1726. static struct kmem_cache_node *early_kmem_cache_node_alloc(gfp_t gfpflags,
  1727. int node)
  1728. {
  1729. struct page *page;
  1730. struct kmem_cache_node *n;
  1731. unsigned long flags;
  1732. BUG_ON(kmalloc_caches->size < sizeof(struct kmem_cache_node));
  1733. page = new_slab(kmalloc_caches, gfpflags, node);
  1734. BUG_ON(!page);
  1735. if (page_to_nid(page) != node) {
  1736. printk(KERN_ERR "SLUB: Unable to allocate memory from "
  1737. "node %d\n", node);
  1738. printk(KERN_ERR "SLUB: Allocating a useless per node structure "
  1739. "in order to be able to continue\n");
  1740. }
  1741. n = page->freelist;
  1742. BUG_ON(!n);
  1743. page->freelist = get_freepointer(kmalloc_caches, n);
  1744. page->inuse++;
  1745. kmalloc_caches->node[node] = n;
  1746. #ifdef CONFIG_SLUB_DEBUG
  1747. init_object(kmalloc_caches, n, 1);
  1748. init_tracking(kmalloc_caches, n);
  1749. #endif
  1750. init_kmem_cache_node(n);
  1751. atomic_long_inc(&n->nr_slabs);
  1752. /*
  1753. * lockdep requires consistent irq usage for each lock
  1754. * so even though there cannot be a race this early in
  1755. * the boot sequence, we still disable irqs.
  1756. */
  1757. local_irq_save(flags);
  1758. add_partial(n, page, 0);
  1759. local_irq_restore(flags);
  1760. return n;
  1761. }
  1762. static void free_kmem_cache_nodes(struct kmem_cache *s)
  1763. {
  1764. int node;
  1765. for_each_node_state(node, N_NORMAL_MEMORY) {
  1766. struct kmem_cache_node *n = s->node[node];
  1767. if (n && n != &s->local_node)
  1768. kmem_cache_free(kmalloc_caches, n);
  1769. s->node[node] = NULL;
  1770. }
  1771. }
  1772. static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
  1773. {
  1774. int node;
  1775. int local_node;
  1776. if (slab_state >= UP)
  1777. local_node = page_to_nid(virt_to_page(s));
  1778. else
  1779. local_node = 0;
  1780. for_each_node_state(node, N_NORMAL_MEMORY) {
  1781. struct kmem_cache_node *n;
  1782. if (local_node == node)
  1783. n = &s->local_node;
  1784. else {
  1785. if (slab_state == DOWN) {
  1786. n = early_kmem_cache_node_alloc(gfpflags,
  1787. node);
  1788. continue;
  1789. }
  1790. n = kmem_cache_alloc_node(kmalloc_caches,
  1791. gfpflags, node);
  1792. if (!n) {
  1793. free_kmem_cache_nodes(s);
  1794. return 0;
  1795. }
  1796. }
  1797. s->node[node] = n;
  1798. init_kmem_cache_node(n);
  1799. }
  1800. return 1;
  1801. }
  1802. #else
  1803. static void free_kmem_cache_nodes(struct kmem_cache *s)
  1804. {
  1805. }
  1806. static int init_kmem_cache_nodes(struct kmem_cache *s, gfp_t gfpflags)
  1807. {
  1808. init_kmem_cache_node(&s->local_node);
  1809. return 1;
  1810. }
  1811. #endif
  1812. /*
  1813. * calculate_sizes() determines the order and the distribution of data within
  1814. * a slab object.
  1815. */
  1816. static int calculate_sizes(struct kmem_cache *s)
  1817. {
  1818. unsigned long flags = s->flags;
  1819. unsigned long size = s->objsize;
  1820. unsigned long align = s->align;
  1821. /*
  1822. * Determine if we can poison the object itself. If the user of
  1823. * the slab may touch the object after free or before allocation
  1824. * then we should never poison the object itself.
  1825. */
  1826. if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
  1827. !s->ctor)
  1828. s->flags |= __OBJECT_POISON;
  1829. else
  1830. s->flags &= ~__OBJECT_POISON;
  1831. /*
  1832. * Round up object size to the next word boundary. We can only
  1833. * place the free pointer at word boundaries and this determines
  1834. * the possible location of the free pointer.
  1835. */
  1836. size = ALIGN(size, sizeof(void *));
  1837. #ifdef CONFIG_SLUB_DEBUG
  1838. /*
  1839. * If we are Redzoning then check if there is some space between the
  1840. * end of the object and the free pointer. If not then add an
  1841. * additional word to have some bytes to store Redzone information.
  1842. */
  1843. if ((flags & SLAB_RED_ZONE) && size == s->objsize)
  1844. size += sizeof(void *);
  1845. #endif
  1846. /*
  1847. * With that we have determined the number of bytes in actual use
  1848. * by the object. This is the potential offset to the free pointer.
  1849. */
  1850. s->inuse = size;
  1851. if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
  1852. s->ctor)) {
  1853. /*
  1854. * Relocate free pointer after the object if it is not
  1855. * permitted to overwrite the first word of the object on
  1856. * kmem_cache_free.
  1857. *
  1858. * This is the case if we do RCU, have a constructor or
  1859. * destructor or are poisoning the objects.
  1860. */
  1861. s->offset = size;
  1862. size += sizeof(void *);
  1863. }
  1864. #ifdef CONFIG_SLUB_DEBUG
  1865. if (flags & SLAB_STORE_USER)
  1866. /*
  1867. * Need to store information about allocs and frees after
  1868. * the object.
  1869. */
  1870. size += 2 * sizeof(struct track);
  1871. if (flags & SLAB_RED_ZONE)
  1872. /*
  1873. * Add some empty padding so that we can catch
  1874. * overwrites from earlier objects rather than let
  1875. * tracking information or the free pointer be
  1876. * corrupted if an user writes before the start
  1877. * of the object.
  1878. */
  1879. size += sizeof(void *);
  1880. #endif
  1881. /*
  1882. * Determine the alignment based on various parameters that the
  1883. * user specified and the dynamic determination of cache line size
  1884. * on bootup.
  1885. */
  1886. align = calculate_alignment(flags, align, s->objsize);
  1887. /*
  1888. * SLUB stores one object immediately after another beginning from
  1889. * offset 0. In order to align the objects we have to simply size
  1890. * each object to conform to the alignment.
  1891. */
  1892. size = ALIGN(size, align);
  1893. s->size = size;
  1894. s->order = calculate_order(size);
  1895. if (s->order < 0)
  1896. return 0;
  1897. /*
  1898. * Determine the number of objects per slab
  1899. */
  1900. s->objects = (PAGE_SIZE << s->order) / size;
  1901. return !!s->objects;
  1902. }
  1903. static int kmem_cache_open(struct kmem_cache *s, gfp_t gfpflags,
  1904. const char *name, size_t size,
  1905. size_t align, unsigned long flags,
  1906. void (*ctor)(struct kmem_cache *, void *))
  1907. {
  1908. memset(s, 0, kmem_size);
  1909. s->name = name;
  1910. s->ctor = ctor;
  1911. s->objsize = size;
  1912. s->align = align;
  1913. s->flags = kmem_cache_flags(size, flags, name, ctor);
  1914. if (!calculate_sizes(s))
  1915. goto error;
  1916. s->refcount = 1;
  1917. #ifdef CONFIG_NUMA
  1918. s->remote_node_defrag_ratio = 100;
  1919. #endif
  1920. if (!init_kmem_cache_nodes(s, gfpflags & ~SLUB_DMA))
  1921. goto error;
  1922. if (alloc_kmem_cache_cpus(s, gfpflags & ~SLUB_DMA))
  1923. return 1;
  1924. free_kmem_cache_nodes(s);
  1925. error:
  1926. if (flags & SLAB_PANIC)
  1927. panic("Cannot create slab %s size=%lu realsize=%u "
  1928. "order=%u offset=%u flags=%lx\n",
  1929. s->name, (unsigned long)size, s->size, s->order,
  1930. s->offset, flags);
  1931. return 0;
  1932. }
  1933. /*
  1934. * Check if a given pointer is valid
  1935. */
  1936. int kmem_ptr_validate(struct kmem_cache *s, const void *object)
  1937. {
  1938. struct page *page;
  1939. page = get_object_page(object);
  1940. if (!page || s != page->slab)
  1941. /* No slab or wrong slab */
  1942. return 0;
  1943. if (!check_valid_pointer(s, page, object))
  1944. return 0;
  1945. /*
  1946. * We could also check if the object is on the slabs freelist.
  1947. * But this would be too expensive and it seems that the main
  1948. * purpose of kmem_ptr_valid is to check if the object belongs
  1949. * to a certain slab.
  1950. */
  1951. return 1;
  1952. }
  1953. EXPORT_SYMBOL(kmem_ptr_validate);
  1954. /*
  1955. * Determine the size of a slab object
  1956. */
  1957. unsigned int kmem_cache_size(struct kmem_cache *s)
  1958. {
  1959. return s->objsize;
  1960. }
  1961. EXPORT_SYMBOL(kmem_cache_size);
  1962. const char *kmem_cache_name(struct kmem_cache *s)
  1963. {
  1964. return s->name;
  1965. }
  1966. EXPORT_SYMBOL(kmem_cache_name);
  1967. /*
  1968. * Attempt to free all slabs on a node. Return the number of slabs we
  1969. * were unable to free.
  1970. */
  1971. static int free_list(struct kmem_cache *s, struct kmem_cache_node *n,
  1972. struct list_head *list)
  1973. {
  1974. int slabs_inuse = 0;
  1975. unsigned long flags;
  1976. struct page *page, *h;
  1977. spin_lock_irqsave(&n->list_lock, flags);
  1978. list_for_each_entry_safe(page, h, list, lru)
  1979. if (!page->inuse) {
  1980. list_del(&page->lru);
  1981. discard_slab(s, page);
  1982. } else
  1983. slabs_inuse++;
  1984. spin_unlock_irqrestore(&n->list_lock, flags);
  1985. return slabs_inuse;
  1986. }
  1987. /*
  1988. * Release all resources used by a slab cache.
  1989. */
  1990. static inline int kmem_cache_close(struct kmem_cache *s)
  1991. {
  1992. int node;
  1993. flush_all(s);
  1994. /* Attempt to free all objects */
  1995. free_kmem_cache_cpus(s);
  1996. for_each_node_state(node, N_NORMAL_MEMORY) {
  1997. struct kmem_cache_node *n = get_node(s, node);
  1998. n->nr_partial -= free_list(s, n, &n->partial);
  1999. if (atomic_long_read(&n->nr_slabs))
  2000. return 1;
  2001. }
  2002. free_kmem_cache_nodes(s);
  2003. return 0;
  2004. }
  2005. /*
  2006. * Close a cache and release the kmem_cache structure
  2007. * (must be used for caches created using kmem_cache_create)
  2008. */
  2009. void kmem_cache_destroy(struct kmem_cache *s)
  2010. {
  2011. down_write(&slub_lock);
  2012. s->refcount--;
  2013. if (!s->refcount) {
  2014. list_del(&s->list);
  2015. up_write(&slub_lock);
  2016. if (kmem_cache_close(s))
  2017. WARN_ON(1);
  2018. sysfs_slab_remove(s);
  2019. } else
  2020. up_write(&slub_lock);
  2021. }
  2022. EXPORT_SYMBOL(kmem_cache_destroy);
  2023. /********************************************************************
  2024. * Kmalloc subsystem
  2025. *******************************************************************/
  2026. struct kmem_cache kmalloc_caches[PAGE_SHIFT] __cacheline_aligned;
  2027. EXPORT_SYMBOL(kmalloc_caches);
  2028. #ifdef CONFIG_ZONE_DMA
  2029. static struct kmem_cache *kmalloc_caches_dma[PAGE_SHIFT];
  2030. #endif
  2031. static int __init setup_slub_min_order(char *str)
  2032. {
  2033. get_option(&str, &slub_min_order);
  2034. return 1;
  2035. }
  2036. __setup("slub_min_order=", setup_slub_min_order);
  2037. static int __init setup_slub_max_order(char *str)
  2038. {
  2039. get_option(&str, &slub_max_order);
  2040. return 1;
  2041. }
  2042. __setup("slub_max_order=", setup_slub_max_order);
  2043. static int __init setup_slub_min_objects(char *str)
  2044. {
  2045. get_option(&str, &slub_min_objects);
  2046. return 1;
  2047. }
  2048. __setup("slub_min_objects=", setup_slub_min_objects);
  2049. static int __init setup_slub_nomerge(char *str)
  2050. {
  2051. slub_nomerge = 1;
  2052. return 1;
  2053. }
  2054. __setup("slub_nomerge", setup_slub_nomerge);
  2055. static struct kmem_cache *create_kmalloc_cache(struct kmem_cache *s,
  2056. const char *name, int size, gfp_t gfp_flags)
  2057. {
  2058. unsigned int flags = 0;
  2059. if (gfp_flags & SLUB_DMA)
  2060. flags = SLAB_CACHE_DMA;
  2061. down_write(&slub_lock);
  2062. if (!kmem_cache_open(s, gfp_flags, name, size, ARCH_KMALLOC_MINALIGN,
  2063. flags, NULL))
  2064. goto panic;
  2065. list_add(&s->list, &slab_caches);
  2066. up_write(&slub_lock);
  2067. if (sysfs_slab_add(s))
  2068. goto panic;
  2069. return s;
  2070. panic:
  2071. panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
  2072. }
  2073. #ifdef CONFIG_ZONE_DMA
  2074. static void sysfs_add_func(struct work_struct *w)
  2075. {
  2076. struct kmem_cache *s;
  2077. down_write(&slub_lock);
  2078. list_for_each_entry(s, &slab_caches, list) {
  2079. if (s->flags & __SYSFS_ADD_DEFERRED) {
  2080. s->flags &= ~__SYSFS_ADD_DEFERRED;
  2081. sysfs_slab_add(s);
  2082. }
  2083. }
  2084. up_write(&slub_lock);
  2085. }
  2086. static DECLARE_WORK(sysfs_add_work, sysfs_add_func);
  2087. static noinline struct kmem_cache *dma_kmalloc_cache(int index, gfp_t flags)
  2088. {
  2089. struct kmem_cache *s;
  2090. char *text;
  2091. size_t realsize;
  2092. s = kmalloc_caches_dma[index];
  2093. if (s)
  2094. return s;
  2095. /* Dynamically create dma cache */
  2096. if (flags & __GFP_WAIT)
  2097. down_write(&slub_lock);
  2098. else {
  2099. if (!down_write_trylock(&slub_lock))
  2100. goto out;
  2101. }
  2102. if (kmalloc_caches_dma[index])
  2103. goto unlock_out;
  2104. realsize = kmalloc_caches[index].objsize;
  2105. text = kasprintf(flags & ~SLUB_DMA, "kmalloc_dma-%d", (unsigned int)realsize),
  2106. s = kmalloc(kmem_size, flags & ~SLUB_DMA);
  2107. if (!s || !text || !kmem_cache_open(s, flags, text,
  2108. realsize, ARCH_KMALLOC_MINALIGN,
  2109. SLAB_CACHE_DMA|__SYSFS_ADD_DEFERRED, NULL)) {
  2110. kfree(s);
  2111. kfree(text);
  2112. goto unlock_out;
  2113. }
  2114. list_add(&s->list, &slab_caches);
  2115. kmalloc_caches_dma[index] = s;
  2116. schedule_work(&sysfs_add_work);
  2117. unlock_out:
  2118. up_write(&slub_lock);
  2119. out:
  2120. return kmalloc_caches_dma[index];
  2121. }
  2122. #endif
  2123. /*
  2124. * Conversion table for small slabs sizes / 8 to the index in the
  2125. * kmalloc array. This is necessary for slabs < 192 since we have non power
  2126. * of two cache sizes there. The size of larger slabs can be determined using
  2127. * fls.
  2128. */
  2129. static s8 size_index[24] = {
  2130. 3, /* 8 */
  2131. 4, /* 16 */
  2132. 5, /* 24 */
  2133. 5, /* 32 */
  2134. 6, /* 40 */
  2135. 6, /* 48 */
  2136. 6, /* 56 */
  2137. 6, /* 64 */
  2138. 1, /* 72 */
  2139. 1, /* 80 */
  2140. 1, /* 88 */
  2141. 1, /* 96 */
  2142. 7, /* 104 */
  2143. 7, /* 112 */
  2144. 7, /* 120 */
  2145. 7, /* 128 */
  2146. 2, /* 136 */
  2147. 2, /* 144 */
  2148. 2, /* 152 */
  2149. 2, /* 160 */
  2150. 2, /* 168 */
  2151. 2, /* 176 */
  2152. 2, /* 184 */
  2153. 2 /* 192 */
  2154. };
  2155. static struct kmem_cache *get_slab(size_t size, gfp_t flags)
  2156. {
  2157. int index;
  2158. if (size <= 192) {
  2159. if (!size)
  2160. return ZERO_SIZE_PTR;
  2161. index = size_index[(size - 1) / 8];
  2162. } else
  2163. index = fls(size - 1);
  2164. #ifdef CONFIG_ZONE_DMA
  2165. if (unlikely((flags & SLUB_DMA)))
  2166. return dma_kmalloc_cache(index, flags);
  2167. #endif
  2168. return &kmalloc_caches[index];
  2169. }
  2170. void *__kmalloc(size_t size, gfp_t flags)
  2171. {
  2172. struct kmem_cache *s;
  2173. if (unlikely(size > PAGE_SIZE / 2))
  2174. return (void *)__get_free_pages(flags | __GFP_COMP,
  2175. get_order(size));
  2176. s = get_slab(size, flags);
  2177. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2178. return s;
  2179. return slab_alloc(s, flags, -1, __builtin_return_address(0));
  2180. }
  2181. EXPORT_SYMBOL(__kmalloc);
  2182. #ifdef CONFIG_NUMA
  2183. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  2184. {
  2185. struct kmem_cache *s;
  2186. if (unlikely(size > PAGE_SIZE / 2))
  2187. return (void *)__get_free_pages(flags | __GFP_COMP,
  2188. get_order(size));
  2189. s = get_slab(size, flags);
  2190. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2191. return s;
  2192. return slab_alloc(s, flags, node, __builtin_return_address(0));
  2193. }
  2194. EXPORT_SYMBOL(__kmalloc_node);
  2195. #endif
  2196. size_t ksize(const void *object)
  2197. {
  2198. struct page *page;
  2199. struct kmem_cache *s;
  2200. BUG_ON(!object);
  2201. if (unlikely(object == ZERO_SIZE_PTR))
  2202. return 0;
  2203. page = virt_to_head_page(object);
  2204. BUG_ON(!page);
  2205. if (unlikely(!PageSlab(page)))
  2206. return PAGE_SIZE << compound_order(page);
  2207. s = page->slab;
  2208. BUG_ON(!s);
  2209. /*
  2210. * Debugging requires use of the padding between object
  2211. * and whatever may come after it.
  2212. */
  2213. if (s->flags & (SLAB_RED_ZONE | SLAB_POISON))
  2214. return s->objsize;
  2215. /*
  2216. * If we have the need to store the freelist pointer
  2217. * back there or track user information then we can
  2218. * only use the space before that information.
  2219. */
  2220. if (s->flags & (SLAB_DESTROY_BY_RCU | SLAB_STORE_USER))
  2221. return s->inuse;
  2222. /*
  2223. * Else we can use all the padding etc for the allocation
  2224. */
  2225. return s->size;
  2226. }
  2227. EXPORT_SYMBOL(ksize);
  2228. void kfree(const void *x)
  2229. {
  2230. struct page *page;
  2231. void *object = (void *)x;
  2232. if (unlikely(ZERO_OR_NULL_PTR(x)))
  2233. return;
  2234. page = virt_to_head_page(x);
  2235. if (unlikely(!PageSlab(page))) {
  2236. put_page(page);
  2237. return;
  2238. }
  2239. slab_free(page->slab, page, object, __builtin_return_address(0));
  2240. }
  2241. EXPORT_SYMBOL(kfree);
  2242. static unsigned long count_partial(struct kmem_cache_node *n)
  2243. {
  2244. unsigned long flags;
  2245. unsigned long x = 0;
  2246. struct page *page;
  2247. spin_lock_irqsave(&n->list_lock, flags);
  2248. list_for_each_entry(page, &n->partial, lru)
  2249. x += page->inuse;
  2250. spin_unlock_irqrestore(&n->list_lock, flags);
  2251. return x;
  2252. }
  2253. /*
  2254. * kmem_cache_shrink removes empty slabs from the partial lists and sorts
  2255. * the remaining slabs by the number of items in use. The slabs with the
  2256. * most items in use come first. New allocations will then fill those up
  2257. * and thus they can be removed from the partial lists.
  2258. *
  2259. * The slabs with the least items are placed last. This results in them
  2260. * being allocated from last increasing the chance that the last objects
  2261. * are freed in them.
  2262. */
  2263. int kmem_cache_shrink(struct kmem_cache *s)
  2264. {
  2265. int node;
  2266. int i;
  2267. struct kmem_cache_node *n;
  2268. struct page *page;
  2269. struct page *t;
  2270. struct list_head *slabs_by_inuse =
  2271. kmalloc(sizeof(struct list_head) * s->objects, GFP_KERNEL);
  2272. unsigned long flags;
  2273. if (!slabs_by_inuse)
  2274. return -ENOMEM;
  2275. flush_all(s);
  2276. for_each_node_state(node, N_NORMAL_MEMORY) {
  2277. n = get_node(s, node);
  2278. if (!n->nr_partial)
  2279. continue;
  2280. for (i = 0; i < s->objects; i++)
  2281. INIT_LIST_HEAD(slabs_by_inuse + i);
  2282. spin_lock_irqsave(&n->list_lock, flags);
  2283. /*
  2284. * Build lists indexed by the items in use in each slab.
  2285. *
  2286. * Note that concurrent frees may occur while we hold the
  2287. * list_lock. page->inuse here is the upper limit.
  2288. */
  2289. list_for_each_entry_safe(page, t, &n->partial, lru) {
  2290. if (!page->inuse && slab_trylock(page)) {
  2291. /*
  2292. * Must hold slab lock here because slab_free
  2293. * may have freed the last object and be
  2294. * waiting to release the slab.
  2295. */
  2296. list_del(&page->lru);
  2297. n->nr_partial--;
  2298. slab_unlock(page);
  2299. discard_slab(s, page);
  2300. } else {
  2301. list_move(&page->lru,
  2302. slabs_by_inuse + page->inuse);
  2303. }
  2304. }
  2305. /*
  2306. * Rebuild the partial list with the slabs filled up most
  2307. * first and the least used slabs at the end.
  2308. */
  2309. for (i = s->objects - 1; i >= 0; i--)
  2310. list_splice(slabs_by_inuse + i, n->partial.prev);
  2311. spin_unlock_irqrestore(&n->list_lock, flags);
  2312. }
  2313. kfree(slabs_by_inuse);
  2314. return 0;
  2315. }
  2316. EXPORT_SYMBOL(kmem_cache_shrink);
  2317. #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
  2318. static int slab_mem_going_offline_callback(void *arg)
  2319. {
  2320. struct kmem_cache *s;
  2321. down_read(&slub_lock);
  2322. list_for_each_entry(s, &slab_caches, list)
  2323. kmem_cache_shrink(s);
  2324. up_read(&slub_lock);
  2325. return 0;
  2326. }
  2327. static void slab_mem_offline_callback(void *arg)
  2328. {
  2329. struct kmem_cache_node *n;
  2330. struct kmem_cache *s;
  2331. struct memory_notify *marg = arg;
  2332. int offline_node;
  2333. offline_node = marg->status_change_nid;
  2334. /*
  2335. * If the node still has available memory. we need kmem_cache_node
  2336. * for it yet.
  2337. */
  2338. if (offline_node < 0)
  2339. return;
  2340. down_read(&slub_lock);
  2341. list_for_each_entry(s, &slab_caches, list) {
  2342. n = get_node(s, offline_node);
  2343. if (n) {
  2344. /*
  2345. * if n->nr_slabs > 0, slabs still exist on the node
  2346. * that is going down. We were unable to free them,
  2347. * and offline_pages() function shoudn't call this
  2348. * callback. So, we must fail.
  2349. */
  2350. BUG_ON(atomic_long_read(&n->nr_slabs));
  2351. s->node[offline_node] = NULL;
  2352. kmem_cache_free(kmalloc_caches, n);
  2353. }
  2354. }
  2355. up_read(&slub_lock);
  2356. }
  2357. static int slab_mem_going_online_callback(void *arg)
  2358. {
  2359. struct kmem_cache_node *n;
  2360. struct kmem_cache *s;
  2361. struct memory_notify *marg = arg;
  2362. int nid = marg->status_change_nid;
  2363. int ret = 0;
  2364. /*
  2365. * If the node's memory is already available, then kmem_cache_node is
  2366. * already created. Nothing to do.
  2367. */
  2368. if (nid < 0)
  2369. return 0;
  2370. /*
  2371. * We are bringing a node online. No memory is availabe yet. We must
  2372. * allocate a kmem_cache_node structure in order to bring the node
  2373. * online.
  2374. */
  2375. down_read(&slub_lock);
  2376. list_for_each_entry(s, &slab_caches, list) {
  2377. /*
  2378. * XXX: kmem_cache_alloc_node will fallback to other nodes
  2379. * since memory is not yet available from the node that
  2380. * is brought up.
  2381. */
  2382. n = kmem_cache_alloc(kmalloc_caches, GFP_KERNEL);
  2383. if (!n) {
  2384. ret = -ENOMEM;
  2385. goto out;
  2386. }
  2387. init_kmem_cache_node(n);
  2388. s->node[nid] = n;
  2389. }
  2390. out:
  2391. up_read(&slub_lock);
  2392. return ret;
  2393. }
  2394. static int slab_memory_callback(struct notifier_block *self,
  2395. unsigned long action, void *arg)
  2396. {
  2397. int ret = 0;
  2398. switch (action) {
  2399. case MEM_GOING_ONLINE:
  2400. ret = slab_mem_going_online_callback(arg);
  2401. break;
  2402. case MEM_GOING_OFFLINE:
  2403. ret = slab_mem_going_offline_callback(arg);
  2404. break;
  2405. case MEM_OFFLINE:
  2406. case MEM_CANCEL_ONLINE:
  2407. slab_mem_offline_callback(arg);
  2408. break;
  2409. case MEM_ONLINE:
  2410. case MEM_CANCEL_OFFLINE:
  2411. break;
  2412. }
  2413. ret = notifier_from_errno(ret);
  2414. return ret;
  2415. }
  2416. #endif /* CONFIG_MEMORY_HOTPLUG */
  2417. /********************************************************************
  2418. * Basic setup of slabs
  2419. *******************************************************************/
  2420. void __init kmem_cache_init(void)
  2421. {
  2422. int i;
  2423. int caches = 0;
  2424. init_alloc_cpu();
  2425. #ifdef CONFIG_NUMA
  2426. /*
  2427. * Must first have the slab cache available for the allocations of the
  2428. * struct kmem_cache_node's. There is special bootstrap code in
  2429. * kmem_cache_open for slab_state == DOWN.
  2430. */
  2431. create_kmalloc_cache(&kmalloc_caches[0], "kmem_cache_node",
  2432. sizeof(struct kmem_cache_node), GFP_KERNEL);
  2433. kmalloc_caches[0].refcount = -1;
  2434. caches++;
  2435. hotplug_memory_notifier(slab_memory_callback, 1);
  2436. #endif
  2437. /* Able to allocate the per node structures */
  2438. slab_state = PARTIAL;
  2439. /* Caches that are not of the two-to-the-power-of size */
  2440. if (KMALLOC_MIN_SIZE <= 64) {
  2441. create_kmalloc_cache(&kmalloc_caches[1],
  2442. "kmalloc-96", 96, GFP_KERNEL);
  2443. caches++;
  2444. }
  2445. if (KMALLOC_MIN_SIZE <= 128) {
  2446. create_kmalloc_cache(&kmalloc_caches[2],
  2447. "kmalloc-192", 192, GFP_KERNEL);
  2448. caches++;
  2449. }
  2450. for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++) {
  2451. create_kmalloc_cache(&kmalloc_caches[i],
  2452. "kmalloc", 1 << i, GFP_KERNEL);
  2453. caches++;
  2454. }
  2455. /*
  2456. * Patch up the size_index table if we have strange large alignment
  2457. * requirements for the kmalloc array. This is only the case for
  2458. * mips it seems. The standard arches will not generate any code here.
  2459. *
  2460. * Largest permitted alignment is 256 bytes due to the way we
  2461. * handle the index determination for the smaller caches.
  2462. *
  2463. * Make sure that nothing crazy happens if someone starts tinkering
  2464. * around with ARCH_KMALLOC_MINALIGN
  2465. */
  2466. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  2467. (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
  2468. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8)
  2469. size_index[(i - 1) / 8] = KMALLOC_SHIFT_LOW;
  2470. slab_state = UP;
  2471. /* Provide the correct kmalloc names now that the caches are up */
  2472. for (i = KMALLOC_SHIFT_LOW; i < PAGE_SHIFT; i++)
  2473. kmalloc_caches[i]. name =
  2474. kasprintf(GFP_KERNEL, "kmalloc-%d", 1 << i);
  2475. #ifdef CONFIG_SMP
  2476. register_cpu_notifier(&slab_notifier);
  2477. kmem_size = offsetof(struct kmem_cache, cpu_slab) +
  2478. nr_cpu_ids * sizeof(struct kmem_cache_cpu *);
  2479. #else
  2480. kmem_size = sizeof(struct kmem_cache);
  2481. #endif
  2482. printk(KERN_INFO "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
  2483. " CPUs=%d, Nodes=%d\n",
  2484. caches, cache_line_size(),
  2485. slub_min_order, slub_max_order, slub_min_objects,
  2486. nr_cpu_ids, nr_node_ids);
  2487. }
  2488. /*
  2489. * Find a mergeable slab cache
  2490. */
  2491. static int slab_unmergeable(struct kmem_cache *s)
  2492. {
  2493. if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
  2494. return 1;
  2495. if (s->ctor)
  2496. return 1;
  2497. /*
  2498. * We may have set a slab to be unmergeable during bootstrap.
  2499. */
  2500. if (s->refcount < 0)
  2501. return 1;
  2502. return 0;
  2503. }
  2504. static struct kmem_cache *find_mergeable(size_t size,
  2505. size_t align, unsigned long flags, const char *name,
  2506. void (*ctor)(struct kmem_cache *, void *))
  2507. {
  2508. struct kmem_cache *s;
  2509. if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
  2510. return NULL;
  2511. if (ctor)
  2512. return NULL;
  2513. size = ALIGN(size, sizeof(void *));
  2514. align = calculate_alignment(flags, align, size);
  2515. size = ALIGN(size, align);
  2516. flags = kmem_cache_flags(size, flags, name, NULL);
  2517. list_for_each_entry(s, &slab_caches, list) {
  2518. if (slab_unmergeable(s))
  2519. continue;
  2520. if (size > s->size)
  2521. continue;
  2522. if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
  2523. continue;
  2524. /*
  2525. * Check if alignment is compatible.
  2526. * Courtesy of Adrian Drzewiecki
  2527. */
  2528. if ((s->size & ~(align - 1)) != s->size)
  2529. continue;
  2530. if (s->size - size >= sizeof(void *))
  2531. continue;
  2532. return s;
  2533. }
  2534. return NULL;
  2535. }
  2536. struct kmem_cache *kmem_cache_create(const char *name, size_t size,
  2537. size_t align, unsigned long flags,
  2538. void (*ctor)(struct kmem_cache *, void *))
  2539. {
  2540. struct kmem_cache *s;
  2541. down_write(&slub_lock);
  2542. s = find_mergeable(size, align, flags, name, ctor);
  2543. if (s) {
  2544. int cpu;
  2545. s->refcount++;
  2546. /*
  2547. * Adjust the object sizes so that we clear
  2548. * the complete object on kzalloc.
  2549. */
  2550. s->objsize = max(s->objsize, (int)size);
  2551. /*
  2552. * And then we need to update the object size in the
  2553. * per cpu structures
  2554. */
  2555. for_each_online_cpu(cpu)
  2556. get_cpu_slab(s, cpu)->objsize = s->objsize;
  2557. s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
  2558. up_write(&slub_lock);
  2559. if (sysfs_slab_alias(s, name))
  2560. goto err;
  2561. return s;
  2562. }
  2563. s = kmalloc(kmem_size, GFP_KERNEL);
  2564. if (s) {
  2565. if (kmem_cache_open(s, GFP_KERNEL, name,
  2566. size, align, flags, ctor)) {
  2567. list_add(&s->list, &slab_caches);
  2568. up_write(&slub_lock);
  2569. if (sysfs_slab_add(s))
  2570. goto err;
  2571. return s;
  2572. }
  2573. kfree(s);
  2574. }
  2575. up_write(&slub_lock);
  2576. err:
  2577. if (flags & SLAB_PANIC)
  2578. panic("Cannot create slabcache %s\n", name);
  2579. else
  2580. s = NULL;
  2581. return s;
  2582. }
  2583. EXPORT_SYMBOL(kmem_cache_create);
  2584. #ifdef CONFIG_SMP
  2585. /*
  2586. * Use the cpu notifier to insure that the cpu slabs are flushed when
  2587. * necessary.
  2588. */
  2589. static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
  2590. unsigned long action, void *hcpu)
  2591. {
  2592. long cpu = (long)hcpu;
  2593. struct kmem_cache *s;
  2594. unsigned long flags;
  2595. switch (action) {
  2596. case CPU_UP_PREPARE:
  2597. case CPU_UP_PREPARE_FROZEN:
  2598. init_alloc_cpu_cpu(cpu);
  2599. down_read(&slub_lock);
  2600. list_for_each_entry(s, &slab_caches, list)
  2601. s->cpu_slab[cpu] = alloc_kmem_cache_cpu(s, cpu,
  2602. GFP_KERNEL);
  2603. up_read(&slub_lock);
  2604. break;
  2605. case CPU_UP_CANCELED:
  2606. case CPU_UP_CANCELED_FROZEN:
  2607. case CPU_DEAD:
  2608. case CPU_DEAD_FROZEN:
  2609. down_read(&slub_lock);
  2610. list_for_each_entry(s, &slab_caches, list) {
  2611. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  2612. local_irq_save(flags);
  2613. __flush_cpu_slab(s, cpu);
  2614. local_irq_restore(flags);
  2615. free_kmem_cache_cpu(c, cpu);
  2616. s->cpu_slab[cpu] = NULL;
  2617. }
  2618. up_read(&slub_lock);
  2619. break;
  2620. default:
  2621. break;
  2622. }
  2623. return NOTIFY_OK;
  2624. }
  2625. static struct notifier_block __cpuinitdata slab_notifier = {
  2626. &slab_cpuup_callback, NULL, 0
  2627. };
  2628. #endif
  2629. void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, void *caller)
  2630. {
  2631. struct kmem_cache *s;
  2632. if (unlikely(size > PAGE_SIZE / 2))
  2633. return (void *)__get_free_pages(gfpflags | __GFP_COMP,
  2634. get_order(size));
  2635. s = get_slab(size, gfpflags);
  2636. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2637. return s;
  2638. return slab_alloc(s, gfpflags, -1, caller);
  2639. }
  2640. void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
  2641. int node, void *caller)
  2642. {
  2643. struct kmem_cache *s;
  2644. if (unlikely(size > PAGE_SIZE / 2))
  2645. return (void *)__get_free_pages(gfpflags | __GFP_COMP,
  2646. get_order(size));
  2647. s = get_slab(size, gfpflags);
  2648. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2649. return s;
  2650. return slab_alloc(s, gfpflags, node, caller);
  2651. }
  2652. #if defined(CONFIG_SYSFS) && defined(CONFIG_SLUB_DEBUG)
  2653. static int validate_slab(struct kmem_cache *s, struct page *page,
  2654. unsigned long *map)
  2655. {
  2656. void *p;
  2657. void *addr = slab_address(page);
  2658. if (!check_slab(s, page) ||
  2659. !on_freelist(s, page, NULL))
  2660. return 0;
  2661. /* Now we know that a valid freelist exists */
  2662. bitmap_zero(map, s->objects);
  2663. for_each_free_object(p, s, page->freelist) {
  2664. set_bit(slab_index(p, s, addr), map);
  2665. if (!check_object(s, page, p, 0))
  2666. return 0;
  2667. }
  2668. for_each_object(p, s, addr)
  2669. if (!test_bit(slab_index(p, s, addr), map))
  2670. if (!check_object(s, page, p, 1))
  2671. return 0;
  2672. return 1;
  2673. }
  2674. static void validate_slab_slab(struct kmem_cache *s, struct page *page,
  2675. unsigned long *map)
  2676. {
  2677. if (slab_trylock(page)) {
  2678. validate_slab(s, page, map);
  2679. slab_unlock(page);
  2680. } else
  2681. printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
  2682. s->name, page);
  2683. if (s->flags & DEBUG_DEFAULT_FLAGS) {
  2684. if (!SlabDebug(page))
  2685. printk(KERN_ERR "SLUB %s: SlabDebug not set "
  2686. "on slab 0x%p\n", s->name, page);
  2687. } else {
  2688. if (SlabDebug(page))
  2689. printk(KERN_ERR "SLUB %s: SlabDebug set on "
  2690. "slab 0x%p\n", s->name, page);
  2691. }
  2692. }
  2693. static int validate_slab_node(struct kmem_cache *s,
  2694. struct kmem_cache_node *n, unsigned long *map)
  2695. {
  2696. unsigned long count = 0;
  2697. struct page *page;
  2698. unsigned long flags;
  2699. spin_lock_irqsave(&n->list_lock, flags);
  2700. list_for_each_entry(page, &n->partial, lru) {
  2701. validate_slab_slab(s, page, map);
  2702. count++;
  2703. }
  2704. if (count != n->nr_partial)
  2705. printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
  2706. "counter=%ld\n", s->name, count, n->nr_partial);
  2707. if (!(s->flags & SLAB_STORE_USER))
  2708. goto out;
  2709. list_for_each_entry(page, &n->full, lru) {
  2710. validate_slab_slab(s, page, map);
  2711. count++;
  2712. }
  2713. if (count != atomic_long_read(&n->nr_slabs))
  2714. printk(KERN_ERR "SLUB: %s %ld slabs counted but "
  2715. "counter=%ld\n", s->name, count,
  2716. atomic_long_read(&n->nr_slabs));
  2717. out:
  2718. spin_unlock_irqrestore(&n->list_lock, flags);
  2719. return count;
  2720. }
  2721. static long validate_slab_cache(struct kmem_cache *s)
  2722. {
  2723. int node;
  2724. unsigned long count = 0;
  2725. unsigned long *map = kmalloc(BITS_TO_LONGS(s->objects) *
  2726. sizeof(unsigned long), GFP_KERNEL);
  2727. if (!map)
  2728. return -ENOMEM;
  2729. flush_all(s);
  2730. for_each_node_state(node, N_NORMAL_MEMORY) {
  2731. struct kmem_cache_node *n = get_node(s, node);
  2732. count += validate_slab_node(s, n, map);
  2733. }
  2734. kfree(map);
  2735. return count;
  2736. }
  2737. #ifdef SLUB_RESILIENCY_TEST
  2738. static void resiliency_test(void)
  2739. {
  2740. u8 *p;
  2741. printk(KERN_ERR "SLUB resiliency testing\n");
  2742. printk(KERN_ERR "-----------------------\n");
  2743. printk(KERN_ERR "A. Corruption after allocation\n");
  2744. p = kzalloc(16, GFP_KERNEL);
  2745. p[16] = 0x12;
  2746. printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
  2747. " 0x12->0x%p\n\n", p + 16);
  2748. validate_slab_cache(kmalloc_caches + 4);
  2749. /* Hmmm... The next two are dangerous */
  2750. p = kzalloc(32, GFP_KERNEL);
  2751. p[32 + sizeof(void *)] = 0x34;
  2752. printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
  2753. " 0x34 -> -0x%p\n", p);
  2754. printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
  2755. validate_slab_cache(kmalloc_caches + 5);
  2756. p = kzalloc(64, GFP_KERNEL);
  2757. p += 64 + (get_cycles() & 0xff) * sizeof(void *);
  2758. *p = 0x56;
  2759. printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
  2760. p);
  2761. printk(KERN_ERR "If allocated object is overwritten then not detectable\n\n");
  2762. validate_slab_cache(kmalloc_caches + 6);
  2763. printk(KERN_ERR "\nB. Corruption after free\n");
  2764. p = kzalloc(128, GFP_KERNEL);
  2765. kfree(p);
  2766. *p = 0x78;
  2767. printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
  2768. validate_slab_cache(kmalloc_caches + 7);
  2769. p = kzalloc(256, GFP_KERNEL);
  2770. kfree(p);
  2771. p[50] = 0x9a;
  2772. printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n", p);
  2773. validate_slab_cache(kmalloc_caches + 8);
  2774. p = kzalloc(512, GFP_KERNEL);
  2775. kfree(p);
  2776. p[512] = 0xab;
  2777. printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
  2778. validate_slab_cache(kmalloc_caches + 9);
  2779. }
  2780. #else
  2781. static void resiliency_test(void) {};
  2782. #endif
  2783. /*
  2784. * Generate lists of code addresses where slabcache objects are allocated
  2785. * and freed.
  2786. */
  2787. struct location {
  2788. unsigned long count;
  2789. void *addr;
  2790. long long sum_time;
  2791. long min_time;
  2792. long max_time;
  2793. long min_pid;
  2794. long max_pid;
  2795. cpumask_t cpus;
  2796. nodemask_t nodes;
  2797. };
  2798. struct loc_track {
  2799. unsigned long max;
  2800. unsigned long count;
  2801. struct location *loc;
  2802. };
  2803. static void free_loc_track(struct loc_track *t)
  2804. {
  2805. if (t->max)
  2806. free_pages((unsigned long)t->loc,
  2807. get_order(sizeof(struct location) * t->max));
  2808. }
  2809. static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
  2810. {
  2811. struct location *l;
  2812. int order;
  2813. order = get_order(sizeof(struct location) * max);
  2814. l = (void *)__get_free_pages(flags, order);
  2815. if (!l)
  2816. return 0;
  2817. if (t->count) {
  2818. memcpy(l, t->loc, sizeof(struct location) * t->count);
  2819. free_loc_track(t);
  2820. }
  2821. t->max = max;
  2822. t->loc = l;
  2823. return 1;
  2824. }
  2825. static int add_location(struct loc_track *t, struct kmem_cache *s,
  2826. const struct track *track)
  2827. {
  2828. long start, end, pos;
  2829. struct location *l;
  2830. void *caddr;
  2831. unsigned long age = jiffies - track->when;
  2832. start = -1;
  2833. end = t->count;
  2834. for ( ; ; ) {
  2835. pos = start + (end - start + 1) / 2;
  2836. /*
  2837. * There is nothing at "end". If we end up there
  2838. * we need to add something to before end.
  2839. */
  2840. if (pos == end)
  2841. break;
  2842. caddr = t->loc[pos].addr;
  2843. if (track->addr == caddr) {
  2844. l = &t->loc[pos];
  2845. l->count++;
  2846. if (track->when) {
  2847. l->sum_time += age;
  2848. if (age < l->min_time)
  2849. l->min_time = age;
  2850. if (age > l->max_time)
  2851. l->max_time = age;
  2852. if (track->pid < l->min_pid)
  2853. l->min_pid = track->pid;
  2854. if (track->pid > l->max_pid)
  2855. l->max_pid = track->pid;
  2856. cpu_set(track->cpu, l->cpus);
  2857. }
  2858. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  2859. return 1;
  2860. }
  2861. if (track->addr < caddr)
  2862. end = pos;
  2863. else
  2864. start = pos;
  2865. }
  2866. /*
  2867. * Not found. Insert new tracking element.
  2868. */
  2869. if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
  2870. return 0;
  2871. l = t->loc + pos;
  2872. if (pos < t->count)
  2873. memmove(l + 1, l,
  2874. (t->count - pos) * sizeof(struct location));
  2875. t->count++;
  2876. l->count = 1;
  2877. l->addr = track->addr;
  2878. l->sum_time = age;
  2879. l->min_time = age;
  2880. l->max_time = age;
  2881. l->min_pid = track->pid;
  2882. l->max_pid = track->pid;
  2883. cpus_clear(l->cpus);
  2884. cpu_set(track->cpu, l->cpus);
  2885. nodes_clear(l->nodes);
  2886. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  2887. return 1;
  2888. }
  2889. static void process_slab(struct loc_track *t, struct kmem_cache *s,
  2890. struct page *page, enum track_item alloc)
  2891. {
  2892. void *addr = slab_address(page);
  2893. DECLARE_BITMAP(map, s->objects);
  2894. void *p;
  2895. bitmap_zero(map, s->objects);
  2896. for_each_free_object(p, s, page->freelist)
  2897. set_bit(slab_index(p, s, addr), map);
  2898. for_each_object(p, s, addr)
  2899. if (!test_bit(slab_index(p, s, addr), map))
  2900. add_location(t, s, get_track(s, p, alloc));
  2901. }
  2902. static int list_locations(struct kmem_cache *s, char *buf,
  2903. enum track_item alloc)
  2904. {
  2905. int len = 0;
  2906. unsigned long i;
  2907. struct loc_track t = { 0, 0, NULL };
  2908. int node;
  2909. if (!alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
  2910. GFP_TEMPORARY))
  2911. return sprintf(buf, "Out of memory\n");
  2912. /* Push back cpu slabs */
  2913. flush_all(s);
  2914. for_each_node_state(node, N_NORMAL_MEMORY) {
  2915. struct kmem_cache_node *n = get_node(s, node);
  2916. unsigned long flags;
  2917. struct page *page;
  2918. if (!atomic_long_read(&n->nr_slabs))
  2919. continue;
  2920. spin_lock_irqsave(&n->list_lock, flags);
  2921. list_for_each_entry(page, &n->partial, lru)
  2922. process_slab(&t, s, page, alloc);
  2923. list_for_each_entry(page, &n->full, lru)
  2924. process_slab(&t, s, page, alloc);
  2925. spin_unlock_irqrestore(&n->list_lock, flags);
  2926. }
  2927. for (i = 0; i < t.count; i++) {
  2928. struct location *l = &t.loc[i];
  2929. if (len > PAGE_SIZE - 100)
  2930. break;
  2931. len += sprintf(buf + len, "%7ld ", l->count);
  2932. if (l->addr)
  2933. len += sprint_symbol(buf + len, (unsigned long)l->addr);
  2934. else
  2935. len += sprintf(buf + len, "<not-available>");
  2936. if (l->sum_time != l->min_time) {
  2937. unsigned long remainder;
  2938. len += sprintf(buf + len, " age=%ld/%ld/%ld",
  2939. l->min_time,
  2940. div_long_long_rem(l->sum_time, l->count, &remainder),
  2941. l->max_time);
  2942. } else
  2943. len += sprintf(buf + len, " age=%ld",
  2944. l->min_time);
  2945. if (l->min_pid != l->max_pid)
  2946. len += sprintf(buf + len, " pid=%ld-%ld",
  2947. l->min_pid, l->max_pid);
  2948. else
  2949. len += sprintf(buf + len, " pid=%ld",
  2950. l->min_pid);
  2951. if (num_online_cpus() > 1 && !cpus_empty(l->cpus) &&
  2952. len < PAGE_SIZE - 60) {
  2953. len += sprintf(buf + len, " cpus=");
  2954. len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
  2955. l->cpus);
  2956. }
  2957. if (num_online_nodes() > 1 && !nodes_empty(l->nodes) &&
  2958. len < PAGE_SIZE - 60) {
  2959. len += sprintf(buf + len, " nodes=");
  2960. len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
  2961. l->nodes);
  2962. }
  2963. len += sprintf(buf + len, "\n");
  2964. }
  2965. free_loc_track(&t);
  2966. if (!t.count)
  2967. len += sprintf(buf, "No data\n");
  2968. return len;
  2969. }
  2970. enum slab_stat_type {
  2971. SL_FULL,
  2972. SL_PARTIAL,
  2973. SL_CPU,
  2974. SL_OBJECTS
  2975. };
  2976. #define SO_FULL (1 << SL_FULL)
  2977. #define SO_PARTIAL (1 << SL_PARTIAL)
  2978. #define SO_CPU (1 << SL_CPU)
  2979. #define SO_OBJECTS (1 << SL_OBJECTS)
  2980. static unsigned long slab_objects(struct kmem_cache *s,
  2981. char *buf, unsigned long flags)
  2982. {
  2983. unsigned long total = 0;
  2984. int cpu;
  2985. int node;
  2986. int x;
  2987. unsigned long *nodes;
  2988. unsigned long *per_cpu;
  2989. nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
  2990. per_cpu = nodes + nr_node_ids;
  2991. for_each_possible_cpu(cpu) {
  2992. struct page *page;
  2993. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  2994. if (!c)
  2995. continue;
  2996. page = c->page;
  2997. node = c->node;
  2998. if (node < 0)
  2999. continue;
  3000. if (page) {
  3001. if (flags & SO_CPU) {
  3002. if (flags & SO_OBJECTS)
  3003. x = page->inuse;
  3004. else
  3005. x = 1;
  3006. total += x;
  3007. nodes[node] += x;
  3008. }
  3009. per_cpu[node]++;
  3010. }
  3011. }
  3012. for_each_node_state(node, N_NORMAL_MEMORY) {
  3013. struct kmem_cache_node *n = get_node(s, node);
  3014. if (flags & SO_PARTIAL) {
  3015. if (flags & SO_OBJECTS)
  3016. x = count_partial(n);
  3017. else
  3018. x = n->nr_partial;
  3019. total += x;
  3020. nodes[node] += x;
  3021. }
  3022. if (flags & SO_FULL) {
  3023. int full_slabs = atomic_long_read(&n->nr_slabs)
  3024. - per_cpu[node]
  3025. - n->nr_partial;
  3026. if (flags & SO_OBJECTS)
  3027. x = full_slabs * s->objects;
  3028. else
  3029. x = full_slabs;
  3030. total += x;
  3031. nodes[node] += x;
  3032. }
  3033. }
  3034. x = sprintf(buf, "%lu", total);
  3035. #ifdef CONFIG_NUMA
  3036. for_each_node_state(node, N_NORMAL_MEMORY)
  3037. if (nodes[node])
  3038. x += sprintf(buf + x, " N%d=%lu",
  3039. node, nodes[node]);
  3040. #endif
  3041. kfree(nodes);
  3042. return x + sprintf(buf + x, "\n");
  3043. }
  3044. static int any_slab_objects(struct kmem_cache *s)
  3045. {
  3046. int node;
  3047. int cpu;
  3048. for_each_possible_cpu(cpu) {
  3049. struct kmem_cache_cpu *c = get_cpu_slab(s, cpu);
  3050. if (c && c->page)
  3051. return 1;
  3052. }
  3053. for_each_online_node(node) {
  3054. struct kmem_cache_node *n = get_node(s, node);
  3055. if (!n)
  3056. continue;
  3057. if (n->nr_partial || atomic_long_read(&n->nr_slabs))
  3058. return 1;
  3059. }
  3060. return 0;
  3061. }
  3062. #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
  3063. #define to_slab(n) container_of(n, struct kmem_cache, kobj);
  3064. struct slab_attribute {
  3065. struct attribute attr;
  3066. ssize_t (*show)(struct kmem_cache *s, char *buf);
  3067. ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
  3068. };
  3069. #define SLAB_ATTR_RO(_name) \
  3070. static struct slab_attribute _name##_attr = __ATTR_RO(_name)
  3071. #define SLAB_ATTR(_name) \
  3072. static struct slab_attribute _name##_attr = \
  3073. __ATTR(_name, 0644, _name##_show, _name##_store)
  3074. static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
  3075. {
  3076. return sprintf(buf, "%d\n", s->size);
  3077. }
  3078. SLAB_ATTR_RO(slab_size);
  3079. static ssize_t align_show(struct kmem_cache *s, char *buf)
  3080. {
  3081. return sprintf(buf, "%d\n", s->align);
  3082. }
  3083. SLAB_ATTR_RO(align);
  3084. static ssize_t object_size_show(struct kmem_cache *s, char *buf)
  3085. {
  3086. return sprintf(buf, "%d\n", s->objsize);
  3087. }
  3088. SLAB_ATTR_RO(object_size);
  3089. static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
  3090. {
  3091. return sprintf(buf, "%d\n", s->objects);
  3092. }
  3093. SLAB_ATTR_RO(objs_per_slab);
  3094. static ssize_t order_show(struct kmem_cache *s, char *buf)
  3095. {
  3096. return sprintf(buf, "%d\n", s->order);
  3097. }
  3098. SLAB_ATTR_RO(order);
  3099. static ssize_t ctor_show(struct kmem_cache *s, char *buf)
  3100. {
  3101. if (s->ctor) {
  3102. int n = sprint_symbol(buf, (unsigned long)s->ctor);
  3103. return n + sprintf(buf + n, "\n");
  3104. }
  3105. return 0;
  3106. }
  3107. SLAB_ATTR_RO(ctor);
  3108. static ssize_t aliases_show(struct kmem_cache *s, char *buf)
  3109. {
  3110. return sprintf(buf, "%d\n", s->refcount - 1);
  3111. }
  3112. SLAB_ATTR_RO(aliases);
  3113. static ssize_t slabs_show(struct kmem_cache *s, char *buf)
  3114. {
  3115. return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU);
  3116. }
  3117. SLAB_ATTR_RO(slabs);
  3118. static ssize_t partial_show(struct kmem_cache *s, char *buf)
  3119. {
  3120. return slab_objects(s, buf, SO_PARTIAL);
  3121. }
  3122. SLAB_ATTR_RO(partial);
  3123. static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
  3124. {
  3125. return slab_objects(s, buf, SO_CPU);
  3126. }
  3127. SLAB_ATTR_RO(cpu_slabs);
  3128. static ssize_t objects_show(struct kmem_cache *s, char *buf)
  3129. {
  3130. return slab_objects(s, buf, SO_FULL|SO_PARTIAL|SO_CPU|SO_OBJECTS);
  3131. }
  3132. SLAB_ATTR_RO(objects);
  3133. static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
  3134. {
  3135. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
  3136. }
  3137. static ssize_t sanity_checks_store(struct kmem_cache *s,
  3138. const char *buf, size_t length)
  3139. {
  3140. s->flags &= ~SLAB_DEBUG_FREE;
  3141. if (buf[0] == '1')
  3142. s->flags |= SLAB_DEBUG_FREE;
  3143. return length;
  3144. }
  3145. SLAB_ATTR(sanity_checks);
  3146. static ssize_t trace_show(struct kmem_cache *s, char *buf)
  3147. {
  3148. return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
  3149. }
  3150. static ssize_t trace_store(struct kmem_cache *s, const char *buf,
  3151. size_t length)
  3152. {
  3153. s->flags &= ~SLAB_TRACE;
  3154. if (buf[0] == '1')
  3155. s->flags |= SLAB_TRACE;
  3156. return length;
  3157. }
  3158. SLAB_ATTR(trace);
  3159. static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
  3160. {
  3161. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
  3162. }
  3163. static ssize_t reclaim_account_store(struct kmem_cache *s,
  3164. const char *buf, size_t length)
  3165. {
  3166. s->flags &= ~SLAB_RECLAIM_ACCOUNT;
  3167. if (buf[0] == '1')
  3168. s->flags |= SLAB_RECLAIM_ACCOUNT;
  3169. return length;
  3170. }
  3171. SLAB_ATTR(reclaim_account);
  3172. static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
  3173. {
  3174. return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
  3175. }
  3176. SLAB_ATTR_RO(hwcache_align);
  3177. #ifdef CONFIG_ZONE_DMA
  3178. static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
  3179. {
  3180. return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
  3181. }
  3182. SLAB_ATTR_RO(cache_dma);
  3183. #endif
  3184. static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
  3185. {
  3186. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
  3187. }
  3188. SLAB_ATTR_RO(destroy_by_rcu);
  3189. static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
  3190. {
  3191. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
  3192. }
  3193. static ssize_t red_zone_store(struct kmem_cache *s,
  3194. const char *buf, size_t length)
  3195. {
  3196. if (any_slab_objects(s))
  3197. return -EBUSY;
  3198. s->flags &= ~SLAB_RED_ZONE;
  3199. if (buf[0] == '1')
  3200. s->flags |= SLAB_RED_ZONE;
  3201. calculate_sizes(s);
  3202. return length;
  3203. }
  3204. SLAB_ATTR(red_zone);
  3205. static ssize_t poison_show(struct kmem_cache *s, char *buf)
  3206. {
  3207. return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
  3208. }
  3209. static ssize_t poison_store(struct kmem_cache *s,
  3210. const char *buf, size_t length)
  3211. {
  3212. if (any_slab_objects(s))
  3213. return -EBUSY;
  3214. s->flags &= ~SLAB_POISON;
  3215. if (buf[0] == '1')
  3216. s->flags |= SLAB_POISON;
  3217. calculate_sizes(s);
  3218. return length;
  3219. }
  3220. SLAB_ATTR(poison);
  3221. static ssize_t store_user_show(struct kmem_cache *s, char *buf)
  3222. {
  3223. return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
  3224. }
  3225. static ssize_t store_user_store(struct kmem_cache *s,
  3226. const char *buf, size_t length)
  3227. {
  3228. if (any_slab_objects(s))
  3229. return -EBUSY;
  3230. s->flags &= ~SLAB_STORE_USER;
  3231. if (buf[0] == '1')
  3232. s->flags |= SLAB_STORE_USER;
  3233. calculate_sizes(s);
  3234. return length;
  3235. }
  3236. SLAB_ATTR(store_user);
  3237. static ssize_t validate_show(struct kmem_cache *s, char *buf)
  3238. {
  3239. return 0;
  3240. }
  3241. static ssize_t validate_store(struct kmem_cache *s,
  3242. const char *buf, size_t length)
  3243. {
  3244. int ret = -EINVAL;
  3245. if (buf[0] == '1') {
  3246. ret = validate_slab_cache(s);
  3247. if (ret >= 0)
  3248. ret = length;
  3249. }
  3250. return ret;
  3251. }
  3252. SLAB_ATTR(validate);
  3253. static ssize_t shrink_show(struct kmem_cache *s, char *buf)
  3254. {
  3255. return 0;
  3256. }
  3257. static ssize_t shrink_store(struct kmem_cache *s,
  3258. const char *buf, size_t length)
  3259. {
  3260. if (buf[0] == '1') {
  3261. int rc = kmem_cache_shrink(s);
  3262. if (rc)
  3263. return rc;
  3264. } else
  3265. return -EINVAL;
  3266. return length;
  3267. }
  3268. SLAB_ATTR(shrink);
  3269. static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
  3270. {
  3271. if (!(s->flags & SLAB_STORE_USER))
  3272. return -ENOSYS;
  3273. return list_locations(s, buf, TRACK_ALLOC);
  3274. }
  3275. SLAB_ATTR_RO(alloc_calls);
  3276. static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
  3277. {
  3278. if (!(s->flags & SLAB_STORE_USER))
  3279. return -ENOSYS;
  3280. return list_locations(s, buf, TRACK_FREE);
  3281. }
  3282. SLAB_ATTR_RO(free_calls);
  3283. #ifdef CONFIG_NUMA
  3284. static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
  3285. {
  3286. return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
  3287. }
  3288. static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
  3289. const char *buf, size_t length)
  3290. {
  3291. int n = simple_strtoul(buf, NULL, 10);
  3292. if (n < 100)
  3293. s->remote_node_defrag_ratio = n * 10;
  3294. return length;
  3295. }
  3296. SLAB_ATTR(remote_node_defrag_ratio);
  3297. #endif
  3298. static struct attribute *slab_attrs[] = {
  3299. &slab_size_attr.attr,
  3300. &object_size_attr.attr,
  3301. &objs_per_slab_attr.attr,
  3302. &order_attr.attr,
  3303. &objects_attr.attr,
  3304. &slabs_attr.attr,
  3305. &partial_attr.attr,
  3306. &cpu_slabs_attr.attr,
  3307. &ctor_attr.attr,
  3308. &aliases_attr.attr,
  3309. &align_attr.attr,
  3310. &sanity_checks_attr.attr,
  3311. &trace_attr.attr,
  3312. &hwcache_align_attr.attr,
  3313. &reclaim_account_attr.attr,
  3314. &destroy_by_rcu_attr.attr,
  3315. &red_zone_attr.attr,
  3316. &poison_attr.attr,
  3317. &store_user_attr.attr,
  3318. &validate_attr.attr,
  3319. &shrink_attr.attr,
  3320. &alloc_calls_attr.attr,
  3321. &free_calls_attr.attr,
  3322. #ifdef CONFIG_ZONE_DMA
  3323. &cache_dma_attr.attr,
  3324. #endif
  3325. #ifdef CONFIG_NUMA
  3326. &remote_node_defrag_ratio_attr.attr,
  3327. #endif
  3328. NULL
  3329. };
  3330. static struct attribute_group slab_attr_group = {
  3331. .attrs = slab_attrs,
  3332. };
  3333. static ssize_t slab_attr_show(struct kobject *kobj,
  3334. struct attribute *attr,
  3335. char *buf)
  3336. {
  3337. struct slab_attribute *attribute;
  3338. struct kmem_cache *s;
  3339. int err;
  3340. attribute = to_slab_attr(attr);
  3341. s = to_slab(kobj);
  3342. if (!attribute->show)
  3343. return -EIO;
  3344. err = attribute->show(s, buf);
  3345. return err;
  3346. }
  3347. static ssize_t slab_attr_store(struct kobject *kobj,
  3348. struct attribute *attr,
  3349. const char *buf, size_t len)
  3350. {
  3351. struct slab_attribute *attribute;
  3352. struct kmem_cache *s;
  3353. int err;
  3354. attribute = to_slab_attr(attr);
  3355. s = to_slab(kobj);
  3356. if (!attribute->store)
  3357. return -EIO;
  3358. err = attribute->store(s, buf, len);
  3359. return err;
  3360. }
  3361. static void kmem_cache_release(struct kobject *kobj)
  3362. {
  3363. struct kmem_cache *s = to_slab(kobj);
  3364. kfree(s);
  3365. }
  3366. static struct sysfs_ops slab_sysfs_ops = {
  3367. .show = slab_attr_show,
  3368. .store = slab_attr_store,
  3369. };
  3370. static struct kobj_type slab_ktype = {
  3371. .sysfs_ops = &slab_sysfs_ops,
  3372. .release = kmem_cache_release
  3373. };
  3374. static int uevent_filter(struct kset *kset, struct kobject *kobj)
  3375. {
  3376. struct kobj_type *ktype = get_ktype(kobj);
  3377. if (ktype == &slab_ktype)
  3378. return 1;
  3379. return 0;
  3380. }
  3381. static struct kset_uevent_ops slab_uevent_ops = {
  3382. .filter = uevent_filter,
  3383. };
  3384. static struct kset *slab_kset;
  3385. #define ID_STR_LENGTH 64
  3386. /* Create a unique string id for a slab cache:
  3387. * format
  3388. * :[flags-]size:[memory address of kmemcache]
  3389. */
  3390. static char *create_unique_id(struct kmem_cache *s)
  3391. {
  3392. char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
  3393. char *p = name;
  3394. BUG_ON(!name);
  3395. *p++ = ':';
  3396. /*
  3397. * First flags affecting slabcache operations. We will only
  3398. * get here for aliasable slabs so we do not need to support
  3399. * too many flags. The flags here must cover all flags that
  3400. * are matched during merging to guarantee that the id is
  3401. * unique.
  3402. */
  3403. if (s->flags & SLAB_CACHE_DMA)
  3404. *p++ = 'd';
  3405. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  3406. *p++ = 'a';
  3407. if (s->flags & SLAB_DEBUG_FREE)
  3408. *p++ = 'F';
  3409. if (p != name + 1)
  3410. *p++ = '-';
  3411. p += sprintf(p, "%07d", s->size);
  3412. BUG_ON(p > name + ID_STR_LENGTH - 1);
  3413. return name;
  3414. }
  3415. static int sysfs_slab_add(struct kmem_cache *s)
  3416. {
  3417. int err;
  3418. const char *name;
  3419. int unmergeable;
  3420. if (slab_state < SYSFS)
  3421. /* Defer until later */
  3422. return 0;
  3423. unmergeable = slab_unmergeable(s);
  3424. if (unmergeable) {
  3425. /*
  3426. * Slabcache can never be merged so we can use the name proper.
  3427. * This is typically the case for debug situations. In that
  3428. * case we can catch duplicate names easily.
  3429. */
  3430. sysfs_remove_link(&slab_kset->kobj, s->name);
  3431. name = s->name;
  3432. } else {
  3433. /*
  3434. * Create a unique name for the slab as a target
  3435. * for the symlinks.
  3436. */
  3437. name = create_unique_id(s);
  3438. }
  3439. s->kobj.kset = slab_kset;
  3440. err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
  3441. if (err) {
  3442. kobject_put(&s->kobj);
  3443. return err;
  3444. }
  3445. err = sysfs_create_group(&s->kobj, &slab_attr_group);
  3446. if (err)
  3447. return err;
  3448. kobject_uevent(&s->kobj, KOBJ_ADD);
  3449. if (!unmergeable) {
  3450. /* Setup first alias */
  3451. sysfs_slab_alias(s, s->name);
  3452. kfree(name);
  3453. }
  3454. return 0;
  3455. }
  3456. static void sysfs_slab_remove(struct kmem_cache *s)
  3457. {
  3458. kobject_uevent(&s->kobj, KOBJ_REMOVE);
  3459. kobject_del(&s->kobj);
  3460. kobject_put(&s->kobj);
  3461. }
  3462. /*
  3463. * Need to buffer aliases during bootup until sysfs becomes
  3464. * available lest we loose that information.
  3465. */
  3466. struct saved_alias {
  3467. struct kmem_cache *s;
  3468. const char *name;
  3469. struct saved_alias *next;
  3470. };
  3471. static struct saved_alias *alias_list;
  3472. static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
  3473. {
  3474. struct saved_alias *al;
  3475. if (slab_state == SYSFS) {
  3476. /*
  3477. * If we have a leftover link then remove it.
  3478. */
  3479. sysfs_remove_link(&slab_kset->kobj, name);
  3480. return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
  3481. }
  3482. al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
  3483. if (!al)
  3484. return -ENOMEM;
  3485. al->s = s;
  3486. al->name = name;
  3487. al->next = alias_list;
  3488. alias_list = al;
  3489. return 0;
  3490. }
  3491. static int __init slab_sysfs_init(void)
  3492. {
  3493. struct kmem_cache *s;
  3494. int err;
  3495. slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
  3496. if (!slab_kset) {
  3497. printk(KERN_ERR "Cannot register slab subsystem.\n");
  3498. return -ENOSYS;
  3499. }
  3500. slab_state = SYSFS;
  3501. list_for_each_entry(s, &slab_caches, list) {
  3502. err = sysfs_slab_add(s);
  3503. if (err)
  3504. printk(KERN_ERR "SLUB: Unable to add boot slab %s"
  3505. " to sysfs\n", s->name);
  3506. }
  3507. while (alias_list) {
  3508. struct saved_alias *al = alias_list;
  3509. alias_list = alias_list->next;
  3510. err = sysfs_slab_alias(al->s, al->name);
  3511. if (err)
  3512. printk(KERN_ERR "SLUB: Unable to add boot slab alias"
  3513. " %s to sysfs\n", s->name);
  3514. kfree(al);
  3515. }
  3516. resiliency_test();
  3517. return 0;
  3518. }
  3519. __initcall(slab_sysfs_init);
  3520. #endif
  3521. /*
  3522. * The /proc/slabinfo ABI
  3523. */
  3524. #ifdef CONFIG_SLABINFO
  3525. ssize_t slabinfo_write(struct file *file, const char __user * buffer,
  3526. size_t count, loff_t *ppos)
  3527. {
  3528. return -EINVAL;
  3529. }
  3530. static void print_slabinfo_header(struct seq_file *m)
  3531. {
  3532. seq_puts(m, "slabinfo - version: 2.1\n");
  3533. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  3534. "<objperslab> <pagesperslab>");
  3535. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  3536. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  3537. seq_putc(m, '\n');
  3538. }
  3539. static void *s_start(struct seq_file *m, loff_t *pos)
  3540. {
  3541. loff_t n = *pos;
  3542. down_read(&slub_lock);
  3543. if (!n)
  3544. print_slabinfo_header(m);
  3545. return seq_list_start(&slab_caches, *pos);
  3546. }
  3547. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  3548. {
  3549. return seq_list_next(p, &slab_caches, pos);
  3550. }
  3551. static void s_stop(struct seq_file *m, void *p)
  3552. {
  3553. up_read(&slub_lock);
  3554. }
  3555. static int s_show(struct seq_file *m, void *p)
  3556. {
  3557. unsigned long nr_partials = 0;
  3558. unsigned long nr_slabs = 0;
  3559. unsigned long nr_inuse = 0;
  3560. unsigned long nr_objs;
  3561. struct kmem_cache *s;
  3562. int node;
  3563. s = list_entry(p, struct kmem_cache, list);
  3564. for_each_online_node(node) {
  3565. struct kmem_cache_node *n = get_node(s, node);
  3566. if (!n)
  3567. continue;
  3568. nr_partials += n->nr_partial;
  3569. nr_slabs += atomic_long_read(&n->nr_slabs);
  3570. nr_inuse += count_partial(n);
  3571. }
  3572. nr_objs = nr_slabs * s->objects;
  3573. nr_inuse += (nr_slabs - nr_partials) * s->objects;
  3574. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
  3575. nr_objs, s->size, s->objects, (1 << s->order));
  3576. seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
  3577. seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
  3578. 0UL);
  3579. seq_putc(m, '\n');
  3580. return 0;
  3581. }
  3582. const struct seq_operations slabinfo_op = {
  3583. .start = s_start,
  3584. .next = s_next,
  3585. .stop = s_stop,
  3586. .show = s_show,
  3587. };
  3588. #endif /* CONFIG_SLABINFO */