slub.c 105 KB

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