slub.c 109 KB

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