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
160 #define FREELIST_BYTE_INDEX (((PAGE_SIZE >> BITS_PER_BYTE) \
161 <= SLAB_OBJ_MIN_SIZE) ? 1 : 0)
163 #if FREELIST_BYTE_INDEX
164 typedef unsigned char freelist_idx_t
;
166 typedef unsigned short freelist_idx_t
;
169 #define SLAB_OBJ_MAX_NUM ((1 << sizeof(freelist_idx_t) * BITS_PER_BYTE) - 1)
172 * true if a page was allocated from pfmemalloc reserves for network-based
175 static bool pfmemalloc_active __read_mostly
;
181 * - LIFO ordering, to hand out cache-warm objects from _alloc
182 * - reduce the number of linked list operations
183 * - reduce spinlock operations
185 * The limit is stored in the per-cpu structure to reduce the data cache
192 unsigned int batchcount
;
193 unsigned int touched
;
196 * Must have this definition in here for the proper
197 * alignment of array_cache. Also simplifies accessing
200 * Entries should not be directly dereferenced as
201 * entries belonging to slabs marked pfmemalloc will
202 * have the lower bits set SLAB_OBJ_PFMEMALLOC
206 #define SLAB_OBJ_PFMEMALLOC 1
207 static inline bool is_obj_pfmemalloc(void *objp
)
209 return (unsigned long)objp
& SLAB_OBJ_PFMEMALLOC
;
212 static inline void set_obj_pfmemalloc(void **objp
)
214 *objp
= (void *)((unsigned long)*objp
| SLAB_OBJ_PFMEMALLOC
);
218 static inline void clear_obj_pfmemalloc(void **objp
)
220 *objp
= (void *)((unsigned long)*objp
& ~SLAB_OBJ_PFMEMALLOC
);
224 * bootstrap: The caches do not work without cpuarrays anymore, but the
225 * cpuarrays are allocated from the generic caches...
227 #define BOOT_CPUCACHE_ENTRIES 1
228 struct arraycache_init
{
229 struct array_cache cache
;
230 void *entries
[BOOT_CPUCACHE_ENTRIES
];
234 * Need this for bootstrapping a per node allocator.
236 #define NUM_INIT_LISTS (3 * MAX_NUMNODES)
237 static struct kmem_cache_node __initdata init_kmem_cache_node
[NUM_INIT_LISTS
];
238 #define CACHE_CACHE 0
239 #define SIZE_AC MAX_NUMNODES
240 #define SIZE_NODE (2 * MAX_NUMNODES)
242 static int drain_freelist(struct kmem_cache
*cache
,
243 struct kmem_cache_node
*n
, int tofree
);
244 static void free_block(struct kmem_cache
*cachep
, void **objpp
, int len
,
246 static int enable_cpucache(struct kmem_cache
*cachep
, gfp_t gfp
);
247 static void cache_reap(struct work_struct
*unused
);
249 static int slab_early_init
= 1;
251 #define INDEX_AC kmalloc_index(sizeof(struct arraycache_init))
252 #define INDEX_NODE kmalloc_index(sizeof(struct kmem_cache_node))
254 static void kmem_cache_node_init(struct kmem_cache_node
*parent
)
256 INIT_LIST_HEAD(&parent
->slabs_full
);
257 INIT_LIST_HEAD(&parent
->slabs_partial
);
258 INIT_LIST_HEAD(&parent
->slabs_free
);
259 parent
->shared
= NULL
;
260 parent
->alien
= NULL
;
261 parent
->colour_next
= 0;
262 spin_lock_init(&parent
->list_lock
);
263 parent
->free_objects
= 0;
264 parent
->free_touched
= 0;
267 #define MAKE_LIST(cachep, listp, slab, nodeid) \
269 INIT_LIST_HEAD(listp); \
270 list_splice(&get_node(cachep, nodeid)->slab, listp); \
273 #define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
275 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
276 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
277 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
280 #define CFLGS_OFF_SLAB (0x80000000UL)
281 #define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
283 #define BATCHREFILL_LIMIT 16
285 * Optimization question: fewer reaps means less probability for unnessary
286 * cpucache drain/refill cycles.
288 * OTOH the cpuarrays can contain lots of objects,
289 * which could lock up otherwise freeable slabs.
291 #define REAPTIMEOUT_AC (2*HZ)
292 #define REAPTIMEOUT_NODE (4*HZ)
295 #define STATS_INC_ACTIVE(x) ((x)->num_active++)
296 #define STATS_DEC_ACTIVE(x) ((x)->num_active--)
297 #define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
298 #define STATS_INC_GROWN(x) ((x)->grown++)
299 #define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
300 #define STATS_SET_HIGH(x) \
302 if ((x)->num_active > (x)->high_mark) \
303 (x)->high_mark = (x)->num_active; \
305 #define STATS_INC_ERR(x) ((x)->errors++)
306 #define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
307 #define STATS_INC_NODEFREES(x) ((x)->node_frees++)
308 #define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
309 #define STATS_SET_FREEABLE(x, i) \
311 if ((x)->max_freeable < i) \
312 (x)->max_freeable = i; \
314 #define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
315 #define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
316 #define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
317 #define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
319 #define STATS_INC_ACTIVE(x) do { } while (0)
320 #define STATS_DEC_ACTIVE(x) do { } while (0)
321 #define STATS_INC_ALLOCED(x) do { } while (0)
322 #define STATS_INC_GROWN(x) do { } while (0)
323 #define STATS_ADD_REAPED(x,y) do { (void)(y); } while (0)
324 #define STATS_SET_HIGH(x) do { } while (0)
325 #define STATS_INC_ERR(x) do { } while (0)
326 #define STATS_INC_NODEALLOCS(x) do { } while (0)
327 #define STATS_INC_NODEFREES(x) do { } while (0)
328 #define STATS_INC_ACOVERFLOW(x) do { } while (0)
329 #define STATS_SET_FREEABLE(x, i) do { } while (0)
330 #define STATS_INC_ALLOCHIT(x) do { } while (0)
331 #define STATS_INC_ALLOCMISS(x) do { } while (0)
332 #define STATS_INC_FREEHIT(x) do { } while (0)
333 #define STATS_INC_FREEMISS(x) do { } while (0)
339 * memory layout of objects:
341 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
342 * the end of an object is aligned with the end of the real
343 * allocation. Catches writes behind the end of the allocation.
344 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
346 * cachep->obj_offset: The real object.
347 * cachep->size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
348 * cachep->size - 1* BYTES_PER_WORD: last caller address
349 * [BYTES_PER_WORD long]
351 static int obj_offset(struct kmem_cache
*cachep
)
353 return cachep
->obj_offset
;
356 static unsigned long long *dbg_redzone1(struct kmem_cache
*cachep
, void *objp
)
358 BUG_ON(!(cachep
->flags
& SLAB_RED_ZONE
));
359 return (unsigned long long*) (objp
+ obj_offset(cachep
) -
360 sizeof(unsigned long long));
363 static unsigned long long *dbg_redzone2(struct kmem_cache
*cachep
, void *objp
)
365 BUG_ON(!(cachep
->flags
& SLAB_RED_ZONE
));
366 if (cachep
->flags
& SLAB_STORE_USER
)
367 return (unsigned long long *)(objp
+ cachep
->size
-
368 sizeof(unsigned long long) -
370 return (unsigned long long *) (objp
+ cachep
->size
-
371 sizeof(unsigned long long));
374 static void **dbg_userword(struct kmem_cache
*cachep
, void *objp
)
376 BUG_ON(!(cachep
->flags
& SLAB_STORE_USER
));
377 return (void **)(objp
+ cachep
->size
- BYTES_PER_WORD
);
382 #define obj_offset(x) 0
383 #define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
384 #define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
385 #define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
389 #define OBJECT_FREE (0)
390 #define OBJECT_ACTIVE (1)
392 #ifdef CONFIG_DEBUG_SLAB_LEAK
394 static void set_obj_status(struct page
*page
, int idx
, int val
)
398 struct kmem_cache
*cachep
= page
->slab_cache
;
400 freelist_size
= cachep
->num
* sizeof(freelist_idx_t
);
401 status
= (char *)page
->freelist
+ freelist_size
;
405 static inline unsigned int get_obj_status(struct page
*page
, int idx
)
409 struct kmem_cache
*cachep
= page
->slab_cache
;
411 freelist_size
= cachep
->num
* sizeof(freelist_idx_t
);
412 status
= (char *)page
->freelist
+ freelist_size
;
418 static inline void set_obj_status(struct page
*page
, int idx
, int val
) {}
423 * Do not go above this order unless 0 objects fit into the slab or
424 * overridden on the command line.
426 #define SLAB_MAX_ORDER_HI 1
427 #define SLAB_MAX_ORDER_LO 0
428 static int slab_max_order
= SLAB_MAX_ORDER_LO
;
429 static bool slab_max_order_set __initdata
;
431 static inline struct kmem_cache
*virt_to_cache(const void *obj
)
433 struct page
*page
= virt_to_head_page(obj
);
434 return page
->slab_cache
;
437 static inline void *index_to_obj(struct kmem_cache
*cache
, struct page
*page
,
440 return page
->s_mem
+ cache
->size
* idx
;
444 * We want to avoid an expensive divide : (offset / cache->size)
445 * Using the fact that size is a constant for a particular cache,
446 * we can replace (offset / cache->size) by
447 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
449 static inline unsigned int obj_to_index(const struct kmem_cache
*cache
,
450 const struct page
*page
, void *obj
)
452 u32 offset
= (obj
- page
->s_mem
);
453 return reciprocal_divide(offset
, cache
->reciprocal_buffer_size
);
456 static struct arraycache_init initarray_generic
=
457 { {0, BOOT_CPUCACHE_ENTRIES
, 1, 0} };
459 /* internal cache of cache description objs */
460 static struct kmem_cache kmem_cache_boot
= {
462 .limit
= BOOT_CPUCACHE_ENTRIES
,
464 .size
= sizeof(struct kmem_cache
),
465 .name
= "kmem_cache",
468 #define BAD_ALIEN_MAGIC 0x01020304ul
470 #ifdef CONFIG_LOCKDEP
473 * Slab sometimes uses the kmalloc slabs to store the slab headers
474 * for other slabs "off slab".
475 * The locking for this is tricky in that it nests within the locks
476 * of all other slabs in a few places; to deal with this special
477 * locking we put on-slab caches into a separate lock-class.
479 * We set lock class for alien array caches which are up during init.
480 * The lock annotation will be lost if all cpus of a node goes down and
481 * then comes back up during hotplug
483 static struct lock_class_key on_slab_l3_key
;
484 static struct lock_class_key on_slab_alc_key
;
486 static struct lock_class_key debugobj_l3_key
;
487 static struct lock_class_key debugobj_alc_key
;
489 static void slab_set_lock_classes(struct kmem_cache
*cachep
,
490 struct lock_class_key
*l3_key
, struct lock_class_key
*alc_key
,
491 struct kmem_cache_node
*n
)
493 struct array_cache
**alc
;
496 lockdep_set_class(&n
->list_lock
, l3_key
);
499 * FIXME: This check for BAD_ALIEN_MAGIC
500 * should go away when common slab code is taught to
501 * work even without alien caches.
502 * Currently, non NUMA code returns BAD_ALIEN_MAGIC
503 * for alloc_alien_cache,
505 if (!alc
|| (unsigned long)alc
== BAD_ALIEN_MAGIC
)
509 lockdep_set_class(&alc
[r
]->lock
, alc_key
);
513 static void slab_set_debugobj_lock_classes_node(struct kmem_cache
*cachep
,
514 struct kmem_cache_node
*n
)
516 slab_set_lock_classes(cachep
, &debugobj_l3_key
, &debugobj_alc_key
, n
);
519 static void slab_set_debugobj_lock_classes(struct kmem_cache
*cachep
)
522 struct kmem_cache_node
*n
;
524 for_each_kmem_cache_node(cachep
, node
, n
)
525 slab_set_debugobj_lock_classes_node(cachep
, n
);
528 static void init_node_lock_keys(int q
)
535 for (i
= 1; i
<= KMALLOC_SHIFT_HIGH
; i
++) {
536 struct kmem_cache_node
*n
;
537 struct kmem_cache
*cache
= kmalloc_caches
[i
];
542 n
= get_node(cache
, q
);
543 if (!n
|| OFF_SLAB(cache
))
546 slab_set_lock_classes(cache
, &on_slab_l3_key
,
547 &on_slab_alc_key
, n
);
551 static void on_slab_lock_classes_node(struct kmem_cache
*cachep
,
552 struct kmem_cache_node
*n
)
554 slab_set_lock_classes(cachep
, &on_slab_l3_key
,
555 &on_slab_alc_key
, n
);
558 static inline void on_slab_lock_classes(struct kmem_cache
*cachep
)
561 struct kmem_cache_node
*n
;
563 VM_BUG_ON(OFF_SLAB(cachep
));
564 for_each_kmem_cache_node(cachep
, node
, n
)
565 on_slab_lock_classes_node(cachep
, n
);
568 static inline void __init
init_lock_keys(void)
573 init_node_lock_keys(node
);
576 static void __init
init_node_lock_keys(int q
)
580 static inline void init_lock_keys(void)
584 static inline void on_slab_lock_classes(struct kmem_cache
*cachep
)
588 static inline void on_slab_lock_classes_node(struct kmem_cache
*cachep
,
589 struct kmem_cache_node
*n
)
593 static void slab_set_debugobj_lock_classes_node(struct kmem_cache
*cachep
,
594 struct kmem_cache_node
*n
)
598 static void slab_set_debugobj_lock_classes(struct kmem_cache
*cachep
)
603 static DEFINE_PER_CPU(struct delayed_work
, slab_reap_work
);
605 static inline struct array_cache
*cpu_cache_get(struct kmem_cache
*cachep
)
607 return cachep
->array
[smp_processor_id()];
610 static size_t calculate_freelist_size(int nr_objs
, size_t align
)
612 size_t freelist_size
;
614 freelist_size
= nr_objs
* sizeof(freelist_idx_t
);
615 if (IS_ENABLED(CONFIG_DEBUG_SLAB_LEAK
))
616 freelist_size
+= nr_objs
* sizeof(char);
619 freelist_size
= ALIGN(freelist_size
, align
);
621 return freelist_size
;
624 static int calculate_nr_objs(size_t slab_size
, size_t buffer_size
,
625 size_t idx_size
, size_t align
)
628 size_t remained_size
;
629 size_t freelist_size
;
632 if (IS_ENABLED(CONFIG_DEBUG_SLAB_LEAK
))
633 extra_space
= sizeof(char);
635 * Ignore padding for the initial guess. The padding
636 * is at most @align-1 bytes, and @buffer_size is at
637 * least @align. In the worst case, this result will
638 * be one greater than the number of objects that fit
639 * into the memory allocation when taking the padding
642 nr_objs
= slab_size
/ (buffer_size
+ idx_size
+ extra_space
);
645 * This calculated number will be either the right
646 * amount, or one greater than what we want.
648 remained_size
= slab_size
- nr_objs
* buffer_size
;
649 freelist_size
= calculate_freelist_size(nr_objs
, align
);
650 if (remained_size
< freelist_size
)
657 * Calculate the number of objects and left-over bytes for a given buffer size.
659 static void cache_estimate(unsigned long gfporder
, size_t buffer_size
,
660 size_t align
, int flags
, size_t *left_over
,
665 size_t slab_size
= PAGE_SIZE
<< gfporder
;
668 * The slab management structure can be either off the slab or
669 * on it. For the latter case, the memory allocated for a
672 * - One unsigned int for each object
673 * - Padding to respect alignment of @align
674 * - @buffer_size bytes for each object
676 * If the slab management structure is off the slab, then the
677 * alignment will already be calculated into the size. Because
678 * the slabs are all pages aligned, the objects will be at the
679 * correct alignment when allocated.
681 if (flags
& CFLGS_OFF_SLAB
) {
683 nr_objs
= slab_size
/ buffer_size
;
686 nr_objs
= calculate_nr_objs(slab_size
, buffer_size
,
687 sizeof(freelist_idx_t
), align
);
688 mgmt_size
= calculate_freelist_size(nr_objs
, align
);
691 *left_over
= slab_size
- nr_objs
*buffer_size
- mgmt_size
;
695 #define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
697 static void __slab_error(const char *function
, struct kmem_cache
*cachep
,
700 printk(KERN_ERR
"slab error in %s(): cache `%s': %s\n",
701 function
, cachep
->name
, msg
);
703 add_taint(TAINT_BAD_PAGE
, LOCKDEP_NOW_UNRELIABLE
);
708 * By default on NUMA we use alien caches to stage the freeing of
709 * objects allocated from other nodes. This causes massive memory
710 * inefficiencies when using fake NUMA setup to split memory into a
711 * large number of small nodes, so it can be disabled on the command
715 static int use_alien_caches __read_mostly
= 1;
716 static int __init
noaliencache_setup(char *s
)
718 use_alien_caches
= 0;
721 __setup("noaliencache", noaliencache_setup
);
723 static int __init
slab_max_order_setup(char *str
)
725 get_option(&str
, &slab_max_order
);
726 slab_max_order
= slab_max_order
< 0 ? 0 :
727 min(slab_max_order
, MAX_ORDER
- 1);
728 slab_max_order_set
= true;
732 __setup("slab_max_order=", slab_max_order_setup
);
736 * Special reaping functions for NUMA systems called from cache_reap().
737 * These take care of doing round robin flushing of alien caches (containing
738 * objects freed on different nodes from which they were allocated) and the
739 * flushing of remote pcps by calling drain_node_pages.
741 static DEFINE_PER_CPU(unsigned long, slab_reap_node
);
743 static void init_reap_node(int cpu
)
747 node
= next_node(cpu_to_mem(cpu
), node_online_map
);
748 if (node
== MAX_NUMNODES
)
749 node
= first_node(node_online_map
);
751 per_cpu(slab_reap_node
, cpu
) = node
;
754 static void next_reap_node(void)
756 int node
= __this_cpu_read(slab_reap_node
);
758 node
= next_node(node
, node_online_map
);
759 if (unlikely(node
>= MAX_NUMNODES
))
760 node
= first_node(node_online_map
);
761 __this_cpu_write(slab_reap_node
, node
);
765 #define init_reap_node(cpu) do { } while (0)
766 #define next_reap_node(void) do { } while (0)
770 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
771 * via the workqueue/eventd.
772 * Add the CPU number into the expiration time to minimize the possibility of
773 * the CPUs getting into lockstep and contending for the global cache chain
776 static void start_cpu_timer(int cpu
)
778 struct delayed_work
*reap_work
= &per_cpu(slab_reap_work
, cpu
);
781 * When this gets called from do_initcalls via cpucache_init(),
782 * init_workqueues() has already run, so keventd will be setup
785 if (keventd_up() && reap_work
->work
.func
== NULL
) {
787 INIT_DEFERRABLE_WORK(reap_work
, cache_reap
);
788 schedule_delayed_work_on(cpu
, reap_work
,
789 __round_jiffies_relative(HZ
, cpu
));
793 static struct array_cache
*alloc_arraycache(int node
, int entries
,
794 int batchcount
, gfp_t gfp
)
796 int memsize
= sizeof(void *) * entries
+ sizeof(struct array_cache
);
797 struct array_cache
*nc
= NULL
;
799 nc
= kmalloc_node(memsize
, gfp
, node
);
801 * The array_cache structures contain pointers to free object.
802 * However, when such objects are allocated or transferred to another
803 * cache the pointers are not cleared and they could be counted as
804 * valid references during a kmemleak scan. Therefore, kmemleak must
805 * not scan such objects.
807 kmemleak_no_scan(nc
);
811 nc
->batchcount
= batchcount
;
813 spin_lock_init(&nc
->lock
);
818 static inline bool is_slab_pfmemalloc(struct page
*page
)
820 return PageSlabPfmemalloc(page
);
823 /* Clears pfmemalloc_active if no slabs have pfmalloc set */
824 static void recheck_pfmemalloc_active(struct kmem_cache
*cachep
,
825 struct array_cache
*ac
)
827 struct kmem_cache_node
*n
= get_node(cachep
, numa_mem_id());
831 if (!pfmemalloc_active
)
834 spin_lock_irqsave(&n
->list_lock
, flags
);
835 list_for_each_entry(page
, &n
->slabs_full
, lru
)
836 if (is_slab_pfmemalloc(page
))
839 list_for_each_entry(page
, &n
->slabs_partial
, lru
)
840 if (is_slab_pfmemalloc(page
))
843 list_for_each_entry(page
, &n
->slabs_free
, lru
)
844 if (is_slab_pfmemalloc(page
))
847 pfmemalloc_active
= false;
849 spin_unlock_irqrestore(&n
->list_lock
, flags
);
852 static void *__ac_get_obj(struct kmem_cache
*cachep
, struct array_cache
*ac
,
853 gfp_t flags
, bool force_refill
)
856 void *objp
= ac
->entry
[--ac
->avail
];
858 /* Ensure the caller is allowed to use objects from PFMEMALLOC slab */
859 if (unlikely(is_obj_pfmemalloc(objp
))) {
860 struct kmem_cache_node
*n
;
862 if (gfp_pfmemalloc_allowed(flags
)) {
863 clear_obj_pfmemalloc(&objp
);
867 /* The caller cannot use PFMEMALLOC objects, find another one */
868 for (i
= 0; i
< ac
->avail
; i
++) {
869 /* If a !PFMEMALLOC object is found, swap them */
870 if (!is_obj_pfmemalloc(ac
->entry
[i
])) {
872 ac
->entry
[i
] = ac
->entry
[ac
->avail
];
873 ac
->entry
[ac
->avail
] = objp
;
879 * If there are empty slabs on the slabs_free list and we are
880 * being forced to refill the cache, mark this one !pfmemalloc.
882 n
= get_node(cachep
, numa_mem_id());
883 if (!list_empty(&n
->slabs_free
) && force_refill
) {
884 struct page
*page
= virt_to_head_page(objp
);
885 ClearPageSlabPfmemalloc(page
);
886 clear_obj_pfmemalloc(&objp
);
887 recheck_pfmemalloc_active(cachep
, ac
);
891 /* No !PFMEMALLOC objects available */
899 static inline void *ac_get_obj(struct kmem_cache
*cachep
,
900 struct array_cache
*ac
, gfp_t flags
, bool force_refill
)
904 if (unlikely(sk_memalloc_socks()))
905 objp
= __ac_get_obj(cachep
, ac
, flags
, force_refill
);
907 objp
= ac
->entry
[--ac
->avail
];
912 static void *__ac_put_obj(struct kmem_cache
*cachep
, struct array_cache
*ac
,
915 if (unlikely(pfmemalloc_active
)) {
916 /* Some pfmemalloc slabs exist, check if this is one */
917 struct page
*page
= virt_to_head_page(objp
);
918 if (PageSlabPfmemalloc(page
))
919 set_obj_pfmemalloc(&objp
);
925 static inline void ac_put_obj(struct kmem_cache
*cachep
, struct array_cache
*ac
,
928 if (unlikely(sk_memalloc_socks()))
929 objp
= __ac_put_obj(cachep
, ac
, objp
);
931 ac
->entry
[ac
->avail
++] = objp
;
935 * Transfer objects in one arraycache to another.
936 * Locking must be handled by the caller.
938 * Return the number of entries transferred.
940 static int transfer_objects(struct array_cache
*to
,
941 struct array_cache
*from
, unsigned int max
)
943 /* Figure out how many entries to transfer */
944 int nr
= min3(from
->avail
, max
, to
->limit
- to
->avail
);
949 memcpy(to
->entry
+ to
->avail
, from
->entry
+ from
->avail
-nr
,
959 #define drain_alien_cache(cachep, alien) do { } while (0)
960 #define reap_alien(cachep, n) do { } while (0)
962 static inline struct array_cache
**alloc_alien_cache(int node
, int limit
, gfp_t gfp
)
964 return (struct array_cache
**)BAD_ALIEN_MAGIC
;
967 static inline void free_alien_cache(struct array_cache
**ac_ptr
)
971 static inline int cache_free_alien(struct kmem_cache
*cachep
, void *objp
)
976 static inline void *alternate_node_alloc(struct kmem_cache
*cachep
,
982 static inline void *____cache_alloc_node(struct kmem_cache
*cachep
,
983 gfp_t flags
, int nodeid
)
988 #else /* CONFIG_NUMA */
990 static void *____cache_alloc_node(struct kmem_cache
*, gfp_t
, int);
991 static void *alternate_node_alloc(struct kmem_cache
*, gfp_t
);
993 static struct array_cache
**alloc_alien_cache(int node
, int limit
, gfp_t gfp
)
995 struct array_cache
**ac_ptr
;
996 int memsize
= sizeof(void *) * nr_node_ids
;
1001 ac_ptr
= kzalloc_node(memsize
, gfp
, node
);
1004 if (i
== node
|| !node_online(i
))
1006 ac_ptr
[i
] = alloc_arraycache(node
, limit
, 0xbaadf00d, gfp
);
1008 for (i
--; i
>= 0; i
--)
1018 static void free_alien_cache(struct array_cache
**ac_ptr
)
1029 static void __drain_alien_cache(struct kmem_cache
*cachep
,
1030 struct array_cache
*ac
, int node
)
1032 struct kmem_cache_node
*n
= get_node(cachep
, node
);
1035 spin_lock(&n
->list_lock
);
1037 * Stuff objects into the remote nodes shared array first.
1038 * That way we could avoid the overhead of putting the objects
1039 * into the free lists and getting them back later.
1042 transfer_objects(n
->shared
, ac
, ac
->limit
);
1044 free_block(cachep
, ac
->entry
, ac
->avail
, node
);
1046 spin_unlock(&n
->list_lock
);
1051 * Called from cache_reap() to regularly drain alien caches round robin.
1053 static void reap_alien(struct kmem_cache
*cachep
, struct kmem_cache_node
*n
)
1055 int node
= __this_cpu_read(slab_reap_node
);
1058 struct array_cache
*ac
= n
->alien
[node
];
1060 if (ac
&& ac
->avail
&& spin_trylock_irq(&ac
->lock
)) {
1061 __drain_alien_cache(cachep
, ac
, node
);
1062 spin_unlock_irq(&ac
->lock
);
1067 static void drain_alien_cache(struct kmem_cache
*cachep
,
1068 struct array_cache
**alien
)
1071 struct array_cache
*ac
;
1072 unsigned long flags
;
1074 for_each_online_node(i
) {
1077 spin_lock_irqsave(&ac
->lock
, flags
);
1078 __drain_alien_cache(cachep
, ac
, i
);
1079 spin_unlock_irqrestore(&ac
->lock
, flags
);
1084 static inline int cache_free_alien(struct kmem_cache
*cachep
, void *objp
)
1086 int nodeid
= page_to_nid(virt_to_page(objp
));
1087 struct kmem_cache_node
*n
;
1088 struct array_cache
*alien
= NULL
;
1091 node
= numa_mem_id();
1094 * Make sure we are not freeing a object from another node to the array
1095 * cache on this cpu.
1097 if (likely(nodeid
== node
))
1100 n
= get_node(cachep
, node
);
1101 STATS_INC_NODEFREES(cachep
);
1102 if (n
->alien
&& n
->alien
[nodeid
]) {
1103 alien
= n
->alien
[nodeid
];
1104 spin_lock(&alien
->lock
);
1105 if (unlikely(alien
->avail
== alien
->limit
)) {
1106 STATS_INC_ACOVERFLOW(cachep
);
1107 __drain_alien_cache(cachep
, alien
, nodeid
);
1109 ac_put_obj(cachep
, alien
, objp
);
1110 spin_unlock(&alien
->lock
);
1112 n
= get_node(cachep
, nodeid
);
1113 spin_lock(&n
->list_lock
);
1114 free_block(cachep
, &objp
, 1, nodeid
);
1115 spin_unlock(&n
->list_lock
);
1122 * Allocates and initializes node for a node on each slab cache, used for
1123 * either memory or cpu hotplug. If memory is being hot-added, the kmem_cache_node
1124 * will be allocated off-node since memory is not yet online for the new node.
1125 * When hotplugging memory or a cpu, existing node are not replaced if
1128 * Must hold slab_mutex.
1130 static int init_cache_node_node(int node
)
1132 struct kmem_cache
*cachep
;
1133 struct kmem_cache_node
*n
;
1134 const int memsize
= sizeof(struct kmem_cache_node
);
1136 list_for_each_entry(cachep
, &slab_caches
, list
) {
1138 * Set up the kmem_cache_node for cpu before we can
1139 * begin anything. Make sure some other cpu on this
1140 * node has not already allocated this
1142 n
= get_node(cachep
, node
);
1144 n
= kmalloc_node(memsize
, GFP_KERNEL
, node
);
1147 kmem_cache_node_init(n
);
1148 n
->next_reap
= jiffies
+ REAPTIMEOUT_NODE
+
1149 ((unsigned long)cachep
) % REAPTIMEOUT_NODE
;
1152 * The kmem_cache_nodes don't come and go as CPUs
1153 * come and go. slab_mutex is sufficient
1156 cachep
->node
[node
] = n
;
1159 spin_lock_irq(&n
->list_lock
);
1161 (1 + nr_cpus_node(node
)) *
1162 cachep
->batchcount
+ cachep
->num
;
1163 spin_unlock_irq(&n
->list_lock
);
1168 static inline int slabs_tofree(struct kmem_cache
*cachep
,
1169 struct kmem_cache_node
*n
)
1171 return (n
->free_objects
+ cachep
->num
- 1) / cachep
->num
;
1174 static void cpuup_canceled(long cpu
)
1176 struct kmem_cache
*cachep
;
1177 struct kmem_cache_node
*n
= NULL
;
1178 int node
= cpu_to_mem(cpu
);
1179 const struct cpumask
*mask
= cpumask_of_node(node
);
1181 list_for_each_entry(cachep
, &slab_caches
, list
) {
1182 struct array_cache
*nc
;
1183 struct array_cache
*shared
;
1184 struct array_cache
**alien
;
1186 /* cpu is dead; no one can alloc from it. */
1187 nc
= cachep
->array
[cpu
];
1188 cachep
->array
[cpu
] = NULL
;
1189 n
= get_node(cachep
, node
);
1192 goto free_array_cache
;
1194 spin_lock_irq(&n
->list_lock
);
1196 /* Free limit for this kmem_cache_node */
1197 n
->free_limit
-= cachep
->batchcount
;
1199 free_block(cachep
, nc
->entry
, nc
->avail
, node
);
1201 if (!cpumask_empty(mask
)) {
1202 spin_unlock_irq(&n
->list_lock
);
1203 goto free_array_cache
;
1208 free_block(cachep
, shared
->entry
,
1209 shared
->avail
, node
);
1216 spin_unlock_irq(&n
->list_lock
);
1220 drain_alien_cache(cachep
, alien
);
1221 free_alien_cache(alien
);
1227 * In the previous loop, all the objects were freed to
1228 * the respective cache's slabs, now we can go ahead and
1229 * shrink each nodelist to its limit.
1231 list_for_each_entry(cachep
, &slab_caches
, list
) {
1232 n
= get_node(cachep
, node
);
1235 drain_freelist(cachep
, n
, slabs_tofree(cachep
, n
));
1239 static int cpuup_prepare(long cpu
)
1241 struct kmem_cache
*cachep
;
1242 struct kmem_cache_node
*n
= NULL
;
1243 int node
= cpu_to_mem(cpu
);
1247 * We need to do this right in the beginning since
1248 * alloc_arraycache's are going to use this list.
1249 * kmalloc_node allows us to add the slab to the right
1250 * kmem_cache_node and not this cpu's kmem_cache_node
1252 err
= init_cache_node_node(node
);
1257 * Now we can go ahead with allocating the shared arrays and
1260 list_for_each_entry(cachep
, &slab_caches
, list
) {
1261 struct array_cache
*nc
;
1262 struct array_cache
*shared
= NULL
;
1263 struct array_cache
**alien
= NULL
;
1265 nc
= alloc_arraycache(node
, cachep
->limit
,
1266 cachep
->batchcount
, GFP_KERNEL
);
1269 if (cachep
->shared
) {
1270 shared
= alloc_arraycache(node
,
1271 cachep
->shared
* cachep
->batchcount
,
1272 0xbaadf00d, GFP_KERNEL
);
1278 if (use_alien_caches
) {
1279 alien
= alloc_alien_cache(node
, cachep
->limit
, GFP_KERNEL
);
1286 cachep
->array
[cpu
] = nc
;
1287 n
= get_node(cachep
, node
);
1290 spin_lock_irq(&n
->list_lock
);
1293 * We are serialised from CPU_DEAD or
1294 * CPU_UP_CANCELLED by the cpucontrol lock
1305 spin_unlock_irq(&n
->list_lock
);
1307 free_alien_cache(alien
);
1308 if (cachep
->flags
& SLAB_DEBUG_OBJECTS
)
1309 slab_set_debugobj_lock_classes_node(cachep
, n
);
1310 else if (!OFF_SLAB(cachep
) &&
1311 !(cachep
->flags
& SLAB_DESTROY_BY_RCU
))
1312 on_slab_lock_classes_node(cachep
, n
);
1314 init_node_lock_keys(node
);
1318 cpuup_canceled(cpu
);
1322 static int cpuup_callback(struct notifier_block
*nfb
,
1323 unsigned long action
, void *hcpu
)
1325 long cpu
= (long)hcpu
;
1329 case CPU_UP_PREPARE
:
1330 case CPU_UP_PREPARE_FROZEN
:
1331 mutex_lock(&slab_mutex
);
1332 err
= cpuup_prepare(cpu
);
1333 mutex_unlock(&slab_mutex
);
1336 case CPU_ONLINE_FROZEN
:
1337 start_cpu_timer(cpu
);
1339 #ifdef CONFIG_HOTPLUG_CPU
1340 case CPU_DOWN_PREPARE
:
1341 case CPU_DOWN_PREPARE_FROZEN
:
1343 * Shutdown cache reaper. Note that the slab_mutex is
1344 * held so that if cache_reap() is invoked it cannot do
1345 * anything expensive but will only modify reap_work
1346 * and reschedule the timer.
1348 cancel_delayed_work_sync(&per_cpu(slab_reap_work
, cpu
));
1349 /* Now the cache_reaper is guaranteed to be not running. */
1350 per_cpu(slab_reap_work
, cpu
).work
.func
= NULL
;
1352 case CPU_DOWN_FAILED
:
1353 case CPU_DOWN_FAILED_FROZEN
:
1354 start_cpu_timer(cpu
);
1357 case CPU_DEAD_FROZEN
:
1359 * Even if all the cpus of a node are down, we don't free the
1360 * kmem_cache_node of any cache. This to avoid a race between
1361 * cpu_down, and a kmalloc allocation from another cpu for
1362 * memory from the node of the cpu going down. The node
1363 * structure is usually allocated from kmem_cache_create() and
1364 * gets destroyed at kmem_cache_destroy().
1368 case CPU_UP_CANCELED
:
1369 case CPU_UP_CANCELED_FROZEN
:
1370 mutex_lock(&slab_mutex
);
1371 cpuup_canceled(cpu
);
1372 mutex_unlock(&slab_mutex
);
1375 return notifier_from_errno(err
);
1378 static struct notifier_block cpucache_notifier
= {
1379 &cpuup_callback
, NULL
, 0
1382 #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
1384 * Drains freelist for a node on each slab cache, used for memory hot-remove.
1385 * Returns -EBUSY if all objects cannot be drained so that the node is not
1388 * Must hold slab_mutex.
1390 static int __meminit
drain_cache_node_node(int node
)
1392 struct kmem_cache
*cachep
;
1395 list_for_each_entry(cachep
, &slab_caches
, list
) {
1396 struct kmem_cache_node
*n
;
1398 n
= get_node(cachep
, node
);
1402 drain_freelist(cachep
, n
, slabs_tofree(cachep
, n
));
1404 if (!list_empty(&n
->slabs_full
) ||
1405 !list_empty(&n
->slabs_partial
)) {
1413 static int __meminit
slab_memory_callback(struct notifier_block
*self
,
1414 unsigned long action
, void *arg
)
1416 struct memory_notify
*mnb
= arg
;
1420 nid
= mnb
->status_change_nid
;
1425 case MEM_GOING_ONLINE
:
1426 mutex_lock(&slab_mutex
);
1427 ret
= init_cache_node_node(nid
);
1428 mutex_unlock(&slab_mutex
);
1430 case MEM_GOING_OFFLINE
:
1431 mutex_lock(&slab_mutex
);
1432 ret
= drain_cache_node_node(nid
);
1433 mutex_unlock(&slab_mutex
);
1437 case MEM_CANCEL_ONLINE
:
1438 case MEM_CANCEL_OFFLINE
:
1442 return notifier_from_errno(ret
);
1444 #endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
1447 * swap the static kmem_cache_node with kmalloced memory
1449 static void __init
init_list(struct kmem_cache
*cachep
, struct kmem_cache_node
*list
,
1452 struct kmem_cache_node
*ptr
;
1454 ptr
= kmalloc_node(sizeof(struct kmem_cache_node
), GFP_NOWAIT
, nodeid
);
1457 memcpy(ptr
, list
, sizeof(struct kmem_cache_node
));
1459 * Do not assume that spinlocks can be initialized via memcpy:
1461 spin_lock_init(&ptr
->list_lock
);
1463 MAKE_ALL_LISTS(cachep
, ptr
, nodeid
);
1464 cachep
->node
[nodeid
] = ptr
;
1468 * For setting up all the kmem_cache_node for cache whose buffer_size is same as
1469 * size of kmem_cache_node.
1471 static void __init
set_up_node(struct kmem_cache
*cachep
, int index
)
1475 for_each_online_node(node
) {
1476 cachep
->node
[node
] = &init_kmem_cache_node
[index
+ node
];
1477 cachep
->node
[node
]->next_reap
= jiffies
+
1479 ((unsigned long)cachep
) % REAPTIMEOUT_NODE
;
1484 * The memory after the last cpu cache pointer is used for the
1487 static void setup_node_pointer(struct kmem_cache
*cachep
)
1489 cachep
->node
= (struct kmem_cache_node
**)&cachep
->array
[nr_cpu_ids
];
1493 * Initialisation. Called after the page allocator have been initialised and
1494 * before smp_init().
1496 void __init
kmem_cache_init(void)
1500 BUILD_BUG_ON(sizeof(((struct page
*)NULL
)->lru
) <
1501 sizeof(struct rcu_head
));
1502 kmem_cache
= &kmem_cache_boot
;
1503 setup_node_pointer(kmem_cache
);
1505 if (num_possible_nodes() == 1)
1506 use_alien_caches
= 0;
1508 for (i
= 0; i
< NUM_INIT_LISTS
; i
++)
1509 kmem_cache_node_init(&init_kmem_cache_node
[i
]);
1511 set_up_node(kmem_cache
, CACHE_CACHE
);
1514 * Fragmentation resistance on low memory - only use bigger
1515 * page orders on machines with more than 32MB of memory if
1516 * not overridden on the command line.
1518 if (!slab_max_order_set
&& totalram_pages
> (32 << 20) >> PAGE_SHIFT
)
1519 slab_max_order
= SLAB_MAX_ORDER_HI
;
1521 /* Bootstrap is tricky, because several objects are allocated
1522 * from caches that do not exist yet:
1523 * 1) initialize the kmem_cache cache: it contains the struct
1524 * kmem_cache structures of all caches, except kmem_cache itself:
1525 * kmem_cache is statically allocated.
1526 * Initially an __init data area is used for the head array and the
1527 * kmem_cache_node structures, it's replaced with a kmalloc allocated
1528 * array at the end of the bootstrap.
1529 * 2) Create the first kmalloc cache.
1530 * The struct kmem_cache for the new cache is allocated normally.
1531 * An __init data area is used for the head array.
1532 * 3) Create the remaining kmalloc caches, with minimally sized
1534 * 4) Replace the __init data head arrays for kmem_cache and the first
1535 * kmalloc cache with kmalloc allocated arrays.
1536 * 5) Replace the __init data for kmem_cache_node for kmem_cache and
1537 * the other cache's with kmalloc allocated memory.
1538 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
1541 /* 1) create the kmem_cache */
1544 * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
1546 create_boot_cache(kmem_cache
, "kmem_cache",
1547 offsetof(struct kmem_cache
, array
[nr_cpu_ids
]) +
1548 nr_node_ids
* sizeof(struct kmem_cache_node
*),
1549 SLAB_HWCACHE_ALIGN
);
1550 list_add(&kmem_cache
->list
, &slab_caches
);
1552 /* 2+3) create the kmalloc caches */
1555 * Initialize the caches that provide memory for the array cache and the
1556 * kmem_cache_node structures first. Without this, further allocations will
1560 kmalloc_caches
[INDEX_AC
] = create_kmalloc_cache("kmalloc-ac",
1561 kmalloc_size(INDEX_AC
), ARCH_KMALLOC_FLAGS
);
1563 if (INDEX_AC
!= INDEX_NODE
)
1564 kmalloc_caches
[INDEX_NODE
] =
1565 create_kmalloc_cache("kmalloc-node",
1566 kmalloc_size(INDEX_NODE
), ARCH_KMALLOC_FLAGS
);
1568 slab_early_init
= 0;
1570 /* 4) Replace the bootstrap head arrays */
1572 struct array_cache
*ptr
;
1574 ptr
= kmalloc(sizeof(struct arraycache_init
), GFP_NOWAIT
);
1576 memcpy(ptr
, cpu_cache_get(kmem_cache
),
1577 sizeof(struct arraycache_init
));
1579 * Do not assume that spinlocks can be initialized via memcpy:
1581 spin_lock_init(&ptr
->lock
);
1583 kmem_cache
->array
[smp_processor_id()] = ptr
;
1585 ptr
= kmalloc(sizeof(struct arraycache_init
), GFP_NOWAIT
);
1587 BUG_ON(cpu_cache_get(kmalloc_caches
[INDEX_AC
])
1588 != &initarray_generic
.cache
);
1589 memcpy(ptr
, cpu_cache_get(kmalloc_caches
[INDEX_AC
]),
1590 sizeof(struct arraycache_init
));
1592 * Do not assume that spinlocks can be initialized via memcpy:
1594 spin_lock_init(&ptr
->lock
);
1596 kmalloc_caches
[INDEX_AC
]->array
[smp_processor_id()] = ptr
;
1598 /* 5) Replace the bootstrap kmem_cache_node */
1602 for_each_online_node(nid
) {
1603 init_list(kmem_cache
, &init_kmem_cache_node
[CACHE_CACHE
+ nid
], nid
);
1605 init_list(kmalloc_caches
[INDEX_AC
],
1606 &init_kmem_cache_node
[SIZE_AC
+ nid
], nid
);
1608 if (INDEX_AC
!= INDEX_NODE
) {
1609 init_list(kmalloc_caches
[INDEX_NODE
],
1610 &init_kmem_cache_node
[SIZE_NODE
+ nid
], nid
);
1615 create_kmalloc_caches(ARCH_KMALLOC_FLAGS
);
1618 void __init
kmem_cache_init_late(void)
1620 struct kmem_cache
*cachep
;
1624 /* 6) resize the head arrays to their final sizes */
1625 mutex_lock(&slab_mutex
);
1626 list_for_each_entry(cachep
, &slab_caches
, list
)
1627 if (enable_cpucache(cachep
, GFP_NOWAIT
))
1629 mutex_unlock(&slab_mutex
);
1631 /* Annotate slab for lockdep -- annotate the malloc caches */
1638 * Register a cpu startup notifier callback that initializes
1639 * cpu_cache_get for all new cpus
1641 register_cpu_notifier(&cpucache_notifier
);
1645 * Register a memory hotplug callback that initializes and frees
1648 hotplug_memory_notifier(slab_memory_callback
, SLAB_CALLBACK_PRI
);
1652 * The reap timers are started later, with a module init call: That part
1653 * of the kernel is not yet operational.
1657 static int __init
cpucache_init(void)
1662 * Register the timers that return unneeded pages to the page allocator
1664 for_each_online_cpu(cpu
)
1665 start_cpu_timer(cpu
);
1671 __initcall(cpucache_init
);
1673 static noinline
void
1674 slab_out_of_memory(struct kmem_cache
*cachep
, gfp_t gfpflags
, int nodeid
)
1677 struct kmem_cache_node
*n
;
1679 unsigned long flags
;
1681 static DEFINE_RATELIMIT_STATE(slab_oom_rs
, DEFAULT_RATELIMIT_INTERVAL
,
1682 DEFAULT_RATELIMIT_BURST
);
1684 if ((gfpflags
& __GFP_NOWARN
) || !__ratelimit(&slab_oom_rs
))
1688 "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n",
1690 printk(KERN_WARNING
" cache: %s, object size: %d, order: %d\n",
1691 cachep
->name
, cachep
->size
, cachep
->gfporder
);
1693 for_each_kmem_cache_node(cachep
, node
, n
) {
1694 unsigned long active_objs
= 0, num_objs
= 0, free_objects
= 0;
1695 unsigned long active_slabs
= 0, num_slabs
= 0;
1697 spin_lock_irqsave(&n
->list_lock
, flags
);
1698 list_for_each_entry(page
, &n
->slabs_full
, lru
) {
1699 active_objs
+= cachep
->num
;
1702 list_for_each_entry(page
, &n
->slabs_partial
, lru
) {
1703 active_objs
+= page
->active
;
1706 list_for_each_entry(page
, &n
->slabs_free
, lru
)
1709 free_objects
+= n
->free_objects
;
1710 spin_unlock_irqrestore(&n
->list_lock
, flags
);
1712 num_slabs
+= active_slabs
;
1713 num_objs
= num_slabs
* cachep
->num
;
1715 " node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
1716 node
, active_slabs
, num_slabs
, active_objs
, num_objs
,
1723 * Interface to system's page allocator. No need to hold the cache-lock.
1725 * If we requested dmaable memory, we will get it. Even if we
1726 * did not request dmaable memory, we might get it, but that
1727 * would be relatively rare and ignorable.
1729 static struct page
*kmem_getpages(struct kmem_cache
*cachep
, gfp_t flags
,
1735 flags
|= cachep
->allocflags
;
1736 if (cachep
->flags
& SLAB_RECLAIM_ACCOUNT
)
1737 flags
|= __GFP_RECLAIMABLE
;
1739 if (memcg_charge_slab(cachep
, flags
, cachep
->gfporder
))
1742 page
= alloc_pages_exact_node(nodeid
, flags
| __GFP_NOTRACK
, cachep
->gfporder
);
1744 memcg_uncharge_slab(cachep
, cachep
->gfporder
);
1745 slab_out_of_memory(cachep
, flags
, nodeid
);
1749 /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
1750 if (unlikely(page
->pfmemalloc
))
1751 pfmemalloc_active
= true;
1753 nr_pages
= (1 << cachep
->gfporder
);
1754 if (cachep
->flags
& SLAB_RECLAIM_ACCOUNT
)
1755 add_zone_page_state(page_zone(page
),
1756 NR_SLAB_RECLAIMABLE
, nr_pages
);
1758 add_zone_page_state(page_zone(page
),
1759 NR_SLAB_UNRECLAIMABLE
, nr_pages
);
1760 __SetPageSlab(page
);
1761 if (page
->pfmemalloc
)
1762 SetPageSlabPfmemalloc(page
);
1764 if (kmemcheck_enabled
&& !(cachep
->flags
& SLAB_NOTRACK
)) {
1765 kmemcheck_alloc_shadow(page
, cachep
->gfporder
, flags
, nodeid
);
1768 kmemcheck_mark_uninitialized_pages(page
, nr_pages
);
1770 kmemcheck_mark_unallocated_pages(page
, nr_pages
);
1777 * Interface to system's page release.
1779 static void kmem_freepages(struct kmem_cache
*cachep
, struct page
*page
)
1781 const unsigned long nr_freed
= (1 << cachep
->gfporder
);
1783 kmemcheck_free_shadow(page
, cachep
->gfporder
);
1785 if (cachep
->flags
& SLAB_RECLAIM_ACCOUNT
)
1786 sub_zone_page_state(page_zone(page
),
1787 NR_SLAB_RECLAIMABLE
, nr_freed
);
1789 sub_zone_page_state(page_zone(page
),
1790 NR_SLAB_UNRECLAIMABLE
, nr_freed
);
1792 BUG_ON(!PageSlab(page
));
1793 __ClearPageSlabPfmemalloc(page
);
1794 __ClearPageSlab(page
);
1795 page_mapcount_reset(page
);
1796 page
->mapping
= NULL
;
1798 if (current
->reclaim_state
)
1799 current
->reclaim_state
->reclaimed_slab
+= nr_freed
;
1800 __free_pages(page
, cachep
->gfporder
);
1801 memcg_uncharge_slab(cachep
, cachep
->gfporder
);
1804 static void kmem_rcu_free(struct rcu_head
*head
)
1806 struct kmem_cache
*cachep
;
1809 page
= container_of(head
, struct page
, rcu_head
);
1810 cachep
= page
->slab_cache
;
1812 kmem_freepages(cachep
, page
);
1817 #ifdef CONFIG_DEBUG_PAGEALLOC
1818 static void store_stackinfo(struct kmem_cache
*cachep
, unsigned long *addr
,
1819 unsigned long caller
)
1821 int size
= cachep
->object_size
;
1823 addr
= (unsigned long *)&((char *)addr
)[obj_offset(cachep
)];
1825 if (size
< 5 * sizeof(unsigned long))
1828 *addr
++ = 0x12345678;
1830 *addr
++ = smp_processor_id();
1831 size
-= 3 * sizeof(unsigned long);
1833 unsigned long *sptr
= &caller
;
1834 unsigned long svalue
;
1836 while (!kstack_end(sptr
)) {
1838 if (kernel_text_address(svalue
)) {
1840 size
-= sizeof(unsigned long);
1841 if (size
<= sizeof(unsigned long))
1847 *addr
++ = 0x87654321;
1851 static void poison_obj(struct kmem_cache
*cachep
, void *addr
, unsigned char val
)
1853 int size
= cachep
->object_size
;
1854 addr
= &((char *)addr
)[obj_offset(cachep
)];
1856 memset(addr
, val
, size
);
1857 *(unsigned char *)(addr
+ size
- 1) = POISON_END
;
1860 static void dump_line(char *data
, int offset
, int limit
)
1863 unsigned char error
= 0;
1866 printk(KERN_ERR
"%03x: ", offset
);
1867 for (i
= 0; i
< limit
; i
++) {
1868 if (data
[offset
+ i
] != POISON_FREE
) {
1869 error
= data
[offset
+ i
];
1873 print_hex_dump(KERN_CONT
, "", 0, 16, 1,
1874 &data
[offset
], limit
, 1);
1876 if (bad_count
== 1) {
1877 error
^= POISON_FREE
;
1878 if (!(error
& (error
- 1))) {
1879 printk(KERN_ERR
"Single bit error detected. Probably "
1882 printk(KERN_ERR
"Run memtest86+ or a similar memory "
1885 printk(KERN_ERR
"Run a memory test tool.\n");
1894 static void print_objinfo(struct kmem_cache
*cachep
, void *objp
, int lines
)
1899 if (cachep
->flags
& SLAB_RED_ZONE
) {
1900 printk(KERN_ERR
"Redzone: 0x%llx/0x%llx.\n",
1901 *dbg_redzone1(cachep
, objp
),
1902 *dbg_redzone2(cachep
, objp
));
1905 if (cachep
->flags
& SLAB_STORE_USER
) {
1906 printk(KERN_ERR
"Last user: [<%p>](%pSR)\n",
1907 *dbg_userword(cachep
, objp
),
1908 *dbg_userword(cachep
, objp
));
1910 realobj
= (char *)objp
+ obj_offset(cachep
);
1911 size
= cachep
->object_size
;
1912 for (i
= 0; i
< size
&& lines
; i
+= 16, lines
--) {
1915 if (i
+ limit
> size
)
1917 dump_line(realobj
, i
, limit
);
1921 static void check_poison_obj(struct kmem_cache
*cachep
, void *objp
)
1927 realobj
= (char *)objp
+ obj_offset(cachep
);
1928 size
= cachep
->object_size
;
1930 for (i
= 0; i
< size
; i
++) {
1931 char exp
= POISON_FREE
;
1934 if (realobj
[i
] != exp
) {
1940 "Slab corruption (%s): %s start=%p, len=%d\n",
1941 print_tainted(), cachep
->name
, realobj
, size
);
1942 print_objinfo(cachep
, objp
, 0);
1944 /* Hexdump the affected line */
1947 if (i
+ limit
> size
)
1949 dump_line(realobj
, i
, limit
);
1952 /* Limit to 5 lines */
1958 /* Print some data about the neighboring objects, if they
1961 struct page
*page
= virt_to_head_page(objp
);
1964 objnr
= obj_to_index(cachep
, page
, objp
);
1966 objp
= index_to_obj(cachep
, page
, objnr
- 1);
1967 realobj
= (char *)objp
+ obj_offset(cachep
);
1968 printk(KERN_ERR
"Prev obj: start=%p, len=%d\n",
1970 print_objinfo(cachep
, objp
, 2);
1972 if (objnr
+ 1 < cachep
->num
) {
1973 objp
= index_to_obj(cachep
, page
, objnr
+ 1);
1974 realobj
= (char *)objp
+ obj_offset(cachep
);
1975 printk(KERN_ERR
"Next obj: start=%p, len=%d\n",
1977 print_objinfo(cachep
, objp
, 2);
1984 static void slab_destroy_debugcheck(struct kmem_cache
*cachep
,
1988 for (i
= 0; i
< cachep
->num
; i
++) {
1989 void *objp
= index_to_obj(cachep
, page
, i
);
1991 if (cachep
->flags
& SLAB_POISON
) {
1992 #ifdef CONFIG_DEBUG_PAGEALLOC
1993 if (cachep
->size
% PAGE_SIZE
== 0 &&
1995 kernel_map_pages(virt_to_page(objp
),
1996 cachep
->size
/ PAGE_SIZE
, 1);
1998 check_poison_obj(cachep
, objp
);
2000 check_poison_obj(cachep
, objp
);
2003 if (cachep
->flags
& SLAB_RED_ZONE
) {
2004 if (*dbg_redzone1(cachep
, objp
) != RED_INACTIVE
)
2005 slab_error(cachep
, "start of a freed object "
2007 if (*dbg_redzone2(cachep
, objp
) != RED_INACTIVE
)
2008 slab_error(cachep
, "end of a freed object "
2014 static void slab_destroy_debugcheck(struct kmem_cache
*cachep
,
2021 * slab_destroy - destroy and release all objects in a slab
2022 * @cachep: cache pointer being destroyed
2023 * @page: page pointer being destroyed
2025 * Destroy all the objs in a slab, and release the mem back to the system.
2026 * Before calling the slab must have been unlinked from the cache. The
2027 * cache-lock is not held/needed.
2029 static void slab_destroy(struct kmem_cache
*cachep
, struct page
*page
)
2033 freelist
= page
->freelist
;
2034 slab_destroy_debugcheck(cachep
, page
);
2035 if (unlikely(cachep
->flags
& SLAB_DESTROY_BY_RCU
)) {
2036 struct rcu_head
*head
;
2039 * RCU free overloads the RCU head over the LRU.
2040 * slab_page has been overloeaded over the LRU,
2041 * however it is not used from now on so that
2042 * we can use it safely.
2044 head
= (void *)&page
->rcu_head
;
2045 call_rcu(head
, kmem_rcu_free
);
2048 kmem_freepages(cachep
, page
);
2052 * From now on, we don't use freelist
2053 * although actual page can be freed in rcu context
2055 if (OFF_SLAB(cachep
))
2056 kmem_cache_free(cachep
->freelist_cache
, freelist
);
2060 * calculate_slab_order - calculate size (page order) of slabs
2061 * @cachep: pointer to the cache that is being created
2062 * @size: size of objects to be created in this cache.
2063 * @align: required alignment for the objects.
2064 * @flags: slab allocation flags
2066 * Also calculates the number of objects per slab.
2068 * This could be made much more intelligent. For now, try to avoid using
2069 * high order pages for slabs. When the gfp() functions are more friendly
2070 * towards high-order requests, this should be changed.
2072 static size_t calculate_slab_order(struct kmem_cache
*cachep
,
2073 size_t size
, size_t align
, unsigned long flags
)
2075 unsigned long offslab_limit
;
2076 size_t left_over
= 0;
2079 for (gfporder
= 0; gfporder
<= KMALLOC_MAX_ORDER
; gfporder
++) {
2083 cache_estimate(gfporder
, size
, align
, flags
, &remainder
, &num
);
2087 /* Can't handle number of objects more than SLAB_OBJ_MAX_NUM */
2088 if (num
> SLAB_OBJ_MAX_NUM
)
2091 if (flags
& CFLGS_OFF_SLAB
) {
2092 size_t freelist_size_per_obj
= sizeof(freelist_idx_t
);
2094 * Max number of objs-per-slab for caches which
2095 * use off-slab slabs. Needed to avoid a possible
2096 * looping condition in cache_grow().
2098 if (IS_ENABLED(CONFIG_DEBUG_SLAB_LEAK
))
2099 freelist_size_per_obj
+= sizeof(char);
2100 offslab_limit
= size
;
2101 offslab_limit
/= freelist_size_per_obj
;
2103 if (num
> offslab_limit
)
2107 /* Found something acceptable - save it away */
2109 cachep
->gfporder
= gfporder
;
2110 left_over
= remainder
;
2113 * A VFS-reclaimable slab tends to have most allocations
2114 * as GFP_NOFS and we really don't want to have to be allocating
2115 * higher-order pages when we are unable to shrink dcache.
2117 if (flags
& SLAB_RECLAIM_ACCOUNT
)
2121 * Large number of objects is good, but very large slabs are
2122 * currently bad for the gfp()s.
2124 if (gfporder
>= slab_max_order
)
2128 * Acceptable internal fragmentation?
2130 if (left_over
* 8 <= (PAGE_SIZE
<< gfporder
))
2136 static int __init_refok
setup_cpu_cache(struct kmem_cache
*cachep
, gfp_t gfp
)
2138 if (slab_state
>= FULL
)
2139 return enable_cpucache(cachep
, gfp
);
2141 if (slab_state
== DOWN
) {
2143 * Note: Creation of first cache (kmem_cache).
2144 * The setup_node is taken care
2145 * of by the caller of __kmem_cache_create
2147 cachep
->array
[smp_processor_id()] = &initarray_generic
.cache
;
2148 slab_state
= PARTIAL
;
2149 } else if (slab_state
== PARTIAL
) {
2151 * Note: the second kmem_cache_create must create the cache
2152 * that's used by kmalloc(24), otherwise the creation of
2153 * further caches will BUG().
2155 cachep
->array
[smp_processor_id()] = &initarray_generic
.cache
;
2158 * If the cache that's used by kmalloc(sizeof(kmem_cache_node)) is
2159 * the second cache, then we need to set up all its node/,
2160 * otherwise the creation of further caches will BUG().
2162 set_up_node(cachep
, SIZE_AC
);
2163 if (INDEX_AC
== INDEX_NODE
)
2164 slab_state
= PARTIAL_NODE
;
2166 slab_state
= PARTIAL_ARRAYCACHE
;
2168 /* Remaining boot caches */
2169 cachep
->array
[smp_processor_id()] =
2170 kmalloc(sizeof(struct arraycache_init
), gfp
);
2172 if (slab_state
== PARTIAL_ARRAYCACHE
) {
2173 set_up_node(cachep
, SIZE_NODE
);
2174 slab_state
= PARTIAL_NODE
;
2177 for_each_online_node(node
) {
2178 cachep
->node
[node
] =
2179 kmalloc_node(sizeof(struct kmem_cache_node
),
2181 BUG_ON(!cachep
->node
[node
]);
2182 kmem_cache_node_init(cachep
->node
[node
]);
2186 cachep
->node
[numa_mem_id()]->next_reap
=
2187 jiffies
+ REAPTIMEOUT_NODE
+
2188 ((unsigned long)cachep
) % REAPTIMEOUT_NODE
;
2190 cpu_cache_get(cachep
)->avail
= 0;
2191 cpu_cache_get(cachep
)->limit
= BOOT_CPUCACHE_ENTRIES
;
2192 cpu_cache_get(cachep
)->batchcount
= 1;
2193 cpu_cache_get(cachep
)->touched
= 0;
2194 cachep
->batchcount
= 1;
2195 cachep
->limit
= BOOT_CPUCACHE_ENTRIES
;
2200 * __kmem_cache_create - Create a cache.
2201 * @cachep: cache management descriptor
2202 * @flags: SLAB flags
2204 * Returns a ptr to the cache on success, NULL on failure.
2205 * Cannot be called within a int, but can be interrupted.
2206 * The @ctor is run when new pages are allocated by the cache.
2210 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2211 * to catch references to uninitialised memory.
2213 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2214 * for buffer overruns.
2216 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2217 * cacheline. This can be beneficial if you're counting cycles as closely
2221 __kmem_cache_create (struct kmem_cache
*cachep
, unsigned long flags
)
2223 size_t left_over
, freelist_size
, ralign
;
2226 size_t size
= cachep
->size
;
2231 * Enable redzoning and last user accounting, except for caches with
2232 * large objects, if the increased size would increase the object size
2233 * above the next power of two: caches with object sizes just above a
2234 * power of two have a significant amount of internal fragmentation.
2236 if (size
< 4096 || fls(size
- 1) == fls(size
-1 + REDZONE_ALIGN
+
2237 2 * sizeof(unsigned long long)))
2238 flags
|= SLAB_RED_ZONE
| SLAB_STORE_USER
;
2239 if (!(flags
& SLAB_DESTROY_BY_RCU
))
2240 flags
|= SLAB_POISON
;
2242 if (flags
& SLAB_DESTROY_BY_RCU
)
2243 BUG_ON(flags
& SLAB_POISON
);
2247 * Check that size is in terms of words. This is needed to avoid
2248 * unaligned accesses for some archs when redzoning is used, and makes
2249 * sure any on-slab bufctl's are also correctly aligned.
2251 if (size
& (BYTES_PER_WORD
- 1)) {
2252 size
+= (BYTES_PER_WORD
- 1);
2253 size
&= ~(BYTES_PER_WORD
- 1);
2257 * Redzoning and user store require word alignment or possibly larger.
2258 * Note this will be overridden by architecture or caller mandated
2259 * alignment if either is greater than BYTES_PER_WORD.
2261 if (flags
& SLAB_STORE_USER
)
2262 ralign
= BYTES_PER_WORD
;
2264 if (flags
& SLAB_RED_ZONE
) {
2265 ralign
= REDZONE_ALIGN
;
2266 /* If redzoning, ensure that the second redzone is suitably
2267 * aligned, by adjusting the object size accordingly. */
2268 size
+= REDZONE_ALIGN
- 1;
2269 size
&= ~(REDZONE_ALIGN
- 1);
2272 /* 3) caller mandated alignment */
2273 if (ralign
< cachep
->align
) {
2274 ralign
= cachep
->align
;
2276 /* disable debug if necessary */
2277 if (ralign
> __alignof__(unsigned long long))
2278 flags
&= ~(SLAB_RED_ZONE
| SLAB_STORE_USER
);
2282 cachep
->align
= ralign
;
2284 if (slab_is_available())
2289 setup_node_pointer(cachep
);
2293 * Both debugging options require word-alignment which is calculated
2296 if (flags
& SLAB_RED_ZONE
) {
2297 /* add space for red zone words */
2298 cachep
->obj_offset
+= sizeof(unsigned long long);
2299 size
+= 2 * sizeof(unsigned long long);
2301 if (flags
& SLAB_STORE_USER
) {
2302 /* user store requires one word storage behind the end of
2303 * the real object. But if the second red zone needs to be
2304 * aligned to 64 bits, we must allow that much space.
2306 if (flags
& SLAB_RED_ZONE
)
2307 size
+= REDZONE_ALIGN
;
2309 size
+= BYTES_PER_WORD
;
2311 #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
2312 if (size
>= kmalloc_size(INDEX_NODE
+ 1)
2313 && cachep
->object_size
> cache_line_size()
2314 && ALIGN(size
, cachep
->align
) < PAGE_SIZE
) {
2315 cachep
->obj_offset
+= PAGE_SIZE
- ALIGN(size
, cachep
->align
);
2322 * Determine if the slab management is 'on' or 'off' slab.
2323 * (bootstrapping cannot cope with offslab caches so don't do
2324 * it too early on. Always use on-slab management when
2325 * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
2327 if ((size
>= (PAGE_SIZE
>> 5)) && !slab_early_init
&&
2328 !(flags
& SLAB_NOLEAKTRACE
))
2330 * Size is large, assume best to place the slab management obj
2331 * off-slab (should allow better packing of objs).
2333 flags
|= CFLGS_OFF_SLAB
;
2335 size
= ALIGN(size
, cachep
->align
);
2337 * We should restrict the number of objects in a slab to implement
2338 * byte sized index. Refer comment on SLAB_OBJ_MIN_SIZE definition.
2340 if (FREELIST_BYTE_INDEX
&& size
< SLAB_OBJ_MIN_SIZE
)
2341 size
= ALIGN(SLAB_OBJ_MIN_SIZE
, cachep
->align
);
2343 left_over
= calculate_slab_order(cachep
, size
, cachep
->align
, flags
);
2348 freelist_size
= calculate_freelist_size(cachep
->num
, cachep
->align
);
2351 * If the slab has been placed off-slab, and we have enough space then
2352 * move it on-slab. This is at the expense of any extra colouring.
2354 if (flags
& CFLGS_OFF_SLAB
&& left_over
>= freelist_size
) {
2355 flags
&= ~CFLGS_OFF_SLAB
;
2356 left_over
-= freelist_size
;
2359 if (flags
& CFLGS_OFF_SLAB
) {
2360 /* really off slab. No need for manual alignment */
2361 freelist_size
= calculate_freelist_size(cachep
->num
, 0);
2363 #ifdef CONFIG_PAGE_POISONING
2364 /* If we're going to use the generic kernel_map_pages()
2365 * poisoning, then it's going to smash the contents of
2366 * the redzone and userword anyhow, so switch them off.
2368 if (size
% PAGE_SIZE
== 0 && flags
& SLAB_POISON
)
2369 flags
&= ~(SLAB_RED_ZONE
| SLAB_STORE_USER
);
2373 cachep
->colour_off
= cache_line_size();
2374 /* Offset must be a multiple of the alignment. */
2375 if (cachep
->colour_off
< cachep
->align
)
2376 cachep
->colour_off
= cachep
->align
;
2377 cachep
->colour
= left_over
/ cachep
->colour_off
;
2378 cachep
->freelist_size
= freelist_size
;
2379 cachep
->flags
= flags
;
2380 cachep
->allocflags
= __GFP_COMP
;
2381 if (CONFIG_ZONE_DMA_FLAG
&& (flags
& SLAB_CACHE_DMA
))
2382 cachep
->allocflags
|= GFP_DMA
;
2383 cachep
->size
= size
;
2384 cachep
->reciprocal_buffer_size
= reciprocal_value(size
);
2386 if (flags
& CFLGS_OFF_SLAB
) {
2387 cachep
->freelist_cache
= kmalloc_slab(freelist_size
, 0u);
2389 * This is a possibility for one of the kmalloc_{dma,}_caches.
2390 * But since we go off slab only for object size greater than
2391 * PAGE_SIZE/8, and kmalloc_{dma,}_caches get created
2392 * in ascending order,this should not happen at all.
2393 * But leave a BUG_ON for some lucky dude.
2395 BUG_ON(ZERO_OR_NULL_PTR(cachep
->freelist_cache
));
2398 err
= setup_cpu_cache(cachep
, gfp
);
2400 __kmem_cache_shutdown(cachep
);
2404 if (flags
& SLAB_DEBUG_OBJECTS
) {
2406 * Would deadlock through slab_destroy()->call_rcu()->
2407 * debug_object_activate()->kmem_cache_alloc().
2409 WARN_ON_ONCE(flags
& SLAB_DESTROY_BY_RCU
);
2411 slab_set_debugobj_lock_classes(cachep
);
2412 } else if (!OFF_SLAB(cachep
) && !(flags
& SLAB_DESTROY_BY_RCU
))
2413 on_slab_lock_classes(cachep
);
2419 static void check_irq_off(void)
2421 BUG_ON(!irqs_disabled());
2424 static void check_irq_on(void)
2426 BUG_ON(irqs_disabled());
2429 static void check_spinlock_acquired(struct kmem_cache
*cachep
)
2433 assert_spin_locked(&get_node(cachep
, numa_mem_id())->list_lock
);
2437 static void check_spinlock_acquired_node(struct kmem_cache
*cachep
, int node
)
2441 assert_spin_locked(&get_node(cachep
, node
)->list_lock
);
2446 #define check_irq_off() do { } while(0)
2447 #define check_irq_on() do { } while(0)
2448 #define check_spinlock_acquired(x) do { } while(0)
2449 #define check_spinlock_acquired_node(x, y) do { } while(0)
2452 static void drain_array(struct kmem_cache
*cachep
, struct kmem_cache_node
*n
,
2453 struct array_cache
*ac
,
2454 int force
, int node
);
2456 static void do_drain(void *arg
)
2458 struct kmem_cache
*cachep
= arg
;
2459 struct array_cache
*ac
;
2460 int node
= numa_mem_id();
2461 struct kmem_cache_node
*n
;
2464 ac
= cpu_cache_get(cachep
);
2465 n
= get_node(cachep
, node
);
2466 spin_lock(&n
->list_lock
);
2467 free_block(cachep
, ac
->entry
, ac
->avail
, node
);
2468 spin_unlock(&n
->list_lock
);
2472 static void drain_cpu_caches(struct kmem_cache
*cachep
)
2474 struct kmem_cache_node
*n
;
2477 on_each_cpu(do_drain
, cachep
, 1);
2479 for_each_kmem_cache_node(cachep
, node
, n
)
2481 drain_alien_cache(cachep
, n
->alien
);
2483 for_each_kmem_cache_node(cachep
, node
, n
)
2484 drain_array(cachep
, n
, n
->shared
, 1, node
);
2488 * Remove slabs from the list of free slabs.
2489 * Specify the number of slabs to drain in tofree.
2491 * Returns the actual number of slabs released.
2493 static int drain_freelist(struct kmem_cache
*cache
,
2494 struct kmem_cache_node
*n
, int tofree
)
2496 struct list_head
*p
;
2501 while (nr_freed
< tofree
&& !list_empty(&n
->slabs_free
)) {
2503 spin_lock_irq(&n
->list_lock
);
2504 p
= n
->slabs_free
.prev
;
2505 if (p
== &n
->slabs_free
) {
2506 spin_unlock_irq(&n
->list_lock
);
2510 page
= list_entry(p
, struct page
, lru
);
2512 BUG_ON(page
->active
);
2514 list_del(&page
->lru
);
2516 * Safe to drop the lock. The slab is no longer linked
2519 n
->free_objects
-= cache
->num
;
2520 spin_unlock_irq(&n
->list_lock
);
2521 slab_destroy(cache
, page
);
2528 int __kmem_cache_shrink(struct kmem_cache
*cachep
)
2532 struct kmem_cache_node
*n
;
2534 drain_cpu_caches(cachep
);
2537 for_each_kmem_cache_node(cachep
, node
, n
) {
2538 drain_freelist(cachep
, n
, slabs_tofree(cachep
, n
));
2540 ret
+= !list_empty(&n
->slabs_full
) ||
2541 !list_empty(&n
->slabs_partial
);
2543 return (ret
? 1 : 0);
2546 int __kmem_cache_shutdown(struct kmem_cache
*cachep
)
2549 struct kmem_cache_node
*n
;
2550 int rc
= __kmem_cache_shrink(cachep
);
2555 for_each_online_cpu(i
)
2556 kfree(cachep
->array
[i
]);
2558 /* NUMA: free the node structures */
2559 for_each_kmem_cache_node(cachep
, i
, n
) {
2561 free_alien_cache(n
->alien
);
2563 cachep
->node
[i
] = NULL
;
2569 * Get the memory for a slab management obj.
2571 * For a slab cache when the slab descriptor is off-slab, the
2572 * slab descriptor can't come from the same cache which is being created,
2573 * Because if it is the case, that means we defer the creation of
2574 * the kmalloc_{dma,}_cache of size sizeof(slab descriptor) to this point.
2575 * And we eventually call down to __kmem_cache_create(), which
2576 * in turn looks up in the kmalloc_{dma,}_caches for the disired-size one.
2577 * This is a "chicken-and-egg" problem.
2579 * So the off-slab slab descriptor shall come from the kmalloc_{dma,}_caches,
2580 * which are all initialized during kmem_cache_init().
2582 static void *alloc_slabmgmt(struct kmem_cache
*cachep
,
2583 struct page
*page
, int colour_off
,
2584 gfp_t local_flags
, int nodeid
)
2587 void *addr
= page_address(page
);
2589 if (OFF_SLAB(cachep
)) {
2590 /* Slab management obj is off-slab. */
2591 freelist
= kmem_cache_alloc_node(cachep
->freelist_cache
,
2592 local_flags
, nodeid
);
2596 freelist
= addr
+ colour_off
;
2597 colour_off
+= cachep
->freelist_size
;
2600 page
->s_mem
= addr
+ colour_off
;
2604 static inline freelist_idx_t
get_free_obj(struct page
*page
, unsigned int idx
)
2606 return ((freelist_idx_t
*)page
->freelist
)[idx
];
2609 static inline void set_free_obj(struct page
*page
,
2610 unsigned int idx
, freelist_idx_t val
)
2612 ((freelist_idx_t
*)(page
->freelist
))[idx
] = val
;
2615 static void cache_init_objs(struct kmem_cache
*cachep
,
2620 for (i
= 0; i
< cachep
->num
; i
++) {
2621 void *objp
= index_to_obj(cachep
, page
, i
);
2623 /* need to poison the objs? */
2624 if (cachep
->flags
& SLAB_POISON
)
2625 poison_obj(cachep
, objp
, POISON_FREE
);
2626 if (cachep
->flags
& SLAB_STORE_USER
)
2627 *dbg_userword(cachep
, objp
) = NULL
;
2629 if (cachep
->flags
& SLAB_RED_ZONE
) {
2630 *dbg_redzone1(cachep
, objp
) = RED_INACTIVE
;
2631 *dbg_redzone2(cachep
, objp
) = RED_INACTIVE
;
2634 * Constructors are not allowed to allocate memory from the same
2635 * cache which they are a constructor for. Otherwise, deadlock.
2636 * They must also be threaded.
2638 if (cachep
->ctor
&& !(cachep
->flags
& SLAB_POISON
))
2639 cachep
->ctor(objp
+ obj_offset(cachep
));
2641 if (cachep
->flags
& SLAB_RED_ZONE
) {
2642 if (*dbg_redzone2(cachep
, objp
) != RED_INACTIVE
)
2643 slab_error(cachep
, "constructor overwrote the"
2644 " end of an object");
2645 if (*dbg_redzone1(cachep
, objp
) != RED_INACTIVE
)
2646 slab_error(cachep
, "constructor overwrote the"
2647 " start of an object");
2649 if ((cachep
->size
% PAGE_SIZE
) == 0 &&
2650 OFF_SLAB(cachep
) && cachep
->flags
& SLAB_POISON
)
2651 kernel_map_pages(virt_to_page(objp
),
2652 cachep
->size
/ PAGE_SIZE
, 0);
2657 set_obj_status(page
, i
, OBJECT_FREE
);
2658 set_free_obj(page
, i
, i
);
2662 static void kmem_flagcheck(struct kmem_cache
*cachep
, gfp_t flags
)
2664 if (CONFIG_ZONE_DMA_FLAG
) {
2665 if (flags
& GFP_DMA
)
2666 BUG_ON(!(cachep
->allocflags
& GFP_DMA
));
2668 BUG_ON(cachep
->allocflags
& GFP_DMA
);
2672 static void *slab_get_obj(struct kmem_cache
*cachep
, struct page
*page
,
2677 objp
= index_to_obj(cachep
, page
, get_free_obj(page
, page
->active
));
2680 WARN_ON(page_to_nid(virt_to_page(objp
)) != nodeid
);
2686 static void slab_put_obj(struct kmem_cache
*cachep
, struct page
*page
,
2687 void *objp
, int nodeid
)
2689 unsigned int objnr
= obj_to_index(cachep
, page
, objp
);
2693 /* Verify that the slab belongs to the intended node */
2694 WARN_ON(page_to_nid(virt_to_page(objp
)) != nodeid
);
2696 /* Verify double free bug */
2697 for (i
= page
->active
; i
< cachep
->num
; i
++) {
2698 if (get_free_obj(page
, i
) == objnr
) {
2699 printk(KERN_ERR
"slab: double free detected in cache "
2700 "'%s', objp %p\n", cachep
->name
, objp
);
2706 set_free_obj(page
, page
->active
, objnr
);
2710 * Map pages beginning at addr to the given cache and slab. This is required
2711 * for the slab allocator to be able to lookup the cache and slab of a
2712 * virtual address for kfree, ksize, and slab debugging.
2714 static void slab_map_pages(struct kmem_cache
*cache
, struct page
*page
,
2717 page
->slab_cache
= cache
;
2718 page
->freelist
= freelist
;
2722 * Grow (by 1) the number of slabs within a cache. This is called by
2723 * kmem_cache_alloc() when there are no active objs left in a cache.
2725 static int cache_grow(struct kmem_cache
*cachep
,
2726 gfp_t flags
, int nodeid
, struct page
*page
)
2731 struct kmem_cache_node
*n
;
2734 * Be lazy and only check for valid flags here, keeping it out of the
2735 * critical path in kmem_cache_alloc().
2737 BUG_ON(flags
& GFP_SLAB_BUG_MASK
);
2738 local_flags
= flags
& (GFP_CONSTRAINT_MASK
|GFP_RECLAIM_MASK
);
2740 /* Take the node list lock to change the colour_next on this node */
2742 n
= get_node(cachep
, nodeid
);
2743 spin_lock(&n
->list_lock
);
2745 /* Get colour for the slab, and cal the next value. */
2746 offset
= n
->colour_next
;
2748 if (n
->colour_next
>= cachep
->colour
)
2750 spin_unlock(&n
->list_lock
);
2752 offset
*= cachep
->colour_off
;
2754 if (local_flags
& __GFP_WAIT
)
2758 * The test for missing atomic flag is performed here, rather than
2759 * the more obvious place, simply to reduce the critical path length
2760 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2761 * will eventually be caught here (where it matters).
2763 kmem_flagcheck(cachep
, flags
);
2766 * Get mem for the objs. Attempt to allocate a physical page from
2770 page
= kmem_getpages(cachep
, local_flags
, nodeid
);
2774 /* Get slab management. */
2775 freelist
= alloc_slabmgmt(cachep
, page
, offset
,
2776 local_flags
& ~GFP_CONSTRAINT_MASK
, nodeid
);
2780 slab_map_pages(cachep
, page
, freelist
);
2782 cache_init_objs(cachep
, page
);
2784 if (local_flags
& __GFP_WAIT
)
2785 local_irq_disable();
2787 spin_lock(&n
->list_lock
);
2789 /* Make slab active. */
2790 list_add_tail(&page
->lru
, &(n
->slabs_free
));
2791 STATS_INC_GROWN(cachep
);
2792 n
->free_objects
+= cachep
->num
;
2793 spin_unlock(&n
->list_lock
);
2796 kmem_freepages(cachep
, page
);
2798 if (local_flags
& __GFP_WAIT
)
2799 local_irq_disable();
2806 * Perform extra freeing checks:
2807 * - detect bad pointers.
2808 * - POISON/RED_ZONE checking
2810 static void kfree_debugcheck(const void *objp
)
2812 if (!virt_addr_valid(objp
)) {
2813 printk(KERN_ERR
"kfree_debugcheck: out of range ptr %lxh.\n",
2814 (unsigned long)objp
);
2819 static inline void verify_redzone_free(struct kmem_cache
*cache
, void *obj
)
2821 unsigned long long redzone1
, redzone2
;
2823 redzone1
= *dbg_redzone1(cache
, obj
);
2824 redzone2
= *dbg_redzone2(cache
, obj
);
2829 if (redzone1
== RED_ACTIVE
&& redzone2
== RED_ACTIVE
)
2832 if (redzone1
== RED_INACTIVE
&& redzone2
== RED_INACTIVE
)
2833 slab_error(cache
, "double free detected");
2835 slab_error(cache
, "memory outside object was overwritten");
2837 printk(KERN_ERR
"%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
2838 obj
, redzone1
, redzone2
);
2841 static void *cache_free_debugcheck(struct kmem_cache
*cachep
, void *objp
,
2842 unsigned long caller
)
2847 BUG_ON(virt_to_cache(objp
) != cachep
);
2849 objp
-= obj_offset(cachep
);
2850 kfree_debugcheck(objp
);
2851 page
= virt_to_head_page(objp
);
2853 if (cachep
->flags
& SLAB_RED_ZONE
) {
2854 verify_redzone_free(cachep
, objp
);
2855 *dbg_redzone1(cachep
, objp
) = RED_INACTIVE
;
2856 *dbg_redzone2(cachep
, objp
) = RED_INACTIVE
;
2858 if (cachep
->flags
& SLAB_STORE_USER
)
2859 *dbg_userword(cachep
, objp
) = (void *)caller
;
2861 objnr
= obj_to_index(cachep
, page
, objp
);
2863 BUG_ON(objnr
>= cachep
->num
);
2864 BUG_ON(objp
!= index_to_obj(cachep
, page
, objnr
));
2866 set_obj_status(page
, objnr
, OBJECT_FREE
);
2867 if (cachep
->flags
& SLAB_POISON
) {
2868 #ifdef CONFIG_DEBUG_PAGEALLOC
2869 if ((cachep
->size
% PAGE_SIZE
)==0 && OFF_SLAB(cachep
)) {
2870 store_stackinfo(cachep
, objp
, caller
);
2871 kernel_map_pages(virt_to_page(objp
),
2872 cachep
->size
/ PAGE_SIZE
, 0);
2874 poison_obj(cachep
, objp
, POISON_FREE
);
2877 poison_obj(cachep
, objp
, POISON_FREE
);
2884 #define kfree_debugcheck(x) do { } while(0)
2885 #define cache_free_debugcheck(x,objp,z) (objp)
2888 static void *cache_alloc_refill(struct kmem_cache
*cachep
, gfp_t flags
,
2892 struct kmem_cache_node
*n
;
2893 struct array_cache
*ac
;
2897 node
= numa_mem_id();
2898 if (unlikely(force_refill
))
2901 ac
= cpu_cache_get(cachep
);
2902 batchcount
= ac
->batchcount
;
2903 if (!ac
->touched
&& batchcount
> BATCHREFILL_LIMIT
) {
2905 * If there was little recent activity on this cache, then
2906 * perform only a partial refill. Otherwise we could generate
2909 batchcount
= BATCHREFILL_LIMIT
;
2911 n
= get_node(cachep
, node
);
2913 BUG_ON(ac
->avail
> 0 || !n
);
2914 spin_lock(&n
->list_lock
);
2916 /* See if we can refill from the shared array */
2917 if (n
->shared
&& transfer_objects(ac
, n
->shared
, batchcount
)) {
2918 n
->shared
->touched
= 1;
2922 while (batchcount
> 0) {
2923 struct list_head
*entry
;
2925 /* Get slab alloc is to come from. */
2926 entry
= n
->slabs_partial
.next
;
2927 if (entry
== &n
->slabs_partial
) {
2928 n
->free_touched
= 1;
2929 entry
= n
->slabs_free
.next
;
2930 if (entry
== &n
->slabs_free
)
2934 page
= list_entry(entry
, struct page
, lru
);
2935 check_spinlock_acquired(cachep
);
2938 * The slab was either on partial or free list so
2939 * there must be at least one object available for
2942 BUG_ON(page
->active
>= cachep
->num
);
2944 while (page
->active
< cachep
->num
&& batchcount
--) {
2945 STATS_INC_ALLOCED(cachep
);
2946 STATS_INC_ACTIVE(cachep
);
2947 STATS_SET_HIGH(cachep
);
2949 ac_put_obj(cachep
, ac
, slab_get_obj(cachep
, page
,
2953 /* move slabp to correct slabp list: */
2954 list_del(&page
->lru
);
2955 if (page
->active
== cachep
->num
)
2956 list_add(&page
->lru
, &n
->slabs_full
);
2958 list_add(&page
->lru
, &n
->slabs_partial
);
2962 n
->free_objects
-= ac
->avail
;
2964 spin_unlock(&n
->list_lock
);
2966 if (unlikely(!ac
->avail
)) {
2969 x
= cache_grow(cachep
, flags
| GFP_THISNODE
, node
, NULL
);
2971 /* cache_grow can reenable interrupts, then ac could change. */
2972 ac
= cpu_cache_get(cachep
);
2973 node
= numa_mem_id();
2975 /* no objects in sight? abort */
2976 if (!x
&& (ac
->avail
== 0 || force_refill
))
2979 if (!ac
->avail
) /* objects refilled by interrupt? */
2984 return ac_get_obj(cachep
, ac
, flags
, force_refill
);
2987 static inline void cache_alloc_debugcheck_before(struct kmem_cache
*cachep
,
2990 might_sleep_if(flags
& __GFP_WAIT
);
2992 kmem_flagcheck(cachep
, flags
);
2997 static void *cache_alloc_debugcheck_after(struct kmem_cache
*cachep
,
2998 gfp_t flags
, void *objp
, unsigned long caller
)
3004 if (cachep
->flags
& SLAB_POISON
) {
3005 #ifdef CONFIG_DEBUG_PAGEALLOC
3006 if ((cachep
->size
% PAGE_SIZE
) == 0 && OFF_SLAB(cachep
))
3007 kernel_map_pages(virt_to_page(objp
),
3008 cachep
->size
/ PAGE_SIZE
, 1);
3010 check_poison_obj(cachep
, objp
);
3012 check_poison_obj(cachep
, objp
);
3014 poison_obj(cachep
, objp
, POISON_INUSE
);
3016 if (cachep
->flags
& SLAB_STORE_USER
)
3017 *dbg_userword(cachep
, objp
) = (void *)caller
;
3019 if (cachep
->flags
& SLAB_RED_ZONE
) {
3020 if (*dbg_redzone1(cachep
, objp
) != RED_INACTIVE
||
3021 *dbg_redzone2(cachep
, objp
) != RED_INACTIVE
) {
3022 slab_error(cachep
, "double free, or memory outside"
3023 " object was overwritten");
3025 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
3026 objp
, *dbg_redzone1(cachep
, objp
),
3027 *dbg_redzone2(cachep
, objp
));
3029 *dbg_redzone1(cachep
, objp
) = RED_ACTIVE
;
3030 *dbg_redzone2(cachep
, objp
) = RED_ACTIVE
;
3033 page
= virt_to_head_page(objp
);
3034 set_obj_status(page
, obj_to_index(cachep
, page
, objp
), OBJECT_ACTIVE
);
3035 objp
+= obj_offset(cachep
);
3036 if (cachep
->ctor
&& cachep
->flags
& SLAB_POISON
)
3038 if (ARCH_SLAB_MINALIGN
&&
3039 ((unsigned long)objp
& (ARCH_SLAB_MINALIGN
-1))) {
3040 printk(KERN_ERR
"0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
3041 objp
, (int)ARCH_SLAB_MINALIGN
);
3046 #define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
3049 static bool slab_should_failslab(struct kmem_cache
*cachep
, gfp_t flags
)
3051 if (cachep
== kmem_cache
)
3054 return should_failslab(cachep
->object_size
, flags
, cachep
->flags
);
3057 static inline void *____cache_alloc(struct kmem_cache
*cachep
, gfp_t flags
)
3060 struct array_cache
*ac
;
3061 bool force_refill
= false;
3065 ac
= cpu_cache_get(cachep
);
3066 if (likely(ac
->avail
)) {
3068 objp
= ac_get_obj(cachep
, ac
, flags
, false);
3071 * Allow for the possibility all avail objects are not allowed
3072 * by the current flags
3075 STATS_INC_ALLOCHIT(cachep
);
3078 force_refill
= true;
3081 STATS_INC_ALLOCMISS(cachep
);
3082 objp
= cache_alloc_refill(cachep
, flags
, force_refill
);
3084 * the 'ac' may be updated by cache_alloc_refill(),
3085 * and kmemleak_erase() requires its correct value.
3087 ac
= cpu_cache_get(cachep
);
3091 * To avoid a false negative, if an object that is in one of the
3092 * per-CPU caches is leaked, we need to make sure kmemleak doesn't
3093 * treat the array pointers as a reference to the object.
3096 kmemleak_erase(&ac
->entry
[ac
->avail
]);
3102 * Try allocating on another node if PF_SPREAD_SLAB is a mempolicy is set.
3104 * If we are in_interrupt, then process context, including cpusets and
3105 * mempolicy, may not apply and should not be used for allocation policy.
3107 static void *alternate_node_alloc(struct kmem_cache
*cachep
, gfp_t flags
)
3109 int nid_alloc
, nid_here
;
3111 if (in_interrupt() || (flags
& __GFP_THISNODE
))
3113 nid_alloc
= nid_here
= numa_mem_id();
3114 if (cpuset_do_slab_mem_spread() && (cachep
->flags
& SLAB_MEM_SPREAD
))
3115 nid_alloc
= cpuset_slab_spread_node();
3116 else if (current
->mempolicy
)
3117 nid_alloc
= mempolicy_slab_node();
3118 if (nid_alloc
!= nid_here
)
3119 return ____cache_alloc_node(cachep
, flags
, nid_alloc
);
3124 * Fallback function if there was no memory available and no objects on a
3125 * certain node and fall back is permitted. First we scan all the
3126 * available node for available objects. If that fails then we
3127 * perform an allocation without specifying a node. This allows the page
3128 * allocator to do its reclaim / fallback magic. We then insert the
3129 * slab into the proper nodelist and then allocate from it.
3131 static void *fallback_alloc(struct kmem_cache
*cache
, gfp_t flags
)
3133 struct zonelist
*zonelist
;
3137 enum zone_type high_zoneidx
= gfp_zone(flags
);
3140 unsigned int cpuset_mems_cookie
;
3142 if (flags
& __GFP_THISNODE
)
3145 local_flags
= flags
& (GFP_CONSTRAINT_MASK
|GFP_RECLAIM_MASK
);
3148 cpuset_mems_cookie
= read_mems_allowed_begin();
3149 zonelist
= node_zonelist(mempolicy_slab_node(), flags
);
3153 * Look through allowed nodes for objects available
3154 * from existing per node queues.
3156 for_each_zone_zonelist(zone
, z
, zonelist
, high_zoneidx
) {
3157 nid
= zone_to_nid(zone
);
3159 if (cpuset_zone_allowed_hardwall(zone
, flags
) &&
3160 get_node(cache
, nid
) &&
3161 get_node(cache
, nid
)->free_objects
) {
3162 obj
= ____cache_alloc_node(cache
,
3163 flags
| GFP_THISNODE
, nid
);
3171 * This allocation will be performed within the constraints
3172 * of the current cpuset / memory policy requirements.
3173 * We may trigger various forms of reclaim on the allowed
3174 * set and go into memory reserves if necessary.
3178 if (local_flags
& __GFP_WAIT
)
3180 kmem_flagcheck(cache
, flags
);
3181 page
= kmem_getpages(cache
, local_flags
, numa_mem_id());
3182 if (local_flags
& __GFP_WAIT
)
3183 local_irq_disable();
3186 * Insert into the appropriate per node queues
3188 nid
= page_to_nid(page
);
3189 if (cache_grow(cache
, flags
, nid
, page
)) {
3190 obj
= ____cache_alloc_node(cache
,
3191 flags
| GFP_THISNODE
, nid
);
3194 * Another processor may allocate the
3195 * objects in the slab since we are
3196 * not holding any locks.
3200 /* cache_grow already freed obj */
3206 if (unlikely(!obj
&& read_mems_allowed_retry(cpuset_mems_cookie
)))
3212 * A interface to enable slab creation on nodeid
3214 static void *____cache_alloc_node(struct kmem_cache
*cachep
, gfp_t flags
,
3217 struct list_head
*entry
;
3219 struct kmem_cache_node
*n
;
3223 VM_BUG_ON(nodeid
> num_online_nodes());
3224 n
= get_node(cachep
, nodeid
);
3229 spin_lock(&n
->list_lock
);
3230 entry
= n
->slabs_partial
.next
;
3231 if (entry
== &n
->slabs_partial
) {
3232 n
->free_touched
= 1;
3233 entry
= n
->slabs_free
.next
;
3234 if (entry
== &n
->slabs_free
)
3238 page
= list_entry(entry
, struct page
, lru
);
3239 check_spinlock_acquired_node(cachep
, nodeid
);
3241 STATS_INC_NODEALLOCS(cachep
);
3242 STATS_INC_ACTIVE(cachep
);
3243 STATS_SET_HIGH(cachep
);
3245 BUG_ON(page
->active
== cachep
->num
);
3247 obj
= slab_get_obj(cachep
, page
, nodeid
);
3249 /* move slabp to correct slabp list: */
3250 list_del(&page
->lru
);
3252 if (page
->active
== cachep
->num
)
3253 list_add(&page
->lru
, &n
->slabs_full
);
3255 list_add(&page
->lru
, &n
->slabs_partial
);
3257 spin_unlock(&n
->list_lock
);
3261 spin_unlock(&n
->list_lock
);
3262 x
= cache_grow(cachep
, flags
| GFP_THISNODE
, nodeid
, NULL
);
3266 return fallback_alloc(cachep
, flags
);
3272 static __always_inline
void *
3273 slab_alloc_node(struct kmem_cache
*cachep
, gfp_t flags
, int nodeid
,
3274 unsigned long caller
)
3276 unsigned long save_flags
;
3278 int slab_node
= numa_mem_id();
3280 flags
&= gfp_allowed_mask
;
3282 lockdep_trace_alloc(flags
);
3284 if (slab_should_failslab(cachep
, flags
))
3287 cachep
= memcg_kmem_get_cache(cachep
, flags
);
3289 cache_alloc_debugcheck_before(cachep
, flags
);
3290 local_irq_save(save_flags
);
3292 if (nodeid
== NUMA_NO_NODE
)
3295 if (unlikely(!get_node(cachep
, nodeid
))) {
3296 /* Node not bootstrapped yet */
3297 ptr
= fallback_alloc(cachep
, flags
);
3301 if (nodeid
== slab_node
) {
3303 * Use the locally cached objects if possible.
3304 * However ____cache_alloc does not allow fallback
3305 * to other nodes. It may fail while we still have
3306 * objects on other nodes available.
3308 ptr
= ____cache_alloc(cachep
, flags
);
3312 /* ___cache_alloc_node can fall back to other nodes */
3313 ptr
= ____cache_alloc_node(cachep
, flags
, nodeid
);
3315 local_irq_restore(save_flags
);
3316 ptr
= cache_alloc_debugcheck_after(cachep
, flags
, ptr
, caller
);
3317 kmemleak_alloc_recursive(ptr
, cachep
->object_size
, 1, cachep
->flags
,
3321 kmemcheck_slab_alloc(cachep
, flags
, ptr
, cachep
->object_size
);
3322 if (unlikely(flags
& __GFP_ZERO
))
3323 memset(ptr
, 0, cachep
->object_size
);
3329 static __always_inline
void *
3330 __do_cache_alloc(struct kmem_cache
*cache
, gfp_t flags
)
3334 if (current
->mempolicy
|| unlikely(current
->flags
& PF_SPREAD_SLAB
)) {
3335 objp
= alternate_node_alloc(cache
, flags
);
3339 objp
= ____cache_alloc(cache
, flags
);
3342 * We may just have run out of memory on the local node.
3343 * ____cache_alloc_node() knows how to locate memory on other nodes
3346 objp
= ____cache_alloc_node(cache
, flags
, numa_mem_id());
3353 static __always_inline
void *
3354 __do_cache_alloc(struct kmem_cache
*cachep
, gfp_t flags
)
3356 return ____cache_alloc(cachep
, flags
);
3359 #endif /* CONFIG_NUMA */
3361 static __always_inline
void *
3362 slab_alloc(struct kmem_cache
*cachep
, gfp_t flags
, unsigned long caller
)
3364 unsigned long save_flags
;
3367 flags
&= gfp_allowed_mask
;
3369 lockdep_trace_alloc(flags
);
3371 if (slab_should_failslab(cachep
, flags
))
3374 cachep
= memcg_kmem_get_cache(cachep
, flags
);
3376 cache_alloc_debugcheck_before(cachep
, flags
);
3377 local_irq_save(save_flags
);
3378 objp
= __do_cache_alloc(cachep
, flags
);
3379 local_irq_restore(save_flags
);
3380 objp
= cache_alloc_debugcheck_after(cachep
, flags
, objp
, caller
);
3381 kmemleak_alloc_recursive(objp
, cachep
->object_size
, 1, cachep
->flags
,
3386 kmemcheck_slab_alloc(cachep
, flags
, objp
, cachep
->object_size
);
3387 if (unlikely(flags
& __GFP_ZERO
))
3388 memset(objp
, 0, cachep
->object_size
);
3395 * Caller needs to acquire correct kmem_cache_node's list_lock
3397 static void free_block(struct kmem_cache
*cachep
, void **objpp
, int nr_objects
,
3401 struct kmem_cache_node
*n
;
3403 for (i
= 0; i
< nr_objects
; i
++) {
3407 clear_obj_pfmemalloc(&objpp
[i
]);
3410 page
= virt_to_head_page(objp
);
3411 n
= get_node(cachep
, node
);
3412 list_del(&page
->lru
);
3413 check_spinlock_acquired_node(cachep
, node
);
3414 slab_put_obj(cachep
, page
, objp
, node
);
3415 STATS_DEC_ACTIVE(cachep
);
3418 /* fixup slab chains */
3419 if (page
->active
== 0) {
3420 if (n
->free_objects
> n
->free_limit
) {
3421 n
->free_objects
-= cachep
->num
;
3422 /* No need to drop any previously held
3423 * lock here, even if we have a off-slab slab
3424 * descriptor it is guaranteed to come from
3425 * a different cache, refer to comments before
3428 slab_destroy(cachep
, page
);
3430 list_add(&page
->lru
, &n
->slabs_free
);
3433 /* Unconditionally move a slab to the end of the
3434 * partial list on free - maximum time for the
3435 * other objects to be freed, too.
3437 list_add_tail(&page
->lru
, &n
->slabs_partial
);
3442 static void cache_flusharray(struct kmem_cache
*cachep
, struct array_cache
*ac
)
3445 struct kmem_cache_node
*n
;
3446 int node
= numa_mem_id();
3448 batchcount
= ac
->batchcount
;
3450 BUG_ON(!batchcount
|| batchcount
> ac
->avail
);
3453 n
= get_node(cachep
, node
);
3454 spin_lock(&n
->list_lock
);
3456 struct array_cache
*shared_array
= n
->shared
;
3457 int max
= shared_array
->limit
- shared_array
->avail
;
3459 if (batchcount
> max
)
3461 memcpy(&(shared_array
->entry
[shared_array
->avail
]),
3462 ac
->entry
, sizeof(void *) * batchcount
);
3463 shared_array
->avail
+= batchcount
;
3468 free_block(cachep
, ac
->entry
, batchcount
, node
);
3473 struct list_head
*p
;
3475 p
= n
->slabs_free
.next
;
3476 while (p
!= &(n
->slabs_free
)) {
3479 page
= list_entry(p
, struct page
, lru
);
3480 BUG_ON(page
->active
);
3485 STATS_SET_FREEABLE(cachep
, i
);
3488 spin_unlock(&n
->list_lock
);
3489 ac
->avail
-= batchcount
;
3490 memmove(ac
->entry
, &(ac
->entry
[batchcount
]), sizeof(void *)*ac
->avail
);
3494 * Release an obj back to its cache. If the obj has a constructed state, it must
3495 * be in this state _before_ it is released. Called with disabled ints.
3497 static inline void __cache_free(struct kmem_cache
*cachep
, void *objp
,
3498 unsigned long caller
)
3500 struct array_cache
*ac
= cpu_cache_get(cachep
);
3503 kmemleak_free_recursive(objp
, cachep
->flags
);
3504 objp
= cache_free_debugcheck(cachep
, objp
, caller
);
3506 kmemcheck_slab_free(cachep
, objp
, cachep
->object_size
);
3509 * Skip calling cache_free_alien() when the platform is not numa.
3510 * This will avoid cache misses that happen while accessing slabp (which
3511 * is per page memory reference) to get nodeid. Instead use a global
3512 * variable to skip the call, which is mostly likely to be present in
3515 if (nr_online_nodes
> 1 && cache_free_alien(cachep
, objp
))
3518 if (likely(ac
->avail
< ac
->limit
)) {
3519 STATS_INC_FREEHIT(cachep
);
3521 STATS_INC_FREEMISS(cachep
);
3522 cache_flusharray(cachep
, ac
);
3525 ac_put_obj(cachep
, ac
, objp
);
3529 * kmem_cache_alloc - Allocate an object
3530 * @cachep: The cache to allocate from.
3531 * @flags: See kmalloc().
3533 * Allocate an object from this cache. The flags are only relevant
3534 * if the cache has no available objects.
3536 void *kmem_cache_alloc(struct kmem_cache
*cachep
, gfp_t flags
)
3538 void *ret
= slab_alloc(cachep
, flags
, _RET_IP_
);
3540 trace_kmem_cache_alloc(_RET_IP_
, ret
,
3541 cachep
->object_size
, cachep
->size
, flags
);
3545 EXPORT_SYMBOL(kmem_cache_alloc
);
3547 #ifdef CONFIG_TRACING
3549 kmem_cache_alloc_trace(struct kmem_cache
*cachep
, gfp_t flags
, size_t size
)
3553 ret
= slab_alloc(cachep
, flags
, _RET_IP_
);
3555 trace_kmalloc(_RET_IP_
, ret
,
3556 size
, cachep
->size
, flags
);
3559 EXPORT_SYMBOL(kmem_cache_alloc_trace
);
3564 * kmem_cache_alloc_node - Allocate an object on the specified node
3565 * @cachep: The cache to allocate from.
3566 * @flags: See kmalloc().
3567 * @nodeid: node number of the target node.
3569 * Identical to kmem_cache_alloc but it will allocate memory on the given
3570 * node, which can improve the performance for cpu bound structures.
3572 * Fallback to other node is possible if __GFP_THISNODE is not set.
3574 void *kmem_cache_alloc_node(struct kmem_cache
*cachep
, gfp_t flags
, int nodeid
)
3576 void *ret
= slab_alloc_node(cachep
, flags
, nodeid
, _RET_IP_
);
3578 trace_kmem_cache_alloc_node(_RET_IP_
, ret
,
3579 cachep
->object_size
, cachep
->size
,
3584 EXPORT_SYMBOL(kmem_cache_alloc_node
);
3586 #ifdef CONFIG_TRACING
3587 void *kmem_cache_alloc_node_trace(struct kmem_cache
*cachep
,
3594 ret
= slab_alloc_node(cachep
, flags
, nodeid
, _RET_IP_
);
3596 trace_kmalloc_node(_RET_IP_
, ret
,
3601 EXPORT_SYMBOL(kmem_cache_alloc_node_trace
);
3604 static __always_inline
void *
3605 __do_kmalloc_node(size_t size
, gfp_t flags
, int node
, unsigned long caller
)
3607 struct kmem_cache
*cachep
;
3609 cachep
= kmalloc_slab(size
, flags
);
3610 if (unlikely(ZERO_OR_NULL_PTR(cachep
)))
3612 return kmem_cache_alloc_node_trace(cachep
, flags
, node
, size
);
3615 #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
3616 void *__kmalloc_node(size_t size
, gfp_t flags
, int node
)
3618 return __do_kmalloc_node(size
, flags
, node
, _RET_IP_
);
3620 EXPORT_SYMBOL(__kmalloc_node
);
3622 void *__kmalloc_node_track_caller(size_t size
, gfp_t flags
,
3623 int node
, unsigned long caller
)
3625 return __do_kmalloc_node(size
, flags
, node
, caller
);
3627 EXPORT_SYMBOL(__kmalloc_node_track_caller
);
3629 void *__kmalloc_node(size_t size
, gfp_t flags
, int node
)
3631 return __do_kmalloc_node(size
, flags
, node
, 0);
3633 EXPORT_SYMBOL(__kmalloc_node
);
3634 #endif /* CONFIG_DEBUG_SLAB || CONFIG_TRACING */
3635 #endif /* CONFIG_NUMA */
3638 * __do_kmalloc - allocate memory
3639 * @size: how many bytes of memory are required.
3640 * @flags: the type of memory to allocate (see kmalloc).
3641 * @caller: function caller for debug tracking of the caller
3643 static __always_inline
void *__do_kmalloc(size_t size
, gfp_t flags
,
3644 unsigned long caller
)
3646 struct kmem_cache
*cachep
;
3649 cachep
= kmalloc_slab(size
, flags
);
3650 if (unlikely(ZERO_OR_NULL_PTR(cachep
)))
3652 ret
= slab_alloc(cachep
, flags
, caller
);
3654 trace_kmalloc(caller
, ret
,
3655 size
, cachep
->size
, flags
);
3661 #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
3662 void *__kmalloc(size_t size
, gfp_t flags
)
3664 return __do_kmalloc(size
, flags
, _RET_IP_
);
3666 EXPORT_SYMBOL(__kmalloc
);
3668 void *__kmalloc_track_caller(size_t size
, gfp_t flags
, unsigned long caller
)
3670 return __do_kmalloc(size
, flags
, caller
);
3672 EXPORT_SYMBOL(__kmalloc_track_caller
);
3675 void *__kmalloc(size_t size
, gfp_t flags
)
3677 return __do_kmalloc(size
, flags
, 0);
3679 EXPORT_SYMBOL(__kmalloc
);
3683 * kmem_cache_free - Deallocate an object
3684 * @cachep: The cache the allocation was from.
3685 * @objp: The previously allocated object.
3687 * Free an object which was previously allocated from this
3690 void kmem_cache_free(struct kmem_cache
*cachep
, void *objp
)
3692 unsigned long flags
;
3693 cachep
= cache_from_obj(cachep
, objp
);
3697 local_irq_save(flags
);
3698 debug_check_no_locks_freed(objp
, cachep
->object_size
);
3699 if (!(cachep
->flags
& SLAB_DEBUG_OBJECTS
))
3700 debug_check_no_obj_freed(objp
, cachep
->object_size
);
3701 __cache_free(cachep
, objp
, _RET_IP_
);
3702 local_irq_restore(flags
);
3704 trace_kmem_cache_free(_RET_IP_
, objp
);
3706 EXPORT_SYMBOL(kmem_cache_free
);
3709 * kfree - free previously allocated memory
3710 * @objp: pointer returned by kmalloc.
3712 * If @objp is NULL, no operation is performed.
3714 * Don't free memory not originally allocated by kmalloc()
3715 * or you will run into trouble.
3717 void kfree(const void *objp
)
3719 struct kmem_cache
*c
;
3720 unsigned long flags
;
3722 trace_kfree(_RET_IP_
, objp
);
3724 if (unlikely(ZERO_OR_NULL_PTR(objp
)))
3726 local_irq_save(flags
);
3727 kfree_debugcheck(objp
);
3728 c
= virt_to_cache(objp
);
3729 debug_check_no_locks_freed(objp
, c
->object_size
);
3731 debug_check_no_obj_freed(objp
, c
->object_size
);
3732 __cache_free(c
, (void *)objp
, _RET_IP_
);
3733 local_irq_restore(flags
);
3735 EXPORT_SYMBOL(kfree
);
3738 * This initializes kmem_cache_node or resizes various caches for all nodes.
3740 static int alloc_kmem_cache_node(struct kmem_cache
*cachep
, gfp_t gfp
)
3743 struct kmem_cache_node
*n
;
3744 struct array_cache
*new_shared
;
3745 struct array_cache
**new_alien
= NULL
;
3747 for_each_online_node(node
) {
3749 if (use_alien_caches
) {
3750 new_alien
= alloc_alien_cache(node
, cachep
->limit
, gfp
);
3756 if (cachep
->shared
) {
3757 new_shared
= alloc_arraycache(node
,
3758 cachep
->shared
*cachep
->batchcount
,
3761 free_alien_cache(new_alien
);
3766 n
= get_node(cachep
, node
);
3768 struct array_cache
*shared
= n
->shared
;
3770 spin_lock_irq(&n
->list_lock
);
3773 free_block(cachep
, shared
->entry
,
3774 shared
->avail
, node
);
3776 n
->shared
= new_shared
;
3778 n
->alien
= new_alien
;
3781 n
->free_limit
= (1 + nr_cpus_node(node
)) *
3782 cachep
->batchcount
+ cachep
->num
;
3783 spin_unlock_irq(&n
->list_lock
);
3785 free_alien_cache(new_alien
);
3788 n
= kmalloc_node(sizeof(struct kmem_cache_node
), gfp
, node
);
3790 free_alien_cache(new_alien
);
3795 kmem_cache_node_init(n
);
3796 n
->next_reap
= jiffies
+ REAPTIMEOUT_NODE
+
3797 ((unsigned long)cachep
) % REAPTIMEOUT_NODE
;
3798 n
->shared
= new_shared
;
3799 n
->alien
= new_alien
;
3800 n
->free_limit
= (1 + nr_cpus_node(node
)) *
3801 cachep
->batchcount
+ cachep
->num
;
3802 cachep
->node
[node
] = n
;
3807 if (!cachep
->list
.next
) {
3808 /* Cache is not active yet. Roll back what we did */
3811 n
= get_node(cachep
, node
);
3814 free_alien_cache(n
->alien
);
3816 cachep
->node
[node
] = NULL
;
3824 struct ccupdate_struct
{
3825 struct kmem_cache
*cachep
;
3826 struct array_cache
*new[0];
3829 static void do_ccupdate_local(void *info
)
3831 struct ccupdate_struct
*new = info
;
3832 struct array_cache
*old
;
3835 old
= cpu_cache_get(new->cachep
);
3837 new->cachep
->array
[smp_processor_id()] = new->new[smp_processor_id()];
3838 new->new[smp_processor_id()] = old
;
3841 /* Always called with the slab_mutex held */
3842 static int __do_tune_cpucache(struct kmem_cache
*cachep
, int limit
,
3843 int batchcount
, int shared
, gfp_t gfp
)
3845 struct ccupdate_struct
*new;
3848 new = kzalloc(sizeof(*new) + nr_cpu_ids
* sizeof(struct array_cache
*),
3853 for_each_online_cpu(i
) {
3854 new->new[i
] = alloc_arraycache(cpu_to_mem(i
), limit
,
3857 for (i
--; i
>= 0; i
--)
3863 new->cachep
= cachep
;
3865 on_each_cpu(do_ccupdate_local
, (void *)new, 1);
3868 cachep
->batchcount
= batchcount
;
3869 cachep
->limit
= limit
;
3870 cachep
->shared
= shared
;
3872 for_each_online_cpu(i
) {
3873 struct array_cache
*ccold
= new->new[i
];
3875 struct kmem_cache_node
*n
;
3880 node
= cpu_to_mem(i
);
3881 n
= get_node(cachep
, node
);
3882 spin_lock_irq(&n
->list_lock
);
3883 free_block(cachep
, ccold
->entry
, ccold
->avail
, node
);
3884 spin_unlock_irq(&n
->list_lock
);
3888 return alloc_kmem_cache_node(cachep
, gfp
);
3891 static int do_tune_cpucache(struct kmem_cache
*cachep
, int limit
,
3892 int batchcount
, int shared
, gfp_t gfp
)
3895 struct kmem_cache
*c
= NULL
;
3898 ret
= __do_tune_cpucache(cachep
, limit
, batchcount
, shared
, gfp
);
3900 if (slab_state
< FULL
)
3903 if ((ret
< 0) || !is_root_cache(cachep
))
3906 VM_BUG_ON(!mutex_is_locked(&slab_mutex
));
3907 for_each_memcg_cache_index(i
) {
3908 c
= cache_from_memcg_idx(cachep
, i
);
3910 /* return value determined by the parent cache only */
3911 __do_tune_cpucache(c
, limit
, batchcount
, shared
, gfp
);
3917 /* Called with slab_mutex held always */
3918 static int enable_cpucache(struct kmem_cache
*cachep
, gfp_t gfp
)
3925 if (!is_root_cache(cachep
)) {
3926 struct kmem_cache
*root
= memcg_root_cache(cachep
);
3927 limit
= root
->limit
;
3928 shared
= root
->shared
;
3929 batchcount
= root
->batchcount
;
3932 if (limit
&& shared
&& batchcount
)
3935 * The head array serves three purposes:
3936 * - create a LIFO ordering, i.e. return objects that are cache-warm
3937 * - reduce the number of spinlock operations.
3938 * - reduce the number of linked list operations on the slab and
3939 * bufctl chains: array operations are cheaper.
3940 * The numbers are guessed, we should auto-tune as described by
3943 if (cachep
->size
> 131072)
3945 else if (cachep
->size
> PAGE_SIZE
)
3947 else if (cachep
->size
> 1024)
3949 else if (cachep
->size
> 256)
3955 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
3956 * allocation behaviour: Most allocs on one cpu, most free operations
3957 * on another cpu. For these cases, an efficient object passing between
3958 * cpus is necessary. This is provided by a shared array. The array
3959 * replaces Bonwick's magazine layer.
3960 * On uniprocessor, it's functionally equivalent (but less efficient)
3961 * to a larger limit. Thus disabled by default.
3964 if (cachep
->size
<= PAGE_SIZE
&& num_possible_cpus() > 1)
3969 * With debugging enabled, large batchcount lead to excessively long
3970 * periods with disabled local interrupts. Limit the batchcount
3975 batchcount
= (limit
+ 1) / 2;
3977 err
= do_tune_cpucache(cachep
, limit
, batchcount
, shared
, gfp
);
3979 printk(KERN_ERR
"enable_cpucache failed for %s, error %d.\n",
3980 cachep
->name
, -err
);
3985 * Drain an array if it contains any elements taking the node lock only if
3986 * necessary. Note that the node listlock also protects the array_cache
3987 * if drain_array() is used on the shared array.
3989 static void drain_array(struct kmem_cache
*cachep
, struct kmem_cache_node
*n
,
3990 struct array_cache
*ac
, int force
, int node
)
3994 if (!ac
|| !ac
->avail
)
3996 if (ac
->touched
&& !force
) {
3999 spin_lock_irq(&n
->list_lock
);
4001 tofree
= force
? ac
->avail
: (ac
->limit
+ 4) / 5;
4002 if (tofree
> ac
->avail
)
4003 tofree
= (ac
->avail
+ 1) / 2;
4004 free_block(cachep
, ac
->entry
, tofree
, node
);
4005 ac
->avail
-= tofree
;
4006 memmove(ac
->entry
, &(ac
->entry
[tofree
]),
4007 sizeof(void *) * ac
->avail
);
4009 spin_unlock_irq(&n
->list_lock
);
4014 * cache_reap - Reclaim memory from caches.
4015 * @w: work descriptor
4017 * Called from workqueue/eventd every few seconds.
4019 * - clear the per-cpu caches for this CPU.
4020 * - return freeable pages to the main free memory pool.
4022 * If we cannot acquire the cache chain mutex then just give up - we'll try
4023 * again on the next iteration.
4025 static void cache_reap(struct work_struct
*w
)
4027 struct kmem_cache
*searchp
;
4028 struct kmem_cache_node
*n
;
4029 int node
= numa_mem_id();
4030 struct delayed_work
*work
= to_delayed_work(w
);
4032 if (!mutex_trylock(&slab_mutex
))
4033 /* Give up. Setup the next iteration. */
4036 list_for_each_entry(searchp
, &slab_caches
, list
) {
4040 * We only take the node lock if absolutely necessary and we
4041 * have established with reasonable certainty that
4042 * we can do some work if the lock was obtained.
4044 n
= get_node(searchp
, node
);
4046 reap_alien(searchp
, n
);
4048 drain_array(searchp
, n
, cpu_cache_get(searchp
), 0, node
);
4051 * These are racy checks but it does not matter
4052 * if we skip one check or scan twice.
4054 if (time_after(n
->next_reap
, jiffies
))
4057 n
->next_reap
= jiffies
+ REAPTIMEOUT_NODE
;
4059 drain_array(searchp
, n
, n
->shared
, 0, node
);
4061 if (n
->free_touched
)
4062 n
->free_touched
= 0;
4066 freed
= drain_freelist(searchp
, n
, (n
->free_limit
+
4067 5 * searchp
->num
- 1) / (5 * searchp
->num
));
4068 STATS_ADD_REAPED(searchp
, freed
);
4074 mutex_unlock(&slab_mutex
);
4077 /* Set up the next iteration */
4078 schedule_delayed_work(work
, round_jiffies_relative(REAPTIMEOUT_AC
));
4081 #ifdef CONFIG_SLABINFO
4082 void get_slabinfo(struct kmem_cache
*cachep
, struct slabinfo
*sinfo
)
4085 unsigned long active_objs
;
4086 unsigned long num_objs
;
4087 unsigned long active_slabs
= 0;
4088 unsigned long num_slabs
, free_objects
= 0, shared_avail
= 0;
4092 struct kmem_cache_node
*n
;
4096 for_each_kmem_cache_node(cachep
, node
, n
) {
4099 spin_lock_irq(&n
->list_lock
);
4101 list_for_each_entry(page
, &n
->slabs_full
, lru
) {
4102 if (page
->active
!= cachep
->num
&& !error
)
4103 error
= "slabs_full accounting error";
4104 active_objs
+= cachep
->num
;
4107 list_for_each_entry(page
, &n
->slabs_partial
, lru
) {
4108 if (page
->active
== cachep
->num
&& !error
)
4109 error
= "slabs_partial accounting error";
4110 if (!page
->active
&& !error
)
4111 error
= "slabs_partial accounting error";
4112 active_objs
+= page
->active
;
4115 list_for_each_entry(page
, &n
->slabs_free
, lru
) {
4116 if (page
->active
&& !error
)
4117 error
= "slabs_free accounting error";
4120 free_objects
+= n
->free_objects
;
4122 shared_avail
+= n
->shared
->avail
;
4124 spin_unlock_irq(&n
->list_lock
);
4126 num_slabs
+= active_slabs
;
4127 num_objs
= num_slabs
* cachep
->num
;
4128 if (num_objs
- active_objs
!= free_objects
&& !error
)
4129 error
= "free_objects accounting error";
4131 name
= cachep
->name
;
4133 printk(KERN_ERR
"slab: cache %s error: %s\n", name
, error
);
4135 sinfo
->active_objs
= active_objs
;
4136 sinfo
->num_objs
= num_objs
;
4137 sinfo
->active_slabs
= active_slabs
;
4138 sinfo
->num_slabs
= num_slabs
;
4139 sinfo
->shared_avail
= shared_avail
;
4140 sinfo
->limit
= cachep
->limit
;
4141 sinfo
->batchcount
= cachep
->batchcount
;
4142 sinfo
->shared
= cachep
->shared
;
4143 sinfo
->objects_per_slab
= cachep
->num
;
4144 sinfo
->cache_order
= cachep
->gfporder
;
4147 void slabinfo_show_stats(struct seq_file
*m
, struct kmem_cache
*cachep
)
4151 unsigned long high
= cachep
->high_mark
;
4152 unsigned long allocs
= cachep
->num_allocations
;
4153 unsigned long grown
= cachep
->grown
;
4154 unsigned long reaped
= cachep
->reaped
;
4155 unsigned long errors
= cachep
->errors
;
4156 unsigned long max_freeable
= cachep
->max_freeable
;
4157 unsigned long node_allocs
= cachep
->node_allocs
;
4158 unsigned long node_frees
= cachep
->node_frees
;
4159 unsigned long overflows
= cachep
->node_overflow
;
4161 seq_printf(m
, " : globalstat %7lu %6lu %5lu %4lu "
4162 "%4lu %4lu %4lu %4lu %4lu",
4163 allocs
, high
, grown
,
4164 reaped
, errors
, max_freeable
, node_allocs
,
4165 node_frees
, overflows
);
4169 unsigned long allochit
= atomic_read(&cachep
->allochit
);
4170 unsigned long allocmiss
= atomic_read(&cachep
->allocmiss
);
4171 unsigned long freehit
= atomic_read(&cachep
->freehit
);
4172 unsigned long freemiss
= atomic_read(&cachep
->freemiss
);
4174 seq_printf(m
, " : cpustat %6lu %6lu %6lu %6lu",
4175 allochit
, allocmiss
, freehit
, freemiss
);
4180 #define MAX_SLABINFO_WRITE 128
4182 * slabinfo_write - Tuning for the slab allocator
4184 * @buffer: user buffer
4185 * @count: data length
4188 ssize_t
slabinfo_write(struct file
*file
, const char __user
*buffer
,
4189 size_t count
, loff_t
*ppos
)
4191 char kbuf
[MAX_SLABINFO_WRITE
+ 1], *tmp
;
4192 int limit
, batchcount
, shared
, res
;
4193 struct kmem_cache
*cachep
;
4195 if (count
> MAX_SLABINFO_WRITE
)
4197 if (copy_from_user(&kbuf
, buffer
, count
))
4199 kbuf
[MAX_SLABINFO_WRITE
] = '\0';
4201 tmp
= strchr(kbuf
, ' ');
4206 if (sscanf(tmp
, " %d %d %d", &limit
, &batchcount
, &shared
) != 3)
4209 /* Find the cache in the chain of caches. */
4210 mutex_lock(&slab_mutex
);
4212 list_for_each_entry(cachep
, &slab_caches
, list
) {
4213 if (!strcmp(cachep
->name
, kbuf
)) {
4214 if (limit
< 1 || batchcount
< 1 ||
4215 batchcount
> limit
|| shared
< 0) {
4218 res
= do_tune_cpucache(cachep
, limit
,
4225 mutex_unlock(&slab_mutex
);
4231 #ifdef CONFIG_DEBUG_SLAB_LEAK
4233 static void *leaks_start(struct seq_file
*m
, loff_t
*pos
)
4235 mutex_lock(&slab_mutex
);
4236 return seq_list_start(&slab_caches
, *pos
);
4239 static inline int add_caller(unsigned long *n
, unsigned long v
)
4249 unsigned long *q
= p
+ 2 * i
;
4263 memmove(p
+ 2, p
, n
[1] * 2 * sizeof(unsigned long) - ((void *)p
- (void *)n
));
4269 static void handle_slab(unsigned long *n
, struct kmem_cache
*c
,
4277 for (i
= 0, p
= page
->s_mem
; i
< c
->num
; i
++, p
+= c
->size
) {
4278 if (get_obj_status(page
, i
) != OBJECT_ACTIVE
)
4281 if (!add_caller(n
, (unsigned long)*dbg_userword(c
, p
)))
4286 static void show_symbol(struct seq_file
*m
, unsigned long address
)
4288 #ifdef CONFIG_KALLSYMS
4289 unsigned long offset
, size
;
4290 char modname
[MODULE_NAME_LEN
], name
[KSYM_NAME_LEN
];
4292 if (lookup_symbol_attrs(address
, &size
, &offset
, modname
, name
) == 0) {
4293 seq_printf(m
, "%s+%#lx/%#lx", name
, offset
, size
);
4295 seq_printf(m
, " [%s]", modname
);
4299 seq_printf(m
, "%p", (void *)address
);
4302 static int leaks_show(struct seq_file
*m
, void *p
)
4304 struct kmem_cache
*cachep
= list_entry(p
, struct kmem_cache
, list
);
4306 struct kmem_cache_node
*n
;
4308 unsigned long *x
= m
->private;
4312 if (!(cachep
->flags
& SLAB_STORE_USER
))
4314 if (!(cachep
->flags
& SLAB_RED_ZONE
))
4317 /* OK, we can do it */
4321 for_each_kmem_cache_node(cachep
, node
, n
) {
4324 spin_lock_irq(&n
->list_lock
);
4326 list_for_each_entry(page
, &n
->slabs_full
, lru
)
4327 handle_slab(x
, cachep
, page
);
4328 list_for_each_entry(page
, &n
->slabs_partial
, lru
)
4329 handle_slab(x
, cachep
, page
);
4330 spin_unlock_irq(&n
->list_lock
);
4332 name
= cachep
->name
;
4334 /* Increase the buffer size */
4335 mutex_unlock(&slab_mutex
);
4336 m
->private = kzalloc(x
[0] * 4 * sizeof(unsigned long), GFP_KERNEL
);
4338 /* Too bad, we are really out */
4340 mutex_lock(&slab_mutex
);
4343 *(unsigned long *)m
->private = x
[0] * 2;
4345 mutex_lock(&slab_mutex
);
4346 /* Now make sure this entry will be retried */
4350 for (i
= 0; i
< x
[1]; i
++) {
4351 seq_printf(m
, "%s: %lu ", name
, x
[2*i
+3]);
4352 show_symbol(m
, x
[2*i
+2]);
4359 static const struct seq_operations slabstats_op
= {
4360 .start
= leaks_start
,
4366 static int slabstats_open(struct inode
*inode
, struct file
*file
)
4368 unsigned long *n
= kzalloc(PAGE_SIZE
, GFP_KERNEL
);
4371 ret
= seq_open(file
, &slabstats_op
);
4373 struct seq_file
*m
= file
->private_data
;
4374 *n
= PAGE_SIZE
/ (2 * sizeof(unsigned long));
4383 static const struct file_operations proc_slabstats_operations
= {
4384 .open
= slabstats_open
,
4386 .llseek
= seq_lseek
,
4387 .release
= seq_release_private
,
4391 static int __init
slab_proc_init(void)
4393 #ifdef CONFIG_DEBUG_SLAB_LEAK
4394 proc_create("slab_allocators", 0, NULL
, &proc_slabstats_operations
);
4398 module_init(slab_proc_init
);
4402 * ksize - get the actual amount of memory allocated for a given object
4403 * @objp: Pointer to the object
4405 * kmalloc may internally round up allocations and return more memory
4406 * than requested. ksize() can be used to determine the actual amount of
4407 * memory allocated. The caller may use this additional memory, even though
4408 * a smaller amount of memory was initially specified with the kmalloc call.
4409 * The caller must guarantee that objp points to a valid object previously
4410 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4411 * must not be freed during the duration of the call.
4413 size_t ksize(const void *objp
)
4416 if (unlikely(objp
== ZERO_SIZE_PTR
))
4419 return virt_to_cache(objp
)->object_size
;
4421 EXPORT_SYMBOL(ksize
);