slub.c 105 KB

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