3 * Written by Mark Hemment, 1996/97.
4 * (markhe@nextd.demon.co.uk)
6 * kmem_cache_destroy() + some cleanup - 1999 Andrea Arcangeli
8 * Major cleanup, different bufctl logic, per-cpu arrays
9 * (c) 2000 Manfred Spraul
11 * Cleanup, make the head arrays unconditional, preparation for NUMA
12 * (c) 2002 Manfred Spraul
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
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.
28 * This means, that your constructor is used only for newly allocated
29 * slabs and you must pass objects with the same intializations to
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.
36 * In order to reduce fragmentation, the slabs are sorted in 3 groups:
37 * full slabs with 0 free objects
39 * empty slabs with no allocated objects
41 * If partial slabs exist, then new allocations come from these slabs,
42 * otherwise from empty slabs or new slabs are allocated.
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.
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.
53 * The c_cpuarray may not be read with enabled local interrupts -
54 * it's changed with a smp_call_function().
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.
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
68 * Further notes from the original documentation:
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()).
76 * At present, each engine can be growing a cache. This should be blocked.
78 * 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>
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.
89 #include <linux/config.h>
90 #include <linux/slab.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>
104 #include <linux/string.h>
105 #include <linux/nodemask.h>
107 #include <asm/uaccess.h>
108 #include <asm/cacheflush.h>
109 #include <asm/tlbflush.h>
110 #include <asm/page.h>
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).
117 * STATS - 1 to collect stats for /proc/slabinfo.
118 * 0 for faster, smaller code (especially in the critical paths).
120 * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
123 #ifdef CONFIG_DEBUG_SLAB
126 #define FORCED_DEBUG 1
130 #define FORCED_DEBUG 0
134 /* Shouldn't this be in a header file somewhere? */
135 #define BYTES_PER_WORD sizeof(void *)
137 #ifndef cache_line_size
138 #define cache_line_size() L1_CACHE_BYTES
141 #ifndef ARCH_KMALLOC_MINALIGN
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.
150 #define ARCH_KMALLOC_MINALIGN 0
153 #ifndef ARCH_SLAB_MINALIGN
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.
161 #define ARCH_SLAB_MINALIGN 0
164 #ifndef ARCH_KMALLOC_FLAGS
165 #define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
168 /* Legal flag mask for kmem_cache_create(). */
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 | \
177 # define CREATE_MASK (SLAB_HWCACHE_ALIGN | SLAB_NO_REAP | \
178 SLAB_CACHE_DMA | SLAB_MUST_HWCACHE_ALIGN | \
179 SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
186 * Bufctl's are used for linking objs within a slab
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.
202 typedef unsigned int kmem_bufctl_t
;
203 #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)
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().
210 static unsigned long offslab_limit
;
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.
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 */
225 unsigned short nodeid
;
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).
239 * rcu_read_lock before reading the address, then rcu_read_unlock after
240 * taking the spinlock within the structure expected at that address.
242 * We assume struct slab_rcu can overlay struct slab when destroying.
245 struct rcu_head head
;
246 kmem_cache_t
*cachep
;
254 * - LIFO ordering, to hand out cache-warm objects from _alloc
255 * - reduce the number of linked list operations
256 * - reduce spinlock operations
258 * The limit is stored in the per-cpu structure to reduce the data cache
265 unsigned int batchcount
;
266 unsigned int touched
;
269 * Must have this definition in here for the proper
270 * alignment of array_cache. Also simplifies accessing
272 * [0] is for gcc 2.95. It should really be [].
276 /* bootstrap: The caches do not work without cpuarrays anymore,
277 * but the cpuarrays are allocated from the generic caches...
279 #define BOOT_CPUCACHE_ENTRIES 1
280 struct arraycache_init
{
281 struct array_cache cache
;
282 void * entries
[BOOT_CPUCACHE_ENTRIES
];
286 * The slab lists for all objects.
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
;
293 unsigned long next_reap
;
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 */
302 * Need this for bootstrapping a per node allocator.
304 #define NUM_INIT_LISTS (2 * MAX_NUMNODES + 1)
305 struct kmem_list3 __initdata initkmem_list3
[NUM_INIT_LISTS
];
306 #define CACHE_CACHE 0
308 #define SIZE_L3 (1 + MAX_NUMNODES)
311 * This function must be completely optimized away if
312 * a constant is passed to it. Mostly the same as
313 * what is in linux/slab.h except it returns an
316 static __always_inline
int index_of(const size_t size
)
318 if (__builtin_constant_p(size
)) {
326 #include "linux/kmalloc_sizes.h"
329 extern void __bad_size(void);
337 #define INDEX_AC index_of(sizeof(struct arraycache_init))
338 #define INDEX_L3 index_of(sizeof(struct kmem_list3))
340 static inline void kmem_list3_init(struct kmem_list3
*parent
)
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;
352 #define MAKE_LIST(cachep, listp, slab, nodeid) \
354 INIT_LIST_HEAD(listp); \
355 list_splice(&(cachep->nodelists[nodeid]->slab), listp); \
358 #define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
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); \
372 /* 1) per-cpu data, touched during every alloc/free */
373 struct array_cache
*array
[NR_CPUS
];
374 unsigned int batchcount
;
377 unsigned int objsize
;
378 /* 2) touched by every alloc & free from the backend */
379 struct kmem_list3
*nodelists
[MAX_NUMNODES
];
380 unsigned int flags
; /* constant flags */
381 unsigned int num
; /* # of objs per slab */
384 /* 3) cache_grow/shrink */
385 /* order of pgs per slab (2^n) */
386 unsigned int gfporder
;
388 /* force GFP flags, e.g. GFP_DMA */
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 */
398 /* constructor func */
399 void (*ctor
)(void *, kmem_cache_t
*, unsigned long);
401 /* de-constructor func */
402 void (*dtor
)(void *, kmem_cache_t
*, unsigned long);
404 /* 4) cache creation/removal */
406 struct list_head next
;
410 unsigned long num_active
;
411 unsigned long num_allocations
;
412 unsigned long high_mark
;
414 unsigned long reaped
;
415 unsigned long errors
;
416 unsigned long max_freeable
;
417 unsigned long node_allocs
;
418 unsigned long node_frees
;
430 #define CFLGS_OFF_SLAB (0x80000000UL)
431 #define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
433 #define BATCHREFILL_LIMIT 16
434 /* Optimization question: fewer reaps means less
435 * probability for unnessary cpucache drain/refill cycles.
437 * OTOH the cpuarrays can contain lots of objects,
438 * which could lock up otherwise freeable slabs.
440 #define REAPTIMEOUT_CPUC (2*HZ)
441 #define REAPTIMEOUT_LIST3 (4*HZ)
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; \
452 #define STATS_INC_ERR(x) ((x)->errors++)
453 #define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
454 #define STATS_INC_NODEFREES(x) ((x)->node_frees++)
455 #define STATS_SET_FREEABLE(x, i) \
456 do { if ((x)->max_freeable < i) \
457 (x)->max_freeable = i; \
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)
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)
473 #define STATS_INC_NODEFREES(x) do { } while (0)
474 #define STATS_SET_FREEABLE(x, i) \
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)
484 /* Magic nums for obj red zoning.
485 * Placed in the first word before and the first word after an obj.
487 #define RED_INACTIVE 0x5A2CF071UL /* when obj is inactive */
488 #define RED_ACTIVE 0x170FC2A5UL /* when obj is active */
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 */
495 /* memory layout of objects:
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:
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]
506 static int obj_dbghead(kmem_cache_t
*cachep
)
508 return cachep
->dbghead
;
511 static int obj_reallen(kmem_cache_t
*cachep
)
513 return cachep
->reallen
;
516 static unsigned long *dbg_redzone1(kmem_cache_t
*cachep
, void *objp
)
518 BUG_ON(!(cachep
->flags
& SLAB_RED_ZONE
));
519 return (unsigned long*) (objp
+obj_dbghead(cachep
)-BYTES_PER_WORD
);
522 static unsigned long *dbg_redzone2(kmem_cache_t
*cachep
, void *objp
)
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
);
530 static void **dbg_userword(kmem_cache_t
*cachep
, void *objp
)
532 BUG_ON(!(cachep
->flags
& SLAB_STORE_USER
));
533 return (void**)(objp
+cachep
->objsize
-BYTES_PER_WORD
);
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;})
547 * Maximum size of an obj (in 2^order pages)
548 * and absolute limit for the gfp order.
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 */
557 #define MAX_OBJ_ORDER 8 /* up to 1Mb */
558 #define MAX_GFP_ORDER 8 /* up to 1Mb */
562 * Do not go above this order unless 0 objects fit into the slab.
564 #define BREAK_GFP_ORDER_HI 1
565 #define BREAK_GFP_ORDER_LO 0
566 static int slab_break_gfp_order
= BREAK_GFP_ORDER_LO
;
568 /* Functions 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.
572 static inline void page_set_cache(struct page
*page
, struct kmem_cache
*cache
)
574 page
->lru
.next
= (struct list_head
*)cache
;
577 static inline struct kmem_cache
*page_get_cache(struct page
*page
)
579 return (struct kmem_cache
*)page
->lru
.next
;
582 static inline void page_set_slab(struct page
*page
, struct slab
*slab
)
584 page
->lru
.prev
= (struct list_head
*)slab
;
587 static inline struct slab
*page_get_slab(struct page
*page
)
589 return (struct slab
*)page
->lru
.prev
;
592 /* These are the default caches for kmalloc. Custom caches can have other sizes. */
593 struct cache_sizes malloc_sizes
[] = {
594 #define CACHE(x) { .cs_size = (x) },
595 #include <linux/kmalloc_sizes.h>
599 EXPORT_SYMBOL(malloc_sizes
);
601 /* Must match cache_sizes above. Out of line to keep cache footprint low. */
607 static struct cache_names __initdata cache_names
[] = {
608 #define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },
609 #include <linux/kmalloc_sizes.h>
614 static struct arraycache_init initarray_cache __initdata
=
615 { { 0, BOOT_CPUCACHE_ENTRIES
, 1, 0} };
616 static struct arraycache_init initarray_generic
=
617 { { 0, BOOT_CPUCACHE_ENTRIES
, 1, 0} };
619 /* internal cache of cache description objs */
620 static kmem_cache_t cache_cache
= {
622 .limit
= BOOT_CPUCACHE_ENTRIES
,
624 .objsize
= sizeof(kmem_cache_t
),
625 .flags
= SLAB_NO_REAP
,
626 .spinlock
= SPIN_LOCK_UNLOCKED
,
627 .name
= "kmem_cache",
629 .reallen
= sizeof(kmem_cache_t
),
633 /* Guard access to the cache-chain. */
634 static struct semaphore cache_chain_sem
;
635 static struct list_head cache_chain
;
638 * vm_enough_memory() looks at this to determine how many
639 * slab-allocated pages are possibly freeable under pressure
641 * SLAB_RECLAIM_ACCOUNT turns this on per-slab
643 atomic_t slab_reclaim_pages
;
646 * chicken and egg problem: delay the per-cpu array allocation
647 * until the general caches are up.
656 static DEFINE_PER_CPU(struct work_struct
, reap_work
);
658 static void free_block(kmem_cache_t
* cachep
, void** objpp
, int len
, int node
);
659 static void enable_cpucache (kmem_cache_t
*cachep
);
660 static void cache_reap (void *unused
);
661 static int __node_shrink(kmem_cache_t
*cachep
, int node
);
663 static inline struct array_cache
*ac_data(kmem_cache_t
*cachep
)
665 return cachep
->array
[smp_processor_id()];
668 static inline kmem_cache_t
*__find_general_cachep(size_t size
, gfp_t gfpflags
)
670 struct cache_sizes
*csizep
= malloc_sizes
;
673 /* This happens if someone tries to call
674 * kmem_cache_create(), or __kmalloc(), before
675 * the generic caches are initialized.
677 BUG_ON(malloc_sizes
[INDEX_AC
].cs_cachep
== NULL
);
679 while (size
> csizep
->cs_size
)
683 * Really subtle: The last entry with cs->cs_size==ULONG_MAX
684 * has cs_{dma,}cachep==NULL. Thus no special case
685 * for large kmalloc calls required.
687 if (unlikely(gfpflags
& GFP_DMA
))
688 return csizep
->cs_dmacachep
;
689 return csizep
->cs_cachep
;
692 kmem_cache_t
*kmem_find_general_cachep(size_t size
, gfp_t gfpflags
)
694 return __find_general_cachep(size
, gfpflags
);
696 EXPORT_SYMBOL(kmem_find_general_cachep
);
698 /* Cal the num objs, wastage, and bytes left over for a given slab size. */
699 static void cache_estimate(unsigned long gfporder
, size_t size
, size_t align
,
700 int flags
, size_t *left_over
, unsigned int *num
)
703 size_t wastage
= PAGE_SIZE
<<gfporder
;
707 if (!(flags
& CFLGS_OFF_SLAB
)) {
708 base
= sizeof(struct slab
);
709 extra
= sizeof(kmem_bufctl_t
);
712 while (i
*size
+ ALIGN(base
+i
*extra
, align
) <= wastage
)
722 wastage
-= ALIGN(base
+i
*extra
, align
);
723 *left_over
= wastage
;
726 #define slab_error(cachep, msg) __slab_error(__FUNCTION__, cachep, msg)
728 static void __slab_error(const char *function
, kmem_cache_t
*cachep
, char *msg
)
730 printk(KERN_ERR
"slab error in %s(): cache `%s': %s\n",
731 function
, cachep
->name
, msg
);
736 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
737 * via the workqueue/eventd.
738 * Add the CPU number into the expiration time to minimize the possibility of
739 * the CPUs getting into lockstep and contending for the global cache chain
742 static void __devinit
start_cpu_timer(int cpu
)
744 struct work_struct
*reap_work
= &per_cpu(reap_work
, cpu
);
747 * When this gets called from do_initcalls via cpucache_init(),
748 * init_workqueues() has already run, so keventd will be setup
751 if (keventd_up() && reap_work
->func
== NULL
) {
752 INIT_WORK(reap_work
, cache_reap
, NULL
);
753 schedule_delayed_work_on(cpu
, reap_work
, HZ
+ 3 * cpu
);
757 static struct array_cache
*alloc_arraycache(int node
, int entries
,
760 int memsize
= sizeof(void*)*entries
+sizeof(struct array_cache
);
761 struct array_cache
*nc
= NULL
;
763 nc
= kmalloc_node(memsize
, GFP_KERNEL
, node
);
767 nc
->batchcount
= batchcount
;
769 spin_lock_init(&nc
->lock
);
775 static inline struct array_cache
**alloc_alien_cache(int node
, int limit
)
777 struct array_cache
**ac_ptr
;
778 int memsize
= sizeof(void*)*MAX_NUMNODES
;
783 ac_ptr
= kmalloc_node(memsize
, GFP_KERNEL
, node
);
786 if (i
== node
|| !node_online(i
)) {
790 ac_ptr
[i
] = alloc_arraycache(node
, limit
, 0xbaadf00d);
792 for (i
--; i
<=0; i
--)
802 static inline void free_alien_cache(struct array_cache
**ac_ptr
)
815 static inline void __drain_alien_cache(kmem_cache_t
*cachep
, struct array_cache
*ac
, int node
)
817 struct kmem_list3
*rl3
= cachep
->nodelists
[node
];
820 spin_lock(&rl3
->list_lock
);
821 free_block(cachep
, ac
->entry
, ac
->avail
, node
);
823 spin_unlock(&rl3
->list_lock
);
827 static void drain_alien_cache(kmem_cache_t
*cachep
, struct kmem_list3
*l3
)
830 struct array_cache
*ac
;
833 for_each_online_node(i
) {
836 spin_lock_irqsave(&ac
->lock
, flags
);
837 __drain_alien_cache(cachep
, ac
, i
);
838 spin_unlock_irqrestore(&ac
->lock
, flags
);
843 #define alloc_alien_cache(node, limit) do { } while (0)
844 #define free_alien_cache(ac_ptr) do { } while (0)
845 #define drain_alien_cache(cachep, l3) do { } while (0)
848 static int __devinit
cpuup_callback(struct notifier_block
*nfb
,
849 unsigned long action
, void *hcpu
)
851 long cpu
= (long)hcpu
;
852 kmem_cache_t
* cachep
;
853 struct kmem_list3
*l3
= NULL
;
854 int node
= cpu_to_node(cpu
);
855 int memsize
= sizeof(struct kmem_list3
);
856 struct array_cache
*nc
= NULL
;
860 down(&cache_chain_sem
);
861 /* we need to do this right in the beginning since
862 * alloc_arraycache's are going to use this list.
863 * kmalloc_node allows us to add the slab to the right
864 * kmem_list3 and not this cpu's kmem_list3
867 list_for_each_entry(cachep
, &cache_chain
, next
) {
868 /* setup the size64 kmemlist for cpu before we can
869 * begin anything. Make sure some other cpu on this
870 * node has not already allocated this
872 if (!cachep
->nodelists
[node
]) {
873 if (!(l3
= kmalloc_node(memsize
,
877 l3
->next_reap
= jiffies
+ REAPTIMEOUT_LIST3
+
878 ((unsigned long)cachep
)%REAPTIMEOUT_LIST3
;
880 cachep
->nodelists
[node
] = l3
;
883 spin_lock_irq(&cachep
->nodelists
[node
]->list_lock
);
884 cachep
->nodelists
[node
]->free_limit
=
885 (1 + nr_cpus_node(node
)) *
886 cachep
->batchcount
+ cachep
->num
;
887 spin_unlock_irq(&cachep
->nodelists
[node
]->list_lock
);
890 /* Now we can go ahead with allocating the shared array's
892 list_for_each_entry(cachep
, &cache_chain
, next
) {
893 nc
= alloc_arraycache(node
, cachep
->limit
,
897 cachep
->array
[cpu
] = nc
;
899 l3
= cachep
->nodelists
[node
];
902 if (!(nc
= alloc_arraycache(node
,
903 cachep
->shared
*cachep
->batchcount
,
907 /* we are serialised from CPU_DEAD or
908 CPU_UP_CANCELLED by the cpucontrol lock */
912 up(&cache_chain_sem
);
915 start_cpu_timer(cpu
);
917 #ifdef CONFIG_HOTPLUG_CPU
920 case CPU_UP_CANCELED
:
921 down(&cache_chain_sem
);
923 list_for_each_entry(cachep
, &cache_chain
, next
) {
924 struct array_cache
*nc
;
927 mask
= node_to_cpumask(node
);
928 spin_lock_irq(&cachep
->spinlock
);
929 /* cpu is dead; no one can alloc from it. */
930 nc
= cachep
->array
[cpu
];
931 cachep
->array
[cpu
] = NULL
;
932 l3
= cachep
->nodelists
[node
];
937 spin_lock(&l3
->list_lock
);
939 /* Free limit for this kmem_list3 */
940 l3
->free_limit
-= cachep
->batchcount
;
942 free_block(cachep
, nc
->entry
, nc
->avail
, node
);
944 if (!cpus_empty(mask
)) {
945 spin_unlock(&l3
->list_lock
);
950 free_block(cachep
, l3
->shared
->entry
,
951 l3
->shared
->avail
, node
);
956 drain_alien_cache(cachep
, l3
);
957 free_alien_cache(l3
->alien
);
961 /* free slabs belonging to this node */
962 if (__node_shrink(cachep
, node
)) {
963 cachep
->nodelists
[node
] = NULL
;
964 spin_unlock(&l3
->list_lock
);
967 spin_unlock(&l3
->list_lock
);
970 spin_unlock_irq(&cachep
->spinlock
);
973 up(&cache_chain_sem
);
979 up(&cache_chain_sem
);
983 static struct notifier_block cpucache_notifier
= { &cpuup_callback
, NULL
, 0 };
986 * swap the static kmem_list3 with kmalloced memory
988 static void init_list(kmem_cache_t
*cachep
, struct kmem_list3
*list
,
991 struct kmem_list3
*ptr
;
993 BUG_ON(cachep
->nodelists
[nodeid
] != list
);
994 ptr
= kmalloc_node(sizeof(struct kmem_list3
), GFP_KERNEL
, nodeid
);
998 memcpy(ptr
, list
, sizeof(struct kmem_list3
));
999 MAKE_ALL_LISTS(cachep
, ptr
, nodeid
);
1000 cachep
->nodelists
[nodeid
] = ptr
;
1005 * Called after the gfp() functions have been enabled, and before smp_init().
1007 void __init
kmem_cache_init(void)
1010 struct cache_sizes
*sizes
;
1011 struct cache_names
*names
;
1014 for (i
= 0; i
< NUM_INIT_LISTS
; i
++) {
1015 kmem_list3_init(&initkmem_list3
[i
]);
1016 if (i
< MAX_NUMNODES
)
1017 cache_cache
.nodelists
[i
] = NULL
;
1021 * Fragmentation resistance on low memory - only use bigger
1022 * page orders on machines with more than 32MB of memory.
1024 if (num_physpages
> (32 << 20) >> PAGE_SHIFT
)
1025 slab_break_gfp_order
= BREAK_GFP_ORDER_HI
;
1027 /* Bootstrap is tricky, because several objects are allocated
1028 * from caches that do not exist yet:
1029 * 1) initialize the cache_cache cache: it contains the kmem_cache_t
1030 * structures of all caches, except cache_cache itself: cache_cache
1031 * is statically allocated.
1032 * Initially an __init data area is used for the head array and the
1033 * kmem_list3 structures, it's replaced with a kmalloc allocated
1034 * array at the end of the bootstrap.
1035 * 2) Create the first kmalloc cache.
1036 * The kmem_cache_t for the new cache is allocated normally.
1037 * An __init data area is used for the head array.
1038 * 3) Create the remaining kmalloc caches, with minimally sized
1040 * 4) Replace the __init data head arrays for cache_cache and the first
1041 * kmalloc cache with kmalloc allocated arrays.
1042 * 5) Replace the __init data for kmem_list3 for cache_cache and
1043 * the other cache's with kmalloc allocated memory.
1044 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
1047 /* 1) create the cache_cache */
1048 init_MUTEX(&cache_chain_sem
);
1049 INIT_LIST_HEAD(&cache_chain
);
1050 list_add(&cache_cache
.next
, &cache_chain
);
1051 cache_cache
.colour_off
= cache_line_size();
1052 cache_cache
.array
[smp_processor_id()] = &initarray_cache
.cache
;
1053 cache_cache
.nodelists
[numa_node_id()] = &initkmem_list3
[CACHE_CACHE
];
1055 cache_cache
.objsize
= ALIGN(cache_cache
.objsize
, cache_line_size());
1057 cache_estimate(0, cache_cache
.objsize
, cache_line_size(), 0,
1058 &left_over
, &cache_cache
.num
);
1059 if (!cache_cache
.num
)
1062 cache_cache
.colour
= left_over
/cache_cache
.colour_off
;
1063 cache_cache
.colour_next
= 0;
1064 cache_cache
.slab_size
= ALIGN(cache_cache
.num
*sizeof(kmem_bufctl_t
) +
1065 sizeof(struct slab
), cache_line_size());
1067 /* 2+3) create the kmalloc caches */
1068 sizes
= malloc_sizes
;
1069 names
= cache_names
;
1071 /* Initialize the caches that provide memory for the array cache
1072 * and the kmem_list3 structures first.
1073 * Without this, further allocations will bug
1076 sizes
[INDEX_AC
].cs_cachep
= kmem_cache_create(names
[INDEX_AC
].name
,
1077 sizes
[INDEX_AC
].cs_size
, ARCH_KMALLOC_MINALIGN
,
1078 (ARCH_KMALLOC_FLAGS
| SLAB_PANIC
), NULL
, NULL
);
1080 if (INDEX_AC
!= INDEX_L3
)
1081 sizes
[INDEX_L3
].cs_cachep
=
1082 kmem_cache_create(names
[INDEX_L3
].name
,
1083 sizes
[INDEX_L3
].cs_size
, ARCH_KMALLOC_MINALIGN
,
1084 (ARCH_KMALLOC_FLAGS
| SLAB_PANIC
), NULL
, NULL
);
1086 while (sizes
->cs_size
!= ULONG_MAX
) {
1088 * For performance, all the general caches are L1 aligned.
1089 * This should be particularly beneficial on SMP boxes, as it
1090 * eliminates "false sharing".
1091 * Note for systems short on memory removing the alignment will
1092 * allow tighter packing of the smaller caches.
1094 if(!sizes
->cs_cachep
)
1095 sizes
->cs_cachep
= kmem_cache_create(names
->name
,
1096 sizes
->cs_size
, ARCH_KMALLOC_MINALIGN
,
1097 (ARCH_KMALLOC_FLAGS
| SLAB_PANIC
), NULL
, NULL
);
1099 /* Inc off-slab bufctl limit until the ceiling is hit. */
1100 if (!(OFF_SLAB(sizes
->cs_cachep
))) {
1101 offslab_limit
= sizes
->cs_size
-sizeof(struct slab
);
1102 offslab_limit
/= sizeof(kmem_bufctl_t
);
1105 sizes
->cs_dmacachep
= kmem_cache_create(names
->name_dma
,
1106 sizes
->cs_size
, ARCH_KMALLOC_MINALIGN
,
1107 (ARCH_KMALLOC_FLAGS
| SLAB_CACHE_DMA
| SLAB_PANIC
),
1113 /* 4) Replace the bootstrap head arrays */
1117 ptr
= kmalloc(sizeof(struct arraycache_init
), GFP_KERNEL
);
1119 local_irq_disable();
1120 BUG_ON(ac_data(&cache_cache
) != &initarray_cache
.cache
);
1121 memcpy(ptr
, ac_data(&cache_cache
),
1122 sizeof(struct arraycache_init
));
1123 cache_cache
.array
[smp_processor_id()] = ptr
;
1126 ptr
= kmalloc(sizeof(struct arraycache_init
), GFP_KERNEL
);
1128 local_irq_disable();
1129 BUG_ON(ac_data(malloc_sizes
[INDEX_AC
].cs_cachep
)
1130 != &initarray_generic
.cache
);
1131 memcpy(ptr
, ac_data(malloc_sizes
[INDEX_AC
].cs_cachep
),
1132 sizeof(struct arraycache_init
));
1133 malloc_sizes
[INDEX_AC
].cs_cachep
->array
[smp_processor_id()] =
1137 /* 5) Replace the bootstrap kmem_list3's */
1140 /* Replace the static kmem_list3 structures for the boot cpu */
1141 init_list(&cache_cache
, &initkmem_list3
[CACHE_CACHE
],
1144 for_each_online_node(node
) {
1145 init_list(malloc_sizes
[INDEX_AC
].cs_cachep
,
1146 &initkmem_list3
[SIZE_AC
+node
], node
);
1148 if (INDEX_AC
!= INDEX_L3
) {
1149 init_list(malloc_sizes
[INDEX_L3
].cs_cachep
,
1150 &initkmem_list3
[SIZE_L3
+node
],
1156 /* 6) resize the head arrays to their final sizes */
1158 kmem_cache_t
*cachep
;
1159 down(&cache_chain_sem
);
1160 list_for_each_entry(cachep
, &cache_chain
, next
)
1161 enable_cpucache(cachep
);
1162 up(&cache_chain_sem
);
1166 g_cpucache_up
= FULL
;
1168 /* Register a cpu startup notifier callback
1169 * that initializes ac_data for all new cpus
1171 register_cpu_notifier(&cpucache_notifier
);
1173 /* The reap timers are started later, with a module init call:
1174 * That part of the kernel is not yet operational.
1178 static int __init
cpucache_init(void)
1183 * Register the timers that return unneeded
1186 for_each_online_cpu(cpu
)
1187 start_cpu_timer(cpu
);
1192 __initcall(cpucache_init
);
1195 * Interface to system's page allocator. No need to hold the cache-lock.
1197 * If we requested dmaable memory, we will get it. Even if we
1198 * did not request dmaable memory, we might get it, but that
1199 * would be relatively rare and ignorable.
1201 static void *kmem_getpages(kmem_cache_t
*cachep
, gfp_t flags
, int nodeid
)
1207 flags
|= cachep
->gfpflags
;
1208 page
= alloc_pages_node(nodeid
, flags
, cachep
->gfporder
);
1211 addr
= page_address(page
);
1213 i
= (1 << cachep
->gfporder
);
1214 if (cachep
->flags
& SLAB_RECLAIM_ACCOUNT
)
1215 atomic_add(i
, &slab_reclaim_pages
);
1216 add_page_state(nr_slab
, i
);
1225 * Interface to system's page release.
1227 static void kmem_freepages(kmem_cache_t
*cachep
, void *addr
)
1229 unsigned long i
= (1<<cachep
->gfporder
);
1230 struct page
*page
= virt_to_page(addr
);
1231 const unsigned long nr_freed
= i
;
1234 if (!TestClearPageSlab(page
))
1238 sub_page_state(nr_slab
, nr_freed
);
1239 if (current
->reclaim_state
)
1240 current
->reclaim_state
->reclaimed_slab
+= nr_freed
;
1241 free_pages((unsigned long)addr
, cachep
->gfporder
);
1242 if (cachep
->flags
& SLAB_RECLAIM_ACCOUNT
)
1243 atomic_sub(1<<cachep
->gfporder
, &slab_reclaim_pages
);
1246 static void kmem_rcu_free(struct rcu_head
*head
)
1248 struct slab_rcu
*slab_rcu
= (struct slab_rcu
*) head
;
1249 kmem_cache_t
*cachep
= slab_rcu
->cachep
;
1251 kmem_freepages(cachep
, slab_rcu
->addr
);
1252 if (OFF_SLAB(cachep
))
1253 kmem_cache_free(cachep
->slabp_cache
, slab_rcu
);
1258 #ifdef CONFIG_DEBUG_PAGEALLOC
1259 static void store_stackinfo(kmem_cache_t
*cachep
, unsigned long *addr
,
1260 unsigned long caller
)
1262 int size
= obj_reallen(cachep
);
1264 addr
= (unsigned long *)&((char*)addr
)[obj_dbghead(cachep
)];
1266 if (size
< 5*sizeof(unsigned long))
1271 *addr
++=smp_processor_id();
1272 size
-= 3*sizeof(unsigned long);
1274 unsigned long *sptr
= &caller
;
1275 unsigned long svalue
;
1277 while (!kstack_end(sptr
)) {
1279 if (kernel_text_address(svalue
)) {
1281 size
-= sizeof(unsigned long);
1282 if (size
<= sizeof(unsigned long))
1292 static void poison_obj(kmem_cache_t
*cachep
, void *addr
, unsigned char val
)
1294 int size
= obj_reallen(cachep
);
1295 addr
= &((char*)addr
)[obj_dbghead(cachep
)];
1297 memset(addr
, val
, size
);
1298 *(unsigned char *)(addr
+size
-1) = POISON_END
;
1301 static void dump_line(char *data
, int offset
, int limit
)
1304 printk(KERN_ERR
"%03x:", offset
);
1305 for (i
=0;i
<limit
;i
++) {
1306 printk(" %02x", (unsigned char)data
[offset
+i
]);
1314 static void print_objinfo(kmem_cache_t
*cachep
, void *objp
, int lines
)
1319 if (cachep
->flags
& SLAB_RED_ZONE
) {
1320 printk(KERN_ERR
"Redzone: 0x%lx/0x%lx.\n",
1321 *dbg_redzone1(cachep
, objp
),
1322 *dbg_redzone2(cachep
, objp
));
1325 if (cachep
->flags
& SLAB_STORE_USER
) {
1326 printk(KERN_ERR
"Last user: [<%p>]",
1327 *dbg_userword(cachep
, objp
));
1328 print_symbol("(%s)",
1329 (unsigned long)*dbg_userword(cachep
, objp
));
1332 realobj
= (char*)objp
+obj_dbghead(cachep
);
1333 size
= obj_reallen(cachep
);
1334 for (i
=0; i
<size
&& lines
;i
+=16, lines
--) {
1339 dump_line(realobj
, i
, limit
);
1343 static void check_poison_obj(kmem_cache_t
*cachep
, void *objp
)
1349 realobj
= (char*)objp
+obj_dbghead(cachep
);
1350 size
= obj_reallen(cachep
);
1352 for (i
=0;i
<size
;i
++) {
1353 char exp
= POISON_FREE
;
1356 if (realobj
[i
] != exp
) {
1361 printk(KERN_ERR
"Slab corruption: start=%p, len=%d\n",
1363 print_objinfo(cachep
, objp
, 0);
1365 /* Hexdump the affected line */
1370 dump_line(realobj
, i
, limit
);
1373 /* Limit to 5 lines */
1379 /* Print some data about the neighboring objects, if they
1382 struct slab
*slabp
= page_get_slab(virt_to_page(objp
));
1385 objnr
= (objp
-slabp
->s_mem
)/cachep
->objsize
;
1387 objp
= slabp
->s_mem
+(objnr
-1)*cachep
->objsize
;
1388 realobj
= (char*)objp
+obj_dbghead(cachep
);
1389 printk(KERN_ERR
"Prev obj: start=%p, len=%d\n",
1391 print_objinfo(cachep
, objp
, 2);
1393 if (objnr
+1 < cachep
->num
) {
1394 objp
= slabp
->s_mem
+(objnr
+1)*cachep
->objsize
;
1395 realobj
= (char*)objp
+obj_dbghead(cachep
);
1396 printk(KERN_ERR
"Next obj: start=%p, len=%d\n",
1398 print_objinfo(cachep
, objp
, 2);
1404 /* Destroy all the objs in a slab, and release the mem back to the system.
1405 * Before calling the slab must have been unlinked from the cache.
1406 * The cache-lock is not held/needed.
1408 static void slab_destroy (kmem_cache_t
*cachep
, struct slab
*slabp
)
1410 void *addr
= slabp
->s_mem
- slabp
->colouroff
;
1414 for (i
= 0; i
< cachep
->num
; i
++) {
1415 void *objp
= slabp
->s_mem
+ cachep
->objsize
* i
;
1417 if (cachep
->flags
& SLAB_POISON
) {
1418 #ifdef CONFIG_DEBUG_PAGEALLOC
1419 if ((cachep
->objsize
%PAGE_SIZE
)==0 && OFF_SLAB(cachep
))
1420 kernel_map_pages(virt_to_page(objp
), cachep
->objsize
/PAGE_SIZE
,1);
1422 check_poison_obj(cachep
, objp
);
1424 check_poison_obj(cachep
, objp
);
1427 if (cachep
->flags
& SLAB_RED_ZONE
) {
1428 if (*dbg_redzone1(cachep
, objp
) != RED_INACTIVE
)
1429 slab_error(cachep
, "start of a freed object "
1431 if (*dbg_redzone2(cachep
, objp
) != RED_INACTIVE
)
1432 slab_error(cachep
, "end of a freed object "
1435 if (cachep
->dtor
&& !(cachep
->flags
& SLAB_POISON
))
1436 (cachep
->dtor
)(objp
+obj_dbghead(cachep
), cachep
, 0);
1441 for (i
= 0; i
< cachep
->num
; i
++) {
1442 void* objp
= slabp
->s_mem
+cachep
->objsize
*i
;
1443 (cachep
->dtor
)(objp
, cachep
, 0);
1448 if (unlikely(cachep
->flags
& SLAB_DESTROY_BY_RCU
)) {
1449 struct slab_rcu
*slab_rcu
;
1451 slab_rcu
= (struct slab_rcu
*) slabp
;
1452 slab_rcu
->cachep
= cachep
;
1453 slab_rcu
->addr
= addr
;
1454 call_rcu(&slab_rcu
->head
, kmem_rcu_free
);
1456 kmem_freepages(cachep
, addr
);
1457 if (OFF_SLAB(cachep
))
1458 kmem_cache_free(cachep
->slabp_cache
, slabp
);
1462 /* For setting up all the kmem_list3s for cache whose objsize is same
1463 as size of kmem_list3. */
1464 static inline void set_up_list3s(kmem_cache_t
*cachep
, int index
)
1468 for_each_online_node(node
) {
1469 cachep
->nodelists
[node
] = &initkmem_list3
[index
+node
];
1470 cachep
->nodelists
[node
]->next_reap
= jiffies
+
1472 ((unsigned long)cachep
)%REAPTIMEOUT_LIST3
;
1477 * calculate_slab_order - calculate size (page order) of slabs and the number
1478 * of objects per slab.
1480 * This could be made much more intelligent. For now, try to avoid using
1481 * high order pages for slabs. When the gfp() functions are more friendly
1482 * towards high-order requests, this should be changed.
1484 static inline size_t calculate_slab_order(kmem_cache_t
*cachep
, size_t size
,
1485 size_t align
, gfp_t flags
)
1487 size_t left_over
= 0;
1489 for ( ; ; cachep
->gfporder
++) {
1493 if (cachep
->gfporder
> MAX_GFP_ORDER
) {
1498 cache_estimate(cachep
->gfporder
, size
, align
, flags
,
1502 /* More than offslab_limit objects will cause problems */
1503 if (flags
& CFLGS_OFF_SLAB
&& cachep
->num
> offslab_limit
)
1507 left_over
= remainder
;
1510 * Large number of objects is good, but very large slabs are
1511 * currently bad for the gfp()s.
1513 if (cachep
->gfporder
>= slab_break_gfp_order
)
1516 if ((left_over
* 8) <= (PAGE_SIZE
<< cachep
->gfporder
))
1517 /* Acceptable internal fragmentation */
1524 * kmem_cache_create - Create a cache.
1525 * @name: A string which is used in /proc/slabinfo to identify this cache.
1526 * @size: The size of objects to be created in this cache.
1527 * @align: The required alignment for the objects.
1528 * @flags: SLAB flags
1529 * @ctor: A constructor for the objects.
1530 * @dtor: A destructor for the objects.
1532 * Returns a ptr to the cache on success, NULL on failure.
1533 * Cannot be called within a int, but can be interrupted.
1534 * The @ctor is run when new pages are allocated by the cache
1535 * and the @dtor is run before the pages are handed back.
1537 * @name must be valid until the cache is destroyed. This implies that
1538 * the module calling this has to destroy the cache before getting
1543 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
1544 * to catch references to uninitialised memory.
1546 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
1547 * for buffer overruns.
1549 * %SLAB_NO_REAP - Don't automatically reap this cache when we're under
1552 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
1553 * cacheline. This can be beneficial if you're counting cycles as closely
1557 kmem_cache_create (const char *name
, size_t size
, size_t align
,
1558 unsigned long flags
, void (*ctor
)(void*, kmem_cache_t
*, unsigned long),
1559 void (*dtor
)(void*, kmem_cache_t
*, unsigned long))
1561 size_t left_over
, slab_size
, ralign
;
1562 kmem_cache_t
*cachep
= NULL
;
1563 struct list_head
*p
;
1566 * Sanity checks... these are all serious usage bugs.
1570 (size
< BYTES_PER_WORD
) ||
1571 (size
> (1<<MAX_OBJ_ORDER
)*PAGE_SIZE
) ||
1573 printk(KERN_ERR
"%s: Early error in slab %s\n",
1574 __FUNCTION__
, name
);
1578 down(&cache_chain_sem
);
1580 list_for_each(p
, &cache_chain
) {
1581 kmem_cache_t
*pc
= list_entry(p
, kmem_cache_t
, next
);
1582 mm_segment_t old_fs
= get_fs();
1587 * This happens when the module gets unloaded and doesn't
1588 * destroy its slab cache and no-one else reuses the vmalloc
1589 * area of the module. Print a warning.
1592 res
= __get_user(tmp
, pc
->name
);
1595 printk("SLAB: cache with size %d has lost its name\n",
1600 if (!strcmp(pc
->name
,name
)) {
1601 printk("kmem_cache_create: duplicate cache %s\n", name
);
1608 WARN_ON(strchr(name
, ' ')); /* It confuses parsers */
1609 if ((flags
& SLAB_DEBUG_INITIAL
) && !ctor
) {
1610 /* No constructor, but inital state check requested */
1611 printk(KERN_ERR
"%s: No con, but init state check "
1612 "requested - %s\n", __FUNCTION__
, name
);
1613 flags
&= ~SLAB_DEBUG_INITIAL
;
1618 * Enable redzoning and last user accounting, except for caches with
1619 * large objects, if the increased size would increase the object size
1620 * above the next power of two: caches with object sizes just above a
1621 * power of two have a significant amount of internal fragmentation.
1623 if ((size
< 4096 || fls(size
-1) == fls(size
-1+3*BYTES_PER_WORD
)))
1624 flags
|= SLAB_RED_ZONE
|SLAB_STORE_USER
;
1625 if (!(flags
& SLAB_DESTROY_BY_RCU
))
1626 flags
|= SLAB_POISON
;
1628 if (flags
& SLAB_DESTROY_BY_RCU
)
1629 BUG_ON(flags
& SLAB_POISON
);
1631 if (flags
& SLAB_DESTROY_BY_RCU
)
1635 * Always checks flags, a caller might be expecting debug
1636 * support which isn't available.
1638 if (flags
& ~CREATE_MASK
)
1641 /* Check that size is in terms of words. This is needed to avoid
1642 * unaligned accesses for some archs when redzoning is used, and makes
1643 * sure any on-slab bufctl's are also correctly aligned.
1645 if (size
& (BYTES_PER_WORD
-1)) {
1646 size
+= (BYTES_PER_WORD
-1);
1647 size
&= ~(BYTES_PER_WORD
-1);
1650 /* calculate out the final buffer alignment: */
1651 /* 1) arch recommendation: can be overridden for debug */
1652 if (flags
& SLAB_HWCACHE_ALIGN
) {
1653 /* Default alignment: as specified by the arch code.
1654 * Except if an object is really small, then squeeze multiple
1655 * objects into one cacheline.
1657 ralign
= cache_line_size();
1658 while (size
<= ralign
/2)
1661 ralign
= BYTES_PER_WORD
;
1663 /* 2) arch mandated alignment: disables debug if necessary */
1664 if (ralign
< ARCH_SLAB_MINALIGN
) {
1665 ralign
= ARCH_SLAB_MINALIGN
;
1666 if (ralign
> BYTES_PER_WORD
)
1667 flags
&= ~(SLAB_RED_ZONE
|SLAB_STORE_USER
);
1669 /* 3) caller mandated alignment: disables debug if necessary */
1670 if (ralign
< align
) {
1672 if (ralign
> BYTES_PER_WORD
)
1673 flags
&= ~(SLAB_RED_ZONE
|SLAB_STORE_USER
);
1675 /* 4) Store it. Note that the debug code below can reduce
1676 * the alignment to BYTES_PER_WORD.
1680 /* Get cache's description obj. */
1681 cachep
= (kmem_cache_t
*) kmem_cache_alloc(&cache_cache
, SLAB_KERNEL
);
1684 memset(cachep
, 0, sizeof(kmem_cache_t
));
1687 cachep
->reallen
= size
;
1689 if (flags
& SLAB_RED_ZONE
) {
1690 /* redzoning only works with word aligned caches */
1691 align
= BYTES_PER_WORD
;
1693 /* add space for red zone words */
1694 cachep
->dbghead
+= BYTES_PER_WORD
;
1695 size
+= 2*BYTES_PER_WORD
;
1697 if (flags
& SLAB_STORE_USER
) {
1698 /* user store requires word alignment and
1699 * one word storage behind the end of the real
1702 align
= BYTES_PER_WORD
;
1703 size
+= BYTES_PER_WORD
;
1705 #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
1706 if (size
>= malloc_sizes
[INDEX_L3
+1].cs_size
&& cachep
->reallen
> cache_line_size() && size
< PAGE_SIZE
) {
1707 cachep
->dbghead
+= PAGE_SIZE
- size
;
1713 /* Determine if the slab management is 'on' or 'off' slab. */
1714 if (size
>= (PAGE_SIZE
>>3))
1716 * Size is large, assume best to place the slab management obj
1717 * off-slab (should allow better packing of objs).
1719 flags
|= CFLGS_OFF_SLAB
;
1721 size
= ALIGN(size
, align
);
1723 if ((flags
& SLAB_RECLAIM_ACCOUNT
) && size
<= PAGE_SIZE
) {
1725 * A VFS-reclaimable slab tends to have most allocations
1726 * as GFP_NOFS and we really don't want to have to be allocating
1727 * higher-order pages when we are unable to shrink dcache.
1729 cachep
->gfporder
= 0;
1730 cache_estimate(cachep
->gfporder
, size
, align
, flags
,
1731 &left_over
, &cachep
->num
);
1733 left_over
= calculate_slab_order(cachep
, size
, align
, flags
);
1736 printk("kmem_cache_create: couldn't create cache %s.\n", name
);
1737 kmem_cache_free(&cache_cache
, cachep
);
1741 slab_size
= ALIGN(cachep
->num
*sizeof(kmem_bufctl_t
)
1742 + sizeof(struct slab
), align
);
1745 * If the slab has been placed off-slab, and we have enough space then
1746 * move it on-slab. This is at the expense of any extra colouring.
1748 if (flags
& CFLGS_OFF_SLAB
&& left_over
>= slab_size
) {
1749 flags
&= ~CFLGS_OFF_SLAB
;
1750 left_over
-= slab_size
;
1753 if (flags
& CFLGS_OFF_SLAB
) {
1754 /* really off slab. No need for manual alignment */
1755 slab_size
= cachep
->num
*sizeof(kmem_bufctl_t
)+sizeof(struct slab
);
1758 cachep
->colour_off
= cache_line_size();
1759 /* Offset must be a multiple of the alignment. */
1760 if (cachep
->colour_off
< align
)
1761 cachep
->colour_off
= align
;
1762 cachep
->colour
= left_over
/cachep
->colour_off
;
1763 cachep
->slab_size
= slab_size
;
1764 cachep
->flags
= flags
;
1765 cachep
->gfpflags
= 0;
1766 if (flags
& SLAB_CACHE_DMA
)
1767 cachep
->gfpflags
|= GFP_DMA
;
1768 spin_lock_init(&cachep
->spinlock
);
1769 cachep
->objsize
= size
;
1771 if (flags
& CFLGS_OFF_SLAB
)
1772 cachep
->slabp_cache
= kmem_find_general_cachep(slab_size
, 0u);
1773 cachep
->ctor
= ctor
;
1774 cachep
->dtor
= dtor
;
1775 cachep
->name
= name
;
1777 /* Don't let CPUs to come and go */
1780 if (g_cpucache_up
== FULL
) {
1781 enable_cpucache(cachep
);
1783 if (g_cpucache_up
== NONE
) {
1784 /* Note: the first kmem_cache_create must create
1785 * the cache that's used by kmalloc(24), otherwise
1786 * the creation of further caches will BUG().
1788 cachep
->array
[smp_processor_id()] =
1789 &initarray_generic
.cache
;
1791 /* If the cache that's used by
1792 * kmalloc(sizeof(kmem_list3)) is the first cache,
1793 * then we need to set up all its list3s, otherwise
1794 * the creation of further caches will BUG().
1796 set_up_list3s(cachep
, SIZE_AC
);
1797 if (INDEX_AC
== INDEX_L3
)
1798 g_cpucache_up
= PARTIAL_L3
;
1800 g_cpucache_up
= PARTIAL_AC
;
1802 cachep
->array
[smp_processor_id()] =
1803 kmalloc(sizeof(struct arraycache_init
),
1806 if (g_cpucache_up
== PARTIAL_AC
) {
1807 set_up_list3s(cachep
, SIZE_L3
);
1808 g_cpucache_up
= PARTIAL_L3
;
1811 for_each_online_node(node
) {
1813 cachep
->nodelists
[node
] =
1814 kmalloc_node(sizeof(struct kmem_list3
),
1816 BUG_ON(!cachep
->nodelists
[node
]);
1817 kmem_list3_init(cachep
->nodelists
[node
]);
1821 cachep
->nodelists
[numa_node_id()]->next_reap
=
1822 jiffies
+ REAPTIMEOUT_LIST3
+
1823 ((unsigned long)cachep
)%REAPTIMEOUT_LIST3
;
1825 BUG_ON(!ac_data(cachep
));
1826 ac_data(cachep
)->avail
= 0;
1827 ac_data(cachep
)->limit
= BOOT_CPUCACHE_ENTRIES
;
1828 ac_data(cachep
)->batchcount
= 1;
1829 ac_data(cachep
)->touched
= 0;
1830 cachep
->batchcount
= 1;
1831 cachep
->limit
= BOOT_CPUCACHE_ENTRIES
;
1834 /* cache setup completed, link it into the list */
1835 list_add(&cachep
->next
, &cache_chain
);
1836 unlock_cpu_hotplug();
1838 if (!cachep
&& (flags
& SLAB_PANIC
))
1839 panic("kmem_cache_create(): failed to create slab `%s'\n",
1841 up(&cache_chain_sem
);
1844 EXPORT_SYMBOL(kmem_cache_create
);
1847 static void check_irq_off(void)
1849 BUG_ON(!irqs_disabled());
1852 static void check_irq_on(void)
1854 BUG_ON(irqs_disabled());
1857 static void check_spinlock_acquired(kmem_cache_t
*cachep
)
1861 assert_spin_locked(&cachep
->nodelists
[numa_node_id()]->list_lock
);
1865 static inline void check_spinlock_acquired_node(kmem_cache_t
*cachep
, int node
)
1869 assert_spin_locked(&cachep
->nodelists
[node
]->list_lock
);
1874 #define check_irq_off() do { } while(0)
1875 #define check_irq_on() do { } while(0)
1876 #define check_spinlock_acquired(x) do { } while(0)
1877 #define check_spinlock_acquired_node(x, y) do { } while(0)
1881 * Waits for all CPUs to execute func().
1883 static void smp_call_function_all_cpus(void (*func
) (void *arg
), void *arg
)
1888 local_irq_disable();
1892 if (smp_call_function(func
, arg
, 1, 1))
1898 static void drain_array_locked(kmem_cache_t
* cachep
,
1899 struct array_cache
*ac
, int force
, int node
);
1901 static void do_drain(void *arg
)
1903 kmem_cache_t
*cachep
= (kmem_cache_t
*)arg
;
1904 struct array_cache
*ac
;
1905 int node
= numa_node_id();
1908 ac
= ac_data(cachep
);
1909 spin_lock(&cachep
->nodelists
[node
]->list_lock
);
1910 free_block(cachep
, ac
->entry
, ac
->avail
, node
);
1911 spin_unlock(&cachep
->nodelists
[node
]->list_lock
);
1915 static void drain_cpu_caches(kmem_cache_t
*cachep
)
1917 struct kmem_list3
*l3
;
1920 smp_call_function_all_cpus(do_drain
, cachep
);
1922 spin_lock_irq(&cachep
->spinlock
);
1923 for_each_online_node(node
) {
1924 l3
= cachep
->nodelists
[node
];
1926 spin_lock(&l3
->list_lock
);
1927 drain_array_locked(cachep
, l3
->shared
, 1, node
);
1928 spin_unlock(&l3
->list_lock
);
1930 drain_alien_cache(cachep
, l3
);
1933 spin_unlock_irq(&cachep
->spinlock
);
1936 static int __node_shrink(kmem_cache_t
*cachep
, int node
)
1939 struct kmem_list3
*l3
= cachep
->nodelists
[node
];
1943 struct list_head
*p
;
1945 p
= l3
->slabs_free
.prev
;
1946 if (p
== &l3
->slabs_free
)
1949 slabp
= list_entry(l3
->slabs_free
.prev
, struct slab
, list
);
1954 list_del(&slabp
->list
);
1956 l3
->free_objects
-= cachep
->num
;
1957 spin_unlock_irq(&l3
->list_lock
);
1958 slab_destroy(cachep
, slabp
);
1959 spin_lock_irq(&l3
->list_lock
);
1961 ret
= !list_empty(&l3
->slabs_full
) ||
1962 !list_empty(&l3
->slabs_partial
);
1966 static int __cache_shrink(kmem_cache_t
*cachep
)
1969 struct kmem_list3
*l3
;
1971 drain_cpu_caches(cachep
);
1974 for_each_online_node(i
) {
1975 l3
= cachep
->nodelists
[i
];
1977 spin_lock_irq(&l3
->list_lock
);
1978 ret
+= __node_shrink(cachep
, i
);
1979 spin_unlock_irq(&l3
->list_lock
);
1982 return (ret
? 1 : 0);
1986 * kmem_cache_shrink - Shrink a cache.
1987 * @cachep: The cache to shrink.
1989 * Releases as many slabs as possible for a cache.
1990 * To help debugging, a zero exit status indicates all slabs were released.
1992 int kmem_cache_shrink(kmem_cache_t
*cachep
)
1994 if (!cachep
|| in_interrupt())
1997 return __cache_shrink(cachep
);
1999 EXPORT_SYMBOL(kmem_cache_shrink
);
2002 * kmem_cache_destroy - delete a cache
2003 * @cachep: the cache to destroy
2005 * Remove a kmem_cache_t object from the slab cache.
2006 * Returns 0 on success.
2008 * It is expected this function will be called by a module when it is
2009 * unloaded. This will remove the cache completely, and avoid a duplicate
2010 * cache being allocated each time a module is loaded and unloaded, if the
2011 * module doesn't have persistent in-kernel storage across loads and unloads.
2013 * The cache must be empty before calling this function.
2015 * The caller must guarantee that noone will allocate memory from the cache
2016 * during the kmem_cache_destroy().
2018 int kmem_cache_destroy(kmem_cache_t
* cachep
)
2021 struct kmem_list3
*l3
;
2023 if (!cachep
|| in_interrupt())
2026 /* Don't let CPUs to come and go */
2029 /* Find the cache in the chain of caches. */
2030 down(&cache_chain_sem
);
2032 * the chain is never empty, cache_cache is never destroyed
2034 list_del(&cachep
->next
);
2035 up(&cache_chain_sem
);
2037 if (__cache_shrink(cachep
)) {
2038 slab_error(cachep
, "Can't free all objects");
2039 down(&cache_chain_sem
);
2040 list_add(&cachep
->next
,&cache_chain
);
2041 up(&cache_chain_sem
);
2042 unlock_cpu_hotplug();
2046 if (unlikely(cachep
->flags
& SLAB_DESTROY_BY_RCU
))
2049 for_each_online_cpu(i
)
2050 kfree(cachep
->array
[i
]);
2052 /* NUMA: free the list3 structures */
2053 for_each_online_node(i
) {
2054 if ((l3
= cachep
->nodelists
[i
])) {
2056 free_alien_cache(l3
->alien
);
2060 kmem_cache_free(&cache_cache
, cachep
);
2062 unlock_cpu_hotplug();
2066 EXPORT_SYMBOL(kmem_cache_destroy
);
2068 /* Get the memory for a slab management obj. */
2069 static struct slab
* alloc_slabmgmt(kmem_cache_t
*cachep
, void *objp
,
2070 int colour_off
, gfp_t local_flags
)
2074 if (OFF_SLAB(cachep
)) {
2075 /* Slab management obj is off-slab. */
2076 slabp
= kmem_cache_alloc(cachep
->slabp_cache
, local_flags
);
2080 slabp
= objp
+colour_off
;
2081 colour_off
+= cachep
->slab_size
;
2084 slabp
->colouroff
= colour_off
;
2085 slabp
->s_mem
= objp
+colour_off
;
2090 static inline kmem_bufctl_t
*slab_bufctl(struct slab
*slabp
)
2092 return (kmem_bufctl_t
*)(slabp
+1);
2095 static void cache_init_objs(kmem_cache_t
*cachep
,
2096 struct slab
*slabp
, unsigned long ctor_flags
)
2100 for (i
= 0; i
< cachep
->num
; i
++) {
2101 void *objp
= slabp
->s_mem
+cachep
->objsize
*i
;
2103 /* need to poison the objs? */
2104 if (cachep
->flags
& SLAB_POISON
)
2105 poison_obj(cachep
, objp
, POISON_FREE
);
2106 if (cachep
->flags
& SLAB_STORE_USER
)
2107 *dbg_userword(cachep
, objp
) = NULL
;
2109 if (cachep
->flags
& SLAB_RED_ZONE
) {
2110 *dbg_redzone1(cachep
, objp
) = RED_INACTIVE
;
2111 *dbg_redzone2(cachep
, objp
) = RED_INACTIVE
;
2114 * Constructors are not allowed to allocate memory from
2115 * the same cache which they are a constructor for.
2116 * Otherwise, deadlock. They must also be threaded.
2118 if (cachep
->ctor
&& !(cachep
->flags
& SLAB_POISON
))
2119 cachep
->ctor(objp
+obj_dbghead(cachep
), cachep
, ctor_flags
);
2121 if (cachep
->flags
& SLAB_RED_ZONE
) {
2122 if (*dbg_redzone2(cachep
, objp
) != RED_INACTIVE
)
2123 slab_error(cachep
, "constructor overwrote the"
2124 " end of an object");
2125 if (*dbg_redzone1(cachep
, objp
) != RED_INACTIVE
)
2126 slab_error(cachep
, "constructor overwrote the"
2127 " start of an object");
2129 if ((cachep
->objsize
% PAGE_SIZE
) == 0 && OFF_SLAB(cachep
) && cachep
->flags
& SLAB_POISON
)
2130 kernel_map_pages(virt_to_page(objp
), cachep
->objsize
/PAGE_SIZE
, 0);
2133 cachep
->ctor(objp
, cachep
, ctor_flags
);
2135 slab_bufctl(slabp
)[i
] = i
+1;
2137 slab_bufctl(slabp
)[i
-1] = BUFCTL_END
;
2141 static void kmem_flagcheck(kmem_cache_t
*cachep
, gfp_t flags
)
2143 if (flags
& SLAB_DMA
) {
2144 if (!(cachep
->gfpflags
& GFP_DMA
))
2147 if (cachep
->gfpflags
& GFP_DMA
)
2152 static void set_slab_attr(kmem_cache_t
*cachep
, struct slab
*slabp
, void *objp
)
2157 /* Nasty!!!!!! I hope this is OK. */
2158 i
= 1 << cachep
->gfporder
;
2159 page
= virt_to_page(objp
);
2161 page_set_cache(page
, cachep
);
2162 page_set_slab(page
, slabp
);
2168 * Grow (by 1) the number of slabs within a cache. This is called by
2169 * kmem_cache_alloc() when there are no active objs left in a cache.
2171 static int cache_grow(kmem_cache_t
*cachep
, gfp_t flags
, int nodeid
)
2177 unsigned long ctor_flags
;
2178 struct kmem_list3
*l3
;
2180 /* Be lazy and only check for valid flags here,
2181 * keeping it out of the critical path in kmem_cache_alloc().
2183 if (flags
& ~(SLAB_DMA
|SLAB_LEVEL_MASK
|SLAB_NO_GROW
))
2185 if (flags
& SLAB_NO_GROW
)
2188 ctor_flags
= SLAB_CTOR_CONSTRUCTOR
;
2189 local_flags
= (flags
& SLAB_LEVEL_MASK
);
2190 if (!(local_flags
& __GFP_WAIT
))
2192 * Not allowed to sleep. Need to tell a constructor about
2193 * this - it might need to know...
2195 ctor_flags
|= SLAB_CTOR_ATOMIC
;
2197 /* About to mess with non-constant members - lock. */
2199 spin_lock(&cachep
->spinlock
);
2201 /* Get colour for the slab, and cal the next value. */
2202 offset
= cachep
->colour_next
;
2203 cachep
->colour_next
++;
2204 if (cachep
->colour_next
>= cachep
->colour
)
2205 cachep
->colour_next
= 0;
2206 offset
*= cachep
->colour_off
;
2208 spin_unlock(&cachep
->spinlock
);
2211 if (local_flags
& __GFP_WAIT
)
2215 * The test for missing atomic flag is performed here, rather than
2216 * the more obvious place, simply to reduce the critical path length
2217 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2218 * will eventually be caught here (where it matters).
2220 kmem_flagcheck(cachep
, flags
);
2222 /* Get mem for the objs.
2223 * Attempt to allocate a physical page from 'nodeid',
2225 if (!(objp
= kmem_getpages(cachep
, flags
, nodeid
)))
2228 /* Get slab management. */
2229 if (!(slabp
= alloc_slabmgmt(cachep
, objp
, offset
, local_flags
)))
2232 slabp
->nodeid
= nodeid
;
2233 set_slab_attr(cachep
, slabp
, objp
);
2235 cache_init_objs(cachep
, slabp
, ctor_flags
);
2237 if (local_flags
& __GFP_WAIT
)
2238 local_irq_disable();
2240 l3
= cachep
->nodelists
[nodeid
];
2241 spin_lock(&l3
->list_lock
);
2243 /* Make slab active. */
2244 list_add_tail(&slabp
->list
, &(l3
->slabs_free
));
2245 STATS_INC_GROWN(cachep
);
2246 l3
->free_objects
+= cachep
->num
;
2247 spin_unlock(&l3
->list_lock
);
2250 kmem_freepages(cachep
, objp
);
2252 if (local_flags
& __GFP_WAIT
)
2253 local_irq_disable();
2260 * Perform extra freeing checks:
2261 * - detect bad pointers.
2262 * - POISON/RED_ZONE checking
2263 * - destructor calls, for caches with POISON+dtor
2265 static void kfree_debugcheck(const void *objp
)
2269 if (!virt_addr_valid(objp
)) {
2270 printk(KERN_ERR
"kfree_debugcheck: out of range ptr %lxh.\n",
2271 (unsigned long)objp
);
2274 page
= virt_to_page(objp
);
2275 if (!PageSlab(page
)) {
2276 printk(KERN_ERR
"kfree_debugcheck: bad ptr %lxh.\n", (unsigned long)objp
);
2281 static void *cache_free_debugcheck(kmem_cache_t
*cachep
, void *objp
,
2288 objp
-= obj_dbghead(cachep
);
2289 kfree_debugcheck(objp
);
2290 page
= virt_to_page(objp
);
2292 if (page_get_cache(page
) != cachep
) {
2293 printk(KERN_ERR
"mismatch in kmem_cache_free: expected cache %p, got %p\n",
2294 page_get_cache(page
),cachep
);
2295 printk(KERN_ERR
"%p is %s.\n", cachep
, cachep
->name
);
2296 printk(KERN_ERR
"%p is %s.\n", page_get_cache(page
), page_get_cache(page
)->name
);
2299 slabp
= page_get_slab(page
);
2301 if (cachep
->flags
& SLAB_RED_ZONE
) {
2302 if (*dbg_redzone1(cachep
, objp
) != RED_ACTIVE
|| *dbg_redzone2(cachep
, objp
) != RED_ACTIVE
) {
2303 slab_error(cachep
, "double free, or memory outside"
2304 " object was overwritten");
2305 printk(KERN_ERR
"%p: redzone 1: 0x%lx, redzone 2: 0x%lx.\n",
2306 objp
, *dbg_redzone1(cachep
, objp
), *dbg_redzone2(cachep
, objp
));
2308 *dbg_redzone1(cachep
, objp
) = RED_INACTIVE
;
2309 *dbg_redzone2(cachep
, objp
) = RED_INACTIVE
;
2311 if (cachep
->flags
& SLAB_STORE_USER
)
2312 *dbg_userword(cachep
, objp
) = caller
;
2314 objnr
= (objp
-slabp
->s_mem
)/cachep
->objsize
;
2316 BUG_ON(objnr
>= cachep
->num
);
2317 BUG_ON(objp
!= slabp
->s_mem
+ objnr
*cachep
->objsize
);
2319 if (cachep
->flags
& SLAB_DEBUG_INITIAL
) {
2320 /* Need to call the slab's constructor so the
2321 * caller can perform a verify of its state (debugging).
2322 * Called without the cache-lock held.
2324 cachep
->ctor(objp
+obj_dbghead(cachep
),
2325 cachep
, SLAB_CTOR_CONSTRUCTOR
|SLAB_CTOR_VERIFY
);
2327 if (cachep
->flags
& SLAB_POISON
&& cachep
->dtor
) {
2328 /* we want to cache poison the object,
2329 * call the destruction callback
2331 cachep
->dtor(objp
+obj_dbghead(cachep
), cachep
, 0);
2333 if (cachep
->flags
& SLAB_POISON
) {
2334 #ifdef CONFIG_DEBUG_PAGEALLOC
2335 if ((cachep
->objsize
% PAGE_SIZE
) == 0 && OFF_SLAB(cachep
)) {
2336 store_stackinfo(cachep
, objp
, (unsigned long)caller
);
2337 kernel_map_pages(virt_to_page(objp
), cachep
->objsize
/PAGE_SIZE
, 0);
2339 poison_obj(cachep
, objp
, POISON_FREE
);
2342 poison_obj(cachep
, objp
, POISON_FREE
);
2348 static void check_slabp(kmem_cache_t
*cachep
, struct slab
*slabp
)
2353 /* Check slab's freelist to see if this obj is there. */
2354 for (i
= slabp
->free
; i
!= BUFCTL_END
; i
= slab_bufctl(slabp
)[i
]) {
2356 if (entries
> cachep
->num
|| i
>= cachep
->num
)
2359 if (entries
!= cachep
->num
- slabp
->inuse
) {
2361 printk(KERN_ERR
"slab: Internal list corruption detected in cache '%s'(%d), slabp %p(%d). Hexdump:\n",
2362 cachep
->name
, cachep
->num
, slabp
, slabp
->inuse
);
2363 for (i
=0;i
<sizeof(slabp
)+cachep
->num
*sizeof(kmem_bufctl_t
);i
++) {
2365 printk("\n%03x:", i
);
2366 printk(" %02x", ((unsigned char*)slabp
)[i
]);
2373 #define kfree_debugcheck(x) do { } while(0)
2374 #define cache_free_debugcheck(x,objp,z) (objp)
2375 #define check_slabp(x,y) do { } while(0)
2378 static void *cache_alloc_refill(kmem_cache_t
*cachep
, gfp_t flags
)
2381 struct kmem_list3
*l3
;
2382 struct array_cache
*ac
;
2385 ac
= ac_data(cachep
);
2387 batchcount
= ac
->batchcount
;
2388 if (!ac
->touched
&& batchcount
> BATCHREFILL_LIMIT
) {
2389 /* if there was little recent activity on this
2390 * cache, then perform only a partial refill.
2391 * Otherwise we could generate refill bouncing.
2393 batchcount
= BATCHREFILL_LIMIT
;
2395 l3
= cachep
->nodelists
[numa_node_id()];
2397 BUG_ON(ac
->avail
> 0 || !l3
);
2398 spin_lock(&l3
->list_lock
);
2401 struct array_cache
*shared_array
= l3
->shared
;
2402 if (shared_array
->avail
) {
2403 if (batchcount
> shared_array
->avail
)
2404 batchcount
= shared_array
->avail
;
2405 shared_array
->avail
-= batchcount
;
2406 ac
->avail
= batchcount
;
2408 &(shared_array
->entry
[shared_array
->avail
]),
2409 sizeof(void*)*batchcount
);
2410 shared_array
->touched
= 1;
2414 while (batchcount
> 0) {
2415 struct list_head
*entry
;
2417 /* Get slab alloc is to come from. */
2418 entry
= l3
->slabs_partial
.next
;
2419 if (entry
== &l3
->slabs_partial
) {
2420 l3
->free_touched
= 1;
2421 entry
= l3
->slabs_free
.next
;
2422 if (entry
== &l3
->slabs_free
)
2426 slabp
= list_entry(entry
, struct slab
, list
);
2427 check_slabp(cachep
, slabp
);
2428 check_spinlock_acquired(cachep
);
2429 while (slabp
->inuse
< cachep
->num
&& batchcount
--) {
2431 STATS_INC_ALLOCED(cachep
);
2432 STATS_INC_ACTIVE(cachep
);
2433 STATS_SET_HIGH(cachep
);
2435 /* get obj pointer */
2436 ac
->entry
[ac
->avail
++] = slabp
->s_mem
+
2437 slabp
->free
*cachep
->objsize
;
2440 next
= slab_bufctl(slabp
)[slabp
->free
];
2442 slab_bufctl(slabp
)[slabp
->free
] = BUFCTL_FREE
;
2443 WARN_ON(numa_node_id() != slabp
->nodeid
);
2447 check_slabp(cachep
, slabp
);
2449 /* move slabp to correct slabp list: */
2450 list_del(&slabp
->list
);
2451 if (slabp
->free
== BUFCTL_END
)
2452 list_add(&slabp
->list
, &l3
->slabs_full
);
2454 list_add(&slabp
->list
, &l3
->slabs_partial
);
2458 l3
->free_objects
-= ac
->avail
;
2460 spin_unlock(&l3
->list_lock
);
2462 if (unlikely(!ac
->avail
)) {
2464 x
= cache_grow(cachep
, flags
, numa_node_id());
2466 // cache_grow can reenable interrupts, then ac could change.
2467 ac
= ac_data(cachep
);
2468 if (!x
&& ac
->avail
== 0) // no objects in sight? abort
2471 if (!ac
->avail
) // objects refilled by interrupt?
2475 return ac
->entry
[--ac
->avail
];
2479 cache_alloc_debugcheck_before(kmem_cache_t
*cachep
, gfp_t flags
)
2481 might_sleep_if(flags
& __GFP_WAIT
);
2483 kmem_flagcheck(cachep
, flags
);
2489 cache_alloc_debugcheck_after(kmem_cache_t
*cachep
,
2490 gfp_t flags
, void *objp
, void *caller
)
2494 if (cachep
->flags
& SLAB_POISON
) {
2495 #ifdef CONFIG_DEBUG_PAGEALLOC
2496 if ((cachep
->objsize
% PAGE_SIZE
) == 0 && OFF_SLAB(cachep
))
2497 kernel_map_pages(virt_to_page(objp
), cachep
->objsize
/PAGE_SIZE
, 1);
2499 check_poison_obj(cachep
, objp
);
2501 check_poison_obj(cachep
, objp
);
2503 poison_obj(cachep
, objp
, POISON_INUSE
);
2505 if (cachep
->flags
& SLAB_STORE_USER
)
2506 *dbg_userword(cachep
, objp
) = caller
;
2508 if (cachep
->flags
& SLAB_RED_ZONE
) {
2509 if (*dbg_redzone1(cachep
, objp
) != RED_INACTIVE
|| *dbg_redzone2(cachep
, objp
) != RED_INACTIVE
) {
2510 slab_error(cachep
, "double free, or memory outside"
2511 " object was overwritten");
2512 printk(KERN_ERR
"%p: redzone 1: 0x%lx, redzone 2: 0x%lx.\n",
2513 objp
, *dbg_redzone1(cachep
, objp
), *dbg_redzone2(cachep
, objp
));
2515 *dbg_redzone1(cachep
, objp
) = RED_ACTIVE
;
2516 *dbg_redzone2(cachep
, objp
) = RED_ACTIVE
;
2518 objp
+= obj_dbghead(cachep
);
2519 if (cachep
->ctor
&& cachep
->flags
& SLAB_POISON
) {
2520 unsigned long ctor_flags
= SLAB_CTOR_CONSTRUCTOR
;
2522 if (!(flags
& __GFP_WAIT
))
2523 ctor_flags
|= SLAB_CTOR_ATOMIC
;
2525 cachep
->ctor(objp
, cachep
, ctor_flags
);
2530 #define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
2533 static inline void *____cache_alloc(kmem_cache_t
*cachep
, gfp_t flags
)
2536 struct array_cache
*ac
;
2539 ac
= ac_data(cachep
);
2540 if (likely(ac
->avail
)) {
2541 STATS_INC_ALLOCHIT(cachep
);
2543 objp
= ac
->entry
[--ac
->avail
];
2545 STATS_INC_ALLOCMISS(cachep
);
2546 objp
= cache_alloc_refill(cachep
, flags
);
2551 static inline void *__cache_alloc(kmem_cache_t
*cachep
, gfp_t flags
)
2553 unsigned long save_flags
;
2556 cache_alloc_debugcheck_before(cachep
, flags
);
2558 local_irq_save(save_flags
);
2559 objp
= ____cache_alloc(cachep
, flags
);
2560 local_irq_restore(save_flags
);
2561 objp
= cache_alloc_debugcheck_after(cachep
, flags
, objp
,
2562 __builtin_return_address(0));
2569 * A interface to enable slab creation on nodeid
2571 static void *__cache_alloc_node(kmem_cache_t
*cachep
, gfp_t flags
, int nodeid
)
2573 struct list_head
*entry
;
2575 struct kmem_list3
*l3
;
2580 l3
= cachep
->nodelists
[nodeid
];
2584 spin_lock(&l3
->list_lock
);
2585 entry
= l3
->slabs_partial
.next
;
2586 if (entry
== &l3
->slabs_partial
) {
2587 l3
->free_touched
= 1;
2588 entry
= l3
->slabs_free
.next
;
2589 if (entry
== &l3
->slabs_free
)
2593 slabp
= list_entry(entry
, struct slab
, list
);
2594 check_spinlock_acquired_node(cachep
, nodeid
);
2595 check_slabp(cachep
, slabp
);
2597 STATS_INC_NODEALLOCS(cachep
);
2598 STATS_INC_ACTIVE(cachep
);
2599 STATS_SET_HIGH(cachep
);
2601 BUG_ON(slabp
->inuse
== cachep
->num
);
2603 /* get obj pointer */
2604 obj
= slabp
->s_mem
+ slabp
->free
*cachep
->objsize
;
2606 next
= slab_bufctl(slabp
)[slabp
->free
];
2608 slab_bufctl(slabp
)[slabp
->free
] = BUFCTL_FREE
;
2611 check_slabp(cachep
, slabp
);
2613 /* move slabp to correct slabp list: */
2614 list_del(&slabp
->list
);
2616 if (slabp
->free
== BUFCTL_END
) {
2617 list_add(&slabp
->list
, &l3
->slabs_full
);
2619 list_add(&slabp
->list
, &l3
->slabs_partial
);
2622 spin_unlock(&l3
->list_lock
);
2626 spin_unlock(&l3
->list_lock
);
2627 x
= cache_grow(cachep
, flags
, nodeid
);
2639 * Caller needs to acquire correct kmem_list's list_lock
2641 static void free_block(kmem_cache_t
*cachep
, void **objpp
, int nr_objects
, int node
)
2644 struct kmem_list3
*l3
;
2646 for (i
= 0; i
< nr_objects
; i
++) {
2647 void *objp
= objpp
[i
];
2651 slabp
= page_get_slab(virt_to_page(objp
));
2652 l3
= cachep
->nodelists
[node
];
2653 list_del(&slabp
->list
);
2654 objnr
= (objp
- slabp
->s_mem
) / cachep
->objsize
;
2655 check_spinlock_acquired_node(cachep
, node
);
2656 check_slabp(cachep
, slabp
);
2659 /* Verify that the slab belongs to the intended node */
2660 WARN_ON(slabp
->nodeid
!= node
);
2662 if (slab_bufctl(slabp
)[objnr
] != BUFCTL_FREE
) {
2663 printk(KERN_ERR
"slab: double free detected in cache "
2664 "'%s', objp %p\n", cachep
->name
, objp
);
2668 slab_bufctl(slabp
)[objnr
] = slabp
->free
;
2669 slabp
->free
= objnr
;
2670 STATS_DEC_ACTIVE(cachep
);
2673 check_slabp(cachep
, slabp
);
2675 /* fixup slab chains */
2676 if (slabp
->inuse
== 0) {
2677 if (l3
->free_objects
> l3
->free_limit
) {
2678 l3
->free_objects
-= cachep
->num
;
2679 slab_destroy(cachep
, slabp
);
2681 list_add(&slabp
->list
, &l3
->slabs_free
);
2684 /* Unconditionally move a slab to the end of the
2685 * partial list on free - maximum time for the
2686 * other objects to be freed, too.
2688 list_add_tail(&slabp
->list
, &l3
->slabs_partial
);
2693 static void cache_flusharray(kmem_cache_t
*cachep
, struct array_cache
*ac
)
2696 struct kmem_list3
*l3
;
2697 int node
= numa_node_id();
2699 batchcount
= ac
->batchcount
;
2701 BUG_ON(!batchcount
|| batchcount
> ac
->avail
);
2704 l3
= cachep
->nodelists
[node
];
2705 spin_lock(&l3
->list_lock
);
2707 struct array_cache
*shared_array
= l3
->shared
;
2708 int max
= shared_array
->limit
-shared_array
->avail
;
2710 if (batchcount
> max
)
2712 memcpy(&(shared_array
->entry
[shared_array
->avail
]),
2714 sizeof(void*)*batchcount
);
2715 shared_array
->avail
+= batchcount
;
2720 free_block(cachep
, ac
->entry
, batchcount
, node
);
2725 struct list_head
*p
;
2727 p
= l3
->slabs_free
.next
;
2728 while (p
!= &(l3
->slabs_free
)) {
2731 slabp
= list_entry(p
, struct slab
, list
);
2732 BUG_ON(slabp
->inuse
);
2737 STATS_SET_FREEABLE(cachep
, i
);
2740 spin_unlock(&l3
->list_lock
);
2741 ac
->avail
-= batchcount
;
2742 memmove(ac
->entry
, &(ac
->entry
[batchcount
]),
2743 sizeof(void*)*ac
->avail
);
2749 * Release an obj back to its cache. If the obj has a constructed
2750 * state, it must be in this state _before_ it is released.
2752 * Called with disabled ints.
2754 static inline void __cache_free(kmem_cache_t
*cachep
, void *objp
)
2756 struct array_cache
*ac
= ac_data(cachep
);
2759 objp
= cache_free_debugcheck(cachep
, objp
, __builtin_return_address(0));
2761 /* Make sure we are not freeing a object from another
2762 * node to the array cache on this cpu.
2767 slabp
= page_get_slab(virt_to_page(objp
));
2768 if (unlikely(slabp
->nodeid
!= numa_node_id())) {
2769 struct array_cache
*alien
= NULL
;
2770 int nodeid
= slabp
->nodeid
;
2771 struct kmem_list3
*l3
= cachep
->nodelists
[numa_node_id()];
2773 STATS_INC_NODEFREES(cachep
);
2774 if (l3
->alien
&& l3
->alien
[nodeid
]) {
2775 alien
= l3
->alien
[nodeid
];
2776 spin_lock(&alien
->lock
);
2777 if (unlikely(alien
->avail
== alien
->limit
))
2778 __drain_alien_cache(cachep
,
2780 alien
->entry
[alien
->avail
++] = objp
;
2781 spin_unlock(&alien
->lock
);
2783 spin_lock(&(cachep
->nodelists
[nodeid
])->
2785 free_block(cachep
, &objp
, 1, nodeid
);
2786 spin_unlock(&(cachep
->nodelists
[nodeid
])->
2793 if (likely(ac
->avail
< ac
->limit
)) {
2794 STATS_INC_FREEHIT(cachep
);
2795 ac
->entry
[ac
->avail
++] = objp
;
2798 STATS_INC_FREEMISS(cachep
);
2799 cache_flusharray(cachep
, ac
);
2800 ac
->entry
[ac
->avail
++] = objp
;
2805 * kmem_cache_alloc - Allocate an object
2806 * @cachep: The cache to allocate from.
2807 * @flags: See kmalloc().
2809 * Allocate an object from this cache. The flags are only relevant
2810 * if the cache has no available objects.
2812 void *kmem_cache_alloc(kmem_cache_t
*cachep
, gfp_t flags
)
2814 return __cache_alloc(cachep
, flags
);
2816 EXPORT_SYMBOL(kmem_cache_alloc
);
2819 * kmem_ptr_validate - check if an untrusted pointer might
2821 * @cachep: the cache we're checking against
2822 * @ptr: pointer to validate
2824 * This verifies that the untrusted pointer looks sane:
2825 * it is _not_ a guarantee that the pointer is actually
2826 * part of the slab cache in question, but it at least
2827 * validates that the pointer can be dereferenced and
2828 * looks half-way sane.
2830 * Currently only used for dentry validation.
2832 int fastcall
kmem_ptr_validate(kmem_cache_t
*cachep
, void *ptr
)
2834 unsigned long addr
= (unsigned long) ptr
;
2835 unsigned long min_addr
= PAGE_OFFSET
;
2836 unsigned long align_mask
= BYTES_PER_WORD
-1;
2837 unsigned long size
= cachep
->objsize
;
2840 if (unlikely(addr
< min_addr
))
2842 if (unlikely(addr
> (unsigned long)high_memory
- size
))
2844 if (unlikely(addr
& align_mask
))
2846 if (unlikely(!kern_addr_valid(addr
)))
2848 if (unlikely(!kern_addr_valid(addr
+ size
- 1)))
2850 page
= virt_to_page(ptr
);
2851 if (unlikely(!PageSlab(page
)))
2853 if (unlikely(page_get_cache(page
) != cachep
))
2862 * kmem_cache_alloc_node - Allocate an object on the specified node
2863 * @cachep: The cache to allocate from.
2864 * @flags: See kmalloc().
2865 * @nodeid: node number of the target node.
2867 * Identical to kmem_cache_alloc, except that this function is slow
2868 * and can sleep. And it will allocate memory on the given node, which
2869 * can improve the performance for cpu bound structures.
2870 * New and improved: it will now make sure that the object gets
2871 * put on the correct node list so that there is no false sharing.
2873 void *kmem_cache_alloc_node(kmem_cache_t
*cachep
, gfp_t flags
, int nodeid
)
2875 unsigned long save_flags
;
2879 return __cache_alloc(cachep
, flags
);
2881 if (unlikely(!cachep
->nodelists
[nodeid
])) {
2882 /* Fall back to __cache_alloc if we run into trouble */
2883 printk(KERN_WARNING
"slab: not allocating in inactive node %d for cache %s\n", nodeid
, cachep
->name
);
2884 return __cache_alloc(cachep
,flags
);
2887 cache_alloc_debugcheck_before(cachep
, flags
);
2888 local_irq_save(save_flags
);
2889 if (nodeid
== numa_node_id())
2890 ptr
= ____cache_alloc(cachep
, flags
);
2892 ptr
= __cache_alloc_node(cachep
, flags
, nodeid
);
2893 local_irq_restore(save_flags
);
2894 ptr
= cache_alloc_debugcheck_after(cachep
, flags
, ptr
, __builtin_return_address(0));
2898 EXPORT_SYMBOL(kmem_cache_alloc_node
);
2900 void *kmalloc_node(size_t size
, gfp_t flags
, int node
)
2902 kmem_cache_t
*cachep
;
2904 cachep
= kmem_find_general_cachep(size
, flags
);
2905 if (unlikely(cachep
== NULL
))
2907 return kmem_cache_alloc_node(cachep
, flags
, node
);
2909 EXPORT_SYMBOL(kmalloc_node
);
2913 * kmalloc - allocate memory
2914 * @size: how many bytes of memory are required.
2915 * @flags: the type of memory to allocate.
2917 * kmalloc is the normal method of allocating memory
2920 * The @flags argument may be one of:
2922 * %GFP_USER - Allocate memory on behalf of user. May sleep.
2924 * %GFP_KERNEL - Allocate normal kernel ram. May sleep.
2926 * %GFP_ATOMIC - Allocation will not sleep. Use inside interrupt handlers.
2928 * Additionally, the %GFP_DMA flag may be set to indicate the memory
2929 * must be suitable for DMA. This can mean different things on different
2930 * platforms. For example, on i386, it means that the memory must come
2931 * from the first 16MB.
2933 void *__kmalloc(size_t size
, gfp_t flags
)
2935 kmem_cache_t
*cachep
;
2937 /* If you want to save a few bytes .text space: replace
2939 * Then kmalloc uses the uninlined functions instead of the inline
2942 cachep
= __find_general_cachep(size
, flags
);
2943 if (unlikely(cachep
== NULL
))
2945 return __cache_alloc(cachep
, flags
);
2947 EXPORT_SYMBOL(__kmalloc
);
2951 * __alloc_percpu - allocate one copy of the object for every present
2952 * cpu in the system, zeroing them.
2953 * Objects should be dereferenced using the per_cpu_ptr macro only.
2955 * @size: how many bytes of memory are required.
2957 void *__alloc_percpu(size_t size
)
2960 struct percpu_data
*pdata
= kmalloc(sizeof (*pdata
), GFP_KERNEL
);
2966 * Cannot use for_each_online_cpu since a cpu may come online
2967 * and we have no way of figuring out how to fix the array
2968 * that we have allocated then....
2971 int node
= cpu_to_node(i
);
2973 if (node_online(node
))
2974 pdata
->ptrs
[i
] = kmalloc_node(size
, GFP_KERNEL
, node
);
2976 pdata
->ptrs
[i
] = kmalloc(size
, GFP_KERNEL
);
2978 if (!pdata
->ptrs
[i
])
2980 memset(pdata
->ptrs
[i
], 0, size
);
2983 /* Catch derefs w/o wrappers */
2984 return (void *) (~(unsigned long) pdata
);
2988 if (!cpu_possible(i
))
2990 kfree(pdata
->ptrs
[i
]);
2995 EXPORT_SYMBOL(__alloc_percpu
);
2999 * kmem_cache_free - Deallocate an object
3000 * @cachep: The cache the allocation was from.
3001 * @objp: The previously allocated object.
3003 * Free an object which was previously allocated from this
3006 void kmem_cache_free(kmem_cache_t
*cachep
, void *objp
)
3008 unsigned long flags
;
3010 local_irq_save(flags
);
3011 __cache_free(cachep
, objp
);
3012 local_irq_restore(flags
);
3014 EXPORT_SYMBOL(kmem_cache_free
);
3017 * kzalloc - allocate memory. The memory is set to zero.
3018 * @size: how many bytes of memory are required.
3019 * @flags: the type of memory to allocate.
3021 void *kzalloc(size_t size
, gfp_t flags
)
3023 void *ret
= kmalloc(size
, flags
);
3025 memset(ret
, 0, size
);
3028 EXPORT_SYMBOL(kzalloc
);
3031 * kfree - free previously allocated memory
3032 * @objp: pointer returned by kmalloc.
3034 * If @objp is NULL, no operation is performed.
3036 * Don't free memory not originally allocated by kmalloc()
3037 * or you will run into trouble.
3039 void kfree(const void *objp
)
3042 unsigned long flags
;
3044 if (unlikely(!objp
))
3046 local_irq_save(flags
);
3047 kfree_debugcheck(objp
);
3048 c
= page_get_cache(virt_to_page(objp
));
3049 __cache_free(c
, (void*)objp
);
3050 local_irq_restore(flags
);
3052 EXPORT_SYMBOL(kfree
);
3056 * free_percpu - free previously allocated percpu memory
3057 * @objp: pointer returned by alloc_percpu.
3059 * Don't free memory not originally allocated by alloc_percpu()
3060 * The complemented objp is to check for that.
3063 free_percpu(const void *objp
)
3066 struct percpu_data
*p
= (struct percpu_data
*) (~(unsigned long) objp
);
3069 * We allocate for all cpus so we cannot use for online cpu here.
3075 EXPORT_SYMBOL(free_percpu
);
3078 unsigned int kmem_cache_size(kmem_cache_t
*cachep
)
3080 return obj_reallen(cachep
);
3082 EXPORT_SYMBOL(kmem_cache_size
);
3084 const char *kmem_cache_name(kmem_cache_t
*cachep
)
3086 return cachep
->name
;
3088 EXPORT_SYMBOL_GPL(kmem_cache_name
);
3091 * This initializes kmem_list3 for all nodes.
3093 static int alloc_kmemlist(kmem_cache_t
*cachep
)
3096 struct kmem_list3
*l3
;
3099 for_each_online_node(node
) {
3100 struct array_cache
*nc
= NULL
, *new;
3101 struct array_cache
**new_alien
= NULL
;
3103 if (!(new_alien
= alloc_alien_cache(node
, cachep
->limit
)))
3106 if (!(new = alloc_arraycache(node
, (cachep
->shared
*
3107 cachep
->batchcount
), 0xbaadf00d)))
3109 if ((l3
= cachep
->nodelists
[node
])) {
3111 spin_lock_irq(&l3
->list_lock
);
3113 if ((nc
= cachep
->nodelists
[node
]->shared
))
3114 free_block(cachep
, nc
->entry
,
3118 if (!cachep
->nodelists
[node
]->alien
) {
3119 l3
->alien
= new_alien
;
3122 l3
->free_limit
= (1 + nr_cpus_node(node
))*
3123 cachep
->batchcount
+ cachep
->num
;
3124 spin_unlock_irq(&l3
->list_lock
);
3126 free_alien_cache(new_alien
);
3129 if (!(l3
= kmalloc_node(sizeof(struct kmem_list3
),
3133 kmem_list3_init(l3
);
3134 l3
->next_reap
= jiffies
+ REAPTIMEOUT_LIST3
+
3135 ((unsigned long)cachep
)%REAPTIMEOUT_LIST3
;
3137 l3
->alien
= new_alien
;
3138 l3
->free_limit
= (1 + nr_cpus_node(node
))*
3139 cachep
->batchcount
+ cachep
->num
;
3140 cachep
->nodelists
[node
] = l3
;
3148 struct ccupdate_struct
{
3149 kmem_cache_t
*cachep
;
3150 struct array_cache
*new[NR_CPUS
];
3153 static void do_ccupdate_local(void *info
)
3155 struct ccupdate_struct
*new = (struct ccupdate_struct
*)info
;
3156 struct array_cache
*old
;
3159 old
= ac_data(new->cachep
);
3161 new->cachep
->array
[smp_processor_id()] = new->new[smp_processor_id()];
3162 new->new[smp_processor_id()] = old
;
3166 static int do_tune_cpucache(kmem_cache_t
*cachep
, int limit
, int batchcount
,
3169 struct ccupdate_struct
new;
3172 memset(&new.new,0,sizeof(new.new));
3173 for_each_online_cpu(i
) {
3174 new.new[i
] = alloc_arraycache(cpu_to_node(i
), limit
, batchcount
);
3176 for (i
--; i
>= 0; i
--) kfree(new.new[i
]);
3180 new.cachep
= cachep
;
3182 smp_call_function_all_cpus(do_ccupdate_local
, (void *)&new);
3185 spin_lock_irq(&cachep
->spinlock
);
3186 cachep
->batchcount
= batchcount
;
3187 cachep
->limit
= limit
;
3188 cachep
->shared
= shared
;
3189 spin_unlock_irq(&cachep
->spinlock
);
3191 for_each_online_cpu(i
) {
3192 struct array_cache
*ccold
= new.new[i
];
3195 spin_lock_irq(&cachep
->nodelists
[cpu_to_node(i
)]->list_lock
);
3196 free_block(cachep
, ccold
->entry
, ccold
->avail
, cpu_to_node(i
));
3197 spin_unlock_irq(&cachep
->nodelists
[cpu_to_node(i
)]->list_lock
);
3201 err
= alloc_kmemlist(cachep
);
3203 printk(KERN_ERR
"alloc_kmemlist failed for %s, error %d.\n",
3204 cachep
->name
, -err
);
3211 static void enable_cpucache(kmem_cache_t
*cachep
)
3216 /* The head array serves three purposes:
3217 * - create a LIFO ordering, i.e. return objects that are cache-warm
3218 * - reduce the number of spinlock operations.
3219 * - reduce the number of linked list operations on the slab and
3220 * bufctl chains: array operations are cheaper.
3221 * The numbers are guessed, we should auto-tune as described by
3224 if (cachep
->objsize
> 131072)
3226 else if (cachep
->objsize
> PAGE_SIZE
)
3228 else if (cachep
->objsize
> 1024)
3230 else if (cachep
->objsize
> 256)
3235 /* Cpu bound tasks (e.g. network routing) can exhibit cpu bound
3236 * allocation behaviour: Most allocs on one cpu, most free operations
3237 * on another cpu. For these cases, an efficient object passing between
3238 * cpus is necessary. This is provided by a shared array. The array
3239 * replaces Bonwick's magazine layer.
3240 * On uniprocessor, it's functionally equivalent (but less efficient)
3241 * to a larger limit. Thus disabled by default.
3245 if (cachep
->objsize
<= PAGE_SIZE
)
3250 /* With debugging enabled, large batchcount lead to excessively
3251 * long periods with disabled local interrupts. Limit the
3257 err
= do_tune_cpucache(cachep
, limit
, (limit
+1)/2, shared
);
3259 printk(KERN_ERR
"enable_cpucache failed for %s, error %d.\n",
3260 cachep
->name
, -err
);
3263 static void drain_array_locked(kmem_cache_t
*cachep
,
3264 struct array_cache
*ac
, int force
, int node
)
3268 check_spinlock_acquired_node(cachep
, node
);
3269 if (ac
->touched
&& !force
) {
3271 } else if (ac
->avail
) {
3272 tofree
= force
? ac
->avail
: (ac
->limit
+4)/5;
3273 if (tofree
> ac
->avail
) {
3274 tofree
= (ac
->avail
+1)/2;
3276 free_block(cachep
, ac
->entry
, tofree
, node
);
3277 ac
->avail
-= tofree
;
3278 memmove(ac
->entry
, &(ac
->entry
[tofree
]),
3279 sizeof(void*)*ac
->avail
);
3284 * cache_reap - Reclaim memory from caches.
3285 * @unused: unused parameter
3287 * Called from workqueue/eventd every few seconds.
3289 * - clear the per-cpu caches for this CPU.
3290 * - return freeable pages to the main free memory pool.
3292 * If we cannot acquire the cache chain semaphore then just give up - we'll
3293 * try again on the next iteration.
3295 static void cache_reap(void *unused
)
3297 struct list_head
*walk
;
3298 struct kmem_list3
*l3
;
3300 if (down_trylock(&cache_chain_sem
)) {
3301 /* Give up. Setup the next iteration. */
3302 schedule_delayed_work(&__get_cpu_var(reap_work
), REAPTIMEOUT_CPUC
);
3306 list_for_each(walk
, &cache_chain
) {
3307 kmem_cache_t
*searchp
;
3308 struct list_head
* p
;
3312 searchp
= list_entry(walk
, kmem_cache_t
, next
);
3314 if (searchp
->flags
& SLAB_NO_REAP
)
3319 l3
= searchp
->nodelists
[numa_node_id()];
3321 drain_alien_cache(searchp
, l3
);
3322 spin_lock_irq(&l3
->list_lock
);
3324 drain_array_locked(searchp
, ac_data(searchp
), 0,
3327 if (time_after(l3
->next_reap
, jiffies
))
3330 l3
->next_reap
= jiffies
+ REAPTIMEOUT_LIST3
;
3333 drain_array_locked(searchp
, l3
->shared
, 0,
3336 if (l3
->free_touched
) {
3337 l3
->free_touched
= 0;
3341 tofree
= (l3
->free_limit
+5*searchp
->num
-1)/(5*searchp
->num
);
3343 p
= l3
->slabs_free
.next
;
3344 if (p
== &(l3
->slabs_free
))
3347 slabp
= list_entry(p
, struct slab
, list
);
3348 BUG_ON(slabp
->inuse
);
3349 list_del(&slabp
->list
);
3350 STATS_INC_REAPED(searchp
);
3352 /* Safe to drop the lock. The slab is no longer
3353 * linked to the cache.
3354 * searchp cannot disappear, we hold
3357 l3
->free_objects
-= searchp
->num
;
3358 spin_unlock_irq(&l3
->list_lock
);
3359 slab_destroy(searchp
, slabp
);
3360 spin_lock_irq(&l3
->list_lock
);
3361 } while(--tofree
> 0);
3363 spin_unlock_irq(&l3
->list_lock
);
3368 up(&cache_chain_sem
);
3369 drain_remote_pages();
3370 /* Setup the next iteration */
3371 schedule_delayed_work(&__get_cpu_var(reap_work
), REAPTIMEOUT_CPUC
);
3374 #ifdef CONFIG_PROC_FS
3376 static void print_slabinfo_header(struct seq_file
*m
)
3379 * Output format version, so at least we can change it
3380 * without _too_ many complaints.
3383 seq_puts(m
, "slabinfo - version: 2.1 (statistics)\n");
3385 seq_puts(m
, "slabinfo - version: 2.1\n");
3387 seq_puts(m
, "# name <active_objs> <num_objs> <objsize> "
3388 "<objperslab> <pagesperslab>");
3389 seq_puts(m
, " : tunables <limit> <batchcount> <sharedfactor>");
3390 seq_puts(m
, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
3392 seq_puts(m
, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
3393 "<error> <maxfreeable> <nodeallocs> <remotefrees>");
3394 seq_puts(m
, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
3399 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
3402 struct list_head
*p
;
3404 down(&cache_chain_sem
);
3406 print_slabinfo_header(m
);
3407 p
= cache_chain
.next
;
3410 if (p
== &cache_chain
)
3413 return list_entry(p
, kmem_cache_t
, next
);
3416 static void *s_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
3418 kmem_cache_t
*cachep
= p
;
3420 return cachep
->next
.next
== &cache_chain
? NULL
3421 : list_entry(cachep
->next
.next
, kmem_cache_t
, next
);
3424 static void s_stop(struct seq_file
*m
, void *p
)
3426 up(&cache_chain_sem
);
3429 static int s_show(struct seq_file
*m
, void *p
)
3431 kmem_cache_t
*cachep
= p
;
3432 struct list_head
*q
;
3434 unsigned long active_objs
;
3435 unsigned long num_objs
;
3436 unsigned long active_slabs
= 0;
3437 unsigned long num_slabs
, free_objects
= 0, shared_avail
= 0;
3441 struct kmem_list3
*l3
;
3444 spin_lock_irq(&cachep
->spinlock
);
3447 for_each_online_node(node
) {
3448 l3
= cachep
->nodelists
[node
];
3452 spin_lock(&l3
->list_lock
);
3454 list_for_each(q
,&l3
->slabs_full
) {
3455 slabp
= list_entry(q
, struct slab
, list
);
3456 if (slabp
->inuse
!= cachep
->num
&& !error
)
3457 error
= "slabs_full accounting error";
3458 active_objs
+= cachep
->num
;
3461 list_for_each(q
,&l3
->slabs_partial
) {
3462 slabp
= list_entry(q
, struct slab
, list
);
3463 if (slabp
->inuse
== cachep
->num
&& !error
)
3464 error
= "slabs_partial inuse accounting error";
3465 if (!slabp
->inuse
&& !error
)
3466 error
= "slabs_partial/inuse accounting error";
3467 active_objs
+= slabp
->inuse
;
3470 list_for_each(q
,&l3
->slabs_free
) {
3471 slabp
= list_entry(q
, struct slab
, list
);
3472 if (slabp
->inuse
&& !error
)
3473 error
= "slabs_free/inuse accounting error";
3476 free_objects
+= l3
->free_objects
;
3477 shared_avail
+= l3
->shared
->avail
;
3479 spin_unlock(&l3
->list_lock
);
3481 num_slabs
+=active_slabs
;
3482 num_objs
= num_slabs
*cachep
->num
;
3483 if (num_objs
- active_objs
!= free_objects
&& !error
)
3484 error
= "free_objects accounting error";
3486 name
= cachep
->name
;
3488 printk(KERN_ERR
"slab: cache %s error: %s\n", name
, error
);
3490 seq_printf(m
, "%-17s %6lu %6lu %6u %4u %4d",
3491 name
, active_objs
, num_objs
, cachep
->objsize
,
3492 cachep
->num
, (1<<cachep
->gfporder
));
3493 seq_printf(m
, " : tunables %4u %4u %4u",
3494 cachep
->limit
, cachep
->batchcount
,
3496 seq_printf(m
, " : slabdata %6lu %6lu %6lu",
3497 active_slabs
, num_slabs
, shared_avail
);
3500 unsigned long high
= cachep
->high_mark
;
3501 unsigned long allocs
= cachep
->num_allocations
;
3502 unsigned long grown
= cachep
->grown
;
3503 unsigned long reaped
= cachep
->reaped
;
3504 unsigned long errors
= cachep
->errors
;
3505 unsigned long max_freeable
= cachep
->max_freeable
;
3506 unsigned long node_allocs
= cachep
->node_allocs
;
3507 unsigned long node_frees
= cachep
->node_frees
;
3509 seq_printf(m
, " : globalstat %7lu %6lu %5lu %4lu \
3510 %4lu %4lu %4lu %4lu",
3511 allocs
, high
, grown
, reaped
, errors
,
3512 max_freeable
, node_allocs
, node_frees
);
3516 unsigned long allochit
= atomic_read(&cachep
->allochit
);
3517 unsigned long allocmiss
= atomic_read(&cachep
->allocmiss
);
3518 unsigned long freehit
= atomic_read(&cachep
->freehit
);
3519 unsigned long freemiss
= atomic_read(&cachep
->freemiss
);
3521 seq_printf(m
, " : cpustat %6lu %6lu %6lu %6lu",
3522 allochit
, allocmiss
, freehit
, freemiss
);
3526 spin_unlock_irq(&cachep
->spinlock
);
3531 * slabinfo_op - iterator that generates /proc/slabinfo
3540 * num-pages-per-slab
3541 * + further values on SMP and with statistics enabled
3544 struct seq_operations slabinfo_op
= {
3551 #define MAX_SLABINFO_WRITE 128
3553 * slabinfo_write - Tuning for the slab allocator
3555 * @buffer: user buffer
3556 * @count: data length
3559 ssize_t
slabinfo_write(struct file
*file
, const char __user
*buffer
,
3560 size_t count
, loff_t
*ppos
)
3562 char kbuf
[MAX_SLABINFO_WRITE
+1], *tmp
;
3563 int limit
, batchcount
, shared
, res
;
3564 struct list_head
*p
;
3566 if (count
> MAX_SLABINFO_WRITE
)
3568 if (copy_from_user(&kbuf
, buffer
, count
))
3570 kbuf
[MAX_SLABINFO_WRITE
] = '\0';
3572 tmp
= strchr(kbuf
, ' ');
3577 if (sscanf(tmp
, " %d %d %d", &limit
, &batchcount
, &shared
) != 3)
3580 /* Find the cache in the chain of caches. */
3581 down(&cache_chain_sem
);
3583 list_for_each(p
,&cache_chain
) {
3584 kmem_cache_t
*cachep
= list_entry(p
, kmem_cache_t
, next
);
3586 if (!strcmp(cachep
->name
, kbuf
)) {
3589 batchcount
> limit
||
3593 res
= do_tune_cpucache(cachep
, limit
,
3594 batchcount
, shared
);
3599 up(&cache_chain_sem
);
3607 * ksize - get the actual amount of memory allocated for a given object
3608 * @objp: Pointer to the object
3610 * kmalloc may internally round up allocations and return more memory
3611 * than requested. ksize() can be used to determine the actual amount of
3612 * memory allocated. The caller may use this additional memory, even though
3613 * a smaller amount of memory was initially specified with the kmalloc call.
3614 * The caller must guarantee that objp points to a valid object previously
3615 * allocated with either kmalloc() or kmem_cache_alloc(). The object
3616 * must not be freed during the duration of the call.
3618 unsigned int ksize(const void *objp
)
3620 if (unlikely(objp
== NULL
))
3623 return obj_reallen(page_get_cache(virt_to_page(objp
)));
3628 * kstrdup - allocate space for and copy an existing string
3630 * @s: the string to duplicate
3631 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
3633 char *kstrdup(const char *s
, gfp_t gfp
)
3641 len
= strlen(s
) + 1;
3642 buf
= kmalloc(len
, gfp
);
3644 memcpy(buf
, s
, len
);
3647 EXPORT_SYMBOL(kstrdup
);