slub.c 110 KB

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