slub.c 116 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950
  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 *new;
  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. if (!c->page)
  1545. goto new_slab;
  1546. slab_lock(c->page);
  1547. if (unlikely(!node_match(c, node)))
  1548. goto another_slab;
  1549. stat(s, ALLOC_REFILL);
  1550. load_freelist:
  1551. object = c->page->freelist;
  1552. if (unlikely(!object))
  1553. goto another_slab;
  1554. if (kmem_cache_debug(s))
  1555. goto debug;
  1556. c->freelist = get_freepointer(s, object);
  1557. c->page->inuse = c->page->objects;
  1558. c->page->freelist = NULL;
  1559. c->node = page_to_nid(c->page);
  1560. unlock_out:
  1561. slab_unlock(c->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. new = get_partial(s, gfpflags, node);
  1572. if (new) {
  1573. c->page = new;
  1574. stat(s, ALLOC_FROM_PARTIAL);
  1575. goto load_freelist;
  1576. }
  1577. gfpflags &= gfp_allowed_mask;
  1578. if (gfpflags & __GFP_WAIT)
  1579. local_irq_enable();
  1580. new = new_slab(s, gfpflags, node);
  1581. if (gfpflags & __GFP_WAIT)
  1582. local_irq_disable();
  1583. if (new) {
  1584. c = __this_cpu_ptr(s->cpu_slab);
  1585. stat(s, ALLOC_SLAB);
  1586. if (c->page)
  1587. flush_slab(s, c);
  1588. slab_lock(new);
  1589. __SetPageSlubFrozen(new);
  1590. c->page = new;
  1591. goto load_freelist;
  1592. }
  1593. if (!(gfpflags & __GFP_NOWARN) && printk_ratelimit())
  1594. slab_out_of_memory(s, gfpflags, node);
  1595. #ifdef CONFIG_CMPXCHG_LOCAL
  1596. local_irq_restore(flags);
  1597. #endif
  1598. return NULL;
  1599. debug:
  1600. if (!alloc_debug_processing(s, c->page, object, addr))
  1601. goto another_slab;
  1602. c->page->inuse++;
  1603. c->page->freelist = get_freepointer(s, object);
  1604. c->node = NUMA_NO_NODE;
  1605. goto unlock_out;
  1606. }
  1607. /*
  1608. * Inlined fastpath so that allocation functions (kmalloc, kmem_cache_alloc)
  1609. * have the fastpath folded into their functions. So no function call
  1610. * overhead for requests that can be satisfied on the fastpath.
  1611. *
  1612. * The fastpath works by first checking if the lockless freelist can be used.
  1613. * If not then __slab_alloc is called for slow processing.
  1614. *
  1615. * Otherwise we can simply pick the next object from the lockless free list.
  1616. */
  1617. static __always_inline void *slab_alloc(struct kmem_cache *s,
  1618. gfp_t gfpflags, int node, unsigned long addr)
  1619. {
  1620. void **object;
  1621. struct kmem_cache_cpu *c;
  1622. #ifdef CONFIG_CMPXCHG_LOCAL
  1623. unsigned long tid;
  1624. #else
  1625. unsigned long flags;
  1626. #endif
  1627. if (slab_pre_alloc_hook(s, gfpflags))
  1628. return NULL;
  1629. #ifndef CONFIG_CMPXCHG_LOCAL
  1630. local_irq_save(flags);
  1631. #else
  1632. redo:
  1633. #endif
  1634. /*
  1635. * Must read kmem_cache cpu data via this cpu ptr. Preemption is
  1636. * enabled. We may switch back and forth between cpus while
  1637. * reading from one cpu area. That does not matter as long
  1638. * as we end up on the original cpu again when doing the cmpxchg.
  1639. */
  1640. c = __this_cpu_ptr(s->cpu_slab);
  1641. #ifdef CONFIG_CMPXCHG_LOCAL
  1642. /*
  1643. * The transaction ids are globally unique per cpu and per operation on
  1644. * a per cpu queue. Thus they can be guarantee that the cmpxchg_double
  1645. * occurs on the right processor and that there was no operation on the
  1646. * linked list in between.
  1647. */
  1648. tid = c->tid;
  1649. barrier();
  1650. #endif
  1651. object = c->freelist;
  1652. if (unlikely(!object || !node_match(c, node)))
  1653. object = __slab_alloc(s, gfpflags, node, addr, c);
  1654. else {
  1655. #ifdef CONFIG_CMPXCHG_LOCAL
  1656. /*
  1657. * The cmpxchg will only match if there was no additonal
  1658. * operation and if we are on the right processor.
  1659. *
  1660. * The cmpxchg does the following atomically (without lock semantics!)
  1661. * 1. Relocate first pointer to the current per cpu area.
  1662. * 2. Verify that tid and freelist have not been changed
  1663. * 3. If they were not changed replace tid and freelist
  1664. *
  1665. * Since this is without lock semantics the protection is only against
  1666. * code executing on this cpu *not* from access by other cpus.
  1667. */
  1668. if (unlikely(!this_cpu_cmpxchg_double(
  1669. s->cpu_slab->freelist, s->cpu_slab->tid,
  1670. object, tid,
  1671. get_freepointer(s, object), next_tid(tid)))) {
  1672. note_cmpxchg_failure("slab_alloc", s, tid);
  1673. goto redo;
  1674. }
  1675. #else
  1676. c->freelist = get_freepointer(s, object);
  1677. #endif
  1678. stat(s, ALLOC_FASTPATH);
  1679. }
  1680. #ifndef CONFIG_CMPXCHG_LOCAL
  1681. local_irq_restore(flags);
  1682. #endif
  1683. if (unlikely(gfpflags & __GFP_ZERO) && object)
  1684. memset(object, 0, s->objsize);
  1685. slab_post_alloc_hook(s, gfpflags, object);
  1686. return object;
  1687. }
  1688. void *kmem_cache_alloc(struct kmem_cache *s, gfp_t gfpflags)
  1689. {
  1690. void *ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
  1691. trace_kmem_cache_alloc(_RET_IP_, ret, s->objsize, s->size, gfpflags);
  1692. return ret;
  1693. }
  1694. EXPORT_SYMBOL(kmem_cache_alloc);
  1695. #ifdef CONFIG_TRACING
  1696. void *kmem_cache_alloc_trace(struct kmem_cache *s, gfp_t gfpflags, size_t size)
  1697. {
  1698. void *ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, _RET_IP_);
  1699. trace_kmalloc(_RET_IP_, ret, size, s->size, gfpflags);
  1700. return ret;
  1701. }
  1702. EXPORT_SYMBOL(kmem_cache_alloc_trace);
  1703. void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
  1704. {
  1705. void *ret = kmalloc_order(size, flags, order);
  1706. trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
  1707. return ret;
  1708. }
  1709. EXPORT_SYMBOL(kmalloc_order_trace);
  1710. #endif
  1711. #ifdef CONFIG_NUMA
  1712. void *kmem_cache_alloc_node(struct kmem_cache *s, gfp_t gfpflags, int node)
  1713. {
  1714. void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
  1715. trace_kmem_cache_alloc_node(_RET_IP_, ret,
  1716. s->objsize, s->size, gfpflags, node);
  1717. return ret;
  1718. }
  1719. EXPORT_SYMBOL(kmem_cache_alloc_node);
  1720. #ifdef CONFIG_TRACING
  1721. void *kmem_cache_alloc_node_trace(struct kmem_cache *s,
  1722. gfp_t gfpflags,
  1723. int node, size_t size)
  1724. {
  1725. void *ret = slab_alloc(s, gfpflags, node, _RET_IP_);
  1726. trace_kmalloc_node(_RET_IP_, ret,
  1727. size, s->size, gfpflags, node);
  1728. return ret;
  1729. }
  1730. EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
  1731. #endif
  1732. #endif
  1733. /*
  1734. * Slow patch handling. This may still be called frequently since objects
  1735. * have a longer lifetime than the cpu slabs in most processing loads.
  1736. *
  1737. * So we still attempt to reduce cache line usage. Just take the slab
  1738. * lock and free the item. If there is no additional partial page
  1739. * handling required then we can return immediately.
  1740. */
  1741. static void __slab_free(struct kmem_cache *s, struct page *page,
  1742. void *x, unsigned long addr)
  1743. {
  1744. void *prior;
  1745. void **object = (void *)x;
  1746. #ifdef CONFIG_CMPXCHG_LOCAL
  1747. unsigned long flags;
  1748. local_irq_save(flags);
  1749. #endif
  1750. slab_lock(page);
  1751. stat(s, FREE_SLOWPATH);
  1752. if (kmem_cache_debug(s))
  1753. goto debug;
  1754. checks_ok:
  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. return;
  1794. debug:
  1795. if (!free_debug_processing(s, page, x, addr))
  1796. goto out_unlock;
  1797. goto checks_ok;
  1798. }
  1799. /*
  1800. * Fastpath with forced inlining to produce a kfree and kmem_cache_free that
  1801. * can perform fastpath freeing without additional function calls.
  1802. *
  1803. * The fastpath is only possible if we are freeing to the current cpu slab
  1804. * of this processor. This typically the case if we have just allocated
  1805. * the item before.
  1806. *
  1807. * If fastpath is not possible then fall back to __slab_free where we deal
  1808. * with all sorts of special processing.
  1809. */
  1810. static __always_inline void slab_free(struct kmem_cache *s,
  1811. struct page *page, void *x, unsigned long addr)
  1812. {
  1813. void **object = (void *)x;
  1814. struct kmem_cache_cpu *c;
  1815. #ifdef CONFIG_CMPXCHG_LOCAL
  1816. unsigned long tid;
  1817. #else
  1818. unsigned long flags;
  1819. #endif
  1820. slab_free_hook(s, x);
  1821. #ifndef CONFIG_CMPXCHG_LOCAL
  1822. local_irq_save(flags);
  1823. #else
  1824. redo:
  1825. #endif
  1826. /*
  1827. * Determine the currently cpus per cpu slab.
  1828. * The cpu may change afterward. However that does not matter since
  1829. * data is retrieved via this pointer. If we are on the same cpu
  1830. * during the cmpxchg then the free will succedd.
  1831. */
  1832. c = __this_cpu_ptr(s->cpu_slab);
  1833. #ifdef CONFIG_CMPXCHG_LOCAL
  1834. tid = c->tid;
  1835. barrier();
  1836. #endif
  1837. if (likely(page == c->page && c->node != NUMA_NO_NODE)) {
  1838. set_freepointer(s, object, c->freelist);
  1839. #ifdef CONFIG_CMPXCHG_LOCAL
  1840. if (unlikely(!this_cpu_cmpxchg_double(
  1841. s->cpu_slab->freelist, s->cpu_slab->tid,
  1842. c->freelist, tid,
  1843. object, next_tid(tid)))) {
  1844. note_cmpxchg_failure("slab_free", s, tid);
  1845. goto redo;
  1846. }
  1847. #else
  1848. c->freelist = object;
  1849. #endif
  1850. stat(s, FREE_FASTPATH);
  1851. } else
  1852. __slab_free(s, page, x, addr);
  1853. #ifndef CONFIG_CMPXCHG_LOCAL
  1854. local_irq_restore(flags);
  1855. #endif
  1856. }
  1857. void kmem_cache_free(struct kmem_cache *s, void *x)
  1858. {
  1859. struct page *page;
  1860. page = virt_to_head_page(x);
  1861. slab_free(s, page, x, _RET_IP_);
  1862. trace_kmem_cache_free(_RET_IP_, x);
  1863. }
  1864. EXPORT_SYMBOL(kmem_cache_free);
  1865. /*
  1866. * Object placement in a slab is made very easy because we always start at
  1867. * offset 0. If we tune the size of the object to the alignment then we can
  1868. * get the required alignment by putting one properly sized object after
  1869. * another.
  1870. *
  1871. * Notice that the allocation order determines the sizes of the per cpu
  1872. * caches. Each processor has always one slab available for allocations.
  1873. * Increasing the allocation order reduces the number of times that slabs
  1874. * must be moved on and off the partial lists and is therefore a factor in
  1875. * locking overhead.
  1876. */
  1877. /*
  1878. * Mininum / Maximum order of slab pages. This influences locking overhead
  1879. * and slab fragmentation. A higher order reduces the number of partial slabs
  1880. * and increases the number of allocations possible without having to
  1881. * take the list_lock.
  1882. */
  1883. static int slub_min_order;
  1884. static int slub_max_order = PAGE_ALLOC_COSTLY_ORDER;
  1885. static int slub_min_objects;
  1886. /*
  1887. * Merge control. If this is set then no merging of slab caches will occur.
  1888. * (Could be removed. This was introduced to pacify the merge skeptics.)
  1889. */
  1890. static int slub_nomerge;
  1891. /*
  1892. * Calculate the order of allocation given an slab object size.
  1893. *
  1894. * The order of allocation has significant impact on performance and other
  1895. * system components. Generally order 0 allocations should be preferred since
  1896. * order 0 does not cause fragmentation in the page allocator. Larger objects
  1897. * be problematic to put into order 0 slabs because there may be too much
  1898. * unused space left. We go to a higher order if more than 1/16th of the slab
  1899. * would be wasted.
  1900. *
  1901. * In order to reach satisfactory performance we must ensure that a minimum
  1902. * number of objects is in one slab. Otherwise we may generate too much
  1903. * activity on the partial lists which requires taking the list_lock. This is
  1904. * less a concern for large slabs though which are rarely used.
  1905. *
  1906. * slub_max_order specifies the order where we begin to stop considering the
  1907. * number of objects in a slab as critical. If we reach slub_max_order then
  1908. * we try to keep the page order as low as possible. So we accept more waste
  1909. * of space in favor of a small page order.
  1910. *
  1911. * Higher order allocations also allow the placement of more objects in a
  1912. * slab and thereby reduce object handling overhead. If the user has
  1913. * requested a higher mininum order then we start with that one instead of
  1914. * the smallest order which will fit the object.
  1915. */
  1916. static inline int slab_order(int size, int min_objects,
  1917. int max_order, int fract_leftover, int reserved)
  1918. {
  1919. int order;
  1920. int rem;
  1921. int min_order = slub_min_order;
  1922. if (order_objects(min_order, size, reserved) > MAX_OBJS_PER_PAGE)
  1923. return get_order(size * MAX_OBJS_PER_PAGE) - 1;
  1924. for (order = max(min_order,
  1925. fls(min_objects * size - 1) - PAGE_SHIFT);
  1926. order <= max_order; order++) {
  1927. unsigned long slab_size = PAGE_SIZE << order;
  1928. if (slab_size < min_objects * size + reserved)
  1929. continue;
  1930. rem = (slab_size - reserved) % size;
  1931. if (rem <= slab_size / fract_leftover)
  1932. break;
  1933. }
  1934. return order;
  1935. }
  1936. static inline int calculate_order(int size, int reserved)
  1937. {
  1938. int order;
  1939. int min_objects;
  1940. int fraction;
  1941. int max_objects;
  1942. /*
  1943. * Attempt to find best configuration for a slab. This
  1944. * works by first attempting to generate a layout with
  1945. * the best configuration and backing off gradually.
  1946. *
  1947. * First we reduce the acceptable waste in a slab. Then
  1948. * we reduce the minimum objects required in a slab.
  1949. */
  1950. min_objects = slub_min_objects;
  1951. if (!min_objects)
  1952. min_objects = 4 * (fls(nr_cpu_ids) + 1);
  1953. max_objects = order_objects(slub_max_order, size, reserved);
  1954. min_objects = min(min_objects, max_objects);
  1955. while (min_objects > 1) {
  1956. fraction = 16;
  1957. while (fraction >= 4) {
  1958. order = slab_order(size, min_objects,
  1959. slub_max_order, fraction, reserved);
  1960. if (order <= slub_max_order)
  1961. return order;
  1962. fraction /= 2;
  1963. }
  1964. min_objects--;
  1965. }
  1966. /*
  1967. * We were unable to place multiple objects in a slab. Now
  1968. * lets see if we can place a single object there.
  1969. */
  1970. order = slab_order(size, 1, slub_max_order, 1, reserved);
  1971. if (order <= slub_max_order)
  1972. return order;
  1973. /*
  1974. * Doh this slab cannot be placed using slub_max_order.
  1975. */
  1976. order = slab_order(size, 1, MAX_ORDER, 1, reserved);
  1977. if (order < MAX_ORDER)
  1978. return order;
  1979. return -ENOSYS;
  1980. }
  1981. /*
  1982. * Figure out what the alignment of the objects will be.
  1983. */
  1984. static unsigned long calculate_alignment(unsigned long flags,
  1985. unsigned long align, unsigned long size)
  1986. {
  1987. /*
  1988. * If the user wants hardware cache aligned objects then follow that
  1989. * suggestion if the object is sufficiently large.
  1990. *
  1991. * The hardware cache alignment cannot override the specified
  1992. * alignment though. If that is greater then use it.
  1993. */
  1994. if (flags & SLAB_HWCACHE_ALIGN) {
  1995. unsigned long ralign = cache_line_size();
  1996. while (size <= ralign / 2)
  1997. ralign /= 2;
  1998. align = max(align, ralign);
  1999. }
  2000. if (align < ARCH_SLAB_MINALIGN)
  2001. align = ARCH_SLAB_MINALIGN;
  2002. return ALIGN(align, sizeof(void *));
  2003. }
  2004. static void
  2005. init_kmem_cache_node(struct kmem_cache_node *n, struct kmem_cache *s)
  2006. {
  2007. n->nr_partial = 0;
  2008. spin_lock_init(&n->list_lock);
  2009. INIT_LIST_HEAD(&n->partial);
  2010. #ifdef CONFIG_SLUB_DEBUG
  2011. atomic_long_set(&n->nr_slabs, 0);
  2012. atomic_long_set(&n->total_objects, 0);
  2013. INIT_LIST_HEAD(&n->full);
  2014. #endif
  2015. }
  2016. static inline int alloc_kmem_cache_cpus(struct kmem_cache *s)
  2017. {
  2018. BUILD_BUG_ON(PERCPU_DYNAMIC_EARLY_SIZE <
  2019. SLUB_PAGE_SHIFT * sizeof(struct kmem_cache_cpu));
  2020. #ifdef CONFIG_CMPXCHG_LOCAL
  2021. /*
  2022. * Must align to double word boundary for the double cmpxchg instructions
  2023. * to work.
  2024. */
  2025. s->cpu_slab = __alloc_percpu(sizeof(struct kmem_cache_cpu), 2 * sizeof(void *));
  2026. #else
  2027. /* Regular alignment is sufficient */
  2028. s->cpu_slab = alloc_percpu(struct kmem_cache_cpu);
  2029. #endif
  2030. if (!s->cpu_slab)
  2031. return 0;
  2032. init_kmem_cache_cpus(s);
  2033. return 1;
  2034. }
  2035. static struct kmem_cache *kmem_cache_node;
  2036. /*
  2037. * No kmalloc_node yet so do it by hand. We know that this is the first
  2038. * slab on the node for this slabcache. There are no concurrent accesses
  2039. * possible.
  2040. *
  2041. * Note that this function only works on the kmalloc_node_cache
  2042. * when allocating for the kmalloc_node_cache. This is used for bootstrapping
  2043. * memory on a fresh node that has no slab structures yet.
  2044. */
  2045. static void early_kmem_cache_node_alloc(int node)
  2046. {
  2047. struct page *page;
  2048. struct kmem_cache_node *n;
  2049. unsigned long flags;
  2050. BUG_ON(kmem_cache_node->size < sizeof(struct kmem_cache_node));
  2051. page = new_slab(kmem_cache_node, GFP_NOWAIT, node);
  2052. BUG_ON(!page);
  2053. if (page_to_nid(page) != node) {
  2054. printk(KERN_ERR "SLUB: Unable to allocate memory from "
  2055. "node %d\n", node);
  2056. printk(KERN_ERR "SLUB: Allocating a useless per node structure "
  2057. "in order to be able to continue\n");
  2058. }
  2059. n = page->freelist;
  2060. BUG_ON(!n);
  2061. page->freelist = get_freepointer(kmem_cache_node, n);
  2062. page->inuse++;
  2063. kmem_cache_node->node[node] = n;
  2064. #ifdef CONFIG_SLUB_DEBUG
  2065. init_object(kmem_cache_node, n, SLUB_RED_ACTIVE);
  2066. init_tracking(kmem_cache_node, n);
  2067. #endif
  2068. init_kmem_cache_node(n, kmem_cache_node);
  2069. inc_slabs_node(kmem_cache_node, node, page->objects);
  2070. /*
  2071. * lockdep requires consistent irq usage for each lock
  2072. * so even though there cannot be a race this early in
  2073. * the boot sequence, we still disable irqs.
  2074. */
  2075. local_irq_save(flags);
  2076. add_partial(n, page, 0);
  2077. local_irq_restore(flags);
  2078. }
  2079. static void free_kmem_cache_nodes(struct kmem_cache *s)
  2080. {
  2081. int node;
  2082. for_each_node_state(node, N_NORMAL_MEMORY) {
  2083. struct kmem_cache_node *n = s->node[node];
  2084. if (n)
  2085. kmem_cache_free(kmem_cache_node, n);
  2086. s->node[node] = NULL;
  2087. }
  2088. }
  2089. static int init_kmem_cache_nodes(struct kmem_cache *s)
  2090. {
  2091. int node;
  2092. for_each_node_state(node, N_NORMAL_MEMORY) {
  2093. struct kmem_cache_node *n;
  2094. if (slab_state == DOWN) {
  2095. early_kmem_cache_node_alloc(node);
  2096. continue;
  2097. }
  2098. n = kmem_cache_alloc_node(kmem_cache_node,
  2099. GFP_KERNEL, node);
  2100. if (!n) {
  2101. free_kmem_cache_nodes(s);
  2102. return 0;
  2103. }
  2104. s->node[node] = n;
  2105. init_kmem_cache_node(n, s);
  2106. }
  2107. return 1;
  2108. }
  2109. static void set_min_partial(struct kmem_cache *s, unsigned long min)
  2110. {
  2111. if (min < MIN_PARTIAL)
  2112. min = MIN_PARTIAL;
  2113. else if (min > MAX_PARTIAL)
  2114. min = MAX_PARTIAL;
  2115. s->min_partial = min;
  2116. }
  2117. /*
  2118. * calculate_sizes() determines the order and the distribution of data within
  2119. * a slab object.
  2120. */
  2121. static int calculate_sizes(struct kmem_cache *s, int forced_order)
  2122. {
  2123. unsigned long flags = s->flags;
  2124. unsigned long size = s->objsize;
  2125. unsigned long align = s->align;
  2126. int order;
  2127. /*
  2128. * Round up object size to the next word boundary. We can only
  2129. * place the free pointer at word boundaries and this determines
  2130. * the possible location of the free pointer.
  2131. */
  2132. size = ALIGN(size, sizeof(void *));
  2133. #ifdef CONFIG_SLUB_DEBUG
  2134. /*
  2135. * Determine if we can poison the object itself. If the user of
  2136. * the slab may touch the object after free or before allocation
  2137. * then we should never poison the object itself.
  2138. */
  2139. if ((flags & SLAB_POISON) && !(flags & SLAB_DESTROY_BY_RCU) &&
  2140. !s->ctor)
  2141. s->flags |= __OBJECT_POISON;
  2142. else
  2143. s->flags &= ~__OBJECT_POISON;
  2144. /*
  2145. * If we are Redzoning then check if there is some space between the
  2146. * end of the object and the free pointer. If not then add an
  2147. * additional word to have some bytes to store Redzone information.
  2148. */
  2149. if ((flags & SLAB_RED_ZONE) && size == s->objsize)
  2150. size += sizeof(void *);
  2151. #endif
  2152. /*
  2153. * With that we have determined the number of bytes in actual use
  2154. * by the object. This is the potential offset to the free pointer.
  2155. */
  2156. s->inuse = size;
  2157. if (((flags & (SLAB_DESTROY_BY_RCU | SLAB_POISON)) ||
  2158. s->ctor)) {
  2159. /*
  2160. * Relocate free pointer after the object if it is not
  2161. * permitted to overwrite the first word of the object on
  2162. * kmem_cache_free.
  2163. *
  2164. * This is the case if we do RCU, have a constructor or
  2165. * destructor or are poisoning the objects.
  2166. */
  2167. s->offset = size;
  2168. size += sizeof(void *);
  2169. }
  2170. #ifdef CONFIG_SLUB_DEBUG
  2171. if (flags & SLAB_STORE_USER)
  2172. /*
  2173. * Need to store information about allocs and frees after
  2174. * the object.
  2175. */
  2176. size += 2 * sizeof(struct track);
  2177. if (flags & SLAB_RED_ZONE)
  2178. /*
  2179. * Add some empty padding so that we can catch
  2180. * overwrites from earlier objects rather than let
  2181. * tracking information or the free pointer be
  2182. * corrupted if a user writes before the start
  2183. * of the object.
  2184. */
  2185. size += sizeof(void *);
  2186. #endif
  2187. /*
  2188. * Determine the alignment based on various parameters that the
  2189. * user specified and the dynamic determination of cache line size
  2190. * on bootup.
  2191. */
  2192. align = calculate_alignment(flags, align, s->objsize);
  2193. s->align = align;
  2194. /*
  2195. * SLUB stores one object immediately after another beginning from
  2196. * offset 0. In order to align the objects we have to simply size
  2197. * each object to conform to the alignment.
  2198. */
  2199. size = ALIGN(size, align);
  2200. s->size = size;
  2201. if (forced_order >= 0)
  2202. order = forced_order;
  2203. else
  2204. order = calculate_order(size, s->reserved);
  2205. if (order < 0)
  2206. return 0;
  2207. s->allocflags = 0;
  2208. if (order)
  2209. s->allocflags |= __GFP_COMP;
  2210. if (s->flags & SLAB_CACHE_DMA)
  2211. s->allocflags |= SLUB_DMA;
  2212. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  2213. s->allocflags |= __GFP_RECLAIMABLE;
  2214. /*
  2215. * Determine the number of objects per slab
  2216. */
  2217. s->oo = oo_make(order, size, s->reserved);
  2218. s->min = oo_make(get_order(size), size, s->reserved);
  2219. if (oo_objects(s->oo) > oo_objects(s->max))
  2220. s->max = s->oo;
  2221. return !!oo_objects(s->oo);
  2222. }
  2223. static int kmem_cache_open(struct kmem_cache *s,
  2224. const char *name, size_t size,
  2225. size_t align, unsigned long flags,
  2226. void (*ctor)(void *))
  2227. {
  2228. memset(s, 0, kmem_size);
  2229. s->name = name;
  2230. s->ctor = ctor;
  2231. s->objsize = size;
  2232. s->align = align;
  2233. s->flags = kmem_cache_flags(size, flags, name, ctor);
  2234. s->reserved = 0;
  2235. if (need_reserve_slab_rcu && (s->flags & SLAB_DESTROY_BY_RCU))
  2236. s->reserved = sizeof(struct rcu_head);
  2237. if (!calculate_sizes(s, -1))
  2238. goto error;
  2239. if (disable_higher_order_debug) {
  2240. /*
  2241. * Disable debugging flags that store metadata if the min slab
  2242. * order increased.
  2243. */
  2244. if (get_order(s->size) > get_order(s->objsize)) {
  2245. s->flags &= ~DEBUG_METADATA_FLAGS;
  2246. s->offset = 0;
  2247. if (!calculate_sizes(s, -1))
  2248. goto error;
  2249. }
  2250. }
  2251. /*
  2252. * The larger the object size is, the more pages we want on the partial
  2253. * list to avoid pounding the page allocator excessively.
  2254. */
  2255. set_min_partial(s, ilog2(s->size));
  2256. s->refcount = 1;
  2257. #ifdef CONFIG_NUMA
  2258. s->remote_node_defrag_ratio = 1000;
  2259. #endif
  2260. if (!init_kmem_cache_nodes(s))
  2261. goto error;
  2262. if (alloc_kmem_cache_cpus(s))
  2263. return 1;
  2264. free_kmem_cache_nodes(s);
  2265. error:
  2266. if (flags & SLAB_PANIC)
  2267. panic("Cannot create slab %s size=%lu realsize=%u "
  2268. "order=%u offset=%u flags=%lx\n",
  2269. s->name, (unsigned long)size, s->size, oo_order(s->oo),
  2270. s->offset, flags);
  2271. return 0;
  2272. }
  2273. /*
  2274. * Determine the size of a slab object
  2275. */
  2276. unsigned int kmem_cache_size(struct kmem_cache *s)
  2277. {
  2278. return s->objsize;
  2279. }
  2280. EXPORT_SYMBOL(kmem_cache_size);
  2281. static void list_slab_objects(struct kmem_cache *s, struct page *page,
  2282. const char *text)
  2283. {
  2284. #ifdef CONFIG_SLUB_DEBUG
  2285. void *addr = page_address(page);
  2286. void *p;
  2287. unsigned long *map = kzalloc(BITS_TO_LONGS(page->objects) *
  2288. sizeof(long), GFP_ATOMIC);
  2289. if (!map)
  2290. return;
  2291. slab_err(s, page, "%s", text);
  2292. slab_lock(page);
  2293. get_map(s, page, map);
  2294. for_each_object(p, s, addr, page->objects) {
  2295. if (!test_bit(slab_index(p, s, addr), map)) {
  2296. printk(KERN_ERR "INFO: Object 0x%p @offset=%tu\n",
  2297. p, p - addr);
  2298. print_tracking(s, p);
  2299. }
  2300. }
  2301. slab_unlock(page);
  2302. kfree(map);
  2303. #endif
  2304. }
  2305. /*
  2306. * Attempt to free all partial slabs on a node.
  2307. */
  2308. static void free_partial(struct kmem_cache *s, struct kmem_cache_node *n)
  2309. {
  2310. unsigned long flags;
  2311. struct page *page, *h;
  2312. spin_lock_irqsave(&n->list_lock, flags);
  2313. list_for_each_entry_safe(page, h, &n->partial, lru) {
  2314. if (!page->inuse) {
  2315. __remove_partial(n, page);
  2316. discard_slab(s, page);
  2317. } else {
  2318. list_slab_objects(s, page,
  2319. "Objects remaining on kmem_cache_close()");
  2320. }
  2321. }
  2322. spin_unlock_irqrestore(&n->list_lock, flags);
  2323. }
  2324. /*
  2325. * Release all resources used by a slab cache.
  2326. */
  2327. static inline int kmem_cache_close(struct kmem_cache *s)
  2328. {
  2329. int node;
  2330. flush_all(s);
  2331. free_percpu(s->cpu_slab);
  2332. /* Attempt to free all objects */
  2333. for_each_node_state(node, N_NORMAL_MEMORY) {
  2334. struct kmem_cache_node *n = get_node(s, node);
  2335. free_partial(s, n);
  2336. if (n->nr_partial || slabs_node(s, node))
  2337. return 1;
  2338. }
  2339. free_kmem_cache_nodes(s);
  2340. return 0;
  2341. }
  2342. /*
  2343. * Close a cache and release the kmem_cache structure
  2344. * (must be used for caches created using kmem_cache_create)
  2345. */
  2346. void kmem_cache_destroy(struct kmem_cache *s)
  2347. {
  2348. down_write(&slub_lock);
  2349. s->refcount--;
  2350. if (!s->refcount) {
  2351. list_del(&s->list);
  2352. if (kmem_cache_close(s)) {
  2353. printk(KERN_ERR "SLUB %s: %s called for cache that "
  2354. "still has objects.\n", s->name, __func__);
  2355. dump_stack();
  2356. }
  2357. if (s->flags & SLAB_DESTROY_BY_RCU)
  2358. rcu_barrier();
  2359. sysfs_slab_remove(s);
  2360. }
  2361. up_write(&slub_lock);
  2362. }
  2363. EXPORT_SYMBOL(kmem_cache_destroy);
  2364. /********************************************************************
  2365. * Kmalloc subsystem
  2366. *******************************************************************/
  2367. struct kmem_cache *kmalloc_caches[SLUB_PAGE_SHIFT];
  2368. EXPORT_SYMBOL(kmalloc_caches);
  2369. static struct kmem_cache *kmem_cache;
  2370. #ifdef CONFIG_ZONE_DMA
  2371. static struct kmem_cache *kmalloc_dma_caches[SLUB_PAGE_SHIFT];
  2372. #endif
  2373. static int __init setup_slub_min_order(char *str)
  2374. {
  2375. get_option(&str, &slub_min_order);
  2376. return 1;
  2377. }
  2378. __setup("slub_min_order=", setup_slub_min_order);
  2379. static int __init setup_slub_max_order(char *str)
  2380. {
  2381. get_option(&str, &slub_max_order);
  2382. slub_max_order = min(slub_max_order, MAX_ORDER - 1);
  2383. return 1;
  2384. }
  2385. __setup("slub_max_order=", setup_slub_max_order);
  2386. static int __init setup_slub_min_objects(char *str)
  2387. {
  2388. get_option(&str, &slub_min_objects);
  2389. return 1;
  2390. }
  2391. __setup("slub_min_objects=", setup_slub_min_objects);
  2392. static int __init setup_slub_nomerge(char *str)
  2393. {
  2394. slub_nomerge = 1;
  2395. return 1;
  2396. }
  2397. __setup("slub_nomerge", setup_slub_nomerge);
  2398. static struct kmem_cache *__init create_kmalloc_cache(const char *name,
  2399. int size, unsigned int flags)
  2400. {
  2401. struct kmem_cache *s;
  2402. s = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
  2403. /*
  2404. * This function is called with IRQs disabled during early-boot on
  2405. * single CPU so there's no need to take slub_lock here.
  2406. */
  2407. if (!kmem_cache_open(s, name, size, ARCH_KMALLOC_MINALIGN,
  2408. flags, NULL))
  2409. goto panic;
  2410. list_add(&s->list, &slab_caches);
  2411. return s;
  2412. panic:
  2413. panic("Creation of kmalloc slab %s size=%d failed.\n", name, size);
  2414. return NULL;
  2415. }
  2416. /*
  2417. * Conversion table for small slabs sizes / 8 to the index in the
  2418. * kmalloc array. This is necessary for slabs < 192 since we have non power
  2419. * of two cache sizes there. The size of larger slabs can be determined using
  2420. * fls.
  2421. */
  2422. static s8 size_index[24] = {
  2423. 3, /* 8 */
  2424. 4, /* 16 */
  2425. 5, /* 24 */
  2426. 5, /* 32 */
  2427. 6, /* 40 */
  2428. 6, /* 48 */
  2429. 6, /* 56 */
  2430. 6, /* 64 */
  2431. 1, /* 72 */
  2432. 1, /* 80 */
  2433. 1, /* 88 */
  2434. 1, /* 96 */
  2435. 7, /* 104 */
  2436. 7, /* 112 */
  2437. 7, /* 120 */
  2438. 7, /* 128 */
  2439. 2, /* 136 */
  2440. 2, /* 144 */
  2441. 2, /* 152 */
  2442. 2, /* 160 */
  2443. 2, /* 168 */
  2444. 2, /* 176 */
  2445. 2, /* 184 */
  2446. 2 /* 192 */
  2447. };
  2448. static inline int size_index_elem(size_t bytes)
  2449. {
  2450. return (bytes - 1) / 8;
  2451. }
  2452. static struct kmem_cache *get_slab(size_t size, gfp_t flags)
  2453. {
  2454. int index;
  2455. if (size <= 192) {
  2456. if (!size)
  2457. return ZERO_SIZE_PTR;
  2458. index = size_index[size_index_elem(size)];
  2459. } else
  2460. index = fls(size - 1);
  2461. #ifdef CONFIG_ZONE_DMA
  2462. if (unlikely((flags & SLUB_DMA)))
  2463. return kmalloc_dma_caches[index];
  2464. #endif
  2465. return kmalloc_caches[index];
  2466. }
  2467. void *__kmalloc(size_t size, gfp_t flags)
  2468. {
  2469. struct kmem_cache *s;
  2470. void *ret;
  2471. if (unlikely(size > SLUB_MAX_SIZE))
  2472. return kmalloc_large(size, flags);
  2473. s = get_slab(size, flags);
  2474. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2475. return s;
  2476. ret = slab_alloc(s, flags, NUMA_NO_NODE, _RET_IP_);
  2477. trace_kmalloc(_RET_IP_, ret, size, s->size, flags);
  2478. return ret;
  2479. }
  2480. EXPORT_SYMBOL(__kmalloc);
  2481. #ifdef CONFIG_NUMA
  2482. static void *kmalloc_large_node(size_t size, gfp_t flags, int node)
  2483. {
  2484. struct page *page;
  2485. void *ptr = NULL;
  2486. flags |= __GFP_COMP | __GFP_NOTRACK;
  2487. page = alloc_pages_node(node, flags, get_order(size));
  2488. if (page)
  2489. ptr = page_address(page);
  2490. kmemleak_alloc(ptr, size, 1, flags);
  2491. return ptr;
  2492. }
  2493. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  2494. {
  2495. struct kmem_cache *s;
  2496. void *ret;
  2497. if (unlikely(size > SLUB_MAX_SIZE)) {
  2498. ret = kmalloc_large_node(size, flags, node);
  2499. trace_kmalloc_node(_RET_IP_, ret,
  2500. size, PAGE_SIZE << get_order(size),
  2501. flags, node);
  2502. return ret;
  2503. }
  2504. s = get_slab(size, flags);
  2505. if (unlikely(ZERO_OR_NULL_PTR(s)))
  2506. return s;
  2507. ret = slab_alloc(s, flags, node, _RET_IP_);
  2508. trace_kmalloc_node(_RET_IP_, ret, size, s->size, flags, node);
  2509. return ret;
  2510. }
  2511. EXPORT_SYMBOL(__kmalloc_node);
  2512. #endif
  2513. size_t ksize(const void *object)
  2514. {
  2515. struct page *page;
  2516. if (unlikely(object == ZERO_SIZE_PTR))
  2517. return 0;
  2518. page = virt_to_head_page(object);
  2519. if (unlikely(!PageSlab(page))) {
  2520. WARN_ON(!PageCompound(page));
  2521. return PAGE_SIZE << compound_order(page);
  2522. }
  2523. return slab_ksize(page->slab);
  2524. }
  2525. EXPORT_SYMBOL(ksize);
  2526. void kfree(const void *x)
  2527. {
  2528. struct page *page;
  2529. void *object = (void *)x;
  2530. trace_kfree(_RET_IP_, x);
  2531. if (unlikely(ZERO_OR_NULL_PTR(x)))
  2532. return;
  2533. page = virt_to_head_page(x);
  2534. if (unlikely(!PageSlab(page))) {
  2535. BUG_ON(!PageCompound(page));
  2536. kmemleak_free(x);
  2537. put_page(page);
  2538. return;
  2539. }
  2540. slab_free(page->slab, page, object, _RET_IP_);
  2541. }
  2542. EXPORT_SYMBOL(kfree);
  2543. /*
  2544. * kmem_cache_shrink removes empty slabs from the partial lists and sorts
  2545. * the remaining slabs by the number of items in use. The slabs with the
  2546. * most items in use come first. New allocations will then fill those up
  2547. * and thus they can be removed from the partial lists.
  2548. *
  2549. * The slabs with the least items are placed last. This results in them
  2550. * being allocated from last increasing the chance that the last objects
  2551. * are freed in them.
  2552. */
  2553. int kmem_cache_shrink(struct kmem_cache *s)
  2554. {
  2555. int node;
  2556. int i;
  2557. struct kmem_cache_node *n;
  2558. struct page *page;
  2559. struct page *t;
  2560. int objects = oo_objects(s->max);
  2561. struct list_head *slabs_by_inuse =
  2562. kmalloc(sizeof(struct list_head) * objects, GFP_KERNEL);
  2563. unsigned long flags;
  2564. if (!slabs_by_inuse)
  2565. return -ENOMEM;
  2566. flush_all(s);
  2567. for_each_node_state(node, N_NORMAL_MEMORY) {
  2568. n = get_node(s, node);
  2569. if (!n->nr_partial)
  2570. continue;
  2571. for (i = 0; i < objects; i++)
  2572. INIT_LIST_HEAD(slabs_by_inuse + i);
  2573. spin_lock_irqsave(&n->list_lock, flags);
  2574. /*
  2575. * Build lists indexed by the items in use in each slab.
  2576. *
  2577. * Note that concurrent frees may occur while we hold the
  2578. * list_lock. page->inuse here is the upper limit.
  2579. */
  2580. list_for_each_entry_safe(page, t, &n->partial, lru) {
  2581. if (!page->inuse && slab_trylock(page)) {
  2582. /*
  2583. * Must hold slab lock here because slab_free
  2584. * may have freed the last object and be
  2585. * waiting to release the slab.
  2586. */
  2587. __remove_partial(n, page);
  2588. slab_unlock(page);
  2589. discard_slab(s, page);
  2590. } else {
  2591. list_move(&page->lru,
  2592. slabs_by_inuse + page->inuse);
  2593. }
  2594. }
  2595. /*
  2596. * Rebuild the partial list with the slabs filled up most
  2597. * first and the least used slabs at the end.
  2598. */
  2599. for (i = objects - 1; i >= 0; i--)
  2600. list_splice(slabs_by_inuse + i, n->partial.prev);
  2601. spin_unlock_irqrestore(&n->list_lock, flags);
  2602. }
  2603. kfree(slabs_by_inuse);
  2604. return 0;
  2605. }
  2606. EXPORT_SYMBOL(kmem_cache_shrink);
  2607. #if defined(CONFIG_MEMORY_HOTPLUG)
  2608. static int slab_mem_going_offline_callback(void *arg)
  2609. {
  2610. struct kmem_cache *s;
  2611. down_read(&slub_lock);
  2612. list_for_each_entry(s, &slab_caches, list)
  2613. kmem_cache_shrink(s);
  2614. up_read(&slub_lock);
  2615. return 0;
  2616. }
  2617. static void slab_mem_offline_callback(void *arg)
  2618. {
  2619. struct kmem_cache_node *n;
  2620. struct kmem_cache *s;
  2621. struct memory_notify *marg = arg;
  2622. int offline_node;
  2623. offline_node = marg->status_change_nid;
  2624. /*
  2625. * If the node still has available memory. we need kmem_cache_node
  2626. * for it yet.
  2627. */
  2628. if (offline_node < 0)
  2629. return;
  2630. down_read(&slub_lock);
  2631. list_for_each_entry(s, &slab_caches, list) {
  2632. n = get_node(s, offline_node);
  2633. if (n) {
  2634. /*
  2635. * if n->nr_slabs > 0, slabs still exist on the node
  2636. * that is going down. We were unable to free them,
  2637. * and offline_pages() function shouldn't call this
  2638. * callback. So, we must fail.
  2639. */
  2640. BUG_ON(slabs_node(s, offline_node));
  2641. s->node[offline_node] = NULL;
  2642. kmem_cache_free(kmem_cache_node, n);
  2643. }
  2644. }
  2645. up_read(&slub_lock);
  2646. }
  2647. static int slab_mem_going_online_callback(void *arg)
  2648. {
  2649. struct kmem_cache_node *n;
  2650. struct kmem_cache *s;
  2651. struct memory_notify *marg = arg;
  2652. int nid = marg->status_change_nid;
  2653. int ret = 0;
  2654. /*
  2655. * If the node's memory is already available, then kmem_cache_node is
  2656. * already created. Nothing to do.
  2657. */
  2658. if (nid < 0)
  2659. return 0;
  2660. /*
  2661. * We are bringing a node online. No memory is available yet. We must
  2662. * allocate a kmem_cache_node structure in order to bring the node
  2663. * online.
  2664. */
  2665. down_read(&slub_lock);
  2666. list_for_each_entry(s, &slab_caches, list) {
  2667. /*
  2668. * XXX: kmem_cache_alloc_node will fallback to other nodes
  2669. * since memory is not yet available from the node that
  2670. * is brought up.
  2671. */
  2672. n = kmem_cache_alloc(kmem_cache_node, GFP_KERNEL);
  2673. if (!n) {
  2674. ret = -ENOMEM;
  2675. goto out;
  2676. }
  2677. init_kmem_cache_node(n, s);
  2678. s->node[nid] = n;
  2679. }
  2680. out:
  2681. up_read(&slub_lock);
  2682. return ret;
  2683. }
  2684. static int slab_memory_callback(struct notifier_block *self,
  2685. unsigned long action, void *arg)
  2686. {
  2687. int ret = 0;
  2688. switch (action) {
  2689. case MEM_GOING_ONLINE:
  2690. ret = slab_mem_going_online_callback(arg);
  2691. break;
  2692. case MEM_GOING_OFFLINE:
  2693. ret = slab_mem_going_offline_callback(arg);
  2694. break;
  2695. case MEM_OFFLINE:
  2696. case MEM_CANCEL_ONLINE:
  2697. slab_mem_offline_callback(arg);
  2698. break;
  2699. case MEM_ONLINE:
  2700. case MEM_CANCEL_OFFLINE:
  2701. break;
  2702. }
  2703. if (ret)
  2704. ret = notifier_from_errno(ret);
  2705. else
  2706. ret = NOTIFY_OK;
  2707. return ret;
  2708. }
  2709. #endif /* CONFIG_MEMORY_HOTPLUG */
  2710. /********************************************************************
  2711. * Basic setup of slabs
  2712. *******************************************************************/
  2713. /*
  2714. * Used for early kmem_cache structures that were allocated using
  2715. * the page allocator
  2716. */
  2717. static void __init kmem_cache_bootstrap_fixup(struct kmem_cache *s)
  2718. {
  2719. int node;
  2720. list_add(&s->list, &slab_caches);
  2721. s->refcount = -1;
  2722. for_each_node_state(node, N_NORMAL_MEMORY) {
  2723. struct kmem_cache_node *n = get_node(s, node);
  2724. struct page *p;
  2725. if (n) {
  2726. list_for_each_entry(p, &n->partial, lru)
  2727. p->slab = s;
  2728. #ifdef CONFIG_SLUB_DEBUG
  2729. list_for_each_entry(p, &n->full, lru)
  2730. p->slab = s;
  2731. #endif
  2732. }
  2733. }
  2734. }
  2735. void __init kmem_cache_init(void)
  2736. {
  2737. int i;
  2738. int caches = 0;
  2739. struct kmem_cache *temp_kmem_cache;
  2740. int order;
  2741. struct kmem_cache *temp_kmem_cache_node;
  2742. unsigned long kmalloc_size;
  2743. kmem_size = offsetof(struct kmem_cache, node) +
  2744. nr_node_ids * sizeof(struct kmem_cache_node *);
  2745. /* Allocate two kmem_caches from the page allocator */
  2746. kmalloc_size = ALIGN(kmem_size, cache_line_size());
  2747. order = get_order(2 * kmalloc_size);
  2748. kmem_cache = (void *)__get_free_pages(GFP_NOWAIT, order);
  2749. /*
  2750. * Must first have the slab cache available for the allocations of the
  2751. * struct kmem_cache_node's. There is special bootstrap code in
  2752. * kmem_cache_open for slab_state == DOWN.
  2753. */
  2754. kmem_cache_node = (void *)kmem_cache + kmalloc_size;
  2755. kmem_cache_open(kmem_cache_node, "kmem_cache_node",
  2756. sizeof(struct kmem_cache_node),
  2757. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  2758. hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
  2759. /* Able to allocate the per node structures */
  2760. slab_state = PARTIAL;
  2761. temp_kmem_cache = kmem_cache;
  2762. kmem_cache_open(kmem_cache, "kmem_cache", kmem_size,
  2763. 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
  2764. kmem_cache = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
  2765. memcpy(kmem_cache, temp_kmem_cache, kmem_size);
  2766. /*
  2767. * Allocate kmem_cache_node properly from the kmem_cache slab.
  2768. * kmem_cache_node is separately allocated so no need to
  2769. * update any list pointers.
  2770. */
  2771. temp_kmem_cache_node = kmem_cache_node;
  2772. kmem_cache_node = kmem_cache_alloc(kmem_cache, GFP_NOWAIT);
  2773. memcpy(kmem_cache_node, temp_kmem_cache_node, kmem_size);
  2774. kmem_cache_bootstrap_fixup(kmem_cache_node);
  2775. caches++;
  2776. kmem_cache_bootstrap_fixup(kmem_cache);
  2777. caches++;
  2778. /* Free temporary boot structure */
  2779. free_pages((unsigned long)temp_kmem_cache, order);
  2780. /* Now we can use the kmem_cache to allocate kmalloc slabs */
  2781. /*
  2782. * Patch up the size_index table if we have strange large alignment
  2783. * requirements for the kmalloc array. This is only the case for
  2784. * MIPS it seems. The standard arches will not generate any code here.
  2785. *
  2786. * Largest permitted alignment is 256 bytes due to the way we
  2787. * handle the index determination for the smaller caches.
  2788. *
  2789. * Make sure that nothing crazy happens if someone starts tinkering
  2790. * around with ARCH_KMALLOC_MINALIGN
  2791. */
  2792. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
  2793. (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
  2794. for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
  2795. int elem = size_index_elem(i);
  2796. if (elem >= ARRAY_SIZE(size_index))
  2797. break;
  2798. size_index[elem] = KMALLOC_SHIFT_LOW;
  2799. }
  2800. if (KMALLOC_MIN_SIZE == 64) {
  2801. /*
  2802. * The 96 byte size cache is not used if the alignment
  2803. * is 64 byte.
  2804. */
  2805. for (i = 64 + 8; i <= 96; i += 8)
  2806. size_index[size_index_elem(i)] = 7;
  2807. } else if (KMALLOC_MIN_SIZE == 128) {
  2808. /*
  2809. * The 192 byte sized cache is not used if the alignment
  2810. * is 128 byte. Redirect kmalloc to use the 256 byte cache
  2811. * instead.
  2812. */
  2813. for (i = 128 + 8; i <= 192; i += 8)
  2814. size_index[size_index_elem(i)] = 8;
  2815. }
  2816. /* Caches that are not of the two-to-the-power-of size */
  2817. if (KMALLOC_MIN_SIZE <= 32) {
  2818. kmalloc_caches[1] = create_kmalloc_cache("kmalloc-96", 96, 0);
  2819. caches++;
  2820. }
  2821. if (KMALLOC_MIN_SIZE <= 64) {
  2822. kmalloc_caches[2] = create_kmalloc_cache("kmalloc-192", 192, 0);
  2823. caches++;
  2824. }
  2825. for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
  2826. kmalloc_caches[i] = create_kmalloc_cache("kmalloc", 1 << i, 0);
  2827. caches++;
  2828. }
  2829. slab_state = UP;
  2830. /* Provide the correct kmalloc names now that the caches are up */
  2831. if (KMALLOC_MIN_SIZE <= 32) {
  2832. kmalloc_caches[1]->name = kstrdup(kmalloc_caches[1]->name, GFP_NOWAIT);
  2833. BUG_ON(!kmalloc_caches[1]->name);
  2834. }
  2835. if (KMALLOC_MIN_SIZE <= 64) {
  2836. kmalloc_caches[2]->name = kstrdup(kmalloc_caches[2]->name, GFP_NOWAIT);
  2837. BUG_ON(!kmalloc_caches[2]->name);
  2838. }
  2839. for (i = KMALLOC_SHIFT_LOW; i < SLUB_PAGE_SHIFT; i++) {
  2840. char *s = kasprintf(GFP_NOWAIT, "kmalloc-%d", 1 << i);
  2841. BUG_ON(!s);
  2842. kmalloc_caches[i]->name = s;
  2843. }
  2844. #ifdef CONFIG_SMP
  2845. register_cpu_notifier(&slab_notifier);
  2846. #endif
  2847. #ifdef CONFIG_ZONE_DMA
  2848. for (i = 0; i < SLUB_PAGE_SHIFT; i++) {
  2849. struct kmem_cache *s = kmalloc_caches[i];
  2850. if (s && s->size) {
  2851. char *name = kasprintf(GFP_NOWAIT,
  2852. "dma-kmalloc-%d", s->objsize);
  2853. BUG_ON(!name);
  2854. kmalloc_dma_caches[i] = create_kmalloc_cache(name,
  2855. s->objsize, SLAB_CACHE_DMA);
  2856. }
  2857. }
  2858. #endif
  2859. printk(KERN_INFO
  2860. "SLUB: Genslabs=%d, HWalign=%d, Order=%d-%d, MinObjects=%d,"
  2861. " CPUs=%d, Nodes=%d\n",
  2862. caches, cache_line_size(),
  2863. slub_min_order, slub_max_order, slub_min_objects,
  2864. nr_cpu_ids, nr_node_ids);
  2865. }
  2866. void __init kmem_cache_init_late(void)
  2867. {
  2868. }
  2869. /*
  2870. * Find a mergeable slab cache
  2871. */
  2872. static int slab_unmergeable(struct kmem_cache *s)
  2873. {
  2874. if (slub_nomerge || (s->flags & SLUB_NEVER_MERGE))
  2875. return 1;
  2876. if (s->ctor)
  2877. return 1;
  2878. /*
  2879. * We may have set a slab to be unmergeable during bootstrap.
  2880. */
  2881. if (s->refcount < 0)
  2882. return 1;
  2883. return 0;
  2884. }
  2885. static struct kmem_cache *find_mergeable(size_t size,
  2886. size_t align, unsigned long flags, const char *name,
  2887. void (*ctor)(void *))
  2888. {
  2889. struct kmem_cache *s;
  2890. if (slub_nomerge || (flags & SLUB_NEVER_MERGE))
  2891. return NULL;
  2892. if (ctor)
  2893. return NULL;
  2894. size = ALIGN(size, sizeof(void *));
  2895. align = calculate_alignment(flags, align, size);
  2896. size = ALIGN(size, align);
  2897. flags = kmem_cache_flags(size, flags, name, NULL);
  2898. list_for_each_entry(s, &slab_caches, list) {
  2899. if (slab_unmergeable(s))
  2900. continue;
  2901. if (size > s->size)
  2902. continue;
  2903. if ((flags & SLUB_MERGE_SAME) != (s->flags & SLUB_MERGE_SAME))
  2904. continue;
  2905. /*
  2906. * Check if alignment is compatible.
  2907. * Courtesy of Adrian Drzewiecki
  2908. */
  2909. if ((s->size & ~(align - 1)) != s->size)
  2910. continue;
  2911. if (s->size - size >= sizeof(void *))
  2912. continue;
  2913. return s;
  2914. }
  2915. return NULL;
  2916. }
  2917. struct kmem_cache *kmem_cache_create(const char *name, size_t size,
  2918. size_t align, unsigned long flags, void (*ctor)(void *))
  2919. {
  2920. struct kmem_cache *s;
  2921. char *n;
  2922. if (WARN_ON(!name))
  2923. return NULL;
  2924. down_write(&slub_lock);
  2925. s = find_mergeable(size, align, flags, name, ctor);
  2926. if (s) {
  2927. s->refcount++;
  2928. /*
  2929. * Adjust the object sizes so that we clear
  2930. * the complete object on kzalloc.
  2931. */
  2932. s->objsize = max(s->objsize, (int)size);
  2933. s->inuse = max_t(int, s->inuse, ALIGN(size, sizeof(void *)));
  2934. if (sysfs_slab_alias(s, name)) {
  2935. s->refcount--;
  2936. goto err;
  2937. }
  2938. up_write(&slub_lock);
  2939. return s;
  2940. }
  2941. n = kstrdup(name, GFP_KERNEL);
  2942. if (!n)
  2943. goto err;
  2944. s = kmalloc(kmem_size, GFP_KERNEL);
  2945. if (s) {
  2946. if (kmem_cache_open(s, n,
  2947. size, align, flags, ctor)) {
  2948. list_add(&s->list, &slab_caches);
  2949. if (sysfs_slab_add(s)) {
  2950. list_del(&s->list);
  2951. kfree(n);
  2952. kfree(s);
  2953. goto err;
  2954. }
  2955. up_write(&slub_lock);
  2956. return s;
  2957. }
  2958. kfree(n);
  2959. kfree(s);
  2960. }
  2961. err:
  2962. up_write(&slub_lock);
  2963. if (flags & SLAB_PANIC)
  2964. panic("Cannot create slabcache %s\n", name);
  2965. else
  2966. s = NULL;
  2967. return s;
  2968. }
  2969. EXPORT_SYMBOL(kmem_cache_create);
  2970. #ifdef CONFIG_SMP
  2971. /*
  2972. * Use the cpu notifier to insure that the cpu slabs are flushed when
  2973. * necessary.
  2974. */
  2975. static int __cpuinit slab_cpuup_callback(struct notifier_block *nfb,
  2976. unsigned long action, void *hcpu)
  2977. {
  2978. long cpu = (long)hcpu;
  2979. struct kmem_cache *s;
  2980. unsigned long flags;
  2981. switch (action) {
  2982. case CPU_UP_CANCELED:
  2983. case CPU_UP_CANCELED_FROZEN:
  2984. case CPU_DEAD:
  2985. case CPU_DEAD_FROZEN:
  2986. down_read(&slub_lock);
  2987. list_for_each_entry(s, &slab_caches, list) {
  2988. local_irq_save(flags);
  2989. __flush_cpu_slab(s, cpu);
  2990. local_irq_restore(flags);
  2991. }
  2992. up_read(&slub_lock);
  2993. break;
  2994. default:
  2995. break;
  2996. }
  2997. return NOTIFY_OK;
  2998. }
  2999. static struct notifier_block __cpuinitdata slab_notifier = {
  3000. .notifier_call = slab_cpuup_callback
  3001. };
  3002. #endif
  3003. void *__kmalloc_track_caller(size_t size, gfp_t gfpflags, unsigned long caller)
  3004. {
  3005. struct kmem_cache *s;
  3006. void *ret;
  3007. if (unlikely(size > SLUB_MAX_SIZE))
  3008. return kmalloc_large(size, gfpflags);
  3009. s = get_slab(size, gfpflags);
  3010. if (unlikely(ZERO_OR_NULL_PTR(s)))
  3011. return s;
  3012. ret = slab_alloc(s, gfpflags, NUMA_NO_NODE, caller);
  3013. /* Honor the call site pointer we recieved. */
  3014. trace_kmalloc(caller, ret, size, s->size, gfpflags);
  3015. return ret;
  3016. }
  3017. #ifdef CONFIG_NUMA
  3018. void *__kmalloc_node_track_caller(size_t size, gfp_t gfpflags,
  3019. int node, unsigned long caller)
  3020. {
  3021. struct kmem_cache *s;
  3022. void *ret;
  3023. if (unlikely(size > SLUB_MAX_SIZE)) {
  3024. ret = kmalloc_large_node(size, gfpflags, node);
  3025. trace_kmalloc_node(caller, ret,
  3026. size, PAGE_SIZE << get_order(size),
  3027. gfpflags, node);
  3028. return ret;
  3029. }
  3030. s = get_slab(size, gfpflags);
  3031. if (unlikely(ZERO_OR_NULL_PTR(s)))
  3032. return s;
  3033. ret = slab_alloc(s, gfpflags, node, caller);
  3034. /* Honor the call site pointer we recieved. */
  3035. trace_kmalloc_node(caller, ret, size, s->size, gfpflags, node);
  3036. return ret;
  3037. }
  3038. #endif
  3039. #ifdef CONFIG_SYSFS
  3040. static int count_inuse(struct page *page)
  3041. {
  3042. return page->inuse;
  3043. }
  3044. static int count_total(struct page *page)
  3045. {
  3046. return page->objects;
  3047. }
  3048. #endif
  3049. #ifdef CONFIG_SLUB_DEBUG
  3050. static int validate_slab(struct kmem_cache *s, struct page *page,
  3051. unsigned long *map)
  3052. {
  3053. void *p;
  3054. void *addr = page_address(page);
  3055. if (!check_slab(s, page) ||
  3056. !on_freelist(s, page, NULL))
  3057. return 0;
  3058. /* Now we know that a valid freelist exists */
  3059. bitmap_zero(map, page->objects);
  3060. get_map(s, page, map);
  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_INACTIVE))
  3064. return 0;
  3065. }
  3066. for_each_object(p, s, addr, page->objects)
  3067. if (!test_bit(slab_index(p, s, addr), map))
  3068. if (!check_object(s, page, p, SLUB_RED_ACTIVE))
  3069. return 0;
  3070. return 1;
  3071. }
  3072. static void validate_slab_slab(struct kmem_cache *s, struct page *page,
  3073. unsigned long *map)
  3074. {
  3075. if (slab_trylock(page)) {
  3076. validate_slab(s, page, map);
  3077. slab_unlock(page);
  3078. } else
  3079. printk(KERN_INFO "SLUB %s: Skipped busy slab 0x%p\n",
  3080. s->name, page);
  3081. }
  3082. static int validate_slab_node(struct kmem_cache *s,
  3083. struct kmem_cache_node *n, unsigned long *map)
  3084. {
  3085. unsigned long count = 0;
  3086. struct page *page;
  3087. unsigned long flags;
  3088. spin_lock_irqsave(&n->list_lock, flags);
  3089. list_for_each_entry(page, &n->partial, lru) {
  3090. validate_slab_slab(s, page, map);
  3091. count++;
  3092. }
  3093. if (count != n->nr_partial)
  3094. printk(KERN_ERR "SLUB %s: %ld partial slabs counted but "
  3095. "counter=%ld\n", s->name, count, n->nr_partial);
  3096. if (!(s->flags & SLAB_STORE_USER))
  3097. goto out;
  3098. list_for_each_entry(page, &n->full, lru) {
  3099. validate_slab_slab(s, page, map);
  3100. count++;
  3101. }
  3102. if (count != atomic_long_read(&n->nr_slabs))
  3103. printk(KERN_ERR "SLUB: %s %ld slabs counted but "
  3104. "counter=%ld\n", s->name, count,
  3105. atomic_long_read(&n->nr_slabs));
  3106. out:
  3107. spin_unlock_irqrestore(&n->list_lock, flags);
  3108. return count;
  3109. }
  3110. static long validate_slab_cache(struct kmem_cache *s)
  3111. {
  3112. int node;
  3113. unsigned long count = 0;
  3114. unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
  3115. sizeof(unsigned long), GFP_KERNEL);
  3116. if (!map)
  3117. return -ENOMEM;
  3118. flush_all(s);
  3119. for_each_node_state(node, N_NORMAL_MEMORY) {
  3120. struct kmem_cache_node *n = get_node(s, node);
  3121. count += validate_slab_node(s, n, map);
  3122. }
  3123. kfree(map);
  3124. return count;
  3125. }
  3126. /*
  3127. * Generate lists of code addresses where slabcache objects are allocated
  3128. * and freed.
  3129. */
  3130. struct location {
  3131. unsigned long count;
  3132. unsigned long addr;
  3133. long long sum_time;
  3134. long min_time;
  3135. long max_time;
  3136. long min_pid;
  3137. long max_pid;
  3138. DECLARE_BITMAP(cpus, NR_CPUS);
  3139. nodemask_t nodes;
  3140. };
  3141. struct loc_track {
  3142. unsigned long max;
  3143. unsigned long count;
  3144. struct location *loc;
  3145. };
  3146. static void free_loc_track(struct loc_track *t)
  3147. {
  3148. if (t->max)
  3149. free_pages((unsigned long)t->loc,
  3150. get_order(sizeof(struct location) * t->max));
  3151. }
  3152. static int alloc_loc_track(struct loc_track *t, unsigned long max, gfp_t flags)
  3153. {
  3154. struct location *l;
  3155. int order;
  3156. order = get_order(sizeof(struct location) * max);
  3157. l = (void *)__get_free_pages(flags, order);
  3158. if (!l)
  3159. return 0;
  3160. if (t->count) {
  3161. memcpy(l, t->loc, sizeof(struct location) * t->count);
  3162. free_loc_track(t);
  3163. }
  3164. t->max = max;
  3165. t->loc = l;
  3166. return 1;
  3167. }
  3168. static int add_location(struct loc_track *t, struct kmem_cache *s,
  3169. const struct track *track)
  3170. {
  3171. long start, end, pos;
  3172. struct location *l;
  3173. unsigned long caddr;
  3174. unsigned long age = jiffies - track->when;
  3175. start = -1;
  3176. end = t->count;
  3177. for ( ; ; ) {
  3178. pos = start + (end - start + 1) / 2;
  3179. /*
  3180. * There is nothing at "end". If we end up there
  3181. * we need to add something to before end.
  3182. */
  3183. if (pos == end)
  3184. break;
  3185. caddr = t->loc[pos].addr;
  3186. if (track->addr == caddr) {
  3187. l = &t->loc[pos];
  3188. l->count++;
  3189. if (track->when) {
  3190. l->sum_time += age;
  3191. if (age < l->min_time)
  3192. l->min_time = age;
  3193. if (age > l->max_time)
  3194. l->max_time = age;
  3195. if (track->pid < l->min_pid)
  3196. l->min_pid = track->pid;
  3197. if (track->pid > l->max_pid)
  3198. l->max_pid = track->pid;
  3199. cpumask_set_cpu(track->cpu,
  3200. to_cpumask(l->cpus));
  3201. }
  3202. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  3203. return 1;
  3204. }
  3205. if (track->addr < caddr)
  3206. end = pos;
  3207. else
  3208. start = pos;
  3209. }
  3210. /*
  3211. * Not found. Insert new tracking element.
  3212. */
  3213. if (t->count >= t->max && !alloc_loc_track(t, 2 * t->max, GFP_ATOMIC))
  3214. return 0;
  3215. l = t->loc + pos;
  3216. if (pos < t->count)
  3217. memmove(l + 1, l,
  3218. (t->count - pos) * sizeof(struct location));
  3219. t->count++;
  3220. l->count = 1;
  3221. l->addr = track->addr;
  3222. l->sum_time = age;
  3223. l->min_time = age;
  3224. l->max_time = age;
  3225. l->min_pid = track->pid;
  3226. l->max_pid = track->pid;
  3227. cpumask_clear(to_cpumask(l->cpus));
  3228. cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
  3229. nodes_clear(l->nodes);
  3230. node_set(page_to_nid(virt_to_page(track)), l->nodes);
  3231. return 1;
  3232. }
  3233. static void process_slab(struct loc_track *t, struct kmem_cache *s,
  3234. struct page *page, enum track_item alloc,
  3235. unsigned long *map)
  3236. {
  3237. void *addr = page_address(page);
  3238. void *p;
  3239. bitmap_zero(map, page->objects);
  3240. get_map(s, page, map);
  3241. for_each_object(p, s, addr, page->objects)
  3242. if (!test_bit(slab_index(p, s, addr), map))
  3243. add_location(t, s, get_track(s, p, alloc));
  3244. }
  3245. static int list_locations(struct kmem_cache *s, char *buf,
  3246. enum track_item alloc)
  3247. {
  3248. int len = 0;
  3249. unsigned long i;
  3250. struct loc_track t = { 0, 0, NULL };
  3251. int node;
  3252. unsigned long *map = kmalloc(BITS_TO_LONGS(oo_objects(s->max)) *
  3253. sizeof(unsigned long), GFP_KERNEL);
  3254. if (!map || !alloc_loc_track(&t, PAGE_SIZE / sizeof(struct location),
  3255. GFP_TEMPORARY)) {
  3256. kfree(map);
  3257. return sprintf(buf, "Out of memory\n");
  3258. }
  3259. /* Push back cpu slabs */
  3260. flush_all(s);
  3261. for_each_node_state(node, N_NORMAL_MEMORY) {
  3262. struct kmem_cache_node *n = get_node(s, node);
  3263. unsigned long flags;
  3264. struct page *page;
  3265. if (!atomic_long_read(&n->nr_slabs))
  3266. continue;
  3267. spin_lock_irqsave(&n->list_lock, flags);
  3268. list_for_each_entry(page, &n->partial, lru)
  3269. process_slab(&t, s, page, alloc, map);
  3270. list_for_each_entry(page, &n->full, lru)
  3271. process_slab(&t, s, page, alloc, map);
  3272. spin_unlock_irqrestore(&n->list_lock, flags);
  3273. }
  3274. for (i = 0; i < t.count; i++) {
  3275. struct location *l = &t.loc[i];
  3276. if (len > PAGE_SIZE - KSYM_SYMBOL_LEN - 100)
  3277. break;
  3278. len += sprintf(buf + len, "%7ld ", l->count);
  3279. if (l->addr)
  3280. len += sprintf(buf + len, "%pS", (void *)l->addr);
  3281. else
  3282. len += sprintf(buf + len, "<not-available>");
  3283. if (l->sum_time != l->min_time) {
  3284. len += sprintf(buf + len, " age=%ld/%ld/%ld",
  3285. l->min_time,
  3286. (long)div_u64(l->sum_time, l->count),
  3287. l->max_time);
  3288. } else
  3289. len += sprintf(buf + len, " age=%ld",
  3290. l->min_time);
  3291. if (l->min_pid != l->max_pid)
  3292. len += sprintf(buf + len, " pid=%ld-%ld",
  3293. l->min_pid, l->max_pid);
  3294. else
  3295. len += sprintf(buf + len, " pid=%ld",
  3296. l->min_pid);
  3297. if (num_online_cpus() > 1 &&
  3298. !cpumask_empty(to_cpumask(l->cpus)) &&
  3299. len < PAGE_SIZE - 60) {
  3300. len += sprintf(buf + len, " cpus=");
  3301. len += cpulist_scnprintf(buf + len, PAGE_SIZE - len - 50,
  3302. to_cpumask(l->cpus));
  3303. }
  3304. if (nr_online_nodes > 1 && !nodes_empty(l->nodes) &&
  3305. len < PAGE_SIZE - 60) {
  3306. len += sprintf(buf + len, " nodes=");
  3307. len += nodelist_scnprintf(buf + len, PAGE_SIZE - len - 50,
  3308. l->nodes);
  3309. }
  3310. len += sprintf(buf + len, "\n");
  3311. }
  3312. free_loc_track(&t);
  3313. kfree(map);
  3314. if (!t.count)
  3315. len += sprintf(buf, "No data\n");
  3316. return len;
  3317. }
  3318. #endif
  3319. #ifdef SLUB_RESILIENCY_TEST
  3320. static void resiliency_test(void)
  3321. {
  3322. u8 *p;
  3323. BUILD_BUG_ON(KMALLOC_MIN_SIZE > 16 || SLUB_PAGE_SHIFT < 10);
  3324. printk(KERN_ERR "SLUB resiliency testing\n");
  3325. printk(KERN_ERR "-----------------------\n");
  3326. printk(KERN_ERR "A. Corruption after allocation\n");
  3327. p = kzalloc(16, GFP_KERNEL);
  3328. p[16] = 0x12;
  3329. printk(KERN_ERR "\n1. kmalloc-16: Clobber Redzone/next pointer"
  3330. " 0x12->0x%p\n\n", p + 16);
  3331. validate_slab_cache(kmalloc_caches[4]);
  3332. /* Hmmm... The next two are dangerous */
  3333. p = kzalloc(32, GFP_KERNEL);
  3334. p[32 + sizeof(void *)] = 0x34;
  3335. printk(KERN_ERR "\n2. kmalloc-32: Clobber next pointer/next slab"
  3336. " 0x34 -> -0x%p\n", p);
  3337. printk(KERN_ERR
  3338. "If allocated object is overwritten then not detectable\n\n");
  3339. validate_slab_cache(kmalloc_caches[5]);
  3340. p = kzalloc(64, GFP_KERNEL);
  3341. p += 64 + (get_cycles() & 0xff) * sizeof(void *);
  3342. *p = 0x56;
  3343. printk(KERN_ERR "\n3. kmalloc-64: corrupting random byte 0x56->0x%p\n",
  3344. p);
  3345. printk(KERN_ERR
  3346. "If allocated object is overwritten then not detectable\n\n");
  3347. validate_slab_cache(kmalloc_caches[6]);
  3348. printk(KERN_ERR "\nB. Corruption after free\n");
  3349. p = kzalloc(128, GFP_KERNEL);
  3350. kfree(p);
  3351. *p = 0x78;
  3352. printk(KERN_ERR "1. kmalloc-128: Clobber first word 0x78->0x%p\n\n", p);
  3353. validate_slab_cache(kmalloc_caches[7]);
  3354. p = kzalloc(256, GFP_KERNEL);
  3355. kfree(p);
  3356. p[50] = 0x9a;
  3357. printk(KERN_ERR "\n2. kmalloc-256: Clobber 50th byte 0x9a->0x%p\n\n",
  3358. p);
  3359. validate_slab_cache(kmalloc_caches[8]);
  3360. p = kzalloc(512, GFP_KERNEL);
  3361. kfree(p);
  3362. p[512] = 0xab;
  3363. printk(KERN_ERR "\n3. kmalloc-512: Clobber redzone 0xab->0x%p\n\n", p);
  3364. validate_slab_cache(kmalloc_caches[9]);
  3365. }
  3366. #else
  3367. #ifdef CONFIG_SYSFS
  3368. static void resiliency_test(void) {};
  3369. #endif
  3370. #endif
  3371. #ifdef CONFIG_SYSFS
  3372. enum slab_stat_type {
  3373. SL_ALL, /* All slabs */
  3374. SL_PARTIAL, /* Only partially allocated slabs */
  3375. SL_CPU, /* Only slabs used for cpu caches */
  3376. SL_OBJECTS, /* Determine allocated objects not slabs */
  3377. SL_TOTAL /* Determine object capacity not slabs */
  3378. };
  3379. #define SO_ALL (1 << SL_ALL)
  3380. #define SO_PARTIAL (1 << SL_PARTIAL)
  3381. #define SO_CPU (1 << SL_CPU)
  3382. #define SO_OBJECTS (1 << SL_OBJECTS)
  3383. #define SO_TOTAL (1 << SL_TOTAL)
  3384. static ssize_t show_slab_objects(struct kmem_cache *s,
  3385. char *buf, unsigned long flags)
  3386. {
  3387. unsigned long total = 0;
  3388. int node;
  3389. int x;
  3390. unsigned long *nodes;
  3391. unsigned long *per_cpu;
  3392. nodes = kzalloc(2 * sizeof(unsigned long) * nr_node_ids, GFP_KERNEL);
  3393. if (!nodes)
  3394. return -ENOMEM;
  3395. per_cpu = nodes + nr_node_ids;
  3396. if (flags & SO_CPU) {
  3397. int cpu;
  3398. for_each_possible_cpu(cpu) {
  3399. struct kmem_cache_cpu *c = per_cpu_ptr(s->cpu_slab, cpu);
  3400. if (!c || c->node < 0)
  3401. continue;
  3402. if (c->page) {
  3403. if (flags & SO_TOTAL)
  3404. x = c->page->objects;
  3405. else if (flags & SO_OBJECTS)
  3406. x = c->page->inuse;
  3407. else
  3408. x = 1;
  3409. total += x;
  3410. nodes[c->node] += x;
  3411. }
  3412. per_cpu[c->node]++;
  3413. }
  3414. }
  3415. lock_memory_hotplug();
  3416. #ifdef CONFIG_SLUB_DEBUG
  3417. if (flags & SO_ALL) {
  3418. for_each_node_state(node, N_NORMAL_MEMORY) {
  3419. struct kmem_cache_node *n = get_node(s, node);
  3420. if (flags & SO_TOTAL)
  3421. x = atomic_long_read(&n->total_objects);
  3422. else if (flags & SO_OBJECTS)
  3423. x = atomic_long_read(&n->total_objects) -
  3424. count_partial(n, count_free);
  3425. else
  3426. x = atomic_long_read(&n->nr_slabs);
  3427. total += x;
  3428. nodes[node] += x;
  3429. }
  3430. } else
  3431. #endif
  3432. if (flags & SO_PARTIAL) {
  3433. for_each_node_state(node, N_NORMAL_MEMORY) {
  3434. struct kmem_cache_node *n = get_node(s, node);
  3435. if (flags & SO_TOTAL)
  3436. x = count_partial(n, count_total);
  3437. else if (flags & SO_OBJECTS)
  3438. x = count_partial(n, count_inuse);
  3439. else
  3440. x = n->nr_partial;
  3441. total += x;
  3442. nodes[node] += x;
  3443. }
  3444. }
  3445. x = sprintf(buf, "%lu", total);
  3446. #ifdef CONFIG_NUMA
  3447. for_each_node_state(node, N_NORMAL_MEMORY)
  3448. if (nodes[node])
  3449. x += sprintf(buf + x, " N%d=%lu",
  3450. node, nodes[node]);
  3451. #endif
  3452. unlock_memory_hotplug();
  3453. kfree(nodes);
  3454. return x + sprintf(buf + x, "\n");
  3455. }
  3456. #ifdef CONFIG_SLUB_DEBUG
  3457. static int any_slab_objects(struct kmem_cache *s)
  3458. {
  3459. int node;
  3460. for_each_online_node(node) {
  3461. struct kmem_cache_node *n = get_node(s, node);
  3462. if (!n)
  3463. continue;
  3464. if (atomic_long_read(&n->total_objects))
  3465. return 1;
  3466. }
  3467. return 0;
  3468. }
  3469. #endif
  3470. #define to_slab_attr(n) container_of(n, struct slab_attribute, attr)
  3471. #define to_slab(n) container_of(n, struct kmem_cache, kobj);
  3472. struct slab_attribute {
  3473. struct attribute attr;
  3474. ssize_t (*show)(struct kmem_cache *s, char *buf);
  3475. ssize_t (*store)(struct kmem_cache *s, const char *x, size_t count);
  3476. };
  3477. #define SLAB_ATTR_RO(_name) \
  3478. static struct slab_attribute _name##_attr = __ATTR_RO(_name)
  3479. #define SLAB_ATTR(_name) \
  3480. static struct slab_attribute _name##_attr = \
  3481. __ATTR(_name, 0644, _name##_show, _name##_store)
  3482. static ssize_t slab_size_show(struct kmem_cache *s, char *buf)
  3483. {
  3484. return sprintf(buf, "%d\n", s->size);
  3485. }
  3486. SLAB_ATTR_RO(slab_size);
  3487. static ssize_t align_show(struct kmem_cache *s, char *buf)
  3488. {
  3489. return sprintf(buf, "%d\n", s->align);
  3490. }
  3491. SLAB_ATTR_RO(align);
  3492. static ssize_t object_size_show(struct kmem_cache *s, char *buf)
  3493. {
  3494. return sprintf(buf, "%d\n", s->objsize);
  3495. }
  3496. SLAB_ATTR_RO(object_size);
  3497. static ssize_t objs_per_slab_show(struct kmem_cache *s, char *buf)
  3498. {
  3499. return sprintf(buf, "%d\n", oo_objects(s->oo));
  3500. }
  3501. SLAB_ATTR_RO(objs_per_slab);
  3502. static ssize_t order_store(struct kmem_cache *s,
  3503. const char *buf, size_t length)
  3504. {
  3505. unsigned long order;
  3506. int err;
  3507. err = strict_strtoul(buf, 10, &order);
  3508. if (err)
  3509. return err;
  3510. if (order > slub_max_order || order < slub_min_order)
  3511. return -EINVAL;
  3512. calculate_sizes(s, order);
  3513. return length;
  3514. }
  3515. static ssize_t order_show(struct kmem_cache *s, char *buf)
  3516. {
  3517. return sprintf(buf, "%d\n", oo_order(s->oo));
  3518. }
  3519. SLAB_ATTR(order);
  3520. static ssize_t min_partial_show(struct kmem_cache *s, char *buf)
  3521. {
  3522. return sprintf(buf, "%lu\n", s->min_partial);
  3523. }
  3524. static ssize_t min_partial_store(struct kmem_cache *s, const char *buf,
  3525. size_t length)
  3526. {
  3527. unsigned long min;
  3528. int err;
  3529. err = strict_strtoul(buf, 10, &min);
  3530. if (err)
  3531. return err;
  3532. set_min_partial(s, min);
  3533. return length;
  3534. }
  3535. SLAB_ATTR(min_partial);
  3536. static ssize_t ctor_show(struct kmem_cache *s, char *buf)
  3537. {
  3538. if (!s->ctor)
  3539. return 0;
  3540. return sprintf(buf, "%pS\n", s->ctor);
  3541. }
  3542. SLAB_ATTR_RO(ctor);
  3543. static ssize_t aliases_show(struct kmem_cache *s, char *buf)
  3544. {
  3545. return sprintf(buf, "%d\n", s->refcount - 1);
  3546. }
  3547. SLAB_ATTR_RO(aliases);
  3548. static ssize_t partial_show(struct kmem_cache *s, char *buf)
  3549. {
  3550. return show_slab_objects(s, buf, SO_PARTIAL);
  3551. }
  3552. SLAB_ATTR_RO(partial);
  3553. static ssize_t cpu_slabs_show(struct kmem_cache *s, char *buf)
  3554. {
  3555. return show_slab_objects(s, buf, SO_CPU);
  3556. }
  3557. SLAB_ATTR_RO(cpu_slabs);
  3558. static ssize_t objects_show(struct kmem_cache *s, char *buf)
  3559. {
  3560. return show_slab_objects(s, buf, SO_ALL|SO_OBJECTS);
  3561. }
  3562. SLAB_ATTR_RO(objects);
  3563. static ssize_t objects_partial_show(struct kmem_cache *s, char *buf)
  3564. {
  3565. return show_slab_objects(s, buf, SO_PARTIAL|SO_OBJECTS);
  3566. }
  3567. SLAB_ATTR_RO(objects_partial);
  3568. static ssize_t reclaim_account_show(struct kmem_cache *s, char *buf)
  3569. {
  3570. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RECLAIM_ACCOUNT));
  3571. }
  3572. static ssize_t reclaim_account_store(struct kmem_cache *s,
  3573. const char *buf, size_t length)
  3574. {
  3575. s->flags &= ~SLAB_RECLAIM_ACCOUNT;
  3576. if (buf[0] == '1')
  3577. s->flags |= SLAB_RECLAIM_ACCOUNT;
  3578. return length;
  3579. }
  3580. SLAB_ATTR(reclaim_account);
  3581. static ssize_t hwcache_align_show(struct kmem_cache *s, char *buf)
  3582. {
  3583. return sprintf(buf, "%d\n", !!(s->flags & SLAB_HWCACHE_ALIGN));
  3584. }
  3585. SLAB_ATTR_RO(hwcache_align);
  3586. #ifdef CONFIG_ZONE_DMA
  3587. static ssize_t cache_dma_show(struct kmem_cache *s, char *buf)
  3588. {
  3589. return sprintf(buf, "%d\n", !!(s->flags & SLAB_CACHE_DMA));
  3590. }
  3591. SLAB_ATTR_RO(cache_dma);
  3592. #endif
  3593. static ssize_t destroy_by_rcu_show(struct kmem_cache *s, char *buf)
  3594. {
  3595. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DESTROY_BY_RCU));
  3596. }
  3597. SLAB_ATTR_RO(destroy_by_rcu);
  3598. static ssize_t reserved_show(struct kmem_cache *s, char *buf)
  3599. {
  3600. return sprintf(buf, "%d\n", s->reserved);
  3601. }
  3602. SLAB_ATTR_RO(reserved);
  3603. #ifdef CONFIG_SLUB_DEBUG
  3604. static ssize_t slabs_show(struct kmem_cache *s, char *buf)
  3605. {
  3606. return show_slab_objects(s, buf, SO_ALL);
  3607. }
  3608. SLAB_ATTR_RO(slabs);
  3609. static ssize_t total_objects_show(struct kmem_cache *s, char *buf)
  3610. {
  3611. return show_slab_objects(s, buf, SO_ALL|SO_TOTAL);
  3612. }
  3613. SLAB_ATTR_RO(total_objects);
  3614. static ssize_t sanity_checks_show(struct kmem_cache *s, char *buf)
  3615. {
  3616. return sprintf(buf, "%d\n", !!(s->flags & SLAB_DEBUG_FREE));
  3617. }
  3618. static ssize_t sanity_checks_store(struct kmem_cache *s,
  3619. const char *buf, size_t length)
  3620. {
  3621. s->flags &= ~SLAB_DEBUG_FREE;
  3622. if (buf[0] == '1')
  3623. s->flags |= SLAB_DEBUG_FREE;
  3624. return length;
  3625. }
  3626. SLAB_ATTR(sanity_checks);
  3627. static ssize_t trace_show(struct kmem_cache *s, char *buf)
  3628. {
  3629. return sprintf(buf, "%d\n", !!(s->flags & SLAB_TRACE));
  3630. }
  3631. static ssize_t trace_store(struct kmem_cache *s, const char *buf,
  3632. size_t length)
  3633. {
  3634. s->flags &= ~SLAB_TRACE;
  3635. if (buf[0] == '1')
  3636. s->flags |= SLAB_TRACE;
  3637. return length;
  3638. }
  3639. SLAB_ATTR(trace);
  3640. static ssize_t red_zone_show(struct kmem_cache *s, char *buf)
  3641. {
  3642. return sprintf(buf, "%d\n", !!(s->flags & SLAB_RED_ZONE));
  3643. }
  3644. static ssize_t red_zone_store(struct kmem_cache *s,
  3645. const char *buf, size_t length)
  3646. {
  3647. if (any_slab_objects(s))
  3648. return -EBUSY;
  3649. s->flags &= ~SLAB_RED_ZONE;
  3650. if (buf[0] == '1')
  3651. s->flags |= SLAB_RED_ZONE;
  3652. calculate_sizes(s, -1);
  3653. return length;
  3654. }
  3655. SLAB_ATTR(red_zone);
  3656. static ssize_t poison_show(struct kmem_cache *s, char *buf)
  3657. {
  3658. return sprintf(buf, "%d\n", !!(s->flags & SLAB_POISON));
  3659. }
  3660. static ssize_t poison_store(struct kmem_cache *s,
  3661. const char *buf, size_t length)
  3662. {
  3663. if (any_slab_objects(s))
  3664. return -EBUSY;
  3665. s->flags &= ~SLAB_POISON;
  3666. if (buf[0] == '1')
  3667. s->flags |= SLAB_POISON;
  3668. calculate_sizes(s, -1);
  3669. return length;
  3670. }
  3671. SLAB_ATTR(poison);
  3672. static ssize_t store_user_show(struct kmem_cache *s, char *buf)
  3673. {
  3674. return sprintf(buf, "%d\n", !!(s->flags & SLAB_STORE_USER));
  3675. }
  3676. static ssize_t store_user_store(struct kmem_cache *s,
  3677. const char *buf, size_t length)
  3678. {
  3679. if (any_slab_objects(s))
  3680. return -EBUSY;
  3681. s->flags &= ~SLAB_STORE_USER;
  3682. if (buf[0] == '1')
  3683. s->flags |= SLAB_STORE_USER;
  3684. calculate_sizes(s, -1);
  3685. return length;
  3686. }
  3687. SLAB_ATTR(store_user);
  3688. static ssize_t validate_show(struct kmem_cache *s, char *buf)
  3689. {
  3690. return 0;
  3691. }
  3692. static ssize_t validate_store(struct kmem_cache *s,
  3693. const char *buf, size_t length)
  3694. {
  3695. int ret = -EINVAL;
  3696. if (buf[0] == '1') {
  3697. ret = validate_slab_cache(s);
  3698. if (ret >= 0)
  3699. ret = length;
  3700. }
  3701. return ret;
  3702. }
  3703. SLAB_ATTR(validate);
  3704. static ssize_t alloc_calls_show(struct kmem_cache *s, char *buf)
  3705. {
  3706. if (!(s->flags & SLAB_STORE_USER))
  3707. return -ENOSYS;
  3708. return list_locations(s, buf, TRACK_ALLOC);
  3709. }
  3710. SLAB_ATTR_RO(alloc_calls);
  3711. static ssize_t free_calls_show(struct kmem_cache *s, char *buf)
  3712. {
  3713. if (!(s->flags & SLAB_STORE_USER))
  3714. return -ENOSYS;
  3715. return list_locations(s, buf, TRACK_FREE);
  3716. }
  3717. SLAB_ATTR_RO(free_calls);
  3718. #endif /* CONFIG_SLUB_DEBUG */
  3719. #ifdef CONFIG_FAILSLAB
  3720. static ssize_t failslab_show(struct kmem_cache *s, char *buf)
  3721. {
  3722. return sprintf(buf, "%d\n", !!(s->flags & SLAB_FAILSLAB));
  3723. }
  3724. static ssize_t failslab_store(struct kmem_cache *s, const char *buf,
  3725. size_t length)
  3726. {
  3727. s->flags &= ~SLAB_FAILSLAB;
  3728. if (buf[0] == '1')
  3729. s->flags |= SLAB_FAILSLAB;
  3730. return length;
  3731. }
  3732. SLAB_ATTR(failslab);
  3733. #endif
  3734. static ssize_t shrink_show(struct kmem_cache *s, char *buf)
  3735. {
  3736. return 0;
  3737. }
  3738. static ssize_t shrink_store(struct kmem_cache *s,
  3739. const char *buf, size_t length)
  3740. {
  3741. if (buf[0] == '1') {
  3742. int rc = kmem_cache_shrink(s);
  3743. if (rc)
  3744. return rc;
  3745. } else
  3746. return -EINVAL;
  3747. return length;
  3748. }
  3749. SLAB_ATTR(shrink);
  3750. #ifdef CONFIG_NUMA
  3751. static ssize_t remote_node_defrag_ratio_show(struct kmem_cache *s, char *buf)
  3752. {
  3753. return sprintf(buf, "%d\n", s->remote_node_defrag_ratio / 10);
  3754. }
  3755. static ssize_t remote_node_defrag_ratio_store(struct kmem_cache *s,
  3756. const char *buf, size_t length)
  3757. {
  3758. unsigned long ratio;
  3759. int err;
  3760. err = strict_strtoul(buf, 10, &ratio);
  3761. if (err)
  3762. return err;
  3763. if (ratio <= 100)
  3764. s->remote_node_defrag_ratio = ratio * 10;
  3765. return length;
  3766. }
  3767. SLAB_ATTR(remote_node_defrag_ratio);
  3768. #endif
  3769. #ifdef CONFIG_SLUB_STATS
  3770. static int show_stat(struct kmem_cache *s, char *buf, enum stat_item si)
  3771. {
  3772. unsigned long sum = 0;
  3773. int cpu;
  3774. int len;
  3775. int *data = kmalloc(nr_cpu_ids * sizeof(int), GFP_KERNEL);
  3776. if (!data)
  3777. return -ENOMEM;
  3778. for_each_online_cpu(cpu) {
  3779. unsigned x = per_cpu_ptr(s->cpu_slab, cpu)->stat[si];
  3780. data[cpu] = x;
  3781. sum += x;
  3782. }
  3783. len = sprintf(buf, "%lu", sum);
  3784. #ifdef CONFIG_SMP
  3785. for_each_online_cpu(cpu) {
  3786. if (data[cpu] && len < PAGE_SIZE - 20)
  3787. len += sprintf(buf + len, " C%d=%u", cpu, data[cpu]);
  3788. }
  3789. #endif
  3790. kfree(data);
  3791. return len + sprintf(buf + len, "\n");
  3792. }
  3793. static void clear_stat(struct kmem_cache *s, enum stat_item si)
  3794. {
  3795. int cpu;
  3796. for_each_online_cpu(cpu)
  3797. per_cpu_ptr(s->cpu_slab, cpu)->stat[si] = 0;
  3798. }
  3799. #define STAT_ATTR(si, text) \
  3800. static ssize_t text##_show(struct kmem_cache *s, char *buf) \
  3801. { \
  3802. return show_stat(s, buf, si); \
  3803. } \
  3804. static ssize_t text##_store(struct kmem_cache *s, \
  3805. const char *buf, size_t length) \
  3806. { \
  3807. if (buf[0] != '0') \
  3808. return -EINVAL; \
  3809. clear_stat(s, si); \
  3810. return length; \
  3811. } \
  3812. SLAB_ATTR(text); \
  3813. STAT_ATTR(ALLOC_FASTPATH, alloc_fastpath);
  3814. STAT_ATTR(ALLOC_SLOWPATH, alloc_slowpath);
  3815. STAT_ATTR(FREE_FASTPATH, free_fastpath);
  3816. STAT_ATTR(FREE_SLOWPATH, free_slowpath);
  3817. STAT_ATTR(FREE_FROZEN, free_frozen);
  3818. STAT_ATTR(FREE_ADD_PARTIAL, free_add_partial);
  3819. STAT_ATTR(FREE_REMOVE_PARTIAL, free_remove_partial);
  3820. STAT_ATTR(ALLOC_FROM_PARTIAL, alloc_from_partial);
  3821. STAT_ATTR(ALLOC_SLAB, alloc_slab);
  3822. STAT_ATTR(ALLOC_REFILL, alloc_refill);
  3823. STAT_ATTR(FREE_SLAB, free_slab);
  3824. STAT_ATTR(CPUSLAB_FLUSH, cpuslab_flush);
  3825. STAT_ATTR(DEACTIVATE_FULL, deactivate_full);
  3826. STAT_ATTR(DEACTIVATE_EMPTY, deactivate_empty);
  3827. STAT_ATTR(DEACTIVATE_TO_HEAD, deactivate_to_head);
  3828. STAT_ATTR(DEACTIVATE_TO_TAIL, deactivate_to_tail);
  3829. STAT_ATTR(DEACTIVATE_REMOTE_FREES, deactivate_remote_frees);
  3830. STAT_ATTR(ORDER_FALLBACK, order_fallback);
  3831. #endif
  3832. static struct attribute *slab_attrs[] = {
  3833. &slab_size_attr.attr,
  3834. &object_size_attr.attr,
  3835. &objs_per_slab_attr.attr,
  3836. &order_attr.attr,
  3837. &min_partial_attr.attr,
  3838. &objects_attr.attr,
  3839. &objects_partial_attr.attr,
  3840. &partial_attr.attr,
  3841. &cpu_slabs_attr.attr,
  3842. &ctor_attr.attr,
  3843. &aliases_attr.attr,
  3844. &align_attr.attr,
  3845. &hwcache_align_attr.attr,
  3846. &reclaim_account_attr.attr,
  3847. &destroy_by_rcu_attr.attr,
  3848. &shrink_attr.attr,
  3849. &reserved_attr.attr,
  3850. #ifdef CONFIG_SLUB_DEBUG
  3851. &total_objects_attr.attr,
  3852. &slabs_attr.attr,
  3853. &sanity_checks_attr.attr,
  3854. &trace_attr.attr,
  3855. &red_zone_attr.attr,
  3856. &poison_attr.attr,
  3857. &store_user_attr.attr,
  3858. &validate_attr.attr,
  3859. &alloc_calls_attr.attr,
  3860. &free_calls_attr.attr,
  3861. #endif
  3862. #ifdef CONFIG_ZONE_DMA
  3863. &cache_dma_attr.attr,
  3864. #endif
  3865. #ifdef CONFIG_NUMA
  3866. &remote_node_defrag_ratio_attr.attr,
  3867. #endif
  3868. #ifdef CONFIG_SLUB_STATS
  3869. &alloc_fastpath_attr.attr,
  3870. &alloc_slowpath_attr.attr,
  3871. &free_fastpath_attr.attr,
  3872. &free_slowpath_attr.attr,
  3873. &free_frozen_attr.attr,
  3874. &free_add_partial_attr.attr,
  3875. &free_remove_partial_attr.attr,
  3876. &alloc_from_partial_attr.attr,
  3877. &alloc_slab_attr.attr,
  3878. &alloc_refill_attr.attr,
  3879. &free_slab_attr.attr,
  3880. &cpuslab_flush_attr.attr,
  3881. &deactivate_full_attr.attr,
  3882. &deactivate_empty_attr.attr,
  3883. &deactivate_to_head_attr.attr,
  3884. &deactivate_to_tail_attr.attr,
  3885. &deactivate_remote_frees_attr.attr,
  3886. &order_fallback_attr.attr,
  3887. #endif
  3888. #ifdef CONFIG_FAILSLAB
  3889. &failslab_attr.attr,
  3890. #endif
  3891. NULL
  3892. };
  3893. static struct attribute_group slab_attr_group = {
  3894. .attrs = slab_attrs,
  3895. };
  3896. static ssize_t slab_attr_show(struct kobject *kobj,
  3897. struct attribute *attr,
  3898. char *buf)
  3899. {
  3900. struct slab_attribute *attribute;
  3901. struct kmem_cache *s;
  3902. int err;
  3903. attribute = to_slab_attr(attr);
  3904. s = to_slab(kobj);
  3905. if (!attribute->show)
  3906. return -EIO;
  3907. err = attribute->show(s, buf);
  3908. return err;
  3909. }
  3910. static ssize_t slab_attr_store(struct kobject *kobj,
  3911. struct attribute *attr,
  3912. const char *buf, size_t len)
  3913. {
  3914. struct slab_attribute *attribute;
  3915. struct kmem_cache *s;
  3916. int err;
  3917. attribute = to_slab_attr(attr);
  3918. s = to_slab(kobj);
  3919. if (!attribute->store)
  3920. return -EIO;
  3921. err = attribute->store(s, buf, len);
  3922. return err;
  3923. }
  3924. static void kmem_cache_release(struct kobject *kobj)
  3925. {
  3926. struct kmem_cache *s = to_slab(kobj);
  3927. kfree(s->name);
  3928. kfree(s);
  3929. }
  3930. static const struct sysfs_ops slab_sysfs_ops = {
  3931. .show = slab_attr_show,
  3932. .store = slab_attr_store,
  3933. };
  3934. static struct kobj_type slab_ktype = {
  3935. .sysfs_ops = &slab_sysfs_ops,
  3936. .release = kmem_cache_release
  3937. };
  3938. static int uevent_filter(struct kset *kset, struct kobject *kobj)
  3939. {
  3940. struct kobj_type *ktype = get_ktype(kobj);
  3941. if (ktype == &slab_ktype)
  3942. return 1;
  3943. return 0;
  3944. }
  3945. static const struct kset_uevent_ops slab_uevent_ops = {
  3946. .filter = uevent_filter,
  3947. };
  3948. static struct kset *slab_kset;
  3949. #define ID_STR_LENGTH 64
  3950. /* Create a unique string id for a slab cache:
  3951. *
  3952. * Format :[flags-]size
  3953. */
  3954. static char *create_unique_id(struct kmem_cache *s)
  3955. {
  3956. char *name = kmalloc(ID_STR_LENGTH, GFP_KERNEL);
  3957. char *p = name;
  3958. BUG_ON(!name);
  3959. *p++ = ':';
  3960. /*
  3961. * First flags affecting slabcache operations. We will only
  3962. * get here for aliasable slabs so we do not need to support
  3963. * too many flags. The flags here must cover all flags that
  3964. * are matched during merging to guarantee that the id is
  3965. * unique.
  3966. */
  3967. if (s->flags & SLAB_CACHE_DMA)
  3968. *p++ = 'd';
  3969. if (s->flags & SLAB_RECLAIM_ACCOUNT)
  3970. *p++ = 'a';
  3971. if (s->flags & SLAB_DEBUG_FREE)
  3972. *p++ = 'F';
  3973. if (!(s->flags & SLAB_NOTRACK))
  3974. *p++ = 't';
  3975. if (p != name + 1)
  3976. *p++ = '-';
  3977. p += sprintf(p, "%07d", s->size);
  3978. BUG_ON(p > name + ID_STR_LENGTH - 1);
  3979. return name;
  3980. }
  3981. static int sysfs_slab_add(struct kmem_cache *s)
  3982. {
  3983. int err;
  3984. const char *name;
  3985. int unmergeable;
  3986. if (slab_state < SYSFS)
  3987. /* Defer until later */
  3988. return 0;
  3989. unmergeable = slab_unmergeable(s);
  3990. if (unmergeable) {
  3991. /*
  3992. * Slabcache can never be merged so we can use the name proper.
  3993. * This is typically the case for debug situations. In that
  3994. * case we can catch duplicate names easily.
  3995. */
  3996. sysfs_remove_link(&slab_kset->kobj, s->name);
  3997. name = s->name;
  3998. } else {
  3999. /*
  4000. * Create a unique name for the slab as a target
  4001. * for the symlinks.
  4002. */
  4003. name = create_unique_id(s);
  4004. }
  4005. s->kobj.kset = slab_kset;
  4006. err = kobject_init_and_add(&s->kobj, &slab_ktype, NULL, name);
  4007. if (err) {
  4008. kobject_put(&s->kobj);
  4009. return err;
  4010. }
  4011. err = sysfs_create_group(&s->kobj, &slab_attr_group);
  4012. if (err) {
  4013. kobject_del(&s->kobj);
  4014. kobject_put(&s->kobj);
  4015. return err;
  4016. }
  4017. kobject_uevent(&s->kobj, KOBJ_ADD);
  4018. if (!unmergeable) {
  4019. /* Setup first alias */
  4020. sysfs_slab_alias(s, s->name);
  4021. kfree(name);
  4022. }
  4023. return 0;
  4024. }
  4025. static void sysfs_slab_remove(struct kmem_cache *s)
  4026. {
  4027. if (slab_state < SYSFS)
  4028. /*
  4029. * Sysfs has not been setup yet so no need to remove the
  4030. * cache from sysfs.
  4031. */
  4032. return;
  4033. kobject_uevent(&s->kobj, KOBJ_REMOVE);
  4034. kobject_del(&s->kobj);
  4035. kobject_put(&s->kobj);
  4036. }
  4037. /*
  4038. * Need to buffer aliases during bootup until sysfs becomes
  4039. * available lest we lose that information.
  4040. */
  4041. struct saved_alias {
  4042. struct kmem_cache *s;
  4043. const char *name;
  4044. struct saved_alias *next;
  4045. };
  4046. static struct saved_alias *alias_list;
  4047. static int sysfs_slab_alias(struct kmem_cache *s, const char *name)
  4048. {
  4049. struct saved_alias *al;
  4050. if (slab_state == SYSFS) {
  4051. /*
  4052. * If we have a leftover link then remove it.
  4053. */
  4054. sysfs_remove_link(&slab_kset->kobj, name);
  4055. return sysfs_create_link(&slab_kset->kobj, &s->kobj, name);
  4056. }
  4057. al = kmalloc(sizeof(struct saved_alias), GFP_KERNEL);
  4058. if (!al)
  4059. return -ENOMEM;
  4060. al->s = s;
  4061. al->name = name;
  4062. al->next = alias_list;
  4063. alias_list = al;
  4064. return 0;
  4065. }
  4066. static int __init slab_sysfs_init(void)
  4067. {
  4068. struct kmem_cache *s;
  4069. int err;
  4070. down_write(&slub_lock);
  4071. slab_kset = kset_create_and_add("slab", &slab_uevent_ops, kernel_kobj);
  4072. if (!slab_kset) {
  4073. up_write(&slub_lock);
  4074. printk(KERN_ERR "Cannot register slab subsystem.\n");
  4075. return -ENOSYS;
  4076. }
  4077. slab_state = SYSFS;
  4078. list_for_each_entry(s, &slab_caches, list) {
  4079. err = sysfs_slab_add(s);
  4080. if (err)
  4081. printk(KERN_ERR "SLUB: Unable to add boot slab %s"
  4082. " to sysfs\n", s->name);
  4083. }
  4084. while (alias_list) {
  4085. struct saved_alias *al = alias_list;
  4086. alias_list = alias_list->next;
  4087. err = sysfs_slab_alias(al->s, al->name);
  4088. if (err)
  4089. printk(KERN_ERR "SLUB: Unable to add boot slab alias"
  4090. " %s to sysfs\n", s->name);
  4091. kfree(al);
  4092. }
  4093. up_write(&slub_lock);
  4094. resiliency_test();
  4095. return 0;
  4096. }
  4097. __initcall(slab_sysfs_init);
  4098. #endif /* CONFIG_SYSFS */
  4099. /*
  4100. * The /proc/slabinfo ABI
  4101. */
  4102. #ifdef CONFIG_SLABINFO
  4103. static void print_slabinfo_header(struct seq_file *m)
  4104. {
  4105. seq_puts(m, "slabinfo - version: 2.1\n");
  4106. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  4107. "<objperslab> <pagesperslab>");
  4108. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  4109. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  4110. seq_putc(m, '\n');
  4111. }
  4112. static void *s_start(struct seq_file *m, loff_t *pos)
  4113. {
  4114. loff_t n = *pos;
  4115. down_read(&slub_lock);
  4116. if (!n)
  4117. print_slabinfo_header(m);
  4118. return seq_list_start(&slab_caches, *pos);
  4119. }
  4120. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  4121. {
  4122. return seq_list_next(p, &slab_caches, pos);
  4123. }
  4124. static void s_stop(struct seq_file *m, void *p)
  4125. {
  4126. up_read(&slub_lock);
  4127. }
  4128. static int s_show(struct seq_file *m, void *p)
  4129. {
  4130. unsigned long nr_partials = 0;
  4131. unsigned long nr_slabs = 0;
  4132. unsigned long nr_inuse = 0;
  4133. unsigned long nr_objs = 0;
  4134. unsigned long nr_free = 0;
  4135. struct kmem_cache *s;
  4136. int node;
  4137. s = list_entry(p, struct kmem_cache, list);
  4138. for_each_online_node(node) {
  4139. struct kmem_cache_node *n = get_node(s, node);
  4140. if (!n)
  4141. continue;
  4142. nr_partials += n->nr_partial;
  4143. nr_slabs += atomic_long_read(&n->nr_slabs);
  4144. nr_objs += atomic_long_read(&n->total_objects);
  4145. nr_free += count_partial(n, count_free);
  4146. }
  4147. nr_inuse = nr_objs - nr_free;
  4148. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", s->name, nr_inuse,
  4149. nr_objs, s->size, oo_objects(s->oo),
  4150. (1 << oo_order(s->oo)));
  4151. seq_printf(m, " : tunables %4u %4u %4u", 0, 0, 0);
  4152. seq_printf(m, " : slabdata %6lu %6lu %6lu", nr_slabs, nr_slabs,
  4153. 0UL);
  4154. seq_putc(m, '\n');
  4155. return 0;
  4156. }
  4157. static const struct seq_operations slabinfo_op = {
  4158. .start = s_start,
  4159. .next = s_next,
  4160. .stop = s_stop,
  4161. .show = s_show,
  4162. };
  4163. static int slabinfo_open(struct inode *inode, struct file *file)
  4164. {
  4165. return seq_open(file, &slabinfo_op);
  4166. }
  4167. static const struct file_operations proc_slabinfo_operations = {
  4168. .open = slabinfo_open,
  4169. .read = seq_read,
  4170. .llseek = seq_lseek,
  4171. .release = seq_release,
  4172. };
  4173. static int __init slab_proc_init(void)
  4174. {
  4175. proc_create("slabinfo", S_IRUGO, NULL, &proc_slabinfo_operations);
  4176. return 0;
  4177. }
  4178. module_init(slab_proc_init);
  4179. #endif /* CONFIG_SLABINFO */