slab.c 82 KB

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