slab.c 119 KB

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