slab.c 82 KB

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