^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Slab allocator functions that are independent of the allocator strategy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * (C) 2012 Christoph Lameter <cl@linux.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/poison.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/cache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/kfence.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/kasan.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <asm/tlbflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <asm/page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/memcontrol.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include <trace/events/kmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #undef CREATE_TRACE_POINTS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #include <trace/hooks/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include "slab.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) enum slab_state slab_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) LIST_HEAD(slab_caches);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) DEFINE_MUTEX(slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct kmem_cache *kmem_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #ifdef CONFIG_HARDENED_USERCOPY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) bool usercopy_fallback __ro_after_init =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) IS_ENABLED(CONFIG_HARDENED_USERCOPY_FALLBACK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) module_param(usercopy_fallback, bool, 0400);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) MODULE_PARM_DESC(usercopy_fallback,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) "WARN instead of reject usercopy whitelist violations");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) static LIST_HEAD(slab_caches_to_rcu_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) static DECLARE_WORK(slab_caches_to_rcu_destroy_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) slab_caches_to_rcu_destroy_workfn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * Set of flags that will prevent slab merging
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) #define SLAB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) SLAB_TRACE | SLAB_TYPESAFE_BY_RCU | SLAB_NOLEAKTRACE | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) SLAB_FAILSLAB | kasan_never_merge())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) #define SLAB_MERGE_SAME (SLAB_RECLAIM_ACCOUNT | SLAB_CACHE_DMA | \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) SLAB_CACHE_DMA32 | SLAB_ACCOUNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * Merge control. If this is set then no merging of slab caches will occur.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) static bool slab_nomerge = !IS_ENABLED(CONFIG_SLAB_MERGE_DEFAULT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) static int __init setup_slab_nomerge(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) slab_nomerge = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) #ifdef CONFIG_SLUB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) __setup_param("slub_nomerge", slub_nomerge, setup_slab_nomerge, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) __setup("slab_nomerge", setup_slab_nomerge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * Determine the size of a slab object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) unsigned int kmem_cache_size(struct kmem_cache *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) return s->object_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) EXPORT_SYMBOL(kmem_cache_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #ifdef CONFIG_DEBUG_VM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static int kmem_cache_sanity_check(const char *name, unsigned int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (!name || in_interrupt() || size > KMALLOC_MAX_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) pr_err("kmem_cache_create(%s) integrity check failed\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) WARN_ON(strchr(name, ' ')); /* It confuses parsers */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) static inline int kmem_cache_sanity_check(const char *name, unsigned int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) void __kmem_cache_free_bulk(struct kmem_cache *s, size_t nr, void **p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) for (i = 0; i < nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) kmem_cache_free(s, p[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) kfree(p[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) int __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) void **p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) for (i = 0; i < nr; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) void *x = p[i] = kmem_cache_alloc(s, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (!x) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) __kmem_cache_free_bulk(s, i, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * Figure out what the alignment of the objects will be given a set of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) * flags, a user specified alignment and the size of the objects.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) static unsigned int calculate_alignment(slab_flags_t flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) unsigned int align, unsigned int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) * If the user wants hardware cache aligned objects then follow that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) * suggestion if the object is sufficiently large.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * The hardware cache alignment cannot override the specified
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * alignment though. If that is greater then use it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (flags & SLAB_HWCACHE_ALIGN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) unsigned int ralign;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) ralign = cache_line_size();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) while (size <= ralign / 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) ralign /= 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) align = max(align, ralign);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (align < ARCH_SLAB_MINALIGN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) align = ARCH_SLAB_MINALIGN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) return ALIGN(align, sizeof(void *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) * Find a mergeable slab cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) int slab_unmergeable(struct kmem_cache *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (slab_nomerge || (s->flags & SLAB_NEVER_MERGE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) if (s->ctor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (s->usersize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * We may have set a slab to be unmergeable during bootstrap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (s->refcount < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) struct kmem_cache *find_mergeable(unsigned int size, unsigned int align,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) slab_flags_t flags, const char *name, void (*ctor)(void *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) struct kmem_cache *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) if (slab_nomerge)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (ctor)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) size = ALIGN(size, sizeof(void *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) align = calculate_alignment(flags, align, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) size = ALIGN(size, align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) flags = kmem_cache_flags(size, flags, name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (flags & SLAB_NEVER_MERGE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) list_for_each_entry_reverse(s, &slab_caches, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) if (slab_unmergeable(s))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) if (size > s->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if ((flags & SLAB_MERGE_SAME) != (s->flags & SLAB_MERGE_SAME))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * Check if alignment is compatible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * Courtesy of Adrian Drzewiecki
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if ((s->size & ~(align - 1)) != s->size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) if (s->size - size >= sizeof(void *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) if (IS_ENABLED(CONFIG_SLAB) && align &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) (align > s->align || s->align % align))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) static struct kmem_cache *create_cache(const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) unsigned int object_size, unsigned int align,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) slab_flags_t flags, unsigned int useroffset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) unsigned int usersize, void (*ctor)(void *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) struct kmem_cache *root_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) struct kmem_cache *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) if (WARN_ON(useroffset + usersize > object_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) useroffset = usersize = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) if (!s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) s->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) s->size = s->object_size = object_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) s->align = align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) s->ctor = ctor;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) s->useroffset = useroffset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) s->usersize = usersize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) err = __kmem_cache_create(s, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) goto out_free_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) s->refcount = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) list_add(&s->list, &slab_caches);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) return ERR_PTR(err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) out_free_cache:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) kmem_cache_free(kmem_cache, s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) * kmem_cache_create_usercopy - Create a cache with a region suitable
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) * for copying to userspace
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) * @name: A string which is used in /proc/slabinfo to identify this cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) * @size: The size of objects to be created in this cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) * @align: The required alignment for the objects.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) * @flags: SLAB flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) * @useroffset: Usercopy region offset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) * @usersize: Usercopy region size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) * @ctor: A constructor for the objects.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) * Cannot be called within a interrupt, but can be interrupted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) * The @ctor is run when new pages are allocated by the cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) * The flags are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) * to catch references to uninitialised memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) * %SLAB_RED_ZONE - Insert `Red` zones around the allocated memory to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) * for buffer overruns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * cacheline. This can be beneficial if you're counting cycles as closely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * as davem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) * Return: a pointer to the cache on success, NULL on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) struct kmem_cache *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) kmem_cache_create_usercopy(const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) unsigned int size, unsigned int align,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) slab_flags_t flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) unsigned int useroffset, unsigned int usersize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) void (*ctor)(void *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) struct kmem_cache *s = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) const char *cache_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) get_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) get_online_mems();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) #ifdef CONFIG_SLUB_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * If no slub_debug was enabled globally, the static key is not yet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * enabled by setup_slub_debug(). Enable it if the cache is being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * created with any of the debugging flags passed explicitly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (flags & SLAB_DEBUG_FLAGS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) static_branch_enable(&slub_debug_enabled);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) mutex_lock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) err = kmem_cache_sanity_check(name, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) /* Refuse requests with allocator specific flags */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) if (flags & ~SLAB_FLAGS_PERMITTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) * Some allocators will constraint the set of valid flags to a subset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) * of all flags. We expect them to define CACHE_CREATE_MASK in this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) * case, and we'll just provide them with a sanitized version of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) * passed flags.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) flags &= CACHE_CREATE_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) /* Fail closed on bad usersize of useroffset values. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) if (WARN_ON(!usersize && useroffset) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) WARN_ON(size < usersize || size - usersize < useroffset))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) usersize = useroffset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (!usersize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) s = __kmem_cache_alias(name, size, align, flags, ctor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) if (s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) cache_name = kstrdup_const(name, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (!cache_name) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) err = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) s = create_cache(cache_name, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) calculate_alignment(flags, align, size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) flags, useroffset, usersize, ctor, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) if (IS_ERR(s)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) err = PTR_ERR(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) kfree_const(cache_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) mutex_unlock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) put_online_mems();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) put_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (flags & SLAB_PANIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) name, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) pr_warn("kmem_cache_create(%s) failed with error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) name, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) EXPORT_SYMBOL(kmem_cache_create_usercopy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) * kmem_cache_create - Create a cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) * @name: A string which is used in /proc/slabinfo to identify this cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) * @size: The size of objects to be created in this cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) * @align: The required alignment for the objects.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) * @flags: SLAB flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) * @ctor: A constructor for the objects.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) * Cannot be called within a interrupt, but can be interrupted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) * The @ctor is run when new pages are allocated by the cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) * The flags are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) * to catch references to uninitialised memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * %SLAB_RED_ZONE - Insert `Red` zones around the allocated memory to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * for buffer overruns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * cacheline. This can be beneficial if you're counting cycles as closely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * as davem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * Return: a pointer to the cache on success, NULL on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) struct kmem_cache *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) kmem_cache_create(const char *name, unsigned int size, unsigned int align,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) slab_flags_t flags, void (*ctor)(void *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) return kmem_cache_create_usercopy(name, size, align, flags, 0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) ctor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) EXPORT_SYMBOL(kmem_cache_create);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) LIST_HEAD(to_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) struct kmem_cache *s, *s2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) * On destruction, SLAB_TYPESAFE_BY_RCU kmem_caches are put on the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) * @slab_caches_to_rcu_destroy list. The slab pages are freed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) * through RCU and the associated kmem_cache are dereferenced
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) * while freeing the pages, so the kmem_caches should be freed only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) * after the pending RCU operations are finished. As rcu_barrier()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) * is a pretty slow operation, we batch all pending destructions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) * asynchronously.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) mutex_lock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) list_splice_init(&slab_caches_to_rcu_destroy, &to_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) mutex_unlock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) if (list_empty(&to_destroy))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) rcu_barrier();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) list_for_each_entry_safe(s, s2, &to_destroy, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) debugfs_slab_release(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) kfence_shutdown_cache(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) #ifdef SLAB_SUPPORTS_SYSFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) sysfs_slab_release(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) slab_kmem_cache_release(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) static int shutdown_cache(struct kmem_cache *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) /* free asan quarantined objects */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) kasan_cache_shutdown(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) if (__kmem_cache_shutdown(s) != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) list_del(&s->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) if (s->flags & SLAB_TYPESAFE_BY_RCU) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) #ifdef SLAB_SUPPORTS_SYSFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) sysfs_slab_unlink(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) list_add_tail(&s->list, &slab_caches_to_rcu_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) schedule_work(&slab_caches_to_rcu_destroy_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) kfence_shutdown_cache(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) debugfs_slab_release(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) #ifdef SLAB_SUPPORTS_SYSFS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) sysfs_slab_unlink(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) sysfs_slab_release(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) slab_kmem_cache_release(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) void slab_kmem_cache_release(struct kmem_cache *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) __kmem_cache_release(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) kfree_const(s->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) kmem_cache_free(kmem_cache, s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) void kmem_cache_destroy(struct kmem_cache *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) if (unlikely(!s))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) get_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) get_online_mems();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) mutex_lock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) s->refcount--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) if (s->refcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) goto out_unlock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) err = shutdown_cache(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) pr_err("kmem_cache_destroy %s: Slab cache still has objects\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) s->name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) out_unlock:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) mutex_unlock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) put_online_mems();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) put_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) EXPORT_SYMBOL(kmem_cache_destroy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) * kmem_cache_shrink - Shrink a cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) * @cachep: The cache to shrink.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) * Releases as many slabs as possible for a cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) * To help debugging, a zero exit status indicates all slabs were released.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) * Return: %0 if all slabs were released, non-zero otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) int kmem_cache_shrink(struct kmem_cache *cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) get_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) get_online_mems();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) kasan_cache_shrink(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) ret = __kmem_cache_shrink(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) put_online_mems();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) put_online_cpus();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) EXPORT_SYMBOL(kmem_cache_shrink);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) bool slab_is_available(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) return slab_state >= UP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) #ifndef CONFIG_SLOB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) /* Create a cache during boot when no slab services are available yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) void __init create_boot_cache(struct kmem_cache *s, const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) unsigned int size, slab_flags_t flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) unsigned int useroffset, unsigned int usersize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) unsigned int align = ARCH_KMALLOC_MINALIGN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) s->name = name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) s->size = s->object_size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) * For power of two sizes, guarantee natural alignment for kmalloc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) * caches, regardless of SL*B debugging options.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) if (is_power_of_2(size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) align = max(align, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) s->align = calculate_alignment(flags, align, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) s->useroffset = useroffset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) s->usersize = usersize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) err = __kmem_cache_create(s, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) panic("Creation of kmalloc slab %s size=%u failed. Reason %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) name, size, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) s->refcount = -1; /* Exempt from merging for now */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) struct kmem_cache *__init create_kmalloc_cache(const char *name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) unsigned int size, slab_flags_t flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) unsigned int useroffset, unsigned int usersize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) if (!s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) panic("Out of memory when creating slab %s\n", name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) create_boot_cache(s, name, size, flags, useroffset, usersize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) kasan_cache_create_kmalloc(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) list_add(&s->list, &slab_caches);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) s->refcount = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) struct kmem_cache *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) kmalloc_caches[NR_KMALLOC_TYPES][KMALLOC_SHIFT_HIGH + 1] __ro_after_init =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) { /* initialization for https://bugs.llvm.org/show_bug.cgi?id=42570 */ };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) EXPORT_SYMBOL(kmalloc_caches);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) * Conversion table for small slabs sizes / 8 to the index in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) * kmalloc array. This is necessary for slabs < 192 since we have non power
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) * of two cache sizes there. The size of larger slabs can be determined using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612) * fls.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) static u8 size_index[24] __ro_after_init = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) 3, /* 8 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) 4, /* 16 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) 5, /* 24 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) 5, /* 32 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) 6, /* 40 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) 6, /* 48 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621) 6, /* 56 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) 6, /* 64 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) 1, /* 72 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) 1, /* 80 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) 1, /* 88 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) 1, /* 96 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627) 7, /* 104 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) 7, /* 112 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) 7, /* 120 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) 7, /* 128 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) 2, /* 136 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) 2, /* 144 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633) 2, /* 152 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) 2, /* 160 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) 2, /* 168 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) 2, /* 176 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) 2, /* 184 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638) 2 /* 192 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) static inline unsigned int size_index_elem(unsigned int bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643) return (bytes - 1) / 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) * Find the kmem_cache structure that serves a given size of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) * allocation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) unsigned int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) struct kmem_cache *s = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) if (size <= 192) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) if (!size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) return ZERO_SIZE_PTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) index = size_index[size_index_elem(size)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) if (WARN_ON_ONCE(size > KMALLOC_MAX_CACHE_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663) index = fls(size - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) trace_android_vh_kmalloc_slab(index, flags, &s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) if (s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) return s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) return kmalloc_caches[kmalloc_type(flags)][index];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) #ifdef CONFIG_ZONE_DMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) #define INIT_KMALLOC_INFO(__size, __short_size) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) .name[KMALLOC_NORMAL] = "kmalloc-" #__short_size, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) .name[KMALLOC_RECLAIM] = "kmalloc-rcl-" #__short_size, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) .name[KMALLOC_DMA] = "dma-kmalloc-" #__short_size, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) .size = __size, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) #define INIT_KMALLOC_INFO(__size, __short_size) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683) { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) .name[KMALLOC_NORMAL] = "kmalloc-" #__short_size, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) .name[KMALLOC_RECLAIM] = "kmalloc-rcl-" #__short_size, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) .size = __size, \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) * kmalloc_info[] is to make slub_debug=,kmalloc-xx option work at boot time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) * kmalloc_index() supports up to 2^26=64MB, so the final entry of the table is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) * kmalloc-67108864.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) const struct kmalloc_info_struct kmalloc_info[] __initconst = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) INIT_KMALLOC_INFO(0, 0),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) INIT_KMALLOC_INFO(96, 96),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) INIT_KMALLOC_INFO(192, 192),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) INIT_KMALLOC_INFO(8, 8),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700) INIT_KMALLOC_INFO(16, 16),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) INIT_KMALLOC_INFO(32, 32),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) INIT_KMALLOC_INFO(64, 64),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) INIT_KMALLOC_INFO(128, 128),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) INIT_KMALLOC_INFO(256, 256),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) INIT_KMALLOC_INFO(512, 512),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) INIT_KMALLOC_INFO(1024, 1k),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) INIT_KMALLOC_INFO(2048, 2k),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) INIT_KMALLOC_INFO(4096, 4k),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) INIT_KMALLOC_INFO(8192, 8k),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710) INIT_KMALLOC_INFO(16384, 16k),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) INIT_KMALLOC_INFO(32768, 32k),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) INIT_KMALLOC_INFO(65536, 64k),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) INIT_KMALLOC_INFO(131072, 128k),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) INIT_KMALLOC_INFO(262144, 256k),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) INIT_KMALLOC_INFO(524288, 512k),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716) INIT_KMALLOC_INFO(1048576, 1M),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) INIT_KMALLOC_INFO(2097152, 2M),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) INIT_KMALLOC_INFO(4194304, 4M),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) INIT_KMALLOC_INFO(8388608, 8M),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) INIT_KMALLOC_INFO(16777216, 16M),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) INIT_KMALLOC_INFO(33554432, 32M),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) INIT_KMALLOC_INFO(67108864, 64M)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) * Patch up the size_index table if we have strange large alignment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727) * requirements for the kmalloc array. This is only the case for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) * MIPS it seems. The standard arches will not generate any code here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) * Largest permitted alignment is 256 bytes due to the way we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) * handle the index determination for the smaller caches.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) * Make sure that nothing crazy happens if someone starts tinkering
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) * around with ARCH_KMALLOC_MINALIGN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) void __init setup_kmalloc_cache_index_table(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740) BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) unsigned int elem = size_index_elem(i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) if (elem >= ARRAY_SIZE(size_index))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748) size_index[elem] = KMALLOC_SHIFT_LOW;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) if (KMALLOC_MIN_SIZE >= 64) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753) * The 96 byte size cache is not used if the alignment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) * is 64 byte.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) for (i = 64 + 8; i <= 96; i += 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) size_index[size_index_elem(i)] = 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 759) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 760)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 761) if (KMALLOC_MIN_SIZE >= 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) * The 192 byte sized cache is not used if the alignment
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) * is 128 byte. Redirect kmalloc to use the 256 byte cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) * instead.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) for (i = 128 + 8; i <= 192; i += 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) size_index[size_index_elem(i)] = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) static void __init
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) new_kmalloc_cache(int idx, enum kmalloc_cache_type type, slab_flags_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) if (type == KMALLOC_RECLAIM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) flags |= SLAB_RECLAIM_ACCOUNT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) kmalloc_caches[type][idx] = create_kmalloc_cache(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) kmalloc_info[idx].name[type],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) kmalloc_info[idx].size, flags, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) kmalloc_info[idx].size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) * Create the kmalloc array. Some of the regular kmalloc arrays
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) * may already have been created because they were needed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) * enable allocations for slab creation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) void __init create_kmalloc_caches(slab_flags_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) enum kmalloc_cache_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) for (type = KMALLOC_NORMAL; type <= KMALLOC_RECLAIM; type++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) if (!kmalloc_caches[type][i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) new_kmalloc_cache(i, type, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) * Caches that are not of the two-to-the-power-of size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) * These have to be created immediately after the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) * earlier power of two caches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804) if (KMALLOC_MIN_SIZE <= 32 && i == 6 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) !kmalloc_caches[type][1])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) new_kmalloc_cache(1, type, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807) if (KMALLOC_MIN_SIZE <= 64 && i == 7 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) !kmalloc_caches[type][2])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) new_kmalloc_cache(2, type, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) /* Kmalloc array is now usable */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) slab_state = UP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) #ifdef CONFIG_ZONE_DMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817) for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) struct kmem_cache *s = kmalloc_caches[KMALLOC_NORMAL][i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) if (s) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821) kmalloc_caches[KMALLOC_DMA][i] = create_kmalloc_cache(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) kmalloc_info[i].name[KMALLOC_DMA],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) kmalloc_info[i].size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) SLAB_CACHE_DMA | flags, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) kmalloc_info[i].size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) #endif /* !CONFIG_SLOB */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) gfp_t kmalloc_fix_flags(gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) gfp_t invalid_mask = flags & GFP_SLAB_BUG_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836) flags &= ~GFP_SLAB_BUG_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) pr_warn("Unexpected gfp: %#x (%pGg). Fixing up to gfp: %#x (%pGg). Fix your code!\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) invalid_mask, &invalid_mask, flags, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) return flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) * To avoid unnecessary overhead, we pass through large allocation requests
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) * directly to the page allocator. We use __GFP_COMP, because we will need to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847) * know the allocation order to free the pages properly in kfree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) void *kmalloc_order(size_t size, gfp_t flags, unsigned int order)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) void *ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854) if (unlikely(flags & GFP_SLAB_BUG_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) flags = kmalloc_fix_flags(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857) flags |= __GFP_COMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) page = alloc_pages(flags, order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) if (likely(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) ret = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) mod_lruvec_page_state(page, NR_SLAB_UNRECLAIMABLE_B,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) PAGE_SIZE << order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) ret = kasan_kmalloc_large(ret, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) /* As ret might get tagged, call kmemleak hook after KASAN. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) kmemleak_alloc(ret, size, 1, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) EXPORT_SYMBOL(kmalloc_order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) #ifdef CONFIG_TRACING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872) void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) void *ret = kmalloc_order(size, flags, order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 876) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 877) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 878) EXPORT_SYMBOL(kmalloc_order_trace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) #ifdef CONFIG_SLAB_FREELIST_RANDOM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882) /* Randomize a generic freelist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) static void freelist_randomize(struct rnd_state *state, unsigned int *list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) unsigned int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) unsigned int rand;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) for (i = 0; i < count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) list[i] = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892) /* Fisher-Yates shuffle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) for (i = count - 1; i > 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) rand = prandom_u32_state(state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) rand %= (i + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) swap(list[i], list[rand]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 897) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 898) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 899)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) /* Create a random sequence per cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) struct rnd_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) if (count < 2 || cachep->random_seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909) cachep->random_seq = kcalloc(count, sizeof(unsigned int), gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) if (!cachep->random_seq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) /* Get best entropy at this stage of boot */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) prandom_seed_state(&state, get_random_long());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) freelist_randomize(&state, cachep->random_seq, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) /* Destroy the per-cache random freelist sequence */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) void cache_random_seq_destroy(struct kmem_cache *cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923) kfree(cachep->random_seq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) cachep->random_seq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) #endif /* CONFIG_SLAB_FREELIST_RANDOM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928) #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) #ifdef CONFIG_SLAB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) #define SLABINFO_RIGHTS (0600)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) #define SLABINFO_RIGHTS (0400)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) static void print_slabinfo_header(struct seq_file *m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) * Output format version, so at least we can change it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) * without _too_ many complaints.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) #ifdef CONFIG_DEBUG_SLAB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) seq_puts(m, "slabinfo - version: 2.1\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) seq_puts(m, "# name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948) seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) #ifdef CONFIG_DEBUG_SLAB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950) seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> <error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) seq_putc(m, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) void *slab_start(struct seq_file *m, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) mutex_lock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) return seq_list_start(&slab_caches, *pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) void *slab_next(struct seq_file *m, void *p, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) return seq_list_next(p, &slab_caches, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967) void slab_stop(struct seq_file *m, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969) mutex_unlock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972) static void cache_show(struct kmem_cache *s, struct seq_file *m)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) struct slabinfo sinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) memset(&sinfo, 0, sizeof(sinfo));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977) get_slabinfo(s, &sinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) s->name, sinfo.active_objs, sinfo.num_objs, s->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) sinfo.objects_per_slab, (1 << sinfo.cache_order));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) seq_printf(m, " : tunables %4u %4u %4u",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) sinfo.limit, sinfo.batchcount, sinfo.shared);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) seq_printf(m, " : slabdata %6lu %6lu %6lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) slabinfo_show_stats(m, s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) seq_putc(m, '\n');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) static int slab_show(struct seq_file *m, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) struct kmem_cache *s = list_entry(p, struct kmem_cache, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) if (p == slab_caches.next)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) print_slabinfo_header(m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) cache_show(s, m);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 999) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) void dump_unreclaimable_slab(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) struct kmem_cache *s, *s2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) struct slabinfo sinfo;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) * Here acquiring slab_mutex is risky since we don't prefer to get
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) * sleep in oom path. But, without mutex hold, it may introduce a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) * risk of crash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) * Use mutex_trylock to protect the list traverse, dump nothing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) * without acquiring the mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) if (!mutex_trylock(&slab_mutex)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) pr_warn("excessive unreclaimable slab but cannot dump stats\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) pr_info("Unreclaimable slab info:\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) pr_info("Name Used Total\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) list_for_each_entry_safe(s, s2, &slab_caches, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) if (s->flags & SLAB_RECLAIM_ACCOUNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) get_slabinfo(s, &sinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) if (sinfo.num_objs > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) pr_info("%-17s %10luKB %10luKB\n", s->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) (sinfo.active_objs * s->size) / 1024,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) (sinfo.num_objs * s->size) / 1024);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) mutex_unlock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) #if defined(CONFIG_MEMCG_KMEM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) int memcg_slab_show(struct seq_file *m, void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) * Deprecated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) * Please, take a look at tools/cgroup/slabinfo.py .
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) * slabinfo_op - iterator that generates /proc/slabinfo
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) * Output layout:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) * cache-name
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) * num-active-objs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) * total-objs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) * object size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) * num-active-slabs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) * total-slabs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) * num-pages-per-slab
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) * + further values on SMP and with statistics enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) static const struct seq_operations slabinfo_op = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) .start = slab_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) .next = slab_next,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) .stop = slab_stop,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) .show = slab_show,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) static int slabinfo_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) return seq_open(file, &slabinfo_op);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) static const struct proc_ops slabinfo_proc_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) .proc_flags = PROC_ENTRY_PERMANENT,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) .proc_open = slabinfo_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) .proc_read = seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) .proc_write = slabinfo_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) .proc_lseek = seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) .proc_release = seq_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) static int __init slab_proc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) proc_create("slabinfo", SLABINFO_RIGHTS, NULL, &slabinfo_proc_ops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) module_init(slab_proc_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) #endif /* CONFIG_SLAB || CONFIG_SLUB_DEBUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) static __always_inline void *__do_krealloc(const void *p, size_t new_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) void *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) size_t ks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) /* Don't use instrumented ksize to allow precise KASAN poisoning. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) if (likely(!ZERO_OR_NULL_PTR(p))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) if (!kasan_check_byte(p))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) ks = kfence_ksize(p) ?: __ksize(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) ks = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) /* If the object still fits, repoison it precisely. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) if (ks >= new_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) p = kasan_krealloc((void *)p, new_size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) return (void *)p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) ret = kmalloc_track_caller(new_size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) if (ret && p) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) /* Disable KASAN checks as the object's redzone is accessed. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) kasan_disable_current();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) memcpy(ret, kasan_reset_tag(p), ks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) kasan_enable_current();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) * krealloc - reallocate memory. The contents will remain unchanged.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) * @p: object to reallocate memory for.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) * @new_size: how many bytes of memory are required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) * @flags: the type of memory to allocate.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) * The contents of the object pointed to are preserved up to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) * lesser of the new and old sizes. If @p is %NULL, krealloc()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) * behaves exactly like kmalloc(). If @new_size is 0 and @p is not a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) * %NULL pointer, the object pointed to is freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) * Return: pointer to the allocated memory or %NULL in case of error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) void *krealloc(const void *p, size_t new_size, gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) void *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) if (unlikely(!new_size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) return ZERO_SIZE_PTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) ret = __do_krealloc(p, new_size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) if (ret && kasan_reset_tag(p) != kasan_reset_tag(ret))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) kfree(p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) EXPORT_SYMBOL(krealloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) * kfree_sensitive - Clear sensitive information in memory before freeing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) * @p: object to free memory of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) * The memory of the object @p points to is zeroed before freed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) * If @p is %NULL, kfree_sensitive() does nothing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) * Note: this function zeroes the whole allocated buffer which can be a good
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) * deal bigger than the requested buffer size passed to kmalloc(). So be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) * careful when using this function in performance sensitive code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) void kfree_sensitive(const void *p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) size_t ks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) void *mem = (void *)p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) ks = ksize(mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) if (ks)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) memzero_explicit(mem, ks);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) kfree(mem);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) EXPORT_SYMBOL(kfree_sensitive);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) * ksize - get the actual amount of memory allocated for a given object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) * @objp: Pointer to the object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) * kmalloc may internally round up allocations and return more memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) * than requested. ksize() can be used to determine the actual amount of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) * memory allocated. The caller may use this additional memory, even though
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) * a smaller amount of memory was initially specified with the kmalloc call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) * The caller must guarantee that objp points to a valid object previously
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) * allocated with either kmalloc() or kmem_cache_alloc(). The object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) * must not be freed during the duration of the call.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) * Return: size of the actual memory used by @objp in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) size_t ksize(const void *objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) * We need to first check that the pointer to the object is valid, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) * only then unpoison the memory. The report printed from ksize() is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) * more useful, then when it's printed later when the behaviour could
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) * be undefined due to a potential use-after-free or double-free.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) * We use kasan_check_byte(), which is supported for the hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) * tag-based KASAN mode, unlike kasan_check_read/write().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) * If the pointed to memory is invalid, we return 0 to avoid users of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) * ksize() writing to and potentially corrupting the memory region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) * We want to perform the check before __ksize(), to avoid potentially
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) * crashing in __ksize() due to accessing invalid metadata.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) if (unlikely(ZERO_OR_NULL_PTR(objp)) || !kasan_check_byte(objp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) size = kfence_ksize(objp) ?: __ksize(objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) * We assume that ksize callers could use whole allocated area,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) * so we need to unpoison this area.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) kasan_unpoison_range(objp, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) EXPORT_SYMBOL(ksize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) /* Tracepoints definitions. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) EXPORT_TRACEPOINT_SYMBOL(kmalloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) EXPORT_TRACEPOINT_SYMBOL(kmalloc_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) EXPORT_TRACEPOINT_SYMBOL(kfree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) int should_failslab(struct kmem_cache *s, gfp_t gfpflags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) if (__should_failslab(s, gfpflags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) ALLOW_ERROR_INJECTION(should_failslab, ERRNO);