slub.c 113 KB

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