slab.c 82 KB

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