slub.c 109 KB

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