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 initializations 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 struct kmem_cache 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 mutex 'slab_mutex'.
72 * The sem is only needed when accessing/extending the cache-chain, which
73 * can never happen inside an interrupt (kmem_cache_create(),
74 * kmem_cache_shrink() and kmem_cache_reap()).
76 * At present, each engine can be growing a cache. This should be blocked.
78 * 15 March 2005. NUMA slab allocator.
79 * Shai Fultheim <shai@scalex86.org>.
80 * Shobhit Dayal <shobhit@calsoftinc.com>
81 * Alok N Kataria <alokk@calsoftinc.com>
82 * Christoph Lameter <christoph@lameter.com>
84 * Modified the slab allocator to be node aware on NUMA systems.
85 * Each node has its own list of partial, free and full slabs.
86 * All object allocations for a node occur from node specific slab lists.
89 #include <linux/slab.h>
91 #include <linux/poison.h>
92 #include <linux/swap.h>
93 #include <linux/cache.h>
94 #include <linux/interrupt.h>
95 #include <linux/init.h>
96 #include <linux/compiler.h>
97 #include <linux/cpuset.h>
98 #include <linux/proc_fs.h>
99 #include <linux/seq_file.h>
100 #include <linux/notifier.h>
101 #include <linux/kallsyms.h>
102 #include <linux/cpu.h>
103 #include <linux/sysctl.h>
104 #include <linux/module.h>
105 #include <linux/rcupdate.h>
106 #include <linux/string.h>
107 #include <linux/uaccess.h>
108 #include <linux/nodemask.h>
109 #include <linux/kmemleak.h>
110 #include <linux/mempolicy.h>
111 #include <linux/mutex.h>
112 #include <linux/fault-inject.h>
113 #include <linux/rtmutex.h>
114 #include <linux/reciprocal_div.h>
115 #include <linux/debugobjects.h>
116 #include <linux/kmemcheck.h>
117 #include <linux/memory.h>
118 #include <linux/prefetch.h>
120 #include <net/sock.h>
122 #include <asm/cacheflush.h>
123 #include <asm/tlbflush.h>
124 #include <asm/page.h>
126 #include <trace/events/kmem.h>
128 #include "internal.h"
133 * DEBUG - 1 for kmem_cache_create() to honour; SLAB_RED_ZONE & SLAB_POISON.
134 * 0 for faster, smaller code (especially in the critical paths).
136 * STATS - 1 to collect stats for /proc/slabinfo.
137 * 0 for faster, smaller code (especially in the critical paths).
139 * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
142 #ifdef CONFIG_DEBUG_SLAB
145 #define FORCED_DEBUG 1
149 #define FORCED_DEBUG 0
152 /* Shouldn't this be in a header file somewhere? */
153 #define BYTES_PER_WORD sizeof(void *)
154 #define REDZONE_ALIGN max(BYTES_PER_WORD, __alignof__(unsigned long long))
156 #ifndef ARCH_KMALLOC_FLAGS
157 #define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
161 * true if a page was allocated from pfmemalloc reserves for network-based
164 static bool pfmemalloc_active __read_mostly
;
169 * Bufctl's are used for linking objs within a slab
172 * This implementation relies on "struct page" for locating the cache &
173 * slab an object belongs to.
174 * This allows the bufctl structure to be small (one int), but limits
175 * the number of objects a slab (not a cache) can contain when off-slab
176 * bufctls are used. The limit is the size of the largest general cache
177 * that does not use off-slab slabs.
178 * For 32bit archs with 4 kB pages, is this 56.
179 * This is not serious, as it is only for large objects, when it is unwise
180 * to have too many per slab.
181 * Note: This limit can be raised by introducing a general cache whose size
182 * is less than 512 (PAGE_SIZE<<3), but greater than 256.
185 typedef unsigned int kmem_bufctl_t
;
186 #define BUFCTL_END (((kmem_bufctl_t)(~0U))-0)
187 #define BUFCTL_FREE (((kmem_bufctl_t)(~0U))-1)
188 #define BUFCTL_ACTIVE (((kmem_bufctl_t)(~0U))-2)
189 #define SLAB_LIMIT (((kmem_bufctl_t)(~0U))-3)
194 * Manages the objs in a slab. Placed either at the beginning of mem allocated
195 * for a slab, or allocated from an general cache.
196 * Slabs are chained into three list: fully used, partial, fully free slabs.
200 struct list_head list
;
201 void *s_mem
; /* including colour offset */
202 unsigned int inuse
; /* num of objs active in slab */
211 * - LIFO ordering, to hand out cache-warm objects from _alloc
212 * - reduce the number of linked list operations
213 * - reduce spinlock operations
215 * The limit is stored in the per-cpu structure to reduce the data cache
222 unsigned int batchcount
;
223 unsigned int touched
;
226 * Must have this definition in here for the proper
227 * alignment of array_cache. Also simplifies accessing
230 * Entries should not be directly dereferenced as
231 * entries belonging to slabs marked pfmemalloc will
232 * have the lower bits set SLAB_OBJ_PFMEMALLOC
236 #define SLAB_OBJ_PFMEMALLOC 1
237 static inline bool is_obj_pfmemalloc(void *objp
)
239 return (unsigned long)objp
& SLAB_OBJ_PFMEMALLOC
;
242 static inline void set_obj_pfmemalloc(void **objp
)
244 *objp
= (void *)((unsigned long)*objp
| SLAB_OBJ_PFMEMALLOC
);
248 static inline void clear_obj_pfmemalloc(void **objp
)
250 *objp
= (void *)((unsigned long)*objp
& ~SLAB_OBJ_PFMEMALLOC
);
254 * bootstrap: The caches do not work without cpuarrays anymore, but the
255 * cpuarrays are allocated from the generic caches...
257 #define BOOT_CPUCACHE_ENTRIES 1
258 struct arraycache_init
{
259 struct array_cache cache
;
260 void *entries
[BOOT_CPUCACHE_ENTRIES
];
264 * Need this for bootstrapping a per node allocator.
266 #define NUM_INIT_LISTS (3 * MAX_NUMNODES)
267 static struct kmem_cache_node __initdata init_kmem_cache_node
[NUM_INIT_LISTS
];
268 #define CACHE_CACHE 0
269 #define SIZE_AC MAX_NUMNODES
270 #define SIZE_NODE (2 * MAX_NUMNODES)
272 static int drain_freelist(struct kmem_cache
*cache
,
273 struct kmem_cache_node
*n
, int tofree
);
274 static void free_block(struct kmem_cache
*cachep
, void **objpp
, int len
,
276 static int enable_cpucache(struct kmem_cache
*cachep
, gfp_t gfp
);
277 static void cache_reap(struct work_struct
*unused
);
279 static int slab_early_init
= 1;
281 #define INDEX_AC kmalloc_index(sizeof(struct arraycache_init))
282 #define INDEX_NODE kmalloc_index(sizeof(struct kmem_cache_node))
284 static void kmem_cache_node_init(struct kmem_cache_node
*parent
)
286 INIT_LIST_HEAD(&parent
->slabs_full
);
287 INIT_LIST_HEAD(&parent
->slabs_partial
);
288 INIT_LIST_HEAD(&parent
->slabs_free
);
289 parent
->shared
= NULL
;
290 parent
->alien
= NULL
;
291 parent
->colour_next
= 0;
292 spin_lock_init(&parent
->list_lock
);
293 parent
->free_objects
= 0;
294 parent
->free_touched
= 0;
297 #define MAKE_LIST(cachep, listp, slab, nodeid) \
299 INIT_LIST_HEAD(listp); \
300 list_splice(&(cachep->node[nodeid]->slab), listp); \
303 #define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
305 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
306 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
307 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
310 #define CFLGS_OFF_SLAB (0x80000000UL)
311 #define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
313 #define BATCHREFILL_LIMIT 16
315 * Optimization question: fewer reaps means less probability for unnessary
316 * cpucache drain/refill cycles.
318 * OTOH the cpuarrays can contain lots of objects,
319 * which could lock up otherwise freeable slabs.
321 #define REAPTIMEOUT_CPUC (2*HZ)
322 #define REAPTIMEOUT_LIST3 (4*HZ)
325 #define STATS_INC_ACTIVE(x) ((x)->num_active++)
326 #define STATS_DEC_ACTIVE(x) ((x)->num_active--)
327 #define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
328 #define STATS_INC_GROWN(x) ((x)->grown++)
329 #define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
330 #define STATS_SET_HIGH(x) \
332 if ((x)->num_active > (x)->high_mark) \
333 (x)->high_mark = (x)->num_active; \
335 #define STATS_INC_ERR(x) ((x)->errors++)
336 #define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
337 #define STATS_INC_NODEFREES(x) ((x)->node_frees++)
338 #define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
339 #define STATS_SET_FREEABLE(x, i) \
341 if ((x)->max_freeable < i) \
342 (x)->max_freeable = i; \
344 #define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
345 #define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
346 #define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
347 #define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
349 #define STATS_INC_ACTIVE(x) do { } while (0)
350 #define STATS_DEC_ACTIVE(x) do { } while (0)
351 #define STATS_INC_ALLOCED(x) do { } while (0)
352 #define STATS_INC_GROWN(x) do { } while (0)
353 #define STATS_ADD_REAPED(x,y) do { (void)(y); } while (0)
354 #define STATS_SET_HIGH(x) do { } while (0)
355 #define STATS_INC_ERR(x) do { } while (0)
356 #define STATS_INC_NODEALLOCS(x) do { } while (0)
357 #define STATS_INC_NODEFREES(x) do { } while (0)
358 #define STATS_INC_ACOVERFLOW(x) do { } while (0)
359 #define STATS_SET_FREEABLE(x, i) do { } while (0)
360 #define STATS_INC_ALLOCHIT(x) do { } while (0)
361 #define STATS_INC_ALLOCMISS(x) do { } while (0)
362 #define STATS_INC_FREEHIT(x) do { } while (0)
363 #define STATS_INC_FREEMISS(x) do { } while (0)
369 * memory layout of objects:
371 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
372 * the end of an object is aligned with the end of the real
373 * allocation. Catches writes behind the end of the allocation.
374 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
376 * cachep->obj_offset: The real object.
377 * cachep->size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
378 * cachep->size - 1* BYTES_PER_WORD: last caller address
379 * [BYTES_PER_WORD long]
381 static int obj_offset(struct kmem_cache
*cachep
)
383 return cachep
->obj_offset
;
386 static unsigned long long *dbg_redzone1(struct kmem_cache
*cachep
, void *objp
)
388 BUG_ON(!(cachep
->flags
& SLAB_RED_ZONE
));
389 return (unsigned long long*) (objp
+ obj_offset(cachep
) -
390 sizeof(unsigned long long));
393 static unsigned long long *dbg_redzone2(struct kmem_cache
*cachep
, void *objp
)
395 BUG_ON(!(cachep
->flags
& SLAB_RED_ZONE
));
396 if (cachep
->flags
& SLAB_STORE_USER
)
397 return (unsigned long long *)(objp
+ cachep
->size
-
398 sizeof(unsigned long long) -
400 return (unsigned long long *) (objp
+ cachep
->size
-
401 sizeof(unsigned long long));
404 static void **dbg_userword(struct kmem_cache
*cachep
, void *objp
)
406 BUG_ON(!(cachep
->flags
& SLAB_STORE_USER
));
407 return (void **)(objp
+ cachep
->size
- BYTES_PER_WORD
);
412 #define obj_offset(x) 0
413 #define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
414 #define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
415 #define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
420 * Do not go above this order unless 0 objects fit into the slab or
421 * overridden on the command line.
423 #define SLAB_MAX_ORDER_HI 1
424 #define SLAB_MAX_ORDER_LO 0
425 static int slab_max_order
= SLAB_MAX_ORDER_LO
;
426 static bool slab_max_order_set __initdata
;
428 static inline struct kmem_cache
*virt_to_cache(const void *obj
)
430 struct page
*page
= virt_to_head_page(obj
);
431 return page
->slab_cache
;
434 static inline struct slab
*virt_to_slab(const void *obj
)
436 struct page
*page
= virt_to_head_page(obj
);
438 VM_BUG_ON(!PageSlab(page
));
439 return page
->slab_page
;
442 static inline void *index_to_obj(struct kmem_cache
*cache
, struct slab
*slab
,
445 return slab
->s_mem
+ cache
->size
* idx
;
449 * We want to avoid an expensive divide : (offset / cache->size)
450 * Using the fact that size is a constant for a particular cache,
451 * we can replace (offset / cache->size) by
452 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
454 static inline unsigned int obj_to_index(const struct kmem_cache
*cache
,
455 const struct slab
*slab
, void *obj
)
457 u32 offset
= (obj
- slab
->s_mem
);
458 return reciprocal_divide(offset
, cache
->reciprocal_buffer_size
);
461 static struct arraycache_init initarray_generic
=
462 { {0, BOOT_CPUCACHE_ENTRIES
, 1, 0} };
464 /* internal cache of cache description objs */
465 static struct kmem_cache kmem_cache_boot
= {
467 .limit
= BOOT_CPUCACHE_ENTRIES
,
469 .size
= sizeof(struct kmem_cache
),
470 .name
= "kmem_cache",
473 #define BAD_ALIEN_MAGIC 0x01020304ul
475 #ifdef CONFIG_LOCKDEP
478 * Slab sometimes uses the kmalloc slabs to store the slab headers
479 * for other slabs "off slab".
480 * The locking for this is tricky in that it nests within the locks
481 * of all other slabs in a few places; to deal with this special
482 * locking we put on-slab caches into a separate lock-class.
484 * We set lock class for alien array caches which are up during init.
485 * The lock annotation will be lost if all cpus of a node goes down and
486 * then comes back up during hotplug
488 static struct lock_class_key on_slab_l3_key
;
489 static struct lock_class_key on_slab_alc_key
;
491 static struct lock_class_key debugobj_l3_key
;
492 static struct lock_class_key debugobj_alc_key
;
494 static void slab_set_lock_classes(struct kmem_cache
*cachep
,
495 struct lock_class_key
*l3_key
, struct lock_class_key
*alc_key
,
498 struct array_cache
**alc
;
499 struct kmem_cache_node
*n
;
506 lockdep_set_class(&n
->list_lock
, l3_key
);
509 * FIXME: This check for BAD_ALIEN_MAGIC
510 * should go away when common slab code is taught to
511 * work even without alien caches.
512 * Currently, non NUMA code returns BAD_ALIEN_MAGIC
513 * for alloc_alien_cache,
515 if (!alc
|| (unsigned long)alc
== BAD_ALIEN_MAGIC
)
519 lockdep_set_class(&alc
[r
]->lock
, alc_key
);
523 static void slab_set_debugobj_lock_classes_node(struct kmem_cache
*cachep
, int node
)
525 slab_set_lock_classes(cachep
, &debugobj_l3_key
, &debugobj_alc_key
, node
);
528 static void slab_set_debugobj_lock_classes(struct kmem_cache
*cachep
)
532 for_each_online_node(node
)
533 slab_set_debugobj_lock_classes_node(cachep
, node
);
536 static void init_node_lock_keys(int q
)
543 for (i
= 1; i
<= KMALLOC_SHIFT_HIGH
; i
++) {
544 struct kmem_cache_node
*n
;
545 struct kmem_cache
*cache
= kmalloc_caches
[i
];
551 if (!n
|| OFF_SLAB(cache
))
554 slab_set_lock_classes(cache
, &on_slab_l3_key
,
555 &on_slab_alc_key
, q
);
559 static void on_slab_lock_classes_node(struct kmem_cache
*cachep
, int q
)
561 if (!cachep
->node
[q
])
564 slab_set_lock_classes(cachep
, &on_slab_l3_key
,
565 &on_slab_alc_key
, q
);
568 static inline void on_slab_lock_classes(struct kmem_cache
*cachep
)
572 VM_BUG_ON(OFF_SLAB(cachep
));
574 on_slab_lock_classes_node(cachep
, node
);
577 static inline void init_lock_keys(void)
582 init_node_lock_keys(node
);
585 static void init_node_lock_keys(int q
)
589 static inline void init_lock_keys(void)
593 static inline void on_slab_lock_classes(struct kmem_cache
*cachep
)
597 static inline void on_slab_lock_classes_node(struct kmem_cache
*cachep
, int node
)
601 static void slab_set_debugobj_lock_classes_node(struct kmem_cache
*cachep
, int node
)
605 static void slab_set_debugobj_lock_classes(struct kmem_cache
*cachep
)
610 static DEFINE_PER_CPU(struct delayed_work
, slab_reap_work
);
612 static inline struct array_cache
*cpu_cache_get(struct kmem_cache
*cachep
)
614 return cachep
->array
[smp_processor_id()];
617 static size_t slab_mgmt_size(size_t nr_objs
, size_t align
)
619 return ALIGN(sizeof(struct slab
)+nr_objs
*sizeof(kmem_bufctl_t
), align
);
623 * Calculate the number of objects and left-over bytes for a given buffer size.
625 static void cache_estimate(unsigned long gfporder
, size_t buffer_size
,
626 size_t align
, int flags
, size_t *left_over
,
631 size_t slab_size
= PAGE_SIZE
<< gfporder
;
634 * The slab management structure can be either off the slab or
635 * on it. For the latter case, the memory allocated for a
639 * - One kmem_bufctl_t for each object
640 * - Padding to respect alignment of @align
641 * - @buffer_size bytes for each object
643 * If the slab management structure is off the slab, then the
644 * alignment will already be calculated into the size. Because
645 * the slabs are all pages aligned, the objects will be at the
646 * correct alignment when allocated.
648 if (flags
& CFLGS_OFF_SLAB
) {
650 nr_objs
= slab_size
/ buffer_size
;
652 if (nr_objs
> SLAB_LIMIT
)
653 nr_objs
= SLAB_LIMIT
;
656 * Ignore padding for the initial guess. The padding
657 * is at most @align-1 bytes, and @buffer_size is at
658 * least @align. In the worst case, this result will
659 * be one greater than the number of objects that fit
660 * into the memory allocation when taking the padding
663 nr_objs
= (slab_size
- sizeof(struct slab
)) /
664 (buffer_size
+ sizeof(kmem_bufctl_t
));
667 * This calculated number will be either the right
668 * amount, or one greater than what we want.
670 if (slab_mgmt_size(nr_objs
, align
) + nr_objs
*buffer_size
674 if (nr_objs
> SLAB_LIMIT
)
675 nr_objs
= SLAB_LIMIT
;
677 mgmt_size
= slab_mgmt_size(nr_objs
, align
);
680 *left_over
= slab_size
- nr_objs
*buffer_size
- mgmt_size
;
684 #define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
686 static void __slab_error(const char *function
, struct kmem_cache
*cachep
,
689 printk(KERN_ERR
"slab error in %s(): cache `%s': %s\n",
690 function
, cachep
->name
, msg
);
692 add_taint(TAINT_BAD_PAGE
, LOCKDEP_NOW_UNRELIABLE
);
697 * By default on NUMA we use alien caches to stage the freeing of
698 * objects allocated from other nodes. This causes massive memory
699 * inefficiencies when using fake NUMA setup to split memory into a
700 * large number of small nodes, so it can be disabled on the command
704 static int use_alien_caches __read_mostly
= 1;
705 static int __init
noaliencache_setup(char *s
)
707 use_alien_caches
= 0;
710 __setup("noaliencache", noaliencache_setup
);
712 static int __init
slab_max_order_setup(char *str
)
714 get_option(&str
, &slab_max_order
);
715 slab_max_order
= slab_max_order
< 0 ? 0 :
716 min(slab_max_order
, MAX_ORDER
- 1);
717 slab_max_order_set
= true;
721 __setup("slab_max_order=", slab_max_order_setup
);
725 * Special reaping functions for NUMA systems called from cache_reap().
726 * These take care of doing round robin flushing of alien caches (containing
727 * objects freed on different nodes from which they were allocated) and the
728 * flushing of remote pcps by calling drain_node_pages.
730 static DEFINE_PER_CPU(unsigned long, slab_reap_node
);
732 static void init_reap_node(int cpu
)
736 node
= next_node(cpu_to_mem(cpu
), node_online_map
);
737 if (node
== MAX_NUMNODES
)
738 node
= first_node(node_online_map
);
740 per_cpu(slab_reap_node
, cpu
) = node
;
743 static void next_reap_node(void)
745 int node
= __this_cpu_read(slab_reap_node
);
747 node
= next_node(node
, node_online_map
);
748 if (unlikely(node
>= MAX_NUMNODES
))
749 node
= first_node(node_online_map
);
750 __this_cpu_write(slab_reap_node
, node
);
754 #define init_reap_node(cpu) do { } while (0)
755 #define next_reap_node(void) do { } while (0)
759 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
760 * via the workqueue/eventd.
761 * Add the CPU number into the expiration time to minimize the possibility of
762 * the CPUs getting into lockstep and contending for the global cache chain
765 static void start_cpu_timer(int cpu
)
767 struct delayed_work
*reap_work
= &per_cpu(slab_reap_work
, cpu
);
770 * When this gets called from do_initcalls via cpucache_init(),
771 * init_workqueues() has already run, so keventd will be setup
774 if (keventd_up() && reap_work
->work
.func
== NULL
) {
776 INIT_DEFERRABLE_WORK(reap_work
, cache_reap
);
777 schedule_delayed_work_on(cpu
, reap_work
,
778 __round_jiffies_relative(HZ
, cpu
));
782 static struct array_cache
*alloc_arraycache(int node
, int entries
,
783 int batchcount
, gfp_t gfp
)
785 int memsize
= sizeof(void *) * entries
+ sizeof(struct array_cache
);
786 struct array_cache
*nc
= NULL
;
788 nc
= kmalloc_node(memsize
, gfp
, node
);
790 * The array_cache structures contain pointers to free object.
791 * However, when such objects are allocated or transferred to another
792 * cache the pointers are not cleared and they could be counted as
793 * valid references during a kmemleak scan. Therefore, kmemleak must
794 * not scan such objects.
796 kmemleak_no_scan(nc
);
800 nc
->batchcount
= batchcount
;
802 spin_lock_init(&nc
->lock
);
807 static inline bool is_slab_pfmemalloc(struct slab
*slabp
)
809 struct page
*page
= virt_to_page(slabp
->s_mem
);
811 return PageSlabPfmemalloc(page
);
814 /* Clears pfmemalloc_active if no slabs have pfmalloc set */
815 static void recheck_pfmemalloc_active(struct kmem_cache
*cachep
,
816 struct array_cache
*ac
)
818 struct kmem_cache_node
*n
= cachep
->node
[numa_mem_id()];
822 if (!pfmemalloc_active
)
825 spin_lock_irqsave(&n
->list_lock
, flags
);
826 list_for_each_entry(slabp
, &n
->slabs_full
, list
)
827 if (is_slab_pfmemalloc(slabp
))
830 list_for_each_entry(slabp
, &n
->slabs_partial
, list
)
831 if (is_slab_pfmemalloc(slabp
))
834 list_for_each_entry(slabp
, &n
->slabs_free
, list
)
835 if (is_slab_pfmemalloc(slabp
))
838 pfmemalloc_active
= false;
840 spin_unlock_irqrestore(&n
->list_lock
, flags
);
843 static void *__ac_get_obj(struct kmem_cache
*cachep
, struct array_cache
*ac
,
844 gfp_t flags
, bool force_refill
)
847 void *objp
= ac
->entry
[--ac
->avail
];
849 /* Ensure the caller is allowed to use objects from PFMEMALLOC slab */
850 if (unlikely(is_obj_pfmemalloc(objp
))) {
851 struct kmem_cache_node
*n
;
853 if (gfp_pfmemalloc_allowed(flags
)) {
854 clear_obj_pfmemalloc(&objp
);
858 /* The caller cannot use PFMEMALLOC objects, find another one */
859 for (i
= 0; i
< ac
->avail
; i
++) {
860 /* If a !PFMEMALLOC object is found, swap them */
861 if (!is_obj_pfmemalloc(ac
->entry
[i
])) {
863 ac
->entry
[i
] = ac
->entry
[ac
->avail
];
864 ac
->entry
[ac
->avail
] = objp
;
870 * If there are empty slabs on the slabs_free list and we are
871 * being forced to refill the cache, mark this one !pfmemalloc.
873 n
= cachep
->node
[numa_mem_id()];
874 if (!list_empty(&n
->slabs_free
) && force_refill
) {
875 struct slab
*slabp
= virt_to_slab(objp
);
876 ClearPageSlabPfmemalloc(virt_to_head_page(slabp
->s_mem
));
877 clear_obj_pfmemalloc(&objp
);
878 recheck_pfmemalloc_active(cachep
, ac
);
882 /* No !PFMEMALLOC objects available */
890 static inline void *ac_get_obj(struct kmem_cache
*cachep
,
891 struct array_cache
*ac
, gfp_t flags
, bool force_refill
)
895 if (unlikely(sk_memalloc_socks()))
896 objp
= __ac_get_obj(cachep
, ac
, flags
, force_refill
);
898 objp
= ac
->entry
[--ac
->avail
];
903 static void *__ac_put_obj(struct kmem_cache
*cachep
, struct array_cache
*ac
,
906 if (unlikely(pfmemalloc_active
)) {
907 /* Some pfmemalloc slabs exist, check if this is one */
908 struct slab
*slabp
= virt_to_slab(objp
);
909 struct page
*page
= virt_to_head_page(slabp
->s_mem
);
910 if (PageSlabPfmemalloc(page
))
911 set_obj_pfmemalloc(&objp
);
917 static inline void ac_put_obj(struct kmem_cache
*cachep
, struct array_cache
*ac
,
920 if (unlikely(sk_memalloc_socks()))
921 objp
= __ac_put_obj(cachep
, ac
, objp
);
923 ac
->entry
[ac
->avail
++] = objp
;
927 * Transfer objects in one arraycache to another.
928 * Locking must be handled by the caller.
930 * Return the number of entries transferred.
932 static int transfer_objects(struct array_cache
*to
,
933 struct array_cache
*from
, unsigned int max
)
935 /* Figure out how many entries to transfer */
936 int nr
= min3(from
->avail
, max
, to
->limit
- to
->avail
);
941 memcpy(to
->entry
+ to
->avail
, from
->entry
+ from
->avail
-nr
,
951 #define drain_alien_cache(cachep, alien) do { } while (0)
952 #define reap_alien(cachep, n) do { } while (0)
954 static inline struct array_cache
**alloc_alien_cache(int node
, int limit
, gfp_t gfp
)
956 return (struct array_cache
**)BAD_ALIEN_MAGIC
;
959 static inline void free_alien_cache(struct array_cache
**ac_ptr
)
963 static inline int cache_free_alien(struct kmem_cache
*cachep
, void *objp
)
968 static inline void *alternate_node_alloc(struct kmem_cache
*cachep
,
974 static inline void *____cache_alloc_node(struct kmem_cache
*cachep
,
975 gfp_t flags
, int nodeid
)
980 #else /* CONFIG_NUMA */
982 static void *____cache_alloc_node(struct kmem_cache
*, gfp_t
, int);
983 static void *alternate_node_alloc(struct kmem_cache
*, gfp_t
);
985 static struct array_cache
**alloc_alien_cache(int node
, int limit
, gfp_t gfp
)
987 struct array_cache
**ac_ptr
;
988 int memsize
= sizeof(void *) * nr_node_ids
;
993 ac_ptr
= kzalloc_node(memsize
, gfp
, node
);
996 if (i
== node
|| !node_online(i
))
998 ac_ptr
[i
] = alloc_arraycache(node
, limit
, 0xbaadf00d, gfp
);
1000 for (i
--; i
>= 0; i
--)
1010 static void free_alien_cache(struct array_cache
**ac_ptr
)
1021 static void __drain_alien_cache(struct kmem_cache
*cachep
,
1022 struct array_cache
*ac
, int node
)
1024 struct kmem_cache_node
*n
= cachep
->node
[node
];
1027 spin_lock(&n
->list_lock
);
1029 * Stuff objects into the remote nodes shared array first.
1030 * That way we could avoid the overhead of putting the objects
1031 * into the free lists and getting them back later.
1034 transfer_objects(n
->shared
, ac
, ac
->limit
);
1036 free_block(cachep
, ac
->entry
, ac
->avail
, node
);
1038 spin_unlock(&n
->list_lock
);
1043 * Called from cache_reap() to regularly drain alien caches round robin.
1045 static void reap_alien(struct kmem_cache
*cachep
, struct kmem_cache_node
*n
)
1047 int node
= __this_cpu_read(slab_reap_node
);
1050 struct array_cache
*ac
= n
->alien
[node
];
1052 if (ac
&& ac
->avail
&& spin_trylock_irq(&ac
->lock
)) {
1053 __drain_alien_cache(cachep
, ac
, node
);
1054 spin_unlock_irq(&ac
->lock
);
1059 static void drain_alien_cache(struct kmem_cache
*cachep
,
1060 struct array_cache
**alien
)
1063 struct array_cache
*ac
;
1064 unsigned long flags
;
1066 for_each_online_node(i
) {
1069 spin_lock_irqsave(&ac
->lock
, flags
);
1070 __drain_alien_cache(cachep
, ac
, i
);
1071 spin_unlock_irqrestore(&ac
->lock
, flags
);
1076 static inline int cache_free_alien(struct kmem_cache
*cachep
, void *objp
)
1078 int nodeid
= page_to_nid(virt_to_page(objp
));
1079 struct kmem_cache_node
*n
;
1080 struct array_cache
*alien
= NULL
;
1083 node
= numa_mem_id();
1086 * Make sure we are not freeing a object from another node to the array
1087 * cache on this cpu.
1089 if (likely(nodeid
== node
))
1092 n
= cachep
->node
[node
];
1093 STATS_INC_NODEFREES(cachep
);
1094 if (n
->alien
&& n
->alien
[nodeid
]) {
1095 alien
= n
->alien
[nodeid
];
1096 spin_lock(&alien
->lock
);
1097 if (unlikely(alien
->avail
== alien
->limit
)) {
1098 STATS_INC_ACOVERFLOW(cachep
);
1099 __drain_alien_cache(cachep
, alien
, nodeid
);
1101 ac_put_obj(cachep
, alien
, objp
);
1102 spin_unlock(&alien
->lock
);
1104 spin_lock(&(cachep
->node
[nodeid
])->list_lock
);
1105 free_block(cachep
, &objp
, 1, nodeid
);
1106 spin_unlock(&(cachep
->node
[nodeid
])->list_lock
);
1113 * Allocates and initializes node for a node on each slab cache, used for
1114 * either memory or cpu hotplug. If memory is being hot-added, the kmem_cache_node
1115 * will be allocated off-node since memory is not yet online for the new node.
1116 * When hotplugging memory or a cpu, existing node are not replaced if
1119 * Must hold slab_mutex.
1121 static int init_cache_node_node(int node
)
1123 struct kmem_cache
*cachep
;
1124 struct kmem_cache_node
*n
;
1125 const int memsize
= sizeof(struct kmem_cache_node
);
1127 list_for_each_entry(cachep
, &slab_caches
, list
) {
1129 * Set up the size64 kmemlist for cpu before we can
1130 * begin anything. Make sure some other cpu on this
1131 * node has not already allocated this
1133 if (!cachep
->node
[node
]) {
1134 n
= kmalloc_node(memsize
, GFP_KERNEL
, node
);
1137 kmem_cache_node_init(n
);
1138 n
->next_reap
= jiffies
+ REAPTIMEOUT_LIST3
+
1139 ((unsigned long)cachep
) % REAPTIMEOUT_LIST3
;
1142 * The l3s don't come and go as CPUs come and
1143 * go. slab_mutex is sufficient
1146 cachep
->node
[node
] = n
;
1149 spin_lock_irq(&cachep
->node
[node
]->list_lock
);
1150 cachep
->node
[node
]->free_limit
=
1151 (1 + nr_cpus_node(node
)) *
1152 cachep
->batchcount
+ cachep
->num
;
1153 spin_unlock_irq(&cachep
->node
[node
]->list_lock
);
1158 static inline int slabs_tofree(struct kmem_cache
*cachep
,
1159 struct kmem_cache_node
*n
)
1161 return (n
->free_objects
+ cachep
->num
- 1) / cachep
->num
;
1164 static void cpuup_canceled(long cpu
)
1166 struct kmem_cache
*cachep
;
1167 struct kmem_cache_node
*n
= NULL
;
1168 int node
= cpu_to_mem(cpu
);
1169 const struct cpumask
*mask
= cpumask_of_node(node
);
1171 list_for_each_entry(cachep
, &slab_caches
, list
) {
1172 struct array_cache
*nc
;
1173 struct array_cache
*shared
;
1174 struct array_cache
**alien
;
1176 /* cpu is dead; no one can alloc from it. */
1177 nc
= cachep
->array
[cpu
];
1178 cachep
->array
[cpu
] = NULL
;
1179 n
= cachep
->node
[node
];
1182 goto free_array_cache
;
1184 spin_lock_irq(&n
->list_lock
);
1186 /* Free limit for this kmem_cache_node */
1187 n
->free_limit
-= cachep
->batchcount
;
1189 free_block(cachep
, nc
->entry
, nc
->avail
, node
);
1191 if (!cpumask_empty(mask
)) {
1192 spin_unlock_irq(&n
->list_lock
);
1193 goto free_array_cache
;
1198 free_block(cachep
, shared
->entry
,
1199 shared
->avail
, node
);
1206 spin_unlock_irq(&n
->list_lock
);
1210 drain_alien_cache(cachep
, alien
);
1211 free_alien_cache(alien
);
1217 * In the previous loop, all the objects were freed to
1218 * the respective cache's slabs, now we can go ahead and
1219 * shrink each nodelist to its limit.
1221 list_for_each_entry(cachep
, &slab_caches
, list
) {
1222 n
= cachep
->node
[node
];
1225 drain_freelist(cachep
, n
, slabs_tofree(cachep
, n
));
1229 static int cpuup_prepare(long cpu
)
1231 struct kmem_cache
*cachep
;
1232 struct kmem_cache_node
*n
= NULL
;
1233 int node
= cpu_to_mem(cpu
);
1237 * We need to do this right in the beginning since
1238 * alloc_arraycache's are going to use this list.
1239 * kmalloc_node allows us to add the slab to the right
1240 * kmem_cache_node and not this cpu's kmem_cache_node
1242 err
= init_cache_node_node(node
);
1247 * Now we can go ahead with allocating the shared arrays and
1250 list_for_each_entry(cachep
, &slab_caches
, list
) {
1251 struct array_cache
*nc
;
1252 struct array_cache
*shared
= NULL
;
1253 struct array_cache
**alien
= NULL
;
1255 nc
= alloc_arraycache(node
, cachep
->limit
,
1256 cachep
->batchcount
, GFP_KERNEL
);
1259 if (cachep
->shared
) {
1260 shared
= alloc_arraycache(node
,
1261 cachep
->shared
* cachep
->batchcount
,
1262 0xbaadf00d, GFP_KERNEL
);
1268 if (use_alien_caches
) {
1269 alien
= alloc_alien_cache(node
, cachep
->limit
, GFP_KERNEL
);
1276 cachep
->array
[cpu
] = nc
;
1277 n
= cachep
->node
[node
];
1280 spin_lock_irq(&n
->list_lock
);
1283 * We are serialised from CPU_DEAD or
1284 * CPU_UP_CANCELLED by the cpucontrol lock
1295 spin_unlock_irq(&n
->list_lock
);
1297 free_alien_cache(alien
);
1298 if (cachep
->flags
& SLAB_DEBUG_OBJECTS
)
1299 slab_set_debugobj_lock_classes_node(cachep
, node
);
1300 else if (!OFF_SLAB(cachep
) &&
1301 !(cachep
->flags
& SLAB_DESTROY_BY_RCU
))
1302 on_slab_lock_classes_node(cachep
, node
);
1304 init_node_lock_keys(node
);
1308 cpuup_canceled(cpu
);
1312 static int cpuup_callback(struct notifier_block
*nfb
,
1313 unsigned long action
, void *hcpu
)
1315 long cpu
= (long)hcpu
;
1319 case CPU_UP_PREPARE
:
1320 case CPU_UP_PREPARE_FROZEN
:
1321 mutex_lock(&slab_mutex
);
1322 err
= cpuup_prepare(cpu
);
1323 mutex_unlock(&slab_mutex
);
1326 case CPU_ONLINE_FROZEN
:
1327 start_cpu_timer(cpu
);
1329 #ifdef CONFIG_HOTPLUG_CPU
1330 case CPU_DOWN_PREPARE
:
1331 case CPU_DOWN_PREPARE_FROZEN
:
1333 * Shutdown cache reaper. Note that the slab_mutex is
1334 * held so that if cache_reap() is invoked it cannot do
1335 * anything expensive but will only modify reap_work
1336 * and reschedule the timer.
1338 cancel_delayed_work_sync(&per_cpu(slab_reap_work
, cpu
));
1339 /* Now the cache_reaper is guaranteed to be not running. */
1340 per_cpu(slab_reap_work
, cpu
).work
.func
= NULL
;
1342 case CPU_DOWN_FAILED
:
1343 case CPU_DOWN_FAILED_FROZEN
:
1344 start_cpu_timer(cpu
);
1347 case CPU_DEAD_FROZEN
:
1349 * Even if all the cpus of a node are down, we don't free the
1350 * kmem_cache_node of any cache. This to avoid a race between
1351 * cpu_down, and a kmalloc allocation from another cpu for
1352 * memory from the node of the cpu going down. The node
1353 * structure is usually allocated from kmem_cache_create() and
1354 * gets destroyed at kmem_cache_destroy().
1358 case CPU_UP_CANCELED
:
1359 case CPU_UP_CANCELED_FROZEN
:
1360 mutex_lock(&slab_mutex
);
1361 cpuup_canceled(cpu
);
1362 mutex_unlock(&slab_mutex
);
1365 return notifier_from_errno(err
);
1368 static struct notifier_block cpucache_notifier
= {
1369 &cpuup_callback
, NULL
, 0
1372 #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
1374 * Drains freelist for a node on each slab cache, used for memory hot-remove.
1375 * Returns -EBUSY if all objects cannot be drained so that the node is not
1378 * Must hold slab_mutex.
1380 static int __meminit
drain_cache_node_node(int node
)
1382 struct kmem_cache
*cachep
;
1385 list_for_each_entry(cachep
, &slab_caches
, list
) {
1386 struct kmem_cache_node
*n
;
1388 n
= cachep
->node
[node
];
1392 drain_freelist(cachep
, n
, slabs_tofree(cachep
, n
));
1394 if (!list_empty(&n
->slabs_full
) ||
1395 !list_empty(&n
->slabs_partial
)) {
1403 static int __meminit
slab_memory_callback(struct notifier_block
*self
,
1404 unsigned long action
, void *arg
)
1406 struct memory_notify
*mnb
= arg
;
1410 nid
= mnb
->status_change_nid
;
1415 case MEM_GOING_ONLINE
:
1416 mutex_lock(&slab_mutex
);
1417 ret
= init_cache_node_node(nid
);
1418 mutex_unlock(&slab_mutex
);
1420 case MEM_GOING_OFFLINE
:
1421 mutex_lock(&slab_mutex
);
1422 ret
= drain_cache_node_node(nid
);
1423 mutex_unlock(&slab_mutex
);
1427 case MEM_CANCEL_ONLINE
:
1428 case MEM_CANCEL_OFFLINE
:
1432 return notifier_from_errno(ret
);
1434 #endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
1437 * swap the static kmem_cache_node with kmalloced memory
1439 static void __init
init_list(struct kmem_cache
*cachep
, struct kmem_cache_node
*list
,
1442 struct kmem_cache_node
*ptr
;
1444 ptr
= kmalloc_node(sizeof(struct kmem_cache_node
), GFP_NOWAIT
, nodeid
);
1447 memcpy(ptr
, list
, sizeof(struct kmem_cache_node
));
1449 * Do not assume that spinlocks can be initialized via memcpy:
1451 spin_lock_init(&ptr
->list_lock
);
1453 MAKE_ALL_LISTS(cachep
, ptr
, nodeid
);
1454 cachep
->node
[nodeid
] = ptr
;
1458 * For setting up all the kmem_cache_node for cache whose buffer_size is same as
1459 * size of kmem_cache_node.
1461 static void __init
set_up_node(struct kmem_cache
*cachep
, int index
)
1465 for_each_online_node(node
) {
1466 cachep
->node
[node
] = &init_kmem_cache_node
[index
+ node
];
1467 cachep
->node
[node
]->next_reap
= jiffies
+
1469 ((unsigned long)cachep
) % REAPTIMEOUT_LIST3
;
1474 * The memory after the last cpu cache pointer is used for the
1477 static void setup_node_pointer(struct kmem_cache
*cachep
)
1479 cachep
->node
= (struct kmem_cache_node
**)&cachep
->array
[nr_cpu_ids
];
1483 * Initialisation. Called after the page allocator have been initialised and
1484 * before smp_init().
1486 void __init
kmem_cache_init(void)
1490 BUILD_BUG_ON(sizeof(((struct page
*)NULL
)->lru
) <
1491 sizeof(struct rcu_head
));
1492 kmem_cache
= &kmem_cache_boot
;
1493 setup_node_pointer(kmem_cache
);
1495 if (num_possible_nodes() == 1)
1496 use_alien_caches
= 0;
1498 for (i
= 0; i
< NUM_INIT_LISTS
; i
++)
1499 kmem_cache_node_init(&init_kmem_cache_node
[i
]);
1501 set_up_node(kmem_cache
, CACHE_CACHE
);
1504 * Fragmentation resistance on low memory - only use bigger
1505 * page orders on machines with more than 32MB of memory if
1506 * not overridden on the command line.
1508 if (!slab_max_order_set
&& totalram_pages
> (32 << 20) >> PAGE_SHIFT
)
1509 slab_max_order
= SLAB_MAX_ORDER_HI
;
1511 /* Bootstrap is tricky, because several objects are allocated
1512 * from caches that do not exist yet:
1513 * 1) initialize the kmem_cache cache: it contains the struct
1514 * kmem_cache structures of all caches, except kmem_cache itself:
1515 * kmem_cache is statically allocated.
1516 * Initially an __init data area is used for the head array and the
1517 * kmem_cache_node structures, it's replaced with a kmalloc allocated
1518 * array at the end of the bootstrap.
1519 * 2) Create the first kmalloc cache.
1520 * The struct kmem_cache for the new cache is allocated normally.
1521 * An __init data area is used for the head array.
1522 * 3) Create the remaining kmalloc caches, with minimally sized
1524 * 4) Replace the __init data head arrays for kmem_cache and the first
1525 * kmalloc cache with kmalloc allocated arrays.
1526 * 5) Replace the __init data for kmem_cache_node for kmem_cache and
1527 * the other cache's with kmalloc allocated memory.
1528 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
1531 /* 1) create the kmem_cache */
1534 * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
1536 create_boot_cache(kmem_cache
, "kmem_cache",
1537 offsetof(struct kmem_cache
, array
[nr_cpu_ids
]) +
1538 nr_node_ids
* sizeof(struct kmem_cache_node
*),
1539 SLAB_HWCACHE_ALIGN
);
1540 list_add(&kmem_cache
->list
, &slab_caches
);
1542 /* 2+3) create the kmalloc caches */
1545 * Initialize the caches that provide memory for the array cache and the
1546 * kmem_cache_node structures first. Without this, further allocations will
1550 kmalloc_caches
[INDEX_AC
] = create_kmalloc_cache("kmalloc-ac",
1551 kmalloc_size(INDEX_AC
), ARCH_KMALLOC_FLAGS
);
1553 if (INDEX_AC
!= INDEX_NODE
)
1554 kmalloc_caches
[INDEX_NODE
] =
1555 create_kmalloc_cache("kmalloc-node",
1556 kmalloc_size(INDEX_NODE
), ARCH_KMALLOC_FLAGS
);
1558 slab_early_init
= 0;
1560 /* 4) Replace the bootstrap head arrays */
1562 struct array_cache
*ptr
;
1564 ptr
= kmalloc(sizeof(struct arraycache_init
), GFP_NOWAIT
);
1566 memcpy(ptr
, cpu_cache_get(kmem_cache
),
1567 sizeof(struct arraycache_init
));
1569 * Do not assume that spinlocks can be initialized via memcpy:
1571 spin_lock_init(&ptr
->lock
);
1573 kmem_cache
->array
[smp_processor_id()] = ptr
;
1575 ptr
= kmalloc(sizeof(struct arraycache_init
), GFP_NOWAIT
);
1577 BUG_ON(cpu_cache_get(kmalloc_caches
[INDEX_AC
])
1578 != &initarray_generic
.cache
);
1579 memcpy(ptr
, cpu_cache_get(kmalloc_caches
[INDEX_AC
]),
1580 sizeof(struct arraycache_init
));
1582 * Do not assume that spinlocks can be initialized via memcpy:
1584 spin_lock_init(&ptr
->lock
);
1586 kmalloc_caches
[INDEX_AC
]->array
[smp_processor_id()] = ptr
;
1588 /* 5) Replace the bootstrap kmem_cache_node */
1592 for_each_online_node(nid
) {
1593 init_list(kmem_cache
, &init_kmem_cache_node
[CACHE_CACHE
+ nid
], nid
);
1595 init_list(kmalloc_caches
[INDEX_AC
],
1596 &init_kmem_cache_node
[SIZE_AC
+ nid
], nid
);
1598 if (INDEX_AC
!= INDEX_NODE
) {
1599 init_list(kmalloc_caches
[INDEX_NODE
],
1600 &init_kmem_cache_node
[SIZE_NODE
+ nid
], nid
);
1605 create_kmalloc_caches(ARCH_KMALLOC_FLAGS
);
1608 void __init
kmem_cache_init_late(void)
1610 struct kmem_cache
*cachep
;
1614 /* 6) resize the head arrays to their final sizes */
1615 mutex_lock(&slab_mutex
);
1616 list_for_each_entry(cachep
, &slab_caches
, list
)
1617 if (enable_cpucache(cachep
, GFP_NOWAIT
))
1619 mutex_unlock(&slab_mutex
);
1621 /* Annotate slab for lockdep -- annotate the malloc caches */
1628 * Register a cpu startup notifier callback that initializes
1629 * cpu_cache_get for all new cpus
1631 register_cpu_notifier(&cpucache_notifier
);
1635 * Register a memory hotplug callback that initializes and frees
1638 hotplug_memory_notifier(slab_memory_callback
, SLAB_CALLBACK_PRI
);
1642 * The reap timers are started later, with a module init call: That part
1643 * of the kernel is not yet operational.
1647 static int __init
cpucache_init(void)
1652 * Register the timers that return unneeded pages to the page allocator
1654 for_each_online_cpu(cpu
)
1655 start_cpu_timer(cpu
);
1661 __initcall(cpucache_init
);
1663 static noinline
void
1664 slab_out_of_memory(struct kmem_cache
*cachep
, gfp_t gfpflags
, int nodeid
)
1666 struct kmem_cache_node
*n
;
1668 unsigned long flags
;
1672 "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n",
1674 printk(KERN_WARNING
" cache: %s, object size: %d, order: %d\n",
1675 cachep
->name
, cachep
->size
, cachep
->gfporder
);
1677 for_each_online_node(node
) {
1678 unsigned long active_objs
= 0, num_objs
= 0, free_objects
= 0;
1679 unsigned long active_slabs
= 0, num_slabs
= 0;
1681 n
= cachep
->node
[node
];
1685 spin_lock_irqsave(&n
->list_lock
, flags
);
1686 list_for_each_entry(slabp
, &n
->slabs_full
, list
) {
1687 active_objs
+= cachep
->num
;
1690 list_for_each_entry(slabp
, &n
->slabs_partial
, list
) {
1691 active_objs
+= slabp
->inuse
;
1694 list_for_each_entry(slabp
, &n
->slabs_free
, list
)
1697 free_objects
+= n
->free_objects
;
1698 spin_unlock_irqrestore(&n
->list_lock
, flags
);
1700 num_slabs
+= active_slabs
;
1701 num_objs
= num_slabs
* cachep
->num
;
1703 " node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
1704 node
, active_slabs
, num_slabs
, active_objs
, num_objs
,
1710 * Interface to system's page allocator. No need to hold the cache-lock.
1712 * If we requested dmaable memory, we will get it. Even if we
1713 * did not request dmaable memory, we might get it, but that
1714 * would be relatively rare and ignorable.
1716 static struct page
*kmem_getpages(struct kmem_cache
*cachep
, gfp_t flags
,
1725 * Nommu uses slab's for process anonymous memory allocations, and thus
1726 * requires __GFP_COMP to properly refcount higher order allocations
1728 flags
|= __GFP_COMP
;
1731 flags
|= cachep
->allocflags
;
1732 if (cachep
->flags
& SLAB_RECLAIM_ACCOUNT
)
1733 flags
|= __GFP_RECLAIMABLE
;
1735 page
= alloc_pages_exact_node(nodeid
, flags
| __GFP_NOTRACK
, cachep
->gfporder
);
1737 if (!(flags
& __GFP_NOWARN
) && printk_ratelimit())
1738 slab_out_of_memory(cachep
, flags
, nodeid
);
1742 /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
1743 if (unlikely(page
->pfmemalloc
))
1744 pfmemalloc_active
= true;
1746 nr_pages
= (1 << cachep
->gfporder
);
1747 if (cachep
->flags
& SLAB_RECLAIM_ACCOUNT
)
1748 add_zone_page_state(page_zone(page
),
1749 NR_SLAB_RECLAIMABLE
, nr_pages
);
1751 add_zone_page_state(page_zone(page
),
1752 NR_SLAB_UNRECLAIMABLE
, nr_pages
);
1753 for (i
= 0; i
< nr_pages
; i
++) {
1754 __SetPageSlab(page
+ i
);
1756 if (page
->pfmemalloc
)
1757 SetPageSlabPfmemalloc(page
);
1759 memcg_bind_pages(cachep
, cachep
->gfporder
);
1761 if (kmemcheck_enabled
&& !(cachep
->flags
& SLAB_NOTRACK
)) {
1762 kmemcheck_alloc_shadow(page
, cachep
->gfporder
, flags
, nodeid
);
1765 kmemcheck_mark_uninitialized_pages(page
, nr_pages
);
1767 kmemcheck_mark_unallocated_pages(page
, nr_pages
);
1774 * Interface to system's page release.
1776 static void kmem_freepages(struct kmem_cache
*cachep
, struct page
*page
)
1778 unsigned long i
= (1 << cachep
->gfporder
);
1779 const unsigned long nr_freed
= i
;
1781 kmemcheck_free_shadow(page
, cachep
->gfporder
);
1783 if (cachep
->flags
& SLAB_RECLAIM_ACCOUNT
)
1784 sub_zone_page_state(page_zone(page
),
1785 NR_SLAB_RECLAIMABLE
, nr_freed
);
1787 sub_zone_page_state(page_zone(page
),
1788 NR_SLAB_UNRECLAIMABLE
, nr_freed
);
1790 __ClearPageSlabPfmemalloc(page
);
1792 BUG_ON(!PageSlab(page
));
1793 __ClearPageSlab(page
);
1797 memcg_release_pages(cachep
, cachep
->gfporder
);
1798 if (current
->reclaim_state
)
1799 current
->reclaim_state
->reclaimed_slab
+= nr_freed
;
1800 __free_memcg_kmem_pages(page
, cachep
->gfporder
);
1803 static void kmem_rcu_free(struct rcu_head
*head
)
1805 struct kmem_cache
*cachep
;
1808 page
= container_of(head
, struct page
, rcu_head
);
1809 cachep
= page
->slab_cache
;
1811 kmem_freepages(cachep
, page
);
1816 #ifdef CONFIG_DEBUG_PAGEALLOC
1817 static void store_stackinfo(struct kmem_cache
*cachep
, unsigned long *addr
,
1818 unsigned long caller
)
1820 int size
= cachep
->object_size
;
1822 addr
= (unsigned long *)&((char *)addr
)[obj_offset(cachep
)];
1824 if (size
< 5 * sizeof(unsigned long))
1827 *addr
++ = 0x12345678;
1829 *addr
++ = smp_processor_id();
1830 size
-= 3 * sizeof(unsigned long);
1832 unsigned long *sptr
= &caller
;
1833 unsigned long svalue
;
1835 while (!kstack_end(sptr
)) {
1837 if (kernel_text_address(svalue
)) {
1839 size
-= sizeof(unsigned long);
1840 if (size
<= sizeof(unsigned long))
1846 *addr
++ = 0x87654321;
1850 static void poison_obj(struct kmem_cache
*cachep
, void *addr
, unsigned char val
)
1852 int size
= cachep
->object_size
;
1853 addr
= &((char *)addr
)[obj_offset(cachep
)];
1855 memset(addr
, val
, size
);
1856 *(unsigned char *)(addr
+ size
- 1) = POISON_END
;
1859 static void dump_line(char *data
, int offset
, int limit
)
1862 unsigned char error
= 0;
1865 printk(KERN_ERR
"%03x: ", offset
);
1866 for (i
= 0; i
< limit
; i
++) {
1867 if (data
[offset
+ i
] != POISON_FREE
) {
1868 error
= data
[offset
+ i
];
1872 print_hex_dump(KERN_CONT
, "", 0, 16, 1,
1873 &data
[offset
], limit
, 1);
1875 if (bad_count
== 1) {
1876 error
^= POISON_FREE
;
1877 if (!(error
& (error
- 1))) {
1878 printk(KERN_ERR
"Single bit error detected. Probably "
1881 printk(KERN_ERR
"Run memtest86+ or a similar memory "
1884 printk(KERN_ERR
"Run a memory test tool.\n");
1893 static void print_objinfo(struct kmem_cache
*cachep
, void *objp
, int lines
)
1898 if (cachep
->flags
& SLAB_RED_ZONE
) {
1899 printk(KERN_ERR
"Redzone: 0x%llx/0x%llx.\n",
1900 *dbg_redzone1(cachep
, objp
),
1901 *dbg_redzone2(cachep
, objp
));
1904 if (cachep
->flags
& SLAB_STORE_USER
) {
1905 printk(KERN_ERR
"Last user: [<%p>](%pSR)\n",
1906 *dbg_userword(cachep
, objp
),
1907 *dbg_userword(cachep
, objp
));
1909 realobj
= (char *)objp
+ obj_offset(cachep
);
1910 size
= cachep
->object_size
;
1911 for (i
= 0; i
< size
&& lines
; i
+= 16, lines
--) {
1914 if (i
+ limit
> size
)
1916 dump_line(realobj
, i
, limit
);
1920 static void check_poison_obj(struct kmem_cache
*cachep
, void *objp
)
1926 realobj
= (char *)objp
+ obj_offset(cachep
);
1927 size
= cachep
->object_size
;
1929 for (i
= 0; i
< size
; i
++) {
1930 char exp
= POISON_FREE
;
1933 if (realobj
[i
] != exp
) {
1939 "Slab corruption (%s): %s start=%p, len=%d\n",
1940 print_tainted(), cachep
->name
, realobj
, size
);
1941 print_objinfo(cachep
, objp
, 0);
1943 /* Hexdump the affected line */
1946 if (i
+ limit
> size
)
1948 dump_line(realobj
, i
, limit
);
1951 /* Limit to 5 lines */
1957 /* Print some data about the neighboring objects, if they
1960 struct slab
*slabp
= virt_to_slab(objp
);
1963 objnr
= obj_to_index(cachep
, slabp
, objp
);
1965 objp
= index_to_obj(cachep
, slabp
, objnr
- 1);
1966 realobj
= (char *)objp
+ obj_offset(cachep
);
1967 printk(KERN_ERR
"Prev obj: start=%p, len=%d\n",
1969 print_objinfo(cachep
, objp
, 2);
1971 if (objnr
+ 1 < cachep
->num
) {
1972 objp
= index_to_obj(cachep
, slabp
, objnr
+ 1);
1973 realobj
= (char *)objp
+ obj_offset(cachep
);
1974 printk(KERN_ERR
"Next obj: start=%p, len=%d\n",
1976 print_objinfo(cachep
, objp
, 2);
1983 static void slab_destroy_debugcheck(struct kmem_cache
*cachep
, struct slab
*slabp
)
1986 for (i
= 0; i
< cachep
->num
; i
++) {
1987 void *objp
= index_to_obj(cachep
, slabp
, i
);
1989 if (cachep
->flags
& SLAB_POISON
) {
1990 #ifdef CONFIG_DEBUG_PAGEALLOC
1991 if (cachep
->size
% PAGE_SIZE
== 0 &&
1993 kernel_map_pages(virt_to_page(objp
),
1994 cachep
->size
/ PAGE_SIZE
, 1);
1996 check_poison_obj(cachep
, objp
);
1998 check_poison_obj(cachep
, objp
);
2001 if (cachep
->flags
& SLAB_RED_ZONE
) {
2002 if (*dbg_redzone1(cachep
, objp
) != RED_INACTIVE
)
2003 slab_error(cachep
, "start of a freed object "
2005 if (*dbg_redzone2(cachep
, objp
) != RED_INACTIVE
)
2006 slab_error(cachep
, "end of a freed object "
2012 static void slab_destroy_debugcheck(struct kmem_cache
*cachep
, struct slab
*slabp
)
2018 * slab_destroy - destroy and release all objects in a slab
2019 * @cachep: cache pointer being destroyed
2020 * @slabp: slab pointer being destroyed
2022 * Destroy all the objs in a slab, and release the mem back to the system.
2023 * Before calling the slab must have been unlinked from the cache. The
2024 * cache-lock is not held/needed.
2026 static void slab_destroy(struct kmem_cache
*cachep
, struct slab
*slabp
)
2028 struct page
*page
= virt_to_head_page(slabp
->s_mem
);
2030 slab_destroy_debugcheck(cachep
, slabp
);
2031 if (unlikely(cachep
->flags
& SLAB_DESTROY_BY_RCU
)) {
2032 struct rcu_head
*head
;
2035 * RCU free overloads the RCU head over the LRU.
2036 * slab_page has been overloeaded over the LRU,
2037 * however it is not used from now on so that
2038 * we can use it safely.
2040 head
= (void *)&page
->rcu_head
;
2041 call_rcu(head
, kmem_rcu_free
);
2044 kmem_freepages(cachep
, page
);
2048 * From now on, we don't use slab management
2049 * although actual page can be freed in rcu context
2051 if (OFF_SLAB(cachep
))
2052 kmem_cache_free(cachep
->slabp_cache
, slabp
);
2056 * calculate_slab_order - calculate size (page order) of slabs
2057 * @cachep: pointer to the cache that is being created
2058 * @size: size of objects to be created in this cache.
2059 * @align: required alignment for the objects.
2060 * @flags: slab allocation flags
2062 * Also calculates the number of objects per slab.
2064 * This could be made much more intelligent. For now, try to avoid using
2065 * high order pages for slabs. When the gfp() functions are more friendly
2066 * towards high-order requests, this should be changed.
2068 static size_t calculate_slab_order(struct kmem_cache
*cachep
,
2069 size_t size
, size_t align
, unsigned long flags
)
2071 unsigned long offslab_limit
;
2072 size_t left_over
= 0;
2075 for (gfporder
= 0; gfporder
<= KMALLOC_MAX_ORDER
; gfporder
++) {
2079 cache_estimate(gfporder
, size
, align
, flags
, &remainder
, &num
);
2083 if (flags
& CFLGS_OFF_SLAB
) {
2085 * Max number of objs-per-slab for caches which
2086 * use off-slab slabs. Needed to avoid a possible
2087 * looping condition in cache_grow().
2089 offslab_limit
= size
- sizeof(struct slab
);
2090 offslab_limit
/= sizeof(kmem_bufctl_t
);
2092 if (num
> offslab_limit
)
2096 /* Found something acceptable - save it away */
2098 cachep
->gfporder
= gfporder
;
2099 left_over
= remainder
;
2102 * A VFS-reclaimable slab tends to have most allocations
2103 * as GFP_NOFS and we really don't want to have to be allocating
2104 * higher-order pages when we are unable to shrink dcache.
2106 if (flags
& SLAB_RECLAIM_ACCOUNT
)
2110 * Large number of objects is good, but very large slabs are
2111 * currently bad for the gfp()s.
2113 if (gfporder
>= slab_max_order
)
2117 * Acceptable internal fragmentation?
2119 if (left_over
* 8 <= (PAGE_SIZE
<< gfporder
))
2125 static int __init_refok
setup_cpu_cache(struct kmem_cache
*cachep
, gfp_t gfp
)
2127 if (slab_state
>= FULL
)
2128 return enable_cpucache(cachep
, gfp
);
2130 if (slab_state
== DOWN
) {
2132 * Note: Creation of first cache (kmem_cache).
2133 * The setup_node is taken care
2134 * of by the caller of __kmem_cache_create
2136 cachep
->array
[smp_processor_id()] = &initarray_generic
.cache
;
2137 slab_state
= PARTIAL
;
2138 } else if (slab_state
== PARTIAL
) {
2140 * Note: the second kmem_cache_create must create the cache
2141 * that's used by kmalloc(24), otherwise the creation of
2142 * further caches will BUG().
2144 cachep
->array
[smp_processor_id()] = &initarray_generic
.cache
;
2147 * If the cache that's used by kmalloc(sizeof(kmem_cache_node)) is
2148 * the second cache, then we need to set up all its node/,
2149 * otherwise the creation of further caches will BUG().
2151 set_up_node(cachep
, SIZE_AC
);
2152 if (INDEX_AC
== INDEX_NODE
)
2153 slab_state
= PARTIAL_NODE
;
2155 slab_state
= PARTIAL_ARRAYCACHE
;
2157 /* Remaining boot caches */
2158 cachep
->array
[smp_processor_id()] =
2159 kmalloc(sizeof(struct arraycache_init
), gfp
);
2161 if (slab_state
== PARTIAL_ARRAYCACHE
) {
2162 set_up_node(cachep
, SIZE_NODE
);
2163 slab_state
= PARTIAL_NODE
;
2166 for_each_online_node(node
) {
2167 cachep
->node
[node
] =
2168 kmalloc_node(sizeof(struct kmem_cache_node
),
2170 BUG_ON(!cachep
->node
[node
]);
2171 kmem_cache_node_init(cachep
->node
[node
]);
2175 cachep
->node
[numa_mem_id()]->next_reap
=
2176 jiffies
+ REAPTIMEOUT_LIST3
+
2177 ((unsigned long)cachep
) % REAPTIMEOUT_LIST3
;
2179 cpu_cache_get(cachep
)->avail
= 0;
2180 cpu_cache_get(cachep
)->limit
= BOOT_CPUCACHE_ENTRIES
;
2181 cpu_cache_get(cachep
)->batchcount
= 1;
2182 cpu_cache_get(cachep
)->touched
= 0;
2183 cachep
->batchcount
= 1;
2184 cachep
->limit
= BOOT_CPUCACHE_ENTRIES
;
2189 * __kmem_cache_create - Create a cache.
2190 * @cachep: cache management descriptor
2191 * @flags: SLAB flags
2193 * Returns a ptr to the cache on success, NULL on failure.
2194 * Cannot be called within a int, but can be interrupted.
2195 * The @ctor is run when new pages are allocated by the cache.
2199 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2200 * to catch references to uninitialised memory.
2202 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2203 * for buffer overruns.
2205 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2206 * cacheline. This can be beneficial if you're counting cycles as closely
2210 __kmem_cache_create (struct kmem_cache
*cachep
, unsigned long flags
)
2212 size_t left_over
, slab_size
, ralign
;
2215 size_t size
= cachep
->size
;
2220 * Enable redzoning and last user accounting, except for caches with
2221 * large objects, if the increased size would increase the object size
2222 * above the next power of two: caches with object sizes just above a
2223 * power of two have a significant amount of internal fragmentation.
2225 if (size
< 4096 || fls(size
- 1) == fls(size
-1 + REDZONE_ALIGN
+
2226 2 * sizeof(unsigned long long)))
2227 flags
|= SLAB_RED_ZONE
| SLAB_STORE_USER
;
2228 if (!(flags
& SLAB_DESTROY_BY_RCU
))
2229 flags
|= SLAB_POISON
;
2231 if (flags
& SLAB_DESTROY_BY_RCU
)
2232 BUG_ON(flags
& SLAB_POISON
);
2236 * Check that size is in terms of words. This is needed to avoid
2237 * unaligned accesses for some archs when redzoning is used, and makes
2238 * sure any on-slab bufctl's are also correctly aligned.
2240 if (size
& (BYTES_PER_WORD
- 1)) {
2241 size
+= (BYTES_PER_WORD
- 1);
2242 size
&= ~(BYTES_PER_WORD
- 1);
2246 * Redzoning and user store require word alignment or possibly larger.
2247 * Note this will be overridden by architecture or caller mandated
2248 * alignment if either is greater than BYTES_PER_WORD.
2250 if (flags
& SLAB_STORE_USER
)
2251 ralign
= BYTES_PER_WORD
;
2253 if (flags
& SLAB_RED_ZONE
) {
2254 ralign
= REDZONE_ALIGN
;
2255 /* If redzoning, ensure that the second redzone is suitably
2256 * aligned, by adjusting the object size accordingly. */
2257 size
+= REDZONE_ALIGN
- 1;
2258 size
&= ~(REDZONE_ALIGN
- 1);
2261 /* 3) caller mandated alignment */
2262 if (ralign
< cachep
->align
) {
2263 ralign
= cachep
->align
;
2265 /* disable debug if necessary */
2266 if (ralign
> __alignof__(unsigned long long))
2267 flags
&= ~(SLAB_RED_ZONE
| SLAB_STORE_USER
);
2271 cachep
->align
= ralign
;
2273 if (slab_is_available())
2278 setup_node_pointer(cachep
);
2282 * Both debugging options require word-alignment which is calculated
2285 if (flags
& SLAB_RED_ZONE
) {
2286 /* add space for red zone words */
2287 cachep
->obj_offset
+= sizeof(unsigned long long);
2288 size
+= 2 * sizeof(unsigned long long);
2290 if (flags
& SLAB_STORE_USER
) {
2291 /* user store requires one word storage behind the end of
2292 * the real object. But if the second red zone needs to be
2293 * aligned to 64 bits, we must allow that much space.
2295 if (flags
& SLAB_RED_ZONE
)
2296 size
+= REDZONE_ALIGN
;
2298 size
+= BYTES_PER_WORD
;
2300 #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
2301 if (size
>= kmalloc_size(INDEX_NODE
+ 1)
2302 && cachep
->object_size
> cache_line_size()
2303 && ALIGN(size
, cachep
->align
) < PAGE_SIZE
) {
2304 cachep
->obj_offset
+= PAGE_SIZE
- ALIGN(size
, cachep
->align
);
2311 * Determine if the slab management is 'on' or 'off' slab.
2312 * (bootstrapping cannot cope with offslab caches so don't do
2313 * it too early on. Always use on-slab management when
2314 * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
2316 if ((size
>= (PAGE_SIZE
>> 3)) && !slab_early_init
&&
2317 !(flags
& SLAB_NOLEAKTRACE
))
2319 * Size is large, assume best to place the slab management obj
2320 * off-slab (should allow better packing of objs).
2322 flags
|= CFLGS_OFF_SLAB
;
2324 size
= ALIGN(size
, cachep
->align
);
2326 left_over
= calculate_slab_order(cachep
, size
, cachep
->align
, flags
);
2331 slab_size
= ALIGN(cachep
->num
* sizeof(kmem_bufctl_t
)
2332 + sizeof(struct slab
), cachep
->align
);
2335 * If the slab has been placed off-slab, and we have enough space then
2336 * move it on-slab. This is at the expense of any extra colouring.
2338 if (flags
& CFLGS_OFF_SLAB
&& left_over
>= slab_size
) {
2339 flags
&= ~CFLGS_OFF_SLAB
;
2340 left_over
-= slab_size
;
2343 if (flags
& CFLGS_OFF_SLAB
) {
2344 /* really off slab. No need for manual alignment */
2346 cachep
->num
* sizeof(kmem_bufctl_t
) + sizeof(struct slab
);
2348 #ifdef CONFIG_PAGE_POISONING
2349 /* If we're going to use the generic kernel_map_pages()
2350 * poisoning, then it's going to smash the contents of
2351 * the redzone and userword anyhow, so switch them off.
2353 if (size
% PAGE_SIZE
== 0 && flags
& SLAB_POISON
)
2354 flags
&= ~(SLAB_RED_ZONE
| SLAB_STORE_USER
);
2358 cachep
->colour_off
= cache_line_size();
2359 /* Offset must be a multiple of the alignment. */
2360 if (cachep
->colour_off
< cachep
->align
)
2361 cachep
->colour_off
= cachep
->align
;
2362 cachep
->colour
= left_over
/ cachep
->colour_off
;
2363 cachep
->slab_size
= slab_size
;
2364 cachep
->flags
= flags
;
2365 cachep
->allocflags
= 0;
2366 if (CONFIG_ZONE_DMA_FLAG
&& (flags
& SLAB_CACHE_DMA
))
2367 cachep
->allocflags
|= GFP_DMA
;
2368 cachep
->size
= size
;
2369 cachep
->reciprocal_buffer_size
= reciprocal_value(size
);
2371 if (flags
& CFLGS_OFF_SLAB
) {
2372 cachep
->slabp_cache
= kmalloc_slab(slab_size
, 0u);
2374 * This is a possibility for one of the malloc_sizes caches.
2375 * But since we go off slab only for object size greater than
2376 * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
2377 * this should not happen at all.
2378 * But leave a BUG_ON for some lucky dude.
2380 BUG_ON(ZERO_OR_NULL_PTR(cachep
->slabp_cache
));
2383 err
= setup_cpu_cache(cachep
, gfp
);
2385 __kmem_cache_shutdown(cachep
);
2389 if (flags
& SLAB_DEBUG_OBJECTS
) {
2391 * Would deadlock through slab_destroy()->call_rcu()->
2392 * debug_object_activate()->kmem_cache_alloc().
2394 WARN_ON_ONCE(flags
& SLAB_DESTROY_BY_RCU
);
2396 slab_set_debugobj_lock_classes(cachep
);
2397 } else if (!OFF_SLAB(cachep
) && !(flags
& SLAB_DESTROY_BY_RCU
))
2398 on_slab_lock_classes(cachep
);
2404 static void check_irq_off(void)
2406 BUG_ON(!irqs_disabled());
2409 static void check_irq_on(void)
2411 BUG_ON(irqs_disabled());
2414 static void check_spinlock_acquired(struct kmem_cache
*cachep
)
2418 assert_spin_locked(&cachep
->node
[numa_mem_id()]->list_lock
);
2422 static void check_spinlock_acquired_node(struct kmem_cache
*cachep
, int node
)
2426 assert_spin_locked(&cachep
->node
[node
]->list_lock
);
2431 #define check_irq_off() do { } while(0)
2432 #define check_irq_on() do { } while(0)
2433 #define check_spinlock_acquired(x) do { } while(0)
2434 #define check_spinlock_acquired_node(x, y) do { } while(0)
2437 static void drain_array(struct kmem_cache
*cachep
, struct kmem_cache_node
*n
,
2438 struct array_cache
*ac
,
2439 int force
, int node
);
2441 static void do_drain(void *arg
)
2443 struct kmem_cache
*cachep
= arg
;
2444 struct array_cache
*ac
;
2445 int node
= numa_mem_id();
2448 ac
= cpu_cache_get(cachep
);
2449 spin_lock(&cachep
->node
[node
]->list_lock
);
2450 free_block(cachep
, ac
->entry
, ac
->avail
, node
);
2451 spin_unlock(&cachep
->node
[node
]->list_lock
);
2455 static void drain_cpu_caches(struct kmem_cache
*cachep
)
2457 struct kmem_cache_node
*n
;
2460 on_each_cpu(do_drain
, cachep
, 1);
2462 for_each_online_node(node
) {
2463 n
= cachep
->node
[node
];
2465 drain_alien_cache(cachep
, n
->alien
);
2468 for_each_online_node(node
) {
2469 n
= cachep
->node
[node
];
2471 drain_array(cachep
, n
, n
->shared
, 1, node
);
2476 * Remove slabs from the list of free slabs.
2477 * Specify the number of slabs to drain in tofree.
2479 * Returns the actual number of slabs released.
2481 static int drain_freelist(struct kmem_cache
*cache
,
2482 struct kmem_cache_node
*n
, int tofree
)
2484 struct list_head
*p
;
2489 while (nr_freed
< tofree
&& !list_empty(&n
->slabs_free
)) {
2491 spin_lock_irq(&n
->list_lock
);
2492 p
= n
->slabs_free
.prev
;
2493 if (p
== &n
->slabs_free
) {
2494 spin_unlock_irq(&n
->list_lock
);
2498 slabp
= list_entry(p
, struct slab
, list
);
2500 BUG_ON(slabp
->inuse
);
2502 list_del(&slabp
->list
);
2504 * Safe to drop the lock. The slab is no longer linked
2507 n
->free_objects
-= cache
->num
;
2508 spin_unlock_irq(&n
->list_lock
);
2509 slab_destroy(cache
, slabp
);
2516 /* Called with slab_mutex held to protect against cpu hotplug */
2517 static int __cache_shrink(struct kmem_cache
*cachep
)
2520 struct kmem_cache_node
*n
;
2522 drain_cpu_caches(cachep
);
2525 for_each_online_node(i
) {
2526 n
= cachep
->node
[i
];
2530 drain_freelist(cachep
, n
, slabs_tofree(cachep
, n
));
2532 ret
+= !list_empty(&n
->slabs_full
) ||
2533 !list_empty(&n
->slabs_partial
);
2535 return (ret
? 1 : 0);
2539 * kmem_cache_shrink - Shrink a cache.
2540 * @cachep: The cache to shrink.
2542 * Releases as many slabs as possible for a cache.
2543 * To help debugging, a zero exit status indicates all slabs were released.
2545 int kmem_cache_shrink(struct kmem_cache
*cachep
)
2548 BUG_ON(!cachep
|| in_interrupt());
2551 mutex_lock(&slab_mutex
);
2552 ret
= __cache_shrink(cachep
);
2553 mutex_unlock(&slab_mutex
);
2557 EXPORT_SYMBOL(kmem_cache_shrink
);
2559 int __kmem_cache_shutdown(struct kmem_cache
*cachep
)
2562 struct kmem_cache_node
*n
;
2563 int rc
= __cache_shrink(cachep
);
2568 for_each_online_cpu(i
)
2569 kfree(cachep
->array
[i
]);
2571 /* NUMA: free the node structures */
2572 for_each_online_node(i
) {
2573 n
= cachep
->node
[i
];
2576 free_alien_cache(n
->alien
);
2584 * Get the memory for a slab management obj.
2585 * For a slab cache when the slab descriptor is off-slab, slab descriptors
2586 * always come from malloc_sizes caches. The slab descriptor cannot
2587 * come from the same cache which is getting created because,
2588 * when we are searching for an appropriate cache for these
2589 * descriptors in kmem_cache_create, we search through the malloc_sizes array.
2590 * If we are creating a malloc_sizes cache here it would not be visible to
2591 * kmem_find_general_cachep till the initialization is complete.
2592 * Hence we cannot have slabp_cache same as the original cache.
2594 static struct slab
*alloc_slabmgmt(struct kmem_cache
*cachep
,
2595 struct page
*page
, int colour_off
,
2596 gfp_t local_flags
, int nodeid
)
2599 void *addr
= page_address(page
);
2601 if (OFF_SLAB(cachep
)) {
2602 /* Slab management obj is off-slab. */
2603 slabp
= kmem_cache_alloc_node(cachep
->slabp_cache
,
2604 local_flags
, nodeid
);
2606 * If the first object in the slab is leaked (it's allocated
2607 * but no one has a reference to it), we want to make sure
2608 * kmemleak does not treat the ->s_mem pointer as a reference
2609 * to the object. Otherwise we will not report the leak.
2611 kmemleak_scan_area(&slabp
->list
, sizeof(struct list_head
),
2616 slabp
= addr
+ colour_off
;
2617 colour_off
+= cachep
->slab_size
;
2620 slabp
->s_mem
= addr
+ colour_off
;
2625 static inline kmem_bufctl_t
*slab_bufctl(struct slab
*slabp
)
2627 return (kmem_bufctl_t
*) (slabp
+ 1);
2630 static void cache_init_objs(struct kmem_cache
*cachep
,
2635 for (i
= 0; i
< cachep
->num
; i
++) {
2636 void *objp
= index_to_obj(cachep
, slabp
, i
);
2638 /* need to poison the objs? */
2639 if (cachep
->flags
& SLAB_POISON
)
2640 poison_obj(cachep
, objp
, POISON_FREE
);
2641 if (cachep
->flags
& SLAB_STORE_USER
)
2642 *dbg_userword(cachep
, objp
) = NULL
;
2644 if (cachep
->flags
& SLAB_RED_ZONE
) {
2645 *dbg_redzone1(cachep
, objp
) = RED_INACTIVE
;
2646 *dbg_redzone2(cachep
, objp
) = RED_INACTIVE
;
2649 * Constructors are not allowed to allocate memory from the same
2650 * cache which they are a constructor for. Otherwise, deadlock.
2651 * They must also be threaded.
2653 if (cachep
->ctor
&& !(cachep
->flags
& SLAB_POISON
))
2654 cachep
->ctor(objp
+ obj_offset(cachep
));
2656 if (cachep
->flags
& SLAB_RED_ZONE
) {
2657 if (*dbg_redzone2(cachep
, objp
) != RED_INACTIVE
)
2658 slab_error(cachep
, "constructor overwrote the"
2659 " end of an object");
2660 if (*dbg_redzone1(cachep
, objp
) != RED_INACTIVE
)
2661 slab_error(cachep
, "constructor overwrote the"
2662 " start of an object");
2664 if ((cachep
->size
% PAGE_SIZE
) == 0 &&
2665 OFF_SLAB(cachep
) && cachep
->flags
& SLAB_POISON
)
2666 kernel_map_pages(virt_to_page(objp
),
2667 cachep
->size
/ PAGE_SIZE
, 0);
2672 slab_bufctl(slabp
)[i
] = i
+ 1;
2674 slab_bufctl(slabp
)[i
- 1] = BUFCTL_END
;
2677 static void kmem_flagcheck(struct kmem_cache
*cachep
, gfp_t flags
)
2679 if (CONFIG_ZONE_DMA_FLAG
) {
2680 if (flags
& GFP_DMA
)
2681 BUG_ON(!(cachep
->allocflags
& GFP_DMA
));
2683 BUG_ON(cachep
->allocflags
& GFP_DMA
);
2687 static void *slab_get_obj(struct kmem_cache
*cachep
, struct slab
*slabp
,
2690 void *objp
= index_to_obj(cachep
, slabp
, slabp
->free
);
2694 next
= slab_bufctl(slabp
)[slabp
->free
];
2696 slab_bufctl(slabp
)[slabp
->free
] = BUFCTL_FREE
;
2697 WARN_ON(page_to_nid(virt_to_page(objp
)) != nodeid
);
2704 static void slab_put_obj(struct kmem_cache
*cachep
, struct slab
*slabp
,
2705 void *objp
, int nodeid
)
2707 unsigned int objnr
= obj_to_index(cachep
, slabp
, objp
);
2710 /* Verify that the slab belongs to the intended node */
2711 WARN_ON(page_to_nid(virt_to_page(objp
)) != nodeid
);
2713 if (slab_bufctl(slabp
)[objnr
] + 1 <= SLAB_LIMIT
+ 1) {
2714 printk(KERN_ERR
"slab: double free detected in cache "
2715 "'%s', objp %p\n", cachep
->name
, objp
);
2719 slab_bufctl(slabp
)[objnr
] = slabp
->free
;
2720 slabp
->free
= objnr
;
2725 * Map pages beginning at addr to the given cache and slab. This is required
2726 * for the slab allocator to be able to lookup the cache and slab of a
2727 * virtual address for kfree, ksize, and slab debugging.
2729 static void slab_map_pages(struct kmem_cache
*cache
, struct slab
*slab
,
2735 if (likely(!PageCompound(page
)))
2736 nr_pages
<<= cache
->gfporder
;
2739 page
->slab_cache
= cache
;
2740 page
->slab_page
= slab
;
2742 } while (--nr_pages
);
2746 * Grow (by 1) the number of slabs within a cache. This is called by
2747 * kmem_cache_alloc() when there are no active objs left in a cache.
2749 static int cache_grow(struct kmem_cache
*cachep
,
2750 gfp_t flags
, int nodeid
, struct page
*page
)
2755 struct kmem_cache_node
*n
;
2758 * Be lazy and only check for valid flags here, keeping it out of the
2759 * critical path in kmem_cache_alloc().
2761 BUG_ON(flags
& GFP_SLAB_BUG_MASK
);
2762 local_flags
= flags
& (GFP_CONSTRAINT_MASK
|GFP_RECLAIM_MASK
);
2764 /* Take the node list lock to change the colour_next on this node */
2766 n
= cachep
->node
[nodeid
];
2767 spin_lock(&n
->list_lock
);
2769 /* Get colour for the slab, and cal the next value. */
2770 offset
= n
->colour_next
;
2772 if (n
->colour_next
>= cachep
->colour
)
2774 spin_unlock(&n
->list_lock
);
2776 offset
*= cachep
->colour_off
;
2778 if (local_flags
& __GFP_WAIT
)
2782 * The test for missing atomic flag is performed here, rather than
2783 * the more obvious place, simply to reduce the critical path length
2784 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2785 * will eventually be caught here (where it matters).
2787 kmem_flagcheck(cachep
, flags
);
2790 * Get mem for the objs. Attempt to allocate a physical page from
2794 page
= kmem_getpages(cachep
, local_flags
, nodeid
);
2798 /* Get slab management. */
2799 slabp
= alloc_slabmgmt(cachep
, page
, offset
,
2800 local_flags
& ~GFP_CONSTRAINT_MASK
, nodeid
);
2804 slab_map_pages(cachep
, slabp
, page
);
2806 cache_init_objs(cachep
, slabp
);
2808 if (local_flags
& __GFP_WAIT
)
2809 local_irq_disable();
2811 spin_lock(&n
->list_lock
);
2813 /* Make slab active. */
2814 list_add_tail(&slabp
->list
, &(n
->slabs_free
));
2815 STATS_INC_GROWN(cachep
);
2816 n
->free_objects
+= cachep
->num
;
2817 spin_unlock(&n
->list_lock
);
2820 kmem_freepages(cachep
, page
);
2822 if (local_flags
& __GFP_WAIT
)
2823 local_irq_disable();
2830 * Perform extra freeing checks:
2831 * - detect bad pointers.
2832 * - POISON/RED_ZONE checking
2834 static void kfree_debugcheck(const void *objp
)
2836 if (!virt_addr_valid(objp
)) {
2837 printk(KERN_ERR
"kfree_debugcheck: out of range ptr %lxh.\n",
2838 (unsigned long)objp
);
2843 static inline void verify_redzone_free(struct kmem_cache
*cache
, void *obj
)
2845 unsigned long long redzone1
, redzone2
;
2847 redzone1
= *dbg_redzone1(cache
, obj
);
2848 redzone2
= *dbg_redzone2(cache
, obj
);
2853 if (redzone1
== RED_ACTIVE
&& redzone2
== RED_ACTIVE
)
2856 if (redzone1
== RED_INACTIVE
&& redzone2
== RED_INACTIVE
)
2857 slab_error(cache
, "double free detected");
2859 slab_error(cache
, "memory outside object was overwritten");
2861 printk(KERN_ERR
"%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
2862 obj
, redzone1
, redzone2
);
2865 static void *cache_free_debugcheck(struct kmem_cache
*cachep
, void *objp
,
2866 unsigned long caller
)
2872 BUG_ON(virt_to_cache(objp
) != cachep
);
2874 objp
-= obj_offset(cachep
);
2875 kfree_debugcheck(objp
);
2876 page
= virt_to_head_page(objp
);
2878 slabp
= page
->slab_page
;
2880 if (cachep
->flags
& SLAB_RED_ZONE
) {
2881 verify_redzone_free(cachep
, objp
);
2882 *dbg_redzone1(cachep
, objp
) = RED_INACTIVE
;
2883 *dbg_redzone2(cachep
, objp
) = RED_INACTIVE
;
2885 if (cachep
->flags
& SLAB_STORE_USER
)
2886 *dbg_userword(cachep
, objp
) = (void *)caller
;
2888 objnr
= obj_to_index(cachep
, slabp
, objp
);
2890 BUG_ON(objnr
>= cachep
->num
);
2891 BUG_ON(objp
!= index_to_obj(cachep
, slabp
, objnr
));
2893 #ifdef CONFIG_DEBUG_SLAB_LEAK
2894 slab_bufctl(slabp
)[objnr
] = BUFCTL_FREE
;
2896 if (cachep
->flags
& SLAB_POISON
) {
2897 #ifdef CONFIG_DEBUG_PAGEALLOC
2898 if ((cachep
->size
% PAGE_SIZE
)==0 && OFF_SLAB(cachep
)) {
2899 store_stackinfo(cachep
, objp
, caller
);
2900 kernel_map_pages(virt_to_page(objp
),
2901 cachep
->size
/ PAGE_SIZE
, 0);
2903 poison_obj(cachep
, objp
, POISON_FREE
);
2906 poison_obj(cachep
, objp
, POISON_FREE
);
2912 static void check_slabp(struct kmem_cache
*cachep
, struct slab
*slabp
)
2917 /* Check slab's freelist to see if this obj is there. */
2918 for (i
= slabp
->free
; i
!= BUFCTL_END
; i
= slab_bufctl(slabp
)[i
]) {
2920 if (entries
> cachep
->num
|| i
>= cachep
->num
)
2923 if (entries
!= cachep
->num
- slabp
->inuse
) {
2925 printk(KERN_ERR
"slab: Internal list corruption detected in "
2926 "cache '%s'(%d), slabp %p(%d). Tainted(%s). Hexdump:\n",
2927 cachep
->name
, cachep
->num
, slabp
, slabp
->inuse
,
2929 print_hex_dump(KERN_ERR
, "", DUMP_PREFIX_OFFSET
, 16, 1, slabp
,
2930 sizeof(*slabp
) + cachep
->num
* sizeof(kmem_bufctl_t
),
2936 #define kfree_debugcheck(x) do { } while(0)
2937 #define cache_free_debugcheck(x,objp,z) (objp)
2938 #define check_slabp(x,y) do { } while(0)
2941 static void *cache_alloc_refill(struct kmem_cache
*cachep
, gfp_t flags
,
2945 struct kmem_cache_node
*n
;
2946 struct array_cache
*ac
;
2950 node
= numa_mem_id();
2951 if (unlikely(force_refill
))
2954 ac
= cpu_cache_get(cachep
);
2955 batchcount
= ac
->batchcount
;
2956 if (!ac
->touched
&& batchcount
> BATCHREFILL_LIMIT
) {
2958 * If there was little recent activity on this cache, then
2959 * perform only a partial refill. Otherwise we could generate
2962 batchcount
= BATCHREFILL_LIMIT
;
2964 n
= cachep
->node
[node
];
2966 BUG_ON(ac
->avail
> 0 || !n
);
2967 spin_lock(&n
->list_lock
);
2969 /* See if we can refill from the shared array */
2970 if (n
->shared
&& transfer_objects(ac
, n
->shared
, batchcount
)) {
2971 n
->shared
->touched
= 1;
2975 while (batchcount
> 0) {
2976 struct list_head
*entry
;
2978 /* Get slab alloc is to come from. */
2979 entry
= n
->slabs_partial
.next
;
2980 if (entry
== &n
->slabs_partial
) {
2981 n
->free_touched
= 1;
2982 entry
= n
->slabs_free
.next
;
2983 if (entry
== &n
->slabs_free
)
2987 slabp
= list_entry(entry
, struct slab
, list
);
2988 check_slabp(cachep
, slabp
);
2989 check_spinlock_acquired(cachep
);
2992 * The slab was either on partial or free list so
2993 * there must be at least one object available for
2996 BUG_ON(slabp
->inuse
>= cachep
->num
);
2998 while (slabp
->inuse
< cachep
->num
&& batchcount
--) {
2999 STATS_INC_ALLOCED(cachep
);
3000 STATS_INC_ACTIVE(cachep
);
3001 STATS_SET_HIGH(cachep
);
3003 ac_put_obj(cachep
, ac
, slab_get_obj(cachep
, slabp
,
3006 check_slabp(cachep
, slabp
);
3008 /* move slabp to correct slabp list: */
3009 list_del(&slabp
->list
);
3010 if (slabp
->free
== BUFCTL_END
)
3011 list_add(&slabp
->list
, &n
->slabs_full
);
3013 list_add(&slabp
->list
, &n
->slabs_partial
);
3017 n
->free_objects
-= ac
->avail
;
3019 spin_unlock(&n
->list_lock
);
3021 if (unlikely(!ac
->avail
)) {
3024 x
= cache_grow(cachep
, flags
| GFP_THISNODE
, node
, NULL
);
3026 /* cache_grow can reenable interrupts, then ac could change. */
3027 ac
= cpu_cache_get(cachep
);
3028 node
= numa_mem_id();
3030 /* no objects in sight? abort */
3031 if (!x
&& (ac
->avail
== 0 || force_refill
))
3034 if (!ac
->avail
) /* objects refilled by interrupt? */
3039 return ac_get_obj(cachep
, ac
, flags
, force_refill
);
3042 static inline void cache_alloc_debugcheck_before(struct kmem_cache
*cachep
,
3045 might_sleep_if(flags
& __GFP_WAIT
);
3047 kmem_flagcheck(cachep
, flags
);
3052 static void *cache_alloc_debugcheck_after(struct kmem_cache
*cachep
,
3053 gfp_t flags
, void *objp
, unsigned long caller
)
3057 if (cachep
->flags
& SLAB_POISON
) {
3058 #ifdef CONFIG_DEBUG_PAGEALLOC
3059 if ((cachep
->size
% PAGE_SIZE
) == 0 && OFF_SLAB(cachep
))
3060 kernel_map_pages(virt_to_page(objp
),
3061 cachep
->size
/ PAGE_SIZE
, 1);
3063 check_poison_obj(cachep
, objp
);
3065 check_poison_obj(cachep
, objp
);
3067 poison_obj(cachep
, objp
, POISON_INUSE
);
3069 if (cachep
->flags
& SLAB_STORE_USER
)
3070 *dbg_userword(cachep
, objp
) = (void *)caller
;
3072 if (cachep
->flags
& SLAB_RED_ZONE
) {
3073 if (*dbg_redzone1(cachep
, objp
) != RED_INACTIVE
||
3074 *dbg_redzone2(cachep
, objp
) != RED_INACTIVE
) {
3075 slab_error(cachep
, "double free, or memory outside"
3076 " object was overwritten");
3078 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
3079 objp
, *dbg_redzone1(cachep
, objp
),
3080 *dbg_redzone2(cachep
, objp
));
3082 *dbg_redzone1(cachep
, objp
) = RED_ACTIVE
;
3083 *dbg_redzone2(cachep
, objp
) = RED_ACTIVE
;
3085 #ifdef CONFIG_DEBUG_SLAB_LEAK
3090 slabp
= virt_to_head_page(objp
)->slab_page
;
3091 objnr
= (unsigned)(objp
- slabp
->s_mem
) / cachep
->size
;
3092 slab_bufctl(slabp
)[objnr
] = BUFCTL_ACTIVE
;
3095 objp
+= obj_offset(cachep
);
3096 if (cachep
->ctor
&& cachep
->flags
& SLAB_POISON
)
3098 if (ARCH_SLAB_MINALIGN
&&
3099 ((unsigned long)objp
& (ARCH_SLAB_MINALIGN
-1))) {
3100 printk(KERN_ERR
"0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
3101 objp
, (int)ARCH_SLAB_MINALIGN
);
3106 #define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
3109 static bool slab_should_failslab(struct kmem_cache
*cachep
, gfp_t flags
)
3111 if (cachep
== kmem_cache
)
3114 return should_failslab(cachep
->object_size
, flags
, cachep
->flags
);
3117 static inline void *____cache_alloc(struct kmem_cache
*cachep
, gfp_t flags
)
3120 struct array_cache
*ac
;
3121 bool force_refill
= false;
3125 ac
= cpu_cache_get(cachep
);
3126 if (likely(ac
->avail
)) {
3128 objp
= ac_get_obj(cachep
, ac
, flags
, false);
3131 * Allow for the possibility all avail objects are not allowed
3132 * by the current flags
3135 STATS_INC_ALLOCHIT(cachep
);
3138 force_refill
= true;
3141 STATS_INC_ALLOCMISS(cachep
);
3142 objp
= cache_alloc_refill(cachep
, flags
, force_refill
);
3144 * the 'ac' may be updated by cache_alloc_refill(),
3145 * and kmemleak_erase() requires its correct value.
3147 ac
= cpu_cache_get(cachep
);
3151 * To avoid a false negative, if an object that is in one of the
3152 * per-CPU caches is leaked, we need to make sure kmemleak doesn't
3153 * treat the array pointers as a reference to the object.
3156 kmemleak_erase(&ac
->entry
[ac
->avail
]);
3162 * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
3164 * If we are in_interrupt, then process context, including cpusets and
3165 * mempolicy, may not apply and should not be used for allocation policy.
3167 static void *alternate_node_alloc(struct kmem_cache
*cachep
, gfp_t flags
)
3169 int nid_alloc
, nid_here
;
3171 if (in_interrupt() || (flags
& __GFP_THISNODE
))
3173 nid_alloc
= nid_here
= numa_mem_id();
3174 if (cpuset_do_slab_mem_spread() && (cachep
->flags
& SLAB_MEM_SPREAD
))
3175 nid_alloc
= cpuset_slab_spread_node();
3176 else if (current
->mempolicy
)
3177 nid_alloc
= slab_node();
3178 if (nid_alloc
!= nid_here
)
3179 return ____cache_alloc_node(cachep
, flags
, nid_alloc
);
3184 * Fallback function if there was no memory available and no objects on a
3185 * certain node and fall back is permitted. First we scan all the
3186 * available node for available objects. If that fails then we
3187 * perform an allocation without specifying a node. This allows the page
3188 * allocator to do its reclaim / fallback magic. We then insert the
3189 * slab into the proper nodelist and then allocate from it.
3191 static void *fallback_alloc(struct kmem_cache
*cache
, gfp_t flags
)
3193 struct zonelist
*zonelist
;
3197 enum zone_type high_zoneidx
= gfp_zone(flags
);
3200 unsigned int cpuset_mems_cookie
;
3202 if (flags
& __GFP_THISNODE
)
3205 local_flags
= flags
& (GFP_CONSTRAINT_MASK
|GFP_RECLAIM_MASK
);
3208 cpuset_mems_cookie
= get_mems_allowed();
3209 zonelist
= node_zonelist(slab_node(), flags
);
3213 * Look through allowed nodes for objects available
3214 * from existing per node queues.
3216 for_each_zone_zonelist(zone
, z
, zonelist
, high_zoneidx
) {
3217 nid
= zone_to_nid(zone
);
3219 if (cpuset_zone_allowed_hardwall(zone
, flags
) &&
3221 cache
->node
[nid
]->free_objects
) {
3222 obj
= ____cache_alloc_node(cache
,
3223 flags
| GFP_THISNODE
, nid
);
3231 * This allocation will be performed within the constraints
3232 * of the current cpuset / memory policy requirements.
3233 * We may trigger various forms of reclaim on the allowed
3234 * set and go into memory reserves if necessary.
3238 if (local_flags
& __GFP_WAIT
)
3240 kmem_flagcheck(cache
, flags
);
3241 page
= kmem_getpages(cache
, local_flags
, numa_mem_id());
3242 if (local_flags
& __GFP_WAIT
)
3243 local_irq_disable();
3246 * Insert into the appropriate per node queues
3248 nid
= page_to_nid(page
);
3249 if (cache_grow(cache
, flags
, nid
, page
)) {
3250 obj
= ____cache_alloc_node(cache
,
3251 flags
| GFP_THISNODE
, nid
);
3254 * Another processor may allocate the
3255 * objects in the slab since we are
3256 * not holding any locks.
3260 /* cache_grow already freed obj */
3266 if (unlikely(!put_mems_allowed(cpuset_mems_cookie
) && !obj
))
3272 * A interface to enable slab creation on nodeid
3274 static void *____cache_alloc_node(struct kmem_cache
*cachep
, gfp_t flags
,
3277 struct list_head
*entry
;
3279 struct kmem_cache_node
*n
;
3283 VM_BUG_ON(nodeid
> num_online_nodes());
3284 n
= cachep
->node
[nodeid
];
3289 spin_lock(&n
->list_lock
);
3290 entry
= n
->slabs_partial
.next
;
3291 if (entry
== &n
->slabs_partial
) {
3292 n
->free_touched
= 1;
3293 entry
= n
->slabs_free
.next
;
3294 if (entry
== &n
->slabs_free
)
3298 slabp
= list_entry(entry
, struct slab
, list
);
3299 check_spinlock_acquired_node(cachep
, nodeid
);
3300 check_slabp(cachep
, slabp
);
3302 STATS_INC_NODEALLOCS(cachep
);
3303 STATS_INC_ACTIVE(cachep
);
3304 STATS_SET_HIGH(cachep
);
3306 BUG_ON(slabp
->inuse
== cachep
->num
);
3308 obj
= slab_get_obj(cachep
, slabp
, nodeid
);
3309 check_slabp(cachep
, slabp
);
3311 /* move slabp to correct slabp list: */
3312 list_del(&slabp
->list
);
3314 if (slabp
->free
== BUFCTL_END
)
3315 list_add(&slabp
->list
, &n
->slabs_full
);
3317 list_add(&slabp
->list
, &n
->slabs_partial
);
3319 spin_unlock(&n
->list_lock
);
3323 spin_unlock(&n
->list_lock
);
3324 x
= cache_grow(cachep
, flags
| GFP_THISNODE
, nodeid
, NULL
);
3328 return fallback_alloc(cachep
, flags
);
3334 static __always_inline
void *
3335 slab_alloc_node(struct kmem_cache
*cachep
, gfp_t flags
, int nodeid
,
3336 unsigned long caller
)
3338 unsigned long save_flags
;
3340 int slab_node
= numa_mem_id();
3342 flags
&= gfp_allowed_mask
;
3344 lockdep_trace_alloc(flags
);
3346 if (slab_should_failslab(cachep
, flags
))
3349 cachep
= memcg_kmem_get_cache(cachep
, flags
);
3351 cache_alloc_debugcheck_before(cachep
, flags
);
3352 local_irq_save(save_flags
);
3354 if (nodeid
== NUMA_NO_NODE
)
3357 if (unlikely(!cachep
->node
[nodeid
])) {
3358 /* Node not bootstrapped yet */
3359 ptr
= fallback_alloc(cachep
, flags
);
3363 if (nodeid
== slab_node
) {
3365 * Use the locally cached objects if possible.
3366 * However ____cache_alloc does not allow fallback
3367 * to other nodes. It may fail while we still have
3368 * objects on other nodes available.
3370 ptr
= ____cache_alloc(cachep
, flags
);
3374 /* ___cache_alloc_node can fall back to other nodes */
3375 ptr
= ____cache_alloc_node(cachep
, flags
, nodeid
);
3377 local_irq_restore(save_flags
);
3378 ptr
= cache_alloc_debugcheck_after(cachep
, flags
, ptr
, caller
);
3379 kmemleak_alloc_recursive(ptr
, cachep
->object_size
, 1, cachep
->flags
,
3383 kmemcheck_slab_alloc(cachep
, flags
, ptr
, cachep
->object_size
);
3385 if (unlikely((flags
& __GFP_ZERO
) && ptr
))
3386 memset(ptr
, 0, cachep
->object_size
);
3391 static __always_inline
void *
3392 __do_cache_alloc(struct kmem_cache
*cache
, gfp_t flags
)
3396 if (unlikely(current
->flags
& (PF_SPREAD_SLAB
| PF_MEMPOLICY
))) {
3397 objp
= alternate_node_alloc(cache
, flags
);
3401 objp
= ____cache_alloc(cache
, flags
);
3404 * We may just have run out of memory on the local node.
3405 * ____cache_alloc_node() knows how to locate memory on other nodes
3408 objp
= ____cache_alloc_node(cache
, flags
, numa_mem_id());
3415 static __always_inline
void *
3416 __do_cache_alloc(struct kmem_cache
*cachep
, gfp_t flags
)
3418 return ____cache_alloc(cachep
, flags
);
3421 #endif /* CONFIG_NUMA */
3423 static __always_inline
void *
3424 slab_alloc(struct kmem_cache
*cachep
, gfp_t flags
, unsigned long caller
)
3426 unsigned long save_flags
;
3429 flags
&= gfp_allowed_mask
;
3431 lockdep_trace_alloc(flags
);
3433 if (slab_should_failslab(cachep
, flags
))
3436 cachep
= memcg_kmem_get_cache(cachep
, flags
);
3438 cache_alloc_debugcheck_before(cachep
, flags
);
3439 local_irq_save(save_flags
);
3440 objp
= __do_cache_alloc(cachep
, flags
);
3441 local_irq_restore(save_flags
);
3442 objp
= cache_alloc_debugcheck_after(cachep
, flags
, objp
, caller
);
3443 kmemleak_alloc_recursive(objp
, cachep
->object_size
, 1, cachep
->flags
,
3448 kmemcheck_slab_alloc(cachep
, flags
, objp
, cachep
->object_size
);
3450 if (unlikely((flags
& __GFP_ZERO
) && objp
))
3451 memset(objp
, 0, cachep
->object_size
);
3457 * Caller needs to acquire correct kmem_list's list_lock
3459 static void free_block(struct kmem_cache
*cachep
, void **objpp
, int nr_objects
,
3463 struct kmem_cache_node
*n
;
3465 for (i
= 0; i
< nr_objects
; i
++) {
3469 clear_obj_pfmemalloc(&objpp
[i
]);
3472 slabp
= virt_to_slab(objp
);
3473 n
= cachep
->node
[node
];
3474 list_del(&slabp
->list
);
3475 check_spinlock_acquired_node(cachep
, node
);
3476 check_slabp(cachep
, slabp
);
3477 slab_put_obj(cachep
, slabp
, objp
, node
);
3478 STATS_DEC_ACTIVE(cachep
);
3480 check_slabp(cachep
, slabp
);
3482 /* fixup slab chains */
3483 if (slabp
->inuse
== 0) {
3484 if (n
->free_objects
> n
->free_limit
) {
3485 n
->free_objects
-= cachep
->num
;
3486 /* No need to drop any previously held
3487 * lock here, even if we have a off-slab slab
3488 * descriptor it is guaranteed to come from
3489 * a different cache, refer to comments before
3492 slab_destroy(cachep
, slabp
);
3494 list_add(&slabp
->list
, &n
->slabs_free
);
3497 /* Unconditionally move a slab to the end of the
3498 * partial list on free - maximum time for the
3499 * other objects to be freed, too.
3501 list_add_tail(&slabp
->list
, &n
->slabs_partial
);
3506 static void cache_flusharray(struct kmem_cache
*cachep
, struct array_cache
*ac
)
3509 struct kmem_cache_node
*n
;
3510 int node
= numa_mem_id();
3512 batchcount
= ac
->batchcount
;
3514 BUG_ON(!batchcount
|| batchcount
> ac
->avail
);
3517 n
= cachep
->node
[node
];
3518 spin_lock(&n
->list_lock
);
3520 struct array_cache
*shared_array
= n
->shared
;
3521 int max
= shared_array
->limit
- shared_array
->avail
;
3523 if (batchcount
> max
)
3525 memcpy(&(shared_array
->entry
[shared_array
->avail
]),
3526 ac
->entry
, sizeof(void *) * batchcount
);
3527 shared_array
->avail
+= batchcount
;
3532 free_block(cachep
, ac
->entry
, batchcount
, node
);
3537 struct list_head
*p
;
3539 p
= n
->slabs_free
.next
;
3540 while (p
!= &(n
->slabs_free
)) {
3543 slabp
= list_entry(p
, struct slab
, list
);
3544 BUG_ON(slabp
->inuse
);
3549 STATS_SET_FREEABLE(cachep
, i
);
3552 spin_unlock(&n
->list_lock
);
3553 ac
->avail
-= batchcount
;
3554 memmove(ac
->entry
, &(ac
->entry
[batchcount
]), sizeof(void *)*ac
->avail
);
3558 * Release an obj back to its cache. If the obj has a constructed state, it must
3559 * be in this state _before_ it is released. Called with disabled ints.
3561 static inline void __cache_free(struct kmem_cache
*cachep
, void *objp
,
3562 unsigned long caller
)
3564 struct array_cache
*ac
= cpu_cache_get(cachep
);
3567 kmemleak_free_recursive(objp
, cachep
->flags
);
3568 objp
= cache_free_debugcheck(cachep
, objp
, caller
);
3570 kmemcheck_slab_free(cachep
, objp
, cachep
->object_size
);
3573 * Skip calling cache_free_alien() when the platform is not numa.
3574 * This will avoid cache misses that happen while accessing slabp (which
3575 * is per page memory reference) to get nodeid. Instead use a global
3576 * variable to skip the call, which is mostly likely to be present in
3579 if (nr_online_nodes
> 1 && cache_free_alien(cachep
, objp
))
3582 if (likely(ac
->avail
< ac
->limit
)) {
3583 STATS_INC_FREEHIT(cachep
);
3585 STATS_INC_FREEMISS(cachep
);
3586 cache_flusharray(cachep
, ac
);
3589 ac_put_obj(cachep
, ac
, objp
);
3593 * kmem_cache_alloc - Allocate an object
3594 * @cachep: The cache to allocate from.
3595 * @flags: See kmalloc().
3597 * Allocate an object from this cache. The flags are only relevant
3598 * if the cache has no available objects.
3600 void *kmem_cache_alloc(struct kmem_cache
*cachep
, gfp_t flags
)
3602 void *ret
= slab_alloc(cachep
, flags
, _RET_IP_
);
3604 trace_kmem_cache_alloc(_RET_IP_
, ret
,
3605 cachep
->object_size
, cachep
->size
, flags
);
3609 EXPORT_SYMBOL(kmem_cache_alloc
);
3611 #ifdef CONFIG_TRACING
3613 kmem_cache_alloc_trace(struct kmem_cache
*cachep
, gfp_t flags
, size_t size
)
3617 ret
= slab_alloc(cachep
, flags
, _RET_IP_
);
3619 trace_kmalloc(_RET_IP_
, ret
,
3620 size
, cachep
->size
, flags
);
3623 EXPORT_SYMBOL(kmem_cache_alloc_trace
);
3628 * kmem_cache_alloc_node - Allocate an object on the specified node
3629 * @cachep: The cache to allocate from.
3630 * @flags: See kmalloc().
3631 * @nodeid: node number of the target node.
3633 * Identical to kmem_cache_alloc but it will allocate memory on the given
3634 * node, which can improve the performance for cpu bound structures.
3636 * Fallback to other node is possible if __GFP_THISNODE is not set.
3638 void *kmem_cache_alloc_node(struct kmem_cache
*cachep
, gfp_t flags
, int nodeid
)
3640 void *ret
= slab_alloc_node(cachep
, flags
, nodeid
, _RET_IP_
);
3642 trace_kmem_cache_alloc_node(_RET_IP_
, ret
,
3643 cachep
->object_size
, cachep
->size
,
3648 EXPORT_SYMBOL(kmem_cache_alloc_node
);
3650 #ifdef CONFIG_TRACING
3651 void *kmem_cache_alloc_node_trace(struct kmem_cache
*cachep
,
3658 ret
= slab_alloc_node(cachep
, flags
, nodeid
, _RET_IP_
);
3660 trace_kmalloc_node(_RET_IP_
, ret
,
3665 EXPORT_SYMBOL(kmem_cache_alloc_node_trace
);
3668 static __always_inline
void *
3669 __do_kmalloc_node(size_t size
, gfp_t flags
, int node
, unsigned long caller
)
3671 struct kmem_cache
*cachep
;
3673 cachep
= kmalloc_slab(size
, flags
);
3674 if (unlikely(ZERO_OR_NULL_PTR(cachep
)))
3676 return kmem_cache_alloc_node_trace(cachep
, flags
, node
, size
);
3679 #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
3680 void *__kmalloc_node(size_t size
, gfp_t flags
, int node
)
3682 return __do_kmalloc_node(size
, flags
, node
, _RET_IP_
);
3684 EXPORT_SYMBOL(__kmalloc_node
);
3686 void *__kmalloc_node_track_caller(size_t size
, gfp_t flags
,
3687 int node
, unsigned long caller
)
3689 return __do_kmalloc_node(size
, flags
, node
, caller
);
3691 EXPORT_SYMBOL(__kmalloc_node_track_caller
);
3693 void *__kmalloc_node(size_t size
, gfp_t flags
, int node
)
3695 return __do_kmalloc_node(size
, flags
, node
, 0);
3697 EXPORT_SYMBOL(__kmalloc_node
);
3698 #endif /* CONFIG_DEBUG_SLAB || CONFIG_TRACING */
3699 #endif /* CONFIG_NUMA */
3702 * __do_kmalloc - allocate memory
3703 * @size: how many bytes of memory are required.
3704 * @flags: the type of memory to allocate (see kmalloc).
3705 * @caller: function caller for debug tracking of the caller
3707 static __always_inline
void *__do_kmalloc(size_t size
, gfp_t flags
,
3708 unsigned long caller
)
3710 struct kmem_cache
*cachep
;
3713 /* If you want to save a few bytes .text space: replace
3715 * Then kmalloc uses the uninlined functions instead of the inline
3718 cachep
= kmalloc_slab(size
, flags
);
3719 if (unlikely(ZERO_OR_NULL_PTR(cachep
)))
3721 ret
= slab_alloc(cachep
, flags
, caller
);
3723 trace_kmalloc(caller
, ret
,
3724 size
, cachep
->size
, flags
);
3730 #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
3731 void *__kmalloc(size_t size
, gfp_t flags
)
3733 return __do_kmalloc(size
, flags
, _RET_IP_
);
3735 EXPORT_SYMBOL(__kmalloc
);
3737 void *__kmalloc_track_caller(size_t size
, gfp_t flags
, unsigned long caller
)
3739 return __do_kmalloc(size
, flags
, caller
);
3741 EXPORT_SYMBOL(__kmalloc_track_caller
);
3744 void *__kmalloc(size_t size
, gfp_t flags
)
3746 return __do_kmalloc(size
, flags
, 0);
3748 EXPORT_SYMBOL(__kmalloc
);
3752 * kmem_cache_free - Deallocate an object
3753 * @cachep: The cache the allocation was from.
3754 * @objp: The previously allocated object.
3756 * Free an object which was previously allocated from this
3759 void kmem_cache_free(struct kmem_cache
*cachep
, void *objp
)
3761 unsigned long flags
;
3762 cachep
= cache_from_obj(cachep
, objp
);
3766 local_irq_save(flags
);
3767 debug_check_no_locks_freed(objp
, cachep
->object_size
);
3768 if (!(cachep
->flags
& SLAB_DEBUG_OBJECTS
))
3769 debug_check_no_obj_freed(objp
, cachep
->object_size
);
3770 __cache_free(cachep
, objp
, _RET_IP_
);
3771 local_irq_restore(flags
);
3773 trace_kmem_cache_free(_RET_IP_
, objp
);
3775 EXPORT_SYMBOL(kmem_cache_free
);
3778 * kfree - free previously allocated memory
3779 * @objp: pointer returned by kmalloc.
3781 * If @objp is NULL, no operation is performed.
3783 * Don't free memory not originally allocated by kmalloc()
3784 * or you will run into trouble.
3786 void kfree(const void *objp
)
3788 struct kmem_cache
*c
;
3789 unsigned long flags
;
3791 trace_kfree(_RET_IP_
, objp
);
3793 if (unlikely(ZERO_OR_NULL_PTR(objp
)))
3795 local_irq_save(flags
);
3796 kfree_debugcheck(objp
);
3797 c
= virt_to_cache(objp
);
3798 debug_check_no_locks_freed(objp
, c
->object_size
);
3800 debug_check_no_obj_freed(objp
, c
->object_size
);
3801 __cache_free(c
, (void *)objp
, _RET_IP_
);
3802 local_irq_restore(flags
);
3804 EXPORT_SYMBOL(kfree
);
3807 * This initializes kmem_cache_node or resizes various caches for all nodes.
3809 static int alloc_kmemlist(struct kmem_cache
*cachep
, gfp_t gfp
)
3812 struct kmem_cache_node
*n
;
3813 struct array_cache
*new_shared
;
3814 struct array_cache
**new_alien
= NULL
;
3816 for_each_online_node(node
) {
3818 if (use_alien_caches
) {
3819 new_alien
= alloc_alien_cache(node
, cachep
->limit
, gfp
);
3825 if (cachep
->shared
) {
3826 new_shared
= alloc_arraycache(node
,
3827 cachep
->shared
*cachep
->batchcount
,
3830 free_alien_cache(new_alien
);
3835 n
= cachep
->node
[node
];
3837 struct array_cache
*shared
= n
->shared
;
3839 spin_lock_irq(&n
->list_lock
);
3842 free_block(cachep
, shared
->entry
,
3843 shared
->avail
, node
);
3845 n
->shared
= new_shared
;
3847 n
->alien
= new_alien
;
3850 n
->free_limit
= (1 + nr_cpus_node(node
)) *
3851 cachep
->batchcount
+ cachep
->num
;
3852 spin_unlock_irq(&n
->list_lock
);
3854 free_alien_cache(new_alien
);
3857 n
= kmalloc_node(sizeof(struct kmem_cache_node
), gfp
, node
);
3859 free_alien_cache(new_alien
);
3864 kmem_cache_node_init(n
);
3865 n
->next_reap
= jiffies
+ REAPTIMEOUT_LIST3
+
3866 ((unsigned long)cachep
) % REAPTIMEOUT_LIST3
;
3867 n
->shared
= new_shared
;
3868 n
->alien
= new_alien
;
3869 n
->free_limit
= (1 + nr_cpus_node(node
)) *
3870 cachep
->batchcount
+ cachep
->num
;
3871 cachep
->node
[node
] = n
;
3876 if (!cachep
->list
.next
) {
3877 /* Cache is not active yet. Roll back what we did */
3880 if (cachep
->node
[node
]) {
3881 n
= cachep
->node
[node
];
3884 free_alien_cache(n
->alien
);
3886 cachep
->node
[node
] = NULL
;
3894 struct ccupdate_struct
{
3895 struct kmem_cache
*cachep
;
3896 struct array_cache
*new[0];
3899 static void do_ccupdate_local(void *info
)
3901 struct ccupdate_struct
*new = info
;
3902 struct array_cache
*old
;
3905 old
= cpu_cache_get(new->cachep
);
3907 new->cachep
->array
[smp_processor_id()] = new->new[smp_processor_id()];
3908 new->new[smp_processor_id()] = old
;
3911 /* Always called with the slab_mutex held */
3912 static int __do_tune_cpucache(struct kmem_cache
*cachep
, int limit
,
3913 int batchcount
, int shared
, gfp_t gfp
)
3915 struct ccupdate_struct
*new;
3918 new = kzalloc(sizeof(*new) + nr_cpu_ids
* sizeof(struct array_cache
*),
3923 for_each_online_cpu(i
) {
3924 new->new[i
] = alloc_arraycache(cpu_to_mem(i
), limit
,
3927 for (i
--; i
>= 0; i
--)
3933 new->cachep
= cachep
;
3935 on_each_cpu(do_ccupdate_local
, (void *)new, 1);
3938 cachep
->batchcount
= batchcount
;
3939 cachep
->limit
= limit
;
3940 cachep
->shared
= shared
;
3942 for_each_online_cpu(i
) {
3943 struct array_cache
*ccold
= new->new[i
];
3946 spin_lock_irq(&cachep
->node
[cpu_to_mem(i
)]->list_lock
);
3947 free_block(cachep
, ccold
->entry
, ccold
->avail
, cpu_to_mem(i
));
3948 spin_unlock_irq(&cachep
->node
[cpu_to_mem(i
)]->list_lock
);
3952 return alloc_kmemlist(cachep
, gfp
);
3955 static int do_tune_cpucache(struct kmem_cache
*cachep
, int limit
,
3956 int batchcount
, int shared
, gfp_t gfp
)
3959 struct kmem_cache
*c
= NULL
;
3962 ret
= __do_tune_cpucache(cachep
, limit
, batchcount
, shared
, gfp
);
3964 if (slab_state
< FULL
)
3967 if ((ret
< 0) || !is_root_cache(cachep
))
3970 VM_BUG_ON(!mutex_is_locked(&slab_mutex
));
3971 for_each_memcg_cache_index(i
) {
3972 c
= cache_from_memcg(cachep
, i
);
3974 /* return value determined by the parent cache only */
3975 __do_tune_cpucache(c
, limit
, batchcount
, shared
, gfp
);
3981 /* Called with slab_mutex held always */
3982 static int enable_cpucache(struct kmem_cache
*cachep
, gfp_t gfp
)
3989 if (!is_root_cache(cachep
)) {
3990 struct kmem_cache
*root
= memcg_root_cache(cachep
);
3991 limit
= root
->limit
;
3992 shared
= root
->shared
;
3993 batchcount
= root
->batchcount
;
3996 if (limit
&& shared
&& batchcount
)
3999 * The head array serves three purposes:
4000 * - create a LIFO ordering, i.e. return objects that are cache-warm
4001 * - reduce the number of spinlock operations.
4002 * - reduce the number of linked list operations on the slab and
4003 * bufctl chains: array operations are cheaper.
4004 * The numbers are guessed, we should auto-tune as described by
4007 if (cachep
->size
> 131072)
4009 else if (cachep
->size
> PAGE_SIZE
)
4011 else if (cachep
->size
> 1024)
4013 else if (cachep
->size
> 256)
4019 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
4020 * allocation behaviour: Most allocs on one cpu, most free operations
4021 * on another cpu. For these cases, an efficient object passing between
4022 * cpus is necessary. This is provided by a shared array. The array
4023 * replaces Bonwick's magazine layer.
4024 * On uniprocessor, it's functionally equivalent (but less efficient)
4025 * to a larger limit. Thus disabled by default.
4028 if (cachep
->size
<= PAGE_SIZE
&& num_possible_cpus() > 1)
4033 * With debugging enabled, large batchcount lead to excessively long
4034 * periods with disabled local interrupts. Limit the batchcount
4039 batchcount
= (limit
+ 1) / 2;
4041 err
= do_tune_cpucache(cachep
, limit
, batchcount
, shared
, gfp
);
4043 printk(KERN_ERR
"enable_cpucache failed for %s, error %d.\n",
4044 cachep
->name
, -err
);
4049 * Drain an array if it contains any elements taking the node lock only if
4050 * necessary. Note that the node listlock also protects the array_cache
4051 * if drain_array() is used on the shared array.
4053 static void drain_array(struct kmem_cache
*cachep
, struct kmem_cache_node
*n
,
4054 struct array_cache
*ac
, int force
, int node
)
4058 if (!ac
|| !ac
->avail
)
4060 if (ac
->touched
&& !force
) {
4063 spin_lock_irq(&n
->list_lock
);
4065 tofree
= force
? ac
->avail
: (ac
->limit
+ 4) / 5;
4066 if (tofree
> ac
->avail
)
4067 tofree
= (ac
->avail
+ 1) / 2;
4068 free_block(cachep
, ac
->entry
, tofree
, node
);
4069 ac
->avail
-= tofree
;
4070 memmove(ac
->entry
, &(ac
->entry
[tofree
]),
4071 sizeof(void *) * ac
->avail
);
4073 spin_unlock_irq(&n
->list_lock
);
4078 * cache_reap - Reclaim memory from caches.
4079 * @w: work descriptor
4081 * Called from workqueue/eventd every few seconds.
4083 * - clear the per-cpu caches for this CPU.
4084 * - return freeable pages to the main free memory pool.
4086 * If we cannot acquire the cache chain mutex then just give up - we'll try
4087 * again on the next iteration.
4089 static void cache_reap(struct work_struct
*w
)
4091 struct kmem_cache
*searchp
;
4092 struct kmem_cache_node
*n
;
4093 int node
= numa_mem_id();
4094 struct delayed_work
*work
= to_delayed_work(w
);
4096 if (!mutex_trylock(&slab_mutex
))
4097 /* Give up. Setup the next iteration. */
4100 list_for_each_entry(searchp
, &slab_caches
, list
) {
4104 * We only take the node lock if absolutely necessary and we
4105 * have established with reasonable certainty that
4106 * we can do some work if the lock was obtained.
4108 n
= searchp
->node
[node
];
4110 reap_alien(searchp
, n
);
4112 drain_array(searchp
, n
, cpu_cache_get(searchp
), 0, node
);
4115 * These are racy checks but it does not matter
4116 * if we skip one check or scan twice.
4118 if (time_after(n
->next_reap
, jiffies
))
4121 n
->next_reap
= jiffies
+ REAPTIMEOUT_LIST3
;
4123 drain_array(searchp
, n
, n
->shared
, 0, node
);
4125 if (n
->free_touched
)
4126 n
->free_touched
= 0;
4130 freed
= drain_freelist(searchp
, n
, (n
->free_limit
+
4131 5 * searchp
->num
- 1) / (5 * searchp
->num
));
4132 STATS_ADD_REAPED(searchp
, freed
);
4138 mutex_unlock(&slab_mutex
);
4141 /* Set up the next iteration */
4142 schedule_delayed_work(work
, round_jiffies_relative(REAPTIMEOUT_CPUC
));
4145 #ifdef CONFIG_SLABINFO
4146 void get_slabinfo(struct kmem_cache
*cachep
, struct slabinfo
*sinfo
)
4149 unsigned long active_objs
;
4150 unsigned long num_objs
;
4151 unsigned long active_slabs
= 0;
4152 unsigned long num_slabs
, free_objects
= 0, shared_avail
= 0;
4156 struct kmem_cache_node
*n
;
4160 for_each_online_node(node
) {
4161 n
= cachep
->node
[node
];
4166 spin_lock_irq(&n
->list_lock
);
4168 list_for_each_entry(slabp
, &n
->slabs_full
, list
) {
4169 if (slabp
->inuse
!= cachep
->num
&& !error
)
4170 error
= "slabs_full accounting error";
4171 active_objs
+= cachep
->num
;
4174 list_for_each_entry(slabp
, &n
->slabs_partial
, list
) {
4175 if (slabp
->inuse
== cachep
->num
&& !error
)
4176 error
= "slabs_partial inuse accounting error";
4177 if (!slabp
->inuse
&& !error
)
4178 error
= "slabs_partial/inuse accounting error";
4179 active_objs
+= slabp
->inuse
;
4182 list_for_each_entry(slabp
, &n
->slabs_free
, list
) {
4183 if (slabp
->inuse
&& !error
)
4184 error
= "slabs_free/inuse accounting error";
4187 free_objects
+= n
->free_objects
;
4189 shared_avail
+= n
->shared
->avail
;
4191 spin_unlock_irq(&n
->list_lock
);
4193 num_slabs
+= active_slabs
;
4194 num_objs
= num_slabs
* cachep
->num
;
4195 if (num_objs
- active_objs
!= free_objects
&& !error
)
4196 error
= "free_objects accounting error";
4198 name
= cachep
->name
;
4200 printk(KERN_ERR
"slab: cache %s error: %s\n", name
, error
);
4202 sinfo
->active_objs
= active_objs
;
4203 sinfo
->num_objs
= num_objs
;
4204 sinfo
->active_slabs
= active_slabs
;
4205 sinfo
->num_slabs
= num_slabs
;
4206 sinfo
->shared_avail
= shared_avail
;
4207 sinfo
->limit
= cachep
->limit
;
4208 sinfo
->batchcount
= cachep
->batchcount
;
4209 sinfo
->shared
= cachep
->shared
;
4210 sinfo
->objects_per_slab
= cachep
->num
;
4211 sinfo
->cache_order
= cachep
->gfporder
;
4214 void slabinfo_show_stats(struct seq_file
*m
, struct kmem_cache
*cachep
)
4218 unsigned long high
= cachep
->high_mark
;
4219 unsigned long allocs
= cachep
->num_allocations
;
4220 unsigned long grown
= cachep
->grown
;
4221 unsigned long reaped
= cachep
->reaped
;
4222 unsigned long errors
= cachep
->errors
;
4223 unsigned long max_freeable
= cachep
->max_freeable
;
4224 unsigned long node_allocs
= cachep
->node_allocs
;
4225 unsigned long node_frees
= cachep
->node_frees
;
4226 unsigned long overflows
= cachep
->node_overflow
;
4228 seq_printf(m
, " : globalstat %7lu %6lu %5lu %4lu "
4229 "%4lu %4lu %4lu %4lu %4lu",
4230 allocs
, high
, grown
,
4231 reaped
, errors
, max_freeable
, node_allocs
,
4232 node_frees
, overflows
);
4236 unsigned long allochit
= atomic_read(&cachep
->allochit
);
4237 unsigned long allocmiss
= atomic_read(&cachep
->allocmiss
);
4238 unsigned long freehit
= atomic_read(&cachep
->freehit
);
4239 unsigned long freemiss
= atomic_read(&cachep
->freemiss
);
4241 seq_printf(m
, " : cpustat %6lu %6lu %6lu %6lu",
4242 allochit
, allocmiss
, freehit
, freemiss
);
4247 #define MAX_SLABINFO_WRITE 128
4249 * slabinfo_write - Tuning for the slab allocator
4251 * @buffer: user buffer
4252 * @count: data length
4255 ssize_t
slabinfo_write(struct file
*file
, const char __user
*buffer
,
4256 size_t count
, loff_t
*ppos
)
4258 char kbuf
[MAX_SLABINFO_WRITE
+ 1], *tmp
;
4259 int limit
, batchcount
, shared
, res
;
4260 struct kmem_cache
*cachep
;
4262 if (count
> MAX_SLABINFO_WRITE
)
4264 if (copy_from_user(&kbuf
, buffer
, count
))
4266 kbuf
[MAX_SLABINFO_WRITE
] = '\0';
4268 tmp
= strchr(kbuf
, ' ');
4273 if (sscanf(tmp
, " %d %d %d", &limit
, &batchcount
, &shared
) != 3)
4276 /* Find the cache in the chain of caches. */
4277 mutex_lock(&slab_mutex
);
4279 list_for_each_entry(cachep
, &slab_caches
, list
) {
4280 if (!strcmp(cachep
->name
, kbuf
)) {
4281 if (limit
< 1 || batchcount
< 1 ||
4282 batchcount
> limit
|| shared
< 0) {
4285 res
= do_tune_cpucache(cachep
, limit
,
4292 mutex_unlock(&slab_mutex
);
4298 #ifdef CONFIG_DEBUG_SLAB_LEAK
4300 static void *leaks_start(struct seq_file
*m
, loff_t
*pos
)
4302 mutex_lock(&slab_mutex
);
4303 return seq_list_start(&slab_caches
, *pos
);
4306 static inline int add_caller(unsigned long *n
, unsigned long v
)
4316 unsigned long *q
= p
+ 2 * i
;
4330 memmove(p
+ 2, p
, n
[1] * 2 * sizeof(unsigned long) - ((void *)p
- (void *)n
));
4336 static void handle_slab(unsigned long *n
, struct kmem_cache
*c
, struct slab
*s
)
4342 for (i
= 0, p
= s
->s_mem
; i
< c
->num
; i
++, p
+= c
->size
) {
4343 if (slab_bufctl(s
)[i
] != BUFCTL_ACTIVE
)
4345 if (!add_caller(n
, (unsigned long)*dbg_userword(c
, p
)))
4350 static void show_symbol(struct seq_file
*m
, unsigned long address
)
4352 #ifdef CONFIG_KALLSYMS
4353 unsigned long offset
, size
;
4354 char modname
[MODULE_NAME_LEN
], name
[KSYM_NAME_LEN
];
4356 if (lookup_symbol_attrs(address
, &size
, &offset
, modname
, name
) == 0) {
4357 seq_printf(m
, "%s+%#lx/%#lx", name
, offset
, size
);
4359 seq_printf(m
, " [%s]", modname
);
4363 seq_printf(m
, "%p", (void *)address
);
4366 static int leaks_show(struct seq_file
*m
, void *p
)
4368 struct kmem_cache
*cachep
= list_entry(p
, struct kmem_cache
, list
);
4370 struct kmem_cache_node
*n
;
4372 unsigned long *x
= m
->private;
4376 if (!(cachep
->flags
& SLAB_STORE_USER
))
4378 if (!(cachep
->flags
& SLAB_RED_ZONE
))
4381 /* OK, we can do it */
4385 for_each_online_node(node
) {
4386 n
= cachep
->node
[node
];
4391 spin_lock_irq(&n
->list_lock
);
4393 list_for_each_entry(slabp
, &n
->slabs_full
, list
)
4394 handle_slab(x
, cachep
, slabp
);
4395 list_for_each_entry(slabp
, &n
->slabs_partial
, list
)
4396 handle_slab(x
, cachep
, slabp
);
4397 spin_unlock_irq(&n
->list_lock
);
4399 name
= cachep
->name
;
4401 /* Increase the buffer size */
4402 mutex_unlock(&slab_mutex
);
4403 m
->private = kzalloc(x
[0] * 4 * sizeof(unsigned long), GFP_KERNEL
);
4405 /* Too bad, we are really out */
4407 mutex_lock(&slab_mutex
);
4410 *(unsigned long *)m
->private = x
[0] * 2;
4412 mutex_lock(&slab_mutex
);
4413 /* Now make sure this entry will be retried */
4417 for (i
= 0; i
< x
[1]; i
++) {
4418 seq_printf(m
, "%s: %lu ", name
, x
[2*i
+3]);
4419 show_symbol(m
, x
[2*i
+2]);
4426 static const struct seq_operations slabstats_op
= {
4427 .start
= leaks_start
,
4433 static int slabstats_open(struct inode
*inode
, struct file
*file
)
4435 unsigned long *n
= kzalloc(PAGE_SIZE
, GFP_KERNEL
);
4438 ret
= seq_open(file
, &slabstats_op
);
4440 struct seq_file
*m
= file
->private_data
;
4441 *n
= PAGE_SIZE
/ (2 * sizeof(unsigned long));
4450 static const struct file_operations proc_slabstats_operations
= {
4451 .open
= slabstats_open
,
4453 .llseek
= seq_lseek
,
4454 .release
= seq_release_private
,
4458 static int __init
slab_proc_init(void)
4460 #ifdef CONFIG_DEBUG_SLAB_LEAK
4461 proc_create("slab_allocators", 0, NULL
, &proc_slabstats_operations
);
4465 module_init(slab_proc_init
);
4469 * ksize - get the actual amount of memory allocated for a given object
4470 * @objp: Pointer to the object
4472 * kmalloc may internally round up allocations and return more memory
4473 * than requested. ksize() can be used to determine the actual amount of
4474 * memory allocated. The caller may use this additional memory, even though
4475 * a smaller amount of memory was initially specified with the kmalloc call.
4476 * The caller must guarantee that objp points to a valid object previously
4477 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4478 * must not be freed during the duration of the call.
4480 size_t ksize(const void *objp
)
4483 if (unlikely(objp
== ZERO_SIZE_PTR
))
4486 return virt_to_cache(objp
)->object_size
;
4488 EXPORT_SYMBOL(ksize
);