slub.c 111 KB

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