1 // SPDX-License-Identifier: GPL-2.0
3 * Slab allocator functions that are independent of the allocator strategy
5 * (C) 2012 Christoph Lameter <cl@linux.com>
7 #include <linux/slab.h>
10 #include <linux/poison.h>
11 #include <linux/interrupt.h>
12 #include <linux/memory.h>
13 #include <linux/cache.h>
14 #include <linux/compiler.h>
15 #include <linux/module.h>
16 #include <linux/cpu.h>
17 #include <linux/uaccess.h>
18 #include <linux/seq_file.h>
19 #include <linux/proc_fs.h>
20 #include <linux/debugfs.h>
21 #include <linux/kasan.h>
22 #include <asm/cacheflush.h>
23 #include <asm/tlbflush.h>
25 #include <linux/memcontrol.h>
27 #define CREATE_TRACE_POINTS
28 #include <trace/events/kmem.h>
34 enum slab_state slab_state
;
35 LIST_HEAD(slab_caches
);
36 DEFINE_MUTEX(slab_mutex
);
37 struct kmem_cache
*kmem_cache
;
39 #ifdef CONFIG_HARDENED_USERCOPY
40 bool usercopy_fallback __ro_after_init
=
41 IS_ENABLED(CONFIG_HARDENED_USERCOPY_FALLBACK
);
42 module_param(usercopy_fallback
, bool, 0400);
43 MODULE_PARM_DESC(usercopy_fallback
,
44 "WARN instead of reject usercopy whitelist violations");
47 static LIST_HEAD(slab_caches_to_rcu_destroy
);
48 static void slab_caches_to_rcu_destroy_workfn(struct work_struct
*work
);
49 static DECLARE_WORK(slab_caches_to_rcu_destroy_work
,
50 slab_caches_to_rcu_destroy_workfn
);
53 * Set of flags that will prevent slab merging
55 #define SLAB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
56 SLAB_TRACE | SLAB_TYPESAFE_BY_RCU | SLAB_NOLEAKTRACE | \
57 SLAB_FAILSLAB | kasan_never_merge())
59 #define SLAB_MERGE_SAME (SLAB_RECLAIM_ACCOUNT | SLAB_CACHE_DMA | \
60 SLAB_CACHE_DMA32 | SLAB_ACCOUNT)
63 * Merge control. If this is set then no merging of slab caches will occur.
65 static bool slab_nomerge
= !IS_ENABLED(CONFIG_SLAB_MERGE_DEFAULT
);
67 static int __init
setup_slab_nomerge(char *str
)
74 __setup_param("slub_nomerge", slub_nomerge
, setup_slab_nomerge
, 0);
77 __setup("slab_nomerge", setup_slab_nomerge
);
80 * Determine the size of a slab object
82 unsigned int kmem_cache_size(struct kmem_cache
*s
)
84 return s
->object_size
;
86 EXPORT_SYMBOL(kmem_cache_size
);
88 #ifdef CONFIG_DEBUG_VM
89 static int kmem_cache_sanity_check(const char *name
, unsigned int size
)
91 if (!name
|| in_interrupt() || size
< sizeof(void *) ||
92 size
> KMALLOC_MAX_SIZE
) {
93 pr_err("kmem_cache_create(%s) integrity check failed\n", name
);
97 WARN_ON(strchr(name
, ' ')); /* It confuses parsers */
101 static inline int kmem_cache_sanity_check(const char *name
, unsigned int size
)
107 void __kmem_cache_free_bulk(struct kmem_cache
*s
, size_t nr
, void **p
)
111 for (i
= 0; i
< nr
; i
++) {
113 kmem_cache_free(s
, p
[i
]);
119 int __kmem_cache_alloc_bulk(struct kmem_cache
*s
, gfp_t flags
, size_t nr
,
124 for (i
= 0; i
< nr
; i
++) {
125 void *x
= p
[i
] = kmem_cache_alloc(s
, flags
);
127 __kmem_cache_free_bulk(s
, i
, p
);
135 * Figure out what the alignment of the objects will be given a set of
136 * flags, a user specified alignment and the size of the objects.
138 static unsigned int calculate_alignment(slab_flags_t flags
,
139 unsigned int align
, unsigned int size
)
142 * If the user wants hardware cache aligned objects then follow that
143 * suggestion if the object is sufficiently large.
145 * The hardware cache alignment cannot override the specified
146 * alignment though. If that is greater then use it.
148 if (flags
& SLAB_HWCACHE_ALIGN
) {
151 ralign
= cache_line_size();
152 while (size
<= ralign
/ 2)
154 align
= max(align
, ralign
);
157 if (align
< ARCH_SLAB_MINALIGN
)
158 align
= ARCH_SLAB_MINALIGN
;
160 return ALIGN(align
, sizeof(void *));
164 * Find a mergeable slab cache
166 int slab_unmergeable(struct kmem_cache
*s
)
168 if (slab_nomerge
|| (s
->flags
& SLAB_NEVER_MERGE
))
178 * We may have set a slab to be unmergeable during bootstrap.
186 struct kmem_cache
*find_mergeable(unsigned int size
, unsigned int align
,
187 slab_flags_t flags
, const char *name
, void (*ctor
)(void *))
189 struct kmem_cache
*s
;
197 size
= ALIGN(size
, sizeof(void *));
198 align
= calculate_alignment(flags
, align
, size
);
199 size
= ALIGN(size
, align
);
200 flags
= kmem_cache_flags(size
, flags
, name
, NULL
);
202 if (flags
& SLAB_NEVER_MERGE
)
205 list_for_each_entry_reverse(s
, &slab_caches
, list
) {
206 if (slab_unmergeable(s
))
212 if ((flags
& SLAB_MERGE_SAME
) != (s
->flags
& SLAB_MERGE_SAME
))
215 * Check if alignment is compatible.
216 * Courtesy of Adrian Drzewiecki
218 if ((s
->size
& ~(align
- 1)) != s
->size
)
221 if (s
->size
- size
>= sizeof(void *))
224 if (IS_ENABLED(CONFIG_SLAB
) && align
&&
225 (align
> s
->align
|| s
->align
% align
))
233 static struct kmem_cache
*create_cache(const char *name
,
234 unsigned int object_size
, unsigned int align
,
235 slab_flags_t flags
, unsigned int useroffset
,
236 unsigned int usersize
, void (*ctor
)(void *),
237 struct kmem_cache
*root_cache
)
239 struct kmem_cache
*s
;
242 if (WARN_ON(useroffset
+ usersize
> object_size
))
243 useroffset
= usersize
= 0;
246 s
= kmem_cache_zalloc(kmem_cache
, GFP_KERNEL
);
251 s
->size
= s
->object_size
= object_size
;
254 s
->useroffset
= useroffset
;
255 s
->usersize
= usersize
;
257 err
= __kmem_cache_create(s
, flags
);
262 list_add(&s
->list
, &slab_caches
);
269 kmem_cache_free(kmem_cache
, s
);
274 * kmem_cache_create_usercopy - Create a cache with a region suitable
275 * for copying to userspace
276 * @name: A string which is used in /proc/slabinfo to identify this cache.
277 * @size: The size of objects to be created in this cache.
278 * @align: The required alignment for the objects.
280 * @useroffset: Usercopy region offset
281 * @usersize: Usercopy region size
282 * @ctor: A constructor for the objects.
284 * Cannot be called within a interrupt, but can be interrupted.
285 * The @ctor is run when new pages are allocated by the cache.
289 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
290 * to catch references to uninitialised memory.
292 * %SLAB_RED_ZONE - Insert `Red` zones around the allocated memory to check
293 * for buffer overruns.
295 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
296 * cacheline. This can be beneficial if you're counting cycles as closely
299 * Return: a pointer to the cache on success, NULL on failure.
302 kmem_cache_create_usercopy(const char *name
,
303 unsigned int size
, unsigned int align
,
305 unsigned int useroffset
, unsigned int usersize
,
306 void (*ctor
)(void *))
308 struct kmem_cache
*s
= NULL
;
309 const char *cache_name
;
315 mutex_lock(&slab_mutex
);
317 err
= kmem_cache_sanity_check(name
, size
);
322 /* Refuse requests with allocator specific flags */
323 if (flags
& ~SLAB_FLAGS_PERMITTED
) {
329 * Some allocators will constraint the set of valid flags to a subset
330 * of all flags. We expect them to define CACHE_CREATE_MASK in this
331 * case, and we'll just provide them with a sanitized version of the
334 flags
&= CACHE_CREATE_MASK
;
336 /* Fail closed on bad usersize of useroffset values. */
337 if (WARN_ON(!usersize
&& useroffset
) ||
338 WARN_ON(size
< usersize
|| size
- usersize
< useroffset
))
339 usersize
= useroffset
= 0;
342 s
= __kmem_cache_alias(name
, size
, align
, flags
, ctor
);
346 cache_name
= kstrdup_const(name
, GFP_KERNEL
);
352 s
= create_cache(cache_name
, size
,
353 calculate_alignment(flags
, align
, size
),
354 flags
, useroffset
, usersize
, ctor
, NULL
);
357 kfree_const(cache_name
);
361 mutex_unlock(&slab_mutex
);
367 if (flags
& SLAB_PANIC
)
368 panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
371 pr_warn("kmem_cache_create(%s) failed with error %d\n",
379 EXPORT_SYMBOL(kmem_cache_create_usercopy
);
382 * kmem_cache_create - Create a cache.
383 * @name: A string which is used in /proc/slabinfo to identify this cache.
384 * @size: The size of objects to be created in this cache.
385 * @align: The required alignment for the objects.
387 * @ctor: A constructor for the objects.
389 * Cannot be called within a interrupt, but can be interrupted.
390 * The @ctor is run when new pages are allocated by the cache.
394 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
395 * to catch references to uninitialised memory.
397 * %SLAB_RED_ZONE - Insert `Red` zones around the allocated memory to check
398 * for buffer overruns.
400 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
401 * cacheline. This can be beneficial if you're counting cycles as closely
404 * Return: a pointer to the cache on success, NULL on failure.
407 kmem_cache_create(const char *name
, unsigned int size
, unsigned int align
,
408 slab_flags_t flags
, void (*ctor
)(void *))
410 return kmem_cache_create_usercopy(name
, size
, align
, flags
, 0, 0,
413 EXPORT_SYMBOL(kmem_cache_create
);
415 static void slab_caches_to_rcu_destroy_workfn(struct work_struct
*work
)
417 LIST_HEAD(to_destroy
);
418 struct kmem_cache
*s
, *s2
;
421 * On destruction, SLAB_TYPESAFE_BY_RCU kmem_caches are put on the
422 * @slab_caches_to_rcu_destroy list. The slab pages are freed
423 * through RCU and the associated kmem_cache are dereferenced
424 * while freeing the pages, so the kmem_caches should be freed only
425 * after the pending RCU operations are finished. As rcu_barrier()
426 * is a pretty slow operation, we batch all pending destructions
429 mutex_lock(&slab_mutex
);
430 list_splice_init(&slab_caches_to_rcu_destroy
, &to_destroy
);
431 mutex_unlock(&slab_mutex
);
433 if (list_empty(&to_destroy
))
438 list_for_each_entry_safe(s
, s2
, &to_destroy
, list
) {
439 #ifdef SLAB_SUPPORTS_SYSFS
440 sysfs_slab_release(s
);
442 slab_kmem_cache_release(s
);
447 static int shutdown_cache(struct kmem_cache
*s
)
449 /* free asan quarantined objects */
450 kasan_cache_shutdown(s
);
452 if (__kmem_cache_shutdown(s
) != 0)
457 if (s
->flags
& SLAB_TYPESAFE_BY_RCU
) {
458 #ifdef SLAB_SUPPORTS_SYSFS
459 sysfs_slab_unlink(s
);
461 list_add_tail(&s
->list
, &slab_caches_to_rcu_destroy
);
462 schedule_work(&slab_caches_to_rcu_destroy_work
);
464 #ifdef SLAB_SUPPORTS_SYSFS
465 sysfs_slab_unlink(s
);
466 sysfs_slab_release(s
);
468 slab_kmem_cache_release(s
);
475 void slab_kmem_cache_release(struct kmem_cache
*s
)
477 __kmem_cache_release(s
);
478 kfree_const(s
->name
);
479 kmem_cache_free(kmem_cache
, s
);
482 void kmem_cache_destroy(struct kmem_cache
*s
)
492 mutex_lock(&slab_mutex
);
498 err
= shutdown_cache(s
);
500 pr_err("kmem_cache_destroy %s: Slab cache still has objects\n",
505 mutex_unlock(&slab_mutex
);
510 EXPORT_SYMBOL(kmem_cache_destroy
);
513 * kmem_cache_shrink - Shrink a cache.
514 * @cachep: The cache to shrink.
516 * Releases as many slabs as possible for a cache.
517 * To help debugging, a zero exit status indicates all slabs were released.
519 * Return: %0 if all slabs were released, non-zero otherwise
521 int kmem_cache_shrink(struct kmem_cache
*cachep
)
527 kasan_cache_shrink(cachep
);
528 ret
= __kmem_cache_shrink(cachep
);
533 EXPORT_SYMBOL(kmem_cache_shrink
);
535 bool slab_is_available(void)
537 return slab_state
>= UP
;
541 /* Create a cache during boot when no slab services are available yet */
542 void __init
create_boot_cache(struct kmem_cache
*s
, const char *name
,
543 unsigned int size
, slab_flags_t flags
,
544 unsigned int useroffset
, unsigned int usersize
)
547 unsigned int align
= ARCH_KMALLOC_MINALIGN
;
550 s
->size
= s
->object_size
= size
;
553 * For power of two sizes, guarantee natural alignment for kmalloc
554 * caches, regardless of SL*B debugging options.
556 if (is_power_of_2(size
))
557 align
= max(align
, size
);
558 s
->align
= calculate_alignment(flags
, align
, size
);
560 s
->useroffset
= useroffset
;
561 s
->usersize
= usersize
;
563 err
= __kmem_cache_create(s
, flags
);
566 panic("Creation of kmalloc slab %s size=%u failed. Reason %d\n",
569 s
->refcount
= -1; /* Exempt from merging for now */
572 struct kmem_cache
*__init
create_kmalloc_cache(const char *name
,
573 unsigned int size
, slab_flags_t flags
,
574 unsigned int useroffset
, unsigned int usersize
)
576 struct kmem_cache
*s
= kmem_cache_zalloc(kmem_cache
, GFP_NOWAIT
);
579 panic("Out of memory when creating slab %s\n", name
);
581 create_boot_cache(s
, name
, size
, flags
, useroffset
, usersize
);
582 list_add(&s
->list
, &slab_caches
);
588 kmalloc_caches
[NR_KMALLOC_TYPES
][KMALLOC_SHIFT_HIGH
+ 1] __ro_after_init
=
589 { /* initialization for https://bugs.llvm.org/show_bug.cgi?id=42570 */ };
590 EXPORT_SYMBOL(kmalloc_caches
);
593 * Conversion table for small slabs sizes / 8 to the index in the
594 * kmalloc array. This is necessary for slabs < 192 since we have non power
595 * of two cache sizes there. The size of larger slabs can be determined using
598 static u8 size_index
[24] __ro_after_init
= {
625 static inline unsigned int size_index_elem(unsigned int bytes
)
627 return (bytes
- 1) / 8;
631 * Find the kmem_cache structure that serves a given size of
634 struct kmem_cache
*kmalloc_slab(size_t size
, gfp_t flags
)
640 return ZERO_SIZE_PTR
;
642 index
= size_index
[size_index_elem(size
)];
644 if (WARN_ON_ONCE(size
> KMALLOC_MAX_CACHE_SIZE
))
646 index
= fls(size
- 1);
649 return kmalloc_caches
[kmalloc_type(flags
)][index
];
652 #ifdef CONFIG_ZONE_DMA
653 #define INIT_KMALLOC_INFO(__size, __short_size) \
655 .name[KMALLOC_NORMAL] = "kmalloc-" #__short_size, \
656 .name[KMALLOC_RECLAIM] = "kmalloc-rcl-" #__short_size, \
657 .name[KMALLOC_DMA] = "dma-kmalloc-" #__short_size, \
661 #define INIT_KMALLOC_INFO(__size, __short_size) \
663 .name[KMALLOC_NORMAL] = "kmalloc-" #__short_size, \
664 .name[KMALLOC_RECLAIM] = "kmalloc-rcl-" #__short_size, \
670 * kmalloc_info[] is to make slub_debug=,kmalloc-xx option work at boot time.
671 * kmalloc_index() supports up to 2^26=64MB, so the final entry of the table is
674 const struct kmalloc_info_struct kmalloc_info
[] __initconst
= {
675 INIT_KMALLOC_INFO(0, 0),
676 INIT_KMALLOC_INFO(96, 96),
677 INIT_KMALLOC_INFO(192, 192),
678 INIT_KMALLOC_INFO(8, 8),
679 INIT_KMALLOC_INFO(16, 16),
680 INIT_KMALLOC_INFO(32, 32),
681 INIT_KMALLOC_INFO(64, 64),
682 INIT_KMALLOC_INFO(128, 128),
683 INIT_KMALLOC_INFO(256, 256),
684 INIT_KMALLOC_INFO(512, 512),
685 INIT_KMALLOC_INFO(1024, 1k
),
686 INIT_KMALLOC_INFO(2048, 2k
),
687 INIT_KMALLOC_INFO(4096, 4k
),
688 INIT_KMALLOC_INFO(8192, 8k
),
689 INIT_KMALLOC_INFO(16384, 16k
),
690 INIT_KMALLOC_INFO(32768, 32k
),
691 INIT_KMALLOC_INFO(65536, 64k
),
692 INIT_KMALLOC_INFO(131072, 128k
),
693 INIT_KMALLOC_INFO(262144, 256k
),
694 INIT_KMALLOC_INFO(524288, 512k
),
695 INIT_KMALLOC_INFO(1048576, 1M
),
696 INIT_KMALLOC_INFO(2097152, 2M
),
697 INIT_KMALLOC_INFO(4194304, 4M
),
698 INIT_KMALLOC_INFO(8388608, 8M
),
699 INIT_KMALLOC_INFO(16777216, 16M
),
700 INIT_KMALLOC_INFO(33554432, 32M
),
701 INIT_KMALLOC_INFO(67108864, 64M
)
705 * Patch up the size_index table if we have strange large alignment
706 * requirements for the kmalloc array. This is only the case for
707 * MIPS it seems. The standard arches will not generate any code here.
709 * Largest permitted alignment is 256 bytes due to the way we
710 * handle the index determination for the smaller caches.
712 * Make sure that nothing crazy happens if someone starts tinkering
713 * around with ARCH_KMALLOC_MINALIGN
715 void __init
setup_kmalloc_cache_index_table(void)
719 BUILD_BUG_ON(KMALLOC_MIN_SIZE
> 256 ||
720 (KMALLOC_MIN_SIZE
& (KMALLOC_MIN_SIZE
- 1)));
722 for (i
= 8; i
< KMALLOC_MIN_SIZE
; i
+= 8) {
723 unsigned int elem
= size_index_elem(i
);
725 if (elem
>= ARRAY_SIZE(size_index
))
727 size_index
[elem
] = KMALLOC_SHIFT_LOW
;
730 if (KMALLOC_MIN_SIZE
>= 64) {
732 * The 96 byte size cache is not used if the alignment
735 for (i
= 64 + 8; i
<= 96; i
+= 8)
736 size_index
[size_index_elem(i
)] = 7;
740 if (KMALLOC_MIN_SIZE
>= 128) {
742 * The 192 byte sized cache is not used if the alignment
743 * is 128 byte. Redirect kmalloc to use the 256 byte cache
746 for (i
= 128 + 8; i
<= 192; i
+= 8)
747 size_index
[size_index_elem(i
)] = 8;
752 new_kmalloc_cache(int idx
, enum kmalloc_cache_type type
, slab_flags_t flags
)
754 if (type
== KMALLOC_RECLAIM
)
755 flags
|= SLAB_RECLAIM_ACCOUNT
;
757 kmalloc_caches
[type
][idx
] = create_kmalloc_cache(
758 kmalloc_info
[idx
].name
[type
],
759 kmalloc_info
[idx
].size
, flags
, 0,
760 kmalloc_info
[idx
].size
);
764 * Create the kmalloc array. Some of the regular kmalloc arrays
765 * may already have been created because they were needed to
766 * enable allocations for slab creation.
768 void __init
create_kmalloc_caches(slab_flags_t flags
)
771 enum kmalloc_cache_type type
;
773 for (type
= KMALLOC_NORMAL
; type
<= KMALLOC_RECLAIM
; type
++) {
774 for (i
= KMALLOC_SHIFT_LOW
; i
<= KMALLOC_SHIFT_HIGH
; i
++) {
775 if (!kmalloc_caches
[type
][i
])
776 new_kmalloc_cache(i
, type
, flags
);
779 * Caches that are not of the two-to-the-power-of size.
780 * These have to be created immediately after the
781 * earlier power of two caches
783 if (KMALLOC_MIN_SIZE
<= 32 && i
== 6 &&
784 !kmalloc_caches
[type
][1])
785 new_kmalloc_cache(1, type
, flags
);
786 if (KMALLOC_MIN_SIZE
<= 64 && i
== 7 &&
787 !kmalloc_caches
[type
][2])
788 new_kmalloc_cache(2, type
, flags
);
792 /* Kmalloc array is now usable */
795 #ifdef CONFIG_ZONE_DMA
796 for (i
= 0; i
<= KMALLOC_SHIFT_HIGH
; i
++) {
797 struct kmem_cache
*s
= kmalloc_caches
[KMALLOC_NORMAL
][i
];
800 kmalloc_caches
[KMALLOC_DMA
][i
] = create_kmalloc_cache(
801 kmalloc_info
[i
].name
[KMALLOC_DMA
],
802 kmalloc_info
[i
].size
,
803 SLAB_CACHE_DMA
| flags
, 0,
804 kmalloc_info
[i
].size
);
809 #endif /* !CONFIG_SLOB */
811 gfp_t
kmalloc_fix_flags(gfp_t flags
)
813 gfp_t invalid_mask
= flags
& GFP_SLAB_BUG_MASK
;
815 flags
&= ~GFP_SLAB_BUG_MASK
;
816 pr_warn("Unexpected gfp: %#x (%pGg). Fixing up to gfp: %#x (%pGg). Fix your code!\n",
817 invalid_mask
, &invalid_mask
, flags
, &flags
);
824 * To avoid unnecessary overhead, we pass through large allocation requests
825 * directly to the page allocator. We use __GFP_COMP, because we will need to
826 * know the allocation order to free the pages properly in kfree.
828 void *kmalloc_order(size_t size
, gfp_t flags
, unsigned int order
)
833 if (unlikely(flags
& GFP_SLAB_BUG_MASK
))
834 flags
= kmalloc_fix_flags(flags
);
837 page
= alloc_pages(flags
, order
);
839 ret
= page_address(page
);
840 mod_node_page_state(page_pgdat(page
), NR_SLAB_UNRECLAIMABLE_B
,
843 ret
= kasan_kmalloc_large(ret
, size
, flags
);
844 /* As ret might get tagged, call kmemleak hook after KASAN. */
845 kmemleak_alloc(ret
, size
, 1, flags
);
848 EXPORT_SYMBOL(kmalloc_order
);
850 #ifdef CONFIG_TRACING
851 void *kmalloc_order_trace(size_t size
, gfp_t flags
, unsigned int order
)
853 void *ret
= kmalloc_order(size
, flags
, order
);
854 trace_kmalloc(_RET_IP_
, ret
, size
, PAGE_SIZE
<< order
, flags
);
857 EXPORT_SYMBOL(kmalloc_order_trace
);
860 #ifdef CONFIG_SLAB_FREELIST_RANDOM
861 /* Randomize a generic freelist */
862 static void freelist_randomize(struct rnd_state
*state
, unsigned int *list
,
868 for (i
= 0; i
< count
; i
++)
871 /* Fisher-Yates shuffle */
872 for (i
= count
- 1; i
> 0; i
--) {
873 rand
= prandom_u32_state(state
);
875 swap(list
[i
], list
[rand
]);
879 /* Create a random sequence per cache */
880 int cache_random_seq_create(struct kmem_cache
*cachep
, unsigned int count
,
883 struct rnd_state state
;
885 if (count
< 2 || cachep
->random_seq
)
888 cachep
->random_seq
= kcalloc(count
, sizeof(unsigned int), gfp
);
889 if (!cachep
->random_seq
)
892 /* Get best entropy at this stage of boot */
893 prandom_seed_state(&state
, get_random_long());
895 freelist_randomize(&state
, cachep
->random_seq
, count
);
899 /* Destroy the per-cache random freelist sequence */
900 void cache_random_seq_destroy(struct kmem_cache
*cachep
)
902 kfree(cachep
->random_seq
);
903 cachep
->random_seq
= NULL
;
905 #endif /* CONFIG_SLAB_FREELIST_RANDOM */
907 #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)
909 #define SLABINFO_RIGHTS (0600)
911 #define SLABINFO_RIGHTS (0400)
914 static void print_slabinfo_header(struct seq_file
*m
)
917 * Output format version, so at least we can change it
918 * without _too_ many complaints.
920 #ifdef CONFIG_DEBUG_SLAB
921 seq_puts(m
, "slabinfo - version: 2.1 (statistics)\n");
923 seq_puts(m
, "slabinfo - version: 2.1\n");
925 seq_puts(m
, "# name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab>");
926 seq_puts(m
, " : tunables <limit> <batchcount> <sharedfactor>");
927 seq_puts(m
, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
928 #ifdef CONFIG_DEBUG_SLAB
929 seq_puts(m
, " : globalstat <listallocs> <maxobjs> <grown> <reaped> <error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
930 seq_puts(m
, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
935 void *slab_start(struct seq_file
*m
, loff_t
*pos
)
937 mutex_lock(&slab_mutex
);
938 return seq_list_start(&slab_caches
, *pos
);
941 void *slab_next(struct seq_file
*m
, void *p
, loff_t
*pos
)
943 return seq_list_next(p
, &slab_caches
, pos
);
946 void slab_stop(struct seq_file
*m
, void *p
)
948 mutex_unlock(&slab_mutex
);
951 static void cache_show(struct kmem_cache
*s
, struct seq_file
*m
)
953 struct slabinfo sinfo
;
955 memset(&sinfo
, 0, sizeof(sinfo
));
956 get_slabinfo(s
, &sinfo
);
958 seq_printf(m
, "%-17s %6lu %6lu %6u %4u %4d",
959 s
->name
, sinfo
.active_objs
, sinfo
.num_objs
, s
->size
,
960 sinfo
.objects_per_slab
, (1 << sinfo
.cache_order
));
962 seq_printf(m
, " : tunables %4u %4u %4u",
963 sinfo
.limit
, sinfo
.batchcount
, sinfo
.shared
);
964 seq_printf(m
, " : slabdata %6lu %6lu %6lu",
965 sinfo
.active_slabs
, sinfo
.num_slabs
, sinfo
.shared_avail
);
966 slabinfo_show_stats(m
, s
);
970 static int slab_show(struct seq_file
*m
, void *p
)
972 struct kmem_cache
*s
= list_entry(p
, struct kmem_cache
, list
);
974 if (p
== slab_caches
.next
)
975 print_slabinfo_header(m
);
980 void dump_unreclaimable_slab(void)
982 struct kmem_cache
*s
;
983 struct slabinfo sinfo
;
986 * Here acquiring slab_mutex is risky since we don't prefer to get
987 * sleep in oom path. But, without mutex hold, it may introduce a
989 * Use mutex_trylock to protect the list traverse, dump nothing
990 * without acquiring the mutex.
992 if (!mutex_trylock(&slab_mutex
)) {
993 pr_warn("excessive unreclaimable slab but cannot dump stats\n");
997 pr_info("Unreclaimable slab info:\n");
998 pr_info("Name Used Total\n");
1000 list_for_each_entry(s
, &slab_caches
, list
) {
1001 if (s
->flags
& SLAB_RECLAIM_ACCOUNT
)
1004 get_slabinfo(s
, &sinfo
);
1006 if (sinfo
.num_objs
> 0)
1007 pr_info("%-17s %10luKB %10luKB\n", s
->name
,
1008 (sinfo
.active_objs
* s
->size
) / 1024,
1009 (sinfo
.num_objs
* s
->size
) / 1024);
1011 mutex_unlock(&slab_mutex
);
1014 #if defined(CONFIG_MEMCG_KMEM)
1015 int memcg_slab_show(struct seq_file
*m
, void *p
)
1019 * Please, take a look at tools/cgroup/slabinfo.py .
1026 * slabinfo_op - iterator that generates /proc/slabinfo
1035 * num-pages-per-slab
1036 * + further values on SMP and with statistics enabled
1038 static const struct seq_operations slabinfo_op
= {
1039 .start
= slab_start
,
1045 static int slabinfo_open(struct inode
*inode
, struct file
*file
)
1047 return seq_open(file
, &slabinfo_op
);
1050 static const struct proc_ops slabinfo_proc_ops
= {
1051 .proc_flags
= PROC_ENTRY_PERMANENT
,
1052 .proc_open
= slabinfo_open
,
1053 .proc_read
= seq_read
,
1054 .proc_write
= slabinfo_write
,
1055 .proc_lseek
= seq_lseek
,
1056 .proc_release
= seq_release
,
1059 static int __init
slab_proc_init(void)
1061 proc_create("slabinfo", SLABINFO_RIGHTS
, NULL
, &slabinfo_proc_ops
);
1064 module_init(slab_proc_init
);
1066 #endif /* CONFIG_SLAB || CONFIG_SLUB_DEBUG */
1068 static __always_inline
void *__do_krealloc(const void *p
, size_t new_size
,
1076 if (ks
>= new_size
) {
1077 p
= kasan_krealloc((void *)p
, new_size
, flags
);
1081 ret
= kmalloc_track_caller(new_size
, flags
);
1089 * krealloc - reallocate memory. The contents will remain unchanged.
1090 * @p: object to reallocate memory for.
1091 * @new_size: how many bytes of memory are required.
1092 * @flags: the type of memory to allocate.
1094 * The contents of the object pointed to are preserved up to the
1095 * lesser of the new and old sizes (__GFP_ZERO flag is effectively ignored).
1096 * If @p is %NULL, krealloc() behaves exactly like kmalloc(). If @new_size
1097 * is 0 and @p is not a %NULL pointer, the object pointed to is freed.
1099 * Return: pointer to the allocated memory or %NULL in case of error
1101 void *krealloc(const void *p
, size_t new_size
, gfp_t flags
)
1105 if (unlikely(!new_size
)) {
1107 return ZERO_SIZE_PTR
;
1110 ret
= __do_krealloc(p
, new_size
, flags
);
1111 if (ret
&& kasan_reset_tag(p
) != kasan_reset_tag(ret
))
1116 EXPORT_SYMBOL(krealloc
);
1119 * kfree_sensitive - Clear sensitive information in memory before freeing
1120 * @p: object to free memory of
1122 * The memory of the object @p points to is zeroed before freed.
1123 * If @p is %NULL, kfree_sensitive() does nothing.
1125 * Note: this function zeroes the whole allocated buffer which can be a good
1126 * deal bigger than the requested buffer size passed to kmalloc(). So be
1127 * careful when using this function in performance sensitive code.
1129 void kfree_sensitive(const void *p
)
1132 void *mem
= (void *)p
;
1136 memzero_explicit(mem
, ks
);
1139 EXPORT_SYMBOL(kfree_sensitive
);
1142 * ksize - get the actual amount of memory allocated for a given object
1143 * @objp: Pointer to the object
1145 * kmalloc may internally round up allocations and return more memory
1146 * than requested. ksize() can be used to determine the actual amount of
1147 * memory allocated. The caller may use this additional memory, even though
1148 * a smaller amount of memory was initially specified with the kmalloc call.
1149 * The caller must guarantee that objp points to a valid object previously
1150 * allocated with either kmalloc() or kmem_cache_alloc(). The object
1151 * must not be freed during the duration of the call.
1153 * Return: size of the actual memory used by @objp in bytes
1155 size_t ksize(const void *objp
)
1160 * We need to check that the pointed to object is valid, and only then
1161 * unpoison the shadow memory below. We use __kasan_check_read(), to
1162 * generate a more useful report at the time ksize() is called (rather
1163 * than later where behaviour is undefined due to potential
1164 * use-after-free or double-free).
1166 * If the pointed to memory is invalid we return 0, to avoid users of
1167 * ksize() writing to and potentially corrupting the memory region.
1169 * We want to perform the check before __ksize(), to avoid potentially
1170 * crashing in __ksize() due to accessing invalid metadata.
1172 if (unlikely(ZERO_OR_NULL_PTR(objp
)) || !__kasan_check_read(objp
, 1))
1175 size
= __ksize(objp
);
1177 * We assume that ksize callers could use whole allocated area,
1178 * so we need to unpoison this area.
1180 kasan_unpoison_range(objp
, size
);
1183 EXPORT_SYMBOL(ksize
);
1185 /* Tracepoints definitions. */
1186 EXPORT_TRACEPOINT_SYMBOL(kmalloc
);
1187 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc
);
1188 EXPORT_TRACEPOINT_SYMBOL(kmalloc_node
);
1189 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node
);
1190 EXPORT_TRACEPOINT_SYMBOL(kfree
);
1191 EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free
);
1193 int should_failslab(struct kmem_cache
*s
, gfp_t gfpflags
)
1195 if (__should_failslab(s
, gfpflags
))
1199 ALLOW_ERROR_INJECTION(should_failslab
, ERRNO
);