slub.c 116 KB

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