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 'cache_chain_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 <trace/kmemtrace.h>
106 #include <linux/rcupdate.h>
107 #include <linux/string.h>
108 #include <linux/uaccess.h>
109 #include <linux/nodemask.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>
118 #include <asm/cacheflush.h>
119 #include <asm/tlbflush.h>
120 #include <asm/page.h>
123 * On !PREEMPT_RT, raw irq flags are used as a per-CPU locking
126 * On PREEMPT_RT, we use per-CPU locks for this. That's why the
127 * calling convention is changed slightly: a new 'flags' argument
128 * is passed to 'irq disable/enable' - the PREEMPT_RT code stores
129 * the CPU number of the lock there.
131 #ifndef CONFIG_PREEMPT_RT
133 # define slab_irq_disable(cpu) \
134 do { local_irq_disable(); (cpu) = smp_processor_id(); } while (0)
135 # define slab_irq_enable(cpu) local_irq_enable()
137 static inline void slab_irq_disable_this_rt(int cpu
)
141 static inline void slab_irq_enable_rt(int cpu
)
145 # define slab_irq_save(flags, cpu) \
146 do { local_irq_save(flags); (cpu) = smp_processor_id(); } while (0)
147 # define slab_irq_restore(flags, cpu) local_irq_restore(flags)
150 * In the __GFP_WAIT case we enable/disable interrupts on !PREEMPT_RT,
151 * which has no per-CPU locking effect since we are holding the cache
152 * lock in that case already.
154 static void slab_irq_enable_GFP_WAIT(gfp_t flags
, int *cpu
)
156 if (flags
& __GFP_WAIT
)
160 static void slab_irq_disable_GFP_WAIT(gfp_t flags
, int *cpu
)
162 if (flags
& __GFP_WAIT
)
166 # define slab_spin_lock_irq(lock, cpu) \
167 do { spin_lock_irq(lock); (cpu) = smp_processor_id(); } while (0)
168 # define slab_spin_unlock_irq(lock, cpu) spin_unlock_irq(lock)
170 # define slab_spin_lock_irqsave(lock, flags, cpu) \
171 do { spin_lock_irqsave(lock, flags); (cpu) = smp_processor_id(); } while (0)
172 # define slab_spin_unlock_irqrestore(lock, flags, cpu) \
173 do { spin_unlock_irqrestore(lock, flags); } while (0)
175 #else /* CONFIG_PREEMPT_RT */
178 * Instead of serializing the per-cpu state by disabling interrupts we do so
179 * by a lock. This keeps the code preemptable - albeit at the cost of remote
180 * memory access when the task does get migrated away.
182 DEFINE_PER_CPU_LOCKED(struct list_head
, slab
) = { 0, };
184 static void _slab_irq_disable(int *cpu
)
186 (void)get_cpu_var_locked(slab
, cpu
);
189 #define slab_irq_disable(cpu) _slab_irq_disable(&(cpu))
191 static inline void slab_irq_enable(int cpu
)
195 list_splice_init(&__get_cpu_var_locked(slab
, cpu
), &list
);
196 put_cpu_var_locked(slab
, cpu
);
198 while (!list_empty(&list
)) {
199 struct page
*page
= list_first_entry(&list
, struct page
, lru
);
200 list_del(&page
->lru
);
201 __free_pages(page
, page
->index
);
205 static inline void slab_irq_disable_this_rt(int cpu
)
207 spin_lock(&__get_cpu_lock(slab
, cpu
));
210 static inline void slab_irq_enable_rt(int cpu
)
214 list_splice_init(&__get_cpu_var_locked(slab
, cpu
), &list
);
215 spin_unlock(&__get_cpu_lock(slab
, cpu
));
217 while (!list_empty(&list
)) {
218 struct page
*page
= list_first_entry(&list
, struct page
, lru
);
219 list_del(&page
->lru
);
220 __free_pages(page
, page
->index
);
224 # define slab_irq_save(flags, cpu) \
225 do { slab_irq_disable(cpu); (void) (flags); } while (0)
226 # define slab_irq_restore(flags, cpu) \
227 do { slab_irq_enable(cpu); (void) (flags); } while (0)
230 * On PREEMPT_RT we have to drop the locks unconditionally to avoid lock
231 * recursion on the cache_grow()->alloc_slabmgmt() path.
233 static void slab_irq_enable_GFP_WAIT(gfp_t flags
, int *cpu
)
235 slab_irq_enable(*cpu
);
238 static void slab_irq_disable_GFP_WAIT(gfp_t flags
, int *cpu
)
240 slab_irq_disable(*cpu
);
243 # define slab_spin_lock_irq(lock, cpu) \
244 do { slab_irq_disable(cpu); spin_lock(lock); } while (0)
245 # define slab_spin_unlock_irq(lock, cpu) \
246 do { spin_unlock(lock); slab_irq_enable(cpu); } while (0)
247 # define slab_spin_lock_irqsave(lock, flags, cpu) \
248 do { slab_irq_disable(cpu); spin_lock_irqsave(lock, flags); } while (0)
249 # define slab_spin_unlock_irqrestore(lock, flags, cpu) \
250 do { spin_unlock_irqrestore(lock, flags); slab_irq_enable(cpu); } while (0)
252 #endif /* CONFIG_PREEMPT_RT */
255 * DEBUG - 1 for kmem_cache_create() to honour; SLAB_RED_ZONE & SLAB_POISON.
256 * 0 for faster, smaller code (especially in the critical paths).
258 * STATS - 1 to collect stats for /proc/slabinfo.
259 * 0 for faster, smaller code (especially in the critical paths).
261 * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
264 #ifdef CONFIG_DEBUG_SLAB
267 #define FORCED_DEBUG 1
271 #define FORCED_DEBUG 0
274 /* Shouldn't this be in a header file somewhere? */
275 #define BYTES_PER_WORD sizeof(void *)
276 #define REDZONE_ALIGN max(BYTES_PER_WORD, __alignof__(unsigned long long))
278 #ifndef ARCH_KMALLOC_MINALIGN
280 * Enforce a minimum alignment for the kmalloc caches.
281 * Usually, the kmalloc caches are cache_line_size() aligned, except when
282 * DEBUG and FORCED_DEBUG are enabled, then they are BYTES_PER_WORD aligned.
283 * Some archs want to perform DMA into kmalloc caches and need a guaranteed
284 * alignment larger than the alignment of a 64-bit integer.
285 * ARCH_KMALLOC_MINALIGN allows that.
286 * Note that increasing this value may disable some debug features.
288 #define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
291 #ifndef ARCH_SLAB_MINALIGN
293 * Enforce a minimum alignment for all caches.
294 * Intended for archs that get misalignment faults even for BYTES_PER_WORD
295 * aligned buffers. Includes ARCH_KMALLOC_MINALIGN.
296 * If possible: Do not enable this flag for CONFIG_DEBUG_SLAB, it disables
297 * some debug features.
299 #define ARCH_SLAB_MINALIGN 0
302 #ifndef ARCH_KMALLOC_FLAGS
303 #define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
306 /* Legal flag mask for kmem_cache_create(). */
308 # define CREATE_MASK (SLAB_RED_ZONE | \
309 SLAB_POISON | SLAB_HWCACHE_ALIGN | \
312 SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
313 SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD | \
314 SLAB_DEBUG_OBJECTS | SLAB_NOTRACK)
316 # define CREATE_MASK (SLAB_HWCACHE_ALIGN | \
318 SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
319 SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD | \
320 SLAB_DEBUG_OBJECTS | SLAB_NOTRACK)
326 * Bufctl's are used for linking objs within a slab
329 * This implementation relies on "struct page" for locating the cache &
330 * slab an object belongs to.
331 * This allows the bufctl structure to be small (one int), but limits
332 * the number of objects a slab (not a cache) can contain when off-slab
333 * bufctls are used. The limit is the size of the largest general cache
334 * that does not use off-slab slabs.
335 * For 32bit archs with 4 kB pages, is this 56.
336 * This is not serious, as it is only for large objects, when it is unwise
337 * to have too many per slab.
338 * Note: This limit can be raised by introducing a general cache whose size
339 * is less than 512 (PAGE_SIZE<<3), but greater than 256.
342 typedef unsigned int kmem_bufctl_t
;
343 #define BUFCTL_END (((kmem_bufctl_t)(~0U))-0)
344 #define BUFCTL_FREE (((kmem_bufctl_t)(~0U))-1)
345 #define BUFCTL_ACTIVE (((kmem_bufctl_t)(~0U))-2)
346 #define SLAB_LIMIT (((kmem_bufctl_t)(~0U))-3)
351 * Manages the objs in a slab. Placed either at the beginning of mem allocated
352 * for a slab, or allocated from an general cache.
353 * Slabs are chained into three list: fully used, partial, fully free slabs.
356 struct list_head list
;
357 unsigned long colouroff
;
358 void *s_mem
; /* including colour offset */
359 unsigned int inuse
; /* num of objs active in slab */
361 unsigned short nodeid
;
367 * slab_destroy on a SLAB_DESTROY_BY_RCU cache uses this structure to
368 * arrange for kmem_freepages to be called via RCU. This is useful if
369 * we need to approach a kernel structure obliquely, from its address
370 * obtained without the usual locking. We can lock the structure to
371 * stabilize it and check it's still at the given address, only if we
372 * can be sure that the memory has not been meanwhile reused for some
373 * other kind of object (which our subsystem's lock might corrupt).
375 * rcu_read_lock before reading the address, then rcu_read_unlock after
376 * taking the spinlock within the structure expected at that address.
378 * We assume struct slab_rcu can overlay struct slab when destroying.
381 struct rcu_head head
;
382 struct kmem_cache
*cachep
;
390 * - LIFO ordering, to hand out cache-warm objects from _alloc
391 * - reduce the number of linked list operations
392 * - reduce spinlock operations
394 * The limit is stored in the per-cpu structure to reduce the data cache
401 unsigned int batchcount
;
402 unsigned int touched
;
405 * Must have this definition in here for the proper
406 * alignment of array_cache. Also simplifies accessing
412 * bootstrap: The caches do not work without cpuarrays anymore, but the
413 * cpuarrays are allocated from the generic caches...
415 #define BOOT_CPUCACHE_ENTRIES 1
416 struct arraycache_init
{
417 struct array_cache cache
;
418 void *entries
[BOOT_CPUCACHE_ENTRIES
];
422 * The slab lists for all objects.
425 struct list_head slabs_partial
; /* partial list first, better asm code */
426 struct list_head slabs_full
;
427 struct list_head slabs_free
;
428 unsigned long free_objects
;
429 unsigned int free_limit
;
430 unsigned int colour_next
; /* Per-node cache coloring */
431 spinlock_t list_lock
;
432 struct array_cache
*shared
; /* shared per node */
433 struct array_cache
**alien
; /* on other nodes */
434 unsigned long next_reap
; /* updated without locking */
435 int free_touched
; /* updated without locking */
439 * Need this for bootstrapping a per node allocator.
441 #define NUM_INIT_LISTS (3 * MAX_NUMNODES)
442 struct kmem_list3 __initdata initkmem_list3
[NUM_INIT_LISTS
];
443 #define CACHE_CACHE 0
444 #define SIZE_AC MAX_NUMNODES
445 #define SIZE_L3 (2 * MAX_NUMNODES)
447 static int drain_freelist(struct kmem_cache
*cache
,
448 struct kmem_list3
*l3
, int tofree
);
449 static void free_block(struct kmem_cache
*cachep
, void **objpp
, int len
,
450 int node
, int *this_cpu
);
451 static int enable_cpucache(struct kmem_cache
*cachep
);
452 static void cache_reap(struct work_struct
*unused
);
455 * This function must be completely optimized away if a constant is passed to
456 * it. Mostly the same as what is in linux/slab.h except it returns an index.
458 static __always_inline
int index_of(const size_t size
)
460 extern void __bad_size(void);
462 if (__builtin_constant_p(size
)) {
470 #include <linux/kmalloc_sizes.h>
478 static int slab_early_init
= 1;
480 #define INDEX_AC index_of(sizeof(struct arraycache_init))
481 #define INDEX_L3 index_of(sizeof(struct kmem_list3))
483 static void kmem_list3_init(struct kmem_list3
*parent
)
485 INIT_LIST_HEAD(&parent
->slabs_full
);
486 INIT_LIST_HEAD(&parent
->slabs_partial
);
487 INIT_LIST_HEAD(&parent
->slabs_free
);
488 parent
->shared
= NULL
;
489 parent
->alien
= NULL
;
490 parent
->colour_next
= 0;
491 spin_lock_init(&parent
->list_lock
);
492 parent
->free_objects
= 0;
493 parent
->free_touched
= 0;
496 #define MAKE_LIST(cachep, listp, slab, nodeid) \
498 INIT_LIST_HEAD(listp); \
499 list_splice(&(cachep->nodelists[nodeid]->slab), listp); \
502 #define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
504 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
505 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
506 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
509 #define CFLGS_OFF_SLAB (0x80000000UL)
510 #define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
512 #define BATCHREFILL_LIMIT 16
514 * Optimization question: fewer reaps means less probability for unnessary
515 * cpucache drain/refill cycles.
517 * OTOH the cpuarrays can contain lots of objects,
518 * which could lock up otherwise freeable slabs.
520 #define REAPTIMEOUT_CPUC (2*HZ)
521 #define REAPTIMEOUT_LIST3 (4*HZ)
524 #define STATS_INC_ACTIVE(x) ((x)->num_active++)
525 #define STATS_DEC_ACTIVE(x) ((x)->num_active--)
526 #define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
527 #define STATS_INC_GROWN(x) ((x)->grown++)
528 #define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
529 #define STATS_SET_HIGH(x) \
531 if ((x)->num_active > (x)->high_mark) \
532 (x)->high_mark = (x)->num_active; \
534 #define STATS_INC_ERR(x) ((x)->errors++)
535 #define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
536 #define STATS_INC_NODEFREES(x) ((x)->node_frees++)
537 #define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
538 #define STATS_SET_FREEABLE(x, i) \
540 if ((x)->max_freeable < i) \
541 (x)->max_freeable = i; \
543 #define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
544 #define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
545 #define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
546 #define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
548 #define STATS_INC_ACTIVE(x) do { } while (0)
549 #define STATS_DEC_ACTIVE(x) do { } while (0)
550 #define STATS_INC_ALLOCED(x) do { } while (0)
551 #define STATS_INC_GROWN(x) do { } while (0)
552 #define STATS_ADD_REAPED(x,y) do { } while (0)
553 #define STATS_SET_HIGH(x) do { } while (0)
554 #define STATS_INC_ERR(x) do { } while (0)
555 #define STATS_INC_NODEALLOCS(x) do { } while (0)
556 #define STATS_INC_NODEFREES(x) do { } while (0)
557 #define STATS_INC_ACOVERFLOW(x) do { } while (0)
558 #define STATS_SET_FREEABLE(x, i) do { } while (0)
559 #define STATS_INC_ALLOCHIT(x) do { } while (0)
560 #define STATS_INC_ALLOCMISS(x) do { } while (0)
561 #define STATS_INC_FREEHIT(x) do { } while (0)
562 #define STATS_INC_FREEMISS(x) do { } while (0)
568 * memory layout of objects:
570 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
571 * the end of an object is aligned with the end of the real
572 * allocation. Catches writes behind the end of the allocation.
573 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
575 * cachep->obj_offset: The real object.
576 * cachep->buffer_size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
577 * cachep->buffer_size - 1* BYTES_PER_WORD: last caller address
578 * [BYTES_PER_WORD long]
580 static int obj_offset(struct kmem_cache
*cachep
)
582 return cachep
->obj_offset
;
585 static int obj_size(struct kmem_cache
*cachep
)
587 return cachep
->obj_size
;
590 static unsigned long long *dbg_redzone1(struct kmem_cache
*cachep
, void *objp
)
592 BUG_ON(!(cachep
->flags
& SLAB_RED_ZONE
));
593 return (unsigned long long*) (objp
+ obj_offset(cachep
) -
594 sizeof(unsigned long long));
597 static unsigned long long *dbg_redzone2(struct kmem_cache
*cachep
, void *objp
)
599 BUG_ON(!(cachep
->flags
& SLAB_RED_ZONE
));
600 if (cachep
->flags
& SLAB_STORE_USER
)
601 return (unsigned long long *)(objp
+ cachep
->buffer_size
-
602 sizeof(unsigned long long) -
604 return (unsigned long long *) (objp
+ cachep
->buffer_size
-
605 sizeof(unsigned long long));
608 static void **dbg_userword(struct kmem_cache
*cachep
, void *objp
)
610 BUG_ON(!(cachep
->flags
& SLAB_STORE_USER
));
611 return (void **)(objp
+ cachep
->buffer_size
- BYTES_PER_WORD
);
616 #define obj_offset(x) 0
617 #define obj_size(cachep) (cachep->buffer_size)
618 #define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
619 #define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
620 #define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
624 #ifdef CONFIG_KMEMTRACE
625 size_t slab_buffer_size(struct kmem_cache
*cachep
)
627 return cachep
->buffer_size
;
629 EXPORT_SYMBOL(slab_buffer_size
);
633 * Do not go above this order unless 0 objects fit into the slab.
635 #define BREAK_GFP_ORDER_HI 1
636 #define BREAK_GFP_ORDER_LO 0
637 static int slab_break_gfp_order
= BREAK_GFP_ORDER_LO
;
640 * Functions for storing/retrieving the cachep and or slab from the page
641 * allocator. These are used to find the slab an obj belongs to. With kfree(),
642 * these are used to find the cache which an obj belongs to.
644 static inline void page_set_cache(struct page
*page
, struct kmem_cache
*cache
)
646 page
->lru
.next
= (struct list_head
*)cache
;
649 static inline struct kmem_cache
*page_get_cache(struct page
*page
)
651 page
= compound_head(page
);
652 BUG_ON(!PageSlab(page
));
653 return (struct kmem_cache
*)page
->lru
.next
;
656 static inline void page_set_slab(struct page
*page
, struct slab
*slab
)
658 page
->lru
.prev
= (struct list_head
*)slab
;
661 static inline struct slab
*page_get_slab(struct page
*page
)
663 BUG_ON(!PageSlab(page
));
664 return (struct slab
*)page
->lru
.prev
;
667 static inline struct kmem_cache
*virt_to_cache(const void *obj
)
669 struct page
*page
= virt_to_head_page(obj
);
670 return page_get_cache(page
);
673 static inline struct slab
*virt_to_slab(const void *obj
)
675 struct page
*page
= virt_to_head_page(obj
);
676 return page_get_slab(page
);
679 static inline void *index_to_obj(struct kmem_cache
*cache
, struct slab
*slab
,
682 return slab
->s_mem
+ cache
->buffer_size
* idx
;
686 * We want to avoid an expensive divide : (offset / cache->buffer_size)
687 * Using the fact that buffer_size is a constant for a particular cache,
688 * we can replace (offset / cache->buffer_size) by
689 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
691 static inline unsigned int obj_to_index(const struct kmem_cache
*cache
,
692 const struct slab
*slab
, void *obj
)
694 u32 offset
= (obj
- slab
->s_mem
);
695 return reciprocal_divide(offset
, cache
->reciprocal_buffer_size
);
699 * These are the default caches for kmalloc. Custom caches can have other sizes.
701 struct cache_sizes malloc_sizes
[] = {
702 #define CACHE(x) { .cs_size = (x) },
703 #include <linux/kmalloc_sizes.h>
707 EXPORT_SYMBOL(malloc_sizes
);
709 /* Must match cache_sizes above. Out of line to keep cache footprint low. */
715 static struct cache_names __initdata cache_names
[] = {
716 #define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },
717 #include <linux/kmalloc_sizes.h>
722 static struct arraycache_init initarray_cache __initdata
=
723 { {0, BOOT_CPUCACHE_ENTRIES
, 1, 0} };
724 static struct arraycache_init initarray_generic
=
725 { {0, BOOT_CPUCACHE_ENTRIES
, 1, 0} };
727 /* internal cache of cache description objs */
728 static struct kmem_cache cache_cache
= {
730 .limit
= BOOT_CPUCACHE_ENTRIES
,
732 .buffer_size
= sizeof(struct kmem_cache
),
733 .name
= "kmem_cache",
736 #define BAD_ALIEN_MAGIC 0x01020304ul
738 #ifdef CONFIG_LOCKDEP
741 * Slab sometimes uses the kmalloc slabs to store the slab headers
742 * for other slabs "off slab".
743 * The locking for this is tricky in that it nests within the locks
744 * of all other slabs in a few places; to deal with this special
745 * locking we put on-slab caches into a separate lock-class.
747 * We set lock class for alien array caches which are up during init.
748 * The lock annotation will be lost if all cpus of a node goes down and
749 * then comes back up during hotplug
751 static struct lock_class_key on_slab_l3_key
;
752 static struct lock_class_key on_slab_alc_key
;
754 static inline void init_lock_keys(void)
758 struct cache_sizes
*s
= malloc_sizes
;
760 while (s
->cs_size
!= ULONG_MAX
) {
762 struct array_cache
**alc
;
764 struct kmem_list3
*l3
= s
->cs_cachep
->nodelists
[q
];
765 if (!l3
|| OFF_SLAB(s
->cs_cachep
))
767 lockdep_set_class(&l3
->list_lock
, &on_slab_l3_key
);
770 * FIXME: This check for BAD_ALIEN_MAGIC
771 * should go away when common slab code is taught to
772 * work even without alien caches.
773 * Currently, non NUMA code returns BAD_ALIEN_MAGIC
774 * for alloc_alien_cache,
776 if (!alc
|| (unsigned long)alc
== BAD_ALIEN_MAGIC
)
780 lockdep_set_class(&alc
[r
]->lock
,
788 static inline void init_lock_keys(void)
794 * Guard access to the cache-chain.
796 static DEFINE_MUTEX(cache_chain_mutex
);
797 static struct list_head cache_chain
;
800 * chicken and egg problem: delay the per-cpu array allocation
801 * until the general caches are up.
811 * used by boot code to determine if it can use slab based allocator
813 int slab_is_available(void)
815 return g_cpucache_up
== FULL
;
818 static DEFINE_PER_CPU(struct delayed_work
, reap_work
);
820 static inline struct array_cache
*
821 cpu_cache_get(struct kmem_cache
*cachep
, int this_cpu
)
823 return cachep
->array
[this_cpu
];
826 static inline struct kmem_cache
*__find_general_cachep(size_t size
,
829 struct cache_sizes
*csizep
= malloc_sizes
;
832 /* This happens if someone tries to call
833 * kmem_cache_create(), or __kmalloc(), before
834 * the generic caches are initialized.
836 BUG_ON(malloc_sizes
[INDEX_AC
].cs_cachep
== NULL
);
839 return ZERO_SIZE_PTR
;
841 while (size
> csizep
->cs_size
)
845 * Really subtle: The last entry with cs->cs_size==ULONG_MAX
846 * has cs_{dma,}cachep==NULL. Thus no special case
847 * for large kmalloc calls required.
849 #ifdef CONFIG_ZONE_DMA
850 if (unlikely(gfpflags
& GFP_DMA
))
851 return csizep
->cs_dmacachep
;
853 return csizep
->cs_cachep
;
856 static struct kmem_cache
*kmem_find_general_cachep(size_t size
, gfp_t gfpflags
)
858 return __find_general_cachep(size
, gfpflags
);
861 static size_t slab_mgmt_size(size_t nr_objs
, size_t align
)
863 return ALIGN(sizeof(struct slab
)+nr_objs
*sizeof(kmem_bufctl_t
), align
);
867 * Calculate the number of objects and left-over bytes for a given buffer size.
869 static void cache_estimate(unsigned long gfporder
, size_t buffer_size
,
870 size_t align
, int flags
, size_t *left_over
,
875 size_t slab_size
= PAGE_SIZE
<< gfporder
;
878 * The slab management structure can be either off the slab or
879 * on it. For the latter case, the memory allocated for a
883 * - One kmem_bufctl_t for each object
884 * - Padding to respect alignment of @align
885 * - @buffer_size bytes for each object
887 * If the slab management structure is off the slab, then the
888 * alignment will already be calculated into the size. Because
889 * the slabs are all pages aligned, the objects will be at the
890 * correct alignment when allocated.
892 if (flags
& CFLGS_OFF_SLAB
) {
894 nr_objs
= slab_size
/ buffer_size
;
896 if (nr_objs
> SLAB_LIMIT
)
897 nr_objs
= SLAB_LIMIT
;
900 * Ignore padding for the initial guess. The padding
901 * is at most @align-1 bytes, and @buffer_size is at
902 * least @align. In the worst case, this result will
903 * be one greater than the number of objects that fit
904 * into the memory allocation when taking the padding
907 nr_objs
= (slab_size
- sizeof(struct slab
)) /
908 (buffer_size
+ sizeof(kmem_bufctl_t
));
911 * This calculated number will be either the right
912 * amount, or one greater than what we want.
914 if (slab_mgmt_size(nr_objs
, align
) + nr_objs
*buffer_size
918 if (nr_objs
> SLAB_LIMIT
)
919 nr_objs
= SLAB_LIMIT
;
921 mgmt_size
= slab_mgmt_size(nr_objs
, align
);
924 *left_over
= slab_size
- nr_objs
*buffer_size
- mgmt_size
;
927 #define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
929 static void __slab_error(const char *function
, struct kmem_cache
*cachep
,
932 printk(KERN_ERR
"slab error in %s(): cache `%s': %s\n",
933 function
, cachep
->name
, msg
);
938 * By default on NUMA we use alien caches to stage the freeing of
939 * objects allocated from other nodes. This causes massive memory
940 * inefficiencies when using fake NUMA setup to split memory into a
941 * large number of small nodes, so it can be disabled on the command
945 static int use_alien_caches __read_mostly
= 1;
946 static int numa_platform __read_mostly
= 1;
947 static int __init
noaliencache_setup(char *s
)
949 use_alien_caches
= 0;
952 __setup("noaliencache", noaliencache_setup
);
956 * Special reaping functions for NUMA systems called from cache_reap().
957 * These take care of doing round robin flushing of alien caches (containing
958 * objects freed on different nodes from which they were allocated) and the
959 * flushing of remote pcps by calling drain_node_pages.
961 static DEFINE_PER_CPU(unsigned long, reap_node
);
963 static void init_reap_node(int cpu
)
967 node
= next_node(cpu_to_node(cpu
), node_online_map
);
968 if (node
== MAX_NUMNODES
)
969 node
= first_node(node_online_map
);
971 per_cpu(reap_node
, cpu
) = node
;
974 static void next_reap_node(void)
976 int node
= __get_cpu_var(reap_node
);
978 node
= next_node(node
, node_online_map
);
979 if (unlikely(node
>= MAX_NUMNODES
))
980 node
= first_node(node_online_map
);
981 __get_cpu_var(reap_node
) = node
;
985 #define init_reap_node(cpu) do { } while (0)
986 #define next_reap_node(void) do { } while (0)
990 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
991 * via the workqueue/eventd.
992 * Add the CPU number into the expiration time to minimize the possibility of
993 * the CPUs getting into lockstep and contending for the global cache chain
996 static void __cpuinit
start_cpu_timer(int cpu
)
998 struct delayed_work
*reap_work
= &per_cpu(reap_work
, cpu
);
1001 * When this gets called from do_initcalls via cpucache_init(),
1002 * init_workqueues() has already run, so keventd will be setup
1005 if (keventd_up() && reap_work
->work
.func
== NULL
) {
1006 init_reap_node(cpu
);
1007 INIT_DELAYED_WORK(reap_work
, cache_reap
);
1008 schedule_delayed_work_on(cpu
, reap_work
,
1009 __round_jiffies_relative(HZ
, cpu
));
1013 static struct array_cache
*alloc_arraycache(int node
, int entries
,
1016 int memsize
= sizeof(void *) * entries
+ sizeof(struct array_cache
);
1017 struct array_cache
*nc
= NULL
;
1019 nc
= kmalloc_node(memsize
, GFP_KERNEL
, node
);
1022 nc
->limit
= entries
;
1023 nc
->batchcount
= batchcount
;
1025 spin_lock_init(&nc
->lock
);
1031 * Transfer objects in one arraycache to another.
1032 * Locking must be handled by the caller.
1034 * Return the number of entries transferred.
1036 static int transfer_objects(struct array_cache
*to
,
1037 struct array_cache
*from
, unsigned int max
)
1039 /* Figure out how many entries to transfer */
1040 int nr
= min(min(from
->avail
, max
), to
->limit
- to
->avail
);
1045 memcpy(to
->entry
+ to
->avail
, from
->entry
+ from
->avail
-nr
,
1046 sizeof(void *) *nr
);
1056 #define drain_alien_cache(cachep, alien) do { } while (0)
1057 #define reap_alien(cachep, l3, this_cpu) 0
1059 static inline struct array_cache
**alloc_alien_cache(int node
, int limit
)
1061 return (struct array_cache
**)BAD_ALIEN_MAGIC
;
1064 static inline void free_alien_cache(struct array_cache
**ac_ptr
)
1069 cache_free_alien(struct kmem_cache
*cachep
, void *objp
, int *this_cpu
)
1074 static inline void *alternate_node_alloc(struct kmem_cache
*cachep
,
1075 gfp_t flags
, int *this_cpu
)
1080 static inline void *____cache_alloc_node(struct kmem_cache
*cachep
,
1081 gfp_t flags
, int nodeid
, int *this_cpu
)
1086 #else /* CONFIG_NUMA */
1088 static void *____cache_alloc_node(struct kmem_cache
*cachep
, gfp_t flags
,
1089 int nodeid
, int *this_cpu
);
1090 static void *alternate_node_alloc(struct kmem_cache
*, gfp_t
, int *);
1092 static struct array_cache
**alloc_alien_cache(int node
, int limit
)
1094 struct array_cache
**ac_ptr
;
1095 int memsize
= sizeof(void *) * nr_node_ids
;
1100 ac_ptr
= kmalloc_node(memsize
, GFP_KERNEL
, node
);
1103 if (i
== node
|| !node_online(i
)) {
1107 ac_ptr
[i
] = alloc_arraycache(node
, limit
, 0xbaadf00d);
1109 for (i
--; i
>= 0; i
--)
1119 static void free_alien_cache(struct array_cache
**ac_ptr
)
1130 static void __drain_alien_cache(struct kmem_cache
*cachep
,
1131 struct array_cache
*ac
, int node
,
1134 struct kmem_list3
*rl3
= cachep
->nodelists
[node
];
1137 spin_lock(&rl3
->list_lock
);
1139 * Stuff objects into the remote nodes shared array first.
1140 * That way we could avoid the overhead of putting the objects
1141 * into the free lists and getting them back later.
1144 transfer_objects(rl3
->shared
, ac
, ac
->limit
);
1146 free_block(cachep
, ac
->entry
, ac
->avail
, node
, this_cpu
);
1148 spin_unlock(&rl3
->list_lock
);
1153 * Called from cache_reap() to regularly drain alien caches round robin.
1156 reap_alien(struct kmem_cache
*cachep
, struct kmem_list3
*l3
, int *this_cpu
)
1158 int node
= per_cpu(reap_node
, *this_cpu
);
1161 struct array_cache
*ac
= l3
->alien
[node
];
1163 if (ac
&& ac
->avail
&& spin_trylock_irq(&ac
->lock
)) {
1164 __drain_alien_cache(cachep
, ac
, node
, this_cpu
);
1165 spin_unlock_irq(&ac
->lock
);
1172 static void drain_alien_cache(struct kmem_cache
*cachep
,
1173 struct array_cache
**alien
)
1175 int i
= 0, this_cpu
;
1176 struct array_cache
*ac
;
1177 unsigned long flags
;
1179 for_each_online_node(i
) {
1182 slab_spin_lock_irqsave(&ac
->lock
, flags
, this_cpu
);
1183 __drain_alien_cache(cachep
, ac
, i
, &this_cpu
);
1184 slab_spin_unlock_irqrestore(&ac
->lock
, flags
, this_cpu
);
1190 cache_free_alien(struct kmem_cache
*cachep
, void *objp
, int *this_cpu
)
1192 struct slab
*slabp
= virt_to_slab(objp
);
1193 int nodeid
= slabp
->nodeid
;
1194 struct kmem_list3
*l3
;
1195 struct array_cache
*alien
= NULL
;
1198 node
= cpu_to_node(*this_cpu
);
1201 * Make sure we are not freeing a object from another node to the array
1202 * cache on this cpu.
1204 if (likely(slabp
->nodeid
== node
))
1207 l3
= cachep
->nodelists
[node
];
1208 STATS_INC_NODEFREES(cachep
);
1209 if (l3
->alien
&& l3
->alien
[nodeid
]) {
1210 alien
= l3
->alien
[nodeid
];
1211 spin_lock(&alien
->lock
);
1212 if (unlikely(alien
->avail
== alien
->limit
)) {
1213 STATS_INC_ACOVERFLOW(cachep
);
1214 __drain_alien_cache(cachep
, alien
, nodeid
, this_cpu
);
1216 alien
->entry
[alien
->avail
++] = objp
;
1217 spin_unlock(&alien
->lock
);
1219 spin_lock(&(cachep
->nodelists
[nodeid
])->list_lock
);
1220 free_block(cachep
, &objp
, 1, nodeid
, this_cpu
);
1221 spin_unlock(&(cachep
->nodelists
[nodeid
])->list_lock
);
1227 static void __cpuinit
cpuup_canceled(int cpu
)
1229 struct kmem_cache
*cachep
;
1230 struct kmem_list3
*l3
= NULL
;
1231 int node
= cpu_to_node(cpu
);
1232 const struct cpumask
*mask
= cpumask_of_node(node
);
1234 list_for_each_entry(cachep
, &cache_chain
, next
) {
1235 struct array_cache
*nc
;
1236 struct array_cache
*shared
;
1237 struct array_cache
**alien
;
1240 /* cpu is dead; no one can alloc from it. */
1241 nc
= cachep
->array
[cpu
];
1242 cachep
->array
[cpu
] = NULL
;
1243 l3
= cachep
->nodelists
[node
];
1246 goto free_array_cache
;
1248 spin_lock_irq(&l3
->list_lock
);
1250 /* Free limit for this kmem_list3 */
1251 l3
->free_limit
-= cachep
->batchcount
;
1253 free_block(cachep
, nc
->entry
, nc
->avail
, node
,
1256 if (!cpus_empty(*mask
)) {
1257 spin_unlock_irq(&l3
->list_lock
);
1258 goto free_array_cache
;
1261 shared
= l3
->shared
;
1263 free_block(cachep
, shared
->entry
,
1264 shared
->avail
, node
, &cpu
);
1271 spin_unlock_irq(&l3
->list_lock
);
1275 drain_alien_cache(cachep
, alien
);
1276 free_alien_cache(alien
);
1280 BUG_ON(cpu
!= orig_cpu
);
1283 * In the previous loop, all the objects were freed to
1284 * the respective cache's slabs, now we can go ahead and
1285 * shrink each nodelist to its limit.
1287 list_for_each_entry(cachep
, &cache_chain
, next
) {
1288 l3
= cachep
->nodelists
[node
];
1291 drain_freelist(cachep
, l3
, l3
->free_objects
);
1295 static int __cpuinit
cpuup_prepare(int cpu
)
1297 struct kmem_cache
*cachep
;
1298 struct kmem_list3
*l3
= NULL
;
1299 int node
= cpu_to_node(cpu
);
1300 const int memsize
= sizeof(struct kmem_list3
);
1303 * We need to do this right in the beginning since
1304 * alloc_arraycache's are going to use this list.
1305 * kmalloc_node allows us to add the slab to the right
1306 * kmem_list3 and not this cpu's kmem_list3
1309 list_for_each_entry(cachep
, &cache_chain
, next
) {
1311 * Set up the size64 kmemlist for cpu before we can
1312 * begin anything. Make sure some other cpu on this
1313 * node has not already allocated this
1315 if (!cachep
->nodelists
[node
]) {
1316 l3
= kmalloc_node(memsize
, GFP_KERNEL
, node
);
1319 kmem_list3_init(l3
);
1320 l3
->next_reap
= jiffies
+ REAPTIMEOUT_LIST3
+
1321 ((unsigned long)cachep
) % REAPTIMEOUT_LIST3
;
1324 * The l3s don't come and go as CPUs come and
1325 * go. cache_chain_mutex is sufficient
1328 cachep
->nodelists
[node
] = l3
;
1331 spin_lock_irq(&cachep
->nodelists
[node
]->list_lock
);
1332 cachep
->nodelists
[node
]->free_limit
=
1333 (1 + nr_cpus_node(node
)) *
1334 cachep
->batchcount
+ cachep
->num
;
1335 spin_unlock_irq(&cachep
->nodelists
[node
]->list_lock
);
1339 * Now we can go ahead with allocating the shared arrays and
1342 list_for_each_entry(cachep
, &cache_chain
, next
) {
1343 struct array_cache
*nc
;
1344 struct array_cache
*shared
= NULL
;
1345 struct array_cache
**alien
= NULL
;
1347 nc
= alloc_arraycache(node
, cachep
->limit
,
1348 cachep
->batchcount
);
1351 if (cachep
->shared
) {
1352 shared
= alloc_arraycache(node
,
1353 cachep
->shared
* cachep
->batchcount
,
1360 if (use_alien_caches
) {
1361 alien
= alloc_alien_cache(node
, cachep
->limit
);
1368 cachep
->array
[cpu
] = nc
;
1369 l3
= cachep
->nodelists
[node
];
1372 spin_lock_irq(&l3
->list_lock
);
1375 * We are serialised from CPU_DEAD or
1376 * CPU_UP_CANCELLED by the cpucontrol lock
1378 l3
->shared
= shared
;
1387 spin_unlock_irq(&l3
->list_lock
);
1389 free_alien_cache(alien
);
1393 cpuup_canceled(cpu
);
1397 static int __cpuinit
cpuup_callback(struct notifier_block
*nfb
,
1398 unsigned long action
, void *hcpu
)
1400 long cpu
= (long)hcpu
;
1405 case CPU_UP_PREPARE
:
1406 case CPU_UP_PREPARE_FROZEN
:
1407 mutex_lock(&cache_chain_mutex
);
1409 * lock/unlock cycle to push any holders away -- no new ones
1410 * can come in due to the cpu still being offline.
1412 * XXX -- weird case anyway, can it happen?
1414 slab_irq_disable_this_rt(cpu
);
1415 slab_irq_enable_rt(cpu
);
1416 err
= cpuup_prepare(cpu
);
1417 mutex_unlock(&cache_chain_mutex
);
1420 case CPU_ONLINE_FROZEN
:
1421 start_cpu_timer(cpu
);
1423 #ifdef CONFIG_HOTPLUG_CPU
1424 case CPU_DOWN_PREPARE
:
1425 case CPU_DOWN_PREPARE_FROZEN
:
1427 * Shutdown cache reaper. Note that the cache_chain_mutex is
1428 * held so that if cache_reap() is invoked it cannot do
1429 * anything expensive but will only modify reap_work
1430 * and reschedule the timer.
1432 cancel_rearming_delayed_work(&per_cpu(reap_work
, cpu
));
1433 /* Now the cache_reaper is guaranteed to be not running. */
1434 per_cpu(reap_work
, cpu
).work
.func
= NULL
;
1436 case CPU_DOWN_FAILED
:
1437 case CPU_DOWN_FAILED_FROZEN
:
1438 start_cpu_timer(cpu
);
1441 case CPU_DEAD_FROZEN
:
1443 * Even if all the cpus of a node are down, we don't free the
1444 * kmem_list3 of any cache. This to avoid a race between
1445 * cpu_down, and a kmalloc allocation from another cpu for
1446 * memory from the node of the cpu going down. The list3
1447 * structure is usually allocated from kmem_cache_create() and
1448 * gets destroyed at kmem_cache_destroy().
1452 case CPU_UP_CANCELED
:
1453 case CPU_UP_CANCELED_FROZEN
:
1454 mutex_lock(&cache_chain_mutex
);
1455 slab_irq_disable_this_rt(cpu
);
1456 cpuup_canceled(cpu
);
1457 slab_irq_enable_rt(cpu
);
1458 mutex_unlock(&cache_chain_mutex
);
1463 return err
? NOTIFY_BAD
: NOTIFY_OK
;
1466 static struct notifier_block __cpuinitdata cpucache_notifier
= {
1467 &cpuup_callback
, NULL
, 0
1471 * swap the static kmem_list3 with kmalloced memory
1473 static void init_list(struct kmem_cache
*cachep
, struct kmem_list3
*list
,
1476 struct kmem_list3
*ptr
;
1479 ptr
= kmalloc_node(sizeof(struct kmem_list3
), GFP_KERNEL
, nodeid
);
1482 WARN_ON(spin_is_locked(&list
->list_lock
));
1483 slab_irq_disable(this_cpu
);
1484 memcpy(ptr
, list
, sizeof(struct kmem_list3
));
1486 * Do not assume that spinlocks can be initialized via memcpy:
1488 spin_lock_init(&ptr
->list_lock
);
1490 MAKE_ALL_LISTS(cachep
, ptr
, nodeid
);
1491 cachep
->nodelists
[nodeid
] = ptr
;
1492 slab_irq_enable(this_cpu
);
1496 * For setting up all the kmem_list3s for cache whose buffer_size is same as
1497 * size of kmem_list3.
1499 static void __init
set_up_list3s(struct kmem_cache
*cachep
, int index
)
1503 for_each_online_node(node
) {
1504 cachep
->nodelists
[node
] = &initkmem_list3
[index
+ node
];
1505 cachep
->nodelists
[node
]->next_reap
= jiffies
+
1507 ((unsigned long)cachep
) % REAPTIMEOUT_LIST3
;
1512 * Initialisation. Called after the page allocator have been initialised and
1513 * before smp_init().
1515 void __init
kmem_cache_init(void)
1518 struct cache_sizes
*sizes
;
1519 struct cache_names
*names
;
1524 #ifdef CONFIG_PREEMPT_RT
1525 for_each_possible_cpu(i
) {
1526 INIT_LIST_HEAD(&__get_cpu_var_locked(slab
, i
));
1530 if (num_possible_nodes() == 1) {
1531 use_alien_caches
= 0;
1535 for (i
= 0; i
< NUM_INIT_LISTS
; i
++) {
1536 kmem_list3_init(&initkmem_list3
[i
]);
1537 if (i
< MAX_NUMNODES
)
1538 cache_cache
.nodelists
[i
] = NULL
;
1540 set_up_list3s(&cache_cache
, CACHE_CACHE
);
1543 * Fragmentation resistance on low memory - only use bigger
1544 * page orders on machines with more than 32MB of memory.
1546 if (num_physpages
> (32 << 20) >> PAGE_SHIFT
)
1547 slab_break_gfp_order
= BREAK_GFP_ORDER_HI
;
1549 /* Bootstrap is tricky, because several objects are allocated
1550 * from caches that do not exist yet:
1551 * 1) initialize the cache_cache cache: it contains the struct
1552 * kmem_cache structures of all caches, except cache_cache itself:
1553 * cache_cache is statically allocated.
1554 * Initially an __init data area is used for the head array and the
1555 * kmem_list3 structures, it's replaced with a kmalloc allocated
1556 * array at the end of the bootstrap.
1557 * 2) Create the first kmalloc cache.
1558 * The struct kmem_cache for the new cache is allocated normally.
1559 * An __init data area is used for the head array.
1560 * 3) Create the remaining kmalloc caches, with minimally sized
1562 * 4) Replace the __init data head arrays for cache_cache and the first
1563 * kmalloc cache with kmalloc allocated arrays.
1564 * 5) Replace the __init data for kmem_list3 for cache_cache and
1565 * the other cache's with kmalloc allocated memory.
1566 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
1569 node
= numa_node_id();
1571 /* 1) create the cache_cache */
1572 INIT_LIST_HEAD(&cache_chain
);
1573 list_add(&cache_cache
.next
, &cache_chain
);
1574 cache_cache
.colour_off
= cache_line_size();
1575 cache_cache
.array
[smp_processor_id()] = &initarray_cache
.cache
;
1576 cache_cache
.nodelists
[node
] = &initkmem_list3
[CACHE_CACHE
+ node
];
1579 * struct kmem_cache size depends on nr_node_ids, which
1580 * can be less than MAX_NUMNODES.
1582 cache_cache
.buffer_size
= offsetof(struct kmem_cache
, nodelists
) +
1583 nr_node_ids
* sizeof(struct kmem_list3
*);
1585 cache_cache
.obj_size
= cache_cache
.buffer_size
;
1587 cache_cache
.buffer_size
= ALIGN(cache_cache
.buffer_size
,
1589 cache_cache
.reciprocal_buffer_size
=
1590 reciprocal_value(cache_cache
.buffer_size
);
1592 for (order
= 0; order
< MAX_ORDER
; order
++) {
1593 cache_estimate(order
, cache_cache
.buffer_size
,
1594 cache_line_size(), 0, &left_over
, &cache_cache
.num
);
1595 if (cache_cache
.num
)
1598 BUG_ON(!cache_cache
.num
);
1599 cache_cache
.gfporder
= order
;
1600 cache_cache
.colour
= left_over
/ cache_cache
.colour_off
;
1601 cache_cache
.slab_size
= ALIGN(cache_cache
.num
* sizeof(kmem_bufctl_t
) +
1602 sizeof(struct slab
), cache_line_size());
1604 /* 2+3) create the kmalloc caches */
1605 sizes
= malloc_sizes
;
1606 names
= cache_names
;
1609 * Initialize the caches that provide memory for the array cache and the
1610 * kmem_list3 structures first. Without this, further allocations will
1614 sizes
[INDEX_AC
].cs_cachep
= kmem_cache_create(names
[INDEX_AC
].name
,
1615 sizes
[INDEX_AC
].cs_size
,
1616 ARCH_KMALLOC_MINALIGN
,
1617 ARCH_KMALLOC_FLAGS
|SLAB_PANIC
,
1620 if (INDEX_AC
!= INDEX_L3
) {
1621 sizes
[INDEX_L3
].cs_cachep
=
1622 kmem_cache_create(names
[INDEX_L3
].name
,
1623 sizes
[INDEX_L3
].cs_size
,
1624 ARCH_KMALLOC_MINALIGN
,
1625 ARCH_KMALLOC_FLAGS
|SLAB_PANIC
,
1629 slab_early_init
= 0;
1631 while (sizes
->cs_size
!= ULONG_MAX
) {
1633 * For performance, all the general caches are L1 aligned.
1634 * This should be particularly beneficial on SMP boxes, as it
1635 * eliminates "false sharing".
1636 * Note for systems short on memory removing the alignment will
1637 * allow tighter packing of the smaller caches.
1639 if (!sizes
->cs_cachep
) {
1640 sizes
->cs_cachep
= kmem_cache_create(names
->name
,
1642 ARCH_KMALLOC_MINALIGN
,
1643 ARCH_KMALLOC_FLAGS
|SLAB_PANIC
,
1646 #ifdef CONFIG_ZONE_DMA
1647 sizes
->cs_dmacachep
= kmem_cache_create(
1650 ARCH_KMALLOC_MINALIGN
,
1651 ARCH_KMALLOC_FLAGS
|SLAB_CACHE_DMA
|
1658 /* 4) Replace the bootstrap head arrays */
1660 struct array_cache
*ptr
;
1663 ptr
= kmalloc(sizeof(struct arraycache_init
), GFP_KERNEL
);
1665 slab_irq_disable(this_cpu
);
1666 BUG_ON(cpu_cache_get(&cache_cache
, this_cpu
) != &initarray_cache
.cache
);
1667 memcpy(ptr
, cpu_cache_get(&cache_cache
, this_cpu
),
1668 sizeof(struct arraycache_init
));
1670 * Do not assume that spinlocks can be initialized via memcpy:
1672 spin_lock_init(&ptr
->lock
);
1673 cache_cache
.array
[this_cpu
] = ptr
;
1674 slab_irq_enable(this_cpu
);
1676 ptr
= kmalloc(sizeof(struct arraycache_init
), GFP_KERNEL
);
1678 slab_irq_disable(this_cpu
);
1679 BUG_ON(cpu_cache_get(malloc_sizes
[INDEX_AC
].cs_cachep
, this_cpu
)
1680 != &initarray_generic
.cache
);
1681 memcpy(ptr
, cpu_cache_get(malloc_sizes
[INDEX_AC
].cs_cachep
, this_cpu
),
1682 sizeof(struct arraycache_init
));
1684 * Do not assume that spinlocks can be initialized via memcpy:
1686 spin_lock_init(&ptr
->lock
);
1687 malloc_sizes
[INDEX_AC
].cs_cachep
->array
[this_cpu
] = ptr
;
1688 slab_irq_enable(this_cpu
);
1690 /* 5) Replace the bootstrap kmem_list3's */
1694 for_each_online_node(nid
) {
1695 init_list(&cache_cache
, &initkmem_list3
[CACHE_CACHE
+ nid
], nid
);
1697 init_list(malloc_sizes
[INDEX_AC
].cs_cachep
,
1698 &initkmem_list3
[SIZE_AC
+ nid
], nid
);
1700 if (INDEX_AC
!= INDEX_L3
) {
1701 init_list(malloc_sizes
[INDEX_L3
].cs_cachep
,
1702 &initkmem_list3
[SIZE_L3
+ nid
], nid
);
1707 /* 6) resize the head arrays to their final sizes */
1709 struct kmem_cache
*cachep
;
1710 mutex_lock(&cache_chain_mutex
);
1711 list_for_each_entry(cachep
, &cache_chain
, next
)
1712 if (enable_cpucache(cachep
))
1714 mutex_unlock(&cache_chain_mutex
);
1717 /* Annotate slab for lockdep -- annotate the malloc caches */
1722 g_cpucache_up
= FULL
;
1725 * Register a cpu startup notifier callback that initializes
1726 * cpu_cache_get for all new cpus
1728 register_cpu_notifier(&cpucache_notifier
);
1731 * The reap timers are started later, with a module init call: That part
1732 * of the kernel is not yet operational.
1736 static int __init
cpucache_init(void)
1741 * Register the timers that return unneeded pages to the page allocator
1743 for_each_online_cpu(cpu
)
1744 start_cpu_timer(cpu
);
1747 __initcall(cpucache_init
);
1750 * Interface to system's page allocator. No need to hold the cache-lock.
1752 * If we requested dmaable memory, we will get it. Even if we
1753 * did not request dmaable memory, we might get it, but that
1754 * would be relatively rare and ignorable.
1756 static void *kmem_getpages(struct kmem_cache
*cachep
, gfp_t flags
, int nodeid
)
1764 * Nommu uses slab's for process anonymous memory allocations, and thus
1765 * requires __GFP_COMP to properly refcount higher order allocations
1767 flags
|= __GFP_COMP
;
1770 flags
|= cachep
->gfpflags
;
1771 if (cachep
->flags
& SLAB_RECLAIM_ACCOUNT
)
1772 flags
|= __GFP_RECLAIMABLE
;
1774 page
= alloc_pages_node(nodeid
, flags
& ~__GFP_NOTRACK
, cachep
->gfporder
);
1778 nr_pages
= (1 << cachep
->gfporder
);
1779 if (cachep
->flags
& SLAB_RECLAIM_ACCOUNT
)
1780 add_zone_page_state(page_zone(page
),
1781 NR_SLAB_RECLAIMABLE
, nr_pages
);
1783 add_zone_page_state(page_zone(page
),
1784 NR_SLAB_UNRECLAIMABLE
, nr_pages
);
1785 for (i
= 0; i
< nr_pages
; i
++)
1786 __SetPageSlab(page
+ i
);
1788 if (kmemcheck_enabled
&& !(cachep
->flags
& SLAB_NOTRACK
)) {
1789 kmemcheck_alloc_shadow(page
, cachep
->gfporder
, flags
, nodeid
);
1792 kmemcheck_mark_uninitialized_pages(page
, nr_pages
);
1794 kmemcheck_mark_unallocated_pages(page
, nr_pages
);
1797 return page_address(page
);
1801 * Interface to system's page release.
1803 static void kmem_freepages(struct kmem_cache
*cachep
, void *addr
, int cpu
)
1805 unsigned long i
= (1 << cachep
->gfporder
);
1806 struct page
*page
, *basepage
= virt_to_page(addr
);
1807 const unsigned long nr_freed
= i
;
1811 kmemcheck_free_shadow(page
, cachep
->gfporder
);
1813 if (cachep
->flags
& SLAB_RECLAIM_ACCOUNT
)
1814 sub_zone_page_state(page_zone(page
),
1815 NR_SLAB_RECLAIMABLE
, nr_freed
);
1817 sub_zone_page_state(page_zone(page
),
1818 NR_SLAB_UNRECLAIMABLE
, nr_freed
);
1821 BUG_ON(!PageSlab(page
));
1822 __ClearPageSlab(page
);
1825 if (current
->reclaim_state
)
1826 current
->reclaim_state
->reclaimed_slab
+= nr_freed
;
1828 #ifdef CONFIG_PREEMPT_RT
1830 basepage
->index
= cachep
->gfporder
;
1831 list_add(&basepage
->lru
, &__get_cpu_var_locked(slab
, cpu
));
1834 free_pages((unsigned long)addr
, cachep
->gfporder
);
1837 static void kmem_rcu_free(struct rcu_head
*head
)
1839 struct slab_rcu
*slab_rcu
= (struct slab_rcu
*)head
;
1840 struct kmem_cache
*cachep
= slab_rcu
->cachep
;
1842 kmem_freepages(cachep
, slab_rcu
->addr
, -1);
1843 if (OFF_SLAB(cachep
))
1844 kmem_cache_free(cachep
->slabp_cache
, slab_rcu
);
1849 #ifdef CONFIG_DEBUG_PAGEALLOC
1850 static void store_stackinfo(struct kmem_cache
*cachep
, unsigned long *addr
,
1851 unsigned long caller
)
1853 int size
= obj_size(cachep
);
1855 addr
= (unsigned long *)&((char *)addr
)[obj_offset(cachep
)];
1857 if (size
< 5 * sizeof(unsigned long))
1860 *addr
++ = 0x12345678;
1862 *addr
++ = raw_smp_processor_id();
1863 size
-= 3 * sizeof(unsigned long);
1865 unsigned long *sptr
= &caller
;
1866 unsigned long svalue
;
1868 while (!kstack_end(sptr
)) {
1870 if (kernel_text_address(svalue
)) {
1872 size
-= sizeof(unsigned long);
1873 if (size
<= sizeof(unsigned long))
1879 *addr
++ = 0x87654321;
1883 static void poison_obj(struct kmem_cache
*cachep
, void *addr
, unsigned char val
)
1885 int size
= obj_size(cachep
);
1886 addr
= &((char *)addr
)[obj_offset(cachep
)];
1888 memset(addr
, val
, size
);
1889 *(unsigned char *)(addr
+ size
- 1) = POISON_END
;
1892 static void dump_line(char *data
, int offset
, int limit
)
1895 unsigned char error
= 0;
1898 printk(KERN_ERR
"%03x:", offset
);
1899 for (i
= 0; i
< limit
; i
++) {
1900 if (data
[offset
+ i
] != POISON_FREE
) {
1901 error
= data
[offset
+ i
];
1904 printk(" %02x", (unsigned char)data
[offset
+ i
]);
1908 if (bad_count
== 1) {
1909 error
^= POISON_FREE
;
1910 if (!(error
& (error
- 1))) {
1911 printk(KERN_ERR
"Single bit error detected. Probably "
1914 printk(KERN_ERR
"Run memtest86+ or a similar memory "
1917 printk(KERN_ERR
"Run a memory test tool.\n");
1926 static void print_objinfo(struct kmem_cache
*cachep
, void *objp
, int lines
)
1931 if (cachep
->flags
& SLAB_RED_ZONE
) {
1932 printk(KERN_ERR
"Redzone: 0x%llx/0x%llx.\n",
1933 *dbg_redzone1(cachep
, objp
),
1934 *dbg_redzone2(cachep
, objp
));
1937 if (cachep
->flags
& SLAB_STORE_USER
) {
1938 printk(KERN_ERR
"Last user: [<%p>]",
1939 *dbg_userword(cachep
, objp
));
1940 print_symbol("(%s)",
1941 (unsigned long)*dbg_userword(cachep
, objp
));
1944 realobj
= (char *)objp
+ obj_offset(cachep
);
1945 size
= obj_size(cachep
);
1946 for (i
= 0; i
< size
&& lines
; i
+= 16, lines
--) {
1949 if (i
+ limit
> size
)
1951 dump_line(realobj
, i
, limit
);
1955 static void check_poison_obj(struct kmem_cache
*cachep
, void *objp
)
1961 realobj
= (char *)objp
+ obj_offset(cachep
);
1962 size
= obj_size(cachep
);
1964 for (i
= 0; i
< size
; i
++) {
1965 char exp
= POISON_FREE
;
1968 if (realobj
[i
] != exp
) {
1974 "Slab corruption: %s start=%p, len=%d\n",
1975 cachep
->name
, realobj
, size
);
1976 print_objinfo(cachep
, objp
, 0);
1978 /* Hexdump the affected line */
1981 if (i
+ limit
> size
)
1983 dump_line(realobj
, i
, limit
);
1986 /* Limit to 5 lines */
1992 /* Print some data about the neighboring objects, if they
1995 struct slab
*slabp
= virt_to_slab(objp
);
1998 objnr
= obj_to_index(cachep
, slabp
, objp
);
2000 objp
= index_to_obj(cachep
, slabp
, objnr
- 1);
2001 realobj
= (char *)objp
+ obj_offset(cachep
);
2002 printk(KERN_ERR
"Prev obj: start=%p, len=%d\n",
2004 print_objinfo(cachep
, objp
, 2);
2006 if (objnr
+ 1 < cachep
->num
) {
2007 objp
= index_to_obj(cachep
, slabp
, objnr
+ 1);
2008 realobj
= (char *)objp
+ obj_offset(cachep
);
2009 printk(KERN_ERR
"Next obj: start=%p, len=%d\n",
2011 print_objinfo(cachep
, objp
, 2);
2018 static void slab_destroy_debugcheck(struct kmem_cache
*cachep
, struct slab
*slabp
)
2021 for (i
= 0; i
< cachep
->num
; i
++) {
2022 void *objp
= index_to_obj(cachep
, slabp
, i
);
2024 if (cachep
->flags
& SLAB_POISON
) {
2025 #ifdef CONFIG_DEBUG_PAGEALLOC
2026 if (cachep
->buffer_size
% PAGE_SIZE
== 0 &&
2028 kernel_map_pages(virt_to_page(objp
),
2029 cachep
->buffer_size
/ PAGE_SIZE
, 1);
2031 check_poison_obj(cachep
, objp
);
2033 check_poison_obj(cachep
, objp
);
2036 if (cachep
->flags
& SLAB_RED_ZONE
) {
2037 if (*dbg_redzone1(cachep
, objp
) != RED_INACTIVE
)
2038 slab_error(cachep
, "start of a freed object "
2040 if (*dbg_redzone2(cachep
, objp
) != RED_INACTIVE
)
2041 slab_error(cachep
, "end of a freed object "
2047 static void slab_destroy_debugcheck(struct kmem_cache
*cachep
, struct slab
*slabp
)
2053 __cache_free(struct kmem_cache
*cachep
, void *objp
, int *this_cpu
);
2057 * slab_destroy - destroy and release all objects in a slab
2058 * @cachep: cache pointer being destroyed
2059 * @slabp: slab pointer being destroyed
2061 * Destroy all the objs in a slab, and release the mem back to the system.
2062 * Before calling the slab must have been unlinked from the cache. The
2063 * cache-lock is not held/needed.
2066 slab_destroy(struct kmem_cache
*cachep
, struct slab
*slabp
, int *this_cpu
)
2068 void *addr
= slabp
->s_mem
- slabp
->colouroff
;
2070 slab_destroy_debugcheck(cachep
, slabp
);
2071 if (unlikely(cachep
->flags
& SLAB_DESTROY_BY_RCU
)) {
2072 struct slab_rcu
*slab_rcu
;
2074 slab_rcu
= (struct slab_rcu
*)slabp
;
2075 slab_rcu
->cachep
= cachep
;
2076 slab_rcu
->addr
= addr
;
2077 call_rcu(&slab_rcu
->head
, kmem_rcu_free
);
2079 kmem_freepages(cachep
, addr
, *this_cpu
);
2080 if (OFF_SLAB(cachep
)) {
2082 __cache_free(cachep
->slabp_cache
, slabp
, this_cpu
);
2084 kmem_cache_free(cachep
->slabp_cache
, slabp
);
2089 static void __kmem_cache_destroy(struct kmem_cache
*cachep
)
2092 struct kmem_list3
*l3
;
2094 for_each_online_cpu(i
)
2095 kfree(cachep
->array
[i
]);
2097 /* NUMA: free the list3 structures */
2098 for_each_online_node(i
) {
2099 l3
= cachep
->nodelists
[i
];
2102 free_alien_cache(l3
->alien
);
2106 kmem_cache_free(&cache_cache
, cachep
);
2111 * calculate_slab_order - calculate size (page order) of slabs
2112 * @cachep: pointer to the cache that is being created
2113 * @size: size of objects to be created in this cache.
2114 * @align: required alignment for the objects.
2115 * @flags: slab allocation flags
2117 * Also calculates the number of objects per slab.
2119 * This could be made much more intelligent. For now, try to avoid using
2120 * high order pages for slabs. When the gfp() functions are more friendly
2121 * towards high-order requests, this should be changed.
2123 static size_t calculate_slab_order(struct kmem_cache
*cachep
,
2124 size_t size
, size_t align
, unsigned long flags
)
2126 unsigned long offslab_limit
;
2127 size_t left_over
= 0;
2130 for (gfporder
= 0; gfporder
<= KMALLOC_MAX_ORDER
; gfporder
++) {
2134 cache_estimate(gfporder
, size
, align
, flags
, &remainder
, &num
);
2138 if (flags
& CFLGS_OFF_SLAB
) {
2140 * Max number of objs-per-slab for caches which
2141 * use off-slab slabs. Needed to avoid a possible
2142 * looping condition in cache_grow().
2144 offslab_limit
= size
- sizeof(struct slab
);
2145 offslab_limit
/= sizeof(kmem_bufctl_t
);
2147 if (num
> offslab_limit
)
2151 /* Found something acceptable - save it away */
2153 cachep
->gfporder
= gfporder
;
2154 left_over
= remainder
;
2157 * A VFS-reclaimable slab tends to have most allocations
2158 * as GFP_NOFS and we really don't want to have to be allocating
2159 * higher-order pages when we are unable to shrink dcache.
2161 if (flags
& SLAB_RECLAIM_ACCOUNT
)
2165 * Large number of objects is good, but very large slabs are
2166 * currently bad for the gfp()s.
2168 if (gfporder
>= slab_break_gfp_order
)
2172 * Acceptable internal fragmentation?
2174 if (left_over
* 8 <= (PAGE_SIZE
<< gfporder
))
2180 static int __init_refok
setup_cpu_cache(struct kmem_cache
*cachep
)
2184 if (g_cpucache_up
== FULL
)
2185 return enable_cpucache(cachep
);
2187 if (g_cpucache_up
== NONE
) {
2189 * Note: the first kmem_cache_create must create the cache
2190 * that's used by kmalloc(24), otherwise the creation of
2191 * further caches will BUG().
2193 cachep
->array
[smp_processor_id()] = &initarray_generic
.cache
;
2196 * If the cache that's used by kmalloc(sizeof(kmem_list3)) is
2197 * the first cache, then we need to set up all its list3s,
2198 * otherwise the creation of further caches will BUG().
2200 set_up_list3s(cachep
, SIZE_AC
);
2201 if (INDEX_AC
== INDEX_L3
)
2202 g_cpucache_up
= PARTIAL_L3
;
2204 g_cpucache_up
= PARTIAL_AC
;
2206 cachep
->array
[smp_processor_id()] =
2207 kmalloc(sizeof(struct arraycache_init
), GFP_KERNEL
);
2209 if (g_cpucache_up
== PARTIAL_AC
) {
2210 set_up_list3s(cachep
, SIZE_L3
);
2211 g_cpucache_up
= PARTIAL_L3
;
2214 for_each_online_node(node
) {
2215 cachep
->nodelists
[node
] =
2216 kmalloc_node(sizeof(struct kmem_list3
),
2218 BUG_ON(!cachep
->nodelists
[node
]);
2219 kmem_list3_init(cachep
->nodelists
[node
]);
2223 cachep
->nodelists
[numa_node_id()]->next_reap
=
2224 jiffies
+ REAPTIMEOUT_LIST3
+
2225 ((unsigned long)cachep
) % REAPTIMEOUT_LIST3
;
2227 this_cpu
= raw_smp_processor_id();
2229 cpu_cache_get(cachep
, this_cpu
)->avail
= 0;
2230 cpu_cache_get(cachep
, this_cpu
)->limit
= BOOT_CPUCACHE_ENTRIES
;
2231 cpu_cache_get(cachep
, this_cpu
)->batchcount
= 1;
2232 cpu_cache_get(cachep
, this_cpu
)->touched
= 0;
2233 cachep
->batchcount
= 1;
2234 cachep
->limit
= BOOT_CPUCACHE_ENTRIES
;
2239 * kmem_cache_create - Create a cache.
2240 * @name: A string which is used in /proc/slabinfo to identify this cache.
2241 * @size: The size of objects to be created in this cache.
2242 * @align: The required alignment for the objects.
2243 * @flags: SLAB flags
2244 * @ctor: A constructor for the objects.
2246 * Returns a ptr to the cache on success, NULL on failure.
2247 * Cannot be called within a int, but can be interrupted.
2248 * The @ctor is run when new pages are allocated by the cache.
2250 * @name must be valid until the cache is destroyed. This implies that
2251 * the module calling this has to destroy the cache before getting unloaded.
2252 * Note that kmem_cache_name() is not guaranteed to return the same pointer,
2253 * therefore applications must manage it themselves.
2257 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2258 * to catch references to uninitialised memory.
2260 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2261 * for buffer overruns.
2263 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2264 * cacheline. This can be beneficial if you're counting cycles as closely
2268 kmem_cache_create (const char *name
, size_t size
, size_t align
,
2269 unsigned long flags
, void (*ctor
)(void *))
2271 size_t left_over
, slab_size
, ralign
;
2272 struct kmem_cache
*cachep
= NULL
, *pc
;
2275 * Sanity checks... these are all serious usage bugs.
2277 if (!name
|| in_interrupt() || (size
< BYTES_PER_WORD
) ||
2278 size
> KMALLOC_MAX_SIZE
) {
2279 printk(KERN_ERR
"%s: Early error in slab %s\n", __func__
,
2285 * We use cache_chain_mutex to ensure a consistent view of
2286 * cpu_online_mask as well. Please see cpuup_callback
2289 mutex_lock(&cache_chain_mutex
);
2291 list_for_each_entry(pc
, &cache_chain
, next
) {
2296 * This happens when the module gets unloaded and doesn't
2297 * destroy its slab cache and no-one else reuses the vmalloc
2298 * area of the module. Print a warning.
2300 res
= probe_kernel_address(pc
->name
, tmp
);
2303 "SLAB: cache with size %d has lost its name\n",
2308 if (!strcmp(pc
->name
, name
)) {
2310 "kmem_cache_create: duplicate cache %s\n", name
);
2317 WARN_ON(strchr(name
, ' ')); /* It confuses parsers */
2320 * Enable redzoning and last user accounting, except for caches with
2321 * large objects, if the increased size would increase the object size
2322 * above the next power of two: caches with object sizes just above a
2323 * power of two have a significant amount of internal fragmentation.
2325 if (size
< 4096 || fls(size
- 1) == fls(size
-1 + REDZONE_ALIGN
+
2326 2 * sizeof(unsigned long long)))
2327 flags
|= SLAB_RED_ZONE
| SLAB_STORE_USER
;
2328 if (!(flags
& SLAB_DESTROY_BY_RCU
))
2329 flags
|= SLAB_POISON
;
2331 if (flags
& SLAB_DESTROY_BY_RCU
)
2332 BUG_ON(flags
& SLAB_POISON
);
2335 * Always checks flags, a caller might be expecting debug support which
2338 BUG_ON(flags
& ~CREATE_MASK
);
2341 * Check that size is in terms of words. This is needed to avoid
2342 * unaligned accesses for some archs when redzoning is used, and makes
2343 * sure any on-slab bufctl's are also correctly aligned.
2345 if (size
& (BYTES_PER_WORD
- 1)) {
2346 size
+= (BYTES_PER_WORD
- 1);
2347 size
&= ~(BYTES_PER_WORD
- 1);
2350 /* calculate the final buffer alignment: */
2352 /* 1) arch recommendation: can be overridden for debug */
2353 if (flags
& SLAB_HWCACHE_ALIGN
) {
2355 * Default alignment: as specified by the arch code. Except if
2356 * an object is really small, then squeeze multiple objects into
2359 ralign
= cache_line_size();
2360 while (size
<= ralign
/ 2)
2363 ralign
= BYTES_PER_WORD
;
2367 * Redzoning and user store require word alignment or possibly larger.
2368 * Note this will be overridden by architecture or caller mandated
2369 * alignment if either is greater than BYTES_PER_WORD.
2371 if (flags
& SLAB_STORE_USER
)
2372 ralign
= BYTES_PER_WORD
;
2374 if (flags
& SLAB_RED_ZONE
) {
2375 ralign
= REDZONE_ALIGN
;
2376 /* If redzoning, ensure that the second redzone is suitably
2377 * aligned, by adjusting the object size accordingly. */
2378 size
+= REDZONE_ALIGN
- 1;
2379 size
&= ~(REDZONE_ALIGN
- 1);
2382 /* 2) arch mandated alignment */
2383 if (ralign
< ARCH_SLAB_MINALIGN
) {
2384 ralign
= ARCH_SLAB_MINALIGN
;
2386 /* 3) caller mandated alignment */
2387 if (ralign
< align
) {
2390 /* disable debug if necessary */
2391 if (ralign
> __alignof__(unsigned long long))
2392 flags
&= ~(SLAB_RED_ZONE
| SLAB_STORE_USER
);
2398 /* Get cache's description obj. */
2399 cachep
= kmem_cache_zalloc(&cache_cache
, GFP_KERNEL
);
2404 cachep
->obj_size
= size
;
2407 * Both debugging options require word-alignment which is calculated
2410 if (flags
& SLAB_RED_ZONE
) {
2411 /* add space for red zone words */
2412 cachep
->obj_offset
+= sizeof(unsigned long long);
2413 size
+= 2 * sizeof(unsigned long long);
2415 if (flags
& SLAB_STORE_USER
) {
2416 /* user store requires one word storage behind the end of
2417 * the real object. But if the second red zone needs to be
2418 * aligned to 64 bits, we must allow that much space.
2420 if (flags
& SLAB_RED_ZONE
)
2421 size
+= REDZONE_ALIGN
;
2423 size
+= BYTES_PER_WORD
;
2425 #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
2426 if (size
>= malloc_sizes
[INDEX_L3
+ 1].cs_size
2427 && cachep
->obj_size
> cache_line_size() && size
< PAGE_SIZE
) {
2428 cachep
->obj_offset
+= PAGE_SIZE
- size
;
2435 * Determine if the slab management is 'on' or 'off' slab.
2436 * (bootstrapping cannot cope with offslab caches so don't do
2439 if ((size
>= (PAGE_SIZE
>> 3)) && !slab_early_init
)
2441 * Size is large, assume best to place the slab management obj
2442 * off-slab (should allow better packing of objs).
2444 flags
|= CFLGS_OFF_SLAB
;
2446 size
= ALIGN(size
, align
);
2448 left_over
= calculate_slab_order(cachep
, size
, align
, flags
);
2452 "kmem_cache_create: couldn't create cache %s.\n", name
);
2453 kmem_cache_free(&cache_cache
, cachep
);
2457 slab_size
= ALIGN(cachep
->num
* sizeof(kmem_bufctl_t
)
2458 + sizeof(struct slab
), align
);
2461 * If the slab has been placed off-slab, and we have enough space then
2462 * move it on-slab. This is at the expense of any extra colouring.
2464 if (flags
& CFLGS_OFF_SLAB
&& left_over
>= slab_size
) {
2465 flags
&= ~CFLGS_OFF_SLAB
;
2466 left_over
-= slab_size
;
2469 if (flags
& CFLGS_OFF_SLAB
) {
2470 /* really off slab. No need for manual alignment */
2472 cachep
->num
* sizeof(kmem_bufctl_t
) + sizeof(struct slab
);
2475 cachep
->colour_off
= cache_line_size();
2476 /* Offset must be a multiple of the alignment. */
2477 if (cachep
->colour_off
< align
)
2478 cachep
->colour_off
= align
;
2479 cachep
->colour
= left_over
/ cachep
->colour_off
;
2480 cachep
->slab_size
= slab_size
;
2481 cachep
->flags
= flags
;
2482 cachep
->gfpflags
= 0;
2483 if (CONFIG_ZONE_DMA_FLAG
&& (flags
& SLAB_CACHE_DMA
))
2484 cachep
->gfpflags
|= GFP_DMA
;
2485 cachep
->buffer_size
= size
;
2486 cachep
->reciprocal_buffer_size
= reciprocal_value(size
);
2488 if (flags
& CFLGS_OFF_SLAB
) {
2489 cachep
->slabp_cache
= kmem_find_general_cachep(slab_size
, 0u);
2491 * This is a possibility for one of the malloc_sizes caches.
2492 * But since we go off slab only for object size greater than
2493 * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
2494 * this should not happen at all.
2495 * But leave a BUG_ON for some lucky dude.
2497 BUG_ON(ZERO_OR_NULL_PTR(cachep
->slabp_cache
));
2499 cachep
->ctor
= ctor
;
2500 cachep
->name
= name
;
2502 if (setup_cpu_cache(cachep
)) {
2503 __kmem_cache_destroy(cachep
);
2508 /* cache setup completed, link it into the list */
2509 list_add(&cachep
->next
, &cache_chain
);
2511 if (!cachep
&& (flags
& SLAB_PANIC
))
2512 panic("kmem_cache_create(): failed to create slab `%s'\n",
2514 mutex_unlock(&cache_chain_mutex
);
2518 EXPORT_SYMBOL(kmem_cache_create
);
2521 static void check_irq_off(void)
2524 * On PREEMPT_RT we use locks to protect the per-CPU lists,
2525 * and keep interrupts enabled.
2527 #ifndef CONFIG_PREEMPT_RT
2528 BUG_ON(!irqs_disabled());
2532 static void check_irq_on(void)
2534 #ifndef CONFIG_PREEMPT_RT
2535 BUG_ON(irqs_disabled());
2539 static void check_spinlock_acquired_node(struct kmem_cache
*cachep
, int node
)
2543 assert_spin_locked(&cachep
->nodelists
[node
]->list_lock
);
2548 #define check_irq_off() do { } while(0)
2549 #define check_irq_on() do { } while(0)
2550 #define check_spinlock_acquired_node(x, y) do { } while(0)
2553 static int drain_array(struct kmem_cache
*cachep
, struct kmem_list3
*l3
,
2554 struct array_cache
*ac
,
2555 int force
, int node
);
2557 static void __do_drain(void *arg
, int this_cpu
)
2559 struct kmem_cache
*cachep
= arg
;
2560 int node
= cpu_to_node(this_cpu
);
2561 struct array_cache
*ac
;
2564 ac
= cpu_cache_get(cachep
, this_cpu
);
2565 spin_lock(&cachep
->nodelists
[node
]->list_lock
);
2566 free_block(cachep
, ac
->entry
, ac
->avail
, node
, &this_cpu
);
2567 spin_unlock(&cachep
->nodelists
[node
]->list_lock
);
2571 #ifdef CONFIG_PREEMPT_RT
2572 static void do_drain(void *arg
, int this_cpu
)
2574 __do_drain(arg
, this_cpu
);
2577 static void do_drain(void *arg
)
2579 __do_drain(arg
, smp_processor_id());
2583 #ifdef CONFIG_PREEMPT_RT
2585 * execute func() for all CPUs. On PREEMPT_RT we dont actually have
2586 * to run on the remote CPUs - we only have to take their CPU-locks.
2587 * (This is a rare operation, so cacheline bouncing is not an issue.)
2590 slab_on_each_cpu(void (*func
)(void *arg
, int this_cpu
), void *arg
)
2595 for_each_online_cpu(i
) {
2596 spin_lock(&__get_cpu_lock(slab
, i
));
2598 spin_unlock(&__get_cpu_lock(slab
, i
));
2602 # define slab_on_each_cpu(func, cachep) on_each_cpu(func, cachep, 1)
2605 static void drain_cpu_caches(struct kmem_cache
*cachep
)
2607 struct kmem_list3
*l3
;
2610 slab_on_each_cpu(do_drain
, cachep
);
2612 for_each_online_node(node
) {
2613 l3
= cachep
->nodelists
[node
];
2614 if (l3
&& l3
->alien
)
2615 drain_alien_cache(cachep
, l3
->alien
);
2618 for_each_online_node(node
) {
2619 l3
= cachep
->nodelists
[node
];
2621 drain_array(cachep
, l3
, l3
->shared
, 1, node
);
2626 * Remove slabs from the list of free slabs.
2627 * Specify the number of slabs to drain in tofree.
2629 * Returns the actual number of slabs released.
2631 static int drain_freelist(struct kmem_cache
*cache
,
2632 struct kmem_list3
*l3
, int tofree
)
2634 struct list_head
*p
;
2635 int nr_freed
, this_cpu
;
2639 while (nr_freed
< tofree
&& !list_empty(&l3
->slabs_free
)) {
2641 slab_spin_lock_irq(&l3
->list_lock
, this_cpu
);
2642 p
= l3
->slabs_free
.prev
;
2643 if (p
== &l3
->slabs_free
) {
2644 slab_spin_unlock_irq(&l3
->list_lock
, this_cpu
);
2648 slabp
= list_entry(p
, struct slab
, list
);
2650 BUG_ON(slabp
->inuse
);
2652 list_del(&slabp
->list
);
2653 l3
->free_objects
-= cache
->num
;
2654 slab_destroy(cache
, slabp
, &this_cpu
);
2655 slab_spin_unlock_irq(&l3
->list_lock
, this_cpu
);
2662 /* Called with cache_chain_mutex held to protect against cpu hotplug */
2663 static int __cache_shrink(struct kmem_cache
*cachep
)
2666 struct kmem_list3
*l3
;
2668 drain_cpu_caches(cachep
);
2671 for_each_online_node(i
) {
2672 l3
= cachep
->nodelists
[i
];
2676 drain_freelist(cachep
, l3
, l3
->free_objects
);
2678 ret
+= !list_empty(&l3
->slabs_full
) ||
2679 !list_empty(&l3
->slabs_partial
);
2681 return (ret
? 1 : 0);
2685 * kmem_cache_shrink - Shrink a cache.
2686 * @cachep: The cache to shrink.
2688 * Releases as many slabs as possible for a cache.
2689 * To help debugging, a zero exit status indicates all slabs were released.
2691 int kmem_cache_shrink(struct kmem_cache
*cachep
)
2694 BUG_ON(!cachep
|| in_interrupt());
2697 mutex_lock(&cache_chain_mutex
);
2698 ret
= __cache_shrink(cachep
);
2699 mutex_unlock(&cache_chain_mutex
);
2703 EXPORT_SYMBOL(kmem_cache_shrink
);
2706 * kmem_cache_destroy - delete a cache
2707 * @cachep: the cache to destroy
2709 * Remove a &struct kmem_cache object from the slab cache.
2711 * It is expected this function will be called by a module when it is
2712 * unloaded. This will remove the cache completely, and avoid a duplicate
2713 * cache being allocated each time a module is loaded and unloaded, if the
2714 * module doesn't have persistent in-kernel storage across loads and unloads.
2716 * The cache must be empty before calling this function.
2718 * The caller must guarantee that noone will allocate memory from the cache
2719 * during the kmem_cache_destroy().
2721 void kmem_cache_destroy(struct kmem_cache
*cachep
)
2723 BUG_ON(!cachep
|| in_interrupt());
2725 /* Find the cache in the chain of caches. */
2727 mutex_lock(&cache_chain_mutex
);
2729 * the chain is never empty, cache_cache is never destroyed
2731 list_del(&cachep
->next
);
2732 if (__cache_shrink(cachep
)) {
2733 slab_error(cachep
, "Can't free all objects");
2734 list_add(&cachep
->next
, &cache_chain
);
2735 mutex_unlock(&cache_chain_mutex
);
2740 if (unlikely(cachep
->flags
& SLAB_DESTROY_BY_RCU
))
2743 __kmem_cache_destroy(cachep
);
2744 mutex_unlock(&cache_chain_mutex
);
2747 EXPORT_SYMBOL(kmem_cache_destroy
);
2750 * Get the memory for a slab management obj.
2751 * For a slab cache when the slab descriptor is off-slab, slab descriptors
2752 * always come from malloc_sizes caches. The slab descriptor cannot
2753 * come from the same cache which is getting created because,
2754 * when we are searching for an appropriate cache for these
2755 * descriptors in kmem_cache_create, we search through the malloc_sizes array.
2756 * If we are creating a malloc_sizes cache here it would not be visible to
2757 * kmem_find_general_cachep till the initialization is complete.
2758 * Hence we cannot have slabp_cache same as the original cache.
2760 static struct slab
*alloc_slabmgmt(struct kmem_cache
*cachep
, void *objp
,
2761 int colour_off
, gfp_t local_flags
,
2766 if (OFF_SLAB(cachep
)) {
2767 /* Slab management obj is off-slab. */
2768 slabp
= kmem_cache_alloc_node(cachep
->slabp_cache
,
2769 local_flags
, nodeid
);
2773 slabp
= objp
+ colour_off
;
2774 colour_off
+= cachep
->slab_size
;
2777 slabp
->colouroff
= colour_off
;
2778 slabp
->s_mem
= objp
+ colour_off
;
2779 slabp
->nodeid
= nodeid
;
2784 static inline kmem_bufctl_t
*slab_bufctl(struct slab
*slabp
)
2786 return (kmem_bufctl_t
*) (slabp
+ 1);
2789 static void cache_init_objs(struct kmem_cache
*cachep
,
2794 for (i
= 0; i
< cachep
->num
; i
++) {
2795 void *objp
= index_to_obj(cachep
, slabp
, i
);
2797 /* need to poison the objs? */
2798 if (cachep
->flags
& SLAB_POISON
)
2799 poison_obj(cachep
, objp
, POISON_FREE
);
2800 if (cachep
->flags
& SLAB_STORE_USER
)
2801 *dbg_userword(cachep
, objp
) = NULL
;
2803 if (cachep
->flags
& SLAB_RED_ZONE
) {
2804 *dbg_redzone1(cachep
, objp
) = RED_INACTIVE
;
2805 *dbg_redzone2(cachep
, objp
) = RED_INACTIVE
;
2808 * Constructors are not allowed to allocate memory from the same
2809 * cache which they are a constructor for. Otherwise, deadlock.
2810 * They must also be threaded.
2812 if (cachep
->ctor
&& !(cachep
->flags
& SLAB_POISON
))
2813 cachep
->ctor(objp
+ obj_offset(cachep
));
2815 if (cachep
->flags
& SLAB_RED_ZONE
) {
2816 if (*dbg_redzone2(cachep
, objp
) != RED_INACTIVE
)
2817 slab_error(cachep
, "constructor overwrote the"
2818 " end of an object");
2819 if (*dbg_redzone1(cachep
, objp
) != RED_INACTIVE
)
2820 slab_error(cachep
, "constructor overwrote the"
2821 " start of an object");
2823 if ((cachep
->buffer_size
% PAGE_SIZE
) == 0 &&
2824 OFF_SLAB(cachep
) && cachep
->flags
& SLAB_POISON
)
2825 kernel_map_pages(virt_to_page(objp
),
2826 cachep
->buffer_size
/ PAGE_SIZE
, 0);
2831 slab_bufctl(slabp
)[i
] = i
+ 1;
2833 slab_bufctl(slabp
)[i
- 1] = BUFCTL_END
;
2836 static void kmem_flagcheck(struct kmem_cache
*cachep
, gfp_t flags
)
2838 if (CONFIG_ZONE_DMA_FLAG
) {
2839 if (flags
& GFP_DMA
)
2840 BUG_ON(!(cachep
->gfpflags
& GFP_DMA
));
2842 BUG_ON(cachep
->gfpflags
& GFP_DMA
);
2846 static void *slab_get_obj(struct kmem_cache
*cachep
, struct slab
*slabp
,
2849 void *objp
= index_to_obj(cachep
, slabp
, slabp
->free
);
2853 next
= slab_bufctl(slabp
)[slabp
->free
];
2855 slab_bufctl(slabp
)[slabp
->free
] = BUFCTL_FREE
;
2856 WARN_ON(slabp
->nodeid
!= nodeid
);
2863 static void slab_put_obj(struct kmem_cache
*cachep
, struct slab
*slabp
,
2864 void *objp
, int nodeid
)
2866 unsigned int objnr
= obj_to_index(cachep
, slabp
, objp
);
2869 /* Verify that the slab belongs to the intended node */
2870 WARN_ON(slabp
->nodeid
!= nodeid
);
2872 if (slab_bufctl(slabp
)[objnr
] + 1 <= SLAB_LIMIT
+ 1) {
2873 printk(KERN_ERR
"slab: double free detected in cache "
2874 "'%s', objp %p\n", cachep
->name
, objp
);
2878 slab_bufctl(slabp
)[objnr
] = slabp
->free
;
2879 slabp
->free
= objnr
;
2884 * Map pages beginning at addr to the given cache and slab. This is required
2885 * for the slab allocator to be able to lookup the cache and slab of a
2886 * virtual address for kfree, ksize, kmem_ptr_validate, and slab debugging.
2888 static void slab_map_pages(struct kmem_cache
*cache
, struct slab
*slab
,
2894 page
= virt_to_page(addr
);
2897 if (likely(!PageCompound(page
)))
2898 nr_pages
<<= cache
->gfporder
;
2901 page_set_cache(page
, cache
);
2902 page_set_slab(page
, slab
);
2904 } while (--nr_pages
);
2908 * Grow (by 1) the number of slabs within a cache. This is called by
2909 * kmem_cache_alloc() when there are no active objs left in a cache.
2911 static int cache_grow(struct kmem_cache
*cachep
, gfp_t flags
, int nodeid
,
2912 void *objp
, int *this_cpu
)
2917 struct kmem_list3
*l3
;
2920 * Be lazy and only check for valid flags here, keeping it out of the
2921 * critical path in kmem_cache_alloc().
2923 BUG_ON(flags
& GFP_SLAB_BUG_MASK
);
2924 local_flags
= flags
& (GFP_CONSTRAINT_MASK
|GFP_RECLAIM_MASK
);
2926 /* Take the l3 list lock to change the colour_next on this node */
2928 l3
= cachep
->nodelists
[nodeid
];
2929 spin_lock(&l3
->list_lock
);
2931 /* Get colour for the slab, and cal the next value. */
2932 offset
= l3
->colour_next
;
2934 if (l3
->colour_next
>= cachep
->colour
)
2935 l3
->colour_next
= 0;
2936 spin_unlock(&l3
->list_lock
);
2938 offset
*= cachep
->colour_off
;
2940 slab_irq_enable_GFP_WAIT(local_flags
, this_cpu
);
2943 * The test for missing atomic flag is performed here, rather than
2944 * the more obvious place, simply to reduce the critical path length
2945 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2946 * will eventually be caught here (where it matters).
2948 kmem_flagcheck(cachep
, flags
);
2951 * Get mem for the objs. Attempt to allocate a physical page from
2955 objp
= kmem_getpages(cachep
, local_flags
, nodeid
);
2959 /* Get slab management. */
2960 slabp
= alloc_slabmgmt(cachep
, objp
, offset
,
2961 local_flags
& ~GFP_CONSTRAINT_MASK
, nodeid
);
2965 slab_map_pages(cachep
, slabp
, objp
);
2967 cache_init_objs(cachep
, slabp
);
2969 slab_irq_disable_GFP_WAIT(local_flags
, this_cpu
);
2972 spin_lock(&l3
->list_lock
);
2974 /* Make slab active. */
2975 list_add_tail(&slabp
->list
, &(l3
->slabs_free
));
2976 STATS_INC_GROWN(cachep
);
2977 l3
->free_objects
+= cachep
->num
;
2978 spin_unlock(&l3
->list_lock
);
2981 kmem_freepages(cachep
, objp
, -1);
2983 slab_irq_disable_GFP_WAIT(local_flags
, this_cpu
);
2990 * Perform extra freeing checks:
2991 * - detect bad pointers.
2992 * - POISON/RED_ZONE checking
2994 static void kfree_debugcheck(const void *objp
)
2996 if (!virt_addr_valid(objp
)) {
2997 printk(KERN_ERR
"kfree_debugcheck: out of range ptr %lxh.\n",
2998 (unsigned long)objp
);
3003 static inline void verify_redzone_free(struct kmem_cache
*cache
, void *obj
)
3005 unsigned long long redzone1
, redzone2
;
3007 redzone1
= *dbg_redzone1(cache
, obj
);
3008 redzone2
= *dbg_redzone2(cache
, obj
);
3013 if (redzone1
== RED_ACTIVE
&& redzone2
== RED_ACTIVE
)
3016 if (redzone1
== RED_INACTIVE
&& redzone2
== RED_INACTIVE
)
3017 slab_error(cache
, "double free detected");
3019 slab_error(cache
, "memory outside object was overwritten");
3021 printk(KERN_ERR
"%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
3022 obj
, redzone1
, redzone2
);
3025 static void *cache_free_debugcheck(struct kmem_cache
*cachep
, void *objp
,
3032 BUG_ON(virt_to_cache(objp
) != cachep
);
3034 objp
-= obj_offset(cachep
);
3035 kfree_debugcheck(objp
);
3036 page
= virt_to_head_page(objp
);
3038 slabp
= page_get_slab(page
);
3040 if (cachep
->flags
& SLAB_RED_ZONE
) {
3041 verify_redzone_free(cachep
, objp
);
3042 *dbg_redzone1(cachep
, objp
) = RED_INACTIVE
;
3043 *dbg_redzone2(cachep
, objp
) = RED_INACTIVE
;
3045 if (cachep
->flags
& SLAB_STORE_USER
)
3046 *dbg_userword(cachep
, objp
) = caller
;
3048 objnr
= obj_to_index(cachep
, slabp
, objp
);
3050 BUG_ON(objnr
>= cachep
->num
);
3051 BUG_ON(objp
!= index_to_obj(cachep
, slabp
, objnr
));
3053 #ifdef CONFIG_DEBUG_SLAB_LEAK
3054 slab_bufctl(slabp
)[objnr
] = BUFCTL_FREE
;
3056 if (cachep
->flags
& SLAB_POISON
) {
3057 #ifdef CONFIG_DEBUG_PAGEALLOC
3058 if ((cachep
->buffer_size
% PAGE_SIZE
)==0 && OFF_SLAB(cachep
)) {
3059 store_stackinfo(cachep
, objp
, (unsigned long)caller
);
3060 kernel_map_pages(virt_to_page(objp
),
3061 cachep
->buffer_size
/ PAGE_SIZE
, 0);
3063 poison_obj(cachep
, objp
, POISON_FREE
);
3066 poison_obj(cachep
, objp
, POISON_FREE
);
3072 static void check_slabp(struct kmem_cache
*cachep
, struct slab
*slabp
)
3077 /* Check slab's freelist to see if this obj is there. */
3078 for (i
= slabp
->free
; i
!= BUFCTL_END
; i
= slab_bufctl(slabp
)[i
]) {
3080 if (entries
> cachep
->num
|| i
>= cachep
->num
)
3083 if (entries
!= cachep
->num
- slabp
->inuse
) {
3085 printk(KERN_ERR
"slab: Internal list corruption detected in "
3086 "cache '%s'(%d), slabp %p(%d). Hexdump:\n",
3087 cachep
->name
, cachep
->num
, slabp
, slabp
->inuse
);
3089 i
< sizeof(*slabp
) + cachep
->num
* sizeof(kmem_bufctl_t
);
3092 printk("\n%03x:", i
);
3093 printk(" %02x", ((unsigned char *)slabp
)[i
]);
3100 #define kfree_debugcheck(x) do { } while(0)
3101 #define cache_free_debugcheck(x,objp,z) (objp)
3102 #define check_slabp(x,y) do { } while(0)
3106 cache_alloc_refill(struct kmem_cache
*cachep
, gfp_t flags
, int *this_cpu
)
3109 struct kmem_list3
*l3
;
3110 struct array_cache
*ac
;
3115 node
= numa_node_id();
3116 ac
= cpu_cache_get(cachep
, *this_cpu
);
3117 batchcount
= ac
->batchcount
;
3118 if (!ac
->touched
&& batchcount
> BATCHREFILL_LIMIT
) {
3120 * If there was little recent activity on this cache, then
3121 * perform only a partial refill. Otherwise we could generate
3124 batchcount
= BATCHREFILL_LIMIT
;
3126 l3
= cachep
->nodelists
[cpu_to_node(*this_cpu
)];
3128 BUG_ON(ac
->avail
> 0 || !l3
);
3129 spin_lock(&l3
->list_lock
);
3131 /* See if we can refill from the shared array */
3132 if (l3
->shared
&& transfer_objects(ac
, l3
->shared
, batchcount
))
3135 while (batchcount
> 0) {
3136 struct list_head
*entry
;
3138 /* Get slab alloc is to come from. */
3139 entry
= l3
->slabs_partial
.next
;
3140 if (entry
== &l3
->slabs_partial
) {
3141 l3
->free_touched
= 1;
3142 entry
= l3
->slabs_free
.next
;
3143 if (entry
== &l3
->slabs_free
)
3147 slabp
= list_entry(entry
, struct slab
, list
);
3148 check_slabp(cachep
, slabp
);
3149 check_spinlock_acquired_node(cachep
, cpu_to_node(*this_cpu
));
3152 * The slab was either on partial or free list so
3153 * there must be at least one object available for
3156 BUG_ON(slabp
->inuse
>= cachep
->num
);
3158 while (slabp
->inuse
< cachep
->num
&& batchcount
--) {
3159 STATS_INC_ALLOCED(cachep
);
3160 STATS_INC_ACTIVE(cachep
);
3161 STATS_SET_HIGH(cachep
);
3163 ac
->entry
[ac
->avail
++] =
3164 slab_get_obj(cachep
, slabp
,
3165 cpu_to_node(*this_cpu
));
3167 check_slabp(cachep
, slabp
);
3169 /* move slabp to correct slabp list: */
3170 list_del(&slabp
->list
);
3171 if (slabp
->free
== BUFCTL_END
)
3172 list_add(&slabp
->list
, &l3
->slabs_full
);
3174 list_add(&slabp
->list
, &l3
->slabs_partial
);
3178 l3
->free_objects
-= ac
->avail
;
3180 spin_unlock(&l3
->list_lock
);
3182 if (unlikely(!ac
->avail
)) {
3184 x
= cache_grow(cachep
, flags
| GFP_THISNODE
, cpu_to_node(*this_cpu
), NULL
, this_cpu
);
3186 /* cache_grow can reenable interrupts, then ac could change. */
3187 ac
= cpu_cache_get(cachep
, *this_cpu
);
3188 if (!x
&& ac
->avail
== 0) /* no objects in sight? abort */
3191 if (!ac
->avail
) /* objects refilled by interrupt? */
3195 return ac
->entry
[--ac
->avail
];
3198 static inline void cache_alloc_debugcheck_before(struct kmem_cache
*cachep
,
3201 might_sleep_if(flags
& __GFP_WAIT
);
3203 kmem_flagcheck(cachep
, flags
);
3208 static void *cache_alloc_debugcheck_after(struct kmem_cache
*cachep
,
3209 gfp_t flags
, void *objp
, void *caller
)
3213 if (cachep
->flags
& SLAB_POISON
) {
3214 #ifdef CONFIG_DEBUG_PAGEALLOC
3215 if ((cachep
->buffer_size
% PAGE_SIZE
) == 0 && OFF_SLAB(cachep
))
3216 kernel_map_pages(virt_to_page(objp
),
3217 cachep
->buffer_size
/ PAGE_SIZE
, 1);
3219 check_poison_obj(cachep
, objp
);
3221 check_poison_obj(cachep
, objp
);
3223 poison_obj(cachep
, objp
, POISON_INUSE
);
3225 if (cachep
->flags
& SLAB_STORE_USER
)
3226 *dbg_userword(cachep
, objp
) = caller
;
3228 if (cachep
->flags
& SLAB_RED_ZONE
) {
3229 if (*dbg_redzone1(cachep
, objp
) != RED_INACTIVE
||
3230 *dbg_redzone2(cachep
, objp
) != RED_INACTIVE
) {
3231 slab_error(cachep
, "double free, or memory outside"
3232 " object was overwritten");
3234 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
3235 objp
, *dbg_redzone1(cachep
, objp
),
3236 *dbg_redzone2(cachep
, objp
));
3238 *dbg_redzone1(cachep
, objp
) = RED_ACTIVE
;
3239 *dbg_redzone2(cachep
, objp
) = RED_ACTIVE
;
3241 #ifdef CONFIG_DEBUG_SLAB_LEAK
3246 slabp
= page_get_slab(virt_to_head_page(objp
));
3247 objnr
= (unsigned)(objp
- slabp
->s_mem
) / cachep
->buffer_size
;
3248 slab_bufctl(slabp
)[objnr
] = BUFCTL_ACTIVE
;
3251 objp
+= obj_offset(cachep
);
3252 if (cachep
->ctor
&& cachep
->flags
& SLAB_POISON
)
3254 #if ARCH_SLAB_MINALIGN
3255 if ((u32
)objp
& (ARCH_SLAB_MINALIGN
-1)) {
3256 printk(KERN_ERR
"0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
3257 objp
, ARCH_SLAB_MINALIGN
);
3263 #define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
3266 static bool slab_should_failslab(struct kmem_cache
*cachep
, gfp_t flags
)
3268 if (cachep
== &cache_cache
)
3271 return should_failslab(obj_size(cachep
), flags
);
3274 static inline void *
3275 ____cache_alloc(struct kmem_cache
*cachep
, gfp_t flags
, int *this_cpu
)
3278 struct array_cache
*ac
;
3282 ac
= cpu_cache_get(cachep
, *this_cpu
);
3283 if (likely(ac
->avail
)) {
3284 STATS_INC_ALLOCHIT(cachep
);
3286 objp
= ac
->entry
[--ac
->avail
];
3288 STATS_INC_ALLOCMISS(cachep
);
3289 objp
= cache_alloc_refill(cachep
, flags
, this_cpu
);
3296 * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
3298 * If we are in_interrupt, then process context, including cpusets and
3299 * mempolicy, may not apply and should not be used for allocation policy.
3301 static void *alternate_node_alloc(struct kmem_cache
*cachep
, gfp_t flags
,
3304 int nid_alloc
, nid_here
;
3306 if (in_interrupt() || (flags
& __GFP_THISNODE
))
3308 nid_alloc
= nid_here
= numa_node_id();
3309 if (cpuset_do_slab_mem_spread() && (cachep
->flags
& SLAB_MEM_SPREAD
))
3310 nid_alloc
= cpuset_mem_spread_node();
3311 else if (current
->mempolicy
)
3312 nid_alloc
= slab_node(current
->mempolicy
);
3313 if (nid_alloc
!= nid_here
)
3314 return ____cache_alloc_node(cachep
, flags
, nid_alloc
, this_cpu
);
3319 * Fallback function if there was no memory available and no objects on a
3320 * certain node and fall back is permitted. First we scan all the
3321 * available nodelists for available objects. If that fails then we
3322 * perform an allocation without specifying a node. This allows the page
3323 * allocator to do its reclaim / fallback magic. We then insert the
3324 * slab into the proper nodelist and then allocate from it.
3326 static void *fallback_alloc(struct kmem_cache
*cache
, gfp_t flags
, int *this_cpu
)
3328 struct zonelist
*zonelist
;
3332 enum zone_type high_zoneidx
= gfp_zone(flags
);
3336 if (flags
& __GFP_THISNODE
)
3339 zonelist
= node_zonelist(slab_node(current
->mempolicy
), flags
);
3340 local_flags
= flags
& (GFP_CONSTRAINT_MASK
|GFP_RECLAIM_MASK
);
3344 * Look through allowed nodes for objects available
3345 * from existing per node queues.
3347 for_each_zone_zonelist(zone
, z
, zonelist
, high_zoneidx
) {
3348 nid
= zone_to_nid(zone
);
3350 if (cpuset_zone_allowed_hardwall(zone
, flags
) &&
3351 cache
->nodelists
[nid
] &&
3352 cache
->nodelists
[nid
]->free_objects
) {
3353 obj
= ____cache_alloc_node(cache
,
3354 flags
| GFP_THISNODE
, nid
,
3363 * This allocation will be performed within the constraints
3364 * of the current cpuset / memory policy requirements.
3365 * We may trigger various forms of reclaim on the allowed
3366 * set and go into memory reserves if necessary.
3368 slab_irq_enable_GFP_WAIT(local_flags
, this_cpu
);
3370 kmem_flagcheck(cache
, flags
);
3371 obj
= kmem_getpages(cache
, local_flags
, -1);
3373 slab_irq_disable_GFP_WAIT(local_flags
, this_cpu
);
3377 * Insert into the appropriate per node queues
3379 nid
= page_to_nid(virt_to_page(obj
));
3380 if (cache_grow(cache
, flags
, nid
, obj
, this_cpu
)) {
3381 obj
= ____cache_alloc_node(cache
,
3382 flags
| GFP_THISNODE
, nid
, this_cpu
);
3385 * Another processor may allocate the
3386 * objects in the slab since we are
3387 * not holding any locks.
3391 /* cache_grow already freed obj */
3400 * A interface to enable slab creation on nodeid
3402 static void *____cache_alloc_node(struct kmem_cache
*cachep
, gfp_t flags
,
3403 int nodeid
, int *this_cpu
)
3405 struct list_head
*entry
;
3407 struct kmem_list3
*l3
;
3411 l3
= cachep
->nodelists
[nodeid
];
3416 spin_lock(&l3
->list_lock
);
3417 entry
= l3
->slabs_partial
.next
;
3418 if (entry
== &l3
->slabs_partial
) {
3419 l3
->free_touched
= 1;
3420 entry
= l3
->slabs_free
.next
;
3421 if (entry
== &l3
->slabs_free
)
3425 slabp
= list_entry(entry
, struct slab
, list
);
3426 check_spinlock_acquired_node(cachep
, nodeid
);
3427 check_slabp(cachep
, slabp
);
3429 STATS_INC_NODEALLOCS(cachep
);
3430 STATS_INC_ACTIVE(cachep
);
3431 STATS_SET_HIGH(cachep
);
3433 BUG_ON(slabp
->inuse
== cachep
->num
);
3435 obj
= slab_get_obj(cachep
, slabp
, nodeid
);
3436 check_slabp(cachep
, slabp
);
3438 /* move slabp to correct slabp list: */
3439 list_del(&slabp
->list
);
3441 if (slabp
->free
== BUFCTL_END
)
3442 list_add(&slabp
->list
, &l3
->slabs_full
);
3444 list_add(&slabp
->list
, &l3
->slabs_partial
);
3446 spin_unlock(&l3
->list_lock
);
3450 spin_unlock(&l3
->list_lock
);
3451 x
= cache_grow(cachep
, flags
| GFP_THISNODE
, nodeid
, NULL
, this_cpu
);
3455 return fallback_alloc(cachep
, flags
, this_cpu
);
3462 * kmem_cache_alloc_node - Allocate an object on the specified node
3463 * @cachep: The cache to allocate from.
3464 * @flags: See kmalloc().
3465 * @nodeid: node number of the target node.
3466 * @caller: return address of caller, used for debug information
3468 * Identical to kmem_cache_alloc but it will allocate memory on the given
3469 * node, which can improve the performance for cpu bound structures.
3471 * Fallback to other node is possible if __GFP_THISNODE is not set.
3473 static __always_inline
void *
3474 __cache_alloc_node(struct kmem_cache
*cachep
, gfp_t flags
, int nodeid
,
3477 unsigned long save_flags
;
3481 lockdep_trace_alloc(flags
);
3483 if (slab_should_failslab(cachep
, flags
))
3486 cache_alloc_debugcheck_before(cachep
, flags
);
3488 slab_irq_save(save_flags
, this_cpu
);
3490 if (unlikely(nodeid
== -1))
3491 nodeid
= cpu_to_node(this_cpu
);
3493 if (unlikely(!cachep
->nodelists
[nodeid
])) {
3494 /* Node not bootstrapped yet */
3495 ptr
= fallback_alloc(cachep
, flags
, &this_cpu
);
3499 if (nodeid
== cpu_to_node(this_cpu
)) {
3501 * Use the locally cached objects if possible.
3502 * However ____cache_alloc does not allow fallback
3503 * to other nodes. It may fail while we still have
3504 * objects on other nodes available.
3506 ptr
= ____cache_alloc(cachep
, flags
, &this_cpu
);
3510 /* ___cache_alloc_node can fall back to other nodes */
3511 ptr
= ____cache_alloc_node(cachep
, flags
, nodeid
, &this_cpu
);
3513 slab_irq_restore(save_flags
, this_cpu
);
3514 ptr
= cache_alloc_debugcheck_after(cachep
, flags
, ptr
, caller
);
3517 kmemcheck_slab_alloc(cachep
, flags
, ptr
, obj_size(cachep
));
3519 if (unlikely((flags
& __GFP_ZERO
) && ptr
))
3520 memset(ptr
, 0, obj_size(cachep
));
3525 static __always_inline
void *
3526 __do_cache_alloc(struct kmem_cache
*cache
, gfp_t flags
, int *this_cpu
)
3530 if (unlikely(current
->flags
& (PF_SPREAD_SLAB
| PF_MEMPOLICY
))) {
3531 objp
= alternate_node_alloc(cache
, flags
, this_cpu
);
3536 objp
= ____cache_alloc(cache
, flags
, this_cpu
);
3538 * We may just have run out of memory on the local node.
3539 * ____cache_alloc_node() knows how to locate memory on other nodes
3542 objp
= ____cache_alloc_node(cache
, flags
,
3543 cpu_to_node(*this_cpu
), this_cpu
);
3549 static __always_inline
void *
3550 __do_cache_alloc(struct kmem_cache
*cachep
, gfp_t flags
, int *this_cpu
)
3552 return ____cache_alloc(cachep
, flags
, this_cpu
);
3555 #endif /* CONFIG_NUMA */
3557 static __always_inline
void *
3558 __cache_alloc(struct kmem_cache
*cachep
, gfp_t flags
, void *caller
)
3560 unsigned long save_flags
;
3564 lockdep_trace_alloc(flags
);
3566 if (slab_should_failslab(cachep
, flags
))
3569 cache_alloc_debugcheck_before(cachep
, flags
);
3570 slab_irq_save(save_flags
, this_cpu
);
3571 objp
= __do_cache_alloc(cachep
, flags
, &this_cpu
);
3572 slab_irq_restore(save_flags
, this_cpu
);
3573 objp
= cache_alloc_debugcheck_after(cachep
, flags
, objp
, caller
);
3577 kmemcheck_slab_alloc(cachep
, flags
, objp
, obj_size(cachep
));
3579 if (unlikely((flags
& __GFP_ZERO
) && objp
))
3580 memset(objp
, 0, obj_size(cachep
));
3586 * Caller needs to acquire correct kmem_list's list_lock
3588 static void free_block(struct kmem_cache
*cachep
, void **objpp
, int nr_objects
,
3589 int node
, int *this_cpu
)
3592 struct kmem_list3
*l3
;
3594 for (i
= 0; i
< nr_objects
; i
++) {
3595 void *objp
= objpp
[i
];
3598 slabp
= virt_to_slab(objp
);
3599 l3
= cachep
->nodelists
[node
];
3600 list_del(&slabp
->list
);
3601 check_spinlock_acquired_node(cachep
, node
);
3602 check_slabp(cachep
, slabp
);
3603 slab_put_obj(cachep
, slabp
, objp
, node
);
3604 STATS_DEC_ACTIVE(cachep
);
3606 check_slabp(cachep
, slabp
);
3608 /* fixup slab chains */
3609 if (slabp
->inuse
== 0) {
3610 if (l3
->free_objects
> l3
->free_limit
) {
3611 l3
->free_objects
-= cachep
->num
;
3612 /* No need to drop any previously held
3613 * lock here, even if we have a off-slab slab
3614 * descriptor it is guaranteed to come from
3615 * a different cache, refer to comments before
3618 slab_destroy(cachep
, slabp
, this_cpu
);
3620 list_add(&slabp
->list
, &l3
->slabs_free
);
3623 /* Unconditionally move a slab to the end of the
3624 * partial list on free - maximum time for the
3625 * other objects to be freed, too.
3627 list_add_tail(&slabp
->list
, &l3
->slabs_partial
);
3633 cache_flusharray(struct kmem_cache
*cachep
, struct array_cache
*ac
, int *this_cpu
)
3636 struct kmem_list3
*l3
;
3637 int node
= cpu_to_node(*this_cpu
);
3639 batchcount
= ac
->batchcount
;
3641 BUG_ON(!batchcount
|| batchcount
> ac
->avail
);
3644 l3
= cachep
->nodelists
[node
];
3645 spin_lock(&l3
->list_lock
);
3647 struct array_cache
*shared_array
= l3
->shared
;
3648 int max
= shared_array
->limit
- shared_array
->avail
;
3650 if (batchcount
> max
)
3652 memcpy(&(shared_array
->entry
[shared_array
->avail
]),
3653 ac
->entry
, sizeof(void *) * batchcount
);
3654 shared_array
->avail
+= batchcount
;
3659 free_block(cachep
, ac
->entry
, batchcount
, node
, this_cpu
);
3664 struct list_head
*p
;
3666 p
= l3
->slabs_free
.next
;
3667 while (p
!= &(l3
->slabs_free
)) {
3670 slabp
= list_entry(p
, struct slab
, list
);
3671 BUG_ON(slabp
->inuse
);
3676 STATS_SET_FREEABLE(cachep
, i
);
3679 spin_unlock(&l3
->list_lock
);
3680 ac
->avail
-= batchcount
;
3681 memmove(ac
->entry
, &(ac
->entry
[batchcount
]), sizeof(void *)*ac
->avail
);
3685 * Release an obj back to its cache. If the obj has a constructed state, it must
3686 * be in this state _before_ it is released. Called with disabled ints.
3688 static void __cache_free(struct kmem_cache
*cachep
, void *objp
, int *this_cpu
)
3690 struct array_cache
*ac
= cpu_cache_get(cachep
, *this_cpu
);
3693 objp
= cache_free_debugcheck(cachep
, objp
, __builtin_return_address(0));
3695 kmemcheck_slab_free(cachep
, objp
, obj_size(cachep
));
3698 * Skip calling cache_free_alien() when the platform is not numa.
3699 * This will avoid cache misses that happen while accessing slabp (which
3700 * is per page memory reference) to get nodeid. Instead use a global
3701 * variable to skip the call, which is mostly likely to be present in
3704 if (numa_platform
&& cache_free_alien(cachep
, objp
, this_cpu
))
3707 if (likely(ac
->avail
< ac
->limit
)) {
3708 STATS_INC_FREEHIT(cachep
);
3709 ac
->entry
[ac
->avail
++] = objp
;
3712 STATS_INC_FREEMISS(cachep
);
3713 cache_flusharray(cachep
, ac
, this_cpu
);
3714 ac
->entry
[ac
->avail
++] = objp
;
3719 * kmem_cache_alloc - Allocate an object
3720 * @cachep: The cache to allocate from.
3721 * @flags: See kmalloc().
3723 * Allocate an object from this cache. The flags are only relevant
3724 * if the cache has no available objects.
3726 void *kmem_cache_alloc(struct kmem_cache
*cachep
, gfp_t flags
)
3728 void *ret
= __cache_alloc(cachep
, flags
, __builtin_return_address(0));
3730 trace_kmem_cache_alloc(_RET_IP_
, ret
,
3731 obj_size(cachep
), cachep
->buffer_size
, flags
);
3735 EXPORT_SYMBOL(kmem_cache_alloc
);
3737 #ifdef CONFIG_KMEMTRACE
3738 void *kmem_cache_alloc_notrace(struct kmem_cache
*cachep
, gfp_t flags
)
3740 return __cache_alloc(cachep
, flags
, __builtin_return_address(0));
3742 EXPORT_SYMBOL(kmem_cache_alloc_notrace
);
3746 * kmem_ptr_validate - check if an untrusted pointer might be a slab entry.
3747 * @cachep: the cache we're checking against
3748 * @ptr: pointer to validate
3750 * This verifies that the untrusted pointer looks sane;
3751 * it is _not_ a guarantee that the pointer is actually
3752 * part of the slab cache in question, but it at least
3753 * validates that the pointer can be dereferenced and
3754 * looks half-way sane.
3756 * Currently only used for dentry validation.
3758 int kmem_ptr_validate(struct kmem_cache
*cachep
, const void *ptr
)
3760 unsigned long addr
= (unsigned long)ptr
;
3761 unsigned long min_addr
= PAGE_OFFSET
;
3762 unsigned long align_mask
= BYTES_PER_WORD
- 1;
3763 unsigned long size
= cachep
->buffer_size
;
3766 if (unlikely(addr
< min_addr
))
3768 if (unlikely(addr
> (unsigned long)high_memory
- size
))
3770 if (unlikely(addr
& align_mask
))
3772 if (unlikely(!kern_addr_valid(addr
)))
3774 if (unlikely(!kern_addr_valid(addr
+ size
- 1)))
3776 page
= virt_to_page(ptr
);
3777 if (unlikely(!PageSlab(page
)))
3779 if (unlikely(page_get_cache(page
) != cachep
))
3787 void *kmem_cache_alloc_node(struct kmem_cache
*cachep
, gfp_t flags
, int nodeid
)
3789 void *ret
= __cache_alloc_node(cachep
, flags
, nodeid
,
3790 __builtin_return_address(0));
3792 trace_kmem_cache_alloc_node(_RET_IP_
, ret
,
3793 obj_size(cachep
), cachep
->buffer_size
,
3798 EXPORT_SYMBOL(kmem_cache_alloc_node
);
3800 #ifdef CONFIG_KMEMTRACE
3801 void *kmem_cache_alloc_node_notrace(struct kmem_cache
*cachep
,
3805 return __cache_alloc_node(cachep
, flags
, nodeid
,
3806 __builtin_return_address(0));
3808 EXPORT_SYMBOL(kmem_cache_alloc_node_notrace
);
3811 static __always_inline
void *
3812 __do_kmalloc_node(size_t size
, gfp_t flags
, int node
, void *caller
)
3814 struct kmem_cache
*cachep
;
3817 cachep
= kmem_find_general_cachep(size
, flags
);
3818 if (unlikely(ZERO_OR_NULL_PTR(cachep
)))
3820 ret
= kmem_cache_alloc_node_notrace(cachep
, flags
, node
);
3822 trace_kmalloc_node((unsigned long) caller
, ret
,
3823 size
, cachep
->buffer_size
, flags
, node
);
3828 #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_KMEMTRACE)
3829 void *__kmalloc_node(size_t size
, gfp_t flags
, int node
)
3831 return __do_kmalloc_node(size
, flags
, node
,
3832 __builtin_return_address(0));
3834 EXPORT_SYMBOL(__kmalloc_node
);
3836 void *__kmalloc_node_track_caller(size_t size
, gfp_t flags
,
3837 int node
, unsigned long caller
)
3839 return __do_kmalloc_node(size
, flags
, node
, (void *)caller
);
3841 EXPORT_SYMBOL(__kmalloc_node_track_caller
);
3843 void *__kmalloc_node(size_t size
, gfp_t flags
, int node
)
3845 return __do_kmalloc_node(size
, flags
, node
, NULL
);
3847 EXPORT_SYMBOL(__kmalloc_node
);
3848 #endif /* CONFIG_DEBUG_SLAB */
3849 #endif /* CONFIG_NUMA */
3852 * __do_kmalloc - allocate memory
3853 * @size: how many bytes of memory are required.
3854 * @flags: the type of memory to allocate (see kmalloc).
3855 * @caller: function caller for debug tracking of the caller
3857 static __always_inline
void *__do_kmalloc(size_t size
, gfp_t flags
,
3860 struct kmem_cache
*cachep
;
3863 /* If you want to save a few bytes .text space: replace
3865 * Then kmalloc uses the uninlined functions instead of the inline
3868 cachep
= __find_general_cachep(size
, flags
);
3869 if (unlikely(ZERO_OR_NULL_PTR(cachep
)))
3871 ret
= __cache_alloc(cachep
, flags
, caller
);
3873 trace_kmalloc((unsigned long) caller
, ret
,
3874 size
, cachep
->buffer_size
, flags
);
3880 #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_KMEMTRACE)
3881 void *__kmalloc(size_t size
, gfp_t flags
)
3883 return __do_kmalloc(size
, flags
, __builtin_return_address(0));
3885 EXPORT_SYMBOL(__kmalloc
);
3887 void *__kmalloc_track_caller(size_t size
, gfp_t flags
, unsigned long caller
)
3889 return __do_kmalloc(size
, flags
, (void *)caller
);
3891 EXPORT_SYMBOL(__kmalloc_track_caller
);
3894 void *__kmalloc(size_t size
, gfp_t flags
)
3896 return __do_kmalloc(size
, flags
, NULL
);
3898 EXPORT_SYMBOL(__kmalloc
);
3902 * kmem_cache_free - Deallocate an object
3903 * @cachep: The cache the allocation was from.
3904 * @objp: The previously allocated object.
3906 * Free an object which was previously allocated from this
3909 void kmem_cache_free(struct kmem_cache
*cachep
, void *objp
)
3911 unsigned long flags
;
3914 slab_irq_save(flags
, this_cpu
);
3915 debug_check_no_locks_freed(objp
, obj_size(cachep
));
3916 if (!(cachep
->flags
& SLAB_DEBUG_OBJECTS
))
3917 debug_check_no_obj_freed(objp
, obj_size(cachep
));
3918 __cache_free(cachep
, objp
, &this_cpu
);
3919 slab_irq_restore(flags
, this_cpu
);
3921 trace_kmem_cache_free(_RET_IP_
, objp
);
3923 EXPORT_SYMBOL(kmem_cache_free
);
3926 * kfree - free previously allocated memory
3927 * @objp: pointer returned by kmalloc.
3929 * If @objp is NULL, no operation is performed.
3931 * Don't free memory not originally allocated by kmalloc()
3932 * or you will run into trouble.
3934 void kfree(const void *objp
)
3936 struct kmem_cache
*c
;
3937 unsigned long flags
;
3940 trace_kfree(_RET_IP_
, objp
);
3942 if (unlikely(ZERO_OR_NULL_PTR(objp
)))
3944 slab_irq_save(flags
, this_cpu
);
3945 kfree_debugcheck(objp
);
3946 c
= virt_to_cache(objp
);
3947 debug_check_no_locks_freed(objp
, obj_size(c
));
3948 debug_check_no_obj_freed(objp
, obj_size(c
));
3949 __cache_free(c
, (void *)objp
, &this_cpu
);
3950 slab_irq_restore(flags
, this_cpu
);
3952 EXPORT_SYMBOL(kfree
);
3954 unsigned int kmem_cache_size(struct kmem_cache
*cachep
)
3956 return obj_size(cachep
);
3958 EXPORT_SYMBOL(kmem_cache_size
);
3960 const char *kmem_cache_name(struct kmem_cache
*cachep
)
3962 return cachep
->name
;
3964 EXPORT_SYMBOL_GPL(kmem_cache_name
);
3967 * This initializes kmem_list3 or resizes various caches for all nodes.
3969 static int alloc_kmemlist(struct kmem_cache
*cachep
)
3972 struct kmem_list3
*l3
;
3973 struct array_cache
*new_shared
;
3974 struct array_cache
**new_alien
= NULL
;
3976 for_each_online_node(node
) {
3978 if (use_alien_caches
) {
3979 new_alien
= alloc_alien_cache(node
, cachep
->limit
);
3985 if (cachep
->shared
) {
3986 new_shared
= alloc_arraycache(node
,
3987 cachep
->shared
*cachep
->batchcount
,
3990 free_alien_cache(new_alien
);
3995 l3
= cachep
->nodelists
[node
];
3997 struct array_cache
*shared
= l3
->shared
;
3999 slab_spin_lock_irq(&l3
->list_lock
, this_cpu
);
4002 free_block(cachep
, shared
->entry
,
4003 shared
->avail
, node
, &this_cpu
);
4005 l3
->shared
= new_shared
;
4007 l3
->alien
= new_alien
;
4010 l3
->free_limit
= (1 + nr_cpus_node(node
)) *
4011 cachep
->batchcount
+ cachep
->num
;
4012 slab_spin_unlock_irq(&l3
->list_lock
, this_cpu
);
4014 free_alien_cache(new_alien
);
4017 l3
= kmalloc_node(sizeof(struct kmem_list3
), GFP_KERNEL
, node
);
4019 free_alien_cache(new_alien
);
4024 kmem_list3_init(l3
);
4025 l3
->next_reap
= jiffies
+ REAPTIMEOUT_LIST3
+
4026 ((unsigned long)cachep
) % REAPTIMEOUT_LIST3
;
4027 l3
->shared
= new_shared
;
4028 l3
->alien
= new_alien
;
4029 l3
->free_limit
= (1 + nr_cpus_node(node
)) *
4030 cachep
->batchcount
+ cachep
->num
;
4031 cachep
->nodelists
[node
] = l3
;
4036 if (!cachep
->next
.next
) {
4037 /* Cache is not active yet. Roll back what we did */
4040 if (cachep
->nodelists
[node
]) {
4041 l3
= cachep
->nodelists
[node
];
4044 free_alien_cache(l3
->alien
);
4046 cachep
->nodelists
[node
] = NULL
;
4054 struct ccupdate_struct
{
4055 struct kmem_cache
*cachep
;
4056 struct array_cache
*new[NR_CPUS
];
4059 static void __do_ccupdate_local(void *info
, int this_cpu
)
4061 struct ccupdate_struct
*new = info
;
4062 struct array_cache
*old
;
4065 old
= cpu_cache_get(new->cachep
, this_cpu
);
4067 new->cachep
->array
[this_cpu
] = new->new[this_cpu
];
4068 new->new[this_cpu
] = old
;
4071 #ifdef CONFIG_PREEMPT_RT
4072 static void do_ccupdate_local(void *arg
, int this_cpu
)
4074 __do_ccupdate_local(arg
, this_cpu
);
4077 static void do_ccupdate_local(void *arg
)
4079 __do_ccupdate_local(arg
, smp_processor_id());
4083 /* Always called with the cache_chain_mutex held */
4084 static int do_tune_cpucache(struct kmem_cache
*cachep
, int limit
,
4085 int batchcount
, int shared
)
4087 struct ccupdate_struct
new;
4090 memset(&new.new, 0, sizeof(new.new));
4091 for_each_online_cpu(i
) {
4092 new.new[i
] = alloc_arraycache(cpu_to_node(i
), limit
,
4095 for (i
--; i
>= 0; i
--)
4100 new.cachep
= cachep
;
4102 slab_on_each_cpu(do_ccupdate_local
, (void *)&new);
4105 cachep
->batchcount
= batchcount
;
4106 cachep
->limit
= limit
;
4107 cachep
->shared
= shared
;
4109 for_each_online_cpu(i
) {
4110 struct array_cache
*ccold
= new.new[i
];
4113 slab_spin_lock_irq(&cachep
->nodelists
[cpu_to_node(i
)]->list_lock
, this_cpu
);
4114 free_block(cachep
, ccold
->entry
, ccold
->avail
, cpu_to_node(i
), &this_cpu
);
4115 slab_spin_unlock_irq(&cachep
->nodelists
[cpu_to_node(i
)]->list_lock
, this_cpu
);
4119 return alloc_kmemlist(cachep
);
4122 /* Called with cache_chain_mutex held always */
4123 static int enable_cpucache(struct kmem_cache
*cachep
)
4129 * The head array serves three purposes:
4130 * - create a LIFO ordering, i.e. return objects that are cache-warm
4131 * - reduce the number of spinlock operations.
4132 * - reduce the number of linked list operations on the slab and
4133 * bufctl chains: array operations are cheaper.
4134 * The numbers are guessed, we should auto-tune as described by
4137 if (cachep
->buffer_size
> 131072)
4139 else if (cachep
->buffer_size
> PAGE_SIZE
)
4141 else if (cachep
->buffer_size
> 1024)
4143 else if (cachep
->buffer_size
> 256)
4149 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
4150 * allocation behaviour: Most allocs on one cpu, most free operations
4151 * on another cpu. For these cases, an efficient object passing between
4152 * cpus is necessary. This is provided by a shared array. The array
4153 * replaces Bonwick's magazine layer.
4154 * On uniprocessor, it's functionally equivalent (but less efficient)
4155 * to a larger limit. Thus disabled by default.
4158 if (cachep
->buffer_size
<= PAGE_SIZE
&& num_possible_cpus() > 1)
4163 * With debugging enabled, large batchcount lead to excessively long
4164 * periods with disabled local interrupts. Limit the batchcount
4169 err
= do_tune_cpucache(cachep
, limit
, (limit
+ 1) / 2, shared
);
4171 printk(KERN_ERR
"enable_cpucache failed for %s, error %d.\n",
4172 cachep
->name
, -err
);
4177 * Drain an array if it contains any elements taking the l3 lock only if
4178 * necessary. Note that the l3 listlock also protects the array_cache
4179 * if drain_array() is used on the shared array.
4180 * returns non-zero if some work is done
4182 int drain_array(struct kmem_cache
*cachep
, struct kmem_list3
*l3
,
4183 struct array_cache
*ac
, int force
, int node
)
4185 int tofree
, this_cpu
;
4187 if (!ac
|| !ac
->avail
)
4189 if (ac
->touched
&& !force
) {
4192 slab_spin_lock_irq(&l3
->list_lock
, this_cpu
);
4194 tofree
= force
? ac
->avail
: (ac
->limit
+ 4) / 5;
4195 if (tofree
> ac
->avail
)
4196 tofree
= (ac
->avail
+ 1) / 2;
4197 free_block(cachep
, ac
->entry
, tofree
, node
, &this_cpu
);
4198 ac
->avail
-= tofree
;
4199 memmove(ac
->entry
, &(ac
->entry
[tofree
]),
4200 sizeof(void *) * ac
->avail
);
4202 slab_spin_unlock_irq(&l3
->list_lock
, this_cpu
);
4208 * cache_reap - Reclaim memory from caches.
4209 * @w: work descriptor
4211 * Called from workqueue/eventd every few seconds.
4213 * - clear the per-cpu caches for this CPU.
4214 * - return freeable pages to the main free memory pool.
4216 * If we cannot acquire the cache chain mutex then just give up - we'll try
4217 * again on the next iteration.
4219 static void cache_reap(struct work_struct
*w
)
4221 int this_cpu
= raw_smp_processor_id(), node
= cpu_to_node(this_cpu
);
4222 struct kmem_cache
*searchp
;
4223 struct kmem_list3
*l3
;
4224 struct delayed_work
*work
=
4225 container_of(w
, struct delayed_work
, work
);
4228 if (!mutex_trylock(&cache_chain_mutex
))
4229 /* Give up. Setup the next iteration. */
4232 list_for_each_entry(searchp
, &cache_chain
, next
) {
4236 * We only take the l3 lock if absolutely necessary and we
4237 * have established with reasonable certainty that
4238 * we can do some work if the lock was obtained.
4240 l3
= searchp
->nodelists
[node
];
4242 work_done
+= reap_alien(searchp
, l3
, &this_cpu
);
4244 node
= cpu_to_node(this_cpu
);
4246 work_done
+= drain_array(searchp
, l3
,
4247 cpu_cache_get(searchp
, this_cpu
), 0, node
);
4250 * These are racy checks but it does not matter
4251 * if we skip one check or scan twice.
4253 if (time_after(l3
->next_reap
, jiffies
))
4256 l3
->next_reap
= jiffies
+ REAPTIMEOUT_LIST3
;
4258 work_done
+= drain_array(searchp
, l3
, l3
->shared
, 0, node
);
4260 if (l3
->free_touched
)
4261 l3
->free_touched
= 0;
4265 freed
= drain_freelist(searchp
, l3
, (l3
->free_limit
+
4266 5 * searchp
->num
- 1) / (5 * searchp
->num
));
4267 STATS_ADD_REAPED(searchp
, freed
);
4273 mutex_unlock(&cache_chain_mutex
);
4276 /* Set up the next iteration */
4277 schedule_delayed_work(work
,
4278 round_jiffies_relative((1+!work_done
) * REAPTIMEOUT_CPUC
));
4281 #ifdef CONFIG_SLABINFO
4283 static void print_slabinfo_header(struct seq_file
*m
)
4286 * Output format version, so at least we can change it
4287 * without _too_ many complaints.
4290 seq_puts(m
, "slabinfo - version: 2.1 (statistics)\n");
4292 seq_puts(m
, "slabinfo - version: 2.1\n");
4294 seq_puts(m
, "# name <active_objs> <num_objs> <objsize> "
4295 "<objperslab> <pagesperslab>");
4296 seq_puts(m
, " : tunables <limit> <batchcount> <sharedfactor>");
4297 seq_puts(m
, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
4299 seq_puts(m
, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
4300 "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
4301 seq_puts(m
, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
4306 static void *s_start(struct seq_file
*m
, loff_t
*pos
)
4310 mutex_lock(&cache_chain_mutex
);
4312 print_slabinfo_header(m
);
4314 return seq_list_start(&cache_chain
, *pos
);
4317 static void *s_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
4319 return seq_list_next(p
, &cache_chain
, pos
);
4322 static void s_stop(struct seq_file
*m
, void *p
)
4324 mutex_unlock(&cache_chain_mutex
);
4327 static int s_show(struct seq_file
*m
, void *p
)
4329 struct kmem_cache
*cachep
= list_entry(p
, struct kmem_cache
, next
);
4331 unsigned long active_objs
;
4332 unsigned long num_objs
;
4333 unsigned long active_slabs
= 0;
4334 unsigned long num_slabs
, free_objects
= 0, shared_avail
= 0;
4338 struct kmem_list3
*l3
;
4342 for_each_online_node(node
) {
4343 l3
= cachep
->nodelists
[node
];
4348 slab_spin_lock_irq(&l3
->list_lock
, this_cpu
);
4350 list_for_each_entry(slabp
, &l3
->slabs_full
, list
) {
4351 if (slabp
->inuse
!= cachep
->num
&& !error
)
4352 error
= "slabs_full accounting error";
4353 active_objs
+= cachep
->num
;
4356 list_for_each_entry(slabp
, &l3
->slabs_partial
, list
) {
4357 if (slabp
->inuse
== cachep
->num
&& !error
)
4358 error
= "slabs_partial inuse accounting error";
4359 if (!slabp
->inuse
&& !error
)
4360 error
= "slabs_partial/inuse accounting error";
4361 active_objs
+= slabp
->inuse
;
4364 list_for_each_entry(slabp
, &l3
->slabs_free
, list
) {
4365 if (slabp
->inuse
&& !error
)
4366 error
= "slabs_free/inuse accounting error";
4369 free_objects
+= l3
->free_objects
;
4371 shared_avail
+= l3
->shared
->avail
;
4373 slab_spin_unlock_irq(&l3
->list_lock
, this_cpu
);
4375 num_slabs
+= active_slabs
;
4376 num_objs
= num_slabs
* cachep
->num
;
4377 if (num_objs
- active_objs
!= free_objects
&& !error
)
4378 error
= "free_objects accounting error";
4380 name
= cachep
->name
;
4382 printk(KERN_ERR
"slab: cache %s error: %s\n", name
, error
);
4384 seq_printf(m
, "%-17s %6lu %6lu %6u %4u %4d",
4385 name
, active_objs
, num_objs
, cachep
->buffer_size
,
4386 cachep
->num
, (1 << cachep
->gfporder
));
4387 seq_printf(m
, " : tunables %4u %4u %4u",
4388 cachep
->limit
, cachep
->batchcount
, cachep
->shared
);
4389 seq_printf(m
, " : slabdata %6lu %6lu %6lu",
4390 active_slabs
, num_slabs
, shared_avail
);
4393 unsigned long high
= cachep
->high_mark
;
4394 unsigned long allocs
= cachep
->num_allocations
;
4395 unsigned long grown
= cachep
->grown
;
4396 unsigned long reaped
= cachep
->reaped
;
4397 unsigned long errors
= cachep
->errors
;
4398 unsigned long max_freeable
= cachep
->max_freeable
;
4399 unsigned long node_allocs
= cachep
->node_allocs
;
4400 unsigned long node_frees
= cachep
->node_frees
;
4401 unsigned long overflows
= cachep
->node_overflow
;
4403 seq_printf(m
, " : globalstat %7lu %6lu %5lu %4lu \
4404 %4lu %4lu %4lu %4lu %4lu", allocs
, high
, grown
,
4405 reaped
, errors
, max_freeable
, node_allocs
,
4406 node_frees
, overflows
);
4410 unsigned long allochit
= atomic_read(&cachep
->allochit
);
4411 unsigned long allocmiss
= atomic_read(&cachep
->allocmiss
);
4412 unsigned long freehit
= atomic_read(&cachep
->freehit
);
4413 unsigned long freemiss
= atomic_read(&cachep
->freemiss
);
4415 seq_printf(m
, " : cpustat %6lu %6lu %6lu %6lu",
4416 allochit
, allocmiss
, freehit
, freemiss
);
4424 * slabinfo_op - iterator that generates /proc/slabinfo
4433 * num-pages-per-slab
4434 * + further values on SMP and with statistics enabled
4437 static const struct seq_operations slabinfo_op
= {
4444 #define MAX_SLABINFO_WRITE 128
4446 * slabinfo_write - Tuning for the slab allocator
4448 * @buffer: user buffer
4449 * @count: data length
4452 ssize_t
slabinfo_write(struct file
*file
, const char __user
* buffer
,
4453 size_t count
, loff_t
*ppos
)
4455 char kbuf
[MAX_SLABINFO_WRITE
+ 1], *tmp
;
4456 int limit
, batchcount
, shared
, res
;
4457 struct kmem_cache
*cachep
;
4459 if (count
> MAX_SLABINFO_WRITE
)
4461 if (copy_from_user(&kbuf
, buffer
, count
))
4463 kbuf
[MAX_SLABINFO_WRITE
] = '\0';
4465 tmp
= strchr(kbuf
, ' ');
4470 if (sscanf(tmp
, " %d %d %d", &limit
, &batchcount
, &shared
) != 3)
4473 /* Find the cache in the chain of caches. */
4474 mutex_lock(&cache_chain_mutex
);
4476 list_for_each_entry(cachep
, &cache_chain
, next
) {
4477 if (!strcmp(cachep
->name
, kbuf
)) {
4478 if (limit
< 1 || batchcount
< 1 ||
4479 batchcount
> limit
|| shared
< 0) {
4482 res
= do_tune_cpucache(cachep
, limit
,
4483 batchcount
, shared
);
4488 mutex_unlock(&cache_chain_mutex
);
4494 static int slabinfo_open(struct inode
*inode
, struct file
*file
)
4496 return seq_open(file
, &slabinfo_op
);
4499 static const struct file_operations proc_slabinfo_operations
= {
4500 .open
= slabinfo_open
,
4502 .write
= slabinfo_write
,
4503 .llseek
= seq_lseek
,
4504 .release
= seq_release
,
4507 #ifdef CONFIG_DEBUG_SLAB_LEAK
4509 static void *leaks_start(struct seq_file
*m
, loff_t
*pos
)
4511 mutex_lock(&cache_chain_mutex
);
4512 return seq_list_start(&cache_chain
, *pos
);
4515 static inline int add_caller(unsigned long *n
, unsigned long v
)
4525 unsigned long *q
= p
+ 2 * i
;
4539 memmove(p
+ 2, p
, n
[1] * 2 * sizeof(unsigned long) - ((void *)p
- (void *)n
));
4545 static void handle_slab(unsigned long *n
, struct kmem_cache
*c
, struct slab
*s
)
4551 for (i
= 0, p
= s
->s_mem
; i
< c
->num
; i
++, p
+= c
->buffer_size
) {
4552 if (slab_bufctl(s
)[i
] != BUFCTL_ACTIVE
)
4554 if (!add_caller(n
, (unsigned long)*dbg_userword(c
, p
)))
4559 static void show_symbol(struct seq_file
*m
, unsigned long address
)
4561 #ifdef CONFIG_KALLSYMS
4562 unsigned long offset
, size
;
4563 char modname
[MODULE_NAME_LEN
], name
[KSYM_NAME_LEN
];
4565 if (lookup_symbol_attrs(address
, &size
, &offset
, modname
, name
) == 0) {
4566 seq_printf(m
, "%s+%#lx/%#lx", name
, offset
, size
);
4568 seq_printf(m
, " [%s]", modname
);
4572 seq_printf(m
, "%p", (void *)address
);
4575 static int leaks_show(struct seq_file
*m
, void *p
)
4577 struct kmem_cache
*cachep
= list_entry(p
, struct kmem_cache
, next
);
4579 struct kmem_list3
*l3
;
4581 unsigned long *n
= m
->private;
4585 if (!(cachep
->flags
& SLAB_STORE_USER
))
4587 if (!(cachep
->flags
& SLAB_RED_ZONE
))
4590 /* OK, we can do it */
4594 for_each_online_node(node
) {
4595 l3
= cachep
->nodelists
[node
];
4600 slab_spin_lock_irq(&l3
->list_lock
, this_cpu
);
4602 list_for_each_entry(slabp
, &l3
->slabs_full
, list
)
4603 handle_slab(n
, cachep
, slabp
);
4604 list_for_each_entry(slabp
, &l3
->slabs_partial
, list
)
4605 handle_slab(n
, cachep
, slabp
);
4606 slab_spin_unlock_irq(&l3
->list_lock
, this_cpu
);
4608 name
= cachep
->name
;
4610 /* Increase the buffer size */
4611 mutex_unlock(&cache_chain_mutex
);
4612 m
->private = kzalloc(n
[0] * 4 * sizeof(unsigned long), GFP_KERNEL
);
4614 /* Too bad, we are really out */
4616 mutex_lock(&cache_chain_mutex
);
4619 *(unsigned long *)m
->private = n
[0] * 2;
4621 mutex_lock(&cache_chain_mutex
);
4622 /* Now make sure this entry will be retried */
4626 for (i
= 0; i
< n
[1]; i
++) {
4627 seq_printf(m
, "%s: %lu ", name
, n
[2*i
+3]);
4628 show_symbol(m
, n
[2*i
+2]);
4635 static const struct seq_operations slabstats_op
= {
4636 .start
= leaks_start
,
4642 static int slabstats_open(struct inode
*inode
, struct file
*file
)
4644 unsigned long *n
= kzalloc(PAGE_SIZE
, GFP_KERNEL
);
4647 ret
= seq_open(file
, &slabstats_op
);
4649 struct seq_file
*m
= file
->private_data
;
4650 *n
= PAGE_SIZE
/ (2 * sizeof(unsigned long));
4659 static const struct file_operations proc_slabstats_operations
= {
4660 .open
= slabstats_open
,
4662 .llseek
= seq_lseek
,
4663 .release
= seq_release_private
,
4667 static int __init
slab_proc_init(void)
4669 proc_create("slabinfo",S_IWUSR
|S_IRUGO
,NULL
,&proc_slabinfo_operations
);
4670 #ifdef CONFIG_DEBUG_SLAB_LEAK
4671 proc_create("slab_allocators", 0, NULL
, &proc_slabstats_operations
);
4675 module_init(slab_proc_init
);
4679 * ksize - get the actual amount of memory allocated for a given object
4680 * @objp: Pointer to the object
4682 * kmalloc may internally round up allocations and return more memory
4683 * than requested. ksize() can be used to determine the actual amount of
4684 * memory allocated. The caller may use this additional memory, even though
4685 * a smaller amount of memory was initially specified with the kmalloc call.
4686 * The caller must guarantee that objp points to a valid object previously
4687 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4688 * must not be freed during the duration of the call.
4690 size_t ksize(const void *objp
)
4693 if (unlikely(objp
== ZERO_SIZE_PTR
))
4696 return obj_size(virt_to_cache(objp
));
4698 EXPORT_SYMBOL(ksize
);