slab.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116
  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. * kcalloc - allocate memory for an array. The memory is set to zero.
  2264. * @n: number of elements.
  2265. * @size: element size.
  2266. * @flags: the type of memory to allocate.
  2267. */
  2268. void *kcalloc(size_t n, size_t size, unsigned int __nocast flags)
  2269. {
  2270. void *ret = NULL;
  2271. if (n != 0 && size > INT_MAX / n)
  2272. return ret;
  2273. ret = kmalloc(n * size, flags);
  2274. if (ret)
  2275. memset(ret, 0, n * size);
  2276. return ret;
  2277. }
  2278. EXPORT_SYMBOL(kcalloc);
  2279. /**
  2280. * kfree - free previously allocated memory
  2281. * @objp: pointer returned by kmalloc.
  2282. *
  2283. * Don't free memory not originally allocated by kmalloc()
  2284. * or you will run into trouble.
  2285. */
  2286. void kfree(const void *objp)
  2287. {
  2288. kmem_cache_t *c;
  2289. unsigned long flags;
  2290. if (unlikely(!objp))
  2291. return;
  2292. local_irq_save(flags);
  2293. kfree_debugcheck(objp);
  2294. c = GET_PAGE_CACHE(virt_to_page(objp));
  2295. __cache_free(c, (void*)objp);
  2296. local_irq_restore(flags);
  2297. }
  2298. EXPORT_SYMBOL(kfree);
  2299. #ifdef CONFIG_SMP
  2300. /**
  2301. * free_percpu - free previously allocated percpu memory
  2302. * @objp: pointer returned by alloc_percpu.
  2303. *
  2304. * Don't free memory not originally allocated by alloc_percpu()
  2305. * The complemented objp is to check for that.
  2306. */
  2307. void
  2308. free_percpu(const void *objp)
  2309. {
  2310. int i;
  2311. struct percpu_data *p = (struct percpu_data *) (~(unsigned long) objp);
  2312. for (i = 0; i < NR_CPUS; i++) {
  2313. if (!cpu_possible(i))
  2314. continue;
  2315. kfree(p->ptrs[i]);
  2316. }
  2317. kfree(p);
  2318. }
  2319. EXPORT_SYMBOL(free_percpu);
  2320. #endif
  2321. unsigned int kmem_cache_size(kmem_cache_t *cachep)
  2322. {
  2323. return obj_reallen(cachep);
  2324. }
  2325. EXPORT_SYMBOL(kmem_cache_size);
  2326. const char *kmem_cache_name(kmem_cache_t *cachep)
  2327. {
  2328. return cachep->name;
  2329. }
  2330. EXPORT_SYMBOL_GPL(kmem_cache_name);
  2331. struct ccupdate_struct {
  2332. kmem_cache_t *cachep;
  2333. struct array_cache *new[NR_CPUS];
  2334. };
  2335. static void do_ccupdate_local(void *info)
  2336. {
  2337. struct ccupdate_struct *new = (struct ccupdate_struct *)info;
  2338. struct array_cache *old;
  2339. check_irq_off();
  2340. old = ac_data(new->cachep);
  2341. new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
  2342. new->new[smp_processor_id()] = old;
  2343. }
  2344. static int do_tune_cpucache(kmem_cache_t *cachep, int limit, int batchcount,
  2345. int shared)
  2346. {
  2347. struct ccupdate_struct new;
  2348. struct array_cache *new_shared;
  2349. int i;
  2350. memset(&new.new,0,sizeof(new.new));
  2351. for (i = 0; i < NR_CPUS; i++) {
  2352. if (cpu_online(i)) {
  2353. new.new[i] = alloc_arraycache(i, limit, batchcount);
  2354. if (!new.new[i]) {
  2355. for (i--; i >= 0; i--) kfree(new.new[i]);
  2356. return -ENOMEM;
  2357. }
  2358. } else {
  2359. new.new[i] = NULL;
  2360. }
  2361. }
  2362. new.cachep = cachep;
  2363. smp_call_function_all_cpus(do_ccupdate_local, (void *)&new);
  2364. check_irq_on();
  2365. spin_lock_irq(&cachep->spinlock);
  2366. cachep->batchcount = batchcount;
  2367. cachep->limit = limit;
  2368. cachep->free_limit = (1+num_online_cpus())*cachep->batchcount + cachep->num;
  2369. spin_unlock_irq(&cachep->spinlock);
  2370. for (i = 0; i < NR_CPUS; i++) {
  2371. struct array_cache *ccold = new.new[i];
  2372. if (!ccold)
  2373. continue;
  2374. spin_lock_irq(&cachep->spinlock);
  2375. free_block(cachep, ac_entry(ccold), ccold->avail);
  2376. spin_unlock_irq(&cachep->spinlock);
  2377. kfree(ccold);
  2378. }
  2379. new_shared = alloc_arraycache(-1, batchcount*shared, 0xbaadf00d);
  2380. if (new_shared) {
  2381. struct array_cache *old;
  2382. spin_lock_irq(&cachep->spinlock);
  2383. old = cachep->lists.shared;
  2384. cachep->lists.shared = new_shared;
  2385. if (old)
  2386. free_block(cachep, ac_entry(old), old->avail);
  2387. spin_unlock_irq(&cachep->spinlock);
  2388. kfree(old);
  2389. }
  2390. return 0;
  2391. }
  2392. static void enable_cpucache(kmem_cache_t *cachep)
  2393. {
  2394. int err;
  2395. int limit, shared;
  2396. /* The head array serves three purposes:
  2397. * - create a LIFO ordering, i.e. return objects that are cache-warm
  2398. * - reduce the number of spinlock operations.
  2399. * - reduce the number of linked list operations on the slab and
  2400. * bufctl chains: array operations are cheaper.
  2401. * The numbers are guessed, we should auto-tune as described by
  2402. * Bonwick.
  2403. */
  2404. if (cachep->objsize > 131072)
  2405. limit = 1;
  2406. else if (cachep->objsize > PAGE_SIZE)
  2407. limit = 8;
  2408. else if (cachep->objsize > 1024)
  2409. limit = 24;
  2410. else if (cachep->objsize > 256)
  2411. limit = 54;
  2412. else
  2413. limit = 120;
  2414. /* Cpu bound tasks (e.g. network routing) can exhibit cpu bound
  2415. * allocation behaviour: Most allocs on one cpu, most free operations
  2416. * on another cpu. For these cases, an efficient object passing between
  2417. * cpus is necessary. This is provided by a shared array. The array
  2418. * replaces Bonwick's magazine layer.
  2419. * On uniprocessor, it's functionally equivalent (but less efficient)
  2420. * to a larger limit. Thus disabled by default.
  2421. */
  2422. shared = 0;
  2423. #ifdef CONFIG_SMP
  2424. if (cachep->objsize <= PAGE_SIZE)
  2425. shared = 8;
  2426. #endif
  2427. #if DEBUG
  2428. /* With debugging enabled, large batchcount lead to excessively
  2429. * long periods with disabled local interrupts. Limit the
  2430. * batchcount
  2431. */
  2432. if (limit > 32)
  2433. limit = 32;
  2434. #endif
  2435. err = do_tune_cpucache(cachep, limit, (limit+1)/2, shared);
  2436. if (err)
  2437. printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
  2438. cachep->name, -err);
  2439. }
  2440. static void drain_array_locked(kmem_cache_t *cachep,
  2441. struct array_cache *ac, int force)
  2442. {
  2443. int tofree;
  2444. check_spinlock_acquired(cachep);
  2445. if (ac->touched && !force) {
  2446. ac->touched = 0;
  2447. } else if (ac->avail) {
  2448. tofree = force ? ac->avail : (ac->limit+4)/5;
  2449. if (tofree > ac->avail) {
  2450. tofree = (ac->avail+1)/2;
  2451. }
  2452. free_block(cachep, ac_entry(ac), tofree);
  2453. ac->avail -= tofree;
  2454. memmove(&ac_entry(ac)[0], &ac_entry(ac)[tofree],
  2455. sizeof(void*)*ac->avail);
  2456. }
  2457. }
  2458. /**
  2459. * cache_reap - Reclaim memory from caches.
  2460. *
  2461. * Called from workqueue/eventd every few seconds.
  2462. * Purpose:
  2463. * - clear the per-cpu caches for this CPU.
  2464. * - return freeable pages to the main free memory pool.
  2465. *
  2466. * If we cannot acquire the cache chain semaphore then just give up - we'll
  2467. * try again on the next iteration.
  2468. */
  2469. static void cache_reap(void *unused)
  2470. {
  2471. struct list_head *walk;
  2472. if (down_trylock(&cache_chain_sem)) {
  2473. /* Give up. Setup the next iteration. */
  2474. schedule_delayed_work(&__get_cpu_var(reap_work), REAPTIMEOUT_CPUC + smp_processor_id());
  2475. return;
  2476. }
  2477. list_for_each(walk, &cache_chain) {
  2478. kmem_cache_t *searchp;
  2479. struct list_head* p;
  2480. int tofree;
  2481. struct slab *slabp;
  2482. searchp = list_entry(walk, kmem_cache_t, next);
  2483. if (searchp->flags & SLAB_NO_REAP)
  2484. goto next;
  2485. check_irq_on();
  2486. spin_lock_irq(&searchp->spinlock);
  2487. drain_array_locked(searchp, ac_data(searchp), 0);
  2488. if(time_after(searchp->lists.next_reap, jiffies))
  2489. goto next_unlock;
  2490. searchp->lists.next_reap = jiffies + REAPTIMEOUT_LIST3;
  2491. if (searchp->lists.shared)
  2492. drain_array_locked(searchp, searchp->lists.shared, 0);
  2493. if (searchp->lists.free_touched) {
  2494. searchp->lists.free_touched = 0;
  2495. goto next_unlock;
  2496. }
  2497. tofree = (searchp->free_limit+5*searchp->num-1)/(5*searchp->num);
  2498. do {
  2499. p = list3_data(searchp)->slabs_free.next;
  2500. if (p == &(list3_data(searchp)->slabs_free))
  2501. break;
  2502. slabp = list_entry(p, struct slab, list);
  2503. BUG_ON(slabp->inuse);
  2504. list_del(&slabp->list);
  2505. STATS_INC_REAPED(searchp);
  2506. /* Safe to drop the lock. The slab is no longer
  2507. * linked to the cache.
  2508. * searchp cannot disappear, we hold
  2509. * cache_chain_lock
  2510. */
  2511. searchp->lists.free_objects -= searchp->num;
  2512. spin_unlock_irq(&searchp->spinlock);
  2513. slab_destroy(searchp, slabp);
  2514. spin_lock_irq(&searchp->spinlock);
  2515. } while(--tofree > 0);
  2516. next_unlock:
  2517. spin_unlock_irq(&searchp->spinlock);
  2518. next:
  2519. cond_resched();
  2520. }
  2521. check_irq_on();
  2522. up(&cache_chain_sem);
  2523. drain_remote_pages();
  2524. /* Setup the next iteration */
  2525. schedule_delayed_work(&__get_cpu_var(reap_work), REAPTIMEOUT_CPUC + smp_processor_id());
  2526. }
  2527. #ifdef CONFIG_PROC_FS
  2528. static void *s_start(struct seq_file *m, loff_t *pos)
  2529. {
  2530. loff_t n = *pos;
  2531. struct list_head *p;
  2532. down(&cache_chain_sem);
  2533. if (!n) {
  2534. /*
  2535. * Output format version, so at least we can change it
  2536. * without _too_ many complaints.
  2537. */
  2538. #if STATS
  2539. seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
  2540. #else
  2541. seq_puts(m, "slabinfo - version: 2.1\n");
  2542. #endif
  2543. seq_puts(m, "# name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab>");
  2544. seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
  2545. seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
  2546. #if STATS
  2547. seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped>"
  2548. " <error> <maxfreeable> <freelimit> <nodeallocs>");
  2549. seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
  2550. #endif
  2551. seq_putc(m, '\n');
  2552. }
  2553. p = cache_chain.next;
  2554. while (n--) {
  2555. p = p->next;
  2556. if (p == &cache_chain)
  2557. return NULL;
  2558. }
  2559. return list_entry(p, kmem_cache_t, next);
  2560. }
  2561. static void *s_next(struct seq_file *m, void *p, loff_t *pos)
  2562. {
  2563. kmem_cache_t *cachep = p;
  2564. ++*pos;
  2565. return cachep->next.next == &cache_chain ? NULL
  2566. : list_entry(cachep->next.next, kmem_cache_t, next);
  2567. }
  2568. static void s_stop(struct seq_file *m, void *p)
  2569. {
  2570. up(&cache_chain_sem);
  2571. }
  2572. static int s_show(struct seq_file *m, void *p)
  2573. {
  2574. kmem_cache_t *cachep = p;
  2575. struct list_head *q;
  2576. struct slab *slabp;
  2577. unsigned long active_objs;
  2578. unsigned long num_objs;
  2579. unsigned long active_slabs = 0;
  2580. unsigned long num_slabs;
  2581. const char *name;
  2582. char *error = NULL;
  2583. check_irq_on();
  2584. spin_lock_irq(&cachep->spinlock);
  2585. active_objs = 0;
  2586. num_slabs = 0;
  2587. list_for_each(q,&cachep->lists.slabs_full) {
  2588. slabp = list_entry(q, struct slab, list);
  2589. if (slabp->inuse != cachep->num && !error)
  2590. error = "slabs_full accounting error";
  2591. active_objs += cachep->num;
  2592. active_slabs++;
  2593. }
  2594. list_for_each(q,&cachep->lists.slabs_partial) {
  2595. slabp = list_entry(q, struct slab, list);
  2596. if (slabp->inuse == cachep->num && !error)
  2597. error = "slabs_partial inuse accounting error";
  2598. if (!slabp->inuse && !error)
  2599. error = "slabs_partial/inuse accounting error";
  2600. active_objs += slabp->inuse;
  2601. active_slabs++;
  2602. }
  2603. list_for_each(q,&cachep->lists.slabs_free) {
  2604. slabp = list_entry(q, struct slab, list);
  2605. if (slabp->inuse && !error)
  2606. error = "slabs_free/inuse accounting error";
  2607. num_slabs++;
  2608. }
  2609. num_slabs+=active_slabs;
  2610. num_objs = num_slabs*cachep->num;
  2611. if (num_objs - active_objs != cachep->lists.free_objects && !error)
  2612. error = "free_objects accounting error";
  2613. name = cachep->name;
  2614. if (error)
  2615. printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
  2616. seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
  2617. name, active_objs, num_objs, cachep->objsize,
  2618. cachep->num, (1<<cachep->gfporder));
  2619. seq_printf(m, " : tunables %4u %4u %4u",
  2620. cachep->limit, cachep->batchcount,
  2621. cachep->lists.shared->limit/cachep->batchcount);
  2622. seq_printf(m, " : slabdata %6lu %6lu %6u",
  2623. active_slabs, num_slabs, cachep->lists.shared->avail);
  2624. #if STATS
  2625. { /* list3 stats */
  2626. unsigned long high = cachep->high_mark;
  2627. unsigned long allocs = cachep->num_allocations;
  2628. unsigned long grown = cachep->grown;
  2629. unsigned long reaped = cachep->reaped;
  2630. unsigned long errors = cachep->errors;
  2631. unsigned long max_freeable = cachep->max_freeable;
  2632. unsigned long free_limit = cachep->free_limit;
  2633. unsigned long node_allocs = cachep->node_allocs;
  2634. seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu %4lu %4lu %4lu %4lu",
  2635. allocs, high, grown, reaped, errors,
  2636. max_freeable, free_limit, node_allocs);
  2637. }
  2638. /* cpu stats */
  2639. {
  2640. unsigned long allochit = atomic_read(&cachep->allochit);
  2641. unsigned long allocmiss = atomic_read(&cachep->allocmiss);
  2642. unsigned long freehit = atomic_read(&cachep->freehit);
  2643. unsigned long freemiss = atomic_read(&cachep->freemiss);
  2644. seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
  2645. allochit, allocmiss, freehit, freemiss);
  2646. }
  2647. #endif
  2648. seq_putc(m, '\n');
  2649. spin_unlock_irq(&cachep->spinlock);
  2650. return 0;
  2651. }
  2652. /*
  2653. * slabinfo_op - iterator that generates /proc/slabinfo
  2654. *
  2655. * Output layout:
  2656. * cache-name
  2657. * num-active-objs
  2658. * total-objs
  2659. * object size
  2660. * num-active-slabs
  2661. * total-slabs
  2662. * num-pages-per-slab
  2663. * + further values on SMP and with statistics enabled
  2664. */
  2665. struct seq_operations slabinfo_op = {
  2666. .start = s_start,
  2667. .next = s_next,
  2668. .stop = s_stop,
  2669. .show = s_show,
  2670. };
  2671. #define MAX_SLABINFO_WRITE 128
  2672. /**
  2673. * slabinfo_write - Tuning for the slab allocator
  2674. * @file: unused
  2675. * @buffer: user buffer
  2676. * @count: data length
  2677. * @ppos: unused
  2678. */
  2679. ssize_t slabinfo_write(struct file *file, const char __user *buffer,
  2680. size_t count, loff_t *ppos)
  2681. {
  2682. char kbuf[MAX_SLABINFO_WRITE+1], *tmp;
  2683. int limit, batchcount, shared, res;
  2684. struct list_head *p;
  2685. if (count > MAX_SLABINFO_WRITE)
  2686. return -EINVAL;
  2687. if (copy_from_user(&kbuf, buffer, count))
  2688. return -EFAULT;
  2689. kbuf[MAX_SLABINFO_WRITE] = '\0';
  2690. tmp = strchr(kbuf, ' ');
  2691. if (!tmp)
  2692. return -EINVAL;
  2693. *tmp = '\0';
  2694. tmp++;
  2695. if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
  2696. return -EINVAL;
  2697. /* Find the cache in the chain of caches. */
  2698. down(&cache_chain_sem);
  2699. res = -EINVAL;
  2700. list_for_each(p,&cache_chain) {
  2701. kmem_cache_t *cachep = list_entry(p, kmem_cache_t, next);
  2702. if (!strcmp(cachep->name, kbuf)) {
  2703. if (limit < 1 ||
  2704. batchcount < 1 ||
  2705. batchcount > limit ||
  2706. shared < 0) {
  2707. res = -EINVAL;
  2708. } else {
  2709. res = do_tune_cpucache(cachep, limit, batchcount, shared);
  2710. }
  2711. break;
  2712. }
  2713. }
  2714. up(&cache_chain_sem);
  2715. if (res >= 0)
  2716. res = count;
  2717. return res;
  2718. }
  2719. #endif
  2720. unsigned int ksize(const void *objp)
  2721. {
  2722. kmem_cache_t *c;
  2723. unsigned long flags;
  2724. unsigned int size = 0;
  2725. if (likely(objp != NULL)) {
  2726. local_irq_save(flags);
  2727. c = GET_PAGE_CACHE(virt_to_page(objp));
  2728. size = kmem_cache_size(c);
  2729. local_irq_restore(flags);
  2730. }
  2731. return size;
  2732. }
  2733. /*
  2734. * kstrdup - allocate space for and copy an existing string
  2735. *
  2736. * @s: the string to duplicate
  2737. * @gfp: the GFP mask used in the kmalloc() call when allocating memory
  2738. */
  2739. char *kstrdup(const char *s, unsigned int __nocast gfp)
  2740. {
  2741. size_t len;
  2742. char *buf;
  2743. if (!s)
  2744. return NULL;
  2745. len = strlen(s) + 1;
  2746. buf = kmalloc(len, gfp);
  2747. if (buf)
  2748. memcpy(buf, s, len);
  2749. return buf;
  2750. }
  2751. EXPORT_SYMBOL(kstrdup);