slub.c 116 KB

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