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
;
195 * Must have this definition in here for the proper
196 * alignment of array_cache. Also simplifies accessing
199 * Entries should not be directly dereferenced as
200 * entries belonging to slabs marked pfmemalloc will
201 * have the lower bits set SLAB_OBJ_PFMEMALLOC
207 struct array_cache ac
;
210 #define SLAB_OBJ_PFMEMALLOC 1
211 static inline bool is_obj_pfmemalloc(void *objp
)
213 return (unsigned long)objp
& SLAB_OBJ_PFMEMALLOC
;
216 static inline void set_obj_pfmemalloc(void **objp
)
218 *objp
= (void *)((unsigned long)*objp
| SLAB_OBJ_PFMEMALLOC
);
222 static inline void clear_obj_pfmemalloc(void **objp
)
224 *objp
= (void *)((unsigned long)*objp
& ~SLAB_OBJ_PFMEMALLOC
);
228 * bootstrap: The caches do not work without cpuarrays anymore, but the
229 * cpuarrays are allocated from the generic caches...
231 #define BOOT_CPUCACHE_ENTRIES 1
232 struct arraycache_init
{
233 struct array_cache cache
;
234 void *entries
[BOOT_CPUCACHE_ENTRIES
];
238 * Need this for bootstrapping a per node allocator.
240 #define NUM_INIT_LISTS (2 * MAX_NUMNODES)
241 static struct kmem_cache_node __initdata init_kmem_cache_node
[NUM_INIT_LISTS
];
242 #define CACHE_CACHE 0
243 #define SIZE_NODE (MAX_NUMNODES)
245 static int drain_freelist(struct kmem_cache
*cache
,
246 struct kmem_cache_node
*n
, int tofree
);
247 static void free_block(struct kmem_cache
*cachep
, void **objpp
, int len
,
248 int node
, struct list_head
*list
);
249 static void slabs_destroy(struct kmem_cache
*cachep
, struct list_head
*list
);
250 static int enable_cpucache(struct kmem_cache
*cachep
, gfp_t gfp
);
251 static void cache_reap(struct work_struct
*unused
);
253 static int slab_early_init
= 1;
255 #define INDEX_NODE kmalloc_index(sizeof(struct kmem_cache_node))
257 static void kmem_cache_node_init(struct kmem_cache_node
*parent
)
259 INIT_LIST_HEAD(&parent
->slabs_full
);
260 INIT_LIST_HEAD(&parent
->slabs_partial
);
261 INIT_LIST_HEAD(&parent
->slabs_free
);
262 parent
->shared
= NULL
;
263 parent
->alien
= NULL
;
264 parent
->colour_next
= 0;
265 spin_lock_init(&parent
->list_lock
);
266 parent
->free_objects
= 0;
267 parent
->free_touched
= 0;
270 #define MAKE_LIST(cachep, listp, slab, nodeid) \
272 INIT_LIST_HEAD(listp); \
273 list_splice(&get_node(cachep, nodeid)->slab, listp); \
276 #define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
278 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
279 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
280 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
283 #define CFLGS_OFF_SLAB (0x80000000UL)
284 #define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
285 #define OFF_SLAB_MIN_SIZE (max_t(size_t, PAGE_SIZE >> 5, KMALLOC_MIN_SIZE + 1))
287 #define BATCHREFILL_LIMIT 16
289 * Optimization question: fewer reaps means less probability for unnessary
290 * cpucache drain/refill cycles.
292 * OTOH the cpuarrays can contain lots of objects,
293 * which could lock up otherwise freeable slabs.
295 #define REAPTIMEOUT_AC (2*HZ)
296 #define REAPTIMEOUT_NODE (4*HZ)
299 #define STATS_INC_ACTIVE(x) ((x)->num_active++)
300 #define STATS_DEC_ACTIVE(x) ((x)->num_active--)
301 #define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
302 #define STATS_INC_GROWN(x) ((x)->grown++)
303 #define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
304 #define STATS_SET_HIGH(x) \
306 if ((x)->num_active > (x)->high_mark) \
307 (x)->high_mark = (x)->num_active; \
309 #define STATS_INC_ERR(x) ((x)->errors++)
310 #define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
311 #define STATS_INC_NODEFREES(x) ((x)->node_frees++)
312 #define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
313 #define STATS_SET_FREEABLE(x, i) \
315 if ((x)->max_freeable < i) \
316 (x)->max_freeable = i; \
318 #define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
319 #define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
320 #define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
321 #define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
323 #define STATS_INC_ACTIVE(x) do { } while (0)
324 #define STATS_DEC_ACTIVE(x) do { } while (0)
325 #define STATS_INC_ALLOCED(x) do { } while (0)
326 #define STATS_INC_GROWN(x) do { } while (0)
327 #define STATS_ADD_REAPED(x,y) do { (void)(y); } while (0)
328 #define STATS_SET_HIGH(x) do { } while (0)
329 #define STATS_INC_ERR(x) do { } while (0)
330 #define STATS_INC_NODEALLOCS(x) do { } while (0)
331 #define STATS_INC_NODEFREES(x) do { } while (0)
332 #define STATS_INC_ACOVERFLOW(x) do { } while (0)
333 #define STATS_SET_FREEABLE(x, i) do { } while (0)
334 #define STATS_INC_ALLOCHIT(x) do { } while (0)
335 #define STATS_INC_ALLOCMISS(x) do { } while (0)
336 #define STATS_INC_FREEHIT(x) do { } while (0)
337 #define STATS_INC_FREEMISS(x) do { } while (0)
343 * memory layout of objects:
345 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
346 * the end of an object is aligned with the end of the real
347 * allocation. Catches writes behind the end of the allocation.
348 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
350 * cachep->obj_offset: The real object.
351 * cachep->size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
352 * cachep->size - 1* BYTES_PER_WORD: last caller address
353 * [BYTES_PER_WORD long]
355 static int obj_offset(struct kmem_cache
*cachep
)
357 return cachep
->obj_offset
;
360 static unsigned long long *dbg_redzone1(struct kmem_cache
*cachep
, void *objp
)
362 BUG_ON(!(cachep
->flags
& SLAB_RED_ZONE
));
363 return (unsigned long long*) (objp
+ obj_offset(cachep
) -
364 sizeof(unsigned long long));
367 static unsigned long long *dbg_redzone2(struct kmem_cache
*cachep
, void *objp
)
369 BUG_ON(!(cachep
->flags
& SLAB_RED_ZONE
));
370 if (cachep
->flags
& SLAB_STORE_USER
)
371 return (unsigned long long *)(objp
+ cachep
->size
-
372 sizeof(unsigned long long) -
374 return (unsigned long long *) (objp
+ cachep
->size
-
375 sizeof(unsigned long long));
378 static void **dbg_userword(struct kmem_cache
*cachep
, void *objp
)
380 BUG_ON(!(cachep
->flags
& SLAB_STORE_USER
));
381 return (void **)(objp
+ cachep
->size
- BYTES_PER_WORD
);
386 #define obj_offset(x) 0
387 #define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
388 #define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
389 #define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
393 #define OBJECT_FREE (0)
394 #define OBJECT_ACTIVE (1)
396 #ifdef CONFIG_DEBUG_SLAB_LEAK
398 static void set_obj_status(struct page
*page
, int idx
, int val
)
402 struct kmem_cache
*cachep
= page
->slab_cache
;
404 freelist_size
= cachep
->num
* sizeof(freelist_idx_t
);
405 status
= (char *)page
->freelist
+ freelist_size
;
409 static inline bool is_store_user_clean(struct kmem_cache
*cachep
)
411 return atomic_read(&cachep
->store_user_clean
) == 1;
414 static inline void set_store_user_clean(struct kmem_cache
*cachep
)
416 atomic_set(&cachep
->store_user_clean
, 1);
419 static inline void set_store_user_dirty(struct kmem_cache
*cachep
)
421 if (is_store_user_clean(cachep
))
422 atomic_set(&cachep
->store_user_clean
, 0);
426 static inline void set_obj_status(struct page
*page
, int idx
, int val
) {}
427 static inline void set_store_user_dirty(struct kmem_cache
*cachep
) {}
432 * Do not go above this order unless 0 objects fit into the slab or
433 * overridden on the command line.
435 #define SLAB_MAX_ORDER_HI 1
436 #define SLAB_MAX_ORDER_LO 0
437 static int slab_max_order
= SLAB_MAX_ORDER_LO
;
438 static bool slab_max_order_set __initdata
;
440 static inline struct kmem_cache
*virt_to_cache(const void *obj
)
442 struct page
*page
= virt_to_head_page(obj
);
443 return page
->slab_cache
;
446 static inline void *index_to_obj(struct kmem_cache
*cache
, struct page
*page
,
449 return page
->s_mem
+ cache
->size
* idx
;
453 * We want to avoid an expensive divide : (offset / cache->size)
454 * Using the fact that size is a constant for a particular cache,
455 * we can replace (offset / cache->size) by
456 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
458 static inline unsigned int obj_to_index(const struct kmem_cache
*cache
,
459 const struct page
*page
, void *obj
)
461 u32 offset
= (obj
- page
->s_mem
);
462 return reciprocal_divide(offset
, cache
->reciprocal_buffer_size
);
465 /* internal cache of cache description objs */
466 static struct kmem_cache kmem_cache_boot
= {
468 .limit
= BOOT_CPUCACHE_ENTRIES
,
470 .size
= sizeof(struct kmem_cache
),
471 .name
= "kmem_cache",
474 #define BAD_ALIEN_MAGIC 0x01020304ul
476 static DEFINE_PER_CPU(struct delayed_work
, slab_reap_work
);
478 static inline struct array_cache
*cpu_cache_get(struct kmem_cache
*cachep
)
480 return this_cpu_ptr(cachep
->cpu_cache
);
483 static size_t calculate_freelist_size(int nr_objs
, size_t align
)
485 size_t freelist_size
;
487 freelist_size
= nr_objs
* sizeof(freelist_idx_t
);
488 if (IS_ENABLED(CONFIG_DEBUG_SLAB_LEAK
))
489 freelist_size
+= nr_objs
* sizeof(char);
492 freelist_size
= ALIGN(freelist_size
, align
);
494 return freelist_size
;
497 static int calculate_nr_objs(size_t slab_size
, size_t buffer_size
,
498 size_t idx_size
, size_t align
)
501 size_t remained_size
;
502 size_t freelist_size
;
505 if (IS_ENABLED(CONFIG_DEBUG_SLAB_LEAK
))
506 extra_space
= sizeof(char);
508 * Ignore padding for the initial guess. The padding
509 * is at most @align-1 bytes, and @buffer_size is at
510 * least @align. In the worst case, this result will
511 * be one greater than the number of objects that fit
512 * into the memory allocation when taking the padding
515 nr_objs
= slab_size
/ (buffer_size
+ idx_size
+ extra_space
);
518 * This calculated number will be either the right
519 * amount, or one greater than what we want.
521 remained_size
= slab_size
- nr_objs
* buffer_size
;
522 freelist_size
= calculate_freelist_size(nr_objs
, align
);
523 if (remained_size
< freelist_size
)
530 * Calculate the number of objects and left-over bytes for a given buffer size.
532 static void cache_estimate(unsigned long gfporder
, size_t buffer_size
,
533 size_t align
, int flags
, size_t *left_over
,
538 size_t slab_size
= PAGE_SIZE
<< gfporder
;
541 * The slab management structure can be either off the slab or
542 * on it. For the latter case, the memory allocated for a
545 * - One unsigned int for each object
546 * - Padding to respect alignment of @align
547 * - @buffer_size bytes for each object
549 * If the slab management structure is off the slab, then the
550 * alignment will already be calculated into the size. Because
551 * the slabs are all pages aligned, the objects will be at the
552 * correct alignment when allocated.
554 if (flags
& CFLGS_OFF_SLAB
) {
556 nr_objs
= slab_size
/ buffer_size
;
559 nr_objs
= calculate_nr_objs(slab_size
, buffer_size
,
560 sizeof(freelist_idx_t
), align
);
561 mgmt_size
= calculate_freelist_size(nr_objs
, align
);
564 *left_over
= slab_size
- nr_objs
*buffer_size
- mgmt_size
;
568 #define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
570 static void __slab_error(const char *function
, struct kmem_cache
*cachep
,
573 printk(KERN_ERR
"slab error in %s(): cache `%s': %s\n",
574 function
, cachep
->name
, msg
);
576 add_taint(TAINT_BAD_PAGE
, LOCKDEP_NOW_UNRELIABLE
);
581 * By default on NUMA we use alien caches to stage the freeing of
582 * objects allocated from other nodes. This causes massive memory
583 * inefficiencies when using fake NUMA setup to split memory into a
584 * large number of small nodes, so it can be disabled on the command
588 static int use_alien_caches __read_mostly
= 1;
589 static int __init
noaliencache_setup(char *s
)
591 use_alien_caches
= 0;
594 __setup("noaliencache", noaliencache_setup
);
596 static int __init
slab_max_order_setup(char *str
)
598 get_option(&str
, &slab_max_order
);
599 slab_max_order
= slab_max_order
< 0 ? 0 :
600 min(slab_max_order
, MAX_ORDER
- 1);
601 slab_max_order_set
= true;
605 __setup("slab_max_order=", slab_max_order_setup
);
609 * Special reaping functions for NUMA systems called from cache_reap().
610 * These take care of doing round robin flushing of alien caches (containing
611 * objects freed on different nodes from which they were allocated) and the
612 * flushing of remote pcps by calling drain_node_pages.
614 static DEFINE_PER_CPU(unsigned long, slab_reap_node
);
616 static void init_reap_node(int cpu
)
620 node
= next_node(cpu_to_mem(cpu
), node_online_map
);
621 if (node
== MAX_NUMNODES
)
622 node
= first_node(node_online_map
);
624 per_cpu(slab_reap_node
, cpu
) = node
;
627 static void next_reap_node(void)
629 int node
= __this_cpu_read(slab_reap_node
);
631 node
= next_node(node
, node_online_map
);
632 if (unlikely(node
>= MAX_NUMNODES
))
633 node
= first_node(node_online_map
);
634 __this_cpu_write(slab_reap_node
, node
);
638 #define init_reap_node(cpu) do { } while (0)
639 #define next_reap_node(void) do { } while (0)
643 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
644 * via the workqueue/eventd.
645 * Add the CPU number into the expiration time to minimize the possibility of
646 * the CPUs getting into lockstep and contending for the global cache chain
649 static void start_cpu_timer(int cpu
)
651 struct delayed_work
*reap_work
= &per_cpu(slab_reap_work
, cpu
);
654 * When this gets called from do_initcalls via cpucache_init(),
655 * init_workqueues() has already run, so keventd will be setup
658 if (keventd_up() && reap_work
->work
.func
== NULL
) {
660 INIT_DEFERRABLE_WORK(reap_work
, cache_reap
);
661 schedule_delayed_work_on(cpu
, reap_work
,
662 __round_jiffies_relative(HZ
, cpu
));
666 static void init_arraycache(struct array_cache
*ac
, int limit
, int batch
)
669 * The array_cache structures contain pointers to free object.
670 * However, when such objects are allocated or transferred to another
671 * cache the pointers are not cleared and they could be counted as
672 * valid references during a kmemleak scan. Therefore, kmemleak must
673 * not scan such objects.
675 kmemleak_no_scan(ac
);
679 ac
->batchcount
= batch
;
684 static struct array_cache
*alloc_arraycache(int node
, int entries
,
685 int batchcount
, gfp_t gfp
)
687 size_t memsize
= sizeof(void *) * entries
+ sizeof(struct array_cache
);
688 struct array_cache
*ac
= NULL
;
690 ac
= kmalloc_node(memsize
, gfp
, node
);
691 init_arraycache(ac
, entries
, batchcount
);
695 static inline bool is_slab_pfmemalloc(struct page
*page
)
697 return PageSlabPfmemalloc(page
);
700 /* Clears pfmemalloc_active if no slabs have pfmalloc set */
701 static void recheck_pfmemalloc_active(struct kmem_cache
*cachep
,
702 struct array_cache
*ac
)
704 struct kmem_cache_node
*n
= get_node(cachep
, numa_mem_id());
708 if (!pfmemalloc_active
)
711 spin_lock_irqsave(&n
->list_lock
, flags
);
712 list_for_each_entry(page
, &n
->slabs_full
, lru
)
713 if (is_slab_pfmemalloc(page
))
716 list_for_each_entry(page
, &n
->slabs_partial
, lru
)
717 if (is_slab_pfmemalloc(page
))
720 list_for_each_entry(page
, &n
->slabs_free
, lru
)
721 if (is_slab_pfmemalloc(page
))
724 pfmemalloc_active
= false;
726 spin_unlock_irqrestore(&n
->list_lock
, flags
);
729 static void *__ac_get_obj(struct kmem_cache
*cachep
, struct array_cache
*ac
,
730 gfp_t flags
, bool force_refill
)
733 void *objp
= ac
->entry
[--ac
->avail
];
735 /* Ensure the caller is allowed to use objects from PFMEMALLOC slab */
736 if (unlikely(is_obj_pfmemalloc(objp
))) {
737 struct kmem_cache_node
*n
;
739 if (gfp_pfmemalloc_allowed(flags
)) {
740 clear_obj_pfmemalloc(&objp
);
744 /* The caller cannot use PFMEMALLOC objects, find another one */
745 for (i
= 0; i
< ac
->avail
; i
++) {
746 /* If a !PFMEMALLOC object is found, swap them */
747 if (!is_obj_pfmemalloc(ac
->entry
[i
])) {
749 ac
->entry
[i
] = ac
->entry
[ac
->avail
];
750 ac
->entry
[ac
->avail
] = objp
;
756 * If there are empty slabs on the slabs_free list and we are
757 * being forced to refill the cache, mark this one !pfmemalloc.
759 n
= get_node(cachep
, numa_mem_id());
760 if (!list_empty(&n
->slabs_free
) && force_refill
) {
761 struct page
*page
= virt_to_head_page(objp
);
762 ClearPageSlabPfmemalloc(page
);
763 clear_obj_pfmemalloc(&objp
);
764 recheck_pfmemalloc_active(cachep
, ac
);
768 /* No !PFMEMALLOC objects available */
776 static inline void *ac_get_obj(struct kmem_cache
*cachep
,
777 struct array_cache
*ac
, gfp_t flags
, bool force_refill
)
781 if (unlikely(sk_memalloc_socks()))
782 objp
= __ac_get_obj(cachep
, ac
, flags
, force_refill
);
784 objp
= ac
->entry
[--ac
->avail
];
789 static noinline
void *__ac_put_obj(struct kmem_cache
*cachep
,
790 struct array_cache
*ac
, void *objp
)
792 if (unlikely(pfmemalloc_active
)) {
793 /* Some pfmemalloc slabs exist, check if this is one */
794 struct page
*page
= virt_to_head_page(objp
);
795 if (PageSlabPfmemalloc(page
))
796 set_obj_pfmemalloc(&objp
);
802 static inline void ac_put_obj(struct kmem_cache
*cachep
, struct array_cache
*ac
,
805 if (unlikely(sk_memalloc_socks()))
806 objp
= __ac_put_obj(cachep
, ac
, objp
);
808 ac
->entry
[ac
->avail
++] = objp
;
812 * Transfer objects in one arraycache to another.
813 * Locking must be handled by the caller.
815 * Return the number of entries transferred.
817 static int transfer_objects(struct array_cache
*to
,
818 struct array_cache
*from
, unsigned int max
)
820 /* Figure out how many entries to transfer */
821 int nr
= min3(from
->avail
, max
, to
->limit
- to
->avail
);
826 memcpy(to
->entry
+ to
->avail
, from
->entry
+ from
->avail
-nr
,
836 #define drain_alien_cache(cachep, alien) do { } while (0)
837 #define reap_alien(cachep, n) do { } while (0)
839 static inline struct alien_cache
**alloc_alien_cache(int node
,
840 int limit
, gfp_t gfp
)
842 return (struct alien_cache
**)BAD_ALIEN_MAGIC
;
845 static inline void free_alien_cache(struct alien_cache
**ac_ptr
)
849 static inline int cache_free_alien(struct kmem_cache
*cachep
, void *objp
)
854 static inline void *alternate_node_alloc(struct kmem_cache
*cachep
,
860 static inline void *____cache_alloc_node(struct kmem_cache
*cachep
,
861 gfp_t flags
, int nodeid
)
866 static inline gfp_t
gfp_exact_node(gfp_t flags
)
871 #else /* CONFIG_NUMA */
873 static void *____cache_alloc_node(struct kmem_cache
*, gfp_t
, int);
874 static void *alternate_node_alloc(struct kmem_cache
*, gfp_t
);
876 static struct alien_cache
*__alloc_alien_cache(int node
, int entries
,
877 int batch
, gfp_t gfp
)
879 size_t memsize
= sizeof(void *) * entries
+ sizeof(struct alien_cache
);
880 struct alien_cache
*alc
= NULL
;
882 alc
= kmalloc_node(memsize
, gfp
, node
);
883 init_arraycache(&alc
->ac
, entries
, batch
);
884 spin_lock_init(&alc
->lock
);
888 static struct alien_cache
**alloc_alien_cache(int node
, int limit
, gfp_t gfp
)
890 struct alien_cache
**alc_ptr
;
891 size_t memsize
= sizeof(void *) * nr_node_ids
;
896 alc_ptr
= kzalloc_node(memsize
, gfp
, node
);
901 if (i
== node
|| !node_online(i
))
903 alc_ptr
[i
] = __alloc_alien_cache(node
, limit
, 0xbaadf00d, gfp
);
905 for (i
--; i
>= 0; i
--)
914 static void free_alien_cache(struct alien_cache
**alc_ptr
)
925 static void __drain_alien_cache(struct kmem_cache
*cachep
,
926 struct array_cache
*ac
, int node
,
927 struct list_head
*list
)
929 struct kmem_cache_node
*n
= get_node(cachep
, node
);
932 spin_lock(&n
->list_lock
);
934 * Stuff objects into the remote nodes shared array first.
935 * That way we could avoid the overhead of putting the objects
936 * into the free lists and getting them back later.
939 transfer_objects(n
->shared
, ac
, ac
->limit
);
941 free_block(cachep
, ac
->entry
, ac
->avail
, node
, list
);
943 spin_unlock(&n
->list_lock
);
948 * Called from cache_reap() to regularly drain alien caches round robin.
950 static void reap_alien(struct kmem_cache
*cachep
, struct kmem_cache_node
*n
)
952 int node
= __this_cpu_read(slab_reap_node
);
955 struct alien_cache
*alc
= n
->alien
[node
];
956 struct array_cache
*ac
;
960 if (ac
->avail
&& spin_trylock_irq(&alc
->lock
)) {
963 __drain_alien_cache(cachep
, ac
, node
, &list
);
964 spin_unlock_irq(&alc
->lock
);
965 slabs_destroy(cachep
, &list
);
971 static void drain_alien_cache(struct kmem_cache
*cachep
,
972 struct alien_cache
**alien
)
975 struct alien_cache
*alc
;
976 struct array_cache
*ac
;
979 for_each_online_node(i
) {
985 spin_lock_irqsave(&alc
->lock
, flags
);
986 __drain_alien_cache(cachep
, ac
, i
, &list
);
987 spin_unlock_irqrestore(&alc
->lock
, flags
);
988 slabs_destroy(cachep
, &list
);
993 static int __cache_free_alien(struct kmem_cache
*cachep
, void *objp
,
994 int node
, int page_node
)
996 struct kmem_cache_node
*n
;
997 struct alien_cache
*alien
= NULL
;
998 struct array_cache
*ac
;
1001 n
= get_node(cachep
, node
);
1002 STATS_INC_NODEFREES(cachep
);
1003 if (n
->alien
&& n
->alien
[page_node
]) {
1004 alien
= n
->alien
[page_node
];
1006 spin_lock(&alien
->lock
);
1007 if (unlikely(ac
->avail
== ac
->limit
)) {
1008 STATS_INC_ACOVERFLOW(cachep
);
1009 __drain_alien_cache(cachep
, ac
, page_node
, &list
);
1011 ac_put_obj(cachep
, ac
, objp
);
1012 spin_unlock(&alien
->lock
);
1013 slabs_destroy(cachep
, &list
);
1015 n
= get_node(cachep
, page_node
);
1016 spin_lock(&n
->list_lock
);
1017 free_block(cachep
, &objp
, 1, page_node
, &list
);
1018 spin_unlock(&n
->list_lock
);
1019 slabs_destroy(cachep
, &list
);
1024 static inline int cache_free_alien(struct kmem_cache
*cachep
, void *objp
)
1026 int page_node
= page_to_nid(virt_to_page(objp
));
1027 int node
= numa_mem_id();
1029 * Make sure we are not freeing a object from another node to the array
1030 * cache on this cpu.
1032 if (likely(node
== page_node
))
1035 return __cache_free_alien(cachep
, objp
, node
, page_node
);
1039 * Construct gfp mask to allocate from a specific node but do not direct reclaim
1040 * or warn about failures. kswapd may still wake to reclaim in the background.
1042 static inline gfp_t
gfp_exact_node(gfp_t flags
)
1044 return (flags
| __GFP_THISNODE
| __GFP_NOWARN
) & ~__GFP_DIRECT_RECLAIM
;
1049 * Allocates and initializes node for a node on each slab cache, used for
1050 * either memory or cpu hotplug. If memory is being hot-added, the kmem_cache_node
1051 * will be allocated off-node since memory is not yet online for the new node.
1052 * When hotplugging memory or a cpu, existing node are not replaced if
1055 * Must hold slab_mutex.
1057 static int init_cache_node_node(int node
)
1059 struct kmem_cache
*cachep
;
1060 struct kmem_cache_node
*n
;
1061 const size_t memsize
= sizeof(struct kmem_cache_node
);
1063 list_for_each_entry(cachep
, &slab_caches
, list
) {
1065 * Set up the kmem_cache_node for cpu before we can
1066 * begin anything. Make sure some other cpu on this
1067 * node has not already allocated this
1069 n
= get_node(cachep
, node
);
1071 n
= kmalloc_node(memsize
, GFP_KERNEL
, node
);
1074 kmem_cache_node_init(n
);
1075 n
->next_reap
= jiffies
+ REAPTIMEOUT_NODE
+
1076 ((unsigned long)cachep
) % REAPTIMEOUT_NODE
;
1079 * The kmem_cache_nodes don't come and go as CPUs
1080 * come and go. slab_mutex is sufficient
1083 cachep
->node
[node
] = n
;
1086 spin_lock_irq(&n
->list_lock
);
1088 (1 + nr_cpus_node(node
)) *
1089 cachep
->batchcount
+ cachep
->num
;
1090 spin_unlock_irq(&n
->list_lock
);
1095 static inline int slabs_tofree(struct kmem_cache
*cachep
,
1096 struct kmem_cache_node
*n
)
1098 return (n
->free_objects
+ cachep
->num
- 1) / cachep
->num
;
1101 static void cpuup_canceled(long cpu
)
1103 struct kmem_cache
*cachep
;
1104 struct kmem_cache_node
*n
= NULL
;
1105 int node
= cpu_to_mem(cpu
);
1106 const struct cpumask
*mask
= cpumask_of_node(node
);
1108 list_for_each_entry(cachep
, &slab_caches
, list
) {
1109 struct array_cache
*nc
;
1110 struct array_cache
*shared
;
1111 struct alien_cache
**alien
;
1114 n
= get_node(cachep
, node
);
1118 spin_lock_irq(&n
->list_lock
);
1120 /* Free limit for this kmem_cache_node */
1121 n
->free_limit
-= cachep
->batchcount
;
1123 /* cpu is dead; no one can alloc from it. */
1124 nc
= per_cpu_ptr(cachep
->cpu_cache
, cpu
);
1126 free_block(cachep
, nc
->entry
, nc
->avail
, node
, &list
);
1130 if (!cpumask_empty(mask
)) {
1131 spin_unlock_irq(&n
->list_lock
);
1137 free_block(cachep
, shared
->entry
,
1138 shared
->avail
, node
, &list
);
1145 spin_unlock_irq(&n
->list_lock
);
1149 drain_alien_cache(cachep
, alien
);
1150 free_alien_cache(alien
);
1154 slabs_destroy(cachep
, &list
);
1157 * In the previous loop, all the objects were freed to
1158 * the respective cache's slabs, now we can go ahead and
1159 * shrink each nodelist to its limit.
1161 list_for_each_entry(cachep
, &slab_caches
, list
) {
1162 n
= get_node(cachep
, node
);
1165 drain_freelist(cachep
, n
, slabs_tofree(cachep
, n
));
1169 static int cpuup_prepare(long cpu
)
1171 struct kmem_cache
*cachep
;
1172 struct kmem_cache_node
*n
= NULL
;
1173 int node
= cpu_to_mem(cpu
);
1177 * We need to do this right in the beginning since
1178 * alloc_arraycache's are going to use this list.
1179 * kmalloc_node allows us to add the slab to the right
1180 * kmem_cache_node and not this cpu's kmem_cache_node
1182 err
= init_cache_node_node(node
);
1187 * Now we can go ahead with allocating the shared arrays and
1190 list_for_each_entry(cachep
, &slab_caches
, list
) {
1191 struct array_cache
*shared
= NULL
;
1192 struct alien_cache
**alien
= NULL
;
1194 if (cachep
->shared
) {
1195 shared
= alloc_arraycache(node
,
1196 cachep
->shared
* cachep
->batchcount
,
1197 0xbaadf00d, GFP_KERNEL
);
1201 if (use_alien_caches
) {
1202 alien
= alloc_alien_cache(node
, cachep
->limit
, GFP_KERNEL
);
1208 n
= get_node(cachep
, node
);
1211 spin_lock_irq(&n
->list_lock
);
1214 * We are serialised from CPU_DEAD or
1215 * CPU_UP_CANCELLED by the cpucontrol lock
1226 spin_unlock_irq(&n
->list_lock
);
1228 free_alien_cache(alien
);
1233 cpuup_canceled(cpu
);
1237 static int cpuup_callback(struct notifier_block
*nfb
,
1238 unsigned long action
, void *hcpu
)
1240 long cpu
= (long)hcpu
;
1244 case CPU_UP_PREPARE
:
1245 case CPU_UP_PREPARE_FROZEN
:
1246 mutex_lock(&slab_mutex
);
1247 err
= cpuup_prepare(cpu
);
1248 mutex_unlock(&slab_mutex
);
1251 case CPU_ONLINE_FROZEN
:
1252 start_cpu_timer(cpu
);
1254 #ifdef CONFIG_HOTPLUG_CPU
1255 case CPU_DOWN_PREPARE
:
1256 case CPU_DOWN_PREPARE_FROZEN
:
1258 * Shutdown cache reaper. Note that the slab_mutex is
1259 * held so that if cache_reap() is invoked it cannot do
1260 * anything expensive but will only modify reap_work
1261 * and reschedule the timer.
1263 cancel_delayed_work_sync(&per_cpu(slab_reap_work
, cpu
));
1264 /* Now the cache_reaper is guaranteed to be not running. */
1265 per_cpu(slab_reap_work
, cpu
).work
.func
= NULL
;
1267 case CPU_DOWN_FAILED
:
1268 case CPU_DOWN_FAILED_FROZEN
:
1269 start_cpu_timer(cpu
);
1272 case CPU_DEAD_FROZEN
:
1274 * Even if all the cpus of a node are down, we don't free the
1275 * kmem_cache_node of any cache. This to avoid a race between
1276 * cpu_down, and a kmalloc allocation from another cpu for
1277 * memory from the node of the cpu going down. The node
1278 * structure is usually allocated from kmem_cache_create() and
1279 * gets destroyed at kmem_cache_destroy().
1283 case CPU_UP_CANCELED
:
1284 case CPU_UP_CANCELED_FROZEN
:
1285 mutex_lock(&slab_mutex
);
1286 cpuup_canceled(cpu
);
1287 mutex_unlock(&slab_mutex
);
1290 return notifier_from_errno(err
);
1293 static struct notifier_block cpucache_notifier
= {
1294 &cpuup_callback
, NULL
, 0
1297 #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
1299 * Drains freelist for a node on each slab cache, used for memory hot-remove.
1300 * Returns -EBUSY if all objects cannot be drained so that the node is not
1303 * Must hold slab_mutex.
1305 static int __meminit
drain_cache_node_node(int node
)
1307 struct kmem_cache
*cachep
;
1310 list_for_each_entry(cachep
, &slab_caches
, list
) {
1311 struct kmem_cache_node
*n
;
1313 n
= get_node(cachep
, node
);
1317 drain_freelist(cachep
, n
, slabs_tofree(cachep
, n
));
1319 if (!list_empty(&n
->slabs_full
) ||
1320 !list_empty(&n
->slabs_partial
)) {
1328 static int __meminit
slab_memory_callback(struct notifier_block
*self
,
1329 unsigned long action
, void *arg
)
1331 struct memory_notify
*mnb
= arg
;
1335 nid
= mnb
->status_change_nid
;
1340 case MEM_GOING_ONLINE
:
1341 mutex_lock(&slab_mutex
);
1342 ret
= init_cache_node_node(nid
);
1343 mutex_unlock(&slab_mutex
);
1345 case MEM_GOING_OFFLINE
:
1346 mutex_lock(&slab_mutex
);
1347 ret
= drain_cache_node_node(nid
);
1348 mutex_unlock(&slab_mutex
);
1352 case MEM_CANCEL_ONLINE
:
1353 case MEM_CANCEL_OFFLINE
:
1357 return notifier_from_errno(ret
);
1359 #endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
1362 * swap the static kmem_cache_node with kmalloced memory
1364 static void __init
init_list(struct kmem_cache
*cachep
, struct kmem_cache_node
*list
,
1367 struct kmem_cache_node
*ptr
;
1369 ptr
= kmalloc_node(sizeof(struct kmem_cache_node
), GFP_NOWAIT
, nodeid
);
1372 memcpy(ptr
, list
, sizeof(struct kmem_cache_node
));
1374 * Do not assume that spinlocks can be initialized via memcpy:
1376 spin_lock_init(&ptr
->list_lock
);
1378 MAKE_ALL_LISTS(cachep
, ptr
, nodeid
);
1379 cachep
->node
[nodeid
] = ptr
;
1383 * For setting up all the kmem_cache_node for cache whose buffer_size is same as
1384 * size of kmem_cache_node.
1386 static void __init
set_up_node(struct kmem_cache
*cachep
, int index
)
1390 for_each_online_node(node
) {
1391 cachep
->node
[node
] = &init_kmem_cache_node
[index
+ node
];
1392 cachep
->node
[node
]->next_reap
= jiffies
+
1394 ((unsigned long)cachep
) % REAPTIMEOUT_NODE
;
1399 * Initialisation. Called after the page allocator have been initialised and
1400 * before smp_init().
1402 void __init
kmem_cache_init(void)
1406 BUILD_BUG_ON(sizeof(((struct page
*)NULL
)->lru
) <
1407 sizeof(struct rcu_head
));
1408 kmem_cache
= &kmem_cache_boot
;
1410 if (num_possible_nodes() == 1)
1411 use_alien_caches
= 0;
1413 for (i
= 0; i
< NUM_INIT_LISTS
; i
++)
1414 kmem_cache_node_init(&init_kmem_cache_node
[i
]);
1417 * Fragmentation resistance on low memory - only use bigger
1418 * page orders on machines with more than 32MB of memory if
1419 * not overridden on the command line.
1421 if (!slab_max_order_set
&& totalram_pages
> (32 << 20) >> PAGE_SHIFT
)
1422 slab_max_order
= SLAB_MAX_ORDER_HI
;
1424 /* Bootstrap is tricky, because several objects are allocated
1425 * from caches that do not exist yet:
1426 * 1) initialize the kmem_cache cache: it contains the struct
1427 * kmem_cache structures of all caches, except kmem_cache itself:
1428 * kmem_cache is statically allocated.
1429 * Initially an __init data area is used for the head array and the
1430 * kmem_cache_node structures, it's replaced with a kmalloc allocated
1431 * array at the end of the bootstrap.
1432 * 2) Create the first kmalloc cache.
1433 * The struct kmem_cache for the new cache is allocated normally.
1434 * An __init data area is used for the head array.
1435 * 3) Create the remaining kmalloc caches, with minimally sized
1437 * 4) Replace the __init data head arrays for kmem_cache and the first
1438 * kmalloc cache with kmalloc allocated arrays.
1439 * 5) Replace the __init data for kmem_cache_node for kmem_cache and
1440 * the other cache's with kmalloc allocated memory.
1441 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
1444 /* 1) create the kmem_cache */
1447 * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
1449 create_boot_cache(kmem_cache
, "kmem_cache",
1450 offsetof(struct kmem_cache
, node
) +
1451 nr_node_ids
* sizeof(struct kmem_cache_node
*),
1452 SLAB_HWCACHE_ALIGN
);
1453 list_add(&kmem_cache
->list
, &slab_caches
);
1454 slab_state
= PARTIAL
;
1457 * Initialize the caches that provide memory for the kmem_cache_node
1458 * structures first. Without this, further allocations will bug.
1460 kmalloc_caches
[INDEX_NODE
] = create_kmalloc_cache("kmalloc-node",
1461 kmalloc_size(INDEX_NODE
), ARCH_KMALLOC_FLAGS
);
1462 slab_state
= PARTIAL_NODE
;
1463 setup_kmalloc_cache_index_table();
1465 slab_early_init
= 0;
1467 /* 5) Replace the bootstrap kmem_cache_node */
1471 for_each_online_node(nid
) {
1472 init_list(kmem_cache
, &init_kmem_cache_node
[CACHE_CACHE
+ nid
], nid
);
1474 init_list(kmalloc_caches
[INDEX_NODE
],
1475 &init_kmem_cache_node
[SIZE_NODE
+ nid
], nid
);
1479 create_kmalloc_caches(ARCH_KMALLOC_FLAGS
);
1482 void __init
kmem_cache_init_late(void)
1484 struct kmem_cache
*cachep
;
1488 /* 6) resize the head arrays to their final sizes */
1489 mutex_lock(&slab_mutex
);
1490 list_for_each_entry(cachep
, &slab_caches
, list
)
1491 if (enable_cpucache(cachep
, GFP_NOWAIT
))
1493 mutex_unlock(&slab_mutex
);
1499 * Register a cpu startup notifier callback that initializes
1500 * cpu_cache_get for all new cpus
1502 register_cpu_notifier(&cpucache_notifier
);
1506 * Register a memory hotplug callback that initializes and frees
1509 hotplug_memory_notifier(slab_memory_callback
, SLAB_CALLBACK_PRI
);
1513 * The reap timers are started later, with a module init call: That part
1514 * of the kernel is not yet operational.
1518 static int __init
cpucache_init(void)
1523 * Register the timers that return unneeded pages to the page allocator
1525 for_each_online_cpu(cpu
)
1526 start_cpu_timer(cpu
);
1532 __initcall(cpucache_init
);
1534 static noinline
void
1535 slab_out_of_memory(struct kmem_cache
*cachep
, gfp_t gfpflags
, int nodeid
)
1538 struct kmem_cache_node
*n
;
1540 unsigned long flags
;
1542 static DEFINE_RATELIMIT_STATE(slab_oom_rs
, DEFAULT_RATELIMIT_INTERVAL
,
1543 DEFAULT_RATELIMIT_BURST
);
1545 if ((gfpflags
& __GFP_NOWARN
) || !__ratelimit(&slab_oom_rs
))
1549 "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n",
1551 printk(KERN_WARNING
" cache: %s, object size: %d, order: %d\n",
1552 cachep
->name
, cachep
->size
, cachep
->gfporder
);
1554 for_each_kmem_cache_node(cachep
, node
, n
) {
1555 unsigned long active_objs
= 0, num_objs
= 0, free_objects
= 0;
1556 unsigned long active_slabs
= 0, num_slabs
= 0;
1558 spin_lock_irqsave(&n
->list_lock
, flags
);
1559 list_for_each_entry(page
, &n
->slabs_full
, lru
) {
1560 active_objs
+= cachep
->num
;
1563 list_for_each_entry(page
, &n
->slabs_partial
, lru
) {
1564 active_objs
+= page
->active
;
1567 list_for_each_entry(page
, &n
->slabs_free
, lru
)
1570 free_objects
+= n
->free_objects
;
1571 spin_unlock_irqrestore(&n
->list_lock
, flags
);
1573 num_slabs
+= active_slabs
;
1574 num_objs
= num_slabs
* cachep
->num
;
1576 " node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
1577 node
, active_slabs
, num_slabs
, active_objs
, num_objs
,
1584 * Interface to system's page allocator. No need to hold the
1585 * kmem_cache_node ->list_lock.
1587 * If we requested dmaable memory, we will get it. Even if we
1588 * did not request dmaable memory, we might get it, but that
1589 * would be relatively rare and ignorable.
1591 static struct page
*kmem_getpages(struct kmem_cache
*cachep
, gfp_t flags
,
1597 flags
|= cachep
->allocflags
;
1598 if (cachep
->flags
& SLAB_RECLAIM_ACCOUNT
)
1599 flags
|= __GFP_RECLAIMABLE
;
1601 page
= __alloc_pages_node(nodeid
, flags
| __GFP_NOTRACK
, cachep
->gfporder
);
1603 slab_out_of_memory(cachep
, flags
, nodeid
);
1607 if (memcg_charge_slab(page
, flags
, cachep
->gfporder
, cachep
)) {
1608 __free_pages(page
, cachep
->gfporder
);
1612 /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
1613 if (page_is_pfmemalloc(page
))
1614 pfmemalloc_active
= true;
1616 nr_pages
= (1 << cachep
->gfporder
);
1617 if (cachep
->flags
& SLAB_RECLAIM_ACCOUNT
)
1618 add_zone_page_state(page_zone(page
),
1619 NR_SLAB_RECLAIMABLE
, nr_pages
);
1621 add_zone_page_state(page_zone(page
),
1622 NR_SLAB_UNRECLAIMABLE
, nr_pages
);
1623 __SetPageSlab(page
);
1624 if (page_is_pfmemalloc(page
))
1625 SetPageSlabPfmemalloc(page
);
1627 if (kmemcheck_enabled
&& !(cachep
->flags
& SLAB_NOTRACK
)) {
1628 kmemcheck_alloc_shadow(page
, cachep
->gfporder
, flags
, nodeid
);
1631 kmemcheck_mark_uninitialized_pages(page
, nr_pages
);
1633 kmemcheck_mark_unallocated_pages(page
, nr_pages
);
1640 * Interface to system's page release.
1642 static void kmem_freepages(struct kmem_cache
*cachep
, struct page
*page
)
1644 const unsigned long nr_freed
= (1 << cachep
->gfporder
);
1646 kmemcheck_free_shadow(page
, cachep
->gfporder
);
1648 if (cachep
->flags
& SLAB_RECLAIM_ACCOUNT
)
1649 sub_zone_page_state(page_zone(page
),
1650 NR_SLAB_RECLAIMABLE
, nr_freed
);
1652 sub_zone_page_state(page_zone(page
),
1653 NR_SLAB_UNRECLAIMABLE
, nr_freed
);
1655 BUG_ON(!PageSlab(page
));
1656 __ClearPageSlabPfmemalloc(page
);
1657 __ClearPageSlab(page
);
1658 page_mapcount_reset(page
);
1659 page
->mapping
= NULL
;
1661 if (current
->reclaim_state
)
1662 current
->reclaim_state
->reclaimed_slab
+= nr_freed
;
1663 __free_kmem_pages(page
, cachep
->gfporder
);
1666 static void kmem_rcu_free(struct rcu_head
*head
)
1668 struct kmem_cache
*cachep
;
1671 page
= container_of(head
, struct page
, rcu_head
);
1672 cachep
= page
->slab_cache
;
1674 kmem_freepages(cachep
, page
);
1678 static bool is_debug_pagealloc_cache(struct kmem_cache
*cachep
)
1680 if (debug_pagealloc_enabled() && OFF_SLAB(cachep
) &&
1681 (cachep
->size
% PAGE_SIZE
) == 0)
1687 #ifdef CONFIG_DEBUG_PAGEALLOC
1688 static void store_stackinfo(struct kmem_cache
*cachep
, unsigned long *addr
,
1689 unsigned long caller
)
1691 int size
= cachep
->object_size
;
1693 addr
= (unsigned long *)&((char *)addr
)[obj_offset(cachep
)];
1695 if (size
< 5 * sizeof(unsigned long))
1698 *addr
++ = 0x12345678;
1700 *addr
++ = smp_processor_id();
1701 size
-= 3 * sizeof(unsigned long);
1703 unsigned long *sptr
= &caller
;
1704 unsigned long svalue
;
1706 while (!kstack_end(sptr
)) {
1708 if (kernel_text_address(svalue
)) {
1710 size
-= sizeof(unsigned long);
1711 if (size
<= sizeof(unsigned long))
1717 *addr
++ = 0x87654321;
1720 static void slab_kernel_map(struct kmem_cache
*cachep
, void *objp
,
1721 int map
, unsigned long caller
)
1723 if (!is_debug_pagealloc_cache(cachep
))
1727 store_stackinfo(cachep
, objp
, caller
);
1729 kernel_map_pages(virt_to_page(objp
), cachep
->size
/ PAGE_SIZE
, map
);
1733 static inline void slab_kernel_map(struct kmem_cache
*cachep
, void *objp
,
1734 int map
, unsigned long caller
) {}
1738 static void poison_obj(struct kmem_cache
*cachep
, void *addr
, unsigned char val
)
1740 int size
= cachep
->object_size
;
1741 addr
= &((char *)addr
)[obj_offset(cachep
)];
1743 memset(addr
, val
, size
);
1744 *(unsigned char *)(addr
+ size
- 1) = POISON_END
;
1747 static void dump_line(char *data
, int offset
, int limit
)
1750 unsigned char error
= 0;
1753 printk(KERN_ERR
"%03x: ", offset
);
1754 for (i
= 0; i
< limit
; i
++) {
1755 if (data
[offset
+ i
] != POISON_FREE
) {
1756 error
= data
[offset
+ i
];
1760 print_hex_dump(KERN_CONT
, "", 0, 16, 1,
1761 &data
[offset
], limit
, 1);
1763 if (bad_count
== 1) {
1764 error
^= POISON_FREE
;
1765 if (!(error
& (error
- 1))) {
1766 printk(KERN_ERR
"Single bit error detected. Probably "
1769 printk(KERN_ERR
"Run memtest86+ or a similar memory "
1772 printk(KERN_ERR
"Run a memory test tool.\n");
1781 static void print_objinfo(struct kmem_cache
*cachep
, void *objp
, int lines
)
1786 if (cachep
->flags
& SLAB_RED_ZONE
) {
1787 printk(KERN_ERR
"Redzone: 0x%llx/0x%llx.\n",
1788 *dbg_redzone1(cachep
, objp
),
1789 *dbg_redzone2(cachep
, objp
));
1792 if (cachep
->flags
& SLAB_STORE_USER
) {
1793 printk(KERN_ERR
"Last user: [<%p>](%pSR)\n",
1794 *dbg_userword(cachep
, objp
),
1795 *dbg_userword(cachep
, objp
));
1797 realobj
= (char *)objp
+ obj_offset(cachep
);
1798 size
= cachep
->object_size
;
1799 for (i
= 0; i
< size
&& lines
; i
+= 16, lines
--) {
1802 if (i
+ limit
> size
)
1804 dump_line(realobj
, i
, limit
);
1808 static void check_poison_obj(struct kmem_cache
*cachep
, void *objp
)
1814 if (is_debug_pagealloc_cache(cachep
))
1817 realobj
= (char *)objp
+ obj_offset(cachep
);
1818 size
= cachep
->object_size
;
1820 for (i
= 0; i
< size
; i
++) {
1821 char exp
= POISON_FREE
;
1824 if (realobj
[i
] != exp
) {
1830 "Slab corruption (%s): %s start=%p, len=%d\n",
1831 print_tainted(), cachep
->name
, realobj
, size
);
1832 print_objinfo(cachep
, objp
, 0);
1834 /* Hexdump the affected line */
1837 if (i
+ limit
> size
)
1839 dump_line(realobj
, i
, limit
);
1842 /* Limit to 5 lines */
1848 /* Print some data about the neighboring objects, if they
1851 struct page
*page
= virt_to_head_page(objp
);
1854 objnr
= obj_to_index(cachep
, page
, objp
);
1856 objp
= index_to_obj(cachep
, page
, objnr
- 1);
1857 realobj
= (char *)objp
+ obj_offset(cachep
);
1858 printk(KERN_ERR
"Prev obj: start=%p, len=%d\n",
1860 print_objinfo(cachep
, objp
, 2);
1862 if (objnr
+ 1 < cachep
->num
) {
1863 objp
= index_to_obj(cachep
, page
, objnr
+ 1);
1864 realobj
= (char *)objp
+ obj_offset(cachep
);
1865 printk(KERN_ERR
"Next obj: start=%p, len=%d\n",
1867 print_objinfo(cachep
, objp
, 2);
1874 static void slab_destroy_debugcheck(struct kmem_cache
*cachep
,
1878 for (i
= 0; i
< cachep
->num
; i
++) {
1879 void *objp
= index_to_obj(cachep
, page
, i
);
1881 if (cachep
->flags
& SLAB_POISON
) {
1882 check_poison_obj(cachep
, objp
);
1883 slab_kernel_map(cachep
, objp
, 1, 0);
1885 if (cachep
->flags
& SLAB_RED_ZONE
) {
1886 if (*dbg_redzone1(cachep
, objp
) != RED_INACTIVE
)
1887 slab_error(cachep
, "start of a freed object "
1889 if (*dbg_redzone2(cachep
, objp
) != RED_INACTIVE
)
1890 slab_error(cachep
, "end of a freed object "
1896 static void slab_destroy_debugcheck(struct kmem_cache
*cachep
,
1903 * slab_destroy - destroy and release all objects in a slab
1904 * @cachep: cache pointer being destroyed
1905 * @page: page pointer being destroyed
1907 * Destroy all the objs in a slab page, and release the mem back to the system.
1908 * Before calling the slab page must have been unlinked from the cache. The
1909 * kmem_cache_node ->list_lock is not held/needed.
1911 static void slab_destroy(struct kmem_cache
*cachep
, struct page
*page
)
1915 freelist
= page
->freelist
;
1916 slab_destroy_debugcheck(cachep
, page
);
1917 if (unlikely(cachep
->flags
& SLAB_DESTROY_BY_RCU
))
1918 call_rcu(&page
->rcu_head
, kmem_rcu_free
);
1920 kmem_freepages(cachep
, page
);
1923 * From now on, we don't use freelist
1924 * although actual page can be freed in rcu context
1926 if (OFF_SLAB(cachep
))
1927 kmem_cache_free(cachep
->freelist_cache
, freelist
);
1930 static void slabs_destroy(struct kmem_cache
*cachep
, struct list_head
*list
)
1932 struct page
*page
, *n
;
1934 list_for_each_entry_safe(page
, n
, list
, lru
) {
1935 list_del(&page
->lru
);
1936 slab_destroy(cachep
, page
);
1941 * calculate_slab_order - calculate size (page order) of slabs
1942 * @cachep: pointer to the cache that is being created
1943 * @size: size of objects to be created in this cache.
1944 * @align: required alignment for the objects.
1945 * @flags: slab allocation flags
1947 * Also calculates the number of objects per slab.
1949 * This could be made much more intelligent. For now, try to avoid using
1950 * high order pages for slabs. When the gfp() functions are more friendly
1951 * towards high-order requests, this should be changed.
1953 static size_t calculate_slab_order(struct kmem_cache
*cachep
,
1954 size_t size
, size_t align
, unsigned long flags
)
1956 unsigned long offslab_limit
;
1957 size_t left_over
= 0;
1960 for (gfporder
= 0; gfporder
<= KMALLOC_MAX_ORDER
; gfporder
++) {
1964 cache_estimate(gfporder
, size
, align
, flags
, &remainder
, &num
);
1968 /* Can't handle number of objects more than SLAB_OBJ_MAX_NUM */
1969 if (num
> SLAB_OBJ_MAX_NUM
)
1972 if (flags
& CFLGS_OFF_SLAB
) {
1973 size_t freelist_size_per_obj
= sizeof(freelist_idx_t
);
1975 * Max number of objs-per-slab for caches which
1976 * use off-slab slabs. Needed to avoid a possible
1977 * looping condition in cache_grow().
1979 if (IS_ENABLED(CONFIG_DEBUG_SLAB_LEAK
))
1980 freelist_size_per_obj
+= sizeof(char);
1981 offslab_limit
= size
;
1982 offslab_limit
/= freelist_size_per_obj
;
1984 if (num
> offslab_limit
)
1988 /* Found something acceptable - save it away */
1990 cachep
->gfporder
= gfporder
;
1991 left_over
= remainder
;
1994 * A VFS-reclaimable slab tends to have most allocations
1995 * as GFP_NOFS and we really don't want to have to be allocating
1996 * higher-order pages when we are unable to shrink dcache.
1998 if (flags
& SLAB_RECLAIM_ACCOUNT
)
2002 * Large number of objects is good, but very large slabs are
2003 * currently bad for the gfp()s.
2005 if (gfporder
>= slab_max_order
)
2009 * Acceptable internal fragmentation?
2011 if (left_over
* 8 <= (PAGE_SIZE
<< gfporder
))
2017 static struct array_cache __percpu
*alloc_kmem_cache_cpus(
2018 struct kmem_cache
*cachep
, int entries
, int batchcount
)
2022 struct array_cache __percpu
*cpu_cache
;
2024 size
= sizeof(void *) * entries
+ sizeof(struct array_cache
);
2025 cpu_cache
= __alloc_percpu(size
, sizeof(void *));
2030 for_each_possible_cpu(cpu
) {
2031 init_arraycache(per_cpu_ptr(cpu_cache
, cpu
),
2032 entries
, batchcount
);
2038 static int __init_refok
setup_cpu_cache(struct kmem_cache
*cachep
, gfp_t gfp
)
2040 if (slab_state
>= FULL
)
2041 return enable_cpucache(cachep
, gfp
);
2043 cachep
->cpu_cache
= alloc_kmem_cache_cpus(cachep
, 1, 1);
2044 if (!cachep
->cpu_cache
)
2047 if (slab_state
== DOWN
) {
2048 /* Creation of first cache (kmem_cache). */
2049 set_up_node(kmem_cache
, CACHE_CACHE
);
2050 } else if (slab_state
== PARTIAL
) {
2051 /* For kmem_cache_node */
2052 set_up_node(cachep
, SIZE_NODE
);
2056 for_each_online_node(node
) {
2057 cachep
->node
[node
] = kmalloc_node(
2058 sizeof(struct kmem_cache_node
), gfp
, node
);
2059 BUG_ON(!cachep
->node
[node
]);
2060 kmem_cache_node_init(cachep
->node
[node
]);
2064 cachep
->node
[numa_mem_id()]->next_reap
=
2065 jiffies
+ REAPTIMEOUT_NODE
+
2066 ((unsigned long)cachep
) % REAPTIMEOUT_NODE
;
2068 cpu_cache_get(cachep
)->avail
= 0;
2069 cpu_cache_get(cachep
)->limit
= BOOT_CPUCACHE_ENTRIES
;
2070 cpu_cache_get(cachep
)->batchcount
= 1;
2071 cpu_cache_get(cachep
)->touched
= 0;
2072 cachep
->batchcount
= 1;
2073 cachep
->limit
= BOOT_CPUCACHE_ENTRIES
;
2077 unsigned long kmem_cache_flags(unsigned long object_size
,
2078 unsigned long flags
, const char *name
,
2079 void (*ctor
)(void *))
2085 __kmem_cache_alias(const char *name
, size_t size
, size_t align
,
2086 unsigned long flags
, void (*ctor
)(void *))
2088 struct kmem_cache
*cachep
;
2090 cachep
= find_mergeable(size
, align
, flags
, name
, ctor
);
2095 * Adjust the object sizes so that we clear
2096 * the complete object on kzalloc.
2098 cachep
->object_size
= max_t(int, cachep
->object_size
, size
);
2104 * __kmem_cache_create - Create a cache.
2105 * @cachep: cache management descriptor
2106 * @flags: SLAB flags
2108 * Returns a ptr to the cache on success, NULL on failure.
2109 * Cannot be called within a int, but can be interrupted.
2110 * The @ctor is run when new pages are allocated by the cache.
2114 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2115 * to catch references to uninitialised memory.
2117 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2118 * for buffer overruns.
2120 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2121 * cacheline. This can be beneficial if you're counting cycles as closely
2125 __kmem_cache_create (struct kmem_cache
*cachep
, unsigned long flags
)
2127 size_t left_over
, freelist_size
;
2128 size_t ralign
= BYTES_PER_WORD
;
2131 size_t size
= cachep
->size
;
2136 * Enable redzoning and last user accounting, except for caches with
2137 * large objects, if the increased size would increase the object size
2138 * above the next power of two: caches with object sizes just above a
2139 * power of two have a significant amount of internal fragmentation.
2141 if (size
< 4096 || fls(size
- 1) == fls(size
-1 + REDZONE_ALIGN
+
2142 2 * sizeof(unsigned long long)))
2143 flags
|= SLAB_RED_ZONE
| SLAB_STORE_USER
;
2144 if (!(flags
& SLAB_DESTROY_BY_RCU
))
2145 flags
|= SLAB_POISON
;
2147 if (flags
& SLAB_DESTROY_BY_RCU
)
2148 BUG_ON(flags
& SLAB_POISON
);
2152 * Check that size is in terms of words. This is needed to avoid
2153 * unaligned accesses for some archs when redzoning is used, and makes
2154 * sure any on-slab bufctl's are also correctly aligned.
2156 if (size
& (BYTES_PER_WORD
- 1)) {
2157 size
+= (BYTES_PER_WORD
- 1);
2158 size
&= ~(BYTES_PER_WORD
- 1);
2161 if (flags
& SLAB_RED_ZONE
) {
2162 ralign
= REDZONE_ALIGN
;
2163 /* If redzoning, ensure that the second redzone is suitably
2164 * aligned, by adjusting the object size accordingly. */
2165 size
+= REDZONE_ALIGN
- 1;
2166 size
&= ~(REDZONE_ALIGN
- 1);
2169 /* 3) caller mandated alignment */
2170 if (ralign
< cachep
->align
) {
2171 ralign
= cachep
->align
;
2173 /* disable debug if necessary */
2174 if (ralign
> __alignof__(unsigned long long))
2175 flags
&= ~(SLAB_RED_ZONE
| SLAB_STORE_USER
);
2179 cachep
->align
= ralign
;
2181 if (slab_is_available())
2189 * Both debugging options require word-alignment which is calculated
2192 if (flags
& SLAB_RED_ZONE
) {
2193 /* add space for red zone words */
2194 cachep
->obj_offset
+= sizeof(unsigned long long);
2195 size
+= 2 * sizeof(unsigned long long);
2197 if (flags
& SLAB_STORE_USER
) {
2198 /* user store requires one word storage behind the end of
2199 * the real object. But if the second red zone needs to be
2200 * aligned to 64 bits, we must allow that much space.
2202 if (flags
& SLAB_RED_ZONE
)
2203 size
+= REDZONE_ALIGN
;
2205 size
+= BYTES_PER_WORD
;
2207 #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
2209 * To activate debug pagealloc, off-slab management is necessary
2210 * requirement. In early phase of initialization, small sized slab
2211 * doesn't get initialized so it would not be possible. So, we need
2212 * to check size >= 256. It guarantees that all necessary small
2213 * sized slab is initialized in current slab initialization sequence.
2215 if (debug_pagealloc_enabled() &&
2216 !slab_early_init
&& size
>= kmalloc_size(INDEX_NODE
) &&
2217 size
>= 256 && cachep
->object_size
> cache_line_size() &&
2218 ALIGN(size
, cachep
->align
) < PAGE_SIZE
) {
2219 cachep
->obj_offset
+= PAGE_SIZE
- ALIGN(size
, cachep
->align
);
2226 * Determine if the slab management is 'on' or 'off' slab.
2227 * (bootstrapping cannot cope with offslab caches so don't do
2228 * it too early on. Always use on-slab management when
2229 * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
2231 if (size
>= OFF_SLAB_MIN_SIZE
&& !slab_early_init
&&
2232 !(flags
& SLAB_NOLEAKTRACE
))
2234 * Size is large, assume best to place the slab management obj
2235 * off-slab (should allow better packing of objs).
2237 flags
|= CFLGS_OFF_SLAB
;
2239 size
= ALIGN(size
, cachep
->align
);
2241 * We should restrict the number of objects in a slab to implement
2242 * byte sized index. Refer comment on SLAB_OBJ_MIN_SIZE definition.
2244 if (FREELIST_BYTE_INDEX
&& size
< SLAB_OBJ_MIN_SIZE
)
2245 size
= ALIGN(SLAB_OBJ_MIN_SIZE
, cachep
->align
);
2247 left_over
= calculate_slab_order(cachep
, size
, cachep
->align
, flags
);
2252 freelist_size
= calculate_freelist_size(cachep
->num
, cachep
->align
);
2255 * If the slab has been placed off-slab, and we have enough space then
2256 * move it on-slab. This is at the expense of any extra colouring.
2258 if (flags
& CFLGS_OFF_SLAB
&& left_over
>= freelist_size
) {
2259 flags
&= ~CFLGS_OFF_SLAB
;
2260 left_over
-= freelist_size
;
2263 if (flags
& CFLGS_OFF_SLAB
) {
2264 /* really off slab. No need for manual alignment */
2265 freelist_size
= calculate_freelist_size(cachep
->num
, 0);
2268 cachep
->colour_off
= cache_line_size();
2269 /* Offset must be a multiple of the alignment. */
2270 if (cachep
->colour_off
< cachep
->align
)
2271 cachep
->colour_off
= cachep
->align
;
2272 cachep
->colour
= left_over
/ cachep
->colour_off
;
2273 cachep
->freelist_size
= freelist_size
;
2274 cachep
->flags
= flags
;
2275 cachep
->allocflags
= __GFP_COMP
;
2276 if (CONFIG_ZONE_DMA_FLAG
&& (flags
& SLAB_CACHE_DMA
))
2277 cachep
->allocflags
|= GFP_DMA
;
2278 cachep
->size
= size
;
2279 cachep
->reciprocal_buffer_size
= reciprocal_value(size
);
2283 * If we're going to use the generic kernel_map_pages()
2284 * poisoning, then it's going to smash the contents of
2285 * the redzone and userword anyhow, so switch them off.
2287 if (IS_ENABLED(CONFIG_PAGE_POISONING
) &&
2288 (cachep
->flags
& SLAB_POISON
) &&
2289 is_debug_pagealloc_cache(cachep
))
2290 cachep
->flags
&= ~(SLAB_RED_ZONE
| SLAB_STORE_USER
);
2293 if (OFF_SLAB(cachep
)) {
2294 cachep
->freelist_cache
= kmalloc_slab(freelist_size
, 0u);
2296 * This is a possibility for one of the kmalloc_{dma,}_caches.
2297 * But since we go off slab only for object size greater than
2298 * OFF_SLAB_MIN_SIZE, and kmalloc_{dma,}_caches get created
2299 * in ascending order,this should not happen at all.
2300 * But leave a BUG_ON for some lucky dude.
2302 BUG_ON(ZERO_OR_NULL_PTR(cachep
->freelist_cache
));
2305 err
= setup_cpu_cache(cachep
, gfp
);
2307 __kmem_cache_shutdown(cachep
);
2315 static void check_irq_off(void)
2317 BUG_ON(!irqs_disabled());
2320 static void check_irq_on(void)
2322 BUG_ON(irqs_disabled());
2325 static void check_spinlock_acquired(struct kmem_cache
*cachep
)
2329 assert_spin_locked(&get_node(cachep
, numa_mem_id())->list_lock
);
2333 static void check_spinlock_acquired_node(struct kmem_cache
*cachep
, int node
)
2337 assert_spin_locked(&get_node(cachep
, node
)->list_lock
);
2342 #define check_irq_off() do { } while(0)
2343 #define check_irq_on() do { } while(0)
2344 #define check_spinlock_acquired(x) do { } while(0)
2345 #define check_spinlock_acquired_node(x, y) do { } while(0)
2348 static void drain_array(struct kmem_cache
*cachep
, struct kmem_cache_node
*n
,
2349 struct array_cache
*ac
,
2350 int force
, int node
);
2352 static void do_drain(void *arg
)
2354 struct kmem_cache
*cachep
= arg
;
2355 struct array_cache
*ac
;
2356 int node
= numa_mem_id();
2357 struct kmem_cache_node
*n
;
2361 ac
= cpu_cache_get(cachep
);
2362 n
= get_node(cachep
, node
);
2363 spin_lock(&n
->list_lock
);
2364 free_block(cachep
, ac
->entry
, ac
->avail
, node
, &list
);
2365 spin_unlock(&n
->list_lock
);
2366 slabs_destroy(cachep
, &list
);
2370 static void drain_cpu_caches(struct kmem_cache
*cachep
)
2372 struct kmem_cache_node
*n
;
2375 on_each_cpu(do_drain
, cachep
, 1);
2377 for_each_kmem_cache_node(cachep
, node
, n
)
2379 drain_alien_cache(cachep
, n
->alien
);
2381 for_each_kmem_cache_node(cachep
, node
, n
)
2382 drain_array(cachep
, n
, n
->shared
, 1, node
);
2386 * Remove slabs from the list of free slabs.
2387 * Specify the number of slabs to drain in tofree.
2389 * Returns the actual number of slabs released.
2391 static int drain_freelist(struct kmem_cache
*cache
,
2392 struct kmem_cache_node
*n
, int tofree
)
2394 struct list_head
*p
;
2399 while (nr_freed
< tofree
&& !list_empty(&n
->slabs_free
)) {
2401 spin_lock_irq(&n
->list_lock
);
2402 p
= n
->slabs_free
.prev
;
2403 if (p
== &n
->slabs_free
) {
2404 spin_unlock_irq(&n
->list_lock
);
2408 page
= list_entry(p
, struct page
, lru
);
2410 BUG_ON(page
->active
);
2412 list_del(&page
->lru
);
2414 * Safe to drop the lock. The slab is no longer linked
2417 n
->free_objects
-= cache
->num
;
2418 spin_unlock_irq(&n
->list_lock
);
2419 slab_destroy(cache
, page
);
2426 int __kmem_cache_shrink(struct kmem_cache
*cachep
, bool deactivate
)
2430 struct kmem_cache_node
*n
;
2432 drain_cpu_caches(cachep
);
2435 for_each_kmem_cache_node(cachep
, node
, n
) {
2436 drain_freelist(cachep
, n
, slabs_tofree(cachep
, n
));
2438 ret
+= !list_empty(&n
->slabs_full
) ||
2439 !list_empty(&n
->slabs_partial
);
2441 return (ret
? 1 : 0);
2444 int __kmem_cache_shutdown(struct kmem_cache
*cachep
)
2447 struct kmem_cache_node
*n
;
2448 int rc
= __kmem_cache_shrink(cachep
, false);
2453 free_percpu(cachep
->cpu_cache
);
2455 /* NUMA: free the node structures */
2456 for_each_kmem_cache_node(cachep
, i
, n
) {
2458 free_alien_cache(n
->alien
);
2460 cachep
->node
[i
] = NULL
;
2466 * Get the memory for a slab management obj.
2468 * For a slab cache when the slab descriptor is off-slab, the
2469 * slab descriptor can't come from the same cache which is being created,
2470 * Because if it is the case, that means we defer the creation of
2471 * the kmalloc_{dma,}_cache of size sizeof(slab descriptor) to this point.
2472 * And we eventually call down to __kmem_cache_create(), which
2473 * in turn looks up in the kmalloc_{dma,}_caches for the disired-size one.
2474 * This is a "chicken-and-egg" problem.
2476 * So the off-slab slab descriptor shall come from the kmalloc_{dma,}_caches,
2477 * which are all initialized during kmem_cache_init().
2479 static void *alloc_slabmgmt(struct kmem_cache
*cachep
,
2480 struct page
*page
, int colour_off
,
2481 gfp_t local_flags
, int nodeid
)
2484 void *addr
= page_address(page
);
2486 if (OFF_SLAB(cachep
)) {
2487 /* Slab management obj is off-slab. */
2488 freelist
= kmem_cache_alloc_node(cachep
->freelist_cache
,
2489 local_flags
, nodeid
);
2493 freelist
= addr
+ colour_off
;
2494 colour_off
+= cachep
->freelist_size
;
2497 page
->s_mem
= addr
+ colour_off
;
2501 static inline freelist_idx_t
get_free_obj(struct page
*page
, unsigned int idx
)
2503 return ((freelist_idx_t
*)page
->freelist
)[idx
];
2506 static inline void set_free_obj(struct page
*page
,
2507 unsigned int idx
, freelist_idx_t val
)
2509 ((freelist_idx_t
*)(page
->freelist
))[idx
] = val
;
2512 static void cache_init_objs(struct kmem_cache
*cachep
,
2517 for (i
= 0; i
< cachep
->num
; i
++) {
2518 void *objp
= index_to_obj(cachep
, page
, i
);
2520 if (cachep
->flags
& SLAB_STORE_USER
)
2521 *dbg_userword(cachep
, objp
) = NULL
;
2523 if (cachep
->flags
& SLAB_RED_ZONE
) {
2524 *dbg_redzone1(cachep
, objp
) = RED_INACTIVE
;
2525 *dbg_redzone2(cachep
, objp
) = RED_INACTIVE
;
2528 * Constructors are not allowed to allocate memory from the same
2529 * cache which they are a constructor for. Otherwise, deadlock.
2530 * They must also be threaded.
2532 if (cachep
->ctor
&& !(cachep
->flags
& SLAB_POISON
))
2533 cachep
->ctor(objp
+ obj_offset(cachep
));
2535 if (cachep
->flags
& SLAB_RED_ZONE
) {
2536 if (*dbg_redzone2(cachep
, objp
) != RED_INACTIVE
)
2537 slab_error(cachep
, "constructor overwrote the"
2538 " end of an object");
2539 if (*dbg_redzone1(cachep
, objp
) != RED_INACTIVE
)
2540 slab_error(cachep
, "constructor overwrote the"
2541 " start of an object");
2543 /* need to poison the objs? */
2544 if (cachep
->flags
& SLAB_POISON
) {
2545 poison_obj(cachep
, objp
, POISON_FREE
);
2546 slab_kernel_map(cachep
, objp
, 0, 0);
2552 set_obj_status(page
, i
, OBJECT_FREE
);
2553 set_free_obj(page
, i
, i
);
2557 static void kmem_flagcheck(struct kmem_cache
*cachep
, gfp_t flags
)
2559 if (CONFIG_ZONE_DMA_FLAG
) {
2560 if (flags
& GFP_DMA
)
2561 BUG_ON(!(cachep
->allocflags
& GFP_DMA
));
2563 BUG_ON(cachep
->allocflags
& GFP_DMA
);
2567 static void *slab_get_obj(struct kmem_cache
*cachep
, struct page
*page
,
2572 objp
= index_to_obj(cachep
, page
, get_free_obj(page
, page
->active
));
2575 WARN_ON(page_to_nid(virt_to_page(objp
)) != nodeid
);
2579 if (cachep
->flags
& SLAB_STORE_USER
)
2580 set_store_user_dirty(cachep
);
2586 static void slab_put_obj(struct kmem_cache
*cachep
, struct page
*page
,
2587 void *objp
, int nodeid
)
2589 unsigned int objnr
= obj_to_index(cachep
, page
, objp
);
2593 /* Verify that the slab belongs to the intended node */
2594 WARN_ON(page_to_nid(virt_to_page(objp
)) != nodeid
);
2596 /* Verify double free bug */
2597 for (i
= page
->active
; i
< cachep
->num
; i
++) {
2598 if (get_free_obj(page
, i
) == objnr
) {
2599 printk(KERN_ERR
"slab: double free detected in cache "
2600 "'%s', objp %p\n", cachep
->name
, objp
);
2606 set_free_obj(page
, page
->active
, objnr
);
2610 * Map pages beginning at addr to the given cache and slab. This is required
2611 * for the slab allocator to be able to lookup the cache and slab of a
2612 * virtual address for kfree, ksize, and slab debugging.
2614 static void slab_map_pages(struct kmem_cache
*cache
, struct page
*page
,
2617 page
->slab_cache
= cache
;
2618 page
->freelist
= freelist
;
2622 * Grow (by 1) the number of slabs within a cache. This is called by
2623 * kmem_cache_alloc() when there are no active objs left in a cache.
2625 static int cache_grow(struct kmem_cache
*cachep
,
2626 gfp_t flags
, int nodeid
, struct page
*page
)
2631 struct kmem_cache_node
*n
;
2634 * Be lazy and only check for valid flags here, keeping it out of the
2635 * critical path in kmem_cache_alloc().
2637 if (unlikely(flags
& GFP_SLAB_BUG_MASK
)) {
2638 pr_emerg("gfp: %u\n", flags
& GFP_SLAB_BUG_MASK
);
2641 local_flags
= flags
& (GFP_CONSTRAINT_MASK
|GFP_RECLAIM_MASK
);
2643 /* Take the node list lock to change the colour_next on this node */
2645 n
= get_node(cachep
, nodeid
);
2646 spin_lock(&n
->list_lock
);
2648 /* Get colour for the slab, and cal the next value. */
2649 offset
= n
->colour_next
;
2651 if (n
->colour_next
>= cachep
->colour
)
2653 spin_unlock(&n
->list_lock
);
2655 offset
*= cachep
->colour_off
;
2657 if (gfpflags_allow_blocking(local_flags
))
2661 * The test for missing atomic flag is performed here, rather than
2662 * the more obvious place, simply to reduce the critical path length
2663 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2664 * will eventually be caught here (where it matters).
2666 kmem_flagcheck(cachep
, flags
);
2669 * Get mem for the objs. Attempt to allocate a physical page from
2673 page
= kmem_getpages(cachep
, local_flags
, nodeid
);
2677 /* Get slab management. */
2678 freelist
= alloc_slabmgmt(cachep
, page
, offset
,
2679 local_flags
& ~GFP_CONSTRAINT_MASK
, nodeid
);
2683 slab_map_pages(cachep
, page
, freelist
);
2685 cache_init_objs(cachep
, page
);
2687 if (gfpflags_allow_blocking(local_flags
))
2688 local_irq_disable();
2690 spin_lock(&n
->list_lock
);
2692 /* Make slab active. */
2693 list_add_tail(&page
->lru
, &(n
->slabs_free
));
2694 STATS_INC_GROWN(cachep
);
2695 n
->free_objects
+= cachep
->num
;
2696 spin_unlock(&n
->list_lock
);
2699 kmem_freepages(cachep
, page
);
2701 if (gfpflags_allow_blocking(local_flags
))
2702 local_irq_disable();
2709 * Perform extra freeing checks:
2710 * - detect bad pointers.
2711 * - POISON/RED_ZONE checking
2713 static void kfree_debugcheck(const void *objp
)
2715 if (!virt_addr_valid(objp
)) {
2716 printk(KERN_ERR
"kfree_debugcheck: out of range ptr %lxh.\n",
2717 (unsigned long)objp
);
2722 static inline void verify_redzone_free(struct kmem_cache
*cache
, void *obj
)
2724 unsigned long long redzone1
, redzone2
;
2726 redzone1
= *dbg_redzone1(cache
, obj
);
2727 redzone2
= *dbg_redzone2(cache
, obj
);
2732 if (redzone1
== RED_ACTIVE
&& redzone2
== RED_ACTIVE
)
2735 if (redzone1
== RED_INACTIVE
&& redzone2
== RED_INACTIVE
)
2736 slab_error(cache
, "double free detected");
2738 slab_error(cache
, "memory outside object was overwritten");
2740 printk(KERN_ERR
"%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
2741 obj
, redzone1
, redzone2
);
2744 static void *cache_free_debugcheck(struct kmem_cache
*cachep
, void *objp
,
2745 unsigned long caller
)
2750 BUG_ON(virt_to_cache(objp
) != cachep
);
2752 objp
-= obj_offset(cachep
);
2753 kfree_debugcheck(objp
);
2754 page
= virt_to_head_page(objp
);
2756 if (cachep
->flags
& SLAB_RED_ZONE
) {
2757 verify_redzone_free(cachep
, objp
);
2758 *dbg_redzone1(cachep
, objp
) = RED_INACTIVE
;
2759 *dbg_redzone2(cachep
, objp
) = RED_INACTIVE
;
2761 if (cachep
->flags
& SLAB_STORE_USER
) {
2762 set_store_user_dirty(cachep
);
2763 *dbg_userword(cachep
, objp
) = (void *)caller
;
2766 objnr
= obj_to_index(cachep
, page
, objp
);
2768 BUG_ON(objnr
>= cachep
->num
);
2769 BUG_ON(objp
!= index_to_obj(cachep
, page
, objnr
));
2771 set_obj_status(page
, objnr
, OBJECT_FREE
);
2772 if (cachep
->flags
& SLAB_POISON
) {
2773 poison_obj(cachep
, objp
, POISON_FREE
);
2774 slab_kernel_map(cachep
, objp
, 0, caller
);
2780 #define kfree_debugcheck(x) do { } while(0)
2781 #define cache_free_debugcheck(x,objp,z) (objp)
2784 static void *cache_alloc_refill(struct kmem_cache
*cachep
, gfp_t flags
,
2788 struct kmem_cache_node
*n
;
2789 struct array_cache
*ac
;
2793 node
= numa_mem_id();
2794 if (unlikely(force_refill
))
2797 ac
= cpu_cache_get(cachep
);
2798 batchcount
= ac
->batchcount
;
2799 if (!ac
->touched
&& batchcount
> BATCHREFILL_LIMIT
) {
2801 * If there was little recent activity on this cache, then
2802 * perform only a partial refill. Otherwise we could generate
2805 batchcount
= BATCHREFILL_LIMIT
;
2807 n
= get_node(cachep
, node
);
2809 BUG_ON(ac
->avail
> 0 || !n
);
2810 spin_lock(&n
->list_lock
);
2812 /* See if we can refill from the shared array */
2813 if (n
->shared
&& transfer_objects(ac
, n
->shared
, batchcount
)) {
2814 n
->shared
->touched
= 1;
2818 while (batchcount
> 0) {
2819 struct list_head
*entry
;
2821 /* Get slab alloc is to come from. */
2822 entry
= n
->slabs_partial
.next
;
2823 if (entry
== &n
->slabs_partial
) {
2824 n
->free_touched
= 1;
2825 entry
= n
->slabs_free
.next
;
2826 if (entry
== &n
->slabs_free
)
2830 page
= list_entry(entry
, struct page
, lru
);
2831 check_spinlock_acquired(cachep
);
2834 * The slab was either on partial or free list so
2835 * there must be at least one object available for
2838 BUG_ON(page
->active
>= cachep
->num
);
2840 while (page
->active
< cachep
->num
&& batchcount
--) {
2841 STATS_INC_ALLOCED(cachep
);
2842 STATS_INC_ACTIVE(cachep
);
2843 STATS_SET_HIGH(cachep
);
2845 ac_put_obj(cachep
, ac
, slab_get_obj(cachep
, page
,
2849 /* move slabp to correct slabp list: */
2850 list_del(&page
->lru
);
2851 if (page
->active
== cachep
->num
)
2852 list_add(&page
->lru
, &n
->slabs_full
);
2854 list_add(&page
->lru
, &n
->slabs_partial
);
2858 n
->free_objects
-= ac
->avail
;
2860 spin_unlock(&n
->list_lock
);
2862 if (unlikely(!ac
->avail
)) {
2865 x
= cache_grow(cachep
, gfp_exact_node(flags
), node
, NULL
);
2867 /* cache_grow can reenable interrupts, then ac could change. */
2868 ac
= cpu_cache_get(cachep
);
2869 node
= numa_mem_id();
2871 /* no objects in sight? abort */
2872 if (!x
&& (ac
->avail
== 0 || force_refill
))
2875 if (!ac
->avail
) /* objects refilled by interrupt? */
2880 return ac_get_obj(cachep
, ac
, flags
, force_refill
);
2883 static inline void cache_alloc_debugcheck_before(struct kmem_cache
*cachep
,
2886 might_sleep_if(gfpflags_allow_blocking(flags
));
2888 kmem_flagcheck(cachep
, flags
);
2893 static void *cache_alloc_debugcheck_after(struct kmem_cache
*cachep
,
2894 gfp_t flags
, void *objp
, unsigned long caller
)
2900 if (cachep
->flags
& SLAB_POISON
) {
2901 check_poison_obj(cachep
, objp
);
2902 slab_kernel_map(cachep
, objp
, 1, 0);
2903 poison_obj(cachep
, objp
, POISON_INUSE
);
2905 if (cachep
->flags
& SLAB_STORE_USER
)
2906 *dbg_userword(cachep
, objp
) = (void *)caller
;
2908 if (cachep
->flags
& SLAB_RED_ZONE
) {
2909 if (*dbg_redzone1(cachep
, objp
) != RED_INACTIVE
||
2910 *dbg_redzone2(cachep
, objp
) != RED_INACTIVE
) {
2911 slab_error(cachep
, "double free, or memory outside"
2912 " object was overwritten");
2914 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
2915 objp
, *dbg_redzone1(cachep
, objp
),
2916 *dbg_redzone2(cachep
, objp
));
2918 *dbg_redzone1(cachep
, objp
) = RED_ACTIVE
;
2919 *dbg_redzone2(cachep
, objp
) = RED_ACTIVE
;
2922 page
= virt_to_head_page(objp
);
2923 set_obj_status(page
, obj_to_index(cachep
, page
, objp
), OBJECT_ACTIVE
);
2924 objp
+= obj_offset(cachep
);
2925 if (cachep
->ctor
&& cachep
->flags
& SLAB_POISON
)
2927 if (ARCH_SLAB_MINALIGN
&&
2928 ((unsigned long)objp
& (ARCH_SLAB_MINALIGN
-1))) {
2929 printk(KERN_ERR
"0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
2930 objp
, (int)ARCH_SLAB_MINALIGN
);
2935 #define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
2938 static bool slab_should_failslab(struct kmem_cache
*cachep
, gfp_t flags
)
2940 if (unlikely(cachep
== kmem_cache
))
2943 return should_failslab(cachep
->object_size
, flags
, cachep
->flags
);
2946 static inline void *____cache_alloc(struct kmem_cache
*cachep
, gfp_t flags
)
2949 struct array_cache
*ac
;
2950 bool force_refill
= false;
2954 ac
= cpu_cache_get(cachep
);
2955 if (likely(ac
->avail
)) {
2957 objp
= ac_get_obj(cachep
, ac
, flags
, false);
2960 * Allow for the possibility all avail objects are not allowed
2961 * by the current flags
2964 STATS_INC_ALLOCHIT(cachep
);
2967 force_refill
= true;
2970 STATS_INC_ALLOCMISS(cachep
);
2971 objp
= cache_alloc_refill(cachep
, flags
, force_refill
);
2973 * the 'ac' may be updated by cache_alloc_refill(),
2974 * and kmemleak_erase() requires its correct value.
2976 ac
= cpu_cache_get(cachep
);
2980 * To avoid a false negative, if an object that is in one of the
2981 * per-CPU caches is leaked, we need to make sure kmemleak doesn't
2982 * treat the array pointers as a reference to the object.
2985 kmemleak_erase(&ac
->entry
[ac
->avail
]);
2991 * Try allocating on another node if PFA_SPREAD_SLAB is a mempolicy is set.
2993 * If we are in_interrupt, then process context, including cpusets and
2994 * mempolicy, may not apply and should not be used for allocation policy.
2996 static void *alternate_node_alloc(struct kmem_cache
*cachep
, gfp_t flags
)
2998 int nid_alloc
, nid_here
;
3000 if (in_interrupt() || (flags
& __GFP_THISNODE
))
3002 nid_alloc
= nid_here
= numa_mem_id();
3003 if (cpuset_do_slab_mem_spread() && (cachep
->flags
& SLAB_MEM_SPREAD
))
3004 nid_alloc
= cpuset_slab_spread_node();
3005 else if (current
->mempolicy
)
3006 nid_alloc
= mempolicy_slab_node();
3007 if (nid_alloc
!= nid_here
)
3008 return ____cache_alloc_node(cachep
, flags
, nid_alloc
);
3013 * Fallback function if there was no memory available and no objects on a
3014 * certain node and fall back is permitted. First we scan all the
3015 * available node for available objects. If that fails then we
3016 * perform an allocation without specifying a node. This allows the page
3017 * allocator to do its reclaim / fallback magic. We then insert the
3018 * slab into the proper nodelist and then allocate from it.
3020 static void *fallback_alloc(struct kmem_cache
*cache
, gfp_t flags
)
3022 struct zonelist
*zonelist
;
3026 enum zone_type high_zoneidx
= gfp_zone(flags
);
3029 unsigned int cpuset_mems_cookie
;
3031 if (flags
& __GFP_THISNODE
)
3034 local_flags
= flags
& (GFP_CONSTRAINT_MASK
|GFP_RECLAIM_MASK
);
3037 cpuset_mems_cookie
= read_mems_allowed_begin();
3038 zonelist
= node_zonelist(mempolicy_slab_node(), flags
);
3042 * Look through allowed nodes for objects available
3043 * from existing per node queues.
3045 for_each_zone_zonelist(zone
, z
, zonelist
, high_zoneidx
) {
3046 nid
= zone_to_nid(zone
);
3048 if (cpuset_zone_allowed(zone
, flags
) &&
3049 get_node(cache
, nid
) &&
3050 get_node(cache
, nid
)->free_objects
) {
3051 obj
= ____cache_alloc_node(cache
,
3052 gfp_exact_node(flags
), nid
);
3060 * This allocation will be performed within the constraints
3061 * of the current cpuset / memory policy requirements.
3062 * We may trigger various forms of reclaim on the allowed
3063 * set and go into memory reserves if necessary.
3067 if (gfpflags_allow_blocking(local_flags
))
3069 kmem_flagcheck(cache
, flags
);
3070 page
= kmem_getpages(cache
, local_flags
, numa_mem_id());
3071 if (gfpflags_allow_blocking(local_flags
))
3072 local_irq_disable();
3075 * Insert into the appropriate per node queues
3077 nid
= page_to_nid(page
);
3078 if (cache_grow(cache
, flags
, nid
, page
)) {
3079 obj
= ____cache_alloc_node(cache
,
3080 gfp_exact_node(flags
), nid
);
3083 * Another processor may allocate the
3084 * objects in the slab since we are
3085 * not holding any locks.
3089 /* cache_grow already freed obj */
3095 if (unlikely(!obj
&& read_mems_allowed_retry(cpuset_mems_cookie
)))
3101 * A interface to enable slab creation on nodeid
3103 static void *____cache_alloc_node(struct kmem_cache
*cachep
, gfp_t flags
,
3106 struct list_head
*entry
;
3108 struct kmem_cache_node
*n
;
3112 VM_BUG_ON(nodeid
< 0 || nodeid
>= MAX_NUMNODES
);
3113 n
= get_node(cachep
, nodeid
);
3118 spin_lock(&n
->list_lock
);
3119 entry
= n
->slabs_partial
.next
;
3120 if (entry
== &n
->slabs_partial
) {
3121 n
->free_touched
= 1;
3122 entry
= n
->slabs_free
.next
;
3123 if (entry
== &n
->slabs_free
)
3127 page
= list_entry(entry
, struct page
, lru
);
3128 check_spinlock_acquired_node(cachep
, nodeid
);
3130 STATS_INC_NODEALLOCS(cachep
);
3131 STATS_INC_ACTIVE(cachep
);
3132 STATS_SET_HIGH(cachep
);
3134 BUG_ON(page
->active
== cachep
->num
);
3136 obj
= slab_get_obj(cachep
, page
, nodeid
);
3138 /* move slabp to correct slabp list: */
3139 list_del(&page
->lru
);
3141 if (page
->active
== cachep
->num
)
3142 list_add(&page
->lru
, &n
->slabs_full
);
3144 list_add(&page
->lru
, &n
->slabs_partial
);
3146 spin_unlock(&n
->list_lock
);
3150 spin_unlock(&n
->list_lock
);
3151 x
= cache_grow(cachep
, gfp_exact_node(flags
), nodeid
, NULL
);
3155 return fallback_alloc(cachep
, flags
);
3161 static __always_inline
void *
3162 slab_alloc_node(struct kmem_cache
*cachep
, gfp_t flags
, int nodeid
,
3163 unsigned long caller
)
3165 unsigned long save_flags
;
3167 int slab_node
= numa_mem_id();
3169 flags
&= gfp_allowed_mask
;
3171 lockdep_trace_alloc(flags
);
3173 if (slab_should_failslab(cachep
, flags
))
3176 cachep
= memcg_kmem_get_cache(cachep
, flags
);
3178 cache_alloc_debugcheck_before(cachep
, flags
);
3179 local_irq_save(save_flags
);
3181 if (nodeid
== NUMA_NO_NODE
)
3184 if (unlikely(!get_node(cachep
, nodeid
))) {
3185 /* Node not bootstrapped yet */
3186 ptr
= fallback_alloc(cachep
, flags
);
3190 if (nodeid
== slab_node
) {
3192 * Use the locally cached objects if possible.
3193 * However ____cache_alloc does not allow fallback
3194 * to other nodes. It may fail while we still have
3195 * objects on other nodes available.
3197 ptr
= ____cache_alloc(cachep
, flags
);
3201 /* ___cache_alloc_node can fall back to other nodes */
3202 ptr
= ____cache_alloc_node(cachep
, flags
, nodeid
);
3204 local_irq_restore(save_flags
);
3205 ptr
= cache_alloc_debugcheck_after(cachep
, flags
, ptr
, caller
);
3206 kmemleak_alloc_recursive(ptr
, cachep
->object_size
, 1, cachep
->flags
,
3210 kmemcheck_slab_alloc(cachep
, flags
, ptr
, cachep
->object_size
);
3211 if (unlikely(flags
& __GFP_ZERO
))
3212 memset(ptr
, 0, cachep
->object_size
);
3215 memcg_kmem_put_cache(cachep
);
3219 static __always_inline
void *
3220 __do_cache_alloc(struct kmem_cache
*cache
, gfp_t flags
)
3224 if (current
->mempolicy
|| cpuset_do_slab_mem_spread()) {
3225 objp
= alternate_node_alloc(cache
, flags
);
3229 objp
= ____cache_alloc(cache
, flags
);
3232 * We may just have run out of memory on the local node.
3233 * ____cache_alloc_node() knows how to locate memory on other nodes
3236 objp
= ____cache_alloc_node(cache
, flags
, numa_mem_id());
3243 static __always_inline
void *
3244 __do_cache_alloc(struct kmem_cache
*cachep
, gfp_t flags
)
3246 return ____cache_alloc(cachep
, flags
);
3249 #endif /* CONFIG_NUMA */
3251 static __always_inline
void *
3252 slab_alloc(struct kmem_cache
*cachep
, gfp_t flags
, unsigned long caller
)
3254 unsigned long save_flags
;
3257 flags
&= gfp_allowed_mask
;
3259 lockdep_trace_alloc(flags
);
3261 if (slab_should_failslab(cachep
, flags
))
3264 cachep
= memcg_kmem_get_cache(cachep
, flags
);
3266 cache_alloc_debugcheck_before(cachep
, flags
);
3267 local_irq_save(save_flags
);
3268 objp
= __do_cache_alloc(cachep
, flags
);
3269 local_irq_restore(save_flags
);
3270 objp
= cache_alloc_debugcheck_after(cachep
, flags
, objp
, caller
);
3271 kmemleak_alloc_recursive(objp
, cachep
->object_size
, 1, cachep
->flags
,
3276 kmemcheck_slab_alloc(cachep
, flags
, objp
, cachep
->object_size
);
3277 if (unlikely(flags
& __GFP_ZERO
))
3278 memset(objp
, 0, cachep
->object_size
);
3281 memcg_kmem_put_cache(cachep
);
3286 * Caller needs to acquire correct kmem_cache_node's list_lock
3287 * @list: List of detached free slabs should be freed by caller
3289 static void free_block(struct kmem_cache
*cachep
, void **objpp
,
3290 int nr_objects
, int node
, struct list_head
*list
)
3293 struct kmem_cache_node
*n
= get_node(cachep
, node
);
3295 for (i
= 0; i
< nr_objects
; i
++) {
3299 clear_obj_pfmemalloc(&objpp
[i
]);
3302 page
= virt_to_head_page(objp
);
3303 list_del(&page
->lru
);
3304 check_spinlock_acquired_node(cachep
, node
);
3305 slab_put_obj(cachep
, page
, objp
, node
);
3306 STATS_DEC_ACTIVE(cachep
);
3309 /* fixup slab chains */
3310 if (page
->active
== 0) {
3311 if (n
->free_objects
> n
->free_limit
) {
3312 n
->free_objects
-= cachep
->num
;
3313 list_add_tail(&page
->lru
, list
);
3315 list_add(&page
->lru
, &n
->slabs_free
);
3318 /* Unconditionally move a slab to the end of the
3319 * partial list on free - maximum time for the
3320 * other objects to be freed, too.
3322 list_add_tail(&page
->lru
, &n
->slabs_partial
);
3327 static void cache_flusharray(struct kmem_cache
*cachep
, struct array_cache
*ac
)
3330 struct kmem_cache_node
*n
;
3331 int node
= numa_mem_id();
3334 batchcount
= ac
->batchcount
;
3336 BUG_ON(!batchcount
|| batchcount
> ac
->avail
);
3339 n
= get_node(cachep
, node
);
3340 spin_lock(&n
->list_lock
);
3342 struct array_cache
*shared_array
= n
->shared
;
3343 int max
= shared_array
->limit
- shared_array
->avail
;
3345 if (batchcount
> max
)
3347 memcpy(&(shared_array
->entry
[shared_array
->avail
]),
3348 ac
->entry
, sizeof(void *) * batchcount
);
3349 shared_array
->avail
+= batchcount
;
3354 free_block(cachep
, ac
->entry
, batchcount
, node
, &list
);
3359 struct list_head
*p
;
3361 p
= n
->slabs_free
.next
;
3362 while (p
!= &(n
->slabs_free
)) {
3365 page
= list_entry(p
, struct page
, lru
);
3366 BUG_ON(page
->active
);
3371 STATS_SET_FREEABLE(cachep
, i
);
3374 spin_unlock(&n
->list_lock
);
3375 slabs_destroy(cachep
, &list
);
3376 ac
->avail
-= batchcount
;
3377 memmove(ac
->entry
, &(ac
->entry
[batchcount
]), sizeof(void *)*ac
->avail
);
3381 * Release an obj back to its cache. If the obj has a constructed state, it must
3382 * be in this state _before_ it is released. Called with disabled ints.
3384 static inline void __cache_free(struct kmem_cache
*cachep
, void *objp
,
3385 unsigned long caller
)
3387 struct array_cache
*ac
= cpu_cache_get(cachep
);
3390 kmemleak_free_recursive(objp
, cachep
->flags
);
3391 objp
= cache_free_debugcheck(cachep
, objp
, caller
);
3393 kmemcheck_slab_free(cachep
, objp
, cachep
->object_size
);
3396 * Skip calling cache_free_alien() when the platform is not numa.
3397 * This will avoid cache misses that happen while accessing slabp (which
3398 * is per page memory reference) to get nodeid. Instead use a global
3399 * variable to skip the call, which is mostly likely to be present in
3402 if (nr_online_nodes
> 1 && cache_free_alien(cachep
, objp
))
3405 if (ac
->avail
< ac
->limit
) {
3406 STATS_INC_FREEHIT(cachep
);
3408 STATS_INC_FREEMISS(cachep
);
3409 cache_flusharray(cachep
, ac
);
3412 ac_put_obj(cachep
, ac
, objp
);
3416 * kmem_cache_alloc - Allocate an object
3417 * @cachep: The cache to allocate from.
3418 * @flags: See kmalloc().
3420 * Allocate an object from this cache. The flags are only relevant
3421 * if the cache has no available objects.
3423 void *kmem_cache_alloc(struct kmem_cache
*cachep
, gfp_t flags
)
3425 void *ret
= slab_alloc(cachep
, flags
, _RET_IP_
);
3427 trace_kmem_cache_alloc(_RET_IP_
, ret
,
3428 cachep
->object_size
, cachep
->size
, flags
);
3432 EXPORT_SYMBOL(kmem_cache_alloc
);
3434 void kmem_cache_free_bulk(struct kmem_cache
*s
, size_t size
, void **p
)
3436 __kmem_cache_free_bulk(s
, size
, p
);
3438 EXPORT_SYMBOL(kmem_cache_free_bulk
);
3440 int kmem_cache_alloc_bulk(struct kmem_cache
*s
, gfp_t flags
, size_t size
,
3443 return __kmem_cache_alloc_bulk(s
, flags
, size
, p
);
3445 EXPORT_SYMBOL(kmem_cache_alloc_bulk
);
3447 #ifdef CONFIG_TRACING
3449 kmem_cache_alloc_trace(struct kmem_cache
*cachep
, gfp_t flags
, size_t size
)
3453 ret
= slab_alloc(cachep
, flags
, _RET_IP_
);
3455 trace_kmalloc(_RET_IP_
, ret
,
3456 size
, cachep
->size
, flags
);
3459 EXPORT_SYMBOL(kmem_cache_alloc_trace
);
3464 * kmem_cache_alloc_node - Allocate an object on the specified node
3465 * @cachep: The cache to allocate from.
3466 * @flags: See kmalloc().
3467 * @nodeid: node number of the target node.
3469 * Identical to kmem_cache_alloc but it will allocate memory on the given
3470 * node, which can improve the performance for cpu bound structures.
3472 * Fallback to other node is possible if __GFP_THISNODE is not set.
3474 void *kmem_cache_alloc_node(struct kmem_cache
*cachep
, gfp_t flags
, int nodeid
)
3476 void *ret
= slab_alloc_node(cachep
, flags
, nodeid
, _RET_IP_
);
3478 trace_kmem_cache_alloc_node(_RET_IP_
, ret
,
3479 cachep
->object_size
, cachep
->size
,
3484 EXPORT_SYMBOL(kmem_cache_alloc_node
);
3486 #ifdef CONFIG_TRACING
3487 void *kmem_cache_alloc_node_trace(struct kmem_cache
*cachep
,
3494 ret
= slab_alloc_node(cachep
, flags
, nodeid
, _RET_IP_
);
3496 trace_kmalloc_node(_RET_IP_
, ret
,
3501 EXPORT_SYMBOL(kmem_cache_alloc_node_trace
);
3504 static __always_inline
void *
3505 __do_kmalloc_node(size_t size
, gfp_t flags
, int node
, unsigned long caller
)
3507 struct kmem_cache
*cachep
;
3509 cachep
= kmalloc_slab(size
, flags
);
3510 if (unlikely(ZERO_OR_NULL_PTR(cachep
)))
3512 return kmem_cache_alloc_node_trace(cachep
, flags
, node
, size
);
3515 void *__kmalloc_node(size_t size
, gfp_t flags
, int node
)
3517 return __do_kmalloc_node(size
, flags
, node
, _RET_IP_
);
3519 EXPORT_SYMBOL(__kmalloc_node
);
3521 void *__kmalloc_node_track_caller(size_t size
, gfp_t flags
,
3522 int node
, unsigned long caller
)
3524 return __do_kmalloc_node(size
, flags
, node
, caller
);
3526 EXPORT_SYMBOL(__kmalloc_node_track_caller
);
3527 #endif /* CONFIG_NUMA */
3530 * __do_kmalloc - allocate memory
3531 * @size: how many bytes of memory are required.
3532 * @flags: the type of memory to allocate (see kmalloc).
3533 * @caller: function caller for debug tracking of the caller
3535 static __always_inline
void *__do_kmalloc(size_t size
, gfp_t flags
,
3536 unsigned long caller
)
3538 struct kmem_cache
*cachep
;
3541 cachep
= kmalloc_slab(size
, flags
);
3542 if (unlikely(ZERO_OR_NULL_PTR(cachep
)))
3544 ret
= slab_alloc(cachep
, flags
, caller
);
3546 trace_kmalloc(caller
, ret
,
3547 size
, cachep
->size
, flags
);
3552 void *__kmalloc(size_t size
, gfp_t flags
)
3554 return __do_kmalloc(size
, flags
, _RET_IP_
);
3556 EXPORT_SYMBOL(__kmalloc
);
3558 void *__kmalloc_track_caller(size_t size
, gfp_t flags
, unsigned long caller
)
3560 return __do_kmalloc(size
, flags
, caller
);
3562 EXPORT_SYMBOL(__kmalloc_track_caller
);
3565 * kmem_cache_free - Deallocate an object
3566 * @cachep: The cache the allocation was from.
3567 * @objp: The previously allocated object.
3569 * Free an object which was previously allocated from this
3572 void kmem_cache_free(struct kmem_cache
*cachep
, void *objp
)
3574 unsigned long flags
;
3575 cachep
= cache_from_obj(cachep
, objp
);
3579 local_irq_save(flags
);
3580 debug_check_no_locks_freed(objp
, cachep
->object_size
);
3581 if (!(cachep
->flags
& SLAB_DEBUG_OBJECTS
))
3582 debug_check_no_obj_freed(objp
, cachep
->object_size
);
3583 __cache_free(cachep
, objp
, _RET_IP_
);
3584 local_irq_restore(flags
);
3586 trace_kmem_cache_free(_RET_IP_
, objp
);
3588 EXPORT_SYMBOL(kmem_cache_free
);
3591 * kfree - free previously allocated memory
3592 * @objp: pointer returned by kmalloc.
3594 * If @objp is NULL, no operation is performed.
3596 * Don't free memory not originally allocated by kmalloc()
3597 * or you will run into trouble.
3599 void kfree(const void *objp
)
3601 struct kmem_cache
*c
;
3602 unsigned long flags
;
3604 trace_kfree(_RET_IP_
, objp
);
3606 if (unlikely(ZERO_OR_NULL_PTR(objp
)))
3608 local_irq_save(flags
);
3609 kfree_debugcheck(objp
);
3610 c
= virt_to_cache(objp
);
3611 debug_check_no_locks_freed(objp
, c
->object_size
);
3613 debug_check_no_obj_freed(objp
, c
->object_size
);
3614 __cache_free(c
, (void *)objp
, _RET_IP_
);
3615 local_irq_restore(flags
);
3617 EXPORT_SYMBOL(kfree
);
3620 * This initializes kmem_cache_node or resizes various caches for all nodes.
3622 static int alloc_kmem_cache_node(struct kmem_cache
*cachep
, gfp_t gfp
)
3625 struct kmem_cache_node
*n
;
3626 struct array_cache
*new_shared
;
3627 struct alien_cache
**new_alien
= NULL
;
3629 for_each_online_node(node
) {
3631 if (use_alien_caches
) {
3632 new_alien
= alloc_alien_cache(node
, cachep
->limit
, gfp
);
3638 if (cachep
->shared
) {
3639 new_shared
= alloc_arraycache(node
,
3640 cachep
->shared
*cachep
->batchcount
,
3643 free_alien_cache(new_alien
);
3648 n
= get_node(cachep
, node
);
3650 struct array_cache
*shared
= n
->shared
;
3653 spin_lock_irq(&n
->list_lock
);
3656 free_block(cachep
, shared
->entry
,
3657 shared
->avail
, node
, &list
);
3659 n
->shared
= new_shared
;
3661 n
->alien
= new_alien
;
3664 n
->free_limit
= (1 + nr_cpus_node(node
)) *
3665 cachep
->batchcount
+ cachep
->num
;
3666 spin_unlock_irq(&n
->list_lock
);
3667 slabs_destroy(cachep
, &list
);
3669 free_alien_cache(new_alien
);
3672 n
= kmalloc_node(sizeof(struct kmem_cache_node
), gfp
, node
);
3674 free_alien_cache(new_alien
);
3679 kmem_cache_node_init(n
);
3680 n
->next_reap
= jiffies
+ REAPTIMEOUT_NODE
+
3681 ((unsigned long)cachep
) % REAPTIMEOUT_NODE
;
3682 n
->shared
= new_shared
;
3683 n
->alien
= new_alien
;
3684 n
->free_limit
= (1 + nr_cpus_node(node
)) *
3685 cachep
->batchcount
+ cachep
->num
;
3686 cachep
->node
[node
] = n
;
3691 if (!cachep
->list
.next
) {
3692 /* Cache is not active yet. Roll back what we did */
3695 n
= get_node(cachep
, node
);
3698 free_alien_cache(n
->alien
);
3700 cachep
->node
[node
] = NULL
;
3708 /* Always called with the slab_mutex held */
3709 static int __do_tune_cpucache(struct kmem_cache
*cachep
, int limit
,
3710 int batchcount
, int shared
, gfp_t gfp
)
3712 struct array_cache __percpu
*cpu_cache
, *prev
;
3715 cpu_cache
= alloc_kmem_cache_cpus(cachep
, limit
, batchcount
);
3719 prev
= cachep
->cpu_cache
;
3720 cachep
->cpu_cache
= cpu_cache
;
3721 kick_all_cpus_sync();
3724 cachep
->batchcount
= batchcount
;
3725 cachep
->limit
= limit
;
3726 cachep
->shared
= shared
;
3731 for_each_online_cpu(cpu
) {
3734 struct kmem_cache_node
*n
;
3735 struct array_cache
*ac
= per_cpu_ptr(prev
, cpu
);
3737 node
= cpu_to_mem(cpu
);
3738 n
= get_node(cachep
, node
);
3739 spin_lock_irq(&n
->list_lock
);
3740 free_block(cachep
, ac
->entry
, ac
->avail
, node
, &list
);
3741 spin_unlock_irq(&n
->list_lock
);
3742 slabs_destroy(cachep
, &list
);
3747 return alloc_kmem_cache_node(cachep
, gfp
);
3750 static int do_tune_cpucache(struct kmem_cache
*cachep
, int limit
,
3751 int batchcount
, int shared
, gfp_t gfp
)
3754 struct kmem_cache
*c
;
3756 ret
= __do_tune_cpucache(cachep
, limit
, batchcount
, shared
, gfp
);
3758 if (slab_state
< FULL
)
3761 if ((ret
< 0) || !is_root_cache(cachep
))
3764 lockdep_assert_held(&slab_mutex
);
3765 for_each_memcg_cache(c
, cachep
) {
3766 /* return value determined by the root cache only */
3767 __do_tune_cpucache(c
, limit
, batchcount
, shared
, gfp
);
3773 /* Called with slab_mutex held always */
3774 static int enable_cpucache(struct kmem_cache
*cachep
, gfp_t gfp
)
3781 if (!is_root_cache(cachep
)) {
3782 struct kmem_cache
*root
= memcg_root_cache(cachep
);
3783 limit
= root
->limit
;
3784 shared
= root
->shared
;
3785 batchcount
= root
->batchcount
;
3788 if (limit
&& shared
&& batchcount
)
3791 * The head array serves three purposes:
3792 * - create a LIFO ordering, i.e. return objects that are cache-warm
3793 * - reduce the number of spinlock operations.
3794 * - reduce the number of linked list operations on the slab and
3795 * bufctl chains: array operations are cheaper.
3796 * The numbers are guessed, we should auto-tune as described by
3799 if (cachep
->size
> 131072)
3801 else if (cachep
->size
> PAGE_SIZE
)
3803 else if (cachep
->size
> 1024)
3805 else if (cachep
->size
> 256)
3811 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
3812 * allocation behaviour: Most allocs on one cpu, most free operations
3813 * on another cpu. For these cases, an efficient object passing between
3814 * cpus is necessary. This is provided by a shared array. The array
3815 * replaces Bonwick's magazine layer.
3816 * On uniprocessor, it's functionally equivalent (but less efficient)
3817 * to a larger limit. Thus disabled by default.
3820 if (cachep
->size
<= PAGE_SIZE
&& num_possible_cpus() > 1)
3825 * With debugging enabled, large batchcount lead to excessively long
3826 * periods with disabled local interrupts. Limit the batchcount
3831 batchcount
= (limit
+ 1) / 2;
3833 err
= do_tune_cpucache(cachep
, limit
, batchcount
, shared
, gfp
);
3835 printk(KERN_ERR
"enable_cpucache failed for %s, error %d.\n",
3836 cachep
->name
, -err
);
3841 * Drain an array if it contains any elements taking the node lock only if
3842 * necessary. Note that the node listlock also protects the array_cache
3843 * if drain_array() is used on the shared array.
3845 static void drain_array(struct kmem_cache
*cachep
, struct kmem_cache_node
*n
,
3846 struct array_cache
*ac
, int force
, int node
)
3851 if (!ac
|| !ac
->avail
)
3853 if (ac
->touched
&& !force
) {
3856 spin_lock_irq(&n
->list_lock
);
3858 tofree
= force
? ac
->avail
: (ac
->limit
+ 4) / 5;
3859 if (tofree
> ac
->avail
)
3860 tofree
= (ac
->avail
+ 1) / 2;
3861 free_block(cachep
, ac
->entry
, tofree
, node
, &list
);
3862 ac
->avail
-= tofree
;
3863 memmove(ac
->entry
, &(ac
->entry
[tofree
]),
3864 sizeof(void *) * ac
->avail
);
3866 spin_unlock_irq(&n
->list_lock
);
3867 slabs_destroy(cachep
, &list
);
3872 * cache_reap - Reclaim memory from caches.
3873 * @w: work descriptor
3875 * Called from workqueue/eventd every few seconds.
3877 * - clear the per-cpu caches for this CPU.
3878 * - return freeable pages to the main free memory pool.
3880 * If we cannot acquire the cache chain mutex then just give up - we'll try
3881 * again on the next iteration.
3883 static void cache_reap(struct work_struct
*w
)
3885 struct kmem_cache
*searchp
;
3886 struct kmem_cache_node
*n
;
3887 int node
= numa_mem_id();
3888 struct delayed_work
*work
= to_delayed_work(w
);
3890 if (!mutex_trylock(&slab_mutex
))
3891 /* Give up. Setup the next iteration. */
3894 list_for_each_entry(searchp
, &slab_caches
, list
) {
3898 * We only take the node lock if absolutely necessary and we
3899 * have established with reasonable certainty that
3900 * we can do some work if the lock was obtained.
3902 n
= get_node(searchp
, node
);
3904 reap_alien(searchp
, n
);
3906 drain_array(searchp
, n
, cpu_cache_get(searchp
), 0, node
);
3909 * These are racy checks but it does not matter
3910 * if we skip one check or scan twice.
3912 if (time_after(n
->next_reap
, jiffies
))
3915 n
->next_reap
= jiffies
+ REAPTIMEOUT_NODE
;
3917 drain_array(searchp
, n
, n
->shared
, 0, node
);
3919 if (n
->free_touched
)
3920 n
->free_touched
= 0;
3924 freed
= drain_freelist(searchp
, n
, (n
->free_limit
+
3925 5 * searchp
->num
- 1) / (5 * searchp
->num
));
3926 STATS_ADD_REAPED(searchp
, freed
);
3932 mutex_unlock(&slab_mutex
);
3935 /* Set up the next iteration */
3936 schedule_delayed_work(work
, round_jiffies_relative(REAPTIMEOUT_AC
));
3939 #ifdef CONFIG_SLABINFO
3940 void get_slabinfo(struct kmem_cache
*cachep
, struct slabinfo
*sinfo
)
3943 unsigned long active_objs
;
3944 unsigned long num_objs
;
3945 unsigned long active_slabs
= 0;
3946 unsigned long num_slabs
, free_objects
= 0, shared_avail
= 0;
3950 struct kmem_cache_node
*n
;
3954 for_each_kmem_cache_node(cachep
, node
, n
) {
3957 spin_lock_irq(&n
->list_lock
);
3959 list_for_each_entry(page
, &n
->slabs_full
, lru
) {
3960 if (page
->active
!= cachep
->num
&& !error
)
3961 error
= "slabs_full accounting error";
3962 active_objs
+= cachep
->num
;
3965 list_for_each_entry(page
, &n
->slabs_partial
, lru
) {
3966 if (page
->active
== cachep
->num
&& !error
)
3967 error
= "slabs_partial accounting error";
3968 if (!page
->active
&& !error
)
3969 error
= "slabs_partial accounting error";
3970 active_objs
+= page
->active
;
3973 list_for_each_entry(page
, &n
->slabs_free
, lru
) {
3974 if (page
->active
&& !error
)
3975 error
= "slabs_free accounting error";
3978 free_objects
+= n
->free_objects
;
3980 shared_avail
+= n
->shared
->avail
;
3982 spin_unlock_irq(&n
->list_lock
);
3984 num_slabs
+= active_slabs
;
3985 num_objs
= num_slabs
* cachep
->num
;
3986 if (num_objs
- active_objs
!= free_objects
&& !error
)
3987 error
= "free_objects accounting error";
3989 name
= cachep
->name
;
3991 printk(KERN_ERR
"slab: cache %s error: %s\n", name
, error
);
3993 sinfo
->active_objs
= active_objs
;
3994 sinfo
->num_objs
= num_objs
;
3995 sinfo
->active_slabs
= active_slabs
;
3996 sinfo
->num_slabs
= num_slabs
;
3997 sinfo
->shared_avail
= shared_avail
;
3998 sinfo
->limit
= cachep
->limit
;
3999 sinfo
->batchcount
= cachep
->batchcount
;
4000 sinfo
->shared
= cachep
->shared
;
4001 sinfo
->objects_per_slab
= cachep
->num
;
4002 sinfo
->cache_order
= cachep
->gfporder
;
4005 void slabinfo_show_stats(struct seq_file
*m
, struct kmem_cache
*cachep
)
4009 unsigned long high
= cachep
->high_mark
;
4010 unsigned long allocs
= cachep
->num_allocations
;
4011 unsigned long grown
= cachep
->grown
;
4012 unsigned long reaped
= cachep
->reaped
;
4013 unsigned long errors
= cachep
->errors
;
4014 unsigned long max_freeable
= cachep
->max_freeable
;
4015 unsigned long node_allocs
= cachep
->node_allocs
;
4016 unsigned long node_frees
= cachep
->node_frees
;
4017 unsigned long overflows
= cachep
->node_overflow
;
4019 seq_printf(m
, " : globalstat %7lu %6lu %5lu %4lu "
4020 "%4lu %4lu %4lu %4lu %4lu",
4021 allocs
, high
, grown
,
4022 reaped
, errors
, max_freeable
, node_allocs
,
4023 node_frees
, overflows
);
4027 unsigned long allochit
= atomic_read(&cachep
->allochit
);
4028 unsigned long allocmiss
= atomic_read(&cachep
->allocmiss
);
4029 unsigned long freehit
= atomic_read(&cachep
->freehit
);
4030 unsigned long freemiss
= atomic_read(&cachep
->freemiss
);
4032 seq_printf(m
, " : cpustat %6lu %6lu %6lu %6lu",
4033 allochit
, allocmiss
, freehit
, freemiss
);
4038 #define MAX_SLABINFO_WRITE 128
4040 * slabinfo_write - Tuning for the slab allocator
4042 * @buffer: user buffer
4043 * @count: data length
4046 ssize_t
slabinfo_write(struct file
*file
, const char __user
*buffer
,
4047 size_t count
, loff_t
*ppos
)
4049 char kbuf
[MAX_SLABINFO_WRITE
+ 1], *tmp
;
4050 int limit
, batchcount
, shared
, res
;
4051 struct kmem_cache
*cachep
;
4053 if (count
> MAX_SLABINFO_WRITE
)
4055 if (copy_from_user(&kbuf
, buffer
, count
))
4057 kbuf
[MAX_SLABINFO_WRITE
] = '\0';
4059 tmp
= strchr(kbuf
, ' ');
4064 if (sscanf(tmp
, " %d %d %d", &limit
, &batchcount
, &shared
) != 3)
4067 /* Find the cache in the chain of caches. */
4068 mutex_lock(&slab_mutex
);
4070 list_for_each_entry(cachep
, &slab_caches
, list
) {
4071 if (!strcmp(cachep
->name
, kbuf
)) {
4072 if (limit
< 1 || batchcount
< 1 ||
4073 batchcount
> limit
|| shared
< 0) {
4076 res
= do_tune_cpucache(cachep
, limit
,
4083 mutex_unlock(&slab_mutex
);
4089 #ifdef CONFIG_DEBUG_SLAB_LEAK
4091 static inline int add_caller(unsigned long *n
, unsigned long v
)
4101 unsigned long *q
= p
+ 2 * i
;
4115 memmove(p
+ 2, p
, n
[1] * 2 * sizeof(unsigned long) - ((void *)p
- (void *)n
));
4121 static void handle_slab(unsigned long *n
, struct kmem_cache
*c
,
4130 for (i
= 0, p
= page
->s_mem
; i
< c
->num
; i
++, p
+= c
->size
) {
4133 for (j
= page
->active
; j
< c
->num
; j
++) {
4134 if (get_free_obj(page
, j
) == i
) {
4144 * probe_kernel_read() is used for DEBUG_PAGEALLOC. page table
4145 * mapping is established when actual object allocation and
4146 * we could mistakenly access the unmapped object in the cpu
4149 if (probe_kernel_read(&v
, dbg_userword(c
, p
), sizeof(v
)))
4152 if (!add_caller(n
, v
))
4157 static void show_symbol(struct seq_file
*m
, unsigned long address
)
4159 #ifdef CONFIG_KALLSYMS
4160 unsigned long offset
, size
;
4161 char modname
[MODULE_NAME_LEN
], name
[KSYM_NAME_LEN
];
4163 if (lookup_symbol_attrs(address
, &size
, &offset
, modname
, name
) == 0) {
4164 seq_printf(m
, "%s+%#lx/%#lx", name
, offset
, size
);
4166 seq_printf(m
, " [%s]", modname
);
4170 seq_printf(m
, "%p", (void *)address
);
4173 static int leaks_show(struct seq_file
*m
, void *p
)
4175 struct kmem_cache
*cachep
= list_entry(p
, struct kmem_cache
, list
);
4177 struct kmem_cache_node
*n
;
4179 unsigned long *x
= m
->private;
4183 if (!(cachep
->flags
& SLAB_STORE_USER
))
4185 if (!(cachep
->flags
& SLAB_RED_ZONE
))
4189 * Set store_user_clean and start to grab stored user information
4190 * for all objects on this cache. If some alloc/free requests comes
4191 * during the processing, information would be wrong so restart
4195 set_store_user_clean(cachep
);
4196 drain_cpu_caches(cachep
);
4200 for_each_kmem_cache_node(cachep
, node
, n
) {
4203 spin_lock_irq(&n
->list_lock
);
4205 list_for_each_entry(page
, &n
->slabs_full
, lru
)
4206 handle_slab(x
, cachep
, page
);
4207 list_for_each_entry(page
, &n
->slabs_partial
, lru
)
4208 handle_slab(x
, cachep
, page
);
4209 spin_unlock_irq(&n
->list_lock
);
4211 } while (!is_store_user_clean(cachep
));
4213 name
= cachep
->name
;
4215 /* Increase the buffer size */
4216 mutex_unlock(&slab_mutex
);
4217 m
->private = kzalloc(x
[0] * 4 * sizeof(unsigned long), GFP_KERNEL
);
4219 /* Too bad, we are really out */
4221 mutex_lock(&slab_mutex
);
4224 *(unsigned long *)m
->private = x
[0] * 2;
4226 mutex_lock(&slab_mutex
);
4227 /* Now make sure this entry will be retried */
4231 for (i
= 0; i
< x
[1]; i
++) {
4232 seq_printf(m
, "%s: %lu ", name
, x
[2*i
+3]);
4233 show_symbol(m
, x
[2*i
+2]);
4240 static const struct seq_operations slabstats_op
= {
4241 .start
= slab_start
,
4247 static int slabstats_open(struct inode
*inode
, struct file
*file
)
4251 n
= __seq_open_private(file
, &slabstats_op
, PAGE_SIZE
);
4255 *n
= PAGE_SIZE
/ (2 * sizeof(unsigned long));
4260 static const struct file_operations proc_slabstats_operations
= {
4261 .open
= slabstats_open
,
4263 .llseek
= seq_lseek
,
4264 .release
= seq_release_private
,
4268 static int __init
slab_proc_init(void)
4270 #ifdef CONFIG_DEBUG_SLAB_LEAK
4271 proc_create("slab_allocators", 0, NULL
, &proc_slabstats_operations
);
4275 module_init(slab_proc_init
);
4278 #ifdef CONFIG_HARDENED_USERCOPY
4280 * Rejects objects that are incorrectly sized.
4282 * Returns NULL if check passes, otherwise const char * to name of cache
4283 * to indicate an error.
4285 const char *__check_heap_object(const void *ptr
, unsigned long n
,
4288 struct kmem_cache
*cachep
;
4290 unsigned long offset
;
4292 /* Find and validate object. */
4293 cachep
= page
->slab_cache
;
4294 objnr
= obj_to_index(cachep
, page
, (void *)ptr
);
4295 BUG_ON(objnr
>= cachep
->num
);
4297 /* Find offset within object. */
4298 offset
= ptr
- index_to_obj(cachep
, page
, objnr
) - obj_offset(cachep
);
4300 /* Allow address range falling entirely within object size. */
4301 if (offset
<= cachep
->object_size
&& n
<= cachep
->object_size
- offset
)
4304 return cachep
->name
;
4306 #endif /* CONFIG_HARDENED_USERCOPY */
4309 * ksize - get the actual amount of memory allocated for a given object
4310 * @objp: Pointer to the object
4312 * kmalloc may internally round up allocations and return more memory
4313 * than requested. ksize() can be used to determine the actual amount of
4314 * memory allocated. The caller may use this additional memory, even though
4315 * a smaller amount of memory was initially specified with the kmalloc call.
4316 * The caller must guarantee that objp points to a valid object previously
4317 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4318 * must not be freed during the duration of the call.
4320 size_t ksize(const void *objp
)
4323 if (unlikely(objp
== ZERO_SIZE_PTR
))
4326 return virt_to_cache(objp
)->object_size
;
4328 EXPORT_SYMBOL(ksize
);