slub.c 109 KB

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