3 * Written by Mark Hemment, 1996/97.
4 * (markhe@nextd.demon.co.uk)
6 * kmem_cache_destroy() + some cleanup - 1999 Andrea Arcangeli
8 * Major cleanup, different bufctl logic, per-cpu arrays
9 * (c) 2000 Manfred Spraul
11 * Cleanup, make the head arrays unconditional, preparation for NUMA
12 * (c) 2002 Manfred Spraul
14 * An implementation of the Slab Allocator as described in outline in;
15 * UNIX Internals: The New Frontiers by Uresh Vahalia
16 * Pub: Prentice Hall ISBN 0-13-101908-2
17 * or with a little more detail in;
18 * The Slab Allocator: An Object-Caching Kernel Memory Allocator
19 * Jeff Bonwick (Sun Microsystems).
20 * Presented at: USENIX Summer 1994 Technical Conference
22 * The memory is organized in caches, one cache for each object type.
23 * (e.g. inode_cache, dentry_cache, buffer_head, vm_area_struct)
24 * Each cache consists out of many slabs (they are small (usually one
25 * page long) and always contiguous), and each slab contains multiple
26 * initialized objects.
28 * This means, that your constructor is used only for newly allocated
29 * slabs and you must pass objects with the same initializations to
32 * Each cache can only support one memory type (GFP_DMA, GFP_HIGHMEM,
33 * normal). If you need a special memory type, then must create a new
34 * cache for that memory type.
36 * In order to reduce fragmentation, the slabs are sorted in 3 groups:
37 * full slabs with 0 free objects
39 * empty slabs with no allocated objects
41 * If partial slabs exist, then new allocations come from these slabs,
42 * otherwise from empty slabs or new slabs are allocated.
44 * kmem_cache_destroy() CAN CRASH if you try to allocate from the cache
45 * during kmem_cache_destroy(). The caller must prevent concurrent allocs.
47 * Each cache has a short per-cpu head array, most allocs
48 * and frees go into that array, and if that array overflows, then 1/2
49 * of the entries in the array are given back into the global cache.
50 * The head array is strictly LIFO and should improve the cache hit rates.
51 * On SMP, it additionally reduces the spinlock operations.
53 * The c_cpuarray may not be read with enabled local interrupts -
54 * it's changed with a smp_call_function().
56 * SMP synchronization:
57 * constructors and destructors are called without any locking.
58 * Several members in struct kmem_cache and struct slab never change, they
59 * are accessed without any locking.
60 * The per-cpu arrays are never accessed from the wrong cpu, no locking,
61 * and local interrupts are disabled so slab code is preempt-safe.
62 * The non-constant members are protected with a per-cache irq spinlock.
64 * Many thanks to Mark Hemment, who wrote another per-cpu slab patch
65 * in 2000 - many ideas in the current implementation are derived from
68 * Further notes from the original documentation:
70 * 11 April '97. Started multi-threading - markhe
71 * The global cache-chain is protected by the mutex 'slab_mutex'.
72 * The sem is only needed when accessing/extending the cache-chain, which
73 * can never happen inside an interrupt (kmem_cache_create(),
74 * kmem_cache_shrink() and kmem_cache_reap()).
76 * At present, each engine can be growing a cache. This should be blocked.
78 * 15 March 2005. NUMA slab allocator.
79 * Shai Fultheim <shai@scalex86.org>.
80 * Shobhit Dayal <shobhit@calsoftinc.com>
81 * Alok N Kataria <alokk@calsoftinc.com>
82 * Christoph Lameter <christoph@lameter.com>
84 * Modified the slab allocator to be node aware on NUMA systems.
85 * Each node has its own list of partial, free and full slabs.
86 * All object allocations for a node occur from node specific slab lists.
89 #include <linux/slab.h>
91 #include <linux/poison.h>
92 #include <linux/swap.h>
93 #include <linux/cache.h>
94 #include <linux/interrupt.h>
95 #include <linux/init.h>
96 #include <linux/compiler.h>
97 #include <linux/cpuset.h>
98 #include <linux/proc_fs.h>
99 #include <linux/seq_file.h>
100 #include <linux/notifier.h>
101 #include <linux/kallsyms.h>
102 #include <linux/cpu.h>
103 #include <linux/sysctl.h>
104 #include <linux/module.h>
105 #include <linux/rcupdate.h>
106 #include <linux/string.h>
107 #include <linux/uaccess.h>
108 #include <linux/nodemask.h>
109 #include <linux/kmemleak.h>
110 #include <linux/mempolicy.h>
111 #include <linux/mutex.h>
112 #include <linux/fault-inject.h>
113 #include <linux/rtmutex.h>
114 #include <linux/reciprocal_div.h>
115 #include <linux/debugobjects.h>
116 #include <linux/kmemcheck.h>
117 #include <linux/memory.h>
118 #include <linux/prefetch.h>
120 #include <net/sock.h>
122 #include <asm/cacheflush.h>
123 #include <asm/tlbflush.h>
124 #include <asm/page.h>
126 #include <trace/events/kmem.h>
128 #include "internal.h"
133 * DEBUG - 1 for kmem_cache_create() to honour; SLAB_RED_ZONE & SLAB_POISON.
134 * 0 for faster, smaller code (especially in the critical paths).
136 * STATS - 1 to collect stats for /proc/slabinfo.
137 * 0 for faster, smaller code (especially in the critical paths).
139 * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
142 #ifdef CONFIG_DEBUG_SLAB
145 #define FORCED_DEBUG 1
149 #define FORCED_DEBUG 0
152 /* Shouldn't this be in a header file somewhere? */
153 #define BYTES_PER_WORD sizeof(void *)
154 #define REDZONE_ALIGN max(BYTES_PER_WORD, __alignof__(unsigned long long))
156 #ifndef ARCH_KMALLOC_FLAGS
157 #define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
161 * true if a page was allocated from pfmemalloc reserves for network-based
164 static bool pfmemalloc_active __read_mostly;
169 * Bufctl's are used for linking objs within a slab
172 * This implementation relies on "struct page" for locating the cache &
173 * slab an object belongs to.
174 * This allows the bufctl structure to be small (one int), but limits
175 * the number of objects a slab (not a cache) can contain when off-slab
176 * bufctls are used. The limit is the size of the largest general cache
177 * that does not use off-slab slabs.
178 * For 32bit archs with 4 kB pages, is this 56.
179 * This is not serious, as it is only for large objects, when it is unwise
180 * to have too many per slab.
181 * Note: This limit can be raised by introducing a general cache whose size
182 * is less than 512 (PAGE_SIZE<<3), but greater than 256.
185 typedef unsigned int kmem_bufctl_t;
186 #define SLAB_LIMIT (((kmem_bufctl_t)(~0U))-3)
191 * Manages the objs in a slab. Placed either at the beginning of mem allocated
192 * for a slab, or allocated from an general cache.
193 * Slabs are chained into three list: fully used, partial, fully free slabs.
197 struct list_head list;
198 void *s_mem; /* including colour offset */
199 unsigned int inuse; /* num of objs active in slab */
208 * - LIFO ordering, to hand out cache-warm objects from _alloc
209 * - reduce the number of linked list operations
210 * - reduce spinlock operations
212 * The limit is stored in the per-cpu structure to reduce the data cache
219 unsigned int batchcount;
220 unsigned int touched;
223 * Must have this definition in here for the proper
224 * alignment of array_cache. Also simplifies accessing
227 * Entries should not be directly dereferenced as
228 * entries belonging to slabs marked pfmemalloc will
229 * have the lower bits set SLAB_OBJ_PFMEMALLOC
233 #define SLAB_OBJ_PFMEMALLOC 1
234 static inline bool is_obj_pfmemalloc(void *objp)
236 return (unsigned long)objp & SLAB_OBJ_PFMEMALLOC;
239 static inline void set_obj_pfmemalloc(void **objp)
241 *objp = (void *)((unsigned long)*objp | SLAB_OBJ_PFMEMALLOC);
245 static inline void clear_obj_pfmemalloc(void **objp)
247 *objp = (void *)((unsigned long)*objp & ~SLAB_OBJ_PFMEMALLOC);
251 * bootstrap: The caches do not work without cpuarrays anymore, but the
252 * cpuarrays are allocated from the generic caches...
254 #define BOOT_CPUCACHE_ENTRIES 1
255 struct arraycache_init {
256 struct array_cache cache;
257 void *entries[BOOT_CPUCACHE_ENTRIES];
261 * Need this for bootstrapping a per node allocator.
263 #define NUM_INIT_LISTS (3 * MAX_NUMNODES)
264 static struct kmem_cache_node __initdata init_kmem_cache_node[NUM_INIT_LISTS];
265 #define CACHE_CACHE 0
266 #define SIZE_AC MAX_NUMNODES
267 #define SIZE_NODE (2 * MAX_NUMNODES)
269 static int drain_freelist(struct kmem_cache *cache,
270 struct kmem_cache_node *n, int tofree);
271 static void free_block(struct kmem_cache *cachep, void **objpp, int len,
273 static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp);
274 static void cache_reap(struct work_struct *unused);
276 static int slab_early_init = 1;
278 #define INDEX_AC kmalloc_index(sizeof(struct arraycache_init))
279 #define INDEX_NODE kmalloc_index(sizeof(struct kmem_cache_node))
281 static void kmem_cache_node_init(struct kmem_cache_node *parent)
283 INIT_LIST_HEAD(&parent->slabs_full);
284 INIT_LIST_HEAD(&parent->slabs_partial);
285 INIT_LIST_HEAD(&parent->slabs_free);
286 parent->shared = NULL;
287 parent->alien = NULL;
288 parent->colour_next = 0;
289 spin_lock_init(&parent->list_lock);
290 parent->free_objects = 0;
291 parent->free_touched = 0;
294 #define MAKE_LIST(cachep, listp, slab, nodeid) \
296 INIT_LIST_HEAD(listp); \
297 list_splice(&(cachep->node[nodeid]->slab), listp); \
300 #define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
302 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
303 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
304 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
307 #define CFLGS_OFF_SLAB (0x80000000UL)
308 #define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
310 #define BATCHREFILL_LIMIT 16
312 * Optimization question: fewer reaps means less probability for unnessary
313 * cpucache drain/refill cycles.
315 * OTOH the cpuarrays can contain lots of objects,
316 * which could lock up otherwise freeable slabs.
318 #define REAPTIMEOUT_CPUC (2*HZ)
319 #define REAPTIMEOUT_LIST3 (4*HZ)
322 #define STATS_INC_ACTIVE(x) ((x)->num_active++)
323 #define STATS_DEC_ACTIVE(x) ((x)->num_active--)
324 #define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
325 #define STATS_INC_GROWN(x) ((x)->grown++)
326 #define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
327 #define STATS_SET_HIGH(x) \
329 if ((x)->num_active > (x)->high_mark) \
330 (x)->high_mark = (x)->num_active; \
332 #define STATS_INC_ERR(x) ((x)->errors++)
333 #define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
334 #define STATS_INC_NODEFREES(x) ((x)->node_frees++)
335 #define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
336 #define STATS_SET_FREEABLE(x, i) \
338 if ((x)->max_freeable < i) \
339 (x)->max_freeable = i; \
341 #define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
342 #define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
343 #define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
344 #define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
346 #define STATS_INC_ACTIVE(x) do { } while (0)
347 #define STATS_DEC_ACTIVE(x) do { } while (0)
348 #define STATS_INC_ALLOCED(x) do { } while (0)
349 #define STATS_INC_GROWN(x) do { } while (0)
350 #define STATS_ADD_REAPED(x,y) do { (void)(y); } while (0)
351 #define STATS_SET_HIGH(x) do { } while (0)
352 #define STATS_INC_ERR(x) do { } while (0)
353 #define STATS_INC_NODEALLOCS(x) do { } while (0)
354 #define STATS_INC_NODEFREES(x) do { } while (0)
355 #define STATS_INC_ACOVERFLOW(x) do { } while (0)
356 #define STATS_SET_FREEABLE(x, i) do { } while (0)
357 #define STATS_INC_ALLOCHIT(x) do { } while (0)
358 #define STATS_INC_ALLOCMISS(x) do { } while (0)
359 #define STATS_INC_FREEHIT(x) do { } while (0)
360 #define STATS_INC_FREEMISS(x) do { } while (0)
366 * memory layout of objects:
368 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
369 * the end of an object is aligned with the end of the real
370 * allocation. Catches writes behind the end of the allocation.
371 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
373 * cachep->obj_offset: The real object.
374 * cachep->size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
375 * cachep->size - 1* BYTES_PER_WORD: last caller address
376 * [BYTES_PER_WORD long]
378 static int obj_offset(struct kmem_cache *cachep)
380 return cachep->obj_offset;
383 static unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
385 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
386 return (unsigned long long*) (objp + obj_offset(cachep) -
387 sizeof(unsigned long long));
390 static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
392 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
393 if (cachep->flags & SLAB_STORE_USER)
394 return (unsigned long long *)(objp + cachep->size -
395 sizeof(unsigned long long) -
397 return (unsigned long long *) (objp + cachep->size -
398 sizeof(unsigned long long));
401 static void **dbg_userword(struct kmem_cache *cachep, void *objp)
403 BUG_ON(!(cachep->flags & SLAB_STORE_USER));
404 return (void **)(objp + cachep->size - BYTES_PER_WORD);
409 #define obj_offset(x) 0
410 #define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
411 #define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
412 #define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
417 * Do not go above this order unless 0 objects fit into the slab or
418 * overridden on the command line.
420 #define SLAB_MAX_ORDER_HI 1
421 #define SLAB_MAX_ORDER_LO 0
422 static int slab_max_order = SLAB_MAX_ORDER_LO;
423 static bool slab_max_order_set __initdata;
425 static inline struct kmem_cache *virt_to_cache(const void *obj)
427 struct page *page = virt_to_head_page(obj);
428 return page->slab_cache;
431 static inline struct slab *virt_to_slab(const void *obj)
433 struct page *page = virt_to_head_page(obj);
435 VM_BUG_ON(!PageSlab(page));
436 return page->slab_page;
439 static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
442 return slab->s_mem + cache->size * idx;
446 * We want to avoid an expensive divide : (offset / cache->size)
447 * Using the fact that size is a constant for a particular cache,
448 * we can replace (offset / cache->size) by
449 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
451 static inline unsigned int obj_to_index(const struct kmem_cache *cache,
452 const struct slab *slab, void *obj)
454 u32 offset = (obj - slab->s_mem);
455 return reciprocal_divide(offset, cache->reciprocal_buffer_size);
458 static struct arraycache_init initarray_generic =
459 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
461 /* internal cache of cache description objs */
462 static struct kmem_cache kmem_cache_boot = {
464 .limit = BOOT_CPUCACHE_ENTRIES,
466 .size = sizeof(struct kmem_cache),
467 .name = "kmem_cache",
470 #define BAD_ALIEN_MAGIC 0x01020304ul
472 #ifdef CONFIG_LOCKDEP
475 * Slab sometimes uses the kmalloc slabs to store the slab headers
476 * for other slabs "off slab".
477 * The locking for this is tricky in that it nests within the locks
478 * of all other slabs in a few places; to deal with this special
479 * locking we put on-slab caches into a separate lock-class.
481 * We set lock class for alien array caches which are up during init.
482 * The lock annotation will be lost if all cpus of a node goes down and
483 * then comes back up during hotplug
485 static struct lock_class_key on_slab_l3_key;
486 static struct lock_class_key on_slab_alc_key;
488 static struct lock_class_key debugobj_l3_key;
489 static struct lock_class_key debugobj_alc_key;
491 static void slab_set_lock_classes(struct kmem_cache *cachep,
492 struct lock_class_key *l3_key, struct lock_class_key *alc_key,
495 struct array_cache **alc;
496 struct kmem_cache_node *n;
503 lockdep_set_class(&n->list_lock, l3_key);
506 * FIXME: This check for BAD_ALIEN_MAGIC
507 * should go away when common slab code is taught to
508 * work even without alien caches.
509 * Currently, non NUMA code returns BAD_ALIEN_MAGIC
510 * for alloc_alien_cache,
512 if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC)
516 lockdep_set_class(&alc[r]->lock, alc_key);
520 static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
522 slab_set_lock_classes(cachep, &debugobj_l3_key, &debugobj_alc_key, node);
525 static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
529 for_each_online_node(node)
530 slab_set_debugobj_lock_classes_node(cachep, node);
533 static void init_node_lock_keys(int q)
540 for (i = 1; i <= KMALLOC_SHIFT_HIGH; i++) {
541 struct kmem_cache_node *n;
542 struct kmem_cache *cache = kmalloc_caches[i];
548 if (!n || OFF_SLAB(cache))
551 slab_set_lock_classes(cache, &on_slab_l3_key,
552 &on_slab_alc_key, q);
556 static void on_slab_lock_classes_node(struct kmem_cache *cachep, int q)
558 if (!cachep->node[q])
561 slab_set_lock_classes(cachep, &on_slab_l3_key,
562 &on_slab_alc_key, q);
565 static inline void on_slab_lock_classes(struct kmem_cache *cachep)
569 VM_BUG_ON(OFF_SLAB(cachep));
571 on_slab_lock_classes_node(cachep, node);
574 static inline void init_lock_keys(void)
579 init_node_lock_keys(node);
582 static void init_node_lock_keys(int q)
586 static inline void init_lock_keys(void)
590 static inline void on_slab_lock_classes(struct kmem_cache *cachep)
594 static inline void on_slab_lock_classes_node(struct kmem_cache *cachep, int node)
598 static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
602 static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
607 static DEFINE_PER_CPU(struct delayed_work, slab_reap_work);
609 static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
611 return cachep->array[smp_processor_id()];
614 static size_t slab_mgmt_size(size_t nr_objs, size_t align)
616 return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align);
620 * Calculate the number of objects and left-over bytes for a given buffer size.
622 static void cache_estimate(unsigned long gfporder, size_t buffer_size,
623 size_t align, int flags, size_t *left_over,
628 size_t slab_size = PAGE_SIZE << gfporder;
631 * The slab management structure can be either off the slab or
632 * on it. For the latter case, the memory allocated for a
636 * - One kmem_bufctl_t for each object
637 * - Padding to respect alignment of @align
638 * - @buffer_size bytes for each object
640 * If the slab management structure is off the slab, then the
641 * alignment will already be calculated into the size. Because
642 * the slabs are all pages aligned, the objects will be at the
643 * correct alignment when allocated.
645 if (flags & CFLGS_OFF_SLAB) {
647 nr_objs = slab_size / buffer_size;
649 if (nr_objs > SLAB_LIMIT)
650 nr_objs = SLAB_LIMIT;
653 * Ignore padding for the initial guess. The padding
654 * is at most @align-1 bytes, and @buffer_size is at
655 * least @align. In the worst case, this result will
656 * be one greater than the number of objects that fit
657 * into the memory allocation when taking the padding
660 nr_objs = (slab_size - sizeof(struct slab)) /
661 (buffer_size + sizeof(kmem_bufctl_t));
664 * This calculated number will be either the right
665 * amount, or one greater than what we want.
667 if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
671 if (nr_objs > SLAB_LIMIT)
672 nr_objs = SLAB_LIMIT;
674 mgmt_size = slab_mgmt_size(nr_objs, align);
677 *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
681 #define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
683 static void __slab_error(const char *function, struct kmem_cache *cachep,
686 printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
687 function, cachep->name, msg);
689 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
694 * By default on NUMA we use alien caches to stage the freeing of
695 * objects allocated from other nodes. This causes massive memory
696 * inefficiencies when using fake NUMA setup to split memory into a
697 * large number of small nodes, so it can be disabled on the command
701 static int use_alien_caches __read_mostly = 1;
702 static int __init noaliencache_setup(char *s)
704 use_alien_caches = 0;
707 __setup("noaliencache", noaliencache_setup);
709 static int __init slab_max_order_setup(char *str)
711 get_option(&str, &slab_max_order);
712 slab_max_order = slab_max_order < 0 ? 0 :
713 min(slab_max_order, MAX_ORDER - 1);
714 slab_max_order_set = true;
718 __setup("slab_max_order=", slab_max_order_setup);
722 * Special reaping functions for NUMA systems called from cache_reap().
723 * These take care of doing round robin flushing of alien caches (containing
724 * objects freed on different nodes from which they were allocated) and the
725 * flushing of remote pcps by calling drain_node_pages.
727 static DEFINE_PER_CPU(unsigned long, slab_reap_node);
729 static void init_reap_node(int cpu)
733 node = next_node(cpu_to_mem(cpu), node_online_map);
734 if (node == MAX_NUMNODES)
735 node = first_node(node_online_map);
737 per_cpu(slab_reap_node, cpu) = node;
740 static void next_reap_node(void)
742 int node = __this_cpu_read(slab_reap_node);
744 node = next_node(node, node_online_map);
745 if (unlikely(node >= MAX_NUMNODES))
746 node = first_node(node_online_map);
747 __this_cpu_write(slab_reap_node, node);
751 #define init_reap_node(cpu) do { } while (0)
752 #define next_reap_node(void) do { } while (0)
756 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
757 * via the workqueue/eventd.
758 * Add the CPU number into the expiration time to minimize the possibility of
759 * the CPUs getting into lockstep and contending for the global cache chain
762 static void start_cpu_timer(int cpu)
764 struct delayed_work *reap_work = &per_cpu(slab_reap_work, cpu);
767 * When this gets called from do_initcalls via cpucache_init(),
768 * init_workqueues() has already run, so keventd will be setup
771 if (keventd_up() && reap_work->work.func == NULL) {
773 INIT_DEFERRABLE_WORK(reap_work, cache_reap);
774 schedule_delayed_work_on(cpu, reap_work,
775 __round_jiffies_relative(HZ, cpu));
779 static struct array_cache *alloc_arraycache(int node, int entries,
780 int batchcount, gfp_t gfp)
782 int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
783 struct array_cache *nc = NULL;
785 nc = kmalloc_node(memsize, gfp, node);
787 * The array_cache structures contain pointers to free object.
788 * However, when such objects are allocated or transferred to another
789 * cache the pointers are not cleared and they could be counted as
790 * valid references during a kmemleak scan. Therefore, kmemleak must
791 * not scan such objects.
793 kmemleak_no_scan(nc);
797 nc->batchcount = batchcount;
799 spin_lock_init(&nc->lock);
804 static inline bool is_slab_pfmemalloc(struct slab *slabp)
806 struct page *page = virt_to_page(slabp->s_mem);
808 return PageSlabPfmemalloc(page);
811 /* Clears pfmemalloc_active if no slabs have pfmalloc set */
812 static void recheck_pfmemalloc_active(struct kmem_cache *cachep,
813 struct array_cache *ac)
815 struct kmem_cache_node *n = cachep->node[numa_mem_id()];
819 if (!pfmemalloc_active)
822 spin_lock_irqsave(&n->list_lock, flags);
823 list_for_each_entry(slabp, &n->slabs_full, list)
824 if (is_slab_pfmemalloc(slabp))
827 list_for_each_entry(slabp, &n->slabs_partial, list)
828 if (is_slab_pfmemalloc(slabp))
831 list_for_each_entry(slabp, &n->slabs_free, list)
832 if (is_slab_pfmemalloc(slabp))
835 pfmemalloc_active = false;
837 spin_unlock_irqrestore(&n->list_lock, flags);
840 static void *__ac_get_obj(struct kmem_cache *cachep, struct array_cache *ac,
841 gfp_t flags, bool force_refill)
844 void *objp = ac->entry[--ac->avail];
846 /* Ensure the caller is allowed to use objects from PFMEMALLOC slab */
847 if (unlikely(is_obj_pfmemalloc(objp))) {
848 struct kmem_cache_node *n;
850 if (gfp_pfmemalloc_allowed(flags)) {
851 clear_obj_pfmemalloc(&objp);
855 /* The caller cannot use PFMEMALLOC objects, find another one */
856 for (i = 0; i < ac->avail; i++) {
857 /* If a !PFMEMALLOC object is found, swap them */
858 if (!is_obj_pfmemalloc(ac->entry[i])) {
860 ac->entry[i] = ac->entry[ac->avail];
861 ac->entry[ac->avail] = objp;
867 * If there are empty slabs on the slabs_free list and we are
868 * being forced to refill the cache, mark this one !pfmemalloc.
870 n = cachep->node[numa_mem_id()];
871 if (!list_empty(&n->slabs_free) && force_refill) {
872 struct slab *slabp = virt_to_slab(objp);
873 ClearPageSlabPfmemalloc(virt_to_head_page(slabp->s_mem));
874 clear_obj_pfmemalloc(&objp);
875 recheck_pfmemalloc_active(cachep, ac);
879 /* No !PFMEMALLOC objects available */
887 static inline void *ac_get_obj(struct kmem_cache *cachep,
888 struct array_cache *ac, gfp_t flags, bool force_refill)
892 if (unlikely(sk_memalloc_socks()))
893 objp = __ac_get_obj(cachep, ac, flags, force_refill);
895 objp = ac->entry[--ac->avail];
900 static void *__ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
903 if (unlikely(pfmemalloc_active)) {
904 /* Some pfmemalloc slabs exist, check if this is one */
905 struct slab *slabp = virt_to_slab(objp);
906 struct page *page = virt_to_head_page(slabp->s_mem);
907 if (PageSlabPfmemalloc(page))
908 set_obj_pfmemalloc(&objp);
914 static inline void ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
917 if (unlikely(sk_memalloc_socks()))
918 objp = __ac_put_obj(cachep, ac, objp);
920 ac->entry[ac->avail++] = objp;
924 * Transfer objects in one arraycache to another.
925 * Locking must be handled by the caller.
927 * Return the number of entries transferred.
929 static int transfer_objects(struct array_cache *to,
930 struct array_cache *from, unsigned int max)
932 /* Figure out how many entries to transfer */
933 int nr = min3(from->avail, max, to->limit - to->avail);
938 memcpy(to->entry + to->avail, from->entry + from->avail -nr,
948 #define drain_alien_cache(cachep, alien) do { } while (0)
949 #define reap_alien(cachep, n) do { } while (0)
951 static inline struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
953 return (struct array_cache **)BAD_ALIEN_MAGIC;
956 static inline void free_alien_cache(struct array_cache **ac_ptr)
960 static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
965 static inline void *alternate_node_alloc(struct kmem_cache *cachep,
971 static inline void *____cache_alloc_node(struct kmem_cache *cachep,
972 gfp_t flags, int nodeid)
977 #else /* CONFIG_NUMA */
979 static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
980 static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
982 static struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
984 struct array_cache **ac_ptr;
985 int memsize = sizeof(void *) * nr_node_ids;
990 ac_ptr = kzalloc_node(memsize, gfp, node);
993 if (i == node || !node_online(i))
995 ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d, gfp);
997 for (i--; i >= 0; i--)
1007 static void free_alien_cache(struct array_cache **ac_ptr)
1018 static void __drain_alien_cache(struct kmem_cache *cachep,
1019 struct array_cache *ac, int node)
1021 struct kmem_cache_node *n = cachep->node[node];
1024 spin_lock(&n->list_lock);
1026 * Stuff objects into the remote nodes shared array first.
1027 * That way we could avoid the overhead of putting the objects
1028 * into the free lists and getting them back later.
1031 transfer_objects(n->shared, ac, ac->limit);
1033 free_block(cachep, ac->entry, ac->avail, node);
1035 spin_unlock(&n->list_lock);
1040 * Called from cache_reap() to regularly drain alien caches round robin.
1042 static void reap_alien(struct kmem_cache *cachep, struct kmem_cache_node *n)
1044 int node = __this_cpu_read(slab_reap_node);
1047 struct array_cache *ac = n->alien[node];
1049 if (ac && ac->avail && spin_trylock_irq(&ac->lock)) {
1050 __drain_alien_cache(cachep, ac, node);
1051 spin_unlock_irq(&ac->lock);
1056 static void drain_alien_cache(struct kmem_cache *cachep,
1057 struct array_cache **alien)
1060 struct array_cache *ac;
1061 unsigned long flags;
1063 for_each_online_node(i) {
1066 spin_lock_irqsave(&ac->lock, flags);
1067 __drain_alien_cache(cachep, ac, i);
1068 spin_unlock_irqrestore(&ac->lock, flags);
1073 static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
1075 int nodeid = page_to_nid(virt_to_page(objp));
1076 struct kmem_cache_node *n;
1077 struct array_cache *alien = NULL;
1080 node = numa_mem_id();
1083 * Make sure we are not freeing a object from another node to the array
1084 * cache on this cpu.
1086 if (likely(nodeid == node))
1089 n = cachep->node[node];
1090 STATS_INC_NODEFREES(cachep);
1091 if (n->alien && n->alien[nodeid]) {
1092 alien = n->alien[nodeid];
1093 spin_lock(&alien->lock);
1094 if (unlikely(alien->avail == alien->limit)) {
1095 STATS_INC_ACOVERFLOW(cachep);
1096 __drain_alien_cache(cachep, alien, nodeid);
1098 ac_put_obj(cachep, alien, objp);
1099 spin_unlock(&alien->lock);
1101 spin_lock(&(cachep->node[nodeid])->list_lock);
1102 free_block(cachep, &objp, 1, nodeid);
1103 spin_unlock(&(cachep->node[nodeid])->list_lock);
1110 * Allocates and initializes node for a node on each slab cache, used for
1111 * either memory or cpu hotplug. If memory is being hot-added, the kmem_cache_node
1112 * will be allocated off-node since memory is not yet online for the new node.
1113 * When hotplugging memory or a cpu, existing node are not replaced if
1116 * Must hold slab_mutex.
1118 static int init_cache_node_node(int node)
1120 struct kmem_cache *cachep;
1121 struct kmem_cache_node *n;
1122 const int memsize = sizeof(struct kmem_cache_node);
1124 list_for_each_entry(cachep, &slab_caches, list) {
1126 * Set up the size64 kmemlist for cpu before we can
1127 * begin anything. Make sure some other cpu on this
1128 * node has not already allocated this
1130 if (!cachep->node[node]) {
1131 n = kmalloc_node(memsize, GFP_KERNEL, node);
1134 kmem_cache_node_init(n);
1135 n->next_reap = jiffies + REAPTIMEOUT_LIST3 +
1136 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1139 * The l3s don't come and go as CPUs come and
1140 * go. slab_mutex is sufficient
1143 cachep->node[node] = n;
1146 spin_lock_irq(&cachep->node[node]->list_lock);
1147 cachep->node[node]->free_limit =
1148 (1 + nr_cpus_node(node)) *
1149 cachep->batchcount + cachep->num;
1150 spin_unlock_irq(&cachep->node[node]->list_lock);
1155 static inline int slabs_tofree(struct kmem_cache *cachep,
1156 struct kmem_cache_node *n)
1158 return (n->free_objects + cachep->num - 1) / cachep->num;
1161 static void cpuup_canceled(long cpu)
1163 struct kmem_cache *cachep;
1164 struct kmem_cache_node *n = NULL;
1165 int node = cpu_to_mem(cpu);
1166 const struct cpumask *mask = cpumask_of_node(node);
1168 list_for_each_entry(cachep, &slab_caches, list) {
1169 struct array_cache *nc;
1170 struct array_cache *shared;
1171 struct array_cache **alien;
1173 /* cpu is dead; no one can alloc from it. */
1174 nc = cachep->array[cpu];
1175 cachep->array[cpu] = NULL;
1176 n = cachep->node[node];
1179 goto free_array_cache;
1181 spin_lock_irq(&n->list_lock);
1183 /* Free limit for this kmem_cache_node */
1184 n->free_limit -= cachep->batchcount;
1186 free_block(cachep, nc->entry, nc->avail, node);
1188 if (!cpumask_empty(mask)) {
1189 spin_unlock_irq(&n->list_lock);
1190 goto free_array_cache;
1195 free_block(cachep, shared->entry,
1196 shared->avail, node);
1203 spin_unlock_irq(&n->list_lock);
1207 drain_alien_cache(cachep, alien);
1208 free_alien_cache(alien);
1214 * In the previous loop, all the objects were freed to
1215 * the respective cache's slabs, now we can go ahead and
1216 * shrink each nodelist to its limit.
1218 list_for_each_entry(cachep, &slab_caches, list) {
1219 n = cachep->node[node];
1222 drain_freelist(cachep, n, slabs_tofree(cachep, n));
1226 static int cpuup_prepare(long cpu)
1228 struct kmem_cache *cachep;
1229 struct kmem_cache_node *n = NULL;
1230 int node = cpu_to_mem(cpu);
1234 * We need to do this right in the beginning since
1235 * alloc_arraycache's are going to use this list.
1236 * kmalloc_node allows us to add the slab to the right
1237 * kmem_cache_node and not this cpu's kmem_cache_node
1239 err = init_cache_node_node(node);
1244 * Now we can go ahead with allocating the shared arrays and
1247 list_for_each_entry(cachep, &slab_caches, list) {
1248 struct array_cache *nc;
1249 struct array_cache *shared = NULL;
1250 struct array_cache **alien = NULL;
1252 nc = alloc_arraycache(node, cachep->limit,
1253 cachep->batchcount, GFP_KERNEL);
1256 if (cachep->shared) {
1257 shared = alloc_arraycache(node,
1258 cachep->shared * cachep->batchcount,
1259 0xbaadf00d, GFP_KERNEL);
1265 if (use_alien_caches) {
1266 alien = alloc_alien_cache(node, cachep->limit, GFP_KERNEL);
1273 cachep->array[cpu] = nc;
1274 n = cachep->node[node];
1277 spin_lock_irq(&n->list_lock);
1280 * We are serialised from CPU_DEAD or
1281 * CPU_UP_CANCELLED by the cpucontrol lock
1292 spin_unlock_irq(&n->list_lock);
1294 free_alien_cache(alien);
1295 if (cachep->flags & SLAB_DEBUG_OBJECTS)
1296 slab_set_debugobj_lock_classes_node(cachep, node);
1297 else if (!OFF_SLAB(cachep) &&
1298 !(cachep->flags & SLAB_DESTROY_BY_RCU))
1299 on_slab_lock_classes_node(cachep, node);
1301 init_node_lock_keys(node);
1305 cpuup_canceled(cpu);
1309 static int cpuup_callback(struct notifier_block *nfb,
1310 unsigned long action, void *hcpu)
1312 long cpu = (long)hcpu;
1316 case CPU_UP_PREPARE:
1317 case CPU_UP_PREPARE_FROZEN:
1318 mutex_lock(&slab_mutex);
1319 err = cpuup_prepare(cpu);
1320 mutex_unlock(&slab_mutex);
1323 case CPU_ONLINE_FROZEN:
1324 start_cpu_timer(cpu);
1326 #ifdef CONFIG_HOTPLUG_CPU
1327 case CPU_DOWN_PREPARE:
1328 case CPU_DOWN_PREPARE_FROZEN:
1330 * Shutdown cache reaper. Note that the slab_mutex is
1331 * held so that if cache_reap() is invoked it cannot do
1332 * anything expensive but will only modify reap_work
1333 * and reschedule the timer.
1335 cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu));
1336 /* Now the cache_reaper is guaranteed to be not running. */
1337 per_cpu(slab_reap_work, cpu).work.func = NULL;
1339 case CPU_DOWN_FAILED:
1340 case CPU_DOWN_FAILED_FROZEN:
1341 start_cpu_timer(cpu);
1344 case CPU_DEAD_FROZEN:
1346 * Even if all the cpus of a node are down, we don't free the
1347 * kmem_cache_node of any cache. This to avoid a race between
1348 * cpu_down, and a kmalloc allocation from another cpu for
1349 * memory from the node of the cpu going down. The node
1350 * structure is usually allocated from kmem_cache_create() and
1351 * gets destroyed at kmem_cache_destroy().
1355 case CPU_UP_CANCELED:
1356 case CPU_UP_CANCELED_FROZEN:
1357 mutex_lock(&slab_mutex);
1358 cpuup_canceled(cpu);
1359 mutex_unlock(&slab_mutex);
1362 return notifier_from_errno(err);
1365 static struct notifier_block cpucache_notifier = {
1366 &cpuup_callback, NULL, 0
1369 #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
1371 * Drains freelist for a node on each slab cache, used for memory hot-remove.
1372 * Returns -EBUSY if all objects cannot be drained so that the node is not
1375 * Must hold slab_mutex.
1377 static int __meminit drain_cache_node_node(int node)
1379 struct kmem_cache *cachep;
1382 list_for_each_entry(cachep, &slab_caches, list) {
1383 struct kmem_cache_node *n;
1385 n = cachep->node[node];
1389 drain_freelist(cachep, n, slabs_tofree(cachep, n));
1391 if (!list_empty(&n->slabs_full) ||
1392 !list_empty(&n->slabs_partial)) {
1400 static int __meminit slab_memory_callback(struct notifier_block *self,
1401 unsigned long action, void *arg)
1403 struct memory_notify *mnb = arg;
1407 nid = mnb->status_change_nid;
1412 case MEM_GOING_ONLINE:
1413 mutex_lock(&slab_mutex);
1414 ret = init_cache_node_node(nid);
1415 mutex_unlock(&slab_mutex);
1417 case MEM_GOING_OFFLINE:
1418 mutex_lock(&slab_mutex);
1419 ret = drain_cache_node_node(nid);
1420 mutex_unlock(&slab_mutex);
1424 case MEM_CANCEL_ONLINE:
1425 case MEM_CANCEL_OFFLINE:
1429 return notifier_from_errno(ret);
1431 #endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
1434 * swap the static kmem_cache_node with kmalloced memory
1436 static void __init init_list(struct kmem_cache *cachep, struct kmem_cache_node *list,
1439 struct kmem_cache_node *ptr;
1441 ptr = kmalloc_node(sizeof(struct kmem_cache_node), GFP_NOWAIT, nodeid);
1444 memcpy(ptr, list, sizeof(struct kmem_cache_node));
1446 * Do not assume that spinlocks can be initialized via memcpy:
1448 spin_lock_init(&ptr->list_lock);
1450 MAKE_ALL_LISTS(cachep, ptr, nodeid);
1451 cachep->node[nodeid] = ptr;
1455 * For setting up all the kmem_cache_node for cache whose buffer_size is same as
1456 * size of kmem_cache_node.
1458 static void __init set_up_node(struct kmem_cache *cachep, int index)
1462 for_each_online_node(node) {
1463 cachep->node[node] = &init_kmem_cache_node[index + node];
1464 cachep->node[node]->next_reap = jiffies +
1466 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1471 * The memory after the last cpu cache pointer is used for the
1474 static void setup_node_pointer(struct kmem_cache *cachep)
1476 cachep->node = (struct kmem_cache_node **)&cachep->array[nr_cpu_ids];
1480 * Initialisation. Called after the page allocator have been initialised and
1481 * before smp_init().
1483 void __init kmem_cache_init(void)
1487 BUILD_BUG_ON(sizeof(((struct page *)NULL)->lru) <
1488 sizeof(struct rcu_head));
1489 kmem_cache = &kmem_cache_boot;
1490 setup_node_pointer(kmem_cache);
1492 if (num_possible_nodes() == 1)
1493 use_alien_caches = 0;
1495 for (i = 0; i < NUM_INIT_LISTS; i++)
1496 kmem_cache_node_init(&init_kmem_cache_node[i]);
1498 set_up_node(kmem_cache, CACHE_CACHE);
1501 * Fragmentation resistance on low memory - only use bigger
1502 * page orders on machines with more than 32MB of memory if
1503 * not overridden on the command line.
1505 if (!slab_max_order_set && totalram_pages > (32 << 20) >> PAGE_SHIFT)
1506 slab_max_order = SLAB_MAX_ORDER_HI;
1508 /* Bootstrap is tricky, because several objects are allocated
1509 * from caches that do not exist yet:
1510 * 1) initialize the kmem_cache cache: it contains the struct
1511 * kmem_cache structures of all caches, except kmem_cache itself:
1512 * kmem_cache is statically allocated.
1513 * Initially an __init data area is used for the head array and the
1514 * kmem_cache_node structures, it's replaced with a kmalloc allocated
1515 * array at the end of the bootstrap.
1516 * 2) Create the first kmalloc cache.
1517 * The struct kmem_cache for the new cache is allocated normally.
1518 * An __init data area is used for the head array.
1519 * 3) Create the remaining kmalloc caches, with minimally sized
1521 * 4) Replace the __init data head arrays for kmem_cache and the first
1522 * kmalloc cache with kmalloc allocated arrays.
1523 * 5) Replace the __init data for kmem_cache_node for kmem_cache and
1524 * the other cache's with kmalloc allocated memory.
1525 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
1528 /* 1) create the kmem_cache */
1531 * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
1533 create_boot_cache(kmem_cache, "kmem_cache",
1534 offsetof(struct kmem_cache, array[nr_cpu_ids]) +
1535 nr_node_ids * sizeof(struct kmem_cache_node *),
1536 SLAB_HWCACHE_ALIGN);
1537 list_add(&kmem_cache->list, &slab_caches);
1539 /* 2+3) create the kmalloc caches */
1542 * Initialize the caches that provide memory for the array cache and the
1543 * kmem_cache_node structures first. Without this, further allocations will
1547 kmalloc_caches[INDEX_AC] = create_kmalloc_cache("kmalloc-ac",
1548 kmalloc_size(INDEX_AC), ARCH_KMALLOC_FLAGS);
1550 if (INDEX_AC != INDEX_NODE)
1551 kmalloc_caches[INDEX_NODE] =
1552 create_kmalloc_cache("kmalloc-node",
1553 kmalloc_size(INDEX_NODE), ARCH_KMALLOC_FLAGS);
1555 slab_early_init = 0;
1557 /* 4) Replace the bootstrap head arrays */
1559 struct array_cache *ptr;
1561 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
1563 memcpy(ptr, cpu_cache_get(kmem_cache),
1564 sizeof(struct arraycache_init));
1566 * Do not assume that spinlocks can be initialized via memcpy:
1568 spin_lock_init(&ptr->lock);
1570 kmem_cache->array[smp_processor_id()] = ptr;
1572 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
1574 BUG_ON(cpu_cache_get(kmalloc_caches[INDEX_AC])
1575 != &initarray_generic.cache);
1576 memcpy(ptr, cpu_cache_get(kmalloc_caches[INDEX_AC]),
1577 sizeof(struct arraycache_init));
1579 * Do not assume that spinlocks can be initialized via memcpy:
1581 spin_lock_init(&ptr->lock);
1583 kmalloc_caches[INDEX_AC]->array[smp_processor_id()] = ptr;
1585 /* 5) Replace the bootstrap kmem_cache_node */
1589 for_each_online_node(nid) {
1590 init_list(kmem_cache, &init_kmem_cache_node[CACHE_CACHE + nid], nid);
1592 init_list(kmalloc_caches[INDEX_AC],
1593 &init_kmem_cache_node[SIZE_AC + nid], nid);
1595 if (INDEX_AC != INDEX_NODE) {
1596 init_list(kmalloc_caches[INDEX_NODE],
1597 &init_kmem_cache_node[SIZE_NODE + nid], nid);
1602 create_kmalloc_caches(ARCH_KMALLOC_FLAGS);
1605 void __init kmem_cache_init_late(void)
1607 struct kmem_cache *cachep;
1611 /* 6) resize the head arrays to their final sizes */
1612 mutex_lock(&slab_mutex);
1613 list_for_each_entry(cachep, &slab_caches, list)
1614 if (enable_cpucache(cachep, GFP_NOWAIT))
1616 mutex_unlock(&slab_mutex);
1618 /* Annotate slab for lockdep -- annotate the malloc caches */
1625 * Register a cpu startup notifier callback that initializes
1626 * cpu_cache_get for all new cpus
1628 register_cpu_notifier(&cpucache_notifier);
1632 * Register a memory hotplug callback that initializes and frees
1635 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
1639 * The reap timers are started later, with a module init call: That part
1640 * of the kernel is not yet operational.
1644 static int __init cpucache_init(void)
1649 * Register the timers that return unneeded pages to the page allocator
1651 for_each_online_cpu(cpu)
1652 start_cpu_timer(cpu);
1658 __initcall(cpucache_init);
1660 static noinline void
1661 slab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid)
1663 struct kmem_cache_node *n;
1665 unsigned long flags;
1669 "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n",
1671 printk(KERN_WARNING " cache: %s, object size: %d, order: %d\n",
1672 cachep->name, cachep->size, cachep->gfporder);
1674 for_each_online_node(node) {
1675 unsigned long active_objs = 0, num_objs = 0, free_objects = 0;
1676 unsigned long active_slabs = 0, num_slabs = 0;
1678 n = cachep->node[node];
1682 spin_lock_irqsave(&n->list_lock, flags);
1683 list_for_each_entry(slabp, &n->slabs_full, list) {
1684 active_objs += cachep->num;
1687 list_for_each_entry(slabp, &n->slabs_partial, list) {
1688 active_objs += slabp->inuse;
1691 list_for_each_entry(slabp, &n->slabs_free, list)
1694 free_objects += n->free_objects;
1695 spin_unlock_irqrestore(&n->list_lock, flags);
1697 num_slabs += active_slabs;
1698 num_objs = num_slabs * cachep->num;
1700 " node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
1701 node, active_slabs, num_slabs, active_objs, num_objs,
1707 * Interface to system's page allocator. No need to hold the cache-lock.
1709 * If we requested dmaable memory, we will get it. Even if we
1710 * did not request dmaable memory, we might get it, but that
1711 * would be relatively rare and ignorable.
1713 static struct page *kmem_getpages(struct kmem_cache *cachep, gfp_t flags,
1719 flags |= cachep->allocflags;
1720 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1721 flags |= __GFP_RECLAIMABLE;
1723 page = alloc_pages_exact_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
1725 if (!(flags & __GFP_NOWARN) && printk_ratelimit())
1726 slab_out_of_memory(cachep, flags, nodeid);
1730 /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
1731 if (unlikely(page->pfmemalloc))
1732 pfmemalloc_active = true;
1734 nr_pages = (1 << cachep->gfporder);
1735 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1736 add_zone_page_state(page_zone(page),
1737 NR_SLAB_RECLAIMABLE, nr_pages);
1739 add_zone_page_state(page_zone(page),
1740 NR_SLAB_UNRECLAIMABLE, nr_pages);
1741 __SetPageSlab(page);
1742 if (page->pfmemalloc)
1743 SetPageSlabPfmemalloc(page);
1744 memcg_bind_pages(cachep, cachep->gfporder);
1746 if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
1747 kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
1750 kmemcheck_mark_uninitialized_pages(page, nr_pages);
1752 kmemcheck_mark_unallocated_pages(page, nr_pages);
1759 * Interface to system's page release.
1761 static void kmem_freepages(struct kmem_cache *cachep, struct page *page)
1763 const unsigned long nr_freed = (1 << cachep->gfporder);
1765 kmemcheck_free_shadow(page, cachep->gfporder);
1767 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1768 sub_zone_page_state(page_zone(page),
1769 NR_SLAB_RECLAIMABLE, nr_freed);
1771 sub_zone_page_state(page_zone(page),
1772 NR_SLAB_UNRECLAIMABLE, nr_freed);
1774 BUG_ON(!PageSlab(page));
1775 __ClearPageSlabPfmemalloc(page);
1776 __ClearPageSlab(page);
1778 memcg_release_pages(cachep, cachep->gfporder);
1779 if (current->reclaim_state)
1780 current->reclaim_state->reclaimed_slab += nr_freed;
1781 __free_memcg_kmem_pages(page, cachep->gfporder);
1784 static void kmem_rcu_free(struct rcu_head *head)
1786 struct kmem_cache *cachep;
1789 page = container_of(head, struct page, rcu_head);
1790 cachep = page->slab_cache;
1792 kmem_freepages(cachep, page);
1797 #ifdef CONFIG_DEBUG_PAGEALLOC
1798 static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
1799 unsigned long caller)
1801 int size = cachep->object_size;
1803 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
1805 if (size < 5 * sizeof(unsigned long))
1808 *addr++ = 0x12345678;
1810 *addr++ = smp_processor_id();
1811 size -= 3 * sizeof(unsigned long);
1813 unsigned long *sptr = &caller;
1814 unsigned long svalue;
1816 while (!kstack_end(sptr)) {
1818 if (kernel_text_address(svalue)) {
1820 size -= sizeof(unsigned long);
1821 if (size <= sizeof(unsigned long))
1827 *addr++ = 0x87654321;
1831 static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
1833 int size = cachep->object_size;
1834 addr = &((char *)addr)[obj_offset(cachep)];
1836 memset(addr, val, size);
1837 *(unsigned char *)(addr + size - 1) = POISON_END;
1840 static void dump_line(char *data, int offset, int limit)
1843 unsigned char error = 0;
1846 printk(KERN_ERR "%03x: ", offset);
1847 for (i = 0; i < limit; i++) {
1848 if (data[offset + i] != POISON_FREE) {
1849 error = data[offset + i];
1853 print_hex_dump(KERN_CONT, "", 0, 16, 1,
1854 &data[offset], limit, 1);
1856 if (bad_count == 1) {
1857 error ^= POISON_FREE;
1858 if (!(error & (error - 1))) {
1859 printk(KERN_ERR "Single bit error detected. Probably "
1862 printk(KERN_ERR "Run memtest86+ or a similar memory "
1865 printk(KERN_ERR "Run a memory test tool.\n");
1874 static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
1879 if (cachep->flags & SLAB_RED_ZONE) {
1880 printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n",
1881 *dbg_redzone1(cachep, objp),
1882 *dbg_redzone2(cachep, objp));
1885 if (cachep->flags & SLAB_STORE_USER) {
1886 printk(KERN_ERR "Last user: [<%p>](%pSR)\n",
1887 *dbg_userword(cachep, objp),
1888 *dbg_userword(cachep, objp));
1890 realobj = (char *)objp + obj_offset(cachep);
1891 size = cachep->object_size;
1892 for (i = 0; i < size && lines; i += 16, lines--) {
1895 if (i + limit > size)
1897 dump_line(realobj, i, limit);
1901 static void check_poison_obj(struct kmem_cache *cachep, void *objp)
1907 realobj = (char *)objp + obj_offset(cachep);
1908 size = cachep->object_size;
1910 for (i = 0; i < size; i++) {
1911 char exp = POISON_FREE;
1914 if (realobj[i] != exp) {
1920 "Slab corruption (%s): %s start=%p, len=%d\n",
1921 print_tainted(), cachep->name, realobj, size);
1922 print_objinfo(cachep, objp, 0);
1924 /* Hexdump the affected line */
1927 if (i + limit > size)
1929 dump_line(realobj, i, limit);
1932 /* Limit to 5 lines */
1938 /* Print some data about the neighboring objects, if they
1941 struct slab *slabp = virt_to_slab(objp);
1944 objnr = obj_to_index(cachep, slabp, objp);
1946 objp = index_to_obj(cachep, slabp, objnr - 1);
1947 realobj = (char *)objp + obj_offset(cachep);
1948 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
1950 print_objinfo(cachep, objp, 2);
1952 if (objnr + 1 < cachep->num) {
1953 objp = index_to_obj(cachep, slabp, objnr + 1);
1954 realobj = (char *)objp + obj_offset(cachep);
1955 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
1957 print_objinfo(cachep, objp, 2);
1964 static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
1967 for (i = 0; i < cachep->num; i++) {
1968 void *objp = index_to_obj(cachep, slabp, i);
1970 if (cachep->flags & SLAB_POISON) {
1971 #ifdef CONFIG_DEBUG_PAGEALLOC
1972 if (cachep->size % PAGE_SIZE == 0 &&
1974 kernel_map_pages(virt_to_page(objp),
1975 cachep->size / PAGE_SIZE, 1);
1977 check_poison_obj(cachep, objp);
1979 check_poison_obj(cachep, objp);
1982 if (cachep->flags & SLAB_RED_ZONE) {
1983 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
1984 slab_error(cachep, "start of a freed object "
1986 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
1987 slab_error(cachep, "end of a freed object "
1993 static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
1999 * slab_destroy - destroy and release all objects in a slab
2000 * @cachep: cache pointer being destroyed
2001 * @slabp: slab pointer being destroyed
2003 * Destroy all the objs in a slab, and release the mem back to the system.
2004 * Before calling the slab must have been unlinked from the cache. The
2005 * cache-lock is not held/needed.
2007 static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
2009 struct page *page = virt_to_head_page(slabp->s_mem);
2011 slab_destroy_debugcheck(cachep, slabp);
2012 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
2013 struct rcu_head *head;
2016 * RCU free overloads the RCU head over the LRU.
2017 * slab_page has been overloeaded over the LRU,
2018 * however it is not used from now on so that
2019 * we can use it safely.
2021 head = (void *)&page->rcu_head;
2022 call_rcu(head, kmem_rcu_free);
2025 kmem_freepages(cachep, page);
2029 * From now on, we don't use slab management
2030 * although actual page can be freed in rcu context
2032 if (OFF_SLAB(cachep))
2033 kmem_cache_free(cachep->slabp_cache, slabp);
2037 * calculate_slab_order - calculate size (page order) of slabs
2038 * @cachep: pointer to the cache that is being created
2039 * @size: size of objects to be created in this cache.
2040 * @align: required alignment for the objects.
2041 * @flags: slab allocation flags
2043 * Also calculates the number of objects per slab.
2045 * This could be made much more intelligent. For now, try to avoid using
2046 * high order pages for slabs. When the gfp() functions are more friendly
2047 * towards high-order requests, this should be changed.
2049 static size_t calculate_slab_order(struct kmem_cache *cachep,
2050 size_t size, size_t align, unsigned long flags)
2052 unsigned long offslab_limit;
2053 size_t left_over = 0;
2056 for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
2060 cache_estimate(gfporder, size, align, flags, &remainder, &num);
2064 if (flags & CFLGS_OFF_SLAB) {
2066 * Max number of objs-per-slab for caches which
2067 * use off-slab slabs. Needed to avoid a possible
2068 * looping condition in cache_grow().
2070 offslab_limit = size - sizeof(struct slab);
2071 offslab_limit /= sizeof(kmem_bufctl_t);
2073 if (num > offslab_limit)
2077 /* Found something acceptable - save it away */
2079 cachep->gfporder = gfporder;
2080 left_over = remainder;
2083 * A VFS-reclaimable slab tends to have most allocations
2084 * as GFP_NOFS and we really don't want to have to be allocating
2085 * higher-order pages when we are unable to shrink dcache.
2087 if (flags & SLAB_RECLAIM_ACCOUNT)
2091 * Large number of objects is good, but very large slabs are
2092 * currently bad for the gfp()s.
2094 if (gfporder >= slab_max_order)
2098 * Acceptable internal fragmentation?
2100 if (left_over * 8 <= (PAGE_SIZE << gfporder))
2106 static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
2108 if (slab_state >= FULL)
2109 return enable_cpucache(cachep, gfp);
2111 if (slab_state == DOWN) {
2113 * Note: Creation of first cache (kmem_cache).
2114 * The setup_node is taken care
2115 * of by the caller of __kmem_cache_create
2117 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2118 slab_state = PARTIAL;
2119 } else if (slab_state == PARTIAL) {
2121 * Note: the second kmem_cache_create must create the cache
2122 * that's used by kmalloc(24), otherwise the creation of
2123 * further caches will BUG().
2125 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2128 * If the cache that's used by kmalloc(sizeof(kmem_cache_node)) is
2129 * the second cache, then we need to set up all its node/,
2130 * otherwise the creation of further caches will BUG().
2132 set_up_node(cachep, SIZE_AC);
2133 if (INDEX_AC == INDEX_NODE)
2134 slab_state = PARTIAL_NODE;
2136 slab_state = PARTIAL_ARRAYCACHE;
2138 /* Remaining boot caches */
2139 cachep->array[smp_processor_id()] =
2140 kmalloc(sizeof(struct arraycache_init), gfp);
2142 if (slab_state == PARTIAL_ARRAYCACHE) {
2143 set_up_node(cachep, SIZE_NODE);
2144 slab_state = PARTIAL_NODE;
2147 for_each_online_node(node) {
2148 cachep->node[node] =
2149 kmalloc_node(sizeof(struct kmem_cache_node),
2151 BUG_ON(!cachep->node[node]);
2152 kmem_cache_node_init(cachep->node[node]);
2156 cachep->node[numa_mem_id()]->next_reap =
2157 jiffies + REAPTIMEOUT_LIST3 +
2158 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
2160 cpu_cache_get(cachep)->avail = 0;
2161 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
2162 cpu_cache_get(cachep)->batchcount = 1;
2163 cpu_cache_get(cachep)->touched = 0;
2164 cachep->batchcount = 1;
2165 cachep->limit = BOOT_CPUCACHE_ENTRIES;
2170 * __kmem_cache_create - Create a cache.
2171 * @cachep: cache management descriptor
2172 * @flags: SLAB flags
2174 * Returns a ptr to the cache on success, NULL on failure.
2175 * Cannot be called within a int, but can be interrupted.
2176 * The @ctor is run when new pages are allocated by the cache.
2180 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2181 * to catch references to uninitialised memory.
2183 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2184 * for buffer overruns.
2186 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2187 * cacheline. This can be beneficial if you're counting cycles as closely
2191 __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
2193 size_t left_over, slab_size, ralign;
2196 size_t size = cachep->size;
2201 * Enable redzoning and last user accounting, except for caches with
2202 * large objects, if the increased size would increase the object size
2203 * above the next power of two: caches with object sizes just above a
2204 * power of two have a significant amount of internal fragmentation.
2206 if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
2207 2 * sizeof(unsigned long long)))
2208 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
2209 if (!(flags & SLAB_DESTROY_BY_RCU))
2210 flags |= SLAB_POISON;
2212 if (flags & SLAB_DESTROY_BY_RCU)
2213 BUG_ON(flags & SLAB_POISON);
2217 * Check that size is in terms of words. This is needed to avoid
2218 * unaligned accesses for some archs when redzoning is used, and makes
2219 * sure any on-slab bufctl's are also correctly aligned.
2221 if (size & (BYTES_PER_WORD - 1)) {
2222 size += (BYTES_PER_WORD - 1);
2223 size &= ~(BYTES_PER_WORD - 1);
2227 * Redzoning and user store require word alignment or possibly larger.
2228 * Note this will be overridden by architecture or caller mandated
2229 * alignment if either is greater than BYTES_PER_WORD.
2231 if (flags & SLAB_STORE_USER)
2232 ralign = BYTES_PER_WORD;
2234 if (flags & SLAB_RED_ZONE) {
2235 ralign = REDZONE_ALIGN;
2236 /* If redzoning, ensure that the second redzone is suitably
2237 * aligned, by adjusting the object size accordingly. */
2238 size += REDZONE_ALIGN - 1;
2239 size &= ~(REDZONE_ALIGN - 1);
2242 /* 3) caller mandated alignment */
2243 if (ralign < cachep->align) {
2244 ralign = cachep->align;
2246 /* disable debug if necessary */
2247 if (ralign > __alignof__(unsigned long long))
2248 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
2252 cachep->align = ralign;
2254 if (slab_is_available())
2259 setup_node_pointer(cachep);
2263 * Both debugging options require word-alignment which is calculated
2266 if (flags & SLAB_RED_ZONE) {
2267 /* add space for red zone words */
2268 cachep->obj_offset += sizeof(unsigned long long);
2269 size += 2 * sizeof(unsigned long long);
2271 if (flags & SLAB_STORE_USER) {
2272 /* user store requires one word storage behind the end of
2273 * the real object. But if the second red zone needs to be
2274 * aligned to 64 bits, we must allow that much space.
2276 if (flags & SLAB_RED_ZONE)
2277 size += REDZONE_ALIGN;
2279 size += BYTES_PER_WORD;
2281 #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
2282 if (size >= kmalloc_size(INDEX_NODE + 1)
2283 && cachep->object_size > cache_line_size()
2284 && ALIGN(size, cachep->align) < PAGE_SIZE) {
2285 cachep->obj_offset += PAGE_SIZE - ALIGN(size, cachep->align);
2292 * Determine if the slab management is 'on' or 'off' slab.
2293 * (bootstrapping cannot cope with offslab caches so don't do
2294 * it too early on. Always use on-slab management when
2295 * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
2297 if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init &&
2298 !(flags & SLAB_NOLEAKTRACE))
2300 * Size is large, assume best to place the slab management obj
2301 * off-slab (should allow better packing of objs).
2303 flags |= CFLGS_OFF_SLAB;
2305 size = ALIGN(size, cachep->align);
2307 left_over = calculate_slab_order(cachep, size, cachep->align, flags);
2312 slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
2313 + sizeof(struct slab), cachep->align);
2316 * If the slab has been placed off-slab, and we have enough space then
2317 * move it on-slab. This is at the expense of any extra colouring.
2319 if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
2320 flags &= ~CFLGS_OFF_SLAB;
2321 left_over -= slab_size;
2324 if (flags & CFLGS_OFF_SLAB) {
2325 /* really off slab. No need for manual alignment */
2327 cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
2329 #ifdef CONFIG_PAGE_POISONING
2330 /* If we're going to use the generic kernel_map_pages()
2331 * poisoning, then it's going to smash the contents of
2332 * the redzone and userword anyhow, so switch them off.
2334 if (size % PAGE_SIZE == 0 && flags & SLAB_POISON)
2335 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
2339 cachep->colour_off = cache_line_size();
2340 /* Offset must be a multiple of the alignment. */
2341 if (cachep->colour_off < cachep->align)
2342 cachep->colour_off = cachep->align;
2343 cachep->colour = left_over / cachep->colour_off;
2344 cachep->slab_size = slab_size;
2345 cachep->flags = flags;
2346 cachep->allocflags = __GFP_COMP;
2347 if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
2348 cachep->allocflags |= GFP_DMA;
2349 cachep->size = size;
2350 cachep->reciprocal_buffer_size = reciprocal_value(size);
2352 if (flags & CFLGS_OFF_SLAB) {
2353 cachep->slabp_cache = kmalloc_slab(slab_size, 0u);
2355 * This is a possibility for one of the malloc_sizes caches.
2356 * But since we go off slab only for object size greater than
2357 * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
2358 * this should not happen at all.
2359 * But leave a BUG_ON for some lucky dude.
2361 BUG_ON(ZERO_OR_NULL_PTR(cachep->slabp_cache));
2364 err = setup_cpu_cache(cachep, gfp);
2366 __kmem_cache_shutdown(cachep);
2370 if (flags & SLAB_DEBUG_OBJECTS) {
2372 * Would deadlock through slab_destroy()->call_rcu()->
2373 * debug_object_activate()->kmem_cache_alloc().
2375 WARN_ON_ONCE(flags & SLAB_DESTROY_BY_RCU);
2377 slab_set_debugobj_lock_classes(cachep);
2378 } else if (!OFF_SLAB(cachep) && !(flags & SLAB_DESTROY_BY_RCU))
2379 on_slab_lock_classes(cachep);
2385 static void check_irq_off(void)
2387 BUG_ON(!irqs_disabled());
2390 static void check_irq_on(void)
2392 BUG_ON(irqs_disabled());
2395 static void check_spinlock_acquired(struct kmem_cache *cachep)
2399 assert_spin_locked(&cachep->node[numa_mem_id()]->list_lock);
2403 static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
2407 assert_spin_locked(&cachep->node[node]->list_lock);
2412 #define check_irq_off() do { } while(0)
2413 #define check_irq_on() do { } while(0)
2414 #define check_spinlock_acquired(x) do { } while(0)
2415 #define check_spinlock_acquired_node(x, y) do { } while(0)
2418 static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
2419 struct array_cache *ac,
2420 int force, int node);
2422 static void do_drain(void *arg)
2424 struct kmem_cache *cachep = arg;
2425 struct array_cache *ac;
2426 int node = numa_mem_id();
2429 ac = cpu_cache_get(cachep);
2430 spin_lock(&cachep->node[node]->list_lock);
2431 free_block(cachep, ac->entry, ac->avail, node);
2432 spin_unlock(&cachep->node[node]->list_lock);
2436 static void drain_cpu_caches(struct kmem_cache *cachep)
2438 struct kmem_cache_node *n;
2441 on_each_cpu(do_drain, cachep, 1);
2443 for_each_online_node(node) {
2444 n = cachep->node[node];
2446 drain_alien_cache(cachep, n->alien);
2449 for_each_online_node(node) {
2450 n = cachep->node[node];
2452 drain_array(cachep, n, n->shared, 1, node);
2457 * Remove slabs from the list of free slabs.
2458 * Specify the number of slabs to drain in tofree.
2460 * Returns the actual number of slabs released.
2462 static int drain_freelist(struct kmem_cache *cache,
2463 struct kmem_cache_node *n, int tofree)
2465 struct list_head *p;
2470 while (nr_freed < tofree && !list_empty(&n->slabs_free)) {
2472 spin_lock_irq(&n->list_lock);
2473 p = n->slabs_free.prev;
2474 if (p == &n->slabs_free) {
2475 spin_unlock_irq(&n->list_lock);
2479 slabp = list_entry(p, struct slab, list);
2481 BUG_ON(slabp->inuse);
2483 list_del(&slabp->list);
2485 * Safe to drop the lock. The slab is no longer linked
2488 n->free_objects -= cache->num;
2489 spin_unlock_irq(&n->list_lock);
2490 slab_destroy(cache, slabp);
2497 /* Called with slab_mutex held to protect against cpu hotplug */
2498 static int __cache_shrink(struct kmem_cache *cachep)
2501 struct kmem_cache_node *n;
2503 drain_cpu_caches(cachep);
2506 for_each_online_node(i) {
2507 n = cachep->node[i];
2511 drain_freelist(cachep, n, slabs_tofree(cachep, n));
2513 ret += !list_empty(&n->slabs_full) ||
2514 !list_empty(&n->slabs_partial);
2516 return (ret ? 1 : 0);
2520 * kmem_cache_shrink - Shrink a cache.
2521 * @cachep: The cache to shrink.
2523 * Releases as many slabs as possible for a cache.
2524 * To help debugging, a zero exit status indicates all slabs were released.
2526 int kmem_cache_shrink(struct kmem_cache *cachep)
2529 BUG_ON(!cachep || in_interrupt());
2532 mutex_lock(&slab_mutex);
2533 ret = __cache_shrink(cachep);
2534 mutex_unlock(&slab_mutex);
2538 EXPORT_SYMBOL(kmem_cache_shrink);
2540 int __kmem_cache_shutdown(struct kmem_cache *cachep)
2543 struct kmem_cache_node *n;
2544 int rc = __cache_shrink(cachep);
2549 for_each_online_cpu(i)
2550 kfree(cachep->array[i]);
2552 /* NUMA: free the node structures */
2553 for_each_online_node(i) {
2554 n = cachep->node[i];
2557 free_alien_cache(n->alien);
2565 * Get the memory for a slab management obj.
2566 * For a slab cache when the slab descriptor is off-slab, slab descriptors
2567 * always come from malloc_sizes caches. The slab descriptor cannot
2568 * come from the same cache which is getting created because,
2569 * when we are searching for an appropriate cache for these
2570 * descriptors in kmem_cache_create, we search through the malloc_sizes array.
2571 * If we are creating a malloc_sizes cache here it would not be visible to
2572 * kmem_find_general_cachep till the initialization is complete.
2573 * Hence we cannot have slabp_cache same as the original cache.
2575 static struct slab *alloc_slabmgmt(struct kmem_cache *cachep,
2576 struct page *page, int colour_off,
2577 gfp_t local_flags, int nodeid)
2580 void *addr = page_address(page);
2582 if (OFF_SLAB(cachep)) {
2583 /* Slab management obj is off-slab. */
2584 slabp = kmem_cache_alloc_node(cachep->slabp_cache,
2585 local_flags, nodeid);
2587 * If the first object in the slab is leaked (it's allocated
2588 * but no one has a reference to it), we want to make sure
2589 * kmemleak does not treat the ->s_mem pointer as a reference
2590 * to the object. Otherwise we will not report the leak.
2592 kmemleak_scan_area(&slabp->list, sizeof(struct list_head),
2597 slabp = addr + colour_off;
2598 colour_off += cachep->slab_size;
2601 slabp->s_mem = addr + colour_off;
2606 static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
2608 return (kmem_bufctl_t *) (slabp + 1);
2611 static void cache_init_objs(struct kmem_cache *cachep,
2616 for (i = 0; i < cachep->num; i++) {
2617 void *objp = index_to_obj(cachep, slabp, i);
2619 /* need to poison the objs? */
2620 if (cachep->flags & SLAB_POISON)
2621 poison_obj(cachep, objp, POISON_FREE);
2622 if (cachep->flags & SLAB_STORE_USER)
2623 *dbg_userword(cachep, objp) = NULL;
2625 if (cachep->flags & SLAB_RED_ZONE) {
2626 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2627 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2630 * Constructors are not allowed to allocate memory from the same
2631 * cache which they are a constructor for. Otherwise, deadlock.
2632 * They must also be threaded.
2634 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
2635 cachep->ctor(objp + obj_offset(cachep));
2637 if (cachep->flags & SLAB_RED_ZONE) {
2638 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2639 slab_error(cachep, "constructor overwrote the"
2640 " end of an object");
2641 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2642 slab_error(cachep, "constructor overwrote the"
2643 " start of an object");
2645 if ((cachep->size % PAGE_SIZE) == 0 &&
2646 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
2647 kernel_map_pages(virt_to_page(objp),
2648 cachep->size / PAGE_SIZE, 0);
2653 slab_bufctl(slabp)[i] = i;
2657 static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
2659 if (CONFIG_ZONE_DMA_FLAG) {
2660 if (flags & GFP_DMA)
2661 BUG_ON(!(cachep->allocflags & GFP_DMA));
2663 BUG_ON(cachep->allocflags & GFP_DMA);
2667 static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
2673 objp = index_to_obj(cachep, slabp, slab_bufctl(slabp)[slabp->free]);
2675 WARN_ON(page_to_nid(virt_to_page(objp)) != nodeid);
2682 static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
2683 void *objp, int nodeid)
2685 unsigned int objnr = obj_to_index(cachep, slabp, objp);
2689 /* Verify that the slab belongs to the intended node */
2690 WARN_ON(page_to_nid(virt_to_page(objp)) != nodeid);
2692 /* Verify double free bug */
2693 for (i = slabp->free; i < cachep->num; i++) {
2694 if (slab_bufctl(slabp)[i] == objnr) {
2695 printk(KERN_ERR "slab: double free detected in cache "
2696 "'%s', objp %p\n", cachep->name, objp);
2702 slab_bufctl(slabp)[slabp->free] = objnr;
2707 * Map pages beginning at addr to the given cache and slab. This is required
2708 * for the slab allocator to be able to lookup the cache and slab of a
2709 * virtual address for kfree, ksize, and slab debugging.
2711 static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
2714 page->slab_cache = cache;
2715 page->slab_page = slab;
2719 * Grow (by 1) the number of slabs within a cache. This is called by
2720 * kmem_cache_alloc() when there are no active objs left in a cache.
2722 static int cache_grow(struct kmem_cache *cachep,
2723 gfp_t flags, int nodeid, struct page *page)
2728 struct kmem_cache_node *n;
2731 * Be lazy and only check for valid flags here, keeping it out of the
2732 * critical path in kmem_cache_alloc().
2734 BUG_ON(flags & GFP_SLAB_BUG_MASK);
2735 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
2737 /* Take the node list lock to change the colour_next on this node */
2739 n = cachep->node[nodeid];
2740 spin_lock(&n->list_lock);
2742 /* Get colour for the slab, and cal the next value. */
2743 offset = n->colour_next;
2745 if (n->colour_next >= cachep->colour)
2747 spin_unlock(&n->list_lock);
2749 offset *= cachep->colour_off;
2751 if (local_flags & __GFP_WAIT)
2755 * The test for missing atomic flag is performed here, rather than
2756 * the more obvious place, simply to reduce the critical path length
2757 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2758 * will eventually be caught here (where it matters).
2760 kmem_flagcheck(cachep, flags);
2763 * Get mem for the objs. Attempt to allocate a physical page from
2767 page = kmem_getpages(cachep, local_flags, nodeid);
2771 /* Get slab management. */
2772 slabp = alloc_slabmgmt(cachep, page, offset,
2773 local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
2777 slab_map_pages(cachep, slabp, page);
2779 cache_init_objs(cachep, slabp);
2781 if (local_flags & __GFP_WAIT)
2782 local_irq_disable();
2784 spin_lock(&n->list_lock);
2786 /* Make slab active. */
2787 list_add_tail(&slabp->list, &(n->slabs_free));
2788 STATS_INC_GROWN(cachep);
2789 n->free_objects += cachep->num;
2790 spin_unlock(&n->list_lock);
2793 kmem_freepages(cachep, page);
2795 if (local_flags & __GFP_WAIT)
2796 local_irq_disable();
2803 * Perform extra freeing checks:
2804 * - detect bad pointers.
2805 * - POISON/RED_ZONE checking
2807 static void kfree_debugcheck(const void *objp)
2809 if (!virt_addr_valid(objp)) {
2810 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
2811 (unsigned long)objp);
2816 static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2818 unsigned long long redzone1, redzone2;
2820 redzone1 = *dbg_redzone1(cache, obj);
2821 redzone2 = *dbg_redzone2(cache, obj);
2826 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2829 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2830 slab_error(cache, "double free detected");
2832 slab_error(cache, "memory outside object was overwritten");
2834 printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
2835 obj, redzone1, redzone2);
2838 static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
2839 unsigned long caller)
2844 BUG_ON(virt_to_cache(objp) != cachep);
2846 objp -= obj_offset(cachep);
2847 kfree_debugcheck(objp);
2848 slabp = virt_to_slab(objp);
2850 if (cachep->flags & SLAB_RED_ZONE) {
2851 verify_redzone_free(cachep, objp);
2852 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2853 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2855 if (cachep->flags & SLAB_STORE_USER)
2856 *dbg_userword(cachep, objp) = (void *)caller;
2858 objnr = obj_to_index(cachep, slabp, objp);
2860 BUG_ON(objnr >= cachep->num);
2861 BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
2863 if (cachep->flags & SLAB_POISON) {
2864 #ifdef CONFIG_DEBUG_PAGEALLOC
2865 if ((cachep->size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
2866 store_stackinfo(cachep, objp, caller);
2867 kernel_map_pages(virt_to_page(objp),
2868 cachep->size / PAGE_SIZE, 0);
2870 poison_obj(cachep, objp, POISON_FREE);
2873 poison_obj(cachep, objp, POISON_FREE);
2880 #define kfree_debugcheck(x) do { } while(0)
2881 #define cache_free_debugcheck(x,objp,z) (objp)
2884 static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags,
2888 struct kmem_cache_node *n;
2889 struct array_cache *ac;
2893 node = numa_mem_id();
2894 if (unlikely(force_refill))
2897 ac = cpu_cache_get(cachep);
2898 batchcount = ac->batchcount;
2899 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
2901 * If there was little recent activity on this cache, then
2902 * perform only a partial refill. Otherwise we could generate
2905 batchcount = BATCHREFILL_LIMIT;
2907 n = cachep->node[node];
2909 BUG_ON(ac->avail > 0 || !n);
2910 spin_lock(&n->list_lock);
2912 /* See if we can refill from the shared array */
2913 if (n->shared && transfer_objects(ac, n->shared, batchcount)) {
2914 n->shared->touched = 1;
2918 while (batchcount > 0) {
2919 struct list_head *entry;
2921 /* Get slab alloc is to come from. */
2922 entry = n->slabs_partial.next;
2923 if (entry == &n->slabs_partial) {
2924 n->free_touched = 1;
2925 entry = n->slabs_free.next;
2926 if (entry == &n->slabs_free)
2930 slabp = list_entry(entry, struct slab, list);
2931 check_spinlock_acquired(cachep);
2934 * The slab was either on partial or free list so
2935 * there must be at least one object available for
2938 BUG_ON(slabp->inuse >= cachep->num);
2940 while (slabp->inuse < cachep->num && batchcount--) {
2941 STATS_INC_ALLOCED(cachep);
2942 STATS_INC_ACTIVE(cachep);
2943 STATS_SET_HIGH(cachep);
2945 ac_put_obj(cachep, ac, slab_get_obj(cachep, slabp,
2949 /* move slabp to correct slabp list: */
2950 list_del(&slabp->list);
2951 if (slabp->free == cachep->num)
2952 list_add(&slabp->list, &n->slabs_full);
2954 list_add(&slabp->list, &n->slabs_partial);
2958 n->free_objects -= ac->avail;
2960 spin_unlock(&n->list_lock);
2962 if (unlikely(!ac->avail)) {
2965 x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
2967 /* cache_grow can reenable interrupts, then ac could change. */
2968 ac = cpu_cache_get(cachep);
2969 node = numa_mem_id();
2971 /* no objects in sight? abort */
2972 if (!x && (ac->avail == 0 || force_refill))
2975 if (!ac->avail) /* objects refilled by interrupt? */
2980 return ac_get_obj(cachep, ac, flags, force_refill);
2983 static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
2986 might_sleep_if(flags & __GFP_WAIT);
2988 kmem_flagcheck(cachep, flags);
2993 static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
2994 gfp_t flags, void *objp, unsigned long caller)
2998 if (cachep->flags & SLAB_POISON) {
2999 #ifdef CONFIG_DEBUG_PAGEALLOC
3000 if ((cachep->size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
3001 kernel_map_pages(virt_to_page(objp),
3002 cachep->size / PAGE_SIZE, 1);
3004 check_poison_obj(cachep, objp);
3006 check_poison_obj(cachep, objp);
3008 poison_obj(cachep, objp, POISON_INUSE);
3010 if (cachep->flags & SLAB_STORE_USER)
3011 *dbg_userword(cachep, objp) = (void *)caller;
3013 if (cachep->flags & SLAB_RED_ZONE) {
3014 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
3015 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
3016 slab_error(cachep, "double free, or memory outside"
3017 " object was overwritten");
3019 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
3020 objp, *dbg_redzone1(cachep, objp),
3021 *dbg_redzone2(cachep, objp));
3023 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
3024 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
3026 objp += obj_offset(cachep);
3027 if (cachep->ctor && cachep->flags & SLAB_POISON)
3029 if (ARCH_SLAB_MINALIGN &&
3030 ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
3031 printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
3032 objp, (int)ARCH_SLAB_MINALIGN);
3037 #define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
3040 static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags)
3042 if (cachep == kmem_cache)
3045 return should_failslab(cachep->object_size, flags, cachep->flags);
3048 static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
3051 struct array_cache *ac;
3052 bool force_refill = false;
3056 ac = cpu_cache_get(cachep);
3057 if (likely(ac->avail)) {
3059 objp = ac_get_obj(cachep, ac, flags, false);
3062 * Allow for the possibility all avail objects are not allowed
3063 * by the current flags
3066 STATS_INC_ALLOCHIT(cachep);
3069 force_refill = true;
3072 STATS_INC_ALLOCMISS(cachep);
3073 objp = cache_alloc_refill(cachep, flags, force_refill);
3075 * the 'ac' may be updated by cache_alloc_refill(),
3076 * and kmemleak_erase() requires its correct value.
3078 ac = cpu_cache_get(cachep);
3082 * To avoid a false negative, if an object that is in one of the
3083 * per-CPU caches is leaked, we need to make sure kmemleak doesn't
3084 * treat the array pointers as a reference to the object.
3087 kmemleak_erase(&ac->entry[ac->avail]);
3093 * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
3095 * If we are in_interrupt, then process context, including cpusets and
3096 * mempolicy, may not apply and should not be used for allocation policy.
3098 static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
3100 int nid_alloc, nid_here;
3102 if (in_interrupt() || (flags & __GFP_THISNODE))
3104 nid_alloc = nid_here = numa_mem_id();
3105 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
3106 nid_alloc = cpuset_slab_spread_node();
3107 else if (current->mempolicy)
3108 nid_alloc = slab_node();
3109 if (nid_alloc != nid_here)
3110 return ____cache_alloc_node(cachep, flags, nid_alloc);
3115 * Fallback function if there was no memory available and no objects on a
3116 * certain node and fall back is permitted. First we scan all the
3117 * available node for available objects. If that fails then we
3118 * perform an allocation without specifying a node. This allows the page
3119 * allocator to do its reclaim / fallback magic. We then insert the
3120 * slab into the proper nodelist and then allocate from it.
3122 static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
3124 struct zonelist *zonelist;
3128 enum zone_type high_zoneidx = gfp_zone(flags);
3131 unsigned int cpuset_mems_cookie;
3133 if (flags & __GFP_THISNODE)
3136 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
3139 cpuset_mems_cookie = get_mems_allowed();
3140 zonelist = node_zonelist(slab_node(), flags);
3144 * Look through allowed nodes for objects available
3145 * from existing per node queues.
3147 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
3148 nid = zone_to_nid(zone);
3150 if (cpuset_zone_allowed_hardwall(zone, flags) &&
3152 cache->node[nid]->free_objects) {
3153 obj = ____cache_alloc_node(cache,
3154 flags | GFP_THISNODE, nid);
3162 * This allocation will be performed within the constraints
3163 * of the current cpuset / memory policy requirements.
3164 * We may trigger various forms of reclaim on the allowed
3165 * set and go into memory reserves if necessary.
3169 if (local_flags & __GFP_WAIT)
3171 kmem_flagcheck(cache, flags);
3172 page = kmem_getpages(cache, local_flags, numa_mem_id());
3173 if (local_flags & __GFP_WAIT)
3174 local_irq_disable();
3177 * Insert into the appropriate per node queues
3179 nid = page_to_nid(page);
3180 if (cache_grow(cache, flags, nid, page)) {
3181 obj = ____cache_alloc_node(cache,
3182 flags | GFP_THISNODE, nid);
3185 * Another processor may allocate the
3186 * objects in the slab since we are
3187 * not holding any locks.
3191 /* cache_grow already freed obj */
3197 if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !obj))
3203 * A interface to enable slab creation on nodeid
3205 static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
3208 struct list_head *entry;
3210 struct kmem_cache_node *n;
3214 VM_BUG_ON(nodeid > num_online_nodes());
3215 n = cachep->node[nodeid];
3220 spin_lock(&n->list_lock);
3221 entry = n->slabs_partial.next;
3222 if (entry == &n->slabs_partial) {
3223 n->free_touched = 1;
3224 entry = n->slabs_free.next;
3225 if (entry == &n->slabs_free)
3229 slabp = list_entry(entry, struct slab, list);
3230 check_spinlock_acquired_node(cachep, nodeid);
3232 STATS_INC_NODEALLOCS(cachep);
3233 STATS_INC_ACTIVE(cachep);
3234 STATS_SET_HIGH(cachep);
3236 BUG_ON(slabp->inuse == cachep->num);
3238 obj = slab_get_obj(cachep, slabp, nodeid);
3240 /* move slabp to correct slabp list: */
3241 list_del(&slabp->list);
3243 if (slabp->free == cachep->num)
3244 list_add(&slabp->list, &n->slabs_full);
3246 list_add(&slabp->list, &n->slabs_partial);
3248 spin_unlock(&n->list_lock);
3252 spin_unlock(&n->list_lock);
3253 x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
3257 return fallback_alloc(cachep, flags);
3263 static __always_inline void *
3264 slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
3265 unsigned long caller)
3267 unsigned long save_flags;
3269 int slab_node = numa_mem_id();
3271 flags &= gfp_allowed_mask;
3273 lockdep_trace_alloc(flags);
3275 if (slab_should_failslab(cachep, flags))
3278 cachep = memcg_kmem_get_cache(cachep, flags);
3280 cache_alloc_debugcheck_before(cachep, flags);
3281 local_irq_save(save_flags);
3283 if (nodeid == NUMA_NO_NODE)
3286 if (unlikely(!cachep->node[nodeid])) {
3287 /* Node not bootstrapped yet */
3288 ptr = fallback_alloc(cachep, flags);
3292 if (nodeid == slab_node) {
3294 * Use the locally cached objects if possible.
3295 * However ____cache_alloc does not allow fallback
3296 * to other nodes. It may fail while we still have
3297 * objects on other nodes available.
3299 ptr = ____cache_alloc(cachep, flags);
3303 /* ___cache_alloc_node can fall back to other nodes */
3304 ptr = ____cache_alloc_node(cachep, flags, nodeid);
3306 local_irq_restore(save_flags);
3307 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
3308 kmemleak_alloc_recursive(ptr, cachep->object_size, 1, cachep->flags,
3312 kmemcheck_slab_alloc(cachep, flags, ptr, cachep->object_size);
3314 if (unlikely((flags & __GFP_ZERO) && ptr))
3315 memset(ptr, 0, cachep->object_size);
3320 static __always_inline void *
3321 __do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
3325 if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
3326 objp = alternate_node_alloc(cache, flags);
3330 objp = ____cache_alloc(cache, flags);
3333 * We may just have run out of memory on the local node.
3334 * ____cache_alloc_node() knows how to locate memory on other nodes
3337 objp = ____cache_alloc_node(cache, flags, numa_mem_id());
3344 static __always_inline void *
3345 __do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
3347 return ____cache_alloc(cachep, flags);
3350 #endif /* CONFIG_NUMA */
3352 static __always_inline void *
3353 slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)
3355 unsigned long save_flags;
3358 flags &= gfp_allowed_mask;
3360 lockdep_trace_alloc(flags);
3362 if (slab_should_failslab(cachep, flags))
3365 cachep = memcg_kmem_get_cache(cachep, flags);
3367 cache_alloc_debugcheck_before(cachep, flags);
3368 local_irq_save(save_flags);
3369 objp = __do_cache_alloc(cachep, flags);
3370 local_irq_restore(save_flags);
3371 objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
3372 kmemleak_alloc_recursive(objp, cachep->object_size, 1, cachep->flags,
3377 kmemcheck_slab_alloc(cachep, flags, objp, cachep->object_size);
3379 if (unlikely((flags & __GFP_ZERO) && objp))
3380 memset(objp, 0, cachep->object_size);
3386 * Caller needs to acquire correct kmem_list's list_lock
3388 static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
3392 struct kmem_cache_node *n;
3394 for (i = 0; i < nr_objects; i++) {
3398 clear_obj_pfmemalloc(&objpp[i]);
3401 slabp = virt_to_slab(objp);
3402 n = cachep->node[node];
3403 list_del(&slabp->list);
3404 check_spinlock_acquired_node(cachep, node);
3405 slab_put_obj(cachep, slabp, objp, node);
3406 STATS_DEC_ACTIVE(cachep);
3409 /* fixup slab chains */
3410 if (slabp->inuse == 0) {
3411 if (n->free_objects > n->free_limit) {
3412 n->free_objects -= cachep->num;
3413 /* No need to drop any previously held
3414 * lock here, even if we have a off-slab slab
3415 * descriptor it is guaranteed to come from
3416 * a different cache, refer to comments before
3419 slab_destroy(cachep, slabp);
3421 list_add(&slabp->list, &n->slabs_free);
3424 /* Unconditionally move a slab to the end of the
3425 * partial list on free - maximum time for the
3426 * other objects to be freed, too.
3428 list_add_tail(&slabp->list, &n->slabs_partial);
3433 static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
3436 struct kmem_cache_node *n;
3437 int node = numa_mem_id();
3439 batchcount = ac->batchcount;
3441 BUG_ON(!batchcount || batchcount > ac->avail);
3444 n = cachep->node[node];
3445 spin_lock(&n->list_lock);
3447 struct array_cache *shared_array = n->shared;
3448 int max = shared_array->limit - shared_array->avail;
3450 if (batchcount > max)
3452 memcpy(&(shared_array->entry[shared_array->avail]),
3453 ac->entry, sizeof(void *) * batchcount);
3454 shared_array->avail += batchcount;
3459 free_block(cachep, ac->entry, batchcount, node);
3464 struct list_head *p;
3466 p = n->slabs_free.next;
3467 while (p != &(n->slabs_free)) {
3470 slabp = list_entry(p, struct slab, list);
3471 BUG_ON(slabp->inuse);
3476 STATS_SET_FREEABLE(cachep, i);
3479 spin_unlock(&n->list_lock);
3480 ac->avail -= batchcount;
3481 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
3485 * Release an obj back to its cache. If the obj has a constructed state, it must
3486 * be in this state _before_ it is released. Called with disabled ints.
3488 static inline void __cache_free(struct kmem_cache *cachep, void *objp,
3489 unsigned long caller)
3491 struct array_cache *ac = cpu_cache_get(cachep);
3494 kmemleak_free_recursive(objp, cachep->flags);
3495 objp = cache_free_debugcheck(cachep, objp, caller);
3497 kmemcheck_slab_free(cachep, objp, cachep->object_size);
3500 * Skip calling cache_free_alien() when the platform is not numa.
3501 * This will avoid cache misses that happen while accessing slabp (which
3502 * is per page memory reference) to get nodeid. Instead use a global
3503 * variable to skip the call, which is mostly likely to be present in
3506 if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
3509 if (likely(ac->avail < ac->limit)) {
3510 STATS_INC_FREEHIT(cachep);
3512 STATS_INC_FREEMISS(cachep);
3513 cache_flusharray(cachep, ac);
3516 ac_put_obj(cachep, ac, objp);
3520 * kmem_cache_alloc - Allocate an object
3521 * @cachep: The cache to allocate from.
3522 * @flags: See kmalloc().
3524 * Allocate an object from this cache. The flags are only relevant
3525 * if the cache has no available objects.
3527 void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
3529 void *ret = slab_alloc(cachep, flags, _RET_IP_);
3531 trace_kmem_cache_alloc(_RET_IP_, ret,
3532 cachep->object_size, cachep->size, flags);
3536 EXPORT_SYMBOL(kmem_cache_alloc);
3538 #ifdef CONFIG_TRACING
3540 kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
3544 ret = slab_alloc(cachep, flags, _RET_IP_);
3546 trace_kmalloc(_RET_IP_, ret,
3547 size, cachep->size, flags);
3550 EXPORT_SYMBOL(kmem_cache_alloc_trace);
3555 * kmem_cache_alloc_node - Allocate an object on the specified node
3556 * @cachep: The cache to allocate from.
3557 * @flags: See kmalloc().
3558 * @nodeid: node number of the target node.
3560 * Identical to kmem_cache_alloc but it will allocate memory on the given
3561 * node, which can improve the performance for cpu bound structures.
3563 * Fallback to other node is possible if __GFP_THISNODE is not set.
3565 void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
3567 void *ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
3569 trace_kmem_cache_alloc_node(_RET_IP_, ret,
3570 cachep->object_size, cachep->size,
3575 EXPORT_SYMBOL(kmem_cache_alloc_node);
3577 #ifdef CONFIG_TRACING
3578 void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
3585 ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
3587 trace_kmalloc_node(_RET_IP_, ret,
3592 EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
3595 static __always_inline void *
3596 __do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller)
3598 struct kmem_cache *cachep;
3600 cachep = kmalloc_slab(size, flags);
3601 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3603 return kmem_cache_alloc_node_trace(cachep, flags, node, size);
3606 #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
3607 void *__kmalloc_node(size_t size, gfp_t flags, int node)
3609 return __do_kmalloc_node(size, flags, node, _RET_IP_);
3611 EXPORT_SYMBOL(__kmalloc_node);
3613 void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
3614 int node, unsigned long caller)
3616 return __do_kmalloc_node(size, flags, node, caller);
3618 EXPORT_SYMBOL(__kmalloc_node_track_caller);
3620 void *__kmalloc_node(size_t size, gfp_t flags, int node)
3622 return __do_kmalloc_node(size, flags, node, 0);
3624 EXPORT_SYMBOL(__kmalloc_node);
3625 #endif /* CONFIG_DEBUG_SLAB || CONFIG_TRACING */
3626 #endif /* CONFIG_NUMA */
3629 * __do_kmalloc - allocate memory
3630 * @size: how many bytes of memory are required.
3631 * @flags: the type of memory to allocate (see kmalloc).
3632 * @caller: function caller for debug tracking of the caller
3634 static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
3635 unsigned long caller)
3637 struct kmem_cache *cachep;
3640 /* If you want to save a few bytes .text space: replace
3642 * Then kmalloc uses the uninlined functions instead of the inline
3645 cachep = kmalloc_slab(size, flags);
3646 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3648 ret = slab_alloc(cachep, flags, caller);
3650 trace_kmalloc(caller, ret,
3651 size, cachep->size, flags);
3657 #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
3658 void *__kmalloc(size_t size, gfp_t flags)
3660 return __do_kmalloc(size, flags, _RET_IP_);
3662 EXPORT_SYMBOL(__kmalloc);
3664 void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
3666 return __do_kmalloc(size, flags, caller);
3668 EXPORT_SYMBOL(__kmalloc_track_caller);
3671 void *__kmalloc(size_t size, gfp_t flags)
3673 return __do_kmalloc(size, flags, 0);
3675 EXPORT_SYMBOL(__kmalloc);
3679 * kmem_cache_free - Deallocate an object
3680 * @cachep: The cache the allocation was from.
3681 * @objp: The previously allocated object.
3683 * Free an object which was previously allocated from this
3686 void kmem_cache_free(struct kmem_cache *cachep, void *objp)
3688 unsigned long flags;
3689 cachep = cache_from_obj(cachep, objp);
3693 local_irq_save(flags);
3694 debug_check_no_locks_freed(objp, cachep->object_size);
3695 if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
3696 debug_check_no_obj_freed(objp, cachep->object_size);
3697 __cache_free(cachep, objp, _RET_IP_);
3698 local_irq_restore(flags);
3700 trace_kmem_cache_free(_RET_IP_, objp);
3702 EXPORT_SYMBOL(kmem_cache_free);
3705 * kfree - free previously allocated memory
3706 * @objp: pointer returned by kmalloc.
3708 * If @objp is NULL, no operation is performed.
3710 * Don't free memory not originally allocated by kmalloc()
3711 * or you will run into trouble.
3713 void kfree(const void *objp)
3715 struct kmem_cache *c;
3716 unsigned long flags;
3718 trace_kfree(_RET_IP_, objp);
3720 if (unlikely(ZERO_OR_NULL_PTR(objp)))
3722 local_irq_save(flags);
3723 kfree_debugcheck(objp);
3724 c = virt_to_cache(objp);
3725 debug_check_no_locks_freed(objp, c->object_size);
3727 debug_check_no_obj_freed(objp, c->object_size);
3728 __cache_free(c, (void *)objp, _RET_IP_);
3729 local_irq_restore(flags);
3731 EXPORT_SYMBOL(kfree);
3734 * This initializes kmem_cache_node or resizes various caches for all nodes.
3736 static int alloc_kmemlist(struct kmem_cache *cachep, gfp_t gfp)
3739 struct kmem_cache_node *n;
3740 struct array_cache *new_shared;
3741 struct array_cache **new_alien = NULL;
3743 for_each_online_node(node) {
3745 if (use_alien_caches) {
3746 new_alien = alloc_alien_cache(node, cachep->limit, gfp);
3752 if (cachep->shared) {
3753 new_shared = alloc_arraycache(node,
3754 cachep->shared*cachep->batchcount,
3757 free_alien_cache(new_alien);
3762 n = cachep->node[node];
3764 struct array_cache *shared = n->shared;
3766 spin_lock_irq(&n->list_lock);
3769 free_block(cachep, shared->entry,
3770 shared->avail, node);
3772 n->shared = new_shared;
3774 n->alien = new_alien;
3777 n->free_limit = (1 + nr_cpus_node(node)) *
3778 cachep->batchcount + cachep->num;
3779 spin_unlock_irq(&n->list_lock);
3781 free_alien_cache(new_alien);
3784 n = kmalloc_node(sizeof(struct kmem_cache_node), gfp, node);
3786 free_alien_cache(new_alien);
3791 kmem_cache_node_init(n);
3792 n->next_reap = jiffies + REAPTIMEOUT_LIST3 +
3793 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
3794 n->shared = new_shared;
3795 n->alien = new_alien;
3796 n->free_limit = (1 + nr_cpus_node(node)) *
3797 cachep->batchcount + cachep->num;
3798 cachep->node[node] = n;
3803 if (!cachep->list.next) {
3804 /* Cache is not active yet. Roll back what we did */
3807 if (cachep->node[node]) {
3808 n = cachep->node[node];
3811 free_alien_cache(n->alien);
3813 cachep->node[node] = NULL;
3821 struct ccupdate_struct {
3822 struct kmem_cache *cachep;
3823 struct array_cache *new[0];
3826 static void do_ccupdate_local(void *info)
3828 struct ccupdate_struct *new = info;
3829 struct array_cache *old;
3832 old = cpu_cache_get(new->cachep);
3834 new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
3835 new->new[smp_processor_id()] = old;
3838 /* Always called with the slab_mutex held */
3839 static int __do_tune_cpucache(struct kmem_cache *cachep, int limit,
3840 int batchcount, int shared, gfp_t gfp)
3842 struct ccupdate_struct *new;
3845 new = kzalloc(sizeof(*new) + nr_cpu_ids * sizeof(struct array_cache *),
3850 for_each_online_cpu(i) {
3851 new->new[i] = alloc_arraycache(cpu_to_mem(i), limit,
3854 for (i--; i >= 0; i--)
3860 new->cachep = cachep;
3862 on_each_cpu(do_ccupdate_local, (void *)new, 1);
3865 cachep->batchcount = batchcount;
3866 cachep->limit = limit;
3867 cachep->shared = shared;
3869 for_each_online_cpu(i) {
3870 struct array_cache *ccold = new->new[i];
3873 spin_lock_irq(&cachep->node[cpu_to_mem(i)]->list_lock);
3874 free_block(cachep, ccold->entry, ccold->avail, cpu_to_mem(i));
3875 spin_unlock_irq(&cachep->node[cpu_to_mem(i)]->list_lock);
3879 return alloc_kmemlist(cachep, gfp);
3882 static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
3883 int batchcount, int shared, gfp_t gfp)
3886 struct kmem_cache *c = NULL;
3889 ret = __do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
3891 if (slab_state < FULL)
3894 if ((ret < 0) || !is_root_cache(cachep))
3897 VM_BUG_ON(!mutex_is_locked(&slab_mutex));
3898 for_each_memcg_cache_index(i) {
3899 c = cache_from_memcg(cachep, i);
3901 /* return value determined by the parent cache only */
3902 __do_tune_cpucache(c, limit, batchcount, shared, gfp);
3908 /* Called with slab_mutex held always */
3909 static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
3916 if (!is_root_cache(cachep)) {
3917 struct kmem_cache *root = memcg_root_cache(cachep);
3918 limit = root->limit;
3919 shared = root->shared;
3920 batchcount = root->batchcount;
3923 if (limit && shared && batchcount)
3926 * The head array serves three purposes:
3927 * - create a LIFO ordering, i.e. return objects that are cache-warm
3928 * - reduce the number of spinlock operations.
3929 * - reduce the number of linked list operations on the slab and
3930 * bufctl chains: array operations are cheaper.
3931 * The numbers are guessed, we should auto-tune as described by
3934 if (cachep->size > 131072)
3936 else if (cachep->size > PAGE_SIZE)
3938 else if (cachep->size > 1024)
3940 else if (cachep->size > 256)
3946 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
3947 * allocation behaviour: Most allocs on one cpu, most free operations
3948 * on another cpu. For these cases, an efficient object passing between
3949 * cpus is necessary. This is provided by a shared array. The array
3950 * replaces Bonwick's magazine layer.
3951 * On uniprocessor, it's functionally equivalent (but less efficient)
3952 * to a larger limit. Thus disabled by default.
3955 if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1)
3960 * With debugging enabled, large batchcount lead to excessively long
3961 * periods with disabled local interrupts. Limit the batchcount
3966 batchcount = (limit + 1) / 2;
3968 err = do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
3970 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
3971 cachep->name, -err);
3976 * Drain an array if it contains any elements taking the node lock only if
3977 * necessary. Note that the node listlock also protects the array_cache
3978 * if drain_array() is used on the shared array.
3980 static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
3981 struct array_cache *ac, int force, int node)
3985 if (!ac || !ac->avail)
3987 if (ac->touched && !force) {
3990 spin_lock_irq(&n->list_lock);
3992 tofree = force ? ac->avail : (ac->limit + 4) / 5;
3993 if (tofree > ac->avail)
3994 tofree = (ac->avail + 1) / 2;
3995 free_block(cachep, ac->entry, tofree, node);
3996 ac->avail -= tofree;
3997 memmove(ac->entry, &(ac->entry[tofree]),
3998 sizeof(void *) * ac->avail);
4000 spin_unlock_irq(&n->list_lock);
4005 * cache_reap - Reclaim memory from caches.
4006 * @w: work descriptor
4008 * Called from workqueue/eventd every few seconds.
4010 * - clear the per-cpu caches for this CPU.
4011 * - return freeable pages to the main free memory pool.
4013 * If we cannot acquire the cache chain mutex then just give up - we'll try
4014 * again on the next iteration.
4016 static void cache_reap(struct work_struct *w)
4018 struct kmem_cache *searchp;
4019 struct kmem_cache_node *n;
4020 int node = numa_mem_id();
4021 struct delayed_work *work = to_delayed_work(w);
4023 if (!mutex_trylock(&slab_mutex))
4024 /* Give up. Setup the next iteration. */
4027 list_for_each_entry(searchp, &slab_caches, list) {
4031 * We only take the node lock if absolutely necessary and we
4032 * have established with reasonable certainty that
4033 * we can do some work if the lock was obtained.
4035 n = searchp->node[node];
4037 reap_alien(searchp, n);
4039 drain_array(searchp, n, cpu_cache_get(searchp), 0, node);
4042 * These are racy checks but it does not matter
4043 * if we skip one check or scan twice.
4045 if (time_after(n->next_reap, jiffies))
4048 n->next_reap = jiffies + REAPTIMEOUT_LIST3;
4050 drain_array(searchp, n, n->shared, 0, node);
4052 if (n->free_touched)
4053 n->free_touched = 0;
4057 freed = drain_freelist(searchp, n, (n->free_limit +
4058 5 * searchp->num - 1) / (5 * searchp->num));
4059 STATS_ADD_REAPED(searchp, freed);
4065 mutex_unlock(&slab_mutex);
4068 /* Set up the next iteration */
4069 schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_CPUC));
4072 #ifdef CONFIG_SLABINFO
4073 void get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo)
4076 unsigned long active_objs;
4077 unsigned long num_objs;
4078 unsigned long active_slabs = 0;
4079 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
4083 struct kmem_cache_node *n;
4087 for_each_online_node(node) {
4088 n = cachep->node[node];
4093 spin_lock_irq(&n->list_lock);
4095 list_for_each_entry(slabp, &n->slabs_full, list) {
4096 if (slabp->inuse != cachep->num && !error)
4097 error = "slabs_full accounting error";
4098 active_objs += cachep->num;
4101 list_for_each_entry(slabp, &n->slabs_partial, list) {
4102 if (slabp->inuse == cachep->num && !error)
4103 error = "slabs_partial inuse accounting error";
4104 if (!slabp->inuse && !error)
4105 error = "slabs_partial/inuse accounting error";
4106 active_objs += slabp->inuse;
4109 list_for_each_entry(slabp, &n->slabs_free, list) {
4110 if (slabp->inuse && !error)
4111 error = "slabs_free/inuse accounting error";
4114 free_objects += n->free_objects;
4116 shared_avail += n->shared->avail;
4118 spin_unlock_irq(&n->list_lock);
4120 num_slabs += active_slabs;
4121 num_objs = num_slabs * cachep->num;
4122 if (num_objs - active_objs != free_objects && !error)
4123 error = "free_objects accounting error";
4125 name = cachep->name;
4127 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
4129 sinfo->active_objs = active_objs;
4130 sinfo->num_objs = num_objs;
4131 sinfo->active_slabs = active_slabs;
4132 sinfo->num_slabs = num_slabs;
4133 sinfo->shared_avail = shared_avail;
4134 sinfo->limit = cachep->limit;
4135 sinfo->batchcount = cachep->batchcount;
4136 sinfo->shared = cachep->shared;
4137 sinfo->objects_per_slab = cachep->num;
4138 sinfo->cache_order = cachep->gfporder;
4141 void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *cachep)
4145 unsigned long high = cachep->high_mark;
4146 unsigned long allocs = cachep->num_allocations;
4147 unsigned long grown = cachep->grown;
4148 unsigned long reaped = cachep->reaped;
4149 unsigned long errors = cachep->errors;
4150 unsigned long max_freeable = cachep->max_freeable;
4151 unsigned long node_allocs = cachep->node_allocs;
4152 unsigned long node_frees = cachep->node_frees;
4153 unsigned long overflows = cachep->node_overflow;
4155 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu "
4156 "%4lu %4lu %4lu %4lu %4lu",
4157 allocs, high, grown,
4158 reaped, errors, max_freeable, node_allocs,
4159 node_frees, overflows);
4163 unsigned long allochit = atomic_read(&cachep->allochit);
4164 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
4165 unsigned long freehit = atomic_read(&cachep->freehit);
4166 unsigned long freemiss = atomic_read(&cachep->freemiss);
4168 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
4169 allochit, allocmiss, freehit, freemiss);
4174 #define MAX_SLABINFO_WRITE 128
4176 * slabinfo_write - Tuning for the slab allocator
4178 * @buffer: user buffer
4179 * @count: data length
4182 ssize_t slabinfo_write(struct file *file, const char __user *buffer,
4183 size_t count, loff_t *ppos)
4185 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
4186 int limit, batchcount, shared, res;
4187 struct kmem_cache *cachep;
4189 if (count > MAX_SLABINFO_WRITE)
4191 if (copy_from_user(&kbuf, buffer, count))
4193 kbuf[MAX_SLABINFO_WRITE] = '\0';
4195 tmp = strchr(kbuf, ' ');
4200 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
4203 /* Find the cache in the chain of caches. */
4204 mutex_lock(&slab_mutex);
4206 list_for_each_entry(cachep, &slab_caches, list) {
4207 if (!strcmp(cachep->name, kbuf)) {
4208 if (limit < 1 || batchcount < 1 ||
4209 batchcount > limit || shared < 0) {
4212 res = do_tune_cpucache(cachep, limit,
4219 mutex_unlock(&slab_mutex);
4225 #ifdef CONFIG_DEBUG_SLAB_LEAK
4227 static void *leaks_start(struct seq_file *m, loff_t *pos)
4229 mutex_lock(&slab_mutex);
4230 return seq_list_start(&slab_caches, *pos);
4233 static inline int add_caller(unsigned long *n, unsigned long v)
4243 unsigned long *q = p + 2 * i;
4257 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4263 static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
4270 for (i = 0, p = s->s_mem; i < c->num; i++, p += c->size) {
4273 for (j = s->free; j < c->num; j++) {
4274 /* Skip freed item */
4275 if (slab_bufctl(s)[j] == i) {
4283 if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
4288 static void show_symbol(struct seq_file *m, unsigned long address)
4290 #ifdef CONFIG_KALLSYMS
4291 unsigned long offset, size;
4292 char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
4294 if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
4295 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
4297 seq_printf(m, " [%s]", modname);
4301 seq_printf(m, "%p", (void *)address);
4304 static int leaks_show(struct seq_file *m, void *p)
4306 struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);
4308 struct kmem_cache_node *n;
4310 unsigned long *x = m->private;
4314 if (!(cachep->flags & SLAB_STORE_USER))
4316 if (!(cachep->flags & SLAB_RED_ZONE))
4319 /* OK, we can do it */
4323 for_each_online_node(node) {
4324 n = cachep->node[node];
4329 spin_lock_irq(&n->list_lock);
4331 list_for_each_entry(slabp, &n->slabs_full, list)
4332 handle_slab(x, cachep, slabp);
4333 list_for_each_entry(slabp, &n->slabs_partial, list)
4334 handle_slab(x, cachep, slabp);
4335 spin_unlock_irq(&n->list_lock);
4337 name = cachep->name;
4339 /* Increase the buffer size */
4340 mutex_unlock(&slab_mutex);
4341 m->private = kzalloc(x[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
4343 /* Too bad, we are really out */
4345 mutex_lock(&slab_mutex);
4348 *(unsigned long *)m->private = x[0] * 2;
4350 mutex_lock(&slab_mutex);
4351 /* Now make sure this entry will be retried */
4355 for (i = 0; i < x[1]; i++) {
4356 seq_printf(m, "%s: %lu ", name, x[2*i+3]);
4357 show_symbol(m, x[2*i+2]);
4364 static const struct seq_operations slabstats_op = {
4365 .start = leaks_start,
4371 static int slabstats_open(struct inode *inode, struct file *file)
4373 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
4376 ret = seq_open(file, &slabstats_op);
4378 struct seq_file *m = file->private_data;
4379 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
4388 static const struct file_operations proc_slabstats_operations = {
4389 .open = slabstats_open,
4391 .llseek = seq_lseek,
4392 .release = seq_release_private,
4396 static int __init slab_proc_init(void)
4398 #ifdef CONFIG_DEBUG_SLAB_LEAK
4399 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
4403 module_init(slab_proc_init);
4407 * ksize - get the actual amount of memory allocated for a given object
4408 * @objp: Pointer to the object
4410 * kmalloc may internally round up allocations and return more memory
4411 * than requested. ksize() can be used to determine the actual amount of
4412 * memory allocated. The caller may use this additional memory, even though
4413 * a smaller amount of memory was initially specified with the kmalloc call.
4414 * The caller must guarantee that objp points to a valid object previously
4415 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4416 * must not be freed during the duration of the call.
4418 size_t ksize(const void *objp)
4421 if (unlikely(objp == ZERO_SIZE_PTR))
4424 return virt_to_cache(objp)->object_size;
4426 EXPORT_SYMBOL(ksize);