slub.c 109 KB

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