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.
80 #include <linux/config.h>
81 #include <linux/slab.h>
83 #include <linux/swap.h>
84 #include <linux/cache.h>
85 #include <linux/interrupt.h>
86 #include <linux/init.h>
87 #include <linux/compiler.h>
88 #include <linux/seq_file.h>
89 #include <linux/notifier.h>
90 #include <linux/kallsyms.h>
91 #include <linux/cpu.h>
92 #include <linux/sysctl.h>
93 #include <linux/module.h>
95 #include <asm/uaccess.h>
96 #include <asm/cacheflush.h>
97 #include <asm/tlbflush.h>
100 * DEBUG - 1 for kmem_cache_create() to honour; SLAB_DEBUG_INITIAL,
101 * SLAB_RED_ZONE & SLAB_POISON.
102 * 0 for faster, smaller code (especially in the critical paths).
104 * STATS - 1 to collect stats for /proc/slabinfo.
105 * 0 for faster, smaller code (especially in the critical paths).
107 * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
110 #ifdef CONFIG_DEBUG_SLAB
113 #define FORCED_DEBUG 1
117 #define FORCED_DEBUG 0
121 /* Shouldn't this be in a header file somewhere? */
122 #define BYTES_PER_WORD sizeof(void *)
124 /* Legal flag mask for kmem_cache_create(). */
126 # define CREATE_MASK (SLAB_DEBUG_INITIAL | SLAB_RED_ZONE | \
127 SLAB_POISON | SLAB_HWCACHE_ALIGN | \
128 SLAB_NO_REAP | SLAB_CACHE_DMA | \
129 SLAB_MUST_HWCACHE_ALIGN | SLAB_STORE_USER | \
130 SLAB_RECLAIM_ACCOUNT )
132 # define CREATE_MASK (SLAB_HWCACHE_ALIGN | SLAB_NO_REAP | \
133 SLAB_CACHE_DMA | SLAB_MUST_HWCACHE_ALIGN | \
134 SLAB_RECLAIM_ACCOUNT)
140 * Bufctl's are used for linking objs within a slab
143 * This implementation relies on "struct page" for locating the cache &
144 * slab an object belongs to.
145 * This allows the bufctl structure to be small (one int), but limits
146 * the number of objects a slab (not a cache) can contain when off-slab
147 * bufctls are used. The limit is the size of the largest general cache
148 * that does not use off-slab slabs.
149 * For 32bit archs with 4 kB pages, is this 56.
150 * This is not serious, as it is only for large objects, when it is unwise
151 * to have too many per slab.
152 * Note: This limit can be raised by introducing a general cache whose size
153 * is less than 512 (PAGE_SIZE<<3), but greater than 256.
156 #define BUFCTL_END 0xffffFFFF
157 #define BUFCTL_FREE 0xffffFFFE
158 #define SLAB_LIMIT 0xffffFFFD
159 typedef unsigned int kmem_bufctl_t
;
161 /* Max number of objs-per-slab for caches which use off-slab slabs.
162 * Needed to avoid a possible looping condition in cache_grow().
164 static unsigned long offslab_limit
;
169 * Manages the objs in a slab. Placed either at the beginning of mem allocated
170 * for a slab, or allocated from an general cache.
171 * Slabs are chained into three list: fully used, partial, fully free slabs.
174 struct list_head list
;
175 unsigned long colouroff
;
176 void *s_mem
; /* including colour offset */
177 unsigned int inuse
; /* num of objs active in slab */
186 * - LIFO ordering, to hand out cache-warm objects from _alloc
187 * - reduce the number of linked list operations
188 * - reduce spinlock operations
190 * The limit is stored in the per-cpu structure to reduce the data cache
197 unsigned int batchcount
;
198 unsigned int touched
;
201 /* bootstrap: The caches do not work without cpuarrays anymore,
202 * but the cpuarrays are allocated from the generic caches...
204 #define BOOT_CPUCACHE_ENTRIES 1
205 struct arraycache_init
{
206 struct array_cache cache
;
207 void * entries
[BOOT_CPUCACHE_ENTRIES
];
211 * The slab lists of all objects.
212 * Hopefully reduce the internal fragmentation
213 * NUMA: The spinlock could be moved from the kmem_cache_t
214 * into this structure, too. Figure out what causes
215 * fewer cross-node spinlock operations.
218 struct list_head slabs_partial
; /* partial list first, better asm code */
219 struct list_head slabs_full
;
220 struct list_head slabs_free
;
221 unsigned long free_objects
;
223 unsigned long next_reap
;
224 struct array_cache
*shared
;
227 #define LIST3_INIT(parent) \
229 .slabs_full = LIST_HEAD_INIT(parent.slabs_full), \
230 .slabs_partial = LIST_HEAD_INIT(parent.slabs_partial), \
231 .slabs_free = LIST_HEAD_INIT(parent.slabs_free) \
233 #define list3_data(cachep) \
237 #define list3_data_ptr(cachep, ptr) \
246 struct kmem_cache_s
{
247 /* 1) per-cpu data, touched during every alloc/free */
248 struct array_cache
*array
[NR_CPUS
];
249 unsigned int batchcount
;
251 /* 2) touched by every alloc & free from the backend */
252 struct kmem_list3 lists
;
253 /* NUMA: kmem_3list_t *nodelists[MAX_NUMNODES] */
254 unsigned int objsize
;
255 unsigned int flags
; /* constant flags */
256 unsigned int num
; /* # of objs per slab */
257 unsigned int free_limit
; /* upper limit of objects in the lists */
260 /* 3) cache_grow/shrink */
261 /* order of pgs per slab (2^n) */
262 unsigned int gfporder
;
264 /* force GFP flags, e.g. GFP_DMA */
265 unsigned int gfpflags
;
267 size_t colour
; /* cache colouring range */
268 unsigned int colour_off
; /* colour offset */
269 unsigned int colour_next
; /* cache colouring */
270 kmem_cache_t
*slabp_cache
;
271 unsigned int dflags
; /* dynamic flags */
273 /* constructor func */
274 void (*ctor
)(void *, kmem_cache_t
*, unsigned long);
276 /* de-constructor func */
277 void (*dtor
)(void *, kmem_cache_t
*, unsigned long);
279 /* 4) cache creation/removal */
281 struct list_head next
;
285 unsigned long num_active
;
286 unsigned long num_allocations
;
287 unsigned long high_mark
;
289 unsigned long reaped
;
290 unsigned long errors
;
291 unsigned long max_freeable
;
303 #define CFLGS_OFF_SLAB (0x80000000UL)
304 #define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
306 #define BATCHREFILL_LIMIT 16
307 /* Optimization question: fewer reaps means less
308 * probability for unnessary cpucache drain/refill cycles.
310 * OTHO the cpuarrays can contain lots of objects,
311 * which could lock up otherwise freeable slabs.
313 #define REAPTIMEOUT_CPUC (2*HZ)
314 #define REAPTIMEOUT_LIST3 (4*HZ)
317 #define STATS_INC_ACTIVE(x) ((x)->num_active++)
318 #define STATS_DEC_ACTIVE(x) ((x)->num_active--)
319 #define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
320 #define STATS_INC_GROWN(x) ((x)->grown++)
321 #define STATS_INC_REAPED(x) ((x)->reaped++)
322 #define STATS_SET_HIGH(x) do { if ((x)->num_active > (x)->high_mark) \
323 (x)->high_mark = (x)->num_active; \
325 #define STATS_INC_ERR(x) ((x)->errors++)
326 #define STATS_SET_FREEABLE(x, i) \
327 do { if ((x)->max_freeable < i) \
328 (x)->max_freeable = i; \
331 #define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
332 #define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
333 #define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
334 #define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
336 #define STATS_INC_ACTIVE(x) do { } while (0)
337 #define STATS_DEC_ACTIVE(x) do { } while (0)
338 #define STATS_INC_ALLOCED(x) do { } while (0)
339 #define STATS_INC_GROWN(x) do { } while (0)
340 #define STATS_INC_REAPED(x) do { } while (0)
341 #define STATS_SET_HIGH(x) do { } while (0)
342 #define STATS_INC_ERR(x) do { } while (0)
343 #define STATS_SET_FREEABLE(x, i) \
346 #define STATS_INC_ALLOCHIT(x) do { } while (0)
347 #define STATS_INC_ALLOCMISS(x) do { } while (0)
348 #define STATS_INC_FREEHIT(x) do { } while (0)
349 #define STATS_INC_FREEMISS(x) do { } while (0)
353 /* Magic nums for obj red zoning.
354 * Placed in the first word before and the first word after an obj.
356 #define RED_INACTIVE 0x5A2CF071UL /* when obj is inactive */
357 #define RED_ACTIVE 0x170FC2A5UL /* when obj is active */
359 /* ...and for poisoning */
360 #define POISON_BEFORE 0x5a /* for use-uninitialised poisoning */
361 #define POISON_AFTER 0x6b /* for use-after-free poisoning */
362 #define POISON_END 0xa5 /* end-byte of poisoning */
364 /* memory layout of objects:
366 * 0 .. cachep->dbghead - BYTES_PER_WORD - 1: padding. This ensures that
367 * the end of an object is aligned with the end of the real
368 * allocation. Catches writes behind the end of the allocation.
369 * cachep->dbghead - BYTES_PER_WORD .. cachep->dbghead - 1:
371 * cachep->dbghead: The real object.
372 * cachep->objsize - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
373 * cachep->objsize - 1* BYTES_PER_WORD: last caller address [BYTES_PER_WORD long]
375 static inline int obj_dbghead(kmem_cache_t
*cachep
)
377 return cachep
->dbghead
;
380 static inline int obj_reallen(kmem_cache_t
*cachep
)
382 return cachep
->reallen
;
385 static unsigned long *dbg_redzone1(kmem_cache_t
*cachep
, void *objp
)
387 BUG_ON(!(cachep
->flags
& SLAB_RED_ZONE
));
388 return (unsigned long*) (objp
+obj_dbghead(cachep
)-BYTES_PER_WORD
);
391 static unsigned long *dbg_redzone2(kmem_cache_t
*cachep
, void *objp
)
393 BUG_ON(!(cachep
->flags
& SLAB_RED_ZONE
));
394 if (cachep
->flags
& SLAB_STORE_USER
)
395 return (unsigned long*) (objp
+cachep
->objsize
-2*BYTES_PER_WORD
);
396 return (unsigned long*) (objp
+cachep
->objsize
-BYTES_PER_WORD
);
399 static void **dbg_userword(kmem_cache_t
*cachep
, void *objp
)
401 BUG_ON(!(cachep
->flags
& SLAB_STORE_USER
));
402 return (void**)(objp
+cachep
->objsize
-BYTES_PER_WORD
);
405 static inline int obj_dbghead(kmem_cache_t
*cachep
)
409 static inline int obj_reallen(kmem_cache_t
*cachep
)
411 return cachep
->objsize
;
413 static inline unsigned long *dbg_redzone1(kmem_cache_t
*cachep
, void *objp
)
418 static inline unsigned long *dbg_redzone2(kmem_cache_t
*cachep
, void *objp
)
423 static inline void **dbg_userword(kmem_cache_t
*cachep
, void *objp
)
431 * Maximum size of an obj (in 2^order pages)
432 * and absolute limit for the gfp order.
434 #if defined(CONFIG_LARGE_ALLOCS)
435 #define MAX_OBJ_ORDER 13 /* up to 32Mb */
436 #define MAX_GFP_ORDER 13 /* up to 32Mb */
437 #elif defined(CONFIG_MMU)
438 #define MAX_OBJ_ORDER 5 /* 32 pages */
439 #define MAX_GFP_ORDER 5 /* 32 pages */
441 #define MAX_OBJ_ORDER 8 /* up to 1Mb */
442 #define MAX_GFP_ORDER 8 /* up to 1Mb */
446 * Do not go above this order unless 0 objects fit into the slab.
448 #define BREAK_GFP_ORDER_HI 2
449 #define BREAK_GFP_ORDER_LO 1
450 static int slab_break_gfp_order
= BREAK_GFP_ORDER_LO
;
452 /* Macros for storing/retrieving the cachep and or slab from the
453 * global 'mem_map'. These are used to find the slab an obj belongs to.
454 * With kfree(), these are used to find the cache which an obj belongs to.
456 #define SET_PAGE_CACHE(pg,x) ((pg)->list.next = (struct list_head *)(x))
457 #define GET_PAGE_CACHE(pg) ((kmem_cache_t *)(pg)->list.next)
458 #define SET_PAGE_SLAB(pg,x) ((pg)->list.prev = (struct list_head *)(x))
459 #define GET_PAGE_SLAB(pg) ((struct slab *)(pg)->list.prev)
461 /* These are the default caches for kmalloc. Custom caches can have other sizes. */
462 struct cache_sizes malloc_sizes
[] = {
463 #define CACHE(x) { .cs_size = (x) },
464 #include <linux/kmalloc_sizes.h>
469 EXPORT_SYMBOL(malloc_sizes
);
471 /* Must match cache_sizes above. Out of line to keep cache footprint low. */
472 static struct cache_names
{
476 #define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },
477 #include <linux/kmalloc_sizes.h>
482 struct arraycache_init initarray_cache __initdata
= { { 0, BOOT_CPUCACHE_ENTRIES
, 1, 0} };
483 struct arraycache_init initarray_generic __initdata
= { { 0, BOOT_CPUCACHE_ENTRIES
, 1, 0} };
485 /* internal cache of cache description objs */
486 static kmem_cache_t cache_cache
= {
487 .lists
= LIST3_INIT(cache_cache
.lists
),
489 .limit
= BOOT_CPUCACHE_ENTRIES
,
490 .objsize
= sizeof(kmem_cache_t
),
491 .flags
= SLAB_NO_REAP
,
492 .spinlock
= SPIN_LOCK_UNLOCKED
,
493 .colour_off
= L1_CACHE_BYTES
,
494 .name
= "kmem_cache",
497 /* Guard access to the cache-chain. */
498 static struct semaphore cache_chain_sem
;
500 struct list_head cache_chain
;
503 * vm_enough_memory() looks at this to determine how many
504 * slab-allocated pages are possibly freeable under pressure
506 * SLAB_RECLAIM_ACCOUNT turns this on per-slab
508 atomic_t slab_reclaim_pages
;
509 EXPORT_SYMBOL(slab_reclaim_pages
);
512 * chicken and egg problem: delay the per-cpu array allocation
513 * until the general caches are up.
521 static DEFINE_PER_CPU(struct timer_list
, reap_timers
);
523 static void reap_timer_fnc(unsigned long data
);
525 static void enable_cpucache (kmem_cache_t
*cachep
);
527 /* Cal the num objs, wastage, and bytes left over for a given slab size. */
528 static void cache_estimate (unsigned long gfporder
, size_t size
,
529 int flags
, size_t *left_over
, unsigned int *num
)
532 size_t wastage
= PAGE_SIZE
<<gfporder
;
536 if (!(flags
& CFLGS_OFF_SLAB
)) {
537 base
= sizeof(struct slab
);
538 extra
= sizeof(kmem_bufctl_t
);
541 while (i
*size
+ L1_CACHE_ALIGN(base
+i
*extra
) <= wastage
)
551 wastage
-= L1_CACHE_ALIGN(base
+i
*extra
);
552 *left_over
= wastage
;
555 #define slab_error(cachep, msg) __slab_error(__FUNCTION__, cachep, msg)
557 static void __slab_error(const char *function
, kmem_cache_t
*cachep
, char *msg
)
559 printk(KERN_ERR
"slab error in %s(): cache `%s': %s\n",
560 function
, cachep
->name
, msg
);
565 * Start the reap timer running on the target CPU. We run at around 1 to 2Hz.
566 * Add the CPU number into the expiry time to minimize the possibility of the
567 * CPUs getting into lockstep and contending for the global cache chain lock.
569 static void start_cpu_timer(int cpu
)
571 struct timer_list
*rt
= &per_cpu(reap_timers
, cpu
);
573 if (rt
->function
== NULL
) {
575 rt
->expires
= jiffies
+ HZ
+ 3*cpu
;
576 rt
->function
= reap_timer_fnc
;
577 add_timer_on(rt
, cpu
);
582 * Note: if someone calls kmem_cache_alloc() on the new
583 * cpu before the cpuup callback had a chance to allocate
584 * the head arrays, it will oops.
585 * Is CPU_ONLINE early enough?
587 static int __devinit
cpuup_callback(struct notifier_block
*nfb
,
588 unsigned long action
,
591 long cpu
= (long)hcpu
;
596 down(&cache_chain_sem
);
597 list_for_each(p
, &cache_chain
) {
599 struct array_cache
*nc
;
601 kmem_cache_t
* cachep
= list_entry(p
, kmem_cache_t
, next
);
602 memsize
= sizeof(void*)*cachep
->limit
+sizeof(struct array_cache
);
603 nc
= kmalloc(memsize
, GFP_KERNEL
);
607 nc
->limit
= cachep
->limit
;
608 nc
->batchcount
= cachep
->batchcount
;
611 spin_lock_irq(&cachep
->spinlock
);
612 cachep
->array
[cpu
] = nc
;
613 cachep
->free_limit
= (1+num_online_cpus())*cachep
->batchcount
615 spin_unlock_irq(&cachep
->spinlock
);
618 up(&cache_chain_sem
);
621 if (g_cpucache_up
== FULL
)
622 start_cpu_timer(cpu
);
624 case CPU_UP_CANCELED
:
625 down(&cache_chain_sem
);
627 list_for_each(p
, &cache_chain
) {
628 struct array_cache
*nc
;
629 kmem_cache_t
* cachep
= list_entry(p
, kmem_cache_t
, next
);
631 nc
= cachep
->array
[cpu
];
632 cachep
->array
[cpu
] = NULL
;
635 up(&cache_chain_sem
);
640 up(&cache_chain_sem
);
644 static struct notifier_block cpucache_notifier
= { &cpuup_callback
, NULL
, 0 };
646 static inline void ** ac_entry(struct array_cache
*ac
)
648 return (void**)(ac
+1);
651 static inline struct array_cache
*ac_data(kmem_cache_t
*cachep
)
653 return cachep
->array
[smp_processor_id()];
657 * Called after the gfp() functions have been enabled, and before smp_init().
659 void __init
kmem_cache_init(void)
662 struct cache_sizes
*sizes
;
663 struct cache_names
*names
;
666 * Fragmentation resistance on low memory - only use bigger
667 * page orders on machines with more than 32MB of memory.
669 if (num_physpages
> (32 << 20) >> PAGE_SHIFT
)
670 slab_break_gfp_order
= BREAK_GFP_ORDER_HI
;
673 /* Bootstrap is tricky, because several objects are allocated
674 * from caches that do not exist yet:
675 * 1) initialize the cache_cache cache: it contains the kmem_cache_t
676 * structures of all caches, except cache_cache itself: cache_cache
677 * is statically allocated.
678 * Initially an __init data area is used for the head array, it's
679 * replaced with a kmalloc allocated array at the end of the bootstrap.
680 * 2) Create the first kmalloc cache.
681 * The kmem_cache_t for the new cache is allocated normally. An __init
682 * data area is used for the head array.
683 * 3) Create the remaining kmalloc caches, with minimally sized head arrays.
684 * 4) Replace the __init data head arrays for cache_cache and the first
685 * kmalloc cache with kmalloc allocated arrays.
686 * 5) Resize the head arrays of the kmalloc caches to their final sizes.
689 /* 1) create the cache_cache */
690 init_MUTEX(&cache_chain_sem
);
691 INIT_LIST_HEAD(&cache_chain
);
692 list_add(&cache_cache
.next
, &cache_chain
);
693 cache_cache
.array
[smp_processor_id()] = &initarray_cache
.cache
;
695 cache_estimate(0, cache_cache
.objsize
, 0,
696 &left_over
, &cache_cache
.num
);
697 if (!cache_cache
.num
)
700 cache_cache
.colour
= left_over
/cache_cache
.colour_off
;
701 cache_cache
.colour_next
= 0;
704 /* 2+3) create the kmalloc caches */
705 sizes
= malloc_sizes
;
708 while (sizes
->cs_size
) {
709 /* For performance, all the general caches are L1 aligned.
710 * This should be particularly beneficial on SMP boxes, as it
711 * eliminates "false sharing".
712 * Note for systems short on memory removing the alignment will
713 * allow tighter packing of the smaller caches. */
714 sizes
->cs_cachep
= kmem_cache_create(
715 names
->name
, sizes
->cs_size
,
716 0, SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
717 if (!sizes
->cs_cachep
)
720 /* Inc off-slab bufctl limit until the ceiling is hit. */
721 if (!(OFF_SLAB(sizes
->cs_cachep
))) {
722 offslab_limit
= sizes
->cs_size
-sizeof(struct slab
);
723 offslab_limit
/= sizeof(kmem_bufctl_t
);
726 sizes
->cs_dmacachep
= kmem_cache_create(
727 names
->name_dma
, sizes
->cs_size
,
728 0, SLAB_CACHE_DMA
|SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
729 if (!sizes
->cs_dmacachep
)
735 /* 4) Replace the bootstrap head arrays */
739 ptr
= kmalloc(sizeof(struct arraycache_init
), GFP_KERNEL
);
741 BUG_ON(ac_data(&cache_cache
) != &initarray_cache
.cache
);
742 memcpy(ptr
, ac_data(&cache_cache
), sizeof(struct arraycache_init
));
743 cache_cache
.array
[smp_processor_id()] = ptr
;
746 ptr
= kmalloc(sizeof(struct arraycache_init
), GFP_KERNEL
);
748 BUG_ON(ac_data(malloc_sizes
[0].cs_cachep
) != &initarray_generic
.cache
);
749 memcpy(ptr
, ac_data(malloc_sizes
[0].cs_cachep
),
750 sizeof(struct arraycache_init
));
751 malloc_sizes
[0].cs_cachep
->array
[smp_processor_id()] = ptr
;
755 /* 5) resize the head arrays to their final sizes */
757 kmem_cache_t
*cachep
;
758 down(&cache_chain_sem
);
759 list_for_each_entry(cachep
, &cache_chain
, next
)
760 enable_cpucache(cachep
);
761 up(&cache_chain_sem
);
765 g_cpucache_up
= FULL
;
767 /* Register a cpu startup notifier callback
768 * that initializes ac_data for all new cpus
770 register_cpu_notifier(&cpucache_notifier
);
773 /* The reap timers are started later, with a module init call:
774 * That part of the kernel is not yet operational.
778 int __init
cpucache_init(void)
783 * Register the timers that return unneeded
786 for (cpu
= 0; cpu
< NR_CPUS
; cpu
++) {
788 start_cpu_timer(cpu
);
794 __initcall(cpucache_init
);
796 /* Interface to system's page allocator. No need to hold the cache-lock.
798 static inline void * kmem_getpages (kmem_cache_t
*cachep
, unsigned long flags
)
803 * If we requested dmaable memory, we will get it. Even if we
804 * did not request dmaable memory, we might get it, but that
805 * would be relatively rare and ignorable.
807 flags
|= cachep
->gfpflags
;
808 if ( cachep
->flags
& SLAB_RECLAIM_ACCOUNT
)
809 atomic_add(1<<cachep
->gfporder
, &slab_reclaim_pages
);
810 addr
= (void*) __get_free_pages(flags
, cachep
->gfporder
);
811 /* Assume that now we have the pages no one else can legally
812 * messes with the 'struct page's.
813 * However vm_scan() might try to test the structure to see if
814 * it is a named-page or buffer-page. The members it tests are
815 * of no interest here.....
820 /* Interface to system's page release. */
821 static inline void kmem_freepages (kmem_cache_t
*cachep
, void *addr
)
823 unsigned long i
= (1<<cachep
->gfporder
);
824 struct page
*page
= virt_to_page(addr
);
825 const unsigned long nr_freed
= i
;
827 /* free_pages() does not clear the type bit - we do that.
828 * The pages have been unlinked from their cache-slab,
829 * but their 'struct page's might be accessed in
830 * vm_scan(). Shouldn't be a worry.
833 if (!TestClearPageSlab(page
))
837 sub_page_state(nr_slab
, nr_freed
);
838 if (current
->reclaim_state
)
839 current
->reclaim_state
->reclaimed_slab
+= nr_freed
;
840 free_pages((unsigned long)addr
, cachep
->gfporder
);
841 if (cachep
->flags
& SLAB_RECLAIM_ACCOUNT
)
842 atomic_sub(1<<cachep
->gfporder
, &slab_reclaim_pages
);
847 #ifdef CONFIG_DEBUG_PAGEALLOC
848 static void store_stackinfo(kmem_cache_t
*cachep
, unsigned long *addr
, unsigned long caller
)
850 int size
= obj_reallen(cachep
);
852 addr
= (unsigned long *)&((char*)addr
)[obj_dbghead(cachep
)];
854 if (size
< 5*sizeof(unsigned long))
859 *addr
++=smp_processor_id();
860 size
-= 3*sizeof(unsigned long);
862 unsigned long *sptr
= &caller
;
863 unsigned long svalue
;
865 while (((long) sptr
& (THREAD_SIZE
-1)) != 0) {
867 if (kernel_text_address(svalue
)) {
869 size
-= sizeof(unsigned long);
870 if (size
<= sizeof(unsigned long))
880 static void poison_obj(kmem_cache_t
*cachep
, void *addr
, unsigned char val
)
882 int size
= obj_reallen(cachep
);
883 addr
= &((char*)addr
)[obj_dbghead(cachep
)];
885 memset(addr
, val
, size
);
886 *(unsigned char *)(addr
+size
-1) = POISON_END
;
889 static void *scan_poisoned_obj(unsigned char* addr
, unsigned int size
)
893 end
= addr
+ size
- 1;
895 for (; addr
< end
; addr
++) {
896 if (*addr
!= POISON_BEFORE
&& *addr
!= POISON_AFTER
)
899 if (*addr
!= POISON_END
)
904 static void check_poison_obj(kmem_cache_t
*cachep
, void *objp
)
908 int size
= obj_reallen(cachep
);
910 realobj
= objp
+obj_dbghead(cachep
);
912 end
= scan_poisoned_obj(realobj
, size
);
915 printk(KERN_ERR
"Slab corruption: start=%p, expend=%p, "
916 "problemat=%p\n", realobj
, realobj
+size
-1, end
);
917 if (cachep
->flags
& SLAB_STORE_USER
) {
918 printk(KERN_ERR
"Last user: [<%p>]", *dbg_userword(cachep
, objp
));
919 print_symbol("(%s)", (unsigned long)*dbg_userword(cachep
, objp
));
922 printk(KERN_ERR
"Data: ");
923 for (s
= 0; s
< size
; s
++) {
924 if (((char*)realobj
)[s
] == POISON_BEFORE
)
926 else if (((char*)realobj
)[s
] == POISON_AFTER
)
929 printk("%02X ", ((unsigned char*)realobj
)[s
]);
932 printk(KERN_ERR
"Next: ");
933 for (; s
< size
+ 32; s
++) {
934 if (((char*)realobj
)[s
] == POISON_BEFORE
)
936 else if (((char*)realobj
)[s
] == POISON_AFTER
)
939 printk("%02X ", ((unsigned char*)realobj
)[s
]);
942 slab_error(cachep
, "object was modified after freeing");
947 /* Destroy all the objs in a slab, and release the mem back to the system.
948 * Before calling the slab must have been unlinked from the cache.
949 * The cache-lock is not held/needed.
951 static void slab_destroy (kmem_cache_t
*cachep
, struct slab
*slabp
)
955 for (i
= 0; i
< cachep
->num
; i
++) {
956 void *objp
= slabp
->s_mem
+ cachep
->objsize
* i
;
958 if (cachep
->flags
& SLAB_POISON
) {
959 #ifdef CONFIG_DEBUG_PAGEALLOC
960 if ((cachep
->objsize
%PAGE_SIZE
)==0 && OFF_SLAB(cachep
))
961 kernel_map_pages(virt_to_page(objp
), cachep
->objsize
/PAGE_SIZE
,1);
963 check_poison_obj(cachep
, objp
);
965 check_poison_obj(cachep
, objp
);
968 if (cachep
->flags
& SLAB_RED_ZONE
) {
969 if (*dbg_redzone1(cachep
, objp
) != RED_INACTIVE
)
970 slab_error(cachep
, "start of a freed object "
972 if (*dbg_redzone2(cachep
, objp
) != RED_INACTIVE
)
973 slab_error(cachep
, "end of a freed object "
976 if (cachep
->dtor
&& !(cachep
->flags
& SLAB_POISON
))
977 (cachep
->dtor
)(objp
+obj_dbghead(cachep
), cachep
, 0);
982 for (i
= 0; i
< cachep
->num
; i
++) {
983 void* objp
= slabp
->s_mem
+cachep
->objsize
*i
;
984 (cachep
->dtor
)(objp
, cachep
, 0);
989 kmem_freepages(cachep
, slabp
->s_mem
-slabp
->colouroff
);
990 if (OFF_SLAB(cachep
))
991 kmem_cache_free(cachep
->slabp_cache
, slabp
);
995 * kmem_cache_create - Create a cache.
996 * @name: A string which is used in /proc/slabinfo to identify this cache.
997 * @size: The size of objects to be created in this cache.
998 * @offset: The offset to use within the page.
1000 * @ctor: A constructor for the objects.
1001 * @dtor: A destructor for the objects.
1003 * Returns a ptr to the cache on success, NULL on failure.
1004 * Cannot be called within a int, but can be interrupted.
1005 * The @ctor is run when new pages are allocated by the cache
1006 * and the @dtor is run before the pages are handed back.
1008 * @name must be valid until the cache is destroyed. This implies that
1009 * the module calling this has to destroy the cache before getting
1014 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
1015 * to catch references to uninitialised memory.
1017 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
1018 * for buffer overruns.
1020 * %SLAB_NO_REAP - Don't automatically reap this cache when we're under
1023 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
1024 * cacheline. This can be beneficial if you're counting cycles as closely
1028 kmem_cache_create (const char *name
, size_t size
, size_t offset
,
1029 unsigned long flags
, void (*ctor
)(void*, kmem_cache_t
*, unsigned long),
1030 void (*dtor
)(void*, kmem_cache_t
*, unsigned long))
1032 const char *func_nm
= KERN_ERR
"kmem_create: ";
1033 size_t left_over
, align
, slab_size
;
1034 kmem_cache_t
*cachep
= NULL
;
1037 * Sanity checks... these are all serious usage bugs.
1041 (size
< BYTES_PER_WORD
) ||
1042 (size
> (1<<MAX_OBJ_ORDER
)*PAGE_SIZE
) ||
1044 (offset
< 0 || offset
> size
))
1048 WARN_ON(strchr(name
, ' ')); /* It confuses parsers */
1049 if ((flags
& SLAB_DEBUG_INITIAL
) && !ctor
) {
1050 /* No constructor, but inital state check requested */
1051 printk("%sNo con, but init state check requested - %s\n", func_nm
, name
);
1052 flags
&= ~SLAB_DEBUG_INITIAL
;
1057 * Enable redzoning and last user accounting, except
1058 * - for caches with forced alignment: redzoning would violate the
1060 * - for caches with large objects, if the increased size would
1061 * increase the object size above the next power of two: caches
1062 * with object sizes just above a power of two have a significant
1063 * amount of internal fragmentation
1065 if ((size
< 4096 || fls(size
-1) == fls(size
-1+3*BYTES_PER_WORD
))
1066 && !(flags
& SLAB_MUST_HWCACHE_ALIGN
)) {
1067 flags
|= SLAB_RED_ZONE
|SLAB_STORE_USER
;
1069 flags
|= SLAB_POISON
;
1074 * Always checks flags, a caller might be expecting debug
1075 * support which isn't available.
1077 if (flags
& ~CREATE_MASK
)
1080 /* Get cache's description obj. */
1081 cachep
= (kmem_cache_t
*) kmem_cache_alloc(&cache_cache
, SLAB_KERNEL
);
1084 memset(cachep
, 0, sizeof(kmem_cache_t
));
1087 cachep
->reallen
= size
;
1089 /* Check that size is in terms of words. This is needed to avoid
1090 * unaligned accesses for some archs when redzoning is used, and makes
1091 * sure any on-slab bufctl's are also correctly aligned.
1093 if (size
& (BYTES_PER_WORD
-1)) {
1094 size
+= (BYTES_PER_WORD
-1);
1095 size
&= ~(BYTES_PER_WORD
-1);
1096 printk("%sForcing size word alignment - %s\n", func_nm
, name
);
1100 if (flags
& SLAB_RED_ZONE
) {
1102 * There is no point trying to honour cache alignment
1105 flags
&= ~SLAB_HWCACHE_ALIGN
;
1106 /* add space for red zone words */
1107 cachep
->dbghead
+= BYTES_PER_WORD
;
1108 size
+= 2*BYTES_PER_WORD
;
1110 if (flags
& SLAB_STORE_USER
) {
1111 flags
&= ~SLAB_HWCACHE_ALIGN
;
1112 size
+= BYTES_PER_WORD
; /* add space */
1114 #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
1115 if (size
> 128 && cachep
->reallen
> L1_CACHE_BYTES
&& size
< PAGE_SIZE
) {
1116 cachep
->dbghead
+= PAGE_SIZE
- size
;
1121 align
= BYTES_PER_WORD
;
1122 if (flags
& SLAB_HWCACHE_ALIGN
)
1123 align
= L1_CACHE_BYTES
;
1125 /* Determine if the slab management is 'on' or 'off' slab. */
1126 if (size
>= (PAGE_SIZE
>>3))
1128 * Size is large, assume best to place the slab management obj
1129 * off-slab (should allow better packing of objs).
1131 flags
|= CFLGS_OFF_SLAB
;
1133 if (flags
& SLAB_HWCACHE_ALIGN
) {
1134 /* Need to adjust size so that objs are cache aligned. */
1135 /* Small obj size, can get at least two per cache line. */
1136 while (size
<= align
/2)
1138 size
= (size
+align
-1)&(~(align
-1));
1141 /* Cal size (in pages) of slabs, and the num of objs per slab.
1142 * This could be made much more intelligent. For now, try to avoid
1143 * using high page-orders for slabs. When the gfp() funcs are more
1144 * friendly towards high-order requests, this should be changed.
1147 unsigned int break_flag
= 0;
1149 cache_estimate(cachep
->gfporder
, size
, flags
,
1150 &left_over
, &cachep
->num
);
1153 if (cachep
->gfporder
>= MAX_GFP_ORDER
)
1157 if (flags
& CFLGS_OFF_SLAB
&& cachep
->num
> offslab_limit
) {
1158 /* Oops, this num of objs will cause problems. */
1165 * Large num of objs is good, but v. large slabs are currently
1166 * bad for the gfp()s.
1168 if (cachep
->gfporder
>= slab_break_gfp_order
)
1171 if ((left_over
*8) <= (PAGE_SIZE
<<cachep
->gfporder
))
1172 break; /* Acceptable internal fragmentation. */
1178 printk("kmem_cache_create: couldn't create cache %s.\n", name
);
1179 kmem_cache_free(&cache_cache
, cachep
);
1183 slab_size
= L1_CACHE_ALIGN(cachep
->num
*sizeof(kmem_bufctl_t
)+sizeof(struct slab
));
1186 * If the slab has been placed off-slab, and we have enough space then
1187 * move it on-slab. This is at the expense of any extra colouring.
1189 if (flags
& CFLGS_OFF_SLAB
&& left_over
>= slab_size
) {
1190 flags
&= ~CFLGS_OFF_SLAB
;
1191 left_over
-= slab_size
;
1194 /* Offset must be a multiple of the alignment. */
1195 offset
+= (align
-1);
1196 offset
&= ~(align
-1);
1198 offset
= L1_CACHE_BYTES
;
1199 cachep
->colour_off
= offset
;
1200 cachep
->colour
= left_over
/offset
;
1202 cachep
->flags
= flags
;
1203 cachep
->gfpflags
= 0;
1204 if (flags
& SLAB_CACHE_DMA
)
1205 cachep
->gfpflags
|= GFP_DMA
;
1206 spin_lock_init(&cachep
->spinlock
);
1207 cachep
->objsize
= size
;
1209 INIT_LIST_HEAD(&cachep
->lists
.slabs_full
);
1210 INIT_LIST_HEAD(&cachep
->lists
.slabs_partial
);
1211 INIT_LIST_HEAD(&cachep
->lists
.slabs_free
);
1213 if (flags
& CFLGS_OFF_SLAB
)
1214 cachep
->slabp_cache
= kmem_find_general_cachep(slab_size
,0);
1215 cachep
->ctor
= ctor
;
1216 cachep
->dtor
= dtor
;
1217 cachep
->name
= name
;
1219 if (g_cpucache_up
== FULL
) {
1220 enable_cpucache(cachep
);
1222 if (g_cpucache_up
== NONE
) {
1223 /* Note: the first kmem_cache_create must create
1224 * the cache that's used by kmalloc(24), otherwise
1225 * the creation of further caches will BUG().
1227 cachep
->array
[smp_processor_id()] = &initarray_generic
.cache
;
1228 g_cpucache_up
= PARTIAL
;
1230 cachep
->array
[smp_processor_id()] = kmalloc(sizeof(struct arraycache_init
),GFP_KERNEL
);
1232 BUG_ON(!ac_data(cachep
));
1233 ac_data(cachep
)->avail
= 0;
1234 ac_data(cachep
)->limit
= BOOT_CPUCACHE_ENTRIES
;
1235 ac_data(cachep
)->batchcount
= 1;
1236 ac_data(cachep
)->touched
= 0;
1237 cachep
->batchcount
= 1;
1238 cachep
->limit
= BOOT_CPUCACHE_ENTRIES
;
1239 cachep
->free_limit
= (1+num_online_cpus())*cachep
->batchcount
1243 cachep
->lists
.next_reap
= jiffies
+ REAPTIMEOUT_LIST3
+
1244 ((unsigned long)cachep
)%REAPTIMEOUT_LIST3
;
1246 /* Need the semaphore to access the chain. */
1247 down(&cache_chain_sem
);
1249 struct list_head
*p
;
1250 mm_segment_t old_fs
;
1254 list_for_each(p
, &cache_chain
) {
1255 kmem_cache_t
*pc
= list_entry(p
, kmem_cache_t
, next
);
1257 /* This happens when the module gets unloaded and doesn't
1258 destroy its slab cache and noone else reuses the vmalloc
1259 area of the module. Print a warning. */
1260 if (__get_user(tmp
,pc
->name
)) {
1261 printk("SLAB: cache with size %d has lost its name\n",
1265 if (!strcmp(pc
->name
,name
)) {
1266 printk("kmem_cache_create: duplicate cache %s\n",name
);
1267 up(&cache_chain_sem
);
1274 /* cache setup completed, link it into the list */
1275 list_add(&cachep
->next
, &cache_chain
);
1276 up(&cache_chain_sem
);
1281 EXPORT_SYMBOL(kmem_cache_create
);
1283 static inline void check_irq_off(void)
1286 BUG_ON(!irqs_disabled());
1290 static inline void check_irq_on(void)
1293 BUG_ON(irqs_disabled());
1297 static inline void check_spinlock_acquired(kmem_cache_t
*cachep
)
1301 BUG_ON(spin_trylock(&cachep
->spinlock
));
1306 * Waits for all CPUs to execute func().
1308 static void smp_call_function_all_cpus(void (*func
) (void *arg
), void *arg
)
1313 local_irq_disable();
1317 if (smp_call_function(func
, arg
, 1, 1))
1323 static void free_block (kmem_cache_t
* cachep
, void** objpp
, int len
);
1324 static void drain_array_locked(kmem_cache_t
* cachep
,
1325 struct array_cache
*ac
, int force
);
1327 static void do_drain(void *arg
)
1329 kmem_cache_t
*cachep
= (kmem_cache_t
*)arg
;
1330 struct array_cache
*ac
;
1333 ac
= ac_data(cachep
);
1334 spin_lock(&cachep
->spinlock
);
1335 free_block(cachep
, &ac_entry(ac
)[0], ac
->avail
);
1336 spin_unlock(&cachep
->spinlock
);
1340 static void drain_cpu_caches(kmem_cache_t
*cachep
)
1342 smp_call_function_all_cpus(do_drain
, cachep
);
1344 spin_lock_irq(&cachep
->spinlock
);
1345 if (cachep
->lists
.shared
)
1346 drain_array_locked(cachep
, cachep
->lists
.shared
, 1);
1347 spin_unlock_irq(&cachep
->spinlock
);
1351 /* NUMA shrink all list3s */
1352 static int __cache_shrink(kmem_cache_t
*cachep
)
1357 drain_cpu_caches(cachep
);
1360 spin_lock_irq(&cachep
->spinlock
);
1363 struct list_head
*p
;
1365 p
= cachep
->lists
.slabs_free
.prev
;
1366 if (p
== &cachep
->lists
.slabs_free
)
1369 slabp
= list_entry(cachep
->lists
.slabs_free
.prev
, struct slab
, list
);
1374 list_del(&slabp
->list
);
1376 cachep
->lists
.free_objects
-= cachep
->num
;
1377 spin_unlock_irq(&cachep
->spinlock
);
1378 slab_destroy(cachep
, slabp
);
1379 spin_lock_irq(&cachep
->spinlock
);
1381 ret
= !list_empty(&cachep
->lists
.slabs_full
) ||
1382 !list_empty(&cachep
->lists
.slabs_partial
);
1383 spin_unlock_irq(&cachep
->spinlock
);
1388 * kmem_cache_shrink - Shrink a cache.
1389 * @cachep: The cache to shrink.
1391 * Releases as many slabs as possible for a cache.
1392 * To help debugging, a zero exit status indicates all slabs were released.
1394 int kmem_cache_shrink(kmem_cache_t
*cachep
)
1396 if (!cachep
|| in_interrupt())
1399 return __cache_shrink(cachep
);
1402 EXPORT_SYMBOL(kmem_cache_shrink
);
1405 * kmem_cache_destroy - delete a cache
1406 * @cachep: the cache to destroy
1408 * Remove a kmem_cache_t object from the slab cache.
1409 * Returns 0 on success.
1411 * It is expected this function will be called by a module when it is
1412 * unloaded. This will remove the cache completely, and avoid a duplicate
1413 * cache being allocated each time a module is loaded and unloaded, if the
1414 * module doesn't have persistent in-kernel storage across loads and unloads.
1416 * The cache must be empty before calling this function.
1418 * The caller must guarantee that noone will allocate memory from the cache
1419 * during the kmem_cache_destroy().
1421 int kmem_cache_destroy (kmem_cache_t
* cachep
)
1425 if (!cachep
|| in_interrupt())
1428 /* Find the cache in the chain of caches. */
1429 down(&cache_chain_sem
);
1431 * the chain is never empty, cache_cache is never destroyed
1433 list_del(&cachep
->next
);
1434 up(&cache_chain_sem
);
1436 if (__cache_shrink(cachep
)) {
1437 slab_error(cachep
, "Can't free all objects");
1438 down(&cache_chain_sem
);
1439 list_add(&cachep
->next
,&cache_chain
);
1440 up(&cache_chain_sem
);
1444 for (i
= 0; i
< NR_CPUS
; i
++)
1445 kfree(cachep
->array
[i
]);
1447 /* NUMA: free the list3 structures */
1448 kfree(cachep
->lists
.shared
);
1449 cachep
->lists
.shared
= NULL
;
1450 kmem_cache_free(&cache_cache
, cachep
);
1455 EXPORT_SYMBOL(kmem_cache_destroy
);
1457 /* Get the memory for a slab management obj. */
1458 static inline struct slab
* alloc_slabmgmt (kmem_cache_t
*cachep
,
1459 void *objp
, int colour_off
, int local_flags
)
1463 if (OFF_SLAB(cachep
)) {
1464 /* Slab management obj is off-slab. */
1465 slabp
= kmem_cache_alloc(cachep
->slabp_cache
, local_flags
);
1469 slabp
= objp
+colour_off
;
1470 colour_off
+= L1_CACHE_ALIGN(cachep
->num
*
1471 sizeof(kmem_bufctl_t
) + sizeof(struct slab
));
1474 slabp
->colouroff
= colour_off
;
1475 slabp
->s_mem
= objp
+colour_off
;
1480 static inline kmem_bufctl_t
*slab_bufctl(struct slab
*slabp
)
1482 return (kmem_bufctl_t
*)(slabp
+1);
1485 static void cache_init_objs (kmem_cache_t
* cachep
,
1486 struct slab
* slabp
, unsigned long ctor_flags
)
1490 for (i
= 0; i
< cachep
->num
; i
++) {
1491 void* objp
= slabp
->s_mem
+cachep
->objsize
*i
;
1493 /* need to poison the objs? */
1494 if (cachep
->flags
& SLAB_POISON
)
1495 poison_obj(cachep
, objp
, POISON_BEFORE
);
1496 if (cachep
->flags
& SLAB_STORE_USER
)
1497 *dbg_userword(cachep
, objp
) = NULL
;
1499 if (cachep
->flags
& SLAB_RED_ZONE
) {
1500 *dbg_redzone1(cachep
, objp
) = RED_INACTIVE
;
1501 *dbg_redzone2(cachep
, objp
) = RED_INACTIVE
;
1504 * Constructors are not allowed to allocate memory from
1505 * the same cache which they are a constructor for.
1506 * Otherwise, deadlock. They must also be threaded.
1508 if (cachep
->ctor
&& !(cachep
->flags
& SLAB_POISON
))
1509 cachep
->ctor(objp
+obj_dbghead(cachep
), cachep
, ctor_flags
);
1511 if (cachep
->flags
& SLAB_RED_ZONE
) {
1512 if (*dbg_redzone2(cachep
, objp
) != RED_INACTIVE
)
1513 slab_error(cachep
, "constructor overwrote the"
1514 " end of an object");
1515 if (*dbg_redzone1(cachep
, objp
) != RED_INACTIVE
)
1516 slab_error(cachep
, "constructor overwrote the"
1517 " start of an object");
1519 if ((cachep
->objsize
% PAGE_SIZE
) == 0 && OFF_SLAB(cachep
) && cachep
->flags
& SLAB_POISON
)
1520 kernel_map_pages(virt_to_page(objp
), cachep
->objsize
/PAGE_SIZE
, 0);
1523 cachep
->ctor(objp
, cachep
, ctor_flags
);
1525 slab_bufctl(slabp
)[i
] = i
+1;
1527 slab_bufctl(slabp
)[i
-1] = BUFCTL_END
;
1531 static void kmem_flagcheck(kmem_cache_t
*cachep
, int flags
)
1533 if (flags
& SLAB_DMA
) {
1534 if (!(cachep
->gfpflags
& GFP_DMA
))
1537 if (cachep
->gfpflags
& GFP_DMA
)
1543 * Grow (by 1) the number of slabs within a cache. This is called by
1544 * kmem_cache_alloc() when there are no active objs left in a cache.
1546 static int cache_grow (kmem_cache_t
* cachep
, int flags
)
1552 unsigned int i
, local_flags
;
1553 unsigned long ctor_flags
;
1555 /* Be lazy and only check for valid flags here,
1556 * keeping it out of the critical path in kmem_cache_alloc().
1558 if (flags
& ~(SLAB_DMA
|SLAB_LEVEL_MASK
|SLAB_NO_GROW
))
1560 if (flags
& SLAB_NO_GROW
)
1563 ctor_flags
= SLAB_CTOR_CONSTRUCTOR
;
1564 local_flags
= (flags
& SLAB_LEVEL_MASK
);
1565 if (!(local_flags
& __GFP_WAIT
))
1567 * Not allowed to sleep. Need to tell a constructor about
1568 * this - it might need to know...
1570 ctor_flags
|= SLAB_CTOR_ATOMIC
;
1572 /* About to mess with non-constant members - lock. */
1574 spin_lock(&cachep
->spinlock
);
1576 /* Get colour for the slab, and cal the next value. */
1577 offset
= cachep
->colour_next
;
1578 cachep
->colour_next
++;
1579 if (cachep
->colour_next
>= cachep
->colour
)
1580 cachep
->colour_next
= 0;
1581 offset
*= cachep
->colour_off
;
1583 spin_unlock(&cachep
->spinlock
);
1585 if (local_flags
& __GFP_WAIT
)
1589 * The test for missing atomic flag is performed here, rather than
1590 * the more obvious place, simply to reduce the critical path length
1591 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
1592 * will eventually be caught here (where it matters).
1594 kmem_flagcheck(cachep
, flags
);
1597 /* Get mem for the objs. */
1598 if (!(objp
= kmem_getpages(cachep
, flags
)))
1601 /* Get slab management. */
1602 if (!(slabp
= alloc_slabmgmt(cachep
, objp
, offset
, local_flags
)))
1605 /* Nasty!!!!!! I hope this is OK. */
1606 i
= 1 << cachep
->gfporder
;
1607 page
= virt_to_page(objp
);
1609 SET_PAGE_CACHE(page
, cachep
);
1610 SET_PAGE_SLAB(page
, slabp
);
1612 inc_page_state(nr_slab
);
1616 cache_init_objs(cachep
, slabp
, ctor_flags
);
1618 if (local_flags
& __GFP_WAIT
)
1619 local_irq_disable();
1621 spin_lock(&cachep
->spinlock
);
1623 /* Make slab active. */
1624 list_add_tail(&slabp
->list
, &(list3_data(cachep
)->slabs_free
));
1625 STATS_INC_GROWN(cachep
);
1626 list3_data(cachep
)->free_objects
+= cachep
->num
;
1627 spin_unlock(&cachep
->spinlock
);
1630 kmem_freepages(cachep
, objp
);
1632 if (local_flags
& __GFP_WAIT
)
1633 local_irq_disable();
1638 * Perform extra freeing checks:
1639 * - detect bad pointers.
1640 * - POISON/RED_ZONE checking
1641 * - destructor calls, for caches with POISON+dtor
1643 static inline void kfree_debugcheck(const void *objp
)
1648 if (!virt_addr_valid(objp
)) {
1649 printk(KERN_ERR
"kfree_debugcheck: out of range ptr %lxh.\n",
1650 (unsigned long)objp
);
1653 page
= virt_to_page(objp
);
1654 if (!PageSlab(page
)) {
1655 printk(KERN_ERR
"kfree_debugcheck: bad ptr %lxh.\n", (unsigned long)objp
);
1661 static inline void *cache_free_debugcheck (kmem_cache_t
* cachep
, void * objp
, void *caller
)
1668 objp
-= obj_dbghead(cachep
);
1669 kfree_debugcheck(objp
);
1670 page
= virt_to_page(objp
);
1672 if (GET_PAGE_CACHE(page
) != cachep
) {
1673 printk(KERN_ERR
"mismatch in kmem_cache_free: expected cache %p, got %p\n",
1674 GET_PAGE_CACHE(page
),cachep
);
1675 printk(KERN_ERR
"%p is %s.\n", cachep
, cachep
->name
);
1676 printk(KERN_ERR
"%p is %s.\n", GET_PAGE_CACHE(page
), GET_PAGE_CACHE(page
)->name
);
1679 slabp
= GET_PAGE_SLAB(page
);
1681 if (cachep
->flags
& SLAB_RED_ZONE
) {
1682 if (*dbg_redzone1(cachep
, objp
) != RED_ACTIVE
|| *dbg_redzone2(cachep
, objp
) != RED_ACTIVE
) {
1683 slab_error(cachep
, "double free, or memory outside"
1684 " object was overwritten");
1685 printk(KERN_ERR
"%p: redzone 1: 0x%lx, redzone 2: 0x%lx.\n",
1686 objp
, *dbg_redzone1(cachep
, objp
), *dbg_redzone2(cachep
, objp
));
1688 *dbg_redzone1(cachep
, objp
) = RED_INACTIVE
;
1689 *dbg_redzone2(cachep
, objp
) = RED_INACTIVE
;
1691 if (cachep
->flags
& SLAB_STORE_USER
)
1692 *dbg_userword(cachep
, objp
) = caller
;
1694 objnr
= (objp
-slabp
->s_mem
)/cachep
->objsize
;
1696 BUG_ON(objnr
>= cachep
->num
);
1697 BUG_ON(objp
!= slabp
->s_mem
+ objnr
*cachep
->objsize
);
1699 if (cachep
->flags
& SLAB_DEBUG_INITIAL
) {
1700 /* Need to call the slab's constructor so the
1701 * caller can perform a verify of its state (debugging).
1702 * Called without the cache-lock held.
1704 cachep
->ctor(objp
+obj_dbghead(cachep
),
1705 cachep
, SLAB_CTOR_CONSTRUCTOR
|SLAB_CTOR_VERIFY
);
1707 if (cachep
->flags
& SLAB_POISON
&& cachep
->dtor
) {
1708 /* we want to cache poison the object,
1709 * call the destruction callback
1711 cachep
->dtor(objp
+obj_dbghead(cachep
), cachep
, 0);
1713 if (cachep
->flags
& SLAB_POISON
) {
1714 #ifdef CONFIG_DEBUG_PAGEALLOC
1715 if ((cachep
->objsize
% PAGE_SIZE
) == 0 && OFF_SLAB(cachep
)) {
1716 store_stackinfo(cachep
, objp
, POISON_AFTER
);
1717 kernel_map_pages(virt_to_page(objp
), cachep
->objsize
/PAGE_SIZE
, 0);
1719 poison_obj(cachep
, objp
, POISON_AFTER
);
1722 poison_obj(cachep
, objp
, POISON_AFTER
);
1729 static inline void check_slabp(kmem_cache_t
*cachep
, struct slab
*slabp
)
1735 check_spinlock_acquired(cachep
);
1736 /* Check slab's freelist to see if this obj is there. */
1737 for (i
= slabp
->free
; i
!= BUFCTL_END
; i
= slab_bufctl(slabp
)[i
]) {
1739 if (entries
> cachep
->num
|| i
< 0 || i
>= cachep
->num
)
1742 if (entries
!= cachep
->num
- slabp
->inuse
) {
1745 printk(KERN_ERR
"slab: Internal list corruption detected in cache '%s'(%d), slabp %p(%d). Hexdump:\n",
1746 cachep
->name
, cachep
->num
, slabp
, slabp
->inuse
);
1747 for (i
=0;i
<sizeof(slabp
)+cachep
->num
*sizeof(kmem_bufctl_t
);i
++) {
1749 printk("\n%03x:", i
);
1750 printk(" %02x", ((unsigned char*)slabp
)[i
]);
1758 static void* cache_alloc_refill(kmem_cache_t
* cachep
, int flags
)
1761 struct kmem_list3
*l3
;
1762 struct array_cache
*ac
;
1765 ac
= ac_data(cachep
);
1767 batchcount
= ac
->batchcount
;
1768 if (!ac
->touched
&& batchcount
> BATCHREFILL_LIMIT
) {
1769 /* if there was little recent activity on this
1770 * cache, then perform only a partial refill.
1771 * Otherwise we could generate refill bouncing.
1773 batchcount
= BATCHREFILL_LIMIT
;
1775 l3
= list3_data(cachep
);
1777 BUG_ON(ac
->avail
> 0);
1778 spin_lock(&cachep
->spinlock
);
1780 struct array_cache
*shared_array
= l3
->shared
;
1781 if (shared_array
->avail
) {
1782 if (batchcount
> shared_array
->avail
)
1783 batchcount
= shared_array
->avail
;
1784 shared_array
->avail
-= batchcount
;
1785 ac
->avail
= batchcount
;
1786 memcpy(ac_entry(ac
), &ac_entry(shared_array
)[shared_array
->avail
],
1787 sizeof(void*)*batchcount
);
1788 shared_array
->touched
= 1;
1792 while (batchcount
> 0) {
1793 struct list_head
*entry
;
1795 /* Get slab alloc is to come from. */
1796 entry
= l3
->slabs_partial
.next
;
1797 if (entry
== &l3
->slabs_partial
) {
1798 l3
->free_touched
= 1;
1799 entry
= l3
->slabs_free
.next
;
1800 if (entry
== &l3
->slabs_free
)
1804 slabp
= list_entry(entry
, struct slab
, list
);
1805 check_slabp(cachep
, slabp
);
1806 check_spinlock_acquired(cachep
);
1807 while (slabp
->inuse
< cachep
->num
&& batchcount
--) {
1809 STATS_INC_ALLOCED(cachep
);
1810 STATS_INC_ACTIVE(cachep
);
1811 STATS_SET_HIGH(cachep
);
1813 /* get obj pointer */
1814 ac_entry(ac
)[ac
->avail
++] = slabp
->s_mem
+ slabp
->free
*cachep
->objsize
;
1817 next
= slab_bufctl(slabp
)[slabp
->free
];
1819 slab_bufctl(slabp
)[slabp
->free
] = BUFCTL_FREE
;
1823 check_slabp(cachep
, slabp
);
1825 /* move slabp to correct slabp list: */
1826 list_del(&slabp
->list
);
1827 if (slabp
->free
== BUFCTL_END
)
1828 list_add(&slabp
->list
, &l3
->slabs_full
);
1830 list_add(&slabp
->list
, &l3
->slabs_partial
);
1834 l3
->free_objects
-= ac
->avail
;
1836 spin_unlock(&cachep
->spinlock
);
1838 if (unlikely(!ac
->avail
)) {
1840 x
= cache_grow(cachep
, flags
);
1842 // cache_grow can reenable interrupts, then ac could change.
1843 ac
= ac_data(cachep
);
1844 if (!x
&& ac
->avail
== 0) // no objects in sight? abort
1847 if (!ac
->avail
) // objects refilled by interrupt?
1851 return ac_entry(ac
)[--ac
->avail
];
1855 cache_alloc_debugcheck_before(kmem_cache_t
*cachep
, int flags
)
1857 might_sleep_if(flags
& __GFP_WAIT
);
1859 kmem_flagcheck(cachep
, flags
);
1863 static inline void *
1864 cache_alloc_debugcheck_after(kmem_cache_t
*cachep
,
1865 unsigned long flags
, void *objp
, void *caller
)
1870 if (cachep
->flags
& SLAB_POISON
) {
1871 #ifdef CONFIG_DEBUG_PAGEALLOC
1872 if ((cachep
->objsize
% PAGE_SIZE
) == 0 && OFF_SLAB(cachep
))
1873 kernel_map_pages(virt_to_page(objp
), cachep
->objsize
/PAGE_SIZE
, 1);
1875 check_poison_obj(cachep
, objp
);
1877 check_poison_obj(cachep
, objp
);
1879 poison_obj(cachep
, objp
, POISON_BEFORE
);
1881 if (cachep
->flags
& SLAB_STORE_USER
)
1882 *dbg_userword(cachep
, objp
) = caller
;
1884 if (cachep
->flags
& SLAB_RED_ZONE
) {
1885 if (*dbg_redzone1(cachep
, objp
) != RED_INACTIVE
|| *dbg_redzone2(cachep
, objp
) != RED_INACTIVE
) {
1886 slab_error(cachep
, "double free, or memory outside"
1887 " object was overwritten");
1888 printk(KERN_ERR
"%p: redzone 1: 0x%lx, redzone 2: 0x%lx.\n",
1889 objp
, *dbg_redzone1(cachep
, objp
), *dbg_redzone2(cachep
, objp
));
1891 *dbg_redzone1(cachep
, objp
) = RED_ACTIVE
;
1892 *dbg_redzone2(cachep
, objp
) = RED_ACTIVE
;
1894 objp
+= obj_dbghead(cachep
);
1895 if (cachep
->ctor
&& cachep
->flags
& SLAB_POISON
) {
1896 unsigned long ctor_flags
= SLAB_CTOR_CONSTRUCTOR
;
1898 if (!(flags
& __GFP_WAIT
))
1899 ctor_flags
|= SLAB_CTOR_ATOMIC
;
1901 cachep
->ctor(objp
, cachep
, ctor_flags
);
1908 static inline void * __cache_alloc (kmem_cache_t
*cachep
, int flags
)
1910 unsigned long save_flags
;
1912 struct array_cache
*ac
;
1914 cache_alloc_debugcheck_before(cachep
, flags
);
1916 local_irq_save(save_flags
);
1917 ac
= ac_data(cachep
);
1918 if (likely(ac
->avail
)) {
1919 STATS_INC_ALLOCHIT(cachep
);
1921 objp
= ac_entry(ac
)[--ac
->avail
];
1923 STATS_INC_ALLOCMISS(cachep
);
1924 objp
= cache_alloc_refill(cachep
, flags
);
1926 local_irq_restore(save_flags
);
1927 objp
= cache_alloc_debugcheck_after(cachep
, flags
, objp
, __builtin_return_address(0));
1932 * NUMA: different approach needed if the spinlock is moved into
1936 static void free_block(kmem_cache_t
*cachep
, void **objpp
, int nr_objects
)
1940 check_spinlock_acquired(cachep
);
1942 /* NUMA: move add into loop */
1943 cachep
->lists
.free_objects
+= nr_objects
;
1945 for (i
= 0; i
< nr_objects
; i
++) {
1946 void *objp
= objpp
[i
];
1950 slabp
= GET_PAGE_SLAB(virt_to_page(objp
));
1951 list_del(&slabp
->list
);
1952 objnr
= (objp
- slabp
->s_mem
) / cachep
->objsize
;
1953 check_slabp(cachep
, slabp
);
1955 if (slab_bufctl(slabp
)[objnr
] != BUFCTL_FREE
) {
1956 printk(KERN_ERR
"slab: double free detected in cache '%s', objp %p.\n",
1957 cachep
->name
, objp
);
1961 slab_bufctl(slabp
)[objnr
] = slabp
->free
;
1962 slabp
->free
= objnr
;
1963 STATS_DEC_ACTIVE(cachep
);
1965 check_slabp(cachep
, slabp
);
1967 /* fixup slab chains */
1968 if (slabp
->inuse
== 0) {
1969 if (cachep
->lists
.free_objects
> cachep
->free_limit
) {
1970 cachep
->lists
.free_objects
-= cachep
->num
;
1971 slab_destroy(cachep
, slabp
);
1973 list_add(&slabp
->list
,
1974 &list3_data_ptr(cachep
, objp
)->slabs_free
);
1977 /* Unconditionally move a slab to the end of the
1978 * partial list on free - maximum time for the
1979 * other objects to be freed, too.
1981 list_add_tail(&slabp
->list
,
1982 &list3_data_ptr(cachep
, objp
)->slabs_partial
);
1987 static void cache_flusharray (kmem_cache_t
* cachep
, struct array_cache
*ac
)
1991 batchcount
= ac
->batchcount
;
1993 BUG_ON(!batchcount
|| batchcount
> ac
->avail
);
1996 spin_lock(&cachep
->spinlock
);
1997 if (cachep
->lists
.shared
) {
1998 struct array_cache
*shared_array
= cachep
->lists
.shared
;
1999 int max
= shared_array
->limit
-shared_array
->avail
;
2001 if (batchcount
> max
)
2003 memcpy(&ac_entry(shared_array
)[shared_array
->avail
],
2005 sizeof(void*)*batchcount
);
2006 shared_array
->avail
+= batchcount
;
2011 free_block(cachep
, &ac_entry(ac
)[0], batchcount
);
2016 struct list_head
*p
;
2018 p
= list3_data(cachep
)->slabs_free
.next
;
2019 while (p
!= &(list3_data(cachep
)->slabs_free
)) {
2022 slabp
= list_entry(p
, struct slab
, list
);
2023 BUG_ON(slabp
->inuse
);
2028 STATS_SET_FREEABLE(cachep
, i
);
2031 spin_unlock(&cachep
->spinlock
);
2032 ac
->avail
-= batchcount
;
2033 memmove(&ac_entry(ac
)[0], &ac_entry(ac
)[batchcount
],
2034 sizeof(void*)*ac
->avail
);
2039 * Release an obj back to its cache. If the obj has a constructed
2040 * state, it must be in this state _before_ it is released.
2042 * Called with disabled ints.
2044 static inline void __cache_free (kmem_cache_t
*cachep
, void* objp
)
2046 struct array_cache
*ac
= ac_data(cachep
);
2049 objp
= cache_free_debugcheck(cachep
, objp
, __builtin_return_address(0));
2051 if (likely(ac
->avail
< ac
->limit
)) {
2052 STATS_INC_FREEHIT(cachep
);
2053 ac_entry(ac
)[ac
->avail
++] = objp
;
2056 STATS_INC_FREEMISS(cachep
);
2057 cache_flusharray(cachep
, ac
);
2058 ac_entry(ac
)[ac
->avail
++] = objp
;
2063 * kmem_cache_alloc - Allocate an object
2064 * @cachep: The cache to allocate from.
2065 * @flags: See kmalloc().
2067 * Allocate an object from this cache. The flags are only relevant
2068 * if the cache has no available objects.
2070 void * kmem_cache_alloc (kmem_cache_t
*cachep
, int flags
)
2072 return __cache_alloc(cachep
, flags
);
2075 EXPORT_SYMBOL(kmem_cache_alloc
);
2078 * kmalloc - allocate memory
2079 * @size: how many bytes of memory are required.
2080 * @flags: the type of memory to allocate.
2082 * kmalloc is the normal method of allocating memory
2085 * The @flags argument may be one of:
2087 * %GFP_USER - Allocate memory on behalf of user. May sleep.
2089 * %GFP_KERNEL - Allocate normal kernel ram. May sleep.
2091 * %GFP_ATOMIC - Allocation will not sleep. Use inside interrupt handlers.
2093 * Additionally, the %GFP_DMA flag may be set to indicate the memory
2094 * must be suitable for DMA. This can mean different things on different
2095 * platforms. For example, on i386, it means that the memory must come
2096 * from the first 16MB.
2098 void * __kmalloc (size_t size
, int flags
)
2100 struct cache_sizes
*csizep
= malloc_sizes
;
2102 for (; csizep
->cs_size
; csizep
++) {
2103 if (size
> csizep
->cs_size
)
2106 /* This happens if someone tries to call
2107 * kmem_cache_create(), or kmalloc(), before
2108 * the generic caches are initialized.
2110 BUG_ON(csizep
->cs_cachep
== NULL
);
2112 return __cache_alloc(flags
& GFP_DMA
?
2113 csizep
->cs_dmacachep
: csizep
->cs_cachep
, flags
);
2118 EXPORT_SYMBOL(__kmalloc
);
2122 * __alloc_percpu - allocate one copy of the object for every present
2123 * cpu in the system, zeroing them.
2124 * Objects should be dereferenced using per_cpu_ptr/get_cpu_ptr
2127 * @size: how many bytes of memory are required.
2128 * @align: the alignment, which can't be greater than SMP_CACHE_BYTES.
2130 void *__alloc_percpu(size_t size
, size_t align
)
2133 struct percpu_data
*pdata
= kmalloc(sizeof (*pdata
), GFP_KERNEL
);
2138 for (i
= 0; i
< NR_CPUS
; i
++) {
2139 if (!cpu_possible(i
))
2141 pdata
->ptrs
[i
] = kmalloc(size
, GFP_KERNEL
);
2142 if (!pdata
->ptrs
[i
])
2144 memset(pdata
->ptrs
[i
], 0, size
);
2147 /* Catch derefs w/o wrappers */
2148 return (void *) (~(unsigned long) pdata
);
2152 if (!cpu_possible(i
))
2154 kfree(pdata
->ptrs
[i
]);
2160 EXPORT_SYMBOL(__alloc_percpu
);
2164 * kmem_cache_free - Deallocate an object
2165 * @cachep: The cache the allocation was from.
2166 * @objp: The previously allocated object.
2168 * Free an object which was previously allocated from this
2171 void kmem_cache_free (kmem_cache_t
*cachep
, void *objp
)
2173 unsigned long flags
;
2175 local_irq_save(flags
);
2176 __cache_free(cachep
, objp
);
2177 local_irq_restore(flags
);
2180 EXPORT_SYMBOL(kmem_cache_free
);
2183 * kfree - free previously allocated memory
2184 * @objp: pointer returned by kmalloc.
2186 * Don't free memory not originally allocated by kmalloc()
2187 * or you will run into trouble.
2189 void kfree (const void *objp
)
2192 unsigned long flags
;
2196 local_irq_save(flags
);
2197 kfree_debugcheck(objp
);
2198 c
= GET_PAGE_CACHE(virt_to_page(objp
));
2199 __cache_free(c
, (void*)objp
);
2200 local_irq_restore(flags
);
2203 EXPORT_SYMBOL(kfree
);
2207 * free_percpu - free previously allocated percpu memory
2208 * @objp: pointer returned by alloc_percpu.
2210 * Don't free memory not originally allocated by alloc_percpu()
2211 * The complemented objp is to check for that.
2214 free_percpu(const void *objp
)
2217 struct percpu_data
*p
= (struct percpu_data
*) (~(unsigned long) objp
);
2219 for (i
= 0; i
< NR_CPUS
; i
++) {
2220 if (!cpu_possible(i
))
2226 EXPORT_SYMBOL(free_percpu
);
2229 unsigned int kmem_cache_size(kmem_cache_t
*cachep
)
2231 return obj_reallen(cachep
);
2234 EXPORT_SYMBOL(kmem_cache_size
);
2236 kmem_cache_t
* kmem_find_general_cachep (size_t size
, int gfpflags
)
2238 struct cache_sizes
*csizep
= malloc_sizes
;
2240 /* This function could be moved to the header file, and
2241 * made inline so consumers can quickly determine what
2242 * cache pointer they require.
2244 for ( ; csizep
->cs_size
; csizep
++) {
2245 if (size
> csizep
->cs_size
)
2249 return (gfpflags
& GFP_DMA
) ? csizep
->cs_dmacachep
: csizep
->cs_cachep
;
2252 EXPORT_SYMBOL(kmem_find_general_cachep
);
2254 struct ccupdate_struct
{
2255 kmem_cache_t
*cachep
;
2256 struct array_cache
*new[NR_CPUS
];
2259 static void do_ccupdate_local(void *info
)
2261 struct ccupdate_struct
*new = (struct ccupdate_struct
*)info
;
2262 struct array_cache
*old
;
2265 old
= ac_data(new->cachep
);
2267 new->cachep
->array
[smp_processor_id()] = new->new[smp_processor_id()];
2268 new->new[smp_processor_id()] = old
;
2272 static int do_tune_cpucache (kmem_cache_t
* cachep
, int limit
, int batchcount
, int shared
)
2274 struct ccupdate_struct
new;
2275 struct array_cache
*new_shared
;
2278 memset(&new.new,0,sizeof(new.new));
2279 for (i
= 0; i
< NR_CPUS
; i
++) {
2280 struct array_cache
*ccnew
;
2282 ccnew
= kmalloc(sizeof(void*)*limit
+
2283 sizeof(struct array_cache
), GFP_KERNEL
);
2285 for (i
--; i
>= 0; i
--) kfree(new.new[i
]);
2289 ccnew
->limit
= limit
;
2290 ccnew
->batchcount
= batchcount
;
2294 new.cachep
= cachep
;
2296 smp_call_function_all_cpus(do_ccupdate_local
, (void *)&new);
2299 spin_lock_irq(&cachep
->spinlock
);
2300 cachep
->batchcount
= batchcount
;
2301 cachep
->limit
= limit
;
2302 cachep
->free_limit
= (1+num_online_cpus())*cachep
->batchcount
+ cachep
->num
;
2303 spin_unlock_irq(&cachep
->spinlock
);
2305 for (i
= 0; i
< NR_CPUS
; i
++) {
2306 struct array_cache
*ccold
= new.new[i
];
2309 spin_lock_irq(&cachep
->spinlock
);
2310 free_block(cachep
, ac_entry(ccold
), ccold
->avail
);
2311 spin_unlock_irq(&cachep
->spinlock
);
2314 new_shared
= kmalloc(sizeof(void*)*batchcount
*shared
+
2315 sizeof(struct array_cache
), GFP_KERNEL
);
2317 struct array_cache
*old
;
2318 new_shared
->avail
= 0;
2319 new_shared
->limit
= batchcount
*shared
;
2320 new_shared
->batchcount
= 0xbaadf00d;
2321 new_shared
->touched
= 0;
2323 spin_lock_irq(&cachep
->spinlock
);
2324 old
= cachep
->lists
.shared
;
2325 cachep
->lists
.shared
= new_shared
;
2327 free_block(cachep
, ac_entry(old
), old
->avail
);
2328 spin_unlock_irq(&cachep
->spinlock
);
2336 static void enable_cpucache (kmem_cache_t
*cachep
)
2341 /* The head array serves three purposes:
2342 * - create a LIFO ordering, i.e. return objects that are cache-warm
2343 * - reduce the number of spinlock operations.
2344 * - reduce the number of linked list operations on the slab and
2345 * bufctl chains: array operations are cheaper.
2346 * The numbers are guessed, we should auto-tune as described by
2349 if (cachep
->objsize
> 131072)
2351 else if (cachep
->objsize
> PAGE_SIZE
)
2353 else if (cachep
->objsize
> 1024)
2355 else if (cachep
->objsize
> 256)
2360 /* Cpu bound tasks (e.g. network routing) can exhibit cpu bound
2361 * allocation behaviour: Most allocs on one cpu, most free operations
2362 * on another cpu. For these cases, an efficient object passing between
2363 * cpus is necessary. This is provided by a shared array. The array
2364 * replaces Bonwick's magazine layer.
2365 * On uniprocessor, it's functionally equivalent (but less efficient)
2366 * to a larger limit. Thus disabled by default.
2370 if (cachep
->objsize
<= PAGE_SIZE
)
2375 /* With debugging enabled, large batchcount lead to excessively
2376 * long periods with disabled local interrupts. Limit the
2382 err
= do_tune_cpucache(cachep
, limit
, (limit
+1)/2, shared
);
2384 printk(KERN_ERR
"enable_cpucache failed for %s, error %d.\n",
2385 cachep
->name
, -err
);
2388 static void drain_array(kmem_cache_t
*cachep
, struct array_cache
*ac
)
2395 } else if (ac
->avail
) {
2396 tofree
= (ac
->limit
+4)/5;
2397 if (tofree
> ac
->avail
) {
2398 tofree
= (ac
->avail
+1)/2;
2400 spin_lock(&cachep
->spinlock
);
2401 free_block(cachep
, ac_entry(ac
), tofree
);
2402 spin_unlock(&cachep
->spinlock
);
2403 ac
->avail
-= tofree
;
2404 memmove(&ac_entry(ac
)[0], &ac_entry(ac
)[tofree
],
2405 sizeof(void*)*ac
->avail
);
2409 static void drain_array_locked(kmem_cache_t
*cachep
,
2410 struct array_cache
*ac
, int force
)
2414 check_spinlock_acquired(cachep
);
2415 if (ac
->touched
&& !force
) {
2417 } else if (ac
->avail
) {
2418 tofree
= force
? ac
->avail
: (ac
->limit
+4)/5;
2419 if (tofree
> ac
->avail
) {
2420 tofree
= (ac
->avail
+1)/2;
2422 free_block(cachep
, ac_entry(ac
), tofree
);
2423 ac
->avail
-= tofree
;
2424 memmove(&ac_entry(ac
)[0], &ac_entry(ac
)[tofree
],
2425 sizeof(void*)*ac
->avail
);
2430 * cache_reap - Reclaim memory from caches.
2432 * Called from a timer, every few seconds
2434 * - clear the per-cpu caches for this CPU.
2435 * - return freeable pages to the main free memory pool.
2437 * If we cannot acquire the cache chain semaphore then just give up - we'll
2438 * try again next timer interrupt.
2440 static inline void cache_reap (void)
2442 struct list_head
*walk
;
2445 BUG_ON(!in_interrupt());
2448 if (down_trylock(&cache_chain_sem
))
2451 list_for_each(walk
, &cache_chain
) {
2452 kmem_cache_t
*searchp
;
2453 struct list_head
* p
;
2457 searchp
= list_entry(walk
, kmem_cache_t
, next
);
2459 if (searchp
->flags
& SLAB_NO_REAP
)
2463 local_irq_disable();
2464 drain_array(searchp
, ac_data(searchp
));
2466 if(time_after(searchp
->lists
.next_reap
, jiffies
))
2469 spin_lock(&searchp
->spinlock
);
2470 if(time_after(searchp
->lists
.next_reap
, jiffies
)) {
2473 searchp
->lists
.next_reap
= jiffies
+ REAPTIMEOUT_LIST3
;
2475 if (searchp
->lists
.shared
)
2476 drain_array_locked(searchp
, searchp
->lists
.shared
, 0);
2478 if (searchp
->lists
.free_touched
) {
2479 searchp
->lists
.free_touched
= 0;
2483 tofree
= (searchp
->free_limit
+5*searchp
->num
-1)/(5*searchp
->num
);
2485 p
= list3_data(searchp
)->slabs_free
.next
;
2486 if (p
== &(list3_data(searchp
)->slabs_free
))
2489 slabp
= list_entry(p
, struct slab
, list
);
2490 BUG_ON(slabp
->inuse
);
2491 list_del(&slabp
->list
);
2492 STATS_INC_REAPED(searchp
);
2494 /* Safe to drop the lock. The slab is no longer
2495 * linked to the cache.
2496 * searchp cannot disappear, we hold
2499 searchp
->lists
.free_objects
-= searchp
->num
;
2500 spin_unlock_irq(&searchp
->spinlock
);
2501 slab_destroy(searchp
, slabp
);
2502 spin_lock_irq(&searchp
->spinlock
);
2503 } while(--tofree
> 0);
2505 spin_unlock(&searchp
->spinlock
);
2512 up(&cache_chain_sem
);
2516 * This is a timer handler. There is on per CPU. It is called periodially
2517 * to shrink this CPU's caches. Otherwise there could be memory tied up
2518 * for long periods (or for ever) due to load changes.
2520 static void reap_timer_fnc(unsigned long data
)
2522 int cpu
= smp_processor_id();
2523 struct timer_list
*rt
= &__get_cpu_var(reap_timers
);
2526 mod_timer(rt
, jiffies
+ REAPTIMEOUT_CPUC
+ cpu
);
2529 #ifdef CONFIG_PROC_FS
2531 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
2534 struct list_head
*p
;
2536 down(&cache_chain_sem
);
2539 * Output format version, so at least we can change it
2540 * without _too_ many complaints.
2543 seq_puts(m
, "slabinfo - version: 2.0 (statistics)\n");
2545 seq_puts(m
, "slabinfo - version: 2.0\n");
2547 seq_puts(m
, "# name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab>");
2548 seq_puts(m
, " : tunables <batchcount> <limit> <sharedfactor>");
2549 seq_puts(m
, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
2551 seq_puts(m
, " : globalstat <listallocs> <maxobjs> <grown> <reaped> <error> <maxfreeable> <freelimit>");
2552 seq_puts(m
, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
2556 p
= cache_chain
.next
;
2559 if (p
== &cache_chain
)
2562 return list_entry(p
, kmem_cache_t
, next
);
2565 static void *s_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
2567 kmem_cache_t
*cachep
= p
;
2569 return cachep
->next
.next
== &cache_chain
? NULL
2570 : list_entry(cachep
->next
.next
, kmem_cache_t
, next
);
2573 static void s_stop(struct seq_file
*m
, void *p
)
2575 up(&cache_chain_sem
);
2578 static int s_show(struct seq_file
*m
, void *p
)
2580 kmem_cache_t
*cachep
= p
;
2581 struct list_head
*q
;
2583 unsigned long active_objs
;
2584 unsigned long num_objs
;
2585 unsigned long active_slabs
= 0;
2586 unsigned long num_slabs
;
2589 mm_segment_t old_fs
;
2593 spin_lock_irq(&cachep
->spinlock
);
2596 list_for_each(q
,&cachep
->lists
.slabs_full
) {
2597 slabp
= list_entry(q
, struct slab
, list
);
2598 if (slabp
->inuse
!= cachep
->num
&& !error
)
2599 error
= "slabs_full accounting error";
2600 active_objs
+= cachep
->num
;
2603 list_for_each(q
,&cachep
->lists
.slabs_partial
) {
2604 slabp
= list_entry(q
, struct slab
, list
);
2605 if (slabp
->inuse
== cachep
->num
&& !error
)
2606 error
= "slabs_partial inuse accounting error";
2607 if (!slabp
->inuse
&& !error
)
2608 error
= "slabs_partial/inuse accounting error";
2609 active_objs
+= slabp
->inuse
;
2612 list_for_each(q
,&cachep
->lists
.slabs_free
) {
2613 slabp
= list_entry(q
, struct slab
, list
);
2614 if (slabp
->inuse
&& !error
)
2615 error
= "slabs_free/inuse accounting error";
2618 num_slabs
+=active_slabs
;
2619 num_objs
= num_slabs
*cachep
->num
;
2620 if (num_objs
- active_objs
!= cachep
->lists
.free_objects
&& !error
)
2621 error
= "free_objects accounting error";
2623 name
= cachep
->name
;
2626 * Check to see if `name' resides inside a module which has been
2627 * unloaded (someone forgot to destroy their cache)
2631 if (__get_user(tmp
, name
))
2636 printk(KERN_ERR
"slab: cache %s error: %s\n", name
, error
);
2638 seq_printf(m
, "%-17s %6lu %6lu %6u %4u %4d",
2639 name
, active_objs
, num_objs
, cachep
->objsize
,
2640 cachep
->num
, (1<<cachep
->gfporder
));
2641 seq_printf(m
, " : tunables %4u %4u %4u",
2642 cachep
->limit
, cachep
->batchcount
,
2643 cachep
->lists
.shared
->limit
/cachep
->batchcount
);
2644 seq_printf(m
, " : slabdata %6lu %6lu %6u",
2645 active_slabs
, num_slabs
, cachep
->lists
.shared
->avail
);
2648 unsigned long high
= cachep
->high_mark
;
2649 unsigned long allocs
= cachep
->num_allocations
;
2650 unsigned long grown
= cachep
->grown
;
2651 unsigned long reaped
= cachep
->reaped
;
2652 unsigned long errors
= cachep
->errors
;
2653 unsigned long max_freeable
= cachep
->max_freeable
;
2654 unsigned long free_limit
= cachep
->free_limit
;
2656 seq_printf(m
, " : globalstat %7lu %6lu %5lu %4lu %4lu %4lu %4lu",
2657 allocs
, high
, grown
, reaped
, errors
,
2658 max_freeable
, free_limit
);
2662 unsigned long allochit
= atomic_read(&cachep
->allochit
);
2663 unsigned long allocmiss
= atomic_read(&cachep
->allocmiss
);
2664 unsigned long freehit
= atomic_read(&cachep
->freehit
);
2665 unsigned long freemiss
= atomic_read(&cachep
->freemiss
);
2667 seq_printf(m
, " : cpustat %6lu %6lu %6lu %6lu",
2668 allochit
, allocmiss
, freehit
, freemiss
);
2672 spin_unlock_irq(&cachep
->spinlock
);
2677 * slabinfo_op - iterator that generates /proc/slabinfo
2686 * num-pages-per-slab
2687 * + further values on SMP and with statistics enabled
2690 struct seq_operations slabinfo_op
= {
2697 #define MAX_SLABINFO_WRITE 128
2699 * slabinfo_write - Tuning for the slab allocator
2701 * @buffer: user buffer
2702 * @count: data length
2705 ssize_t
slabinfo_write(struct file
*file
, const char __user
*buffer
,
2706 size_t count
, loff_t
*ppos
)
2708 char kbuf
[MAX_SLABINFO_WRITE
+1], *tmp
;
2709 int limit
, batchcount
, shared
, res
;
2710 struct list_head
*p
;
2712 if (count
> MAX_SLABINFO_WRITE
)
2714 if (copy_from_user(&kbuf
, buffer
, count
))
2716 kbuf
[MAX_SLABINFO_WRITE
] = '\0';
2718 tmp
= strchr(kbuf
, ' ');
2723 if (sscanf(tmp
, " %d %d %d", &limit
, &batchcount
, &shared
) != 3)
2726 /* Find the cache in the chain of caches. */
2727 down(&cache_chain_sem
);
2729 list_for_each(p
,&cache_chain
) {
2730 kmem_cache_t
*cachep
= list_entry(p
, kmem_cache_t
, next
);
2732 if (!strcmp(cachep
->name
, kbuf
)) {
2735 batchcount
> limit
||
2739 res
= do_tune_cpucache(cachep
, limit
, batchcount
, shared
);
2744 up(&cache_chain_sem
);
2751 unsigned int ksize(const void *objp
)
2754 unsigned long flags
;
2755 unsigned int size
= 0;
2757 if (likely(objp
!= NULL
)) {
2758 local_irq_save(flags
);
2759 c
= GET_PAGE_CACHE(virt_to_page(objp
));
2760 size
= kmem_cache_size(c
);
2761 local_irq_restore(flags
);
2767 void ptrinfo(unsigned long addr
)
2771 printk("Dumping data about address %p.\n", (void*)addr
);
2772 if (!virt_addr_valid((void*)addr
)) {
2773 printk("virt addr invalid.\n");
2777 pgd_t
*pgd
= pgd_offset_k(addr
);
2779 if (pgd_none(*pgd
)) {
2780 printk("No pgd.\n");
2783 pmd
= pmd_offset(pgd
, addr
);
2784 if (pmd_none(*pmd
)) {
2785 printk("No pmd.\n");
2789 if (pmd_large(*pmd
)) {
2790 printk("Large page.\n");
2794 printk("normal page, pte_val 0x%llx\n",
2795 (unsigned long long)pte_val(*pte_offset_kernel(pmd
, addr
)));
2798 page
= virt_to_page((void*)addr
);
2799 printk("struct page at %p, flags %lxh.\n", page
, page
->flags
);
2800 if (PageSlab(page
)) {
2803 unsigned long flags
;
2807 c
= GET_PAGE_CACHE(page
);
2808 printk("belongs to cache %s.\n",c
->name
);
2810 spin_lock_irqsave(&c
->spinlock
, flags
);
2811 s
= GET_PAGE_SLAB(page
);
2812 printk("slabp %p with %d inuse objects (from %d).\n",
2813 s
, s
->inuse
, c
->num
);
2816 objnr
= (addr
-(unsigned long)s
->s_mem
)/c
->objsize
;
2817 objp
= s
->s_mem
+c
->objsize
*objnr
;
2818 printk("points into object no %d, starting at %p, len %d.\n",
2819 objnr
, objp
, c
->objsize
);
2820 if (objnr
>= c
->num
) {
2821 printk("Bad obj number.\n");
2823 kernel_map_pages(virt_to_page(objp
),
2824 c
->objsize
/PAGE_SIZE
, 1);
2826 if (c
->flags
& SLAB_RED_ZONE
)
2827 printk("redzone: 0x%lx/0x%lx.\n",
2828 *dbg_redzone1(c
, objp
),
2829 *dbg_redzone2(c
, objp
));
2831 if (c
->flags
& SLAB_STORE_USER
)
2832 printk("Last user: %p.\n",
2833 *dbg_userword(c
, objp
));
2835 spin_unlock_irqrestore(&c
->spinlock
, flags
);