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 * An implementation of the Slab Allocator as described in outline in;
12 * UNIX Internals: The New Frontiers by Uresh Vahalia
13 * Pub: Prentice Hall ISBN 0-13-101908-2
14 * or with a little more detail in;
15 * The Slab Allocator: An Object-Caching Kernel Memory Allocator
16 * Jeff Bonwick (Sun Microsystems).
17 * Presented at: USENIX Summer 1994 Technical Conference
20 * The memory is organized in caches, one cache for each object type.
21 * (e.g. inode_cache, dentry_cache, buffer_head, vm_area_struct)
22 * Each cache consists out of many slabs (they are small (usually one
23 * page long) and always contiguous), and each slab contains multiple
24 * initialized objects.
26 * Each cache can only support one memory type (GFP_DMA, GFP_HIGHMEM,
27 * normal). If you need a special memory type, then must create a new
28 * cache for that memory type.
30 * In order to reduce fragmentation, the slabs are sorted in 3 groups:
31 * full slabs with 0 free objects
33 * empty slabs with no allocated objects
35 * If partial slabs exist, then new allocations come from these slabs,
36 * otherwise from empty slabs or new slabs are allocated.
38 * kmem_cache_destroy() CAN CRASH if you try to allocate from the cache
39 * during kmem_cache_destroy(). The caller must prevent concurrent allocs.
41 * On SMP systems, each cache has a short per-cpu head array, most allocs
42 * and frees go into that array, and if that array overflows, then 1/2
43 * of the entries in the array are given back into the global cache.
44 * This reduces the number of spinlock operations.
46 * The c_cpuarray may not be read with enabled local interrupts.
48 * SMP synchronization:
49 * constructors and destructors are called without any locking.
50 * Several members in kmem_cache_t and slab_t never change, they
51 * are accessed without any locking.
52 * The per-cpu arrays are never accessed from the wrong cpu, no locking.
53 * The non-constant members are protected with a per-cache irq spinlock.
55 * Further notes from the original documentation:
57 * 11 April '97. Started multi-threading - markhe
58 * The global cache-chain is protected by the semaphore 'cache_chain_sem'.
59 * The sem is only needed when accessing/extending the cache-chain, which
60 * can never happen inside an interrupt (kmem_cache_create(),
61 * kmem_cache_shrink() and kmem_cache_reap()).
63 * To prevent kmem_cache_shrink() trying to shrink a 'growing' cache (which
64 * maybe be sleeping and therefore not holding the semaphore/lock), the
65 * growing field is used. This also prevents reaping from a cache.
67 * At present, each engine can be growing a cache. This should be blocked.
71 #include <linux/config.h>
72 #include <linux/slab.h>
73 #include <linux/interrupt.h>
74 #include <linux/init.h>
75 #include <asm/uaccess.h>
78 * DEBUG - 1 for kmem_cache_create() to honour; SLAB_DEBUG_INITIAL,
79 * SLAB_RED_ZONE & SLAB_POISON.
80 * 0 for faster, smaller code (especially in the critical paths).
82 * STATS - 1 to collect stats for /proc/slabinfo.
83 * 0 for faster, smaller code (especially in the critical paths).
85 * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
90 #define FORCED_DEBUG 0
93 * Parameters for kmem_cache_reap
95 #define REAP_SCANLEN 10
96 #define REAP_PERFECT 10
98 /* Shouldn't this be in a header file somewhere? */
99 #define BYTES_PER_WORD sizeof(void *)
101 /* Legal flag mask for kmem_cache_create(). */
103 # define CREATE_MASK (SLAB_DEBUG_INITIAL | SLAB_RED_ZONE | \
104 SLAB_POISON | SLAB_HWCACHE_ALIGN | \
105 SLAB_NO_REAP | SLAB_CACHE_DMA)
107 # define CREATE_MASK (SLAB_HWCACHE_ALIGN | SLAB_NO_REAP | SLAB_CACHE_DMA)
113 * Bufctl's are used for linking objs within a slab
116 * This implementaion relies on "struct page" for locating the cache &
117 * slab an object belongs to.
118 * This allows the bufctl structure to be small (one int), but limits
119 * the number of objects a slab (not a cache) can contain when off-slab
120 * bufctls are used. The limit is the size of the largest general cache
121 * that does not use off-slab slabs.
122 * For 32bit archs with 4 kB pages, is this 56.
123 * This is not serious, as it is only for large objects, when it is unwise
124 * to have too many per slab.
125 * Note: This limit can be raised by introducing a general cache whose size
126 * is less than 512 (PAGE_SIZE<<3), but greater than 256.
129 #define BUFCTL_END 0xffffFFFF
130 #define SLAB_LIMIT 0xffffFFFE
131 typedef unsigned int kmem_bufctl_t
;
133 /* Max number of objs-per-slab for caches which use off-slab slabs.
134 * Needed to avoid a possible looping condition in kmem_cache_grow().
136 static unsigned long offslab_limit
;
141 * Manages the objs in a slab. Placed either at the beginning of mem allocated
142 * for a slab, or allocated from an general cache.
143 * Slabs are chained into one ordered list: fully used, partial, then fully
146 typedef struct slab_s
{
147 struct list_head list
;
148 unsigned long colouroff
;
149 void *s_mem
; /* including colour offset */
150 unsigned int inuse
; /* num of objs active in slab */
154 #define slab_bufctl(slabp) \
155 ((kmem_bufctl_t *)(((slab_t*)slabp)+1))
161 * The limit is stored in the per-cpu structure to reduce the data cache
164 typedef struct cpucache_s
{
169 #define cc_entry(cpucache) \
170 ((void **)(((cpucache_t*)cpucache)+1))
171 #define cc_data(cachep) \
172 ((cachep)->cpudata[smp_processor_id()])
179 #define CACHE_NAMELEN 20 /* max name length for a slab cache */
181 struct kmem_cache_s
{
182 /* 1) each alloc & free */
183 /* full, partial first, then free */
184 struct list_head slabs
;
185 struct list_head
*firstnotfull
;
186 unsigned int objsize
;
187 unsigned int flags
; /* constant flags */
188 unsigned int num
; /* # of objs per slab */
191 unsigned int batchcount
;
194 /* 2) slab additions /removals */
195 /* order of pgs per slab (2^n) */
196 unsigned int gfporder
;
198 /* force GFP flags, e.g. GFP_DMA */
199 unsigned int gfpflags
;
201 size_t colour
; /* cache colouring range */
202 unsigned int colour_off
; /* colour offset */
203 unsigned int colour_next
; /* cache colouring */
204 kmem_cache_t
*slabp_cache
;
205 unsigned int growing
;
206 unsigned int dflags
; /* dynamic flags */
208 /* constructor func */
209 void (*ctor
)(void *, kmem_cache_t
*, unsigned long);
211 /* de-constructor func */
212 void (*dtor
)(void *, kmem_cache_t
*, unsigned long);
214 unsigned long failures
;
216 /* 3) cache creation/removal */
217 char name
[CACHE_NAMELEN
];
218 struct list_head next
;
220 /* 4) per-cpu data */
221 cpucache_t
*cpudata
[NR_CPUS
];
224 unsigned long num_active
;
225 unsigned long num_allocations
;
226 unsigned long high_mark
;
228 unsigned long reaped
;
229 unsigned long errors
;
239 /* internal c_flags */
240 #define CFLGS_OFF_SLAB 0x010000UL /* slab management in own cache */
241 #define CFLGS_OPTIMIZE 0x020000UL /* optimized slab lookup */
243 /* c_dflags (dynamic flags). Need to hold the spinlock to access this member */
244 #define DFLGS_GROWN 0x000001UL /* don't reap a recently grown */
246 #define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
247 #define OPTIMIZE(x) ((x)->flags & CFLGS_OPTIMIZE)
248 #define GROWN(x) ((x)->dlags & DFLGS_GROWN)
251 #define STATS_INC_ACTIVE(x) ((x)->num_active++)
252 #define STATS_DEC_ACTIVE(x) ((x)->num_active--)
253 #define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
254 #define STATS_INC_GROWN(x) ((x)->grown++)
255 #define STATS_INC_REAPED(x) ((x)->reaped++)
256 #define STATS_SET_HIGH(x) do { if ((x)->num_active > (x)->high_mark) \
257 (x)->high_mark = (x)->num_active; \
259 #define STATS_INC_ERR(x) ((x)->errors++)
261 #define STATS_INC_ACTIVE(x) do { } while (0)
262 #define STATS_DEC_ACTIVE(x) do { } while (0)
263 #define STATS_INC_ALLOCED(x) do { } while (0)
264 #define STATS_INC_GROWN(x) do { } while (0)
265 #define STATS_INC_REAPED(x) do { } while (0)
266 #define STATS_SET_HIGH(x) do { } while (0)
267 #define STATS_INC_ERR(x) do { } while (0)
270 #if STATS && defined(CONFIG_SMP)
271 #define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
272 #define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
273 #define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
274 #define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
276 #define STATS_INC_ALLOCHIT(x) do { } while (0)
277 #define STATS_INC_ALLOCMISS(x) do { } while (0)
278 #define STATS_INC_FREEHIT(x) do { } while (0)
279 #define STATS_INC_FREEMISS(x) do { } while (0)
283 /* Magic nums for obj red zoning.
284 * Placed in the first word before and the first word after an obj.
286 #define RED_MAGIC1 0x5A2CF071UL /* when obj is active */
287 #define RED_MAGIC2 0x170FC2A5UL /* when obj is inactive */
289 /* ...and for poisoning */
290 #define POISON_BYTE 0x5a /* byte value for poisoning */
291 #define POISON_END 0xa5 /* end-byte of poisoning */
295 /* maximum size of an obj (in 2^order pages) */
296 #define MAX_OBJ_ORDER 5 /* 32 pages */
299 * Do not go above this order unless 0 objects fit into the slab.
301 #define BREAK_GFP_ORDER_HI 2
302 #define BREAK_GFP_ORDER_LO 1
303 static int slab_break_gfp_order
= BREAK_GFP_ORDER_LO
;
306 * Absolute limit for the gfp order
308 #define MAX_GFP_ORDER 5 /* 32 pages */
311 /* Macros for storing/retrieving the cachep and or slab from the
312 * global 'mem_map'. These are used to find the slab an obj belongs to.
313 * With kfree(), these are used to find the cache which an obj belongs to.
315 #define SET_PAGE_CACHE(pg,x) ((pg)->list.next = (struct list_head *)(x))
316 #define GET_PAGE_CACHE(pg) ((kmem_cache_t *)(pg)->list.next)
317 #define SET_PAGE_SLAB(pg,x) ((pg)->list.prev = (struct list_head *)(x))
318 #define GET_PAGE_SLAB(pg) ((slab_t *)(pg)->list.prev)
320 /* Size description struct for general caches. */
321 typedef struct cache_sizes
{
323 kmem_cache_t
*cs_cachep
;
324 kmem_cache_t
*cs_dmacachep
;
327 static cache_sizes_t cache_sizes
[] = {
328 #if PAGE_SIZE == 4096
339 { 16384, NULL
, NULL
},
340 { 32768, NULL
, NULL
},
341 { 65536, NULL
, NULL
},
342 {131072, NULL
, NULL
},
346 /* internal cache of cache description objs */
347 static kmem_cache_t cache_cache
= {
348 slabs
: LIST_HEAD_INIT(cache_cache
.slabs
),
349 firstnotfull
: &cache_cache
.slabs
,
350 objsize
: sizeof(kmem_cache_t
),
352 spinlock
: SPIN_LOCK_UNLOCKED
,
353 colour_off
: L1_CACHE_BYTES
,
357 /* Guard access to the cache-chain. */
358 static struct semaphore cache_chain_sem
;
360 /* Place maintainer for reaping. */
361 static kmem_cache_t
*clock_searchp
= &cache_cache
;
363 #define cache_chain (cache_cache.next)
367 * chicken and egg problem: delay the per-cpu array allocation
368 * until the general caches are up.
370 static int g_cpucache_up
;
372 static void enable_cpucache (kmem_cache_t
*cachep
);
373 static void enable_all_cpucaches (void);
376 /* Cal the num objs, wastage, and bytes left over for a given slab size. */
377 static void kmem_cache_estimate (unsigned long gfporder
, size_t size
,
378 int flags
, size_t *left_over
, unsigned int *num
)
381 size_t wastage
= PAGE_SIZE
<<gfporder
;
385 if (!(flags
& CFLGS_OFF_SLAB
)) {
386 base
= sizeof(slab_t
);
387 extra
= sizeof(kmem_bufctl_t
);
390 while (i
*size
+ L1_CACHE_ALIGN(base
+i
*extra
) <= wastage
)
400 wastage
-= L1_CACHE_ALIGN(base
+i
*extra
);
401 *left_over
= wastage
;
404 /* Initialisation - setup the `cache' cache. */
405 void __init
kmem_cache_init(void)
409 init_MUTEX(&cache_chain_sem
);
410 INIT_LIST_HEAD(&cache_chain
);
412 kmem_cache_estimate(0, cache_cache
.objsize
, 0,
413 &left_over
, &cache_cache
.num
);
414 if (!cache_cache
.num
)
417 cache_cache
.colour
= left_over
/cache_cache
.colour_off
;
418 cache_cache
.colour_next
= 0;
422 /* Initialisation - setup remaining internal and general caches.
423 * Called after the gfp() functions have been enabled, and before smp_init().
425 void __init
kmem_cache_sizes_init(void)
427 cache_sizes_t
*sizes
= cache_sizes
;
430 * Fragmentation resistance on low memory - only use bigger
431 * page orders on machines with more than 32MB of memory.
433 if (num_physpages
> (32 << 20) >> PAGE_SHIFT
)
434 slab_break_gfp_order
= BREAK_GFP_ORDER_HI
;
436 /* For performance, all the general caches are L1 aligned.
437 * This should be particularly beneficial on SMP boxes, as it
438 * eliminates "false sharing".
439 * Note for systems short on memory removing the alignment will
440 * allow tighter packing of the smaller caches. */
441 sprintf(name
,"size-%Zd",sizes
->cs_size
);
442 if (!(sizes
->cs_cachep
=
443 kmem_cache_create(name
, sizes
->cs_size
,
444 0, SLAB_HWCACHE_ALIGN
, NULL
, NULL
))) {
448 /* Inc off-slab bufctl limit until the ceiling is hit. */
449 if (!(OFF_SLAB(sizes
->cs_cachep
))) {
450 offslab_limit
= sizes
->cs_size
-sizeof(slab_t
);
453 sprintf(name
, "size-%Zd(DMA)",sizes
->cs_size
);
454 sizes
->cs_dmacachep
= kmem_cache_create(name
, sizes
->cs_size
, 0,
455 SLAB_CACHE_DMA
|SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
456 if (!sizes
->cs_dmacachep
)
459 } while (sizes
->cs_size
);
462 int __init
kmem_cpucache_init(void)
466 enable_all_cpucaches();
471 __initcall(kmem_cpucache_init
);
473 /* Interface to system's page allocator. No need to hold the cache-lock.
475 static inline void * kmem_getpages (kmem_cache_t
*cachep
, unsigned long flags
)
480 * If we requested dmaable memory, we will get it. Even if we
481 * did not request dmaable memory, we might get it, but that
482 * would be relatively rare and ignorable.
484 flags
|= cachep
->gfpflags
;
485 addr
= (void*) __get_free_pages(flags
, cachep
->gfporder
);
486 /* Assume that now we have the pages no one else can legally
487 * messes with the 'struct page's.
488 * However vm_scan() might try to test the structure to see if
489 * it is a named-page or buffer-page. The members it tests are
490 * of no interest here.....
495 /* Interface to system's page release. */
496 static inline void kmem_freepages (kmem_cache_t
*cachep
, void *addr
)
498 unsigned long i
= (1<<cachep
->gfporder
);
499 struct page
*page
= virt_to_page(addr
);
501 /* free_pages() does not clear the type bit - we do that.
502 * The pages have been unlinked from their cache-slab,
503 * but their 'struct page's might be accessed in
504 * vm_scan(). Shouldn't be a worry.
510 free_pages((unsigned long)addr
, cachep
->gfporder
);
514 static inline void kmem_poison_obj (kmem_cache_t
*cachep
, void *addr
)
516 int size
= cachep
->objsize
;
517 if (cachep
->flags
& SLAB_RED_ZONE
) {
518 addr
+= BYTES_PER_WORD
;
519 size
-= 2*BYTES_PER_WORD
;
521 memset(addr
, POISON_BYTE
, size
);
522 *(unsigned char *)(addr
+size
-1) = POISON_END
;
525 static inline int kmem_check_poison_obj (kmem_cache_t
*cachep
, void *addr
)
527 int size
= cachep
->objsize
;
529 if (cachep
->flags
& SLAB_RED_ZONE
) {
530 addr
+= BYTES_PER_WORD
;
531 size
-= 2*BYTES_PER_WORD
;
533 end
= memchr(addr
, POISON_END
, size
);
534 if (end
!= (addr
+size
-1))
540 /* Destroy all the objs in a slab, and release the mem back to the system.
541 * Before calling the slab must have been unlinked from the cache.
542 * The cache-lock is not held/needed.
544 static void kmem_slab_destroy (kmem_cache_t
*cachep
, slab_t
*slabp
)
548 || cachep
->flags
& (SLAB_POISON
| SLAB_RED_ZONE
)
552 for (i
= 0; i
< cachep
->num
; i
++) {
553 void* objp
= slabp
->s_mem
+cachep
->objsize
*i
;
555 if (cachep
->flags
& SLAB_RED_ZONE
) {
556 if (*((unsigned long*)(objp
)) != RED_MAGIC1
)
558 if (*((unsigned long*)(objp
+ cachep
->objsize
559 -BYTES_PER_WORD
)) != RED_MAGIC1
)
561 objp
+= BYTES_PER_WORD
;
565 (cachep
->dtor
)(objp
, cachep
, 0);
567 if (cachep
->flags
& SLAB_RED_ZONE
) {
568 objp
-= BYTES_PER_WORD
;
570 if ((cachep
->flags
& SLAB_POISON
) &&
571 kmem_check_poison_obj(cachep
, objp
))
577 kmem_freepages(cachep
, slabp
->s_mem
-slabp
->colouroff
);
578 if (OFF_SLAB(cachep
))
579 kmem_cache_free(cachep
->slabp_cache
, slabp
);
583 * kmem_cache_create - Create a cache.
584 * @name: A string which is used in /proc/slabinfo to identify this cache.
585 * @size: The size of objects to be created in this cache.
586 * @offset: The offset to use within the page.
588 * @ctor: A constructor for the objects.
589 * @dtor: A destructor for the objects.
591 * Returns a ptr to the cache on success, NULL on failure.
592 * Cannot be called within a int, but can be interrupted.
593 * The @ctor is run when new pages are allocated by the cache
594 * and the @dtor is run before the pages are handed back.
597 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
598 * to catch references to uninitialised memory.
600 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
601 * for buffer overruns.
603 * %SLAB_NO_REAP - Don't automatically reap this cache when we're under
606 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
607 * cacheline. This can be beneficial if you're counting cycles as closely
611 kmem_cache_create (const char *name
, size_t size
, size_t offset
,
612 unsigned long flags
, void (*ctor
)(void*, kmem_cache_t
*, unsigned long),
613 void (*dtor
)(void*, kmem_cache_t
*, unsigned long))
615 const char *func_nm
= KERN_ERR
"kmem_create: ";
616 size_t left_over
, align
, slab_size
;
617 kmem_cache_t
*cachep
= NULL
;
620 * Sanity checks... these are all serious usage bugs.
623 ((strlen(name
) >= CACHE_NAMELEN
- 1)) ||
625 (size
< BYTES_PER_WORD
) ||
626 (size
> (1<<MAX_OBJ_ORDER
)*PAGE_SIZE
) ||
628 (offset
< 0 || offset
> size
))
632 if ((flags
& SLAB_DEBUG_INITIAL
) && !ctor
) {
633 /* No constructor, but inital state check requested */
634 printk("%sNo con, but init state check requested - %s\n", func_nm
, name
);
635 flags
&= ~SLAB_DEBUG_INITIAL
;
638 if ((flags
& SLAB_POISON
) && ctor
) {
639 /* request for poisoning, but we can't do that with a constructor */
640 printk("%sPoisoning requested, but con given - %s\n", func_nm
, name
);
641 flags
&= ~SLAB_POISON
;
644 if (size
< (PAGE_SIZE
>>3))
646 * do not red zone large object, causes severe
649 flags
|= SLAB_RED_ZONE
;
651 flags
|= SLAB_POISON
;
656 * Always checks flags, a caller might be expecting debug
657 * support which isn't available.
659 if (flags
& ~CREATE_MASK
)
662 /* Get cache's description obj. */
663 cachep
= (kmem_cache_t
*) kmem_cache_alloc(&cache_cache
, SLAB_KERNEL
);
666 memset(cachep
, 0, sizeof(kmem_cache_t
));
668 /* Check that size is in terms of words. This is needed to avoid
669 * unaligned accesses for some archs when redzoning is used, and makes
670 * sure any on-slab bufctl's are also correctly aligned.
672 if (size
& (BYTES_PER_WORD
-1)) {
673 size
+= (BYTES_PER_WORD
-1);
674 size
&= ~(BYTES_PER_WORD
-1);
675 printk("%sForcing size word alignment - %s\n", func_nm
, name
);
679 if (flags
& SLAB_RED_ZONE
) {
681 * There is no point trying to honour cache alignment
684 flags
&= ~SLAB_HWCACHE_ALIGN
;
685 size
+= 2*BYTES_PER_WORD
; /* words for redzone */
688 align
= BYTES_PER_WORD
;
689 if (flags
& SLAB_HWCACHE_ALIGN
)
690 align
= L1_CACHE_BYTES
;
692 /* Determine if the slab management is 'on' or 'off' slab. */
693 if (size
>= (PAGE_SIZE
>>3))
695 * Size is large, assume best to place the slab management obj
696 * off-slab (should allow better packing of objs).
698 flags
|= CFLGS_OFF_SLAB
;
700 if (flags
& SLAB_HWCACHE_ALIGN
) {
701 /* Need to adjust size so that objs are cache aligned. */
702 /* Small obj size, can get at least two per cache line. */
703 /* FIXME: only power of 2 supported, was better */
704 while (size
< align
/2)
706 size
= (size
+align
-1)&(~(align
-1));
709 /* Cal size (in pages) of slabs, and the num of objs per slab.
710 * This could be made much more intelligent. For now, try to avoid
711 * using high page-orders for slabs. When the gfp() funcs are more
712 * friendly towards high-order requests, this should be changed.
715 unsigned int break_flag
= 0;
717 kmem_cache_estimate(cachep
->gfporder
, size
, flags
,
718 &left_over
, &cachep
->num
);
721 if (cachep
->gfporder
>= MAX_GFP_ORDER
)
725 if (flags
& CFLGS_OFF_SLAB
&& cachep
->num
> offslab_limit
) {
726 /* Oops, this num of objs will cause problems. */
733 * Large num of objs is good, but v. large slabs are currently
734 * bad for the gfp()s.
736 if (cachep
->gfporder
>= slab_break_gfp_order
)
739 if ((left_over
*8) <= (PAGE_SIZE
<<cachep
->gfporder
))
740 break; /* Acceptable internal fragmentation. */
746 printk("kmem_cache_create: couldn't create cache %s.\n", name
);
747 kmem_cache_free(&cache_cache
, cachep
);
751 slab_size
= L1_CACHE_ALIGN(cachep
->num
*sizeof(kmem_bufctl_t
)+sizeof(slab_t
));
754 * If the slab has been placed off-slab, and we have enough space then
755 * move it on-slab. This is at the expense of any extra colouring.
757 if (flags
& CFLGS_OFF_SLAB
&& left_over
>= slab_size
) {
758 flags
&= ~CFLGS_OFF_SLAB
;
759 left_over
-= slab_size
;
762 /* Offset must be a multiple of the alignment. */
764 offset
&= ~(align
-1);
766 offset
= L1_CACHE_BYTES
;
767 cachep
->colour_off
= offset
;
768 cachep
->colour
= left_over
/offset
;
770 /* init remaining fields */
771 if (!cachep
->gfporder
&& !(flags
& CFLGS_OFF_SLAB
))
772 flags
|= CFLGS_OPTIMIZE
;
774 cachep
->flags
= flags
;
775 cachep
->gfpflags
= 0;
776 if (flags
& SLAB_CACHE_DMA
)
777 cachep
->gfpflags
|= GFP_DMA
;
778 spin_lock_init(&cachep
->spinlock
);
779 cachep
->objsize
= size
;
780 INIT_LIST_HEAD(&cachep
->slabs
);
781 cachep
->firstnotfull
= &cachep
->slabs
;
783 if (flags
& CFLGS_OFF_SLAB
)
784 cachep
->slabp_cache
= kmem_find_general_cachep(slab_size
,0);
787 /* Copy name over so we don't have problems with unloaded modules */
788 strcpy(cachep
->name
, name
);
792 enable_cpucache(cachep
);
794 /* Need the semaphore to access the chain. */
795 down(&cache_chain_sem
);
799 list_for_each(p
, &cache_chain
) {
800 kmem_cache_t
*pc
= list_entry(p
, kmem_cache_t
, next
);
802 /* The name field is constant - no lock needed. */
803 if (!strcmp(pc
->name
, name
))
808 /* There is no reason to lock our new cache before we
809 * link it in - no one knows about it yet...
811 list_add(&cachep
->next
, &cache_chain
);
812 up(&cache_chain_sem
);
818 * This check if the kmem_cache_t pointer is chained in the cache_cache
821 static int is_chained_kmem_cache(kmem_cache_t
* cachep
)
826 /* Find the cache in the chain of caches. */
827 down(&cache_chain_sem
);
828 list_for_each(p
, &cache_chain
) {
829 if (p
== &cachep
->next
) {
834 up(&cache_chain_sem
);
841 * Waits for all CPUs to execute func().
843 static void smp_call_function_all_cpus(void (*func
) (void *arg
), void *arg
)
849 if (smp_call_function(func
, arg
, 1, 1))
852 typedef struct ccupdate_struct_s
854 kmem_cache_t
*cachep
;
855 cpucache_t
*new[NR_CPUS
];
858 static void do_ccupdate_local(void *info
)
860 ccupdate_struct_t
*new = (ccupdate_struct_t
*)info
;
861 cpucache_t
*old
= cc_data(new->cachep
);
863 cc_data(new->cachep
) = new->new[smp_processor_id()];
864 new->new[smp_processor_id()] = old
;
867 static void free_block (kmem_cache_t
* cachep
, void** objpp
, int len
);
869 static void drain_cpu_caches(kmem_cache_t
*cachep
)
871 ccupdate_struct_t
new;
874 memset(&new.new,0,sizeof(new.new));
878 down(&cache_chain_sem
);
879 smp_call_function_all_cpus(do_ccupdate_local
, (void *)&new);
881 for (i
= 0; i
< smp_num_cpus
; i
++) {
882 cpucache_t
* ccold
= new.new[cpu_logical_map(i
)];
883 if (!ccold
|| (ccold
->avail
== 0))
886 free_block(cachep
, cc_entry(ccold
), ccold
->avail
);
890 smp_call_function_all_cpus(do_ccupdate_local
, (void *)&new);
891 up(&cache_chain_sem
);
895 #define drain_cpu_caches(cachep) do { } while (0)
898 static int __kmem_cache_shrink(kmem_cache_t
*cachep
)
903 drain_cpu_caches(cachep
);
905 spin_lock_irq(&cachep
->spinlock
);
907 /* If the cache is growing, stop shrinking. */
908 while (!cachep
->growing
) {
911 p
= cachep
->slabs
.prev
;
912 if (p
== &cachep
->slabs
)
915 slabp
= list_entry(cachep
->slabs
.prev
, slab_t
, list
);
919 list_del(&slabp
->list
);
920 if (cachep
->firstnotfull
== &slabp
->list
)
921 cachep
->firstnotfull
= &cachep
->slabs
;
923 spin_unlock_irq(&cachep
->spinlock
);
924 kmem_slab_destroy(cachep
, slabp
);
925 spin_lock_irq(&cachep
->spinlock
);
927 ret
= !list_empty(&cachep
->slabs
);
928 spin_unlock_irq(&cachep
->spinlock
);
933 * kmem_cache_shrink - Shrink a cache.
934 * @cachep: The cache to shrink.
936 * Releases as many slabs as possible for a cache.
937 * To help debugging, a zero exit status indicates all slabs were released.
939 int kmem_cache_shrink(kmem_cache_t
*cachep
)
941 if (!cachep
|| in_interrupt() || !is_chained_kmem_cache(cachep
))
944 return __kmem_cache_shrink(cachep
);
948 * kmem_cache_destroy - delete a cache
949 * @cachep: the cache to destroy
951 * Remove a kmem_cache_t object from the slab cache.
952 * Returns 0 on success.
954 * It is expected this function will be called by a module when it is
955 * unloaded. This will remove the cache completely, and avoid a duplicate
956 * cache being allocated each time a module is loaded and unloaded, if the
957 * module doesn't have persistent in-kernel storage across loads and unloads.
959 * The caller must guarantee that noone will allocate memory from the cache
960 * during the kmem_cache_destroy().
962 int kmem_cache_destroy (kmem_cache_t
* cachep
)
964 if (!cachep
|| in_interrupt() || cachep
->growing
)
967 /* Find the cache in the chain of caches. */
968 down(&cache_chain_sem
);
969 /* the chain is never empty, cache_cache is never destroyed */
970 if (clock_searchp
== cachep
)
971 clock_searchp
= list_entry(cachep
->next
.next
,
973 list_del(&cachep
->next
);
974 up(&cache_chain_sem
);
976 if (__kmem_cache_shrink(cachep
)) {
977 printk(KERN_ERR
"kmem_cache_destroy: Can't free all objects %p\n",
979 down(&cache_chain_sem
);
980 list_add(&cachep
->next
,&cache_chain
);
981 up(&cache_chain_sem
);
987 for (i
= 0; i
< NR_CPUS
; i
++)
988 kfree(cachep
->cpudata
[i
]);
991 kmem_cache_free(&cache_cache
, cachep
);
996 /* Get the memory for a slab management obj. */
997 static inline slab_t
* kmem_cache_slabmgmt (kmem_cache_t
*cachep
,
998 void *objp
, int colour_off
, int local_flags
)
1002 if (OFF_SLAB(cachep
)) {
1003 /* Slab management obj is off-slab. */
1004 slabp
= kmem_cache_alloc(cachep
->slabp_cache
, local_flags
);
1010 * if you enable OPTIMIZE
1012 slabp
= objp
+colour_off
;
1013 colour_off
+= L1_CACHE_ALIGN(cachep
->num
*
1014 sizeof(kmem_bufctl_t
) + sizeof(slab_t
));
1017 slabp
->colouroff
= colour_off
;
1018 slabp
->s_mem
= objp
+colour_off
;
1023 static inline void kmem_cache_init_objs (kmem_cache_t
* cachep
,
1024 slab_t
* slabp
, unsigned long ctor_flags
)
1028 for (i
= 0; i
< cachep
->num
; i
++) {
1029 void* objp
= slabp
->s_mem
+cachep
->objsize
*i
;
1031 if (cachep
->flags
& SLAB_RED_ZONE
) {
1032 *((unsigned long*)(objp
)) = RED_MAGIC1
;
1033 *((unsigned long*)(objp
+ cachep
->objsize
-
1034 BYTES_PER_WORD
)) = RED_MAGIC1
;
1035 objp
+= BYTES_PER_WORD
;
1040 * Constructors are not allowed to allocate memory from
1041 * the same cache which they are a constructor for.
1042 * Otherwise, deadlock. They must also be threaded.
1045 cachep
->ctor(objp
, cachep
, ctor_flags
);
1047 if (cachep
->flags
& SLAB_RED_ZONE
)
1048 objp
-= BYTES_PER_WORD
;
1049 if (cachep
->flags
& SLAB_POISON
)
1050 /* need to poison the objs */
1051 kmem_poison_obj(cachep
, objp
);
1052 if (cachep
->flags
& SLAB_RED_ZONE
) {
1053 if (*((unsigned long*)(objp
)) != RED_MAGIC1
)
1055 if (*((unsigned long*)(objp
+ cachep
->objsize
-
1056 BYTES_PER_WORD
)) != RED_MAGIC1
)
1060 slab_bufctl(slabp
)[i
] = i
+1;
1062 slab_bufctl(slabp
)[i
-1] = BUFCTL_END
;
1067 * Grow (by 1) the number of slabs within a cache. This is called by
1068 * kmem_cache_alloc() when there are no active objs left in a cache.
1070 static int kmem_cache_grow (kmem_cache_t
* cachep
, int flags
)
1076 unsigned int i
, local_flags
;
1077 unsigned long ctor_flags
;
1078 unsigned long save_flags
;
1080 /* Be lazy and only check for valid flags here,
1081 * keeping it out of the critical path in kmem_cache_alloc().
1083 if (flags
& ~(SLAB_DMA
|SLAB_LEVEL_MASK
|SLAB_NO_GROW
))
1085 if (flags
& SLAB_NO_GROW
)
1089 * The test for missing atomic flag is performed here, rather than
1090 * the more obvious place, simply to reduce the critical path length
1091 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
1092 * will eventually be caught here (where it matters).
1094 if (in_interrupt() && (flags
& SLAB_LEVEL_MASK
) != SLAB_ATOMIC
)
1097 ctor_flags
= SLAB_CTOR_CONSTRUCTOR
;
1098 local_flags
= (flags
& SLAB_LEVEL_MASK
);
1099 if (local_flags
== SLAB_ATOMIC
)
1101 * Not allowed to sleep. Need to tell a constructor about
1102 * this - it might need to know...
1104 ctor_flags
|= SLAB_CTOR_ATOMIC
;
1106 /* About to mess with non-constant members - lock. */
1107 spin_lock_irqsave(&cachep
->spinlock
, save_flags
);
1109 /* Get colour for the slab, and cal the next value. */
1110 offset
= cachep
->colour_next
;
1111 cachep
->colour_next
++;
1112 if (cachep
->colour_next
>= cachep
->colour
)
1113 cachep
->colour_next
= 0;
1114 offset
*= cachep
->colour_off
;
1115 cachep
->dflags
|= DFLGS_GROWN
;
1118 spin_unlock_irqrestore(&cachep
->spinlock
, save_flags
);
1120 /* A series of memory allocations for a new slab.
1121 * Neither the cache-chain semaphore, or cache-lock, are
1122 * held, but the incrementing c_growing prevents this
1123 * cache from being reaped or shrunk.
1124 * Note: The cache could be selected in for reaping in
1125 * kmem_cache_reap(), but when the final test is made the
1126 * growing value will be seen.
1129 /* Get mem for the objs. */
1130 if (!(objp
= kmem_getpages(cachep
, flags
)))
1133 /* Get slab management. */
1134 if (!(slabp
= kmem_cache_slabmgmt(cachep
, objp
, offset
, local_flags
)))
1137 /* Nasty!!!!!! I hope this is OK. */
1138 i
= 1 << cachep
->gfporder
;
1139 page
= virt_to_page(objp
);
1141 SET_PAGE_CACHE(page
, cachep
);
1142 SET_PAGE_SLAB(page
, slabp
);
1147 kmem_cache_init_objs(cachep
, slabp
, ctor_flags
);
1149 spin_lock_irqsave(&cachep
->spinlock
, save_flags
);
1152 /* Make slab active. */
1153 list_add_tail(&slabp
->list
,&cachep
->slabs
);
1154 if (cachep
->firstnotfull
== &cachep
->slabs
)
1155 cachep
->firstnotfull
= &slabp
->list
;
1156 STATS_INC_GROWN(cachep
);
1157 cachep
->failures
= 0;
1159 spin_unlock_irqrestore(&cachep
->spinlock
, save_flags
);
1162 kmem_freepages(cachep
, objp
);
1164 spin_lock_irqsave(&cachep
->spinlock
, save_flags
);
1166 spin_unlock_irqrestore(&cachep
->spinlock
, save_flags
);
1171 * Perform extra freeing checks:
1172 * - detect double free
1173 * - detect bad pointers.
1174 * Called with the cache-lock held.
1178 static int kmem_extra_free_checks (kmem_cache_t
* cachep
,
1179 slab_t
*slabp
, void * objp
)
1182 unsigned int objnr
= (objp
-slabp
->s_mem
)/cachep
->objsize
;
1184 if (objnr
>= cachep
->num
)
1186 if (objp
!= slabp
->s_mem
+ objnr
*cachep
->objsize
)
1189 /* Check slab's freelist to see if this obj is there. */
1190 for (i
= slabp
->free
; i
!= BUFCTL_END
; i
= slab_bufctl(slabp
)[i
]) {
1198 static inline void kmem_cache_alloc_head(kmem_cache_t
*cachep
, int flags
)
1201 if (flags
& SLAB_DMA
) {
1202 if (!(cachep
->gfpflags
& GFP_DMA
))
1205 if (cachep
->gfpflags
& GFP_DMA
)
1211 static inline void * kmem_cache_alloc_one_tail (kmem_cache_t
*cachep
,
1216 STATS_INC_ALLOCED(cachep
);
1217 STATS_INC_ACTIVE(cachep
);
1218 STATS_SET_HIGH(cachep
);
1220 /* get obj pointer */
1222 objp
= slabp
->s_mem
+ slabp
->free
*cachep
->objsize
;
1223 slabp
->free
=slab_bufctl(slabp
)[slabp
->free
];
1225 if (slabp
->free
== BUFCTL_END
)
1226 /* slab now full: move to next slab for next alloc */
1227 cachep
->firstnotfull
= slabp
->list
.next
;
1229 if (cachep
->flags
& SLAB_POISON
)
1230 if (kmem_check_poison_obj(cachep
, objp
))
1232 if (cachep
->flags
& SLAB_RED_ZONE
) {
1233 /* Set alloc red-zone, and check old one. */
1234 if (xchg((unsigned long *)objp
, RED_MAGIC2
) !=
1237 if (xchg((unsigned long *)(objp
+cachep
->objsize
-
1238 BYTES_PER_WORD
), RED_MAGIC2
) != RED_MAGIC1
)
1240 objp
+= BYTES_PER_WORD
;
1247 * Returns a ptr to an obj in the given cache.
1248 * caller must guarantee synchronization
1249 * #define for the goto optimization 8-)
1251 #define kmem_cache_alloc_one(cachep) \
1255 /* Get slab alloc is to come from. */ \
1257 struct list_head* p = cachep->firstnotfull; \
1258 if (p == &cachep->slabs) \
1259 goto alloc_new_slab; \
1260 slabp = list_entry(p,slab_t, list); \
1262 kmem_cache_alloc_one_tail(cachep, slabp); \
1266 void* kmem_cache_alloc_batch(kmem_cache_t
* cachep
, int flags
)
1268 int batchcount
= cachep
->batchcount
;
1269 cpucache_t
* cc
= cc_data(cachep
);
1271 spin_lock(&cachep
->spinlock
);
1272 while (batchcount
--) {
1273 /* Get slab alloc is to come from. */
1274 struct list_head
*p
= cachep
->firstnotfull
;
1277 if (p
== &cachep
->slabs
)
1279 slabp
= list_entry(p
,slab_t
, list
);
1280 cc_entry(cc
)[cc
->avail
++] =
1281 kmem_cache_alloc_one_tail(cachep
, slabp
);
1283 spin_unlock(&cachep
->spinlock
);
1286 return cc_entry(cc
)[--cc
->avail
];
1291 static inline void * __kmem_cache_alloc (kmem_cache_t
*cachep
, int flags
)
1293 unsigned long save_flags
;
1296 kmem_cache_alloc_head(cachep
, flags
);
1298 local_irq_save(save_flags
);
1301 cpucache_t
*cc
= cc_data(cachep
);
1305 STATS_INC_ALLOCHIT(cachep
);
1306 objp
= cc_entry(cc
)[--cc
->avail
];
1308 STATS_INC_ALLOCMISS(cachep
);
1309 objp
= kmem_cache_alloc_batch(cachep
,flags
);
1311 goto alloc_new_slab_nolock
;
1314 spin_lock(&cachep
->spinlock
);
1315 objp
= kmem_cache_alloc_one(cachep
);
1316 spin_unlock(&cachep
->spinlock
);
1320 objp
= kmem_cache_alloc_one(cachep
);
1322 local_irq_restore(save_flags
);
1326 spin_unlock(&cachep
->spinlock
);
1327 alloc_new_slab_nolock
:
1329 local_irq_restore(save_flags
);
1330 if (kmem_cache_grow(cachep
, flags
))
1331 /* Someone may have stolen our objs. Doesn't matter, we'll
1332 * just come back here again.
1339 * Release an obj back to its cache. If the obj has a constructed
1340 * state, it should be in this state _before_ it is released.
1341 * - caller is responsible for the synchronization
1345 # define CHECK_NR(pg) \
1347 if (!VALID_PAGE(pg)) { \
1348 printk(KERN_ERR "kfree: out of range ptr %lxh.\n", \
1349 (unsigned long)objp); \
1353 # define CHECK_PAGE(page) \
1356 if (!PageSlab(page)) { \
1357 printk(KERN_ERR "kfree: bad ptr %lxh.\n", \
1358 (unsigned long)objp); \
1364 # define CHECK_PAGE(pg) do { } while (0)
1367 static inline void kmem_cache_free_one(kmem_cache_t
*cachep
, void *objp
)
1371 CHECK_PAGE(virt_to_page(objp
));
1372 /* reduces memory footprint
1374 if (OPTIMIZE(cachep))
1375 slabp = (void*)((unsigned long)objp&(~(PAGE_SIZE-1)));
1378 slabp
= GET_PAGE_SLAB(virt_to_page(objp
));
1381 if (cachep
->flags
& SLAB_DEBUG_INITIAL
)
1382 /* Need to call the slab's constructor so the
1383 * caller can perform a verify of its state (debugging).
1384 * Called without the cache-lock held.
1386 cachep
->ctor(objp
, cachep
, SLAB_CTOR_CONSTRUCTOR
|SLAB_CTOR_VERIFY
);
1388 if (cachep
->flags
& SLAB_RED_ZONE
) {
1389 objp
-= BYTES_PER_WORD
;
1390 if (xchg((unsigned long *)objp
, RED_MAGIC1
) != RED_MAGIC2
)
1391 /* Either write before start, or a double free. */
1393 if (xchg((unsigned long *)(objp
+cachep
->objsize
-
1394 BYTES_PER_WORD
), RED_MAGIC1
) != RED_MAGIC2
)
1395 /* Either write past end, or a double free. */
1398 if (cachep
->flags
& SLAB_POISON
)
1399 kmem_poison_obj(cachep
, objp
);
1400 if (kmem_extra_free_checks(cachep
, slabp
, objp
))
1404 unsigned int objnr
= (objp
-slabp
->s_mem
)/cachep
->objsize
;
1406 slab_bufctl(slabp
)[objnr
] = slabp
->free
;
1407 slabp
->free
= objnr
;
1409 STATS_DEC_ACTIVE(cachep
);
1411 /* fixup slab chain */
1412 if (slabp
->inuse
-- == cachep
->num
)
1413 goto moveslab_partial
;
1420 * Even if the page is now empty, we can set c_firstnotfull to
1421 * slabp: there are no partial slabs in this case
1424 struct list_head
*t
= cachep
->firstnotfull
;
1426 cachep
->firstnotfull
= &slabp
->list
;
1427 if (slabp
->list
.next
== t
)
1429 list_del(&slabp
->list
);
1430 list_add_tail(&slabp
->list
, t
);
1435 * was partial, now empty.
1436 * c_firstnotfull might point to slabp
1440 struct list_head
*t
= cachep
->firstnotfull
->prev
;
1442 list_del(&slabp
->list
);
1443 list_add_tail(&slabp
->list
, &cachep
->slabs
);
1444 if (cachep
->firstnotfull
== &slabp
->list
)
1445 cachep
->firstnotfull
= t
->next
;
1451 static inline void __free_block (kmem_cache_t
* cachep
,
1452 void** objpp
, int len
)
1454 for ( ; len
> 0; len
--, objpp
++)
1455 kmem_cache_free_one(cachep
, *objpp
);
1458 static void free_block (kmem_cache_t
* cachep
, void** objpp
, int len
)
1460 spin_lock(&cachep
->spinlock
);
1461 __free_block(cachep
, objpp
, len
);
1462 spin_unlock(&cachep
->spinlock
);
1468 * called with disabled ints
1470 static inline void __kmem_cache_free (kmem_cache_t
*cachep
, void* objp
)
1473 cpucache_t
*cc
= cc_data(cachep
);
1475 CHECK_PAGE(virt_to_page(objp
));
1478 if (cc
->avail
< cc
->limit
) {
1479 STATS_INC_FREEHIT(cachep
);
1480 cc_entry(cc
)[cc
->avail
++] = objp
;
1483 STATS_INC_FREEMISS(cachep
);
1484 batchcount
= cachep
->batchcount
;
1485 cc
->avail
-= batchcount
;
1487 &cc_entry(cc
)[cc
->avail
],batchcount
);
1488 cc_entry(cc
)[cc
->avail
++] = objp
;
1491 free_block(cachep
, &objp
, 1);
1494 kmem_cache_free_one(cachep
, objp
);
1499 * kmem_cache_alloc - Allocate an object
1500 * @cachep: The cache to allocate from.
1501 * @flags: See kmalloc().
1503 * Allocate an object from this cache. The flags are only relevant
1504 * if the cache has no available objects.
1506 void * kmem_cache_alloc (kmem_cache_t
*cachep
, int flags
)
1508 return __kmem_cache_alloc(cachep
, flags
);
1512 * kmalloc - allocate memory
1513 * @size: how many bytes of memory are required.
1514 * @flags: the type of memory to allocate.
1516 * kmalloc is the normal method of allocating memory
1517 * in the kernel. The @flags argument may be one of:
1521 * %GFP_ATOMIC - allocation will not sleep. Use inside interrupt handlers.
1523 * %GFP_USER - allocate memory on behalf of user. May sleep.
1525 * %GFP_KERNEL - allocate normal kernel ram. May sleep.
1527 * %GFP_NFS - has a slightly lower probability of sleeping than %GFP_KERNEL.
1528 * Don't use unless you're in the NFS code.
1530 * %GFP_KSWAPD - Don't use unless you're modifying kswapd.
1532 void * kmalloc (size_t size
, int flags
)
1534 cache_sizes_t
*csizep
= cache_sizes
;
1536 for (; csizep
->cs_size
; csizep
++) {
1537 if (size
> csizep
->cs_size
)
1539 return __kmem_cache_alloc(flags
& GFP_DMA
?
1540 csizep
->cs_dmacachep
: csizep
->cs_cachep
, flags
);
1542 BUG(); // too big size
1547 * kmem_cache_free - Deallocate an object
1548 * @cachep: The cache the allocation was from.
1549 * @objp: The previously allocated object.
1551 * Free an object which was previously allocated from this
1554 void kmem_cache_free (kmem_cache_t
*cachep
, void *objp
)
1556 unsigned long flags
;
1558 CHECK_PAGE(virt_to_page(objp
));
1559 if (cachep
!= GET_PAGE_CACHE(virt_to_page(objp
)))
1563 local_irq_save(flags
);
1564 __kmem_cache_free(cachep
, objp
);
1565 local_irq_restore(flags
);
1569 * kfree - free previously allocated memory
1570 * @objp: pointer returned by kmalloc.
1572 * Don't free memory not originally allocated by kmalloc()
1573 * or you will run into trouble.
1575 void kfree (const void *objp
)
1578 unsigned long flags
;
1582 local_irq_save(flags
);
1583 CHECK_PAGE(virt_to_page(objp
));
1584 c
= GET_PAGE_CACHE(virt_to_page(objp
));
1585 __kmem_cache_free(c
, (void*)objp
);
1586 local_irq_restore(flags
);
1589 kmem_cache_t
* kmem_find_general_cachep (size_t size
, int gfpflags
)
1591 cache_sizes_t
*csizep
= cache_sizes
;
1593 /* This function could be moved to the header file, and
1594 * made inline so consumers can quickly determine what
1595 * cache pointer they require.
1597 for ( ; csizep
->cs_size
; csizep
++) {
1598 if (size
> csizep
->cs_size
)
1602 return (gfpflags
& GFP_DMA
) ? csizep
->cs_dmacachep
: csizep
->cs_cachep
;
1607 /* called with cache_chain_sem acquired. */
1608 static int kmem_tune_cpucache (kmem_cache_t
* cachep
, int limit
, int batchcount
)
1610 ccupdate_struct_t
new;
1614 * These are admin-provided, so we are more graceful.
1620 if (batchcount
> limit
)
1622 if (limit
!= 0 && !batchcount
)
1625 memset(&new.new,0,sizeof(new.new));
1627 for (i
= 0; i
< smp_num_cpus
; i
++) {
1630 ccnew
= kmalloc(sizeof(void*)*limit
+
1631 sizeof(cpucache_t
), GFP_KERNEL
);
1634 ccnew
->limit
= limit
;
1636 new.new[cpu_logical_map(i
)] = ccnew
;
1639 new.cachep
= cachep
;
1640 spin_lock_irq(&cachep
->spinlock
);
1641 cachep
->batchcount
= batchcount
;
1642 spin_unlock_irq(&cachep
->spinlock
);
1644 smp_call_function_all_cpus(do_ccupdate_local
, (void *)&new);
1646 for (i
= 0; i
< smp_num_cpus
; i
++) {
1647 cpucache_t
* ccold
= new.new[cpu_logical_map(i
)];
1650 local_irq_disable();
1651 free_block(cachep
, cc_entry(ccold
), ccold
->avail
);
1657 for (i
--; i
>= 0; i
--)
1658 kfree(new.new[cpu_logical_map(i
)]);
1662 static void enable_cpucache (kmem_cache_t
*cachep
)
1667 /* FIXME: optimize */
1668 if (cachep
->objsize
> PAGE_SIZE
)
1670 if (cachep
->objsize
> 1024)
1672 else if (cachep
->objsize
> 256)
1677 err
= kmem_tune_cpucache(cachep
, limit
, limit
/2);
1679 printk(KERN_ERR
"enable_cpucache failed for %s, error %d.\n",
1680 cachep
->name
, -err
);
1683 static void enable_all_cpucaches (void)
1685 struct list_head
* p
;
1687 down(&cache_chain_sem
);
1689 p
= &cache_cache
.next
;
1691 kmem_cache_t
* cachep
= list_entry(p
, kmem_cache_t
, next
);
1693 enable_cpucache(cachep
);
1694 p
= cachep
->next
.next
;
1695 } while (p
!= &cache_cache
.next
);
1697 up(&cache_chain_sem
);
1702 * kmem_cache_reap - Reclaim memory from caches.
1703 * @gfp_mask: the type of memory required.
1705 * Called from try_to_free_page().
1707 void kmem_cache_reap (int gfp_mask
)
1710 kmem_cache_t
*searchp
;
1711 kmem_cache_t
*best_cachep
;
1712 unsigned int best_pages
;
1713 unsigned int best_len
;
1716 if (gfp_mask
& __GFP_WAIT
)
1717 down(&cache_chain_sem
);
1719 if (down_trylock(&cache_chain_sem
))
1722 scan
= REAP_SCANLEN
;
1726 searchp
= clock_searchp
;
1729 struct list_head
* p
;
1730 unsigned int full_free
;
1732 /* It's safe to test this without holding the cache-lock. */
1733 if (searchp
->flags
& SLAB_NO_REAP
)
1735 spin_lock_irq(&searchp
->spinlock
);
1736 if (searchp
->growing
)
1738 if (searchp
->dflags
& DFLGS_GROWN
) {
1739 searchp
->dflags
&= ~DFLGS_GROWN
;
1744 cpucache_t
*cc
= cc_data(searchp
);
1745 if (cc
&& cc
->avail
) {
1746 __free_block(searchp
, cc_entry(cc
), cc
->avail
);
1753 p
= searchp
->slabs
.prev
;
1754 while (p
!= &searchp
->slabs
) {
1755 slabp
= list_entry(p
, slab_t
, list
);
1763 * Try to avoid slabs with constructors and/or
1764 * more than one page per slab (as it can be difficult
1765 * to get high orders from gfp()).
1767 pages
= full_free
* (1<<searchp
->gfporder
);
1769 pages
= (pages
*4+1)/5;
1770 if (searchp
->gfporder
)
1771 pages
= (pages
*4+1)/5;
1772 if (pages
> best_pages
) {
1773 best_cachep
= searchp
;
1774 best_len
= full_free
;
1776 if (full_free
>= REAP_PERFECT
) {
1777 clock_searchp
= list_entry(searchp
->next
.next
,
1783 spin_unlock_irq(&searchp
->spinlock
);
1785 searchp
= list_entry(searchp
->next
.next
,kmem_cache_t
,next
);
1786 } while (--scan
&& searchp
!= clock_searchp
);
1788 clock_searchp
= searchp
;
1791 /* couldn't find anything to reap */
1794 spin_lock_irq(&best_cachep
->spinlock
);
1796 /* free only 80% of the free slabs */
1797 best_len
= (best_len
*4 + 1)/5;
1798 for (scan
= 0; scan
< best_len
; scan
++) {
1799 struct list_head
*p
;
1801 if (best_cachep
->growing
)
1803 p
= best_cachep
->slabs
.prev
;
1804 if (p
== &best_cachep
->slabs
)
1806 slabp
= list_entry(p
,slab_t
,list
);
1809 list_del(&slabp
->list
);
1810 if (best_cachep
->firstnotfull
== &slabp
->list
)
1811 best_cachep
->firstnotfull
= &best_cachep
->slabs
;
1812 STATS_INC_REAPED(best_cachep
);
1814 /* Safe to drop the lock. The slab is no longer linked to the
1817 spin_unlock_irq(&best_cachep
->spinlock
);
1818 kmem_slab_destroy(best_cachep
, slabp
);
1819 spin_lock_irq(&best_cachep
->spinlock
);
1821 spin_unlock_irq(&best_cachep
->spinlock
);
1823 up(&cache_chain_sem
);
1827 #ifdef CONFIG_PROC_FS
1829 * cache-name num-active-objs total-objs
1830 * obj-size num-active-slabs total-slabs
1831 * num-pages-per-slab
1839 if (len-off > count) \
1844 static int proc_getdata (char*page
, char**start
, off_t off
, int count
)
1846 struct list_head
*p
;
1849 /* Output format version, so at least we can change it without _too_
1852 len
+= sprintf(page
+len
, "slabinfo - version: 1.1"
1862 down(&cache_chain_sem
);
1863 p
= &cache_cache
.next
;
1865 kmem_cache_t
*cachep
;
1866 struct list_head
*q
;
1868 unsigned long active_objs
;
1869 unsigned long num_objs
;
1870 unsigned long active_slabs
= 0;
1871 unsigned long num_slabs
;
1872 cachep
= list_entry(p
, kmem_cache_t
, next
);
1874 spin_lock_irq(&cachep
->spinlock
);
1877 list_for_each(q
,&cachep
->slabs
) {
1878 slabp
= list_entry(q
, slab_t
, list
);
1879 active_objs
+= slabp
->inuse
;
1880 num_objs
+= cachep
->num
;
1886 num_slabs
+=active_slabs
;
1887 num_objs
= num_slabs
*cachep
->num
;
1889 len
+= sprintf(page
+len
, "%-17s %6lu %6lu %6u %4lu %4lu %4u",
1890 cachep
->name
, active_objs
, num_objs
, cachep
->objsize
,
1891 active_slabs
, num_slabs
, (1<<cachep
->gfporder
));
1895 unsigned long errors
= cachep
->errors
;
1896 unsigned long high
= cachep
->high_mark
;
1897 unsigned long grown
= cachep
->grown
;
1898 unsigned long reaped
= cachep
->reaped
;
1899 unsigned long allocs
= cachep
->num_allocations
;
1901 len
+= sprintf(page
+len
, " : %6lu %7lu %5lu %4lu %4lu",
1902 high
, allocs
, grown
, reaped
, errors
);
1907 unsigned int batchcount
= cachep
->batchcount
;
1910 if (cc_data(cachep
))
1911 limit
= cc_data(cachep
)->limit
;
1914 len
+= sprintf(page
+len
, " : %4u %4u",
1918 #if STATS && defined(CONFIG_SMP)
1920 unsigned long allochit
= atomic_read(&cachep
->allochit
);
1921 unsigned long allocmiss
= atomic_read(&cachep
->allocmiss
);
1922 unsigned long freehit
= atomic_read(&cachep
->freehit
);
1923 unsigned long freemiss
= atomic_read(&cachep
->freemiss
);
1924 len
+= sprintf(page
+len
, " : %6lu %6lu %6lu %6lu",
1925 allochit
, allocmiss
, freehit
, freemiss
);
1928 len
+= sprintf(page
+len
,"\n");
1929 spin_unlock_irq(&cachep
->spinlock
);
1931 p
= cachep
->next
.next
;
1932 } while (p
!= &cache_cache
.next
);
1934 up(&cache_chain_sem
);
1942 * slabinfo_read_proc - generates /proc/slabinfo
1943 * @page: scratch area, one page long
1944 * @start: pointer to the pointer to the output buffer
1945 * @off: offset within /proc/slabinfo the caller is interested in
1946 * @count: requested len in bytes
1950 * The contents of the buffer are
1957 * num-pages-per-slab
1958 * + further values on SMP and with statistics enabled
1960 int slabinfo_read_proc (char *page
, char **start
, off_t off
,
1961 int count
, int *eof
, void *data
)
1963 int len
= proc_getdata(page
, start
, off
, count
);
1964 len
-= (*start
-page
);
1967 if (len
>count
) len
= count
;
1972 #define MAX_SLABINFO_WRITE 128
1974 * slabinfo_write_proc - SMP tuning for the slab allocator
1976 * @buffer: user buffer
1980 int slabinfo_write_proc (struct file
*file
, const char *buffer
,
1981 unsigned long count
, void *data
)
1984 char kbuf
[MAX_SLABINFO_WRITE
], *tmp
;
1985 int limit
, batchcount
, res
;
1986 struct list_head
*p
;
1988 if (count
> MAX_SLABINFO_WRITE
)
1990 if (copy_from_user(&kbuf
, buffer
, count
))
1993 tmp
= strchr(kbuf
, ' ');
1998 limit
= simple_strtol(tmp
, &tmp
, 10);
2001 batchcount
= simple_strtol(tmp
, &tmp
, 10);
2003 /* Find the cache in the chain of caches. */
2004 down(&cache_chain_sem
);
2006 list_for_each(p
,&cache_chain
) {
2007 kmem_cache_t
*cachep
= list_entry(p
, kmem_cache_t
, next
);
2009 if (!strcmp(cachep
->name
, kbuf
)) {
2010 res
= kmem_tune_cpucache(cachep
, limit
, batchcount
);
2014 up(&cache_chain_sem
);