slub.c 105 KB

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