slab.c 108 KB

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