slab.c 111 KB

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