slub.c 112 KB

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