^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) * linux/mm/slab.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * Written by Mark Hemment, 1996/97.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * (markhe@nextd.demon.co.uk)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * kmem_cache_destroy() + some cleanup - 1999 Andrea Arcangeli
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Major cleanup, different bufctl logic, per-cpu arrays
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * (c) 2000 Manfred Spraul
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * Cleanup, make the head arrays unconditional, preparation for NUMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * (c) 2002 Manfred Spraul
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * An implementation of the Slab Allocator as described in outline in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * UNIX Internals: The New Frontiers by Uresh Vahalia
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * Pub: Prentice Hall ISBN 0-13-101908-2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * or with a little more detail in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * The Slab Allocator: An Object-Caching Kernel Memory Allocator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) * Jeff Bonwick (Sun Microsystems).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * Presented at: USENIX Summer 1994 Technical Conference
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) * The memory is organized in caches, one cache for each object type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * (e.g. inode_cache, dentry_cache, buffer_head, vm_area_struct)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * Each cache consists out of many slabs (they are small (usually one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * page long) and always contiguous), and each slab contains multiple
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * initialized objects.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * This means, that your constructor is used only for newly allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * slabs and you must pass objects with the same initializations to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) * kmem_cache_free.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) * Each cache can only support one memory type (GFP_DMA, GFP_HIGHMEM,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) * normal). If you need a special memory type, then must create a new
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) * cache for that memory type.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) * In order to reduce fragmentation, the slabs are sorted in 3 groups:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) * full slabs with 0 free objects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) * partial slabs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) * empty slabs with no allocated objects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) * If partial slabs exist, then new allocations come from these slabs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) * otherwise from empty slabs or new slabs are allocated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * kmem_cache_destroy() CAN CRASH if you try to allocate from the cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) * during kmem_cache_destroy(). The caller must prevent concurrent allocs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) * Each cache has a short per-cpu head array, most allocs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) * and frees go into that array, and if that array overflows, then 1/2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * of the entries in the array are given back into the global cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * The head array is strictly LIFO and should improve the cache hit rates.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) * On SMP, it additionally reduces the spinlock operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) * The c_cpuarray may not be read with enabled local interrupts -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) * it's changed with a smp_call_function().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * SMP synchronization:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * constructors and destructors are called without any locking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * Several members in struct kmem_cache and struct slab never change, they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * are accessed without any locking.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) * The per-cpu arrays are never accessed from the wrong cpu, no locking,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) * and local interrupts are disabled so slab code is preempt-safe.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) * The non-constant members are protected with a per-cache irq spinlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) * Many thanks to Mark Hemment, who wrote another per-cpu slab patch
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) * in 2000 - many ideas in the current implementation are derived from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) * his patch.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) * Further notes from the original documentation:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) * 11 April '97. Started multi-threading - markhe
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) * The global cache-chain is protected by the mutex 'slab_mutex'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) * The sem is only needed when accessing/extending the cache-chain, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) * can never happen inside an interrupt (kmem_cache_create(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) * kmem_cache_shrink() and kmem_cache_reap()).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) * At present, each engine can be growing a cache. This should be blocked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) * 15 March 2005. NUMA slab allocator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * Shai Fultheim <shai@scalex86.org>.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) * Shobhit Dayal <shobhit@calsoftinc.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * Alok N Kataria <alokk@calsoftinc.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * Christoph Lameter <christoph@lameter.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * Modified the slab allocator to be node aware on NUMA systems.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) * Each node has its own list of partial, free and full slabs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) * All object allocations for a node occur from node specific slab lists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) #include <linux/poison.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) #include <linux/swap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) #include <linux/cache.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) #include <linux/interrupt.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) #include <linux/cpuset.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #include <linux/proc_fs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) #include <linux/kallsyms.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) #include <linux/kfence.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) #include <linux/cpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) #include <linux/sysctl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) #include <linux/rcupdate.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) #include <linux/nodemask.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) #include <linux/kmemleak.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) #include <linux/mempolicy.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) #include <linux/fault-inject.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) #include <linux/rtmutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) #include <linux/reciprocal_div.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) #include <linux/debugobjects.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) #include <linux/memory.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) #include <linux/prefetch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) #include <linux/sched/task_stack.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) #include <net/sock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) #include <asm/cacheflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) #include <asm/tlbflush.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) #include <asm/page.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) #include <trace/events/kmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) #include "internal.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) #include "slab.h"
^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) * DEBUG - 1 for kmem_cache_create() to honour; SLAB_RED_ZONE & SLAB_POISON.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * 0 for faster, smaller code (especially in the critical paths).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * STATS - 1 to collect stats for /proc/slabinfo.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * 0 for faster, smaller code (especially in the critical paths).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) #ifdef CONFIG_DEBUG_SLAB
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) #define DEBUG 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) #define STATS 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) #define FORCED_DEBUG 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) #define DEBUG 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) #define STATS 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) #define FORCED_DEBUG 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) /* Shouldn't this be in a header file somewhere? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) #define BYTES_PER_WORD sizeof(void *)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) #define REDZONE_ALIGN max(BYTES_PER_WORD, __alignof__(unsigned long long))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) #ifndef ARCH_KMALLOC_FLAGS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) #define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) #define FREELIST_BYTE_INDEX (((PAGE_SIZE >> BITS_PER_BYTE) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) <= SLAB_OBJ_MIN_SIZE) ? 1 : 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) #if FREELIST_BYTE_INDEX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) typedef unsigned char freelist_idx_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) typedef unsigned short freelist_idx_t;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) #define SLAB_OBJ_MAX_NUM ((1 << sizeof(freelist_idx_t) * BITS_PER_BYTE) - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) * struct array_cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) * Purpose:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * - LIFO ordering, to hand out cache-warm objects from _alloc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) * - reduce the number of linked list operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * - reduce spinlock operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * The limit is stored in the per-cpu structure to reduce the data cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * footprint.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) struct array_cache {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) unsigned int avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) unsigned int limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) unsigned int batchcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) unsigned int touched;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) void *entry[]; /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) * Must have this definition in here for the proper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) * alignment of array_cache. Also simplifies accessing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) * the entries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) struct alien_cache {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) spinlock_t lock;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) struct array_cache ac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) * Need this for bootstrapping a per node allocator.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) #define NUM_INIT_LISTS (2 * MAX_NUMNODES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) static struct kmem_cache_node __initdata init_kmem_cache_node[NUM_INIT_LISTS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) #define CACHE_CACHE 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) #define SIZE_NODE (MAX_NUMNODES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) static int drain_freelist(struct kmem_cache *cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) struct kmem_cache_node *n, int tofree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) static void free_block(struct kmem_cache *cachep, void **objpp, int len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) int node, struct list_head *list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static void slabs_destroy(struct kmem_cache *cachep, struct list_head *list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) static void cache_reap(struct work_struct *unused);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) static inline void fixup_objfreelist_debug(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) void **list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) static inline void fixup_slab_list(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct kmem_cache_node *n, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) void **list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) static int slab_early_init = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) #define INDEX_NODE kmalloc_index(sizeof(struct kmem_cache_node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) static void kmem_cache_node_init(struct kmem_cache_node *parent)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) INIT_LIST_HEAD(&parent->slabs_full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) INIT_LIST_HEAD(&parent->slabs_partial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) INIT_LIST_HEAD(&parent->slabs_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) parent->total_slabs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) parent->free_slabs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) parent->shared = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) parent->alien = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) parent->colour_next = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) spin_lock_init(&parent->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) parent->free_objects = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) parent->free_touched = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) #define MAKE_LIST(cachep, listp, slab, nodeid) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) INIT_LIST_HEAD(listp); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) list_splice(&get_node(cachep, nodeid)->slab, listp); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) #define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) #define CFLGS_OBJFREELIST_SLAB ((slab_flags_t __force)0x40000000U)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) #define CFLGS_OFF_SLAB ((slab_flags_t __force)0x80000000U)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) #define OBJFREELIST_SLAB(x) ((x)->flags & CFLGS_OBJFREELIST_SLAB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) #define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) #define BATCHREFILL_LIMIT 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) * Optimization question: fewer reaps means less probability for unnessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) * cpucache drain/refill cycles.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) * OTOH the cpuarrays can contain lots of objects,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) * which could lock up otherwise freeable slabs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) #define REAPTIMEOUT_AC (2*HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) #define REAPTIMEOUT_NODE (4*HZ)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) #if STATS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) #define STATS_INC_ACTIVE(x) ((x)->num_active++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) #define STATS_DEC_ACTIVE(x) ((x)->num_active--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) #define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) #define STATS_INC_GROWN(x) ((x)->grown++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) #define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) #define STATS_SET_HIGH(x) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if ((x)->num_active > (x)->high_mark) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) (x)->high_mark = (x)->num_active; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) #define STATS_INC_ERR(x) ((x)->errors++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) #define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) #define STATS_INC_NODEFREES(x) ((x)->node_frees++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) #define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) #define STATS_SET_FREEABLE(x, i) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) do { \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) if ((x)->max_freeable < i) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) (x)->max_freeable = i; \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) #define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) #define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) #define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) #define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) #define STATS_INC_ACTIVE(x) do { } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) #define STATS_DEC_ACTIVE(x) do { } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) #define STATS_INC_ALLOCED(x) do { } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) #define STATS_INC_GROWN(x) do { } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) #define STATS_ADD_REAPED(x,y) do { (void)(y); } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) #define STATS_SET_HIGH(x) do { } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) #define STATS_INC_ERR(x) do { } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) #define STATS_INC_NODEALLOCS(x) do { } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) #define STATS_INC_NODEFREES(x) do { } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) #define STATS_INC_ACOVERFLOW(x) do { } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) #define STATS_SET_FREEABLE(x, i) do { } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) #define STATS_INC_ALLOCHIT(x) do { } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) #define STATS_INC_ALLOCMISS(x) do { } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) #define STATS_INC_FREEHIT(x) do { } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) #define STATS_INC_FREEMISS(x) do { } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) #if DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) * memory layout of objects:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) * 0 : objp
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) * the end of an object is aligned with the end of the real
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) * allocation. Catches writes behind the end of the allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) * redzone word.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) * cachep->obj_offset: The real object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) * cachep->size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) * cachep->size - 1* BYTES_PER_WORD: last caller address
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) * [BYTES_PER_WORD long]
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) static int obj_offset(struct kmem_cache *cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) return cachep->obj_offset;
^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) static unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) return (unsigned long long*) (objp + obj_offset(cachep) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) sizeof(unsigned long long));
^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) static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) if (cachep->flags & SLAB_STORE_USER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) return (unsigned long long *)(objp + cachep->size -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) sizeof(unsigned long long) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) REDZONE_ALIGN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) return (unsigned long long *) (objp + cachep->size -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) sizeof(unsigned long long));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) static void **dbg_userword(struct kmem_cache *cachep, void *objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) BUG_ON(!(cachep->flags & SLAB_STORE_USER));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) return (void **)(objp + cachep->size - BYTES_PER_WORD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) #define obj_offset(x) 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) #define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) #define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) #define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * Do not go above this order unless 0 objects fit into the slab or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * overridden on the command line.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) #define SLAB_MAX_ORDER_HI 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) #define SLAB_MAX_ORDER_LO 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) static int slab_max_order = SLAB_MAX_ORDER_LO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) static bool slab_max_order_set __initdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) static inline void *index_to_obj(struct kmem_cache *cache, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) unsigned int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) return page->s_mem + cache->size * idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) #define BOOT_CPUCACHE_ENTRIES 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) /* internal cache of cache description objs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) static struct kmem_cache kmem_cache_boot = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) .batchcount = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) .limit = BOOT_CPUCACHE_ENTRIES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) .shared = 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) .size = sizeof(struct kmem_cache),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) .name = "kmem_cache",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) static DEFINE_PER_CPU(struct delayed_work, slab_reap_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) return this_cpu_ptr(cachep->cpu_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) * Calculate the number of objects and left-over bytes for a given buffer size.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) static unsigned int cache_estimate(unsigned long gfporder, size_t buffer_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) slab_flags_t flags, size_t *left_over)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) unsigned int num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) size_t slab_size = PAGE_SIZE << gfporder;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) * The slab management structure can be either off the slab or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) * on it. For the latter case, the memory allocated for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) * slab is used for:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) * - @buffer_size bytes for each object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) * - One freelist_idx_t for each object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) * We don't need to consider alignment of freelist because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) * freelist will be at the end of slab page. The objects will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) * at the correct alignment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) * If the slab management structure is off the slab, then the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) * alignment will already be calculated into the size. Because
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) * the slabs are all pages aligned, the objects will be at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) * correct alignment when allocated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) if (flags & (CFLGS_OBJFREELIST_SLAB | CFLGS_OFF_SLAB)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) num = slab_size / buffer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) *left_over = slab_size % buffer_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) num = slab_size / (buffer_size + sizeof(freelist_idx_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) *left_over = slab_size %
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) (buffer_size + sizeof(freelist_idx_t));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) return num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436) #if DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) #define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) static void __slab_error(const char *function, struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) char *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) pr_err("slab error in %s(): cache `%s': %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) function, cachep->name, msg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) dump_stack();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) * By default on NUMA we use alien caches to stage the freeing of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) * objects allocated from other nodes. This causes massive memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452) * inefficiencies when using fake NUMA setup to split memory into a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) * large number of small nodes, so it can be disabled on the command
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) * line
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457) static int use_alien_caches __read_mostly = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) static int __init noaliencache_setup(char *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) use_alien_caches = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) __setup("noaliencache", noaliencache_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) static int __init slab_max_order_setup(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467) get_option(&str, &slab_max_order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) slab_max_order = slab_max_order < 0 ? 0 :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) min(slab_max_order, MAX_ORDER - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) slab_max_order_set = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) __setup("slab_max_order=", slab_max_order_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) #ifdef CONFIG_NUMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) * Special reaping functions for NUMA systems called from cache_reap().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479) * These take care of doing round robin flushing of alien caches (containing
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) * objects freed on different nodes from which they were allocated) and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) * flushing of remote pcps by calling drain_node_pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) static DEFINE_PER_CPU(unsigned long, slab_reap_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485) static void init_reap_node(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) per_cpu(slab_reap_node, cpu) = next_node_in(cpu_to_mem(cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488) node_online_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491) static void next_reap_node(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) int node = __this_cpu_read(slab_reap_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) node = next_node_in(node, node_online_map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) __this_cpu_write(slab_reap_node, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) #define init_reap_node(cpu) do { } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) #define next_reap_node(void) do { } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506) * via the workqueue/eventd.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) * Add the CPU number into the expiration time to minimize the possibility of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) * the CPUs getting into lockstep and contending for the global cache chain
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) * lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511) static void start_cpu_timer(int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) struct delayed_work *reap_work = &per_cpu(slab_reap_work, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) if (reap_work->work.func == NULL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) init_reap_node(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) INIT_DEFERRABLE_WORK(reap_work, cache_reap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) schedule_delayed_work_on(cpu, reap_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519) __round_jiffies_relative(HZ, cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) static void init_arraycache(struct array_cache *ac, int limit, int batch)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) if (ac) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) ac->avail = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527) ac->limit = limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) ac->batchcount = batch;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) ac->touched = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) static struct array_cache *alloc_arraycache(int node, int entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) int batchcount, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) size_t memsize = sizeof(void *) * entries + sizeof(struct array_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) struct array_cache *ac = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) ac = kmalloc_node(memsize, gfp, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) * The array_cache structures contain pointers to free object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) * However, when such objects are allocated or transferred to another
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) * cache the pointers are not cleared and they could be counted as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) * valid references during a kmemleak scan. Therefore, kmemleak must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) * not scan such objects.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) kmemleak_no_scan(ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548) init_arraycache(ac, entries, batchcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) return ac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) static noinline void cache_free_pfmemalloc(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) struct page *page, void *objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) struct kmem_cache_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) int page_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) page_node = page_to_nid(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) n = get_node(cachep, page_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562) spin_lock(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) free_block(cachep, &objp, 1, page_node, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) spin_unlock(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) slabs_destroy(cachep, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) * Transfer objects in one arraycache to another.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) * Locking must be handled by the caller.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) * Return the number of entries transferred.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) static int transfer_objects(struct array_cache *to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576) struct array_cache *from, unsigned int max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) /* Figure out how many entries to transfer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) int nr = min3(from->avail, max, to->limit - to->avail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) if (!nr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) memcpy(to->entry + to->avail, from->entry + from->avail -nr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585) sizeof(void *) *nr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587) from->avail -= nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) to->avail += nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) return nr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) /* &alien->lock must be held by alien callers. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) static __always_inline void __free_one(struct array_cache *ac, void *objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595) /* Avoid trivial double-free. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) if (IS_ENABLED(CONFIG_SLAB_FREELIST_HARDENED) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) WARN_ON_ONCE(ac->avail > 0 && ac->entry[ac->avail - 1] == objp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) ac->entry[ac->avail++] = objp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602) #ifndef CONFIG_NUMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) #define drain_alien_cache(cachep, alien) do { } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) #define reap_alien(cachep, n) do { } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607) static inline struct alien_cache **alloc_alien_cache(int node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) int limit, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) static inline void free_alien_cache(struct alien_cache **ac_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 621)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 622) static inline void *alternate_node_alloc(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 623) gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 624) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 625) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 626) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 628) static inline void *____cache_alloc_node(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 629) gfp_t flags, int nodeid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 630) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 631) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 633)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 634) static inline gfp_t gfp_exact_node(gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 635) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 636) return flags & ~__GFP_NOFAIL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 637) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 638)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 639) #else /* CONFIG_NUMA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 641) static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 642) static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 644) static struct alien_cache *__alloc_alien_cache(int node, int entries,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 645) int batch, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 646) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 647) size_t memsize = sizeof(void *) * entries + sizeof(struct alien_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 648) struct alien_cache *alc = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 650) alc = kmalloc_node(memsize, gfp, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 651) if (alc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 652) kmemleak_no_scan(alc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 653) init_arraycache(&alc->ac, entries, batch);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 654) spin_lock_init(&alc->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 655) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 656) return alc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 657) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 658)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 659) static struct alien_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 660) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 661) struct alien_cache **alc_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 662) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 663)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 664) if (limit > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 665) limit = 12;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 666) alc_ptr = kcalloc_node(nr_node_ids, sizeof(void *), gfp, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 667) if (!alc_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 668) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 670) for_each_node(i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 671) if (i == node || !node_online(i))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 672) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 673) alc_ptr[i] = __alloc_alien_cache(node, limit, 0xbaadf00d, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 674) if (!alc_ptr[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 675) for (i--; i >= 0; i--)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 676) kfree(alc_ptr[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 677) kfree(alc_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 678) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 679) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 680) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 681) return alc_ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 684) static void free_alien_cache(struct alien_cache **alc_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 686) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 688) if (!alc_ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 689) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 690) for_each_node(i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 691) kfree(alc_ptr[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 692) kfree(alc_ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 693) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 694)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 695) static void __drain_alien_cache(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 696) struct array_cache *ac, int node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 697) struct list_head *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 698) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 699) struct kmem_cache_node *n = get_node(cachep, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 700)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 701) if (ac->avail) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 702) spin_lock(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 703) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 704) * Stuff objects into the remote nodes shared array first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 705) * That way we could avoid the overhead of putting the objects
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 706) * into the free lists and getting them back later.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 707) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 708) if (n->shared)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 709) transfer_objects(n->shared, ac, ac->limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 710)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 711) free_block(cachep, ac->entry, ac->avail, node, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 712) ac->avail = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 713) spin_unlock(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 714) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 715) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 716)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 717) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 718) * Called from cache_reap() to regularly drain alien caches round robin.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 719) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 720) static void reap_alien(struct kmem_cache *cachep, struct kmem_cache_node *n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 721) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 722) int node = __this_cpu_read(slab_reap_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 724) if (n->alien) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 725) struct alien_cache *alc = n->alien[node];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 726) struct array_cache *ac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 728) if (alc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 729) ac = &alc->ac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 730) if (ac->avail && spin_trylock_irq(&alc->lock)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 731) LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 733) __drain_alien_cache(cachep, ac, node, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 734) spin_unlock_irq(&alc->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 735) slabs_destroy(cachep, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 737) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 738) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 740)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 741) static void drain_alien_cache(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 742) struct alien_cache **alien)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 743) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 744) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 745) struct alien_cache *alc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 746) struct array_cache *ac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 747) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 748)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 749) for_each_online_node(i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 750) alc = alien[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 751) if (alc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 752) LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 753)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 754) ac = &alc->ac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 755) spin_lock_irqsave(&alc->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 756) __drain_alien_cache(cachep, ac, i, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 757) spin_unlock_irqrestore(&alc->lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 758) slabs_destroy(cachep, &list);
^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) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 762)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 763) static int __cache_free_alien(struct kmem_cache *cachep, void *objp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 764) int node, int page_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 765) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 766) struct kmem_cache_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 767) struct alien_cache *alien = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 768) struct array_cache *ac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 769) LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 771) n = get_node(cachep, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 772) STATS_INC_NODEFREES(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 773) if (n->alien && n->alien[page_node]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 774) alien = n->alien[page_node];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 775) ac = &alien->ac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 776) spin_lock(&alien->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 777) if (unlikely(ac->avail == ac->limit)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 778) STATS_INC_ACOVERFLOW(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 779) __drain_alien_cache(cachep, ac, page_node, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 781) __free_one(ac, objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 782) spin_unlock(&alien->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 783) slabs_destroy(cachep, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 784) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 785) n = get_node(cachep, page_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 786) spin_lock(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 787) free_block(cachep, &objp, 1, page_node, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 788) spin_unlock(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 789) slabs_destroy(cachep, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 790) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 791) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 792) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 794) static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 795) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 796) int page_node = page_to_nid(virt_to_page(objp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 797) int node = numa_mem_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 798) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 799) * Make sure we are not freeing a object from another node to the array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 800) * cache on this cpu.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 801) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 802) if (likely(node == page_node))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 803) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 804)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 805) return __cache_free_alien(cachep, objp, node, page_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 806) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 808) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 809) * Construct gfp mask to allocate from a specific node but do not reclaim or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 810) * warn about failures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 811) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 812) static inline gfp_t gfp_exact_node(gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 813) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 814) return (flags | __GFP_THISNODE | __GFP_NOWARN) & ~(__GFP_RECLAIM|__GFP_NOFAIL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 815) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 816) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 817)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 818) static int init_cache_node(struct kmem_cache *cachep, int node, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 819) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 820) struct kmem_cache_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 822) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 823) * Set up the kmem_cache_node for cpu before we can
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 824) * begin anything. Make sure some other cpu on this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 825) * node has not already allocated this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 826) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 827) n = get_node(cachep, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 828) if (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 829) spin_lock_irq(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 830) n->free_limit = (1 + nr_cpus_node(node)) * cachep->batchcount +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 831) cachep->num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 832) spin_unlock_irq(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 833)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 834) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 835) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 836)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 837) n = kmalloc_node(sizeof(struct kmem_cache_node), gfp, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 838) if (!n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 839) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 841) kmem_cache_node_init(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 842) n->next_reap = jiffies + REAPTIMEOUT_NODE +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 843) ((unsigned long)cachep) % REAPTIMEOUT_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 845) n->free_limit =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 846) (1 + nr_cpus_node(node)) * cachep->batchcount + cachep->num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 848) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 849) * The kmem_cache_nodes don't come and go as CPUs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 850) * come and go. slab_mutex is sufficient
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 851) * protection here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 852) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 853) cachep->node[node] = n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 855) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 856) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 857)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 858) #if (defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)) || defined(CONFIG_SMP)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 859) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 860) * Allocates and initializes node for a node on each slab cache, used for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 861) * either memory or cpu hotplug. If memory is being hot-added, the kmem_cache_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 862) * will be allocated off-node since memory is not yet online for the new node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 863) * When hotplugging memory or a cpu, existing node are not replaced if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 864) * already in use.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 865) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 866) * Must hold slab_mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 867) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 868) static int init_cache_node_node(int node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 869) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 870) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 871) struct kmem_cache *cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 872)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 873) list_for_each_entry(cachep, &slab_caches, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 874) ret = init_cache_node(cachep, node, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 875) if (ret)
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 879) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 880) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 881) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 882)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 883) static int setup_kmem_cache_node(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 884) int node, gfp_t gfp, bool force_change)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 885) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 886) int ret = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 887) struct kmem_cache_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 888) struct array_cache *old_shared = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 889) struct array_cache *new_shared = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 890) struct alien_cache **new_alien = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 891) LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 893) if (use_alien_caches) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 894) new_alien = alloc_alien_cache(node, cachep->limit, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 895) if (!new_alien)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 896) goto fail;
^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) if (cachep->shared) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 900) new_shared = alloc_arraycache(node,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 901) cachep->shared * cachep->batchcount, 0xbaadf00d, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 902) if (!new_shared)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 903) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 904) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 905)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 906) ret = init_cache_node(cachep, node, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 907) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 908) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 910) n = get_node(cachep, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 911) spin_lock_irq(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 912) if (n->shared && force_change) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 913) free_block(cachep, n->shared->entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 914) n->shared->avail, node, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 915) n->shared->avail = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 916) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 917)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 918) if (!n->shared || force_change) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 919) old_shared = n->shared;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 920) n->shared = new_shared;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 921) new_shared = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 922) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 923)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 924) if (!n->alien) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 925) n->alien = new_alien;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 926) new_alien = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 927) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 928)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 929) spin_unlock_irq(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 930) slabs_destroy(cachep, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 931)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 932) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 933) * To protect lockless access to n->shared during irq disabled context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 934) * If n->shared isn't NULL in irq disabled context, accessing to it is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 935) * guaranteed to be valid until irq is re-enabled, because it will be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 936) * freed after synchronize_rcu().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 937) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 938) if (old_shared && force_change)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 939) synchronize_rcu();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 940)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 941) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 942) kfree(old_shared);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 943) kfree(new_shared);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 944) free_alien_cache(new_alien);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 946) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 947) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 949) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 951) static void cpuup_canceled(long cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 952) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 953) struct kmem_cache *cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 954) struct kmem_cache_node *n = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 955) int node = cpu_to_mem(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 956) const struct cpumask *mask = cpumask_of_node(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 958) list_for_each_entry(cachep, &slab_caches, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 959) struct array_cache *nc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 960) struct array_cache *shared;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 961) struct alien_cache **alien;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 962) LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 963)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 964) n = get_node(cachep, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 965) if (!n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 966) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 967)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 968) spin_lock_irq(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 969)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 970) /* Free limit for this kmem_cache_node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 971) n->free_limit -= cachep->batchcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 972)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 973) /* cpu is dead; no one can alloc from it. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 974) nc = per_cpu_ptr(cachep->cpu_cache, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 975) free_block(cachep, nc->entry, nc->avail, node, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 976) nc->avail = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 977)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 978) if (!cpumask_empty(mask)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 979) spin_unlock_irq(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 980) goto free_slab;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 981) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 983) shared = n->shared;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 984) if (shared) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 985) free_block(cachep, shared->entry,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 986) shared->avail, node, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 987) n->shared = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 988) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 989)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 990) alien = n->alien;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 991) n->alien = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 992)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 993) spin_unlock_irq(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 994)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 995) kfree(shared);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 996) if (alien) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 997) drain_alien_cache(cachep, alien);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 998) free_alien_cache(alien);
^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) free_slab:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) slabs_destroy(cachep, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) * In the previous loop, all the objects were freed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) * the respective cache's slabs, now we can go ahead and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) * shrink each nodelist to its limit.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) list_for_each_entry(cachep, &slab_caches, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) n = get_node(cachep, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) if (!n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) drain_freelist(cachep, n, INT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) static int cpuup_prepare(long cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) struct kmem_cache *cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) int node = cpu_to_mem(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) * We need to do this right in the beginning since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) * alloc_arraycache's are going to use this list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) * kmalloc_node allows us to add the slab to the right
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) * kmem_cache_node and not this cpu's kmem_cache_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) err = init_cache_node_node(node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) * Now we can go ahead with allocating the shared arrays and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) * array caches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) list_for_each_entry(cachep, &slab_caches, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) err = setup_kmem_cache_node(cachep, node, GFP_KERNEL, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) goto bad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) bad:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) cpuup_canceled(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) int slab_prepare_cpu(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) mutex_lock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) err = cpuup_prepare(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) mutex_unlock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) * This is called for a failed online attempt and for a successful
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) * offline.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) * Even if all the cpus of a node are down, we don't free the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) * kmem_cache_node of any cache. This to avoid a race between cpu_down, and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) * a kmalloc allocation from another cpu for memory from the node of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) * the cpu going down. The kmem_cache_node structure is usually allocated from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) * kmem_cache_create() and gets destroyed at kmem_cache_destroy().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) int slab_dead_cpu(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) mutex_lock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) cpuup_canceled(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) mutex_unlock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) static int slab_online_cpu(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) start_cpu_timer(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) static int slab_offline_cpu(unsigned int cpu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) * Shutdown cache reaper. Note that the slab_mutex is held so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) * that if cache_reap() is invoked it cannot do anything
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) * expensive but will only modify reap_work and reschedule the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) * timer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) /* Now the cache_reaper is guaranteed to be not running. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) per_cpu(slab_reap_work, cpu).work.func = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) * Drains freelist for a node on each slab cache, used for memory hot-remove.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) * Returns -EBUSY if all objects cannot be drained so that the node is not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) * removed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) * Must hold slab_mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) static int __meminit drain_cache_node_node(int node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) struct kmem_cache *cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) list_for_each_entry(cachep, &slab_caches, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) struct kmem_cache_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) n = get_node(cachep, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) if (!n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) drain_freelist(cachep, n, INT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) if (!list_empty(&n->slabs_full) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) !list_empty(&n->slabs_partial)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) ret = -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) static int __meminit slab_memory_callback(struct notifier_block *self,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) unsigned long action, void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) struct memory_notify *mnb = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) int nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) nid = mnb->status_change_nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) if (nid < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) switch (action) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) case MEM_GOING_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) mutex_lock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) ret = init_cache_node_node(nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) mutex_unlock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) case MEM_GOING_OFFLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) mutex_lock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) ret = drain_cache_node_node(nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) mutex_unlock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) case MEM_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) case MEM_OFFLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) case MEM_CANCEL_ONLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) case MEM_CANCEL_OFFLINE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) return notifier_from_errno(ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) #endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) * swap the static kmem_cache_node with kmalloced memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) static void __init init_list(struct kmem_cache *cachep, struct kmem_cache_node *list,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) int nodeid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) struct kmem_cache_node *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) ptr = kmalloc_node(sizeof(struct kmem_cache_node), GFP_NOWAIT, nodeid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) BUG_ON(!ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) memcpy(ptr, list, sizeof(struct kmem_cache_node));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) * Do not assume that spinlocks can be initialized via memcpy:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) spin_lock_init(&ptr->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) MAKE_ALL_LISTS(cachep, ptr, nodeid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) cachep->node[nodeid] = ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) * For setting up all the kmem_cache_node for cache whose buffer_size is same as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) * size of kmem_cache_node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) static void __init set_up_node(struct kmem_cache *cachep, int index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) int node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) for_each_online_node(node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) cachep->node[node] = &init_kmem_cache_node[index + node];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) cachep->node[node]->next_reap = jiffies +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) REAPTIMEOUT_NODE +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) ((unsigned long)cachep) % REAPTIMEOUT_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) * Initialisation. Called after the page allocator have been initialised and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) * before smp_init().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) void __init kmem_cache_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) kmem_cache = &kmem_cache_boot;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) if (!IS_ENABLED(CONFIG_NUMA) || num_possible_nodes() == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) use_alien_caches = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) for (i = 0; i < NUM_INIT_LISTS; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) kmem_cache_node_init(&init_kmem_cache_node[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) * Fragmentation resistance on low memory - only use bigger
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) * page orders on machines with more than 32MB of memory if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) * not overridden on the command line.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) if (!slab_max_order_set && totalram_pages() > (32 << 20) >> PAGE_SHIFT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) slab_max_order = SLAB_MAX_ORDER_HI;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) /* Bootstrap is tricky, because several objects are allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) * from caches that do not exist yet:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) * 1) initialize the kmem_cache cache: it contains the struct
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) * kmem_cache structures of all caches, except kmem_cache itself:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) * kmem_cache is statically allocated.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) * Initially an __init data area is used for the head array and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) * kmem_cache_node structures, it's replaced with a kmalloc allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) * array at the end of the bootstrap.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) * 2) Create the first kmalloc cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) * The struct kmem_cache for the new cache is allocated normally.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) * An __init data area is used for the head array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) * 3) Create the remaining kmalloc caches, with minimally sized
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) * head arrays.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) * 4) Replace the __init data head arrays for kmem_cache and the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) * kmalloc cache with kmalloc allocated arrays.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) * 5) Replace the __init data for kmem_cache_node for kmem_cache and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) * the other cache's with kmalloc allocated memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) * 6) Resize the head arrays of the kmalloc caches to their final sizes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) /* 1) create the kmem_cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) create_boot_cache(kmem_cache, "kmem_cache",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) offsetof(struct kmem_cache, node) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) nr_node_ids * sizeof(struct kmem_cache_node *),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) SLAB_HWCACHE_ALIGN, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) list_add(&kmem_cache->list, &slab_caches);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) slab_state = PARTIAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) * Initialize the caches that provide memory for the kmem_cache_node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) * structures first. Without this, further allocations will bug.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) kmalloc_caches[KMALLOC_NORMAL][INDEX_NODE] = create_kmalloc_cache(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) kmalloc_info[INDEX_NODE].name[KMALLOC_NORMAL],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) kmalloc_info[INDEX_NODE].size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) ARCH_KMALLOC_FLAGS, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) kmalloc_info[INDEX_NODE].size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) slab_state = PARTIAL_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) setup_kmalloc_cache_index_table();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) slab_early_init = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) /* 5) Replace the bootstrap kmem_cache_node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) int nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) for_each_online_node(nid) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) init_list(kmem_cache, &init_kmem_cache_node[CACHE_CACHE + nid], nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) init_list(kmalloc_caches[KMALLOC_NORMAL][INDEX_NODE],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) &init_kmem_cache_node[SIZE_NODE + nid], nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) create_kmalloc_caches(ARCH_KMALLOC_FLAGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) void __init kmem_cache_init_late(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) struct kmem_cache *cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) /* 6) resize the head arrays to their final sizes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) mutex_lock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) list_for_each_entry(cachep, &slab_caches, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) if (enable_cpucache(cachep, GFP_NOWAIT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) mutex_unlock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) /* Done! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) slab_state = FULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) #ifdef CONFIG_NUMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) * Register a memory hotplug callback that initializes and frees
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) * node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) * The reap timers are started later, with a module init call: That part
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) * of the kernel is not yet operational.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) static int __init cpucache_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) * Register the timers that return unneeded pages to the page allocator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "SLAB online",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) slab_online_cpu, slab_offline_cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) WARN_ON(ret < 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) __initcall(cpucache_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) static noinline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) slab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) #if DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) struct kmem_cache_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) int node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) static DEFINE_RATELIMIT_STATE(slab_oom_rs, DEFAULT_RATELIMIT_INTERVAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) DEFAULT_RATELIMIT_BURST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) if ((gfpflags & __GFP_NOWARN) || !__ratelimit(&slab_oom_rs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) pr_warn("SLAB: Unable to allocate memory on node %d, gfp=%#x(%pGg)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) nodeid, gfpflags, &gfpflags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) pr_warn(" cache: %s, object size: %d, order: %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) cachep->name, cachep->size, cachep->gfporder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) for_each_kmem_cache_node(cachep, node, n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) unsigned long total_slabs, free_slabs, free_objs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) spin_lock_irqsave(&n->list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) total_slabs = n->total_slabs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) free_slabs = n->free_slabs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) free_objs = n->free_objects;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) spin_unlock_irqrestore(&n->list_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) pr_warn(" node %d: slabs: %ld/%ld, objs: %ld/%ld\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) node, total_slabs - free_slabs, total_slabs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) (total_slabs * cachep->num) - free_objs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) total_slabs * cachep->num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) * Interface to system's page allocator. No need to hold the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) * kmem_cache_node ->list_lock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) * If we requested dmaable memory, we will get it. Even if we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) * did not request dmaable memory, we might get it, but that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) * would be relatively rare and ignorable.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) static struct page *kmem_getpages(struct kmem_cache *cachep, gfp_t flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) int nodeid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) flags |= cachep->allocflags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) page = __alloc_pages_node(nodeid, flags, cachep->gfporder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) if (!page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) slab_out_of_memory(cachep, flags, nodeid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) account_slab_page(page, cachep->gfporder, cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) __SetPageSlab(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) if (sk_memalloc_socks() && page_is_pfmemalloc(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) SetPageSlabPfmemalloc(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) return page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) * Interface to system's page release.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) static void kmem_freepages(struct kmem_cache *cachep, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) int order = cachep->gfporder;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) BUG_ON(!PageSlab(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400) __ClearPageSlabPfmemalloc(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401) __ClearPageSlab(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402) page_mapcount_reset(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) page->mapping = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) if (current->reclaim_state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) current->reclaim_state->reclaimed_slab += 1 << order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) unaccount_slab_page(page, order, cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) __free_pages(page, order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) static void kmem_rcu_free(struct rcu_head *head)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) struct kmem_cache *cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) page = container_of(head, struct page, rcu_head);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) cachep = page->slab_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) kmem_freepages(cachep, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) #if DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) static bool is_debug_pagealloc_cache(struct kmem_cache *cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) if (debug_pagealloc_enabled_static() && OFF_SLAB(cachep) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) (cachep->size % PAGE_SIZE) == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) #ifdef CONFIG_DEBUG_PAGEALLOC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) static void slab_kernel_map(struct kmem_cache *cachep, void *objp, int map)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) if (!is_debug_pagealloc_cache(cachep))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) __kernel_map_pages(virt_to_page(objp), cachep->size / PAGE_SIZE, map);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) static inline void slab_kernel_map(struct kmem_cache *cachep, void *objp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) int map) {}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) int size = cachep->object_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) addr = &((char *)addr)[obj_offset(cachep)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) memset(addr, val, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) *(unsigned char *)(addr + size - 1) = POISON_END;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) static void dump_line(char *data, int offset, int limit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) unsigned char error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) int bad_count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) pr_err("%03x: ", offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) for (i = 0; i < limit; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) if (data[offset + i] != POISON_FREE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) error = data[offset + i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) bad_count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) print_hex_dump(KERN_CONT, "", 0, 16, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) &data[offset], limit, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) if (bad_count == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) error ^= POISON_FREE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) if (!(error & (error - 1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) pr_err("Single bit error detected. Probably bad RAM.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) #ifdef CONFIG_X86
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) pr_err("Run memtest86+ or a similar memory test tool.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) pr_err("Run a memory test tool.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) #if DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) int i, size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) char *realobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) if (cachep->flags & SLAB_RED_ZONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) pr_err("Redzone: 0x%llx/0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) *dbg_redzone1(cachep, objp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) *dbg_redzone2(cachep, objp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) if (cachep->flags & SLAB_STORE_USER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) pr_err("Last user: (%pSR)\n", *dbg_userword(cachep, objp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) realobj = (char *)objp + obj_offset(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) size = cachep->object_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) for (i = 0; i < size && lines; i += 16, lines--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) int limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) limit = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) if (i + limit > size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) limit = size - i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) dump_line(realobj, i, limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) static void check_poison_obj(struct kmem_cache *cachep, void *objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) char *realobj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) int size, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) int lines = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) if (is_debug_pagealloc_cache(cachep))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) realobj = (char *)objp + obj_offset(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) size = cachep->object_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) for (i = 0; i < size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) char exp = POISON_FREE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) if (i == size - 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) exp = POISON_END;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) if (realobj[i] != exp) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) int limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) /* Mismatch ! */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) /* Print header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) if (lines == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) pr_err("Slab corruption (%s): %s start=%px, len=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) print_tainted(), cachep->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) realobj, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) print_objinfo(cachep, objp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) /* Hexdump the affected line */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) i = (i / 16) * 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) limit = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) if (i + limit > size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) limit = size - i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) dump_line(realobj, i, limit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) i += 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) lines++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) /* Limit to 5 lines */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) if (lines > 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) if (lines != 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) /* Print some data about the neighboring objects, if they
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) * exist:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) struct page *page = virt_to_head_page(objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) unsigned int objnr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) objnr = obj_to_index(cachep, page, objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) if (objnr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) objp = index_to_obj(cachep, page, objnr - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) realobj = (char *)objp + obj_offset(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) pr_err("Prev obj: start=%px, len=%d\n", realobj, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) print_objinfo(cachep, objp, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) if (objnr + 1 < cachep->num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) objp = index_to_obj(cachep, page, objnr + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) realobj = (char *)objp + obj_offset(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) pr_err("Next obj: start=%px, len=%d\n", realobj, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) print_objinfo(cachep, objp, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) #if DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) static void slab_destroy_debugcheck(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) if (OBJFREELIST_SLAB(cachep) && cachep->flags & SLAB_POISON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) poison_obj(cachep, page->freelist - obj_offset(cachep),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) POISON_FREE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) for (i = 0; i < cachep->num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) void *objp = index_to_obj(cachep, page, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) if (cachep->flags & SLAB_POISON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) check_poison_obj(cachep, objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) slab_kernel_map(cachep, objp, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) if (cachep->flags & SLAB_RED_ZONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) slab_error(cachep, "start of a freed object was overwritten");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) slab_error(cachep, "end of a freed object was overwritten");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) static void slab_destroy_debugcheck(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) * slab_destroy - destroy and release all objects in a slab
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) * @cachep: cache pointer being destroyed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) * @page: page pointer being destroyed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) * Destroy all the objs in a slab page, and release the mem back to the system.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) * Before calling the slab page must have been unlinked from the cache. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) * kmem_cache_node ->list_lock is not held/needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) static void slab_destroy(struct kmem_cache *cachep, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) void *freelist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) freelist = page->freelist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) slab_destroy_debugcheck(cachep, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) if (unlikely(cachep->flags & SLAB_TYPESAFE_BY_RCU))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) call_rcu(&page->rcu_head, kmem_rcu_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) kmem_freepages(cachep, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) * From now on, we don't use freelist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) * although actual page can be freed in rcu context
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) if (OFF_SLAB(cachep))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) kmem_cache_free(cachep->freelist_cache, freelist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) * Update the size of the caches before calling slabs_destroy as it may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) * recursively call kfree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) static void slabs_destroy(struct kmem_cache *cachep, struct list_head *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) struct page *page, *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) list_for_each_entry_safe(page, n, list, slab_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) list_del(&page->slab_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) slab_destroy(cachep, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) * calculate_slab_order - calculate size (page order) of slabs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) * @cachep: pointer to the cache that is being created
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) * @size: size of objects to be created in this cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) * @flags: slab allocation flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) * Also calculates the number of objects per slab.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) * This could be made much more intelligent. For now, try to avoid using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) * high order pages for slabs. When the gfp() functions are more friendly
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) * towards high-order requests, this should be changed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) * Return: number of left-over bytes in a slab
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) static size_t calculate_slab_order(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) size_t size, slab_flags_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) size_t left_over = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) int gfporder;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) unsigned int num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) size_t remainder;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) num = cache_estimate(gfporder, size, flags, &remainder);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) if (!num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) /* Can't handle number of objects more than SLAB_OBJ_MAX_NUM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) if (num > SLAB_OBJ_MAX_NUM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) if (flags & CFLGS_OFF_SLAB) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) struct kmem_cache *freelist_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) size_t freelist_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) freelist_size = num * sizeof(freelist_idx_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) freelist_cache = kmalloc_slab(freelist_size, 0u);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) if (!freelist_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) * Needed to avoid possible looping condition
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) * in cache_grow_begin()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) if (OFF_SLAB(freelist_cache))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) /* check if off slab has enough benefit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) if (freelist_cache->size > cachep->size / 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) /* Found something acceptable - save it away */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) cachep->num = num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) cachep->gfporder = gfporder;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) left_over = remainder;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) * A VFS-reclaimable slab tends to have most allocations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) * as GFP_NOFS and we really don't want to have to be allocating
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) * higher-order pages when we are unable to shrink dcache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) if (flags & SLAB_RECLAIM_ACCOUNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) * Large number of objects is good, but very large slabs are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) * currently bad for the gfp()s.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) if (gfporder >= slab_max_order)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) * Acceptable internal fragmentation?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) if (left_over * 8 <= (PAGE_SIZE << gfporder))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) return left_over;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) static struct array_cache __percpu *alloc_kmem_cache_cpus(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) struct kmem_cache *cachep, int entries, int batchcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) struct array_cache __percpu *cpu_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) size = sizeof(void *) * entries + sizeof(struct array_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) cpu_cache = __alloc_percpu(size, sizeof(void *));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) if (!cpu_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745) for_each_possible_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746) init_arraycache(per_cpu_ptr(cpu_cache, cpu),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747) entries, batchcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750) return cpu_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753) static int __ref setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) if (slab_state >= FULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) return enable_cpucache(cachep, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) cachep->cpu_cache = alloc_kmem_cache_cpus(cachep, 1, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) if (!cachep->cpu_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) if (slab_state == DOWN) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) /* Creation of first cache (kmem_cache). */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) set_up_node(kmem_cache, CACHE_CACHE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) } else if (slab_state == PARTIAL) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) /* For kmem_cache_node */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) set_up_node(cachep, SIZE_NODE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) int node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) for_each_online_node(node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) cachep->node[node] = kmalloc_node(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) sizeof(struct kmem_cache_node), gfp, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) BUG_ON(!cachep->node[node]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) kmem_cache_node_init(cachep->node[node]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) cachep->node[numa_mem_id()]->next_reap =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) jiffies + REAPTIMEOUT_NODE +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) ((unsigned long)cachep) % REAPTIMEOUT_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) cpu_cache_get(cachep)->avail = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) cpu_cache_get(cachep)->batchcount = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) cpu_cache_get(cachep)->touched = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) cachep->batchcount = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) cachep->limit = BOOT_CPUCACHE_ENTRIES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) slab_flags_t kmem_cache_flags(unsigned int object_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) slab_flags_t flags, const char *name)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) return flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) struct kmem_cache *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) __kmem_cache_alias(const char *name, unsigned int size, unsigned int align,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) slab_flags_t flags, void (*ctor)(void *))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) struct kmem_cache *cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) cachep = find_mergeable(size, align, flags, name, ctor);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) if (cachep) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) cachep->refcount++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) * Adjust the object sizes so that we clear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) * the complete object on kzalloc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) cachep->object_size = max_t(int, cachep->object_size, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) return cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) static bool set_objfreelist_slab_cache(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) size_t size, slab_flags_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) size_t left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) cachep->num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) * If slab auto-initialization on free is enabled, store the freelist
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) * off-slab, so that its contents don't end up in one of the allocated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) * objects.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) if (unlikely(slab_want_init_on_free(cachep)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) if (cachep->ctor || flags & SLAB_TYPESAFE_BY_RCU)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) left = calculate_slab_order(cachep, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) flags | CFLGS_OBJFREELIST_SLAB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) if (!cachep->num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) if (cachep->num * sizeof(freelist_idx_t) > cachep->object_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) cachep->colour = left / cachep->colour_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) static bool set_off_slab_cache(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) size_t size, slab_flags_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) size_t left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) cachep->num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) * Always use on-slab management when SLAB_NOLEAKTRACE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) * to avoid recursive calls into kmemleak.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) if (flags & SLAB_NOLEAKTRACE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) * Size is large, assume best to place the slab management obj
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) * off-slab (should allow better packing of objs).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) left = calculate_slab_order(cachep, size, flags | CFLGS_OFF_SLAB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) if (!cachep->num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) * If the slab has been placed off-slab, and we have enough space then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) * move it on-slab. This is at the expense of any extra colouring.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) if (left >= cachep->num * sizeof(freelist_idx_t))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) cachep->colour = left / cachep->colour_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) static bool set_on_slab_cache(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) size_t size, slab_flags_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) size_t left;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887) cachep->num = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889) left = calculate_slab_order(cachep, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890) if (!cachep->num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) cachep->colour = left / cachep->colour_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) * __kmem_cache_create - Create a cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) * @cachep: cache management descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) * @flags: SLAB flags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) * Returns a ptr to the cache on success, NULL on failure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) * Cannot be called within a int, but can be interrupted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) * The @ctor is run when new pages are allocated by the cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) * The flags are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) * to catch references to uninitialised memory.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) * for buffer overruns.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) * cacheline. This can be beneficial if you're counting cycles as closely
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) * as davem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) * Return: a pointer to the created cache or %NULL in case of error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921) int __kmem_cache_create(struct kmem_cache *cachep, slab_flags_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923) size_t ralign = BYTES_PER_WORD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924) gfp_t gfp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) unsigned int size = cachep->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) #if DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) #if FORCED_DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) * Enable redzoning and last user accounting, except for caches with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) * large objects, if the increased size would increase the object size
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) * above the next power of two: caches with object sizes just above a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) * power of two have a significant amount of internal fragmentation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 2 * sizeof(unsigned long long)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) if (!(flags & SLAB_TYPESAFE_BY_RCU))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) flags |= SLAB_POISON;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) * Check that size is in terms of words. This is needed to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) * unaligned accesses for some archs when redzoning is used, and makes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) * sure any on-slab bufctl's are also correctly aligned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) size = ALIGN(size, BYTES_PER_WORD);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) if (flags & SLAB_RED_ZONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) ralign = REDZONE_ALIGN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) /* If redzoning, ensure that the second redzone is suitably
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) * aligned, by adjusting the object size accordingly. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) size = ALIGN(size, REDZONE_ALIGN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) /* 3) caller mandated alignment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) if (ralign < cachep->align) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) ralign = cachep->align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) /* disable debug if necessary */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) if (ralign > __alignof__(unsigned long long))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) * 4) Store it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) cachep->align = ralign;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969) cachep->colour_off = cache_line_size();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970) /* Offset must be a multiple of the alignment. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971) if (cachep->colour_off < cachep->align)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972) cachep->colour_off = cachep->align;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) if (slab_is_available())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) gfp = GFP_KERNEL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) gfp = GFP_NOWAIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) #if DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) * Both debugging options require word-alignment which is calculated
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) * into align above.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) if (flags & SLAB_RED_ZONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) /* add space for red zone words */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) cachep->obj_offset += sizeof(unsigned long long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) size += 2 * sizeof(unsigned long long);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) if (flags & SLAB_STORE_USER) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) /* user store requires one word storage behind the end of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) * the real object. But if the second red zone needs to be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) * aligned to 64 bits, we must allow that much space.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) if (flags & SLAB_RED_ZONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) size += REDZONE_ALIGN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) size += BYTES_PER_WORD;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) kasan_cache_create(cachep, &size, &flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) size = ALIGN(size, cachep->align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) * We should restrict the number of objects in a slab to implement
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) * byte sized index. Refer comment on SLAB_OBJ_MIN_SIZE definition.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) if (FREELIST_BYTE_INDEX && size < SLAB_OBJ_MIN_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) size = ALIGN(SLAB_OBJ_MIN_SIZE, cachep->align);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) #if DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) * To activate debug pagealloc, off-slab management is necessary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) * requirement. In early phase of initialization, small sized slab
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) * doesn't get initialized so it would not be possible. So, we need
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) * to check size >= 256. It guarantees that all necessary small
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) * sized slab is initialized in current slab initialization sequence.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) if (debug_pagealloc_enabled_static() && (flags & SLAB_POISON) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) size >= 256 && cachep->object_size > cache_line_size()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) if (size < PAGE_SIZE || size % PAGE_SIZE == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) size_t tmp_size = ALIGN(size, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) if (set_off_slab_cache(cachep, tmp_size, flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) flags |= CFLGS_OFF_SLAB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) cachep->obj_offset += tmp_size - size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) size = tmp_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) if (set_objfreelist_slab_cache(cachep, size, flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) flags |= CFLGS_OBJFREELIST_SLAB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) if (set_off_slab_cache(cachep, size, flags)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) flags |= CFLGS_OFF_SLAB;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) if (set_on_slab_cache(cachep, size, flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) cachep->freelist_size = cachep->num * sizeof(freelist_idx_t);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) cachep->flags = flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) cachep->allocflags = __GFP_COMP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) if (flags & SLAB_CACHE_DMA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) cachep->allocflags |= GFP_DMA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) if (flags & SLAB_CACHE_DMA32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) cachep->allocflags |= GFP_DMA32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) if (flags & SLAB_RECLAIM_ACCOUNT)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) cachep->allocflags |= __GFP_RECLAIMABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) cachep->size = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) cachep->reciprocal_buffer_size = reciprocal_value(size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) #if DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) * If we're going to use the generic kernel_map_pages()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) * poisoning, then it's going to smash the contents of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) * the redzone and userword anyhow, so switch them off.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) if (IS_ENABLED(CONFIG_PAGE_POISONING) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) (cachep->flags & SLAB_POISON) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) is_debug_pagealloc_cache(cachep))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) cachep->flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) if (OFF_SLAB(cachep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) cachep->freelist_cache =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) kmalloc_slab(cachep->freelist_size, 0u);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) err = setup_cpu_cache(cachep, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) __kmem_cache_release(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) #if DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) static void check_irq_off(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) BUG_ON(!irqs_disabled());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) static void check_irq_on(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) BUG_ON(irqs_disabled());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) static void check_mutex_acquired(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) BUG_ON(!mutex_is_locked(&slab_mutex));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) static void check_spinlock_acquired(struct kmem_cache *cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) check_irq_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) assert_spin_locked(&get_node(cachep, numa_mem_id())->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) #ifdef CONFIG_SMP
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) check_irq_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) assert_spin_locked(&get_node(cachep, node)->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) #define check_irq_off() do { } while(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) #define check_irq_on() do { } while(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) #define check_mutex_acquired() do { } while(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) #define check_spinlock_acquired(x) do { } while(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) #define check_spinlock_acquired_node(x, y) do { } while(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) static void drain_array_locked(struct kmem_cache *cachep, struct array_cache *ac,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) int node, bool free_all, struct list_head *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) int tofree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) if (!ac || !ac->avail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) tofree = free_all ? ac->avail : (ac->limit + 4) / 5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) if (tofree > ac->avail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) tofree = (ac->avail + 1) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) free_block(cachep, ac->entry, tofree, node, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) ac->avail -= tofree;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) memmove(ac->entry, &(ac->entry[tofree]), sizeof(void *) * ac->avail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) static void do_drain(void *arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) struct kmem_cache *cachep = arg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) struct array_cache *ac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) int node = numa_mem_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) struct kmem_cache_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152) LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154) check_irq_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155) ac = cpu_cache_get(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156) n = get_node(cachep, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157) spin_lock(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158) free_block(cachep, ac->entry, ac->avail, node, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159) spin_unlock(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) ac->avail = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) slabs_destroy(cachep, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) static void drain_cpu_caches(struct kmem_cache *cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) struct kmem_cache_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) int node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) on_each_cpu(do_drain, cachep, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) check_irq_on();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) for_each_kmem_cache_node(cachep, node, n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) if (n->alien)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) drain_alien_cache(cachep, n->alien);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) for_each_kmem_cache_node(cachep, node, n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) spin_lock_irq(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) drain_array_locked(cachep, n->shared, node, true, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) spin_unlock_irq(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) slabs_destroy(cachep, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) * Remove slabs from the list of free slabs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) * Specify the number of slabs to drain in tofree.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) * Returns the actual number of slabs released.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) static int drain_freelist(struct kmem_cache *cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) struct kmem_cache_node *n, int tofree)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) struct list_head *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) int nr_freed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) nr_freed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) while (nr_freed < tofree && !list_empty(&n->slabs_free)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) spin_lock_irq(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) p = n->slabs_free.prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) if (p == &n->slabs_free) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) spin_unlock_irq(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) page = list_entry(p, struct page, slab_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) list_del(&page->slab_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) n->free_slabs--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) n->total_slabs--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) * Safe to drop the lock. The slab is no longer linked
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) * to the cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) n->free_objects -= cache->num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) spin_unlock_irq(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) slab_destroy(cache, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) nr_freed++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) return nr_freed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225) bool __kmem_cache_empty(struct kmem_cache *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227) int node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228) struct kmem_cache_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) for_each_kmem_cache_node(s, node, n)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) if (!list_empty(&n->slabs_full) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) !list_empty(&n->slabs_partial))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) int __kmem_cache_shrink(struct kmem_cache *cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) int node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) struct kmem_cache_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) drain_cpu_caches(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) check_irq_on();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) for_each_kmem_cache_node(cachep, node, n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) drain_freelist(cachep, n, INT_MAX);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) ret += !list_empty(&n->slabs_full) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) !list_empty(&n->slabs_partial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) return (ret ? 1 : 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) int __kmem_cache_shutdown(struct kmem_cache *cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) return __kmem_cache_shrink(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) void __kmem_cache_release(struct kmem_cache *cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) struct kmem_cache_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) cache_random_seq_destroy(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) free_percpu(cachep->cpu_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) /* NUMA: free the node structures */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) for_each_kmem_cache_node(cachep, i, n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) kfree(n->shared);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) free_alien_cache(n->alien);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) kfree(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) cachep->node[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) * Get the memory for a slab management obj.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) * For a slab cache when the slab descriptor is off-slab, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) * slab descriptor can't come from the same cache which is being created,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283) * Because if it is the case, that means we defer the creation of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284) * the kmalloc_{dma,}_cache of size sizeof(slab descriptor) to this point.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285) * And we eventually call down to __kmem_cache_create(), which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286) * in turn looks up in the kmalloc_{dma,}_caches for the disired-size one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287) * This is a "chicken-and-egg" problem.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289) * So the off-slab slab descriptor shall come from the kmalloc_{dma,}_caches,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) * which are all initialized during kmem_cache_init().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) static void *alloc_slabmgmt(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) struct page *page, int colour_off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) gfp_t local_flags, int nodeid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) void *freelist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) void *addr = page_address(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) page->s_mem = addr + colour_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) page->active = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) if (OBJFREELIST_SLAB(cachep))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) freelist = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) else if (OFF_SLAB(cachep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) /* Slab management obj is off-slab. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) freelist = kmem_cache_alloc_node(cachep->freelist_cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) local_flags, nodeid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) /* We will use last bytes at the slab for freelist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) freelist = addr + (PAGE_SIZE << cachep->gfporder) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) cachep->freelist_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) return freelist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) static inline freelist_idx_t get_free_obj(struct page *page, unsigned int idx)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) return ((freelist_idx_t *)page->freelist)[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) static inline void set_free_obj(struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) unsigned int idx, freelist_idx_t val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) ((freelist_idx_t *)(page->freelist))[idx] = val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) static void cache_init_objs_debug(struct kmem_cache *cachep, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330) #if DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333) for (i = 0; i < cachep->num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334) void *objp = index_to_obj(cachep, page, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336) if (cachep->flags & SLAB_STORE_USER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) *dbg_userword(cachep, objp) = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) if (cachep->flags & SLAB_RED_ZONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) *dbg_redzone1(cachep, objp) = RED_INACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) *dbg_redzone2(cachep, objp) = RED_INACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) * Constructors are not allowed to allocate memory from the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) * cache which they are a constructor for. Otherwise, deadlock.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) * They must also be threaded.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) if (cachep->ctor && !(cachep->flags & SLAB_POISON)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) kasan_unpoison_object_data(cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) objp + obj_offset(cachep));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) cachep->ctor(objp + obj_offset(cachep));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) kasan_poison_object_data(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) cachep, objp + obj_offset(cachep));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) if (cachep->flags & SLAB_RED_ZONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) slab_error(cachep, "constructor overwrote the end of an object");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) slab_error(cachep, "constructor overwrote the start of an object");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) /* need to poison the objs? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) if (cachep->flags & SLAB_POISON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) poison_obj(cachep, objp, POISON_FREE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) slab_kernel_map(cachep, objp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) #ifdef CONFIG_SLAB_FREELIST_RANDOM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) /* Hold information during a freelist initialization */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) union freelist_init_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) unsigned int pos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) unsigned int *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) unsigned int count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) struct rnd_state rnd_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) * Initialize the state based on the randomization methode available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) * return true if the pre-computed list is available, false otherwize.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) static bool freelist_state_initialize(union freelist_init_state *state,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) unsigned int count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) bool ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) unsigned int rand;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) /* Use best entropy available to define a random shift */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) rand = get_random_int();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) /* Use a random state if the pre-computed list is not available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) if (!cachep->random_seq) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) prandom_seed_state(&state->rnd_state, rand);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) ret = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) state->list = cachep->random_seq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) state->count = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) state->pos = rand % count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) ret = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) /* Get the next entry on the list and randomize it using a random shift */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) static freelist_idx_t next_random_slot(union freelist_init_state *state)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) if (state->pos >= state->count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) state->pos = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) return state->list[state->pos++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) /* Swap two freelist entries */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) static void swap_free_obj(struct page *page, unsigned int a, unsigned int b)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) swap(((freelist_idx_t *)page->freelist)[a],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) ((freelist_idx_t *)page->freelist)[b]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) * Shuffle the freelist initialization state based on pre-computed lists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) * return true if the list was successfully shuffled, false otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) static bool shuffle_freelist(struct kmem_cache *cachep, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) unsigned int objfreelist = 0, i, rand, count = cachep->num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) union freelist_init_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) bool precomputed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) if (count < 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) precomputed = freelist_state_initialize(&state, cachep, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) /* Take a random entry as the objfreelist */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) if (OBJFREELIST_SLAB(cachep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) if (!precomputed)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) objfreelist = count - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) objfreelist = next_random_slot(&state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) page->freelist = index_to_obj(cachep, page, objfreelist) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) obj_offset(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) count--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) * On early boot, generate the list dynamically.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) * Later use a pre-computed list for speed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) if (!precomputed) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) for (i = 0; i < count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456) set_free_obj(page, i, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458) /* Fisher-Yates shuffle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459) for (i = count - 1; i > 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460) rand = prandom_u32_state(&state.rnd_state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) rand %= (i + 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) swap_free_obj(page, i, rand);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) for (i = 0; i < count; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) set_free_obj(page, i, next_random_slot(&state));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) if (OBJFREELIST_SLAB(cachep))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) set_free_obj(page, cachep->num - 1, objfreelist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) static inline bool shuffle_freelist(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) #endif /* CONFIG_SLAB_FREELIST_RANDOM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) static void cache_init_objs(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) void *objp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) bool shuffled;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) cache_init_objs_debug(cachep, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) /* Try to randomize the freelist if enabled */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) shuffled = shuffle_freelist(cachep, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) if (!shuffled && OBJFREELIST_SLAB(cachep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) page->freelist = index_to_obj(cachep, page, cachep->num - 1) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) obj_offset(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) for (i = 0; i < cachep->num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) objp = index_to_obj(cachep, page, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) objp = kasan_init_slab_obj(cachep, objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) /* constructor could break poison info */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) if (DEBUG == 0 && cachep->ctor) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) kasan_unpoison_object_data(cachep, objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) cachep->ctor(objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) kasan_poison_object_data(cachep, objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) if (!shuffled)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) set_free_obj(page, i, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2515) static void *slab_get_obj(struct kmem_cache *cachep, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2516) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2517) void *objp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2518)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2519) objp = index_to_obj(cachep, page, get_free_obj(page, page->active));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2520) page->active++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2521)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2522) return objp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2525) static void slab_put_obj(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2526) struct page *page, void *objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2527) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2528) unsigned int objnr = obj_to_index(cachep, page, objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2529) #if DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2530) unsigned int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2531)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2532) /* Verify double free bug */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2533) for (i = page->active; i < cachep->num; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2534) if (get_free_obj(page, i) == objnr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2535) pr_err("slab: double free detected in cache '%s', objp %px\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2536) cachep->name, objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2537) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2538) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2539) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2540) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2541) page->active--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2542) if (!page->freelist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2543) page->freelist = objp + obj_offset(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2544)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2545) set_free_obj(page, page->active, objnr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2546) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2547)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2548) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2549) * Map pages beginning at addr to the given cache and slab. This is required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2550) * for the slab allocator to be able to lookup the cache and slab of a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2551) * virtual address for kfree, ksize, and slab debugging.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2552) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2553) static void slab_map_pages(struct kmem_cache *cache, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2554) void *freelist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2555) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2556) page->slab_cache = cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2557) page->freelist = freelist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2558) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2559)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2560) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2561) * Grow (by 1) the number of slabs within a cache. This is called by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2562) * kmem_cache_alloc() when there are no active objs left in a cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2563) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2564) static struct page *cache_grow_begin(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2565) gfp_t flags, int nodeid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2566) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2567) void *freelist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2568) size_t offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2569) gfp_t local_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2570) int page_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2571) struct kmem_cache_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2572) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2574) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2575) * Be lazy and only check for valid flags here, keeping it out of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2576) * critical path in kmem_cache_alloc().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2577) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2578) if (unlikely(flags & GFP_SLAB_BUG_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2579) flags = kmalloc_fix_flags(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2580)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2581) WARN_ON_ONCE(cachep->ctor && (flags & __GFP_ZERO));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2582) local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2583)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2584) check_irq_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2585) if (gfpflags_allow_blocking(local_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2586) local_irq_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2588) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2589) * Get mem for the objs. Attempt to allocate a physical page from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2590) * 'nodeid'.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2591) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2592) page = kmem_getpages(cachep, local_flags, nodeid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2593) if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2594) goto failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2596) page_node = page_to_nid(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2597) n = get_node(cachep, page_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2598)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2599) /* Get colour for the slab, and cal the next value. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2600) n->colour_next++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2601) if (n->colour_next >= cachep->colour)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2602) n->colour_next = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2604) offset = n->colour_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2605) if (offset >= cachep->colour)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2606) offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2608) offset *= cachep->colour_off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2609)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2610) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2611) * Call kasan_poison_slab() before calling alloc_slabmgmt(), so
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2612) * page_address() in the latter returns a non-tagged pointer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2613) * as it should be for slab pages.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2614) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2615) kasan_poison_slab(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2616)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2617) /* Get slab management. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2618) freelist = alloc_slabmgmt(cachep, page, offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2619) local_flags & ~GFP_CONSTRAINT_MASK, page_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2620) if (OFF_SLAB(cachep) && !freelist)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2621) goto opps1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2622)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2623) slab_map_pages(cachep, page, freelist);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2624)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2625) cache_init_objs(cachep, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2627) if (gfpflags_allow_blocking(local_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2628) local_irq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2629)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2630) return page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2631)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2632) opps1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2633) kmem_freepages(cachep, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2634) failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2635) if (gfpflags_allow_blocking(local_flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2636) local_irq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2637) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2638) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2639)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2640) static void cache_grow_end(struct kmem_cache *cachep, struct page *page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2641) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2642) struct kmem_cache_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2643) void *list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2644)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2645) check_irq_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2646)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2647) if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2648) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2650) INIT_LIST_HEAD(&page->slab_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2651) n = get_node(cachep, page_to_nid(page));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2652)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2653) spin_lock(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2654) n->total_slabs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2655) if (!page->active) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2656) list_add_tail(&page->slab_list, &n->slabs_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2657) n->free_slabs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2658) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2659) fixup_slab_list(cachep, n, page, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2660)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2661) STATS_INC_GROWN(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2662) n->free_objects += cachep->num - page->active;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2663) spin_unlock(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2664)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2665) fixup_objfreelist_debug(cachep, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2666) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2667)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2668) #if DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2669)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2670) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2671) * Perform extra freeing checks:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2672) * - detect bad pointers.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2673) * - POISON/RED_ZONE checking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2674) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2675) static void kfree_debugcheck(const void *objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2676) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2677) if (!virt_addr_valid(objp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2678) pr_err("kfree_debugcheck: out of range ptr %lxh\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2679) (unsigned long)objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2680) BUG();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2681) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2682) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2683)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2684) static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2685) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2686) unsigned long long redzone1, redzone2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2687)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2688) redzone1 = *dbg_redzone1(cache, obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2689) redzone2 = *dbg_redzone2(cache, obj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2690)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2691) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2692) * Redzone is ok.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2693) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2694) if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2695) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2696)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2697) if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2698) slab_error(cache, "double free detected");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2699) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2700) slab_error(cache, "memory outside object was overwritten");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2701)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2702) pr_err("%px: redzone 1:0x%llx, redzone 2:0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2703) obj, redzone1, redzone2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2704) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2705)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2706) static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2707) unsigned long caller)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2708) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2709) unsigned int objnr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2710) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2711)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2712) BUG_ON(virt_to_cache(objp) != cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2713)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2714) objp -= obj_offset(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2715) kfree_debugcheck(objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2716) page = virt_to_head_page(objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2717)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2718) if (cachep->flags & SLAB_RED_ZONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2719) verify_redzone_free(cachep, objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2720) *dbg_redzone1(cachep, objp) = RED_INACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2721) *dbg_redzone2(cachep, objp) = RED_INACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2722) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2723) if (cachep->flags & SLAB_STORE_USER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2724) *dbg_userword(cachep, objp) = (void *)caller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2725)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2726) objnr = obj_to_index(cachep, page, objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2727)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2728) BUG_ON(objnr >= cachep->num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2729) BUG_ON(objp != index_to_obj(cachep, page, objnr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2730)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2731) if (cachep->flags & SLAB_POISON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2732) poison_obj(cachep, objp, POISON_FREE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2733) slab_kernel_map(cachep, objp, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2734) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2735) return objp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2736) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2737)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2738) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2739) #define kfree_debugcheck(x) do { } while(0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2740) #define cache_free_debugcheck(x,objp,z) (objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2741) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2742)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2743) static inline void fixup_objfreelist_debug(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2744) void **list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2745) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2746) #if DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2747) void *next = *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2748) void *objp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2749)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2750) while (next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2751) objp = next - obj_offset(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2752) next = *(void **)next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2753) poison_obj(cachep, objp, POISON_FREE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2754) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2755) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2756) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2757)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2758) static inline void fixup_slab_list(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2759) struct kmem_cache_node *n, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2760) void **list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2761) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2762) /* move slabp to correct slabp list: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2763) list_del(&page->slab_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2764) if (page->active == cachep->num) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2765) list_add(&page->slab_list, &n->slabs_full);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2766) if (OBJFREELIST_SLAB(cachep)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2767) #if DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2768) /* Poisoning will be done without holding the lock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2769) if (cachep->flags & SLAB_POISON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2770) void **objp = page->freelist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2771)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2772) *objp = *list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2773) *list = objp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2774) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2775) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2776) page->freelist = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2777) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2778) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2779) list_add(&page->slab_list, &n->slabs_partial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2780) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2781)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2782) /* Try to find non-pfmemalloc slab if needed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2783) static noinline struct page *get_valid_first_slab(struct kmem_cache_node *n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2784) struct page *page, bool pfmemalloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2785) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2786) if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2787) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2788)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2789) if (pfmemalloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2790) return page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2791)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2792) if (!PageSlabPfmemalloc(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2793) return page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2794)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2795) /* No need to keep pfmemalloc slab if we have enough free objects */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2796) if (n->free_objects > n->free_limit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2797) ClearPageSlabPfmemalloc(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2798) return page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2799) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2801) /* Move pfmemalloc slab to the end of list to speed up next search */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2802) list_del(&page->slab_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2803) if (!page->active) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2804) list_add_tail(&page->slab_list, &n->slabs_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2805) n->free_slabs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2806) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2807) list_add_tail(&page->slab_list, &n->slabs_partial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2808)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2809) list_for_each_entry(page, &n->slabs_partial, slab_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2810) if (!PageSlabPfmemalloc(page))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2811) return page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2812) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2813)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2814) n->free_touched = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2815) list_for_each_entry(page, &n->slabs_free, slab_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2816) if (!PageSlabPfmemalloc(page)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2817) n->free_slabs--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2818) return page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2820) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2821)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2822) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2823) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2824)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2825) static struct page *get_first_slab(struct kmem_cache_node *n, bool pfmemalloc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2826) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2827) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2828)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2829) assert_spin_locked(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2830) page = list_first_entry_or_null(&n->slabs_partial, struct page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2831) slab_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2832) if (!page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2833) n->free_touched = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2834) page = list_first_entry_or_null(&n->slabs_free, struct page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2835) slab_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2836) if (page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2837) n->free_slabs--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2838) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2839)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2840) if (sk_memalloc_socks())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2841) page = get_valid_first_slab(n, page, pfmemalloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2842)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2843) return page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2844) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2846) static noinline void *cache_alloc_pfmemalloc(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2847) struct kmem_cache_node *n, gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2848) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2849) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2850) void *obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2851) void *list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2852)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2853) if (!gfp_pfmemalloc_allowed(flags))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2854) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2855)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2856) spin_lock(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2857) page = get_first_slab(n, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2858) if (!page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2859) spin_unlock(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2860) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2862)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2863) obj = slab_get_obj(cachep, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2864) n->free_objects--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2865)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2866) fixup_slab_list(cachep, n, page, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2868) spin_unlock(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2869) fixup_objfreelist_debug(cachep, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2870)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2871) return obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2872) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2873)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2874) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2875) * Slab list should be fixed up by fixup_slab_list() for existing slab
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2876) * or cache_grow_end() for new slab
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2877) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2878) static __always_inline int alloc_block(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2879) struct array_cache *ac, struct page *page, int batchcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2880) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2881) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2882) * There must be at least one object available for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2883) * allocation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2884) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2885) BUG_ON(page->active >= cachep->num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2886)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2887) while (page->active < cachep->num && batchcount--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2888) STATS_INC_ALLOCED(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2889) STATS_INC_ACTIVE(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2890) STATS_SET_HIGH(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2891)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2892) ac->entry[ac->avail++] = slab_get_obj(cachep, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2893) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2894)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2895) return batchcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2896) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2897)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2898) static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2899) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2900) int batchcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2901) struct kmem_cache_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2902) struct array_cache *ac, *shared;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2903) int node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2904) void *list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2905) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2906)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2907) check_irq_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2908) node = numa_mem_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2909)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2910) ac = cpu_cache_get(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2911) batchcount = ac->batchcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2912) if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2913) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2914) * If there was little recent activity on this cache, then
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2915) * perform only a partial refill. Otherwise we could generate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2916) * refill bouncing.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2917) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2918) batchcount = BATCHREFILL_LIMIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2919) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2920) n = get_node(cachep, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2921)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2922) BUG_ON(ac->avail > 0 || !n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2923) shared = READ_ONCE(n->shared);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2924) if (!n->free_objects && (!shared || !shared->avail))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2925) goto direct_grow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2926)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2927) spin_lock(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2928) shared = READ_ONCE(n->shared);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2929)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2930) /* See if we can refill from the shared array */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2931) if (shared && transfer_objects(ac, shared, batchcount)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2932) shared->touched = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2933) goto alloc_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2934) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2935)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2936) while (batchcount > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2937) /* Get slab alloc is to come from. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2938) page = get_first_slab(n, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2939) if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2940) goto must_grow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2941)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2942) check_spinlock_acquired(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2943)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2944) batchcount = alloc_block(cachep, ac, page, batchcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2945) fixup_slab_list(cachep, n, page, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2946) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2947)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2948) must_grow:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2949) n->free_objects -= ac->avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2950) alloc_done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2951) spin_unlock(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2952) fixup_objfreelist_debug(cachep, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2954) direct_grow:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2955) if (unlikely(!ac->avail)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2956) /* Check if we can use obj in pfmemalloc slab */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2957) if (sk_memalloc_socks()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2958) void *obj = cache_alloc_pfmemalloc(cachep, n, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2959)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2960) if (obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2961) return obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2962) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2963)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2964) page = cache_grow_begin(cachep, gfp_exact_node(flags), node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2965)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2966) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2967) * cache_grow_begin() can reenable interrupts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2968) * then ac could change.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2969) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2970) ac = cpu_cache_get(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2971) if (!ac->avail && page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2972) alloc_block(cachep, ac, page, batchcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2973) cache_grow_end(cachep, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2974)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2975) if (!ac->avail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2976) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2977) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2978) ac->touched = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2980) return ac->entry[--ac->avail];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2981) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2982)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2983) static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2984) gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2985) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2986) might_sleep_if(gfpflags_allow_blocking(flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2987) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2988)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2989) #if DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2990) static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2991) gfp_t flags, void *objp, unsigned long caller)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2992) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2993) WARN_ON_ONCE(cachep->ctor && (flags & __GFP_ZERO));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2994) if (!objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2995) return objp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2996) if (cachep->flags & SLAB_POISON) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2997) check_poison_obj(cachep, objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2998) slab_kernel_map(cachep, objp, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2999) poison_obj(cachep, objp, POISON_INUSE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3000) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3001) if (cachep->flags & SLAB_STORE_USER)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3002) *dbg_userword(cachep, objp) = (void *)caller;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3003)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3004) if (cachep->flags & SLAB_RED_ZONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3005) if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3006) *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3007) slab_error(cachep, "double free, or memory outside object was overwritten");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3008) pr_err("%px: redzone 1:0x%llx, redzone 2:0x%llx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3009) objp, *dbg_redzone1(cachep, objp),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3010) *dbg_redzone2(cachep, objp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3011) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3012) *dbg_redzone1(cachep, objp) = RED_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3013) *dbg_redzone2(cachep, objp) = RED_ACTIVE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3014) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3015)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3016) objp += obj_offset(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3017) if (cachep->ctor && cachep->flags & SLAB_POISON)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3018) cachep->ctor(objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3019) if (ARCH_SLAB_MINALIGN &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3020) ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3021) pr_err("0x%px: not aligned to ARCH_SLAB_MINALIGN=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3022) objp, (int)ARCH_SLAB_MINALIGN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3023) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3024) return objp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3025) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3026) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3027) #define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3028) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3029)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3030) static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3031) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3032) void *objp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3033) struct array_cache *ac;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3034)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3035) check_irq_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3036)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3037) ac = cpu_cache_get(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3038) if (likely(ac->avail)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3039) ac->touched = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3040) objp = ac->entry[--ac->avail];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3041)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3042) STATS_INC_ALLOCHIT(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3043) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3044) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3046) STATS_INC_ALLOCMISS(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3047) objp = cache_alloc_refill(cachep, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3048) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3049) * the 'ac' may be updated by cache_alloc_refill(),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3050) * and kmemleak_erase() requires its correct value.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3051) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3052) ac = cpu_cache_get(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3053)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3054) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3055) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3056) * To avoid a false negative, if an object that is in one of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3057) * per-CPU caches is leaked, we need to make sure kmemleak doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3058) * treat the array pointers as a reference to the object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3059) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3060) if (objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3061) kmemleak_erase(&ac->entry[ac->avail]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3062) return objp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3063) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3064)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3065) #ifdef CONFIG_NUMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3066) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3067) * Try allocating on another node if PFA_SPREAD_SLAB is a mempolicy is set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3068) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3069) * If we are in_interrupt, then process context, including cpusets and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3070) * mempolicy, may not apply and should not be used for allocation policy.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3071) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3072) static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3073) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3074) int nid_alloc, nid_here;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3075)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3076) if (in_interrupt() || (flags & __GFP_THISNODE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3077) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3078) nid_alloc = nid_here = numa_mem_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3079) if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3080) nid_alloc = cpuset_slab_spread_node();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3081) else if (current->mempolicy)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3082) nid_alloc = mempolicy_slab_node();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3083) if (nid_alloc != nid_here)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3084) return ____cache_alloc_node(cachep, flags, nid_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3085) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3086) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3087)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3088) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3089) * Fallback function if there was no memory available and no objects on a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3090) * certain node and fall back is permitted. First we scan all the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3091) * available node for available objects. If that fails then we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3092) * perform an allocation without specifying a node. This allows the page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3093) * allocator to do its reclaim / fallback magic. We then insert the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3094) * slab into the proper nodelist and then allocate from it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3095) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3096) static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3097) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3098) struct zonelist *zonelist;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3099) struct zoneref *z;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3100) struct zone *zone;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3101) enum zone_type highest_zoneidx = gfp_zone(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3102) void *obj = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3103) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3104) int nid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3105) unsigned int cpuset_mems_cookie;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3107) if (flags & __GFP_THISNODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3108) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3110) retry_cpuset:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3111) cpuset_mems_cookie = read_mems_allowed_begin();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3112) zonelist = node_zonelist(mempolicy_slab_node(), flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3114) retry:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3115) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3116) * Look through allowed nodes for objects available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3117) * from existing per node queues.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3118) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3119) for_each_zone_zonelist(zone, z, zonelist, highest_zoneidx) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3120) nid = zone_to_nid(zone);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3122) if (cpuset_zone_allowed(zone, flags) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3123) get_node(cache, nid) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3124) get_node(cache, nid)->free_objects) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3125) obj = ____cache_alloc_node(cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3126) gfp_exact_node(flags), nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3127) if (obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3128) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3129) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3132) if (!obj) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3133) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3134) * This allocation will be performed within the constraints
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3135) * of the current cpuset / memory policy requirements.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3136) * We may trigger various forms of reclaim on the allowed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3137) * set and go into memory reserves if necessary.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3138) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3139) page = cache_grow_begin(cache, flags, numa_mem_id());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3140) cache_grow_end(cache, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3141) if (page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3142) nid = page_to_nid(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3143) obj = ____cache_alloc_node(cache,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3144) gfp_exact_node(flags), nid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3145)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3146) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3147) * Another processor may allocate the objects in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3148) * the slab since we are not holding any locks.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3149) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3150) if (!obj)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3151) goto retry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3152) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3155) if (unlikely(!obj && read_mems_allowed_retry(cpuset_mems_cookie)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3156) goto retry_cpuset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3157) return obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3159)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3160) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3161) * A interface to enable slab creation on nodeid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3162) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3163) static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3164) int nodeid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3165) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3166) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3167) struct kmem_cache_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3168) void *obj = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3169) void *list = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3171) VM_BUG_ON(nodeid < 0 || nodeid >= MAX_NUMNODES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3172) n = get_node(cachep, nodeid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3173) BUG_ON(!n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3174)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3175) check_irq_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3176) spin_lock(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3177) page = get_first_slab(n, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3178) if (!page)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3179) goto must_grow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3181) check_spinlock_acquired_node(cachep, nodeid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3183) STATS_INC_NODEALLOCS(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3184) STATS_INC_ACTIVE(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3185) STATS_SET_HIGH(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3187) BUG_ON(page->active == cachep->num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3189) obj = slab_get_obj(cachep, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3190) n->free_objects--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3192) fixup_slab_list(cachep, n, page, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3194) spin_unlock(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3195) fixup_objfreelist_debug(cachep, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3196) return obj;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3198) must_grow:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3199) spin_unlock(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3200) page = cache_grow_begin(cachep, gfp_exact_node(flags), nodeid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3201) if (page) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3202) /* This slab isn't counted yet so don't update free_objects */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3203) obj = slab_get_obj(cachep, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3205) cache_grow_end(cachep, page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3207) return obj ? obj : fallback_alloc(cachep, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3208) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3209)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3210) static __always_inline void *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3211) slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid, size_t orig_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3212) unsigned long caller)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3213) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3214) unsigned long save_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3215) void *ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3216) int slab_node = numa_mem_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3217) struct obj_cgroup *objcg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3218) bool init = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3219)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3220) flags &= gfp_allowed_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3221) cachep = slab_pre_alloc_hook(cachep, &objcg, 1, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3222) if (unlikely(!cachep))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3223) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3224)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3225) ptr = kfence_alloc(cachep, orig_size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3226) if (unlikely(ptr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3227) goto out_hooks;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3229) cache_alloc_debugcheck_before(cachep, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3230) local_irq_save(save_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3232) if (nodeid == NUMA_NO_NODE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3233) nodeid = slab_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3235) if (unlikely(!get_node(cachep, nodeid))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3236) /* Node not bootstrapped yet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3237) ptr = fallback_alloc(cachep, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3238) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3239) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3240)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3241) if (nodeid == slab_node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3242) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3243) * Use the locally cached objects if possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3244) * However ____cache_alloc does not allow fallback
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3245) * to other nodes. It may fail while we still have
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3246) * objects on other nodes available.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3247) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3248) ptr = ____cache_alloc(cachep, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3249) if (ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3250) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3252) /* ___cache_alloc_node can fall back to other nodes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3253) ptr = ____cache_alloc_node(cachep, flags, nodeid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3254) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3255) local_irq_restore(save_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3256) ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3257) init = slab_want_init_on_alloc(flags, cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3259) out_hooks:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3260) slab_post_alloc_hook(cachep, objcg, flags, 1, &ptr, init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3261) return ptr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3263)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3264) static __always_inline void *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3265) __do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3266) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3267) void *objp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3269) if (current->mempolicy || cpuset_do_slab_mem_spread()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3270) objp = alternate_node_alloc(cache, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3271) if (objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3272) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3274) objp = ____cache_alloc(cache, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3276) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3277) * We may just have run out of memory on the local node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3278) * ____cache_alloc_node() knows how to locate memory on other nodes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3279) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3280) if (!objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3281) objp = ____cache_alloc_node(cache, flags, numa_mem_id());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3283) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3284) return objp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3286) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3287)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3288) static __always_inline void *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3289) __do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3290) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3291) return ____cache_alloc(cachep, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3292) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3294) #endif /* CONFIG_NUMA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3296) static __always_inline void *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3297) slab_alloc(struct kmem_cache *cachep, gfp_t flags, size_t orig_size, unsigned long caller)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3298) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3299) unsigned long save_flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3300) void *objp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3301) struct obj_cgroup *objcg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3302) bool init = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3303)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3304) flags &= gfp_allowed_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3305) cachep = slab_pre_alloc_hook(cachep, &objcg, 1, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3306) if (unlikely(!cachep))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3307) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3308)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3309) objp = kfence_alloc(cachep, orig_size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3310) if (unlikely(objp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3311) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3312)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3313) cache_alloc_debugcheck_before(cachep, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3314) local_irq_save(save_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3315) objp = __do_cache_alloc(cachep, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3316) local_irq_restore(save_flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3317) objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3318) prefetchw(objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3319) init = slab_want_init_on_alloc(flags, cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3321) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3322) slab_post_alloc_hook(cachep, objcg, flags, 1, &objp, init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3323) return objp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3324) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3326) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3327) * Caller needs to acquire correct kmem_cache_node's list_lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3328) * @list: List of detached free slabs should be freed by caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3329) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3330) static void free_block(struct kmem_cache *cachep, void **objpp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3331) int nr_objects, int node, struct list_head *list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3332) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3333) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3334) struct kmem_cache_node *n = get_node(cachep, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3335) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3337) n->free_objects += nr_objects;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3339) for (i = 0; i < nr_objects; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3340) void *objp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3341) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3342)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3343) objp = objpp[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3345) page = virt_to_head_page(objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3346) list_del(&page->slab_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3347) check_spinlock_acquired_node(cachep, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3348) slab_put_obj(cachep, page, objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3349) STATS_DEC_ACTIVE(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3351) /* fixup slab chains */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3352) if (page->active == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3353) list_add(&page->slab_list, &n->slabs_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3354) n->free_slabs++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3355) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3356) /* Unconditionally move a slab to the end of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3357) * partial list on free - maximum time for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3358) * other objects to be freed, too.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3359) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3360) list_add_tail(&page->slab_list, &n->slabs_partial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3361) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3362) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3364) while (n->free_objects > n->free_limit && !list_empty(&n->slabs_free)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3365) n->free_objects -= cachep->num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3366)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3367) page = list_last_entry(&n->slabs_free, struct page, slab_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3368) list_move(&page->slab_list, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3369) n->free_slabs--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3370) n->total_slabs--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3371) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3372) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3374) static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3375) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3376) int batchcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3377) struct kmem_cache_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3378) int node = numa_mem_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3379) LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3380)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3381) batchcount = ac->batchcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3383) check_irq_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3384) n = get_node(cachep, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3385) spin_lock(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3386) if (n->shared) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3387) struct array_cache *shared_array = n->shared;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3388) int max = shared_array->limit - shared_array->avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3389) if (max) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3390) if (batchcount > max)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3391) batchcount = max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3392) memcpy(&(shared_array->entry[shared_array->avail]),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3393) ac->entry, sizeof(void *) * batchcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3394) shared_array->avail += batchcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3395) goto free_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3396) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3399) free_block(cachep, ac->entry, batchcount, node, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3400) free_done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3401) #if STATS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3402) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3403) int i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3404) struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3406) list_for_each_entry(page, &n->slabs_free, slab_list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3407) BUG_ON(page->active);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3408)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3409) i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3411) STATS_SET_FREEABLE(cachep, i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3412) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3413) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3414) spin_unlock(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3415) ac->avail -= batchcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3416) memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3417) slabs_destroy(cachep, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3418) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3419)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3420) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3421) * Release an obj back to its cache. If the obj has a constructed state, it must
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3422) * be in this state _before_ it is released. Called with disabled ints.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3423) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3424) static __always_inline void __cache_free(struct kmem_cache *cachep, void *objp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3425) unsigned long caller)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3426) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3427) bool init;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3428)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3429) if (is_kfence_address(objp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3430) kmemleak_free_recursive(objp, cachep->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3431) __kfence_free(objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3432) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3433) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3434)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3435) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3436) * As memory initialization might be integrated into KASAN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3437) * kasan_slab_free and initialization memset must be
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3438) * kept together to avoid discrepancies in behavior.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3439) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3440) init = slab_want_init_on_free(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3441) if (init && !kasan_has_integrated_init())
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3442) memset(objp, 0, cachep->object_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3443) /* KASAN might put objp into memory quarantine, delaying its reuse. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3444) if (kasan_slab_free(cachep, objp, init))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3445) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3446)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3447) /* Use KCSAN to help debug racy use-after-free. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3448) if (!(cachep->flags & SLAB_TYPESAFE_BY_RCU))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3449) __kcsan_check_access(objp, cachep->object_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3450) KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ASSERT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3451)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3452) ___cache_free(cachep, objp, caller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3454)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3455) void ___cache_free(struct kmem_cache *cachep, void *objp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3456) unsigned long caller)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3457) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3458) struct array_cache *ac = cpu_cache_get(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3459)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3460) check_irq_off();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3461) kmemleak_free_recursive(objp, cachep->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3462) objp = cache_free_debugcheck(cachep, objp, caller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3463) memcg_slab_free_hook(cachep, &objp, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3464)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3465) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3466) * Skip calling cache_free_alien() when the platform is not numa.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3467) * This will avoid cache misses that happen while accessing slabp (which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3468) * is per page memory reference) to get nodeid. Instead use a global
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3469) * variable to skip the call, which is mostly likely to be present in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3470) * the cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3471) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3472) if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3473) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3474)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3475) if (ac->avail < ac->limit) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3476) STATS_INC_FREEHIT(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3477) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3478) STATS_INC_FREEMISS(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3479) cache_flusharray(cachep, ac);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3480) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3481)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3482) if (sk_memalloc_socks()) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3483) struct page *page = virt_to_head_page(objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3484)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3485) if (unlikely(PageSlabPfmemalloc(page))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3486) cache_free_pfmemalloc(cachep, page, objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3487) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3488) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3489) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3490)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3491) __free_one(ac, objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3492) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3493)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3494) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3495) * kmem_cache_alloc - Allocate an object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3496) * @cachep: The cache to allocate from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3497) * @flags: See kmalloc().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3498) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3499) * Allocate an object from this cache. The flags are only relevant
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3500) * if the cache has no available objects.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3501) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3502) * Return: pointer to the new object or %NULL in case of error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3503) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3504) void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3505) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3506) void *ret = slab_alloc(cachep, flags, cachep->object_size, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3507)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3508) trace_kmem_cache_alloc(_RET_IP_, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3509) cachep->object_size, cachep->size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3510)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3511) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3512) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3513) EXPORT_SYMBOL(kmem_cache_alloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3514)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3515) static __always_inline void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3516) cache_alloc_debugcheck_after_bulk(struct kmem_cache *s, gfp_t flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3517) size_t size, void **p, unsigned long caller)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3518) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3519) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3520)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3521) for (i = 0; i < size; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3522) p[i] = cache_alloc_debugcheck_after(s, flags, p[i], caller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3523) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3524)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3525) int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3526) void **p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3527) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3528) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3529) struct obj_cgroup *objcg = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3530)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3531) s = slab_pre_alloc_hook(s, &objcg, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3532) if (!s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3533) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3534)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3535) cache_alloc_debugcheck_before(s, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3536)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3537) local_irq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3538) for (i = 0; i < size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3539) void *objp = kfence_alloc(s, s->object_size, flags) ?: __do_cache_alloc(s, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3540)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3541) if (unlikely(!objp))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3542) goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3543) p[i] = objp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3544) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3545) local_irq_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3546)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3547) cache_alloc_debugcheck_after_bulk(s, flags, size, p, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3549) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3550) * memcg and kmem_cache debug support and memory initialization.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3551) * Done outside of the IRQ disabled section.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3552) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3553) slab_post_alloc_hook(s, objcg, flags, size, p,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3554) slab_want_init_on_alloc(flags, s));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3555) /* FIXME: Trace call missing. Christoph would like a bulk variant */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3556) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3557) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3558) local_irq_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3559) cache_alloc_debugcheck_after_bulk(s, flags, i, p, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3560) slab_post_alloc_hook(s, objcg, flags, i, p, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3561) __kmem_cache_free_bulk(s, i, p);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3562) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3563) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3564) EXPORT_SYMBOL(kmem_cache_alloc_bulk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3565)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3566) #ifdef CONFIG_TRACING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3567) void *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3568) kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3569) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3570) void *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3571)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3572) ret = slab_alloc(cachep, flags, size, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3573)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3574) ret = kasan_kmalloc(cachep, ret, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3575) trace_kmalloc(_RET_IP_, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3576) size, cachep->size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3577) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3578) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3579) EXPORT_SYMBOL(kmem_cache_alloc_trace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3580) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3581)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3582) #ifdef CONFIG_NUMA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3583) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3584) * kmem_cache_alloc_node - Allocate an object on the specified node
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3585) * @cachep: The cache to allocate from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3586) * @flags: See kmalloc().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3587) * @nodeid: node number of the target node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3588) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3589) * Identical to kmem_cache_alloc but it will allocate memory on the given
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3590) * node, which can improve the performance for cpu bound structures.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3591) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3592) * Fallback to other node is possible if __GFP_THISNODE is not set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3593) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3594) * Return: pointer to the new object or %NULL in case of error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3595) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3596) void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3597) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3598) void *ret = slab_alloc_node(cachep, flags, nodeid, cachep->object_size, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3599)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3600) trace_kmem_cache_alloc_node(_RET_IP_, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3601) cachep->object_size, cachep->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3602) flags, nodeid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3603)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3604) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3605) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3606) EXPORT_SYMBOL(kmem_cache_alloc_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3608) #ifdef CONFIG_TRACING
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3609) void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3610) gfp_t flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3611) int nodeid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3612) size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3613) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3614) void *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3616) ret = slab_alloc_node(cachep, flags, nodeid, size, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3617)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3618) ret = kasan_kmalloc(cachep, ret, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3619) trace_kmalloc_node(_RET_IP_, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3620) size, cachep->size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3621) flags, nodeid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3622) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3623) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3624) EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3625) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3626)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3627) static __always_inline void *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3628) __do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3629) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3630) struct kmem_cache *cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3631) void *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3632)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3633) if (unlikely(size > KMALLOC_MAX_CACHE_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3634) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3635) cachep = kmalloc_slab(size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3636) if (unlikely(ZERO_OR_NULL_PTR(cachep)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3637) return cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3638) ret = kmem_cache_alloc_node_trace(cachep, flags, node, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3639) ret = kasan_kmalloc(cachep, ret, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3640)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3641) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3642) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3643)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3644) void *__kmalloc_node(size_t size, gfp_t flags, int node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3645) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3646) return __do_kmalloc_node(size, flags, node, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3647) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3648) EXPORT_SYMBOL(__kmalloc_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3649)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3650) void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3651) int node, unsigned long caller)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3652) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3653) return __do_kmalloc_node(size, flags, node, caller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3654) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3655) EXPORT_SYMBOL(__kmalloc_node_track_caller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3656) #endif /* CONFIG_NUMA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3657)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3658) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3659) * __do_kmalloc - allocate memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3660) * @size: how many bytes of memory are required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3661) * @flags: the type of memory to allocate (see kmalloc).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3662) * @caller: function caller for debug tracking of the caller
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3663) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3664) * Return: pointer to the allocated memory or %NULL in case of error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3665) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3666) static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3667) unsigned long caller)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3668) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3669) struct kmem_cache *cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3670) void *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3671)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3672) if (unlikely(size > KMALLOC_MAX_CACHE_SIZE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3673) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3674) cachep = kmalloc_slab(size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3675) if (unlikely(ZERO_OR_NULL_PTR(cachep)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3676) return cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3677) ret = slab_alloc(cachep, flags, size, caller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3678)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3679) ret = kasan_kmalloc(cachep, ret, size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3680) trace_kmalloc(caller, ret,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3681) size, cachep->size, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3682)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3683) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3684) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3685)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3686) void *__kmalloc(size_t size, gfp_t flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3687) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3688) return __do_kmalloc(size, flags, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3689) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3690) EXPORT_SYMBOL(__kmalloc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3691)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3692) void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3694) return __do_kmalloc(size, flags, caller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3695) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3696) EXPORT_SYMBOL(__kmalloc_track_caller);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3697)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3698) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3699) * kmem_cache_free - Deallocate an object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3700) * @cachep: The cache the allocation was from.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3701) * @objp: The previously allocated object.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3702) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3703) * Free an object which was previously allocated from this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3704) * cache.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3705) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3706) void kmem_cache_free(struct kmem_cache *cachep, void *objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3707) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3708) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3709) cachep = cache_from_obj(cachep, objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3710) if (!cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3711) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3712)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3713) local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3714) debug_check_no_locks_freed(objp, cachep->object_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3715) if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3716) debug_check_no_obj_freed(objp, cachep->object_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3717) __cache_free(cachep, objp, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3718) local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3719)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3720) trace_kmem_cache_free(_RET_IP_, objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3721) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3722) EXPORT_SYMBOL(kmem_cache_free);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3723)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3724) void kmem_cache_free_bulk(struct kmem_cache *orig_s, size_t size, void **p)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3725) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3726) struct kmem_cache *s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3727) size_t i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3728)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3729) local_irq_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3730) for (i = 0; i < size; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3731) void *objp = p[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3732)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3733) if (!orig_s) /* called via kfree_bulk */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3734) s = virt_to_cache(objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3735) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3736) s = cache_from_obj(orig_s, objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3737) if (!s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3738) continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3739)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3740) debug_check_no_locks_freed(objp, s->object_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3741) if (!(s->flags & SLAB_DEBUG_OBJECTS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3742) debug_check_no_obj_freed(objp, s->object_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3743)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3744) __cache_free(s, objp, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3745) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3746) local_irq_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3747)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3748) /* FIXME: add tracing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3749) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3750) EXPORT_SYMBOL(kmem_cache_free_bulk);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3751)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3752) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3753) * kfree - free previously allocated memory
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3754) * @objp: pointer returned by kmalloc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3755) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3756) * If @objp is NULL, no operation is performed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3757) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3758) * Don't free memory not originally allocated by kmalloc()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3759) * or you will run into trouble.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3760) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3761) void kfree(const void *objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3762) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3763) struct kmem_cache *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3764) unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3765)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3766) trace_kfree(_RET_IP_, objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3767)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3768) if (unlikely(ZERO_OR_NULL_PTR(objp)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3769) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3770) local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3771) kfree_debugcheck(objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3772) c = virt_to_cache(objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3773) if (!c) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3774) local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3775) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3776) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3777) debug_check_no_locks_freed(objp, c->object_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3778)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3779) debug_check_no_obj_freed(objp, c->object_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3780) __cache_free(c, (void *)objp, _RET_IP_);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3781) local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3782) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3783) EXPORT_SYMBOL(kfree);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3784)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3785) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3786) * This initializes kmem_cache_node or resizes various caches for all nodes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3787) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3788) static int setup_kmem_cache_nodes(struct kmem_cache *cachep, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3789) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3790) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3791) int node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3792) struct kmem_cache_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3793)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3794) for_each_online_node(node) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3795) ret = setup_kmem_cache_node(cachep, node, gfp, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3796) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3797) goto fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3798)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3799) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3800)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3801) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3802)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3803) fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3804) if (!cachep->list.next) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3805) /* Cache is not active yet. Roll back what we did */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3806) node--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3807) while (node >= 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3808) n = get_node(cachep, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3809) if (n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3810) kfree(n->shared);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3811) free_alien_cache(n->alien);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3812) kfree(n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3813) cachep->node[node] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3814) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3815) node--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3816) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3817) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3818) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3819) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3820)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3821) /* Always called with the slab_mutex held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3822) static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3823) int batchcount, int shared, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3824) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3825) struct array_cache __percpu *cpu_cache, *prev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3826) int cpu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3827)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3828) cpu_cache = alloc_kmem_cache_cpus(cachep, limit, batchcount);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3829) if (!cpu_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3830) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3831)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3832) prev = cachep->cpu_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3833) cachep->cpu_cache = cpu_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3834) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3835) * Without a previous cpu_cache there's no need to synchronize remote
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3836) * cpus, so skip the IPIs.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3837) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3838) if (prev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3839) kick_all_cpus_sync();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3840)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3841) check_irq_on();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3842) cachep->batchcount = batchcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3843) cachep->limit = limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3844) cachep->shared = shared;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3845)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3846) if (!prev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3847) goto setup_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3848)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3849) for_each_online_cpu(cpu) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3850) LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3851) int node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3852) struct kmem_cache_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3853) struct array_cache *ac = per_cpu_ptr(prev, cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3854)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3855) node = cpu_to_mem(cpu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3856) n = get_node(cachep, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3857) spin_lock_irq(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3858) free_block(cachep, ac->entry, ac->avail, node, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3859) spin_unlock_irq(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3860) slabs_destroy(cachep, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3861) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3862) free_percpu(prev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3863)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3864) setup_node:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3865) return setup_kmem_cache_nodes(cachep, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3866) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3867)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3868) /* Called with slab_mutex held always */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3869) static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3870) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3871) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3872) int limit = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3873) int shared = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3874) int batchcount = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3875)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3876) err = cache_random_seq_create(cachep, cachep->num, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3877) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3878) goto end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3879)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3880) if (limit && shared && batchcount)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3881) goto skip_setup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3882) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3883) * The head array serves three purposes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3884) * - create a LIFO ordering, i.e. return objects that are cache-warm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3885) * - reduce the number of spinlock operations.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3886) * - reduce the number of linked list operations on the slab and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3887) * bufctl chains: array operations are cheaper.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3888) * The numbers are guessed, we should auto-tune as described by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3889) * Bonwick.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3890) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3891) if (cachep->size > 131072)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3892) limit = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3893) else if (cachep->size > PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3894) limit = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3895) else if (cachep->size > 1024)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3896) limit = 24;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3897) else if (cachep->size > 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3898) limit = 54;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3899) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3900) limit = 120;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3901)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3902) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3903) * CPU bound tasks (e.g. network routing) can exhibit cpu bound
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3904) * allocation behaviour: Most allocs on one cpu, most free operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3905) * on another cpu. For these cases, an efficient object passing between
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3906) * cpus is necessary. This is provided by a shared array. The array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3907) * replaces Bonwick's magazine layer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3908) * On uniprocessor, it's functionally equivalent (but less efficient)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3909) * to a larger limit. Thus disabled by default.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3910) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3911) shared = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3912) if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3913) shared = 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3914)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3915) #if DEBUG
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3916) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3917) * With debugging enabled, large batchcount lead to excessively long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3918) * periods with disabled local interrupts. Limit the batchcount
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3919) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3920) if (limit > 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3921) limit = 32;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3922) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3923) batchcount = (limit + 1) / 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3924) skip_setup:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3925) err = do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3926) end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3927) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3928) pr_err("enable_cpucache failed for %s, error %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3929) cachep->name, -err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3930) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3931) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3932)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3933) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3934) * Drain an array if it contains any elements taking the node lock only if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3935) * necessary. Note that the node listlock also protects the array_cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3936) * if drain_array() is used on the shared array.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3937) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3938) static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3939) struct array_cache *ac, int node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3940) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3941) LIST_HEAD(list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3942)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3943) /* ac from n->shared can be freed if we don't hold the slab_mutex. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3944) check_mutex_acquired();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3945)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3946) if (!ac || !ac->avail)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3947) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3948)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3949) if (ac->touched) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3950) ac->touched = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3951) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3952) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3953)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3954) spin_lock_irq(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3955) drain_array_locked(cachep, ac, node, false, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3956) spin_unlock_irq(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3957)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3958) slabs_destroy(cachep, &list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3959) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3960)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3961) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3962) * cache_reap - Reclaim memory from caches.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3963) * @w: work descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3964) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3965) * Called from workqueue/eventd every few seconds.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3966) * Purpose:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3967) * - clear the per-cpu caches for this CPU.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3968) * - return freeable pages to the main free memory pool.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3969) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3970) * If we cannot acquire the cache chain mutex then just give up - we'll try
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3971) * again on the next iteration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3972) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3973) static void cache_reap(struct work_struct *w)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3974) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3975) struct kmem_cache *searchp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3976) struct kmem_cache_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3977) int node = numa_mem_id();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3978) struct delayed_work *work = to_delayed_work(w);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3979)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3980) if (!mutex_trylock(&slab_mutex))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3981) /* Give up. Setup the next iteration. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3982) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3983)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3984) list_for_each_entry(searchp, &slab_caches, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3985) check_irq_on();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3986)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3987) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3988) * We only take the node lock if absolutely necessary and we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3989) * have established with reasonable certainty that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3990) * we can do some work if the lock was obtained.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3991) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3992) n = get_node(searchp, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3993)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3994) reap_alien(searchp, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3995)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3996) drain_array(searchp, n, cpu_cache_get(searchp), node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3997)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3998) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3999) * These are racy checks but it does not matter
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4000) * if we skip one check or scan twice.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4001) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4002) if (time_after(n->next_reap, jiffies))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4003) goto next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4004)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4005) n->next_reap = jiffies + REAPTIMEOUT_NODE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4006)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4007) drain_array(searchp, n, n->shared, node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4008)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4009) if (n->free_touched)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4010) n->free_touched = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4011) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4012) int freed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4013)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4014) freed = drain_freelist(searchp, n, (n->free_limit +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4015) 5 * searchp->num - 1) / (5 * searchp->num));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4016) STATS_ADD_REAPED(searchp, freed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4017) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4018) next:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4019) cond_resched();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4020) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4021) check_irq_on();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4022) mutex_unlock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4023) next_reap_node();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4024) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4025) /* Set up the next iteration */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4026) schedule_delayed_work_on(smp_processor_id(), work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4027) round_jiffies_relative(REAPTIMEOUT_AC));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4028) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4029)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4030) void get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4031) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4032) unsigned long active_objs, num_objs, active_slabs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4033) unsigned long total_slabs = 0, free_objs = 0, shared_avail = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4034) unsigned long free_slabs = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4035) int node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4036) struct kmem_cache_node *n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4037)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4038) for_each_kmem_cache_node(cachep, node, n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4039) check_irq_on();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4040) spin_lock_irq(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4041)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4042) total_slabs += n->total_slabs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4043) free_slabs += n->free_slabs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4044) free_objs += n->free_objects;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4045)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4046) if (n->shared)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4047) shared_avail += n->shared->avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4048)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4049) spin_unlock_irq(&n->list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4050) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4051) num_objs = total_slabs * cachep->num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4052) active_slabs = total_slabs - free_slabs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4053) active_objs = num_objs - free_objs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4054)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4055) sinfo->active_objs = active_objs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4056) sinfo->num_objs = num_objs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4057) sinfo->active_slabs = active_slabs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4058) sinfo->num_slabs = total_slabs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4059) sinfo->shared_avail = shared_avail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4060) sinfo->limit = cachep->limit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4061) sinfo->batchcount = cachep->batchcount;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4062) sinfo->shared = cachep->shared;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4063) sinfo->objects_per_slab = cachep->num;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4064) sinfo->cache_order = cachep->gfporder;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4065) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4066) EXPORT_SYMBOL_GPL(get_slabinfo);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4067)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4068) void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *cachep)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4069) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4070) #if STATS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4071) { /* node stats */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4072) unsigned long high = cachep->high_mark;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4073) unsigned long allocs = cachep->num_allocations;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4074) unsigned long grown = cachep->grown;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4075) unsigned long reaped = cachep->reaped;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4076) unsigned long errors = cachep->errors;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4077) unsigned long max_freeable = cachep->max_freeable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4078) unsigned long node_allocs = cachep->node_allocs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4079) unsigned long node_frees = cachep->node_frees;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4080) unsigned long overflows = cachep->node_overflow;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4081)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4082) seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu %4lu %4lu %4lu %4lu %4lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4083) allocs, high, grown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4084) reaped, errors, max_freeable, node_allocs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4085) node_frees, overflows);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4086) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4087) /* cpu stats */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4088) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4089) unsigned long allochit = atomic_read(&cachep->allochit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4090) unsigned long allocmiss = atomic_read(&cachep->allocmiss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4091) unsigned long freehit = atomic_read(&cachep->freehit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4092) unsigned long freemiss = atomic_read(&cachep->freemiss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4093)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4094) seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4095) allochit, allocmiss, freehit, freemiss);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4096) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4097) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4098) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4099)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4100) #define MAX_SLABINFO_WRITE 128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4101) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4102) * slabinfo_write - Tuning for the slab allocator
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4103) * @file: unused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4104) * @buffer: user buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4105) * @count: data length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4106) * @ppos: unused
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4107) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4108) * Return: %0 on success, negative error code otherwise.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4109) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4110) ssize_t slabinfo_write(struct file *file, const char __user *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4111) size_t count, loff_t *ppos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4112) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4113) char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4114) int limit, batchcount, shared, res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4115) struct kmem_cache *cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4116)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4117) if (count > MAX_SLABINFO_WRITE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4118) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4119) if (copy_from_user(&kbuf, buffer, count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4120) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4121) kbuf[MAX_SLABINFO_WRITE] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4123) tmp = strchr(kbuf, ' ');
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4124) if (!tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4125) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4126) *tmp = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4127) tmp++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4128) if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4129) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4131) /* Find the cache in the chain of caches. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4132) mutex_lock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4133) res = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4134) list_for_each_entry(cachep, &slab_caches, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4135) if (!strcmp(cachep->name, kbuf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4136) if (limit < 1 || batchcount < 1 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4137) batchcount > limit || shared < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4138) res = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4139) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4140) res = do_tune_cpucache(cachep, limit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4141) batchcount, shared,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4142) GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4143) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4144) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4147) mutex_unlock(&slab_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4148) if (res >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4149) res = count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4150) return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4151) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4153) #ifdef CONFIG_HARDENED_USERCOPY
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4154) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4155) * Rejects incorrectly sized objects and objects that are to be copied
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4156) * to/from userspace but do not fall entirely within the containing slab
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4157) * cache's usercopy region.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4158) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4159) * Returns NULL if check passes, otherwise const char * to name of cache
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4160) * to indicate an error.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4161) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4162) void __check_heap_object(const void *ptr, unsigned long n, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4163) bool to_user)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4164) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4165) struct kmem_cache *cachep;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4166) unsigned int objnr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4167) unsigned long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4169) ptr = kasan_reset_tag(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4170)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4171) /* Find and validate object. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4172) cachep = page->slab_cache;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4173) objnr = obj_to_index(cachep, page, (void *)ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4174) BUG_ON(objnr >= cachep->num);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4176) /* Find offset within object. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4177) if (is_kfence_address(ptr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4178) offset = ptr - kfence_object_start(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4179) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4180) offset = ptr - index_to_obj(cachep, page, objnr) - obj_offset(cachep);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4181)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4182) /* Allow address range falling entirely within usercopy region. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4183) if (offset >= cachep->useroffset &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4184) offset - cachep->useroffset <= cachep->usersize &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4185) n <= cachep->useroffset - offset + cachep->usersize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4186) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4187)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4188) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4189) * If the copy is still within the allocated object, produce
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4190) * a warning instead of rejecting the copy. This is intended
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4191) * to be a temporary method to find any missing usercopy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4192) * whitelists.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4193) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4194) if (usercopy_fallback &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4195) offset <= cachep->object_size &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4196) n <= cachep->object_size - offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4197) usercopy_warn("SLAB object", cachep->name, to_user, offset, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4198) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4199) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4200)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4201) usercopy_abort("SLAB object", cachep->name, to_user, offset, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4203) #endif /* CONFIG_HARDENED_USERCOPY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4205) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4206) * __ksize -- Uninstrumented ksize.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4207) * @objp: pointer to the object
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4208) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4209) * Unlike ksize(), __ksize() is uninstrumented, and does not provide the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4210) * safety checks as ksize() with KASAN instrumentation enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4211) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4212) * Return: size of the actual memory used by @objp in bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4213) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4214) size_t __ksize(const void *objp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4216) struct kmem_cache *c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4217) size_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4219) BUG_ON(!objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4220) if (unlikely(objp == ZERO_SIZE_PTR))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4221) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4222)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4223) c = virt_to_cache(objp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4224) size = c ? c->object_size : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4226) return size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4227) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4228) EXPORT_SYMBOL(__ksize);