slab.c 82 KB

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