slub.c 107 KB

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