slub.c 105 KB

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