slab.c 96 KB

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