blob: d30423f167a2c0a5c4c4d13b0678495fffa86189 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 *
Christoph Lametere498be72005-09-09 13:03:32 -070078 * 15 March 2005. NUMA slab allocator.
79 * Shai Fultheim <shai@scalex86.org>.
80 * Shobhit Dayal <shobhit@calsoftinc.com>
81 * Alok N Kataria <alokk@calsoftinc.com>
82 * Christoph Lameter <christoph@lameter.com>
83 *
84 * Modified the slab allocator to be node aware on NUMA systems.
85 * Each node has its own list of partial, free and full slabs.
86 * All object allocations for a node occur from node specific slab lists.
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 */
88
89#include <linux/config.h>
90#include <linux/slab.h>
91#include <linux/mm.h>
92#include <linux/swap.h>
93#include <linux/cache.h>
94#include <linux/interrupt.h>
95#include <linux/init.h>
96#include <linux/compiler.h>
97#include <linux/seq_file.h>
98#include <linux/notifier.h>
99#include <linux/kallsyms.h>
100#include <linux/cpu.h>
101#include <linux/sysctl.h>
102#include <linux/module.h>
103#include <linux/rcupdate.h>
Paulo Marques543537b2005-06-23 00:09:02 -0700104#include <linux/string.h>
Christoph Lametere498be72005-09-09 13:03:32 -0700105#include <linux/nodemask.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107#include <asm/uaccess.h>
108#include <asm/cacheflush.h>
109#include <asm/tlbflush.h>
110#include <asm/page.h>
111
112/*
113 * DEBUG - 1 for kmem_cache_create() to honour; SLAB_DEBUG_INITIAL,
114 * SLAB_RED_ZONE & SLAB_POISON.
115 * 0 for faster, smaller code (especially in the critical paths).
116 *
117 * STATS - 1 to collect stats for /proc/slabinfo.
118 * 0 for faster, smaller code (especially in the critical paths).
119 *
120 * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
121 */
122
123#ifdef CONFIG_DEBUG_SLAB
124#define DEBUG 1
125#define STATS 1
126#define FORCED_DEBUG 1
127#else
128#define DEBUG 0
129#define STATS 0
130#define FORCED_DEBUG 0
131#endif
132
133
134/* Shouldn't this be in a header file somewhere? */
135#define BYTES_PER_WORD sizeof(void *)
136
137#ifndef cache_line_size
138#define cache_line_size() L1_CACHE_BYTES
139#endif
140
141#ifndef ARCH_KMALLOC_MINALIGN
142/*
143 * Enforce a minimum alignment for the kmalloc caches.
144 * Usually, the kmalloc caches are cache_line_size() aligned, except when
145 * DEBUG and FORCED_DEBUG are enabled, then they are BYTES_PER_WORD aligned.
146 * Some archs want to perform DMA into kmalloc caches and need a guaranteed
147 * alignment larger than BYTES_PER_WORD. ARCH_KMALLOC_MINALIGN allows that.
148 * Note that this flag disables some debug features.
149 */
150#define ARCH_KMALLOC_MINALIGN 0
151#endif
152
153#ifndef ARCH_SLAB_MINALIGN
154/*
155 * Enforce a minimum alignment for all caches.
156 * Intended for archs that get misalignment faults even for BYTES_PER_WORD
157 * aligned buffers. Includes ARCH_KMALLOC_MINALIGN.
158 * If possible: Do not enable this flag for CONFIG_DEBUG_SLAB, it disables
159 * some debug features.
160 */
161#define ARCH_SLAB_MINALIGN 0
162#endif
163
164#ifndef ARCH_KMALLOC_FLAGS
165#define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
166#endif
167
168/* Legal flag mask for kmem_cache_create(). */
169#if DEBUG
170# define CREATE_MASK (SLAB_DEBUG_INITIAL | SLAB_RED_ZONE | \
171 SLAB_POISON | SLAB_HWCACHE_ALIGN | \
172 SLAB_NO_REAP | SLAB_CACHE_DMA | \
173 SLAB_MUST_HWCACHE_ALIGN | SLAB_STORE_USER | \
174 SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
175 SLAB_DESTROY_BY_RCU)
176#else
177# define CREATE_MASK (SLAB_HWCACHE_ALIGN | SLAB_NO_REAP | \
178 SLAB_CACHE_DMA | SLAB_MUST_HWCACHE_ALIGN | \
179 SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
180 SLAB_DESTROY_BY_RCU)
181#endif
182
183/*
184 * kmem_bufctl_t:
185 *
186 * Bufctl's are used for linking objs within a slab
187 * linked offsets.
188 *
189 * This implementation relies on "struct page" for locating the cache &
190 * slab an object belongs to.
191 * This allows the bufctl structure to be small (one int), but limits
192 * the number of objects a slab (not a cache) can contain when off-slab
193 * bufctls are used. The limit is the size of the largest general cache
194 * that does not use off-slab slabs.
195 * For 32bit archs with 4 kB pages, is this 56.
196 * This is not serious, as it is only for large objects, when it is unwise
197 * to have too many per slab.
198 * Note: This limit can be raised by introducing a general cache whose size
199 * is less than 512 (PAGE_SIZE<<3), but greater than 256.
200 */
201
Kyle Moffettfa5b08d2005-09-03 15:55:03 -0700202typedef unsigned int kmem_bufctl_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203#define BUFCTL_END (((kmem_bufctl_t)(~0U))-0)
204#define BUFCTL_FREE (((kmem_bufctl_t)(~0U))-1)
205#define SLAB_LIMIT (((kmem_bufctl_t)(~0U))-2)
206
207/* Max number of objs-per-slab for caches which use off-slab slabs.
208 * Needed to avoid a possible looping condition in cache_grow().
209 */
210static unsigned long offslab_limit;
211
212/*
213 * struct slab
214 *
215 * Manages the objs in a slab. Placed either at the beginning of mem allocated
216 * for a slab, or allocated from an general cache.
217 * Slabs are chained into three list: fully used, partial, fully free slabs.
218 */
219struct slab {
220 struct list_head list;
221 unsigned long colouroff;
222 void *s_mem; /* including colour offset */
223 unsigned int inuse; /* num of objs active in slab */
224 kmem_bufctl_t free;
Christoph Lametere498be72005-09-09 13:03:32 -0700225 unsigned short nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226};
227
228/*
229 * struct slab_rcu
230 *
231 * slab_destroy on a SLAB_DESTROY_BY_RCU cache uses this structure to
232 * arrange for kmem_freepages to be called via RCU. This is useful if
233 * we need to approach a kernel structure obliquely, from its address
234 * obtained without the usual locking. We can lock the structure to
235 * stabilize it and check it's still at the given address, only if we
236 * can be sure that the memory has not been meanwhile reused for some
237 * other kind of object (which our subsystem's lock might corrupt).
238 *
239 * rcu_read_lock before reading the address, then rcu_read_unlock after
240 * taking the spinlock within the structure expected at that address.
241 *
242 * We assume struct slab_rcu can overlay struct slab when destroying.
243 */
244struct slab_rcu {
245 struct rcu_head head;
246 kmem_cache_t *cachep;
247 void *addr;
248};
249
250/*
251 * struct array_cache
252 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * Purpose:
254 * - LIFO ordering, to hand out cache-warm objects from _alloc
255 * - reduce the number of linked list operations
256 * - reduce spinlock operations
257 *
258 * The limit is stored in the per-cpu structure to reduce the data cache
259 * footprint.
260 *
261 */
262struct array_cache {
263 unsigned int avail;
264 unsigned int limit;
265 unsigned int batchcount;
266 unsigned int touched;
Christoph Lametere498be72005-09-09 13:03:32 -0700267 spinlock_t lock;
268 void *entry[0]; /*
269 * Must have this definition in here for the proper
270 * alignment of array_cache. Also simplifies accessing
271 * the entries.
272 * [0] is for gcc 2.95. It should really be [].
273 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274};
275
276/* bootstrap: The caches do not work without cpuarrays anymore,
277 * but the cpuarrays are allocated from the generic caches...
278 */
279#define BOOT_CPUCACHE_ENTRIES 1
280struct arraycache_init {
281 struct array_cache cache;
282 void * entries[BOOT_CPUCACHE_ENTRIES];
283};
284
285/*
Christoph Lametere498be72005-09-09 13:03:32 -0700286 * The slab lists for all objects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 */
288struct kmem_list3 {
289 struct list_head slabs_partial; /* partial list first, better asm code */
290 struct list_head slabs_full;
291 struct list_head slabs_free;
292 unsigned long free_objects;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 unsigned long next_reap;
Christoph Lametere498be72005-09-09 13:03:32 -0700294 int free_touched;
295 unsigned int free_limit;
296 spinlock_t list_lock;
297 struct array_cache *shared; /* shared per node */
298 struct array_cache **alien; /* on other nodes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299};
300
Christoph Lametere498be72005-09-09 13:03:32 -0700301/*
302 * Need this for bootstrapping a per node allocator.
303 */
304#define NUM_INIT_LISTS (2 * MAX_NUMNODES + 1)
305struct kmem_list3 __initdata initkmem_list3[NUM_INIT_LISTS];
306#define CACHE_CACHE 0
307#define SIZE_AC 1
308#define SIZE_L3 (1 + MAX_NUMNODES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Christoph Lametere498be72005-09-09 13:03:32 -0700310/*
Ivan Kokshaysky7243cc02005-09-22 21:43:58 -0700311 * This function must be completely optimized away if
Christoph Lametere498be72005-09-09 13:03:32 -0700312 * a constant is passed to it. Mostly the same as
313 * what is in linux/slab.h except it returns an
314 * index.
315 */
Ivan Kokshaysky7243cc02005-09-22 21:43:58 -0700316static __always_inline int index_of(const size_t size)
Christoph Lametere498be72005-09-09 13:03:32 -0700317{
318 if (__builtin_constant_p(size)) {
319 int i = 0;
320
321#define CACHE(x) \
322 if (size <=x) \
323 return i; \
324 else \
325 i++;
326#include "linux/kmalloc_sizes.h"
327#undef CACHE
328 {
329 extern void __bad_size(void);
330 __bad_size();
331 }
Ivan Kokshaysky7243cc02005-09-22 21:43:58 -0700332 } else
333 BUG();
Christoph Lametere498be72005-09-09 13:03:32 -0700334 return 0;
335}
336
337#define INDEX_AC index_of(sizeof(struct arraycache_init))
338#define INDEX_L3 index_of(sizeof(struct kmem_list3))
339
340static inline void kmem_list3_init(struct kmem_list3 *parent)
341{
342 INIT_LIST_HEAD(&parent->slabs_full);
343 INIT_LIST_HEAD(&parent->slabs_partial);
344 INIT_LIST_HEAD(&parent->slabs_free);
345 parent->shared = NULL;
346 parent->alien = NULL;
347 spin_lock_init(&parent->list_lock);
348 parent->free_objects = 0;
349 parent->free_touched = 0;
350}
351
352#define MAKE_LIST(cachep, listp, slab, nodeid) \
353 do { \
354 INIT_LIST_HEAD(listp); \
355 list_splice(&(cachep->nodelists[nodeid]->slab), listp); \
356 } while (0)
357
358#define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
359 do { \
360 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
361 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
362 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
363 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365/*
366 * kmem_cache_t
367 *
368 * manages a cache.
369 */
370
371struct kmem_cache_s {
372/* 1) per-cpu data, touched during every alloc/free */
373 struct array_cache *array[NR_CPUS];
374 unsigned int batchcount;
375 unsigned int limit;
Christoph Lametere498be72005-09-09 13:03:32 -0700376 unsigned int shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 unsigned int objsize;
Christoph Lametere498be72005-09-09 13:03:32 -0700378/* 2) touched by every alloc & free from the backend */
379 struct kmem_list3 *nodelists[MAX_NUMNODES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 unsigned int flags; /* constant flags */
381 unsigned int num; /* # of objs per slab */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 spinlock_t spinlock;
383
384/* 3) cache_grow/shrink */
385 /* order of pgs per slab (2^n) */
386 unsigned int gfporder;
387
388 /* force GFP flags, e.g. GFP_DMA */
Al Viro6daa0e22005-10-21 03:18:50 -0400389 gfp_t gfpflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 size_t colour; /* cache colouring range */
392 unsigned int colour_off; /* colour offset */
393 unsigned int colour_next; /* cache colouring */
394 kmem_cache_t *slabp_cache;
395 unsigned int slab_size;
396 unsigned int dflags; /* dynamic flags */
397
398 /* constructor func */
399 void (*ctor)(void *, kmem_cache_t *, unsigned long);
400
401 /* de-constructor func */
402 void (*dtor)(void *, kmem_cache_t *, unsigned long);
403
404/* 4) cache creation/removal */
405 const char *name;
406 struct list_head next;
407
408/* 5) statistics */
409#if STATS
410 unsigned long num_active;
411 unsigned long num_allocations;
412 unsigned long high_mark;
413 unsigned long grown;
414 unsigned long reaped;
415 unsigned long errors;
416 unsigned long max_freeable;
417 unsigned long node_allocs;
Christoph Lametere498be72005-09-09 13:03:32 -0700418 unsigned long node_frees;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 atomic_t allochit;
420 atomic_t allocmiss;
421 atomic_t freehit;
422 atomic_t freemiss;
423#endif
424#if DEBUG
425 int dbghead;
426 int reallen;
427#endif
428};
429
430#define CFLGS_OFF_SLAB (0x80000000UL)
431#define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
432
433#define BATCHREFILL_LIMIT 16
434/* Optimization question: fewer reaps means less
435 * probability for unnessary cpucache drain/refill cycles.
436 *
437 * OTHO the cpuarrays can contain lots of objects,
438 * which could lock up otherwise freeable slabs.
439 */
440#define REAPTIMEOUT_CPUC (2*HZ)
441#define REAPTIMEOUT_LIST3 (4*HZ)
442
443#if STATS
444#define STATS_INC_ACTIVE(x) ((x)->num_active++)
445#define STATS_DEC_ACTIVE(x) ((x)->num_active--)
446#define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
447#define STATS_INC_GROWN(x) ((x)->grown++)
448#define STATS_INC_REAPED(x) ((x)->reaped++)
449#define STATS_SET_HIGH(x) do { if ((x)->num_active > (x)->high_mark) \
450 (x)->high_mark = (x)->num_active; \
451 } while (0)
452#define STATS_INC_ERR(x) ((x)->errors++)
453#define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
Christoph Lametere498be72005-09-09 13:03:32 -0700454#define STATS_INC_NODEFREES(x) ((x)->node_frees++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455#define STATS_SET_FREEABLE(x, i) \
456 do { if ((x)->max_freeable < i) \
457 (x)->max_freeable = i; \
458 } while (0)
459
460#define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
461#define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
462#define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
463#define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
464#else
465#define STATS_INC_ACTIVE(x) do { } while (0)
466#define STATS_DEC_ACTIVE(x) do { } while (0)
467#define STATS_INC_ALLOCED(x) do { } while (0)
468#define STATS_INC_GROWN(x) do { } while (0)
469#define STATS_INC_REAPED(x) do { } while (0)
470#define STATS_SET_HIGH(x) do { } while (0)
471#define STATS_INC_ERR(x) do { } while (0)
472#define STATS_INC_NODEALLOCS(x) do { } while (0)
Christoph Lametere498be72005-09-09 13:03:32 -0700473#define STATS_INC_NODEFREES(x) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474#define STATS_SET_FREEABLE(x, i) \
475 do { } while (0)
476
477#define STATS_INC_ALLOCHIT(x) do { } while (0)
478#define STATS_INC_ALLOCMISS(x) do { } while (0)
479#define STATS_INC_FREEHIT(x) do { } while (0)
480#define STATS_INC_FREEMISS(x) do { } while (0)
481#endif
482
483#if DEBUG
484/* Magic nums for obj red zoning.
485 * Placed in the first word before and the first word after an obj.
486 */
487#define RED_INACTIVE 0x5A2CF071UL /* when obj is inactive */
488#define RED_ACTIVE 0x170FC2A5UL /* when obj is active */
489
490/* ...and for poisoning */
491#define POISON_INUSE 0x5a /* for use-uninitialised poisoning */
492#define POISON_FREE 0x6b /* for use-after-free poisoning */
493#define POISON_END 0xa5 /* end-byte of poisoning */
494
495/* memory layout of objects:
496 * 0 : objp
497 * 0 .. cachep->dbghead - BYTES_PER_WORD - 1: padding. This ensures that
498 * the end of an object is aligned with the end of the real
499 * allocation. Catches writes behind the end of the allocation.
500 * cachep->dbghead - BYTES_PER_WORD .. cachep->dbghead - 1:
501 * redzone word.
502 * cachep->dbghead: The real object.
503 * cachep->objsize - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
504 * cachep->objsize - 1* BYTES_PER_WORD: last caller address [BYTES_PER_WORD long]
505 */
506static int obj_dbghead(kmem_cache_t *cachep)
507{
508 return cachep->dbghead;
509}
510
511static int obj_reallen(kmem_cache_t *cachep)
512{
513 return cachep->reallen;
514}
515
516static unsigned long *dbg_redzone1(kmem_cache_t *cachep, void *objp)
517{
518 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
519 return (unsigned long*) (objp+obj_dbghead(cachep)-BYTES_PER_WORD);
520}
521
522static unsigned long *dbg_redzone2(kmem_cache_t *cachep, void *objp)
523{
524 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
525 if (cachep->flags & SLAB_STORE_USER)
526 return (unsigned long*) (objp+cachep->objsize-2*BYTES_PER_WORD);
527 return (unsigned long*) (objp+cachep->objsize-BYTES_PER_WORD);
528}
529
530static void **dbg_userword(kmem_cache_t *cachep, void *objp)
531{
532 BUG_ON(!(cachep->flags & SLAB_STORE_USER));
533 return (void**)(objp+cachep->objsize-BYTES_PER_WORD);
534}
535
536#else
537
538#define obj_dbghead(x) 0
539#define obj_reallen(cachep) (cachep->objsize)
540#define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long *)NULL;})
541#define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long *)NULL;})
542#define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
543
544#endif
545
546/*
547 * Maximum size of an obj (in 2^order pages)
548 * and absolute limit for the gfp order.
549 */
550#if defined(CONFIG_LARGE_ALLOCS)
551#define MAX_OBJ_ORDER 13 /* up to 32Mb */
552#define MAX_GFP_ORDER 13 /* up to 32Mb */
553#elif defined(CONFIG_MMU)
554#define MAX_OBJ_ORDER 5 /* 32 pages */
555#define MAX_GFP_ORDER 5 /* 32 pages */
556#else
557#define MAX_OBJ_ORDER 8 /* up to 1Mb */
558#define MAX_GFP_ORDER 8 /* up to 1Mb */
559#endif
560
561/*
562 * Do not go above this order unless 0 objects fit into the slab.
563 */
564#define BREAK_GFP_ORDER_HI 1
565#define BREAK_GFP_ORDER_LO 0
566static int slab_break_gfp_order = BREAK_GFP_ORDER_LO;
567
568/* Macros for storing/retrieving the cachep and or slab from the
569 * global 'mem_map'. These are used to find the slab an obj belongs to.
570 * With kfree(), these are used to find the cache which an obj belongs to.
571 */
572#define SET_PAGE_CACHE(pg,x) ((pg)->lru.next = (struct list_head *)(x))
573#define GET_PAGE_CACHE(pg) ((kmem_cache_t *)(pg)->lru.next)
574#define SET_PAGE_SLAB(pg,x) ((pg)->lru.prev = (struct list_head *)(x))
575#define GET_PAGE_SLAB(pg) ((struct slab *)(pg)->lru.prev)
576
577/* These are the default caches for kmalloc. Custom caches can have other sizes. */
578struct cache_sizes malloc_sizes[] = {
579#define CACHE(x) { .cs_size = (x) },
580#include <linux/kmalloc_sizes.h>
581 CACHE(ULONG_MAX)
582#undef CACHE
583};
584EXPORT_SYMBOL(malloc_sizes);
585
586/* Must match cache_sizes above. Out of line to keep cache footprint low. */
587struct cache_names {
588 char *name;
589 char *name_dma;
590};
591
592static struct cache_names __initdata cache_names[] = {
593#define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },
594#include <linux/kmalloc_sizes.h>
595 { NULL, }
596#undef CACHE
597};
598
599static struct arraycache_init initarray_cache __initdata =
600 { { 0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
601static struct arraycache_init initarray_generic =
602 { { 0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
603
604/* internal cache of cache description objs */
605static kmem_cache_t cache_cache = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 .batchcount = 1,
607 .limit = BOOT_CPUCACHE_ENTRIES,
Christoph Lametere498be72005-09-09 13:03:32 -0700608 .shared = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 .objsize = sizeof(kmem_cache_t),
610 .flags = SLAB_NO_REAP,
611 .spinlock = SPIN_LOCK_UNLOCKED,
612 .name = "kmem_cache",
613#if DEBUG
614 .reallen = sizeof(kmem_cache_t),
615#endif
616};
617
618/* Guard access to the cache-chain. */
619static struct semaphore cache_chain_sem;
620static struct list_head cache_chain;
621
622/*
623 * vm_enough_memory() looks at this to determine how many
624 * slab-allocated pages are possibly freeable under pressure
625 *
626 * SLAB_RECLAIM_ACCOUNT turns this on per-slab
627 */
628atomic_t slab_reclaim_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629
630/*
631 * chicken and egg problem: delay the per-cpu array allocation
632 * until the general caches are up.
633 */
634static enum {
635 NONE,
Christoph Lametere498be72005-09-09 13:03:32 -0700636 PARTIAL_AC,
637 PARTIAL_L3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 FULL
639} g_cpucache_up;
640
641static DEFINE_PER_CPU(struct work_struct, reap_work);
642
Christoph Lameterff694162005-09-22 21:44:02 -0700643static void free_block(kmem_cache_t* cachep, void** objpp, int len, int node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644static void enable_cpucache (kmem_cache_t *cachep);
645static void cache_reap (void *unused);
Christoph Lametere498be72005-09-09 13:03:32 -0700646static int __node_shrink(kmem_cache_t *cachep, int node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648static inline struct array_cache *ac_data(kmem_cache_t *cachep)
649{
650 return cachep->array[smp_processor_id()];
651}
652
Al Virodd0fc662005-10-07 07:46:04 +0100653static inline kmem_cache_t *__find_general_cachep(size_t size, gfp_t gfpflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654{
655 struct cache_sizes *csizep = malloc_sizes;
656
657#if DEBUG
658 /* This happens if someone tries to call
659 * kmem_cache_create(), or __kmalloc(), before
660 * the generic caches are initialized.
661 */
Alok Katariac7e43c72005-09-14 12:17:53 -0700662 BUG_ON(malloc_sizes[INDEX_AC].cs_cachep == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663#endif
664 while (size > csizep->cs_size)
665 csizep++;
666
667 /*
Martin Hicks0abf40c2005-09-03 15:54:54 -0700668 * Really subtle: The last entry with cs->cs_size==ULONG_MAX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 * has cs_{dma,}cachep==NULL. Thus no special case
670 * for large kmalloc calls required.
671 */
672 if (unlikely(gfpflags & GFP_DMA))
673 return csizep->cs_dmacachep;
674 return csizep->cs_cachep;
675}
676
Al Virodd0fc662005-10-07 07:46:04 +0100677kmem_cache_t *kmem_find_general_cachep(size_t size, gfp_t gfpflags)
Manfred Spraul97e2bde2005-05-01 08:58:38 -0700678{
679 return __find_general_cachep(size, gfpflags);
680}
681EXPORT_SYMBOL(kmem_find_general_cachep);
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683/* Cal the num objs, wastage, and bytes left over for a given slab size. */
684static void cache_estimate(unsigned long gfporder, size_t size, size_t align,
685 int flags, size_t *left_over, unsigned int *num)
686{
687 int i;
688 size_t wastage = PAGE_SIZE<<gfporder;
689 size_t extra = 0;
690 size_t base = 0;
691
692 if (!(flags & CFLGS_OFF_SLAB)) {
693 base = sizeof(struct slab);
694 extra = sizeof(kmem_bufctl_t);
695 }
696 i = 0;
697 while (i*size + ALIGN(base+i*extra, align) <= wastage)
698 i++;
699 if (i > 0)
700 i--;
701
702 if (i > SLAB_LIMIT)
703 i = SLAB_LIMIT;
704
705 *num = i;
706 wastage -= i*size;
707 wastage -= ALIGN(base+i*extra, align);
708 *left_over = wastage;
709}
710
711#define slab_error(cachep, msg) __slab_error(__FUNCTION__, cachep, msg)
712
713static void __slab_error(const char *function, kmem_cache_t *cachep, char *msg)
714{
715 printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
716 function, cachep->name, msg);
717 dump_stack();
718}
719
720/*
721 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
722 * via the workqueue/eventd.
723 * Add the CPU number into the expiration time to minimize the possibility of
724 * the CPUs getting into lockstep and contending for the global cache chain
725 * lock.
726 */
727static void __devinit start_cpu_timer(int cpu)
728{
729 struct work_struct *reap_work = &per_cpu(reap_work, cpu);
730
731 /*
732 * When this gets called from do_initcalls via cpucache_init(),
733 * init_workqueues() has already run, so keventd will be setup
734 * at that time.
735 */
736 if (keventd_up() && reap_work->func == NULL) {
737 INIT_WORK(reap_work, cache_reap, NULL);
738 schedule_delayed_work_on(cpu, reap_work, HZ + 3 * cpu);
739 }
740}
741
Christoph Lametere498be72005-09-09 13:03:32 -0700742static struct array_cache *alloc_arraycache(int node, int entries,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 int batchcount)
744{
745 int memsize = sizeof(void*)*entries+sizeof(struct array_cache);
746 struct array_cache *nc = NULL;
747
Christoph Lametere498be72005-09-09 13:03:32 -0700748 nc = kmalloc_node(memsize, GFP_KERNEL, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (nc) {
750 nc->avail = 0;
751 nc->limit = entries;
752 nc->batchcount = batchcount;
753 nc->touched = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700754 spin_lock_init(&nc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756 return nc;
757}
758
Christoph Lametere498be72005-09-09 13:03:32 -0700759#ifdef CONFIG_NUMA
760static inline struct array_cache **alloc_alien_cache(int node, int limit)
761{
762 struct array_cache **ac_ptr;
763 int memsize = sizeof(void*)*MAX_NUMNODES;
764 int i;
765
766 if (limit > 1)
767 limit = 12;
768 ac_ptr = kmalloc_node(memsize, GFP_KERNEL, node);
769 if (ac_ptr) {
770 for_each_node(i) {
771 if (i == node || !node_online(i)) {
772 ac_ptr[i] = NULL;
773 continue;
774 }
775 ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d);
776 if (!ac_ptr[i]) {
777 for (i--; i <=0; i--)
778 kfree(ac_ptr[i]);
779 kfree(ac_ptr);
780 return NULL;
781 }
782 }
783 }
784 return ac_ptr;
785}
786
787static inline void free_alien_cache(struct array_cache **ac_ptr)
788{
789 int i;
790
791 if (!ac_ptr)
792 return;
793
794 for_each_node(i)
795 kfree(ac_ptr[i]);
796
797 kfree(ac_ptr);
798}
799
800static inline void __drain_alien_cache(kmem_cache_t *cachep, struct array_cache *ac, int node)
801{
802 struct kmem_list3 *rl3 = cachep->nodelists[node];
803
804 if (ac->avail) {
805 spin_lock(&rl3->list_lock);
Christoph Lameterff694162005-09-22 21:44:02 -0700806 free_block(cachep, ac->entry, ac->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -0700807 ac->avail = 0;
808 spin_unlock(&rl3->list_lock);
809 }
810}
811
812static void drain_alien_cache(kmem_cache_t *cachep, struct kmem_list3 *l3)
813{
814 int i=0;
815 struct array_cache *ac;
816 unsigned long flags;
817
818 for_each_online_node(i) {
819 ac = l3->alien[i];
820 if (ac) {
821 spin_lock_irqsave(&ac->lock, flags);
822 __drain_alien_cache(cachep, ac, i);
823 spin_unlock_irqrestore(&ac->lock, flags);
824 }
825 }
826}
827#else
828#define alloc_alien_cache(node, limit) do { } while (0)
829#define free_alien_cache(ac_ptr) do { } while (0)
830#define drain_alien_cache(cachep, l3) do { } while (0)
831#endif
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833static int __devinit cpuup_callback(struct notifier_block *nfb,
834 unsigned long action, void *hcpu)
835{
836 long cpu = (long)hcpu;
837 kmem_cache_t* cachep;
Christoph Lametere498be72005-09-09 13:03:32 -0700838 struct kmem_list3 *l3 = NULL;
839 int node = cpu_to_node(cpu);
840 int memsize = sizeof(struct kmem_list3);
841 struct array_cache *nc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 switch (action) {
844 case CPU_UP_PREPARE:
845 down(&cache_chain_sem);
Christoph Lametere498be72005-09-09 13:03:32 -0700846 /* we need to do this right in the beginning since
847 * alloc_arraycache's are going to use this list.
848 * kmalloc_node allows us to add the slab to the right
849 * kmem_list3 and not this cpu's kmem_list3
850 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Christoph Lametere498be72005-09-09 13:03:32 -0700852 list_for_each_entry(cachep, &cache_chain, next) {
853 /* setup the size64 kmemlist for cpu before we can
854 * begin anything. Make sure some other cpu on this
855 * node has not already allocated this
856 */
857 if (!cachep->nodelists[node]) {
858 if (!(l3 = kmalloc_node(memsize,
859 GFP_KERNEL, node)))
860 goto bad;
861 kmem_list3_init(l3);
862 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
863 ((unsigned long)cachep)%REAPTIMEOUT_LIST3;
864
865 cachep->nodelists[node] = l3;
866 }
867
868 spin_lock_irq(&cachep->nodelists[node]->list_lock);
869 cachep->nodelists[node]->free_limit =
870 (1 + nr_cpus_node(node)) *
871 cachep->batchcount + cachep->num;
872 spin_unlock_irq(&cachep->nodelists[node]->list_lock);
873 }
874
875 /* Now we can go ahead with allocating the shared array's
876 & array cache's */
877 list_for_each_entry(cachep, &cache_chain, next) {
878 nc = alloc_arraycache(node, cachep->limit,
879 cachep->batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (!nc)
881 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 cachep->array[cpu] = nc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
Christoph Lametere498be72005-09-09 13:03:32 -0700884 l3 = cachep->nodelists[node];
885 BUG_ON(!l3);
886 if (!l3->shared) {
887 if (!(nc = alloc_arraycache(node,
888 cachep->shared*cachep->batchcount,
889 0xbaadf00d)))
890 goto bad;
891
892 /* we are serialised from CPU_DEAD or
893 CPU_UP_CANCELLED by the cpucontrol lock */
894 l3->shared = nc;
895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 }
897 up(&cache_chain_sem);
898 break;
899 case CPU_ONLINE:
900 start_cpu_timer(cpu);
901 break;
902#ifdef CONFIG_HOTPLUG_CPU
903 case CPU_DEAD:
904 /* fall thru */
905 case CPU_UP_CANCELED:
906 down(&cache_chain_sem);
907
908 list_for_each_entry(cachep, &cache_chain, next) {
909 struct array_cache *nc;
Christoph Lametere498be72005-09-09 13:03:32 -0700910 cpumask_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911
Christoph Lametere498be72005-09-09 13:03:32 -0700912 mask = node_to_cpumask(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 spin_lock_irq(&cachep->spinlock);
914 /* cpu is dead; no one can alloc from it. */
915 nc = cachep->array[cpu];
916 cachep->array[cpu] = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -0700917 l3 = cachep->nodelists[node];
918
919 if (!l3)
920 goto unlock_cache;
921
922 spin_lock(&l3->list_lock);
923
924 /* Free limit for this kmem_list3 */
925 l3->free_limit -= cachep->batchcount;
926 if (nc)
Christoph Lameterff694162005-09-22 21:44:02 -0700927 free_block(cachep, nc->entry, nc->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -0700928
929 if (!cpus_empty(mask)) {
930 spin_unlock(&l3->list_lock);
931 goto unlock_cache;
932 }
933
934 if (l3->shared) {
935 free_block(cachep, l3->shared->entry,
Christoph Lameterff694162005-09-22 21:44:02 -0700936 l3->shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -0700937 kfree(l3->shared);
938 l3->shared = NULL;
939 }
940 if (l3->alien) {
941 drain_alien_cache(cachep, l3);
942 free_alien_cache(l3->alien);
943 l3->alien = NULL;
944 }
945
946 /* free slabs belonging to this node */
947 if (__node_shrink(cachep, node)) {
948 cachep->nodelists[node] = NULL;
949 spin_unlock(&l3->list_lock);
950 kfree(l3);
951 } else {
952 spin_unlock(&l3->list_lock);
953 }
954unlock_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 spin_unlock_irq(&cachep->spinlock);
956 kfree(nc);
957 }
958 up(&cache_chain_sem);
959 break;
960#endif
961 }
962 return NOTIFY_OK;
963bad:
964 up(&cache_chain_sem);
965 return NOTIFY_BAD;
966}
967
968static struct notifier_block cpucache_notifier = { &cpuup_callback, NULL, 0 };
969
Christoph Lametere498be72005-09-09 13:03:32 -0700970/*
971 * swap the static kmem_list3 with kmalloced memory
972 */
973static void init_list(kmem_cache_t *cachep, struct kmem_list3 *list,
974 int nodeid)
975{
976 struct kmem_list3 *ptr;
977
978 BUG_ON(cachep->nodelists[nodeid] != list);
979 ptr = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, nodeid);
980 BUG_ON(!ptr);
981
982 local_irq_disable();
983 memcpy(ptr, list, sizeof(struct kmem_list3));
984 MAKE_ALL_LISTS(cachep, ptr, nodeid);
985 cachep->nodelists[nodeid] = ptr;
986 local_irq_enable();
987}
988
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989/* Initialisation.
990 * Called after the gfp() functions have been enabled, and before smp_init().
991 */
992void __init kmem_cache_init(void)
993{
994 size_t left_over;
995 struct cache_sizes *sizes;
996 struct cache_names *names;
Christoph Lametere498be72005-09-09 13:03:32 -0700997 int i;
998
999 for (i = 0; i < NUM_INIT_LISTS; i++) {
1000 kmem_list3_init(&initkmem_list3[i]);
1001 if (i < MAX_NUMNODES)
1002 cache_cache.nodelists[i] = NULL;
1003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
1005 /*
1006 * Fragmentation resistance on low memory - only use bigger
1007 * page orders on machines with more than 32MB of memory.
1008 */
1009 if (num_physpages > (32 << 20) >> PAGE_SHIFT)
1010 slab_break_gfp_order = BREAK_GFP_ORDER_HI;
1011
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 /* Bootstrap is tricky, because several objects are allocated
1013 * from caches that do not exist yet:
1014 * 1) initialize the cache_cache cache: it contains the kmem_cache_t
1015 * structures of all caches, except cache_cache itself: cache_cache
1016 * is statically allocated.
Christoph Lametere498be72005-09-09 13:03:32 -07001017 * Initially an __init data area is used for the head array and the
1018 * kmem_list3 structures, it's replaced with a kmalloc allocated
1019 * array at the end of the bootstrap.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 * 2) Create the first kmalloc cache.
Christoph Lametere498be72005-09-09 13:03:32 -07001021 * The kmem_cache_t for the new cache is allocated normally.
1022 * An __init data area is used for the head array.
1023 * 3) Create the remaining kmalloc caches, with minimally sized
1024 * head arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 * 4) Replace the __init data head arrays for cache_cache and the first
1026 * kmalloc cache with kmalloc allocated arrays.
Christoph Lametere498be72005-09-09 13:03:32 -07001027 * 5) Replace the __init data for kmem_list3 for cache_cache and
1028 * the other cache's with kmalloc allocated memory.
1029 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 */
1031
1032 /* 1) create the cache_cache */
1033 init_MUTEX(&cache_chain_sem);
1034 INIT_LIST_HEAD(&cache_chain);
1035 list_add(&cache_cache.next, &cache_chain);
1036 cache_cache.colour_off = cache_line_size();
1037 cache_cache.array[smp_processor_id()] = &initarray_cache.cache;
Christoph Lametere498be72005-09-09 13:03:32 -07001038 cache_cache.nodelists[numa_node_id()] = &initkmem_list3[CACHE_CACHE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 cache_cache.objsize = ALIGN(cache_cache.objsize, cache_line_size());
1041
1042 cache_estimate(0, cache_cache.objsize, cache_line_size(), 0,
1043 &left_over, &cache_cache.num);
1044 if (!cache_cache.num)
1045 BUG();
1046
1047 cache_cache.colour = left_over/cache_cache.colour_off;
1048 cache_cache.colour_next = 0;
1049 cache_cache.slab_size = ALIGN(cache_cache.num*sizeof(kmem_bufctl_t) +
1050 sizeof(struct slab), cache_line_size());
1051
1052 /* 2+3) create the kmalloc caches */
1053 sizes = malloc_sizes;
1054 names = cache_names;
1055
Christoph Lametere498be72005-09-09 13:03:32 -07001056 /* Initialize the caches that provide memory for the array cache
1057 * and the kmem_list3 structures first.
1058 * Without this, further allocations will bug
1059 */
1060
1061 sizes[INDEX_AC].cs_cachep = kmem_cache_create(names[INDEX_AC].name,
1062 sizes[INDEX_AC].cs_size, ARCH_KMALLOC_MINALIGN,
1063 (ARCH_KMALLOC_FLAGS | SLAB_PANIC), NULL, NULL);
1064
1065 if (INDEX_AC != INDEX_L3)
1066 sizes[INDEX_L3].cs_cachep =
1067 kmem_cache_create(names[INDEX_L3].name,
1068 sizes[INDEX_L3].cs_size, ARCH_KMALLOC_MINALIGN,
1069 (ARCH_KMALLOC_FLAGS | SLAB_PANIC), NULL, NULL);
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 while (sizes->cs_size != ULONG_MAX) {
Christoph Lametere498be72005-09-09 13:03:32 -07001072 /*
1073 * For performance, all the general caches are L1 aligned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 * This should be particularly beneficial on SMP boxes, as it
1075 * eliminates "false sharing".
1076 * Note for systems short on memory removing the alignment will
Christoph Lametere498be72005-09-09 13:03:32 -07001077 * allow tighter packing of the smaller caches.
1078 */
1079 if(!sizes->cs_cachep)
1080 sizes->cs_cachep = kmem_cache_create(names->name,
1081 sizes->cs_size, ARCH_KMALLOC_MINALIGN,
1082 (ARCH_KMALLOC_FLAGS | SLAB_PANIC), NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 /* Inc off-slab bufctl limit until the ceiling is hit. */
1085 if (!(OFF_SLAB(sizes->cs_cachep))) {
1086 offslab_limit = sizes->cs_size-sizeof(struct slab);
1087 offslab_limit /= sizeof(kmem_bufctl_t);
1088 }
1089
1090 sizes->cs_dmacachep = kmem_cache_create(names->name_dma,
1091 sizes->cs_size, ARCH_KMALLOC_MINALIGN,
1092 (ARCH_KMALLOC_FLAGS | SLAB_CACHE_DMA | SLAB_PANIC),
1093 NULL, NULL);
1094
1095 sizes++;
1096 names++;
1097 }
1098 /* 4) Replace the bootstrap head arrays */
1099 {
1100 void * ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001101
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
Christoph Lametere498be72005-09-09 13:03:32 -07001103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 local_irq_disable();
1105 BUG_ON(ac_data(&cache_cache) != &initarray_cache.cache);
Christoph Lametere498be72005-09-09 13:03:32 -07001106 memcpy(ptr, ac_data(&cache_cache),
1107 sizeof(struct arraycache_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 cache_cache.array[smp_processor_id()] = ptr;
1109 local_irq_enable();
Christoph Lametere498be72005-09-09 13:03:32 -07001110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
Christoph Lametere498be72005-09-09 13:03:32 -07001112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 local_irq_disable();
Christoph Lametere498be72005-09-09 13:03:32 -07001114 BUG_ON(ac_data(malloc_sizes[INDEX_AC].cs_cachep)
1115 != &initarray_generic.cache);
1116 memcpy(ptr, ac_data(malloc_sizes[INDEX_AC].cs_cachep),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 sizeof(struct arraycache_init));
Christoph Lametere498be72005-09-09 13:03:32 -07001118 malloc_sizes[INDEX_AC].cs_cachep->array[smp_processor_id()] =
1119 ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 local_irq_enable();
1121 }
Christoph Lametere498be72005-09-09 13:03:32 -07001122 /* 5) Replace the bootstrap kmem_list3's */
1123 {
1124 int node;
1125 /* Replace the static kmem_list3 structures for the boot cpu */
1126 init_list(&cache_cache, &initkmem_list3[CACHE_CACHE],
1127 numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Christoph Lametere498be72005-09-09 13:03:32 -07001129 for_each_online_node(node) {
1130 init_list(malloc_sizes[INDEX_AC].cs_cachep,
1131 &initkmem_list3[SIZE_AC+node], node);
1132
1133 if (INDEX_AC != INDEX_L3) {
1134 init_list(malloc_sizes[INDEX_L3].cs_cachep,
1135 &initkmem_list3[SIZE_L3+node],
1136 node);
1137 }
1138 }
1139 }
1140
1141 /* 6) resize the head arrays to their final sizes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 {
1143 kmem_cache_t *cachep;
1144 down(&cache_chain_sem);
1145 list_for_each_entry(cachep, &cache_chain, next)
1146 enable_cpucache(cachep);
1147 up(&cache_chain_sem);
1148 }
1149
1150 /* Done! */
1151 g_cpucache_up = FULL;
1152
1153 /* Register a cpu startup notifier callback
1154 * that initializes ac_data for all new cpus
1155 */
1156 register_cpu_notifier(&cpucache_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
1158 /* The reap timers are started later, with a module init call:
1159 * That part of the kernel is not yet operational.
1160 */
1161}
1162
1163static int __init cpucache_init(void)
1164{
1165 int cpu;
1166
1167 /*
1168 * Register the timers that return unneeded
1169 * pages to gfp.
1170 */
Christoph Lametere498be72005-09-09 13:03:32 -07001171 for_each_online_cpu(cpu)
1172 start_cpu_timer(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
1174 return 0;
1175}
1176
1177__initcall(cpucache_init);
1178
1179/*
1180 * Interface to system's page allocator. No need to hold the cache-lock.
1181 *
1182 * If we requested dmaable memory, we will get it. Even if we
1183 * did not request dmaable memory, we might get it, but that
1184 * would be relatively rare and ignorable.
1185 */
Al Virodd0fc662005-10-07 07:46:04 +01001186static void *kmem_getpages(kmem_cache_t *cachep, gfp_t flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187{
1188 struct page *page;
1189 void *addr;
1190 int i;
1191
1192 flags |= cachep->gfpflags;
1193 if (likely(nodeid == -1)) {
1194 page = alloc_pages(flags, cachep->gfporder);
1195 } else {
1196 page = alloc_pages_node(nodeid, flags, cachep->gfporder);
1197 }
1198 if (!page)
1199 return NULL;
1200 addr = page_address(page);
1201
1202 i = (1 << cachep->gfporder);
1203 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1204 atomic_add(i, &slab_reclaim_pages);
1205 add_page_state(nr_slab, i);
1206 while (i--) {
1207 SetPageSlab(page);
1208 page++;
1209 }
1210 return addr;
1211}
1212
1213/*
1214 * Interface to system's page release.
1215 */
1216static void kmem_freepages(kmem_cache_t *cachep, void *addr)
1217{
1218 unsigned long i = (1<<cachep->gfporder);
1219 struct page *page = virt_to_page(addr);
1220 const unsigned long nr_freed = i;
1221
1222 while (i--) {
1223 if (!TestClearPageSlab(page))
1224 BUG();
1225 page++;
1226 }
1227 sub_page_state(nr_slab, nr_freed);
1228 if (current->reclaim_state)
1229 current->reclaim_state->reclaimed_slab += nr_freed;
1230 free_pages((unsigned long)addr, cachep->gfporder);
1231 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1232 atomic_sub(1<<cachep->gfporder, &slab_reclaim_pages);
1233}
1234
1235static void kmem_rcu_free(struct rcu_head *head)
1236{
1237 struct slab_rcu *slab_rcu = (struct slab_rcu *) head;
1238 kmem_cache_t *cachep = slab_rcu->cachep;
1239
1240 kmem_freepages(cachep, slab_rcu->addr);
1241 if (OFF_SLAB(cachep))
1242 kmem_cache_free(cachep->slabp_cache, slab_rcu);
1243}
1244
1245#if DEBUG
1246
1247#ifdef CONFIG_DEBUG_PAGEALLOC
1248static void store_stackinfo(kmem_cache_t *cachep, unsigned long *addr,
1249 unsigned long caller)
1250{
1251 int size = obj_reallen(cachep);
1252
1253 addr = (unsigned long *)&((char*)addr)[obj_dbghead(cachep)];
1254
1255 if (size < 5*sizeof(unsigned long))
1256 return;
1257
1258 *addr++=0x12345678;
1259 *addr++=caller;
1260 *addr++=smp_processor_id();
1261 size -= 3*sizeof(unsigned long);
1262 {
1263 unsigned long *sptr = &caller;
1264 unsigned long svalue;
1265
1266 while (!kstack_end(sptr)) {
1267 svalue = *sptr++;
1268 if (kernel_text_address(svalue)) {
1269 *addr++=svalue;
1270 size -= sizeof(unsigned long);
1271 if (size <= sizeof(unsigned long))
1272 break;
1273 }
1274 }
1275
1276 }
1277 *addr++=0x87654321;
1278}
1279#endif
1280
1281static void poison_obj(kmem_cache_t *cachep, void *addr, unsigned char val)
1282{
1283 int size = obj_reallen(cachep);
1284 addr = &((char*)addr)[obj_dbghead(cachep)];
1285
1286 memset(addr, val, size);
1287 *(unsigned char *)(addr+size-1) = POISON_END;
1288}
1289
1290static void dump_line(char *data, int offset, int limit)
1291{
1292 int i;
1293 printk(KERN_ERR "%03x:", offset);
1294 for (i=0;i<limit;i++) {
1295 printk(" %02x", (unsigned char)data[offset+i]);
1296 }
1297 printk("\n");
1298}
1299#endif
1300
1301#if DEBUG
1302
1303static void print_objinfo(kmem_cache_t *cachep, void *objp, int lines)
1304{
1305 int i, size;
1306 char *realobj;
1307
1308 if (cachep->flags & SLAB_RED_ZONE) {
1309 printk(KERN_ERR "Redzone: 0x%lx/0x%lx.\n",
1310 *dbg_redzone1(cachep, objp),
1311 *dbg_redzone2(cachep, objp));
1312 }
1313
1314 if (cachep->flags & SLAB_STORE_USER) {
1315 printk(KERN_ERR "Last user: [<%p>]",
1316 *dbg_userword(cachep, objp));
1317 print_symbol("(%s)",
1318 (unsigned long)*dbg_userword(cachep, objp));
1319 printk("\n");
1320 }
1321 realobj = (char*)objp+obj_dbghead(cachep);
1322 size = obj_reallen(cachep);
1323 for (i=0; i<size && lines;i+=16, lines--) {
1324 int limit;
1325 limit = 16;
1326 if (i+limit > size)
1327 limit = size-i;
1328 dump_line(realobj, i, limit);
1329 }
1330}
1331
1332static void check_poison_obj(kmem_cache_t *cachep, void *objp)
1333{
1334 char *realobj;
1335 int size, i;
1336 int lines = 0;
1337
1338 realobj = (char*)objp+obj_dbghead(cachep);
1339 size = obj_reallen(cachep);
1340
1341 for (i=0;i<size;i++) {
1342 char exp = POISON_FREE;
1343 if (i == size-1)
1344 exp = POISON_END;
1345 if (realobj[i] != exp) {
1346 int limit;
1347 /* Mismatch ! */
1348 /* Print header */
1349 if (lines == 0) {
1350 printk(KERN_ERR "Slab corruption: start=%p, len=%d\n",
1351 realobj, size);
1352 print_objinfo(cachep, objp, 0);
1353 }
1354 /* Hexdump the affected line */
1355 i = (i/16)*16;
1356 limit = 16;
1357 if (i+limit > size)
1358 limit = size-i;
1359 dump_line(realobj, i, limit);
1360 i += 16;
1361 lines++;
1362 /* Limit to 5 lines */
1363 if (lines > 5)
1364 break;
1365 }
1366 }
1367 if (lines != 0) {
1368 /* Print some data about the neighboring objects, if they
1369 * exist:
1370 */
1371 struct slab *slabp = GET_PAGE_SLAB(virt_to_page(objp));
1372 int objnr;
1373
1374 objnr = (objp-slabp->s_mem)/cachep->objsize;
1375 if (objnr) {
1376 objp = slabp->s_mem+(objnr-1)*cachep->objsize;
1377 realobj = (char*)objp+obj_dbghead(cachep);
1378 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
1379 realobj, size);
1380 print_objinfo(cachep, objp, 2);
1381 }
1382 if (objnr+1 < cachep->num) {
1383 objp = slabp->s_mem+(objnr+1)*cachep->objsize;
1384 realobj = (char*)objp+obj_dbghead(cachep);
1385 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
1386 realobj, size);
1387 print_objinfo(cachep, objp, 2);
1388 }
1389 }
1390}
1391#endif
1392
1393/* Destroy all the objs in a slab, and release the mem back to the system.
1394 * Before calling the slab must have been unlinked from the cache.
1395 * The cache-lock is not held/needed.
1396 */
1397static void slab_destroy (kmem_cache_t *cachep, struct slab *slabp)
1398{
1399 void *addr = slabp->s_mem - slabp->colouroff;
1400
1401#if DEBUG
1402 int i;
1403 for (i = 0; i < cachep->num; i++) {
1404 void *objp = slabp->s_mem + cachep->objsize * i;
1405
1406 if (cachep->flags & SLAB_POISON) {
1407#ifdef CONFIG_DEBUG_PAGEALLOC
1408 if ((cachep->objsize%PAGE_SIZE)==0 && OFF_SLAB(cachep))
1409 kernel_map_pages(virt_to_page(objp), cachep->objsize/PAGE_SIZE,1);
1410 else
1411 check_poison_obj(cachep, objp);
1412#else
1413 check_poison_obj(cachep, objp);
1414#endif
1415 }
1416 if (cachep->flags & SLAB_RED_ZONE) {
1417 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
1418 slab_error(cachep, "start of a freed object "
1419 "was overwritten");
1420 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
1421 slab_error(cachep, "end of a freed object "
1422 "was overwritten");
1423 }
1424 if (cachep->dtor && !(cachep->flags & SLAB_POISON))
1425 (cachep->dtor)(objp+obj_dbghead(cachep), cachep, 0);
1426 }
1427#else
1428 if (cachep->dtor) {
1429 int i;
1430 for (i = 0; i < cachep->num; i++) {
1431 void* objp = slabp->s_mem+cachep->objsize*i;
1432 (cachep->dtor)(objp, cachep, 0);
1433 }
1434 }
1435#endif
1436
1437 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
1438 struct slab_rcu *slab_rcu;
1439
1440 slab_rcu = (struct slab_rcu *) slabp;
1441 slab_rcu->cachep = cachep;
1442 slab_rcu->addr = addr;
1443 call_rcu(&slab_rcu->head, kmem_rcu_free);
1444 } else {
1445 kmem_freepages(cachep, addr);
1446 if (OFF_SLAB(cachep))
1447 kmem_cache_free(cachep->slabp_cache, slabp);
1448 }
1449}
1450
Christoph Lametere498be72005-09-09 13:03:32 -07001451/* For setting up all the kmem_list3s for cache whose objsize is same
1452 as size of kmem_list3. */
1453static inline void set_up_list3s(kmem_cache_t *cachep, int index)
1454{
1455 int node;
1456
1457 for_each_online_node(node) {
1458 cachep->nodelists[node] = &initkmem_list3[index+node];
1459 cachep->nodelists[node]->next_reap = jiffies +
1460 REAPTIMEOUT_LIST3 +
1461 ((unsigned long)cachep)%REAPTIMEOUT_LIST3;
1462 }
1463}
1464
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465/**
1466 * kmem_cache_create - Create a cache.
1467 * @name: A string which is used in /proc/slabinfo to identify this cache.
1468 * @size: The size of objects to be created in this cache.
1469 * @align: The required alignment for the objects.
1470 * @flags: SLAB flags
1471 * @ctor: A constructor for the objects.
1472 * @dtor: A destructor for the objects.
1473 *
1474 * Returns a ptr to the cache on success, NULL on failure.
1475 * Cannot be called within a int, but can be interrupted.
1476 * The @ctor is run when new pages are allocated by the cache
1477 * and the @dtor is run before the pages are handed back.
1478 *
1479 * @name must be valid until the cache is destroyed. This implies that
1480 * the module calling this has to destroy the cache before getting
1481 * unloaded.
1482 *
1483 * The flags are
1484 *
1485 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
1486 * to catch references to uninitialised memory.
1487 *
1488 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
1489 * for buffer overruns.
1490 *
1491 * %SLAB_NO_REAP - Don't automatically reap this cache when we're under
1492 * memory pressure.
1493 *
1494 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
1495 * cacheline. This can be beneficial if you're counting cycles as closely
1496 * as davem.
1497 */
1498kmem_cache_t *
1499kmem_cache_create (const char *name, size_t size, size_t align,
1500 unsigned long flags, void (*ctor)(void*, kmem_cache_t *, unsigned long),
1501 void (*dtor)(void*, kmem_cache_t *, unsigned long))
1502{
1503 size_t left_over, slab_size, ralign;
1504 kmem_cache_t *cachep = NULL;
1505
1506 /*
1507 * Sanity checks... these are all serious usage bugs.
1508 */
1509 if ((!name) ||
1510 in_interrupt() ||
1511 (size < BYTES_PER_WORD) ||
1512 (size > (1<<MAX_OBJ_ORDER)*PAGE_SIZE) ||
1513 (dtor && !ctor)) {
1514 printk(KERN_ERR "%s: Early error in slab %s\n",
1515 __FUNCTION__, name);
1516 BUG();
1517 }
1518
1519#if DEBUG
1520 WARN_ON(strchr(name, ' ')); /* It confuses parsers */
1521 if ((flags & SLAB_DEBUG_INITIAL) && !ctor) {
1522 /* No constructor, but inital state check requested */
1523 printk(KERN_ERR "%s: No con, but init state check "
1524 "requested - %s\n", __FUNCTION__, name);
1525 flags &= ~SLAB_DEBUG_INITIAL;
1526 }
1527
1528#if FORCED_DEBUG
1529 /*
1530 * Enable redzoning and last user accounting, except for caches with
1531 * large objects, if the increased size would increase the object size
1532 * above the next power of two: caches with object sizes just above a
1533 * power of two have a significant amount of internal fragmentation.
1534 */
1535 if ((size < 4096 || fls(size-1) == fls(size-1+3*BYTES_PER_WORD)))
1536 flags |= SLAB_RED_ZONE|SLAB_STORE_USER;
1537 if (!(flags & SLAB_DESTROY_BY_RCU))
1538 flags |= SLAB_POISON;
1539#endif
1540 if (flags & SLAB_DESTROY_BY_RCU)
1541 BUG_ON(flags & SLAB_POISON);
1542#endif
1543 if (flags & SLAB_DESTROY_BY_RCU)
1544 BUG_ON(dtor);
1545
1546 /*
1547 * Always checks flags, a caller might be expecting debug
1548 * support which isn't available.
1549 */
1550 if (flags & ~CREATE_MASK)
1551 BUG();
1552
1553 /* Check that size is in terms of words. This is needed to avoid
1554 * unaligned accesses for some archs when redzoning is used, and makes
1555 * sure any on-slab bufctl's are also correctly aligned.
1556 */
1557 if (size & (BYTES_PER_WORD-1)) {
1558 size += (BYTES_PER_WORD-1);
1559 size &= ~(BYTES_PER_WORD-1);
1560 }
1561
1562 /* calculate out the final buffer alignment: */
1563 /* 1) arch recommendation: can be overridden for debug */
1564 if (flags & SLAB_HWCACHE_ALIGN) {
1565 /* Default alignment: as specified by the arch code.
1566 * Except if an object is really small, then squeeze multiple
1567 * objects into one cacheline.
1568 */
1569 ralign = cache_line_size();
1570 while (size <= ralign/2)
1571 ralign /= 2;
1572 } else {
1573 ralign = BYTES_PER_WORD;
1574 }
1575 /* 2) arch mandated alignment: disables debug if necessary */
1576 if (ralign < ARCH_SLAB_MINALIGN) {
1577 ralign = ARCH_SLAB_MINALIGN;
1578 if (ralign > BYTES_PER_WORD)
1579 flags &= ~(SLAB_RED_ZONE|SLAB_STORE_USER);
1580 }
1581 /* 3) caller mandated alignment: disables debug if necessary */
1582 if (ralign < align) {
1583 ralign = align;
1584 if (ralign > BYTES_PER_WORD)
1585 flags &= ~(SLAB_RED_ZONE|SLAB_STORE_USER);
1586 }
1587 /* 4) Store it. Note that the debug code below can reduce
1588 * the alignment to BYTES_PER_WORD.
1589 */
1590 align = ralign;
1591
1592 /* Get cache's description obj. */
1593 cachep = (kmem_cache_t *) kmem_cache_alloc(&cache_cache, SLAB_KERNEL);
1594 if (!cachep)
1595 goto opps;
1596 memset(cachep, 0, sizeof(kmem_cache_t));
1597
1598#if DEBUG
1599 cachep->reallen = size;
1600
1601 if (flags & SLAB_RED_ZONE) {
1602 /* redzoning only works with word aligned caches */
1603 align = BYTES_PER_WORD;
1604
1605 /* add space for red zone words */
1606 cachep->dbghead += BYTES_PER_WORD;
1607 size += 2*BYTES_PER_WORD;
1608 }
1609 if (flags & SLAB_STORE_USER) {
1610 /* user store requires word alignment and
1611 * one word storage behind the end of the real
1612 * object.
1613 */
1614 align = BYTES_PER_WORD;
1615 size += BYTES_PER_WORD;
1616 }
1617#if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
Christoph Lametere498be72005-09-09 13:03:32 -07001618 if (size >= malloc_sizes[INDEX_L3+1].cs_size && cachep->reallen > cache_line_size() && size < PAGE_SIZE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 cachep->dbghead += PAGE_SIZE - size;
1620 size = PAGE_SIZE;
1621 }
1622#endif
1623#endif
1624
1625 /* Determine if the slab management is 'on' or 'off' slab. */
1626 if (size >= (PAGE_SIZE>>3))
1627 /*
1628 * Size is large, assume best to place the slab management obj
1629 * off-slab (should allow better packing of objs).
1630 */
1631 flags |= CFLGS_OFF_SLAB;
1632
1633 size = ALIGN(size, align);
1634
1635 if ((flags & SLAB_RECLAIM_ACCOUNT) && size <= PAGE_SIZE) {
1636 /*
1637 * A VFS-reclaimable slab tends to have most allocations
1638 * as GFP_NOFS and we really don't want to have to be allocating
1639 * higher-order pages when we are unable to shrink dcache.
1640 */
1641 cachep->gfporder = 0;
1642 cache_estimate(cachep->gfporder, size, align, flags,
1643 &left_over, &cachep->num);
1644 } else {
1645 /*
1646 * Calculate size (in pages) of slabs, and the num of objs per
1647 * slab. This could be made much more intelligent. For now,
1648 * try to avoid using high page-orders for slabs. When the
1649 * gfp() funcs are more friendly towards high-order requests,
1650 * this should be changed.
1651 */
1652 do {
1653 unsigned int break_flag = 0;
1654cal_wastage:
1655 cache_estimate(cachep->gfporder, size, align, flags,
1656 &left_over, &cachep->num);
1657 if (break_flag)
1658 break;
1659 if (cachep->gfporder >= MAX_GFP_ORDER)
1660 break;
1661 if (!cachep->num)
1662 goto next;
1663 if (flags & CFLGS_OFF_SLAB &&
1664 cachep->num > offslab_limit) {
1665 /* This num of objs will cause problems. */
1666 cachep->gfporder--;
1667 break_flag++;
1668 goto cal_wastage;
1669 }
1670
1671 /*
1672 * Large num of objs is good, but v. large slabs are
1673 * currently bad for the gfp()s.
1674 */
1675 if (cachep->gfporder >= slab_break_gfp_order)
1676 break;
1677
1678 if ((left_over*8) <= (PAGE_SIZE<<cachep->gfporder))
1679 break; /* Acceptable internal fragmentation. */
1680next:
1681 cachep->gfporder++;
1682 } while (1);
1683 }
1684
1685 if (!cachep->num) {
1686 printk("kmem_cache_create: couldn't create cache %s.\n", name);
1687 kmem_cache_free(&cache_cache, cachep);
1688 cachep = NULL;
1689 goto opps;
1690 }
1691 slab_size = ALIGN(cachep->num*sizeof(kmem_bufctl_t)
1692 + sizeof(struct slab), align);
1693
1694 /*
1695 * If the slab has been placed off-slab, and we have enough space then
1696 * move it on-slab. This is at the expense of any extra colouring.
1697 */
1698 if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
1699 flags &= ~CFLGS_OFF_SLAB;
1700 left_over -= slab_size;
1701 }
1702
1703 if (flags & CFLGS_OFF_SLAB) {
1704 /* really off slab. No need for manual alignment */
1705 slab_size = cachep->num*sizeof(kmem_bufctl_t)+sizeof(struct slab);
1706 }
1707
1708 cachep->colour_off = cache_line_size();
1709 /* Offset must be a multiple of the alignment. */
1710 if (cachep->colour_off < align)
1711 cachep->colour_off = align;
1712 cachep->colour = left_over/cachep->colour_off;
1713 cachep->slab_size = slab_size;
1714 cachep->flags = flags;
1715 cachep->gfpflags = 0;
1716 if (flags & SLAB_CACHE_DMA)
1717 cachep->gfpflags |= GFP_DMA;
1718 spin_lock_init(&cachep->spinlock);
1719 cachep->objsize = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
1721 if (flags & CFLGS_OFF_SLAB)
Victor Fuscob2d55072005-09-10 00:26:36 -07001722 cachep->slabp_cache = kmem_find_general_cachep(slab_size, 0u);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 cachep->ctor = ctor;
1724 cachep->dtor = dtor;
1725 cachep->name = name;
1726
1727 /* Don't let CPUs to come and go */
1728 lock_cpu_hotplug();
1729
1730 if (g_cpucache_up == FULL) {
1731 enable_cpucache(cachep);
1732 } else {
1733 if (g_cpucache_up == NONE) {
1734 /* Note: the first kmem_cache_create must create
1735 * the cache that's used by kmalloc(24), otherwise
1736 * the creation of further caches will BUG().
1737 */
Christoph Lametere498be72005-09-09 13:03:32 -07001738 cachep->array[smp_processor_id()] =
1739 &initarray_generic.cache;
1740
1741 /* If the cache that's used by
1742 * kmalloc(sizeof(kmem_list3)) is the first cache,
1743 * then we need to set up all its list3s, otherwise
1744 * the creation of further caches will BUG().
1745 */
1746 set_up_list3s(cachep, SIZE_AC);
1747 if (INDEX_AC == INDEX_L3)
1748 g_cpucache_up = PARTIAL_L3;
1749 else
1750 g_cpucache_up = PARTIAL_AC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07001752 cachep->array[smp_processor_id()] =
1753 kmalloc(sizeof(struct arraycache_init),
1754 GFP_KERNEL);
1755
1756 if (g_cpucache_up == PARTIAL_AC) {
1757 set_up_list3s(cachep, SIZE_L3);
1758 g_cpucache_up = PARTIAL_L3;
1759 } else {
1760 int node;
1761 for_each_online_node(node) {
1762
1763 cachep->nodelists[node] =
1764 kmalloc_node(sizeof(struct kmem_list3),
1765 GFP_KERNEL, node);
1766 BUG_ON(!cachep->nodelists[node]);
1767 kmem_list3_init(cachep->nodelists[node]);
1768 }
1769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 }
Christoph Lametere498be72005-09-09 13:03:32 -07001771 cachep->nodelists[numa_node_id()]->next_reap =
1772 jiffies + REAPTIMEOUT_LIST3 +
1773 ((unsigned long)cachep)%REAPTIMEOUT_LIST3;
1774
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 BUG_ON(!ac_data(cachep));
1776 ac_data(cachep)->avail = 0;
1777 ac_data(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
1778 ac_data(cachep)->batchcount = 1;
1779 ac_data(cachep)->touched = 0;
1780 cachep->batchcount = 1;
1781 cachep->limit = BOOT_CPUCACHE_ENTRIES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 }
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 /* Need the semaphore to access the chain. */
1785 down(&cache_chain_sem);
1786 {
1787 struct list_head *p;
1788 mm_segment_t old_fs;
1789
1790 old_fs = get_fs();
1791 set_fs(KERNEL_DS);
1792 list_for_each(p, &cache_chain) {
1793 kmem_cache_t *pc = list_entry(p, kmem_cache_t, next);
1794 char tmp;
1795 /* This happens when the module gets unloaded and doesn't
1796 destroy its slab cache and noone else reuses the vmalloc
1797 area of the module. Print a warning. */
1798 if (__get_user(tmp,pc->name)) {
1799 printk("SLAB: cache with size %d has lost its name\n",
1800 pc->objsize);
1801 continue;
1802 }
1803 if (!strcmp(pc->name,name)) {
1804 printk("kmem_cache_create: duplicate cache %s\n",name);
1805 up(&cache_chain_sem);
1806 unlock_cpu_hotplug();
1807 BUG();
1808 }
1809 }
1810 set_fs(old_fs);
1811 }
1812
1813 /* cache setup completed, link it into the list */
1814 list_add(&cachep->next, &cache_chain);
1815 up(&cache_chain_sem);
1816 unlock_cpu_hotplug();
1817opps:
1818 if (!cachep && (flags & SLAB_PANIC))
1819 panic("kmem_cache_create(): failed to create slab `%s'\n",
1820 name);
1821 return cachep;
1822}
1823EXPORT_SYMBOL(kmem_cache_create);
1824
1825#if DEBUG
1826static void check_irq_off(void)
1827{
1828 BUG_ON(!irqs_disabled());
1829}
1830
1831static void check_irq_on(void)
1832{
1833 BUG_ON(irqs_disabled());
1834}
1835
1836static void check_spinlock_acquired(kmem_cache_t *cachep)
1837{
1838#ifdef CONFIG_SMP
1839 check_irq_off();
Christoph Lametere498be72005-09-09 13:03:32 -07001840 assert_spin_locked(&cachep->nodelists[numa_node_id()]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841#endif
1842}
Christoph Lametere498be72005-09-09 13:03:32 -07001843
1844static inline void check_spinlock_acquired_node(kmem_cache_t *cachep, int node)
1845{
1846#ifdef CONFIG_SMP
1847 check_irq_off();
1848 assert_spin_locked(&cachep->nodelists[node]->list_lock);
1849#endif
1850}
1851
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852#else
1853#define check_irq_off() do { } while(0)
1854#define check_irq_on() do { } while(0)
1855#define check_spinlock_acquired(x) do { } while(0)
Christoph Lametere498be72005-09-09 13:03:32 -07001856#define check_spinlock_acquired_node(x, y) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857#endif
1858
1859/*
1860 * Waits for all CPUs to execute func().
1861 */
1862static void smp_call_function_all_cpus(void (*func) (void *arg), void *arg)
1863{
1864 check_irq_on();
1865 preempt_disable();
1866
1867 local_irq_disable();
1868 func(arg);
1869 local_irq_enable();
1870
1871 if (smp_call_function(func, arg, 1, 1))
1872 BUG();
1873
1874 preempt_enable();
1875}
1876
1877static void drain_array_locked(kmem_cache_t* cachep,
Christoph Lametere498be72005-09-09 13:03:32 -07001878 struct array_cache *ac, int force, int node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880static void do_drain(void *arg)
1881{
1882 kmem_cache_t *cachep = (kmem_cache_t*)arg;
1883 struct array_cache *ac;
Christoph Lameterff694162005-09-22 21:44:02 -07001884 int node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
1886 check_irq_off();
1887 ac = ac_data(cachep);
Christoph Lameterff694162005-09-22 21:44:02 -07001888 spin_lock(&cachep->nodelists[node]->list_lock);
1889 free_block(cachep, ac->entry, ac->avail, node);
1890 spin_unlock(&cachep->nodelists[node]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 ac->avail = 0;
1892}
1893
1894static void drain_cpu_caches(kmem_cache_t *cachep)
1895{
Christoph Lametere498be72005-09-09 13:03:32 -07001896 struct kmem_list3 *l3;
1897 int node;
1898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 smp_call_function_all_cpus(do_drain, cachep);
1900 check_irq_on();
1901 spin_lock_irq(&cachep->spinlock);
Christoph Lametere498be72005-09-09 13:03:32 -07001902 for_each_online_node(node) {
1903 l3 = cachep->nodelists[node];
1904 if (l3) {
1905 spin_lock(&l3->list_lock);
1906 drain_array_locked(cachep, l3->shared, 1, node);
1907 spin_unlock(&l3->list_lock);
1908 if (l3->alien)
1909 drain_alien_cache(cachep, l3);
1910 }
1911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 spin_unlock_irq(&cachep->spinlock);
1913}
1914
Christoph Lametere498be72005-09-09 13:03:32 -07001915static int __node_shrink(kmem_cache_t *cachep, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916{
1917 struct slab *slabp;
Christoph Lametere498be72005-09-09 13:03:32 -07001918 struct kmem_list3 *l3 = cachep->nodelists[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 int ret;
1920
Christoph Lametere498be72005-09-09 13:03:32 -07001921 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 struct list_head *p;
1923
Christoph Lametere498be72005-09-09 13:03:32 -07001924 p = l3->slabs_free.prev;
1925 if (p == &l3->slabs_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 break;
1927
Christoph Lametere498be72005-09-09 13:03:32 -07001928 slabp = list_entry(l3->slabs_free.prev, struct slab, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929#if DEBUG
1930 if (slabp->inuse)
1931 BUG();
1932#endif
1933 list_del(&slabp->list);
1934
Christoph Lametere498be72005-09-09 13:03:32 -07001935 l3->free_objects -= cachep->num;
1936 spin_unlock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 slab_destroy(cachep, slabp);
Christoph Lametere498be72005-09-09 13:03:32 -07001938 spin_lock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
Christoph Lametere498be72005-09-09 13:03:32 -07001940 ret = !list_empty(&l3->slabs_full) ||
1941 !list_empty(&l3->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 return ret;
1943}
1944
Christoph Lametere498be72005-09-09 13:03:32 -07001945static int __cache_shrink(kmem_cache_t *cachep)
1946{
1947 int ret = 0, i = 0;
1948 struct kmem_list3 *l3;
1949
1950 drain_cpu_caches(cachep);
1951
1952 check_irq_on();
1953 for_each_online_node(i) {
1954 l3 = cachep->nodelists[i];
1955 if (l3) {
1956 spin_lock_irq(&l3->list_lock);
1957 ret += __node_shrink(cachep, i);
1958 spin_unlock_irq(&l3->list_lock);
1959 }
1960 }
1961 return (ret ? 1 : 0);
1962}
1963
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964/**
1965 * kmem_cache_shrink - Shrink a cache.
1966 * @cachep: The cache to shrink.
1967 *
1968 * Releases as many slabs as possible for a cache.
1969 * To help debugging, a zero exit status indicates all slabs were released.
1970 */
1971int kmem_cache_shrink(kmem_cache_t *cachep)
1972{
1973 if (!cachep || in_interrupt())
1974 BUG();
1975
1976 return __cache_shrink(cachep);
1977}
1978EXPORT_SYMBOL(kmem_cache_shrink);
1979
1980/**
1981 * kmem_cache_destroy - delete a cache
1982 * @cachep: the cache to destroy
1983 *
1984 * Remove a kmem_cache_t object from the slab cache.
1985 * Returns 0 on success.
1986 *
1987 * It is expected this function will be called by a module when it is
1988 * unloaded. This will remove the cache completely, and avoid a duplicate
1989 * cache being allocated each time a module is loaded and unloaded, if the
1990 * module doesn't have persistent in-kernel storage across loads and unloads.
1991 *
1992 * The cache must be empty before calling this function.
1993 *
1994 * The caller must guarantee that noone will allocate memory from the cache
1995 * during the kmem_cache_destroy().
1996 */
1997int kmem_cache_destroy(kmem_cache_t * cachep)
1998{
1999 int i;
Christoph Lametere498be72005-09-09 13:03:32 -07002000 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 if (!cachep || in_interrupt())
2003 BUG();
2004
2005 /* Don't let CPUs to come and go */
2006 lock_cpu_hotplug();
2007
2008 /* Find the cache in the chain of caches. */
2009 down(&cache_chain_sem);
2010 /*
2011 * the chain is never empty, cache_cache is never destroyed
2012 */
2013 list_del(&cachep->next);
2014 up(&cache_chain_sem);
2015
2016 if (__cache_shrink(cachep)) {
2017 slab_error(cachep, "Can't free all objects");
2018 down(&cache_chain_sem);
2019 list_add(&cachep->next,&cache_chain);
2020 up(&cache_chain_sem);
2021 unlock_cpu_hotplug();
2022 return 1;
2023 }
2024
2025 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU))
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002026 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Christoph Lametere498be72005-09-09 13:03:32 -07002028 for_each_online_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 kfree(cachep->array[i]);
2030
2031 /* NUMA: free the list3 structures */
Christoph Lametere498be72005-09-09 13:03:32 -07002032 for_each_online_node(i) {
2033 if ((l3 = cachep->nodelists[i])) {
2034 kfree(l3->shared);
2035 free_alien_cache(l3->alien);
2036 kfree(l3);
2037 }
2038 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 kmem_cache_free(&cache_cache, cachep);
2040
2041 unlock_cpu_hotplug();
2042
2043 return 0;
2044}
2045EXPORT_SYMBOL(kmem_cache_destroy);
2046
2047/* Get the memory for a slab management obj. */
Christoph Lametere498be72005-09-09 13:03:32 -07002048static struct slab* alloc_slabmgmt(kmem_cache_t *cachep, void *objp,
Al Virodd0fc662005-10-07 07:46:04 +01002049 int colour_off, gfp_t local_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
2051 struct slab *slabp;
2052
2053 if (OFF_SLAB(cachep)) {
2054 /* Slab management obj is off-slab. */
2055 slabp = kmem_cache_alloc(cachep->slabp_cache, local_flags);
2056 if (!slabp)
2057 return NULL;
2058 } else {
2059 slabp = objp+colour_off;
2060 colour_off += cachep->slab_size;
2061 }
2062 slabp->inuse = 0;
2063 slabp->colouroff = colour_off;
2064 slabp->s_mem = objp+colour_off;
2065
2066 return slabp;
2067}
2068
2069static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
2070{
2071 return (kmem_bufctl_t *)(slabp+1);
2072}
2073
2074static void cache_init_objs(kmem_cache_t *cachep,
2075 struct slab *slabp, unsigned long ctor_flags)
2076{
2077 int i;
2078
2079 for (i = 0; i < cachep->num; i++) {
Christoph Lametere498be72005-09-09 13:03:32 -07002080 void *objp = slabp->s_mem+cachep->objsize*i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081#if DEBUG
2082 /* need to poison the objs? */
2083 if (cachep->flags & SLAB_POISON)
2084 poison_obj(cachep, objp, POISON_FREE);
2085 if (cachep->flags & SLAB_STORE_USER)
2086 *dbg_userword(cachep, objp) = NULL;
2087
2088 if (cachep->flags & SLAB_RED_ZONE) {
2089 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2090 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2091 }
2092 /*
2093 * Constructors are not allowed to allocate memory from
2094 * the same cache which they are a constructor for.
2095 * Otherwise, deadlock. They must also be threaded.
2096 */
2097 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
2098 cachep->ctor(objp+obj_dbghead(cachep), cachep, ctor_flags);
2099
2100 if (cachep->flags & SLAB_RED_ZONE) {
2101 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2102 slab_error(cachep, "constructor overwrote the"
2103 " end of an object");
2104 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2105 slab_error(cachep, "constructor overwrote the"
2106 " start of an object");
2107 }
2108 if ((cachep->objsize % PAGE_SIZE) == 0 && OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
2109 kernel_map_pages(virt_to_page(objp), cachep->objsize/PAGE_SIZE, 0);
2110#else
2111 if (cachep->ctor)
2112 cachep->ctor(objp, cachep, ctor_flags);
2113#endif
2114 slab_bufctl(slabp)[i] = i+1;
2115 }
2116 slab_bufctl(slabp)[i-1] = BUFCTL_END;
2117 slabp->free = 0;
2118}
2119
Al Viro6daa0e22005-10-21 03:18:50 -04002120static void kmem_flagcheck(kmem_cache_t *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121{
2122 if (flags & SLAB_DMA) {
2123 if (!(cachep->gfpflags & GFP_DMA))
2124 BUG();
2125 } else {
2126 if (cachep->gfpflags & GFP_DMA)
2127 BUG();
2128 }
2129}
2130
2131static void set_slab_attr(kmem_cache_t *cachep, struct slab *slabp, void *objp)
2132{
2133 int i;
2134 struct page *page;
2135
2136 /* Nasty!!!!!! I hope this is OK. */
2137 i = 1 << cachep->gfporder;
2138 page = virt_to_page(objp);
2139 do {
2140 SET_PAGE_CACHE(page, cachep);
2141 SET_PAGE_SLAB(page, slabp);
2142 page++;
2143 } while (--i);
2144}
2145
2146/*
2147 * Grow (by 1) the number of slabs within a cache. This is called by
2148 * kmem_cache_alloc() when there are no active objs left in a cache.
2149 */
Al Virodd0fc662005-10-07 07:46:04 +01002150static int cache_grow(kmem_cache_t *cachep, gfp_t flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151{
2152 struct slab *slabp;
2153 void *objp;
2154 size_t offset;
Al Viro6daa0e22005-10-21 03:18:50 -04002155 gfp_t local_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 unsigned long ctor_flags;
Christoph Lametere498be72005-09-09 13:03:32 -07002157 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158
2159 /* Be lazy and only check for valid flags here,
2160 * keeping it out of the critical path in kmem_cache_alloc().
2161 */
2162 if (flags & ~(SLAB_DMA|SLAB_LEVEL_MASK|SLAB_NO_GROW))
2163 BUG();
2164 if (flags & SLAB_NO_GROW)
2165 return 0;
2166
2167 ctor_flags = SLAB_CTOR_CONSTRUCTOR;
2168 local_flags = (flags & SLAB_LEVEL_MASK);
2169 if (!(local_flags & __GFP_WAIT))
2170 /*
2171 * Not allowed to sleep. Need to tell a constructor about
2172 * this - it might need to know...
2173 */
2174 ctor_flags |= SLAB_CTOR_ATOMIC;
2175
2176 /* About to mess with non-constant members - lock. */
2177 check_irq_off();
2178 spin_lock(&cachep->spinlock);
2179
2180 /* Get colour for the slab, and cal the next value. */
2181 offset = cachep->colour_next;
2182 cachep->colour_next++;
2183 if (cachep->colour_next >= cachep->colour)
2184 cachep->colour_next = 0;
2185 offset *= cachep->colour_off;
2186
2187 spin_unlock(&cachep->spinlock);
2188
Christoph Lametere498be72005-09-09 13:03:32 -07002189 check_irq_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 if (local_flags & __GFP_WAIT)
2191 local_irq_enable();
2192
2193 /*
2194 * The test for missing atomic flag is performed here, rather than
2195 * the more obvious place, simply to reduce the critical path length
2196 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2197 * will eventually be caught here (where it matters).
2198 */
2199 kmem_flagcheck(cachep, flags);
2200
Christoph Lametere498be72005-09-09 13:03:32 -07002201 /* Get mem for the objs.
2202 * Attempt to allocate a physical page from 'nodeid',
2203 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 if (!(objp = kmem_getpages(cachep, flags, nodeid)))
2205 goto failed;
2206
2207 /* Get slab management. */
2208 if (!(slabp = alloc_slabmgmt(cachep, objp, offset, local_flags)))
2209 goto opps1;
2210
Christoph Lametere498be72005-09-09 13:03:32 -07002211 slabp->nodeid = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 set_slab_attr(cachep, slabp, objp);
2213
2214 cache_init_objs(cachep, slabp, ctor_flags);
2215
2216 if (local_flags & __GFP_WAIT)
2217 local_irq_disable();
2218 check_irq_off();
Christoph Lametere498be72005-09-09 13:03:32 -07002219 l3 = cachep->nodelists[nodeid];
2220 spin_lock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222 /* Make slab active. */
Christoph Lametere498be72005-09-09 13:03:32 -07002223 list_add_tail(&slabp->list, &(l3->slabs_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 STATS_INC_GROWN(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07002225 l3->free_objects += cachep->num;
2226 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 return 1;
2228opps1:
2229 kmem_freepages(cachep, objp);
2230failed:
2231 if (local_flags & __GFP_WAIT)
2232 local_irq_disable();
2233 return 0;
2234}
2235
2236#if DEBUG
2237
2238/*
2239 * Perform extra freeing checks:
2240 * - detect bad pointers.
2241 * - POISON/RED_ZONE checking
2242 * - destructor calls, for caches with POISON+dtor
2243 */
2244static void kfree_debugcheck(const void *objp)
2245{
2246 struct page *page;
2247
2248 if (!virt_addr_valid(objp)) {
2249 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
2250 (unsigned long)objp);
2251 BUG();
2252 }
2253 page = virt_to_page(objp);
2254 if (!PageSlab(page)) {
2255 printk(KERN_ERR "kfree_debugcheck: bad ptr %lxh.\n", (unsigned long)objp);
2256 BUG();
2257 }
2258}
2259
2260static void *cache_free_debugcheck(kmem_cache_t *cachep, void *objp,
2261 void *caller)
2262{
2263 struct page *page;
2264 unsigned int objnr;
2265 struct slab *slabp;
2266
2267 objp -= obj_dbghead(cachep);
2268 kfree_debugcheck(objp);
2269 page = virt_to_page(objp);
2270
2271 if (GET_PAGE_CACHE(page) != cachep) {
2272 printk(KERN_ERR "mismatch in kmem_cache_free: expected cache %p, got %p\n",
2273 GET_PAGE_CACHE(page),cachep);
2274 printk(KERN_ERR "%p is %s.\n", cachep, cachep->name);
2275 printk(KERN_ERR "%p is %s.\n", GET_PAGE_CACHE(page), GET_PAGE_CACHE(page)->name);
2276 WARN_ON(1);
2277 }
2278 slabp = GET_PAGE_SLAB(page);
2279
2280 if (cachep->flags & SLAB_RED_ZONE) {
2281 if (*dbg_redzone1(cachep, objp) != RED_ACTIVE || *dbg_redzone2(cachep, objp) != RED_ACTIVE) {
2282 slab_error(cachep, "double free, or memory outside"
2283 " object was overwritten");
2284 printk(KERN_ERR "%p: redzone 1: 0x%lx, redzone 2: 0x%lx.\n",
2285 objp, *dbg_redzone1(cachep, objp), *dbg_redzone2(cachep, objp));
2286 }
2287 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2288 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2289 }
2290 if (cachep->flags & SLAB_STORE_USER)
2291 *dbg_userword(cachep, objp) = caller;
2292
2293 objnr = (objp-slabp->s_mem)/cachep->objsize;
2294
2295 BUG_ON(objnr >= cachep->num);
2296 BUG_ON(objp != slabp->s_mem + objnr*cachep->objsize);
2297
2298 if (cachep->flags & SLAB_DEBUG_INITIAL) {
2299 /* Need to call the slab's constructor so the
2300 * caller can perform a verify of its state (debugging).
2301 * Called without the cache-lock held.
2302 */
2303 cachep->ctor(objp+obj_dbghead(cachep),
2304 cachep, SLAB_CTOR_CONSTRUCTOR|SLAB_CTOR_VERIFY);
2305 }
2306 if (cachep->flags & SLAB_POISON && cachep->dtor) {
2307 /* we want to cache poison the object,
2308 * call the destruction callback
2309 */
2310 cachep->dtor(objp+obj_dbghead(cachep), cachep, 0);
2311 }
2312 if (cachep->flags & SLAB_POISON) {
2313#ifdef CONFIG_DEBUG_PAGEALLOC
2314 if ((cachep->objsize % PAGE_SIZE) == 0 && OFF_SLAB(cachep)) {
2315 store_stackinfo(cachep, objp, (unsigned long)caller);
2316 kernel_map_pages(virt_to_page(objp), cachep->objsize/PAGE_SIZE, 0);
2317 } else {
2318 poison_obj(cachep, objp, POISON_FREE);
2319 }
2320#else
2321 poison_obj(cachep, objp, POISON_FREE);
2322#endif
2323 }
2324 return objp;
2325}
2326
2327static void check_slabp(kmem_cache_t *cachep, struct slab *slabp)
2328{
2329 kmem_bufctl_t i;
2330 int entries = 0;
2331
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 /* Check slab's freelist to see if this obj is there. */
2333 for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
2334 entries++;
2335 if (entries > cachep->num || i >= cachep->num)
2336 goto bad;
2337 }
2338 if (entries != cachep->num - slabp->inuse) {
2339bad:
2340 printk(KERN_ERR "slab: Internal list corruption detected in cache '%s'(%d), slabp %p(%d). Hexdump:\n",
2341 cachep->name, cachep->num, slabp, slabp->inuse);
2342 for (i=0;i<sizeof(slabp)+cachep->num*sizeof(kmem_bufctl_t);i++) {
2343 if ((i%16)==0)
2344 printk("\n%03x:", i);
2345 printk(" %02x", ((unsigned char*)slabp)[i]);
2346 }
2347 printk("\n");
2348 BUG();
2349 }
2350}
2351#else
2352#define kfree_debugcheck(x) do { } while(0)
2353#define cache_free_debugcheck(x,objp,z) (objp)
2354#define check_slabp(x,y) do { } while(0)
2355#endif
2356
Al Virodd0fc662005-10-07 07:46:04 +01002357static void *cache_alloc_refill(kmem_cache_t *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358{
2359 int batchcount;
2360 struct kmem_list3 *l3;
2361 struct array_cache *ac;
2362
2363 check_irq_off();
2364 ac = ac_data(cachep);
2365retry:
2366 batchcount = ac->batchcount;
2367 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
2368 /* if there was little recent activity on this
2369 * cache, then perform only a partial refill.
2370 * Otherwise we could generate refill bouncing.
2371 */
2372 batchcount = BATCHREFILL_LIMIT;
2373 }
Christoph Lametere498be72005-09-09 13:03:32 -07002374 l3 = cachep->nodelists[numa_node_id()];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Christoph Lametere498be72005-09-09 13:03:32 -07002376 BUG_ON(ac->avail > 0 || !l3);
2377 spin_lock(&l3->list_lock);
2378
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if (l3->shared) {
2380 struct array_cache *shared_array = l3->shared;
2381 if (shared_array->avail) {
2382 if (batchcount > shared_array->avail)
2383 batchcount = shared_array->avail;
2384 shared_array->avail -= batchcount;
2385 ac->avail = batchcount;
Christoph Lametere498be72005-09-09 13:03:32 -07002386 memcpy(ac->entry,
2387 &(shared_array->entry[shared_array->avail]),
2388 sizeof(void*)*batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 shared_array->touched = 1;
2390 goto alloc_done;
2391 }
2392 }
2393 while (batchcount > 0) {
2394 struct list_head *entry;
2395 struct slab *slabp;
2396 /* Get slab alloc is to come from. */
2397 entry = l3->slabs_partial.next;
2398 if (entry == &l3->slabs_partial) {
2399 l3->free_touched = 1;
2400 entry = l3->slabs_free.next;
2401 if (entry == &l3->slabs_free)
2402 goto must_grow;
2403 }
2404
2405 slabp = list_entry(entry, struct slab, list);
2406 check_slabp(cachep, slabp);
2407 check_spinlock_acquired(cachep);
2408 while (slabp->inuse < cachep->num && batchcount--) {
2409 kmem_bufctl_t next;
2410 STATS_INC_ALLOCED(cachep);
2411 STATS_INC_ACTIVE(cachep);
2412 STATS_SET_HIGH(cachep);
2413
2414 /* get obj pointer */
Christoph Lametere498be72005-09-09 13:03:32 -07002415 ac->entry[ac->avail++] = slabp->s_mem +
2416 slabp->free*cachep->objsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417
2418 slabp->inuse++;
2419 next = slab_bufctl(slabp)[slabp->free];
2420#if DEBUG
2421 slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
2422#endif
2423 slabp->free = next;
2424 }
2425 check_slabp(cachep, slabp);
2426
2427 /* move slabp to correct slabp list: */
2428 list_del(&slabp->list);
2429 if (slabp->free == BUFCTL_END)
2430 list_add(&slabp->list, &l3->slabs_full);
2431 else
2432 list_add(&slabp->list, &l3->slabs_partial);
2433 }
2434
2435must_grow:
2436 l3->free_objects -= ac->avail;
2437alloc_done:
Christoph Lametere498be72005-09-09 13:03:32 -07002438 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439
2440 if (unlikely(!ac->avail)) {
2441 int x;
Christoph Lametere498be72005-09-09 13:03:32 -07002442 x = cache_grow(cachep, flags, numa_node_id());
2443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 // cache_grow can reenable interrupts, then ac could change.
2445 ac = ac_data(cachep);
2446 if (!x && ac->avail == 0) // no objects in sight? abort
2447 return NULL;
2448
2449 if (!ac->avail) // objects refilled by interrupt?
2450 goto retry;
2451 }
2452 ac->touched = 1;
Christoph Lametere498be72005-09-09 13:03:32 -07002453 return ac->entry[--ac->avail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454}
2455
2456static inline void
Al Virodd0fc662005-10-07 07:46:04 +01002457cache_alloc_debugcheck_before(kmem_cache_t *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458{
2459 might_sleep_if(flags & __GFP_WAIT);
2460#if DEBUG
2461 kmem_flagcheck(cachep, flags);
2462#endif
2463}
2464
2465#if DEBUG
2466static void *
2467cache_alloc_debugcheck_after(kmem_cache_t *cachep,
Al Virodd0fc662005-10-07 07:46:04 +01002468 gfp_t flags, void *objp, void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
2470 if (!objp)
2471 return objp;
2472 if (cachep->flags & SLAB_POISON) {
2473#ifdef CONFIG_DEBUG_PAGEALLOC
2474 if ((cachep->objsize % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
2475 kernel_map_pages(virt_to_page(objp), cachep->objsize/PAGE_SIZE, 1);
2476 else
2477 check_poison_obj(cachep, objp);
2478#else
2479 check_poison_obj(cachep, objp);
2480#endif
2481 poison_obj(cachep, objp, POISON_INUSE);
2482 }
2483 if (cachep->flags & SLAB_STORE_USER)
2484 *dbg_userword(cachep, objp) = caller;
2485
2486 if (cachep->flags & SLAB_RED_ZONE) {
2487 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE || *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
2488 slab_error(cachep, "double free, or memory outside"
2489 " object was overwritten");
2490 printk(KERN_ERR "%p: redzone 1: 0x%lx, redzone 2: 0x%lx.\n",
2491 objp, *dbg_redzone1(cachep, objp), *dbg_redzone2(cachep, objp));
2492 }
2493 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
2494 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
2495 }
2496 objp += obj_dbghead(cachep);
2497 if (cachep->ctor && cachep->flags & SLAB_POISON) {
2498 unsigned long ctor_flags = SLAB_CTOR_CONSTRUCTOR;
2499
2500 if (!(flags & __GFP_WAIT))
2501 ctor_flags |= SLAB_CTOR_ATOMIC;
2502
2503 cachep->ctor(objp, cachep, ctor_flags);
2504 }
2505 return objp;
2506}
2507#else
2508#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
2509#endif
2510
Al Virodd0fc662005-10-07 07:46:04 +01002511static inline void *____cache_alloc(kmem_cache_t *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 void* objp;
2514 struct array_cache *ac;
2515
Alok N Kataria5c382302005-09-27 21:45:46 -07002516 check_irq_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 ac = ac_data(cachep);
2518 if (likely(ac->avail)) {
2519 STATS_INC_ALLOCHIT(cachep);
2520 ac->touched = 1;
Christoph Lametere498be72005-09-09 13:03:32 -07002521 objp = ac->entry[--ac->avail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 } else {
2523 STATS_INC_ALLOCMISS(cachep);
2524 objp = cache_alloc_refill(cachep, flags);
2525 }
Alok N Kataria5c382302005-09-27 21:45:46 -07002526 return objp;
2527}
2528
Al Virodd0fc662005-10-07 07:46:04 +01002529static inline void *__cache_alloc(kmem_cache_t *cachep, gfp_t flags)
Alok N Kataria5c382302005-09-27 21:45:46 -07002530{
2531 unsigned long save_flags;
2532 void* objp;
2533
2534 cache_alloc_debugcheck_before(cachep, flags);
2535
2536 local_irq_save(save_flags);
2537 objp = ____cache_alloc(cachep, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 local_irq_restore(save_flags);
Eric Dumazet34342e82005-09-03 15:55:06 -07002539 objp = cache_alloc_debugcheck_after(cachep, flags, objp,
2540 __builtin_return_address(0));
2541 prefetchw(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 return objp;
2543}
2544
Christoph Lametere498be72005-09-09 13:03:32 -07002545#ifdef CONFIG_NUMA
2546/*
2547 * A interface to enable slab creation on nodeid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548 */
Al Viro6daa0e22005-10-21 03:18:50 -04002549static void *__cache_alloc_node(kmem_cache_t *cachep, gfp_t flags, int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07002550{
2551 struct list_head *entry;
2552 struct slab *slabp;
2553 struct kmem_list3 *l3;
2554 void *obj;
2555 kmem_bufctl_t next;
2556 int x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557
Christoph Lametere498be72005-09-09 13:03:32 -07002558 l3 = cachep->nodelists[nodeid];
2559 BUG_ON(!l3);
2560
2561retry:
2562 spin_lock(&l3->list_lock);
2563 entry = l3->slabs_partial.next;
2564 if (entry == &l3->slabs_partial) {
2565 l3->free_touched = 1;
2566 entry = l3->slabs_free.next;
2567 if (entry == &l3->slabs_free)
2568 goto must_grow;
2569 }
2570
2571 slabp = list_entry(entry, struct slab, list);
2572 check_spinlock_acquired_node(cachep, nodeid);
2573 check_slabp(cachep, slabp);
2574
2575 STATS_INC_NODEALLOCS(cachep);
2576 STATS_INC_ACTIVE(cachep);
2577 STATS_SET_HIGH(cachep);
2578
2579 BUG_ON(slabp->inuse == cachep->num);
2580
2581 /* get obj pointer */
2582 obj = slabp->s_mem + slabp->free*cachep->objsize;
2583 slabp->inuse++;
2584 next = slab_bufctl(slabp)[slabp->free];
2585#if DEBUG
2586 slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
2587#endif
2588 slabp->free = next;
2589 check_slabp(cachep, slabp);
2590 l3->free_objects--;
2591 /* move slabp to correct slabp list: */
2592 list_del(&slabp->list);
2593
2594 if (slabp->free == BUFCTL_END) {
2595 list_add(&slabp->list, &l3->slabs_full);
2596 } else {
2597 list_add(&slabp->list, &l3->slabs_partial);
2598 }
2599
2600 spin_unlock(&l3->list_lock);
2601 goto done;
2602
2603must_grow:
2604 spin_unlock(&l3->list_lock);
2605 x = cache_grow(cachep, flags, nodeid);
2606
2607 if (!x)
2608 return NULL;
2609
2610 goto retry;
2611done:
2612 return obj;
2613}
2614#endif
2615
2616/*
2617 * Caller needs to acquire correct kmem_list's list_lock
2618 */
Christoph Lameterff694162005-09-22 21:44:02 -07002619static void free_block(kmem_cache_t *cachep, void **objpp, int nr_objects, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620{
2621 int i;
Christoph Lametere498be72005-09-09 13:03:32 -07002622 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
2624 for (i = 0; i < nr_objects; i++) {
2625 void *objp = objpp[i];
2626 struct slab *slabp;
2627 unsigned int objnr;
2628
2629 slabp = GET_PAGE_SLAB(virt_to_page(objp));
Christoph Lameterff694162005-09-22 21:44:02 -07002630 l3 = cachep->nodelists[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 list_del(&slabp->list);
2632 objnr = (objp - slabp->s_mem) / cachep->objsize;
Christoph Lameterff694162005-09-22 21:44:02 -07002633 check_spinlock_acquired_node(cachep, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 check_slabp(cachep, slabp);
Christoph Lametere498be72005-09-09 13:03:32 -07002635
2636
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637#if DEBUG
2638 if (slab_bufctl(slabp)[objnr] != BUFCTL_FREE) {
Christoph Lametere498be72005-09-09 13:03:32 -07002639 printk(KERN_ERR "slab: double free detected in cache "
2640 "'%s', objp %p\n", cachep->name, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 BUG();
2642 }
2643#endif
2644 slab_bufctl(slabp)[objnr] = slabp->free;
2645 slabp->free = objnr;
2646 STATS_DEC_ACTIVE(cachep);
2647 slabp->inuse--;
Christoph Lametere498be72005-09-09 13:03:32 -07002648 l3->free_objects++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 check_slabp(cachep, slabp);
2650
2651 /* fixup slab chains */
2652 if (slabp->inuse == 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07002653 if (l3->free_objects > l3->free_limit) {
2654 l3->free_objects -= cachep->num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 slab_destroy(cachep, slabp);
2656 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07002657 list_add(&slabp->list, &l3->slabs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 }
2659 } else {
2660 /* Unconditionally move a slab to the end of the
2661 * partial list on free - maximum time for the
2662 * other objects to be freed, too.
2663 */
Christoph Lametere498be72005-09-09 13:03:32 -07002664 list_add_tail(&slabp->list, &l3->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 }
2666 }
2667}
2668
2669static void cache_flusharray(kmem_cache_t *cachep, struct array_cache *ac)
2670{
2671 int batchcount;
Christoph Lametere498be72005-09-09 13:03:32 -07002672 struct kmem_list3 *l3;
Christoph Lameterff694162005-09-22 21:44:02 -07002673 int node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674
2675 batchcount = ac->batchcount;
2676#if DEBUG
2677 BUG_ON(!batchcount || batchcount > ac->avail);
2678#endif
2679 check_irq_off();
Christoph Lameterff694162005-09-22 21:44:02 -07002680 l3 = cachep->nodelists[node];
Christoph Lametere498be72005-09-09 13:03:32 -07002681 spin_lock(&l3->list_lock);
2682 if (l3->shared) {
2683 struct array_cache *shared_array = l3->shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 int max = shared_array->limit-shared_array->avail;
2685 if (max) {
2686 if (batchcount > max)
2687 batchcount = max;
Christoph Lametere498be72005-09-09 13:03:32 -07002688 memcpy(&(shared_array->entry[shared_array->avail]),
2689 ac->entry,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 sizeof(void*)*batchcount);
2691 shared_array->avail += batchcount;
2692 goto free_done;
2693 }
2694 }
2695
Christoph Lameterff694162005-09-22 21:44:02 -07002696 free_block(cachep, ac->entry, batchcount, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697free_done:
2698#if STATS
2699 {
2700 int i = 0;
2701 struct list_head *p;
2702
Christoph Lametere498be72005-09-09 13:03:32 -07002703 p = l3->slabs_free.next;
2704 while (p != &(l3->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 struct slab *slabp;
2706
2707 slabp = list_entry(p, struct slab, list);
2708 BUG_ON(slabp->inuse);
2709
2710 i++;
2711 p = p->next;
2712 }
2713 STATS_SET_FREEABLE(cachep, i);
2714 }
2715#endif
Christoph Lametere498be72005-09-09 13:03:32 -07002716 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 ac->avail -= batchcount;
Christoph Lametere498be72005-09-09 13:03:32 -07002718 memmove(ac->entry, &(ac->entry[batchcount]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 sizeof(void*)*ac->avail);
2720}
2721
Christoph Lametere498be72005-09-09 13:03:32 -07002722
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723/*
2724 * __cache_free
2725 * Release an obj back to its cache. If the obj has a constructed
2726 * state, it must be in this state _before_ it is released.
2727 *
2728 * Called with disabled ints.
2729 */
2730static inline void __cache_free(kmem_cache_t *cachep, void *objp)
2731{
2732 struct array_cache *ac = ac_data(cachep);
2733
2734 check_irq_off();
2735 objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));
2736
Christoph Lametere498be72005-09-09 13:03:32 -07002737 /* Make sure we are not freeing a object from another
2738 * node to the array cache on this cpu.
2739 */
2740#ifdef CONFIG_NUMA
2741 {
2742 struct slab *slabp;
2743 slabp = GET_PAGE_SLAB(virt_to_page(objp));
2744 if (unlikely(slabp->nodeid != numa_node_id())) {
2745 struct array_cache *alien = NULL;
2746 int nodeid = slabp->nodeid;
2747 struct kmem_list3 *l3 = cachep->nodelists[numa_node_id()];
2748
2749 STATS_INC_NODEFREES(cachep);
2750 if (l3->alien && l3->alien[nodeid]) {
2751 alien = l3->alien[nodeid];
2752 spin_lock(&alien->lock);
2753 if (unlikely(alien->avail == alien->limit))
2754 __drain_alien_cache(cachep,
2755 alien, nodeid);
2756 alien->entry[alien->avail++] = objp;
2757 spin_unlock(&alien->lock);
2758 } else {
2759 spin_lock(&(cachep->nodelists[nodeid])->
2760 list_lock);
Christoph Lameterff694162005-09-22 21:44:02 -07002761 free_block(cachep, &objp, 1, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07002762 spin_unlock(&(cachep->nodelists[nodeid])->
2763 list_lock);
2764 }
2765 return;
2766 }
2767 }
2768#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 if (likely(ac->avail < ac->limit)) {
2770 STATS_INC_FREEHIT(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07002771 ac->entry[ac->avail++] = objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 return;
2773 } else {
2774 STATS_INC_FREEMISS(cachep);
2775 cache_flusharray(cachep, ac);
Christoph Lametere498be72005-09-09 13:03:32 -07002776 ac->entry[ac->avail++] = objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 }
2778}
2779
2780/**
2781 * kmem_cache_alloc - Allocate an object
2782 * @cachep: The cache to allocate from.
2783 * @flags: See kmalloc().
2784 *
2785 * Allocate an object from this cache. The flags are only relevant
2786 * if the cache has no available objects.
2787 */
Al Virodd0fc662005-10-07 07:46:04 +01002788void *kmem_cache_alloc(kmem_cache_t *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789{
2790 return __cache_alloc(cachep, flags);
2791}
2792EXPORT_SYMBOL(kmem_cache_alloc);
2793
2794/**
2795 * kmem_ptr_validate - check if an untrusted pointer might
2796 * be a slab entry.
2797 * @cachep: the cache we're checking against
2798 * @ptr: pointer to validate
2799 *
2800 * This verifies that the untrusted pointer looks sane:
2801 * it is _not_ a guarantee that the pointer is actually
2802 * part of the slab cache in question, but it at least
2803 * validates that the pointer can be dereferenced and
2804 * looks half-way sane.
2805 *
2806 * Currently only used for dentry validation.
2807 */
2808int fastcall kmem_ptr_validate(kmem_cache_t *cachep, void *ptr)
2809{
2810 unsigned long addr = (unsigned long) ptr;
2811 unsigned long min_addr = PAGE_OFFSET;
2812 unsigned long align_mask = BYTES_PER_WORD-1;
2813 unsigned long size = cachep->objsize;
2814 struct page *page;
2815
2816 if (unlikely(addr < min_addr))
2817 goto out;
2818 if (unlikely(addr > (unsigned long)high_memory - size))
2819 goto out;
2820 if (unlikely(addr & align_mask))
2821 goto out;
2822 if (unlikely(!kern_addr_valid(addr)))
2823 goto out;
2824 if (unlikely(!kern_addr_valid(addr + size - 1)))
2825 goto out;
2826 page = virt_to_page(ptr);
2827 if (unlikely(!PageSlab(page)))
2828 goto out;
2829 if (unlikely(GET_PAGE_CACHE(page) != cachep))
2830 goto out;
2831 return 1;
2832out:
2833 return 0;
2834}
2835
2836#ifdef CONFIG_NUMA
2837/**
2838 * kmem_cache_alloc_node - Allocate an object on the specified node
2839 * @cachep: The cache to allocate from.
2840 * @flags: See kmalloc().
2841 * @nodeid: node number of the target node.
2842 *
2843 * Identical to kmem_cache_alloc, except that this function is slow
2844 * and can sleep. And it will allocate memory on the given node, which
2845 * can improve the performance for cpu bound structures.
Christoph Lametere498be72005-09-09 13:03:32 -07002846 * New and improved: it will now make sure that the object gets
2847 * put on the correct node list so that there is no false sharing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 */
Al Virodd0fc662005-10-07 07:46:04 +01002849void *kmem_cache_alloc_node(kmem_cache_t *cachep, gfp_t flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850{
Christoph Lametere498be72005-09-09 13:03:32 -07002851 unsigned long save_flags;
2852 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
Christoph Lameterff694162005-09-22 21:44:02 -07002854 if (nodeid == -1)
Christoph Lametere498be72005-09-09 13:03:32 -07002855 return __cache_alloc(cachep, flags);
Christoph Lameter83b78bd2005-07-06 10:47:07 -07002856
Christoph Lametere498be72005-09-09 13:03:32 -07002857 if (unlikely(!cachep->nodelists[nodeid])) {
2858 /* Fall back to __cache_alloc if we run into trouble */
2859 printk(KERN_WARNING "slab: not allocating in inactive node %d for cache %s\n", nodeid, cachep->name);
2860 return __cache_alloc(cachep,flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Christoph Lametere498be72005-09-09 13:03:32 -07002863 cache_alloc_debugcheck_before(cachep, flags);
2864 local_irq_save(save_flags);
Alok N Kataria5c382302005-09-27 21:45:46 -07002865 if (nodeid == numa_node_id())
2866 ptr = ____cache_alloc(cachep, flags);
2867 else
2868 ptr = __cache_alloc_node(cachep, flags, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07002869 local_irq_restore(save_flags);
2870 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Christoph Lametere498be72005-09-09 13:03:32 -07002872 return ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873}
2874EXPORT_SYMBOL(kmem_cache_alloc_node);
2875
Al Virodd0fc662005-10-07 07:46:04 +01002876void *kmalloc_node(size_t size, gfp_t flags, int node)
Manfred Spraul97e2bde2005-05-01 08:58:38 -07002877{
2878 kmem_cache_t *cachep;
2879
2880 cachep = kmem_find_general_cachep(size, flags);
2881 if (unlikely(cachep == NULL))
2882 return NULL;
2883 return kmem_cache_alloc_node(cachep, flags, node);
2884}
2885EXPORT_SYMBOL(kmalloc_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886#endif
2887
2888/**
2889 * kmalloc - allocate memory
2890 * @size: how many bytes of memory are required.
2891 * @flags: the type of memory to allocate.
2892 *
2893 * kmalloc is the normal method of allocating memory
2894 * in the kernel.
2895 *
2896 * The @flags argument may be one of:
2897 *
2898 * %GFP_USER - Allocate memory on behalf of user. May sleep.
2899 *
2900 * %GFP_KERNEL - Allocate normal kernel ram. May sleep.
2901 *
2902 * %GFP_ATOMIC - Allocation will not sleep. Use inside interrupt handlers.
2903 *
2904 * Additionally, the %GFP_DMA flag may be set to indicate the memory
2905 * must be suitable for DMA. This can mean different things on different
2906 * platforms. For example, on i386, it means that the memory must come
2907 * from the first 16MB.
2908 */
Al Virodd0fc662005-10-07 07:46:04 +01002909void *__kmalloc(size_t size, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910{
2911 kmem_cache_t *cachep;
2912
Manfred Spraul97e2bde2005-05-01 08:58:38 -07002913 /* If you want to save a few bytes .text space: replace
2914 * __ with kmem_.
2915 * Then kmalloc uses the uninlined functions instead of the inline
2916 * functions.
2917 */
2918 cachep = __find_general_cachep(size, flags);
Andrew Mortondbdb9042005-09-23 13:24:10 -07002919 if (unlikely(cachep == NULL))
2920 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 return __cache_alloc(cachep, flags);
2922}
2923EXPORT_SYMBOL(__kmalloc);
2924
2925#ifdef CONFIG_SMP
2926/**
2927 * __alloc_percpu - allocate one copy of the object for every present
2928 * cpu in the system, zeroing them.
2929 * Objects should be dereferenced using the per_cpu_ptr macro only.
2930 *
2931 * @size: how many bytes of memory are required.
2932 * @align: the alignment, which can't be greater than SMP_CACHE_BYTES.
2933 */
2934void *__alloc_percpu(size_t size, size_t align)
2935{
2936 int i;
2937 struct percpu_data *pdata = kmalloc(sizeof (*pdata), GFP_KERNEL);
2938
2939 if (!pdata)
2940 return NULL;
2941
Christoph Lametere498be72005-09-09 13:03:32 -07002942 /*
2943 * Cannot use for_each_online_cpu since a cpu may come online
2944 * and we have no way of figuring out how to fix the array
2945 * that we have allocated then....
2946 */
2947 for_each_cpu(i) {
2948 int node = cpu_to_node(i);
2949
2950 if (node_online(node))
2951 pdata->ptrs[i] = kmalloc_node(size, GFP_KERNEL, node);
2952 else
2953 pdata->ptrs[i] = kmalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954
2955 if (!pdata->ptrs[i])
2956 goto unwind_oom;
2957 memset(pdata->ptrs[i], 0, size);
2958 }
2959
2960 /* Catch derefs w/o wrappers */
2961 return (void *) (~(unsigned long) pdata);
2962
2963unwind_oom:
2964 while (--i >= 0) {
2965 if (!cpu_possible(i))
2966 continue;
2967 kfree(pdata->ptrs[i]);
2968 }
2969 kfree(pdata);
2970 return NULL;
2971}
2972EXPORT_SYMBOL(__alloc_percpu);
2973#endif
2974
2975/**
2976 * kmem_cache_free - Deallocate an object
2977 * @cachep: The cache the allocation was from.
2978 * @objp: The previously allocated object.
2979 *
2980 * Free an object which was previously allocated from this
2981 * cache.
2982 */
2983void kmem_cache_free(kmem_cache_t *cachep, void *objp)
2984{
2985 unsigned long flags;
2986
2987 local_irq_save(flags);
2988 __cache_free(cachep, objp);
2989 local_irq_restore(flags);
2990}
2991EXPORT_SYMBOL(kmem_cache_free);
2992
2993/**
Pekka J Enbergdd392712005-09-06 15:18:31 -07002994 * kzalloc - allocate memory. The memory is set to zero.
2995 * @size: how many bytes of memory are required.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 * @flags: the type of memory to allocate.
2997 */
Al Virodd0fc662005-10-07 07:46:04 +01002998void *kzalloc(size_t size, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
Pekka J Enbergdd392712005-09-06 15:18:31 -07003000 void *ret = kmalloc(size, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 if (ret)
Pekka J Enbergdd392712005-09-06 15:18:31 -07003002 memset(ret, 0, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 return ret;
3004}
Pekka J Enbergdd392712005-09-06 15:18:31 -07003005EXPORT_SYMBOL(kzalloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
3007/**
3008 * kfree - free previously allocated memory
3009 * @objp: pointer returned by kmalloc.
3010 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07003011 * If @objp is NULL, no operation is performed.
3012 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 * Don't free memory not originally allocated by kmalloc()
3014 * or you will run into trouble.
3015 */
3016void kfree(const void *objp)
3017{
3018 kmem_cache_t *c;
3019 unsigned long flags;
3020
3021 if (unlikely(!objp))
3022 return;
3023 local_irq_save(flags);
3024 kfree_debugcheck(objp);
3025 c = GET_PAGE_CACHE(virt_to_page(objp));
3026 __cache_free(c, (void*)objp);
3027 local_irq_restore(flags);
3028}
3029EXPORT_SYMBOL(kfree);
3030
3031#ifdef CONFIG_SMP
3032/**
3033 * free_percpu - free previously allocated percpu memory
3034 * @objp: pointer returned by alloc_percpu.
3035 *
3036 * Don't free memory not originally allocated by alloc_percpu()
3037 * The complemented objp is to check for that.
3038 */
3039void
3040free_percpu(const void *objp)
3041{
3042 int i;
3043 struct percpu_data *p = (struct percpu_data *) (~(unsigned long) objp);
3044
Christoph Lametere498be72005-09-09 13:03:32 -07003045 /*
3046 * We allocate for all cpus so we cannot use for online cpu here.
3047 */
3048 for_each_cpu(i)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 kfree(p->ptrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 kfree(p);
3051}
3052EXPORT_SYMBOL(free_percpu);
3053#endif
3054
3055unsigned int kmem_cache_size(kmem_cache_t *cachep)
3056{
3057 return obj_reallen(cachep);
3058}
3059EXPORT_SYMBOL(kmem_cache_size);
3060
Arnaldo Carvalho de Melo19449722005-06-18 22:46:19 -07003061const char *kmem_cache_name(kmem_cache_t *cachep)
3062{
3063 return cachep->name;
3064}
3065EXPORT_SYMBOL_GPL(kmem_cache_name);
3066
Christoph Lametere498be72005-09-09 13:03:32 -07003067/*
3068 * This initializes kmem_list3 for all nodes.
3069 */
3070static int alloc_kmemlist(kmem_cache_t *cachep)
3071{
3072 int node;
3073 struct kmem_list3 *l3;
3074 int err = 0;
3075
3076 for_each_online_node(node) {
3077 struct array_cache *nc = NULL, *new;
3078 struct array_cache **new_alien = NULL;
3079#ifdef CONFIG_NUMA
3080 if (!(new_alien = alloc_alien_cache(node, cachep->limit)))
3081 goto fail;
3082#endif
3083 if (!(new = alloc_arraycache(node, (cachep->shared*
3084 cachep->batchcount), 0xbaadf00d)))
3085 goto fail;
3086 if ((l3 = cachep->nodelists[node])) {
3087
3088 spin_lock_irq(&l3->list_lock);
3089
3090 if ((nc = cachep->nodelists[node]->shared))
3091 free_block(cachep, nc->entry,
Christoph Lameterff694162005-09-22 21:44:02 -07003092 nc->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07003093
3094 l3->shared = new;
3095 if (!cachep->nodelists[node]->alien) {
3096 l3->alien = new_alien;
3097 new_alien = NULL;
3098 }
3099 l3->free_limit = (1 + nr_cpus_node(node))*
3100 cachep->batchcount + cachep->num;
3101 spin_unlock_irq(&l3->list_lock);
3102 kfree(nc);
3103 free_alien_cache(new_alien);
3104 continue;
3105 }
3106 if (!(l3 = kmalloc_node(sizeof(struct kmem_list3),
3107 GFP_KERNEL, node)))
3108 goto fail;
3109
3110 kmem_list3_init(l3);
3111 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
3112 ((unsigned long)cachep)%REAPTIMEOUT_LIST3;
3113 l3->shared = new;
3114 l3->alien = new_alien;
3115 l3->free_limit = (1 + nr_cpus_node(node))*
3116 cachep->batchcount + cachep->num;
3117 cachep->nodelists[node] = l3;
3118 }
3119 return err;
3120fail:
3121 err = -ENOMEM;
3122 return err;
3123}
3124
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125struct ccupdate_struct {
3126 kmem_cache_t *cachep;
3127 struct array_cache *new[NR_CPUS];
3128};
3129
3130static void do_ccupdate_local(void *info)
3131{
3132 struct ccupdate_struct *new = (struct ccupdate_struct *)info;
3133 struct array_cache *old;
3134
3135 check_irq_off();
3136 old = ac_data(new->cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
3139 new->new[smp_processor_id()] = old;
3140}
3141
3142
3143static int do_tune_cpucache(kmem_cache_t *cachep, int limit, int batchcount,
3144 int shared)
3145{
3146 struct ccupdate_struct new;
Christoph Lametere498be72005-09-09 13:03:32 -07003147 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
3149 memset(&new.new,0,sizeof(new.new));
Christoph Lametere498be72005-09-09 13:03:32 -07003150 for_each_online_cpu(i) {
3151 new.new[i] = alloc_arraycache(cpu_to_node(i), limit, batchcount);
3152 if (!new.new[i]) {
3153 for (i--; i >= 0; i--) kfree(new.new[i]);
3154 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 }
3156 }
3157 new.cachep = cachep;
3158
3159 smp_call_function_all_cpus(do_ccupdate_local, (void *)&new);
Christoph Lametere498be72005-09-09 13:03:32 -07003160
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 check_irq_on();
3162 spin_lock_irq(&cachep->spinlock);
3163 cachep->batchcount = batchcount;
3164 cachep->limit = limit;
Christoph Lametere498be72005-09-09 13:03:32 -07003165 cachep->shared = shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 spin_unlock_irq(&cachep->spinlock);
3167
Christoph Lametere498be72005-09-09 13:03:32 -07003168 for_each_online_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 struct array_cache *ccold = new.new[i];
3170 if (!ccold)
3171 continue;
Christoph Lametere498be72005-09-09 13:03:32 -07003172 spin_lock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
Christoph Lameterff694162005-09-22 21:44:02 -07003173 free_block(cachep, ccold->entry, ccold->avail, cpu_to_node(i));
Christoph Lametere498be72005-09-09 13:03:32 -07003174 spin_unlock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175 kfree(ccold);
3176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Christoph Lametere498be72005-09-09 13:03:32 -07003178 err = alloc_kmemlist(cachep);
3179 if (err) {
3180 printk(KERN_ERR "alloc_kmemlist failed for %s, error %d.\n",
3181 cachep->name, -err);
3182 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 return 0;
3185}
3186
3187
3188static void enable_cpucache(kmem_cache_t *cachep)
3189{
3190 int err;
3191 int limit, shared;
3192
3193 /* The head array serves three purposes:
3194 * - create a LIFO ordering, i.e. return objects that are cache-warm
3195 * - reduce the number of spinlock operations.
3196 * - reduce the number of linked list operations on the slab and
3197 * bufctl chains: array operations are cheaper.
3198 * The numbers are guessed, we should auto-tune as described by
3199 * Bonwick.
3200 */
3201 if (cachep->objsize > 131072)
3202 limit = 1;
3203 else if (cachep->objsize > PAGE_SIZE)
3204 limit = 8;
3205 else if (cachep->objsize > 1024)
3206 limit = 24;
3207 else if (cachep->objsize > 256)
3208 limit = 54;
3209 else
3210 limit = 120;
3211
3212 /* Cpu bound tasks (e.g. network routing) can exhibit cpu bound
3213 * allocation behaviour: Most allocs on one cpu, most free operations
3214 * on another cpu. For these cases, an efficient object passing between
3215 * cpus is necessary. This is provided by a shared array. The array
3216 * replaces Bonwick's magazine layer.
3217 * On uniprocessor, it's functionally equivalent (but less efficient)
3218 * to a larger limit. Thus disabled by default.
3219 */
3220 shared = 0;
3221#ifdef CONFIG_SMP
3222 if (cachep->objsize <= PAGE_SIZE)
3223 shared = 8;
3224#endif
3225
3226#if DEBUG
3227 /* With debugging enabled, large batchcount lead to excessively
3228 * long periods with disabled local interrupts. Limit the
3229 * batchcount
3230 */
3231 if (limit > 32)
3232 limit = 32;
3233#endif
3234 err = do_tune_cpucache(cachep, limit, (limit+1)/2, shared);
3235 if (err)
3236 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
3237 cachep->name, -err);
3238}
3239
3240static void drain_array_locked(kmem_cache_t *cachep,
Christoph Lametere498be72005-09-09 13:03:32 -07003241 struct array_cache *ac, int force, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242{
3243 int tofree;
3244
Christoph Lametere498be72005-09-09 13:03:32 -07003245 check_spinlock_acquired_node(cachep, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 if (ac->touched && !force) {
3247 ac->touched = 0;
3248 } else if (ac->avail) {
3249 tofree = force ? ac->avail : (ac->limit+4)/5;
3250 if (tofree > ac->avail) {
3251 tofree = (ac->avail+1)/2;
3252 }
Christoph Lameterff694162005-09-22 21:44:02 -07003253 free_block(cachep, ac->entry, tofree, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 ac->avail -= tofree;
Christoph Lametere498be72005-09-09 13:03:32 -07003255 memmove(ac->entry, &(ac->entry[tofree]),
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 sizeof(void*)*ac->avail);
3257 }
3258}
3259
3260/**
3261 * cache_reap - Reclaim memory from caches.
3262 *
3263 * Called from workqueue/eventd every few seconds.
3264 * Purpose:
3265 * - clear the per-cpu caches for this CPU.
3266 * - return freeable pages to the main free memory pool.
3267 *
3268 * If we cannot acquire the cache chain semaphore then just give up - we'll
3269 * try again on the next iteration.
3270 */
3271static void cache_reap(void *unused)
3272{
3273 struct list_head *walk;
Christoph Lametere498be72005-09-09 13:03:32 -07003274 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275
3276 if (down_trylock(&cache_chain_sem)) {
3277 /* Give up. Setup the next iteration. */
3278 schedule_delayed_work(&__get_cpu_var(reap_work), REAPTIMEOUT_CPUC + smp_processor_id());
3279 return;
3280 }
3281
3282 list_for_each(walk, &cache_chain) {
3283 kmem_cache_t *searchp;
3284 struct list_head* p;
3285 int tofree;
3286 struct slab *slabp;
3287
3288 searchp = list_entry(walk, kmem_cache_t, next);
3289
3290 if (searchp->flags & SLAB_NO_REAP)
3291 goto next;
3292
3293 check_irq_on();
3294
Christoph Lametere498be72005-09-09 13:03:32 -07003295 l3 = searchp->nodelists[numa_node_id()];
3296 if (l3->alien)
3297 drain_alien_cache(searchp, l3);
3298 spin_lock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299
Christoph Lametere498be72005-09-09 13:03:32 -07003300 drain_array_locked(searchp, ac_data(searchp), 0,
3301 numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
Christoph Lametere498be72005-09-09 13:03:32 -07003303 if (time_after(l3->next_reap, jiffies))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 goto next_unlock;
3305
Christoph Lametere498be72005-09-09 13:03:32 -07003306 l3->next_reap = jiffies + REAPTIMEOUT_LIST3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307
Christoph Lametere498be72005-09-09 13:03:32 -07003308 if (l3->shared)
3309 drain_array_locked(searchp, l3->shared, 0,
3310 numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311
Christoph Lametere498be72005-09-09 13:03:32 -07003312 if (l3->free_touched) {
3313 l3->free_touched = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 goto next_unlock;
3315 }
3316
Christoph Lametere498be72005-09-09 13:03:32 -07003317 tofree = (l3->free_limit+5*searchp->num-1)/(5*searchp->num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 do {
Christoph Lametere498be72005-09-09 13:03:32 -07003319 p = l3->slabs_free.next;
3320 if (p == &(l3->slabs_free))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321 break;
3322
3323 slabp = list_entry(p, struct slab, list);
3324 BUG_ON(slabp->inuse);
3325 list_del(&slabp->list);
3326 STATS_INC_REAPED(searchp);
3327
3328 /* Safe to drop the lock. The slab is no longer
3329 * linked to the cache.
3330 * searchp cannot disappear, we hold
3331 * cache_chain_lock
3332 */
Christoph Lametere498be72005-09-09 13:03:32 -07003333 l3->free_objects -= searchp->num;
3334 spin_unlock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 slab_destroy(searchp, slabp);
Christoph Lametere498be72005-09-09 13:03:32 -07003336 spin_lock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337 } while(--tofree > 0);
3338next_unlock:
Christoph Lametere498be72005-09-09 13:03:32 -07003339 spin_unlock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340next:
3341 cond_resched();
3342 }
3343 check_irq_on();
3344 up(&cache_chain_sem);
Christoph Lameter4ae7c032005-06-21 17:14:57 -07003345 drain_remote_pages();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 /* Setup the next iteration */
3347 schedule_delayed_work(&__get_cpu_var(reap_work), REAPTIMEOUT_CPUC + smp_processor_id());
3348}
3349
3350#ifdef CONFIG_PROC_FS
3351
3352static void *s_start(struct seq_file *m, loff_t *pos)
3353{
3354 loff_t n = *pos;
3355 struct list_head *p;
3356
3357 down(&cache_chain_sem);
3358 if (!n) {
3359 /*
3360 * Output format version, so at least we can change it
3361 * without _too_ many complaints.
3362 */
3363#if STATS
3364 seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
3365#else
3366 seq_puts(m, "slabinfo - version: 2.1\n");
3367#endif
3368 seq_puts(m, "# name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab>");
3369 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
3370 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
3371#if STATS
3372 seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped>"
Christoph Lametere498be72005-09-09 13:03:32 -07003373 " <error> <maxfreeable> <nodeallocs> <remotefrees>");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
3375#endif
3376 seq_putc(m, '\n');
3377 }
3378 p = cache_chain.next;
3379 while (n--) {
3380 p = p->next;
3381 if (p == &cache_chain)
3382 return NULL;
3383 }
3384 return list_entry(p, kmem_cache_t, next);
3385}
3386
3387static void *s_next(struct seq_file *m, void *p, loff_t *pos)
3388{
3389 kmem_cache_t *cachep = p;
3390 ++*pos;
3391 return cachep->next.next == &cache_chain ? NULL
3392 : list_entry(cachep->next.next, kmem_cache_t, next);
3393}
3394
3395static void s_stop(struct seq_file *m, void *p)
3396{
3397 up(&cache_chain_sem);
3398}
3399
3400static int s_show(struct seq_file *m, void *p)
3401{
3402 kmem_cache_t *cachep = p;
3403 struct list_head *q;
3404 struct slab *slabp;
3405 unsigned long active_objs;
3406 unsigned long num_objs;
3407 unsigned long active_slabs = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07003408 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
3409 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410 char *error = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003411 int node;
3412 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
3414 check_irq_on();
3415 spin_lock_irq(&cachep->spinlock);
3416 active_objs = 0;
3417 num_slabs = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07003418 for_each_online_node(node) {
3419 l3 = cachep->nodelists[node];
3420 if (!l3)
3421 continue;
3422
3423 spin_lock(&l3->list_lock);
3424
3425 list_for_each(q,&l3->slabs_full) {
3426 slabp = list_entry(q, struct slab, list);
3427 if (slabp->inuse != cachep->num && !error)
3428 error = "slabs_full accounting error";
3429 active_objs += cachep->num;
3430 active_slabs++;
3431 }
3432 list_for_each(q,&l3->slabs_partial) {
3433 slabp = list_entry(q, struct slab, list);
3434 if (slabp->inuse == cachep->num && !error)
3435 error = "slabs_partial inuse accounting error";
3436 if (!slabp->inuse && !error)
3437 error = "slabs_partial/inuse accounting error";
3438 active_objs += slabp->inuse;
3439 active_slabs++;
3440 }
3441 list_for_each(q,&l3->slabs_free) {
3442 slabp = list_entry(q, struct slab, list);
3443 if (slabp->inuse && !error)
3444 error = "slabs_free/inuse accounting error";
3445 num_slabs++;
3446 }
3447 free_objects += l3->free_objects;
3448 shared_avail += l3->shared->avail;
3449
3450 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 }
3452 num_slabs+=active_slabs;
3453 num_objs = num_slabs*cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07003454 if (num_objs - active_objs != free_objects && !error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 error = "free_objects accounting error";
3456
3457 name = cachep->name;
3458 if (error)
3459 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
3460
3461 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
3462 name, active_objs, num_objs, cachep->objsize,
3463 cachep->num, (1<<cachep->gfporder));
3464 seq_printf(m, " : tunables %4u %4u %4u",
3465 cachep->limit, cachep->batchcount,
Christoph Lametere498be72005-09-09 13:03:32 -07003466 cachep->shared);
3467 seq_printf(m, " : slabdata %6lu %6lu %6lu",
3468 active_slabs, num_slabs, shared_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469#if STATS
3470 { /* list3 stats */
3471 unsigned long high = cachep->high_mark;
3472 unsigned long allocs = cachep->num_allocations;
3473 unsigned long grown = cachep->grown;
3474 unsigned long reaped = cachep->reaped;
3475 unsigned long errors = cachep->errors;
3476 unsigned long max_freeable = cachep->max_freeable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 unsigned long node_allocs = cachep->node_allocs;
Christoph Lametere498be72005-09-09 13:03:32 -07003478 unsigned long node_frees = cachep->node_frees;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479
Christoph Lametere498be72005-09-09 13:03:32 -07003480 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu \
3481 %4lu %4lu %4lu %4lu",
3482 allocs, high, grown, reaped, errors,
3483 max_freeable, node_allocs, node_frees);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 }
3485 /* cpu stats */
3486 {
3487 unsigned long allochit = atomic_read(&cachep->allochit);
3488 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
3489 unsigned long freehit = atomic_read(&cachep->freehit);
3490 unsigned long freemiss = atomic_read(&cachep->freemiss);
3491
3492 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
3493 allochit, allocmiss, freehit, freemiss);
3494 }
3495#endif
3496 seq_putc(m, '\n');
3497 spin_unlock_irq(&cachep->spinlock);
3498 return 0;
3499}
3500
3501/*
3502 * slabinfo_op - iterator that generates /proc/slabinfo
3503 *
3504 * Output layout:
3505 * cache-name
3506 * num-active-objs
3507 * total-objs
3508 * object size
3509 * num-active-slabs
3510 * total-slabs
3511 * num-pages-per-slab
3512 * + further values on SMP and with statistics enabled
3513 */
3514
3515struct seq_operations slabinfo_op = {
3516 .start = s_start,
3517 .next = s_next,
3518 .stop = s_stop,
3519 .show = s_show,
3520};
3521
3522#define MAX_SLABINFO_WRITE 128
3523/**
3524 * slabinfo_write - Tuning for the slab allocator
3525 * @file: unused
3526 * @buffer: user buffer
3527 * @count: data length
3528 * @ppos: unused
3529 */
3530ssize_t slabinfo_write(struct file *file, const char __user *buffer,
3531 size_t count, loff_t *ppos)
3532{
3533 char kbuf[MAX_SLABINFO_WRITE+1], *tmp;
3534 int limit, batchcount, shared, res;
3535 struct list_head *p;
3536
3537 if (count > MAX_SLABINFO_WRITE)
3538 return -EINVAL;
3539 if (copy_from_user(&kbuf, buffer, count))
3540 return -EFAULT;
3541 kbuf[MAX_SLABINFO_WRITE] = '\0';
3542
3543 tmp = strchr(kbuf, ' ');
3544 if (!tmp)
3545 return -EINVAL;
3546 *tmp = '\0';
3547 tmp++;
3548 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
3549 return -EINVAL;
3550
3551 /* Find the cache in the chain of caches. */
3552 down(&cache_chain_sem);
3553 res = -EINVAL;
3554 list_for_each(p,&cache_chain) {
3555 kmem_cache_t *cachep = list_entry(p, kmem_cache_t, next);
3556
3557 if (!strcmp(cachep->name, kbuf)) {
3558 if (limit < 1 ||
3559 batchcount < 1 ||
3560 batchcount > limit ||
3561 shared < 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07003562 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07003564 res = do_tune_cpucache(cachep, limit,
3565 batchcount, shared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 }
3567 break;
3568 }
3569 }
3570 up(&cache_chain_sem);
3571 if (res >= 0)
3572 res = count;
3573 return res;
3574}
3575#endif
3576
Manfred Spraul00e145b2005-09-03 15:55:07 -07003577/**
3578 * ksize - get the actual amount of memory allocated for a given object
3579 * @objp: Pointer to the object
3580 *
3581 * kmalloc may internally round up allocations and return more memory
3582 * than requested. ksize() can be used to determine the actual amount of
3583 * memory allocated. The caller may use this additional memory, even though
3584 * a smaller amount of memory was initially specified with the kmalloc call.
3585 * The caller must guarantee that objp points to a valid object previously
3586 * allocated with either kmalloc() or kmem_cache_alloc(). The object
3587 * must not be freed during the duration of the call.
3588 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589unsigned int ksize(const void *objp)
3590{
Manfred Spraul00e145b2005-09-03 15:55:07 -07003591 if (unlikely(objp == NULL))
3592 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
Manfred Spraul00e145b2005-09-03 15:55:07 -07003594 return obj_reallen(GET_PAGE_CACHE(virt_to_page(objp)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595}
Paulo Marques543537b2005-06-23 00:09:02 -07003596
3597
3598/*
3599 * kstrdup - allocate space for and copy an existing string
3600 *
3601 * @s: the string to duplicate
3602 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
3603 */
Al Virodd0fc662005-10-07 07:46:04 +01003604char *kstrdup(const char *s, gfp_t gfp)
Paulo Marques543537b2005-06-23 00:09:02 -07003605{
3606 size_t len;
3607 char *buf;
3608
3609 if (!s)
3610 return NULL;
3611
3612 len = strlen(s) + 1;
3613 buf = kmalloc(len, gfp);
3614 if (buf)
3615 memcpy(buf, s, len);
3616 return buf;
3617}
3618EXPORT_SYMBOL(kstrdup);