slub.c 108 KB

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