slab.c 81 KB

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