18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * linux/mm/slab.c 48c2ecf20Sopenharmony_ci * Written by Mark Hemment, 1996/97. 58c2ecf20Sopenharmony_ci * (markhe@nextd.demon.co.uk) 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * kmem_cache_destroy() + some cleanup - 1999 Andrea Arcangeli 88c2ecf20Sopenharmony_ci * 98c2ecf20Sopenharmony_ci * Major cleanup, different bufctl logic, per-cpu arrays 108c2ecf20Sopenharmony_ci * (c) 2000 Manfred Spraul 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * Cleanup, make the head arrays unconditional, preparation for NUMA 138c2ecf20Sopenharmony_ci * (c) 2002 Manfred Spraul 148c2ecf20Sopenharmony_ci * 158c2ecf20Sopenharmony_ci * An implementation of the Slab Allocator as described in outline in; 168c2ecf20Sopenharmony_ci * UNIX Internals: The New Frontiers by Uresh Vahalia 178c2ecf20Sopenharmony_ci * Pub: Prentice Hall ISBN 0-13-101908-2 188c2ecf20Sopenharmony_ci * or with a little more detail in; 198c2ecf20Sopenharmony_ci * The Slab Allocator: An Object-Caching Kernel Memory Allocator 208c2ecf20Sopenharmony_ci * Jeff Bonwick (Sun Microsystems). 218c2ecf20Sopenharmony_ci * Presented at: USENIX Summer 1994 Technical Conference 228c2ecf20Sopenharmony_ci * 238c2ecf20Sopenharmony_ci * The memory is organized in caches, one cache for each object type. 248c2ecf20Sopenharmony_ci * (e.g. inode_cache, dentry_cache, buffer_head, vm_area_struct) 258c2ecf20Sopenharmony_ci * Each cache consists out of many slabs (they are small (usually one 268c2ecf20Sopenharmony_ci * page long) and always contiguous), and each slab contains multiple 278c2ecf20Sopenharmony_ci * initialized objects. 288c2ecf20Sopenharmony_ci * 298c2ecf20Sopenharmony_ci * This means, that your constructor is used only for newly allocated 308c2ecf20Sopenharmony_ci * slabs and you must pass objects with the same initializations to 318c2ecf20Sopenharmony_ci * kmem_cache_free. 328c2ecf20Sopenharmony_ci * 338c2ecf20Sopenharmony_ci * Each cache can only support one memory type (GFP_DMA, GFP_HIGHMEM, 348c2ecf20Sopenharmony_ci * normal). If you need a special memory type, then must create a new 358c2ecf20Sopenharmony_ci * cache for that memory type. 368c2ecf20Sopenharmony_ci * 378c2ecf20Sopenharmony_ci * In order to reduce fragmentation, the slabs are sorted in 3 groups: 388c2ecf20Sopenharmony_ci * full slabs with 0 free objects 398c2ecf20Sopenharmony_ci * partial slabs 408c2ecf20Sopenharmony_ci * empty slabs with no allocated objects 418c2ecf20Sopenharmony_ci * 428c2ecf20Sopenharmony_ci * If partial slabs exist, then new allocations come from these slabs, 438c2ecf20Sopenharmony_ci * otherwise from empty slabs or new slabs are allocated. 448c2ecf20Sopenharmony_ci * 458c2ecf20Sopenharmony_ci * kmem_cache_destroy() CAN CRASH if you try to allocate from the cache 468c2ecf20Sopenharmony_ci * during kmem_cache_destroy(). The caller must prevent concurrent allocs. 478c2ecf20Sopenharmony_ci * 488c2ecf20Sopenharmony_ci * Each cache has a short per-cpu head array, most allocs 498c2ecf20Sopenharmony_ci * and frees go into that array, and if that array overflows, then 1/2 508c2ecf20Sopenharmony_ci * of the entries in the array are given back into the global cache. 518c2ecf20Sopenharmony_ci * The head array is strictly LIFO and should improve the cache hit rates. 528c2ecf20Sopenharmony_ci * On SMP, it additionally reduces the spinlock operations. 538c2ecf20Sopenharmony_ci * 548c2ecf20Sopenharmony_ci * The c_cpuarray may not be read with enabled local interrupts - 558c2ecf20Sopenharmony_ci * it's changed with a smp_call_function(). 568c2ecf20Sopenharmony_ci * 578c2ecf20Sopenharmony_ci * SMP synchronization: 588c2ecf20Sopenharmony_ci * constructors and destructors are called without any locking. 598c2ecf20Sopenharmony_ci * Several members in struct kmem_cache and struct slab never change, they 608c2ecf20Sopenharmony_ci * are accessed without any locking. 618c2ecf20Sopenharmony_ci * The per-cpu arrays are never accessed from the wrong cpu, no locking, 628c2ecf20Sopenharmony_ci * and local interrupts are disabled so slab code is preempt-safe. 638c2ecf20Sopenharmony_ci * The non-constant members are protected with a per-cache irq spinlock. 648c2ecf20Sopenharmony_ci * 658c2ecf20Sopenharmony_ci * Many thanks to Mark Hemment, who wrote another per-cpu slab patch 668c2ecf20Sopenharmony_ci * in 2000 - many ideas in the current implementation are derived from 678c2ecf20Sopenharmony_ci * his patch. 688c2ecf20Sopenharmony_ci * 698c2ecf20Sopenharmony_ci * Further notes from the original documentation: 708c2ecf20Sopenharmony_ci * 718c2ecf20Sopenharmony_ci * 11 April '97. Started multi-threading - markhe 728c2ecf20Sopenharmony_ci * The global cache-chain is protected by the mutex 'slab_mutex'. 738c2ecf20Sopenharmony_ci * The sem is only needed when accessing/extending the cache-chain, which 748c2ecf20Sopenharmony_ci * can never happen inside an interrupt (kmem_cache_create(), 758c2ecf20Sopenharmony_ci * kmem_cache_shrink() and kmem_cache_reap()). 768c2ecf20Sopenharmony_ci * 778c2ecf20Sopenharmony_ci * At present, each engine can be growing a cache. This should be blocked. 788c2ecf20Sopenharmony_ci * 798c2ecf20Sopenharmony_ci * 15 March 2005. NUMA slab allocator. 808c2ecf20Sopenharmony_ci * Shai Fultheim <shai@scalex86.org>. 818c2ecf20Sopenharmony_ci * Shobhit Dayal <shobhit@calsoftinc.com> 828c2ecf20Sopenharmony_ci * Alok N Kataria <alokk@calsoftinc.com> 838c2ecf20Sopenharmony_ci * Christoph Lameter <christoph@lameter.com> 848c2ecf20Sopenharmony_ci * 858c2ecf20Sopenharmony_ci * Modified the slab allocator to be node aware on NUMA systems. 868c2ecf20Sopenharmony_ci * Each node has its own list of partial, free and full slabs. 878c2ecf20Sopenharmony_ci * All object allocations for a node occur from node specific slab lists. 888c2ecf20Sopenharmony_ci */ 898c2ecf20Sopenharmony_ci 908c2ecf20Sopenharmony_ci#include <linux/slab.h> 918c2ecf20Sopenharmony_ci#include <linux/mm.h> 928c2ecf20Sopenharmony_ci#include <linux/poison.h> 938c2ecf20Sopenharmony_ci#include <linux/swap.h> 948c2ecf20Sopenharmony_ci#include <linux/cache.h> 958c2ecf20Sopenharmony_ci#include <linux/interrupt.h> 968c2ecf20Sopenharmony_ci#include <linux/init.h> 978c2ecf20Sopenharmony_ci#include <linux/compiler.h> 988c2ecf20Sopenharmony_ci#include <linux/cpuset.h> 998c2ecf20Sopenharmony_ci#include <linux/proc_fs.h> 1008c2ecf20Sopenharmony_ci#include <linux/seq_file.h> 1018c2ecf20Sopenharmony_ci#include <linux/notifier.h> 1028c2ecf20Sopenharmony_ci#include <linux/kallsyms.h> 1038c2ecf20Sopenharmony_ci#include <linux/cpu.h> 1048c2ecf20Sopenharmony_ci#include <linux/sysctl.h> 1058c2ecf20Sopenharmony_ci#include <linux/module.h> 1068c2ecf20Sopenharmony_ci#include <linux/rcupdate.h> 1078c2ecf20Sopenharmony_ci#include <linux/string.h> 1088c2ecf20Sopenharmony_ci#include <linux/uaccess.h> 1098c2ecf20Sopenharmony_ci#include <linux/nodemask.h> 1108c2ecf20Sopenharmony_ci#include <linux/kmemleak.h> 1118c2ecf20Sopenharmony_ci#include <linux/mempolicy.h> 1128c2ecf20Sopenharmony_ci#include <linux/mutex.h> 1138c2ecf20Sopenharmony_ci#include <linux/fault-inject.h> 1148c2ecf20Sopenharmony_ci#include <linux/rtmutex.h> 1158c2ecf20Sopenharmony_ci#include <linux/reciprocal_div.h> 1168c2ecf20Sopenharmony_ci#include <linux/debugobjects.h> 1178c2ecf20Sopenharmony_ci#include <linux/memory.h> 1188c2ecf20Sopenharmony_ci#include <linux/prefetch.h> 1198c2ecf20Sopenharmony_ci#include <linux/sched/task_stack.h> 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci#include <net/sock.h> 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci#include <asm/cacheflush.h> 1248c2ecf20Sopenharmony_ci#include <asm/tlbflush.h> 1258c2ecf20Sopenharmony_ci#include <asm/page.h> 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci#include <trace/events/kmem.h> 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci#include "internal.h" 1308c2ecf20Sopenharmony_ci 1318c2ecf20Sopenharmony_ci#include "slab.h" 1328c2ecf20Sopenharmony_ci 1338c2ecf20Sopenharmony_ci/* 1348c2ecf20Sopenharmony_ci * DEBUG - 1 for kmem_cache_create() to honour; SLAB_RED_ZONE & SLAB_POISON. 1358c2ecf20Sopenharmony_ci * 0 for faster, smaller code (especially in the critical paths). 1368c2ecf20Sopenharmony_ci * 1378c2ecf20Sopenharmony_ci * STATS - 1 to collect stats for /proc/slabinfo. 1388c2ecf20Sopenharmony_ci * 0 for faster, smaller code (especially in the critical paths). 1398c2ecf20Sopenharmony_ci * 1408c2ecf20Sopenharmony_ci * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible) 1418c2ecf20Sopenharmony_ci */ 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_SLAB 1448c2ecf20Sopenharmony_ci#define DEBUG 1 1458c2ecf20Sopenharmony_ci#define STATS 1 1468c2ecf20Sopenharmony_ci#define FORCED_DEBUG 1 1478c2ecf20Sopenharmony_ci#else 1488c2ecf20Sopenharmony_ci#define DEBUG 0 1498c2ecf20Sopenharmony_ci#define STATS 0 1508c2ecf20Sopenharmony_ci#define FORCED_DEBUG 0 1518c2ecf20Sopenharmony_ci#endif 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci/* Shouldn't this be in a header file somewhere? */ 1548c2ecf20Sopenharmony_ci#define BYTES_PER_WORD sizeof(void *) 1558c2ecf20Sopenharmony_ci#define REDZONE_ALIGN max(BYTES_PER_WORD, __alignof__(unsigned long long)) 1568c2ecf20Sopenharmony_ci 1578c2ecf20Sopenharmony_ci#ifndef ARCH_KMALLOC_FLAGS 1588c2ecf20Sopenharmony_ci#define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN 1598c2ecf20Sopenharmony_ci#endif 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci#define FREELIST_BYTE_INDEX (((PAGE_SIZE >> BITS_PER_BYTE) \ 1628c2ecf20Sopenharmony_ci <= SLAB_OBJ_MIN_SIZE) ? 1 : 0) 1638c2ecf20Sopenharmony_ci 1648c2ecf20Sopenharmony_ci#if FREELIST_BYTE_INDEX 1658c2ecf20Sopenharmony_citypedef unsigned char freelist_idx_t; 1668c2ecf20Sopenharmony_ci#else 1678c2ecf20Sopenharmony_citypedef unsigned short freelist_idx_t; 1688c2ecf20Sopenharmony_ci#endif 1698c2ecf20Sopenharmony_ci 1708c2ecf20Sopenharmony_ci#define SLAB_OBJ_MAX_NUM ((1 << sizeof(freelist_idx_t) * BITS_PER_BYTE) - 1) 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci/* 1738c2ecf20Sopenharmony_ci * struct array_cache 1748c2ecf20Sopenharmony_ci * 1758c2ecf20Sopenharmony_ci * Purpose: 1768c2ecf20Sopenharmony_ci * - LIFO ordering, to hand out cache-warm objects from _alloc 1778c2ecf20Sopenharmony_ci * - reduce the number of linked list operations 1788c2ecf20Sopenharmony_ci * - reduce spinlock operations 1798c2ecf20Sopenharmony_ci * 1808c2ecf20Sopenharmony_ci * The limit is stored in the per-cpu structure to reduce the data cache 1818c2ecf20Sopenharmony_ci * footprint. 1828c2ecf20Sopenharmony_ci * 1838c2ecf20Sopenharmony_ci */ 1848c2ecf20Sopenharmony_cistruct array_cache { 1858c2ecf20Sopenharmony_ci unsigned int avail; 1868c2ecf20Sopenharmony_ci unsigned int limit; 1878c2ecf20Sopenharmony_ci unsigned int batchcount; 1888c2ecf20Sopenharmony_ci unsigned int touched; 1898c2ecf20Sopenharmony_ci void *entry[]; /* 1908c2ecf20Sopenharmony_ci * Must have this definition in here for the proper 1918c2ecf20Sopenharmony_ci * alignment of array_cache. Also simplifies accessing 1928c2ecf20Sopenharmony_ci * the entries. 1938c2ecf20Sopenharmony_ci */ 1948c2ecf20Sopenharmony_ci}; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_cistruct alien_cache { 1978c2ecf20Sopenharmony_ci spinlock_t lock; 1988c2ecf20Sopenharmony_ci struct array_cache ac; 1998c2ecf20Sopenharmony_ci}; 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci/* 2028c2ecf20Sopenharmony_ci * Need this for bootstrapping a per node allocator. 2038c2ecf20Sopenharmony_ci */ 2048c2ecf20Sopenharmony_ci#define NUM_INIT_LISTS (2 * MAX_NUMNODES) 2058c2ecf20Sopenharmony_cistatic struct kmem_cache_node __initdata init_kmem_cache_node[NUM_INIT_LISTS]; 2068c2ecf20Sopenharmony_ci#define CACHE_CACHE 0 2078c2ecf20Sopenharmony_ci#define SIZE_NODE (MAX_NUMNODES) 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_cistatic int drain_freelist(struct kmem_cache *cache, 2108c2ecf20Sopenharmony_ci struct kmem_cache_node *n, int tofree); 2118c2ecf20Sopenharmony_cistatic void free_block(struct kmem_cache *cachep, void **objpp, int len, 2128c2ecf20Sopenharmony_ci int node, struct list_head *list); 2138c2ecf20Sopenharmony_cistatic void slabs_destroy(struct kmem_cache *cachep, struct list_head *list); 2148c2ecf20Sopenharmony_cistatic int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp); 2158c2ecf20Sopenharmony_cistatic void cache_reap(struct work_struct *unused); 2168c2ecf20Sopenharmony_ci 2178c2ecf20Sopenharmony_cistatic inline void fixup_objfreelist_debug(struct kmem_cache *cachep, 2188c2ecf20Sopenharmony_ci void **list); 2198c2ecf20Sopenharmony_cistatic inline void fixup_slab_list(struct kmem_cache *cachep, 2208c2ecf20Sopenharmony_ci struct kmem_cache_node *n, struct page *page, 2218c2ecf20Sopenharmony_ci void **list); 2228c2ecf20Sopenharmony_cistatic int slab_early_init = 1; 2238c2ecf20Sopenharmony_ci 2248c2ecf20Sopenharmony_ci#define INDEX_NODE kmalloc_index(sizeof(struct kmem_cache_node)) 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_cistatic void kmem_cache_node_init(struct kmem_cache_node *parent) 2278c2ecf20Sopenharmony_ci{ 2288c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&parent->slabs_full); 2298c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&parent->slabs_partial); 2308c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&parent->slabs_free); 2318c2ecf20Sopenharmony_ci parent->total_slabs = 0; 2328c2ecf20Sopenharmony_ci parent->free_slabs = 0; 2338c2ecf20Sopenharmony_ci parent->shared = NULL; 2348c2ecf20Sopenharmony_ci parent->alien = NULL; 2358c2ecf20Sopenharmony_ci parent->colour_next = 0; 2368c2ecf20Sopenharmony_ci spin_lock_init(&parent->list_lock); 2378c2ecf20Sopenharmony_ci parent->free_objects = 0; 2388c2ecf20Sopenharmony_ci parent->free_touched = 0; 2398c2ecf20Sopenharmony_ci} 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ci#define MAKE_LIST(cachep, listp, slab, nodeid) \ 2428c2ecf20Sopenharmony_ci do { \ 2438c2ecf20Sopenharmony_ci INIT_LIST_HEAD(listp); \ 2448c2ecf20Sopenharmony_ci list_splice(&get_node(cachep, nodeid)->slab, listp); \ 2458c2ecf20Sopenharmony_ci } while (0) 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ci#define MAKE_ALL_LISTS(cachep, ptr, nodeid) \ 2488c2ecf20Sopenharmony_ci do { \ 2498c2ecf20Sopenharmony_ci MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \ 2508c2ecf20Sopenharmony_ci MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \ 2518c2ecf20Sopenharmony_ci MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \ 2528c2ecf20Sopenharmony_ci } while (0) 2538c2ecf20Sopenharmony_ci 2548c2ecf20Sopenharmony_ci#define CFLGS_OBJFREELIST_SLAB ((slab_flags_t __force)0x40000000U) 2558c2ecf20Sopenharmony_ci#define CFLGS_OFF_SLAB ((slab_flags_t __force)0x80000000U) 2568c2ecf20Sopenharmony_ci#define OBJFREELIST_SLAB(x) ((x)->flags & CFLGS_OBJFREELIST_SLAB) 2578c2ecf20Sopenharmony_ci#define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB) 2588c2ecf20Sopenharmony_ci 2598c2ecf20Sopenharmony_ci#define BATCHREFILL_LIMIT 16 2608c2ecf20Sopenharmony_ci/* 2618c2ecf20Sopenharmony_ci * Optimization question: fewer reaps means less probability for unnessary 2628c2ecf20Sopenharmony_ci * cpucache drain/refill cycles. 2638c2ecf20Sopenharmony_ci * 2648c2ecf20Sopenharmony_ci * OTOH the cpuarrays can contain lots of objects, 2658c2ecf20Sopenharmony_ci * which could lock up otherwise freeable slabs. 2668c2ecf20Sopenharmony_ci */ 2678c2ecf20Sopenharmony_ci#define REAPTIMEOUT_AC (2*HZ) 2688c2ecf20Sopenharmony_ci#define REAPTIMEOUT_NODE (4*HZ) 2698c2ecf20Sopenharmony_ci 2708c2ecf20Sopenharmony_ci#if STATS 2718c2ecf20Sopenharmony_ci#define STATS_INC_ACTIVE(x) ((x)->num_active++) 2728c2ecf20Sopenharmony_ci#define STATS_DEC_ACTIVE(x) ((x)->num_active--) 2738c2ecf20Sopenharmony_ci#define STATS_INC_ALLOCED(x) ((x)->num_allocations++) 2748c2ecf20Sopenharmony_ci#define STATS_INC_GROWN(x) ((x)->grown++) 2758c2ecf20Sopenharmony_ci#define STATS_ADD_REAPED(x,y) ((x)->reaped += (y)) 2768c2ecf20Sopenharmony_ci#define STATS_SET_HIGH(x) \ 2778c2ecf20Sopenharmony_ci do { \ 2788c2ecf20Sopenharmony_ci if ((x)->num_active > (x)->high_mark) \ 2798c2ecf20Sopenharmony_ci (x)->high_mark = (x)->num_active; \ 2808c2ecf20Sopenharmony_ci } while (0) 2818c2ecf20Sopenharmony_ci#define STATS_INC_ERR(x) ((x)->errors++) 2828c2ecf20Sopenharmony_ci#define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++) 2838c2ecf20Sopenharmony_ci#define STATS_INC_NODEFREES(x) ((x)->node_frees++) 2848c2ecf20Sopenharmony_ci#define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++) 2858c2ecf20Sopenharmony_ci#define STATS_SET_FREEABLE(x, i) \ 2868c2ecf20Sopenharmony_ci do { \ 2878c2ecf20Sopenharmony_ci if ((x)->max_freeable < i) \ 2888c2ecf20Sopenharmony_ci (x)->max_freeable = i; \ 2898c2ecf20Sopenharmony_ci } while (0) 2908c2ecf20Sopenharmony_ci#define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit) 2918c2ecf20Sopenharmony_ci#define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss) 2928c2ecf20Sopenharmony_ci#define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit) 2938c2ecf20Sopenharmony_ci#define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss) 2948c2ecf20Sopenharmony_ci#else 2958c2ecf20Sopenharmony_ci#define STATS_INC_ACTIVE(x) do { } while (0) 2968c2ecf20Sopenharmony_ci#define STATS_DEC_ACTIVE(x) do { } while (0) 2978c2ecf20Sopenharmony_ci#define STATS_INC_ALLOCED(x) do { } while (0) 2988c2ecf20Sopenharmony_ci#define STATS_INC_GROWN(x) do { } while (0) 2998c2ecf20Sopenharmony_ci#define STATS_ADD_REAPED(x,y) do { (void)(y); } while (0) 3008c2ecf20Sopenharmony_ci#define STATS_SET_HIGH(x) do { } while (0) 3018c2ecf20Sopenharmony_ci#define STATS_INC_ERR(x) do { } while (0) 3028c2ecf20Sopenharmony_ci#define STATS_INC_NODEALLOCS(x) do { } while (0) 3038c2ecf20Sopenharmony_ci#define STATS_INC_NODEFREES(x) do { } while (0) 3048c2ecf20Sopenharmony_ci#define STATS_INC_ACOVERFLOW(x) do { } while (0) 3058c2ecf20Sopenharmony_ci#define STATS_SET_FREEABLE(x, i) do { } while (0) 3068c2ecf20Sopenharmony_ci#define STATS_INC_ALLOCHIT(x) do { } while (0) 3078c2ecf20Sopenharmony_ci#define STATS_INC_ALLOCMISS(x) do { } while (0) 3088c2ecf20Sopenharmony_ci#define STATS_INC_FREEHIT(x) do { } while (0) 3098c2ecf20Sopenharmony_ci#define STATS_INC_FREEMISS(x) do { } while (0) 3108c2ecf20Sopenharmony_ci#endif 3118c2ecf20Sopenharmony_ci 3128c2ecf20Sopenharmony_ci#if DEBUG 3138c2ecf20Sopenharmony_ci 3148c2ecf20Sopenharmony_ci/* 3158c2ecf20Sopenharmony_ci * memory layout of objects: 3168c2ecf20Sopenharmony_ci * 0 : objp 3178c2ecf20Sopenharmony_ci * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that 3188c2ecf20Sopenharmony_ci * the end of an object is aligned with the end of the real 3198c2ecf20Sopenharmony_ci * allocation. Catches writes behind the end of the allocation. 3208c2ecf20Sopenharmony_ci * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1: 3218c2ecf20Sopenharmony_ci * redzone word. 3228c2ecf20Sopenharmony_ci * cachep->obj_offset: The real object. 3238c2ecf20Sopenharmony_ci * cachep->size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long] 3248c2ecf20Sopenharmony_ci * cachep->size - 1* BYTES_PER_WORD: last caller address 3258c2ecf20Sopenharmony_ci * [BYTES_PER_WORD long] 3268c2ecf20Sopenharmony_ci */ 3278c2ecf20Sopenharmony_cistatic int obj_offset(struct kmem_cache *cachep) 3288c2ecf20Sopenharmony_ci{ 3298c2ecf20Sopenharmony_ci return cachep->obj_offset; 3308c2ecf20Sopenharmony_ci} 3318c2ecf20Sopenharmony_ci 3328c2ecf20Sopenharmony_cistatic unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp) 3338c2ecf20Sopenharmony_ci{ 3348c2ecf20Sopenharmony_ci BUG_ON(!(cachep->flags & SLAB_RED_ZONE)); 3358c2ecf20Sopenharmony_ci return (unsigned long long*) (objp + obj_offset(cachep) - 3368c2ecf20Sopenharmony_ci sizeof(unsigned long long)); 3378c2ecf20Sopenharmony_ci} 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_cistatic unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp) 3408c2ecf20Sopenharmony_ci{ 3418c2ecf20Sopenharmony_ci BUG_ON(!(cachep->flags & SLAB_RED_ZONE)); 3428c2ecf20Sopenharmony_ci if (cachep->flags & SLAB_STORE_USER) 3438c2ecf20Sopenharmony_ci return (unsigned long long *)(objp + cachep->size - 3448c2ecf20Sopenharmony_ci sizeof(unsigned long long) - 3458c2ecf20Sopenharmony_ci REDZONE_ALIGN); 3468c2ecf20Sopenharmony_ci return (unsigned long long *) (objp + cachep->size - 3478c2ecf20Sopenharmony_ci sizeof(unsigned long long)); 3488c2ecf20Sopenharmony_ci} 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_cistatic void **dbg_userword(struct kmem_cache *cachep, void *objp) 3518c2ecf20Sopenharmony_ci{ 3528c2ecf20Sopenharmony_ci BUG_ON(!(cachep->flags & SLAB_STORE_USER)); 3538c2ecf20Sopenharmony_ci return (void **)(objp + cachep->size - BYTES_PER_WORD); 3548c2ecf20Sopenharmony_ci} 3558c2ecf20Sopenharmony_ci 3568c2ecf20Sopenharmony_ci#else 3578c2ecf20Sopenharmony_ci 3588c2ecf20Sopenharmony_ci#define obj_offset(x) 0 3598c2ecf20Sopenharmony_ci#define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;}) 3608c2ecf20Sopenharmony_ci#define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;}) 3618c2ecf20Sopenharmony_ci#define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;}) 3628c2ecf20Sopenharmony_ci 3638c2ecf20Sopenharmony_ci#endif 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_ci/* 3668c2ecf20Sopenharmony_ci * Do not go above this order unless 0 objects fit into the slab or 3678c2ecf20Sopenharmony_ci * overridden on the command line. 3688c2ecf20Sopenharmony_ci */ 3698c2ecf20Sopenharmony_ci#define SLAB_MAX_ORDER_HI 1 3708c2ecf20Sopenharmony_ci#define SLAB_MAX_ORDER_LO 0 3718c2ecf20Sopenharmony_cistatic int slab_max_order = SLAB_MAX_ORDER_LO; 3728c2ecf20Sopenharmony_cistatic bool slab_max_order_set __initdata; 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_cistatic inline void *index_to_obj(struct kmem_cache *cache, struct page *page, 3758c2ecf20Sopenharmony_ci unsigned int idx) 3768c2ecf20Sopenharmony_ci{ 3778c2ecf20Sopenharmony_ci return page->s_mem + cache->size * idx; 3788c2ecf20Sopenharmony_ci} 3798c2ecf20Sopenharmony_ci 3808c2ecf20Sopenharmony_ci#define BOOT_CPUCACHE_ENTRIES 1 3818c2ecf20Sopenharmony_ci/* internal cache of cache description objs */ 3828c2ecf20Sopenharmony_cistatic struct kmem_cache kmem_cache_boot = { 3838c2ecf20Sopenharmony_ci .batchcount = 1, 3848c2ecf20Sopenharmony_ci .limit = BOOT_CPUCACHE_ENTRIES, 3858c2ecf20Sopenharmony_ci .shared = 1, 3868c2ecf20Sopenharmony_ci .size = sizeof(struct kmem_cache), 3878c2ecf20Sopenharmony_ci .name = "kmem_cache", 3888c2ecf20Sopenharmony_ci}; 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_cistatic DEFINE_PER_CPU(struct delayed_work, slab_reap_work); 3918c2ecf20Sopenharmony_ci 3928c2ecf20Sopenharmony_cistatic inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep) 3938c2ecf20Sopenharmony_ci{ 3948c2ecf20Sopenharmony_ci return this_cpu_ptr(cachep->cpu_cache); 3958c2ecf20Sopenharmony_ci} 3968c2ecf20Sopenharmony_ci 3978c2ecf20Sopenharmony_ci/* 3988c2ecf20Sopenharmony_ci * Calculate the number of objects and left-over bytes for a given buffer size. 3998c2ecf20Sopenharmony_ci */ 4008c2ecf20Sopenharmony_cistatic unsigned int cache_estimate(unsigned long gfporder, size_t buffer_size, 4018c2ecf20Sopenharmony_ci slab_flags_t flags, size_t *left_over) 4028c2ecf20Sopenharmony_ci{ 4038c2ecf20Sopenharmony_ci unsigned int num; 4048c2ecf20Sopenharmony_ci size_t slab_size = PAGE_SIZE << gfporder; 4058c2ecf20Sopenharmony_ci 4068c2ecf20Sopenharmony_ci /* 4078c2ecf20Sopenharmony_ci * The slab management structure can be either off the slab or 4088c2ecf20Sopenharmony_ci * on it. For the latter case, the memory allocated for a 4098c2ecf20Sopenharmony_ci * slab is used for: 4108c2ecf20Sopenharmony_ci * 4118c2ecf20Sopenharmony_ci * - @buffer_size bytes for each object 4128c2ecf20Sopenharmony_ci * - One freelist_idx_t for each object 4138c2ecf20Sopenharmony_ci * 4148c2ecf20Sopenharmony_ci * We don't need to consider alignment of freelist because 4158c2ecf20Sopenharmony_ci * freelist will be at the end of slab page. The objects will be 4168c2ecf20Sopenharmony_ci * at the correct alignment. 4178c2ecf20Sopenharmony_ci * 4188c2ecf20Sopenharmony_ci * If the slab management structure is off the slab, then the 4198c2ecf20Sopenharmony_ci * alignment will already be calculated into the size. Because 4208c2ecf20Sopenharmony_ci * the slabs are all pages aligned, the objects will be at the 4218c2ecf20Sopenharmony_ci * correct alignment when allocated. 4228c2ecf20Sopenharmony_ci */ 4238c2ecf20Sopenharmony_ci if (flags & (CFLGS_OBJFREELIST_SLAB | CFLGS_OFF_SLAB)) { 4248c2ecf20Sopenharmony_ci num = slab_size / buffer_size; 4258c2ecf20Sopenharmony_ci *left_over = slab_size % buffer_size; 4268c2ecf20Sopenharmony_ci } else { 4278c2ecf20Sopenharmony_ci num = slab_size / (buffer_size + sizeof(freelist_idx_t)); 4288c2ecf20Sopenharmony_ci *left_over = slab_size % 4298c2ecf20Sopenharmony_ci (buffer_size + sizeof(freelist_idx_t)); 4308c2ecf20Sopenharmony_ci } 4318c2ecf20Sopenharmony_ci 4328c2ecf20Sopenharmony_ci return num; 4338c2ecf20Sopenharmony_ci} 4348c2ecf20Sopenharmony_ci 4358c2ecf20Sopenharmony_ci#if DEBUG 4368c2ecf20Sopenharmony_ci#define slab_error(cachep, msg) __slab_error(__func__, cachep, msg) 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_cistatic void __slab_error(const char *function, struct kmem_cache *cachep, 4398c2ecf20Sopenharmony_ci char *msg) 4408c2ecf20Sopenharmony_ci{ 4418c2ecf20Sopenharmony_ci pr_err("slab error in %s(): cache `%s': %s\n", 4428c2ecf20Sopenharmony_ci function, cachep->name, msg); 4438c2ecf20Sopenharmony_ci dump_stack(); 4448c2ecf20Sopenharmony_ci add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); 4458c2ecf20Sopenharmony_ci} 4468c2ecf20Sopenharmony_ci#endif 4478c2ecf20Sopenharmony_ci 4488c2ecf20Sopenharmony_ci/* 4498c2ecf20Sopenharmony_ci * By default on NUMA we use alien caches to stage the freeing of 4508c2ecf20Sopenharmony_ci * objects allocated from other nodes. This causes massive memory 4518c2ecf20Sopenharmony_ci * inefficiencies when using fake NUMA setup to split memory into a 4528c2ecf20Sopenharmony_ci * large number of small nodes, so it can be disabled on the command 4538c2ecf20Sopenharmony_ci * line 4548c2ecf20Sopenharmony_ci */ 4558c2ecf20Sopenharmony_ci 4568c2ecf20Sopenharmony_cistatic int use_alien_caches __read_mostly = 1; 4578c2ecf20Sopenharmony_cistatic int __init noaliencache_setup(char *s) 4588c2ecf20Sopenharmony_ci{ 4598c2ecf20Sopenharmony_ci use_alien_caches = 0; 4608c2ecf20Sopenharmony_ci return 1; 4618c2ecf20Sopenharmony_ci} 4628c2ecf20Sopenharmony_ci__setup("noaliencache", noaliencache_setup); 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_cistatic int __init slab_max_order_setup(char *str) 4658c2ecf20Sopenharmony_ci{ 4668c2ecf20Sopenharmony_ci get_option(&str, &slab_max_order); 4678c2ecf20Sopenharmony_ci slab_max_order = slab_max_order < 0 ? 0 : 4688c2ecf20Sopenharmony_ci min(slab_max_order, MAX_ORDER - 1); 4698c2ecf20Sopenharmony_ci slab_max_order_set = true; 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci return 1; 4728c2ecf20Sopenharmony_ci} 4738c2ecf20Sopenharmony_ci__setup("slab_max_order=", slab_max_order_setup); 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ci#ifdef CONFIG_NUMA 4768c2ecf20Sopenharmony_ci/* 4778c2ecf20Sopenharmony_ci * Special reaping functions for NUMA systems called from cache_reap(). 4788c2ecf20Sopenharmony_ci * These take care of doing round robin flushing of alien caches (containing 4798c2ecf20Sopenharmony_ci * objects freed on different nodes from which they were allocated) and the 4808c2ecf20Sopenharmony_ci * flushing of remote pcps by calling drain_node_pages. 4818c2ecf20Sopenharmony_ci */ 4828c2ecf20Sopenharmony_cistatic DEFINE_PER_CPU(unsigned long, slab_reap_node); 4838c2ecf20Sopenharmony_ci 4848c2ecf20Sopenharmony_cistatic void init_reap_node(int cpu) 4858c2ecf20Sopenharmony_ci{ 4868c2ecf20Sopenharmony_ci per_cpu(slab_reap_node, cpu) = next_node_in(cpu_to_mem(cpu), 4878c2ecf20Sopenharmony_ci node_online_map); 4888c2ecf20Sopenharmony_ci} 4898c2ecf20Sopenharmony_ci 4908c2ecf20Sopenharmony_cistatic void next_reap_node(void) 4918c2ecf20Sopenharmony_ci{ 4928c2ecf20Sopenharmony_ci int node = __this_cpu_read(slab_reap_node); 4938c2ecf20Sopenharmony_ci 4948c2ecf20Sopenharmony_ci node = next_node_in(node, node_online_map); 4958c2ecf20Sopenharmony_ci __this_cpu_write(slab_reap_node, node); 4968c2ecf20Sopenharmony_ci} 4978c2ecf20Sopenharmony_ci 4988c2ecf20Sopenharmony_ci#else 4998c2ecf20Sopenharmony_ci#define init_reap_node(cpu) do { } while (0) 5008c2ecf20Sopenharmony_ci#define next_reap_node(void) do { } while (0) 5018c2ecf20Sopenharmony_ci#endif 5028c2ecf20Sopenharmony_ci 5038c2ecf20Sopenharmony_ci/* 5048c2ecf20Sopenharmony_ci * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz 5058c2ecf20Sopenharmony_ci * via the workqueue/eventd. 5068c2ecf20Sopenharmony_ci * Add the CPU number into the expiration time to minimize the possibility of 5078c2ecf20Sopenharmony_ci * the CPUs getting into lockstep and contending for the global cache chain 5088c2ecf20Sopenharmony_ci * lock. 5098c2ecf20Sopenharmony_ci */ 5108c2ecf20Sopenharmony_cistatic void start_cpu_timer(int cpu) 5118c2ecf20Sopenharmony_ci{ 5128c2ecf20Sopenharmony_ci struct delayed_work *reap_work = &per_cpu(slab_reap_work, cpu); 5138c2ecf20Sopenharmony_ci 5148c2ecf20Sopenharmony_ci if (reap_work->work.func == NULL) { 5158c2ecf20Sopenharmony_ci init_reap_node(cpu); 5168c2ecf20Sopenharmony_ci INIT_DEFERRABLE_WORK(reap_work, cache_reap); 5178c2ecf20Sopenharmony_ci schedule_delayed_work_on(cpu, reap_work, 5188c2ecf20Sopenharmony_ci __round_jiffies_relative(HZ, cpu)); 5198c2ecf20Sopenharmony_ci } 5208c2ecf20Sopenharmony_ci} 5218c2ecf20Sopenharmony_ci 5228c2ecf20Sopenharmony_cistatic void init_arraycache(struct array_cache *ac, int limit, int batch) 5238c2ecf20Sopenharmony_ci{ 5248c2ecf20Sopenharmony_ci if (ac) { 5258c2ecf20Sopenharmony_ci ac->avail = 0; 5268c2ecf20Sopenharmony_ci ac->limit = limit; 5278c2ecf20Sopenharmony_ci ac->batchcount = batch; 5288c2ecf20Sopenharmony_ci ac->touched = 0; 5298c2ecf20Sopenharmony_ci } 5308c2ecf20Sopenharmony_ci} 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_cistatic struct array_cache *alloc_arraycache(int node, int entries, 5338c2ecf20Sopenharmony_ci int batchcount, gfp_t gfp) 5348c2ecf20Sopenharmony_ci{ 5358c2ecf20Sopenharmony_ci size_t memsize = sizeof(void *) * entries + sizeof(struct array_cache); 5368c2ecf20Sopenharmony_ci struct array_cache *ac = NULL; 5378c2ecf20Sopenharmony_ci 5388c2ecf20Sopenharmony_ci ac = kmalloc_node(memsize, gfp, node); 5398c2ecf20Sopenharmony_ci /* 5408c2ecf20Sopenharmony_ci * The array_cache structures contain pointers to free object. 5418c2ecf20Sopenharmony_ci * However, when such objects are allocated or transferred to another 5428c2ecf20Sopenharmony_ci * cache the pointers are not cleared and they could be counted as 5438c2ecf20Sopenharmony_ci * valid references during a kmemleak scan. Therefore, kmemleak must 5448c2ecf20Sopenharmony_ci * not scan such objects. 5458c2ecf20Sopenharmony_ci */ 5468c2ecf20Sopenharmony_ci kmemleak_no_scan(ac); 5478c2ecf20Sopenharmony_ci init_arraycache(ac, entries, batchcount); 5488c2ecf20Sopenharmony_ci return ac; 5498c2ecf20Sopenharmony_ci} 5508c2ecf20Sopenharmony_ci 5518c2ecf20Sopenharmony_cistatic noinline void cache_free_pfmemalloc(struct kmem_cache *cachep, 5528c2ecf20Sopenharmony_ci struct page *page, void *objp) 5538c2ecf20Sopenharmony_ci{ 5548c2ecf20Sopenharmony_ci struct kmem_cache_node *n; 5558c2ecf20Sopenharmony_ci int page_node; 5568c2ecf20Sopenharmony_ci LIST_HEAD(list); 5578c2ecf20Sopenharmony_ci 5588c2ecf20Sopenharmony_ci page_node = page_to_nid(page); 5598c2ecf20Sopenharmony_ci n = get_node(cachep, page_node); 5608c2ecf20Sopenharmony_ci 5618c2ecf20Sopenharmony_ci spin_lock(&n->list_lock); 5628c2ecf20Sopenharmony_ci free_block(cachep, &objp, 1, page_node, &list); 5638c2ecf20Sopenharmony_ci spin_unlock(&n->list_lock); 5648c2ecf20Sopenharmony_ci 5658c2ecf20Sopenharmony_ci slabs_destroy(cachep, &list); 5668c2ecf20Sopenharmony_ci} 5678c2ecf20Sopenharmony_ci 5688c2ecf20Sopenharmony_ci/* 5698c2ecf20Sopenharmony_ci * Transfer objects in one arraycache to another. 5708c2ecf20Sopenharmony_ci * Locking must be handled by the caller. 5718c2ecf20Sopenharmony_ci * 5728c2ecf20Sopenharmony_ci * Return the number of entries transferred. 5738c2ecf20Sopenharmony_ci */ 5748c2ecf20Sopenharmony_cistatic int transfer_objects(struct array_cache *to, 5758c2ecf20Sopenharmony_ci struct array_cache *from, unsigned int max) 5768c2ecf20Sopenharmony_ci{ 5778c2ecf20Sopenharmony_ci /* Figure out how many entries to transfer */ 5788c2ecf20Sopenharmony_ci int nr = min3(from->avail, max, to->limit - to->avail); 5798c2ecf20Sopenharmony_ci 5808c2ecf20Sopenharmony_ci if (!nr) 5818c2ecf20Sopenharmony_ci return 0; 5828c2ecf20Sopenharmony_ci 5838c2ecf20Sopenharmony_ci memcpy(to->entry + to->avail, from->entry + from->avail -nr, 5848c2ecf20Sopenharmony_ci sizeof(void *) *nr); 5858c2ecf20Sopenharmony_ci 5868c2ecf20Sopenharmony_ci from->avail -= nr; 5878c2ecf20Sopenharmony_ci to->avail += nr; 5888c2ecf20Sopenharmony_ci return nr; 5898c2ecf20Sopenharmony_ci} 5908c2ecf20Sopenharmony_ci 5918c2ecf20Sopenharmony_ci/* &alien->lock must be held by alien callers. */ 5928c2ecf20Sopenharmony_cistatic __always_inline void __free_one(struct array_cache *ac, void *objp) 5938c2ecf20Sopenharmony_ci{ 5948c2ecf20Sopenharmony_ci /* Avoid trivial double-free. */ 5958c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_SLAB_FREELIST_HARDENED) && 5968c2ecf20Sopenharmony_ci WARN_ON_ONCE(ac->avail > 0 && ac->entry[ac->avail - 1] == objp)) 5978c2ecf20Sopenharmony_ci return; 5988c2ecf20Sopenharmony_ci ac->entry[ac->avail++] = objp; 5998c2ecf20Sopenharmony_ci} 6008c2ecf20Sopenharmony_ci 6018c2ecf20Sopenharmony_ci#ifndef CONFIG_NUMA 6028c2ecf20Sopenharmony_ci 6038c2ecf20Sopenharmony_ci#define drain_alien_cache(cachep, alien) do { } while (0) 6048c2ecf20Sopenharmony_ci#define reap_alien(cachep, n) do { } while (0) 6058c2ecf20Sopenharmony_ci 6068c2ecf20Sopenharmony_cistatic inline struct alien_cache **alloc_alien_cache(int node, 6078c2ecf20Sopenharmony_ci int limit, gfp_t gfp) 6088c2ecf20Sopenharmony_ci{ 6098c2ecf20Sopenharmony_ci return NULL; 6108c2ecf20Sopenharmony_ci} 6118c2ecf20Sopenharmony_ci 6128c2ecf20Sopenharmony_cistatic inline void free_alien_cache(struct alien_cache **ac_ptr) 6138c2ecf20Sopenharmony_ci{ 6148c2ecf20Sopenharmony_ci} 6158c2ecf20Sopenharmony_ci 6168c2ecf20Sopenharmony_cistatic inline int cache_free_alien(struct kmem_cache *cachep, void *objp) 6178c2ecf20Sopenharmony_ci{ 6188c2ecf20Sopenharmony_ci return 0; 6198c2ecf20Sopenharmony_ci} 6208c2ecf20Sopenharmony_ci 6218c2ecf20Sopenharmony_cistatic inline void *alternate_node_alloc(struct kmem_cache *cachep, 6228c2ecf20Sopenharmony_ci gfp_t flags) 6238c2ecf20Sopenharmony_ci{ 6248c2ecf20Sopenharmony_ci return NULL; 6258c2ecf20Sopenharmony_ci} 6268c2ecf20Sopenharmony_ci 6278c2ecf20Sopenharmony_cistatic inline void *____cache_alloc_node(struct kmem_cache *cachep, 6288c2ecf20Sopenharmony_ci gfp_t flags, int nodeid) 6298c2ecf20Sopenharmony_ci{ 6308c2ecf20Sopenharmony_ci return NULL; 6318c2ecf20Sopenharmony_ci} 6328c2ecf20Sopenharmony_ci 6338c2ecf20Sopenharmony_cistatic inline gfp_t gfp_exact_node(gfp_t flags) 6348c2ecf20Sopenharmony_ci{ 6358c2ecf20Sopenharmony_ci return flags & ~__GFP_NOFAIL; 6368c2ecf20Sopenharmony_ci} 6378c2ecf20Sopenharmony_ci 6388c2ecf20Sopenharmony_ci#else /* CONFIG_NUMA */ 6398c2ecf20Sopenharmony_ci 6408c2ecf20Sopenharmony_cistatic void *____cache_alloc_node(struct kmem_cache *, gfp_t, int); 6418c2ecf20Sopenharmony_cistatic void *alternate_node_alloc(struct kmem_cache *, gfp_t); 6428c2ecf20Sopenharmony_ci 6438c2ecf20Sopenharmony_cistatic struct alien_cache *__alloc_alien_cache(int node, int entries, 6448c2ecf20Sopenharmony_ci int batch, gfp_t gfp) 6458c2ecf20Sopenharmony_ci{ 6468c2ecf20Sopenharmony_ci size_t memsize = sizeof(void *) * entries + sizeof(struct alien_cache); 6478c2ecf20Sopenharmony_ci struct alien_cache *alc = NULL; 6488c2ecf20Sopenharmony_ci 6498c2ecf20Sopenharmony_ci alc = kmalloc_node(memsize, gfp, node); 6508c2ecf20Sopenharmony_ci if (alc) { 6518c2ecf20Sopenharmony_ci kmemleak_no_scan(alc); 6528c2ecf20Sopenharmony_ci init_arraycache(&alc->ac, entries, batch); 6538c2ecf20Sopenharmony_ci spin_lock_init(&alc->lock); 6548c2ecf20Sopenharmony_ci } 6558c2ecf20Sopenharmony_ci return alc; 6568c2ecf20Sopenharmony_ci} 6578c2ecf20Sopenharmony_ci 6588c2ecf20Sopenharmony_cistatic struct alien_cache **alloc_alien_cache(int node, int limit, gfp_t gfp) 6598c2ecf20Sopenharmony_ci{ 6608c2ecf20Sopenharmony_ci struct alien_cache **alc_ptr; 6618c2ecf20Sopenharmony_ci int i; 6628c2ecf20Sopenharmony_ci 6638c2ecf20Sopenharmony_ci if (limit > 1) 6648c2ecf20Sopenharmony_ci limit = 12; 6658c2ecf20Sopenharmony_ci alc_ptr = kcalloc_node(nr_node_ids, sizeof(void *), gfp, node); 6668c2ecf20Sopenharmony_ci if (!alc_ptr) 6678c2ecf20Sopenharmony_ci return NULL; 6688c2ecf20Sopenharmony_ci 6698c2ecf20Sopenharmony_ci for_each_node(i) { 6708c2ecf20Sopenharmony_ci if (i == node || !node_online(i)) 6718c2ecf20Sopenharmony_ci continue; 6728c2ecf20Sopenharmony_ci alc_ptr[i] = __alloc_alien_cache(node, limit, 0xbaadf00d, gfp); 6738c2ecf20Sopenharmony_ci if (!alc_ptr[i]) { 6748c2ecf20Sopenharmony_ci for (i--; i >= 0; i--) 6758c2ecf20Sopenharmony_ci kfree(alc_ptr[i]); 6768c2ecf20Sopenharmony_ci kfree(alc_ptr); 6778c2ecf20Sopenharmony_ci return NULL; 6788c2ecf20Sopenharmony_ci } 6798c2ecf20Sopenharmony_ci } 6808c2ecf20Sopenharmony_ci return alc_ptr; 6818c2ecf20Sopenharmony_ci} 6828c2ecf20Sopenharmony_ci 6838c2ecf20Sopenharmony_cistatic void free_alien_cache(struct alien_cache **alc_ptr) 6848c2ecf20Sopenharmony_ci{ 6858c2ecf20Sopenharmony_ci int i; 6868c2ecf20Sopenharmony_ci 6878c2ecf20Sopenharmony_ci if (!alc_ptr) 6888c2ecf20Sopenharmony_ci return; 6898c2ecf20Sopenharmony_ci for_each_node(i) 6908c2ecf20Sopenharmony_ci kfree(alc_ptr[i]); 6918c2ecf20Sopenharmony_ci kfree(alc_ptr); 6928c2ecf20Sopenharmony_ci} 6938c2ecf20Sopenharmony_ci 6948c2ecf20Sopenharmony_cistatic void __drain_alien_cache(struct kmem_cache *cachep, 6958c2ecf20Sopenharmony_ci struct array_cache *ac, int node, 6968c2ecf20Sopenharmony_ci struct list_head *list) 6978c2ecf20Sopenharmony_ci{ 6988c2ecf20Sopenharmony_ci struct kmem_cache_node *n = get_node(cachep, node); 6998c2ecf20Sopenharmony_ci 7008c2ecf20Sopenharmony_ci if (ac->avail) { 7018c2ecf20Sopenharmony_ci spin_lock(&n->list_lock); 7028c2ecf20Sopenharmony_ci /* 7038c2ecf20Sopenharmony_ci * Stuff objects into the remote nodes shared array first. 7048c2ecf20Sopenharmony_ci * That way we could avoid the overhead of putting the objects 7058c2ecf20Sopenharmony_ci * into the free lists and getting them back later. 7068c2ecf20Sopenharmony_ci */ 7078c2ecf20Sopenharmony_ci if (n->shared) 7088c2ecf20Sopenharmony_ci transfer_objects(n->shared, ac, ac->limit); 7098c2ecf20Sopenharmony_ci 7108c2ecf20Sopenharmony_ci free_block(cachep, ac->entry, ac->avail, node, list); 7118c2ecf20Sopenharmony_ci ac->avail = 0; 7128c2ecf20Sopenharmony_ci spin_unlock(&n->list_lock); 7138c2ecf20Sopenharmony_ci } 7148c2ecf20Sopenharmony_ci} 7158c2ecf20Sopenharmony_ci 7168c2ecf20Sopenharmony_ci/* 7178c2ecf20Sopenharmony_ci * Called from cache_reap() to regularly drain alien caches round robin. 7188c2ecf20Sopenharmony_ci */ 7198c2ecf20Sopenharmony_cistatic void reap_alien(struct kmem_cache *cachep, struct kmem_cache_node *n) 7208c2ecf20Sopenharmony_ci{ 7218c2ecf20Sopenharmony_ci int node = __this_cpu_read(slab_reap_node); 7228c2ecf20Sopenharmony_ci 7238c2ecf20Sopenharmony_ci if (n->alien) { 7248c2ecf20Sopenharmony_ci struct alien_cache *alc = n->alien[node]; 7258c2ecf20Sopenharmony_ci struct array_cache *ac; 7268c2ecf20Sopenharmony_ci 7278c2ecf20Sopenharmony_ci if (alc) { 7288c2ecf20Sopenharmony_ci ac = &alc->ac; 7298c2ecf20Sopenharmony_ci if (ac->avail && spin_trylock_irq(&alc->lock)) { 7308c2ecf20Sopenharmony_ci LIST_HEAD(list); 7318c2ecf20Sopenharmony_ci 7328c2ecf20Sopenharmony_ci __drain_alien_cache(cachep, ac, node, &list); 7338c2ecf20Sopenharmony_ci spin_unlock_irq(&alc->lock); 7348c2ecf20Sopenharmony_ci slabs_destroy(cachep, &list); 7358c2ecf20Sopenharmony_ci } 7368c2ecf20Sopenharmony_ci } 7378c2ecf20Sopenharmony_ci } 7388c2ecf20Sopenharmony_ci} 7398c2ecf20Sopenharmony_ci 7408c2ecf20Sopenharmony_cistatic void drain_alien_cache(struct kmem_cache *cachep, 7418c2ecf20Sopenharmony_ci struct alien_cache **alien) 7428c2ecf20Sopenharmony_ci{ 7438c2ecf20Sopenharmony_ci int i = 0; 7448c2ecf20Sopenharmony_ci struct alien_cache *alc; 7458c2ecf20Sopenharmony_ci struct array_cache *ac; 7468c2ecf20Sopenharmony_ci unsigned long flags; 7478c2ecf20Sopenharmony_ci 7488c2ecf20Sopenharmony_ci for_each_online_node(i) { 7498c2ecf20Sopenharmony_ci alc = alien[i]; 7508c2ecf20Sopenharmony_ci if (alc) { 7518c2ecf20Sopenharmony_ci LIST_HEAD(list); 7528c2ecf20Sopenharmony_ci 7538c2ecf20Sopenharmony_ci ac = &alc->ac; 7548c2ecf20Sopenharmony_ci spin_lock_irqsave(&alc->lock, flags); 7558c2ecf20Sopenharmony_ci __drain_alien_cache(cachep, ac, i, &list); 7568c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&alc->lock, flags); 7578c2ecf20Sopenharmony_ci slabs_destroy(cachep, &list); 7588c2ecf20Sopenharmony_ci } 7598c2ecf20Sopenharmony_ci } 7608c2ecf20Sopenharmony_ci} 7618c2ecf20Sopenharmony_ci 7628c2ecf20Sopenharmony_cistatic int __cache_free_alien(struct kmem_cache *cachep, void *objp, 7638c2ecf20Sopenharmony_ci int node, int page_node) 7648c2ecf20Sopenharmony_ci{ 7658c2ecf20Sopenharmony_ci struct kmem_cache_node *n; 7668c2ecf20Sopenharmony_ci struct alien_cache *alien = NULL; 7678c2ecf20Sopenharmony_ci struct array_cache *ac; 7688c2ecf20Sopenharmony_ci LIST_HEAD(list); 7698c2ecf20Sopenharmony_ci 7708c2ecf20Sopenharmony_ci n = get_node(cachep, node); 7718c2ecf20Sopenharmony_ci STATS_INC_NODEFREES(cachep); 7728c2ecf20Sopenharmony_ci if (n->alien && n->alien[page_node]) { 7738c2ecf20Sopenharmony_ci alien = n->alien[page_node]; 7748c2ecf20Sopenharmony_ci ac = &alien->ac; 7758c2ecf20Sopenharmony_ci spin_lock(&alien->lock); 7768c2ecf20Sopenharmony_ci if (unlikely(ac->avail == ac->limit)) { 7778c2ecf20Sopenharmony_ci STATS_INC_ACOVERFLOW(cachep); 7788c2ecf20Sopenharmony_ci __drain_alien_cache(cachep, ac, page_node, &list); 7798c2ecf20Sopenharmony_ci } 7808c2ecf20Sopenharmony_ci __free_one(ac, objp); 7818c2ecf20Sopenharmony_ci spin_unlock(&alien->lock); 7828c2ecf20Sopenharmony_ci slabs_destroy(cachep, &list); 7838c2ecf20Sopenharmony_ci } else { 7848c2ecf20Sopenharmony_ci n = get_node(cachep, page_node); 7858c2ecf20Sopenharmony_ci spin_lock(&n->list_lock); 7868c2ecf20Sopenharmony_ci free_block(cachep, &objp, 1, page_node, &list); 7878c2ecf20Sopenharmony_ci spin_unlock(&n->list_lock); 7888c2ecf20Sopenharmony_ci slabs_destroy(cachep, &list); 7898c2ecf20Sopenharmony_ci } 7908c2ecf20Sopenharmony_ci return 1; 7918c2ecf20Sopenharmony_ci} 7928c2ecf20Sopenharmony_ci 7938c2ecf20Sopenharmony_cistatic inline int cache_free_alien(struct kmem_cache *cachep, void *objp) 7948c2ecf20Sopenharmony_ci{ 7958c2ecf20Sopenharmony_ci int page_node = page_to_nid(virt_to_page(objp)); 7968c2ecf20Sopenharmony_ci int node = numa_mem_id(); 7978c2ecf20Sopenharmony_ci /* 7988c2ecf20Sopenharmony_ci * Make sure we are not freeing a object from another node to the array 7998c2ecf20Sopenharmony_ci * cache on this cpu. 8008c2ecf20Sopenharmony_ci */ 8018c2ecf20Sopenharmony_ci if (likely(node == page_node)) 8028c2ecf20Sopenharmony_ci return 0; 8038c2ecf20Sopenharmony_ci 8048c2ecf20Sopenharmony_ci return __cache_free_alien(cachep, objp, node, page_node); 8058c2ecf20Sopenharmony_ci} 8068c2ecf20Sopenharmony_ci 8078c2ecf20Sopenharmony_ci/* 8088c2ecf20Sopenharmony_ci * Construct gfp mask to allocate from a specific node but do not reclaim or 8098c2ecf20Sopenharmony_ci * warn about failures. 8108c2ecf20Sopenharmony_ci */ 8118c2ecf20Sopenharmony_cistatic inline gfp_t gfp_exact_node(gfp_t flags) 8128c2ecf20Sopenharmony_ci{ 8138c2ecf20Sopenharmony_ci return (flags | __GFP_THISNODE | __GFP_NOWARN) & ~(__GFP_RECLAIM|__GFP_NOFAIL); 8148c2ecf20Sopenharmony_ci} 8158c2ecf20Sopenharmony_ci#endif 8168c2ecf20Sopenharmony_ci 8178c2ecf20Sopenharmony_cistatic int init_cache_node(struct kmem_cache *cachep, int node, gfp_t gfp) 8188c2ecf20Sopenharmony_ci{ 8198c2ecf20Sopenharmony_ci struct kmem_cache_node *n; 8208c2ecf20Sopenharmony_ci 8218c2ecf20Sopenharmony_ci /* 8228c2ecf20Sopenharmony_ci * Set up the kmem_cache_node for cpu before we can 8238c2ecf20Sopenharmony_ci * begin anything. Make sure some other cpu on this 8248c2ecf20Sopenharmony_ci * node has not already allocated this 8258c2ecf20Sopenharmony_ci */ 8268c2ecf20Sopenharmony_ci n = get_node(cachep, node); 8278c2ecf20Sopenharmony_ci if (n) { 8288c2ecf20Sopenharmony_ci spin_lock_irq(&n->list_lock); 8298c2ecf20Sopenharmony_ci n->free_limit = (1 + nr_cpus_node(node)) * cachep->batchcount + 8308c2ecf20Sopenharmony_ci cachep->num; 8318c2ecf20Sopenharmony_ci spin_unlock_irq(&n->list_lock); 8328c2ecf20Sopenharmony_ci 8338c2ecf20Sopenharmony_ci return 0; 8348c2ecf20Sopenharmony_ci } 8358c2ecf20Sopenharmony_ci 8368c2ecf20Sopenharmony_ci n = kmalloc_node(sizeof(struct kmem_cache_node), gfp, node); 8378c2ecf20Sopenharmony_ci if (!n) 8388c2ecf20Sopenharmony_ci return -ENOMEM; 8398c2ecf20Sopenharmony_ci 8408c2ecf20Sopenharmony_ci kmem_cache_node_init(n); 8418c2ecf20Sopenharmony_ci n->next_reap = jiffies + REAPTIMEOUT_NODE + 8428c2ecf20Sopenharmony_ci ((unsigned long)cachep) % REAPTIMEOUT_NODE; 8438c2ecf20Sopenharmony_ci 8448c2ecf20Sopenharmony_ci n->free_limit = 8458c2ecf20Sopenharmony_ci (1 + nr_cpus_node(node)) * cachep->batchcount + cachep->num; 8468c2ecf20Sopenharmony_ci 8478c2ecf20Sopenharmony_ci /* 8488c2ecf20Sopenharmony_ci * The kmem_cache_nodes don't come and go as CPUs 8498c2ecf20Sopenharmony_ci * come and go. slab_mutex is sufficient 8508c2ecf20Sopenharmony_ci * protection here. 8518c2ecf20Sopenharmony_ci */ 8528c2ecf20Sopenharmony_ci cachep->node[node] = n; 8538c2ecf20Sopenharmony_ci 8548c2ecf20Sopenharmony_ci return 0; 8558c2ecf20Sopenharmony_ci} 8568c2ecf20Sopenharmony_ci 8578c2ecf20Sopenharmony_ci#if (defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)) || defined(CONFIG_SMP) 8588c2ecf20Sopenharmony_ci/* 8598c2ecf20Sopenharmony_ci * Allocates and initializes node for a node on each slab cache, used for 8608c2ecf20Sopenharmony_ci * either memory or cpu hotplug. If memory is being hot-added, the kmem_cache_node 8618c2ecf20Sopenharmony_ci * will be allocated off-node since memory is not yet online for the new node. 8628c2ecf20Sopenharmony_ci * When hotplugging memory or a cpu, existing node are not replaced if 8638c2ecf20Sopenharmony_ci * already in use. 8648c2ecf20Sopenharmony_ci * 8658c2ecf20Sopenharmony_ci * Must hold slab_mutex. 8668c2ecf20Sopenharmony_ci */ 8678c2ecf20Sopenharmony_cistatic int init_cache_node_node(int node) 8688c2ecf20Sopenharmony_ci{ 8698c2ecf20Sopenharmony_ci int ret; 8708c2ecf20Sopenharmony_ci struct kmem_cache *cachep; 8718c2ecf20Sopenharmony_ci 8728c2ecf20Sopenharmony_ci list_for_each_entry(cachep, &slab_caches, list) { 8738c2ecf20Sopenharmony_ci ret = init_cache_node(cachep, node, GFP_KERNEL); 8748c2ecf20Sopenharmony_ci if (ret) 8758c2ecf20Sopenharmony_ci return ret; 8768c2ecf20Sopenharmony_ci } 8778c2ecf20Sopenharmony_ci 8788c2ecf20Sopenharmony_ci return 0; 8798c2ecf20Sopenharmony_ci} 8808c2ecf20Sopenharmony_ci#endif 8818c2ecf20Sopenharmony_ci 8828c2ecf20Sopenharmony_cistatic int setup_kmem_cache_node(struct kmem_cache *cachep, 8838c2ecf20Sopenharmony_ci int node, gfp_t gfp, bool force_change) 8848c2ecf20Sopenharmony_ci{ 8858c2ecf20Sopenharmony_ci int ret = -ENOMEM; 8868c2ecf20Sopenharmony_ci struct kmem_cache_node *n; 8878c2ecf20Sopenharmony_ci struct array_cache *old_shared = NULL; 8888c2ecf20Sopenharmony_ci struct array_cache *new_shared = NULL; 8898c2ecf20Sopenharmony_ci struct alien_cache **new_alien = NULL; 8908c2ecf20Sopenharmony_ci LIST_HEAD(list); 8918c2ecf20Sopenharmony_ci 8928c2ecf20Sopenharmony_ci if (use_alien_caches) { 8938c2ecf20Sopenharmony_ci new_alien = alloc_alien_cache(node, cachep->limit, gfp); 8948c2ecf20Sopenharmony_ci if (!new_alien) 8958c2ecf20Sopenharmony_ci goto fail; 8968c2ecf20Sopenharmony_ci } 8978c2ecf20Sopenharmony_ci 8988c2ecf20Sopenharmony_ci if (cachep->shared) { 8998c2ecf20Sopenharmony_ci new_shared = alloc_arraycache(node, 9008c2ecf20Sopenharmony_ci cachep->shared * cachep->batchcount, 0xbaadf00d, gfp); 9018c2ecf20Sopenharmony_ci if (!new_shared) 9028c2ecf20Sopenharmony_ci goto fail; 9038c2ecf20Sopenharmony_ci } 9048c2ecf20Sopenharmony_ci 9058c2ecf20Sopenharmony_ci ret = init_cache_node(cachep, node, gfp); 9068c2ecf20Sopenharmony_ci if (ret) 9078c2ecf20Sopenharmony_ci goto fail; 9088c2ecf20Sopenharmony_ci 9098c2ecf20Sopenharmony_ci n = get_node(cachep, node); 9108c2ecf20Sopenharmony_ci spin_lock_irq(&n->list_lock); 9118c2ecf20Sopenharmony_ci if (n->shared && force_change) { 9128c2ecf20Sopenharmony_ci free_block(cachep, n->shared->entry, 9138c2ecf20Sopenharmony_ci n->shared->avail, node, &list); 9148c2ecf20Sopenharmony_ci n->shared->avail = 0; 9158c2ecf20Sopenharmony_ci } 9168c2ecf20Sopenharmony_ci 9178c2ecf20Sopenharmony_ci if (!n->shared || force_change) { 9188c2ecf20Sopenharmony_ci old_shared = n->shared; 9198c2ecf20Sopenharmony_ci n->shared = new_shared; 9208c2ecf20Sopenharmony_ci new_shared = NULL; 9218c2ecf20Sopenharmony_ci } 9228c2ecf20Sopenharmony_ci 9238c2ecf20Sopenharmony_ci if (!n->alien) { 9248c2ecf20Sopenharmony_ci n->alien = new_alien; 9258c2ecf20Sopenharmony_ci new_alien = NULL; 9268c2ecf20Sopenharmony_ci } 9278c2ecf20Sopenharmony_ci 9288c2ecf20Sopenharmony_ci spin_unlock_irq(&n->list_lock); 9298c2ecf20Sopenharmony_ci slabs_destroy(cachep, &list); 9308c2ecf20Sopenharmony_ci 9318c2ecf20Sopenharmony_ci /* 9328c2ecf20Sopenharmony_ci * To protect lockless access to n->shared during irq disabled context. 9338c2ecf20Sopenharmony_ci * If n->shared isn't NULL in irq disabled context, accessing to it is 9348c2ecf20Sopenharmony_ci * guaranteed to be valid until irq is re-enabled, because it will be 9358c2ecf20Sopenharmony_ci * freed after synchronize_rcu(). 9368c2ecf20Sopenharmony_ci */ 9378c2ecf20Sopenharmony_ci if (old_shared && force_change) 9388c2ecf20Sopenharmony_ci synchronize_rcu(); 9398c2ecf20Sopenharmony_ci 9408c2ecf20Sopenharmony_cifail: 9418c2ecf20Sopenharmony_ci kfree(old_shared); 9428c2ecf20Sopenharmony_ci kfree(new_shared); 9438c2ecf20Sopenharmony_ci free_alien_cache(new_alien); 9448c2ecf20Sopenharmony_ci 9458c2ecf20Sopenharmony_ci return ret; 9468c2ecf20Sopenharmony_ci} 9478c2ecf20Sopenharmony_ci 9488c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP 9498c2ecf20Sopenharmony_ci 9508c2ecf20Sopenharmony_cistatic void cpuup_canceled(long cpu) 9518c2ecf20Sopenharmony_ci{ 9528c2ecf20Sopenharmony_ci struct kmem_cache *cachep; 9538c2ecf20Sopenharmony_ci struct kmem_cache_node *n = NULL; 9548c2ecf20Sopenharmony_ci int node = cpu_to_mem(cpu); 9558c2ecf20Sopenharmony_ci const struct cpumask *mask = cpumask_of_node(node); 9568c2ecf20Sopenharmony_ci 9578c2ecf20Sopenharmony_ci list_for_each_entry(cachep, &slab_caches, list) { 9588c2ecf20Sopenharmony_ci struct array_cache *nc; 9598c2ecf20Sopenharmony_ci struct array_cache *shared; 9608c2ecf20Sopenharmony_ci struct alien_cache **alien; 9618c2ecf20Sopenharmony_ci LIST_HEAD(list); 9628c2ecf20Sopenharmony_ci 9638c2ecf20Sopenharmony_ci n = get_node(cachep, node); 9648c2ecf20Sopenharmony_ci if (!n) 9658c2ecf20Sopenharmony_ci continue; 9668c2ecf20Sopenharmony_ci 9678c2ecf20Sopenharmony_ci spin_lock_irq(&n->list_lock); 9688c2ecf20Sopenharmony_ci 9698c2ecf20Sopenharmony_ci /* Free limit for this kmem_cache_node */ 9708c2ecf20Sopenharmony_ci n->free_limit -= cachep->batchcount; 9718c2ecf20Sopenharmony_ci 9728c2ecf20Sopenharmony_ci /* cpu is dead; no one can alloc from it. */ 9738c2ecf20Sopenharmony_ci nc = per_cpu_ptr(cachep->cpu_cache, cpu); 9748c2ecf20Sopenharmony_ci free_block(cachep, nc->entry, nc->avail, node, &list); 9758c2ecf20Sopenharmony_ci nc->avail = 0; 9768c2ecf20Sopenharmony_ci 9778c2ecf20Sopenharmony_ci if (!cpumask_empty(mask)) { 9788c2ecf20Sopenharmony_ci spin_unlock_irq(&n->list_lock); 9798c2ecf20Sopenharmony_ci goto free_slab; 9808c2ecf20Sopenharmony_ci } 9818c2ecf20Sopenharmony_ci 9828c2ecf20Sopenharmony_ci shared = n->shared; 9838c2ecf20Sopenharmony_ci if (shared) { 9848c2ecf20Sopenharmony_ci free_block(cachep, shared->entry, 9858c2ecf20Sopenharmony_ci shared->avail, node, &list); 9868c2ecf20Sopenharmony_ci n->shared = NULL; 9878c2ecf20Sopenharmony_ci } 9888c2ecf20Sopenharmony_ci 9898c2ecf20Sopenharmony_ci alien = n->alien; 9908c2ecf20Sopenharmony_ci n->alien = NULL; 9918c2ecf20Sopenharmony_ci 9928c2ecf20Sopenharmony_ci spin_unlock_irq(&n->list_lock); 9938c2ecf20Sopenharmony_ci 9948c2ecf20Sopenharmony_ci kfree(shared); 9958c2ecf20Sopenharmony_ci if (alien) { 9968c2ecf20Sopenharmony_ci drain_alien_cache(cachep, alien); 9978c2ecf20Sopenharmony_ci free_alien_cache(alien); 9988c2ecf20Sopenharmony_ci } 9998c2ecf20Sopenharmony_ci 10008c2ecf20Sopenharmony_cifree_slab: 10018c2ecf20Sopenharmony_ci slabs_destroy(cachep, &list); 10028c2ecf20Sopenharmony_ci } 10038c2ecf20Sopenharmony_ci /* 10048c2ecf20Sopenharmony_ci * In the previous loop, all the objects were freed to 10058c2ecf20Sopenharmony_ci * the respective cache's slabs, now we can go ahead and 10068c2ecf20Sopenharmony_ci * shrink each nodelist to its limit. 10078c2ecf20Sopenharmony_ci */ 10088c2ecf20Sopenharmony_ci list_for_each_entry(cachep, &slab_caches, list) { 10098c2ecf20Sopenharmony_ci n = get_node(cachep, node); 10108c2ecf20Sopenharmony_ci if (!n) 10118c2ecf20Sopenharmony_ci continue; 10128c2ecf20Sopenharmony_ci drain_freelist(cachep, n, INT_MAX); 10138c2ecf20Sopenharmony_ci } 10148c2ecf20Sopenharmony_ci} 10158c2ecf20Sopenharmony_ci 10168c2ecf20Sopenharmony_cistatic int cpuup_prepare(long cpu) 10178c2ecf20Sopenharmony_ci{ 10188c2ecf20Sopenharmony_ci struct kmem_cache *cachep; 10198c2ecf20Sopenharmony_ci int node = cpu_to_mem(cpu); 10208c2ecf20Sopenharmony_ci int err; 10218c2ecf20Sopenharmony_ci 10228c2ecf20Sopenharmony_ci /* 10238c2ecf20Sopenharmony_ci * We need to do this right in the beginning since 10248c2ecf20Sopenharmony_ci * alloc_arraycache's are going to use this list. 10258c2ecf20Sopenharmony_ci * kmalloc_node allows us to add the slab to the right 10268c2ecf20Sopenharmony_ci * kmem_cache_node and not this cpu's kmem_cache_node 10278c2ecf20Sopenharmony_ci */ 10288c2ecf20Sopenharmony_ci err = init_cache_node_node(node); 10298c2ecf20Sopenharmony_ci if (err < 0) 10308c2ecf20Sopenharmony_ci goto bad; 10318c2ecf20Sopenharmony_ci 10328c2ecf20Sopenharmony_ci /* 10338c2ecf20Sopenharmony_ci * Now we can go ahead with allocating the shared arrays and 10348c2ecf20Sopenharmony_ci * array caches 10358c2ecf20Sopenharmony_ci */ 10368c2ecf20Sopenharmony_ci list_for_each_entry(cachep, &slab_caches, list) { 10378c2ecf20Sopenharmony_ci err = setup_kmem_cache_node(cachep, node, GFP_KERNEL, false); 10388c2ecf20Sopenharmony_ci if (err) 10398c2ecf20Sopenharmony_ci goto bad; 10408c2ecf20Sopenharmony_ci } 10418c2ecf20Sopenharmony_ci 10428c2ecf20Sopenharmony_ci return 0; 10438c2ecf20Sopenharmony_cibad: 10448c2ecf20Sopenharmony_ci cpuup_canceled(cpu); 10458c2ecf20Sopenharmony_ci return -ENOMEM; 10468c2ecf20Sopenharmony_ci} 10478c2ecf20Sopenharmony_ci 10488c2ecf20Sopenharmony_ciint slab_prepare_cpu(unsigned int cpu) 10498c2ecf20Sopenharmony_ci{ 10508c2ecf20Sopenharmony_ci int err; 10518c2ecf20Sopenharmony_ci 10528c2ecf20Sopenharmony_ci mutex_lock(&slab_mutex); 10538c2ecf20Sopenharmony_ci err = cpuup_prepare(cpu); 10548c2ecf20Sopenharmony_ci mutex_unlock(&slab_mutex); 10558c2ecf20Sopenharmony_ci return err; 10568c2ecf20Sopenharmony_ci} 10578c2ecf20Sopenharmony_ci 10588c2ecf20Sopenharmony_ci/* 10598c2ecf20Sopenharmony_ci * This is called for a failed online attempt and for a successful 10608c2ecf20Sopenharmony_ci * offline. 10618c2ecf20Sopenharmony_ci * 10628c2ecf20Sopenharmony_ci * Even if all the cpus of a node are down, we don't free the 10638c2ecf20Sopenharmony_ci * kmem_cache_node of any cache. This to avoid a race between cpu_down, and 10648c2ecf20Sopenharmony_ci * a kmalloc allocation from another cpu for memory from the node of 10658c2ecf20Sopenharmony_ci * the cpu going down. The kmem_cache_node structure is usually allocated from 10668c2ecf20Sopenharmony_ci * kmem_cache_create() and gets destroyed at kmem_cache_destroy(). 10678c2ecf20Sopenharmony_ci */ 10688c2ecf20Sopenharmony_ciint slab_dead_cpu(unsigned int cpu) 10698c2ecf20Sopenharmony_ci{ 10708c2ecf20Sopenharmony_ci mutex_lock(&slab_mutex); 10718c2ecf20Sopenharmony_ci cpuup_canceled(cpu); 10728c2ecf20Sopenharmony_ci mutex_unlock(&slab_mutex); 10738c2ecf20Sopenharmony_ci return 0; 10748c2ecf20Sopenharmony_ci} 10758c2ecf20Sopenharmony_ci#endif 10768c2ecf20Sopenharmony_ci 10778c2ecf20Sopenharmony_cistatic int slab_online_cpu(unsigned int cpu) 10788c2ecf20Sopenharmony_ci{ 10798c2ecf20Sopenharmony_ci start_cpu_timer(cpu); 10808c2ecf20Sopenharmony_ci return 0; 10818c2ecf20Sopenharmony_ci} 10828c2ecf20Sopenharmony_ci 10838c2ecf20Sopenharmony_cistatic int slab_offline_cpu(unsigned int cpu) 10848c2ecf20Sopenharmony_ci{ 10858c2ecf20Sopenharmony_ci /* 10868c2ecf20Sopenharmony_ci * Shutdown cache reaper. Note that the slab_mutex is held so 10878c2ecf20Sopenharmony_ci * that if cache_reap() is invoked it cannot do anything 10888c2ecf20Sopenharmony_ci * expensive but will only modify reap_work and reschedule the 10898c2ecf20Sopenharmony_ci * timer. 10908c2ecf20Sopenharmony_ci */ 10918c2ecf20Sopenharmony_ci cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu)); 10928c2ecf20Sopenharmony_ci /* Now the cache_reaper is guaranteed to be not running. */ 10938c2ecf20Sopenharmony_ci per_cpu(slab_reap_work, cpu).work.func = NULL; 10948c2ecf20Sopenharmony_ci return 0; 10958c2ecf20Sopenharmony_ci} 10968c2ecf20Sopenharmony_ci 10978c2ecf20Sopenharmony_ci#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG) 10988c2ecf20Sopenharmony_ci/* 10998c2ecf20Sopenharmony_ci * Drains freelist for a node on each slab cache, used for memory hot-remove. 11008c2ecf20Sopenharmony_ci * Returns -EBUSY if all objects cannot be drained so that the node is not 11018c2ecf20Sopenharmony_ci * removed. 11028c2ecf20Sopenharmony_ci * 11038c2ecf20Sopenharmony_ci * Must hold slab_mutex. 11048c2ecf20Sopenharmony_ci */ 11058c2ecf20Sopenharmony_cistatic int __meminit drain_cache_node_node(int node) 11068c2ecf20Sopenharmony_ci{ 11078c2ecf20Sopenharmony_ci struct kmem_cache *cachep; 11088c2ecf20Sopenharmony_ci int ret = 0; 11098c2ecf20Sopenharmony_ci 11108c2ecf20Sopenharmony_ci list_for_each_entry(cachep, &slab_caches, list) { 11118c2ecf20Sopenharmony_ci struct kmem_cache_node *n; 11128c2ecf20Sopenharmony_ci 11138c2ecf20Sopenharmony_ci n = get_node(cachep, node); 11148c2ecf20Sopenharmony_ci if (!n) 11158c2ecf20Sopenharmony_ci continue; 11168c2ecf20Sopenharmony_ci 11178c2ecf20Sopenharmony_ci drain_freelist(cachep, n, INT_MAX); 11188c2ecf20Sopenharmony_ci 11198c2ecf20Sopenharmony_ci if (!list_empty(&n->slabs_full) || 11208c2ecf20Sopenharmony_ci !list_empty(&n->slabs_partial)) { 11218c2ecf20Sopenharmony_ci ret = -EBUSY; 11228c2ecf20Sopenharmony_ci break; 11238c2ecf20Sopenharmony_ci } 11248c2ecf20Sopenharmony_ci } 11258c2ecf20Sopenharmony_ci return ret; 11268c2ecf20Sopenharmony_ci} 11278c2ecf20Sopenharmony_ci 11288c2ecf20Sopenharmony_cistatic int __meminit slab_memory_callback(struct notifier_block *self, 11298c2ecf20Sopenharmony_ci unsigned long action, void *arg) 11308c2ecf20Sopenharmony_ci{ 11318c2ecf20Sopenharmony_ci struct memory_notify *mnb = arg; 11328c2ecf20Sopenharmony_ci int ret = 0; 11338c2ecf20Sopenharmony_ci int nid; 11348c2ecf20Sopenharmony_ci 11358c2ecf20Sopenharmony_ci nid = mnb->status_change_nid; 11368c2ecf20Sopenharmony_ci if (nid < 0) 11378c2ecf20Sopenharmony_ci goto out; 11388c2ecf20Sopenharmony_ci 11398c2ecf20Sopenharmony_ci switch (action) { 11408c2ecf20Sopenharmony_ci case MEM_GOING_ONLINE: 11418c2ecf20Sopenharmony_ci mutex_lock(&slab_mutex); 11428c2ecf20Sopenharmony_ci ret = init_cache_node_node(nid); 11438c2ecf20Sopenharmony_ci mutex_unlock(&slab_mutex); 11448c2ecf20Sopenharmony_ci break; 11458c2ecf20Sopenharmony_ci case MEM_GOING_OFFLINE: 11468c2ecf20Sopenharmony_ci mutex_lock(&slab_mutex); 11478c2ecf20Sopenharmony_ci ret = drain_cache_node_node(nid); 11488c2ecf20Sopenharmony_ci mutex_unlock(&slab_mutex); 11498c2ecf20Sopenharmony_ci break; 11508c2ecf20Sopenharmony_ci case MEM_ONLINE: 11518c2ecf20Sopenharmony_ci case MEM_OFFLINE: 11528c2ecf20Sopenharmony_ci case MEM_CANCEL_ONLINE: 11538c2ecf20Sopenharmony_ci case MEM_CANCEL_OFFLINE: 11548c2ecf20Sopenharmony_ci break; 11558c2ecf20Sopenharmony_ci } 11568c2ecf20Sopenharmony_ciout: 11578c2ecf20Sopenharmony_ci return notifier_from_errno(ret); 11588c2ecf20Sopenharmony_ci} 11598c2ecf20Sopenharmony_ci#endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */ 11608c2ecf20Sopenharmony_ci 11618c2ecf20Sopenharmony_ci/* 11628c2ecf20Sopenharmony_ci * swap the static kmem_cache_node with kmalloced memory 11638c2ecf20Sopenharmony_ci */ 11648c2ecf20Sopenharmony_cistatic void __init init_list(struct kmem_cache *cachep, struct kmem_cache_node *list, 11658c2ecf20Sopenharmony_ci int nodeid) 11668c2ecf20Sopenharmony_ci{ 11678c2ecf20Sopenharmony_ci struct kmem_cache_node *ptr; 11688c2ecf20Sopenharmony_ci 11698c2ecf20Sopenharmony_ci ptr = kmalloc_node(sizeof(struct kmem_cache_node), GFP_NOWAIT, nodeid); 11708c2ecf20Sopenharmony_ci BUG_ON(!ptr); 11718c2ecf20Sopenharmony_ci 11728c2ecf20Sopenharmony_ci memcpy(ptr, list, sizeof(struct kmem_cache_node)); 11738c2ecf20Sopenharmony_ci /* 11748c2ecf20Sopenharmony_ci * Do not assume that spinlocks can be initialized via memcpy: 11758c2ecf20Sopenharmony_ci */ 11768c2ecf20Sopenharmony_ci spin_lock_init(&ptr->list_lock); 11778c2ecf20Sopenharmony_ci 11788c2ecf20Sopenharmony_ci MAKE_ALL_LISTS(cachep, ptr, nodeid); 11798c2ecf20Sopenharmony_ci cachep->node[nodeid] = ptr; 11808c2ecf20Sopenharmony_ci} 11818c2ecf20Sopenharmony_ci 11828c2ecf20Sopenharmony_ci/* 11838c2ecf20Sopenharmony_ci * For setting up all the kmem_cache_node for cache whose buffer_size is same as 11848c2ecf20Sopenharmony_ci * size of kmem_cache_node. 11858c2ecf20Sopenharmony_ci */ 11868c2ecf20Sopenharmony_cistatic void __init set_up_node(struct kmem_cache *cachep, int index) 11878c2ecf20Sopenharmony_ci{ 11888c2ecf20Sopenharmony_ci int node; 11898c2ecf20Sopenharmony_ci 11908c2ecf20Sopenharmony_ci for_each_online_node(node) { 11918c2ecf20Sopenharmony_ci cachep->node[node] = &init_kmem_cache_node[index + node]; 11928c2ecf20Sopenharmony_ci cachep->node[node]->next_reap = jiffies + 11938c2ecf20Sopenharmony_ci REAPTIMEOUT_NODE + 11948c2ecf20Sopenharmony_ci ((unsigned long)cachep) % REAPTIMEOUT_NODE; 11958c2ecf20Sopenharmony_ci } 11968c2ecf20Sopenharmony_ci} 11978c2ecf20Sopenharmony_ci 11988c2ecf20Sopenharmony_ci/* 11998c2ecf20Sopenharmony_ci * Initialisation. Called after the page allocator have been initialised and 12008c2ecf20Sopenharmony_ci * before smp_init(). 12018c2ecf20Sopenharmony_ci */ 12028c2ecf20Sopenharmony_civoid __init kmem_cache_init(void) 12038c2ecf20Sopenharmony_ci{ 12048c2ecf20Sopenharmony_ci int i; 12058c2ecf20Sopenharmony_ci 12068c2ecf20Sopenharmony_ci kmem_cache = &kmem_cache_boot; 12078c2ecf20Sopenharmony_ci 12088c2ecf20Sopenharmony_ci if (!IS_ENABLED(CONFIG_NUMA) || num_possible_nodes() == 1) 12098c2ecf20Sopenharmony_ci use_alien_caches = 0; 12108c2ecf20Sopenharmony_ci 12118c2ecf20Sopenharmony_ci for (i = 0; i < NUM_INIT_LISTS; i++) 12128c2ecf20Sopenharmony_ci kmem_cache_node_init(&init_kmem_cache_node[i]); 12138c2ecf20Sopenharmony_ci 12148c2ecf20Sopenharmony_ci /* 12158c2ecf20Sopenharmony_ci * Fragmentation resistance on low memory - only use bigger 12168c2ecf20Sopenharmony_ci * page orders on machines with more than 32MB of memory if 12178c2ecf20Sopenharmony_ci * not overridden on the command line. 12188c2ecf20Sopenharmony_ci */ 12198c2ecf20Sopenharmony_ci if (!slab_max_order_set && totalram_pages() > (32 << 20) >> PAGE_SHIFT) 12208c2ecf20Sopenharmony_ci slab_max_order = SLAB_MAX_ORDER_HI; 12218c2ecf20Sopenharmony_ci 12228c2ecf20Sopenharmony_ci /* Bootstrap is tricky, because several objects are allocated 12238c2ecf20Sopenharmony_ci * from caches that do not exist yet: 12248c2ecf20Sopenharmony_ci * 1) initialize the kmem_cache cache: it contains the struct 12258c2ecf20Sopenharmony_ci * kmem_cache structures of all caches, except kmem_cache itself: 12268c2ecf20Sopenharmony_ci * kmem_cache is statically allocated. 12278c2ecf20Sopenharmony_ci * Initially an __init data area is used for the head array and the 12288c2ecf20Sopenharmony_ci * kmem_cache_node structures, it's replaced with a kmalloc allocated 12298c2ecf20Sopenharmony_ci * array at the end of the bootstrap. 12308c2ecf20Sopenharmony_ci * 2) Create the first kmalloc cache. 12318c2ecf20Sopenharmony_ci * The struct kmem_cache for the new cache is allocated normally. 12328c2ecf20Sopenharmony_ci * An __init data area is used for the head array. 12338c2ecf20Sopenharmony_ci * 3) Create the remaining kmalloc caches, with minimally sized 12348c2ecf20Sopenharmony_ci * head arrays. 12358c2ecf20Sopenharmony_ci * 4) Replace the __init data head arrays for kmem_cache and the first 12368c2ecf20Sopenharmony_ci * kmalloc cache with kmalloc allocated arrays. 12378c2ecf20Sopenharmony_ci * 5) Replace the __init data for kmem_cache_node for kmem_cache and 12388c2ecf20Sopenharmony_ci * the other cache's with kmalloc allocated memory. 12398c2ecf20Sopenharmony_ci * 6) Resize the head arrays of the kmalloc caches to their final sizes. 12408c2ecf20Sopenharmony_ci */ 12418c2ecf20Sopenharmony_ci 12428c2ecf20Sopenharmony_ci /* 1) create the kmem_cache */ 12438c2ecf20Sopenharmony_ci 12448c2ecf20Sopenharmony_ci /* 12458c2ecf20Sopenharmony_ci * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids 12468c2ecf20Sopenharmony_ci */ 12478c2ecf20Sopenharmony_ci create_boot_cache(kmem_cache, "kmem_cache", 12488c2ecf20Sopenharmony_ci offsetof(struct kmem_cache, node) + 12498c2ecf20Sopenharmony_ci nr_node_ids * sizeof(struct kmem_cache_node *), 12508c2ecf20Sopenharmony_ci SLAB_HWCACHE_ALIGN, 0, 0); 12518c2ecf20Sopenharmony_ci list_add(&kmem_cache->list, &slab_caches); 12528c2ecf20Sopenharmony_ci slab_state = PARTIAL; 12538c2ecf20Sopenharmony_ci 12548c2ecf20Sopenharmony_ci /* 12558c2ecf20Sopenharmony_ci * Initialize the caches that provide memory for the kmem_cache_node 12568c2ecf20Sopenharmony_ci * structures first. Without this, further allocations will bug. 12578c2ecf20Sopenharmony_ci */ 12588c2ecf20Sopenharmony_ci kmalloc_caches[KMALLOC_NORMAL][INDEX_NODE] = create_kmalloc_cache( 12598c2ecf20Sopenharmony_ci kmalloc_info[INDEX_NODE].name[KMALLOC_NORMAL], 12608c2ecf20Sopenharmony_ci kmalloc_info[INDEX_NODE].size, 12618c2ecf20Sopenharmony_ci ARCH_KMALLOC_FLAGS, 0, 12628c2ecf20Sopenharmony_ci kmalloc_info[INDEX_NODE].size); 12638c2ecf20Sopenharmony_ci slab_state = PARTIAL_NODE; 12648c2ecf20Sopenharmony_ci setup_kmalloc_cache_index_table(); 12658c2ecf20Sopenharmony_ci 12668c2ecf20Sopenharmony_ci slab_early_init = 0; 12678c2ecf20Sopenharmony_ci 12688c2ecf20Sopenharmony_ci /* 5) Replace the bootstrap kmem_cache_node */ 12698c2ecf20Sopenharmony_ci { 12708c2ecf20Sopenharmony_ci int nid; 12718c2ecf20Sopenharmony_ci 12728c2ecf20Sopenharmony_ci for_each_online_node(nid) { 12738c2ecf20Sopenharmony_ci init_list(kmem_cache, &init_kmem_cache_node[CACHE_CACHE + nid], nid); 12748c2ecf20Sopenharmony_ci 12758c2ecf20Sopenharmony_ci init_list(kmalloc_caches[KMALLOC_NORMAL][INDEX_NODE], 12768c2ecf20Sopenharmony_ci &init_kmem_cache_node[SIZE_NODE + nid], nid); 12778c2ecf20Sopenharmony_ci } 12788c2ecf20Sopenharmony_ci } 12798c2ecf20Sopenharmony_ci 12808c2ecf20Sopenharmony_ci create_kmalloc_caches(ARCH_KMALLOC_FLAGS); 12818c2ecf20Sopenharmony_ci} 12828c2ecf20Sopenharmony_ci 12838c2ecf20Sopenharmony_civoid __init kmem_cache_init_late(void) 12848c2ecf20Sopenharmony_ci{ 12858c2ecf20Sopenharmony_ci struct kmem_cache *cachep; 12868c2ecf20Sopenharmony_ci 12878c2ecf20Sopenharmony_ci /* 6) resize the head arrays to their final sizes */ 12888c2ecf20Sopenharmony_ci mutex_lock(&slab_mutex); 12898c2ecf20Sopenharmony_ci list_for_each_entry(cachep, &slab_caches, list) 12908c2ecf20Sopenharmony_ci if (enable_cpucache(cachep, GFP_NOWAIT)) 12918c2ecf20Sopenharmony_ci BUG(); 12928c2ecf20Sopenharmony_ci mutex_unlock(&slab_mutex); 12938c2ecf20Sopenharmony_ci 12948c2ecf20Sopenharmony_ci /* Done! */ 12958c2ecf20Sopenharmony_ci slab_state = FULL; 12968c2ecf20Sopenharmony_ci 12978c2ecf20Sopenharmony_ci#ifdef CONFIG_NUMA 12988c2ecf20Sopenharmony_ci /* 12998c2ecf20Sopenharmony_ci * Register a memory hotplug callback that initializes and frees 13008c2ecf20Sopenharmony_ci * node. 13018c2ecf20Sopenharmony_ci */ 13028c2ecf20Sopenharmony_ci hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI); 13038c2ecf20Sopenharmony_ci#endif 13048c2ecf20Sopenharmony_ci 13058c2ecf20Sopenharmony_ci /* 13068c2ecf20Sopenharmony_ci * The reap timers are started later, with a module init call: That part 13078c2ecf20Sopenharmony_ci * of the kernel is not yet operational. 13088c2ecf20Sopenharmony_ci */ 13098c2ecf20Sopenharmony_ci} 13108c2ecf20Sopenharmony_ci 13118c2ecf20Sopenharmony_cistatic int __init cpucache_init(void) 13128c2ecf20Sopenharmony_ci{ 13138c2ecf20Sopenharmony_ci int ret; 13148c2ecf20Sopenharmony_ci 13158c2ecf20Sopenharmony_ci /* 13168c2ecf20Sopenharmony_ci * Register the timers that return unneeded pages to the page allocator 13178c2ecf20Sopenharmony_ci */ 13188c2ecf20Sopenharmony_ci ret = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "SLAB online", 13198c2ecf20Sopenharmony_ci slab_online_cpu, slab_offline_cpu); 13208c2ecf20Sopenharmony_ci WARN_ON(ret < 0); 13218c2ecf20Sopenharmony_ci 13228c2ecf20Sopenharmony_ci return 0; 13238c2ecf20Sopenharmony_ci} 13248c2ecf20Sopenharmony_ci__initcall(cpucache_init); 13258c2ecf20Sopenharmony_ci 13268c2ecf20Sopenharmony_cistatic noinline void 13278c2ecf20Sopenharmony_cislab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid) 13288c2ecf20Sopenharmony_ci{ 13298c2ecf20Sopenharmony_ci#if DEBUG 13308c2ecf20Sopenharmony_ci struct kmem_cache_node *n; 13318c2ecf20Sopenharmony_ci unsigned long flags; 13328c2ecf20Sopenharmony_ci int node; 13338c2ecf20Sopenharmony_ci static DEFINE_RATELIMIT_STATE(slab_oom_rs, DEFAULT_RATELIMIT_INTERVAL, 13348c2ecf20Sopenharmony_ci DEFAULT_RATELIMIT_BURST); 13358c2ecf20Sopenharmony_ci 13368c2ecf20Sopenharmony_ci if ((gfpflags & __GFP_NOWARN) || !__ratelimit(&slab_oom_rs)) 13378c2ecf20Sopenharmony_ci return; 13388c2ecf20Sopenharmony_ci 13398c2ecf20Sopenharmony_ci pr_warn("SLAB: Unable to allocate memory on node %d, gfp=%#x(%pGg)\n", 13408c2ecf20Sopenharmony_ci nodeid, gfpflags, &gfpflags); 13418c2ecf20Sopenharmony_ci pr_warn(" cache: %s, object size: %d, order: %d\n", 13428c2ecf20Sopenharmony_ci cachep->name, cachep->size, cachep->gfporder); 13438c2ecf20Sopenharmony_ci 13448c2ecf20Sopenharmony_ci for_each_kmem_cache_node(cachep, node, n) { 13458c2ecf20Sopenharmony_ci unsigned long total_slabs, free_slabs, free_objs; 13468c2ecf20Sopenharmony_ci 13478c2ecf20Sopenharmony_ci spin_lock_irqsave(&n->list_lock, flags); 13488c2ecf20Sopenharmony_ci total_slabs = n->total_slabs; 13498c2ecf20Sopenharmony_ci free_slabs = n->free_slabs; 13508c2ecf20Sopenharmony_ci free_objs = n->free_objects; 13518c2ecf20Sopenharmony_ci spin_unlock_irqrestore(&n->list_lock, flags); 13528c2ecf20Sopenharmony_ci 13538c2ecf20Sopenharmony_ci pr_warn(" node %d: slabs: %ld/%ld, objs: %ld/%ld\n", 13548c2ecf20Sopenharmony_ci node, total_slabs - free_slabs, total_slabs, 13558c2ecf20Sopenharmony_ci (total_slabs * cachep->num) - free_objs, 13568c2ecf20Sopenharmony_ci total_slabs * cachep->num); 13578c2ecf20Sopenharmony_ci } 13588c2ecf20Sopenharmony_ci#endif 13598c2ecf20Sopenharmony_ci} 13608c2ecf20Sopenharmony_ci 13618c2ecf20Sopenharmony_ci/* 13628c2ecf20Sopenharmony_ci * Interface to system's page allocator. No need to hold the 13638c2ecf20Sopenharmony_ci * kmem_cache_node ->list_lock. 13648c2ecf20Sopenharmony_ci * 13658c2ecf20Sopenharmony_ci * If we requested dmaable memory, we will get it. Even if we 13668c2ecf20Sopenharmony_ci * did not request dmaable memory, we might get it, but that 13678c2ecf20Sopenharmony_ci * would be relatively rare and ignorable. 13688c2ecf20Sopenharmony_ci */ 13698c2ecf20Sopenharmony_cistatic struct page *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, 13708c2ecf20Sopenharmony_ci int nodeid) 13718c2ecf20Sopenharmony_ci{ 13728c2ecf20Sopenharmony_ci struct page *page; 13738c2ecf20Sopenharmony_ci 13748c2ecf20Sopenharmony_ci flags |= cachep->allocflags; 13758c2ecf20Sopenharmony_ci 13768c2ecf20Sopenharmony_ci page = __alloc_pages_node(nodeid, flags, cachep->gfporder); 13778c2ecf20Sopenharmony_ci if (!page) { 13788c2ecf20Sopenharmony_ci slab_out_of_memory(cachep, flags, nodeid); 13798c2ecf20Sopenharmony_ci return NULL; 13808c2ecf20Sopenharmony_ci } 13818c2ecf20Sopenharmony_ci 13828c2ecf20Sopenharmony_ci account_slab_page(page, cachep->gfporder, cachep); 13838c2ecf20Sopenharmony_ci __SetPageSlab(page); 13848c2ecf20Sopenharmony_ci /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */ 13858c2ecf20Sopenharmony_ci if (sk_memalloc_socks() && page_is_pfmemalloc(page)) 13868c2ecf20Sopenharmony_ci SetPageSlabPfmemalloc(page); 13878c2ecf20Sopenharmony_ci 13888c2ecf20Sopenharmony_ci return page; 13898c2ecf20Sopenharmony_ci} 13908c2ecf20Sopenharmony_ci 13918c2ecf20Sopenharmony_ci/* 13928c2ecf20Sopenharmony_ci * Interface to system's page release. 13938c2ecf20Sopenharmony_ci */ 13948c2ecf20Sopenharmony_cistatic void kmem_freepages(struct kmem_cache *cachep, struct page *page) 13958c2ecf20Sopenharmony_ci{ 13968c2ecf20Sopenharmony_ci int order = cachep->gfporder; 13978c2ecf20Sopenharmony_ci 13988c2ecf20Sopenharmony_ci BUG_ON(!PageSlab(page)); 13998c2ecf20Sopenharmony_ci __ClearPageSlabPfmemalloc(page); 14008c2ecf20Sopenharmony_ci __ClearPageSlab(page); 14018c2ecf20Sopenharmony_ci page_mapcount_reset(page); 14028c2ecf20Sopenharmony_ci page->mapping = NULL; 14038c2ecf20Sopenharmony_ci 14048c2ecf20Sopenharmony_ci if (current->reclaim_state) 14058c2ecf20Sopenharmony_ci current->reclaim_state->reclaimed_slab += 1 << order; 14068c2ecf20Sopenharmony_ci unaccount_slab_page(page, order, cachep); 14078c2ecf20Sopenharmony_ci __free_pages(page, order); 14088c2ecf20Sopenharmony_ci} 14098c2ecf20Sopenharmony_ci 14108c2ecf20Sopenharmony_cistatic void kmem_rcu_free(struct rcu_head *head) 14118c2ecf20Sopenharmony_ci{ 14128c2ecf20Sopenharmony_ci struct kmem_cache *cachep; 14138c2ecf20Sopenharmony_ci struct page *page; 14148c2ecf20Sopenharmony_ci 14158c2ecf20Sopenharmony_ci page = container_of(head, struct page, rcu_head); 14168c2ecf20Sopenharmony_ci cachep = page->slab_cache; 14178c2ecf20Sopenharmony_ci 14188c2ecf20Sopenharmony_ci kmem_freepages(cachep, page); 14198c2ecf20Sopenharmony_ci} 14208c2ecf20Sopenharmony_ci 14218c2ecf20Sopenharmony_ci#if DEBUG 14228c2ecf20Sopenharmony_cistatic bool is_debug_pagealloc_cache(struct kmem_cache *cachep) 14238c2ecf20Sopenharmony_ci{ 14248c2ecf20Sopenharmony_ci if (debug_pagealloc_enabled_static() && OFF_SLAB(cachep) && 14258c2ecf20Sopenharmony_ci (cachep->size % PAGE_SIZE) == 0) 14268c2ecf20Sopenharmony_ci return true; 14278c2ecf20Sopenharmony_ci 14288c2ecf20Sopenharmony_ci return false; 14298c2ecf20Sopenharmony_ci} 14308c2ecf20Sopenharmony_ci 14318c2ecf20Sopenharmony_ci#ifdef CONFIG_DEBUG_PAGEALLOC 14328c2ecf20Sopenharmony_cistatic void slab_kernel_map(struct kmem_cache *cachep, void *objp, int map) 14338c2ecf20Sopenharmony_ci{ 14348c2ecf20Sopenharmony_ci if (!is_debug_pagealloc_cache(cachep)) 14358c2ecf20Sopenharmony_ci return; 14368c2ecf20Sopenharmony_ci 14378c2ecf20Sopenharmony_ci kernel_map_pages(virt_to_page(objp), cachep->size / PAGE_SIZE, map); 14388c2ecf20Sopenharmony_ci} 14398c2ecf20Sopenharmony_ci 14408c2ecf20Sopenharmony_ci#else 14418c2ecf20Sopenharmony_cistatic inline void slab_kernel_map(struct kmem_cache *cachep, void *objp, 14428c2ecf20Sopenharmony_ci int map) {} 14438c2ecf20Sopenharmony_ci 14448c2ecf20Sopenharmony_ci#endif 14458c2ecf20Sopenharmony_ci 14468c2ecf20Sopenharmony_cistatic void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val) 14478c2ecf20Sopenharmony_ci{ 14488c2ecf20Sopenharmony_ci int size = cachep->object_size; 14498c2ecf20Sopenharmony_ci addr = &((char *)addr)[obj_offset(cachep)]; 14508c2ecf20Sopenharmony_ci 14518c2ecf20Sopenharmony_ci memset(addr, val, size); 14528c2ecf20Sopenharmony_ci *(unsigned char *)(addr + size - 1) = POISON_END; 14538c2ecf20Sopenharmony_ci} 14548c2ecf20Sopenharmony_ci 14558c2ecf20Sopenharmony_cistatic void dump_line(char *data, int offset, int limit) 14568c2ecf20Sopenharmony_ci{ 14578c2ecf20Sopenharmony_ci int i; 14588c2ecf20Sopenharmony_ci unsigned char error = 0; 14598c2ecf20Sopenharmony_ci int bad_count = 0; 14608c2ecf20Sopenharmony_ci 14618c2ecf20Sopenharmony_ci pr_err("%03x: ", offset); 14628c2ecf20Sopenharmony_ci for (i = 0; i < limit; i++) { 14638c2ecf20Sopenharmony_ci if (data[offset + i] != POISON_FREE) { 14648c2ecf20Sopenharmony_ci error = data[offset + i]; 14658c2ecf20Sopenharmony_ci bad_count++; 14668c2ecf20Sopenharmony_ci } 14678c2ecf20Sopenharmony_ci } 14688c2ecf20Sopenharmony_ci print_hex_dump(KERN_CONT, "", 0, 16, 1, 14698c2ecf20Sopenharmony_ci &data[offset], limit, 1); 14708c2ecf20Sopenharmony_ci 14718c2ecf20Sopenharmony_ci if (bad_count == 1) { 14728c2ecf20Sopenharmony_ci error ^= POISON_FREE; 14738c2ecf20Sopenharmony_ci if (!(error & (error - 1))) { 14748c2ecf20Sopenharmony_ci pr_err("Single bit error detected. Probably bad RAM.\n"); 14758c2ecf20Sopenharmony_ci#ifdef CONFIG_X86 14768c2ecf20Sopenharmony_ci pr_err("Run memtest86+ or a similar memory test tool.\n"); 14778c2ecf20Sopenharmony_ci#else 14788c2ecf20Sopenharmony_ci pr_err("Run a memory test tool.\n"); 14798c2ecf20Sopenharmony_ci#endif 14808c2ecf20Sopenharmony_ci } 14818c2ecf20Sopenharmony_ci } 14828c2ecf20Sopenharmony_ci} 14838c2ecf20Sopenharmony_ci#endif 14848c2ecf20Sopenharmony_ci 14858c2ecf20Sopenharmony_ci#if DEBUG 14868c2ecf20Sopenharmony_ci 14878c2ecf20Sopenharmony_cistatic void print_objinfo(struct kmem_cache *cachep, void *objp, int lines) 14888c2ecf20Sopenharmony_ci{ 14898c2ecf20Sopenharmony_ci int i, size; 14908c2ecf20Sopenharmony_ci char *realobj; 14918c2ecf20Sopenharmony_ci 14928c2ecf20Sopenharmony_ci if (cachep->flags & SLAB_RED_ZONE) { 14938c2ecf20Sopenharmony_ci pr_err("Redzone: 0x%llx/0x%llx\n", 14948c2ecf20Sopenharmony_ci *dbg_redzone1(cachep, objp), 14958c2ecf20Sopenharmony_ci *dbg_redzone2(cachep, objp)); 14968c2ecf20Sopenharmony_ci } 14978c2ecf20Sopenharmony_ci 14988c2ecf20Sopenharmony_ci if (cachep->flags & SLAB_STORE_USER) 14998c2ecf20Sopenharmony_ci pr_err("Last user: (%pSR)\n", *dbg_userword(cachep, objp)); 15008c2ecf20Sopenharmony_ci realobj = (char *)objp + obj_offset(cachep); 15018c2ecf20Sopenharmony_ci size = cachep->object_size; 15028c2ecf20Sopenharmony_ci for (i = 0; i < size && lines; i += 16, lines--) { 15038c2ecf20Sopenharmony_ci int limit; 15048c2ecf20Sopenharmony_ci limit = 16; 15058c2ecf20Sopenharmony_ci if (i + limit > size) 15068c2ecf20Sopenharmony_ci limit = size - i; 15078c2ecf20Sopenharmony_ci dump_line(realobj, i, limit); 15088c2ecf20Sopenharmony_ci } 15098c2ecf20Sopenharmony_ci} 15108c2ecf20Sopenharmony_ci 15118c2ecf20Sopenharmony_cistatic void check_poison_obj(struct kmem_cache *cachep, void *objp) 15128c2ecf20Sopenharmony_ci{ 15138c2ecf20Sopenharmony_ci char *realobj; 15148c2ecf20Sopenharmony_ci int size, i; 15158c2ecf20Sopenharmony_ci int lines = 0; 15168c2ecf20Sopenharmony_ci 15178c2ecf20Sopenharmony_ci if (is_debug_pagealloc_cache(cachep)) 15188c2ecf20Sopenharmony_ci return; 15198c2ecf20Sopenharmony_ci 15208c2ecf20Sopenharmony_ci realobj = (char *)objp + obj_offset(cachep); 15218c2ecf20Sopenharmony_ci size = cachep->object_size; 15228c2ecf20Sopenharmony_ci 15238c2ecf20Sopenharmony_ci for (i = 0; i < size; i++) { 15248c2ecf20Sopenharmony_ci char exp = POISON_FREE; 15258c2ecf20Sopenharmony_ci if (i == size - 1) 15268c2ecf20Sopenharmony_ci exp = POISON_END; 15278c2ecf20Sopenharmony_ci if (realobj[i] != exp) { 15288c2ecf20Sopenharmony_ci int limit; 15298c2ecf20Sopenharmony_ci /* Mismatch ! */ 15308c2ecf20Sopenharmony_ci /* Print header */ 15318c2ecf20Sopenharmony_ci if (lines == 0) { 15328c2ecf20Sopenharmony_ci pr_err("Slab corruption (%s): %s start=%px, len=%d\n", 15338c2ecf20Sopenharmony_ci print_tainted(), cachep->name, 15348c2ecf20Sopenharmony_ci realobj, size); 15358c2ecf20Sopenharmony_ci print_objinfo(cachep, objp, 0); 15368c2ecf20Sopenharmony_ci } 15378c2ecf20Sopenharmony_ci /* Hexdump the affected line */ 15388c2ecf20Sopenharmony_ci i = (i / 16) * 16; 15398c2ecf20Sopenharmony_ci limit = 16; 15408c2ecf20Sopenharmony_ci if (i + limit > size) 15418c2ecf20Sopenharmony_ci limit = size - i; 15428c2ecf20Sopenharmony_ci dump_line(realobj, i, limit); 15438c2ecf20Sopenharmony_ci i += 16; 15448c2ecf20Sopenharmony_ci lines++; 15458c2ecf20Sopenharmony_ci /* Limit to 5 lines */ 15468c2ecf20Sopenharmony_ci if (lines > 5) 15478c2ecf20Sopenharmony_ci break; 15488c2ecf20Sopenharmony_ci } 15498c2ecf20Sopenharmony_ci } 15508c2ecf20Sopenharmony_ci if (lines != 0) { 15518c2ecf20Sopenharmony_ci /* Print some data about the neighboring objects, if they 15528c2ecf20Sopenharmony_ci * exist: 15538c2ecf20Sopenharmony_ci */ 15548c2ecf20Sopenharmony_ci struct page *page = virt_to_head_page(objp); 15558c2ecf20Sopenharmony_ci unsigned int objnr; 15568c2ecf20Sopenharmony_ci 15578c2ecf20Sopenharmony_ci objnr = obj_to_index(cachep, page, objp); 15588c2ecf20Sopenharmony_ci if (objnr) { 15598c2ecf20Sopenharmony_ci objp = index_to_obj(cachep, page, objnr - 1); 15608c2ecf20Sopenharmony_ci realobj = (char *)objp + obj_offset(cachep); 15618c2ecf20Sopenharmony_ci pr_err("Prev obj: start=%px, len=%d\n", realobj, size); 15628c2ecf20Sopenharmony_ci print_objinfo(cachep, objp, 2); 15638c2ecf20Sopenharmony_ci } 15648c2ecf20Sopenharmony_ci if (objnr + 1 < cachep->num) { 15658c2ecf20Sopenharmony_ci objp = index_to_obj(cachep, page, objnr + 1); 15668c2ecf20Sopenharmony_ci realobj = (char *)objp + obj_offset(cachep); 15678c2ecf20Sopenharmony_ci pr_err("Next obj: start=%px, len=%d\n", realobj, size); 15688c2ecf20Sopenharmony_ci print_objinfo(cachep, objp, 2); 15698c2ecf20Sopenharmony_ci } 15708c2ecf20Sopenharmony_ci } 15718c2ecf20Sopenharmony_ci} 15728c2ecf20Sopenharmony_ci#endif 15738c2ecf20Sopenharmony_ci 15748c2ecf20Sopenharmony_ci#if DEBUG 15758c2ecf20Sopenharmony_cistatic void slab_destroy_debugcheck(struct kmem_cache *cachep, 15768c2ecf20Sopenharmony_ci struct page *page) 15778c2ecf20Sopenharmony_ci{ 15788c2ecf20Sopenharmony_ci int i; 15798c2ecf20Sopenharmony_ci 15808c2ecf20Sopenharmony_ci if (OBJFREELIST_SLAB(cachep) && cachep->flags & SLAB_POISON) { 15818c2ecf20Sopenharmony_ci poison_obj(cachep, page->freelist - obj_offset(cachep), 15828c2ecf20Sopenharmony_ci POISON_FREE); 15838c2ecf20Sopenharmony_ci } 15848c2ecf20Sopenharmony_ci 15858c2ecf20Sopenharmony_ci for (i = 0; i < cachep->num; i++) { 15868c2ecf20Sopenharmony_ci void *objp = index_to_obj(cachep, page, i); 15878c2ecf20Sopenharmony_ci 15888c2ecf20Sopenharmony_ci if (cachep->flags & SLAB_POISON) { 15898c2ecf20Sopenharmony_ci check_poison_obj(cachep, objp); 15908c2ecf20Sopenharmony_ci slab_kernel_map(cachep, objp, 1); 15918c2ecf20Sopenharmony_ci } 15928c2ecf20Sopenharmony_ci if (cachep->flags & SLAB_RED_ZONE) { 15938c2ecf20Sopenharmony_ci if (*dbg_redzone1(cachep, objp) != RED_INACTIVE) 15948c2ecf20Sopenharmony_ci slab_error(cachep, "start of a freed object was overwritten"); 15958c2ecf20Sopenharmony_ci if (*dbg_redzone2(cachep, objp) != RED_INACTIVE) 15968c2ecf20Sopenharmony_ci slab_error(cachep, "end of a freed object was overwritten"); 15978c2ecf20Sopenharmony_ci } 15988c2ecf20Sopenharmony_ci } 15998c2ecf20Sopenharmony_ci} 16008c2ecf20Sopenharmony_ci#else 16018c2ecf20Sopenharmony_cistatic void slab_destroy_debugcheck(struct kmem_cache *cachep, 16028c2ecf20Sopenharmony_ci struct page *page) 16038c2ecf20Sopenharmony_ci{ 16048c2ecf20Sopenharmony_ci} 16058c2ecf20Sopenharmony_ci#endif 16068c2ecf20Sopenharmony_ci 16078c2ecf20Sopenharmony_ci/** 16088c2ecf20Sopenharmony_ci * slab_destroy - destroy and release all objects in a slab 16098c2ecf20Sopenharmony_ci * @cachep: cache pointer being destroyed 16108c2ecf20Sopenharmony_ci * @page: page pointer being destroyed 16118c2ecf20Sopenharmony_ci * 16128c2ecf20Sopenharmony_ci * Destroy all the objs in a slab page, and release the mem back to the system. 16138c2ecf20Sopenharmony_ci * Before calling the slab page must have been unlinked from the cache. The 16148c2ecf20Sopenharmony_ci * kmem_cache_node ->list_lock is not held/needed. 16158c2ecf20Sopenharmony_ci */ 16168c2ecf20Sopenharmony_cistatic void slab_destroy(struct kmem_cache *cachep, struct page *page) 16178c2ecf20Sopenharmony_ci{ 16188c2ecf20Sopenharmony_ci void *freelist; 16198c2ecf20Sopenharmony_ci 16208c2ecf20Sopenharmony_ci freelist = page->freelist; 16218c2ecf20Sopenharmony_ci slab_destroy_debugcheck(cachep, page); 16228c2ecf20Sopenharmony_ci if (unlikely(cachep->flags & SLAB_TYPESAFE_BY_RCU)) 16238c2ecf20Sopenharmony_ci call_rcu(&page->rcu_head, kmem_rcu_free); 16248c2ecf20Sopenharmony_ci else 16258c2ecf20Sopenharmony_ci kmem_freepages(cachep, page); 16268c2ecf20Sopenharmony_ci 16278c2ecf20Sopenharmony_ci /* 16288c2ecf20Sopenharmony_ci * From now on, we don't use freelist 16298c2ecf20Sopenharmony_ci * although actual page can be freed in rcu context 16308c2ecf20Sopenharmony_ci */ 16318c2ecf20Sopenharmony_ci if (OFF_SLAB(cachep)) 16328c2ecf20Sopenharmony_ci kmem_cache_free(cachep->freelist_cache, freelist); 16338c2ecf20Sopenharmony_ci} 16348c2ecf20Sopenharmony_ci 16358c2ecf20Sopenharmony_ci/* 16368c2ecf20Sopenharmony_ci * Update the size of the caches before calling slabs_destroy as it may 16378c2ecf20Sopenharmony_ci * recursively call kfree. 16388c2ecf20Sopenharmony_ci */ 16398c2ecf20Sopenharmony_cistatic void slabs_destroy(struct kmem_cache *cachep, struct list_head *list) 16408c2ecf20Sopenharmony_ci{ 16418c2ecf20Sopenharmony_ci struct page *page, *n; 16428c2ecf20Sopenharmony_ci 16438c2ecf20Sopenharmony_ci list_for_each_entry_safe(page, n, list, slab_list) { 16448c2ecf20Sopenharmony_ci list_del(&page->slab_list); 16458c2ecf20Sopenharmony_ci slab_destroy(cachep, page); 16468c2ecf20Sopenharmony_ci } 16478c2ecf20Sopenharmony_ci} 16488c2ecf20Sopenharmony_ci 16498c2ecf20Sopenharmony_ci/** 16508c2ecf20Sopenharmony_ci * calculate_slab_order - calculate size (page order) of slabs 16518c2ecf20Sopenharmony_ci * @cachep: pointer to the cache that is being created 16528c2ecf20Sopenharmony_ci * @size: size of objects to be created in this cache. 16538c2ecf20Sopenharmony_ci * @flags: slab allocation flags 16548c2ecf20Sopenharmony_ci * 16558c2ecf20Sopenharmony_ci * Also calculates the number of objects per slab. 16568c2ecf20Sopenharmony_ci * 16578c2ecf20Sopenharmony_ci * This could be made much more intelligent. For now, try to avoid using 16588c2ecf20Sopenharmony_ci * high order pages for slabs. When the gfp() functions are more friendly 16598c2ecf20Sopenharmony_ci * towards high-order requests, this should be changed. 16608c2ecf20Sopenharmony_ci * 16618c2ecf20Sopenharmony_ci * Return: number of left-over bytes in a slab 16628c2ecf20Sopenharmony_ci */ 16638c2ecf20Sopenharmony_cistatic size_t calculate_slab_order(struct kmem_cache *cachep, 16648c2ecf20Sopenharmony_ci size_t size, slab_flags_t flags) 16658c2ecf20Sopenharmony_ci{ 16668c2ecf20Sopenharmony_ci size_t left_over = 0; 16678c2ecf20Sopenharmony_ci int gfporder; 16688c2ecf20Sopenharmony_ci 16698c2ecf20Sopenharmony_ci for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) { 16708c2ecf20Sopenharmony_ci unsigned int num; 16718c2ecf20Sopenharmony_ci size_t remainder; 16728c2ecf20Sopenharmony_ci 16738c2ecf20Sopenharmony_ci num = cache_estimate(gfporder, size, flags, &remainder); 16748c2ecf20Sopenharmony_ci if (!num) 16758c2ecf20Sopenharmony_ci continue; 16768c2ecf20Sopenharmony_ci 16778c2ecf20Sopenharmony_ci /* Can't handle number of objects more than SLAB_OBJ_MAX_NUM */ 16788c2ecf20Sopenharmony_ci if (num > SLAB_OBJ_MAX_NUM) 16798c2ecf20Sopenharmony_ci break; 16808c2ecf20Sopenharmony_ci 16818c2ecf20Sopenharmony_ci if (flags & CFLGS_OFF_SLAB) { 16828c2ecf20Sopenharmony_ci struct kmem_cache *freelist_cache; 16838c2ecf20Sopenharmony_ci size_t freelist_size; 16848c2ecf20Sopenharmony_ci 16858c2ecf20Sopenharmony_ci freelist_size = num * sizeof(freelist_idx_t); 16868c2ecf20Sopenharmony_ci freelist_cache = kmalloc_slab(freelist_size, 0u); 16878c2ecf20Sopenharmony_ci if (!freelist_cache) 16888c2ecf20Sopenharmony_ci continue; 16898c2ecf20Sopenharmony_ci 16908c2ecf20Sopenharmony_ci /* 16918c2ecf20Sopenharmony_ci * Needed to avoid possible looping condition 16928c2ecf20Sopenharmony_ci * in cache_grow_begin() 16938c2ecf20Sopenharmony_ci */ 16948c2ecf20Sopenharmony_ci if (OFF_SLAB(freelist_cache)) 16958c2ecf20Sopenharmony_ci continue; 16968c2ecf20Sopenharmony_ci 16978c2ecf20Sopenharmony_ci /* check if off slab has enough benefit */ 16988c2ecf20Sopenharmony_ci if (freelist_cache->size > cachep->size / 2) 16998c2ecf20Sopenharmony_ci continue; 17008c2ecf20Sopenharmony_ci } 17018c2ecf20Sopenharmony_ci 17028c2ecf20Sopenharmony_ci /* Found something acceptable - save it away */ 17038c2ecf20Sopenharmony_ci cachep->num = num; 17048c2ecf20Sopenharmony_ci cachep->gfporder = gfporder; 17058c2ecf20Sopenharmony_ci left_over = remainder; 17068c2ecf20Sopenharmony_ci 17078c2ecf20Sopenharmony_ci /* 17088c2ecf20Sopenharmony_ci * A VFS-reclaimable slab tends to have most allocations 17098c2ecf20Sopenharmony_ci * as GFP_NOFS and we really don't want to have to be allocating 17108c2ecf20Sopenharmony_ci * higher-order pages when we are unable to shrink dcache. 17118c2ecf20Sopenharmony_ci */ 17128c2ecf20Sopenharmony_ci if (flags & SLAB_RECLAIM_ACCOUNT) 17138c2ecf20Sopenharmony_ci break; 17148c2ecf20Sopenharmony_ci 17158c2ecf20Sopenharmony_ci /* 17168c2ecf20Sopenharmony_ci * Large number of objects is good, but very large slabs are 17178c2ecf20Sopenharmony_ci * currently bad for the gfp()s. 17188c2ecf20Sopenharmony_ci */ 17198c2ecf20Sopenharmony_ci if (gfporder >= slab_max_order) 17208c2ecf20Sopenharmony_ci break; 17218c2ecf20Sopenharmony_ci 17228c2ecf20Sopenharmony_ci /* 17238c2ecf20Sopenharmony_ci * Acceptable internal fragmentation? 17248c2ecf20Sopenharmony_ci */ 17258c2ecf20Sopenharmony_ci if (left_over * 8 <= (PAGE_SIZE << gfporder)) 17268c2ecf20Sopenharmony_ci break; 17278c2ecf20Sopenharmony_ci } 17288c2ecf20Sopenharmony_ci return left_over; 17298c2ecf20Sopenharmony_ci} 17308c2ecf20Sopenharmony_ci 17318c2ecf20Sopenharmony_cistatic struct array_cache __percpu *alloc_kmem_cache_cpus( 17328c2ecf20Sopenharmony_ci struct kmem_cache *cachep, int entries, int batchcount) 17338c2ecf20Sopenharmony_ci{ 17348c2ecf20Sopenharmony_ci int cpu; 17358c2ecf20Sopenharmony_ci size_t size; 17368c2ecf20Sopenharmony_ci struct array_cache __percpu *cpu_cache; 17378c2ecf20Sopenharmony_ci 17388c2ecf20Sopenharmony_ci size = sizeof(void *) * entries + sizeof(struct array_cache); 17398c2ecf20Sopenharmony_ci cpu_cache = __alloc_percpu(size, sizeof(void *)); 17408c2ecf20Sopenharmony_ci 17418c2ecf20Sopenharmony_ci if (!cpu_cache) 17428c2ecf20Sopenharmony_ci return NULL; 17438c2ecf20Sopenharmony_ci 17448c2ecf20Sopenharmony_ci for_each_possible_cpu(cpu) { 17458c2ecf20Sopenharmony_ci init_arraycache(per_cpu_ptr(cpu_cache, cpu), 17468c2ecf20Sopenharmony_ci entries, batchcount); 17478c2ecf20Sopenharmony_ci } 17488c2ecf20Sopenharmony_ci 17498c2ecf20Sopenharmony_ci return cpu_cache; 17508c2ecf20Sopenharmony_ci} 17518c2ecf20Sopenharmony_ci 17528c2ecf20Sopenharmony_cistatic int __ref setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp) 17538c2ecf20Sopenharmony_ci{ 17548c2ecf20Sopenharmony_ci if (slab_state >= FULL) 17558c2ecf20Sopenharmony_ci return enable_cpucache(cachep, gfp); 17568c2ecf20Sopenharmony_ci 17578c2ecf20Sopenharmony_ci cachep->cpu_cache = alloc_kmem_cache_cpus(cachep, 1, 1); 17588c2ecf20Sopenharmony_ci if (!cachep->cpu_cache) 17598c2ecf20Sopenharmony_ci return 1; 17608c2ecf20Sopenharmony_ci 17618c2ecf20Sopenharmony_ci if (slab_state == DOWN) { 17628c2ecf20Sopenharmony_ci /* Creation of first cache (kmem_cache). */ 17638c2ecf20Sopenharmony_ci set_up_node(kmem_cache, CACHE_CACHE); 17648c2ecf20Sopenharmony_ci } else if (slab_state == PARTIAL) { 17658c2ecf20Sopenharmony_ci /* For kmem_cache_node */ 17668c2ecf20Sopenharmony_ci set_up_node(cachep, SIZE_NODE); 17678c2ecf20Sopenharmony_ci } else { 17688c2ecf20Sopenharmony_ci int node; 17698c2ecf20Sopenharmony_ci 17708c2ecf20Sopenharmony_ci for_each_online_node(node) { 17718c2ecf20Sopenharmony_ci cachep->node[node] = kmalloc_node( 17728c2ecf20Sopenharmony_ci sizeof(struct kmem_cache_node), gfp, node); 17738c2ecf20Sopenharmony_ci BUG_ON(!cachep->node[node]); 17748c2ecf20Sopenharmony_ci kmem_cache_node_init(cachep->node[node]); 17758c2ecf20Sopenharmony_ci } 17768c2ecf20Sopenharmony_ci } 17778c2ecf20Sopenharmony_ci 17788c2ecf20Sopenharmony_ci cachep->node[numa_mem_id()]->next_reap = 17798c2ecf20Sopenharmony_ci jiffies + REAPTIMEOUT_NODE + 17808c2ecf20Sopenharmony_ci ((unsigned long)cachep) % REAPTIMEOUT_NODE; 17818c2ecf20Sopenharmony_ci 17828c2ecf20Sopenharmony_ci cpu_cache_get(cachep)->avail = 0; 17838c2ecf20Sopenharmony_ci cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES; 17848c2ecf20Sopenharmony_ci cpu_cache_get(cachep)->batchcount = 1; 17858c2ecf20Sopenharmony_ci cpu_cache_get(cachep)->touched = 0; 17868c2ecf20Sopenharmony_ci cachep->batchcount = 1; 17878c2ecf20Sopenharmony_ci cachep->limit = BOOT_CPUCACHE_ENTRIES; 17888c2ecf20Sopenharmony_ci return 0; 17898c2ecf20Sopenharmony_ci} 17908c2ecf20Sopenharmony_ci 17918c2ecf20Sopenharmony_cislab_flags_t kmem_cache_flags(unsigned int object_size, 17928c2ecf20Sopenharmony_ci slab_flags_t flags, const char *name) 17938c2ecf20Sopenharmony_ci{ 17948c2ecf20Sopenharmony_ci return flags; 17958c2ecf20Sopenharmony_ci} 17968c2ecf20Sopenharmony_ci 17978c2ecf20Sopenharmony_cistruct kmem_cache * 17988c2ecf20Sopenharmony_ci__kmem_cache_alias(const char *name, unsigned int size, unsigned int align, 17998c2ecf20Sopenharmony_ci slab_flags_t flags, void (*ctor)(void *)) 18008c2ecf20Sopenharmony_ci{ 18018c2ecf20Sopenharmony_ci struct kmem_cache *cachep; 18028c2ecf20Sopenharmony_ci 18038c2ecf20Sopenharmony_ci cachep = find_mergeable(size, align, flags, name, ctor); 18048c2ecf20Sopenharmony_ci if (cachep) { 18058c2ecf20Sopenharmony_ci cachep->refcount++; 18068c2ecf20Sopenharmony_ci 18078c2ecf20Sopenharmony_ci /* 18088c2ecf20Sopenharmony_ci * Adjust the object sizes so that we clear 18098c2ecf20Sopenharmony_ci * the complete object on kzalloc. 18108c2ecf20Sopenharmony_ci */ 18118c2ecf20Sopenharmony_ci cachep->object_size = max_t(int, cachep->object_size, size); 18128c2ecf20Sopenharmony_ci } 18138c2ecf20Sopenharmony_ci return cachep; 18148c2ecf20Sopenharmony_ci} 18158c2ecf20Sopenharmony_ci 18168c2ecf20Sopenharmony_cistatic bool set_objfreelist_slab_cache(struct kmem_cache *cachep, 18178c2ecf20Sopenharmony_ci size_t size, slab_flags_t flags) 18188c2ecf20Sopenharmony_ci{ 18198c2ecf20Sopenharmony_ci size_t left; 18208c2ecf20Sopenharmony_ci 18218c2ecf20Sopenharmony_ci cachep->num = 0; 18228c2ecf20Sopenharmony_ci 18238c2ecf20Sopenharmony_ci /* 18248c2ecf20Sopenharmony_ci * If slab auto-initialization on free is enabled, store the freelist 18258c2ecf20Sopenharmony_ci * off-slab, so that its contents don't end up in one of the allocated 18268c2ecf20Sopenharmony_ci * objects. 18278c2ecf20Sopenharmony_ci */ 18288c2ecf20Sopenharmony_ci if (unlikely(slab_want_init_on_free(cachep))) 18298c2ecf20Sopenharmony_ci return false; 18308c2ecf20Sopenharmony_ci 18318c2ecf20Sopenharmony_ci if (cachep->ctor || flags & SLAB_TYPESAFE_BY_RCU) 18328c2ecf20Sopenharmony_ci return false; 18338c2ecf20Sopenharmony_ci 18348c2ecf20Sopenharmony_ci left = calculate_slab_order(cachep, size, 18358c2ecf20Sopenharmony_ci flags | CFLGS_OBJFREELIST_SLAB); 18368c2ecf20Sopenharmony_ci if (!cachep->num) 18378c2ecf20Sopenharmony_ci return false; 18388c2ecf20Sopenharmony_ci 18398c2ecf20Sopenharmony_ci if (cachep->num * sizeof(freelist_idx_t) > cachep->object_size) 18408c2ecf20Sopenharmony_ci return false; 18418c2ecf20Sopenharmony_ci 18428c2ecf20Sopenharmony_ci cachep->colour = left / cachep->colour_off; 18438c2ecf20Sopenharmony_ci 18448c2ecf20Sopenharmony_ci return true; 18458c2ecf20Sopenharmony_ci} 18468c2ecf20Sopenharmony_ci 18478c2ecf20Sopenharmony_cistatic bool set_off_slab_cache(struct kmem_cache *cachep, 18488c2ecf20Sopenharmony_ci size_t size, slab_flags_t flags) 18498c2ecf20Sopenharmony_ci{ 18508c2ecf20Sopenharmony_ci size_t left; 18518c2ecf20Sopenharmony_ci 18528c2ecf20Sopenharmony_ci cachep->num = 0; 18538c2ecf20Sopenharmony_ci 18548c2ecf20Sopenharmony_ci /* 18558c2ecf20Sopenharmony_ci * Always use on-slab management when SLAB_NOLEAKTRACE 18568c2ecf20Sopenharmony_ci * to avoid recursive calls into kmemleak. 18578c2ecf20Sopenharmony_ci */ 18588c2ecf20Sopenharmony_ci if (flags & SLAB_NOLEAKTRACE) 18598c2ecf20Sopenharmony_ci return false; 18608c2ecf20Sopenharmony_ci 18618c2ecf20Sopenharmony_ci /* 18628c2ecf20Sopenharmony_ci * Size is large, assume best to place the slab management obj 18638c2ecf20Sopenharmony_ci * off-slab (should allow better packing of objs). 18648c2ecf20Sopenharmony_ci */ 18658c2ecf20Sopenharmony_ci left = calculate_slab_order(cachep, size, flags | CFLGS_OFF_SLAB); 18668c2ecf20Sopenharmony_ci if (!cachep->num) 18678c2ecf20Sopenharmony_ci return false; 18688c2ecf20Sopenharmony_ci 18698c2ecf20Sopenharmony_ci /* 18708c2ecf20Sopenharmony_ci * If the slab has been placed off-slab, and we have enough space then 18718c2ecf20Sopenharmony_ci * move it on-slab. This is at the expense of any extra colouring. 18728c2ecf20Sopenharmony_ci */ 18738c2ecf20Sopenharmony_ci if (left >= cachep->num * sizeof(freelist_idx_t)) 18748c2ecf20Sopenharmony_ci return false; 18758c2ecf20Sopenharmony_ci 18768c2ecf20Sopenharmony_ci cachep->colour = left / cachep->colour_off; 18778c2ecf20Sopenharmony_ci 18788c2ecf20Sopenharmony_ci return true; 18798c2ecf20Sopenharmony_ci} 18808c2ecf20Sopenharmony_ci 18818c2ecf20Sopenharmony_cistatic bool set_on_slab_cache(struct kmem_cache *cachep, 18828c2ecf20Sopenharmony_ci size_t size, slab_flags_t flags) 18838c2ecf20Sopenharmony_ci{ 18848c2ecf20Sopenharmony_ci size_t left; 18858c2ecf20Sopenharmony_ci 18868c2ecf20Sopenharmony_ci cachep->num = 0; 18878c2ecf20Sopenharmony_ci 18888c2ecf20Sopenharmony_ci left = calculate_slab_order(cachep, size, flags); 18898c2ecf20Sopenharmony_ci if (!cachep->num) 18908c2ecf20Sopenharmony_ci return false; 18918c2ecf20Sopenharmony_ci 18928c2ecf20Sopenharmony_ci cachep->colour = left / cachep->colour_off; 18938c2ecf20Sopenharmony_ci 18948c2ecf20Sopenharmony_ci return true; 18958c2ecf20Sopenharmony_ci} 18968c2ecf20Sopenharmony_ci 18978c2ecf20Sopenharmony_ci/** 18988c2ecf20Sopenharmony_ci * __kmem_cache_create - Create a cache. 18998c2ecf20Sopenharmony_ci * @cachep: cache management descriptor 19008c2ecf20Sopenharmony_ci * @flags: SLAB flags 19018c2ecf20Sopenharmony_ci * 19028c2ecf20Sopenharmony_ci * Returns a ptr to the cache on success, NULL on failure. 19038c2ecf20Sopenharmony_ci * Cannot be called within a int, but can be interrupted. 19048c2ecf20Sopenharmony_ci * The @ctor is run when new pages are allocated by the cache. 19058c2ecf20Sopenharmony_ci * 19068c2ecf20Sopenharmony_ci * The flags are 19078c2ecf20Sopenharmony_ci * 19088c2ecf20Sopenharmony_ci * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5) 19098c2ecf20Sopenharmony_ci * to catch references to uninitialised memory. 19108c2ecf20Sopenharmony_ci * 19118c2ecf20Sopenharmony_ci * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check 19128c2ecf20Sopenharmony_ci * for buffer overruns. 19138c2ecf20Sopenharmony_ci * 19148c2ecf20Sopenharmony_ci * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware 19158c2ecf20Sopenharmony_ci * cacheline. This can be beneficial if you're counting cycles as closely 19168c2ecf20Sopenharmony_ci * as davem. 19178c2ecf20Sopenharmony_ci * 19188c2ecf20Sopenharmony_ci * Return: a pointer to the created cache or %NULL in case of error 19198c2ecf20Sopenharmony_ci */ 19208c2ecf20Sopenharmony_ciint __kmem_cache_create(struct kmem_cache *cachep, slab_flags_t flags) 19218c2ecf20Sopenharmony_ci{ 19228c2ecf20Sopenharmony_ci size_t ralign = BYTES_PER_WORD; 19238c2ecf20Sopenharmony_ci gfp_t gfp; 19248c2ecf20Sopenharmony_ci int err; 19258c2ecf20Sopenharmony_ci unsigned int size = cachep->size; 19268c2ecf20Sopenharmony_ci 19278c2ecf20Sopenharmony_ci#if DEBUG 19288c2ecf20Sopenharmony_ci#if FORCED_DEBUG 19298c2ecf20Sopenharmony_ci /* 19308c2ecf20Sopenharmony_ci * Enable redzoning and last user accounting, except for caches with 19318c2ecf20Sopenharmony_ci * large objects, if the increased size would increase the object size 19328c2ecf20Sopenharmony_ci * above the next power of two: caches with object sizes just above a 19338c2ecf20Sopenharmony_ci * power of two have a significant amount of internal fragmentation. 19348c2ecf20Sopenharmony_ci */ 19358c2ecf20Sopenharmony_ci if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN + 19368c2ecf20Sopenharmony_ci 2 * sizeof(unsigned long long))) 19378c2ecf20Sopenharmony_ci flags |= SLAB_RED_ZONE | SLAB_STORE_USER; 19388c2ecf20Sopenharmony_ci if (!(flags & SLAB_TYPESAFE_BY_RCU)) 19398c2ecf20Sopenharmony_ci flags |= SLAB_POISON; 19408c2ecf20Sopenharmony_ci#endif 19418c2ecf20Sopenharmony_ci#endif 19428c2ecf20Sopenharmony_ci 19438c2ecf20Sopenharmony_ci /* 19448c2ecf20Sopenharmony_ci * Check that size is in terms of words. This is needed to avoid 19458c2ecf20Sopenharmony_ci * unaligned accesses for some archs when redzoning is used, and makes 19468c2ecf20Sopenharmony_ci * sure any on-slab bufctl's are also correctly aligned. 19478c2ecf20Sopenharmony_ci */ 19488c2ecf20Sopenharmony_ci size = ALIGN(size, BYTES_PER_WORD); 19498c2ecf20Sopenharmony_ci 19508c2ecf20Sopenharmony_ci if (flags & SLAB_RED_ZONE) { 19518c2ecf20Sopenharmony_ci ralign = REDZONE_ALIGN; 19528c2ecf20Sopenharmony_ci /* If redzoning, ensure that the second redzone is suitably 19538c2ecf20Sopenharmony_ci * aligned, by adjusting the object size accordingly. */ 19548c2ecf20Sopenharmony_ci size = ALIGN(size, REDZONE_ALIGN); 19558c2ecf20Sopenharmony_ci } 19568c2ecf20Sopenharmony_ci 19578c2ecf20Sopenharmony_ci /* 3) caller mandated alignment */ 19588c2ecf20Sopenharmony_ci if (ralign < cachep->align) { 19598c2ecf20Sopenharmony_ci ralign = cachep->align; 19608c2ecf20Sopenharmony_ci } 19618c2ecf20Sopenharmony_ci /* disable debug if necessary */ 19628c2ecf20Sopenharmony_ci if (ralign > __alignof__(unsigned long long)) 19638c2ecf20Sopenharmony_ci flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER); 19648c2ecf20Sopenharmony_ci /* 19658c2ecf20Sopenharmony_ci * 4) Store it. 19668c2ecf20Sopenharmony_ci */ 19678c2ecf20Sopenharmony_ci cachep->align = ralign; 19688c2ecf20Sopenharmony_ci cachep->colour_off = cache_line_size(); 19698c2ecf20Sopenharmony_ci /* Offset must be a multiple of the alignment. */ 19708c2ecf20Sopenharmony_ci if (cachep->colour_off < cachep->align) 19718c2ecf20Sopenharmony_ci cachep->colour_off = cachep->align; 19728c2ecf20Sopenharmony_ci 19738c2ecf20Sopenharmony_ci if (slab_is_available()) 19748c2ecf20Sopenharmony_ci gfp = GFP_KERNEL; 19758c2ecf20Sopenharmony_ci else 19768c2ecf20Sopenharmony_ci gfp = GFP_NOWAIT; 19778c2ecf20Sopenharmony_ci 19788c2ecf20Sopenharmony_ci#if DEBUG 19798c2ecf20Sopenharmony_ci 19808c2ecf20Sopenharmony_ci /* 19818c2ecf20Sopenharmony_ci * Both debugging options require word-alignment which is calculated 19828c2ecf20Sopenharmony_ci * into align above. 19838c2ecf20Sopenharmony_ci */ 19848c2ecf20Sopenharmony_ci if (flags & SLAB_RED_ZONE) { 19858c2ecf20Sopenharmony_ci /* add space for red zone words */ 19868c2ecf20Sopenharmony_ci cachep->obj_offset += sizeof(unsigned long long); 19878c2ecf20Sopenharmony_ci size += 2 * sizeof(unsigned long long); 19888c2ecf20Sopenharmony_ci } 19898c2ecf20Sopenharmony_ci if (flags & SLAB_STORE_USER) { 19908c2ecf20Sopenharmony_ci /* user store requires one word storage behind the end of 19918c2ecf20Sopenharmony_ci * the real object. But if the second red zone needs to be 19928c2ecf20Sopenharmony_ci * aligned to 64 bits, we must allow that much space. 19938c2ecf20Sopenharmony_ci */ 19948c2ecf20Sopenharmony_ci if (flags & SLAB_RED_ZONE) 19958c2ecf20Sopenharmony_ci size += REDZONE_ALIGN; 19968c2ecf20Sopenharmony_ci else 19978c2ecf20Sopenharmony_ci size += BYTES_PER_WORD; 19988c2ecf20Sopenharmony_ci } 19998c2ecf20Sopenharmony_ci#endif 20008c2ecf20Sopenharmony_ci 20018c2ecf20Sopenharmony_ci kasan_cache_create(cachep, &size, &flags); 20028c2ecf20Sopenharmony_ci 20038c2ecf20Sopenharmony_ci size = ALIGN(size, cachep->align); 20048c2ecf20Sopenharmony_ci /* 20058c2ecf20Sopenharmony_ci * We should restrict the number of objects in a slab to implement 20068c2ecf20Sopenharmony_ci * byte sized index. Refer comment on SLAB_OBJ_MIN_SIZE definition. 20078c2ecf20Sopenharmony_ci */ 20088c2ecf20Sopenharmony_ci if (FREELIST_BYTE_INDEX && size < SLAB_OBJ_MIN_SIZE) 20098c2ecf20Sopenharmony_ci size = ALIGN(SLAB_OBJ_MIN_SIZE, cachep->align); 20108c2ecf20Sopenharmony_ci 20118c2ecf20Sopenharmony_ci#if DEBUG 20128c2ecf20Sopenharmony_ci /* 20138c2ecf20Sopenharmony_ci * To activate debug pagealloc, off-slab management is necessary 20148c2ecf20Sopenharmony_ci * requirement. In early phase of initialization, small sized slab 20158c2ecf20Sopenharmony_ci * doesn't get initialized so it would not be possible. So, we need 20168c2ecf20Sopenharmony_ci * to check size >= 256. It guarantees that all necessary small 20178c2ecf20Sopenharmony_ci * sized slab is initialized in current slab initialization sequence. 20188c2ecf20Sopenharmony_ci */ 20198c2ecf20Sopenharmony_ci if (debug_pagealloc_enabled_static() && (flags & SLAB_POISON) && 20208c2ecf20Sopenharmony_ci size >= 256 && cachep->object_size > cache_line_size()) { 20218c2ecf20Sopenharmony_ci if (size < PAGE_SIZE || size % PAGE_SIZE == 0) { 20228c2ecf20Sopenharmony_ci size_t tmp_size = ALIGN(size, PAGE_SIZE); 20238c2ecf20Sopenharmony_ci 20248c2ecf20Sopenharmony_ci if (set_off_slab_cache(cachep, tmp_size, flags)) { 20258c2ecf20Sopenharmony_ci flags |= CFLGS_OFF_SLAB; 20268c2ecf20Sopenharmony_ci cachep->obj_offset += tmp_size - size; 20278c2ecf20Sopenharmony_ci size = tmp_size; 20288c2ecf20Sopenharmony_ci goto done; 20298c2ecf20Sopenharmony_ci } 20308c2ecf20Sopenharmony_ci } 20318c2ecf20Sopenharmony_ci } 20328c2ecf20Sopenharmony_ci#endif 20338c2ecf20Sopenharmony_ci 20348c2ecf20Sopenharmony_ci if (set_objfreelist_slab_cache(cachep, size, flags)) { 20358c2ecf20Sopenharmony_ci flags |= CFLGS_OBJFREELIST_SLAB; 20368c2ecf20Sopenharmony_ci goto done; 20378c2ecf20Sopenharmony_ci } 20388c2ecf20Sopenharmony_ci 20398c2ecf20Sopenharmony_ci if (set_off_slab_cache(cachep, size, flags)) { 20408c2ecf20Sopenharmony_ci flags |= CFLGS_OFF_SLAB; 20418c2ecf20Sopenharmony_ci goto done; 20428c2ecf20Sopenharmony_ci } 20438c2ecf20Sopenharmony_ci 20448c2ecf20Sopenharmony_ci if (set_on_slab_cache(cachep, size, flags)) 20458c2ecf20Sopenharmony_ci goto done; 20468c2ecf20Sopenharmony_ci 20478c2ecf20Sopenharmony_ci return -E2BIG; 20488c2ecf20Sopenharmony_ci 20498c2ecf20Sopenharmony_cidone: 20508c2ecf20Sopenharmony_ci cachep->freelist_size = cachep->num * sizeof(freelist_idx_t); 20518c2ecf20Sopenharmony_ci cachep->flags = flags; 20528c2ecf20Sopenharmony_ci cachep->allocflags = __GFP_COMP; 20538c2ecf20Sopenharmony_ci if (flags & SLAB_CACHE_DMA) 20548c2ecf20Sopenharmony_ci cachep->allocflags |= GFP_DMA; 20558c2ecf20Sopenharmony_ci if (flags & SLAB_CACHE_DMA32) 20568c2ecf20Sopenharmony_ci cachep->allocflags |= GFP_DMA32; 20578c2ecf20Sopenharmony_ci if (flags & SLAB_RECLAIM_ACCOUNT) 20588c2ecf20Sopenharmony_ci cachep->allocflags |= __GFP_RECLAIMABLE; 20598c2ecf20Sopenharmony_ci cachep->size = size; 20608c2ecf20Sopenharmony_ci cachep->reciprocal_buffer_size = reciprocal_value(size); 20618c2ecf20Sopenharmony_ci 20628c2ecf20Sopenharmony_ci#if DEBUG 20638c2ecf20Sopenharmony_ci /* 20648c2ecf20Sopenharmony_ci * If we're going to use the generic kernel_map_pages() 20658c2ecf20Sopenharmony_ci * poisoning, then it's going to smash the contents of 20668c2ecf20Sopenharmony_ci * the redzone and userword anyhow, so switch them off. 20678c2ecf20Sopenharmony_ci */ 20688c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_PAGE_POISONING) && 20698c2ecf20Sopenharmony_ci (cachep->flags & SLAB_POISON) && 20708c2ecf20Sopenharmony_ci is_debug_pagealloc_cache(cachep)) 20718c2ecf20Sopenharmony_ci cachep->flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER); 20728c2ecf20Sopenharmony_ci#endif 20738c2ecf20Sopenharmony_ci 20748c2ecf20Sopenharmony_ci if (OFF_SLAB(cachep)) { 20758c2ecf20Sopenharmony_ci cachep->freelist_cache = 20768c2ecf20Sopenharmony_ci kmalloc_slab(cachep->freelist_size, 0u); 20778c2ecf20Sopenharmony_ci } 20788c2ecf20Sopenharmony_ci 20798c2ecf20Sopenharmony_ci err = setup_cpu_cache(cachep, gfp); 20808c2ecf20Sopenharmony_ci if (err) { 20818c2ecf20Sopenharmony_ci __kmem_cache_release(cachep); 20828c2ecf20Sopenharmony_ci return err; 20838c2ecf20Sopenharmony_ci } 20848c2ecf20Sopenharmony_ci 20858c2ecf20Sopenharmony_ci return 0; 20868c2ecf20Sopenharmony_ci} 20878c2ecf20Sopenharmony_ci 20888c2ecf20Sopenharmony_ci#if DEBUG 20898c2ecf20Sopenharmony_cistatic void check_irq_off(void) 20908c2ecf20Sopenharmony_ci{ 20918c2ecf20Sopenharmony_ci BUG_ON(!irqs_disabled()); 20928c2ecf20Sopenharmony_ci} 20938c2ecf20Sopenharmony_ci 20948c2ecf20Sopenharmony_cistatic void check_irq_on(void) 20958c2ecf20Sopenharmony_ci{ 20968c2ecf20Sopenharmony_ci BUG_ON(irqs_disabled()); 20978c2ecf20Sopenharmony_ci} 20988c2ecf20Sopenharmony_ci 20998c2ecf20Sopenharmony_cistatic void check_mutex_acquired(void) 21008c2ecf20Sopenharmony_ci{ 21018c2ecf20Sopenharmony_ci BUG_ON(!mutex_is_locked(&slab_mutex)); 21028c2ecf20Sopenharmony_ci} 21038c2ecf20Sopenharmony_ci 21048c2ecf20Sopenharmony_cistatic void check_spinlock_acquired(struct kmem_cache *cachep) 21058c2ecf20Sopenharmony_ci{ 21068c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP 21078c2ecf20Sopenharmony_ci check_irq_off(); 21088c2ecf20Sopenharmony_ci assert_spin_locked(&get_node(cachep, numa_mem_id())->list_lock); 21098c2ecf20Sopenharmony_ci#endif 21108c2ecf20Sopenharmony_ci} 21118c2ecf20Sopenharmony_ci 21128c2ecf20Sopenharmony_cistatic void check_spinlock_acquired_node(struct kmem_cache *cachep, int node) 21138c2ecf20Sopenharmony_ci{ 21148c2ecf20Sopenharmony_ci#ifdef CONFIG_SMP 21158c2ecf20Sopenharmony_ci check_irq_off(); 21168c2ecf20Sopenharmony_ci assert_spin_locked(&get_node(cachep, node)->list_lock); 21178c2ecf20Sopenharmony_ci#endif 21188c2ecf20Sopenharmony_ci} 21198c2ecf20Sopenharmony_ci 21208c2ecf20Sopenharmony_ci#else 21218c2ecf20Sopenharmony_ci#define check_irq_off() do { } while(0) 21228c2ecf20Sopenharmony_ci#define check_irq_on() do { } while(0) 21238c2ecf20Sopenharmony_ci#define check_mutex_acquired() do { } while(0) 21248c2ecf20Sopenharmony_ci#define check_spinlock_acquired(x) do { } while(0) 21258c2ecf20Sopenharmony_ci#define check_spinlock_acquired_node(x, y) do { } while(0) 21268c2ecf20Sopenharmony_ci#endif 21278c2ecf20Sopenharmony_ci 21288c2ecf20Sopenharmony_cistatic void drain_array_locked(struct kmem_cache *cachep, struct array_cache *ac, 21298c2ecf20Sopenharmony_ci int node, bool free_all, struct list_head *list) 21308c2ecf20Sopenharmony_ci{ 21318c2ecf20Sopenharmony_ci int tofree; 21328c2ecf20Sopenharmony_ci 21338c2ecf20Sopenharmony_ci if (!ac || !ac->avail) 21348c2ecf20Sopenharmony_ci return; 21358c2ecf20Sopenharmony_ci 21368c2ecf20Sopenharmony_ci tofree = free_all ? ac->avail : (ac->limit + 4) / 5; 21378c2ecf20Sopenharmony_ci if (tofree > ac->avail) 21388c2ecf20Sopenharmony_ci tofree = (ac->avail + 1) / 2; 21398c2ecf20Sopenharmony_ci 21408c2ecf20Sopenharmony_ci free_block(cachep, ac->entry, tofree, node, list); 21418c2ecf20Sopenharmony_ci ac->avail -= tofree; 21428c2ecf20Sopenharmony_ci memmove(ac->entry, &(ac->entry[tofree]), sizeof(void *) * ac->avail); 21438c2ecf20Sopenharmony_ci} 21448c2ecf20Sopenharmony_ci 21458c2ecf20Sopenharmony_cistatic void do_drain(void *arg) 21468c2ecf20Sopenharmony_ci{ 21478c2ecf20Sopenharmony_ci struct kmem_cache *cachep = arg; 21488c2ecf20Sopenharmony_ci struct array_cache *ac; 21498c2ecf20Sopenharmony_ci int node = numa_mem_id(); 21508c2ecf20Sopenharmony_ci struct kmem_cache_node *n; 21518c2ecf20Sopenharmony_ci LIST_HEAD(list); 21528c2ecf20Sopenharmony_ci 21538c2ecf20Sopenharmony_ci check_irq_off(); 21548c2ecf20Sopenharmony_ci ac = cpu_cache_get(cachep); 21558c2ecf20Sopenharmony_ci n = get_node(cachep, node); 21568c2ecf20Sopenharmony_ci spin_lock(&n->list_lock); 21578c2ecf20Sopenharmony_ci free_block(cachep, ac->entry, ac->avail, node, &list); 21588c2ecf20Sopenharmony_ci spin_unlock(&n->list_lock); 21598c2ecf20Sopenharmony_ci ac->avail = 0; 21608c2ecf20Sopenharmony_ci slabs_destroy(cachep, &list); 21618c2ecf20Sopenharmony_ci} 21628c2ecf20Sopenharmony_ci 21638c2ecf20Sopenharmony_cistatic void drain_cpu_caches(struct kmem_cache *cachep) 21648c2ecf20Sopenharmony_ci{ 21658c2ecf20Sopenharmony_ci struct kmem_cache_node *n; 21668c2ecf20Sopenharmony_ci int node; 21678c2ecf20Sopenharmony_ci LIST_HEAD(list); 21688c2ecf20Sopenharmony_ci 21698c2ecf20Sopenharmony_ci on_each_cpu(do_drain, cachep, 1); 21708c2ecf20Sopenharmony_ci check_irq_on(); 21718c2ecf20Sopenharmony_ci for_each_kmem_cache_node(cachep, node, n) 21728c2ecf20Sopenharmony_ci if (n->alien) 21738c2ecf20Sopenharmony_ci drain_alien_cache(cachep, n->alien); 21748c2ecf20Sopenharmony_ci 21758c2ecf20Sopenharmony_ci for_each_kmem_cache_node(cachep, node, n) { 21768c2ecf20Sopenharmony_ci spin_lock_irq(&n->list_lock); 21778c2ecf20Sopenharmony_ci drain_array_locked(cachep, n->shared, node, true, &list); 21788c2ecf20Sopenharmony_ci spin_unlock_irq(&n->list_lock); 21798c2ecf20Sopenharmony_ci 21808c2ecf20Sopenharmony_ci slabs_destroy(cachep, &list); 21818c2ecf20Sopenharmony_ci } 21828c2ecf20Sopenharmony_ci} 21838c2ecf20Sopenharmony_ci 21848c2ecf20Sopenharmony_ci/* 21858c2ecf20Sopenharmony_ci * Remove slabs from the list of free slabs. 21868c2ecf20Sopenharmony_ci * Specify the number of slabs to drain in tofree. 21878c2ecf20Sopenharmony_ci * 21888c2ecf20Sopenharmony_ci * Returns the actual number of slabs released. 21898c2ecf20Sopenharmony_ci */ 21908c2ecf20Sopenharmony_cistatic int drain_freelist(struct kmem_cache *cache, 21918c2ecf20Sopenharmony_ci struct kmem_cache_node *n, int tofree) 21928c2ecf20Sopenharmony_ci{ 21938c2ecf20Sopenharmony_ci struct list_head *p; 21948c2ecf20Sopenharmony_ci int nr_freed; 21958c2ecf20Sopenharmony_ci struct page *page; 21968c2ecf20Sopenharmony_ci 21978c2ecf20Sopenharmony_ci nr_freed = 0; 21988c2ecf20Sopenharmony_ci while (nr_freed < tofree && !list_empty(&n->slabs_free)) { 21998c2ecf20Sopenharmony_ci 22008c2ecf20Sopenharmony_ci spin_lock_irq(&n->list_lock); 22018c2ecf20Sopenharmony_ci p = n->slabs_free.prev; 22028c2ecf20Sopenharmony_ci if (p == &n->slabs_free) { 22038c2ecf20Sopenharmony_ci spin_unlock_irq(&n->list_lock); 22048c2ecf20Sopenharmony_ci goto out; 22058c2ecf20Sopenharmony_ci } 22068c2ecf20Sopenharmony_ci 22078c2ecf20Sopenharmony_ci page = list_entry(p, struct page, slab_list); 22088c2ecf20Sopenharmony_ci list_del(&page->slab_list); 22098c2ecf20Sopenharmony_ci n->free_slabs--; 22108c2ecf20Sopenharmony_ci n->total_slabs--; 22118c2ecf20Sopenharmony_ci /* 22128c2ecf20Sopenharmony_ci * Safe to drop the lock. The slab is no longer linked 22138c2ecf20Sopenharmony_ci * to the cache. 22148c2ecf20Sopenharmony_ci */ 22158c2ecf20Sopenharmony_ci n->free_objects -= cache->num; 22168c2ecf20Sopenharmony_ci spin_unlock_irq(&n->list_lock); 22178c2ecf20Sopenharmony_ci slab_destroy(cache, page); 22188c2ecf20Sopenharmony_ci nr_freed++; 22198c2ecf20Sopenharmony_ci } 22208c2ecf20Sopenharmony_ciout: 22218c2ecf20Sopenharmony_ci return nr_freed; 22228c2ecf20Sopenharmony_ci} 22238c2ecf20Sopenharmony_ci 22248c2ecf20Sopenharmony_cibool __kmem_cache_empty(struct kmem_cache *s) 22258c2ecf20Sopenharmony_ci{ 22268c2ecf20Sopenharmony_ci int node; 22278c2ecf20Sopenharmony_ci struct kmem_cache_node *n; 22288c2ecf20Sopenharmony_ci 22298c2ecf20Sopenharmony_ci for_each_kmem_cache_node(s, node, n) 22308c2ecf20Sopenharmony_ci if (!list_empty(&n->slabs_full) || 22318c2ecf20Sopenharmony_ci !list_empty(&n->slabs_partial)) 22328c2ecf20Sopenharmony_ci return false; 22338c2ecf20Sopenharmony_ci return true; 22348c2ecf20Sopenharmony_ci} 22358c2ecf20Sopenharmony_ci 22368c2ecf20Sopenharmony_ciint __kmem_cache_shrink(struct kmem_cache *cachep) 22378c2ecf20Sopenharmony_ci{ 22388c2ecf20Sopenharmony_ci int ret = 0; 22398c2ecf20Sopenharmony_ci int node; 22408c2ecf20Sopenharmony_ci struct kmem_cache_node *n; 22418c2ecf20Sopenharmony_ci 22428c2ecf20Sopenharmony_ci drain_cpu_caches(cachep); 22438c2ecf20Sopenharmony_ci 22448c2ecf20Sopenharmony_ci check_irq_on(); 22458c2ecf20Sopenharmony_ci for_each_kmem_cache_node(cachep, node, n) { 22468c2ecf20Sopenharmony_ci drain_freelist(cachep, n, INT_MAX); 22478c2ecf20Sopenharmony_ci 22488c2ecf20Sopenharmony_ci ret += !list_empty(&n->slabs_full) || 22498c2ecf20Sopenharmony_ci !list_empty(&n->slabs_partial); 22508c2ecf20Sopenharmony_ci } 22518c2ecf20Sopenharmony_ci return (ret ? 1 : 0); 22528c2ecf20Sopenharmony_ci} 22538c2ecf20Sopenharmony_ci 22548c2ecf20Sopenharmony_ciint __kmem_cache_shutdown(struct kmem_cache *cachep) 22558c2ecf20Sopenharmony_ci{ 22568c2ecf20Sopenharmony_ci return __kmem_cache_shrink(cachep); 22578c2ecf20Sopenharmony_ci} 22588c2ecf20Sopenharmony_ci 22598c2ecf20Sopenharmony_civoid __kmem_cache_release(struct kmem_cache *cachep) 22608c2ecf20Sopenharmony_ci{ 22618c2ecf20Sopenharmony_ci int i; 22628c2ecf20Sopenharmony_ci struct kmem_cache_node *n; 22638c2ecf20Sopenharmony_ci 22648c2ecf20Sopenharmony_ci cache_random_seq_destroy(cachep); 22658c2ecf20Sopenharmony_ci 22668c2ecf20Sopenharmony_ci free_percpu(cachep->cpu_cache); 22678c2ecf20Sopenharmony_ci 22688c2ecf20Sopenharmony_ci /* NUMA: free the node structures */ 22698c2ecf20Sopenharmony_ci for_each_kmem_cache_node(cachep, i, n) { 22708c2ecf20Sopenharmony_ci kfree(n->shared); 22718c2ecf20Sopenharmony_ci free_alien_cache(n->alien); 22728c2ecf20Sopenharmony_ci kfree(n); 22738c2ecf20Sopenharmony_ci cachep->node[i] = NULL; 22748c2ecf20Sopenharmony_ci } 22758c2ecf20Sopenharmony_ci} 22768c2ecf20Sopenharmony_ci 22778c2ecf20Sopenharmony_ci/* 22788c2ecf20Sopenharmony_ci * Get the memory for a slab management obj. 22798c2ecf20Sopenharmony_ci * 22808c2ecf20Sopenharmony_ci * For a slab cache when the slab descriptor is off-slab, the 22818c2ecf20Sopenharmony_ci * slab descriptor can't come from the same cache which is being created, 22828c2ecf20Sopenharmony_ci * Because if it is the case, that means we defer the creation of 22838c2ecf20Sopenharmony_ci * the kmalloc_{dma,}_cache of size sizeof(slab descriptor) to this point. 22848c2ecf20Sopenharmony_ci * And we eventually call down to __kmem_cache_create(), which 22858c2ecf20Sopenharmony_ci * in turn looks up in the kmalloc_{dma,}_caches for the disired-size one. 22868c2ecf20Sopenharmony_ci * This is a "chicken-and-egg" problem. 22878c2ecf20Sopenharmony_ci * 22888c2ecf20Sopenharmony_ci * So the off-slab slab descriptor shall come from the kmalloc_{dma,}_caches, 22898c2ecf20Sopenharmony_ci * which are all initialized during kmem_cache_init(). 22908c2ecf20Sopenharmony_ci */ 22918c2ecf20Sopenharmony_cistatic void *alloc_slabmgmt(struct kmem_cache *cachep, 22928c2ecf20Sopenharmony_ci struct page *page, int colour_off, 22938c2ecf20Sopenharmony_ci gfp_t local_flags, int nodeid) 22948c2ecf20Sopenharmony_ci{ 22958c2ecf20Sopenharmony_ci void *freelist; 22968c2ecf20Sopenharmony_ci void *addr = page_address(page); 22978c2ecf20Sopenharmony_ci 22988c2ecf20Sopenharmony_ci page->s_mem = addr + colour_off; 22998c2ecf20Sopenharmony_ci page->active = 0; 23008c2ecf20Sopenharmony_ci 23018c2ecf20Sopenharmony_ci if (OBJFREELIST_SLAB(cachep)) 23028c2ecf20Sopenharmony_ci freelist = NULL; 23038c2ecf20Sopenharmony_ci else if (OFF_SLAB(cachep)) { 23048c2ecf20Sopenharmony_ci /* Slab management obj is off-slab. */ 23058c2ecf20Sopenharmony_ci freelist = kmem_cache_alloc_node(cachep->freelist_cache, 23068c2ecf20Sopenharmony_ci local_flags, nodeid); 23078c2ecf20Sopenharmony_ci } else { 23088c2ecf20Sopenharmony_ci /* We will use last bytes at the slab for freelist */ 23098c2ecf20Sopenharmony_ci freelist = addr + (PAGE_SIZE << cachep->gfporder) - 23108c2ecf20Sopenharmony_ci cachep->freelist_size; 23118c2ecf20Sopenharmony_ci } 23128c2ecf20Sopenharmony_ci 23138c2ecf20Sopenharmony_ci return freelist; 23148c2ecf20Sopenharmony_ci} 23158c2ecf20Sopenharmony_ci 23168c2ecf20Sopenharmony_cistatic inline freelist_idx_t get_free_obj(struct page *page, unsigned int idx) 23178c2ecf20Sopenharmony_ci{ 23188c2ecf20Sopenharmony_ci return ((freelist_idx_t *)page->freelist)[idx]; 23198c2ecf20Sopenharmony_ci} 23208c2ecf20Sopenharmony_ci 23218c2ecf20Sopenharmony_cistatic inline void set_free_obj(struct page *page, 23228c2ecf20Sopenharmony_ci unsigned int idx, freelist_idx_t val) 23238c2ecf20Sopenharmony_ci{ 23248c2ecf20Sopenharmony_ci ((freelist_idx_t *)(page->freelist))[idx] = val; 23258c2ecf20Sopenharmony_ci} 23268c2ecf20Sopenharmony_ci 23278c2ecf20Sopenharmony_cistatic void cache_init_objs_debug(struct kmem_cache *cachep, struct page *page) 23288c2ecf20Sopenharmony_ci{ 23298c2ecf20Sopenharmony_ci#if DEBUG 23308c2ecf20Sopenharmony_ci int i; 23318c2ecf20Sopenharmony_ci 23328c2ecf20Sopenharmony_ci for (i = 0; i < cachep->num; i++) { 23338c2ecf20Sopenharmony_ci void *objp = index_to_obj(cachep, page, i); 23348c2ecf20Sopenharmony_ci 23358c2ecf20Sopenharmony_ci if (cachep->flags & SLAB_STORE_USER) 23368c2ecf20Sopenharmony_ci *dbg_userword(cachep, objp) = NULL; 23378c2ecf20Sopenharmony_ci 23388c2ecf20Sopenharmony_ci if (cachep->flags & SLAB_RED_ZONE) { 23398c2ecf20Sopenharmony_ci *dbg_redzone1(cachep, objp) = RED_INACTIVE; 23408c2ecf20Sopenharmony_ci *dbg_redzone2(cachep, objp) = RED_INACTIVE; 23418c2ecf20Sopenharmony_ci } 23428c2ecf20Sopenharmony_ci /* 23438c2ecf20Sopenharmony_ci * Constructors are not allowed to allocate memory from the same 23448c2ecf20Sopenharmony_ci * cache which they are a constructor for. Otherwise, deadlock. 23458c2ecf20Sopenharmony_ci * They must also be threaded. 23468c2ecf20Sopenharmony_ci */ 23478c2ecf20Sopenharmony_ci if (cachep->ctor && !(cachep->flags & SLAB_POISON)) { 23488c2ecf20Sopenharmony_ci kasan_unpoison_object_data(cachep, 23498c2ecf20Sopenharmony_ci objp + obj_offset(cachep)); 23508c2ecf20Sopenharmony_ci cachep->ctor(objp + obj_offset(cachep)); 23518c2ecf20Sopenharmony_ci kasan_poison_object_data( 23528c2ecf20Sopenharmony_ci cachep, objp + obj_offset(cachep)); 23538c2ecf20Sopenharmony_ci } 23548c2ecf20Sopenharmony_ci 23558c2ecf20Sopenharmony_ci if (cachep->flags & SLAB_RED_ZONE) { 23568c2ecf20Sopenharmony_ci if (*dbg_redzone2(cachep, objp) != RED_INACTIVE) 23578c2ecf20Sopenharmony_ci slab_error(cachep, "constructor overwrote the end of an object"); 23588c2ecf20Sopenharmony_ci if (*dbg_redzone1(cachep, objp) != RED_INACTIVE) 23598c2ecf20Sopenharmony_ci slab_error(cachep, "constructor overwrote the start of an object"); 23608c2ecf20Sopenharmony_ci } 23618c2ecf20Sopenharmony_ci /* need to poison the objs? */ 23628c2ecf20Sopenharmony_ci if (cachep->flags & SLAB_POISON) { 23638c2ecf20Sopenharmony_ci poison_obj(cachep, objp, POISON_FREE); 23648c2ecf20Sopenharmony_ci slab_kernel_map(cachep, objp, 0); 23658c2ecf20Sopenharmony_ci } 23668c2ecf20Sopenharmony_ci } 23678c2ecf20Sopenharmony_ci#endif 23688c2ecf20Sopenharmony_ci} 23698c2ecf20Sopenharmony_ci 23708c2ecf20Sopenharmony_ci#ifdef CONFIG_SLAB_FREELIST_RANDOM 23718c2ecf20Sopenharmony_ci/* Hold information during a freelist initialization */ 23728c2ecf20Sopenharmony_ciunion freelist_init_state { 23738c2ecf20Sopenharmony_ci struct { 23748c2ecf20Sopenharmony_ci unsigned int pos; 23758c2ecf20Sopenharmony_ci unsigned int *list; 23768c2ecf20Sopenharmony_ci unsigned int count; 23778c2ecf20Sopenharmony_ci }; 23788c2ecf20Sopenharmony_ci struct rnd_state rnd_state; 23798c2ecf20Sopenharmony_ci}; 23808c2ecf20Sopenharmony_ci 23818c2ecf20Sopenharmony_ci/* 23828c2ecf20Sopenharmony_ci * Initialize the state based on the randomization methode available. 23838c2ecf20Sopenharmony_ci * return true if the pre-computed list is available, false otherwize. 23848c2ecf20Sopenharmony_ci */ 23858c2ecf20Sopenharmony_cistatic bool freelist_state_initialize(union freelist_init_state *state, 23868c2ecf20Sopenharmony_ci struct kmem_cache *cachep, 23878c2ecf20Sopenharmony_ci unsigned int count) 23888c2ecf20Sopenharmony_ci{ 23898c2ecf20Sopenharmony_ci bool ret; 23908c2ecf20Sopenharmony_ci unsigned int rand; 23918c2ecf20Sopenharmony_ci 23928c2ecf20Sopenharmony_ci /* Use best entropy available to define a random shift */ 23938c2ecf20Sopenharmony_ci rand = get_random_int(); 23948c2ecf20Sopenharmony_ci 23958c2ecf20Sopenharmony_ci /* Use a random state if the pre-computed list is not available */ 23968c2ecf20Sopenharmony_ci if (!cachep->random_seq) { 23978c2ecf20Sopenharmony_ci prandom_seed_state(&state->rnd_state, rand); 23988c2ecf20Sopenharmony_ci ret = false; 23998c2ecf20Sopenharmony_ci } else { 24008c2ecf20Sopenharmony_ci state->list = cachep->random_seq; 24018c2ecf20Sopenharmony_ci state->count = count; 24028c2ecf20Sopenharmony_ci state->pos = rand % count; 24038c2ecf20Sopenharmony_ci ret = true; 24048c2ecf20Sopenharmony_ci } 24058c2ecf20Sopenharmony_ci return ret; 24068c2ecf20Sopenharmony_ci} 24078c2ecf20Sopenharmony_ci 24088c2ecf20Sopenharmony_ci/* Get the next entry on the list and randomize it using a random shift */ 24098c2ecf20Sopenharmony_cistatic freelist_idx_t next_random_slot(union freelist_init_state *state) 24108c2ecf20Sopenharmony_ci{ 24118c2ecf20Sopenharmony_ci if (state->pos >= state->count) 24128c2ecf20Sopenharmony_ci state->pos = 0; 24138c2ecf20Sopenharmony_ci return state->list[state->pos++]; 24148c2ecf20Sopenharmony_ci} 24158c2ecf20Sopenharmony_ci 24168c2ecf20Sopenharmony_ci/* Swap two freelist entries */ 24178c2ecf20Sopenharmony_cistatic void swap_free_obj(struct page *page, unsigned int a, unsigned int b) 24188c2ecf20Sopenharmony_ci{ 24198c2ecf20Sopenharmony_ci swap(((freelist_idx_t *)page->freelist)[a], 24208c2ecf20Sopenharmony_ci ((freelist_idx_t *)page->freelist)[b]); 24218c2ecf20Sopenharmony_ci} 24228c2ecf20Sopenharmony_ci 24238c2ecf20Sopenharmony_ci/* 24248c2ecf20Sopenharmony_ci * Shuffle the freelist initialization state based on pre-computed lists. 24258c2ecf20Sopenharmony_ci * return true if the list was successfully shuffled, false otherwise. 24268c2ecf20Sopenharmony_ci */ 24278c2ecf20Sopenharmony_cistatic bool shuffle_freelist(struct kmem_cache *cachep, struct page *page) 24288c2ecf20Sopenharmony_ci{ 24298c2ecf20Sopenharmony_ci unsigned int objfreelist = 0, i, rand, count = cachep->num; 24308c2ecf20Sopenharmony_ci union freelist_init_state state; 24318c2ecf20Sopenharmony_ci bool precomputed; 24328c2ecf20Sopenharmony_ci 24338c2ecf20Sopenharmony_ci if (count < 2) 24348c2ecf20Sopenharmony_ci return false; 24358c2ecf20Sopenharmony_ci 24368c2ecf20Sopenharmony_ci precomputed = freelist_state_initialize(&state, cachep, count); 24378c2ecf20Sopenharmony_ci 24388c2ecf20Sopenharmony_ci /* Take a random entry as the objfreelist */ 24398c2ecf20Sopenharmony_ci if (OBJFREELIST_SLAB(cachep)) { 24408c2ecf20Sopenharmony_ci if (!precomputed) 24418c2ecf20Sopenharmony_ci objfreelist = count - 1; 24428c2ecf20Sopenharmony_ci else 24438c2ecf20Sopenharmony_ci objfreelist = next_random_slot(&state); 24448c2ecf20Sopenharmony_ci page->freelist = index_to_obj(cachep, page, objfreelist) + 24458c2ecf20Sopenharmony_ci obj_offset(cachep); 24468c2ecf20Sopenharmony_ci count--; 24478c2ecf20Sopenharmony_ci } 24488c2ecf20Sopenharmony_ci 24498c2ecf20Sopenharmony_ci /* 24508c2ecf20Sopenharmony_ci * On early boot, generate the list dynamically. 24518c2ecf20Sopenharmony_ci * Later use a pre-computed list for speed. 24528c2ecf20Sopenharmony_ci */ 24538c2ecf20Sopenharmony_ci if (!precomputed) { 24548c2ecf20Sopenharmony_ci for (i = 0; i < count; i++) 24558c2ecf20Sopenharmony_ci set_free_obj(page, i, i); 24568c2ecf20Sopenharmony_ci 24578c2ecf20Sopenharmony_ci /* Fisher-Yates shuffle */ 24588c2ecf20Sopenharmony_ci for (i = count - 1; i > 0; i--) { 24598c2ecf20Sopenharmony_ci rand = prandom_u32_state(&state.rnd_state); 24608c2ecf20Sopenharmony_ci rand %= (i + 1); 24618c2ecf20Sopenharmony_ci swap_free_obj(page, i, rand); 24628c2ecf20Sopenharmony_ci } 24638c2ecf20Sopenharmony_ci } else { 24648c2ecf20Sopenharmony_ci for (i = 0; i < count; i++) 24658c2ecf20Sopenharmony_ci set_free_obj(page, i, next_random_slot(&state)); 24668c2ecf20Sopenharmony_ci } 24678c2ecf20Sopenharmony_ci 24688c2ecf20Sopenharmony_ci if (OBJFREELIST_SLAB(cachep)) 24698c2ecf20Sopenharmony_ci set_free_obj(page, cachep->num - 1, objfreelist); 24708c2ecf20Sopenharmony_ci 24718c2ecf20Sopenharmony_ci return true; 24728c2ecf20Sopenharmony_ci} 24738c2ecf20Sopenharmony_ci#else 24748c2ecf20Sopenharmony_cistatic inline bool shuffle_freelist(struct kmem_cache *cachep, 24758c2ecf20Sopenharmony_ci struct page *page) 24768c2ecf20Sopenharmony_ci{ 24778c2ecf20Sopenharmony_ci return false; 24788c2ecf20Sopenharmony_ci} 24798c2ecf20Sopenharmony_ci#endif /* CONFIG_SLAB_FREELIST_RANDOM */ 24808c2ecf20Sopenharmony_ci 24818c2ecf20Sopenharmony_cistatic void cache_init_objs(struct kmem_cache *cachep, 24828c2ecf20Sopenharmony_ci struct page *page) 24838c2ecf20Sopenharmony_ci{ 24848c2ecf20Sopenharmony_ci int i; 24858c2ecf20Sopenharmony_ci void *objp; 24868c2ecf20Sopenharmony_ci bool shuffled; 24878c2ecf20Sopenharmony_ci 24888c2ecf20Sopenharmony_ci cache_init_objs_debug(cachep, page); 24898c2ecf20Sopenharmony_ci 24908c2ecf20Sopenharmony_ci /* Try to randomize the freelist if enabled */ 24918c2ecf20Sopenharmony_ci shuffled = shuffle_freelist(cachep, page); 24928c2ecf20Sopenharmony_ci 24938c2ecf20Sopenharmony_ci if (!shuffled && OBJFREELIST_SLAB(cachep)) { 24948c2ecf20Sopenharmony_ci page->freelist = index_to_obj(cachep, page, cachep->num - 1) + 24958c2ecf20Sopenharmony_ci obj_offset(cachep); 24968c2ecf20Sopenharmony_ci } 24978c2ecf20Sopenharmony_ci 24988c2ecf20Sopenharmony_ci for (i = 0; i < cachep->num; i++) { 24998c2ecf20Sopenharmony_ci objp = index_to_obj(cachep, page, i); 25008c2ecf20Sopenharmony_ci objp = kasan_init_slab_obj(cachep, objp); 25018c2ecf20Sopenharmony_ci 25028c2ecf20Sopenharmony_ci /* constructor could break poison info */ 25038c2ecf20Sopenharmony_ci if (DEBUG == 0 && cachep->ctor) { 25048c2ecf20Sopenharmony_ci kasan_unpoison_object_data(cachep, objp); 25058c2ecf20Sopenharmony_ci cachep->ctor(objp); 25068c2ecf20Sopenharmony_ci kasan_poison_object_data(cachep, objp); 25078c2ecf20Sopenharmony_ci } 25088c2ecf20Sopenharmony_ci 25098c2ecf20Sopenharmony_ci if (!shuffled) 25108c2ecf20Sopenharmony_ci set_free_obj(page, i, i); 25118c2ecf20Sopenharmony_ci } 25128c2ecf20Sopenharmony_ci} 25138c2ecf20Sopenharmony_ci 25148c2ecf20Sopenharmony_cistatic void *slab_get_obj(struct kmem_cache *cachep, struct page *page) 25158c2ecf20Sopenharmony_ci{ 25168c2ecf20Sopenharmony_ci void *objp; 25178c2ecf20Sopenharmony_ci 25188c2ecf20Sopenharmony_ci objp = index_to_obj(cachep, page, get_free_obj(page, page->active)); 25198c2ecf20Sopenharmony_ci page->active++; 25208c2ecf20Sopenharmony_ci 25218c2ecf20Sopenharmony_ci return objp; 25228c2ecf20Sopenharmony_ci} 25238c2ecf20Sopenharmony_ci 25248c2ecf20Sopenharmony_cistatic void slab_put_obj(struct kmem_cache *cachep, 25258c2ecf20Sopenharmony_ci struct page *page, void *objp) 25268c2ecf20Sopenharmony_ci{ 25278c2ecf20Sopenharmony_ci unsigned int objnr = obj_to_index(cachep, page, objp); 25288c2ecf20Sopenharmony_ci#if DEBUG 25298c2ecf20Sopenharmony_ci unsigned int i; 25308c2ecf20Sopenharmony_ci 25318c2ecf20Sopenharmony_ci /* Verify double free bug */ 25328c2ecf20Sopenharmony_ci for (i = page->active; i < cachep->num; i++) { 25338c2ecf20Sopenharmony_ci if (get_free_obj(page, i) == objnr) { 25348c2ecf20Sopenharmony_ci pr_err("slab: double free detected in cache '%s', objp %px\n", 25358c2ecf20Sopenharmony_ci cachep->name, objp); 25368c2ecf20Sopenharmony_ci BUG(); 25378c2ecf20Sopenharmony_ci } 25388c2ecf20Sopenharmony_ci } 25398c2ecf20Sopenharmony_ci#endif 25408c2ecf20Sopenharmony_ci page->active--; 25418c2ecf20Sopenharmony_ci if (!page->freelist) 25428c2ecf20Sopenharmony_ci page->freelist = objp + obj_offset(cachep); 25438c2ecf20Sopenharmony_ci 25448c2ecf20Sopenharmony_ci set_free_obj(page, page->active, objnr); 25458c2ecf20Sopenharmony_ci} 25468c2ecf20Sopenharmony_ci 25478c2ecf20Sopenharmony_ci/* 25488c2ecf20Sopenharmony_ci * Map pages beginning at addr to the given cache and slab. This is required 25498c2ecf20Sopenharmony_ci * for the slab allocator to be able to lookup the cache and slab of a 25508c2ecf20Sopenharmony_ci * virtual address for kfree, ksize, and slab debugging. 25518c2ecf20Sopenharmony_ci */ 25528c2ecf20Sopenharmony_cistatic void slab_map_pages(struct kmem_cache *cache, struct page *page, 25538c2ecf20Sopenharmony_ci void *freelist) 25548c2ecf20Sopenharmony_ci{ 25558c2ecf20Sopenharmony_ci page->slab_cache = cache; 25568c2ecf20Sopenharmony_ci page->freelist = freelist; 25578c2ecf20Sopenharmony_ci} 25588c2ecf20Sopenharmony_ci 25598c2ecf20Sopenharmony_ci/* 25608c2ecf20Sopenharmony_ci * Grow (by 1) the number of slabs within a cache. This is called by 25618c2ecf20Sopenharmony_ci * kmem_cache_alloc() when there are no active objs left in a cache. 25628c2ecf20Sopenharmony_ci */ 25638c2ecf20Sopenharmony_cistatic struct page *cache_grow_begin(struct kmem_cache *cachep, 25648c2ecf20Sopenharmony_ci gfp_t flags, int nodeid) 25658c2ecf20Sopenharmony_ci{ 25668c2ecf20Sopenharmony_ci void *freelist; 25678c2ecf20Sopenharmony_ci size_t offset; 25688c2ecf20Sopenharmony_ci gfp_t local_flags; 25698c2ecf20Sopenharmony_ci int page_node; 25708c2ecf20Sopenharmony_ci struct kmem_cache_node *n; 25718c2ecf20Sopenharmony_ci struct page *page; 25728c2ecf20Sopenharmony_ci 25738c2ecf20Sopenharmony_ci /* 25748c2ecf20Sopenharmony_ci * Be lazy and only check for valid flags here, keeping it out of the 25758c2ecf20Sopenharmony_ci * critical path in kmem_cache_alloc(). 25768c2ecf20Sopenharmony_ci */ 25778c2ecf20Sopenharmony_ci if (unlikely(flags & GFP_SLAB_BUG_MASK)) 25788c2ecf20Sopenharmony_ci flags = kmalloc_fix_flags(flags); 25798c2ecf20Sopenharmony_ci 25808c2ecf20Sopenharmony_ci WARN_ON_ONCE(cachep->ctor && (flags & __GFP_ZERO)); 25818c2ecf20Sopenharmony_ci local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK); 25828c2ecf20Sopenharmony_ci 25838c2ecf20Sopenharmony_ci check_irq_off(); 25848c2ecf20Sopenharmony_ci if (gfpflags_allow_blocking(local_flags)) 25858c2ecf20Sopenharmony_ci local_irq_enable(); 25868c2ecf20Sopenharmony_ci 25878c2ecf20Sopenharmony_ci /* 25888c2ecf20Sopenharmony_ci * Get mem for the objs. Attempt to allocate a physical page from 25898c2ecf20Sopenharmony_ci * 'nodeid'. 25908c2ecf20Sopenharmony_ci */ 25918c2ecf20Sopenharmony_ci page = kmem_getpages(cachep, local_flags, nodeid); 25928c2ecf20Sopenharmony_ci if (!page) 25938c2ecf20Sopenharmony_ci goto failed; 25948c2ecf20Sopenharmony_ci 25958c2ecf20Sopenharmony_ci page_node = page_to_nid(page); 25968c2ecf20Sopenharmony_ci n = get_node(cachep, page_node); 25978c2ecf20Sopenharmony_ci 25988c2ecf20Sopenharmony_ci /* Get colour for the slab, and cal the next value. */ 25998c2ecf20Sopenharmony_ci n->colour_next++; 26008c2ecf20Sopenharmony_ci if (n->colour_next >= cachep->colour) 26018c2ecf20Sopenharmony_ci n->colour_next = 0; 26028c2ecf20Sopenharmony_ci 26038c2ecf20Sopenharmony_ci offset = n->colour_next; 26048c2ecf20Sopenharmony_ci if (offset >= cachep->colour) 26058c2ecf20Sopenharmony_ci offset = 0; 26068c2ecf20Sopenharmony_ci 26078c2ecf20Sopenharmony_ci offset *= cachep->colour_off; 26088c2ecf20Sopenharmony_ci 26098c2ecf20Sopenharmony_ci /* 26108c2ecf20Sopenharmony_ci * Call kasan_poison_slab() before calling alloc_slabmgmt(), so 26118c2ecf20Sopenharmony_ci * page_address() in the latter returns a non-tagged pointer, 26128c2ecf20Sopenharmony_ci * as it should be for slab pages. 26138c2ecf20Sopenharmony_ci */ 26148c2ecf20Sopenharmony_ci kasan_poison_slab(page); 26158c2ecf20Sopenharmony_ci 26168c2ecf20Sopenharmony_ci /* Get slab management. */ 26178c2ecf20Sopenharmony_ci freelist = alloc_slabmgmt(cachep, page, offset, 26188c2ecf20Sopenharmony_ci local_flags & ~GFP_CONSTRAINT_MASK, page_node); 26198c2ecf20Sopenharmony_ci if (OFF_SLAB(cachep) && !freelist) 26208c2ecf20Sopenharmony_ci goto opps1; 26218c2ecf20Sopenharmony_ci 26228c2ecf20Sopenharmony_ci slab_map_pages(cachep, page, freelist); 26238c2ecf20Sopenharmony_ci 26248c2ecf20Sopenharmony_ci cache_init_objs(cachep, page); 26258c2ecf20Sopenharmony_ci 26268c2ecf20Sopenharmony_ci if (gfpflags_allow_blocking(local_flags)) 26278c2ecf20Sopenharmony_ci local_irq_disable(); 26288c2ecf20Sopenharmony_ci 26298c2ecf20Sopenharmony_ci return page; 26308c2ecf20Sopenharmony_ci 26318c2ecf20Sopenharmony_ciopps1: 26328c2ecf20Sopenharmony_ci kmem_freepages(cachep, page); 26338c2ecf20Sopenharmony_cifailed: 26348c2ecf20Sopenharmony_ci if (gfpflags_allow_blocking(local_flags)) 26358c2ecf20Sopenharmony_ci local_irq_disable(); 26368c2ecf20Sopenharmony_ci return NULL; 26378c2ecf20Sopenharmony_ci} 26388c2ecf20Sopenharmony_ci 26398c2ecf20Sopenharmony_cistatic void cache_grow_end(struct kmem_cache *cachep, struct page *page) 26408c2ecf20Sopenharmony_ci{ 26418c2ecf20Sopenharmony_ci struct kmem_cache_node *n; 26428c2ecf20Sopenharmony_ci void *list = NULL; 26438c2ecf20Sopenharmony_ci 26448c2ecf20Sopenharmony_ci check_irq_off(); 26458c2ecf20Sopenharmony_ci 26468c2ecf20Sopenharmony_ci if (!page) 26478c2ecf20Sopenharmony_ci return; 26488c2ecf20Sopenharmony_ci 26498c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&page->slab_list); 26508c2ecf20Sopenharmony_ci n = get_node(cachep, page_to_nid(page)); 26518c2ecf20Sopenharmony_ci 26528c2ecf20Sopenharmony_ci spin_lock(&n->list_lock); 26538c2ecf20Sopenharmony_ci n->total_slabs++; 26548c2ecf20Sopenharmony_ci if (!page->active) { 26558c2ecf20Sopenharmony_ci list_add_tail(&page->slab_list, &n->slabs_free); 26568c2ecf20Sopenharmony_ci n->free_slabs++; 26578c2ecf20Sopenharmony_ci } else 26588c2ecf20Sopenharmony_ci fixup_slab_list(cachep, n, page, &list); 26598c2ecf20Sopenharmony_ci 26608c2ecf20Sopenharmony_ci STATS_INC_GROWN(cachep); 26618c2ecf20Sopenharmony_ci n->free_objects += cachep->num - page->active; 26628c2ecf20Sopenharmony_ci spin_unlock(&n->list_lock); 26638c2ecf20Sopenharmony_ci 26648c2ecf20Sopenharmony_ci fixup_objfreelist_debug(cachep, &list); 26658c2ecf20Sopenharmony_ci} 26668c2ecf20Sopenharmony_ci 26678c2ecf20Sopenharmony_ci#if DEBUG 26688c2ecf20Sopenharmony_ci 26698c2ecf20Sopenharmony_ci/* 26708c2ecf20Sopenharmony_ci * Perform extra freeing checks: 26718c2ecf20Sopenharmony_ci * - detect bad pointers. 26728c2ecf20Sopenharmony_ci * - POISON/RED_ZONE checking 26738c2ecf20Sopenharmony_ci */ 26748c2ecf20Sopenharmony_cistatic void kfree_debugcheck(const void *objp) 26758c2ecf20Sopenharmony_ci{ 26768c2ecf20Sopenharmony_ci if (!virt_addr_valid(objp)) { 26778c2ecf20Sopenharmony_ci pr_err("kfree_debugcheck: out of range ptr %lxh\n", 26788c2ecf20Sopenharmony_ci (unsigned long)objp); 26798c2ecf20Sopenharmony_ci BUG(); 26808c2ecf20Sopenharmony_ci } 26818c2ecf20Sopenharmony_ci} 26828c2ecf20Sopenharmony_ci 26838c2ecf20Sopenharmony_cistatic inline void verify_redzone_free(struct kmem_cache *cache, void *obj) 26848c2ecf20Sopenharmony_ci{ 26858c2ecf20Sopenharmony_ci unsigned long long redzone1, redzone2; 26868c2ecf20Sopenharmony_ci 26878c2ecf20Sopenharmony_ci redzone1 = *dbg_redzone1(cache, obj); 26888c2ecf20Sopenharmony_ci redzone2 = *dbg_redzone2(cache, obj); 26898c2ecf20Sopenharmony_ci 26908c2ecf20Sopenharmony_ci /* 26918c2ecf20Sopenharmony_ci * Redzone is ok. 26928c2ecf20Sopenharmony_ci */ 26938c2ecf20Sopenharmony_ci if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE) 26948c2ecf20Sopenharmony_ci return; 26958c2ecf20Sopenharmony_ci 26968c2ecf20Sopenharmony_ci if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE) 26978c2ecf20Sopenharmony_ci slab_error(cache, "double free detected"); 26988c2ecf20Sopenharmony_ci else 26998c2ecf20Sopenharmony_ci slab_error(cache, "memory outside object was overwritten"); 27008c2ecf20Sopenharmony_ci 27018c2ecf20Sopenharmony_ci pr_err("%px: redzone 1:0x%llx, redzone 2:0x%llx\n", 27028c2ecf20Sopenharmony_ci obj, redzone1, redzone2); 27038c2ecf20Sopenharmony_ci} 27048c2ecf20Sopenharmony_ci 27058c2ecf20Sopenharmony_cistatic void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp, 27068c2ecf20Sopenharmony_ci unsigned long caller) 27078c2ecf20Sopenharmony_ci{ 27088c2ecf20Sopenharmony_ci unsigned int objnr; 27098c2ecf20Sopenharmony_ci struct page *page; 27108c2ecf20Sopenharmony_ci 27118c2ecf20Sopenharmony_ci BUG_ON(virt_to_cache(objp) != cachep); 27128c2ecf20Sopenharmony_ci 27138c2ecf20Sopenharmony_ci objp -= obj_offset(cachep); 27148c2ecf20Sopenharmony_ci kfree_debugcheck(objp); 27158c2ecf20Sopenharmony_ci page = virt_to_head_page(objp); 27168c2ecf20Sopenharmony_ci 27178c2ecf20Sopenharmony_ci if (cachep->flags & SLAB_RED_ZONE) { 27188c2ecf20Sopenharmony_ci verify_redzone_free(cachep, objp); 27198c2ecf20Sopenharmony_ci *dbg_redzone1(cachep, objp) = RED_INACTIVE; 27208c2ecf20Sopenharmony_ci *dbg_redzone2(cachep, objp) = RED_INACTIVE; 27218c2ecf20Sopenharmony_ci } 27228c2ecf20Sopenharmony_ci if (cachep->flags & SLAB_STORE_USER) 27238c2ecf20Sopenharmony_ci *dbg_userword(cachep, objp) = (void *)caller; 27248c2ecf20Sopenharmony_ci 27258c2ecf20Sopenharmony_ci objnr = obj_to_index(cachep, page, objp); 27268c2ecf20Sopenharmony_ci 27278c2ecf20Sopenharmony_ci BUG_ON(objnr >= cachep->num); 27288c2ecf20Sopenharmony_ci BUG_ON(objp != index_to_obj(cachep, page, objnr)); 27298c2ecf20Sopenharmony_ci 27308c2ecf20Sopenharmony_ci if (cachep->flags & SLAB_POISON) { 27318c2ecf20Sopenharmony_ci poison_obj(cachep, objp, POISON_FREE); 27328c2ecf20Sopenharmony_ci slab_kernel_map(cachep, objp, 0); 27338c2ecf20Sopenharmony_ci } 27348c2ecf20Sopenharmony_ci return objp; 27358c2ecf20Sopenharmony_ci} 27368c2ecf20Sopenharmony_ci 27378c2ecf20Sopenharmony_ci#else 27388c2ecf20Sopenharmony_ci#define kfree_debugcheck(x) do { } while(0) 27398c2ecf20Sopenharmony_ci#define cache_free_debugcheck(x,objp,z) (objp) 27408c2ecf20Sopenharmony_ci#endif 27418c2ecf20Sopenharmony_ci 27428c2ecf20Sopenharmony_cistatic inline void fixup_objfreelist_debug(struct kmem_cache *cachep, 27438c2ecf20Sopenharmony_ci void **list) 27448c2ecf20Sopenharmony_ci{ 27458c2ecf20Sopenharmony_ci#if DEBUG 27468c2ecf20Sopenharmony_ci void *next = *list; 27478c2ecf20Sopenharmony_ci void *objp; 27488c2ecf20Sopenharmony_ci 27498c2ecf20Sopenharmony_ci while (next) { 27508c2ecf20Sopenharmony_ci objp = next - obj_offset(cachep); 27518c2ecf20Sopenharmony_ci next = *(void **)next; 27528c2ecf20Sopenharmony_ci poison_obj(cachep, objp, POISON_FREE); 27538c2ecf20Sopenharmony_ci } 27548c2ecf20Sopenharmony_ci#endif 27558c2ecf20Sopenharmony_ci} 27568c2ecf20Sopenharmony_ci 27578c2ecf20Sopenharmony_cistatic inline void fixup_slab_list(struct kmem_cache *cachep, 27588c2ecf20Sopenharmony_ci struct kmem_cache_node *n, struct page *page, 27598c2ecf20Sopenharmony_ci void **list) 27608c2ecf20Sopenharmony_ci{ 27618c2ecf20Sopenharmony_ci /* move slabp to correct slabp list: */ 27628c2ecf20Sopenharmony_ci list_del(&page->slab_list); 27638c2ecf20Sopenharmony_ci if (page->active == cachep->num) { 27648c2ecf20Sopenharmony_ci list_add(&page->slab_list, &n->slabs_full); 27658c2ecf20Sopenharmony_ci if (OBJFREELIST_SLAB(cachep)) { 27668c2ecf20Sopenharmony_ci#if DEBUG 27678c2ecf20Sopenharmony_ci /* Poisoning will be done without holding the lock */ 27688c2ecf20Sopenharmony_ci if (cachep->flags & SLAB_POISON) { 27698c2ecf20Sopenharmony_ci void **objp = page->freelist; 27708c2ecf20Sopenharmony_ci 27718c2ecf20Sopenharmony_ci *objp = *list; 27728c2ecf20Sopenharmony_ci *list = objp; 27738c2ecf20Sopenharmony_ci } 27748c2ecf20Sopenharmony_ci#endif 27758c2ecf20Sopenharmony_ci page->freelist = NULL; 27768c2ecf20Sopenharmony_ci } 27778c2ecf20Sopenharmony_ci } else 27788c2ecf20Sopenharmony_ci list_add(&page->slab_list, &n->slabs_partial); 27798c2ecf20Sopenharmony_ci} 27808c2ecf20Sopenharmony_ci 27818c2ecf20Sopenharmony_ci/* Try to find non-pfmemalloc slab if needed */ 27828c2ecf20Sopenharmony_cistatic noinline struct page *get_valid_first_slab(struct kmem_cache_node *n, 27838c2ecf20Sopenharmony_ci struct page *page, bool pfmemalloc) 27848c2ecf20Sopenharmony_ci{ 27858c2ecf20Sopenharmony_ci if (!page) 27868c2ecf20Sopenharmony_ci return NULL; 27878c2ecf20Sopenharmony_ci 27888c2ecf20Sopenharmony_ci if (pfmemalloc) 27898c2ecf20Sopenharmony_ci return page; 27908c2ecf20Sopenharmony_ci 27918c2ecf20Sopenharmony_ci if (!PageSlabPfmemalloc(page)) 27928c2ecf20Sopenharmony_ci return page; 27938c2ecf20Sopenharmony_ci 27948c2ecf20Sopenharmony_ci /* No need to keep pfmemalloc slab if we have enough free objects */ 27958c2ecf20Sopenharmony_ci if (n->free_objects > n->free_limit) { 27968c2ecf20Sopenharmony_ci ClearPageSlabPfmemalloc(page); 27978c2ecf20Sopenharmony_ci return page; 27988c2ecf20Sopenharmony_ci } 27998c2ecf20Sopenharmony_ci 28008c2ecf20Sopenharmony_ci /* Move pfmemalloc slab to the end of list to speed up next search */ 28018c2ecf20Sopenharmony_ci list_del(&page->slab_list); 28028c2ecf20Sopenharmony_ci if (!page->active) { 28038c2ecf20Sopenharmony_ci list_add_tail(&page->slab_list, &n->slabs_free); 28048c2ecf20Sopenharmony_ci n->free_slabs++; 28058c2ecf20Sopenharmony_ci } else 28068c2ecf20Sopenharmony_ci list_add_tail(&page->slab_list, &n->slabs_partial); 28078c2ecf20Sopenharmony_ci 28088c2ecf20Sopenharmony_ci list_for_each_entry(page, &n->slabs_partial, slab_list) { 28098c2ecf20Sopenharmony_ci if (!PageSlabPfmemalloc(page)) 28108c2ecf20Sopenharmony_ci return page; 28118c2ecf20Sopenharmony_ci } 28128c2ecf20Sopenharmony_ci 28138c2ecf20Sopenharmony_ci n->free_touched = 1; 28148c2ecf20Sopenharmony_ci list_for_each_entry(page, &n->slabs_free, slab_list) { 28158c2ecf20Sopenharmony_ci if (!PageSlabPfmemalloc(page)) { 28168c2ecf20Sopenharmony_ci n->free_slabs--; 28178c2ecf20Sopenharmony_ci return page; 28188c2ecf20Sopenharmony_ci } 28198c2ecf20Sopenharmony_ci } 28208c2ecf20Sopenharmony_ci 28218c2ecf20Sopenharmony_ci return NULL; 28228c2ecf20Sopenharmony_ci} 28238c2ecf20Sopenharmony_ci 28248c2ecf20Sopenharmony_cistatic struct page *get_first_slab(struct kmem_cache_node *n, bool pfmemalloc) 28258c2ecf20Sopenharmony_ci{ 28268c2ecf20Sopenharmony_ci struct page *page; 28278c2ecf20Sopenharmony_ci 28288c2ecf20Sopenharmony_ci assert_spin_locked(&n->list_lock); 28298c2ecf20Sopenharmony_ci page = list_first_entry_or_null(&n->slabs_partial, struct page, 28308c2ecf20Sopenharmony_ci slab_list); 28318c2ecf20Sopenharmony_ci if (!page) { 28328c2ecf20Sopenharmony_ci n->free_touched = 1; 28338c2ecf20Sopenharmony_ci page = list_first_entry_or_null(&n->slabs_free, struct page, 28348c2ecf20Sopenharmony_ci slab_list); 28358c2ecf20Sopenharmony_ci if (page) 28368c2ecf20Sopenharmony_ci n->free_slabs--; 28378c2ecf20Sopenharmony_ci } 28388c2ecf20Sopenharmony_ci 28398c2ecf20Sopenharmony_ci if (sk_memalloc_socks()) 28408c2ecf20Sopenharmony_ci page = get_valid_first_slab(n, page, pfmemalloc); 28418c2ecf20Sopenharmony_ci 28428c2ecf20Sopenharmony_ci return page; 28438c2ecf20Sopenharmony_ci} 28448c2ecf20Sopenharmony_ci 28458c2ecf20Sopenharmony_cistatic noinline void *cache_alloc_pfmemalloc(struct kmem_cache *cachep, 28468c2ecf20Sopenharmony_ci struct kmem_cache_node *n, gfp_t flags) 28478c2ecf20Sopenharmony_ci{ 28488c2ecf20Sopenharmony_ci struct page *page; 28498c2ecf20Sopenharmony_ci void *obj; 28508c2ecf20Sopenharmony_ci void *list = NULL; 28518c2ecf20Sopenharmony_ci 28528c2ecf20Sopenharmony_ci if (!gfp_pfmemalloc_allowed(flags)) 28538c2ecf20Sopenharmony_ci return NULL; 28548c2ecf20Sopenharmony_ci 28558c2ecf20Sopenharmony_ci spin_lock(&n->list_lock); 28568c2ecf20Sopenharmony_ci page = get_first_slab(n, true); 28578c2ecf20Sopenharmony_ci if (!page) { 28588c2ecf20Sopenharmony_ci spin_unlock(&n->list_lock); 28598c2ecf20Sopenharmony_ci return NULL; 28608c2ecf20Sopenharmony_ci } 28618c2ecf20Sopenharmony_ci 28628c2ecf20Sopenharmony_ci obj = slab_get_obj(cachep, page); 28638c2ecf20Sopenharmony_ci n->free_objects--; 28648c2ecf20Sopenharmony_ci 28658c2ecf20Sopenharmony_ci fixup_slab_list(cachep, n, page, &list); 28668c2ecf20Sopenharmony_ci 28678c2ecf20Sopenharmony_ci spin_unlock(&n->list_lock); 28688c2ecf20Sopenharmony_ci fixup_objfreelist_debug(cachep, &list); 28698c2ecf20Sopenharmony_ci 28708c2ecf20Sopenharmony_ci return obj; 28718c2ecf20Sopenharmony_ci} 28728c2ecf20Sopenharmony_ci 28738c2ecf20Sopenharmony_ci/* 28748c2ecf20Sopenharmony_ci * Slab list should be fixed up by fixup_slab_list() for existing slab 28758c2ecf20Sopenharmony_ci * or cache_grow_end() for new slab 28768c2ecf20Sopenharmony_ci */ 28778c2ecf20Sopenharmony_cistatic __always_inline int alloc_block(struct kmem_cache *cachep, 28788c2ecf20Sopenharmony_ci struct array_cache *ac, struct page *page, int batchcount) 28798c2ecf20Sopenharmony_ci{ 28808c2ecf20Sopenharmony_ci /* 28818c2ecf20Sopenharmony_ci * There must be at least one object available for 28828c2ecf20Sopenharmony_ci * allocation. 28838c2ecf20Sopenharmony_ci */ 28848c2ecf20Sopenharmony_ci BUG_ON(page->active >= cachep->num); 28858c2ecf20Sopenharmony_ci 28868c2ecf20Sopenharmony_ci while (page->active < cachep->num && batchcount--) { 28878c2ecf20Sopenharmony_ci STATS_INC_ALLOCED(cachep); 28888c2ecf20Sopenharmony_ci STATS_INC_ACTIVE(cachep); 28898c2ecf20Sopenharmony_ci STATS_SET_HIGH(cachep); 28908c2ecf20Sopenharmony_ci 28918c2ecf20Sopenharmony_ci ac->entry[ac->avail++] = slab_get_obj(cachep, page); 28928c2ecf20Sopenharmony_ci } 28938c2ecf20Sopenharmony_ci 28948c2ecf20Sopenharmony_ci return batchcount; 28958c2ecf20Sopenharmony_ci} 28968c2ecf20Sopenharmony_ci 28978c2ecf20Sopenharmony_cistatic void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags) 28988c2ecf20Sopenharmony_ci{ 28998c2ecf20Sopenharmony_ci int batchcount; 29008c2ecf20Sopenharmony_ci struct kmem_cache_node *n; 29018c2ecf20Sopenharmony_ci struct array_cache *ac, *shared; 29028c2ecf20Sopenharmony_ci int node; 29038c2ecf20Sopenharmony_ci void *list = NULL; 29048c2ecf20Sopenharmony_ci struct page *page; 29058c2ecf20Sopenharmony_ci 29068c2ecf20Sopenharmony_ci check_irq_off(); 29078c2ecf20Sopenharmony_ci node = numa_mem_id(); 29088c2ecf20Sopenharmony_ci 29098c2ecf20Sopenharmony_ci ac = cpu_cache_get(cachep); 29108c2ecf20Sopenharmony_ci batchcount = ac->batchcount; 29118c2ecf20Sopenharmony_ci if (!ac->touched && batchcount > BATCHREFILL_LIMIT) { 29128c2ecf20Sopenharmony_ci /* 29138c2ecf20Sopenharmony_ci * If there was little recent activity on this cache, then 29148c2ecf20Sopenharmony_ci * perform only a partial refill. Otherwise we could generate 29158c2ecf20Sopenharmony_ci * refill bouncing. 29168c2ecf20Sopenharmony_ci */ 29178c2ecf20Sopenharmony_ci batchcount = BATCHREFILL_LIMIT; 29188c2ecf20Sopenharmony_ci } 29198c2ecf20Sopenharmony_ci n = get_node(cachep, node); 29208c2ecf20Sopenharmony_ci 29218c2ecf20Sopenharmony_ci BUG_ON(ac->avail > 0 || !n); 29228c2ecf20Sopenharmony_ci shared = READ_ONCE(n->shared); 29238c2ecf20Sopenharmony_ci if (!n->free_objects && (!shared || !shared->avail)) 29248c2ecf20Sopenharmony_ci goto direct_grow; 29258c2ecf20Sopenharmony_ci 29268c2ecf20Sopenharmony_ci spin_lock(&n->list_lock); 29278c2ecf20Sopenharmony_ci shared = READ_ONCE(n->shared); 29288c2ecf20Sopenharmony_ci 29298c2ecf20Sopenharmony_ci /* See if we can refill from the shared array */ 29308c2ecf20Sopenharmony_ci if (shared && transfer_objects(ac, shared, batchcount)) { 29318c2ecf20Sopenharmony_ci shared->touched = 1; 29328c2ecf20Sopenharmony_ci goto alloc_done; 29338c2ecf20Sopenharmony_ci } 29348c2ecf20Sopenharmony_ci 29358c2ecf20Sopenharmony_ci while (batchcount > 0) { 29368c2ecf20Sopenharmony_ci /* Get slab alloc is to come from. */ 29378c2ecf20Sopenharmony_ci page = get_first_slab(n, false); 29388c2ecf20Sopenharmony_ci if (!page) 29398c2ecf20Sopenharmony_ci goto must_grow; 29408c2ecf20Sopenharmony_ci 29418c2ecf20Sopenharmony_ci check_spinlock_acquired(cachep); 29428c2ecf20Sopenharmony_ci 29438c2ecf20Sopenharmony_ci batchcount = alloc_block(cachep, ac, page, batchcount); 29448c2ecf20Sopenharmony_ci fixup_slab_list(cachep, n, page, &list); 29458c2ecf20Sopenharmony_ci } 29468c2ecf20Sopenharmony_ci 29478c2ecf20Sopenharmony_cimust_grow: 29488c2ecf20Sopenharmony_ci n->free_objects -= ac->avail; 29498c2ecf20Sopenharmony_cialloc_done: 29508c2ecf20Sopenharmony_ci spin_unlock(&n->list_lock); 29518c2ecf20Sopenharmony_ci fixup_objfreelist_debug(cachep, &list); 29528c2ecf20Sopenharmony_ci 29538c2ecf20Sopenharmony_cidirect_grow: 29548c2ecf20Sopenharmony_ci if (unlikely(!ac->avail)) { 29558c2ecf20Sopenharmony_ci /* Check if we can use obj in pfmemalloc slab */ 29568c2ecf20Sopenharmony_ci if (sk_memalloc_socks()) { 29578c2ecf20Sopenharmony_ci void *obj = cache_alloc_pfmemalloc(cachep, n, flags); 29588c2ecf20Sopenharmony_ci 29598c2ecf20Sopenharmony_ci if (obj) 29608c2ecf20Sopenharmony_ci return obj; 29618c2ecf20Sopenharmony_ci } 29628c2ecf20Sopenharmony_ci 29638c2ecf20Sopenharmony_ci page = cache_grow_begin(cachep, gfp_exact_node(flags), node); 29648c2ecf20Sopenharmony_ci 29658c2ecf20Sopenharmony_ci /* 29668c2ecf20Sopenharmony_ci * cache_grow_begin() can reenable interrupts, 29678c2ecf20Sopenharmony_ci * then ac could change. 29688c2ecf20Sopenharmony_ci */ 29698c2ecf20Sopenharmony_ci ac = cpu_cache_get(cachep); 29708c2ecf20Sopenharmony_ci if (!ac->avail && page) 29718c2ecf20Sopenharmony_ci alloc_block(cachep, ac, page, batchcount); 29728c2ecf20Sopenharmony_ci cache_grow_end(cachep, page); 29738c2ecf20Sopenharmony_ci 29748c2ecf20Sopenharmony_ci if (!ac->avail) 29758c2ecf20Sopenharmony_ci return NULL; 29768c2ecf20Sopenharmony_ci } 29778c2ecf20Sopenharmony_ci ac->touched = 1; 29788c2ecf20Sopenharmony_ci 29798c2ecf20Sopenharmony_ci return ac->entry[--ac->avail]; 29808c2ecf20Sopenharmony_ci} 29818c2ecf20Sopenharmony_ci 29828c2ecf20Sopenharmony_cistatic inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep, 29838c2ecf20Sopenharmony_ci gfp_t flags) 29848c2ecf20Sopenharmony_ci{ 29858c2ecf20Sopenharmony_ci might_sleep_if(gfpflags_allow_blocking(flags)); 29868c2ecf20Sopenharmony_ci} 29878c2ecf20Sopenharmony_ci 29888c2ecf20Sopenharmony_ci#if DEBUG 29898c2ecf20Sopenharmony_cistatic void *cache_alloc_debugcheck_after(struct kmem_cache *cachep, 29908c2ecf20Sopenharmony_ci gfp_t flags, void *objp, unsigned long caller) 29918c2ecf20Sopenharmony_ci{ 29928c2ecf20Sopenharmony_ci WARN_ON_ONCE(cachep->ctor && (flags & __GFP_ZERO)); 29938c2ecf20Sopenharmony_ci if (!objp) 29948c2ecf20Sopenharmony_ci return objp; 29958c2ecf20Sopenharmony_ci if (cachep->flags & SLAB_POISON) { 29968c2ecf20Sopenharmony_ci check_poison_obj(cachep, objp); 29978c2ecf20Sopenharmony_ci slab_kernel_map(cachep, objp, 1); 29988c2ecf20Sopenharmony_ci poison_obj(cachep, objp, POISON_INUSE); 29998c2ecf20Sopenharmony_ci } 30008c2ecf20Sopenharmony_ci if (cachep->flags & SLAB_STORE_USER) 30018c2ecf20Sopenharmony_ci *dbg_userword(cachep, objp) = (void *)caller; 30028c2ecf20Sopenharmony_ci 30038c2ecf20Sopenharmony_ci if (cachep->flags & SLAB_RED_ZONE) { 30048c2ecf20Sopenharmony_ci if (*dbg_redzone1(cachep, objp) != RED_INACTIVE || 30058c2ecf20Sopenharmony_ci *dbg_redzone2(cachep, objp) != RED_INACTIVE) { 30068c2ecf20Sopenharmony_ci slab_error(cachep, "double free, or memory outside object was overwritten"); 30078c2ecf20Sopenharmony_ci pr_err("%px: redzone 1:0x%llx, redzone 2:0x%llx\n", 30088c2ecf20Sopenharmony_ci objp, *dbg_redzone1(cachep, objp), 30098c2ecf20Sopenharmony_ci *dbg_redzone2(cachep, objp)); 30108c2ecf20Sopenharmony_ci } 30118c2ecf20Sopenharmony_ci *dbg_redzone1(cachep, objp) = RED_ACTIVE; 30128c2ecf20Sopenharmony_ci *dbg_redzone2(cachep, objp) = RED_ACTIVE; 30138c2ecf20Sopenharmony_ci } 30148c2ecf20Sopenharmony_ci 30158c2ecf20Sopenharmony_ci objp += obj_offset(cachep); 30168c2ecf20Sopenharmony_ci if (cachep->ctor && cachep->flags & SLAB_POISON) 30178c2ecf20Sopenharmony_ci cachep->ctor(objp); 30188c2ecf20Sopenharmony_ci if (ARCH_SLAB_MINALIGN && 30198c2ecf20Sopenharmony_ci ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) { 30208c2ecf20Sopenharmony_ci pr_err("0x%px: not aligned to ARCH_SLAB_MINALIGN=%d\n", 30218c2ecf20Sopenharmony_ci objp, (int)ARCH_SLAB_MINALIGN); 30228c2ecf20Sopenharmony_ci } 30238c2ecf20Sopenharmony_ci return objp; 30248c2ecf20Sopenharmony_ci} 30258c2ecf20Sopenharmony_ci#else 30268c2ecf20Sopenharmony_ci#define cache_alloc_debugcheck_after(a,b,objp,d) (objp) 30278c2ecf20Sopenharmony_ci#endif 30288c2ecf20Sopenharmony_ci 30298c2ecf20Sopenharmony_cistatic inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags) 30308c2ecf20Sopenharmony_ci{ 30318c2ecf20Sopenharmony_ci void *objp; 30328c2ecf20Sopenharmony_ci struct array_cache *ac; 30338c2ecf20Sopenharmony_ci 30348c2ecf20Sopenharmony_ci check_irq_off(); 30358c2ecf20Sopenharmony_ci 30368c2ecf20Sopenharmony_ci ac = cpu_cache_get(cachep); 30378c2ecf20Sopenharmony_ci if (likely(ac->avail)) { 30388c2ecf20Sopenharmony_ci ac->touched = 1; 30398c2ecf20Sopenharmony_ci objp = ac->entry[--ac->avail]; 30408c2ecf20Sopenharmony_ci 30418c2ecf20Sopenharmony_ci STATS_INC_ALLOCHIT(cachep); 30428c2ecf20Sopenharmony_ci goto out; 30438c2ecf20Sopenharmony_ci } 30448c2ecf20Sopenharmony_ci 30458c2ecf20Sopenharmony_ci STATS_INC_ALLOCMISS(cachep); 30468c2ecf20Sopenharmony_ci objp = cache_alloc_refill(cachep, flags); 30478c2ecf20Sopenharmony_ci /* 30488c2ecf20Sopenharmony_ci * the 'ac' may be updated by cache_alloc_refill(), 30498c2ecf20Sopenharmony_ci * and kmemleak_erase() requires its correct value. 30508c2ecf20Sopenharmony_ci */ 30518c2ecf20Sopenharmony_ci ac = cpu_cache_get(cachep); 30528c2ecf20Sopenharmony_ci 30538c2ecf20Sopenharmony_ciout: 30548c2ecf20Sopenharmony_ci /* 30558c2ecf20Sopenharmony_ci * To avoid a false negative, if an object that is in one of the 30568c2ecf20Sopenharmony_ci * per-CPU caches is leaked, we need to make sure kmemleak doesn't 30578c2ecf20Sopenharmony_ci * treat the array pointers as a reference to the object. 30588c2ecf20Sopenharmony_ci */ 30598c2ecf20Sopenharmony_ci if (objp) 30608c2ecf20Sopenharmony_ci kmemleak_erase(&ac->entry[ac->avail]); 30618c2ecf20Sopenharmony_ci return objp; 30628c2ecf20Sopenharmony_ci} 30638c2ecf20Sopenharmony_ci 30648c2ecf20Sopenharmony_ci#ifdef CONFIG_NUMA 30658c2ecf20Sopenharmony_ci/* 30668c2ecf20Sopenharmony_ci * Try allocating on another node if PFA_SPREAD_SLAB is a mempolicy is set. 30678c2ecf20Sopenharmony_ci * 30688c2ecf20Sopenharmony_ci * If we are in_interrupt, then process context, including cpusets and 30698c2ecf20Sopenharmony_ci * mempolicy, may not apply and should not be used for allocation policy. 30708c2ecf20Sopenharmony_ci */ 30718c2ecf20Sopenharmony_cistatic void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags) 30728c2ecf20Sopenharmony_ci{ 30738c2ecf20Sopenharmony_ci int nid_alloc, nid_here; 30748c2ecf20Sopenharmony_ci 30758c2ecf20Sopenharmony_ci if (in_interrupt() || (flags & __GFP_THISNODE)) 30768c2ecf20Sopenharmony_ci return NULL; 30778c2ecf20Sopenharmony_ci nid_alloc = nid_here = numa_mem_id(); 30788c2ecf20Sopenharmony_ci if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD)) 30798c2ecf20Sopenharmony_ci nid_alloc = cpuset_slab_spread_node(); 30808c2ecf20Sopenharmony_ci else if (current->mempolicy) 30818c2ecf20Sopenharmony_ci nid_alloc = mempolicy_slab_node(); 30828c2ecf20Sopenharmony_ci if (nid_alloc != nid_here) 30838c2ecf20Sopenharmony_ci return ____cache_alloc_node(cachep, flags, nid_alloc); 30848c2ecf20Sopenharmony_ci return NULL; 30858c2ecf20Sopenharmony_ci} 30868c2ecf20Sopenharmony_ci 30878c2ecf20Sopenharmony_ci/* 30888c2ecf20Sopenharmony_ci * Fallback function if there was no memory available and no objects on a 30898c2ecf20Sopenharmony_ci * certain node and fall back is permitted. First we scan all the 30908c2ecf20Sopenharmony_ci * available node for available objects. If that fails then we 30918c2ecf20Sopenharmony_ci * perform an allocation without specifying a node. This allows the page 30928c2ecf20Sopenharmony_ci * allocator to do its reclaim / fallback magic. We then insert the 30938c2ecf20Sopenharmony_ci * slab into the proper nodelist and then allocate from it. 30948c2ecf20Sopenharmony_ci */ 30958c2ecf20Sopenharmony_cistatic void *fallback_alloc(struct kmem_cache *cache, gfp_t flags) 30968c2ecf20Sopenharmony_ci{ 30978c2ecf20Sopenharmony_ci struct zonelist *zonelist; 30988c2ecf20Sopenharmony_ci struct zoneref *z; 30998c2ecf20Sopenharmony_ci struct zone *zone; 31008c2ecf20Sopenharmony_ci enum zone_type highest_zoneidx = gfp_zone(flags); 31018c2ecf20Sopenharmony_ci void *obj = NULL; 31028c2ecf20Sopenharmony_ci struct page *page; 31038c2ecf20Sopenharmony_ci int nid; 31048c2ecf20Sopenharmony_ci unsigned int cpuset_mems_cookie; 31058c2ecf20Sopenharmony_ci 31068c2ecf20Sopenharmony_ci if (flags & __GFP_THISNODE) 31078c2ecf20Sopenharmony_ci return NULL; 31088c2ecf20Sopenharmony_ci 31098c2ecf20Sopenharmony_ciretry_cpuset: 31108c2ecf20Sopenharmony_ci cpuset_mems_cookie = read_mems_allowed_begin(); 31118c2ecf20Sopenharmony_ci zonelist = node_zonelist(mempolicy_slab_node(), flags); 31128c2ecf20Sopenharmony_ci 31138c2ecf20Sopenharmony_ciretry: 31148c2ecf20Sopenharmony_ci /* 31158c2ecf20Sopenharmony_ci * Look through allowed nodes for objects available 31168c2ecf20Sopenharmony_ci * from existing per node queues. 31178c2ecf20Sopenharmony_ci */ 31188c2ecf20Sopenharmony_ci for_each_zone_zonelist(zone, z, zonelist, highest_zoneidx) { 31198c2ecf20Sopenharmony_ci nid = zone_to_nid(zone); 31208c2ecf20Sopenharmony_ci 31218c2ecf20Sopenharmony_ci if (cpuset_zone_allowed(zone, flags) && 31228c2ecf20Sopenharmony_ci get_node(cache, nid) && 31238c2ecf20Sopenharmony_ci get_node(cache, nid)->free_objects) { 31248c2ecf20Sopenharmony_ci obj = ____cache_alloc_node(cache, 31258c2ecf20Sopenharmony_ci gfp_exact_node(flags), nid); 31268c2ecf20Sopenharmony_ci if (obj) 31278c2ecf20Sopenharmony_ci break; 31288c2ecf20Sopenharmony_ci } 31298c2ecf20Sopenharmony_ci } 31308c2ecf20Sopenharmony_ci 31318c2ecf20Sopenharmony_ci if (!obj) { 31328c2ecf20Sopenharmony_ci /* 31338c2ecf20Sopenharmony_ci * This allocation will be performed within the constraints 31348c2ecf20Sopenharmony_ci * of the current cpuset / memory policy requirements. 31358c2ecf20Sopenharmony_ci * We may trigger various forms of reclaim on the allowed 31368c2ecf20Sopenharmony_ci * set and go into memory reserves if necessary. 31378c2ecf20Sopenharmony_ci */ 31388c2ecf20Sopenharmony_ci page = cache_grow_begin(cache, flags, numa_mem_id()); 31398c2ecf20Sopenharmony_ci cache_grow_end(cache, page); 31408c2ecf20Sopenharmony_ci if (page) { 31418c2ecf20Sopenharmony_ci nid = page_to_nid(page); 31428c2ecf20Sopenharmony_ci obj = ____cache_alloc_node(cache, 31438c2ecf20Sopenharmony_ci gfp_exact_node(flags), nid); 31448c2ecf20Sopenharmony_ci 31458c2ecf20Sopenharmony_ci /* 31468c2ecf20Sopenharmony_ci * Another processor may allocate the objects in 31478c2ecf20Sopenharmony_ci * the slab since we are not holding any locks. 31488c2ecf20Sopenharmony_ci */ 31498c2ecf20Sopenharmony_ci if (!obj) 31508c2ecf20Sopenharmony_ci goto retry; 31518c2ecf20Sopenharmony_ci } 31528c2ecf20Sopenharmony_ci } 31538c2ecf20Sopenharmony_ci 31548c2ecf20Sopenharmony_ci if (unlikely(!obj && read_mems_allowed_retry(cpuset_mems_cookie))) 31558c2ecf20Sopenharmony_ci goto retry_cpuset; 31568c2ecf20Sopenharmony_ci return obj; 31578c2ecf20Sopenharmony_ci} 31588c2ecf20Sopenharmony_ci 31598c2ecf20Sopenharmony_ci/* 31608c2ecf20Sopenharmony_ci * A interface to enable slab creation on nodeid 31618c2ecf20Sopenharmony_ci */ 31628c2ecf20Sopenharmony_cistatic void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, 31638c2ecf20Sopenharmony_ci int nodeid) 31648c2ecf20Sopenharmony_ci{ 31658c2ecf20Sopenharmony_ci struct page *page; 31668c2ecf20Sopenharmony_ci struct kmem_cache_node *n; 31678c2ecf20Sopenharmony_ci void *obj = NULL; 31688c2ecf20Sopenharmony_ci void *list = NULL; 31698c2ecf20Sopenharmony_ci 31708c2ecf20Sopenharmony_ci VM_BUG_ON(nodeid < 0 || nodeid >= MAX_NUMNODES); 31718c2ecf20Sopenharmony_ci n = get_node(cachep, nodeid); 31728c2ecf20Sopenharmony_ci BUG_ON(!n); 31738c2ecf20Sopenharmony_ci 31748c2ecf20Sopenharmony_ci check_irq_off(); 31758c2ecf20Sopenharmony_ci spin_lock(&n->list_lock); 31768c2ecf20Sopenharmony_ci page = get_first_slab(n, false); 31778c2ecf20Sopenharmony_ci if (!page) 31788c2ecf20Sopenharmony_ci goto must_grow; 31798c2ecf20Sopenharmony_ci 31808c2ecf20Sopenharmony_ci check_spinlock_acquired_node(cachep, nodeid); 31818c2ecf20Sopenharmony_ci 31828c2ecf20Sopenharmony_ci STATS_INC_NODEALLOCS(cachep); 31838c2ecf20Sopenharmony_ci STATS_INC_ACTIVE(cachep); 31848c2ecf20Sopenharmony_ci STATS_SET_HIGH(cachep); 31858c2ecf20Sopenharmony_ci 31868c2ecf20Sopenharmony_ci BUG_ON(page->active == cachep->num); 31878c2ecf20Sopenharmony_ci 31888c2ecf20Sopenharmony_ci obj = slab_get_obj(cachep, page); 31898c2ecf20Sopenharmony_ci n->free_objects--; 31908c2ecf20Sopenharmony_ci 31918c2ecf20Sopenharmony_ci fixup_slab_list(cachep, n, page, &list); 31928c2ecf20Sopenharmony_ci 31938c2ecf20Sopenharmony_ci spin_unlock(&n->list_lock); 31948c2ecf20Sopenharmony_ci fixup_objfreelist_debug(cachep, &list); 31958c2ecf20Sopenharmony_ci return obj; 31968c2ecf20Sopenharmony_ci 31978c2ecf20Sopenharmony_cimust_grow: 31988c2ecf20Sopenharmony_ci spin_unlock(&n->list_lock); 31998c2ecf20Sopenharmony_ci page = cache_grow_begin(cachep, gfp_exact_node(flags), nodeid); 32008c2ecf20Sopenharmony_ci if (page) { 32018c2ecf20Sopenharmony_ci /* This slab isn't counted yet so don't update free_objects */ 32028c2ecf20Sopenharmony_ci obj = slab_get_obj(cachep, page); 32038c2ecf20Sopenharmony_ci } 32048c2ecf20Sopenharmony_ci cache_grow_end(cachep, page); 32058c2ecf20Sopenharmony_ci 32068c2ecf20Sopenharmony_ci return obj ? obj : fallback_alloc(cachep, flags); 32078c2ecf20Sopenharmony_ci} 32088c2ecf20Sopenharmony_ci 32098c2ecf20Sopenharmony_cistatic __always_inline void * 32108c2ecf20Sopenharmony_cislab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid, 32118c2ecf20Sopenharmony_ci unsigned long caller) 32128c2ecf20Sopenharmony_ci{ 32138c2ecf20Sopenharmony_ci unsigned long save_flags; 32148c2ecf20Sopenharmony_ci void *ptr; 32158c2ecf20Sopenharmony_ci int slab_node = numa_mem_id(); 32168c2ecf20Sopenharmony_ci struct obj_cgroup *objcg = NULL; 32178c2ecf20Sopenharmony_ci 32188c2ecf20Sopenharmony_ci flags &= gfp_allowed_mask; 32198c2ecf20Sopenharmony_ci cachep = slab_pre_alloc_hook(cachep, &objcg, 1, flags); 32208c2ecf20Sopenharmony_ci if (unlikely(!cachep)) 32218c2ecf20Sopenharmony_ci return NULL; 32228c2ecf20Sopenharmony_ci 32238c2ecf20Sopenharmony_ci cache_alloc_debugcheck_before(cachep, flags); 32248c2ecf20Sopenharmony_ci local_irq_save(save_flags); 32258c2ecf20Sopenharmony_ci 32268c2ecf20Sopenharmony_ci if (nodeid == NUMA_NO_NODE) 32278c2ecf20Sopenharmony_ci nodeid = slab_node; 32288c2ecf20Sopenharmony_ci 32298c2ecf20Sopenharmony_ci if (unlikely(!get_node(cachep, nodeid))) { 32308c2ecf20Sopenharmony_ci /* Node not bootstrapped yet */ 32318c2ecf20Sopenharmony_ci ptr = fallback_alloc(cachep, flags); 32328c2ecf20Sopenharmony_ci goto out; 32338c2ecf20Sopenharmony_ci } 32348c2ecf20Sopenharmony_ci 32358c2ecf20Sopenharmony_ci if (nodeid == slab_node) { 32368c2ecf20Sopenharmony_ci /* 32378c2ecf20Sopenharmony_ci * Use the locally cached objects if possible. 32388c2ecf20Sopenharmony_ci * However ____cache_alloc does not allow fallback 32398c2ecf20Sopenharmony_ci * to other nodes. It may fail while we still have 32408c2ecf20Sopenharmony_ci * objects on other nodes available. 32418c2ecf20Sopenharmony_ci */ 32428c2ecf20Sopenharmony_ci ptr = ____cache_alloc(cachep, flags); 32438c2ecf20Sopenharmony_ci if (ptr) 32448c2ecf20Sopenharmony_ci goto out; 32458c2ecf20Sopenharmony_ci } 32468c2ecf20Sopenharmony_ci /* ___cache_alloc_node can fall back to other nodes */ 32478c2ecf20Sopenharmony_ci ptr = ____cache_alloc_node(cachep, flags, nodeid); 32488c2ecf20Sopenharmony_ci out: 32498c2ecf20Sopenharmony_ci local_irq_restore(save_flags); 32508c2ecf20Sopenharmony_ci ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller); 32518c2ecf20Sopenharmony_ci 32528c2ecf20Sopenharmony_ci if (unlikely(slab_want_init_on_alloc(flags, cachep)) && ptr) 32538c2ecf20Sopenharmony_ci memset(ptr, 0, cachep->object_size); 32548c2ecf20Sopenharmony_ci 32558c2ecf20Sopenharmony_ci slab_post_alloc_hook(cachep, objcg, flags, 1, &ptr); 32568c2ecf20Sopenharmony_ci return ptr; 32578c2ecf20Sopenharmony_ci} 32588c2ecf20Sopenharmony_ci 32598c2ecf20Sopenharmony_cistatic __always_inline void * 32608c2ecf20Sopenharmony_ci__do_cache_alloc(struct kmem_cache *cache, gfp_t flags) 32618c2ecf20Sopenharmony_ci{ 32628c2ecf20Sopenharmony_ci void *objp; 32638c2ecf20Sopenharmony_ci 32648c2ecf20Sopenharmony_ci if (current->mempolicy || cpuset_do_slab_mem_spread()) { 32658c2ecf20Sopenharmony_ci objp = alternate_node_alloc(cache, flags); 32668c2ecf20Sopenharmony_ci if (objp) 32678c2ecf20Sopenharmony_ci goto out; 32688c2ecf20Sopenharmony_ci } 32698c2ecf20Sopenharmony_ci objp = ____cache_alloc(cache, flags); 32708c2ecf20Sopenharmony_ci 32718c2ecf20Sopenharmony_ci /* 32728c2ecf20Sopenharmony_ci * We may just have run out of memory on the local node. 32738c2ecf20Sopenharmony_ci * ____cache_alloc_node() knows how to locate memory on other nodes 32748c2ecf20Sopenharmony_ci */ 32758c2ecf20Sopenharmony_ci if (!objp) 32768c2ecf20Sopenharmony_ci objp = ____cache_alloc_node(cache, flags, numa_mem_id()); 32778c2ecf20Sopenharmony_ci 32788c2ecf20Sopenharmony_ci out: 32798c2ecf20Sopenharmony_ci return objp; 32808c2ecf20Sopenharmony_ci} 32818c2ecf20Sopenharmony_ci#else 32828c2ecf20Sopenharmony_ci 32838c2ecf20Sopenharmony_cistatic __always_inline void * 32848c2ecf20Sopenharmony_ci__do_cache_alloc(struct kmem_cache *cachep, gfp_t flags) 32858c2ecf20Sopenharmony_ci{ 32868c2ecf20Sopenharmony_ci return ____cache_alloc(cachep, flags); 32878c2ecf20Sopenharmony_ci} 32888c2ecf20Sopenharmony_ci 32898c2ecf20Sopenharmony_ci#endif /* CONFIG_NUMA */ 32908c2ecf20Sopenharmony_ci 32918c2ecf20Sopenharmony_cistatic __always_inline void * 32928c2ecf20Sopenharmony_cislab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller) 32938c2ecf20Sopenharmony_ci{ 32948c2ecf20Sopenharmony_ci unsigned long save_flags; 32958c2ecf20Sopenharmony_ci void *objp; 32968c2ecf20Sopenharmony_ci struct obj_cgroup *objcg = NULL; 32978c2ecf20Sopenharmony_ci 32988c2ecf20Sopenharmony_ci flags &= gfp_allowed_mask; 32998c2ecf20Sopenharmony_ci cachep = slab_pre_alloc_hook(cachep, &objcg, 1, flags); 33008c2ecf20Sopenharmony_ci if (unlikely(!cachep)) 33018c2ecf20Sopenharmony_ci return NULL; 33028c2ecf20Sopenharmony_ci 33038c2ecf20Sopenharmony_ci cache_alloc_debugcheck_before(cachep, flags); 33048c2ecf20Sopenharmony_ci local_irq_save(save_flags); 33058c2ecf20Sopenharmony_ci objp = __do_cache_alloc(cachep, flags); 33068c2ecf20Sopenharmony_ci local_irq_restore(save_flags); 33078c2ecf20Sopenharmony_ci objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller); 33088c2ecf20Sopenharmony_ci prefetchw(objp); 33098c2ecf20Sopenharmony_ci 33108c2ecf20Sopenharmony_ci if (unlikely(slab_want_init_on_alloc(flags, cachep)) && objp) 33118c2ecf20Sopenharmony_ci memset(objp, 0, cachep->object_size); 33128c2ecf20Sopenharmony_ci 33138c2ecf20Sopenharmony_ci slab_post_alloc_hook(cachep, objcg, flags, 1, &objp); 33148c2ecf20Sopenharmony_ci return objp; 33158c2ecf20Sopenharmony_ci} 33168c2ecf20Sopenharmony_ci 33178c2ecf20Sopenharmony_ci/* 33188c2ecf20Sopenharmony_ci * Caller needs to acquire correct kmem_cache_node's list_lock 33198c2ecf20Sopenharmony_ci * @list: List of detached free slabs should be freed by caller 33208c2ecf20Sopenharmony_ci */ 33218c2ecf20Sopenharmony_cistatic void free_block(struct kmem_cache *cachep, void **objpp, 33228c2ecf20Sopenharmony_ci int nr_objects, int node, struct list_head *list) 33238c2ecf20Sopenharmony_ci{ 33248c2ecf20Sopenharmony_ci int i; 33258c2ecf20Sopenharmony_ci struct kmem_cache_node *n = get_node(cachep, node); 33268c2ecf20Sopenharmony_ci struct page *page; 33278c2ecf20Sopenharmony_ci 33288c2ecf20Sopenharmony_ci n->free_objects += nr_objects; 33298c2ecf20Sopenharmony_ci 33308c2ecf20Sopenharmony_ci for (i = 0; i < nr_objects; i++) { 33318c2ecf20Sopenharmony_ci void *objp; 33328c2ecf20Sopenharmony_ci struct page *page; 33338c2ecf20Sopenharmony_ci 33348c2ecf20Sopenharmony_ci objp = objpp[i]; 33358c2ecf20Sopenharmony_ci 33368c2ecf20Sopenharmony_ci page = virt_to_head_page(objp); 33378c2ecf20Sopenharmony_ci list_del(&page->slab_list); 33388c2ecf20Sopenharmony_ci check_spinlock_acquired_node(cachep, node); 33398c2ecf20Sopenharmony_ci slab_put_obj(cachep, page, objp); 33408c2ecf20Sopenharmony_ci STATS_DEC_ACTIVE(cachep); 33418c2ecf20Sopenharmony_ci 33428c2ecf20Sopenharmony_ci /* fixup slab chains */ 33438c2ecf20Sopenharmony_ci if (page->active == 0) { 33448c2ecf20Sopenharmony_ci list_add(&page->slab_list, &n->slabs_free); 33458c2ecf20Sopenharmony_ci n->free_slabs++; 33468c2ecf20Sopenharmony_ci } else { 33478c2ecf20Sopenharmony_ci /* Unconditionally move a slab to the end of the 33488c2ecf20Sopenharmony_ci * partial list on free - maximum time for the 33498c2ecf20Sopenharmony_ci * other objects to be freed, too. 33508c2ecf20Sopenharmony_ci */ 33518c2ecf20Sopenharmony_ci list_add_tail(&page->slab_list, &n->slabs_partial); 33528c2ecf20Sopenharmony_ci } 33538c2ecf20Sopenharmony_ci } 33548c2ecf20Sopenharmony_ci 33558c2ecf20Sopenharmony_ci while (n->free_objects > n->free_limit && !list_empty(&n->slabs_free)) { 33568c2ecf20Sopenharmony_ci n->free_objects -= cachep->num; 33578c2ecf20Sopenharmony_ci 33588c2ecf20Sopenharmony_ci page = list_last_entry(&n->slabs_free, struct page, slab_list); 33598c2ecf20Sopenharmony_ci list_move(&page->slab_list, list); 33608c2ecf20Sopenharmony_ci n->free_slabs--; 33618c2ecf20Sopenharmony_ci n->total_slabs--; 33628c2ecf20Sopenharmony_ci } 33638c2ecf20Sopenharmony_ci} 33648c2ecf20Sopenharmony_ci 33658c2ecf20Sopenharmony_cistatic void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac) 33668c2ecf20Sopenharmony_ci{ 33678c2ecf20Sopenharmony_ci int batchcount; 33688c2ecf20Sopenharmony_ci struct kmem_cache_node *n; 33698c2ecf20Sopenharmony_ci int node = numa_mem_id(); 33708c2ecf20Sopenharmony_ci LIST_HEAD(list); 33718c2ecf20Sopenharmony_ci 33728c2ecf20Sopenharmony_ci batchcount = ac->batchcount; 33738c2ecf20Sopenharmony_ci 33748c2ecf20Sopenharmony_ci check_irq_off(); 33758c2ecf20Sopenharmony_ci n = get_node(cachep, node); 33768c2ecf20Sopenharmony_ci spin_lock(&n->list_lock); 33778c2ecf20Sopenharmony_ci if (n->shared) { 33788c2ecf20Sopenharmony_ci struct array_cache *shared_array = n->shared; 33798c2ecf20Sopenharmony_ci int max = shared_array->limit - shared_array->avail; 33808c2ecf20Sopenharmony_ci if (max) { 33818c2ecf20Sopenharmony_ci if (batchcount > max) 33828c2ecf20Sopenharmony_ci batchcount = max; 33838c2ecf20Sopenharmony_ci memcpy(&(shared_array->entry[shared_array->avail]), 33848c2ecf20Sopenharmony_ci ac->entry, sizeof(void *) * batchcount); 33858c2ecf20Sopenharmony_ci shared_array->avail += batchcount; 33868c2ecf20Sopenharmony_ci goto free_done; 33878c2ecf20Sopenharmony_ci } 33888c2ecf20Sopenharmony_ci } 33898c2ecf20Sopenharmony_ci 33908c2ecf20Sopenharmony_ci free_block(cachep, ac->entry, batchcount, node, &list); 33918c2ecf20Sopenharmony_cifree_done: 33928c2ecf20Sopenharmony_ci#if STATS 33938c2ecf20Sopenharmony_ci { 33948c2ecf20Sopenharmony_ci int i = 0; 33958c2ecf20Sopenharmony_ci struct page *page; 33968c2ecf20Sopenharmony_ci 33978c2ecf20Sopenharmony_ci list_for_each_entry(page, &n->slabs_free, slab_list) { 33988c2ecf20Sopenharmony_ci BUG_ON(page->active); 33998c2ecf20Sopenharmony_ci 34008c2ecf20Sopenharmony_ci i++; 34018c2ecf20Sopenharmony_ci } 34028c2ecf20Sopenharmony_ci STATS_SET_FREEABLE(cachep, i); 34038c2ecf20Sopenharmony_ci } 34048c2ecf20Sopenharmony_ci#endif 34058c2ecf20Sopenharmony_ci spin_unlock(&n->list_lock); 34068c2ecf20Sopenharmony_ci ac->avail -= batchcount; 34078c2ecf20Sopenharmony_ci memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail); 34088c2ecf20Sopenharmony_ci slabs_destroy(cachep, &list); 34098c2ecf20Sopenharmony_ci} 34108c2ecf20Sopenharmony_ci 34118c2ecf20Sopenharmony_ci/* 34128c2ecf20Sopenharmony_ci * Release an obj back to its cache. If the obj has a constructed state, it must 34138c2ecf20Sopenharmony_ci * be in this state _before_ it is released. Called with disabled ints. 34148c2ecf20Sopenharmony_ci */ 34158c2ecf20Sopenharmony_cistatic __always_inline void __cache_free(struct kmem_cache *cachep, void *objp, 34168c2ecf20Sopenharmony_ci unsigned long caller) 34178c2ecf20Sopenharmony_ci{ 34188c2ecf20Sopenharmony_ci /* Put the object into the quarantine, don't touch it for now. */ 34198c2ecf20Sopenharmony_ci if (kasan_slab_free(cachep, objp, _RET_IP_)) 34208c2ecf20Sopenharmony_ci return; 34218c2ecf20Sopenharmony_ci 34228c2ecf20Sopenharmony_ci /* Use KCSAN to help debug racy use-after-free. */ 34238c2ecf20Sopenharmony_ci if (!(cachep->flags & SLAB_TYPESAFE_BY_RCU)) 34248c2ecf20Sopenharmony_ci __kcsan_check_access(objp, cachep->object_size, 34258c2ecf20Sopenharmony_ci KCSAN_ACCESS_WRITE | KCSAN_ACCESS_ASSERT); 34268c2ecf20Sopenharmony_ci 34278c2ecf20Sopenharmony_ci ___cache_free(cachep, objp, caller); 34288c2ecf20Sopenharmony_ci} 34298c2ecf20Sopenharmony_ci 34308c2ecf20Sopenharmony_civoid ___cache_free(struct kmem_cache *cachep, void *objp, 34318c2ecf20Sopenharmony_ci unsigned long caller) 34328c2ecf20Sopenharmony_ci{ 34338c2ecf20Sopenharmony_ci struct array_cache *ac = cpu_cache_get(cachep); 34348c2ecf20Sopenharmony_ci 34358c2ecf20Sopenharmony_ci check_irq_off(); 34368c2ecf20Sopenharmony_ci if (unlikely(slab_want_init_on_free(cachep))) 34378c2ecf20Sopenharmony_ci memset(objp, 0, cachep->object_size); 34388c2ecf20Sopenharmony_ci kmemleak_free_recursive(objp, cachep->flags); 34398c2ecf20Sopenharmony_ci objp = cache_free_debugcheck(cachep, objp, caller); 34408c2ecf20Sopenharmony_ci memcg_slab_free_hook(cachep, &objp, 1); 34418c2ecf20Sopenharmony_ci 34428c2ecf20Sopenharmony_ci /* 34438c2ecf20Sopenharmony_ci * Skip calling cache_free_alien() when the platform is not numa. 34448c2ecf20Sopenharmony_ci * This will avoid cache misses that happen while accessing slabp (which 34458c2ecf20Sopenharmony_ci * is per page memory reference) to get nodeid. Instead use a global 34468c2ecf20Sopenharmony_ci * variable to skip the call, which is mostly likely to be present in 34478c2ecf20Sopenharmony_ci * the cache. 34488c2ecf20Sopenharmony_ci */ 34498c2ecf20Sopenharmony_ci if (nr_online_nodes > 1 && cache_free_alien(cachep, objp)) 34508c2ecf20Sopenharmony_ci return; 34518c2ecf20Sopenharmony_ci 34528c2ecf20Sopenharmony_ci if (ac->avail < ac->limit) { 34538c2ecf20Sopenharmony_ci STATS_INC_FREEHIT(cachep); 34548c2ecf20Sopenharmony_ci } else { 34558c2ecf20Sopenharmony_ci STATS_INC_FREEMISS(cachep); 34568c2ecf20Sopenharmony_ci cache_flusharray(cachep, ac); 34578c2ecf20Sopenharmony_ci } 34588c2ecf20Sopenharmony_ci 34598c2ecf20Sopenharmony_ci if (sk_memalloc_socks()) { 34608c2ecf20Sopenharmony_ci struct page *page = virt_to_head_page(objp); 34618c2ecf20Sopenharmony_ci 34628c2ecf20Sopenharmony_ci if (unlikely(PageSlabPfmemalloc(page))) { 34638c2ecf20Sopenharmony_ci cache_free_pfmemalloc(cachep, page, objp); 34648c2ecf20Sopenharmony_ci return; 34658c2ecf20Sopenharmony_ci } 34668c2ecf20Sopenharmony_ci } 34678c2ecf20Sopenharmony_ci 34688c2ecf20Sopenharmony_ci __free_one(ac, objp); 34698c2ecf20Sopenharmony_ci} 34708c2ecf20Sopenharmony_ci 34718c2ecf20Sopenharmony_ci/** 34728c2ecf20Sopenharmony_ci * kmem_cache_alloc - Allocate an object 34738c2ecf20Sopenharmony_ci * @cachep: The cache to allocate from. 34748c2ecf20Sopenharmony_ci * @flags: See kmalloc(). 34758c2ecf20Sopenharmony_ci * 34768c2ecf20Sopenharmony_ci * Allocate an object from this cache. The flags are only relevant 34778c2ecf20Sopenharmony_ci * if the cache has no available objects. 34788c2ecf20Sopenharmony_ci * 34798c2ecf20Sopenharmony_ci * Return: pointer to the new object or %NULL in case of error 34808c2ecf20Sopenharmony_ci */ 34818c2ecf20Sopenharmony_civoid *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags) 34828c2ecf20Sopenharmony_ci{ 34838c2ecf20Sopenharmony_ci void *ret = slab_alloc(cachep, flags, _RET_IP_); 34848c2ecf20Sopenharmony_ci 34858c2ecf20Sopenharmony_ci trace_kmem_cache_alloc(_RET_IP_, ret, 34868c2ecf20Sopenharmony_ci cachep->object_size, cachep->size, flags); 34878c2ecf20Sopenharmony_ci 34888c2ecf20Sopenharmony_ci return ret; 34898c2ecf20Sopenharmony_ci} 34908c2ecf20Sopenharmony_ciEXPORT_SYMBOL(kmem_cache_alloc); 34918c2ecf20Sopenharmony_ci 34928c2ecf20Sopenharmony_cistatic __always_inline void 34938c2ecf20Sopenharmony_cicache_alloc_debugcheck_after_bulk(struct kmem_cache *s, gfp_t flags, 34948c2ecf20Sopenharmony_ci size_t size, void **p, unsigned long caller) 34958c2ecf20Sopenharmony_ci{ 34968c2ecf20Sopenharmony_ci size_t i; 34978c2ecf20Sopenharmony_ci 34988c2ecf20Sopenharmony_ci for (i = 0; i < size; i++) 34998c2ecf20Sopenharmony_ci p[i] = cache_alloc_debugcheck_after(s, flags, p[i], caller); 35008c2ecf20Sopenharmony_ci} 35018c2ecf20Sopenharmony_ci 35028c2ecf20Sopenharmony_ciint kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size, 35038c2ecf20Sopenharmony_ci void **p) 35048c2ecf20Sopenharmony_ci{ 35058c2ecf20Sopenharmony_ci size_t i; 35068c2ecf20Sopenharmony_ci struct obj_cgroup *objcg = NULL; 35078c2ecf20Sopenharmony_ci 35088c2ecf20Sopenharmony_ci s = slab_pre_alloc_hook(s, &objcg, size, flags); 35098c2ecf20Sopenharmony_ci if (!s) 35108c2ecf20Sopenharmony_ci return 0; 35118c2ecf20Sopenharmony_ci 35128c2ecf20Sopenharmony_ci cache_alloc_debugcheck_before(s, flags); 35138c2ecf20Sopenharmony_ci 35148c2ecf20Sopenharmony_ci local_irq_disable(); 35158c2ecf20Sopenharmony_ci for (i = 0; i < size; i++) { 35168c2ecf20Sopenharmony_ci void *objp = __do_cache_alloc(s, flags); 35178c2ecf20Sopenharmony_ci 35188c2ecf20Sopenharmony_ci if (unlikely(!objp)) 35198c2ecf20Sopenharmony_ci goto error; 35208c2ecf20Sopenharmony_ci p[i] = objp; 35218c2ecf20Sopenharmony_ci } 35228c2ecf20Sopenharmony_ci local_irq_enable(); 35238c2ecf20Sopenharmony_ci 35248c2ecf20Sopenharmony_ci cache_alloc_debugcheck_after_bulk(s, flags, size, p, _RET_IP_); 35258c2ecf20Sopenharmony_ci 35268c2ecf20Sopenharmony_ci /* Clear memory outside IRQ disabled section */ 35278c2ecf20Sopenharmony_ci if (unlikely(slab_want_init_on_alloc(flags, s))) 35288c2ecf20Sopenharmony_ci for (i = 0; i < size; i++) 35298c2ecf20Sopenharmony_ci memset(p[i], 0, s->object_size); 35308c2ecf20Sopenharmony_ci 35318c2ecf20Sopenharmony_ci slab_post_alloc_hook(s, objcg, flags, size, p); 35328c2ecf20Sopenharmony_ci /* FIXME: Trace call missing. Christoph would like a bulk variant */ 35338c2ecf20Sopenharmony_ci return size; 35348c2ecf20Sopenharmony_cierror: 35358c2ecf20Sopenharmony_ci local_irq_enable(); 35368c2ecf20Sopenharmony_ci cache_alloc_debugcheck_after_bulk(s, flags, i, p, _RET_IP_); 35378c2ecf20Sopenharmony_ci slab_post_alloc_hook(s, objcg, flags, i, p); 35388c2ecf20Sopenharmony_ci __kmem_cache_free_bulk(s, i, p); 35398c2ecf20Sopenharmony_ci return 0; 35408c2ecf20Sopenharmony_ci} 35418c2ecf20Sopenharmony_ciEXPORT_SYMBOL(kmem_cache_alloc_bulk); 35428c2ecf20Sopenharmony_ci 35438c2ecf20Sopenharmony_ci#ifdef CONFIG_TRACING 35448c2ecf20Sopenharmony_civoid * 35458c2ecf20Sopenharmony_cikmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size) 35468c2ecf20Sopenharmony_ci{ 35478c2ecf20Sopenharmony_ci void *ret; 35488c2ecf20Sopenharmony_ci 35498c2ecf20Sopenharmony_ci ret = slab_alloc(cachep, flags, _RET_IP_); 35508c2ecf20Sopenharmony_ci 35518c2ecf20Sopenharmony_ci ret = kasan_kmalloc(cachep, ret, size, flags); 35528c2ecf20Sopenharmony_ci trace_kmalloc(_RET_IP_, ret, 35538c2ecf20Sopenharmony_ci size, cachep->size, flags); 35548c2ecf20Sopenharmony_ci return ret; 35558c2ecf20Sopenharmony_ci} 35568c2ecf20Sopenharmony_ciEXPORT_SYMBOL(kmem_cache_alloc_trace); 35578c2ecf20Sopenharmony_ci#endif 35588c2ecf20Sopenharmony_ci 35598c2ecf20Sopenharmony_ci#ifdef CONFIG_NUMA 35608c2ecf20Sopenharmony_ci/** 35618c2ecf20Sopenharmony_ci * kmem_cache_alloc_node - Allocate an object on the specified node 35628c2ecf20Sopenharmony_ci * @cachep: The cache to allocate from. 35638c2ecf20Sopenharmony_ci * @flags: See kmalloc(). 35648c2ecf20Sopenharmony_ci * @nodeid: node number of the target node. 35658c2ecf20Sopenharmony_ci * 35668c2ecf20Sopenharmony_ci * Identical to kmem_cache_alloc but it will allocate memory on the given 35678c2ecf20Sopenharmony_ci * node, which can improve the performance for cpu bound structures. 35688c2ecf20Sopenharmony_ci * 35698c2ecf20Sopenharmony_ci * Fallback to other node is possible if __GFP_THISNODE is not set. 35708c2ecf20Sopenharmony_ci * 35718c2ecf20Sopenharmony_ci * Return: pointer to the new object or %NULL in case of error 35728c2ecf20Sopenharmony_ci */ 35738c2ecf20Sopenharmony_civoid *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid) 35748c2ecf20Sopenharmony_ci{ 35758c2ecf20Sopenharmony_ci void *ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_); 35768c2ecf20Sopenharmony_ci 35778c2ecf20Sopenharmony_ci trace_kmem_cache_alloc_node(_RET_IP_, ret, 35788c2ecf20Sopenharmony_ci cachep->object_size, cachep->size, 35798c2ecf20Sopenharmony_ci flags, nodeid); 35808c2ecf20Sopenharmony_ci 35818c2ecf20Sopenharmony_ci return ret; 35828c2ecf20Sopenharmony_ci} 35838c2ecf20Sopenharmony_ciEXPORT_SYMBOL(kmem_cache_alloc_node); 35848c2ecf20Sopenharmony_ci 35858c2ecf20Sopenharmony_ci#ifdef CONFIG_TRACING 35868c2ecf20Sopenharmony_civoid *kmem_cache_alloc_node_trace(struct kmem_cache *cachep, 35878c2ecf20Sopenharmony_ci gfp_t flags, 35888c2ecf20Sopenharmony_ci int nodeid, 35898c2ecf20Sopenharmony_ci size_t size) 35908c2ecf20Sopenharmony_ci{ 35918c2ecf20Sopenharmony_ci void *ret; 35928c2ecf20Sopenharmony_ci 35938c2ecf20Sopenharmony_ci ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_); 35948c2ecf20Sopenharmony_ci 35958c2ecf20Sopenharmony_ci ret = kasan_kmalloc(cachep, ret, size, flags); 35968c2ecf20Sopenharmony_ci trace_kmalloc_node(_RET_IP_, ret, 35978c2ecf20Sopenharmony_ci size, cachep->size, 35988c2ecf20Sopenharmony_ci flags, nodeid); 35998c2ecf20Sopenharmony_ci return ret; 36008c2ecf20Sopenharmony_ci} 36018c2ecf20Sopenharmony_ciEXPORT_SYMBOL(kmem_cache_alloc_node_trace); 36028c2ecf20Sopenharmony_ci#endif 36038c2ecf20Sopenharmony_ci 36048c2ecf20Sopenharmony_cistatic __always_inline void * 36058c2ecf20Sopenharmony_ci__do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller) 36068c2ecf20Sopenharmony_ci{ 36078c2ecf20Sopenharmony_ci struct kmem_cache *cachep; 36088c2ecf20Sopenharmony_ci void *ret; 36098c2ecf20Sopenharmony_ci 36108c2ecf20Sopenharmony_ci if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) 36118c2ecf20Sopenharmony_ci return NULL; 36128c2ecf20Sopenharmony_ci cachep = kmalloc_slab(size, flags); 36138c2ecf20Sopenharmony_ci if (unlikely(ZERO_OR_NULL_PTR(cachep))) 36148c2ecf20Sopenharmony_ci return cachep; 36158c2ecf20Sopenharmony_ci ret = kmem_cache_alloc_node_trace(cachep, flags, node, size); 36168c2ecf20Sopenharmony_ci ret = kasan_kmalloc(cachep, ret, size, flags); 36178c2ecf20Sopenharmony_ci 36188c2ecf20Sopenharmony_ci return ret; 36198c2ecf20Sopenharmony_ci} 36208c2ecf20Sopenharmony_ci 36218c2ecf20Sopenharmony_civoid *__kmalloc_node(size_t size, gfp_t flags, int node) 36228c2ecf20Sopenharmony_ci{ 36238c2ecf20Sopenharmony_ci return __do_kmalloc_node(size, flags, node, _RET_IP_); 36248c2ecf20Sopenharmony_ci} 36258c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__kmalloc_node); 36268c2ecf20Sopenharmony_ci 36278c2ecf20Sopenharmony_civoid *__kmalloc_node_track_caller(size_t size, gfp_t flags, 36288c2ecf20Sopenharmony_ci int node, unsigned long caller) 36298c2ecf20Sopenharmony_ci{ 36308c2ecf20Sopenharmony_ci return __do_kmalloc_node(size, flags, node, caller); 36318c2ecf20Sopenharmony_ci} 36328c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__kmalloc_node_track_caller); 36338c2ecf20Sopenharmony_ci#endif /* CONFIG_NUMA */ 36348c2ecf20Sopenharmony_ci 36358c2ecf20Sopenharmony_ci/** 36368c2ecf20Sopenharmony_ci * __do_kmalloc - allocate memory 36378c2ecf20Sopenharmony_ci * @size: how many bytes of memory are required. 36388c2ecf20Sopenharmony_ci * @flags: the type of memory to allocate (see kmalloc). 36398c2ecf20Sopenharmony_ci * @caller: function caller for debug tracking of the caller 36408c2ecf20Sopenharmony_ci * 36418c2ecf20Sopenharmony_ci * Return: pointer to the allocated memory or %NULL in case of error 36428c2ecf20Sopenharmony_ci */ 36438c2ecf20Sopenharmony_cistatic __always_inline void *__do_kmalloc(size_t size, gfp_t flags, 36448c2ecf20Sopenharmony_ci unsigned long caller) 36458c2ecf20Sopenharmony_ci{ 36468c2ecf20Sopenharmony_ci struct kmem_cache *cachep; 36478c2ecf20Sopenharmony_ci void *ret; 36488c2ecf20Sopenharmony_ci 36498c2ecf20Sopenharmony_ci if (unlikely(size > KMALLOC_MAX_CACHE_SIZE)) 36508c2ecf20Sopenharmony_ci return NULL; 36518c2ecf20Sopenharmony_ci cachep = kmalloc_slab(size, flags); 36528c2ecf20Sopenharmony_ci if (unlikely(ZERO_OR_NULL_PTR(cachep))) 36538c2ecf20Sopenharmony_ci return cachep; 36548c2ecf20Sopenharmony_ci ret = slab_alloc(cachep, flags, caller); 36558c2ecf20Sopenharmony_ci 36568c2ecf20Sopenharmony_ci ret = kasan_kmalloc(cachep, ret, size, flags); 36578c2ecf20Sopenharmony_ci trace_kmalloc(caller, ret, 36588c2ecf20Sopenharmony_ci size, cachep->size, flags); 36598c2ecf20Sopenharmony_ci 36608c2ecf20Sopenharmony_ci return ret; 36618c2ecf20Sopenharmony_ci} 36628c2ecf20Sopenharmony_ci 36638c2ecf20Sopenharmony_civoid *__kmalloc(size_t size, gfp_t flags) 36648c2ecf20Sopenharmony_ci{ 36658c2ecf20Sopenharmony_ci return __do_kmalloc(size, flags, _RET_IP_); 36668c2ecf20Sopenharmony_ci} 36678c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__kmalloc); 36688c2ecf20Sopenharmony_ci 36698c2ecf20Sopenharmony_civoid *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller) 36708c2ecf20Sopenharmony_ci{ 36718c2ecf20Sopenharmony_ci return __do_kmalloc(size, flags, caller); 36728c2ecf20Sopenharmony_ci} 36738c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__kmalloc_track_caller); 36748c2ecf20Sopenharmony_ci 36758c2ecf20Sopenharmony_ci/** 36768c2ecf20Sopenharmony_ci * kmem_cache_free - Deallocate an object 36778c2ecf20Sopenharmony_ci * @cachep: The cache the allocation was from. 36788c2ecf20Sopenharmony_ci * @objp: The previously allocated object. 36798c2ecf20Sopenharmony_ci * 36808c2ecf20Sopenharmony_ci * Free an object which was previously allocated from this 36818c2ecf20Sopenharmony_ci * cache. 36828c2ecf20Sopenharmony_ci */ 36838c2ecf20Sopenharmony_civoid kmem_cache_free(struct kmem_cache *cachep, void *objp) 36848c2ecf20Sopenharmony_ci{ 36858c2ecf20Sopenharmony_ci unsigned long flags; 36868c2ecf20Sopenharmony_ci cachep = cache_from_obj(cachep, objp); 36878c2ecf20Sopenharmony_ci if (!cachep) 36888c2ecf20Sopenharmony_ci return; 36898c2ecf20Sopenharmony_ci 36908c2ecf20Sopenharmony_ci local_irq_save(flags); 36918c2ecf20Sopenharmony_ci debug_check_no_locks_freed(objp, cachep->object_size); 36928c2ecf20Sopenharmony_ci if (!(cachep->flags & SLAB_DEBUG_OBJECTS)) 36938c2ecf20Sopenharmony_ci debug_check_no_obj_freed(objp, cachep->object_size); 36948c2ecf20Sopenharmony_ci __cache_free(cachep, objp, _RET_IP_); 36958c2ecf20Sopenharmony_ci local_irq_restore(flags); 36968c2ecf20Sopenharmony_ci 36978c2ecf20Sopenharmony_ci trace_kmem_cache_free(_RET_IP_, objp); 36988c2ecf20Sopenharmony_ci} 36998c2ecf20Sopenharmony_ciEXPORT_SYMBOL(kmem_cache_free); 37008c2ecf20Sopenharmony_ci 37018c2ecf20Sopenharmony_civoid kmem_cache_free_bulk(struct kmem_cache *orig_s, size_t size, void **p) 37028c2ecf20Sopenharmony_ci{ 37038c2ecf20Sopenharmony_ci struct kmem_cache *s; 37048c2ecf20Sopenharmony_ci size_t i; 37058c2ecf20Sopenharmony_ci 37068c2ecf20Sopenharmony_ci local_irq_disable(); 37078c2ecf20Sopenharmony_ci for (i = 0; i < size; i++) { 37088c2ecf20Sopenharmony_ci void *objp = p[i]; 37098c2ecf20Sopenharmony_ci 37108c2ecf20Sopenharmony_ci if (!orig_s) /* called via kfree_bulk */ 37118c2ecf20Sopenharmony_ci s = virt_to_cache(objp); 37128c2ecf20Sopenharmony_ci else 37138c2ecf20Sopenharmony_ci s = cache_from_obj(orig_s, objp); 37148c2ecf20Sopenharmony_ci if (!s) 37158c2ecf20Sopenharmony_ci continue; 37168c2ecf20Sopenharmony_ci 37178c2ecf20Sopenharmony_ci debug_check_no_locks_freed(objp, s->object_size); 37188c2ecf20Sopenharmony_ci if (!(s->flags & SLAB_DEBUG_OBJECTS)) 37198c2ecf20Sopenharmony_ci debug_check_no_obj_freed(objp, s->object_size); 37208c2ecf20Sopenharmony_ci 37218c2ecf20Sopenharmony_ci __cache_free(s, objp, _RET_IP_); 37228c2ecf20Sopenharmony_ci } 37238c2ecf20Sopenharmony_ci local_irq_enable(); 37248c2ecf20Sopenharmony_ci 37258c2ecf20Sopenharmony_ci /* FIXME: add tracing */ 37268c2ecf20Sopenharmony_ci} 37278c2ecf20Sopenharmony_ciEXPORT_SYMBOL(kmem_cache_free_bulk); 37288c2ecf20Sopenharmony_ci 37298c2ecf20Sopenharmony_ci/** 37308c2ecf20Sopenharmony_ci * kfree - free previously allocated memory 37318c2ecf20Sopenharmony_ci * @objp: pointer returned by kmalloc. 37328c2ecf20Sopenharmony_ci * 37338c2ecf20Sopenharmony_ci * If @objp is NULL, no operation is performed. 37348c2ecf20Sopenharmony_ci * 37358c2ecf20Sopenharmony_ci * Don't free memory not originally allocated by kmalloc() 37368c2ecf20Sopenharmony_ci * or you will run into trouble. 37378c2ecf20Sopenharmony_ci */ 37388c2ecf20Sopenharmony_civoid kfree(const void *objp) 37398c2ecf20Sopenharmony_ci{ 37408c2ecf20Sopenharmony_ci struct kmem_cache *c; 37418c2ecf20Sopenharmony_ci unsigned long flags; 37428c2ecf20Sopenharmony_ci 37438c2ecf20Sopenharmony_ci trace_kfree(_RET_IP_, objp); 37448c2ecf20Sopenharmony_ci 37458c2ecf20Sopenharmony_ci if (unlikely(ZERO_OR_NULL_PTR(objp))) 37468c2ecf20Sopenharmony_ci return; 37478c2ecf20Sopenharmony_ci local_irq_save(flags); 37488c2ecf20Sopenharmony_ci kfree_debugcheck(objp); 37498c2ecf20Sopenharmony_ci c = virt_to_cache(objp); 37508c2ecf20Sopenharmony_ci if (!c) { 37518c2ecf20Sopenharmony_ci local_irq_restore(flags); 37528c2ecf20Sopenharmony_ci return; 37538c2ecf20Sopenharmony_ci } 37548c2ecf20Sopenharmony_ci debug_check_no_locks_freed(objp, c->object_size); 37558c2ecf20Sopenharmony_ci 37568c2ecf20Sopenharmony_ci debug_check_no_obj_freed(objp, c->object_size); 37578c2ecf20Sopenharmony_ci __cache_free(c, (void *)objp, _RET_IP_); 37588c2ecf20Sopenharmony_ci local_irq_restore(flags); 37598c2ecf20Sopenharmony_ci} 37608c2ecf20Sopenharmony_ciEXPORT_SYMBOL(kfree); 37618c2ecf20Sopenharmony_ci 37628c2ecf20Sopenharmony_ci/* 37638c2ecf20Sopenharmony_ci * This initializes kmem_cache_node or resizes various caches for all nodes. 37648c2ecf20Sopenharmony_ci */ 37658c2ecf20Sopenharmony_cistatic int setup_kmem_cache_nodes(struct kmem_cache *cachep, gfp_t gfp) 37668c2ecf20Sopenharmony_ci{ 37678c2ecf20Sopenharmony_ci int ret; 37688c2ecf20Sopenharmony_ci int node; 37698c2ecf20Sopenharmony_ci struct kmem_cache_node *n; 37708c2ecf20Sopenharmony_ci 37718c2ecf20Sopenharmony_ci for_each_online_node(node) { 37728c2ecf20Sopenharmony_ci ret = setup_kmem_cache_node(cachep, node, gfp, true); 37738c2ecf20Sopenharmony_ci if (ret) 37748c2ecf20Sopenharmony_ci goto fail; 37758c2ecf20Sopenharmony_ci 37768c2ecf20Sopenharmony_ci } 37778c2ecf20Sopenharmony_ci 37788c2ecf20Sopenharmony_ci return 0; 37798c2ecf20Sopenharmony_ci 37808c2ecf20Sopenharmony_cifail: 37818c2ecf20Sopenharmony_ci if (!cachep->list.next) { 37828c2ecf20Sopenharmony_ci /* Cache is not active yet. Roll back what we did */ 37838c2ecf20Sopenharmony_ci node--; 37848c2ecf20Sopenharmony_ci while (node >= 0) { 37858c2ecf20Sopenharmony_ci n = get_node(cachep, node); 37868c2ecf20Sopenharmony_ci if (n) { 37878c2ecf20Sopenharmony_ci kfree(n->shared); 37888c2ecf20Sopenharmony_ci free_alien_cache(n->alien); 37898c2ecf20Sopenharmony_ci kfree(n); 37908c2ecf20Sopenharmony_ci cachep->node[node] = NULL; 37918c2ecf20Sopenharmony_ci } 37928c2ecf20Sopenharmony_ci node--; 37938c2ecf20Sopenharmony_ci } 37948c2ecf20Sopenharmony_ci } 37958c2ecf20Sopenharmony_ci return -ENOMEM; 37968c2ecf20Sopenharmony_ci} 37978c2ecf20Sopenharmony_ci 37988c2ecf20Sopenharmony_ci/* Always called with the slab_mutex held */ 37998c2ecf20Sopenharmony_cistatic int do_tune_cpucache(struct kmem_cache *cachep, int limit, 38008c2ecf20Sopenharmony_ci int batchcount, int shared, gfp_t gfp) 38018c2ecf20Sopenharmony_ci{ 38028c2ecf20Sopenharmony_ci struct array_cache __percpu *cpu_cache, *prev; 38038c2ecf20Sopenharmony_ci int cpu; 38048c2ecf20Sopenharmony_ci 38058c2ecf20Sopenharmony_ci cpu_cache = alloc_kmem_cache_cpus(cachep, limit, batchcount); 38068c2ecf20Sopenharmony_ci if (!cpu_cache) 38078c2ecf20Sopenharmony_ci return -ENOMEM; 38088c2ecf20Sopenharmony_ci 38098c2ecf20Sopenharmony_ci prev = cachep->cpu_cache; 38108c2ecf20Sopenharmony_ci cachep->cpu_cache = cpu_cache; 38118c2ecf20Sopenharmony_ci /* 38128c2ecf20Sopenharmony_ci * Without a previous cpu_cache there's no need to synchronize remote 38138c2ecf20Sopenharmony_ci * cpus, so skip the IPIs. 38148c2ecf20Sopenharmony_ci */ 38158c2ecf20Sopenharmony_ci if (prev) 38168c2ecf20Sopenharmony_ci kick_all_cpus_sync(); 38178c2ecf20Sopenharmony_ci 38188c2ecf20Sopenharmony_ci check_irq_on(); 38198c2ecf20Sopenharmony_ci cachep->batchcount = batchcount; 38208c2ecf20Sopenharmony_ci cachep->limit = limit; 38218c2ecf20Sopenharmony_ci cachep->shared = shared; 38228c2ecf20Sopenharmony_ci 38238c2ecf20Sopenharmony_ci if (!prev) 38248c2ecf20Sopenharmony_ci goto setup_node; 38258c2ecf20Sopenharmony_ci 38268c2ecf20Sopenharmony_ci for_each_online_cpu(cpu) { 38278c2ecf20Sopenharmony_ci LIST_HEAD(list); 38288c2ecf20Sopenharmony_ci int node; 38298c2ecf20Sopenharmony_ci struct kmem_cache_node *n; 38308c2ecf20Sopenharmony_ci struct array_cache *ac = per_cpu_ptr(prev, cpu); 38318c2ecf20Sopenharmony_ci 38328c2ecf20Sopenharmony_ci node = cpu_to_mem(cpu); 38338c2ecf20Sopenharmony_ci n = get_node(cachep, node); 38348c2ecf20Sopenharmony_ci spin_lock_irq(&n->list_lock); 38358c2ecf20Sopenharmony_ci free_block(cachep, ac->entry, ac->avail, node, &list); 38368c2ecf20Sopenharmony_ci spin_unlock_irq(&n->list_lock); 38378c2ecf20Sopenharmony_ci slabs_destroy(cachep, &list); 38388c2ecf20Sopenharmony_ci } 38398c2ecf20Sopenharmony_ci free_percpu(prev); 38408c2ecf20Sopenharmony_ci 38418c2ecf20Sopenharmony_cisetup_node: 38428c2ecf20Sopenharmony_ci return setup_kmem_cache_nodes(cachep, gfp); 38438c2ecf20Sopenharmony_ci} 38448c2ecf20Sopenharmony_ci 38458c2ecf20Sopenharmony_ci/* Called with slab_mutex held always */ 38468c2ecf20Sopenharmony_cistatic int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp) 38478c2ecf20Sopenharmony_ci{ 38488c2ecf20Sopenharmony_ci int err; 38498c2ecf20Sopenharmony_ci int limit = 0; 38508c2ecf20Sopenharmony_ci int shared = 0; 38518c2ecf20Sopenharmony_ci int batchcount = 0; 38528c2ecf20Sopenharmony_ci 38538c2ecf20Sopenharmony_ci err = cache_random_seq_create(cachep, cachep->num, gfp); 38548c2ecf20Sopenharmony_ci if (err) 38558c2ecf20Sopenharmony_ci goto end; 38568c2ecf20Sopenharmony_ci 38578c2ecf20Sopenharmony_ci if (limit && shared && batchcount) 38588c2ecf20Sopenharmony_ci goto skip_setup; 38598c2ecf20Sopenharmony_ci /* 38608c2ecf20Sopenharmony_ci * The head array serves three purposes: 38618c2ecf20Sopenharmony_ci * - create a LIFO ordering, i.e. return objects that are cache-warm 38628c2ecf20Sopenharmony_ci * - reduce the number of spinlock operations. 38638c2ecf20Sopenharmony_ci * - reduce the number of linked list operations on the slab and 38648c2ecf20Sopenharmony_ci * bufctl chains: array operations are cheaper. 38658c2ecf20Sopenharmony_ci * The numbers are guessed, we should auto-tune as described by 38668c2ecf20Sopenharmony_ci * Bonwick. 38678c2ecf20Sopenharmony_ci */ 38688c2ecf20Sopenharmony_ci if (cachep->size > 131072) 38698c2ecf20Sopenharmony_ci limit = 1; 38708c2ecf20Sopenharmony_ci else if (cachep->size > PAGE_SIZE) 38718c2ecf20Sopenharmony_ci limit = 8; 38728c2ecf20Sopenharmony_ci else if (cachep->size > 1024) 38738c2ecf20Sopenharmony_ci limit = 24; 38748c2ecf20Sopenharmony_ci else if (cachep->size > 256) 38758c2ecf20Sopenharmony_ci limit = 54; 38768c2ecf20Sopenharmony_ci else 38778c2ecf20Sopenharmony_ci limit = 120; 38788c2ecf20Sopenharmony_ci 38798c2ecf20Sopenharmony_ci /* 38808c2ecf20Sopenharmony_ci * CPU bound tasks (e.g. network routing) can exhibit cpu bound 38818c2ecf20Sopenharmony_ci * allocation behaviour: Most allocs on one cpu, most free operations 38828c2ecf20Sopenharmony_ci * on another cpu. For these cases, an efficient object passing between 38838c2ecf20Sopenharmony_ci * cpus is necessary. This is provided by a shared array. The array 38848c2ecf20Sopenharmony_ci * replaces Bonwick's magazine layer. 38858c2ecf20Sopenharmony_ci * On uniprocessor, it's functionally equivalent (but less efficient) 38868c2ecf20Sopenharmony_ci * to a larger limit. Thus disabled by default. 38878c2ecf20Sopenharmony_ci */ 38888c2ecf20Sopenharmony_ci shared = 0; 38898c2ecf20Sopenharmony_ci if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1) 38908c2ecf20Sopenharmony_ci shared = 8; 38918c2ecf20Sopenharmony_ci 38928c2ecf20Sopenharmony_ci#if DEBUG 38938c2ecf20Sopenharmony_ci /* 38948c2ecf20Sopenharmony_ci * With debugging enabled, large batchcount lead to excessively long 38958c2ecf20Sopenharmony_ci * periods with disabled local interrupts. Limit the batchcount 38968c2ecf20Sopenharmony_ci */ 38978c2ecf20Sopenharmony_ci if (limit > 32) 38988c2ecf20Sopenharmony_ci limit = 32; 38998c2ecf20Sopenharmony_ci#endif 39008c2ecf20Sopenharmony_ci batchcount = (limit + 1) / 2; 39018c2ecf20Sopenharmony_ciskip_setup: 39028c2ecf20Sopenharmony_ci err = do_tune_cpucache(cachep, limit, batchcount, shared, gfp); 39038c2ecf20Sopenharmony_ciend: 39048c2ecf20Sopenharmony_ci if (err) 39058c2ecf20Sopenharmony_ci pr_err("enable_cpucache failed for %s, error %d\n", 39068c2ecf20Sopenharmony_ci cachep->name, -err); 39078c2ecf20Sopenharmony_ci return err; 39088c2ecf20Sopenharmony_ci} 39098c2ecf20Sopenharmony_ci 39108c2ecf20Sopenharmony_ci/* 39118c2ecf20Sopenharmony_ci * Drain an array if it contains any elements taking the node lock only if 39128c2ecf20Sopenharmony_ci * necessary. Note that the node listlock also protects the array_cache 39138c2ecf20Sopenharmony_ci * if drain_array() is used on the shared array. 39148c2ecf20Sopenharmony_ci */ 39158c2ecf20Sopenharmony_cistatic void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n, 39168c2ecf20Sopenharmony_ci struct array_cache *ac, int node) 39178c2ecf20Sopenharmony_ci{ 39188c2ecf20Sopenharmony_ci LIST_HEAD(list); 39198c2ecf20Sopenharmony_ci 39208c2ecf20Sopenharmony_ci /* ac from n->shared can be freed if we don't hold the slab_mutex. */ 39218c2ecf20Sopenharmony_ci check_mutex_acquired(); 39228c2ecf20Sopenharmony_ci 39238c2ecf20Sopenharmony_ci if (!ac || !ac->avail) 39248c2ecf20Sopenharmony_ci return; 39258c2ecf20Sopenharmony_ci 39268c2ecf20Sopenharmony_ci if (ac->touched) { 39278c2ecf20Sopenharmony_ci ac->touched = 0; 39288c2ecf20Sopenharmony_ci return; 39298c2ecf20Sopenharmony_ci } 39308c2ecf20Sopenharmony_ci 39318c2ecf20Sopenharmony_ci spin_lock_irq(&n->list_lock); 39328c2ecf20Sopenharmony_ci drain_array_locked(cachep, ac, node, false, &list); 39338c2ecf20Sopenharmony_ci spin_unlock_irq(&n->list_lock); 39348c2ecf20Sopenharmony_ci 39358c2ecf20Sopenharmony_ci slabs_destroy(cachep, &list); 39368c2ecf20Sopenharmony_ci} 39378c2ecf20Sopenharmony_ci 39388c2ecf20Sopenharmony_ci/** 39398c2ecf20Sopenharmony_ci * cache_reap - Reclaim memory from caches. 39408c2ecf20Sopenharmony_ci * @w: work descriptor 39418c2ecf20Sopenharmony_ci * 39428c2ecf20Sopenharmony_ci * Called from workqueue/eventd every few seconds. 39438c2ecf20Sopenharmony_ci * Purpose: 39448c2ecf20Sopenharmony_ci * - clear the per-cpu caches for this CPU. 39458c2ecf20Sopenharmony_ci * - return freeable pages to the main free memory pool. 39468c2ecf20Sopenharmony_ci * 39478c2ecf20Sopenharmony_ci * If we cannot acquire the cache chain mutex then just give up - we'll try 39488c2ecf20Sopenharmony_ci * again on the next iteration. 39498c2ecf20Sopenharmony_ci */ 39508c2ecf20Sopenharmony_cistatic void cache_reap(struct work_struct *w) 39518c2ecf20Sopenharmony_ci{ 39528c2ecf20Sopenharmony_ci struct kmem_cache *searchp; 39538c2ecf20Sopenharmony_ci struct kmem_cache_node *n; 39548c2ecf20Sopenharmony_ci int node = numa_mem_id(); 39558c2ecf20Sopenharmony_ci struct delayed_work *work = to_delayed_work(w); 39568c2ecf20Sopenharmony_ci 39578c2ecf20Sopenharmony_ci if (!mutex_trylock(&slab_mutex)) 39588c2ecf20Sopenharmony_ci /* Give up. Setup the next iteration. */ 39598c2ecf20Sopenharmony_ci goto out; 39608c2ecf20Sopenharmony_ci 39618c2ecf20Sopenharmony_ci list_for_each_entry(searchp, &slab_caches, list) { 39628c2ecf20Sopenharmony_ci check_irq_on(); 39638c2ecf20Sopenharmony_ci 39648c2ecf20Sopenharmony_ci /* 39658c2ecf20Sopenharmony_ci * We only take the node lock if absolutely necessary and we 39668c2ecf20Sopenharmony_ci * have established with reasonable certainty that 39678c2ecf20Sopenharmony_ci * we can do some work if the lock was obtained. 39688c2ecf20Sopenharmony_ci */ 39698c2ecf20Sopenharmony_ci n = get_node(searchp, node); 39708c2ecf20Sopenharmony_ci 39718c2ecf20Sopenharmony_ci reap_alien(searchp, n); 39728c2ecf20Sopenharmony_ci 39738c2ecf20Sopenharmony_ci drain_array(searchp, n, cpu_cache_get(searchp), node); 39748c2ecf20Sopenharmony_ci 39758c2ecf20Sopenharmony_ci /* 39768c2ecf20Sopenharmony_ci * These are racy checks but it does not matter 39778c2ecf20Sopenharmony_ci * if we skip one check or scan twice. 39788c2ecf20Sopenharmony_ci */ 39798c2ecf20Sopenharmony_ci if (time_after(n->next_reap, jiffies)) 39808c2ecf20Sopenharmony_ci goto next; 39818c2ecf20Sopenharmony_ci 39828c2ecf20Sopenharmony_ci n->next_reap = jiffies + REAPTIMEOUT_NODE; 39838c2ecf20Sopenharmony_ci 39848c2ecf20Sopenharmony_ci drain_array(searchp, n, n->shared, node); 39858c2ecf20Sopenharmony_ci 39868c2ecf20Sopenharmony_ci if (n->free_touched) 39878c2ecf20Sopenharmony_ci n->free_touched = 0; 39888c2ecf20Sopenharmony_ci else { 39898c2ecf20Sopenharmony_ci int freed; 39908c2ecf20Sopenharmony_ci 39918c2ecf20Sopenharmony_ci freed = drain_freelist(searchp, n, (n->free_limit + 39928c2ecf20Sopenharmony_ci 5 * searchp->num - 1) / (5 * searchp->num)); 39938c2ecf20Sopenharmony_ci STATS_ADD_REAPED(searchp, freed); 39948c2ecf20Sopenharmony_ci } 39958c2ecf20Sopenharmony_cinext: 39968c2ecf20Sopenharmony_ci cond_resched(); 39978c2ecf20Sopenharmony_ci } 39988c2ecf20Sopenharmony_ci check_irq_on(); 39998c2ecf20Sopenharmony_ci mutex_unlock(&slab_mutex); 40008c2ecf20Sopenharmony_ci next_reap_node(); 40018c2ecf20Sopenharmony_ciout: 40028c2ecf20Sopenharmony_ci /* Set up the next iteration */ 40038c2ecf20Sopenharmony_ci schedule_delayed_work_on(smp_processor_id(), work, 40048c2ecf20Sopenharmony_ci round_jiffies_relative(REAPTIMEOUT_AC)); 40058c2ecf20Sopenharmony_ci} 40068c2ecf20Sopenharmony_ci 40078c2ecf20Sopenharmony_civoid get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo) 40088c2ecf20Sopenharmony_ci{ 40098c2ecf20Sopenharmony_ci unsigned long active_objs, num_objs, active_slabs; 40108c2ecf20Sopenharmony_ci unsigned long total_slabs = 0, free_objs = 0, shared_avail = 0; 40118c2ecf20Sopenharmony_ci unsigned long free_slabs = 0; 40128c2ecf20Sopenharmony_ci int node; 40138c2ecf20Sopenharmony_ci struct kmem_cache_node *n; 40148c2ecf20Sopenharmony_ci 40158c2ecf20Sopenharmony_ci for_each_kmem_cache_node(cachep, node, n) { 40168c2ecf20Sopenharmony_ci check_irq_on(); 40178c2ecf20Sopenharmony_ci spin_lock_irq(&n->list_lock); 40188c2ecf20Sopenharmony_ci 40198c2ecf20Sopenharmony_ci total_slabs += n->total_slabs; 40208c2ecf20Sopenharmony_ci free_slabs += n->free_slabs; 40218c2ecf20Sopenharmony_ci free_objs += n->free_objects; 40228c2ecf20Sopenharmony_ci 40238c2ecf20Sopenharmony_ci if (n->shared) 40248c2ecf20Sopenharmony_ci shared_avail += n->shared->avail; 40258c2ecf20Sopenharmony_ci 40268c2ecf20Sopenharmony_ci spin_unlock_irq(&n->list_lock); 40278c2ecf20Sopenharmony_ci } 40288c2ecf20Sopenharmony_ci num_objs = total_slabs * cachep->num; 40298c2ecf20Sopenharmony_ci active_slabs = total_slabs - free_slabs; 40308c2ecf20Sopenharmony_ci active_objs = num_objs - free_objs; 40318c2ecf20Sopenharmony_ci 40328c2ecf20Sopenharmony_ci sinfo->active_objs = active_objs; 40338c2ecf20Sopenharmony_ci sinfo->num_objs = num_objs; 40348c2ecf20Sopenharmony_ci sinfo->active_slabs = active_slabs; 40358c2ecf20Sopenharmony_ci sinfo->num_slabs = total_slabs; 40368c2ecf20Sopenharmony_ci sinfo->shared_avail = shared_avail; 40378c2ecf20Sopenharmony_ci sinfo->limit = cachep->limit; 40388c2ecf20Sopenharmony_ci sinfo->batchcount = cachep->batchcount; 40398c2ecf20Sopenharmony_ci sinfo->shared = cachep->shared; 40408c2ecf20Sopenharmony_ci sinfo->objects_per_slab = cachep->num; 40418c2ecf20Sopenharmony_ci sinfo->cache_order = cachep->gfporder; 40428c2ecf20Sopenharmony_ci} 40438c2ecf20Sopenharmony_ci 40448c2ecf20Sopenharmony_civoid slabinfo_show_stats(struct seq_file *m, struct kmem_cache *cachep) 40458c2ecf20Sopenharmony_ci{ 40468c2ecf20Sopenharmony_ci#if STATS 40478c2ecf20Sopenharmony_ci { /* node stats */ 40488c2ecf20Sopenharmony_ci unsigned long high = cachep->high_mark; 40498c2ecf20Sopenharmony_ci unsigned long allocs = cachep->num_allocations; 40508c2ecf20Sopenharmony_ci unsigned long grown = cachep->grown; 40518c2ecf20Sopenharmony_ci unsigned long reaped = cachep->reaped; 40528c2ecf20Sopenharmony_ci unsigned long errors = cachep->errors; 40538c2ecf20Sopenharmony_ci unsigned long max_freeable = cachep->max_freeable; 40548c2ecf20Sopenharmony_ci unsigned long node_allocs = cachep->node_allocs; 40558c2ecf20Sopenharmony_ci unsigned long node_frees = cachep->node_frees; 40568c2ecf20Sopenharmony_ci unsigned long overflows = cachep->node_overflow; 40578c2ecf20Sopenharmony_ci 40588c2ecf20Sopenharmony_ci seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu %4lu %4lu %4lu %4lu %4lu", 40598c2ecf20Sopenharmony_ci allocs, high, grown, 40608c2ecf20Sopenharmony_ci reaped, errors, max_freeable, node_allocs, 40618c2ecf20Sopenharmony_ci node_frees, overflows); 40628c2ecf20Sopenharmony_ci } 40638c2ecf20Sopenharmony_ci /* cpu stats */ 40648c2ecf20Sopenharmony_ci { 40658c2ecf20Sopenharmony_ci unsigned long allochit = atomic_read(&cachep->allochit); 40668c2ecf20Sopenharmony_ci unsigned long allocmiss = atomic_read(&cachep->allocmiss); 40678c2ecf20Sopenharmony_ci unsigned long freehit = atomic_read(&cachep->freehit); 40688c2ecf20Sopenharmony_ci unsigned long freemiss = atomic_read(&cachep->freemiss); 40698c2ecf20Sopenharmony_ci 40708c2ecf20Sopenharmony_ci seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu", 40718c2ecf20Sopenharmony_ci allochit, allocmiss, freehit, freemiss); 40728c2ecf20Sopenharmony_ci } 40738c2ecf20Sopenharmony_ci#endif 40748c2ecf20Sopenharmony_ci} 40758c2ecf20Sopenharmony_ci 40768c2ecf20Sopenharmony_ci#define MAX_SLABINFO_WRITE 128 40778c2ecf20Sopenharmony_ci/** 40788c2ecf20Sopenharmony_ci * slabinfo_write - Tuning for the slab allocator 40798c2ecf20Sopenharmony_ci * @file: unused 40808c2ecf20Sopenharmony_ci * @buffer: user buffer 40818c2ecf20Sopenharmony_ci * @count: data length 40828c2ecf20Sopenharmony_ci * @ppos: unused 40838c2ecf20Sopenharmony_ci * 40848c2ecf20Sopenharmony_ci * Return: %0 on success, negative error code otherwise. 40858c2ecf20Sopenharmony_ci */ 40868c2ecf20Sopenharmony_cissize_t slabinfo_write(struct file *file, const char __user *buffer, 40878c2ecf20Sopenharmony_ci size_t count, loff_t *ppos) 40888c2ecf20Sopenharmony_ci{ 40898c2ecf20Sopenharmony_ci char kbuf[MAX_SLABINFO_WRITE + 1], *tmp; 40908c2ecf20Sopenharmony_ci int limit, batchcount, shared, res; 40918c2ecf20Sopenharmony_ci struct kmem_cache *cachep; 40928c2ecf20Sopenharmony_ci 40938c2ecf20Sopenharmony_ci if (count > MAX_SLABINFO_WRITE) 40948c2ecf20Sopenharmony_ci return -EINVAL; 40958c2ecf20Sopenharmony_ci if (copy_from_user(&kbuf, buffer, count)) 40968c2ecf20Sopenharmony_ci return -EFAULT; 40978c2ecf20Sopenharmony_ci kbuf[MAX_SLABINFO_WRITE] = '\0'; 40988c2ecf20Sopenharmony_ci 40998c2ecf20Sopenharmony_ci tmp = strchr(kbuf, ' '); 41008c2ecf20Sopenharmony_ci if (!tmp) 41018c2ecf20Sopenharmony_ci return -EINVAL; 41028c2ecf20Sopenharmony_ci *tmp = '\0'; 41038c2ecf20Sopenharmony_ci tmp++; 41048c2ecf20Sopenharmony_ci if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3) 41058c2ecf20Sopenharmony_ci return -EINVAL; 41068c2ecf20Sopenharmony_ci 41078c2ecf20Sopenharmony_ci /* Find the cache in the chain of caches. */ 41088c2ecf20Sopenharmony_ci mutex_lock(&slab_mutex); 41098c2ecf20Sopenharmony_ci res = -EINVAL; 41108c2ecf20Sopenharmony_ci list_for_each_entry(cachep, &slab_caches, list) { 41118c2ecf20Sopenharmony_ci if (!strcmp(cachep->name, kbuf)) { 41128c2ecf20Sopenharmony_ci if (limit < 1 || batchcount < 1 || 41138c2ecf20Sopenharmony_ci batchcount > limit || shared < 0) { 41148c2ecf20Sopenharmony_ci res = 0; 41158c2ecf20Sopenharmony_ci } else { 41168c2ecf20Sopenharmony_ci res = do_tune_cpucache(cachep, limit, 41178c2ecf20Sopenharmony_ci batchcount, shared, 41188c2ecf20Sopenharmony_ci GFP_KERNEL); 41198c2ecf20Sopenharmony_ci } 41208c2ecf20Sopenharmony_ci break; 41218c2ecf20Sopenharmony_ci } 41228c2ecf20Sopenharmony_ci } 41238c2ecf20Sopenharmony_ci mutex_unlock(&slab_mutex); 41248c2ecf20Sopenharmony_ci if (res >= 0) 41258c2ecf20Sopenharmony_ci res = count; 41268c2ecf20Sopenharmony_ci return res; 41278c2ecf20Sopenharmony_ci} 41288c2ecf20Sopenharmony_ci 41298c2ecf20Sopenharmony_ci#ifdef CONFIG_HARDENED_USERCOPY 41308c2ecf20Sopenharmony_ci/* 41318c2ecf20Sopenharmony_ci * Rejects incorrectly sized objects and objects that are to be copied 41328c2ecf20Sopenharmony_ci * to/from userspace but do not fall entirely within the containing slab 41338c2ecf20Sopenharmony_ci * cache's usercopy region. 41348c2ecf20Sopenharmony_ci * 41358c2ecf20Sopenharmony_ci * Returns NULL if check passes, otherwise const char * to name of cache 41368c2ecf20Sopenharmony_ci * to indicate an error. 41378c2ecf20Sopenharmony_ci */ 41388c2ecf20Sopenharmony_civoid __check_heap_object(const void *ptr, unsigned long n, struct page *page, 41398c2ecf20Sopenharmony_ci bool to_user) 41408c2ecf20Sopenharmony_ci{ 41418c2ecf20Sopenharmony_ci struct kmem_cache *cachep; 41428c2ecf20Sopenharmony_ci unsigned int objnr; 41438c2ecf20Sopenharmony_ci unsigned long offset; 41448c2ecf20Sopenharmony_ci 41458c2ecf20Sopenharmony_ci ptr = kasan_reset_tag(ptr); 41468c2ecf20Sopenharmony_ci 41478c2ecf20Sopenharmony_ci /* Find and validate object. */ 41488c2ecf20Sopenharmony_ci cachep = page->slab_cache; 41498c2ecf20Sopenharmony_ci objnr = obj_to_index(cachep, page, (void *)ptr); 41508c2ecf20Sopenharmony_ci BUG_ON(objnr >= cachep->num); 41518c2ecf20Sopenharmony_ci 41528c2ecf20Sopenharmony_ci /* Find offset within object. */ 41538c2ecf20Sopenharmony_ci offset = ptr - index_to_obj(cachep, page, objnr) - obj_offset(cachep); 41548c2ecf20Sopenharmony_ci 41558c2ecf20Sopenharmony_ci /* Allow address range falling entirely within usercopy region. */ 41568c2ecf20Sopenharmony_ci if (offset >= cachep->useroffset && 41578c2ecf20Sopenharmony_ci offset - cachep->useroffset <= cachep->usersize && 41588c2ecf20Sopenharmony_ci n <= cachep->useroffset - offset + cachep->usersize) 41598c2ecf20Sopenharmony_ci return; 41608c2ecf20Sopenharmony_ci 41618c2ecf20Sopenharmony_ci /* 41628c2ecf20Sopenharmony_ci * If the copy is still within the allocated object, produce 41638c2ecf20Sopenharmony_ci * a warning instead of rejecting the copy. This is intended 41648c2ecf20Sopenharmony_ci * to be a temporary method to find any missing usercopy 41658c2ecf20Sopenharmony_ci * whitelists. 41668c2ecf20Sopenharmony_ci */ 41678c2ecf20Sopenharmony_ci if (usercopy_fallback && 41688c2ecf20Sopenharmony_ci offset <= cachep->object_size && 41698c2ecf20Sopenharmony_ci n <= cachep->object_size - offset) { 41708c2ecf20Sopenharmony_ci usercopy_warn("SLAB object", cachep->name, to_user, offset, n); 41718c2ecf20Sopenharmony_ci return; 41728c2ecf20Sopenharmony_ci } 41738c2ecf20Sopenharmony_ci 41748c2ecf20Sopenharmony_ci usercopy_abort("SLAB object", cachep->name, to_user, offset, n); 41758c2ecf20Sopenharmony_ci} 41768c2ecf20Sopenharmony_ci#endif /* CONFIG_HARDENED_USERCOPY */ 41778c2ecf20Sopenharmony_ci 41788c2ecf20Sopenharmony_ci/** 41798c2ecf20Sopenharmony_ci * __ksize -- Uninstrumented ksize. 41808c2ecf20Sopenharmony_ci * @objp: pointer to the object 41818c2ecf20Sopenharmony_ci * 41828c2ecf20Sopenharmony_ci * Unlike ksize(), __ksize() is uninstrumented, and does not provide the same 41838c2ecf20Sopenharmony_ci * safety checks as ksize() with KASAN instrumentation enabled. 41848c2ecf20Sopenharmony_ci * 41858c2ecf20Sopenharmony_ci * Return: size of the actual memory used by @objp in bytes 41868c2ecf20Sopenharmony_ci */ 41878c2ecf20Sopenharmony_cisize_t __ksize(const void *objp) 41888c2ecf20Sopenharmony_ci{ 41898c2ecf20Sopenharmony_ci struct kmem_cache *c; 41908c2ecf20Sopenharmony_ci size_t size; 41918c2ecf20Sopenharmony_ci 41928c2ecf20Sopenharmony_ci BUG_ON(!objp); 41938c2ecf20Sopenharmony_ci if (unlikely(objp == ZERO_SIZE_PTR)) 41948c2ecf20Sopenharmony_ci return 0; 41958c2ecf20Sopenharmony_ci 41968c2ecf20Sopenharmony_ci c = virt_to_cache(objp); 41978c2ecf20Sopenharmony_ci size = c ? c->object_size : 0; 41988c2ecf20Sopenharmony_ci 41998c2ecf20Sopenharmony_ci return size; 42008c2ecf20Sopenharmony_ci} 42018c2ecf20Sopenharmony_ciEXPORT_SYMBOL(__ksize); 4202