slab.c 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683
  1. /*
  2. * linux/mm/slab.c
  3. * Written by Mark Hemment, 1996/97.
  4. * (markhe@nextd.demon.co.uk)
  5. *
  6. * kmem_cache_destroy() + some cleanup - 1999 Andrea Arcangeli
  7. *
  8. * Major cleanup, different bufctl logic, per-cpu arrays
  9. * (c) 2000 Manfred Spraul
  10. *
  11. * Cleanup, make the head arrays unconditional, preparation for NUMA
  12. * (c) 2002 Manfred Spraul
  13. *
  14. * An implementation of the Slab Allocator as described in outline in;
  15. * UNIX Internals: The New Frontiers by Uresh Vahalia
  16. * Pub: Prentice Hall ISBN 0-13-101908-2
  17. * or with a little more detail in;
  18. * The Slab Allocator: An Object-Caching Kernel Memory Allocator
  19. * Jeff Bonwick (Sun Microsystems).
  20. * Presented at: USENIX Summer 1994 Technical Conference
  21. *
  22. * The memory is organized in caches, one cache for each object type.
  23. * (e.g. inode_cache, dentry_cache, buffer_head, vm_area_struct)
  24. * Each cache consists out of many slabs (they are small (usually one
  25. * page long) and always contiguous), and each slab contains multiple
  26. * initialized objects.
  27. *
  28. * This means, that your constructor is used only for newly allocated
  29. * slabs and you must pass objects with the same initializations to
  30. * kmem_cache_free.
  31. *
  32. * Each cache can only support one memory type (GFP_DMA, GFP_HIGHMEM,
  33. * normal). If you need a special memory type, then must create a new
  34. * cache for that memory type.
  35. *
  36. * In order to reduce fragmentation, the slabs are sorted in 3 groups:
  37. * full slabs with 0 free objects
  38. * partial slabs
  39. * empty slabs with no allocated objects
  40. *
  41. * If partial slabs exist, then new allocations come from these slabs,
  42. * otherwise from empty slabs or new slabs are allocated.
  43. *
  44. * kmem_cache_destroy() CAN CRASH if you try to allocate from the cache
  45. * during kmem_cache_destroy(). The caller must prevent concurrent allocs.
  46. *
  47. * Each cache has a short per-cpu head array, most allocs
  48. * and frees go into that array, and if that array overflows, then 1/2
  49. * of the entries in the array are given back into the global cache.
  50. * The head array is strictly LIFO and should improve the cache hit rates.
  51. * On SMP, it additionally reduces the spinlock operations.
  52. *
  53. * The c_cpuarray may not be read with enabled local interrupts -
  54. * it's changed with a smp_call_function().
  55. *
  56. * SMP synchronization:
  57. * constructors and destructors are called without any locking.
  58. * Several members in struct kmem_cache and struct slab never change, they
  59. * are accessed without any locking.
  60. * The per-cpu arrays are never accessed from the wrong cpu, no locking,
  61. * and local interrupts are disabled so slab code is preempt-safe.
  62. * The non-constant members are protected with a per-cache irq spinlock.
  63. *
  64. * Many thanks to Mark Hemment, who wrote another per-cpu slab patch
  65. * in 2000 - many ideas in the current implementation are derived from
  66. * his patch.
  67. *
  68. * Further notes from the original documentation:
  69. *
  70. * 11 April '97. Started multi-threading - markhe
  71. * The global cache-chain is protected by the mutex 'cache_chain_mutex'.
  72. * The sem is only needed when accessing/extending the cache-chain, which
  73. * can never happen inside an interrupt (kmem_cache_create(),
  74. * kmem_cache_shrink() and kmem_cache_reap()).
  75. *
  76. * At present, each engine can be growing a cache. This should be blocked.
  77. *
  78. * 15 March 2005. NUMA slab allocator.
  79. * Shai Fultheim <shai@scalex86.org>.
  80. * Shobhit Dayal <shobhit@calsoftinc.com>
  81. * Alok N Kataria <alokk@calsoftinc.com>
  82. * Christoph Lameter <christoph@lameter.com>
  83. *
  84. * Modified the slab allocator to be node aware on NUMA systems.
  85. * Each node has its own list of partial, free and full slabs.
  86. * All object allocations for a node occur from node specific slab lists.
  87. */
  88. #include <linux/slab.h>
  89. #include <linux/mm.h>
  90. #include <linux/poison.h>
  91. #include <linux/swap.h>
  92. #include <linux/cache.h>
  93. #include <linux/interrupt.h>
  94. #include <linux/init.h>
  95. #include <linux/compiler.h>
  96. #include <linux/cpuset.h>
  97. #include <linux/proc_fs.h>
  98. #include <linux/seq_file.h>
  99. #include <linux/notifier.h>
  100. #include <linux/kallsyms.h>
  101. #include <linux/cpu.h>
  102. #include <linux/sysctl.h>
  103. #include <linux/module.h>
  104. #include <linux/rcupdate.h>
  105. #include <linux/string.h>
  106. #include <linux/uaccess.h>
  107. #include <linux/nodemask.h>
  108. #include <linux/kmemleak.h>
  109. #include <linux/mempolicy.h>
  110. #include <linux/mutex.h>
  111. #include <linux/fault-inject.h>
  112. #include <linux/rtmutex.h>
  113. #include <linux/reciprocal_div.h>
  114. #include <linux/debugobjects.h>
  115. #include <linux/kmemcheck.h>
  116. #include <linux/memory.h>
  117. #include <linux/prefetch.h>
  118. #include <asm/cacheflush.h>
  119. #include <asm/tlbflush.h>
  120. #include <asm/page.h>
  121. #include <trace/events/kmem.h>
  122. /*
  123. * DEBUG - 1 for kmem_cache_create() to honour; SLAB_RED_ZONE & SLAB_POISON.
  124. * 0 for faster, smaller code (especially in the critical paths).
  125. *
  126. * STATS - 1 to collect stats for /proc/slabinfo.
  127. * 0 for faster, smaller code (especially in the critical paths).
  128. *
  129. * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
  130. */
  131. #ifdef CONFIG_DEBUG_SLAB
  132. #define DEBUG 1
  133. #define STATS 1
  134. #define FORCED_DEBUG 1
  135. #else
  136. #define DEBUG 0
  137. #define STATS 0
  138. #define FORCED_DEBUG 0
  139. #endif
  140. /* Shouldn't this be in a header file somewhere? */
  141. #define BYTES_PER_WORD sizeof(void *)
  142. #define REDZONE_ALIGN max(BYTES_PER_WORD, __alignof__(unsigned long long))
  143. #ifndef ARCH_KMALLOC_FLAGS
  144. #define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
  145. #endif
  146. /* Legal flag mask for kmem_cache_create(). */
  147. #if DEBUG
  148. # define CREATE_MASK (SLAB_RED_ZONE | \
  149. SLAB_POISON | SLAB_HWCACHE_ALIGN | \
  150. SLAB_CACHE_DMA | \
  151. SLAB_STORE_USER | \
  152. SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
  153. SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD | \
  154. SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE | SLAB_NOTRACK)
  155. #else
  156. # define CREATE_MASK (SLAB_HWCACHE_ALIGN | \
  157. SLAB_CACHE_DMA | \
  158. SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
  159. SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD | \
  160. SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE | SLAB_NOTRACK)
  161. #endif
  162. /*
  163. * kmem_bufctl_t:
  164. *
  165. * Bufctl's are used for linking objs within a slab
  166. * linked offsets.
  167. *
  168. * This implementation relies on "struct page" for locating the cache &
  169. * slab an object belongs to.
  170. * This allows the bufctl structure to be small (one int), but limits
  171. * the number of objects a slab (not a cache) can contain when off-slab
  172. * bufctls are used. The limit is the size of the largest general cache
  173. * that does not use off-slab slabs.
  174. * For 32bit archs with 4 kB pages, is this 56.
  175. * This is not serious, as it is only for large objects, when it is unwise
  176. * to have too many per slab.
  177. * Note: This limit can be raised by introducing a general cache whose size
  178. * is less than 512 (PAGE_SIZE<<3), but greater than 256.
  179. */
  180. typedef unsigned int kmem_bufctl_t;
  181. #define BUFCTL_END (((kmem_bufctl_t)(~0U))-0)
  182. #define BUFCTL_FREE (((kmem_bufctl_t)(~0U))-1)
  183. #define BUFCTL_ACTIVE (((kmem_bufctl_t)(~0U))-2)
  184. #define SLAB_LIMIT (((kmem_bufctl_t)(~0U))-3)
  185. /*
  186. * struct slab_rcu
  187. *
  188. * slab_destroy on a SLAB_DESTROY_BY_RCU cache uses this structure to
  189. * arrange for kmem_freepages to be called via RCU. This is useful if
  190. * we need to approach a kernel structure obliquely, from its address
  191. * obtained without the usual locking. We can lock the structure to
  192. * stabilize it and check it's still at the given address, only if we
  193. * can be sure that the memory has not been meanwhile reused for some
  194. * other kind of object (which our subsystem's lock might corrupt).
  195. *
  196. * rcu_read_lock before reading the address, then rcu_read_unlock after
  197. * taking the spinlock within the structure expected at that address.
  198. */
  199. struct slab_rcu {
  200. struct rcu_head head;
  201. struct kmem_cache *cachep;
  202. void *addr;
  203. };
  204. /*
  205. * struct slab
  206. *
  207. * Manages the objs in a slab. Placed either at the beginning of mem allocated
  208. * for a slab, or allocated from an general cache.
  209. * Slabs are chained into three list: fully used, partial, fully free slabs.
  210. */
  211. struct slab {
  212. union {
  213. struct {
  214. struct list_head list;
  215. unsigned long colouroff;
  216. void *s_mem; /* including colour offset */
  217. unsigned int inuse; /* num of objs active in slab */
  218. kmem_bufctl_t free;
  219. unsigned short nodeid;
  220. };
  221. struct slab_rcu __slab_cover_slab_rcu;
  222. };
  223. };
  224. /*
  225. * struct array_cache
  226. *
  227. * Purpose:
  228. * - LIFO ordering, to hand out cache-warm objects from _alloc
  229. * - reduce the number of linked list operations
  230. * - reduce spinlock operations
  231. *
  232. * The limit is stored in the per-cpu structure to reduce the data cache
  233. * footprint.
  234. *
  235. */
  236. struct array_cache {
  237. unsigned int avail;
  238. unsigned int limit;
  239. unsigned int batchcount;
  240. unsigned int touched;
  241. spinlock_t lock;
  242. void *entry[]; /*
  243. * Must have this definition in here for the proper
  244. * alignment of array_cache. Also simplifies accessing
  245. * the entries.
  246. */
  247. };
  248. /*
  249. * bootstrap: The caches do not work without cpuarrays anymore, but the
  250. * cpuarrays are allocated from the generic caches...
  251. */
  252. #define BOOT_CPUCACHE_ENTRIES 1
  253. struct arraycache_init {
  254. struct array_cache cache;
  255. void *entries[BOOT_CPUCACHE_ENTRIES];
  256. };
  257. /*
  258. * The slab lists for all objects.
  259. */
  260. struct kmem_list3 {
  261. struct list_head slabs_partial; /* partial list first, better asm code */
  262. struct list_head slabs_full;
  263. struct list_head slabs_free;
  264. unsigned long free_objects;
  265. unsigned int free_limit;
  266. unsigned int colour_next; /* Per-node cache coloring */
  267. spinlock_t list_lock;
  268. struct array_cache *shared; /* shared per node */
  269. struct array_cache **alien; /* on other nodes */
  270. unsigned long next_reap; /* updated without locking */
  271. int free_touched; /* updated without locking */
  272. };
  273. /*
  274. * Need this for bootstrapping a per node allocator.
  275. */
  276. #define NUM_INIT_LISTS (3 * MAX_NUMNODES)
  277. static struct kmem_list3 __initdata initkmem_list3[NUM_INIT_LISTS];
  278. #define CACHE_CACHE 0
  279. #define SIZE_AC MAX_NUMNODES
  280. #define SIZE_L3 (2 * MAX_NUMNODES)
  281. static int drain_freelist(struct kmem_cache *cache,
  282. struct kmem_list3 *l3, int tofree);
  283. static void free_block(struct kmem_cache *cachep, void **objpp, int len,
  284. int node);
  285. static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp);
  286. static void cache_reap(struct work_struct *unused);
  287. /*
  288. * This function must be completely optimized away if a constant is passed to
  289. * it. Mostly the same as what is in linux/slab.h except it returns an index.
  290. */
  291. static __always_inline int index_of(const size_t size)
  292. {
  293. extern void __bad_size(void);
  294. if (__builtin_constant_p(size)) {
  295. int i = 0;
  296. #define CACHE(x) \
  297. if (size <=x) \
  298. return i; \
  299. else \
  300. i++;
  301. #include <linux/kmalloc_sizes.h>
  302. #undef CACHE
  303. __bad_size();
  304. } else
  305. __bad_size();
  306. return 0;
  307. }
  308. static int slab_early_init = 1;
  309. #define INDEX_AC index_of(sizeof(struct arraycache_init))
  310. #define INDEX_L3 index_of(sizeof(struct kmem_list3))
  311. static void kmem_list3_init(struct kmem_list3 *parent)
  312. {
  313. INIT_LIST_HEAD(&parent->slabs_full);
  314. INIT_LIST_HEAD(&parent->slabs_partial);
  315. INIT_LIST_HEAD(&parent->slabs_free);
  316. parent->shared = NULL;
  317. parent->alien = NULL;
  318. parent->colour_next = 0;
  319. spin_lock_init(&parent->list_lock);
  320. parent->free_objects = 0;
  321. parent->free_touched = 0;
  322. }
  323. #define MAKE_LIST(cachep, listp, slab, nodeid) \
  324. do { \
  325. INIT_LIST_HEAD(listp); \
  326. list_splice(&(cachep->nodelists[nodeid]->slab), listp); \
  327. } while (0)
  328. #define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
  329. do { \
  330. MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
  331. MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
  332. MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
  333. } while (0)
  334. #define CFLGS_OFF_SLAB (0x80000000UL)
  335. #define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
  336. #define BATCHREFILL_LIMIT 16
  337. /*
  338. * Optimization question: fewer reaps means less probability for unnessary
  339. * cpucache drain/refill cycles.
  340. *
  341. * OTOH the cpuarrays can contain lots of objects,
  342. * which could lock up otherwise freeable slabs.
  343. */
  344. #define REAPTIMEOUT_CPUC (2*HZ)
  345. #define REAPTIMEOUT_LIST3 (4*HZ)
  346. #if STATS
  347. #define STATS_INC_ACTIVE(x) ((x)->num_active++)
  348. #define STATS_DEC_ACTIVE(x) ((x)->num_active--)
  349. #define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
  350. #define STATS_INC_GROWN(x) ((x)->grown++)
  351. #define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
  352. #define STATS_SET_HIGH(x) \
  353. do { \
  354. if ((x)->num_active > (x)->high_mark) \
  355. (x)->high_mark = (x)->num_active; \
  356. } while (0)
  357. #define STATS_INC_ERR(x) ((x)->errors++)
  358. #define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
  359. #define STATS_INC_NODEFREES(x) ((x)->node_frees++)
  360. #define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
  361. #define STATS_SET_FREEABLE(x, i) \
  362. do { \
  363. if ((x)->max_freeable < i) \
  364. (x)->max_freeable = i; \
  365. } while (0)
  366. #define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
  367. #define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
  368. #define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
  369. #define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
  370. #else
  371. #define STATS_INC_ACTIVE(x) do { } while (0)
  372. #define STATS_DEC_ACTIVE(x) do { } while (0)
  373. #define STATS_INC_ALLOCED(x) do { } while (0)
  374. #define STATS_INC_GROWN(x) do { } while (0)
  375. #define STATS_ADD_REAPED(x,y) do { (void)(y); } while (0)
  376. #define STATS_SET_HIGH(x) do { } while (0)
  377. #define STATS_INC_ERR(x) do { } while (0)
  378. #define STATS_INC_NODEALLOCS(x) do { } while (0)
  379. #define STATS_INC_NODEFREES(x) do { } while (0)
  380. #define STATS_INC_ACOVERFLOW(x) do { } while (0)
  381. #define STATS_SET_FREEABLE(x, i) do { } while (0)
  382. #define STATS_INC_ALLOCHIT(x) do { } while (0)
  383. #define STATS_INC_ALLOCMISS(x) do { } while (0)
  384. #define STATS_INC_FREEHIT(x) do { } while (0)
  385. #define STATS_INC_FREEMISS(x) do { } while (0)
  386. #endif
  387. #if DEBUG
  388. /*
  389. * memory layout of objects:
  390. * 0 : objp
  391. * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
  392. * the end of an object is aligned with the end of the real
  393. * allocation. Catches writes behind the end of the allocation.
  394. * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
  395. * redzone word.
  396. * cachep->obj_offset: The real object.
  397. * cachep->buffer_size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
  398. * cachep->buffer_size - 1* BYTES_PER_WORD: last caller address
  399. * [BYTES_PER_WORD long]
  400. */
  401. static int obj_offset(struct kmem_cache *cachep)
  402. {
  403. return cachep->obj_offset;
  404. }
  405. static int obj_size(struct kmem_cache *cachep)
  406. {
  407. return cachep->obj_size;
  408. }
  409. static unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
  410. {
  411. BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
  412. return (unsigned long long*) (objp + obj_offset(cachep) -
  413. sizeof(unsigned long long));
  414. }
  415. static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
  416. {
  417. BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
  418. if (cachep->flags & SLAB_STORE_USER)
  419. return (unsigned long long *)(objp + cachep->buffer_size -
  420. sizeof(unsigned long long) -
  421. REDZONE_ALIGN);
  422. return (unsigned long long *) (objp + cachep->buffer_size -
  423. sizeof(unsigned long long));
  424. }
  425. static void **dbg_userword(struct kmem_cache *cachep, void *objp)
  426. {
  427. BUG_ON(!(cachep->flags & SLAB_STORE_USER));
  428. return (void **)(objp + cachep->buffer_size - BYTES_PER_WORD);
  429. }
  430. #else
  431. #define obj_offset(x) 0
  432. #define obj_size(cachep) (cachep->buffer_size)
  433. #define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
  434. #define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
  435. #define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
  436. #endif
  437. #ifdef CONFIG_TRACING
  438. size_t slab_buffer_size(struct kmem_cache *cachep)
  439. {
  440. return cachep->buffer_size;
  441. }
  442. EXPORT_SYMBOL(slab_buffer_size);
  443. #endif
  444. /*
  445. * Do not go above this order unless 0 objects fit into the slab or
  446. * overridden on the command line.
  447. */
  448. #define SLAB_MAX_ORDER_HI 1
  449. #define SLAB_MAX_ORDER_LO 0
  450. static int slab_max_order = SLAB_MAX_ORDER_LO;
  451. static bool slab_max_order_set __initdata;
  452. /*
  453. * Functions for storing/retrieving the cachep and or slab from the page
  454. * allocator. These are used to find the slab an obj belongs to. With kfree(),
  455. * these are used to find the cache which an obj belongs to.
  456. */
  457. static inline void page_set_cache(struct page *page, struct kmem_cache *cache)
  458. {
  459. page->lru.next = (struct list_head *)cache;
  460. }
  461. static inline struct kmem_cache *page_get_cache(struct page *page)
  462. {
  463. page = compound_head(page);
  464. BUG_ON(!PageSlab(page));
  465. return (struct kmem_cache *)page->lru.next;
  466. }
  467. static inline void page_set_slab(struct page *page, struct slab *slab)
  468. {
  469. page->lru.prev = (struct list_head *)slab;
  470. }
  471. static inline struct slab *page_get_slab(struct page *page)
  472. {
  473. BUG_ON(!PageSlab(page));
  474. return (struct slab *)page->lru.prev;
  475. }
  476. static inline struct kmem_cache *virt_to_cache(const void *obj)
  477. {
  478. struct page *page = virt_to_head_page(obj);
  479. return page_get_cache(page);
  480. }
  481. static inline struct slab *virt_to_slab(const void *obj)
  482. {
  483. struct page *page = virt_to_head_page(obj);
  484. return page_get_slab(page);
  485. }
  486. static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
  487. unsigned int idx)
  488. {
  489. return slab->s_mem + cache->buffer_size * idx;
  490. }
  491. /*
  492. * We want to avoid an expensive divide : (offset / cache->buffer_size)
  493. * Using the fact that buffer_size is a constant for a particular cache,
  494. * we can replace (offset / cache->buffer_size) by
  495. * reciprocal_divide(offset, cache->reciprocal_buffer_size)
  496. */
  497. static inline unsigned int obj_to_index(const struct kmem_cache *cache,
  498. const struct slab *slab, void *obj)
  499. {
  500. u32 offset = (obj - slab->s_mem);
  501. return reciprocal_divide(offset, cache->reciprocal_buffer_size);
  502. }
  503. /*
  504. * These are the default caches for kmalloc. Custom caches can have other sizes.
  505. */
  506. struct cache_sizes malloc_sizes[] = {
  507. #define CACHE(x) { .cs_size = (x) },
  508. #include <linux/kmalloc_sizes.h>
  509. CACHE(ULONG_MAX)
  510. #undef CACHE
  511. };
  512. EXPORT_SYMBOL(malloc_sizes);
  513. /* Must match cache_sizes above. Out of line to keep cache footprint low. */
  514. struct cache_names {
  515. char *name;
  516. char *name_dma;
  517. };
  518. static struct cache_names __initdata cache_names[] = {
  519. #define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },
  520. #include <linux/kmalloc_sizes.h>
  521. {NULL,}
  522. #undef CACHE
  523. };
  524. static struct arraycache_init initarray_cache __initdata =
  525. { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
  526. static struct arraycache_init initarray_generic =
  527. { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
  528. /* internal cache of cache description objs */
  529. static struct kmem_list3 *cache_cache_nodelists[MAX_NUMNODES];
  530. static struct kmem_cache cache_cache = {
  531. .nodelists = cache_cache_nodelists,
  532. .batchcount = 1,
  533. .limit = BOOT_CPUCACHE_ENTRIES,
  534. .shared = 1,
  535. .buffer_size = sizeof(struct kmem_cache),
  536. .name = "kmem_cache",
  537. };
  538. #define BAD_ALIEN_MAGIC 0x01020304ul
  539. /*
  540. * chicken and egg problem: delay the per-cpu array allocation
  541. * until the general caches are up.
  542. */
  543. static enum {
  544. NONE,
  545. PARTIAL_AC,
  546. PARTIAL_L3,
  547. EARLY,
  548. LATE,
  549. FULL
  550. } g_cpucache_up;
  551. /*
  552. * used by boot code to determine if it can use slab based allocator
  553. */
  554. int slab_is_available(void)
  555. {
  556. return g_cpucache_up >= EARLY;
  557. }
  558. #ifdef CONFIG_LOCKDEP
  559. /*
  560. * Slab sometimes uses the kmalloc slabs to store the slab headers
  561. * for other slabs "off slab".
  562. * The locking for this is tricky in that it nests within the locks
  563. * of all other slabs in a few places; to deal with this special
  564. * locking we put on-slab caches into a separate lock-class.
  565. *
  566. * We set lock class for alien array caches which are up during init.
  567. * The lock annotation will be lost if all cpus of a node goes down and
  568. * then comes back up during hotplug
  569. */
  570. static struct lock_class_key on_slab_l3_key;
  571. static struct lock_class_key on_slab_alc_key;
  572. static struct lock_class_key debugobj_l3_key;
  573. static struct lock_class_key debugobj_alc_key;
  574. static void slab_set_lock_classes(struct kmem_cache *cachep,
  575. struct lock_class_key *l3_key, struct lock_class_key *alc_key,
  576. int q)
  577. {
  578. struct array_cache **alc;
  579. struct kmem_list3 *l3;
  580. int r;
  581. l3 = cachep->nodelists[q];
  582. if (!l3)
  583. return;
  584. lockdep_set_class(&l3->list_lock, l3_key);
  585. alc = l3->alien;
  586. /*
  587. * FIXME: This check for BAD_ALIEN_MAGIC
  588. * should go away when common slab code is taught to
  589. * work even without alien caches.
  590. * Currently, non NUMA code returns BAD_ALIEN_MAGIC
  591. * for alloc_alien_cache,
  592. */
  593. if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC)
  594. return;
  595. for_each_node(r) {
  596. if (alc[r])
  597. lockdep_set_class(&alc[r]->lock, alc_key);
  598. }
  599. }
  600. static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
  601. {
  602. slab_set_lock_classes(cachep, &debugobj_l3_key, &debugobj_alc_key, node);
  603. }
  604. static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
  605. {
  606. int node;
  607. for_each_online_node(node)
  608. slab_set_debugobj_lock_classes_node(cachep, node);
  609. }
  610. static void init_node_lock_keys(int q)
  611. {
  612. struct cache_sizes *s = malloc_sizes;
  613. if (g_cpucache_up < LATE)
  614. return;
  615. for (s = malloc_sizes; s->cs_size != ULONG_MAX; s++) {
  616. struct kmem_list3 *l3;
  617. l3 = s->cs_cachep->nodelists[q];
  618. if (!l3 || OFF_SLAB(s->cs_cachep))
  619. continue;
  620. slab_set_lock_classes(s->cs_cachep, &on_slab_l3_key,
  621. &on_slab_alc_key, q);
  622. }
  623. }
  624. static inline void init_lock_keys(void)
  625. {
  626. int node;
  627. for_each_node(node)
  628. init_node_lock_keys(node);
  629. }
  630. #else
  631. static void init_node_lock_keys(int q)
  632. {
  633. }
  634. static inline void init_lock_keys(void)
  635. {
  636. }
  637. static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
  638. {
  639. }
  640. static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
  641. {
  642. }
  643. #endif
  644. /*
  645. * Guard access to the cache-chain.
  646. */
  647. static DEFINE_MUTEX(cache_chain_mutex);
  648. static struct list_head cache_chain;
  649. static DEFINE_PER_CPU(struct delayed_work, slab_reap_work);
  650. static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
  651. {
  652. return cachep->array[smp_processor_id()];
  653. }
  654. static inline struct kmem_cache *__find_general_cachep(size_t size,
  655. gfp_t gfpflags)
  656. {
  657. struct cache_sizes *csizep = malloc_sizes;
  658. #if DEBUG
  659. /* This happens if someone tries to call
  660. * kmem_cache_create(), or __kmalloc(), before
  661. * the generic caches are initialized.
  662. */
  663. BUG_ON(malloc_sizes[INDEX_AC].cs_cachep == NULL);
  664. #endif
  665. if (!size)
  666. return ZERO_SIZE_PTR;
  667. while (size > csizep->cs_size)
  668. csizep++;
  669. /*
  670. * Really subtle: The last entry with cs->cs_size==ULONG_MAX
  671. * has cs_{dma,}cachep==NULL. Thus no special case
  672. * for large kmalloc calls required.
  673. */
  674. #ifdef CONFIG_ZONE_DMA
  675. if (unlikely(gfpflags & GFP_DMA))
  676. return csizep->cs_dmacachep;
  677. #endif
  678. return csizep->cs_cachep;
  679. }
  680. static struct kmem_cache *kmem_find_general_cachep(size_t size, gfp_t gfpflags)
  681. {
  682. return __find_general_cachep(size, gfpflags);
  683. }
  684. static size_t slab_mgmt_size(size_t nr_objs, size_t align)
  685. {
  686. return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align);
  687. }
  688. /*
  689. * Calculate the number of objects and left-over bytes for a given buffer size.
  690. */
  691. static void cache_estimate(unsigned long gfporder, size_t buffer_size,
  692. size_t align, int flags, size_t *left_over,
  693. unsigned int *num)
  694. {
  695. int nr_objs;
  696. size_t mgmt_size;
  697. size_t slab_size = PAGE_SIZE << gfporder;
  698. /*
  699. * The slab management structure can be either off the slab or
  700. * on it. For the latter case, the memory allocated for a
  701. * slab is used for:
  702. *
  703. * - The struct slab
  704. * - One kmem_bufctl_t for each object
  705. * - Padding to respect alignment of @align
  706. * - @buffer_size bytes for each object
  707. *
  708. * If the slab management structure is off the slab, then the
  709. * alignment will already be calculated into the size. Because
  710. * the slabs are all pages aligned, the objects will be at the
  711. * correct alignment when allocated.
  712. */
  713. if (flags & CFLGS_OFF_SLAB) {
  714. mgmt_size = 0;
  715. nr_objs = slab_size / buffer_size;
  716. if (nr_objs > SLAB_LIMIT)
  717. nr_objs = SLAB_LIMIT;
  718. } else {
  719. /*
  720. * Ignore padding for the initial guess. The padding
  721. * is at most @align-1 bytes, and @buffer_size is at
  722. * least @align. In the worst case, this result will
  723. * be one greater than the number of objects that fit
  724. * into the memory allocation when taking the padding
  725. * into account.
  726. */
  727. nr_objs = (slab_size - sizeof(struct slab)) /
  728. (buffer_size + sizeof(kmem_bufctl_t));
  729. /*
  730. * This calculated number will be either the right
  731. * amount, or one greater than what we want.
  732. */
  733. if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
  734. > slab_size)
  735. nr_objs--;
  736. if (nr_objs > SLAB_LIMIT)
  737. nr_objs = SLAB_LIMIT;
  738. mgmt_size = slab_mgmt_size(nr_objs, align);
  739. }
  740. *num = nr_objs;
  741. *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
  742. }
  743. #define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
  744. static void __slab_error(const char *function, struct kmem_cache *cachep,
  745. char *msg)
  746. {
  747. printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
  748. function, cachep->name, msg);
  749. dump_stack();
  750. }
  751. /*
  752. * By default on NUMA we use alien caches to stage the freeing of
  753. * objects allocated from other nodes. This causes massive memory
  754. * inefficiencies when using fake NUMA setup to split memory into a
  755. * large number of small nodes, so it can be disabled on the command
  756. * line
  757. */
  758. static int use_alien_caches __read_mostly = 1;
  759. static int __init noaliencache_setup(char *s)
  760. {
  761. use_alien_caches = 0;
  762. return 1;
  763. }
  764. __setup("noaliencache", noaliencache_setup);
  765. static int __init slab_max_order_setup(char *str)
  766. {
  767. get_option(&str, &slab_max_order);
  768. slab_max_order = slab_max_order < 0 ? 0 :
  769. min(slab_max_order, MAX_ORDER - 1);
  770. slab_max_order_set = true;
  771. return 1;
  772. }
  773. __setup("slab_max_order=", slab_max_order_setup);
  774. #ifdef CONFIG_NUMA
  775. /*
  776. * Special reaping functions for NUMA systems called from cache_reap().
  777. * These take care of doing round robin flushing of alien caches (containing
  778. * objects freed on different nodes from which they were allocated) and the
  779. * flushing of remote pcps by calling drain_node_pages.
  780. */
  781. static DEFINE_PER_CPU(unsigned long, slab_reap_node);
  782. static void init_reap_node(int cpu)
  783. {
  784. int node;
  785. node = next_node(cpu_to_mem(cpu), node_online_map);
  786. if (node == MAX_NUMNODES)
  787. node = first_node(node_online_map);
  788. per_cpu(slab_reap_node, cpu) = node;
  789. }
  790. static void next_reap_node(void)
  791. {
  792. int node = __this_cpu_read(slab_reap_node);
  793. node = next_node(node, node_online_map);
  794. if (unlikely(node >= MAX_NUMNODES))
  795. node = first_node(node_online_map);
  796. __this_cpu_write(slab_reap_node, node);
  797. }
  798. #else
  799. #define init_reap_node(cpu) do { } while (0)
  800. #define next_reap_node(void) do { } while (0)
  801. #endif
  802. /*
  803. * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
  804. * via the workqueue/eventd.
  805. * Add the CPU number into the expiration time to minimize the possibility of
  806. * the CPUs getting into lockstep and contending for the global cache chain
  807. * lock.
  808. */
  809. static void __cpuinit start_cpu_timer(int cpu)
  810. {
  811. struct delayed_work *reap_work = &per_cpu(slab_reap_work, cpu);
  812. /*
  813. * When this gets called from do_initcalls via cpucache_init(),
  814. * init_workqueues() has already run, so keventd will be setup
  815. * at that time.
  816. */
  817. if (keventd_up() && reap_work->work.func == NULL) {
  818. init_reap_node(cpu);
  819. INIT_DELAYED_WORK_DEFERRABLE(reap_work, cache_reap);
  820. schedule_delayed_work_on(cpu, reap_work,
  821. __round_jiffies_relative(HZ, cpu));
  822. }
  823. }
  824. static struct array_cache *alloc_arraycache(int node, int entries,
  825. int batchcount, gfp_t gfp)
  826. {
  827. int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
  828. struct array_cache *nc = NULL;
  829. nc = kmalloc_node(memsize, gfp, node);
  830. /*
  831. * The array_cache structures contain pointers to free object.
  832. * However, when such objects are allocated or transferred to another
  833. * cache the pointers are not cleared and they could be counted as
  834. * valid references during a kmemleak scan. Therefore, kmemleak must
  835. * not scan such objects.
  836. */
  837. kmemleak_no_scan(nc);
  838. if (nc) {
  839. nc->avail = 0;
  840. nc->limit = entries;
  841. nc->batchcount = batchcount;
  842. nc->touched = 0;
  843. spin_lock_init(&nc->lock);
  844. }
  845. return nc;
  846. }
  847. /*
  848. * Transfer objects in one arraycache to another.
  849. * Locking must be handled by the caller.
  850. *
  851. * Return the number of entries transferred.
  852. */
  853. static int transfer_objects(struct array_cache *to,
  854. struct array_cache *from, unsigned int max)
  855. {
  856. /* Figure out how many entries to transfer */
  857. int nr = min3(from->avail, max, to->limit - to->avail);
  858. if (!nr)
  859. return 0;
  860. memcpy(to->entry + to->avail, from->entry + from->avail -nr,
  861. sizeof(void *) *nr);
  862. from->avail -= nr;
  863. to->avail += nr;
  864. return nr;
  865. }
  866. #ifndef CONFIG_NUMA
  867. #define drain_alien_cache(cachep, alien) do { } while (0)
  868. #define reap_alien(cachep, l3) do { } while (0)
  869. static inline struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
  870. {
  871. return (struct array_cache **)BAD_ALIEN_MAGIC;
  872. }
  873. static inline void free_alien_cache(struct array_cache **ac_ptr)
  874. {
  875. }
  876. static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
  877. {
  878. return 0;
  879. }
  880. static inline void *alternate_node_alloc(struct kmem_cache *cachep,
  881. gfp_t flags)
  882. {
  883. return NULL;
  884. }
  885. static inline void *____cache_alloc_node(struct kmem_cache *cachep,
  886. gfp_t flags, int nodeid)
  887. {
  888. return NULL;
  889. }
  890. #else /* CONFIG_NUMA */
  891. static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
  892. static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
  893. static struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
  894. {
  895. struct array_cache **ac_ptr;
  896. int memsize = sizeof(void *) * nr_node_ids;
  897. int i;
  898. if (limit > 1)
  899. limit = 12;
  900. ac_ptr = kzalloc_node(memsize, gfp, node);
  901. if (ac_ptr) {
  902. for_each_node(i) {
  903. if (i == node || !node_online(i))
  904. continue;
  905. ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d, gfp);
  906. if (!ac_ptr[i]) {
  907. for (i--; i >= 0; i--)
  908. kfree(ac_ptr[i]);
  909. kfree(ac_ptr);
  910. return NULL;
  911. }
  912. }
  913. }
  914. return ac_ptr;
  915. }
  916. static void free_alien_cache(struct array_cache **ac_ptr)
  917. {
  918. int i;
  919. if (!ac_ptr)
  920. return;
  921. for_each_node(i)
  922. kfree(ac_ptr[i]);
  923. kfree(ac_ptr);
  924. }
  925. static void __drain_alien_cache(struct kmem_cache *cachep,
  926. struct array_cache *ac, int node)
  927. {
  928. struct kmem_list3 *rl3 = cachep->nodelists[node];
  929. if (ac->avail) {
  930. spin_lock(&rl3->list_lock);
  931. /*
  932. * Stuff objects into the remote nodes shared array first.
  933. * That way we could avoid the overhead of putting the objects
  934. * into the free lists and getting them back later.
  935. */
  936. if (rl3->shared)
  937. transfer_objects(rl3->shared, ac, ac->limit);
  938. free_block(cachep, ac->entry, ac->avail, node);
  939. ac->avail = 0;
  940. spin_unlock(&rl3->list_lock);
  941. }
  942. }
  943. /*
  944. * Called from cache_reap() to regularly drain alien caches round robin.
  945. */
  946. static void reap_alien(struct kmem_cache *cachep, struct kmem_list3 *l3)
  947. {
  948. int node = __this_cpu_read(slab_reap_node);
  949. if (l3->alien) {
  950. struct array_cache *ac = l3->alien[node];
  951. if (ac && ac->avail && spin_trylock_irq(&ac->lock)) {
  952. __drain_alien_cache(cachep, ac, node);
  953. spin_unlock_irq(&ac->lock);
  954. }
  955. }
  956. }
  957. static void drain_alien_cache(struct kmem_cache *cachep,
  958. struct array_cache **alien)
  959. {
  960. int i = 0;
  961. struct array_cache *ac;
  962. unsigned long flags;
  963. for_each_online_node(i) {
  964. ac = alien[i];
  965. if (ac) {
  966. spin_lock_irqsave(&ac->lock, flags);
  967. __drain_alien_cache(cachep, ac, i);
  968. spin_unlock_irqrestore(&ac->lock, flags);
  969. }
  970. }
  971. }
  972. static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
  973. {
  974. struct slab *slabp = virt_to_slab(objp);
  975. int nodeid = slabp->nodeid;
  976. struct kmem_list3 *l3;
  977. struct array_cache *alien = NULL;
  978. int node;
  979. node = numa_mem_id();
  980. /*
  981. * Make sure we are not freeing a object from another node to the array
  982. * cache on this cpu.
  983. */
  984. if (likely(slabp->nodeid == node))
  985. return 0;
  986. l3 = cachep->nodelists[node];
  987. STATS_INC_NODEFREES(cachep);
  988. if (l3->alien && l3->alien[nodeid]) {
  989. alien = l3->alien[nodeid];
  990. spin_lock(&alien->lock);
  991. if (unlikely(alien->avail == alien->limit)) {
  992. STATS_INC_ACOVERFLOW(cachep);
  993. __drain_alien_cache(cachep, alien, nodeid);
  994. }
  995. alien->entry[alien->avail++] = objp;
  996. spin_unlock(&alien->lock);
  997. } else {
  998. spin_lock(&(cachep->nodelists[nodeid])->list_lock);
  999. free_block(cachep, &objp, 1, nodeid);
  1000. spin_unlock(&(cachep->nodelists[nodeid])->list_lock);
  1001. }
  1002. return 1;
  1003. }
  1004. #endif
  1005. /*
  1006. * Allocates and initializes nodelists for a node on each slab cache, used for
  1007. * either memory or cpu hotplug. If memory is being hot-added, the kmem_list3
  1008. * will be allocated off-node since memory is not yet online for the new node.
  1009. * When hotplugging memory or a cpu, existing nodelists are not replaced if
  1010. * already in use.
  1011. *
  1012. * Must hold cache_chain_mutex.
  1013. */
  1014. static int init_cache_nodelists_node(int node)
  1015. {
  1016. struct kmem_cache *cachep;
  1017. struct kmem_list3 *l3;
  1018. const int memsize = sizeof(struct kmem_list3);
  1019. list_for_each_entry(cachep, &cache_chain, next) {
  1020. /*
  1021. * Set up the size64 kmemlist for cpu before we can
  1022. * begin anything. Make sure some other cpu on this
  1023. * node has not already allocated this
  1024. */
  1025. if (!cachep->nodelists[node]) {
  1026. l3 = kmalloc_node(memsize, GFP_KERNEL, node);
  1027. if (!l3)
  1028. return -ENOMEM;
  1029. kmem_list3_init(l3);
  1030. l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
  1031. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  1032. /*
  1033. * The l3s don't come and go as CPUs come and
  1034. * go. cache_chain_mutex is sufficient
  1035. * protection here.
  1036. */
  1037. cachep->nodelists[node] = l3;
  1038. }
  1039. spin_lock_irq(&cachep->nodelists[node]->list_lock);
  1040. cachep->nodelists[node]->free_limit =
  1041. (1 + nr_cpus_node(node)) *
  1042. cachep->batchcount + cachep->num;
  1043. spin_unlock_irq(&cachep->nodelists[node]->list_lock);
  1044. }
  1045. return 0;
  1046. }
  1047. static void __cpuinit cpuup_canceled(long cpu)
  1048. {
  1049. struct kmem_cache *cachep;
  1050. struct kmem_list3 *l3 = NULL;
  1051. int node = cpu_to_mem(cpu);
  1052. const struct cpumask *mask = cpumask_of_node(node);
  1053. list_for_each_entry(cachep, &cache_chain, next) {
  1054. struct array_cache *nc;
  1055. struct array_cache *shared;
  1056. struct array_cache **alien;
  1057. /* cpu is dead; no one can alloc from it. */
  1058. nc = cachep->array[cpu];
  1059. cachep->array[cpu] = NULL;
  1060. l3 = cachep->nodelists[node];
  1061. if (!l3)
  1062. goto free_array_cache;
  1063. spin_lock_irq(&l3->list_lock);
  1064. /* Free limit for this kmem_list3 */
  1065. l3->free_limit -= cachep->batchcount;
  1066. if (nc)
  1067. free_block(cachep, nc->entry, nc->avail, node);
  1068. if (!cpumask_empty(mask)) {
  1069. spin_unlock_irq(&l3->list_lock);
  1070. goto free_array_cache;
  1071. }
  1072. shared = l3->shared;
  1073. if (shared) {
  1074. free_block(cachep, shared->entry,
  1075. shared->avail, node);
  1076. l3->shared = NULL;
  1077. }
  1078. alien = l3->alien;
  1079. l3->alien = NULL;
  1080. spin_unlock_irq(&l3->list_lock);
  1081. kfree(shared);
  1082. if (alien) {
  1083. drain_alien_cache(cachep, alien);
  1084. free_alien_cache(alien);
  1085. }
  1086. free_array_cache:
  1087. kfree(nc);
  1088. }
  1089. /*
  1090. * In the previous loop, all the objects were freed to
  1091. * the respective cache's slabs, now we can go ahead and
  1092. * shrink each nodelist to its limit.
  1093. */
  1094. list_for_each_entry(cachep, &cache_chain, next) {
  1095. l3 = cachep->nodelists[node];
  1096. if (!l3)
  1097. continue;
  1098. drain_freelist(cachep, l3, l3->free_objects);
  1099. }
  1100. }
  1101. static int __cpuinit cpuup_prepare(long cpu)
  1102. {
  1103. struct kmem_cache *cachep;
  1104. struct kmem_list3 *l3 = NULL;
  1105. int node = cpu_to_mem(cpu);
  1106. int err;
  1107. /*
  1108. * We need to do this right in the beginning since
  1109. * alloc_arraycache's are going to use this list.
  1110. * kmalloc_node allows us to add the slab to the right
  1111. * kmem_list3 and not this cpu's kmem_list3
  1112. */
  1113. err = init_cache_nodelists_node(node);
  1114. if (err < 0)
  1115. goto bad;
  1116. /*
  1117. * Now we can go ahead with allocating the shared arrays and
  1118. * array caches
  1119. */
  1120. list_for_each_entry(cachep, &cache_chain, next) {
  1121. struct array_cache *nc;
  1122. struct array_cache *shared = NULL;
  1123. struct array_cache **alien = NULL;
  1124. nc = alloc_arraycache(node, cachep->limit,
  1125. cachep->batchcount, GFP_KERNEL);
  1126. if (!nc)
  1127. goto bad;
  1128. if (cachep->shared) {
  1129. shared = alloc_arraycache(node,
  1130. cachep->shared * cachep->batchcount,
  1131. 0xbaadf00d, GFP_KERNEL);
  1132. if (!shared) {
  1133. kfree(nc);
  1134. goto bad;
  1135. }
  1136. }
  1137. if (use_alien_caches) {
  1138. alien = alloc_alien_cache(node, cachep->limit, GFP_KERNEL);
  1139. if (!alien) {
  1140. kfree(shared);
  1141. kfree(nc);
  1142. goto bad;
  1143. }
  1144. }
  1145. cachep->array[cpu] = nc;
  1146. l3 = cachep->nodelists[node];
  1147. BUG_ON(!l3);
  1148. spin_lock_irq(&l3->list_lock);
  1149. if (!l3->shared) {
  1150. /*
  1151. * We are serialised from CPU_DEAD or
  1152. * CPU_UP_CANCELLED by the cpucontrol lock
  1153. */
  1154. l3->shared = shared;
  1155. shared = NULL;
  1156. }
  1157. #ifdef CONFIG_NUMA
  1158. if (!l3->alien) {
  1159. l3->alien = alien;
  1160. alien = NULL;
  1161. }
  1162. #endif
  1163. spin_unlock_irq(&l3->list_lock);
  1164. kfree(shared);
  1165. free_alien_cache(alien);
  1166. if (cachep->flags & SLAB_DEBUG_OBJECTS)
  1167. slab_set_debugobj_lock_classes_node(cachep, node);
  1168. }
  1169. init_node_lock_keys(node);
  1170. return 0;
  1171. bad:
  1172. cpuup_canceled(cpu);
  1173. return -ENOMEM;
  1174. }
  1175. static int __cpuinit cpuup_callback(struct notifier_block *nfb,
  1176. unsigned long action, void *hcpu)
  1177. {
  1178. long cpu = (long)hcpu;
  1179. int err = 0;
  1180. switch (action) {
  1181. case CPU_UP_PREPARE:
  1182. case CPU_UP_PREPARE_FROZEN:
  1183. mutex_lock(&cache_chain_mutex);
  1184. err = cpuup_prepare(cpu);
  1185. mutex_unlock(&cache_chain_mutex);
  1186. break;
  1187. case CPU_ONLINE:
  1188. case CPU_ONLINE_FROZEN:
  1189. start_cpu_timer(cpu);
  1190. break;
  1191. #ifdef CONFIG_HOTPLUG_CPU
  1192. case CPU_DOWN_PREPARE:
  1193. case CPU_DOWN_PREPARE_FROZEN:
  1194. /*
  1195. * Shutdown cache reaper. Note that the cache_chain_mutex is
  1196. * held so that if cache_reap() is invoked it cannot do
  1197. * anything expensive but will only modify reap_work
  1198. * and reschedule the timer.
  1199. */
  1200. cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu));
  1201. /* Now the cache_reaper is guaranteed to be not running. */
  1202. per_cpu(slab_reap_work, cpu).work.func = NULL;
  1203. break;
  1204. case CPU_DOWN_FAILED:
  1205. case CPU_DOWN_FAILED_FROZEN:
  1206. start_cpu_timer(cpu);
  1207. break;
  1208. case CPU_DEAD:
  1209. case CPU_DEAD_FROZEN:
  1210. /*
  1211. * Even if all the cpus of a node are down, we don't free the
  1212. * kmem_list3 of any cache. This to avoid a race between
  1213. * cpu_down, and a kmalloc allocation from another cpu for
  1214. * memory from the node of the cpu going down. The list3
  1215. * structure is usually allocated from kmem_cache_create() and
  1216. * gets destroyed at kmem_cache_destroy().
  1217. */
  1218. /* fall through */
  1219. #endif
  1220. case CPU_UP_CANCELED:
  1221. case CPU_UP_CANCELED_FROZEN:
  1222. mutex_lock(&cache_chain_mutex);
  1223. cpuup_canceled(cpu);
  1224. mutex_unlock(&cache_chain_mutex);
  1225. break;
  1226. }
  1227. return notifier_from_errno(err);
  1228. }
  1229. static struct notifier_block __cpuinitdata cpucache_notifier = {
  1230. &cpuup_callback, NULL, 0
  1231. };
  1232. #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
  1233. /*
  1234. * Drains freelist for a node on each slab cache, used for memory hot-remove.
  1235. * Returns -EBUSY if all objects cannot be drained so that the node is not
  1236. * removed.
  1237. *
  1238. * Must hold cache_chain_mutex.
  1239. */
  1240. static int __meminit drain_cache_nodelists_node(int node)
  1241. {
  1242. struct kmem_cache *cachep;
  1243. int ret = 0;
  1244. list_for_each_entry(cachep, &cache_chain, next) {
  1245. struct kmem_list3 *l3;
  1246. l3 = cachep->nodelists[node];
  1247. if (!l3)
  1248. continue;
  1249. drain_freelist(cachep, l3, l3->free_objects);
  1250. if (!list_empty(&l3->slabs_full) ||
  1251. !list_empty(&l3->slabs_partial)) {
  1252. ret = -EBUSY;
  1253. break;
  1254. }
  1255. }
  1256. return ret;
  1257. }
  1258. static int __meminit slab_memory_callback(struct notifier_block *self,
  1259. unsigned long action, void *arg)
  1260. {
  1261. struct memory_notify *mnb = arg;
  1262. int ret = 0;
  1263. int nid;
  1264. nid = mnb->status_change_nid;
  1265. if (nid < 0)
  1266. goto out;
  1267. switch (action) {
  1268. case MEM_GOING_ONLINE:
  1269. mutex_lock(&cache_chain_mutex);
  1270. ret = init_cache_nodelists_node(nid);
  1271. mutex_unlock(&cache_chain_mutex);
  1272. break;
  1273. case MEM_GOING_OFFLINE:
  1274. mutex_lock(&cache_chain_mutex);
  1275. ret = drain_cache_nodelists_node(nid);
  1276. mutex_unlock(&cache_chain_mutex);
  1277. break;
  1278. case MEM_ONLINE:
  1279. case MEM_OFFLINE:
  1280. case MEM_CANCEL_ONLINE:
  1281. case MEM_CANCEL_OFFLINE:
  1282. break;
  1283. }
  1284. out:
  1285. return notifier_from_errno(ret);
  1286. }
  1287. #endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
  1288. /*
  1289. * swap the static kmem_list3 with kmalloced memory
  1290. */
  1291. static void __init init_list(struct kmem_cache *cachep, struct kmem_list3 *list,
  1292. int nodeid)
  1293. {
  1294. struct kmem_list3 *ptr;
  1295. ptr = kmalloc_node(sizeof(struct kmem_list3), GFP_NOWAIT, nodeid);
  1296. BUG_ON(!ptr);
  1297. memcpy(ptr, list, sizeof(struct kmem_list3));
  1298. /*
  1299. * Do not assume that spinlocks can be initialized via memcpy:
  1300. */
  1301. spin_lock_init(&ptr->list_lock);
  1302. MAKE_ALL_LISTS(cachep, ptr, nodeid);
  1303. cachep->nodelists[nodeid] = ptr;
  1304. }
  1305. /*
  1306. * For setting up all the kmem_list3s for cache whose buffer_size is same as
  1307. * size of kmem_list3.
  1308. */
  1309. static void __init set_up_list3s(struct kmem_cache *cachep, int index)
  1310. {
  1311. int node;
  1312. for_each_online_node(node) {
  1313. cachep->nodelists[node] = &initkmem_list3[index + node];
  1314. cachep->nodelists[node]->next_reap = jiffies +
  1315. REAPTIMEOUT_LIST3 +
  1316. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  1317. }
  1318. }
  1319. /*
  1320. * Initialisation. Called after the page allocator have been initialised and
  1321. * before smp_init().
  1322. */
  1323. void __init kmem_cache_init(void)
  1324. {
  1325. size_t left_over;
  1326. struct cache_sizes *sizes;
  1327. struct cache_names *names;
  1328. int i;
  1329. int order;
  1330. int node;
  1331. if (num_possible_nodes() == 1)
  1332. use_alien_caches = 0;
  1333. for (i = 0; i < NUM_INIT_LISTS; i++) {
  1334. kmem_list3_init(&initkmem_list3[i]);
  1335. if (i < MAX_NUMNODES)
  1336. cache_cache.nodelists[i] = NULL;
  1337. }
  1338. set_up_list3s(&cache_cache, CACHE_CACHE);
  1339. /*
  1340. * Fragmentation resistance on low memory - only use bigger
  1341. * page orders on machines with more than 32MB of memory if
  1342. * not overridden on the command line.
  1343. */
  1344. if (!slab_max_order_set && totalram_pages > (32 << 20) >> PAGE_SHIFT)
  1345. slab_max_order = SLAB_MAX_ORDER_HI;
  1346. /* Bootstrap is tricky, because several objects are allocated
  1347. * from caches that do not exist yet:
  1348. * 1) initialize the cache_cache cache: it contains the struct
  1349. * kmem_cache structures of all caches, except cache_cache itself:
  1350. * cache_cache is statically allocated.
  1351. * Initially an __init data area is used for the head array and the
  1352. * kmem_list3 structures, it's replaced with a kmalloc allocated
  1353. * array at the end of the bootstrap.
  1354. * 2) Create the first kmalloc cache.
  1355. * The struct kmem_cache for the new cache is allocated normally.
  1356. * An __init data area is used for the head array.
  1357. * 3) Create the remaining kmalloc caches, with minimally sized
  1358. * head arrays.
  1359. * 4) Replace the __init data head arrays for cache_cache and the first
  1360. * kmalloc cache with kmalloc allocated arrays.
  1361. * 5) Replace the __init data for kmem_list3 for cache_cache and
  1362. * the other cache's with kmalloc allocated memory.
  1363. * 6) Resize the head arrays of the kmalloc caches to their final sizes.
  1364. */
  1365. node = numa_mem_id();
  1366. /* 1) create the cache_cache */
  1367. INIT_LIST_HEAD(&cache_chain);
  1368. list_add(&cache_cache.next, &cache_chain);
  1369. cache_cache.colour_off = cache_line_size();
  1370. cache_cache.array[smp_processor_id()] = &initarray_cache.cache;
  1371. cache_cache.nodelists[node] = &initkmem_list3[CACHE_CACHE + node];
  1372. /*
  1373. * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
  1374. */
  1375. cache_cache.buffer_size = offsetof(struct kmem_cache, array[nr_cpu_ids]) +
  1376. nr_node_ids * sizeof(struct kmem_list3 *);
  1377. #if DEBUG
  1378. cache_cache.obj_size = cache_cache.buffer_size;
  1379. #endif
  1380. cache_cache.buffer_size = ALIGN(cache_cache.buffer_size,
  1381. cache_line_size());
  1382. cache_cache.reciprocal_buffer_size =
  1383. reciprocal_value(cache_cache.buffer_size);
  1384. for (order = 0; order < MAX_ORDER; order++) {
  1385. cache_estimate(order, cache_cache.buffer_size,
  1386. cache_line_size(), 0, &left_over, &cache_cache.num);
  1387. if (cache_cache.num)
  1388. break;
  1389. }
  1390. BUG_ON(!cache_cache.num);
  1391. cache_cache.gfporder = order;
  1392. cache_cache.colour = left_over / cache_cache.colour_off;
  1393. cache_cache.slab_size = ALIGN(cache_cache.num * sizeof(kmem_bufctl_t) +
  1394. sizeof(struct slab), cache_line_size());
  1395. /* 2+3) create the kmalloc caches */
  1396. sizes = malloc_sizes;
  1397. names = cache_names;
  1398. /*
  1399. * Initialize the caches that provide memory for the array cache and the
  1400. * kmem_list3 structures first. Without this, further allocations will
  1401. * bug.
  1402. */
  1403. sizes[INDEX_AC].cs_cachep = kmem_cache_create(names[INDEX_AC].name,
  1404. sizes[INDEX_AC].cs_size,
  1405. ARCH_KMALLOC_MINALIGN,
  1406. ARCH_KMALLOC_FLAGS|SLAB_PANIC,
  1407. NULL);
  1408. if (INDEX_AC != INDEX_L3) {
  1409. sizes[INDEX_L3].cs_cachep =
  1410. kmem_cache_create(names[INDEX_L3].name,
  1411. sizes[INDEX_L3].cs_size,
  1412. ARCH_KMALLOC_MINALIGN,
  1413. ARCH_KMALLOC_FLAGS|SLAB_PANIC,
  1414. NULL);
  1415. }
  1416. slab_early_init = 0;
  1417. while (sizes->cs_size != ULONG_MAX) {
  1418. /*
  1419. * For performance, all the general caches are L1 aligned.
  1420. * This should be particularly beneficial on SMP boxes, as it
  1421. * eliminates "false sharing".
  1422. * Note for systems short on memory removing the alignment will
  1423. * allow tighter packing of the smaller caches.
  1424. */
  1425. if (!sizes->cs_cachep) {
  1426. sizes->cs_cachep = kmem_cache_create(names->name,
  1427. sizes->cs_size,
  1428. ARCH_KMALLOC_MINALIGN,
  1429. ARCH_KMALLOC_FLAGS|SLAB_PANIC,
  1430. NULL);
  1431. }
  1432. #ifdef CONFIG_ZONE_DMA
  1433. sizes->cs_dmacachep = kmem_cache_create(
  1434. names->name_dma,
  1435. sizes->cs_size,
  1436. ARCH_KMALLOC_MINALIGN,
  1437. ARCH_KMALLOC_FLAGS|SLAB_CACHE_DMA|
  1438. SLAB_PANIC,
  1439. NULL);
  1440. #endif
  1441. sizes++;
  1442. names++;
  1443. }
  1444. /* 4) Replace the bootstrap head arrays */
  1445. {
  1446. struct array_cache *ptr;
  1447. ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
  1448. BUG_ON(cpu_cache_get(&cache_cache) != &initarray_cache.cache);
  1449. memcpy(ptr, cpu_cache_get(&cache_cache),
  1450. sizeof(struct arraycache_init));
  1451. /*
  1452. * Do not assume that spinlocks can be initialized via memcpy:
  1453. */
  1454. spin_lock_init(&ptr->lock);
  1455. cache_cache.array[smp_processor_id()] = ptr;
  1456. ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
  1457. BUG_ON(cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep)
  1458. != &initarray_generic.cache);
  1459. memcpy(ptr, cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep),
  1460. sizeof(struct arraycache_init));
  1461. /*
  1462. * Do not assume that spinlocks can be initialized via memcpy:
  1463. */
  1464. spin_lock_init(&ptr->lock);
  1465. malloc_sizes[INDEX_AC].cs_cachep->array[smp_processor_id()] =
  1466. ptr;
  1467. }
  1468. /* 5) Replace the bootstrap kmem_list3's */
  1469. {
  1470. int nid;
  1471. for_each_online_node(nid) {
  1472. init_list(&cache_cache, &initkmem_list3[CACHE_CACHE + nid], nid);
  1473. init_list(malloc_sizes[INDEX_AC].cs_cachep,
  1474. &initkmem_list3[SIZE_AC + nid], nid);
  1475. if (INDEX_AC != INDEX_L3) {
  1476. init_list(malloc_sizes[INDEX_L3].cs_cachep,
  1477. &initkmem_list3[SIZE_L3 + nid], nid);
  1478. }
  1479. }
  1480. }
  1481. g_cpucache_up = EARLY;
  1482. }
  1483. void __init kmem_cache_init_late(void)
  1484. {
  1485. struct kmem_cache *cachep;
  1486. g_cpucache_up = LATE;
  1487. /* 6) resize the head arrays to their final sizes */
  1488. mutex_lock(&cache_chain_mutex);
  1489. list_for_each_entry(cachep, &cache_chain, next)
  1490. if (enable_cpucache(cachep, GFP_NOWAIT))
  1491. BUG();
  1492. mutex_unlock(&cache_chain_mutex);
  1493. /* Annotate slab for lockdep -- annotate the malloc caches */
  1494. init_lock_keys();
  1495. /* Done! */
  1496. g_cpucache_up = FULL;
  1497. /*
  1498. * Register a cpu startup notifier callback that initializes
  1499. * cpu_cache_get for all new cpus
  1500. */
  1501. register_cpu_notifier(&cpucache_notifier);
  1502. #ifdef CONFIG_NUMA
  1503. /*
  1504. * Register a memory hotplug callback that initializes and frees
  1505. * nodelists.
  1506. */
  1507. hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
  1508. #endif
  1509. /*
  1510. * The reap timers are started later, with a module init call: That part
  1511. * of the kernel is not yet operational.
  1512. */
  1513. }
  1514. static int __init cpucache_init(void)
  1515. {
  1516. int cpu;
  1517. /*
  1518. * Register the timers that return unneeded pages to the page allocator
  1519. */
  1520. for_each_online_cpu(cpu)
  1521. start_cpu_timer(cpu);
  1522. return 0;
  1523. }
  1524. __initcall(cpucache_init);
  1525. static noinline void
  1526. slab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid)
  1527. {
  1528. struct kmem_list3 *l3;
  1529. struct slab *slabp;
  1530. unsigned long flags;
  1531. int node;
  1532. printk(KERN_WARNING
  1533. "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n",
  1534. nodeid, gfpflags);
  1535. printk(KERN_WARNING " cache: %s, object size: %d, order: %d\n",
  1536. cachep->name, cachep->buffer_size, cachep->gfporder);
  1537. for_each_online_node(node) {
  1538. unsigned long active_objs = 0, num_objs = 0, free_objects = 0;
  1539. unsigned long active_slabs = 0, num_slabs = 0;
  1540. l3 = cachep->nodelists[node];
  1541. if (!l3)
  1542. continue;
  1543. spin_lock_irqsave(&l3->list_lock, flags);
  1544. list_for_each_entry(slabp, &l3->slabs_full, list) {
  1545. active_objs += cachep->num;
  1546. active_slabs++;
  1547. }
  1548. list_for_each_entry(slabp, &l3->slabs_partial, list) {
  1549. active_objs += slabp->inuse;
  1550. active_slabs++;
  1551. }
  1552. list_for_each_entry(slabp, &l3->slabs_free, list)
  1553. num_slabs++;
  1554. free_objects += l3->free_objects;
  1555. spin_unlock_irqrestore(&l3->list_lock, flags);
  1556. num_slabs += active_slabs;
  1557. num_objs = num_slabs * cachep->num;
  1558. printk(KERN_WARNING
  1559. " node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
  1560. node, active_slabs, num_slabs, active_objs, num_objs,
  1561. free_objects);
  1562. }
  1563. }
  1564. /*
  1565. * Interface to system's page allocator. No need to hold the cache-lock.
  1566. *
  1567. * If we requested dmaable memory, we will get it. Even if we
  1568. * did not request dmaable memory, we might get it, but that
  1569. * would be relatively rare and ignorable.
  1570. */
  1571. static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
  1572. {
  1573. struct page *page;
  1574. int nr_pages;
  1575. int i;
  1576. #ifndef CONFIG_MMU
  1577. /*
  1578. * Nommu uses slab's for process anonymous memory allocations, and thus
  1579. * requires __GFP_COMP to properly refcount higher order allocations
  1580. */
  1581. flags |= __GFP_COMP;
  1582. #endif
  1583. flags |= cachep->gfpflags;
  1584. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1585. flags |= __GFP_RECLAIMABLE;
  1586. page = alloc_pages_exact_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
  1587. if (!page) {
  1588. if (!(flags & __GFP_NOWARN) && printk_ratelimit())
  1589. slab_out_of_memory(cachep, flags, nodeid);
  1590. return NULL;
  1591. }
  1592. nr_pages = (1 << cachep->gfporder);
  1593. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1594. add_zone_page_state(page_zone(page),
  1595. NR_SLAB_RECLAIMABLE, nr_pages);
  1596. else
  1597. add_zone_page_state(page_zone(page),
  1598. NR_SLAB_UNRECLAIMABLE, nr_pages);
  1599. for (i = 0; i < nr_pages; i++)
  1600. __SetPageSlab(page + i);
  1601. if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
  1602. kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
  1603. if (cachep->ctor)
  1604. kmemcheck_mark_uninitialized_pages(page, nr_pages);
  1605. else
  1606. kmemcheck_mark_unallocated_pages(page, nr_pages);
  1607. }
  1608. return page_address(page);
  1609. }
  1610. /*
  1611. * Interface to system's page release.
  1612. */
  1613. static void kmem_freepages(struct kmem_cache *cachep, void *addr)
  1614. {
  1615. unsigned long i = (1 << cachep->gfporder);
  1616. struct page *page = virt_to_page(addr);
  1617. const unsigned long nr_freed = i;
  1618. kmemcheck_free_shadow(page, cachep->gfporder);
  1619. if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
  1620. sub_zone_page_state(page_zone(page),
  1621. NR_SLAB_RECLAIMABLE, nr_freed);
  1622. else
  1623. sub_zone_page_state(page_zone(page),
  1624. NR_SLAB_UNRECLAIMABLE, nr_freed);
  1625. while (i--) {
  1626. BUG_ON(!PageSlab(page));
  1627. __ClearPageSlab(page);
  1628. page++;
  1629. }
  1630. if (current->reclaim_state)
  1631. current->reclaim_state->reclaimed_slab += nr_freed;
  1632. free_pages((unsigned long)addr, cachep->gfporder);
  1633. }
  1634. static void kmem_rcu_free(struct rcu_head *head)
  1635. {
  1636. struct slab_rcu *slab_rcu = (struct slab_rcu *)head;
  1637. struct kmem_cache *cachep = slab_rcu->cachep;
  1638. kmem_freepages(cachep, slab_rcu->addr);
  1639. if (OFF_SLAB(cachep))
  1640. kmem_cache_free(cachep->slabp_cache, slab_rcu);
  1641. }
  1642. #if DEBUG
  1643. #ifdef CONFIG_DEBUG_PAGEALLOC
  1644. static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
  1645. unsigned long caller)
  1646. {
  1647. int size = obj_size(cachep);
  1648. addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
  1649. if (size < 5 * sizeof(unsigned long))
  1650. return;
  1651. *addr++ = 0x12345678;
  1652. *addr++ = caller;
  1653. *addr++ = smp_processor_id();
  1654. size -= 3 * sizeof(unsigned long);
  1655. {
  1656. unsigned long *sptr = &caller;
  1657. unsigned long svalue;
  1658. while (!kstack_end(sptr)) {
  1659. svalue = *sptr++;
  1660. if (kernel_text_address(svalue)) {
  1661. *addr++ = svalue;
  1662. size -= sizeof(unsigned long);
  1663. if (size <= sizeof(unsigned long))
  1664. break;
  1665. }
  1666. }
  1667. }
  1668. *addr++ = 0x87654321;
  1669. }
  1670. #endif
  1671. static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
  1672. {
  1673. int size = obj_size(cachep);
  1674. addr = &((char *)addr)[obj_offset(cachep)];
  1675. memset(addr, val, size);
  1676. *(unsigned char *)(addr + size - 1) = POISON_END;
  1677. }
  1678. static void dump_line(char *data, int offset, int limit)
  1679. {
  1680. int i;
  1681. unsigned char error = 0;
  1682. int bad_count = 0;
  1683. printk(KERN_ERR "%03x: ", offset);
  1684. for (i = 0; i < limit; i++) {
  1685. if (data[offset + i] != POISON_FREE) {
  1686. error = data[offset + i];
  1687. bad_count++;
  1688. }
  1689. }
  1690. print_hex_dump(KERN_CONT, "", 0, 16, 1,
  1691. &data[offset], limit, 1);
  1692. if (bad_count == 1) {
  1693. error ^= POISON_FREE;
  1694. if (!(error & (error - 1))) {
  1695. printk(KERN_ERR "Single bit error detected. Probably "
  1696. "bad RAM.\n");
  1697. #ifdef CONFIG_X86
  1698. printk(KERN_ERR "Run memtest86+ or a similar memory "
  1699. "test tool.\n");
  1700. #else
  1701. printk(KERN_ERR "Run a memory test tool.\n");
  1702. #endif
  1703. }
  1704. }
  1705. }
  1706. #endif
  1707. #if DEBUG
  1708. static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
  1709. {
  1710. int i, size;
  1711. char *realobj;
  1712. if (cachep->flags & SLAB_RED_ZONE) {
  1713. printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n",
  1714. *dbg_redzone1(cachep, objp),
  1715. *dbg_redzone2(cachep, objp));
  1716. }
  1717. if (cachep->flags & SLAB_STORE_USER) {
  1718. printk(KERN_ERR "Last user: [<%p>]",
  1719. *dbg_userword(cachep, objp));
  1720. print_symbol("(%s)",
  1721. (unsigned long)*dbg_userword(cachep, objp));
  1722. printk("\n");
  1723. }
  1724. realobj = (char *)objp + obj_offset(cachep);
  1725. size = obj_size(cachep);
  1726. for (i = 0; i < size && lines; i += 16, lines--) {
  1727. int limit;
  1728. limit = 16;
  1729. if (i + limit > size)
  1730. limit = size - i;
  1731. dump_line(realobj, i, limit);
  1732. }
  1733. }
  1734. static void check_poison_obj(struct kmem_cache *cachep, void *objp)
  1735. {
  1736. char *realobj;
  1737. int size, i;
  1738. int lines = 0;
  1739. realobj = (char *)objp + obj_offset(cachep);
  1740. size = obj_size(cachep);
  1741. for (i = 0; i < size; i++) {
  1742. char exp = POISON_FREE;
  1743. if (i == size - 1)
  1744. exp = POISON_END;
  1745. if (realobj[i] != exp) {
  1746. int limit;
  1747. /* Mismatch ! */
  1748. /* Print header */
  1749. if (lines == 0) {
  1750. printk(KERN_ERR
  1751. "Slab corruption (%s): %s start=%p, len=%d\n",
  1752. print_tainted(), cachep->name, realobj, size);
  1753. print_objinfo(cachep, objp, 0);
  1754. }
  1755. /* Hexdump the affected line */
  1756. i = (i / 16) * 16;
  1757. limit = 16;
  1758. if (i + limit > size)
  1759. limit = size - i;
  1760. dump_line(realobj, i, limit);
  1761. i += 16;
  1762. lines++;
  1763. /* Limit to 5 lines */
  1764. if (lines > 5)
  1765. break;
  1766. }
  1767. }
  1768. if (lines != 0) {
  1769. /* Print some data about the neighboring objects, if they
  1770. * exist:
  1771. */
  1772. struct slab *slabp = virt_to_slab(objp);
  1773. unsigned int objnr;
  1774. objnr = obj_to_index(cachep, slabp, objp);
  1775. if (objnr) {
  1776. objp = index_to_obj(cachep, slabp, objnr - 1);
  1777. realobj = (char *)objp + obj_offset(cachep);
  1778. printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
  1779. realobj, size);
  1780. print_objinfo(cachep, objp, 2);
  1781. }
  1782. if (objnr + 1 < cachep->num) {
  1783. objp = index_to_obj(cachep, slabp, objnr + 1);
  1784. realobj = (char *)objp + obj_offset(cachep);
  1785. printk(KERN_ERR "Next obj: start=%p, len=%d\n",
  1786. realobj, size);
  1787. print_objinfo(cachep, objp, 2);
  1788. }
  1789. }
  1790. }
  1791. #endif
  1792. #if DEBUG
  1793. static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
  1794. {
  1795. int i;
  1796. for (i = 0; i < cachep->num; i++) {
  1797. void *objp = index_to_obj(cachep, slabp, i);
  1798. if (cachep->flags & SLAB_POISON) {
  1799. #ifdef CONFIG_DEBUG_PAGEALLOC
  1800. if (cachep->buffer_size % PAGE_SIZE == 0 &&
  1801. OFF_SLAB(cachep))
  1802. kernel_map_pages(virt_to_page(objp),
  1803. cachep->buffer_size / PAGE_SIZE, 1);
  1804. else
  1805. check_poison_obj(cachep, objp);
  1806. #else
  1807. check_poison_obj(cachep, objp);
  1808. #endif
  1809. }
  1810. if (cachep->flags & SLAB_RED_ZONE) {
  1811. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
  1812. slab_error(cachep, "start of a freed object "
  1813. "was overwritten");
  1814. if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
  1815. slab_error(cachep, "end of a freed object "
  1816. "was overwritten");
  1817. }
  1818. }
  1819. }
  1820. #else
  1821. static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
  1822. {
  1823. }
  1824. #endif
  1825. /**
  1826. * slab_destroy - destroy and release all objects in a slab
  1827. * @cachep: cache pointer being destroyed
  1828. * @slabp: slab pointer being destroyed
  1829. *
  1830. * Destroy all the objs in a slab, and release the mem back to the system.
  1831. * Before calling the slab must have been unlinked from the cache. The
  1832. * cache-lock is not held/needed.
  1833. */
  1834. static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
  1835. {
  1836. void *addr = slabp->s_mem - slabp->colouroff;
  1837. slab_destroy_debugcheck(cachep, slabp);
  1838. if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
  1839. struct slab_rcu *slab_rcu;
  1840. slab_rcu = (struct slab_rcu *)slabp;
  1841. slab_rcu->cachep = cachep;
  1842. slab_rcu->addr = addr;
  1843. call_rcu(&slab_rcu->head, kmem_rcu_free);
  1844. } else {
  1845. kmem_freepages(cachep, addr);
  1846. if (OFF_SLAB(cachep))
  1847. kmem_cache_free(cachep->slabp_cache, slabp);
  1848. }
  1849. }
  1850. static void __kmem_cache_destroy(struct kmem_cache *cachep)
  1851. {
  1852. int i;
  1853. struct kmem_list3 *l3;
  1854. for_each_online_cpu(i)
  1855. kfree(cachep->array[i]);
  1856. /* NUMA: free the list3 structures */
  1857. for_each_online_node(i) {
  1858. l3 = cachep->nodelists[i];
  1859. if (l3) {
  1860. kfree(l3->shared);
  1861. free_alien_cache(l3->alien);
  1862. kfree(l3);
  1863. }
  1864. }
  1865. kmem_cache_free(&cache_cache, cachep);
  1866. }
  1867. /**
  1868. * calculate_slab_order - calculate size (page order) of slabs
  1869. * @cachep: pointer to the cache that is being created
  1870. * @size: size of objects to be created in this cache.
  1871. * @align: required alignment for the objects.
  1872. * @flags: slab allocation flags
  1873. *
  1874. * Also calculates the number of objects per slab.
  1875. *
  1876. * This could be made much more intelligent. For now, try to avoid using
  1877. * high order pages for slabs. When the gfp() functions are more friendly
  1878. * towards high-order requests, this should be changed.
  1879. */
  1880. static size_t calculate_slab_order(struct kmem_cache *cachep,
  1881. size_t size, size_t align, unsigned long flags)
  1882. {
  1883. unsigned long offslab_limit;
  1884. size_t left_over = 0;
  1885. int gfporder;
  1886. for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
  1887. unsigned int num;
  1888. size_t remainder;
  1889. cache_estimate(gfporder, size, align, flags, &remainder, &num);
  1890. if (!num)
  1891. continue;
  1892. if (flags & CFLGS_OFF_SLAB) {
  1893. /*
  1894. * Max number of objs-per-slab for caches which
  1895. * use off-slab slabs. Needed to avoid a possible
  1896. * looping condition in cache_grow().
  1897. */
  1898. offslab_limit = size - sizeof(struct slab);
  1899. offslab_limit /= sizeof(kmem_bufctl_t);
  1900. if (num > offslab_limit)
  1901. break;
  1902. }
  1903. /* Found something acceptable - save it away */
  1904. cachep->num = num;
  1905. cachep->gfporder = gfporder;
  1906. left_over = remainder;
  1907. /*
  1908. * A VFS-reclaimable slab tends to have most allocations
  1909. * as GFP_NOFS and we really don't want to have to be allocating
  1910. * higher-order pages when we are unable to shrink dcache.
  1911. */
  1912. if (flags & SLAB_RECLAIM_ACCOUNT)
  1913. break;
  1914. /*
  1915. * Large number of objects is good, but very large slabs are
  1916. * currently bad for the gfp()s.
  1917. */
  1918. if (gfporder >= slab_max_order)
  1919. break;
  1920. /*
  1921. * Acceptable internal fragmentation?
  1922. */
  1923. if (left_over * 8 <= (PAGE_SIZE << gfporder))
  1924. break;
  1925. }
  1926. return left_over;
  1927. }
  1928. static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
  1929. {
  1930. if (g_cpucache_up == FULL)
  1931. return enable_cpucache(cachep, gfp);
  1932. if (g_cpucache_up == NONE) {
  1933. /*
  1934. * Note: the first kmem_cache_create must create the cache
  1935. * that's used by kmalloc(24), otherwise the creation of
  1936. * further caches will BUG().
  1937. */
  1938. cachep->array[smp_processor_id()] = &initarray_generic.cache;
  1939. /*
  1940. * If the cache that's used by kmalloc(sizeof(kmem_list3)) is
  1941. * the first cache, then we need to set up all its list3s,
  1942. * otherwise the creation of further caches will BUG().
  1943. */
  1944. set_up_list3s(cachep, SIZE_AC);
  1945. if (INDEX_AC == INDEX_L3)
  1946. g_cpucache_up = PARTIAL_L3;
  1947. else
  1948. g_cpucache_up = PARTIAL_AC;
  1949. } else {
  1950. cachep->array[smp_processor_id()] =
  1951. kmalloc(sizeof(struct arraycache_init), gfp);
  1952. if (g_cpucache_up == PARTIAL_AC) {
  1953. set_up_list3s(cachep, SIZE_L3);
  1954. g_cpucache_up = PARTIAL_L3;
  1955. } else {
  1956. int node;
  1957. for_each_online_node(node) {
  1958. cachep->nodelists[node] =
  1959. kmalloc_node(sizeof(struct kmem_list3),
  1960. gfp, node);
  1961. BUG_ON(!cachep->nodelists[node]);
  1962. kmem_list3_init(cachep->nodelists[node]);
  1963. }
  1964. }
  1965. }
  1966. cachep->nodelists[numa_mem_id()]->next_reap =
  1967. jiffies + REAPTIMEOUT_LIST3 +
  1968. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  1969. cpu_cache_get(cachep)->avail = 0;
  1970. cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
  1971. cpu_cache_get(cachep)->batchcount = 1;
  1972. cpu_cache_get(cachep)->touched = 0;
  1973. cachep->batchcount = 1;
  1974. cachep->limit = BOOT_CPUCACHE_ENTRIES;
  1975. return 0;
  1976. }
  1977. /**
  1978. * kmem_cache_create - Create a cache.
  1979. * @name: A string which is used in /proc/slabinfo to identify this cache.
  1980. * @size: The size of objects to be created in this cache.
  1981. * @align: The required alignment for the objects.
  1982. * @flags: SLAB flags
  1983. * @ctor: A constructor for the objects.
  1984. *
  1985. * Returns a ptr to the cache on success, NULL on failure.
  1986. * Cannot be called within a int, but can be interrupted.
  1987. * The @ctor is run when new pages are allocated by the cache.
  1988. *
  1989. * @name must be valid until the cache is destroyed. This implies that
  1990. * the module calling this has to destroy the cache before getting unloaded.
  1991. *
  1992. * The flags are
  1993. *
  1994. * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
  1995. * to catch references to uninitialised memory.
  1996. *
  1997. * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
  1998. * for buffer overruns.
  1999. *
  2000. * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
  2001. * cacheline. This can be beneficial if you're counting cycles as closely
  2002. * as davem.
  2003. */
  2004. struct kmem_cache *
  2005. kmem_cache_create (const char *name, size_t size, size_t align,
  2006. unsigned long flags, void (*ctor)(void *))
  2007. {
  2008. size_t left_over, slab_size, ralign;
  2009. struct kmem_cache *cachep = NULL, *pc;
  2010. gfp_t gfp;
  2011. /*
  2012. * Sanity checks... these are all serious usage bugs.
  2013. */
  2014. if (!name || in_interrupt() || (size < BYTES_PER_WORD) ||
  2015. size > KMALLOC_MAX_SIZE) {
  2016. printk(KERN_ERR "%s: Early error in slab %s\n", __func__,
  2017. name);
  2018. BUG();
  2019. }
  2020. /*
  2021. * We use cache_chain_mutex to ensure a consistent view of
  2022. * cpu_online_mask as well. Please see cpuup_callback
  2023. */
  2024. if (slab_is_available()) {
  2025. get_online_cpus();
  2026. mutex_lock(&cache_chain_mutex);
  2027. }
  2028. list_for_each_entry(pc, &cache_chain, next) {
  2029. char tmp;
  2030. int res;
  2031. /*
  2032. * This happens when the module gets unloaded and doesn't
  2033. * destroy its slab cache and no-one else reuses the vmalloc
  2034. * area of the module. Print a warning.
  2035. */
  2036. res = probe_kernel_address(pc->name, tmp);
  2037. if (res) {
  2038. printk(KERN_ERR
  2039. "SLAB: cache with size %d has lost its name\n",
  2040. pc->buffer_size);
  2041. continue;
  2042. }
  2043. if (!strcmp(pc->name, name)) {
  2044. printk(KERN_ERR
  2045. "kmem_cache_create: duplicate cache %s\n", name);
  2046. dump_stack();
  2047. goto oops;
  2048. }
  2049. }
  2050. #if DEBUG
  2051. WARN_ON(strchr(name, ' ')); /* It confuses parsers */
  2052. #if FORCED_DEBUG
  2053. /*
  2054. * Enable redzoning and last user accounting, except for caches with
  2055. * large objects, if the increased size would increase the object size
  2056. * above the next power of two: caches with object sizes just above a
  2057. * power of two have a significant amount of internal fragmentation.
  2058. */
  2059. if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
  2060. 2 * sizeof(unsigned long long)))
  2061. flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
  2062. if (!(flags & SLAB_DESTROY_BY_RCU))
  2063. flags |= SLAB_POISON;
  2064. #endif
  2065. if (flags & SLAB_DESTROY_BY_RCU)
  2066. BUG_ON(flags & SLAB_POISON);
  2067. #endif
  2068. /*
  2069. * Always checks flags, a caller might be expecting debug support which
  2070. * isn't available.
  2071. */
  2072. BUG_ON(flags & ~CREATE_MASK);
  2073. /*
  2074. * Check that size is in terms of words. This is needed to avoid
  2075. * unaligned accesses for some archs when redzoning is used, and makes
  2076. * sure any on-slab bufctl's are also correctly aligned.
  2077. */
  2078. if (size & (BYTES_PER_WORD - 1)) {
  2079. size += (BYTES_PER_WORD - 1);
  2080. size &= ~(BYTES_PER_WORD - 1);
  2081. }
  2082. /* calculate the final buffer alignment: */
  2083. /* 1) arch recommendation: can be overridden for debug */
  2084. if (flags & SLAB_HWCACHE_ALIGN) {
  2085. /*
  2086. * Default alignment: as specified by the arch code. Except if
  2087. * an object is really small, then squeeze multiple objects into
  2088. * one cacheline.
  2089. */
  2090. ralign = cache_line_size();
  2091. while (size <= ralign / 2)
  2092. ralign /= 2;
  2093. } else {
  2094. ralign = BYTES_PER_WORD;
  2095. }
  2096. /*
  2097. * Redzoning and user store require word alignment or possibly larger.
  2098. * Note this will be overridden by architecture or caller mandated
  2099. * alignment if either is greater than BYTES_PER_WORD.
  2100. */
  2101. if (flags & SLAB_STORE_USER)
  2102. ralign = BYTES_PER_WORD;
  2103. if (flags & SLAB_RED_ZONE) {
  2104. ralign = REDZONE_ALIGN;
  2105. /* If redzoning, ensure that the second redzone is suitably
  2106. * aligned, by adjusting the object size accordingly. */
  2107. size += REDZONE_ALIGN - 1;
  2108. size &= ~(REDZONE_ALIGN - 1);
  2109. }
  2110. /* 2) arch mandated alignment */
  2111. if (ralign < ARCH_SLAB_MINALIGN) {
  2112. ralign = ARCH_SLAB_MINALIGN;
  2113. }
  2114. /* 3) caller mandated alignment */
  2115. if (ralign < align) {
  2116. ralign = align;
  2117. }
  2118. /* disable debug if necessary */
  2119. if (ralign > __alignof__(unsigned long long))
  2120. flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
  2121. /*
  2122. * 4) Store it.
  2123. */
  2124. align = ralign;
  2125. if (slab_is_available())
  2126. gfp = GFP_KERNEL;
  2127. else
  2128. gfp = GFP_NOWAIT;
  2129. /* Get cache's description obj. */
  2130. cachep = kmem_cache_zalloc(&cache_cache, gfp);
  2131. if (!cachep)
  2132. goto oops;
  2133. cachep->nodelists = (struct kmem_list3 **)&cachep->array[nr_cpu_ids];
  2134. #if DEBUG
  2135. cachep->obj_size = size;
  2136. /*
  2137. * Both debugging options require word-alignment which is calculated
  2138. * into align above.
  2139. */
  2140. if (flags & SLAB_RED_ZONE) {
  2141. /* add space for red zone words */
  2142. cachep->obj_offset += sizeof(unsigned long long);
  2143. size += 2 * sizeof(unsigned long long);
  2144. }
  2145. if (flags & SLAB_STORE_USER) {
  2146. /* user store requires one word storage behind the end of
  2147. * the real object. But if the second red zone needs to be
  2148. * aligned to 64 bits, we must allow that much space.
  2149. */
  2150. if (flags & SLAB_RED_ZONE)
  2151. size += REDZONE_ALIGN;
  2152. else
  2153. size += BYTES_PER_WORD;
  2154. }
  2155. #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
  2156. if (size >= malloc_sizes[INDEX_L3 + 1].cs_size
  2157. && cachep->obj_size > cache_line_size() && ALIGN(size, align) < PAGE_SIZE) {
  2158. cachep->obj_offset += PAGE_SIZE - ALIGN(size, align);
  2159. size = PAGE_SIZE;
  2160. }
  2161. #endif
  2162. #endif
  2163. /*
  2164. * Determine if the slab management is 'on' or 'off' slab.
  2165. * (bootstrapping cannot cope with offslab caches so don't do
  2166. * it too early on. Always use on-slab management when
  2167. * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
  2168. */
  2169. if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init &&
  2170. !(flags & SLAB_NOLEAKTRACE))
  2171. /*
  2172. * Size is large, assume best to place the slab management obj
  2173. * off-slab (should allow better packing of objs).
  2174. */
  2175. flags |= CFLGS_OFF_SLAB;
  2176. size = ALIGN(size, align);
  2177. left_over = calculate_slab_order(cachep, size, align, flags);
  2178. if (!cachep->num) {
  2179. printk(KERN_ERR
  2180. "kmem_cache_create: couldn't create cache %s.\n", name);
  2181. kmem_cache_free(&cache_cache, cachep);
  2182. cachep = NULL;
  2183. goto oops;
  2184. }
  2185. slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
  2186. + sizeof(struct slab), align);
  2187. /*
  2188. * If the slab has been placed off-slab, and we have enough space then
  2189. * move it on-slab. This is at the expense of any extra colouring.
  2190. */
  2191. if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
  2192. flags &= ~CFLGS_OFF_SLAB;
  2193. left_over -= slab_size;
  2194. }
  2195. if (flags & CFLGS_OFF_SLAB) {
  2196. /* really off slab. No need for manual alignment */
  2197. slab_size =
  2198. cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
  2199. #ifdef CONFIG_PAGE_POISONING
  2200. /* If we're going to use the generic kernel_map_pages()
  2201. * poisoning, then it's going to smash the contents of
  2202. * the redzone and userword anyhow, so switch them off.
  2203. */
  2204. if (size % PAGE_SIZE == 0 && flags & SLAB_POISON)
  2205. flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
  2206. #endif
  2207. }
  2208. cachep->colour_off = cache_line_size();
  2209. /* Offset must be a multiple of the alignment. */
  2210. if (cachep->colour_off < align)
  2211. cachep->colour_off = align;
  2212. cachep->colour = left_over / cachep->colour_off;
  2213. cachep->slab_size = slab_size;
  2214. cachep->flags = flags;
  2215. cachep->gfpflags = 0;
  2216. if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
  2217. cachep->gfpflags |= GFP_DMA;
  2218. cachep->buffer_size = size;
  2219. cachep->reciprocal_buffer_size = reciprocal_value(size);
  2220. if (flags & CFLGS_OFF_SLAB) {
  2221. cachep->slabp_cache = kmem_find_general_cachep(slab_size, 0u);
  2222. /*
  2223. * This is a possibility for one of the malloc_sizes caches.
  2224. * But since we go off slab only for object size greater than
  2225. * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
  2226. * this should not happen at all.
  2227. * But leave a BUG_ON for some lucky dude.
  2228. */
  2229. BUG_ON(ZERO_OR_NULL_PTR(cachep->slabp_cache));
  2230. }
  2231. cachep->ctor = ctor;
  2232. cachep->name = name;
  2233. if (setup_cpu_cache(cachep, gfp)) {
  2234. __kmem_cache_destroy(cachep);
  2235. cachep = NULL;
  2236. goto oops;
  2237. }
  2238. if (flags & SLAB_DEBUG_OBJECTS) {
  2239. /*
  2240. * Would deadlock through slab_destroy()->call_rcu()->
  2241. * debug_object_activate()->kmem_cache_alloc().
  2242. */
  2243. WARN_ON_ONCE(flags & SLAB_DESTROY_BY_RCU);
  2244. slab_set_debugobj_lock_classes(cachep);
  2245. }
  2246. /* cache setup completed, link it into the list */
  2247. list_add(&cachep->next, &cache_chain);
  2248. oops:
  2249. if (!cachep && (flags & SLAB_PANIC))
  2250. panic("kmem_cache_create(): failed to create slab `%s'\n",
  2251. name);
  2252. if (slab_is_available()) {
  2253. mutex_unlock(&cache_chain_mutex);
  2254. put_online_cpus();
  2255. }
  2256. return cachep;
  2257. }
  2258. EXPORT_SYMBOL(kmem_cache_create);
  2259. #if DEBUG
  2260. static void check_irq_off(void)
  2261. {
  2262. BUG_ON(!irqs_disabled());
  2263. }
  2264. static void check_irq_on(void)
  2265. {
  2266. BUG_ON(irqs_disabled());
  2267. }
  2268. static void check_spinlock_acquired(struct kmem_cache *cachep)
  2269. {
  2270. #ifdef CONFIG_SMP
  2271. check_irq_off();
  2272. assert_spin_locked(&cachep->nodelists[numa_mem_id()]->list_lock);
  2273. #endif
  2274. }
  2275. static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
  2276. {
  2277. #ifdef CONFIG_SMP
  2278. check_irq_off();
  2279. assert_spin_locked(&cachep->nodelists[node]->list_lock);
  2280. #endif
  2281. }
  2282. #else
  2283. #define check_irq_off() do { } while(0)
  2284. #define check_irq_on() do { } while(0)
  2285. #define check_spinlock_acquired(x) do { } while(0)
  2286. #define check_spinlock_acquired_node(x, y) do { } while(0)
  2287. #endif
  2288. static void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
  2289. struct array_cache *ac,
  2290. int force, int node);
  2291. static void do_drain(void *arg)
  2292. {
  2293. struct kmem_cache *cachep = arg;
  2294. struct array_cache *ac;
  2295. int node = numa_mem_id();
  2296. check_irq_off();
  2297. ac = cpu_cache_get(cachep);
  2298. spin_lock(&cachep->nodelists[node]->list_lock);
  2299. free_block(cachep, ac->entry, ac->avail, node);
  2300. spin_unlock(&cachep->nodelists[node]->list_lock);
  2301. ac->avail = 0;
  2302. }
  2303. static void drain_cpu_caches(struct kmem_cache *cachep)
  2304. {
  2305. struct kmem_list3 *l3;
  2306. int node;
  2307. on_each_cpu(do_drain, cachep, 1);
  2308. check_irq_on();
  2309. for_each_online_node(node) {
  2310. l3 = cachep->nodelists[node];
  2311. if (l3 && l3->alien)
  2312. drain_alien_cache(cachep, l3->alien);
  2313. }
  2314. for_each_online_node(node) {
  2315. l3 = cachep->nodelists[node];
  2316. if (l3)
  2317. drain_array(cachep, l3, l3->shared, 1, node);
  2318. }
  2319. }
  2320. /*
  2321. * Remove slabs from the list of free slabs.
  2322. * Specify the number of slabs to drain in tofree.
  2323. *
  2324. * Returns the actual number of slabs released.
  2325. */
  2326. static int drain_freelist(struct kmem_cache *cache,
  2327. struct kmem_list3 *l3, int tofree)
  2328. {
  2329. struct list_head *p;
  2330. int nr_freed;
  2331. struct slab *slabp;
  2332. nr_freed = 0;
  2333. while (nr_freed < tofree && !list_empty(&l3->slabs_free)) {
  2334. spin_lock_irq(&l3->list_lock);
  2335. p = l3->slabs_free.prev;
  2336. if (p == &l3->slabs_free) {
  2337. spin_unlock_irq(&l3->list_lock);
  2338. goto out;
  2339. }
  2340. slabp = list_entry(p, struct slab, list);
  2341. #if DEBUG
  2342. BUG_ON(slabp->inuse);
  2343. #endif
  2344. list_del(&slabp->list);
  2345. /*
  2346. * Safe to drop the lock. The slab is no longer linked
  2347. * to the cache.
  2348. */
  2349. l3->free_objects -= cache->num;
  2350. spin_unlock_irq(&l3->list_lock);
  2351. slab_destroy(cache, slabp);
  2352. nr_freed++;
  2353. }
  2354. out:
  2355. return nr_freed;
  2356. }
  2357. /* Called with cache_chain_mutex held to protect against cpu hotplug */
  2358. static int __cache_shrink(struct kmem_cache *cachep)
  2359. {
  2360. int ret = 0, i = 0;
  2361. struct kmem_list3 *l3;
  2362. drain_cpu_caches(cachep);
  2363. check_irq_on();
  2364. for_each_online_node(i) {
  2365. l3 = cachep->nodelists[i];
  2366. if (!l3)
  2367. continue;
  2368. drain_freelist(cachep, l3, l3->free_objects);
  2369. ret += !list_empty(&l3->slabs_full) ||
  2370. !list_empty(&l3->slabs_partial);
  2371. }
  2372. return (ret ? 1 : 0);
  2373. }
  2374. /**
  2375. * kmem_cache_shrink - Shrink a cache.
  2376. * @cachep: The cache to shrink.
  2377. *
  2378. * Releases as many slabs as possible for a cache.
  2379. * To help debugging, a zero exit status indicates all slabs were released.
  2380. */
  2381. int kmem_cache_shrink(struct kmem_cache *cachep)
  2382. {
  2383. int ret;
  2384. BUG_ON(!cachep || in_interrupt());
  2385. get_online_cpus();
  2386. mutex_lock(&cache_chain_mutex);
  2387. ret = __cache_shrink(cachep);
  2388. mutex_unlock(&cache_chain_mutex);
  2389. put_online_cpus();
  2390. return ret;
  2391. }
  2392. EXPORT_SYMBOL(kmem_cache_shrink);
  2393. /**
  2394. * kmem_cache_destroy - delete a cache
  2395. * @cachep: the cache to destroy
  2396. *
  2397. * Remove a &struct kmem_cache object from the slab cache.
  2398. *
  2399. * It is expected this function will be called by a module when it is
  2400. * unloaded. This will remove the cache completely, and avoid a duplicate
  2401. * cache being allocated each time a module is loaded and unloaded, if the
  2402. * module doesn't have persistent in-kernel storage across loads and unloads.
  2403. *
  2404. * The cache must be empty before calling this function.
  2405. *
  2406. * The caller must guarantee that no one will allocate memory from the cache
  2407. * during the kmem_cache_destroy().
  2408. */
  2409. void kmem_cache_destroy(struct kmem_cache *cachep)
  2410. {
  2411. BUG_ON(!cachep || in_interrupt());
  2412. /* Find the cache in the chain of caches. */
  2413. get_online_cpus();
  2414. mutex_lock(&cache_chain_mutex);
  2415. /*
  2416. * the chain is never empty, cache_cache is never destroyed
  2417. */
  2418. list_del(&cachep->next);
  2419. if (__cache_shrink(cachep)) {
  2420. slab_error(cachep, "Can't free all objects");
  2421. list_add(&cachep->next, &cache_chain);
  2422. mutex_unlock(&cache_chain_mutex);
  2423. put_online_cpus();
  2424. return;
  2425. }
  2426. if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU))
  2427. rcu_barrier();
  2428. __kmem_cache_destroy(cachep);
  2429. mutex_unlock(&cache_chain_mutex);
  2430. put_online_cpus();
  2431. }
  2432. EXPORT_SYMBOL(kmem_cache_destroy);
  2433. /*
  2434. * Get the memory for a slab management obj.
  2435. * For a slab cache when the slab descriptor is off-slab, slab descriptors
  2436. * always come from malloc_sizes caches. The slab descriptor cannot
  2437. * come from the same cache which is getting created because,
  2438. * when we are searching for an appropriate cache for these
  2439. * descriptors in kmem_cache_create, we search through the malloc_sizes array.
  2440. * If we are creating a malloc_sizes cache here it would not be visible to
  2441. * kmem_find_general_cachep till the initialization is complete.
  2442. * Hence we cannot have slabp_cache same as the original cache.
  2443. */
  2444. static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
  2445. int colour_off, gfp_t local_flags,
  2446. int nodeid)
  2447. {
  2448. struct slab *slabp;
  2449. if (OFF_SLAB(cachep)) {
  2450. /* Slab management obj is off-slab. */
  2451. slabp = kmem_cache_alloc_node(cachep->slabp_cache,
  2452. local_flags, nodeid);
  2453. /*
  2454. * If the first object in the slab is leaked (it's allocated
  2455. * but no one has a reference to it), we want to make sure
  2456. * kmemleak does not treat the ->s_mem pointer as a reference
  2457. * to the object. Otherwise we will not report the leak.
  2458. */
  2459. kmemleak_scan_area(&slabp->list, sizeof(struct list_head),
  2460. local_flags);
  2461. if (!slabp)
  2462. return NULL;
  2463. } else {
  2464. slabp = objp + colour_off;
  2465. colour_off += cachep->slab_size;
  2466. }
  2467. slabp->inuse = 0;
  2468. slabp->colouroff = colour_off;
  2469. slabp->s_mem = objp + colour_off;
  2470. slabp->nodeid = nodeid;
  2471. slabp->free = 0;
  2472. return slabp;
  2473. }
  2474. static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
  2475. {
  2476. return (kmem_bufctl_t *) (slabp + 1);
  2477. }
  2478. static void cache_init_objs(struct kmem_cache *cachep,
  2479. struct slab *slabp)
  2480. {
  2481. int i;
  2482. for (i = 0; i < cachep->num; i++) {
  2483. void *objp = index_to_obj(cachep, slabp, i);
  2484. #if DEBUG
  2485. /* need to poison the objs? */
  2486. if (cachep->flags & SLAB_POISON)
  2487. poison_obj(cachep, objp, POISON_FREE);
  2488. if (cachep->flags & SLAB_STORE_USER)
  2489. *dbg_userword(cachep, objp) = NULL;
  2490. if (cachep->flags & SLAB_RED_ZONE) {
  2491. *dbg_redzone1(cachep, objp) = RED_INACTIVE;
  2492. *dbg_redzone2(cachep, objp) = RED_INACTIVE;
  2493. }
  2494. /*
  2495. * Constructors are not allowed to allocate memory from the same
  2496. * cache which they are a constructor for. Otherwise, deadlock.
  2497. * They must also be threaded.
  2498. */
  2499. if (cachep->ctor && !(cachep->flags & SLAB_POISON))
  2500. cachep->ctor(objp + obj_offset(cachep));
  2501. if (cachep->flags & SLAB_RED_ZONE) {
  2502. if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
  2503. slab_error(cachep, "constructor overwrote the"
  2504. " end of an object");
  2505. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
  2506. slab_error(cachep, "constructor overwrote the"
  2507. " start of an object");
  2508. }
  2509. if ((cachep->buffer_size % PAGE_SIZE) == 0 &&
  2510. OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
  2511. kernel_map_pages(virt_to_page(objp),
  2512. cachep->buffer_size / PAGE_SIZE, 0);
  2513. #else
  2514. if (cachep->ctor)
  2515. cachep->ctor(objp);
  2516. #endif
  2517. slab_bufctl(slabp)[i] = i + 1;
  2518. }
  2519. slab_bufctl(slabp)[i - 1] = BUFCTL_END;
  2520. }
  2521. static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
  2522. {
  2523. if (CONFIG_ZONE_DMA_FLAG) {
  2524. if (flags & GFP_DMA)
  2525. BUG_ON(!(cachep->gfpflags & GFP_DMA));
  2526. else
  2527. BUG_ON(cachep->gfpflags & GFP_DMA);
  2528. }
  2529. }
  2530. static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
  2531. int nodeid)
  2532. {
  2533. void *objp = index_to_obj(cachep, slabp, slabp->free);
  2534. kmem_bufctl_t next;
  2535. slabp->inuse++;
  2536. next = slab_bufctl(slabp)[slabp->free];
  2537. #if DEBUG
  2538. slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
  2539. WARN_ON(slabp->nodeid != nodeid);
  2540. #endif
  2541. slabp->free = next;
  2542. return objp;
  2543. }
  2544. static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
  2545. void *objp, int nodeid)
  2546. {
  2547. unsigned int objnr = obj_to_index(cachep, slabp, objp);
  2548. #if DEBUG
  2549. /* Verify that the slab belongs to the intended node */
  2550. WARN_ON(slabp->nodeid != nodeid);
  2551. if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) {
  2552. printk(KERN_ERR "slab: double free detected in cache "
  2553. "'%s', objp %p\n", cachep->name, objp);
  2554. BUG();
  2555. }
  2556. #endif
  2557. slab_bufctl(slabp)[objnr] = slabp->free;
  2558. slabp->free = objnr;
  2559. slabp->inuse--;
  2560. }
  2561. /*
  2562. * Map pages beginning at addr to the given cache and slab. This is required
  2563. * for the slab allocator to be able to lookup the cache and slab of a
  2564. * virtual address for kfree, ksize, and slab debugging.
  2565. */
  2566. static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
  2567. void *addr)
  2568. {
  2569. int nr_pages;
  2570. struct page *page;
  2571. page = virt_to_page(addr);
  2572. nr_pages = 1;
  2573. if (likely(!PageCompound(page)))
  2574. nr_pages <<= cache->gfporder;
  2575. do {
  2576. page_set_cache(page, cache);
  2577. page_set_slab(page, slab);
  2578. page++;
  2579. } while (--nr_pages);
  2580. }
  2581. /*
  2582. * Grow (by 1) the number of slabs within a cache. This is called by
  2583. * kmem_cache_alloc() when there are no active objs left in a cache.
  2584. */
  2585. static int cache_grow(struct kmem_cache *cachep,
  2586. gfp_t flags, int nodeid, void *objp)
  2587. {
  2588. struct slab *slabp;
  2589. size_t offset;
  2590. gfp_t local_flags;
  2591. struct kmem_list3 *l3;
  2592. /*
  2593. * Be lazy and only check for valid flags here, keeping it out of the
  2594. * critical path in kmem_cache_alloc().
  2595. */
  2596. BUG_ON(flags & GFP_SLAB_BUG_MASK);
  2597. local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
  2598. /* Take the l3 list lock to change the colour_next on this node */
  2599. check_irq_off();
  2600. l3 = cachep->nodelists[nodeid];
  2601. spin_lock(&l3->list_lock);
  2602. /* Get colour for the slab, and cal the next value. */
  2603. offset = l3->colour_next;
  2604. l3->colour_next++;
  2605. if (l3->colour_next >= cachep->colour)
  2606. l3->colour_next = 0;
  2607. spin_unlock(&l3->list_lock);
  2608. offset *= cachep->colour_off;
  2609. if (local_flags & __GFP_WAIT)
  2610. local_irq_enable();
  2611. /*
  2612. * The test for missing atomic flag is performed here, rather than
  2613. * the more obvious place, simply to reduce the critical path length
  2614. * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
  2615. * will eventually be caught here (where it matters).
  2616. */
  2617. kmem_flagcheck(cachep, flags);
  2618. /*
  2619. * Get mem for the objs. Attempt to allocate a physical page from
  2620. * 'nodeid'.
  2621. */
  2622. if (!objp)
  2623. objp = kmem_getpages(cachep, local_flags, nodeid);
  2624. if (!objp)
  2625. goto failed;
  2626. /* Get slab management. */
  2627. slabp = alloc_slabmgmt(cachep, objp, offset,
  2628. local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
  2629. if (!slabp)
  2630. goto opps1;
  2631. slab_map_pages(cachep, slabp, objp);
  2632. cache_init_objs(cachep, slabp);
  2633. if (local_flags & __GFP_WAIT)
  2634. local_irq_disable();
  2635. check_irq_off();
  2636. spin_lock(&l3->list_lock);
  2637. /* Make slab active. */
  2638. list_add_tail(&slabp->list, &(l3->slabs_free));
  2639. STATS_INC_GROWN(cachep);
  2640. l3->free_objects += cachep->num;
  2641. spin_unlock(&l3->list_lock);
  2642. return 1;
  2643. opps1:
  2644. kmem_freepages(cachep, objp);
  2645. failed:
  2646. if (local_flags & __GFP_WAIT)
  2647. local_irq_disable();
  2648. return 0;
  2649. }
  2650. #if DEBUG
  2651. /*
  2652. * Perform extra freeing checks:
  2653. * - detect bad pointers.
  2654. * - POISON/RED_ZONE checking
  2655. */
  2656. static void kfree_debugcheck(const void *objp)
  2657. {
  2658. if (!virt_addr_valid(objp)) {
  2659. printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
  2660. (unsigned long)objp);
  2661. BUG();
  2662. }
  2663. }
  2664. static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
  2665. {
  2666. unsigned long long redzone1, redzone2;
  2667. redzone1 = *dbg_redzone1(cache, obj);
  2668. redzone2 = *dbg_redzone2(cache, obj);
  2669. /*
  2670. * Redzone is ok.
  2671. */
  2672. if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
  2673. return;
  2674. if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
  2675. slab_error(cache, "double free detected");
  2676. else
  2677. slab_error(cache, "memory outside object was overwritten");
  2678. printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
  2679. obj, redzone1, redzone2);
  2680. }
  2681. static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
  2682. void *caller)
  2683. {
  2684. struct page *page;
  2685. unsigned int objnr;
  2686. struct slab *slabp;
  2687. BUG_ON(virt_to_cache(objp) != cachep);
  2688. objp -= obj_offset(cachep);
  2689. kfree_debugcheck(objp);
  2690. page = virt_to_head_page(objp);
  2691. slabp = page_get_slab(page);
  2692. if (cachep->flags & SLAB_RED_ZONE) {
  2693. verify_redzone_free(cachep, objp);
  2694. *dbg_redzone1(cachep, objp) = RED_INACTIVE;
  2695. *dbg_redzone2(cachep, objp) = RED_INACTIVE;
  2696. }
  2697. if (cachep->flags & SLAB_STORE_USER)
  2698. *dbg_userword(cachep, objp) = caller;
  2699. objnr = obj_to_index(cachep, slabp, objp);
  2700. BUG_ON(objnr >= cachep->num);
  2701. BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
  2702. #ifdef CONFIG_DEBUG_SLAB_LEAK
  2703. slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
  2704. #endif
  2705. if (cachep->flags & SLAB_POISON) {
  2706. #ifdef CONFIG_DEBUG_PAGEALLOC
  2707. if ((cachep->buffer_size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
  2708. store_stackinfo(cachep, objp, (unsigned long)caller);
  2709. kernel_map_pages(virt_to_page(objp),
  2710. cachep->buffer_size / PAGE_SIZE, 0);
  2711. } else {
  2712. poison_obj(cachep, objp, POISON_FREE);
  2713. }
  2714. #else
  2715. poison_obj(cachep, objp, POISON_FREE);
  2716. #endif
  2717. }
  2718. return objp;
  2719. }
  2720. static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
  2721. {
  2722. kmem_bufctl_t i;
  2723. int entries = 0;
  2724. /* Check slab's freelist to see if this obj is there. */
  2725. for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
  2726. entries++;
  2727. if (entries > cachep->num || i >= cachep->num)
  2728. goto bad;
  2729. }
  2730. if (entries != cachep->num - slabp->inuse) {
  2731. bad:
  2732. printk(KERN_ERR "slab: Internal list corruption detected in "
  2733. "cache '%s'(%d), slabp %p(%d). Tainted(%s). Hexdump:\n",
  2734. cachep->name, cachep->num, slabp, slabp->inuse,
  2735. print_tainted());
  2736. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, slabp,
  2737. sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t),
  2738. 1);
  2739. BUG();
  2740. }
  2741. }
  2742. #else
  2743. #define kfree_debugcheck(x) do { } while(0)
  2744. #define cache_free_debugcheck(x,objp,z) (objp)
  2745. #define check_slabp(x,y) do { } while(0)
  2746. #endif
  2747. static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags)
  2748. {
  2749. int batchcount;
  2750. struct kmem_list3 *l3;
  2751. struct array_cache *ac;
  2752. int node;
  2753. retry:
  2754. check_irq_off();
  2755. node = numa_mem_id();
  2756. ac = cpu_cache_get(cachep);
  2757. batchcount = ac->batchcount;
  2758. if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
  2759. /*
  2760. * If there was little recent activity on this cache, then
  2761. * perform only a partial refill. Otherwise we could generate
  2762. * refill bouncing.
  2763. */
  2764. batchcount = BATCHREFILL_LIMIT;
  2765. }
  2766. l3 = cachep->nodelists[node];
  2767. BUG_ON(ac->avail > 0 || !l3);
  2768. spin_lock(&l3->list_lock);
  2769. /* See if we can refill from the shared array */
  2770. if (l3->shared && transfer_objects(ac, l3->shared, batchcount)) {
  2771. l3->shared->touched = 1;
  2772. goto alloc_done;
  2773. }
  2774. while (batchcount > 0) {
  2775. struct list_head *entry;
  2776. struct slab *slabp;
  2777. /* Get slab alloc is to come from. */
  2778. entry = l3->slabs_partial.next;
  2779. if (entry == &l3->slabs_partial) {
  2780. l3->free_touched = 1;
  2781. entry = l3->slabs_free.next;
  2782. if (entry == &l3->slabs_free)
  2783. goto must_grow;
  2784. }
  2785. slabp = list_entry(entry, struct slab, list);
  2786. check_slabp(cachep, slabp);
  2787. check_spinlock_acquired(cachep);
  2788. /*
  2789. * The slab was either on partial or free list so
  2790. * there must be at least one object available for
  2791. * allocation.
  2792. */
  2793. BUG_ON(slabp->inuse >= cachep->num);
  2794. while (slabp->inuse < cachep->num && batchcount--) {
  2795. STATS_INC_ALLOCED(cachep);
  2796. STATS_INC_ACTIVE(cachep);
  2797. STATS_SET_HIGH(cachep);
  2798. ac->entry[ac->avail++] = slab_get_obj(cachep, slabp,
  2799. node);
  2800. }
  2801. check_slabp(cachep, slabp);
  2802. /* move slabp to correct slabp list: */
  2803. list_del(&slabp->list);
  2804. if (slabp->free == BUFCTL_END)
  2805. list_add(&slabp->list, &l3->slabs_full);
  2806. else
  2807. list_add(&slabp->list, &l3->slabs_partial);
  2808. }
  2809. must_grow:
  2810. l3->free_objects -= ac->avail;
  2811. alloc_done:
  2812. spin_unlock(&l3->list_lock);
  2813. if (unlikely(!ac->avail)) {
  2814. int x;
  2815. x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
  2816. /* cache_grow can reenable interrupts, then ac could change. */
  2817. ac = cpu_cache_get(cachep);
  2818. if (!x && ac->avail == 0) /* no objects in sight? abort */
  2819. return NULL;
  2820. if (!ac->avail) /* objects refilled by interrupt? */
  2821. goto retry;
  2822. }
  2823. ac->touched = 1;
  2824. return ac->entry[--ac->avail];
  2825. }
  2826. static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
  2827. gfp_t flags)
  2828. {
  2829. might_sleep_if(flags & __GFP_WAIT);
  2830. #if DEBUG
  2831. kmem_flagcheck(cachep, flags);
  2832. #endif
  2833. }
  2834. #if DEBUG
  2835. static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
  2836. gfp_t flags, void *objp, void *caller)
  2837. {
  2838. if (!objp)
  2839. return objp;
  2840. if (cachep->flags & SLAB_POISON) {
  2841. #ifdef CONFIG_DEBUG_PAGEALLOC
  2842. if ((cachep->buffer_size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
  2843. kernel_map_pages(virt_to_page(objp),
  2844. cachep->buffer_size / PAGE_SIZE, 1);
  2845. else
  2846. check_poison_obj(cachep, objp);
  2847. #else
  2848. check_poison_obj(cachep, objp);
  2849. #endif
  2850. poison_obj(cachep, objp, POISON_INUSE);
  2851. }
  2852. if (cachep->flags & SLAB_STORE_USER)
  2853. *dbg_userword(cachep, objp) = caller;
  2854. if (cachep->flags & SLAB_RED_ZONE) {
  2855. if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
  2856. *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
  2857. slab_error(cachep, "double free, or memory outside"
  2858. " object was overwritten");
  2859. printk(KERN_ERR
  2860. "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
  2861. objp, *dbg_redzone1(cachep, objp),
  2862. *dbg_redzone2(cachep, objp));
  2863. }
  2864. *dbg_redzone1(cachep, objp) = RED_ACTIVE;
  2865. *dbg_redzone2(cachep, objp) = RED_ACTIVE;
  2866. }
  2867. #ifdef CONFIG_DEBUG_SLAB_LEAK
  2868. {
  2869. struct slab *slabp;
  2870. unsigned objnr;
  2871. slabp = page_get_slab(virt_to_head_page(objp));
  2872. objnr = (unsigned)(objp - slabp->s_mem) / cachep->buffer_size;
  2873. slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE;
  2874. }
  2875. #endif
  2876. objp += obj_offset(cachep);
  2877. if (cachep->ctor && cachep->flags & SLAB_POISON)
  2878. cachep->ctor(objp);
  2879. if (ARCH_SLAB_MINALIGN &&
  2880. ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
  2881. printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
  2882. objp, (int)ARCH_SLAB_MINALIGN);
  2883. }
  2884. return objp;
  2885. }
  2886. #else
  2887. #define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
  2888. #endif
  2889. static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags)
  2890. {
  2891. if (cachep == &cache_cache)
  2892. return false;
  2893. return should_failslab(obj_size(cachep), flags, cachep->flags);
  2894. }
  2895. static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  2896. {
  2897. void *objp;
  2898. struct array_cache *ac;
  2899. check_irq_off();
  2900. ac = cpu_cache_get(cachep);
  2901. if (likely(ac->avail)) {
  2902. STATS_INC_ALLOCHIT(cachep);
  2903. ac->touched = 1;
  2904. objp = ac->entry[--ac->avail];
  2905. } else {
  2906. STATS_INC_ALLOCMISS(cachep);
  2907. objp = cache_alloc_refill(cachep, flags);
  2908. /*
  2909. * the 'ac' may be updated by cache_alloc_refill(),
  2910. * and kmemleak_erase() requires its correct value.
  2911. */
  2912. ac = cpu_cache_get(cachep);
  2913. }
  2914. /*
  2915. * To avoid a false negative, if an object that is in one of the
  2916. * per-CPU caches is leaked, we need to make sure kmemleak doesn't
  2917. * treat the array pointers as a reference to the object.
  2918. */
  2919. if (objp)
  2920. kmemleak_erase(&ac->entry[ac->avail]);
  2921. return objp;
  2922. }
  2923. #ifdef CONFIG_NUMA
  2924. /*
  2925. * Try allocating on another node if PFA_SPREAD_SLAB|PF_MEMPOLICY.
  2926. *
  2927. * If we are in_interrupt, then process context, including cpusets and
  2928. * mempolicy, may not apply and should not be used for allocation policy.
  2929. */
  2930. static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
  2931. {
  2932. int nid_alloc, nid_here;
  2933. if (in_interrupt() || (flags & __GFP_THISNODE))
  2934. return NULL;
  2935. nid_alloc = nid_here = numa_mem_id();
  2936. if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
  2937. nid_alloc = cpuset_slab_spread_node();
  2938. else if (current->mempolicy)
  2939. nid_alloc = slab_node();
  2940. if (nid_alloc != nid_here)
  2941. return ____cache_alloc_node(cachep, flags, nid_alloc);
  2942. return NULL;
  2943. }
  2944. /*
  2945. * Fallback function if there was no memory available and no objects on a
  2946. * certain node and fall back is permitted. First we scan all the
  2947. * available nodelists for available objects. If that fails then we
  2948. * perform an allocation without specifying a node. This allows the page
  2949. * allocator to do its reclaim / fallback magic. We then insert the
  2950. * slab into the proper nodelist and then allocate from it.
  2951. */
  2952. static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
  2953. {
  2954. struct zonelist *zonelist;
  2955. gfp_t local_flags;
  2956. struct zoneref *z;
  2957. struct zone *zone;
  2958. enum zone_type high_zoneidx = gfp_zone(flags);
  2959. void *obj = NULL;
  2960. int nid;
  2961. unsigned int cpuset_mems_cookie;
  2962. if (flags & __GFP_THISNODE)
  2963. return NULL;
  2964. local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
  2965. retry_cpuset:
  2966. cpuset_mems_cookie = get_mems_allowed();
  2967. zonelist = node_zonelist(slab_node(), flags);
  2968. retry:
  2969. /*
  2970. * Look through allowed nodes for objects available
  2971. * from existing per node queues.
  2972. */
  2973. for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
  2974. nid = zone_to_nid(zone);
  2975. if (cpuset_zone_allowed_hardwall(zone, flags) &&
  2976. cache->nodelists[nid] &&
  2977. cache->nodelists[nid]->free_objects) {
  2978. obj = ____cache_alloc_node(cache,
  2979. flags | GFP_THISNODE, nid);
  2980. if (obj)
  2981. break;
  2982. }
  2983. }
  2984. if (!obj) {
  2985. /*
  2986. * This allocation will be performed within the constraints
  2987. * of the current cpuset / memory policy requirements.
  2988. * We may trigger various forms of reclaim on the allowed
  2989. * set and go into memory reserves if necessary.
  2990. */
  2991. if (local_flags & __GFP_WAIT)
  2992. local_irq_enable();
  2993. kmem_flagcheck(cache, flags);
  2994. obj = kmem_getpages(cache, local_flags, numa_mem_id());
  2995. if (local_flags & __GFP_WAIT)
  2996. local_irq_disable();
  2997. if (obj) {
  2998. /*
  2999. * Insert into the appropriate per node queues
  3000. */
  3001. nid = page_to_nid(virt_to_page(obj));
  3002. if (cache_grow(cache, flags, nid, obj)) {
  3003. obj = ____cache_alloc_node(cache,
  3004. flags | GFP_THISNODE, nid);
  3005. if (!obj)
  3006. /*
  3007. * Another processor may allocate the
  3008. * objects in the slab since we are
  3009. * not holding any locks.
  3010. */
  3011. goto retry;
  3012. } else {
  3013. /* cache_grow already freed obj */
  3014. obj = NULL;
  3015. }
  3016. }
  3017. }
  3018. if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !obj))
  3019. goto retry_cpuset;
  3020. return obj;
  3021. }
  3022. /*
  3023. * A interface to enable slab creation on nodeid
  3024. */
  3025. static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
  3026. int nodeid)
  3027. {
  3028. struct list_head *entry;
  3029. struct slab *slabp;
  3030. struct kmem_list3 *l3;
  3031. void *obj;
  3032. int x;
  3033. l3 = cachep->nodelists[nodeid];
  3034. BUG_ON(!l3);
  3035. retry:
  3036. check_irq_off();
  3037. spin_lock(&l3->list_lock);
  3038. entry = l3->slabs_partial.next;
  3039. if (entry == &l3->slabs_partial) {
  3040. l3->free_touched = 1;
  3041. entry = l3->slabs_free.next;
  3042. if (entry == &l3->slabs_free)
  3043. goto must_grow;
  3044. }
  3045. slabp = list_entry(entry, struct slab, list);
  3046. check_spinlock_acquired_node(cachep, nodeid);
  3047. check_slabp(cachep, slabp);
  3048. STATS_INC_NODEALLOCS(cachep);
  3049. STATS_INC_ACTIVE(cachep);
  3050. STATS_SET_HIGH(cachep);
  3051. BUG_ON(slabp->inuse == cachep->num);
  3052. obj = slab_get_obj(cachep, slabp, nodeid);
  3053. check_slabp(cachep, slabp);
  3054. l3->free_objects--;
  3055. /* move slabp to correct slabp list: */
  3056. list_del(&slabp->list);
  3057. if (slabp->free == BUFCTL_END)
  3058. list_add(&slabp->list, &l3->slabs_full);
  3059. else
  3060. list_add(&slabp->list, &l3->slabs_partial);
  3061. spin_unlock(&l3->list_lock);
  3062. goto done;
  3063. must_grow:
  3064. spin_unlock(&l3->list_lock);
  3065. x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
  3066. if (x)
  3067. goto retry;
  3068. return fallback_alloc(cachep, flags);
  3069. done:
  3070. return obj;
  3071. }
  3072. /**
  3073. * kmem_cache_alloc_node - Allocate an object on the specified node
  3074. * @cachep: The cache to allocate from.
  3075. * @flags: See kmalloc().
  3076. * @nodeid: node number of the target node.
  3077. * @caller: return address of caller, used for debug information
  3078. *
  3079. * Identical to kmem_cache_alloc but it will allocate memory on the given
  3080. * node, which can improve the performance for cpu bound structures.
  3081. *
  3082. * Fallback to other node is possible if __GFP_THISNODE is not set.
  3083. */
  3084. static __always_inline void *
  3085. __cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
  3086. void *caller)
  3087. {
  3088. unsigned long save_flags;
  3089. void *ptr;
  3090. int slab_node = numa_mem_id();
  3091. flags &= gfp_allowed_mask;
  3092. lockdep_trace_alloc(flags);
  3093. if (slab_should_failslab(cachep, flags))
  3094. return NULL;
  3095. cache_alloc_debugcheck_before(cachep, flags);
  3096. local_irq_save(save_flags);
  3097. if (nodeid == NUMA_NO_NODE)
  3098. nodeid = slab_node;
  3099. if (unlikely(!cachep->nodelists[nodeid])) {
  3100. /* Node not bootstrapped yet */
  3101. ptr = fallback_alloc(cachep, flags);
  3102. goto out;
  3103. }
  3104. if (nodeid == slab_node) {
  3105. /*
  3106. * Use the locally cached objects if possible.
  3107. * However ____cache_alloc does not allow fallback
  3108. * to other nodes. It may fail while we still have
  3109. * objects on other nodes available.
  3110. */
  3111. ptr = ____cache_alloc(cachep, flags);
  3112. if (ptr)
  3113. goto out;
  3114. }
  3115. /* ___cache_alloc_node can fall back to other nodes */
  3116. ptr = ____cache_alloc_node(cachep, flags, nodeid);
  3117. out:
  3118. local_irq_restore(save_flags);
  3119. ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
  3120. kmemleak_alloc_recursive(ptr, obj_size(cachep), 1, cachep->flags,
  3121. flags);
  3122. if (likely(ptr))
  3123. kmemcheck_slab_alloc(cachep, flags, ptr, obj_size(cachep));
  3124. if (unlikely((flags & __GFP_ZERO) && ptr))
  3125. memset(ptr, 0, obj_size(cachep));
  3126. return ptr;
  3127. }
  3128. static __always_inline void *
  3129. __do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
  3130. {
  3131. void *objp;
  3132. if (unlikely((current->flags & PF_MEMPOLICY) || cpuset_do_slab_mem_spread())) {
  3133. objp = alternate_node_alloc(cache, flags);
  3134. if (objp)
  3135. goto out;
  3136. }
  3137. objp = ____cache_alloc(cache, flags);
  3138. /*
  3139. * We may just have run out of memory on the local node.
  3140. * ____cache_alloc_node() knows how to locate memory on other nodes
  3141. */
  3142. if (!objp)
  3143. objp = ____cache_alloc_node(cache, flags, numa_mem_id());
  3144. out:
  3145. return objp;
  3146. }
  3147. #else
  3148. static __always_inline void *
  3149. __do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  3150. {
  3151. return ____cache_alloc(cachep, flags);
  3152. }
  3153. #endif /* CONFIG_NUMA */
  3154. static __always_inline void *
  3155. __cache_alloc(struct kmem_cache *cachep, gfp_t flags, void *caller)
  3156. {
  3157. unsigned long save_flags;
  3158. void *objp;
  3159. flags &= gfp_allowed_mask;
  3160. lockdep_trace_alloc(flags);
  3161. if (slab_should_failslab(cachep, flags))
  3162. return NULL;
  3163. cache_alloc_debugcheck_before(cachep, flags);
  3164. local_irq_save(save_flags);
  3165. objp = __do_cache_alloc(cachep, flags);
  3166. local_irq_restore(save_flags);
  3167. objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
  3168. kmemleak_alloc_recursive(objp, obj_size(cachep), 1, cachep->flags,
  3169. flags);
  3170. prefetchw(objp);
  3171. if (likely(objp))
  3172. kmemcheck_slab_alloc(cachep, flags, objp, obj_size(cachep));
  3173. if (unlikely((flags & __GFP_ZERO) && objp))
  3174. memset(objp, 0, obj_size(cachep));
  3175. return objp;
  3176. }
  3177. /*
  3178. * Caller needs to acquire correct kmem_list's list_lock
  3179. */
  3180. static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
  3181. int node)
  3182. {
  3183. int i;
  3184. struct kmem_list3 *l3;
  3185. for (i = 0; i < nr_objects; i++) {
  3186. void *objp = objpp[i];
  3187. struct slab *slabp;
  3188. slabp = virt_to_slab(objp);
  3189. l3 = cachep->nodelists[node];
  3190. list_del(&slabp->list);
  3191. check_spinlock_acquired_node(cachep, node);
  3192. check_slabp(cachep, slabp);
  3193. slab_put_obj(cachep, slabp, objp, node);
  3194. STATS_DEC_ACTIVE(cachep);
  3195. l3->free_objects++;
  3196. check_slabp(cachep, slabp);
  3197. /* fixup slab chains */
  3198. if (slabp->inuse == 0) {
  3199. if (l3->free_objects > l3->free_limit) {
  3200. l3->free_objects -= cachep->num;
  3201. /* No need to drop any previously held
  3202. * lock here, even if we have a off-slab slab
  3203. * descriptor it is guaranteed to come from
  3204. * a different cache, refer to comments before
  3205. * alloc_slabmgmt.
  3206. */
  3207. slab_destroy(cachep, slabp);
  3208. } else {
  3209. list_add(&slabp->list, &l3->slabs_free);
  3210. }
  3211. } else {
  3212. /* Unconditionally move a slab to the end of the
  3213. * partial list on free - maximum time for the
  3214. * other objects to be freed, too.
  3215. */
  3216. list_add_tail(&slabp->list, &l3->slabs_partial);
  3217. }
  3218. }
  3219. }
  3220. static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
  3221. {
  3222. int batchcount;
  3223. struct kmem_list3 *l3;
  3224. int node = numa_mem_id();
  3225. batchcount = ac->batchcount;
  3226. #if DEBUG
  3227. BUG_ON(!batchcount || batchcount > ac->avail);
  3228. #endif
  3229. check_irq_off();
  3230. l3 = cachep->nodelists[node];
  3231. spin_lock(&l3->list_lock);
  3232. if (l3->shared) {
  3233. struct array_cache *shared_array = l3->shared;
  3234. int max = shared_array->limit - shared_array->avail;
  3235. if (max) {
  3236. if (batchcount > max)
  3237. batchcount = max;
  3238. memcpy(&(shared_array->entry[shared_array->avail]),
  3239. ac->entry, sizeof(void *) * batchcount);
  3240. shared_array->avail += batchcount;
  3241. goto free_done;
  3242. }
  3243. }
  3244. free_block(cachep, ac->entry, batchcount, node);
  3245. free_done:
  3246. #if STATS
  3247. {
  3248. int i = 0;
  3249. struct list_head *p;
  3250. p = l3->slabs_free.next;
  3251. while (p != &(l3->slabs_free)) {
  3252. struct slab *slabp;
  3253. slabp = list_entry(p, struct slab, list);
  3254. BUG_ON(slabp->inuse);
  3255. i++;
  3256. p = p->next;
  3257. }
  3258. STATS_SET_FREEABLE(cachep, i);
  3259. }
  3260. #endif
  3261. spin_unlock(&l3->list_lock);
  3262. ac->avail -= batchcount;
  3263. memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
  3264. }
  3265. /*
  3266. * Release an obj back to its cache. If the obj has a constructed state, it must
  3267. * be in this state _before_ it is released. Called with disabled ints.
  3268. */
  3269. static inline void __cache_free(struct kmem_cache *cachep, void *objp,
  3270. void *caller)
  3271. {
  3272. struct array_cache *ac = cpu_cache_get(cachep);
  3273. check_irq_off();
  3274. kmemleak_free_recursive(objp, cachep->flags);
  3275. objp = cache_free_debugcheck(cachep, objp, caller);
  3276. kmemcheck_slab_free(cachep, objp, obj_size(cachep));
  3277. /*
  3278. * Skip calling cache_free_alien() when the platform is not numa.
  3279. * This will avoid cache misses that happen while accessing slabp (which
  3280. * is per page memory reference) to get nodeid. Instead use a global
  3281. * variable to skip the call, which is mostly likely to be present in
  3282. * the cache.
  3283. */
  3284. if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
  3285. return;
  3286. if (likely(ac->avail < ac->limit)) {
  3287. STATS_INC_FREEHIT(cachep);
  3288. } else {
  3289. STATS_INC_FREEMISS(cachep);
  3290. cache_flusharray(cachep, ac);
  3291. }
  3292. ac->entry[ac->avail++] = objp;
  3293. }
  3294. /**
  3295. * kmem_cache_alloc - Allocate an object
  3296. * @cachep: The cache to allocate from.
  3297. * @flags: See kmalloc().
  3298. *
  3299. * Allocate an object from this cache. The flags are only relevant
  3300. * if the cache has no available objects.
  3301. */
  3302. void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
  3303. {
  3304. void *ret = __cache_alloc(cachep, flags, __builtin_return_address(0));
  3305. trace_kmem_cache_alloc(_RET_IP_, ret,
  3306. obj_size(cachep), cachep->buffer_size, flags);
  3307. return ret;
  3308. }
  3309. EXPORT_SYMBOL(kmem_cache_alloc);
  3310. #ifdef CONFIG_TRACING
  3311. void *
  3312. kmem_cache_alloc_trace(size_t size, struct kmem_cache *cachep, gfp_t flags)
  3313. {
  3314. void *ret;
  3315. ret = __cache_alloc(cachep, flags, __builtin_return_address(0));
  3316. trace_kmalloc(_RET_IP_, ret,
  3317. size, slab_buffer_size(cachep), flags);
  3318. return ret;
  3319. }
  3320. EXPORT_SYMBOL(kmem_cache_alloc_trace);
  3321. #endif
  3322. #ifdef CONFIG_NUMA
  3323. void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
  3324. {
  3325. void *ret = __cache_alloc_node(cachep, flags, nodeid,
  3326. __builtin_return_address(0));
  3327. trace_kmem_cache_alloc_node(_RET_IP_, ret,
  3328. obj_size(cachep), cachep->buffer_size,
  3329. flags, nodeid);
  3330. return ret;
  3331. }
  3332. EXPORT_SYMBOL(kmem_cache_alloc_node);
  3333. #ifdef CONFIG_TRACING
  3334. void *kmem_cache_alloc_node_trace(size_t size,
  3335. struct kmem_cache *cachep,
  3336. gfp_t flags,
  3337. int nodeid)
  3338. {
  3339. void *ret;
  3340. ret = __cache_alloc_node(cachep, flags, nodeid,
  3341. __builtin_return_address(0));
  3342. trace_kmalloc_node(_RET_IP_, ret,
  3343. size, slab_buffer_size(cachep),
  3344. flags, nodeid);
  3345. return ret;
  3346. }
  3347. EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
  3348. #endif
  3349. static __always_inline void *
  3350. __do_kmalloc_node(size_t size, gfp_t flags, int node, void *caller)
  3351. {
  3352. struct kmem_cache *cachep;
  3353. cachep = kmem_find_general_cachep(size, flags);
  3354. if (unlikely(ZERO_OR_NULL_PTR(cachep)))
  3355. return cachep;
  3356. return kmem_cache_alloc_node_trace(size, cachep, flags, node);
  3357. }
  3358. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
  3359. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  3360. {
  3361. return __do_kmalloc_node(size, flags, node,
  3362. __builtin_return_address(0));
  3363. }
  3364. EXPORT_SYMBOL(__kmalloc_node);
  3365. void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
  3366. int node, unsigned long caller)
  3367. {
  3368. return __do_kmalloc_node(size, flags, node, (void *)caller);
  3369. }
  3370. EXPORT_SYMBOL(__kmalloc_node_track_caller);
  3371. #else
  3372. void *__kmalloc_node(size_t size, gfp_t flags, int node)
  3373. {
  3374. return __do_kmalloc_node(size, flags, node, NULL);
  3375. }
  3376. EXPORT_SYMBOL(__kmalloc_node);
  3377. #endif /* CONFIG_DEBUG_SLAB || CONFIG_TRACING */
  3378. #endif /* CONFIG_NUMA */
  3379. /**
  3380. * __do_kmalloc - allocate memory
  3381. * @size: how many bytes of memory are required.
  3382. * @flags: the type of memory to allocate (see kmalloc).
  3383. * @caller: function caller for debug tracking of the caller
  3384. */
  3385. static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
  3386. void *caller)
  3387. {
  3388. struct kmem_cache *cachep;
  3389. void *ret;
  3390. /* If you want to save a few bytes .text space: replace
  3391. * __ with kmem_.
  3392. * Then kmalloc uses the uninlined functions instead of the inline
  3393. * functions.
  3394. */
  3395. cachep = __find_general_cachep(size, flags);
  3396. if (unlikely(ZERO_OR_NULL_PTR(cachep)))
  3397. return cachep;
  3398. ret = __cache_alloc(cachep, flags, caller);
  3399. trace_kmalloc((unsigned long) caller, ret,
  3400. size, cachep->buffer_size, flags);
  3401. return ret;
  3402. }
  3403. #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
  3404. void *__kmalloc(size_t size, gfp_t flags)
  3405. {
  3406. return __do_kmalloc(size, flags, __builtin_return_address(0));
  3407. }
  3408. EXPORT_SYMBOL(__kmalloc);
  3409. void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
  3410. {
  3411. return __do_kmalloc(size, flags, (void *)caller);
  3412. }
  3413. EXPORT_SYMBOL(__kmalloc_track_caller);
  3414. #else
  3415. void *__kmalloc(size_t size, gfp_t flags)
  3416. {
  3417. return __do_kmalloc(size, flags, NULL);
  3418. }
  3419. EXPORT_SYMBOL(__kmalloc);
  3420. #endif
  3421. /**
  3422. * kmem_cache_free - Deallocate an object
  3423. * @cachep: The cache the allocation was from.
  3424. * @objp: The previously allocated object.
  3425. *
  3426. * Free an object which was previously allocated from this
  3427. * cache.
  3428. */
  3429. void kmem_cache_free(struct kmem_cache *cachep, void *objp)
  3430. {
  3431. unsigned long flags;
  3432. local_irq_save(flags);
  3433. debug_check_no_locks_freed(objp, obj_size(cachep));
  3434. if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
  3435. debug_check_no_obj_freed(objp, obj_size(cachep));
  3436. __cache_free(cachep, objp, __builtin_return_address(0));
  3437. local_irq_restore(flags);
  3438. trace_kmem_cache_free(_RET_IP_, objp);
  3439. }
  3440. EXPORT_SYMBOL(kmem_cache_free);
  3441. /**
  3442. * kfree - free previously allocated memory
  3443. * @objp: pointer returned by kmalloc.
  3444. *
  3445. * If @objp is NULL, no operation is performed.
  3446. *
  3447. * Don't free memory not originally allocated by kmalloc()
  3448. * or you will run into trouble.
  3449. */
  3450. void kfree(const void *objp)
  3451. {
  3452. struct kmem_cache *c;
  3453. unsigned long flags;
  3454. trace_kfree(_RET_IP_, objp);
  3455. if (unlikely(ZERO_OR_NULL_PTR(objp)))
  3456. return;
  3457. local_irq_save(flags);
  3458. kfree_debugcheck(objp);
  3459. c = virt_to_cache(objp);
  3460. debug_check_no_locks_freed(objp, obj_size(c));
  3461. debug_check_no_obj_freed(objp, obj_size(c));
  3462. __cache_free(c, (void *)objp, __builtin_return_address(0));
  3463. local_irq_restore(flags);
  3464. }
  3465. EXPORT_SYMBOL(kfree);
  3466. unsigned int kmem_cache_size(struct kmem_cache *cachep)
  3467. {
  3468. return obj_size(cachep);
  3469. }
  3470. EXPORT_SYMBOL(kmem_cache_size);
  3471. /*
  3472. * This initializes kmem_list3 or resizes various caches for all nodes.
  3473. */
  3474. static int alloc_kmemlist(struct kmem_cache *cachep, gfp_t gfp)
  3475. {
  3476. int node;
  3477. struct kmem_list3 *l3;
  3478. struct array_cache *new_shared;
  3479. struct array_cache **new_alien = NULL;
  3480. for_each_online_node(node) {
  3481. if (use_alien_caches) {
  3482. new_alien = alloc_alien_cache(node, cachep->limit, gfp);
  3483. if (!new_alien)
  3484. goto fail;
  3485. }
  3486. new_shared = NULL;
  3487. if (cachep->shared) {
  3488. new_shared = alloc_arraycache(node,
  3489. cachep->shared*cachep->batchcount,
  3490. 0xbaadf00d, gfp);
  3491. if (!new_shared) {
  3492. free_alien_cache(new_alien);
  3493. goto fail;
  3494. }
  3495. }
  3496. l3 = cachep->nodelists[node];
  3497. if (l3) {
  3498. struct array_cache *shared = l3->shared;
  3499. spin_lock_irq(&l3->list_lock);
  3500. if (shared)
  3501. free_block(cachep, shared->entry,
  3502. shared->avail, node);
  3503. l3->shared = new_shared;
  3504. if (!l3->alien) {
  3505. l3->alien = new_alien;
  3506. new_alien = NULL;
  3507. }
  3508. l3->free_limit = (1 + nr_cpus_node(node)) *
  3509. cachep->batchcount + cachep->num;
  3510. spin_unlock_irq(&l3->list_lock);
  3511. kfree(shared);
  3512. free_alien_cache(new_alien);
  3513. continue;
  3514. }
  3515. l3 = kmalloc_node(sizeof(struct kmem_list3), gfp, node);
  3516. if (!l3) {
  3517. free_alien_cache(new_alien);
  3518. kfree(new_shared);
  3519. goto fail;
  3520. }
  3521. kmem_list3_init(l3);
  3522. l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
  3523. ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
  3524. l3->shared = new_shared;
  3525. l3->alien = new_alien;
  3526. l3->free_limit = (1 + nr_cpus_node(node)) *
  3527. cachep->batchcount + cachep->num;
  3528. cachep->nodelists[node] = l3;
  3529. }
  3530. return 0;
  3531. fail:
  3532. if (!cachep->next.next) {
  3533. /* Cache is not active yet. Roll back what we did */
  3534. node--;
  3535. while (node >= 0) {
  3536. if (cachep->nodelists[node]) {
  3537. l3 = cachep->nodelists[node];
  3538. kfree(l3->shared);
  3539. free_alien_cache(l3->alien);
  3540. kfree(l3);
  3541. cachep->nodelists[node] = NULL;
  3542. }
  3543. node--;
  3544. }
  3545. }
  3546. return -ENOMEM;
  3547. }
  3548. struct ccupdate_struct {
  3549. struct kmem_cache *cachep;
  3550. struct array_cache *new[0];
  3551. };
  3552. static void do_ccupdate_local(void *info)
  3553. {
  3554. struct ccupdate_struct *new = info;
  3555. struct array_cache *old;
  3556. check_irq_off();
  3557. old = cpu_cache_get(new->cachep);
  3558. new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
  3559. new->new[smp_processor_id()] = old;
  3560. }
  3561. /* Always called with the cache_chain_mutex held */
  3562. static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
  3563. int batchcount, int shared, gfp_t gfp)
  3564. {
  3565. struct ccupdate_struct *new;
  3566. int i;
  3567. new = kzalloc(sizeof(*new) + nr_cpu_ids * sizeof(struct array_cache *),
  3568. gfp);
  3569. if (!new)
  3570. return -ENOMEM;
  3571. for_each_online_cpu(i) {
  3572. new->new[i] = alloc_arraycache(cpu_to_mem(i), limit,
  3573. batchcount, gfp);
  3574. if (!new->new[i]) {
  3575. for (i--; i >= 0; i--)
  3576. kfree(new->new[i]);
  3577. kfree(new);
  3578. return -ENOMEM;
  3579. }
  3580. }
  3581. new->cachep = cachep;
  3582. on_each_cpu(do_ccupdate_local, (void *)new, 1);
  3583. check_irq_on();
  3584. cachep->batchcount = batchcount;
  3585. cachep->limit = limit;
  3586. cachep->shared = shared;
  3587. for_each_online_cpu(i) {
  3588. struct array_cache *ccold = new->new[i];
  3589. if (!ccold)
  3590. continue;
  3591. spin_lock_irq(&cachep->nodelists[cpu_to_mem(i)]->list_lock);
  3592. free_block(cachep, ccold->entry, ccold->avail, cpu_to_mem(i));
  3593. spin_unlock_irq(&cachep->nodelists[cpu_to_mem(i)]->list_lock);
  3594. kfree(ccold);
  3595. }
  3596. kfree(new);
  3597. return alloc_kmemlist(cachep, gfp);
  3598. }
  3599. /* Called with cache_chain_mutex held always */
  3600. static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
  3601. {
  3602. int err;
  3603. int limit, shared;
  3604. /*
  3605. * The head array serves three purposes:
  3606. * - create a LIFO ordering, i.e. return objects that are cache-warm
  3607. * - reduce the number of spinlock operations.
  3608. * - reduce the number of linked list operations on the slab and
  3609. * bufctl chains: array operations are cheaper.
  3610. * The numbers are guessed, we should auto-tune as described by
  3611. * Bonwick.
  3612. */
  3613. if (cachep->buffer_size > 131072)
  3614. limit = 1;
  3615. else if (cachep->buffer_size > PAGE_SIZE)
  3616. limit = 8;
  3617. else if (cachep->buffer_size > 1024)
  3618. limit = 24;
  3619. else if (cachep->buffer_size > 256)
  3620. limit = 54;
  3621. else
  3622. limit = 120;
  3623. /*
  3624. * CPU bound tasks (e.g. network routing) can exhibit cpu bound
  3625. * allocation behaviour: Most allocs on one cpu, most free operations
  3626. * on another cpu. For these cases, an efficient object passing between
  3627. * cpus is necessary. This is provided by a shared array. The array
  3628. * replaces Bonwick's magazine layer.
  3629. * On uniprocessor, it's functionally equivalent (but less efficient)
  3630. * to a larger limit. Thus disabled by default.
  3631. */
  3632. shared = 0;
  3633. if (cachep->buffer_size <= PAGE_SIZE && num_possible_cpus() > 1)
  3634. shared = 8;
  3635. #if DEBUG
  3636. /*
  3637. * With debugging enabled, large batchcount lead to excessively long
  3638. * periods with disabled local interrupts. Limit the batchcount
  3639. */
  3640. if (limit > 32)
  3641. limit = 32;
  3642. #endif
  3643. err = do_tune_cpucache(cachep, limit, (limit + 1) / 2, shared, gfp);
  3644. if (err)
  3645. printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
  3646. cachep->name, -err);
  3647. return err;
  3648. }
  3649. /*
  3650. * Drain an array if it contains any elements taking the l3 lock only if
  3651. * necessary. Note that the l3 listlock also protects the array_cache
  3652. * if drain_array() is used on the shared array.
  3653. */
  3654. static void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
  3655. struct array_cache *ac, int force, int node)
  3656. {
  3657. int tofree;
  3658. if (!ac || !ac->avail)
  3659. return;
  3660. if (ac->touched && !force) {
  3661. ac->touched = 0;
  3662. } else {
  3663. spin_lock_irq(&l3->list_lock);
  3664. if (ac->avail) {
  3665. tofree = force ? ac->avail : (ac->limit + 4) / 5;
  3666. if (tofree > ac->avail)
  3667. tofree = (ac->avail + 1) / 2;
  3668. free_block(cachep, ac->entry, tofree, node);
  3669. ac->avail -= tofree;
  3670. memmove(ac->entry, &(ac->entry[tofree]),
  3671. sizeof(void *) * ac->avail);
  3672. }
  3673. spin_unlock_irq(&l3->list_lock);
  3674. }
  3675. }
  3676. /**
  3677. * cache_reap - Reclaim memory from caches.
  3678. * @w: work descriptor
  3679. *
  3680. * Called from workqueue/eventd every few seconds.
  3681. * Purpose:
  3682. * - clear the per-cpu caches for this CPU.
  3683. * - return freeable pages to the main free memory pool.
  3684. *
  3685. * If we cannot acquire the cache chain mutex then just give up - we'll try
  3686. * again on the next iteration.
  3687. */
  3688. static void cache_reap(struct work_struct *w)
  3689. {
  3690. struct kmem_cache *searchp;
  3691. struct kmem_list3 *l3;
  3692. int node = numa_mem_id();
  3693. struct delayed_work *work = to_delayed_work(w);
  3694. if (!mutex_trylock(&cache_chain_mutex))
  3695. /* Give up. Setup the next iteration. */
  3696. goto out;
  3697. list_for_each_entry(searchp, &cache_chain, next) {
  3698. check_irq_on();
  3699. /*
  3700. * We only take the l3 lock if absolutely necessary and we
  3701. * have established with reasonable certainty that
  3702. * we can do some work if the lock was obtained.
  3703. */
  3704. l3 = searchp->nodelists[node];
  3705. reap_alien(searchp, l3);
  3706. drain_array(searchp, l3, cpu_cache_get(searchp), 0, node);
  3707. /*
  3708. * These are racy checks but it does not matter
  3709. * if we skip one check or scan twice.
  3710. */
  3711. if (time_after(l3->next_reap, jiffies))
  3712. goto next;
  3713. l3->next_reap = jiffies + REAPTIMEOUT_LIST3;
  3714. drain_array(searchp, l3, l3->shared, 0, node);
  3715. if (l3->free_touched)
  3716. l3->free_touched = 0;
  3717. else {
  3718. int freed;
  3719. freed = drain_freelist(searchp, l3, (l3->free_limit +
  3720. 5 * searchp->num - 1) / (5 * searchp->num));
  3721. STATS_ADD_REAPED(searchp, freed);
  3722. }
  3723. next:
  3724. cond_resched();
  3725. }
  3726. check_irq_on();
  3727. mutex_unlock(&cache_chain_mutex);
  3728. next_reap_node();
  3729. out:
  3730. /* Set up the next iteration */
  3731. schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_CPUC));
  3732. }
  3733. #ifdef CONFIG_SLABINFO
  3734. static void print_slabinfo_header(struct seq_file *m)
  3735. {
  3736. /*
  3737. * Output format version, so at least we can change it
  3738. * without _too_ many complaints.
  3739. */
  3740. #if STATS
  3741. seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
  3742. #else
  3743. seq_puts(m, "slabinfo - version: 2.1\n");
  3744. #endif
  3745. seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
  3746. "<objperslab> <pagesperslab>");
  3747. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  3748. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  3749. #if STATS
  3750. seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
  3751. "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
  3752. seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
  3753. #endif
  3754. seq_putc(m, '\n');
  3755. }
  3756. static void *s_start(struct seq_file *m, loff_t *pos)
  3757. {
  3758. loff_t n = *pos;
  3759. mutex_lock(&cache_chain_mutex);
  3760. if (!n)
  3761. print_slabinfo_header(m);
  3762. return seq_list_start(&cache_chain, *pos);
  3763. }
  3764. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  3765. {
  3766. return seq_list_next(p, &cache_chain, pos);
  3767. }
  3768. static void s_stop(struct seq_file *m, void *p)
  3769. {
  3770. mutex_unlock(&cache_chain_mutex);
  3771. }
  3772. static int s_show(struct seq_file *m, void *p)
  3773. {
  3774. struct kmem_cache *cachep = list_entry(p, struct kmem_cache, next);
  3775. struct slab *slabp;
  3776. unsigned long active_objs;
  3777. unsigned long num_objs;
  3778. unsigned long active_slabs = 0;
  3779. unsigned long num_slabs, free_objects = 0, shared_avail = 0;
  3780. const char *name;
  3781. char *error = NULL;
  3782. int node;
  3783. struct kmem_list3 *l3;
  3784. active_objs = 0;
  3785. num_slabs = 0;
  3786. for_each_online_node(node) {
  3787. l3 = cachep->nodelists[node];
  3788. if (!l3)
  3789. continue;
  3790. check_irq_on();
  3791. spin_lock_irq(&l3->list_lock);
  3792. list_for_each_entry(slabp, &l3->slabs_full, list) {
  3793. if (slabp->inuse != cachep->num && !error)
  3794. error = "slabs_full accounting error";
  3795. active_objs += cachep->num;
  3796. active_slabs++;
  3797. }
  3798. list_for_each_entry(slabp, &l3->slabs_partial, list) {
  3799. if (slabp->inuse == cachep->num && !error)
  3800. error = "slabs_partial inuse accounting error";
  3801. if (!slabp->inuse && !error)
  3802. error = "slabs_partial/inuse accounting error";
  3803. active_objs += slabp->inuse;
  3804. active_slabs++;
  3805. }
  3806. list_for_each_entry(slabp, &l3->slabs_free, list) {
  3807. if (slabp->inuse && !error)
  3808. error = "slabs_free/inuse accounting error";
  3809. num_slabs++;
  3810. }
  3811. free_objects += l3->free_objects;
  3812. if (l3->shared)
  3813. shared_avail += l3->shared->avail;
  3814. spin_unlock_irq(&l3->list_lock);
  3815. }
  3816. num_slabs += active_slabs;
  3817. num_objs = num_slabs * cachep->num;
  3818. if (num_objs - active_objs != free_objects && !error)
  3819. error = "free_objects accounting error";
  3820. name = cachep->name;
  3821. if (error)
  3822. printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
  3823. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  3824. name, active_objs, num_objs, cachep->buffer_size,
  3825. cachep->num, (1 << cachep->gfporder));
  3826. seq_printf(m, " : tunables %4u %4u %4u",
  3827. cachep->limit, cachep->batchcount, cachep->shared);
  3828. seq_printf(m, " : slabdata %6lu %6lu %6lu",
  3829. active_slabs, num_slabs, shared_avail);
  3830. #if STATS
  3831. { /* list3 stats */
  3832. unsigned long high = cachep->high_mark;
  3833. unsigned long allocs = cachep->num_allocations;
  3834. unsigned long grown = cachep->grown;
  3835. unsigned long reaped = cachep->reaped;
  3836. unsigned long errors = cachep->errors;
  3837. unsigned long max_freeable = cachep->max_freeable;
  3838. unsigned long node_allocs = cachep->node_allocs;
  3839. unsigned long node_frees = cachep->node_frees;
  3840. unsigned long overflows = cachep->node_overflow;
  3841. seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu "
  3842. "%4lu %4lu %4lu %4lu %4lu",
  3843. allocs, high, grown,
  3844. reaped, errors, max_freeable, node_allocs,
  3845. node_frees, overflows);
  3846. }
  3847. /* cpu stats */
  3848. {
  3849. unsigned long allochit = atomic_read(&cachep->allochit);
  3850. unsigned long allocmiss = atomic_read(&cachep->allocmiss);
  3851. unsigned long freehit = atomic_read(&cachep->freehit);
  3852. unsigned long freemiss = atomic_read(&cachep->freemiss);
  3853. seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
  3854. allochit, allocmiss, freehit, freemiss);
  3855. }
  3856. #endif
  3857. seq_putc(m, '\n');
  3858. return 0;
  3859. }
  3860. /*
  3861. * slabinfo_op - iterator that generates /proc/slabinfo
  3862. *
  3863. * Output layout:
  3864. * cache-name
  3865. * num-active-objs
  3866. * total-objs
  3867. * object size
  3868. * num-active-slabs
  3869. * total-slabs
  3870. * num-pages-per-slab
  3871. * + further values on SMP and with statistics enabled
  3872. */
  3873. static const struct seq_operations slabinfo_op = {
  3874. .start = s_start,
  3875. .next = s_next,
  3876. .stop = s_stop,
  3877. .show = s_show,
  3878. };
  3879. #define MAX_SLABINFO_WRITE 128
  3880. /**
  3881. * slabinfo_write - Tuning for the slab allocator
  3882. * @file: unused
  3883. * @buffer: user buffer
  3884. * @count: data length
  3885. * @ppos: unused
  3886. */
  3887. static ssize_t slabinfo_write(struct file *file, const char __user *buffer,
  3888. size_t count, loff_t *ppos)
  3889. {
  3890. char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
  3891. int limit, batchcount, shared, res;
  3892. struct kmem_cache *cachep;
  3893. if (count > MAX_SLABINFO_WRITE)
  3894. return -EINVAL;
  3895. if (copy_from_user(&kbuf, buffer, count))
  3896. return -EFAULT;
  3897. kbuf[MAX_SLABINFO_WRITE] = '\0';
  3898. tmp = strchr(kbuf, ' ');
  3899. if (!tmp)
  3900. return -EINVAL;
  3901. *tmp = '\0';
  3902. tmp++;
  3903. if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
  3904. return -EINVAL;
  3905. /* Find the cache in the chain of caches. */
  3906. mutex_lock(&cache_chain_mutex);
  3907. res = -EINVAL;
  3908. list_for_each_entry(cachep, &cache_chain, next) {
  3909. if (!strcmp(cachep->name, kbuf)) {
  3910. if (limit < 1 || batchcount < 1 ||
  3911. batchcount > limit || shared < 0) {
  3912. res = 0;
  3913. } else {
  3914. res = do_tune_cpucache(cachep, limit,
  3915. batchcount, shared,
  3916. GFP_KERNEL);
  3917. }
  3918. break;
  3919. }
  3920. }
  3921. mutex_unlock(&cache_chain_mutex);
  3922. if (res >= 0)
  3923. res = count;
  3924. return res;
  3925. }
  3926. static int slabinfo_open(struct inode *inode, struct file *file)
  3927. {
  3928. return seq_open(file, &slabinfo_op);
  3929. }
  3930. static const struct file_operations proc_slabinfo_operations = {
  3931. .open = slabinfo_open,
  3932. .read = seq_read,
  3933. .write = slabinfo_write,
  3934. .llseek = seq_lseek,
  3935. .release = seq_release,
  3936. };
  3937. #ifdef CONFIG_DEBUG_SLAB_LEAK
  3938. static void *leaks_start(struct seq_file *m, loff_t *pos)
  3939. {
  3940. mutex_lock(&cache_chain_mutex);
  3941. return seq_list_start(&cache_chain, *pos);
  3942. }
  3943. static inline int add_caller(unsigned long *n, unsigned long v)
  3944. {
  3945. unsigned long *p;
  3946. int l;
  3947. if (!v)
  3948. return 1;
  3949. l = n[1];
  3950. p = n + 2;
  3951. while (l) {
  3952. int i = l/2;
  3953. unsigned long *q = p + 2 * i;
  3954. if (*q == v) {
  3955. q[1]++;
  3956. return 1;
  3957. }
  3958. if (*q > v) {
  3959. l = i;
  3960. } else {
  3961. p = q + 2;
  3962. l -= i + 1;
  3963. }
  3964. }
  3965. if (++n[1] == n[0])
  3966. return 0;
  3967. memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
  3968. p[0] = v;
  3969. p[1] = 1;
  3970. return 1;
  3971. }
  3972. static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
  3973. {
  3974. void *p;
  3975. int i;
  3976. if (n[0] == n[1])
  3977. return;
  3978. for (i = 0, p = s->s_mem; i < c->num; i++, p += c->buffer_size) {
  3979. if (slab_bufctl(s)[i] != BUFCTL_ACTIVE)
  3980. continue;
  3981. if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
  3982. return;
  3983. }
  3984. }
  3985. static void show_symbol(struct seq_file *m, unsigned long address)
  3986. {
  3987. #ifdef CONFIG_KALLSYMS
  3988. unsigned long offset, size;
  3989. char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
  3990. if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
  3991. seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
  3992. if (modname[0])
  3993. seq_printf(m, " [%s]", modname);
  3994. return;
  3995. }
  3996. #endif
  3997. seq_printf(m, "%p", (void *)address);
  3998. }
  3999. static int leaks_show(struct seq_file *m, void *p)
  4000. {
  4001. struct kmem_cache *cachep = list_entry(p, struct kmem_cache, next);
  4002. struct slab *slabp;
  4003. struct kmem_list3 *l3;
  4004. const char *name;
  4005. unsigned long *n = m->private;
  4006. int node;
  4007. int i;
  4008. if (!(cachep->flags & SLAB_STORE_USER))
  4009. return 0;
  4010. if (!(cachep->flags & SLAB_RED_ZONE))
  4011. return 0;
  4012. /* OK, we can do it */
  4013. n[1] = 0;
  4014. for_each_online_node(node) {
  4015. l3 = cachep->nodelists[node];
  4016. if (!l3)
  4017. continue;
  4018. check_irq_on();
  4019. spin_lock_irq(&l3->list_lock);
  4020. list_for_each_entry(slabp, &l3->slabs_full, list)
  4021. handle_slab(n, cachep, slabp);
  4022. list_for_each_entry(slabp, &l3->slabs_partial, list)
  4023. handle_slab(n, cachep, slabp);
  4024. spin_unlock_irq(&l3->list_lock);
  4025. }
  4026. name = cachep->name;
  4027. if (n[0] == n[1]) {
  4028. /* Increase the buffer size */
  4029. mutex_unlock(&cache_chain_mutex);
  4030. m->private = kzalloc(n[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
  4031. if (!m->private) {
  4032. /* Too bad, we are really out */
  4033. m->private = n;
  4034. mutex_lock(&cache_chain_mutex);
  4035. return -ENOMEM;
  4036. }
  4037. *(unsigned long *)m->private = n[0] * 2;
  4038. kfree(n);
  4039. mutex_lock(&cache_chain_mutex);
  4040. /* Now make sure this entry will be retried */
  4041. m->count = m->size;
  4042. return 0;
  4043. }
  4044. for (i = 0; i < n[1]; i++) {
  4045. seq_printf(m, "%s: %lu ", name, n[2*i+3]);
  4046. show_symbol(m, n[2*i+2]);
  4047. seq_putc(m, '\n');
  4048. }
  4049. return 0;
  4050. }
  4051. static const struct seq_operations slabstats_op = {
  4052. .start = leaks_start,
  4053. .next = s_next,
  4054. .stop = s_stop,
  4055. .show = leaks_show,
  4056. };
  4057. static int slabstats_open(struct inode *inode, struct file *file)
  4058. {
  4059. unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
  4060. int ret = -ENOMEM;
  4061. if (n) {
  4062. ret = seq_open(file, &slabstats_op);
  4063. if (!ret) {
  4064. struct seq_file *m = file->private_data;
  4065. *n = PAGE_SIZE / (2 * sizeof(unsigned long));
  4066. m->private = n;
  4067. n = NULL;
  4068. }
  4069. kfree(n);
  4070. }
  4071. return ret;
  4072. }
  4073. static const struct file_operations proc_slabstats_operations = {
  4074. .open = slabstats_open,
  4075. .read = seq_read,
  4076. .llseek = seq_lseek,
  4077. .release = seq_release_private,
  4078. };
  4079. #endif
  4080. static int __init slab_proc_init(void)
  4081. {
  4082. proc_create("slabinfo",S_IWUSR|S_IRUSR,NULL,&proc_slabinfo_operations);
  4083. #ifdef CONFIG_DEBUG_SLAB_LEAK
  4084. proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
  4085. #endif
  4086. return 0;
  4087. }
  4088. module_init(slab_proc_init);
  4089. #endif
  4090. /**
  4091. * ksize - get the actual amount of memory allocated for a given object
  4092. * @objp: Pointer to the object
  4093. *
  4094. * kmalloc may internally round up allocations and return more memory
  4095. * than requested. ksize() can be used to determine the actual amount of
  4096. * memory allocated. The caller may use this additional memory, even though
  4097. * a smaller amount of memory was initially specified with the kmalloc call.
  4098. * The caller must guarantee that objp points to a valid object previously
  4099. * allocated with either kmalloc() or kmem_cache_alloc(). The object
  4100. * must not be freed during the duration of the call.
  4101. */
  4102. size_t ksize(const void *objp)
  4103. {
  4104. BUG_ON(!objp);
  4105. if (unlikely(objp == ZERO_SIZE_PTR))
  4106. return 0;
  4107. return obj_size(virt_to_cache(objp));
  4108. }
  4109. EXPORT_SYMBOL(ksize);