Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/mm/slab.c |
| 3 | * Written by Mark Hemment, 1996/97. |
| 4 | * (markhe@nextd.demon.co.uk) |
| 5 | * |
| 6 | * kmem_cache_destroy() + some cleanup - 1999 Andrea Arcangeli |
| 7 | * |
| 8 | * Major cleanup, different bufctl logic, per-cpu arrays |
| 9 | * (c) 2000 Manfred Spraul |
| 10 | * |
| 11 | * Cleanup, make the head arrays unconditional, preparation for NUMA |
| 12 | * (c) 2002 Manfred Spraul |
| 13 | * |
| 14 | * An implementation of the Slab Allocator as described in outline in; |
| 15 | * UNIX Internals: The New Frontiers by Uresh Vahalia |
| 16 | * Pub: Prentice Hall ISBN 0-13-101908-2 |
| 17 | * or with a little more detail in; |
| 18 | * The Slab Allocator: An Object-Caching Kernel Memory Allocator |
| 19 | * Jeff Bonwick (Sun Microsystems). |
| 20 | * Presented at: USENIX Summer 1994 Technical Conference |
| 21 | * |
| 22 | * The memory is organized in caches, one cache for each object type. |
| 23 | * (e.g. inode_cache, dentry_cache, buffer_head, vm_area_struct) |
| 24 | * Each cache consists out of many slabs (they are small (usually one |
| 25 | * page long) and always contiguous), and each slab contains multiple |
| 26 | * initialized objects. |
| 27 | * |
| 28 | * This means, that your constructor is used only for newly allocated |
Simon Arlott | 183ff22 | 2007-10-20 01:27:18 +0200 | [diff] [blame] | 29 | * slabs and you must pass objects with the same initializations to |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | * kmem_cache_free. |
| 31 | * |
| 32 | * Each cache can only support one memory type (GFP_DMA, GFP_HIGHMEM, |
| 33 | * normal). If you need a special memory type, then must create a new |
| 34 | * cache for that memory type. |
| 35 | * |
| 36 | * In order to reduce fragmentation, the slabs are sorted in 3 groups: |
| 37 | * full slabs with 0 free objects |
| 38 | * partial slabs |
| 39 | * empty slabs with no allocated objects |
| 40 | * |
| 41 | * If partial slabs exist, then new allocations come from these slabs, |
| 42 | * otherwise from empty slabs or new slabs are allocated. |
| 43 | * |
| 44 | * kmem_cache_destroy() CAN CRASH if you try to allocate from the cache |
| 45 | * during kmem_cache_destroy(). The caller must prevent concurrent allocs. |
| 46 | * |
| 47 | * Each cache has a short per-cpu head array, most allocs |
| 48 | * and frees go into that array, and if that array overflows, then 1/2 |
| 49 | * of the entries in the array are given back into the global cache. |
| 50 | * The head array is strictly LIFO and should improve the cache hit rates. |
| 51 | * On SMP, it additionally reduces the spinlock operations. |
| 52 | * |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 53 | * The c_cpuarray may not be read with enabled local interrupts - |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | * it's changed with a smp_call_function(). |
| 55 | * |
| 56 | * SMP synchronization: |
| 57 | * constructors and destructors are called without any locking. |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 58 | * Several members in struct kmem_cache and struct slab never change, they |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | * are accessed without any locking. |
| 60 | * The per-cpu arrays are never accessed from the wrong cpu, no locking, |
| 61 | * and local interrupts are disabled so slab code is preempt-safe. |
| 62 | * The non-constant members are protected with a per-cache irq spinlock. |
| 63 | * |
| 64 | * Many thanks to Mark Hemment, who wrote another per-cpu slab patch |
| 65 | * in 2000 - many ideas in the current implementation are derived from |
| 66 | * his patch. |
| 67 | * |
| 68 | * Further notes from the original documentation: |
| 69 | * |
| 70 | * 11 April '97. Started multi-threading - markhe |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 71 | * The global cache-chain is protected by the mutex 'slab_mutex'. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | * The sem is only needed when accessing/extending the cache-chain, which |
| 73 | * can never happen inside an interrupt (kmem_cache_create(), |
| 74 | * kmem_cache_shrink() and kmem_cache_reap()). |
| 75 | * |
| 76 | * At present, each engine can be growing a cache. This should be blocked. |
| 77 | * |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 78 | * 15 March 2005. NUMA slab allocator. |
| 79 | * Shai Fultheim <shai@scalex86.org>. |
| 80 | * Shobhit Dayal <shobhit@calsoftinc.com> |
| 81 | * Alok N Kataria <alokk@calsoftinc.com> |
| 82 | * Christoph Lameter <christoph@lameter.com> |
| 83 | * |
| 84 | * Modified the slab allocator to be node aware on NUMA systems. |
| 85 | * Each node has its own list of partial, free and full slabs. |
| 86 | * All object allocations for a node occur from node specific slab lists. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | */ |
| 88 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | #include <linux/slab.h> |
| 90 | #include <linux/mm.h> |
Randy Dunlap | c9cf552 | 2006-06-27 02:53:52 -0700 | [diff] [blame] | 91 | #include <linux/poison.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | #include <linux/swap.h> |
| 93 | #include <linux/cache.h> |
| 94 | #include <linux/interrupt.h> |
| 95 | #include <linux/init.h> |
| 96 | #include <linux/compiler.h> |
Paul Jackson | 101a500 | 2006-03-24 03:16:07 -0800 | [diff] [blame] | 97 | #include <linux/cpuset.h> |
Alexey Dobriyan | a0ec95a | 2008-10-06 00:59:10 +0400 | [diff] [blame] | 98 | #include <linux/proc_fs.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | #include <linux/seq_file.h> |
| 100 | #include <linux/notifier.h> |
| 101 | #include <linux/kallsyms.h> |
| 102 | #include <linux/cpu.h> |
| 103 | #include <linux/sysctl.h> |
| 104 | #include <linux/module.h> |
| 105 | #include <linux/rcupdate.h> |
Paulo Marques | 543537b | 2005-06-23 00:09:02 -0700 | [diff] [blame] | 106 | #include <linux/string.h> |
Andrew Morton | 138ae66 | 2006-12-06 20:36:41 -0800 | [diff] [blame] | 107 | #include <linux/uaccess.h> |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 108 | #include <linux/nodemask.h> |
Catalin Marinas | d5cff63 | 2009-06-11 13:22:40 +0100 | [diff] [blame] | 109 | #include <linux/kmemleak.h> |
Christoph Lameter | dc85da1 | 2006-01-18 17:42:36 -0800 | [diff] [blame] | 110 | #include <linux/mempolicy.h> |
Ingo Molnar | fc0abb1 | 2006-01-18 17:42:33 -0800 | [diff] [blame] | 111 | #include <linux/mutex.h> |
Akinobu Mita | 8a8b650 | 2006-12-08 02:39:44 -0800 | [diff] [blame] | 112 | #include <linux/fault-inject.h> |
Ingo Molnar | e7eebaf | 2006-06-27 02:54:55 -0700 | [diff] [blame] | 113 | #include <linux/rtmutex.h> |
Eric Dumazet | 6a2d7a9 | 2006-12-13 00:34:27 -0800 | [diff] [blame] | 114 | #include <linux/reciprocal_div.h> |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 115 | #include <linux/debugobjects.h> |
Pekka Enberg | c175eea | 2008-05-09 20:35:53 +0200 | [diff] [blame] | 116 | #include <linux/kmemcheck.h> |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 117 | #include <linux/memory.h> |
Linus Torvalds | 268bb0c | 2011-05-20 12:50:29 -0700 | [diff] [blame] | 118 | #include <linux/prefetch.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | |
Mel Gorman | 381760e | 2012-07-31 16:44:30 -0700 | [diff] [blame] | 120 | #include <net/sock.h> |
| 121 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | #include <asm/cacheflush.h> |
| 123 | #include <asm/tlbflush.h> |
| 124 | #include <asm/page.h> |
| 125 | |
Steven Rostedt | 4dee6b6 | 2012-01-09 17:15:42 -0500 | [diff] [blame] | 126 | #include <trace/events/kmem.h> |
| 127 | |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 128 | #include "internal.h" |
| 129 | |
Glauber Costa | b9ce5ef | 2012-12-18 14:22:46 -0800 | [diff] [blame] | 130 | #include "slab.h" |
| 131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | /* |
Christoph Lameter | 50953fe | 2007-05-06 14:50:16 -0700 | [diff] [blame] | 133 | * DEBUG - 1 for kmem_cache_create() to honour; SLAB_RED_ZONE & SLAB_POISON. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | * 0 for faster, smaller code (especially in the critical paths). |
| 135 | * |
| 136 | * STATS - 1 to collect stats for /proc/slabinfo. |
| 137 | * 0 for faster, smaller code (especially in the critical paths). |
| 138 | * |
| 139 | * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible) |
| 140 | */ |
| 141 | |
| 142 | #ifdef CONFIG_DEBUG_SLAB |
| 143 | #define DEBUG 1 |
| 144 | #define STATS 1 |
| 145 | #define FORCED_DEBUG 1 |
| 146 | #else |
| 147 | #define DEBUG 0 |
| 148 | #define STATS 0 |
| 149 | #define FORCED_DEBUG 0 |
| 150 | #endif |
| 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | /* Shouldn't this be in a header file somewhere? */ |
| 153 | #define BYTES_PER_WORD sizeof(void *) |
David Woodhouse | 87a927c | 2007-07-04 21:26:44 -0400 | [diff] [blame] | 154 | #define REDZONE_ALIGN max(BYTES_PER_WORD, __alignof__(unsigned long long)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | #ifndef ARCH_KMALLOC_FLAGS |
| 157 | #define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN |
| 158 | #endif |
| 159 | |
Joonsoo Kim | f315e3f | 2013-12-02 17:49:41 +0900 | [diff] [blame] | 160 | #define FREELIST_BYTE_INDEX (((PAGE_SIZE >> BITS_PER_BYTE) \ |
| 161 | <= SLAB_OBJ_MIN_SIZE) ? 1 : 0) |
| 162 | |
| 163 | #if FREELIST_BYTE_INDEX |
| 164 | typedef unsigned char freelist_idx_t; |
| 165 | #else |
| 166 | typedef unsigned short freelist_idx_t; |
| 167 | #endif |
| 168 | |
David Miller | 30321c7 | 2014-05-05 16:20:04 -0400 | [diff] [blame] | 169 | #define SLAB_OBJ_MAX_NUM ((1 << sizeof(freelist_idx_t) * BITS_PER_BYTE) - 1) |
Joonsoo Kim | f315e3f | 2013-12-02 17:49:41 +0900 | [diff] [blame] | 170 | |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 171 | /* |
| 172 | * true if a page was allocated from pfmemalloc reserves for network-based |
| 173 | * swap |
| 174 | */ |
| 175 | static bool pfmemalloc_active __read_mostly; |
| 176 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | * struct array_cache |
| 179 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | * Purpose: |
| 181 | * - LIFO ordering, to hand out cache-warm objects from _alloc |
| 182 | * - reduce the number of linked list operations |
| 183 | * - reduce spinlock operations |
| 184 | * |
| 185 | * The limit is stored in the per-cpu structure to reduce the data cache |
| 186 | * footprint. |
| 187 | * |
| 188 | */ |
| 189 | struct array_cache { |
| 190 | unsigned int avail; |
| 191 | unsigned int limit; |
| 192 | unsigned int batchcount; |
| 193 | unsigned int touched; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 194 | spinlock_t lock; |
Robert P. J. Day | bda5b65 | 2007-10-16 23:30:05 -0700 | [diff] [blame] | 195 | void *entry[]; /* |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 196 | * Must have this definition in here for the proper |
| 197 | * alignment of array_cache. Also simplifies accessing |
| 198 | * the entries. |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 199 | * |
| 200 | * Entries should not be directly dereferenced as |
| 201 | * entries belonging to slabs marked pfmemalloc will |
| 202 | * have the lower bits set SLAB_OBJ_PFMEMALLOC |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 203 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | }; |
| 205 | |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 206 | #define SLAB_OBJ_PFMEMALLOC 1 |
| 207 | static inline bool is_obj_pfmemalloc(void *objp) |
| 208 | { |
| 209 | return (unsigned long)objp & SLAB_OBJ_PFMEMALLOC; |
| 210 | } |
| 211 | |
| 212 | static inline void set_obj_pfmemalloc(void **objp) |
| 213 | { |
| 214 | *objp = (void *)((unsigned long)*objp | SLAB_OBJ_PFMEMALLOC); |
| 215 | return; |
| 216 | } |
| 217 | |
| 218 | static inline void clear_obj_pfmemalloc(void **objp) |
| 219 | { |
| 220 | *objp = (void *)((unsigned long)*objp & ~SLAB_OBJ_PFMEMALLOC); |
| 221 | } |
| 222 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 223 | /* |
| 224 | * bootstrap: The caches do not work without cpuarrays anymore, but the |
| 225 | * cpuarrays are allocated from the generic caches... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | */ |
| 227 | #define BOOT_CPUCACHE_ENTRIES 1 |
| 228 | struct arraycache_init { |
| 229 | struct array_cache cache; |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 230 | void *entries[BOOT_CPUCACHE_ENTRIES]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | /* |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 234 | * Need this for bootstrapping a per node allocator. |
| 235 | */ |
Pekka Enberg | 556a169 | 2008-01-25 08:20:51 +0200 | [diff] [blame] | 236 | #define NUM_INIT_LISTS (3 * MAX_NUMNODES) |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 237 | static struct kmem_cache_node __initdata init_kmem_cache_node[NUM_INIT_LISTS]; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 238 | #define CACHE_CACHE 0 |
Pekka Enberg | 556a169 | 2008-01-25 08:20:51 +0200 | [diff] [blame] | 239 | #define SIZE_AC MAX_NUMNODES |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 240 | #define SIZE_NODE (2 * MAX_NUMNODES) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Christoph Lameter | ed11d9e | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 242 | static int drain_freelist(struct kmem_cache *cache, |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 243 | struct kmem_cache_node *n, int tofree); |
Christoph Lameter | ed11d9e | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 244 | static void free_block(struct kmem_cache *cachep, void **objpp, int len, |
| 245 | int node); |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 246 | static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp); |
David Howells | 65f27f3 | 2006-11-22 14:55:48 +0000 | [diff] [blame] | 247 | static void cache_reap(struct work_struct *unused); |
Christoph Lameter | ed11d9e | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 248 | |
Ingo Molnar | e0a4272 | 2006-06-23 02:03:46 -0700 | [diff] [blame] | 249 | static int slab_early_init = 1; |
| 250 | |
Christoph Lameter | e336601 | 2013-01-10 19:14:18 +0000 | [diff] [blame] | 251 | #define INDEX_AC kmalloc_index(sizeof(struct arraycache_init)) |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 252 | #define INDEX_NODE kmalloc_index(sizeof(struct kmem_cache_node)) |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 253 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 254 | static void kmem_cache_node_init(struct kmem_cache_node *parent) |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 255 | { |
| 256 | INIT_LIST_HEAD(&parent->slabs_full); |
| 257 | INIT_LIST_HEAD(&parent->slabs_partial); |
| 258 | INIT_LIST_HEAD(&parent->slabs_free); |
| 259 | parent->shared = NULL; |
| 260 | parent->alien = NULL; |
Ravikiran G Thirumalai | 2e1217c | 2006-02-04 23:27:56 -0800 | [diff] [blame] | 261 | parent->colour_next = 0; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 262 | spin_lock_init(&parent->list_lock); |
| 263 | parent->free_objects = 0; |
| 264 | parent->free_touched = 0; |
| 265 | } |
| 266 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 267 | #define MAKE_LIST(cachep, listp, slab, nodeid) \ |
| 268 | do { \ |
| 269 | INIT_LIST_HEAD(listp); \ |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 270 | list_splice(&get_node(cachep, nodeid)->slab, listp); \ |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 271 | } while (0) |
| 272 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 273 | #define MAKE_ALL_LISTS(cachep, ptr, nodeid) \ |
| 274 | do { \ |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 275 | MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \ |
| 276 | MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \ |
| 277 | MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \ |
| 278 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | #define CFLGS_OFF_SLAB (0x80000000UL) |
| 281 | #define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB) |
| 282 | |
| 283 | #define BATCHREFILL_LIMIT 16 |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 284 | /* |
| 285 | * Optimization question: fewer reaps means less probability for unnessary |
| 286 | * cpucache drain/refill cycles. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | * |
Adrian Bunk | dc6f3f2 | 2005-11-08 16:44:08 +0100 | [diff] [blame] | 288 | * OTOH the cpuarrays can contain lots of objects, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | * which could lock up otherwise freeable slabs. |
| 290 | */ |
Jianyu Zhan | 5f0985b | 2014-03-30 17:02:20 +0800 | [diff] [blame] | 291 | #define REAPTIMEOUT_AC (2*HZ) |
| 292 | #define REAPTIMEOUT_NODE (4*HZ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | |
| 294 | #if STATS |
| 295 | #define STATS_INC_ACTIVE(x) ((x)->num_active++) |
| 296 | #define STATS_DEC_ACTIVE(x) ((x)->num_active--) |
| 297 | #define STATS_INC_ALLOCED(x) ((x)->num_allocations++) |
| 298 | #define STATS_INC_GROWN(x) ((x)->grown++) |
Christoph Lameter | ed11d9e | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 299 | #define STATS_ADD_REAPED(x,y) ((x)->reaped += (y)) |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 300 | #define STATS_SET_HIGH(x) \ |
| 301 | do { \ |
| 302 | if ((x)->num_active > (x)->high_mark) \ |
| 303 | (x)->high_mark = (x)->num_active; \ |
| 304 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | #define STATS_INC_ERR(x) ((x)->errors++) |
| 306 | #define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++) |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 307 | #define STATS_INC_NODEFREES(x) ((x)->node_frees++) |
Ravikiran G Thirumalai | fb7faf3 | 2006-04-10 22:52:54 -0700 | [diff] [blame] | 308 | #define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++) |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 309 | #define STATS_SET_FREEABLE(x, i) \ |
| 310 | do { \ |
| 311 | if ((x)->max_freeable < i) \ |
| 312 | (x)->max_freeable = i; \ |
| 313 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | #define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit) |
| 315 | #define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss) |
| 316 | #define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit) |
| 317 | #define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss) |
| 318 | #else |
| 319 | #define STATS_INC_ACTIVE(x) do { } while (0) |
| 320 | #define STATS_DEC_ACTIVE(x) do { } while (0) |
| 321 | #define STATS_INC_ALLOCED(x) do { } while (0) |
| 322 | #define STATS_INC_GROWN(x) do { } while (0) |
Andi Kleen | 4e60c86 | 2010-08-09 17:19:03 -0700 | [diff] [blame] | 323 | #define STATS_ADD_REAPED(x,y) do { (void)(y); } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | #define STATS_SET_HIGH(x) do { } while (0) |
| 325 | #define STATS_INC_ERR(x) do { } while (0) |
| 326 | #define STATS_INC_NODEALLOCS(x) do { } while (0) |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 327 | #define STATS_INC_NODEFREES(x) do { } while (0) |
Ravikiran G Thirumalai | fb7faf3 | 2006-04-10 22:52:54 -0700 | [diff] [blame] | 328 | #define STATS_INC_ACOVERFLOW(x) do { } while (0) |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 329 | #define STATS_SET_FREEABLE(x, i) do { } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | #define STATS_INC_ALLOCHIT(x) do { } while (0) |
| 331 | #define STATS_INC_ALLOCMISS(x) do { } while (0) |
| 332 | #define STATS_INC_FREEHIT(x) do { } while (0) |
| 333 | #define STATS_INC_FREEMISS(x) do { } while (0) |
| 334 | #endif |
| 335 | |
| 336 | #if DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 338 | /* |
| 339 | * memory layout of objects: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | * 0 : objp |
Manfred Spraul | 3dafccf | 2006-02-01 03:05:42 -0800 | [diff] [blame] | 341 | * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | * the end of an object is aligned with the end of the real |
| 343 | * allocation. Catches writes behind the end of the allocation. |
Manfred Spraul | 3dafccf | 2006-02-01 03:05:42 -0800 | [diff] [blame] | 344 | * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | * redzone word. |
Manfred Spraul | 3dafccf | 2006-02-01 03:05:42 -0800 | [diff] [blame] | 346 | * cachep->obj_offset: The real object. |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 347 | * cachep->size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long] |
| 348 | * cachep->size - 1* BYTES_PER_WORD: last caller address |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 349 | * [BYTES_PER_WORD long] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | */ |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 351 | static int obj_offset(struct kmem_cache *cachep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | { |
Manfred Spraul | 3dafccf | 2006-02-01 03:05:42 -0800 | [diff] [blame] | 353 | return cachep->obj_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | } |
| 355 | |
David Woodhouse | b46b8f1 | 2007-05-08 00:22:59 -0700 | [diff] [blame] | 356 | static unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | { |
| 358 | BUG_ON(!(cachep->flags & SLAB_RED_ZONE)); |
David Woodhouse | b46b8f1 | 2007-05-08 00:22:59 -0700 | [diff] [blame] | 359 | return (unsigned long long*) (objp + obj_offset(cachep) - |
| 360 | sizeof(unsigned long long)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
| 362 | |
David Woodhouse | b46b8f1 | 2007-05-08 00:22:59 -0700 | [diff] [blame] | 363 | static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | { |
| 365 | BUG_ON(!(cachep->flags & SLAB_RED_ZONE)); |
| 366 | if (cachep->flags & SLAB_STORE_USER) |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 367 | return (unsigned long long *)(objp + cachep->size - |
David Woodhouse | b46b8f1 | 2007-05-08 00:22:59 -0700 | [diff] [blame] | 368 | sizeof(unsigned long long) - |
David Woodhouse | 87a927c | 2007-07-04 21:26:44 -0400 | [diff] [blame] | 369 | REDZONE_ALIGN); |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 370 | return (unsigned long long *) (objp + cachep->size - |
David Woodhouse | b46b8f1 | 2007-05-08 00:22:59 -0700 | [diff] [blame] | 371 | sizeof(unsigned long long)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 374 | static void **dbg_userword(struct kmem_cache *cachep, void *objp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | { |
| 376 | BUG_ON(!(cachep->flags & SLAB_STORE_USER)); |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 377 | return (void **)(objp + cachep->size - BYTES_PER_WORD); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | #else |
| 381 | |
Manfred Spraul | 3dafccf | 2006-02-01 03:05:42 -0800 | [diff] [blame] | 382 | #define obj_offset(x) 0 |
David Woodhouse | b46b8f1 | 2007-05-08 00:22:59 -0700 | [diff] [blame] | 383 | #define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;}) |
| 384 | #define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;}) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | #define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;}) |
| 386 | |
| 387 | #endif |
| 388 | |
Joonsoo Kim | 0378730 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 389 | #define OBJECT_FREE (0) |
| 390 | #define OBJECT_ACTIVE (1) |
| 391 | |
| 392 | #ifdef CONFIG_DEBUG_SLAB_LEAK |
| 393 | |
| 394 | static void set_obj_status(struct page *page, int idx, int val) |
| 395 | { |
| 396 | int freelist_size; |
| 397 | char *status; |
| 398 | struct kmem_cache *cachep = page->slab_cache; |
| 399 | |
| 400 | freelist_size = cachep->num * sizeof(freelist_idx_t); |
| 401 | status = (char *)page->freelist + freelist_size; |
| 402 | status[idx] = val; |
| 403 | } |
| 404 | |
| 405 | static inline unsigned int get_obj_status(struct page *page, int idx) |
| 406 | { |
| 407 | int freelist_size; |
| 408 | char *status; |
| 409 | struct kmem_cache *cachep = page->slab_cache; |
| 410 | |
| 411 | freelist_size = cachep->num * sizeof(freelist_idx_t); |
| 412 | status = (char *)page->freelist + freelist_size; |
| 413 | |
| 414 | return status[idx]; |
| 415 | } |
| 416 | |
| 417 | #else |
| 418 | static inline void set_obj_status(struct page *page, int idx, int val) {} |
| 419 | |
| 420 | #endif |
| 421 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | /* |
David Rientjes | 3df1ccc | 2011-10-18 22:09:28 -0700 | [diff] [blame] | 423 | * Do not go above this order unless 0 objects fit into the slab or |
| 424 | * overridden on the command line. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | */ |
David Rientjes | 543585c | 2011-10-18 22:09:24 -0700 | [diff] [blame] | 426 | #define SLAB_MAX_ORDER_HI 1 |
| 427 | #define SLAB_MAX_ORDER_LO 0 |
| 428 | static int slab_max_order = SLAB_MAX_ORDER_LO; |
David Rientjes | 3df1ccc | 2011-10-18 22:09:28 -0700 | [diff] [blame] | 429 | static bool slab_max_order_set __initdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | |
Pekka Enberg | 6ed5eb221 | 2006-02-01 03:05:49 -0800 | [diff] [blame] | 431 | static inline struct kmem_cache *virt_to_cache(const void *obj) |
| 432 | { |
Christoph Lameter | b49af68 | 2007-05-06 14:49:41 -0700 | [diff] [blame] | 433 | struct page *page = virt_to_head_page(obj); |
Christoph Lameter | 3502608 | 2012-06-13 10:24:56 -0500 | [diff] [blame] | 434 | return page->slab_cache; |
Pekka Enberg | 6ed5eb221 | 2006-02-01 03:05:49 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 437 | static inline void *index_to_obj(struct kmem_cache *cache, struct page *page, |
Pekka Enberg | 8fea4e9 | 2006-03-22 00:08:10 -0800 | [diff] [blame] | 438 | unsigned int idx) |
| 439 | { |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 440 | return page->s_mem + cache->size * idx; |
Pekka Enberg | 8fea4e9 | 2006-03-22 00:08:10 -0800 | [diff] [blame] | 441 | } |
| 442 | |
Eric Dumazet | 6a2d7a9 | 2006-12-13 00:34:27 -0800 | [diff] [blame] | 443 | /* |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 444 | * We want to avoid an expensive divide : (offset / cache->size) |
| 445 | * Using the fact that size is a constant for a particular cache, |
| 446 | * we can replace (offset / cache->size) by |
Eric Dumazet | 6a2d7a9 | 2006-12-13 00:34:27 -0800 | [diff] [blame] | 447 | * reciprocal_divide(offset, cache->reciprocal_buffer_size) |
| 448 | */ |
| 449 | static inline unsigned int obj_to_index(const struct kmem_cache *cache, |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 450 | const struct page *page, void *obj) |
Pekka Enberg | 8fea4e9 | 2006-03-22 00:08:10 -0800 | [diff] [blame] | 451 | { |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 452 | u32 offset = (obj - page->s_mem); |
Eric Dumazet | 6a2d7a9 | 2006-12-13 00:34:27 -0800 | [diff] [blame] | 453 | return reciprocal_divide(offset, cache->reciprocal_buffer_size); |
Pekka Enberg | 8fea4e9 | 2006-03-22 00:08:10 -0800 | [diff] [blame] | 454 | } |
| 455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | static struct arraycache_init initarray_generic = |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 457 | { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
| 459 | /* internal cache of cache description objs */ |
Christoph Lameter | 9b030cb | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 460 | static struct kmem_cache kmem_cache_boot = { |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 461 | .batchcount = 1, |
| 462 | .limit = BOOT_CPUCACHE_ENTRIES, |
| 463 | .shared = 1, |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 464 | .size = sizeof(struct kmem_cache), |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 465 | .name = "kmem_cache", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | }; |
| 467 | |
Ravikiran G Thirumalai | 056c624 | 2006-09-25 23:31:38 -0700 | [diff] [blame] | 468 | #define BAD_ALIEN_MAGIC 0x01020304ul |
| 469 | |
Pekka Enberg | ce79ddc | 2009-11-23 22:01:15 +0200 | [diff] [blame] | 470 | #ifdef CONFIG_LOCKDEP |
| 471 | |
| 472 | /* |
| 473 | * Slab sometimes uses the kmalloc slabs to store the slab headers |
| 474 | * for other slabs "off slab". |
| 475 | * The locking for this is tricky in that it nests within the locks |
| 476 | * of all other slabs in a few places; to deal with this special |
| 477 | * locking we put on-slab caches into a separate lock-class. |
| 478 | * |
| 479 | * We set lock class for alien array caches which are up during init. |
| 480 | * The lock annotation will be lost if all cpus of a node goes down and |
| 481 | * then comes back up during hotplug |
| 482 | */ |
| 483 | static struct lock_class_key on_slab_l3_key; |
| 484 | static struct lock_class_key on_slab_alc_key; |
| 485 | |
Peter Zijlstra | 83835b3 | 2011-07-22 15:26:05 +0200 | [diff] [blame] | 486 | static struct lock_class_key debugobj_l3_key; |
| 487 | static struct lock_class_key debugobj_alc_key; |
| 488 | |
| 489 | static void slab_set_lock_classes(struct kmem_cache *cachep, |
| 490 | struct lock_class_key *l3_key, struct lock_class_key *alc_key, |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 491 | struct kmem_cache_node *n) |
Peter Zijlstra | 83835b3 | 2011-07-22 15:26:05 +0200 | [diff] [blame] | 492 | { |
| 493 | struct array_cache **alc; |
Peter Zijlstra | 83835b3 | 2011-07-22 15:26:05 +0200 | [diff] [blame] | 494 | int r; |
| 495 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 496 | lockdep_set_class(&n->list_lock, l3_key); |
| 497 | alc = n->alien; |
Peter Zijlstra | 83835b3 | 2011-07-22 15:26:05 +0200 | [diff] [blame] | 498 | /* |
| 499 | * FIXME: This check for BAD_ALIEN_MAGIC |
| 500 | * should go away when common slab code is taught to |
| 501 | * work even without alien caches. |
| 502 | * Currently, non NUMA code returns BAD_ALIEN_MAGIC |
| 503 | * for alloc_alien_cache, |
| 504 | */ |
| 505 | if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC) |
| 506 | return; |
| 507 | for_each_node(r) { |
| 508 | if (alc[r]) |
| 509 | lockdep_set_class(&alc[r]->lock, alc_key); |
| 510 | } |
| 511 | } |
| 512 | |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 513 | static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, |
| 514 | struct kmem_cache_node *n) |
Peter Zijlstra | 83835b3 | 2011-07-22 15:26:05 +0200 | [diff] [blame] | 515 | { |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 516 | slab_set_lock_classes(cachep, &debugobj_l3_key, &debugobj_alc_key, n); |
Peter Zijlstra | 83835b3 | 2011-07-22 15:26:05 +0200 | [diff] [blame] | 517 | } |
| 518 | |
| 519 | static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep) |
| 520 | { |
| 521 | int node; |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 522 | struct kmem_cache_node *n; |
Peter Zijlstra | 83835b3 | 2011-07-22 15:26:05 +0200 | [diff] [blame] | 523 | |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 524 | for_each_kmem_cache_node(cachep, node, n) |
| 525 | slab_set_debugobj_lock_classes_node(cachep, n); |
Peter Zijlstra | 83835b3 | 2011-07-22 15:26:05 +0200 | [diff] [blame] | 526 | } |
| 527 | |
Pekka Enberg | ce79ddc | 2009-11-23 22:01:15 +0200 | [diff] [blame] | 528 | static void init_node_lock_keys(int q) |
| 529 | { |
Christoph Lameter | e336601 | 2013-01-10 19:14:18 +0000 | [diff] [blame] | 530 | int i; |
Pekka Enberg | ce79ddc | 2009-11-23 22:01:15 +0200 | [diff] [blame] | 531 | |
Christoph Lameter | 97d0660 | 2012-07-06 15:25:11 -0500 | [diff] [blame] | 532 | if (slab_state < UP) |
Pekka Enberg | ce79ddc | 2009-11-23 22:01:15 +0200 | [diff] [blame] | 533 | return; |
| 534 | |
Christoph Lameter | 0f8f809 | 2013-07-02 12:12:10 -0700 | [diff] [blame] | 535 | for (i = 1; i <= KMALLOC_SHIFT_HIGH; i++) { |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 536 | struct kmem_cache_node *n; |
Christoph Lameter | e336601 | 2013-01-10 19:14:18 +0000 | [diff] [blame] | 537 | struct kmem_cache *cache = kmalloc_caches[i]; |
Pekka Enberg | ce79ddc | 2009-11-23 22:01:15 +0200 | [diff] [blame] | 538 | |
Christoph Lameter | e336601 | 2013-01-10 19:14:18 +0000 | [diff] [blame] | 539 | if (!cache) |
Pekka Enberg | 00afa75 | 2009-12-27 14:33:14 +0200 | [diff] [blame] | 540 | continue; |
Peter Zijlstra | 83835b3 | 2011-07-22 15:26:05 +0200 | [diff] [blame] | 541 | |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 542 | n = get_node(cache, q); |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 543 | if (!n || OFF_SLAB(cache)) |
Christoph Lameter | e336601 | 2013-01-10 19:14:18 +0000 | [diff] [blame] | 544 | continue; |
| 545 | |
| 546 | slab_set_lock_classes(cache, &on_slab_l3_key, |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 547 | &on_slab_alc_key, n); |
Pekka Enberg | ce79ddc | 2009-11-23 22:01:15 +0200 | [diff] [blame] | 548 | } |
| 549 | } |
| 550 | |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 551 | static void on_slab_lock_classes_node(struct kmem_cache *cachep, |
| 552 | struct kmem_cache_node *n) |
Glauber Costa | 6ccfb5b | 2012-12-18 14:22:31 -0800 | [diff] [blame] | 553 | { |
Glauber Costa | 6ccfb5b | 2012-12-18 14:22:31 -0800 | [diff] [blame] | 554 | slab_set_lock_classes(cachep, &on_slab_l3_key, |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 555 | &on_slab_alc_key, n); |
Glauber Costa | 6ccfb5b | 2012-12-18 14:22:31 -0800 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | static inline void on_slab_lock_classes(struct kmem_cache *cachep) |
| 559 | { |
| 560 | int node; |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 561 | struct kmem_cache_node *n; |
Glauber Costa | 6ccfb5b | 2012-12-18 14:22:31 -0800 | [diff] [blame] | 562 | |
| 563 | VM_BUG_ON(OFF_SLAB(cachep)); |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 564 | for_each_kmem_cache_node(cachep, node, n) |
| 565 | on_slab_lock_classes_node(cachep, n); |
Glauber Costa | 6ccfb5b | 2012-12-18 14:22:31 -0800 | [diff] [blame] | 566 | } |
| 567 | |
Fabian Frederick | 1536cb3 | 2014-08-06 16:04:05 -0700 | [diff] [blame] | 568 | static inline void __init init_lock_keys(void) |
Pekka Enberg | ce79ddc | 2009-11-23 22:01:15 +0200 | [diff] [blame] | 569 | { |
| 570 | int node; |
| 571 | |
| 572 | for_each_node(node) |
| 573 | init_node_lock_keys(node); |
| 574 | } |
| 575 | #else |
Fabian Frederick | 1536cb3 | 2014-08-06 16:04:05 -0700 | [diff] [blame] | 576 | static void __init init_node_lock_keys(int q) |
Pekka Enberg | ce79ddc | 2009-11-23 22:01:15 +0200 | [diff] [blame] | 577 | { |
| 578 | } |
| 579 | |
| 580 | static inline void init_lock_keys(void) |
| 581 | { |
| 582 | } |
Peter Zijlstra | 83835b3 | 2011-07-22 15:26:05 +0200 | [diff] [blame] | 583 | |
Glauber Costa | 6ccfb5b | 2012-12-18 14:22:31 -0800 | [diff] [blame] | 584 | static inline void on_slab_lock_classes(struct kmem_cache *cachep) |
| 585 | { |
| 586 | } |
| 587 | |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 588 | static inline void on_slab_lock_classes_node(struct kmem_cache *cachep, |
| 589 | struct kmem_cache_node *n) |
Glauber Costa | 6ccfb5b | 2012-12-18 14:22:31 -0800 | [diff] [blame] | 590 | { |
| 591 | } |
| 592 | |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 593 | static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, |
| 594 | struct kmem_cache_node *n) |
Peter Zijlstra | 83835b3 | 2011-07-22 15:26:05 +0200 | [diff] [blame] | 595 | { |
| 596 | } |
| 597 | |
| 598 | static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep) |
| 599 | { |
| 600 | } |
Pekka Enberg | ce79ddc | 2009-11-23 22:01:15 +0200 | [diff] [blame] | 601 | #endif |
| 602 | |
Tejun Heo | 1871e52 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 603 | static DEFINE_PER_CPU(struct delayed_work, slab_reap_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 605 | static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 606 | { |
| 607 | return cachep->array[smp_processor_id()]; |
| 608 | } |
| 609 | |
Joonsoo Kim | 0378730 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 610 | static size_t calculate_freelist_size(int nr_objs, size_t align) |
| 611 | { |
| 612 | size_t freelist_size; |
| 613 | |
| 614 | freelist_size = nr_objs * sizeof(freelist_idx_t); |
| 615 | if (IS_ENABLED(CONFIG_DEBUG_SLAB_LEAK)) |
| 616 | freelist_size += nr_objs * sizeof(char); |
| 617 | |
| 618 | if (align) |
| 619 | freelist_size = ALIGN(freelist_size, align); |
| 620 | |
| 621 | return freelist_size; |
| 622 | } |
| 623 | |
Joonsoo Kim | 9cef2e2 | 2013-12-02 17:49:39 +0900 | [diff] [blame] | 624 | static int calculate_nr_objs(size_t slab_size, size_t buffer_size, |
| 625 | size_t idx_size, size_t align) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | { |
Joonsoo Kim | 9cef2e2 | 2013-12-02 17:49:39 +0900 | [diff] [blame] | 627 | int nr_objs; |
Joonsoo Kim | 0378730 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 628 | size_t remained_size; |
Joonsoo Kim | 9cef2e2 | 2013-12-02 17:49:39 +0900 | [diff] [blame] | 629 | size_t freelist_size; |
Joonsoo Kim | 0378730 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 630 | int extra_space = 0; |
Joonsoo Kim | 9cef2e2 | 2013-12-02 17:49:39 +0900 | [diff] [blame] | 631 | |
Joonsoo Kim | 0378730 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 632 | if (IS_ENABLED(CONFIG_DEBUG_SLAB_LEAK)) |
| 633 | extra_space = sizeof(char); |
Joonsoo Kim | 9cef2e2 | 2013-12-02 17:49:39 +0900 | [diff] [blame] | 634 | /* |
| 635 | * Ignore padding for the initial guess. The padding |
| 636 | * is at most @align-1 bytes, and @buffer_size is at |
| 637 | * least @align. In the worst case, this result will |
| 638 | * be one greater than the number of objects that fit |
| 639 | * into the memory allocation when taking the padding |
| 640 | * into account. |
| 641 | */ |
Joonsoo Kim | 0378730 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 642 | nr_objs = slab_size / (buffer_size + idx_size + extra_space); |
Joonsoo Kim | 9cef2e2 | 2013-12-02 17:49:39 +0900 | [diff] [blame] | 643 | |
| 644 | /* |
| 645 | * This calculated number will be either the right |
| 646 | * amount, or one greater than what we want. |
| 647 | */ |
Joonsoo Kim | 0378730 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 648 | remained_size = slab_size - nr_objs * buffer_size; |
| 649 | freelist_size = calculate_freelist_size(nr_objs, align); |
| 650 | if (remained_size < freelist_size) |
Joonsoo Kim | 9cef2e2 | 2013-12-02 17:49:39 +0900 | [diff] [blame] | 651 | nr_objs--; |
| 652 | |
| 653 | return nr_objs; |
Steven Rostedt | fbaccac | 2006-02-01 03:05:45 -0800 | [diff] [blame] | 654 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 656 | /* |
| 657 | * Calculate the number of objects and left-over bytes for a given buffer size. |
| 658 | */ |
Steven Rostedt | fbaccac | 2006-02-01 03:05:45 -0800 | [diff] [blame] | 659 | static void cache_estimate(unsigned long gfporder, size_t buffer_size, |
| 660 | size_t align, int flags, size_t *left_over, |
| 661 | unsigned int *num) |
| 662 | { |
| 663 | int nr_objs; |
| 664 | size_t mgmt_size; |
| 665 | size_t slab_size = PAGE_SIZE << gfporder; |
| 666 | |
| 667 | /* |
| 668 | * The slab management structure can be either off the slab or |
| 669 | * on it. For the latter case, the memory allocated for a |
| 670 | * slab is used for: |
| 671 | * |
Joonsoo Kim | 1602517 | 2013-10-24 10:07:46 +0900 | [diff] [blame] | 672 | * - One unsigned int for each object |
Steven Rostedt | fbaccac | 2006-02-01 03:05:45 -0800 | [diff] [blame] | 673 | * - Padding to respect alignment of @align |
| 674 | * - @buffer_size bytes for each object |
| 675 | * |
| 676 | * If the slab management structure is off the slab, then the |
| 677 | * alignment will already be calculated into the size. Because |
| 678 | * the slabs are all pages aligned, the objects will be at the |
| 679 | * correct alignment when allocated. |
| 680 | */ |
| 681 | if (flags & CFLGS_OFF_SLAB) { |
| 682 | mgmt_size = 0; |
| 683 | nr_objs = slab_size / buffer_size; |
| 684 | |
Steven Rostedt | fbaccac | 2006-02-01 03:05:45 -0800 | [diff] [blame] | 685 | } else { |
Joonsoo Kim | 9cef2e2 | 2013-12-02 17:49:39 +0900 | [diff] [blame] | 686 | nr_objs = calculate_nr_objs(slab_size, buffer_size, |
Joonsoo Kim | a41adfa | 2013-12-02 17:49:42 +0900 | [diff] [blame] | 687 | sizeof(freelist_idx_t), align); |
Joonsoo Kim | 0378730 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 688 | mgmt_size = calculate_freelist_size(nr_objs, align); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 689 | } |
Steven Rostedt | fbaccac | 2006-02-01 03:05:45 -0800 | [diff] [blame] | 690 | *num = nr_objs; |
| 691 | *left_over = slab_size - nr_objs*buffer_size - mgmt_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | } |
| 693 | |
Christoph Lameter | f28510d | 2012-09-11 19:49:38 +0000 | [diff] [blame] | 694 | #if DEBUG |
Harvey Harrison | d40cee2 | 2008-04-30 00:55:07 -0700 | [diff] [blame] | 695 | #define slab_error(cachep, msg) __slab_error(__func__, cachep, msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 697 | static void __slab_error(const char *function, struct kmem_cache *cachep, |
| 698 | char *msg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | { |
| 700 | printk(KERN_ERR "slab error in %s(): cache `%s': %s\n", |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 701 | function, cachep->name, msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 702 | dump_stack(); |
Rusty Russell | 373d4d0 | 2013-01-21 17:17:39 +1030 | [diff] [blame] | 703 | add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | } |
Christoph Lameter | f28510d | 2012-09-11 19:49:38 +0000 | [diff] [blame] | 705 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | |
Paul Menage | 3395ee0 | 2006-12-06 20:32:16 -0800 | [diff] [blame] | 707 | /* |
| 708 | * By default on NUMA we use alien caches to stage the freeing of |
| 709 | * objects allocated from other nodes. This causes massive memory |
| 710 | * inefficiencies when using fake NUMA setup to split memory into a |
| 711 | * large number of small nodes, so it can be disabled on the command |
| 712 | * line |
| 713 | */ |
| 714 | |
| 715 | static int use_alien_caches __read_mostly = 1; |
| 716 | static int __init noaliencache_setup(char *s) |
| 717 | { |
| 718 | use_alien_caches = 0; |
| 719 | return 1; |
| 720 | } |
| 721 | __setup("noaliencache", noaliencache_setup); |
| 722 | |
David Rientjes | 3df1ccc | 2011-10-18 22:09:28 -0700 | [diff] [blame] | 723 | static int __init slab_max_order_setup(char *str) |
| 724 | { |
| 725 | get_option(&str, &slab_max_order); |
| 726 | slab_max_order = slab_max_order < 0 ? 0 : |
| 727 | min(slab_max_order, MAX_ORDER - 1); |
| 728 | slab_max_order_set = true; |
| 729 | |
| 730 | return 1; |
| 731 | } |
| 732 | __setup("slab_max_order=", slab_max_order_setup); |
| 733 | |
Christoph Lameter | 8fce4d8 | 2006-03-09 17:33:54 -0800 | [diff] [blame] | 734 | #ifdef CONFIG_NUMA |
| 735 | /* |
| 736 | * Special reaping functions for NUMA systems called from cache_reap(). |
| 737 | * These take care of doing round robin flushing of alien caches (containing |
| 738 | * objects freed on different nodes from which they were allocated) and the |
| 739 | * flushing of remote pcps by calling drain_node_pages. |
| 740 | */ |
Tejun Heo | 1871e52 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 741 | static DEFINE_PER_CPU(unsigned long, slab_reap_node); |
Christoph Lameter | 8fce4d8 | 2006-03-09 17:33:54 -0800 | [diff] [blame] | 742 | |
| 743 | static void init_reap_node(int cpu) |
| 744 | { |
| 745 | int node; |
| 746 | |
Lee Schermerhorn | 7d6e6d0 | 2010-05-26 14:45:03 -0700 | [diff] [blame] | 747 | node = next_node(cpu_to_mem(cpu), node_online_map); |
Christoph Lameter | 8fce4d8 | 2006-03-09 17:33:54 -0800 | [diff] [blame] | 748 | if (node == MAX_NUMNODES) |
Paul Jackson | 442295c | 2006-03-22 00:09:11 -0800 | [diff] [blame] | 749 | node = first_node(node_online_map); |
Christoph Lameter | 8fce4d8 | 2006-03-09 17:33:54 -0800 | [diff] [blame] | 750 | |
Tejun Heo | 1871e52 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 751 | per_cpu(slab_reap_node, cpu) = node; |
Christoph Lameter | 8fce4d8 | 2006-03-09 17:33:54 -0800 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | static void next_reap_node(void) |
| 755 | { |
Christoph Lameter | 909ea96 | 2010-12-08 16:22:55 +0100 | [diff] [blame] | 756 | int node = __this_cpu_read(slab_reap_node); |
Christoph Lameter | 8fce4d8 | 2006-03-09 17:33:54 -0800 | [diff] [blame] | 757 | |
Christoph Lameter | 8fce4d8 | 2006-03-09 17:33:54 -0800 | [diff] [blame] | 758 | node = next_node(node, node_online_map); |
| 759 | if (unlikely(node >= MAX_NUMNODES)) |
| 760 | node = first_node(node_online_map); |
Christoph Lameter | 909ea96 | 2010-12-08 16:22:55 +0100 | [diff] [blame] | 761 | __this_cpu_write(slab_reap_node, node); |
Christoph Lameter | 8fce4d8 | 2006-03-09 17:33:54 -0800 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | #else |
| 765 | #define init_reap_node(cpu) do { } while (0) |
| 766 | #define next_reap_node(void) do { } while (0) |
| 767 | #endif |
| 768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | /* |
| 770 | * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz |
| 771 | * via the workqueue/eventd. |
| 772 | * Add the CPU number into the expiration time to minimize the possibility of |
| 773 | * the CPUs getting into lockstep and contending for the global cache chain |
| 774 | * lock. |
| 775 | */ |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 776 | static void start_cpu_timer(int cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | { |
Tejun Heo | 1871e52 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 778 | struct delayed_work *reap_work = &per_cpu(slab_reap_work, cpu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 | |
| 780 | /* |
| 781 | * When this gets called from do_initcalls via cpucache_init(), |
| 782 | * init_workqueues() has already run, so keventd will be setup |
| 783 | * at that time. |
| 784 | */ |
David Howells | 52bad64 | 2006-11-22 14:54:01 +0000 | [diff] [blame] | 785 | if (keventd_up() && reap_work->work.func == NULL) { |
Christoph Lameter | 8fce4d8 | 2006-03-09 17:33:54 -0800 | [diff] [blame] | 786 | init_reap_node(cpu); |
Tejun Heo | 203b42f | 2012-08-21 13:18:23 -0700 | [diff] [blame] | 787 | INIT_DEFERRABLE_WORK(reap_work, cache_reap); |
Arjan van de Ven | 2b28421 | 2006-12-10 02:21:28 -0800 | [diff] [blame] | 788 | schedule_delayed_work_on(cpu, reap_work, |
| 789 | __round_jiffies_relative(HZ, cpu)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | } |
| 791 | } |
| 792 | |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 793 | static struct array_cache *alloc_arraycache(int node, int entries, |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 794 | int batchcount, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | { |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 796 | int memsize = sizeof(void *) * entries + sizeof(struct array_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | struct array_cache *nc = NULL; |
| 798 | |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 799 | nc = kmalloc_node(memsize, gfp, node); |
Catalin Marinas | d5cff63 | 2009-06-11 13:22:40 +0100 | [diff] [blame] | 800 | /* |
| 801 | * The array_cache structures contain pointers to free object. |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 802 | * However, when such objects are allocated or transferred to another |
Catalin Marinas | d5cff63 | 2009-06-11 13:22:40 +0100 | [diff] [blame] | 803 | * cache the pointers are not cleared and they could be counted as |
| 804 | * valid references during a kmemleak scan. Therefore, kmemleak must |
| 805 | * not scan such objects. |
| 806 | */ |
| 807 | kmemleak_no_scan(nc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | if (nc) { |
| 809 | nc->avail = 0; |
| 810 | nc->limit = entries; |
| 811 | nc->batchcount = batchcount; |
| 812 | nc->touched = 0; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 813 | spin_lock_init(&nc->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 814 | } |
| 815 | return nc; |
| 816 | } |
| 817 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 818 | static inline bool is_slab_pfmemalloc(struct page *page) |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 819 | { |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 820 | return PageSlabPfmemalloc(page); |
| 821 | } |
| 822 | |
| 823 | /* Clears pfmemalloc_active if no slabs have pfmalloc set */ |
| 824 | static void recheck_pfmemalloc_active(struct kmem_cache *cachep, |
| 825 | struct array_cache *ac) |
| 826 | { |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 827 | struct kmem_cache_node *n = get_node(cachep, numa_mem_id()); |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 828 | struct page *page; |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 829 | unsigned long flags; |
| 830 | |
| 831 | if (!pfmemalloc_active) |
| 832 | return; |
| 833 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 834 | spin_lock_irqsave(&n->list_lock, flags); |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 835 | list_for_each_entry(page, &n->slabs_full, lru) |
| 836 | if (is_slab_pfmemalloc(page)) |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 837 | goto out; |
| 838 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 839 | list_for_each_entry(page, &n->slabs_partial, lru) |
| 840 | if (is_slab_pfmemalloc(page)) |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 841 | goto out; |
| 842 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 843 | list_for_each_entry(page, &n->slabs_free, lru) |
| 844 | if (is_slab_pfmemalloc(page)) |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 845 | goto out; |
| 846 | |
| 847 | pfmemalloc_active = false; |
| 848 | out: |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 849 | spin_unlock_irqrestore(&n->list_lock, flags); |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 850 | } |
| 851 | |
Mel Gorman | 381760e | 2012-07-31 16:44:30 -0700 | [diff] [blame] | 852 | static void *__ac_get_obj(struct kmem_cache *cachep, struct array_cache *ac, |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 853 | gfp_t flags, bool force_refill) |
| 854 | { |
| 855 | int i; |
| 856 | void *objp = ac->entry[--ac->avail]; |
| 857 | |
| 858 | /* Ensure the caller is allowed to use objects from PFMEMALLOC slab */ |
| 859 | if (unlikely(is_obj_pfmemalloc(objp))) { |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 860 | struct kmem_cache_node *n; |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 861 | |
| 862 | if (gfp_pfmemalloc_allowed(flags)) { |
| 863 | clear_obj_pfmemalloc(&objp); |
| 864 | return objp; |
| 865 | } |
| 866 | |
| 867 | /* The caller cannot use PFMEMALLOC objects, find another one */ |
Joonsoo Kim | d014dc2 | 2012-09-17 14:09:06 -0700 | [diff] [blame] | 868 | for (i = 0; i < ac->avail; i++) { |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 869 | /* If a !PFMEMALLOC object is found, swap them */ |
| 870 | if (!is_obj_pfmemalloc(ac->entry[i])) { |
| 871 | objp = ac->entry[i]; |
| 872 | ac->entry[i] = ac->entry[ac->avail]; |
| 873 | ac->entry[ac->avail] = objp; |
| 874 | return objp; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | /* |
| 879 | * If there are empty slabs on the slabs_free list and we are |
| 880 | * being forced to refill the cache, mark this one !pfmemalloc. |
| 881 | */ |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 882 | n = get_node(cachep, numa_mem_id()); |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 883 | if (!list_empty(&n->slabs_free) && force_refill) { |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 884 | struct page *page = virt_to_head_page(objp); |
Joonsoo Kim | 7ecccf9 | 2013-10-24 10:07:50 +0900 | [diff] [blame] | 885 | ClearPageSlabPfmemalloc(page); |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 886 | clear_obj_pfmemalloc(&objp); |
| 887 | recheck_pfmemalloc_active(cachep, ac); |
| 888 | return objp; |
| 889 | } |
| 890 | |
| 891 | /* No !PFMEMALLOC objects available */ |
| 892 | ac->avail++; |
| 893 | objp = NULL; |
| 894 | } |
| 895 | |
| 896 | return objp; |
| 897 | } |
| 898 | |
Mel Gorman | 381760e | 2012-07-31 16:44:30 -0700 | [diff] [blame] | 899 | static inline void *ac_get_obj(struct kmem_cache *cachep, |
| 900 | struct array_cache *ac, gfp_t flags, bool force_refill) |
| 901 | { |
| 902 | void *objp; |
| 903 | |
| 904 | if (unlikely(sk_memalloc_socks())) |
| 905 | objp = __ac_get_obj(cachep, ac, flags, force_refill); |
| 906 | else |
| 907 | objp = ac->entry[--ac->avail]; |
| 908 | |
| 909 | return objp; |
| 910 | } |
| 911 | |
| 912 | static void *__ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac, |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 913 | void *objp) |
| 914 | { |
| 915 | if (unlikely(pfmemalloc_active)) { |
| 916 | /* Some pfmemalloc slabs exist, check if this is one */ |
Mel Gorman | 30c29be | 2012-09-17 14:09:03 -0700 | [diff] [blame] | 917 | struct page *page = virt_to_head_page(objp); |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 918 | if (PageSlabPfmemalloc(page)) |
| 919 | set_obj_pfmemalloc(&objp); |
| 920 | } |
| 921 | |
Mel Gorman | 381760e | 2012-07-31 16:44:30 -0700 | [diff] [blame] | 922 | return objp; |
| 923 | } |
| 924 | |
| 925 | static inline void ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac, |
| 926 | void *objp) |
| 927 | { |
| 928 | if (unlikely(sk_memalloc_socks())) |
| 929 | objp = __ac_put_obj(cachep, ac, objp); |
| 930 | |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 931 | ac->entry[ac->avail++] = objp; |
| 932 | } |
| 933 | |
Christoph Lameter | 3ded175 | 2006-03-25 03:06:44 -0800 | [diff] [blame] | 934 | /* |
| 935 | * Transfer objects in one arraycache to another. |
| 936 | * Locking must be handled by the caller. |
| 937 | * |
| 938 | * Return the number of entries transferred. |
| 939 | */ |
| 940 | static int transfer_objects(struct array_cache *to, |
| 941 | struct array_cache *from, unsigned int max) |
| 942 | { |
| 943 | /* Figure out how many entries to transfer */ |
Hagen Paul Pfeifer | 732eacc | 2010-10-26 14:22:23 -0700 | [diff] [blame] | 944 | int nr = min3(from->avail, max, to->limit - to->avail); |
Christoph Lameter | 3ded175 | 2006-03-25 03:06:44 -0800 | [diff] [blame] | 945 | |
| 946 | if (!nr) |
| 947 | return 0; |
| 948 | |
| 949 | memcpy(to->entry + to->avail, from->entry + from->avail -nr, |
| 950 | sizeof(void *) *nr); |
| 951 | |
| 952 | from->avail -= nr; |
| 953 | to->avail += nr; |
Christoph Lameter | 3ded175 | 2006-03-25 03:06:44 -0800 | [diff] [blame] | 954 | return nr; |
| 955 | } |
| 956 | |
Christoph Lameter | 765c450 | 2006-09-27 01:50:08 -0700 | [diff] [blame] | 957 | #ifndef CONFIG_NUMA |
| 958 | |
| 959 | #define drain_alien_cache(cachep, alien) do { } while (0) |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 960 | #define reap_alien(cachep, n) do { } while (0) |
Christoph Lameter | 765c450 | 2006-09-27 01:50:08 -0700 | [diff] [blame] | 961 | |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 962 | static inline struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp) |
Christoph Lameter | 765c450 | 2006-09-27 01:50:08 -0700 | [diff] [blame] | 963 | { |
| 964 | return (struct array_cache **)BAD_ALIEN_MAGIC; |
| 965 | } |
| 966 | |
| 967 | static inline void free_alien_cache(struct array_cache **ac_ptr) |
| 968 | { |
| 969 | } |
| 970 | |
| 971 | static inline int cache_free_alien(struct kmem_cache *cachep, void *objp) |
| 972 | { |
| 973 | return 0; |
| 974 | } |
| 975 | |
| 976 | static inline void *alternate_node_alloc(struct kmem_cache *cachep, |
| 977 | gfp_t flags) |
| 978 | { |
| 979 | return NULL; |
| 980 | } |
| 981 | |
Christoph Hellwig | 8b98c16 | 2006-12-06 20:32:30 -0800 | [diff] [blame] | 982 | static inline void *____cache_alloc_node(struct kmem_cache *cachep, |
Christoph Lameter | 765c450 | 2006-09-27 01:50:08 -0700 | [diff] [blame] | 983 | gfp_t flags, int nodeid) |
| 984 | { |
| 985 | return NULL; |
| 986 | } |
| 987 | |
| 988 | #else /* CONFIG_NUMA */ |
| 989 | |
Christoph Hellwig | 8b98c16 | 2006-12-06 20:32:30 -0800 | [diff] [blame] | 990 | static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int); |
Paul Jackson | c61afb1 | 2006-03-24 03:16:08 -0800 | [diff] [blame] | 991 | static void *alternate_node_alloc(struct kmem_cache *, gfp_t); |
Christoph Lameter | dc85da1 | 2006-01-18 17:42:36 -0800 | [diff] [blame] | 992 | |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 993 | static struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp) |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 994 | { |
| 995 | struct array_cache **ac_ptr; |
Christoph Lameter | 8ef8286 | 2007-02-20 13:57:52 -0800 | [diff] [blame] | 996 | int memsize = sizeof(void *) * nr_node_ids; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 997 | int i; |
| 998 | |
| 999 | if (limit > 1) |
| 1000 | limit = 12; |
Haicheng Li | f3186a9 | 2010-01-06 15:25:23 +0800 | [diff] [blame] | 1001 | ac_ptr = kzalloc_node(memsize, gfp, node); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1002 | if (ac_ptr) { |
| 1003 | for_each_node(i) { |
Haicheng Li | f3186a9 | 2010-01-06 15:25:23 +0800 | [diff] [blame] | 1004 | if (i == node || !node_online(i)) |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1005 | continue; |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 1006 | ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d, gfp); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1007 | if (!ac_ptr[i]) { |
Akinobu Mita | cc550de | 2007-11-14 16:58:35 -0800 | [diff] [blame] | 1008 | for (i--; i >= 0; i--) |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1009 | kfree(ac_ptr[i]); |
| 1010 | kfree(ac_ptr); |
| 1011 | return NULL; |
| 1012 | } |
| 1013 | } |
| 1014 | } |
| 1015 | return ac_ptr; |
| 1016 | } |
| 1017 | |
Pekka Enberg | 5295a74 | 2006-02-01 03:05:48 -0800 | [diff] [blame] | 1018 | static void free_alien_cache(struct array_cache **ac_ptr) |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1019 | { |
| 1020 | int i; |
| 1021 | |
| 1022 | if (!ac_ptr) |
| 1023 | return; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1024 | for_each_node(i) |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1025 | kfree(ac_ptr[i]); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1026 | kfree(ac_ptr); |
| 1027 | } |
| 1028 | |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 1029 | static void __drain_alien_cache(struct kmem_cache *cachep, |
Pekka Enberg | 5295a74 | 2006-02-01 03:05:48 -0800 | [diff] [blame] | 1030 | struct array_cache *ac, int node) |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1031 | { |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 1032 | struct kmem_cache_node *n = get_node(cachep, node); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1033 | |
| 1034 | if (ac->avail) { |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1035 | spin_lock(&n->list_lock); |
Christoph Lameter | e00946f | 2006-03-25 03:06:45 -0800 | [diff] [blame] | 1036 | /* |
| 1037 | * Stuff objects into the remote nodes shared array first. |
| 1038 | * That way we could avoid the overhead of putting the objects |
| 1039 | * into the free lists and getting them back later. |
| 1040 | */ |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1041 | if (n->shared) |
| 1042 | transfer_objects(n->shared, ac, ac->limit); |
Christoph Lameter | e00946f | 2006-03-25 03:06:45 -0800 | [diff] [blame] | 1043 | |
Christoph Lameter | ff69416 | 2005-09-22 21:44:02 -0700 | [diff] [blame] | 1044 | free_block(cachep, ac->entry, ac->avail, node); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1045 | ac->avail = 0; |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1046 | spin_unlock(&n->list_lock); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1047 | } |
| 1048 | } |
| 1049 | |
Christoph Lameter | 8fce4d8 | 2006-03-09 17:33:54 -0800 | [diff] [blame] | 1050 | /* |
| 1051 | * Called from cache_reap() to regularly drain alien caches round robin. |
| 1052 | */ |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1053 | static void reap_alien(struct kmem_cache *cachep, struct kmem_cache_node *n) |
Christoph Lameter | 8fce4d8 | 2006-03-09 17:33:54 -0800 | [diff] [blame] | 1054 | { |
Christoph Lameter | 909ea96 | 2010-12-08 16:22:55 +0100 | [diff] [blame] | 1055 | int node = __this_cpu_read(slab_reap_node); |
Christoph Lameter | 8fce4d8 | 2006-03-09 17:33:54 -0800 | [diff] [blame] | 1056 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1057 | if (n->alien) { |
| 1058 | struct array_cache *ac = n->alien[node]; |
Christoph Lameter | e00946f | 2006-03-25 03:06:45 -0800 | [diff] [blame] | 1059 | |
| 1060 | if (ac && ac->avail && spin_trylock_irq(&ac->lock)) { |
Christoph Lameter | 8fce4d8 | 2006-03-09 17:33:54 -0800 | [diff] [blame] | 1061 | __drain_alien_cache(cachep, ac, node); |
| 1062 | spin_unlock_irq(&ac->lock); |
| 1063 | } |
| 1064 | } |
| 1065 | } |
| 1066 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 1067 | static void drain_alien_cache(struct kmem_cache *cachep, |
| 1068 | struct array_cache **alien) |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1069 | { |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1070 | int i = 0; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1071 | struct array_cache *ac; |
| 1072 | unsigned long flags; |
| 1073 | |
| 1074 | for_each_online_node(i) { |
Ravikiran G Thirumalai | 4484ebf | 2006-02-04 23:27:59 -0800 | [diff] [blame] | 1075 | ac = alien[i]; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1076 | if (ac) { |
| 1077 | spin_lock_irqsave(&ac->lock, flags); |
| 1078 | __drain_alien_cache(cachep, ac, i); |
| 1079 | spin_unlock_irqrestore(&ac->lock, flags); |
| 1080 | } |
| 1081 | } |
| 1082 | } |
Pekka Enberg | 729bd0b | 2006-06-23 02:03:05 -0700 | [diff] [blame] | 1083 | |
Ingo Molnar | 873623d | 2006-07-13 14:44:38 +0200 | [diff] [blame] | 1084 | static inline int cache_free_alien(struct kmem_cache *cachep, void *objp) |
Pekka Enberg | 729bd0b | 2006-06-23 02:03:05 -0700 | [diff] [blame] | 1085 | { |
Joonsoo Kim | 1ea991b | 2013-10-24 10:07:40 +0900 | [diff] [blame] | 1086 | int nodeid = page_to_nid(virt_to_page(objp)); |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1087 | struct kmem_cache_node *n; |
Pekka Enberg | 729bd0b | 2006-06-23 02:03:05 -0700 | [diff] [blame] | 1088 | struct array_cache *alien = NULL; |
Pekka Enberg | 1ca4cb2 | 2006-10-06 00:43:52 -0700 | [diff] [blame] | 1089 | int node; |
| 1090 | |
Lee Schermerhorn | 7d6e6d0 | 2010-05-26 14:45:03 -0700 | [diff] [blame] | 1091 | node = numa_mem_id(); |
Pekka Enberg | 729bd0b | 2006-06-23 02:03:05 -0700 | [diff] [blame] | 1092 | |
| 1093 | /* |
| 1094 | * Make sure we are not freeing a object from another node to the array |
| 1095 | * cache on this cpu. |
| 1096 | */ |
Joonsoo Kim | 1ea991b | 2013-10-24 10:07:40 +0900 | [diff] [blame] | 1097 | if (likely(nodeid == node)) |
Pekka Enberg | 729bd0b | 2006-06-23 02:03:05 -0700 | [diff] [blame] | 1098 | return 0; |
| 1099 | |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 1100 | n = get_node(cachep, node); |
Pekka Enberg | 729bd0b | 2006-06-23 02:03:05 -0700 | [diff] [blame] | 1101 | STATS_INC_NODEFREES(cachep); |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1102 | if (n->alien && n->alien[nodeid]) { |
| 1103 | alien = n->alien[nodeid]; |
Ingo Molnar | 873623d | 2006-07-13 14:44:38 +0200 | [diff] [blame] | 1104 | spin_lock(&alien->lock); |
Pekka Enberg | 729bd0b | 2006-06-23 02:03:05 -0700 | [diff] [blame] | 1105 | if (unlikely(alien->avail == alien->limit)) { |
| 1106 | STATS_INC_ACOVERFLOW(cachep); |
| 1107 | __drain_alien_cache(cachep, alien, nodeid); |
| 1108 | } |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 1109 | ac_put_obj(cachep, alien, objp); |
Pekka Enberg | 729bd0b | 2006-06-23 02:03:05 -0700 | [diff] [blame] | 1110 | spin_unlock(&alien->lock); |
| 1111 | } else { |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 1112 | n = get_node(cachep, nodeid); |
| 1113 | spin_lock(&n->list_lock); |
Pekka Enberg | 729bd0b | 2006-06-23 02:03:05 -0700 | [diff] [blame] | 1114 | free_block(cachep, &objp, 1, nodeid); |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 1115 | spin_unlock(&n->list_lock); |
Pekka Enberg | 729bd0b | 2006-06-23 02:03:05 -0700 | [diff] [blame] | 1116 | } |
| 1117 | return 1; |
| 1118 | } |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1119 | #endif |
| 1120 | |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1121 | /* |
Christoph Lameter | 6a67368 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1122 | * Allocates and initializes node for a node on each slab cache, used for |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1123 | * either memory or cpu hotplug. If memory is being hot-added, the kmem_cache_node |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1124 | * will be allocated off-node since memory is not yet online for the new node. |
Christoph Lameter | 6a67368 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1125 | * When hotplugging memory or a cpu, existing node are not replaced if |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1126 | * already in use. |
| 1127 | * |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 1128 | * Must hold slab_mutex. |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1129 | */ |
Christoph Lameter | 6a67368 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1130 | static int init_cache_node_node(int node) |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1131 | { |
| 1132 | struct kmem_cache *cachep; |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1133 | struct kmem_cache_node *n; |
Christoph Lameter | 6744f08 | 2013-01-10 19:12:17 +0000 | [diff] [blame] | 1134 | const int memsize = sizeof(struct kmem_cache_node); |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1135 | |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 1136 | list_for_each_entry(cachep, &slab_caches, list) { |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1137 | /* |
Jianyu Zhan | 5f0985b | 2014-03-30 17:02:20 +0800 | [diff] [blame] | 1138 | * Set up the kmem_cache_node for cpu before we can |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1139 | * begin anything. Make sure some other cpu on this |
| 1140 | * node has not already allocated this |
| 1141 | */ |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 1142 | n = get_node(cachep, node); |
| 1143 | if (!n) { |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1144 | n = kmalloc_node(memsize, GFP_KERNEL, node); |
| 1145 | if (!n) |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1146 | return -ENOMEM; |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1147 | kmem_cache_node_init(n); |
Jianyu Zhan | 5f0985b | 2014-03-30 17:02:20 +0800 | [diff] [blame] | 1148 | n->next_reap = jiffies + REAPTIMEOUT_NODE + |
| 1149 | ((unsigned long)cachep) % REAPTIMEOUT_NODE; |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1150 | |
| 1151 | /* |
Jianyu Zhan | 5f0985b | 2014-03-30 17:02:20 +0800 | [diff] [blame] | 1152 | * The kmem_cache_nodes don't come and go as CPUs |
| 1153 | * come and go. slab_mutex is sufficient |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1154 | * protection here. |
| 1155 | */ |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1156 | cachep->node[node] = n; |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 1159 | spin_lock_irq(&n->list_lock); |
| 1160 | n->free_limit = |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1161 | (1 + nr_cpus_node(node)) * |
| 1162 | cachep->batchcount + cachep->num; |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 1163 | spin_unlock_irq(&n->list_lock); |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1164 | } |
| 1165 | return 0; |
| 1166 | } |
| 1167 | |
Wanpeng Li | 0fa8103 | 2013-07-04 08:33:22 +0800 | [diff] [blame] | 1168 | static inline int slabs_tofree(struct kmem_cache *cachep, |
| 1169 | struct kmem_cache_node *n) |
| 1170 | { |
| 1171 | return (n->free_objects + cachep->num - 1) / cachep->num; |
| 1172 | } |
| 1173 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 1174 | static void cpuup_canceled(long cpu) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | { |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1176 | struct kmem_cache *cachep; |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1177 | struct kmem_cache_node *n = NULL; |
Lee Schermerhorn | 7d6e6d0 | 2010-05-26 14:45:03 -0700 | [diff] [blame] | 1178 | int node = cpu_to_mem(cpu); |
Rusty Russell | a70f730 | 2009-03-13 14:49:46 +1030 | [diff] [blame] | 1179 | const struct cpumask *mask = cpumask_of_node(node); |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1180 | |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 1181 | list_for_each_entry(cachep, &slab_caches, list) { |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1182 | struct array_cache *nc; |
| 1183 | struct array_cache *shared; |
| 1184 | struct array_cache **alien; |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1185 | |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1186 | /* cpu is dead; no one can alloc from it. */ |
| 1187 | nc = cachep->array[cpu]; |
| 1188 | cachep->array[cpu] = NULL; |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 1189 | n = get_node(cachep, node); |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1190 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1191 | if (!n) |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1192 | goto free_array_cache; |
| 1193 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1194 | spin_lock_irq(&n->list_lock); |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1195 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1196 | /* Free limit for this kmem_cache_node */ |
| 1197 | n->free_limit -= cachep->batchcount; |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1198 | if (nc) |
| 1199 | free_block(cachep, nc->entry, nc->avail, node); |
| 1200 | |
Rusty Russell | 58463c1 | 2009-12-17 11:43:12 -0600 | [diff] [blame] | 1201 | if (!cpumask_empty(mask)) { |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1202 | spin_unlock_irq(&n->list_lock); |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1203 | goto free_array_cache; |
| 1204 | } |
| 1205 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1206 | shared = n->shared; |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1207 | if (shared) { |
| 1208 | free_block(cachep, shared->entry, |
| 1209 | shared->avail, node); |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1210 | n->shared = NULL; |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1211 | } |
| 1212 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1213 | alien = n->alien; |
| 1214 | n->alien = NULL; |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1215 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1216 | spin_unlock_irq(&n->list_lock); |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1217 | |
| 1218 | kfree(shared); |
| 1219 | if (alien) { |
| 1220 | drain_alien_cache(cachep, alien); |
| 1221 | free_alien_cache(alien); |
| 1222 | } |
| 1223 | free_array_cache: |
| 1224 | kfree(nc); |
| 1225 | } |
| 1226 | /* |
| 1227 | * In the previous loop, all the objects were freed to |
| 1228 | * the respective cache's slabs, now we can go ahead and |
| 1229 | * shrink each nodelist to its limit. |
| 1230 | */ |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 1231 | list_for_each_entry(cachep, &slab_caches, list) { |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 1232 | n = get_node(cachep, node); |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1233 | if (!n) |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1234 | continue; |
Wanpeng Li | 0fa8103 | 2013-07-04 08:33:22 +0800 | [diff] [blame] | 1235 | drain_freelist(cachep, n, slabs_tofree(cachep, n)); |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1236 | } |
| 1237 | } |
| 1238 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 1239 | static int cpuup_prepare(long cpu) |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1240 | { |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 1241 | struct kmem_cache *cachep; |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1242 | struct kmem_cache_node *n = NULL; |
Lee Schermerhorn | 7d6e6d0 | 2010-05-26 14:45:03 -0700 | [diff] [blame] | 1243 | int node = cpu_to_mem(cpu); |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1244 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1245 | |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1246 | /* |
| 1247 | * We need to do this right in the beginning since |
| 1248 | * alloc_arraycache's are going to use this list. |
| 1249 | * kmalloc_node allows us to add the slab to the right |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1250 | * kmem_cache_node and not this cpu's kmem_cache_node |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1251 | */ |
Christoph Lameter | 6a67368 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1252 | err = init_cache_node_node(node); |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1253 | if (err < 0) |
| 1254 | goto bad; |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1255 | |
| 1256 | /* |
| 1257 | * Now we can go ahead with allocating the shared arrays and |
| 1258 | * array caches |
| 1259 | */ |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 1260 | list_for_each_entry(cachep, &slab_caches, list) { |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1261 | struct array_cache *nc; |
| 1262 | struct array_cache *shared = NULL; |
| 1263 | struct array_cache **alien = NULL; |
| 1264 | |
| 1265 | nc = alloc_arraycache(node, cachep->limit, |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 1266 | cachep->batchcount, GFP_KERNEL); |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1267 | if (!nc) |
| 1268 | goto bad; |
| 1269 | if (cachep->shared) { |
| 1270 | shared = alloc_arraycache(node, |
| 1271 | cachep->shared * cachep->batchcount, |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 1272 | 0xbaadf00d, GFP_KERNEL); |
Akinobu Mita | 12d00f6 | 2007-10-18 03:05:11 -0700 | [diff] [blame] | 1273 | if (!shared) { |
| 1274 | kfree(nc); |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1275 | goto bad; |
Akinobu Mita | 12d00f6 | 2007-10-18 03:05:11 -0700 | [diff] [blame] | 1276 | } |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1277 | } |
| 1278 | if (use_alien_caches) { |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 1279 | alien = alloc_alien_cache(node, cachep->limit, GFP_KERNEL); |
Akinobu Mita | 12d00f6 | 2007-10-18 03:05:11 -0700 | [diff] [blame] | 1280 | if (!alien) { |
| 1281 | kfree(shared); |
| 1282 | kfree(nc); |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1283 | goto bad; |
Akinobu Mita | 12d00f6 | 2007-10-18 03:05:11 -0700 | [diff] [blame] | 1284 | } |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1285 | } |
| 1286 | cachep->array[cpu] = nc; |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 1287 | n = get_node(cachep, node); |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1288 | BUG_ON(!n); |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1289 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1290 | spin_lock_irq(&n->list_lock); |
| 1291 | if (!n->shared) { |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1292 | /* |
| 1293 | * We are serialised from CPU_DEAD or |
| 1294 | * CPU_UP_CANCELLED by the cpucontrol lock |
| 1295 | */ |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1296 | n->shared = shared; |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1297 | shared = NULL; |
| 1298 | } |
| 1299 | #ifdef CONFIG_NUMA |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1300 | if (!n->alien) { |
| 1301 | n->alien = alien; |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1302 | alien = NULL; |
| 1303 | } |
| 1304 | #endif |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1305 | spin_unlock_irq(&n->list_lock); |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1306 | kfree(shared); |
| 1307 | free_alien_cache(alien); |
Peter Zijlstra | 83835b3 | 2011-07-22 15:26:05 +0200 | [diff] [blame] | 1308 | if (cachep->flags & SLAB_DEBUG_OBJECTS) |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 1309 | slab_set_debugobj_lock_classes_node(cachep, n); |
Glauber Costa | 6ccfb5b | 2012-12-18 14:22:31 -0800 | [diff] [blame] | 1310 | else if (!OFF_SLAB(cachep) && |
| 1311 | !(cachep->flags & SLAB_DESTROY_BY_RCU)) |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 1312 | on_slab_lock_classes_node(cachep, n); |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1313 | } |
Pekka Enberg | ce79ddc | 2009-11-23 22:01:15 +0200 | [diff] [blame] | 1314 | init_node_lock_keys(node); |
| 1315 | |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1316 | return 0; |
| 1317 | bad: |
Akinobu Mita | 12d00f6 | 2007-10-18 03:05:11 -0700 | [diff] [blame] | 1318 | cpuup_canceled(cpu); |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1319 | return -ENOMEM; |
| 1320 | } |
| 1321 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 1322 | static int cpuup_callback(struct notifier_block *nfb, |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1323 | unsigned long action, void *hcpu) |
| 1324 | { |
| 1325 | long cpu = (long)hcpu; |
| 1326 | int err = 0; |
| 1327 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1328 | switch (action) { |
Heiko Carstens | 38c3bd9 | 2007-05-09 02:34:05 -0700 | [diff] [blame] | 1329 | case CPU_UP_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 1330 | case CPU_UP_PREPARE_FROZEN: |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 1331 | mutex_lock(&slab_mutex); |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1332 | err = cpuup_prepare(cpu); |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 1333 | mutex_unlock(&slab_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 | break; |
| 1335 | case CPU_ONLINE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 1336 | case CPU_ONLINE_FROZEN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | start_cpu_timer(cpu); |
| 1338 | break; |
| 1339 | #ifdef CONFIG_HOTPLUG_CPU |
Christoph Lameter | 5830c59 | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 1340 | case CPU_DOWN_PREPARE: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 1341 | case CPU_DOWN_PREPARE_FROZEN: |
Christoph Lameter | 5830c59 | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 1342 | /* |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 1343 | * Shutdown cache reaper. Note that the slab_mutex is |
Christoph Lameter | 5830c59 | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 1344 | * held so that if cache_reap() is invoked it cannot do |
| 1345 | * anything expensive but will only modify reap_work |
| 1346 | * and reschedule the timer. |
| 1347 | */ |
Tejun Heo | afe2c51 | 2010-12-14 16:21:17 +0100 | [diff] [blame] | 1348 | cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu)); |
Christoph Lameter | 5830c59 | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 1349 | /* Now the cache_reaper is guaranteed to be not running. */ |
Tejun Heo | 1871e52 | 2009-10-29 22:34:13 +0900 | [diff] [blame] | 1350 | per_cpu(slab_reap_work, cpu).work.func = NULL; |
Christoph Lameter | 5830c59 | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 1351 | break; |
| 1352 | case CPU_DOWN_FAILED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 1353 | case CPU_DOWN_FAILED_FROZEN: |
Christoph Lameter | 5830c59 | 2007-05-09 02:34:22 -0700 | [diff] [blame] | 1354 | start_cpu_timer(cpu); |
| 1355 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | case CPU_DEAD: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 1357 | case CPU_DEAD_FROZEN: |
Ravikiran G Thirumalai | 4484ebf | 2006-02-04 23:27:59 -0800 | [diff] [blame] | 1358 | /* |
| 1359 | * Even if all the cpus of a node are down, we don't free the |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1360 | * kmem_cache_node of any cache. This to avoid a race between |
Ravikiran G Thirumalai | 4484ebf | 2006-02-04 23:27:59 -0800 | [diff] [blame] | 1361 | * cpu_down, and a kmalloc allocation from another cpu for |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1362 | * memory from the node of the cpu going down. The node |
Ravikiran G Thirumalai | 4484ebf | 2006-02-04 23:27:59 -0800 | [diff] [blame] | 1363 | * structure is usually allocated from kmem_cache_create() and |
| 1364 | * gets destroyed at kmem_cache_destroy(). |
| 1365 | */ |
Simon Arlott | 183ff22 | 2007-10-20 01:27:18 +0200 | [diff] [blame] | 1366 | /* fall through */ |
Ravikiran G Thirumalai | 8f5be20 | 2006-12-06 20:32:14 -0800 | [diff] [blame] | 1367 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | case CPU_UP_CANCELED: |
Rafael J. Wysocki | 8bb7844 | 2007-05-09 02:35:10 -0700 | [diff] [blame] | 1369 | case CPU_UP_CANCELED_FROZEN: |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 1370 | mutex_lock(&slab_mutex); |
Akinobu Mita | fbf1e47 | 2007-10-18 03:05:09 -0700 | [diff] [blame] | 1371 | cpuup_canceled(cpu); |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 1372 | mutex_unlock(&slab_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | } |
Akinobu Mita | eac4068 | 2010-05-26 14:43:32 -0700 | [diff] [blame] | 1375 | return notifier_from_errno(err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | } |
| 1377 | |
Paul Gortmaker | 0db0628 | 2013-06-19 14:53:51 -0400 | [diff] [blame] | 1378 | static struct notifier_block cpucache_notifier = { |
Chandra Seetharaman | 74b85f3 | 2006-06-27 02:54:09 -0700 | [diff] [blame] | 1379 | &cpuup_callback, NULL, 0 |
| 1380 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1382 | #if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG) |
| 1383 | /* |
| 1384 | * Drains freelist for a node on each slab cache, used for memory hot-remove. |
| 1385 | * Returns -EBUSY if all objects cannot be drained so that the node is not |
| 1386 | * removed. |
| 1387 | * |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 1388 | * Must hold slab_mutex. |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1389 | */ |
Christoph Lameter | 6a67368 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1390 | static int __meminit drain_cache_node_node(int node) |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1391 | { |
| 1392 | struct kmem_cache *cachep; |
| 1393 | int ret = 0; |
| 1394 | |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 1395 | list_for_each_entry(cachep, &slab_caches, list) { |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1396 | struct kmem_cache_node *n; |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1397 | |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 1398 | n = get_node(cachep, node); |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1399 | if (!n) |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1400 | continue; |
| 1401 | |
Wanpeng Li | 0fa8103 | 2013-07-04 08:33:22 +0800 | [diff] [blame] | 1402 | drain_freelist(cachep, n, slabs_tofree(cachep, n)); |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1403 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1404 | if (!list_empty(&n->slabs_full) || |
| 1405 | !list_empty(&n->slabs_partial)) { |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1406 | ret = -EBUSY; |
| 1407 | break; |
| 1408 | } |
| 1409 | } |
| 1410 | return ret; |
| 1411 | } |
| 1412 | |
| 1413 | static int __meminit slab_memory_callback(struct notifier_block *self, |
| 1414 | unsigned long action, void *arg) |
| 1415 | { |
| 1416 | struct memory_notify *mnb = arg; |
| 1417 | int ret = 0; |
| 1418 | int nid; |
| 1419 | |
| 1420 | nid = mnb->status_change_nid; |
| 1421 | if (nid < 0) |
| 1422 | goto out; |
| 1423 | |
| 1424 | switch (action) { |
| 1425 | case MEM_GOING_ONLINE: |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 1426 | mutex_lock(&slab_mutex); |
Christoph Lameter | 6a67368 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1427 | ret = init_cache_node_node(nid); |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 1428 | mutex_unlock(&slab_mutex); |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1429 | break; |
| 1430 | case MEM_GOING_OFFLINE: |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 1431 | mutex_lock(&slab_mutex); |
Christoph Lameter | 6a67368 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1432 | ret = drain_cache_node_node(nid); |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 1433 | mutex_unlock(&slab_mutex); |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1434 | break; |
| 1435 | case MEM_ONLINE: |
| 1436 | case MEM_OFFLINE: |
| 1437 | case MEM_CANCEL_ONLINE: |
| 1438 | case MEM_CANCEL_OFFLINE: |
| 1439 | break; |
| 1440 | } |
| 1441 | out: |
Prarit Bhargava | 5fda1bd | 2011-03-22 16:30:49 -0700 | [diff] [blame] | 1442 | return notifier_from_errno(ret); |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1443 | } |
| 1444 | #endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */ |
| 1445 | |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1446 | /* |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1447 | * swap the static kmem_cache_node with kmalloced memory |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1448 | */ |
Christoph Lameter | 6744f08 | 2013-01-10 19:12:17 +0000 | [diff] [blame] | 1449 | static void __init init_list(struct kmem_cache *cachep, struct kmem_cache_node *list, |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1450 | int nodeid) |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1451 | { |
Christoph Lameter | 6744f08 | 2013-01-10 19:12:17 +0000 | [diff] [blame] | 1452 | struct kmem_cache_node *ptr; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1453 | |
Christoph Lameter | 6744f08 | 2013-01-10 19:12:17 +0000 | [diff] [blame] | 1454 | ptr = kmalloc_node(sizeof(struct kmem_cache_node), GFP_NOWAIT, nodeid); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1455 | BUG_ON(!ptr); |
| 1456 | |
Christoph Lameter | 6744f08 | 2013-01-10 19:12:17 +0000 | [diff] [blame] | 1457 | memcpy(ptr, list, sizeof(struct kmem_cache_node)); |
Ingo Molnar | 2b2d549 | 2006-07-03 00:25:28 -0700 | [diff] [blame] | 1458 | /* |
| 1459 | * Do not assume that spinlocks can be initialized via memcpy: |
| 1460 | */ |
| 1461 | spin_lock_init(&ptr->list_lock); |
| 1462 | |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1463 | MAKE_ALL_LISTS(cachep, ptr, nodeid); |
Christoph Lameter | 6a67368 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1464 | cachep->node[nodeid] = ptr; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 1467 | /* |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1468 | * For setting up all the kmem_cache_node for cache whose buffer_size is same as |
| 1469 | * size of kmem_cache_node. |
Pekka Enberg | 556a169 | 2008-01-25 08:20:51 +0200 | [diff] [blame] | 1470 | */ |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1471 | static void __init set_up_node(struct kmem_cache *cachep, int index) |
Pekka Enberg | 556a169 | 2008-01-25 08:20:51 +0200 | [diff] [blame] | 1472 | { |
| 1473 | int node; |
| 1474 | |
| 1475 | for_each_online_node(node) { |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1476 | cachep->node[node] = &init_kmem_cache_node[index + node]; |
Christoph Lameter | 6a67368 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1477 | cachep->node[node]->next_reap = jiffies + |
Jianyu Zhan | 5f0985b | 2014-03-30 17:02:20 +0800 | [diff] [blame] | 1478 | REAPTIMEOUT_NODE + |
| 1479 | ((unsigned long)cachep) % REAPTIMEOUT_NODE; |
Pekka Enberg | 556a169 | 2008-01-25 08:20:51 +0200 | [diff] [blame] | 1480 | } |
| 1481 | } |
| 1482 | |
| 1483 | /* |
Christoph Lameter | 3c58346 | 2012-11-28 16:23:01 +0000 | [diff] [blame] | 1484 | * The memory after the last cpu cache pointer is used for the |
Christoph Lameter | 6a67368 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1485 | * the node pointer. |
Christoph Lameter | 3c58346 | 2012-11-28 16:23:01 +0000 | [diff] [blame] | 1486 | */ |
Christoph Lameter | 6a67368 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1487 | static void setup_node_pointer(struct kmem_cache *cachep) |
Christoph Lameter | 3c58346 | 2012-11-28 16:23:01 +0000 | [diff] [blame] | 1488 | { |
Christoph Lameter | 6a67368 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1489 | cachep->node = (struct kmem_cache_node **)&cachep->array[nr_cpu_ids]; |
Christoph Lameter | 3c58346 | 2012-11-28 16:23:01 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | /* |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 1493 | * Initialisation. Called after the page allocator have been initialised and |
| 1494 | * before smp_init(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | */ |
| 1496 | void __init kmem_cache_init(void) |
| 1497 | { |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1498 | int i; |
| 1499 | |
Joonsoo Kim | 6812670 | 2013-10-24 10:07:42 +0900 | [diff] [blame] | 1500 | BUILD_BUG_ON(sizeof(((struct page *)NULL)->lru) < |
| 1501 | sizeof(struct rcu_head)); |
Christoph Lameter | 9b030cb | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 1502 | kmem_cache = &kmem_cache_boot; |
Christoph Lameter | 6a67368 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1503 | setup_node_pointer(kmem_cache); |
Christoph Lameter | 9b030cb | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 1504 | |
Mel Gorman | b6e68bc | 2009-06-16 15:32:16 -0700 | [diff] [blame] | 1505 | if (num_possible_nodes() == 1) |
Siddha, Suresh B | 62918a0 | 2007-05-02 19:27:18 +0200 | [diff] [blame] | 1506 | use_alien_caches = 0; |
| 1507 | |
Christoph Lameter | 3c58346 | 2012-11-28 16:23:01 +0000 | [diff] [blame] | 1508 | for (i = 0; i < NUM_INIT_LISTS; i++) |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1509 | kmem_cache_node_init(&init_kmem_cache_node[i]); |
Christoph Lameter | 3c58346 | 2012-11-28 16:23:01 +0000 | [diff] [blame] | 1510 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1511 | set_up_node(kmem_cache, CACHE_CACHE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | |
| 1513 | /* |
| 1514 | * Fragmentation resistance on low memory - only use bigger |
David Rientjes | 3df1ccc | 2011-10-18 22:09:28 -0700 | [diff] [blame] | 1515 | * page orders on machines with more than 32MB of memory if |
| 1516 | * not overridden on the command line. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | */ |
David Rientjes | 3df1ccc | 2011-10-18 22:09:28 -0700 | [diff] [blame] | 1518 | if (!slab_max_order_set && totalram_pages > (32 << 20) >> PAGE_SHIFT) |
David Rientjes | 543585c | 2011-10-18 22:09:24 -0700 | [diff] [blame] | 1519 | slab_max_order = SLAB_MAX_ORDER_HI; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | /* Bootstrap is tricky, because several objects are allocated |
| 1522 | * from caches that do not exist yet: |
Christoph Lameter | 9b030cb | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 1523 | * 1) initialize the kmem_cache cache: it contains the struct |
| 1524 | * kmem_cache structures of all caches, except kmem_cache itself: |
| 1525 | * kmem_cache is statically allocated. |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1526 | * Initially an __init data area is used for the head array and the |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1527 | * kmem_cache_node structures, it's replaced with a kmalloc allocated |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1528 | * array at the end of the bootstrap. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | * 2) Create the first kmalloc cache. |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 1530 | * The struct kmem_cache for the new cache is allocated normally. |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1531 | * An __init data area is used for the head array. |
| 1532 | * 3) Create the remaining kmalloc caches, with minimally sized |
| 1533 | * head arrays. |
Christoph Lameter | 9b030cb | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 1534 | * 4) Replace the __init data head arrays for kmem_cache and the first |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1535 | * kmalloc cache with kmalloc allocated arrays. |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1536 | * 5) Replace the __init data for kmem_cache_node for kmem_cache and |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1537 | * the other cache's with kmalloc allocated memory. |
| 1538 | * 6) Resize the head arrays of the kmalloc caches to their final sizes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1539 | */ |
| 1540 | |
Christoph Lameter | 9b030cb | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 1541 | /* 1) create the kmem_cache */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | |
Eric Dumazet | 8da3430 | 2007-05-06 14:49:29 -0700 | [diff] [blame] | 1543 | /* |
Eric Dumazet | b56efcf | 2011-07-20 19:04:23 +0200 | [diff] [blame] | 1544 | * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids |
Eric Dumazet | 8da3430 | 2007-05-06 14:49:29 -0700 | [diff] [blame] | 1545 | */ |
Christoph Lameter | 2f9baa9 | 2012-11-28 16:23:09 +0000 | [diff] [blame] | 1546 | create_boot_cache(kmem_cache, "kmem_cache", |
| 1547 | offsetof(struct kmem_cache, array[nr_cpu_ids]) + |
Christoph Lameter | 6744f08 | 2013-01-10 19:12:17 +0000 | [diff] [blame] | 1548 | nr_node_ids * sizeof(struct kmem_cache_node *), |
Christoph Lameter | 2f9baa9 | 2012-11-28 16:23:09 +0000 | [diff] [blame] | 1549 | SLAB_HWCACHE_ALIGN); |
| 1550 | list_add(&kmem_cache->list, &slab_caches); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | |
| 1552 | /* 2+3) create the kmalloc caches */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1553 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 1554 | /* |
| 1555 | * Initialize the caches that provide memory for the array cache and the |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1556 | * kmem_cache_node structures first. Without this, further allocations will |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 1557 | * bug. |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1558 | */ |
| 1559 | |
Christoph Lameter | e336601 | 2013-01-10 19:14:18 +0000 | [diff] [blame] | 1560 | kmalloc_caches[INDEX_AC] = create_kmalloc_cache("kmalloc-ac", |
| 1561 | kmalloc_size(INDEX_AC), ARCH_KMALLOC_FLAGS); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1562 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1563 | if (INDEX_AC != INDEX_NODE) |
| 1564 | kmalloc_caches[INDEX_NODE] = |
| 1565 | create_kmalloc_cache("kmalloc-node", |
| 1566 | kmalloc_size(INDEX_NODE), ARCH_KMALLOC_FLAGS); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1567 | |
Ingo Molnar | e0a4272 | 2006-06-23 02:03:46 -0700 | [diff] [blame] | 1568 | slab_early_init = 0; |
| 1569 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | /* 4) Replace the bootstrap head arrays */ |
| 1571 | { |
Ingo Molnar | 2b2d549 | 2006-07-03 00:25:28 -0700 | [diff] [blame] | 1572 | struct array_cache *ptr; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1573 | |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 1574 | ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1575 | |
Christoph Lameter | 9b030cb | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 1576 | memcpy(ptr, cpu_cache_get(kmem_cache), |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1577 | sizeof(struct arraycache_init)); |
Ingo Molnar | 2b2d549 | 2006-07-03 00:25:28 -0700 | [diff] [blame] | 1578 | /* |
| 1579 | * Do not assume that spinlocks can be initialized via memcpy: |
| 1580 | */ |
| 1581 | spin_lock_init(&ptr->lock); |
| 1582 | |
Christoph Lameter | 9b030cb | 2012-09-05 00:20:33 +0000 | [diff] [blame] | 1583 | kmem_cache->array[smp_processor_id()] = ptr; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1584 | |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 1585 | ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1586 | |
Christoph Lameter | e336601 | 2013-01-10 19:14:18 +0000 | [diff] [blame] | 1587 | BUG_ON(cpu_cache_get(kmalloc_caches[INDEX_AC]) |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1588 | != &initarray_generic.cache); |
Christoph Lameter | e336601 | 2013-01-10 19:14:18 +0000 | [diff] [blame] | 1589 | memcpy(ptr, cpu_cache_get(kmalloc_caches[INDEX_AC]), |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1590 | sizeof(struct arraycache_init)); |
Ingo Molnar | 2b2d549 | 2006-07-03 00:25:28 -0700 | [diff] [blame] | 1591 | /* |
| 1592 | * Do not assume that spinlocks can be initialized via memcpy: |
| 1593 | */ |
| 1594 | spin_lock_init(&ptr->lock); |
| 1595 | |
Christoph Lameter | e336601 | 2013-01-10 19:14:18 +0000 | [diff] [blame] | 1596 | kmalloc_caches[INDEX_AC]->array[smp_processor_id()] = ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1597 | } |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1598 | /* 5) Replace the bootstrap kmem_cache_node */ |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1599 | { |
Pekka Enberg | 1ca4cb2 | 2006-10-06 00:43:52 -0700 | [diff] [blame] | 1600 | int nid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | |
Mel Gorman | 9c09a95 | 2008-01-24 05:49:54 -0800 | [diff] [blame] | 1602 | for_each_online_node(nid) { |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1603 | init_list(kmem_cache, &init_kmem_cache_node[CACHE_CACHE + nid], nid); |
Pekka Enberg | 556a169 | 2008-01-25 08:20:51 +0200 | [diff] [blame] | 1604 | |
Christoph Lameter | e336601 | 2013-01-10 19:14:18 +0000 | [diff] [blame] | 1605 | init_list(kmalloc_caches[INDEX_AC], |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1606 | &init_kmem_cache_node[SIZE_AC + nid], nid); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1607 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1608 | if (INDEX_AC != INDEX_NODE) { |
| 1609 | init_list(kmalloc_caches[INDEX_NODE], |
| 1610 | &init_kmem_cache_node[SIZE_NODE + nid], nid); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1611 | } |
| 1612 | } |
| 1613 | } |
| 1614 | |
Christoph Lameter | f97d5f63 | 2013-01-10 19:12:17 +0000 | [diff] [blame] | 1615 | create_kmalloc_caches(ARCH_KMALLOC_FLAGS); |
Pekka Enberg | 8429db5 | 2009-06-12 15:58:59 +0300 | [diff] [blame] | 1616 | } |
Ravikiran G Thirumalai | 056c624 | 2006-09-25 23:31:38 -0700 | [diff] [blame] | 1617 | |
Pekka Enberg | 8429db5 | 2009-06-12 15:58:59 +0300 | [diff] [blame] | 1618 | void __init kmem_cache_init_late(void) |
| 1619 | { |
| 1620 | struct kmem_cache *cachep; |
| 1621 | |
Christoph Lameter | 97d0660 | 2012-07-06 15:25:11 -0500 | [diff] [blame] | 1622 | slab_state = UP; |
Peter Zijlstra | 52cef18 | 2011-11-28 21:12:40 +0100 | [diff] [blame] | 1623 | |
Pekka Enberg | 8429db5 | 2009-06-12 15:58:59 +0300 | [diff] [blame] | 1624 | /* 6) resize the head arrays to their final sizes */ |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 1625 | mutex_lock(&slab_mutex); |
| 1626 | list_for_each_entry(cachep, &slab_caches, list) |
Pekka Enberg | 8429db5 | 2009-06-12 15:58:59 +0300 | [diff] [blame] | 1627 | if (enable_cpucache(cachep, GFP_NOWAIT)) |
| 1628 | BUG(); |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 1629 | mutex_unlock(&slab_mutex); |
Ravikiran G Thirumalai | 056c624 | 2006-09-25 23:31:38 -0700 | [diff] [blame] | 1630 | |
Michael Wang | 947ca18 | 2012-09-05 10:33:18 +0800 | [diff] [blame] | 1631 | /* Annotate slab for lockdep -- annotate the malloc caches */ |
| 1632 | init_lock_keys(); |
| 1633 | |
Christoph Lameter | 97d0660 | 2012-07-06 15:25:11 -0500 | [diff] [blame] | 1634 | /* Done! */ |
| 1635 | slab_state = FULL; |
| 1636 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 1637 | /* |
| 1638 | * Register a cpu startup notifier callback that initializes |
| 1639 | * cpu_cache_get for all new cpus |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1640 | */ |
| 1641 | register_cpu_notifier(&cpucache_notifier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1642 | |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1643 | #ifdef CONFIG_NUMA |
| 1644 | /* |
| 1645 | * Register a memory hotplug callback that initializes and frees |
Christoph Lameter | 6a67368 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1646 | * node. |
David Rientjes | 8f9f8d9 | 2010-03-27 19:40:47 -0700 | [diff] [blame] | 1647 | */ |
| 1648 | hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI); |
| 1649 | #endif |
| 1650 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 1651 | /* |
| 1652 | * The reap timers are started later, with a module init call: That part |
| 1653 | * of the kernel is not yet operational. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1654 | */ |
| 1655 | } |
| 1656 | |
| 1657 | static int __init cpucache_init(void) |
| 1658 | { |
| 1659 | int cpu; |
| 1660 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 1661 | /* |
| 1662 | * Register the timers that return unneeded pages to the page allocator |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1663 | */ |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 1664 | for_each_online_cpu(cpu) |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 1665 | start_cpu_timer(cpu); |
Glauber Costa | a164f896 | 2012-06-21 00:59:18 +0400 | [diff] [blame] | 1666 | |
| 1667 | /* Done! */ |
Christoph Lameter | 97d0660 | 2012-07-06 15:25:11 -0500 | [diff] [blame] | 1668 | slab_state = FULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1669 | return 0; |
| 1670 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | __initcall(cpucache_init); |
| 1672 | |
Rafael Aquini | 8bdec19 | 2012-03-09 17:27:27 -0300 | [diff] [blame] | 1673 | static noinline void |
| 1674 | slab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid) |
| 1675 | { |
David Rientjes | 9a02d69 | 2014-06-04 16:06:36 -0700 | [diff] [blame] | 1676 | #if DEBUG |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1677 | struct kmem_cache_node *n; |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 1678 | struct page *page; |
Rafael Aquini | 8bdec19 | 2012-03-09 17:27:27 -0300 | [diff] [blame] | 1679 | unsigned long flags; |
| 1680 | int node; |
David Rientjes | 9a02d69 | 2014-06-04 16:06:36 -0700 | [diff] [blame] | 1681 | static DEFINE_RATELIMIT_STATE(slab_oom_rs, DEFAULT_RATELIMIT_INTERVAL, |
| 1682 | DEFAULT_RATELIMIT_BURST); |
| 1683 | |
| 1684 | if ((gfpflags & __GFP_NOWARN) || !__ratelimit(&slab_oom_rs)) |
| 1685 | return; |
Rafael Aquini | 8bdec19 | 2012-03-09 17:27:27 -0300 | [diff] [blame] | 1686 | |
| 1687 | printk(KERN_WARNING |
| 1688 | "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n", |
| 1689 | nodeid, gfpflags); |
| 1690 | printk(KERN_WARNING " cache: %s, object size: %d, order: %d\n", |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 1691 | cachep->name, cachep->size, cachep->gfporder); |
Rafael Aquini | 8bdec19 | 2012-03-09 17:27:27 -0300 | [diff] [blame] | 1692 | |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 1693 | for_each_kmem_cache_node(cachep, node, n) { |
Rafael Aquini | 8bdec19 | 2012-03-09 17:27:27 -0300 | [diff] [blame] | 1694 | unsigned long active_objs = 0, num_objs = 0, free_objects = 0; |
| 1695 | unsigned long active_slabs = 0, num_slabs = 0; |
| 1696 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1697 | spin_lock_irqsave(&n->list_lock, flags); |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 1698 | list_for_each_entry(page, &n->slabs_full, lru) { |
Rafael Aquini | 8bdec19 | 2012-03-09 17:27:27 -0300 | [diff] [blame] | 1699 | active_objs += cachep->num; |
| 1700 | active_slabs++; |
| 1701 | } |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 1702 | list_for_each_entry(page, &n->slabs_partial, lru) { |
| 1703 | active_objs += page->active; |
Rafael Aquini | 8bdec19 | 2012-03-09 17:27:27 -0300 | [diff] [blame] | 1704 | active_slabs++; |
| 1705 | } |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 1706 | list_for_each_entry(page, &n->slabs_free, lru) |
Rafael Aquini | 8bdec19 | 2012-03-09 17:27:27 -0300 | [diff] [blame] | 1707 | num_slabs++; |
| 1708 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 1709 | free_objects += n->free_objects; |
| 1710 | spin_unlock_irqrestore(&n->list_lock, flags); |
Rafael Aquini | 8bdec19 | 2012-03-09 17:27:27 -0300 | [diff] [blame] | 1711 | |
| 1712 | num_slabs += active_slabs; |
| 1713 | num_objs = num_slabs * cachep->num; |
| 1714 | printk(KERN_WARNING |
| 1715 | " node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n", |
| 1716 | node, active_slabs, num_slabs, active_objs, num_objs, |
| 1717 | free_objects); |
| 1718 | } |
David Rientjes | 9a02d69 | 2014-06-04 16:06:36 -0700 | [diff] [blame] | 1719 | #endif |
Rafael Aquini | 8bdec19 | 2012-03-09 17:27:27 -0300 | [diff] [blame] | 1720 | } |
| 1721 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1722 | /* |
| 1723 | * Interface to system's page allocator. No need to hold the cache-lock. |
| 1724 | * |
| 1725 | * If we requested dmaable memory, we will get it. Even if we |
| 1726 | * did not request dmaable memory, we might get it, but that |
| 1727 | * would be relatively rare and ignorable. |
| 1728 | */ |
Joonsoo Kim | 0c3aa83 | 2013-10-24 10:07:38 +0900 | [diff] [blame] | 1729 | static struct page *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, |
| 1730 | int nodeid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | { |
| 1732 | struct page *page; |
Christoph Hellwig | e1b6aa6 | 2006-06-23 02:03:17 -0700 | [diff] [blame] | 1733 | int nr_pages; |
Christoph Lameter | 765c450 | 2006-09-27 01:50:08 -0700 | [diff] [blame] | 1734 | |
Glauber Costa | a618e89 | 2012-06-14 16:17:21 +0400 | [diff] [blame] | 1735 | flags |= cachep->allocflags; |
Mel Gorman | e12ba74 | 2007-10-16 01:25:52 -0700 | [diff] [blame] | 1736 | if (cachep->flags & SLAB_RECLAIM_ACCOUNT) |
| 1737 | flags |= __GFP_RECLAIMABLE; |
Christoph Hellwig | e1b6aa6 | 2006-06-23 02:03:17 -0700 | [diff] [blame] | 1738 | |
Vladimir Davydov | 5dfb417 | 2014-06-04 16:06:38 -0700 | [diff] [blame] | 1739 | if (memcg_charge_slab(cachep, flags, cachep->gfporder)) |
| 1740 | return NULL; |
| 1741 | |
Linus Torvalds | 517d086 | 2009-06-16 19:50:13 -0700 | [diff] [blame] | 1742 | page = alloc_pages_exact_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder); |
Rafael Aquini | 8bdec19 | 2012-03-09 17:27:27 -0300 | [diff] [blame] | 1743 | if (!page) { |
Vladimir Davydov | 5dfb417 | 2014-06-04 16:06:38 -0700 | [diff] [blame] | 1744 | memcg_uncharge_slab(cachep, cachep->gfporder); |
David Rientjes | 9a02d69 | 2014-06-04 16:06:36 -0700 | [diff] [blame] | 1745 | slab_out_of_memory(cachep, flags, nodeid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | return NULL; |
Rafael Aquini | 8bdec19 | 2012-03-09 17:27:27 -0300 | [diff] [blame] | 1747 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | |
Mel Gorman | b37f1dd | 2012-07-31 16:44:03 -0700 | [diff] [blame] | 1749 | /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */ |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 1750 | if (unlikely(page->pfmemalloc)) |
| 1751 | pfmemalloc_active = true; |
| 1752 | |
Christoph Hellwig | e1b6aa6 | 2006-06-23 02:03:17 -0700 | [diff] [blame] | 1753 | nr_pages = (1 << cachep->gfporder); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | if (cachep->flags & SLAB_RECLAIM_ACCOUNT) |
Christoph Lameter | 972d1a7 | 2006-09-25 23:31:51 -0700 | [diff] [blame] | 1755 | add_zone_page_state(page_zone(page), |
| 1756 | NR_SLAB_RECLAIMABLE, nr_pages); |
| 1757 | else |
| 1758 | add_zone_page_state(page_zone(page), |
| 1759 | NR_SLAB_UNRECLAIMABLE, nr_pages); |
Joonsoo Kim | a57a498 | 2013-10-24 10:07:44 +0900 | [diff] [blame] | 1760 | __SetPageSlab(page); |
| 1761 | if (page->pfmemalloc) |
| 1762 | SetPageSlabPfmemalloc(page); |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 1763 | |
Vegard Nossum | b1eeab6 | 2008-11-25 16:55:53 +0100 | [diff] [blame] | 1764 | if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) { |
| 1765 | kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid); |
| 1766 | |
| 1767 | if (cachep->ctor) |
| 1768 | kmemcheck_mark_uninitialized_pages(page, nr_pages); |
| 1769 | else |
| 1770 | kmemcheck_mark_unallocated_pages(page, nr_pages); |
| 1771 | } |
Pekka Enberg | c175eea | 2008-05-09 20:35:53 +0200 | [diff] [blame] | 1772 | |
Joonsoo Kim | 0c3aa83 | 2013-10-24 10:07:38 +0900 | [diff] [blame] | 1773 | return page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | /* |
| 1777 | * Interface to system's page release. |
| 1778 | */ |
Joonsoo Kim | 0c3aa83 | 2013-10-24 10:07:38 +0900 | [diff] [blame] | 1779 | static void kmem_freepages(struct kmem_cache *cachep, struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | { |
Joonsoo Kim | a57a498 | 2013-10-24 10:07:44 +0900 | [diff] [blame] | 1781 | const unsigned long nr_freed = (1 << cachep->gfporder); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | |
Vegard Nossum | b1eeab6 | 2008-11-25 16:55:53 +0100 | [diff] [blame] | 1783 | kmemcheck_free_shadow(page, cachep->gfporder); |
Pekka Enberg | c175eea | 2008-05-09 20:35:53 +0200 | [diff] [blame] | 1784 | |
Christoph Lameter | 972d1a7 | 2006-09-25 23:31:51 -0700 | [diff] [blame] | 1785 | if (cachep->flags & SLAB_RECLAIM_ACCOUNT) |
| 1786 | sub_zone_page_state(page_zone(page), |
| 1787 | NR_SLAB_RECLAIMABLE, nr_freed); |
| 1788 | else |
| 1789 | sub_zone_page_state(page_zone(page), |
| 1790 | NR_SLAB_UNRECLAIMABLE, nr_freed); |
Joonsoo Kim | 73293c2 | 2013-10-24 10:07:37 +0900 | [diff] [blame] | 1791 | |
Joonsoo Kim | a57a498 | 2013-10-24 10:07:44 +0900 | [diff] [blame] | 1792 | BUG_ON(!PageSlab(page)); |
Joonsoo Kim | 73293c2 | 2013-10-24 10:07:37 +0900 | [diff] [blame] | 1793 | __ClearPageSlabPfmemalloc(page); |
Joonsoo Kim | a57a498 | 2013-10-24 10:07:44 +0900 | [diff] [blame] | 1794 | __ClearPageSlab(page); |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 1795 | page_mapcount_reset(page); |
| 1796 | page->mapping = NULL; |
Glauber Costa | 1f458cb | 2012-12-18 14:22:50 -0800 | [diff] [blame] | 1797 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | if (current->reclaim_state) |
| 1799 | current->reclaim_state->reclaimed_slab += nr_freed; |
Vladimir Davydov | 5dfb417 | 2014-06-04 16:06:38 -0700 | [diff] [blame] | 1800 | __free_pages(page, cachep->gfporder); |
| 1801 | memcg_uncharge_slab(cachep, cachep->gfporder); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | static void kmem_rcu_free(struct rcu_head *head) |
| 1805 | { |
Joonsoo Kim | 6812670 | 2013-10-24 10:07:42 +0900 | [diff] [blame] | 1806 | struct kmem_cache *cachep; |
| 1807 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1808 | |
Joonsoo Kim | 6812670 | 2013-10-24 10:07:42 +0900 | [diff] [blame] | 1809 | page = container_of(head, struct page, rcu_head); |
| 1810 | cachep = page->slab_cache; |
| 1811 | |
| 1812 | kmem_freepages(cachep, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | } |
| 1814 | |
| 1815 | #if DEBUG |
| 1816 | |
| 1817 | #ifdef CONFIG_DEBUG_PAGEALLOC |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 1818 | static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr, |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1819 | unsigned long caller) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | { |
Christoph Lameter | 8c138bc | 2012-06-13 10:24:58 -0500 | [diff] [blame] | 1821 | int size = cachep->object_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | |
Manfred Spraul | 3dafccf | 2006-02-01 03:05:42 -0800 | [diff] [blame] | 1823 | addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1824 | |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1825 | if (size < 5 * sizeof(unsigned long)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1826 | return; |
| 1827 | |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1828 | *addr++ = 0x12345678; |
| 1829 | *addr++ = caller; |
| 1830 | *addr++ = smp_processor_id(); |
| 1831 | size -= 3 * sizeof(unsigned long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | { |
| 1833 | unsigned long *sptr = &caller; |
| 1834 | unsigned long svalue; |
| 1835 | |
| 1836 | while (!kstack_end(sptr)) { |
| 1837 | svalue = *sptr++; |
| 1838 | if (kernel_text_address(svalue)) { |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1839 | *addr++ = svalue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | size -= sizeof(unsigned long); |
| 1841 | if (size <= sizeof(unsigned long)) |
| 1842 | break; |
| 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | } |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1847 | *addr++ = 0x87654321; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | } |
| 1849 | #endif |
| 1850 | |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 1851 | static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | { |
Christoph Lameter | 8c138bc | 2012-06-13 10:24:58 -0500 | [diff] [blame] | 1853 | int size = cachep->object_size; |
Manfred Spraul | 3dafccf | 2006-02-01 03:05:42 -0800 | [diff] [blame] | 1854 | addr = &((char *)addr)[obj_offset(cachep)]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1855 | |
| 1856 | memset(addr, val, size); |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1857 | *(unsigned char *)(addr + size - 1) = POISON_END; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | static void dump_line(char *data, int offset, int limit) |
| 1861 | { |
| 1862 | int i; |
Dave Jones | aa83aa4 | 2006-09-29 01:59:51 -0700 | [diff] [blame] | 1863 | unsigned char error = 0; |
| 1864 | int bad_count = 0; |
| 1865 | |
Sebastian Andrzej Siewior | fdde6ab | 2011-07-29 18:22:13 +0200 | [diff] [blame] | 1866 | printk(KERN_ERR "%03x: ", offset); |
Dave Jones | aa83aa4 | 2006-09-29 01:59:51 -0700 | [diff] [blame] | 1867 | for (i = 0; i < limit; i++) { |
| 1868 | if (data[offset + i] != POISON_FREE) { |
| 1869 | error = data[offset + i]; |
| 1870 | bad_count++; |
| 1871 | } |
Dave Jones | aa83aa4 | 2006-09-29 01:59:51 -0700 | [diff] [blame] | 1872 | } |
Sebastian Andrzej Siewior | fdde6ab | 2011-07-29 18:22:13 +0200 | [diff] [blame] | 1873 | print_hex_dump(KERN_CONT, "", 0, 16, 1, |
| 1874 | &data[offset], limit, 1); |
Dave Jones | aa83aa4 | 2006-09-29 01:59:51 -0700 | [diff] [blame] | 1875 | |
| 1876 | if (bad_count == 1) { |
| 1877 | error ^= POISON_FREE; |
| 1878 | if (!(error & (error - 1))) { |
| 1879 | printk(KERN_ERR "Single bit error detected. Probably " |
| 1880 | "bad RAM.\n"); |
| 1881 | #ifdef CONFIG_X86 |
| 1882 | printk(KERN_ERR "Run memtest86+ or a similar memory " |
| 1883 | "test tool.\n"); |
| 1884 | #else |
| 1885 | printk(KERN_ERR "Run a memory test tool.\n"); |
| 1886 | #endif |
| 1887 | } |
| 1888 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | } |
| 1890 | #endif |
| 1891 | |
| 1892 | #if DEBUG |
| 1893 | |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 1894 | static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | { |
| 1896 | int i, size; |
| 1897 | char *realobj; |
| 1898 | |
| 1899 | if (cachep->flags & SLAB_RED_ZONE) { |
David Woodhouse | b46b8f1 | 2007-05-08 00:22:59 -0700 | [diff] [blame] | 1900 | printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n", |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 1901 | *dbg_redzone1(cachep, objp), |
| 1902 | *dbg_redzone2(cachep, objp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | if (cachep->flags & SLAB_STORE_USER) { |
Joe Perches | 071361d | 2012-12-12 10:19:12 -0800 | [diff] [blame] | 1906 | printk(KERN_ERR "Last user: [<%p>](%pSR)\n", |
| 1907 | *dbg_userword(cachep, objp), |
| 1908 | *dbg_userword(cachep, objp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | } |
Manfred Spraul | 3dafccf | 2006-02-01 03:05:42 -0800 | [diff] [blame] | 1910 | realobj = (char *)objp + obj_offset(cachep); |
Christoph Lameter | 8c138bc | 2012-06-13 10:24:58 -0500 | [diff] [blame] | 1911 | size = cachep->object_size; |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1912 | for (i = 0; i < size && lines; i += 16, lines--) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | int limit; |
| 1914 | limit = 16; |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1915 | if (i + limit > size) |
| 1916 | limit = size - i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | dump_line(realobj, i, limit); |
| 1918 | } |
| 1919 | } |
| 1920 | |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 1921 | static void check_poison_obj(struct kmem_cache *cachep, void *objp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | { |
| 1923 | char *realobj; |
| 1924 | int size, i; |
| 1925 | int lines = 0; |
| 1926 | |
Manfred Spraul | 3dafccf | 2006-02-01 03:05:42 -0800 | [diff] [blame] | 1927 | realobj = (char *)objp + obj_offset(cachep); |
Christoph Lameter | 8c138bc | 2012-06-13 10:24:58 -0500 | [diff] [blame] | 1928 | size = cachep->object_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1930 | for (i = 0; i < size; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | char exp = POISON_FREE; |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1932 | if (i == size - 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | exp = POISON_END; |
| 1934 | if (realobj[i] != exp) { |
| 1935 | int limit; |
| 1936 | /* Mismatch ! */ |
| 1937 | /* Print header */ |
| 1938 | if (lines == 0) { |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1939 | printk(KERN_ERR |
Dave Jones | face37f | 2011-11-15 15:03:52 -0800 | [diff] [blame] | 1940 | "Slab corruption (%s): %s start=%p, len=%d\n", |
| 1941 | print_tainted(), cachep->name, realobj, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | print_objinfo(cachep, objp, 0); |
| 1943 | } |
| 1944 | /* Hexdump the affected line */ |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1945 | i = (i / 16) * 16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | limit = 16; |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1947 | if (i + limit > size) |
| 1948 | limit = size - i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | dump_line(realobj, i, limit); |
| 1950 | i += 16; |
| 1951 | lines++; |
| 1952 | /* Limit to 5 lines */ |
| 1953 | if (lines > 5) |
| 1954 | break; |
| 1955 | } |
| 1956 | } |
| 1957 | if (lines != 0) { |
| 1958 | /* Print some data about the neighboring objects, if they |
| 1959 | * exist: |
| 1960 | */ |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 1961 | struct page *page = virt_to_head_page(objp); |
Pekka Enberg | 8fea4e9 | 2006-03-22 00:08:10 -0800 | [diff] [blame] | 1962 | unsigned int objnr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 1964 | objnr = obj_to_index(cachep, page, objp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | if (objnr) { |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 1966 | objp = index_to_obj(cachep, page, objnr - 1); |
Manfred Spraul | 3dafccf | 2006-02-01 03:05:42 -0800 | [diff] [blame] | 1967 | realobj = (char *)objp + obj_offset(cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | printk(KERN_ERR "Prev obj: start=%p, len=%d\n", |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1969 | realobj, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | print_objinfo(cachep, objp, 2); |
| 1971 | } |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1972 | if (objnr + 1 < cachep->num) { |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 1973 | objp = index_to_obj(cachep, page, objnr + 1); |
Manfred Spraul | 3dafccf | 2006-02-01 03:05:42 -0800 | [diff] [blame] | 1974 | realobj = (char *)objp + obj_offset(cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | printk(KERN_ERR "Next obj: start=%p, len=%d\n", |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1976 | realobj, size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | print_objinfo(cachep, objp, 2); |
| 1978 | } |
| 1979 | } |
| 1980 | } |
| 1981 | #endif |
| 1982 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | #if DEBUG |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 1984 | static void slab_destroy_debugcheck(struct kmem_cache *cachep, |
| 1985 | struct page *page) |
Matthew Dobson | 12dd36f | 2006-02-01 03:05:46 -0800 | [diff] [blame] | 1986 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | int i; |
| 1988 | for (i = 0; i < cachep->num; i++) { |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 1989 | void *objp = index_to_obj(cachep, page, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | |
| 1991 | if (cachep->flags & SLAB_POISON) { |
| 1992 | #ifdef CONFIG_DEBUG_PAGEALLOC |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 1993 | if (cachep->size % PAGE_SIZE == 0 && |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 1994 | OFF_SLAB(cachep)) |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 1995 | kernel_map_pages(virt_to_page(objp), |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 1996 | cachep->size / PAGE_SIZE, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | else |
| 1998 | check_poison_obj(cachep, objp); |
| 1999 | #else |
| 2000 | check_poison_obj(cachep, objp); |
| 2001 | #endif |
| 2002 | } |
| 2003 | if (cachep->flags & SLAB_RED_ZONE) { |
| 2004 | if (*dbg_redzone1(cachep, objp) != RED_INACTIVE) |
| 2005 | slab_error(cachep, "start of a freed object " |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 2006 | "was overwritten"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | if (*dbg_redzone2(cachep, objp) != RED_INACTIVE) |
| 2008 | slab_error(cachep, "end of a freed object " |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 2009 | "was overwritten"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | } |
Matthew Dobson | 12dd36f | 2006-02-01 03:05:46 -0800 | [diff] [blame] | 2012 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | #else |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2014 | static void slab_destroy_debugcheck(struct kmem_cache *cachep, |
| 2015 | struct page *page) |
Matthew Dobson | 12dd36f | 2006-02-01 03:05:46 -0800 | [diff] [blame] | 2016 | { |
Matthew Dobson | 12dd36f | 2006-02-01 03:05:46 -0800 | [diff] [blame] | 2017 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2018 | #endif |
| 2019 | |
Randy Dunlap | 911851e | 2006-03-22 00:08:14 -0800 | [diff] [blame] | 2020 | /** |
| 2021 | * slab_destroy - destroy and release all objects in a slab |
| 2022 | * @cachep: cache pointer being destroyed |
Masanari Iida | cb8ee1a | 2014-01-28 02:57:08 +0900 | [diff] [blame] | 2023 | * @page: page pointer being destroyed |
Randy Dunlap | 911851e | 2006-03-22 00:08:14 -0800 | [diff] [blame] | 2024 | * |
Matthew Dobson | 12dd36f | 2006-02-01 03:05:46 -0800 | [diff] [blame] | 2025 | * Destroy all the objs in a slab, and release the mem back to the system. |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2026 | * Before calling the slab must have been unlinked from the cache. The |
| 2027 | * cache-lock is not held/needed. |
Matthew Dobson | 12dd36f | 2006-02-01 03:05:46 -0800 | [diff] [blame] | 2028 | */ |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2029 | static void slab_destroy(struct kmem_cache *cachep, struct page *page) |
Matthew Dobson | 12dd36f | 2006-02-01 03:05:46 -0800 | [diff] [blame] | 2030 | { |
Joonsoo Kim | 7e00735 | 2013-10-30 19:04:01 +0900 | [diff] [blame] | 2031 | void *freelist; |
Matthew Dobson | 12dd36f | 2006-02-01 03:05:46 -0800 | [diff] [blame] | 2032 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2033 | freelist = page->freelist; |
| 2034 | slab_destroy_debugcheck(cachep, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) { |
Joonsoo Kim | 6812670 | 2013-10-24 10:07:42 +0900 | [diff] [blame] | 2036 | struct rcu_head *head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | |
Joonsoo Kim | 6812670 | 2013-10-24 10:07:42 +0900 | [diff] [blame] | 2038 | /* |
| 2039 | * RCU free overloads the RCU head over the LRU. |
| 2040 | * slab_page has been overloeaded over the LRU, |
| 2041 | * however it is not used from now on so that |
| 2042 | * we can use it safely. |
| 2043 | */ |
| 2044 | head = (void *)&page->rcu_head; |
| 2045 | call_rcu(head, kmem_rcu_free); |
| 2046 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | } else { |
Joonsoo Kim | 0c3aa83 | 2013-10-24 10:07:38 +0900 | [diff] [blame] | 2048 | kmem_freepages(cachep, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | } |
Joonsoo Kim | 6812670 | 2013-10-24 10:07:42 +0900 | [diff] [blame] | 2050 | |
| 2051 | /* |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2052 | * From now on, we don't use freelist |
Joonsoo Kim | 6812670 | 2013-10-24 10:07:42 +0900 | [diff] [blame] | 2053 | * although actual page can be freed in rcu context |
| 2054 | */ |
| 2055 | if (OFF_SLAB(cachep)) |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2056 | kmem_cache_free(cachep->freelist_cache, freelist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | /** |
Randy.Dunlap | a70773d | 2006-02-01 03:05:52 -0800 | [diff] [blame] | 2060 | * calculate_slab_order - calculate size (page order) of slabs |
| 2061 | * @cachep: pointer to the cache that is being created |
| 2062 | * @size: size of objects to be created in this cache. |
| 2063 | * @align: required alignment for the objects. |
| 2064 | * @flags: slab allocation flags |
| 2065 | * |
| 2066 | * Also calculates the number of objects per slab. |
Pekka Enberg | 4d268eb | 2006-01-08 01:00:36 -0800 | [diff] [blame] | 2067 | * |
| 2068 | * This could be made much more intelligent. For now, try to avoid using |
| 2069 | * high order pages for slabs. When the gfp() functions are more friendly |
| 2070 | * towards high-order requests, this should be changed. |
| 2071 | */ |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2072 | static size_t calculate_slab_order(struct kmem_cache *cachep, |
Randy Dunlap | ee13d78 | 2006-02-01 03:05:53 -0800 | [diff] [blame] | 2073 | size_t size, size_t align, unsigned long flags) |
Pekka Enberg | 4d268eb | 2006-01-08 01:00:36 -0800 | [diff] [blame] | 2074 | { |
Ingo Molnar | b1ab41c | 2006-06-02 15:44:58 +0200 | [diff] [blame] | 2075 | unsigned long offslab_limit; |
Pekka Enberg | 4d268eb | 2006-01-08 01:00:36 -0800 | [diff] [blame] | 2076 | size_t left_over = 0; |
Linus Torvalds | 9888e6f | 2006-03-06 17:44:43 -0800 | [diff] [blame] | 2077 | int gfporder; |
Pekka Enberg | 4d268eb | 2006-01-08 01:00:36 -0800 | [diff] [blame] | 2078 | |
Christoph Lameter | 0aa817f | 2007-05-16 22:11:01 -0700 | [diff] [blame] | 2079 | for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) { |
Pekka Enberg | 4d268eb | 2006-01-08 01:00:36 -0800 | [diff] [blame] | 2080 | unsigned int num; |
| 2081 | size_t remainder; |
| 2082 | |
Linus Torvalds | 9888e6f | 2006-03-06 17:44:43 -0800 | [diff] [blame] | 2083 | cache_estimate(gfporder, size, align, flags, &remainder, &num); |
Pekka Enberg | 4d268eb | 2006-01-08 01:00:36 -0800 | [diff] [blame] | 2084 | if (!num) |
| 2085 | continue; |
Linus Torvalds | 9888e6f | 2006-03-06 17:44:43 -0800 | [diff] [blame] | 2086 | |
Joonsoo Kim | f315e3f | 2013-12-02 17:49:41 +0900 | [diff] [blame] | 2087 | /* Can't handle number of objects more than SLAB_OBJ_MAX_NUM */ |
| 2088 | if (num > SLAB_OBJ_MAX_NUM) |
| 2089 | break; |
| 2090 | |
Ingo Molnar | b1ab41c | 2006-06-02 15:44:58 +0200 | [diff] [blame] | 2091 | if (flags & CFLGS_OFF_SLAB) { |
Joonsoo Kim | 0378730 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 2092 | size_t freelist_size_per_obj = sizeof(freelist_idx_t); |
Ingo Molnar | b1ab41c | 2006-06-02 15:44:58 +0200 | [diff] [blame] | 2093 | /* |
| 2094 | * Max number of objs-per-slab for caches which |
| 2095 | * use off-slab slabs. Needed to avoid a possible |
| 2096 | * looping condition in cache_grow(). |
| 2097 | */ |
Joonsoo Kim | 0378730 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 2098 | if (IS_ENABLED(CONFIG_DEBUG_SLAB_LEAK)) |
| 2099 | freelist_size_per_obj += sizeof(char); |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2100 | offslab_limit = size; |
Joonsoo Kim | 0378730 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 2101 | offslab_limit /= freelist_size_per_obj; |
Ingo Molnar | b1ab41c | 2006-06-02 15:44:58 +0200 | [diff] [blame] | 2102 | |
| 2103 | if (num > offslab_limit) |
| 2104 | break; |
| 2105 | } |
Pekka Enberg | 4d268eb | 2006-01-08 01:00:36 -0800 | [diff] [blame] | 2106 | |
Linus Torvalds | 9888e6f | 2006-03-06 17:44:43 -0800 | [diff] [blame] | 2107 | /* Found something acceptable - save it away */ |
Pekka Enberg | 4d268eb | 2006-01-08 01:00:36 -0800 | [diff] [blame] | 2108 | cachep->num = num; |
Linus Torvalds | 9888e6f | 2006-03-06 17:44:43 -0800 | [diff] [blame] | 2109 | cachep->gfporder = gfporder; |
Pekka Enberg | 4d268eb | 2006-01-08 01:00:36 -0800 | [diff] [blame] | 2110 | left_over = remainder; |
| 2111 | |
| 2112 | /* |
Linus Torvalds | f78bb8a | 2006-03-08 10:33:05 -0800 | [diff] [blame] | 2113 | * A VFS-reclaimable slab tends to have most allocations |
| 2114 | * as GFP_NOFS and we really don't want to have to be allocating |
| 2115 | * higher-order pages when we are unable to shrink dcache. |
| 2116 | */ |
| 2117 | if (flags & SLAB_RECLAIM_ACCOUNT) |
| 2118 | break; |
| 2119 | |
| 2120 | /* |
Pekka Enberg | 4d268eb | 2006-01-08 01:00:36 -0800 | [diff] [blame] | 2121 | * Large number of objects is good, but very large slabs are |
| 2122 | * currently bad for the gfp()s. |
| 2123 | */ |
David Rientjes | 543585c | 2011-10-18 22:09:24 -0700 | [diff] [blame] | 2124 | if (gfporder >= slab_max_order) |
Pekka Enberg | 4d268eb | 2006-01-08 01:00:36 -0800 | [diff] [blame] | 2125 | break; |
| 2126 | |
Linus Torvalds | 9888e6f | 2006-03-06 17:44:43 -0800 | [diff] [blame] | 2127 | /* |
| 2128 | * Acceptable internal fragmentation? |
| 2129 | */ |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2130 | if (left_over * 8 <= (PAGE_SIZE << gfporder)) |
Pekka Enberg | 4d268eb | 2006-01-08 01:00:36 -0800 | [diff] [blame] | 2131 | break; |
| 2132 | } |
| 2133 | return left_over; |
| 2134 | } |
| 2135 | |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 2136 | static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp) |
Pekka Enberg | f30cf7d | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2137 | { |
Christoph Lameter | 97d0660 | 2012-07-06 15:25:11 -0500 | [diff] [blame] | 2138 | if (slab_state >= FULL) |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 2139 | return enable_cpucache(cachep, gfp); |
Christoph Lameter | 2ed3a4e | 2006-09-25 23:31:38 -0700 | [diff] [blame] | 2140 | |
Christoph Lameter | 97d0660 | 2012-07-06 15:25:11 -0500 | [diff] [blame] | 2141 | if (slab_state == DOWN) { |
Pekka Enberg | f30cf7d | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2142 | /* |
Christoph Lameter | 2f9baa9 | 2012-11-28 16:23:09 +0000 | [diff] [blame] | 2143 | * Note: Creation of first cache (kmem_cache). |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2144 | * The setup_node is taken care |
Christoph Lameter | 2f9baa9 | 2012-11-28 16:23:09 +0000 | [diff] [blame] | 2145 | * of by the caller of __kmem_cache_create |
| 2146 | */ |
| 2147 | cachep->array[smp_processor_id()] = &initarray_generic.cache; |
| 2148 | slab_state = PARTIAL; |
| 2149 | } else if (slab_state == PARTIAL) { |
| 2150 | /* |
| 2151 | * Note: the second kmem_cache_create must create the cache |
Pekka Enberg | f30cf7d | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2152 | * that's used by kmalloc(24), otherwise the creation of |
| 2153 | * further caches will BUG(). |
| 2154 | */ |
| 2155 | cachep->array[smp_processor_id()] = &initarray_generic.cache; |
| 2156 | |
| 2157 | /* |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2158 | * If the cache that's used by kmalloc(sizeof(kmem_cache_node)) is |
| 2159 | * the second cache, then we need to set up all its node/, |
Pekka Enberg | f30cf7d | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2160 | * otherwise the creation of further caches will BUG(). |
| 2161 | */ |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2162 | set_up_node(cachep, SIZE_AC); |
| 2163 | if (INDEX_AC == INDEX_NODE) |
| 2164 | slab_state = PARTIAL_NODE; |
Pekka Enberg | f30cf7d | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2165 | else |
Christoph Lameter | 97d0660 | 2012-07-06 15:25:11 -0500 | [diff] [blame] | 2166 | slab_state = PARTIAL_ARRAYCACHE; |
Pekka Enberg | f30cf7d | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2167 | } else { |
Christoph Lameter | 2f9baa9 | 2012-11-28 16:23:09 +0000 | [diff] [blame] | 2168 | /* Remaining boot caches */ |
Pekka Enberg | f30cf7d | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2169 | cachep->array[smp_processor_id()] = |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 2170 | kmalloc(sizeof(struct arraycache_init), gfp); |
Pekka Enberg | f30cf7d | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2171 | |
Christoph Lameter | 97d0660 | 2012-07-06 15:25:11 -0500 | [diff] [blame] | 2172 | if (slab_state == PARTIAL_ARRAYCACHE) { |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2173 | set_up_node(cachep, SIZE_NODE); |
| 2174 | slab_state = PARTIAL_NODE; |
Pekka Enberg | f30cf7d | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2175 | } else { |
| 2176 | int node; |
Pekka Enberg | 556a169 | 2008-01-25 08:20:51 +0200 | [diff] [blame] | 2177 | for_each_online_node(node) { |
Christoph Lameter | 6a67368 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2178 | cachep->node[node] = |
Christoph Lameter | 6744f08 | 2013-01-10 19:12:17 +0000 | [diff] [blame] | 2179 | kmalloc_node(sizeof(struct kmem_cache_node), |
Pekka Enberg | eb91f1d | 2009-06-12 14:56:09 +0300 | [diff] [blame] | 2180 | gfp, node); |
Christoph Lameter | 6a67368 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2181 | BUG_ON(!cachep->node[node]); |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2182 | kmem_cache_node_init(cachep->node[node]); |
Pekka Enberg | f30cf7d | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2183 | } |
| 2184 | } |
| 2185 | } |
Christoph Lameter | 6a67368 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2186 | cachep->node[numa_mem_id()]->next_reap = |
Jianyu Zhan | 5f0985b | 2014-03-30 17:02:20 +0800 | [diff] [blame] | 2187 | jiffies + REAPTIMEOUT_NODE + |
| 2188 | ((unsigned long)cachep) % REAPTIMEOUT_NODE; |
Pekka Enberg | f30cf7d | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2189 | |
| 2190 | cpu_cache_get(cachep)->avail = 0; |
| 2191 | cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES; |
| 2192 | cpu_cache_get(cachep)->batchcount = 1; |
| 2193 | cpu_cache_get(cachep)->touched = 0; |
| 2194 | cachep->batchcount = 1; |
| 2195 | cachep->limit = BOOT_CPUCACHE_ENTRIES; |
Christoph Lameter | 2ed3a4e | 2006-09-25 23:31:38 -0700 | [diff] [blame] | 2196 | return 0; |
Pekka Enberg | f30cf7d | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2197 | } |
| 2198 | |
Pekka Enberg | 4d268eb | 2006-01-08 01:00:36 -0800 | [diff] [blame] | 2199 | /** |
Christoph Lameter | 039363f | 2012-07-06 15:25:10 -0500 | [diff] [blame] | 2200 | * __kmem_cache_create - Create a cache. |
Randy Dunlap | a755b76 | 2012-11-06 17:10:10 -0800 | [diff] [blame] | 2201 | * @cachep: cache management descriptor |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | * @flags: SLAB flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | * |
| 2204 | * Returns a ptr to the cache on success, NULL on failure. |
| 2205 | * Cannot be called within a int, but can be interrupted. |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2206 | * The @ctor is run when new pages are allocated by the cache. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2208 | * The flags are |
| 2209 | * |
| 2210 | * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5) |
| 2211 | * to catch references to uninitialised memory. |
| 2212 | * |
| 2213 | * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check |
| 2214 | * for buffer overruns. |
| 2215 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2216 | * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware |
| 2217 | * cacheline. This can be beneficial if you're counting cycles as closely |
| 2218 | * as davem. |
| 2219 | */ |
Christoph Lameter | 278b1bb | 2012-09-05 00:20:34 +0000 | [diff] [blame] | 2220 | int |
Christoph Lameter | 8a13a4c | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 2221 | __kmem_cache_create (struct kmem_cache *cachep, unsigned long flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2222 | { |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2223 | size_t left_over, freelist_size, ralign; |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 2224 | gfp_t gfp; |
Christoph Lameter | 278b1bb | 2012-09-05 00:20:34 +0000 | [diff] [blame] | 2225 | int err; |
Christoph Lameter | 8a13a4c | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 2226 | size_t size = cachep->size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2228 | #if DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2229 | #if FORCED_DEBUG |
| 2230 | /* |
| 2231 | * Enable redzoning and last user accounting, except for caches with |
| 2232 | * large objects, if the increased size would increase the object size |
| 2233 | * above the next power of two: caches with object sizes just above a |
| 2234 | * power of two have a significant amount of internal fragmentation. |
| 2235 | */ |
David Woodhouse | 87a927c | 2007-07-04 21:26:44 -0400 | [diff] [blame] | 2236 | if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN + |
| 2237 | 2 * sizeof(unsigned long long))) |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 2238 | flags |= SLAB_RED_ZONE | SLAB_STORE_USER; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2239 | if (!(flags & SLAB_DESTROY_BY_RCU)) |
| 2240 | flags |= SLAB_POISON; |
| 2241 | #endif |
| 2242 | if (flags & SLAB_DESTROY_BY_RCU) |
| 2243 | BUG_ON(flags & SLAB_POISON); |
| 2244 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2246 | /* |
| 2247 | * Check that size is in terms of words. This is needed to avoid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2248 | * unaligned accesses for some archs when redzoning is used, and makes |
| 2249 | * sure any on-slab bufctl's are also correctly aligned. |
| 2250 | */ |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 2251 | if (size & (BYTES_PER_WORD - 1)) { |
| 2252 | size += (BYTES_PER_WORD - 1); |
| 2253 | size &= ~(BYTES_PER_WORD - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | } |
| 2255 | |
Pekka Enberg | ca5f970 | 2006-09-25 23:31:25 -0700 | [diff] [blame] | 2256 | /* |
David Woodhouse | 87a927c | 2007-07-04 21:26:44 -0400 | [diff] [blame] | 2257 | * Redzoning and user store require word alignment or possibly larger. |
| 2258 | * Note this will be overridden by architecture or caller mandated |
| 2259 | * alignment if either is greater than BYTES_PER_WORD. |
Pekka Enberg | ca5f970 | 2006-09-25 23:31:25 -0700 | [diff] [blame] | 2260 | */ |
David Woodhouse | 87a927c | 2007-07-04 21:26:44 -0400 | [diff] [blame] | 2261 | if (flags & SLAB_STORE_USER) |
| 2262 | ralign = BYTES_PER_WORD; |
| 2263 | |
| 2264 | if (flags & SLAB_RED_ZONE) { |
| 2265 | ralign = REDZONE_ALIGN; |
| 2266 | /* If redzoning, ensure that the second redzone is suitably |
| 2267 | * aligned, by adjusting the object size accordingly. */ |
| 2268 | size += REDZONE_ALIGN - 1; |
| 2269 | size &= ~(REDZONE_ALIGN - 1); |
| 2270 | } |
Pekka Enberg | ca5f970 | 2006-09-25 23:31:25 -0700 | [diff] [blame] | 2271 | |
Kevin Hilman | a44b56d | 2006-12-06 20:32:11 -0800 | [diff] [blame] | 2272 | /* 3) caller mandated alignment */ |
Christoph Lameter | 8a13a4c | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 2273 | if (ralign < cachep->align) { |
| 2274 | ralign = cachep->align; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | } |
Pekka Enberg | 3ff84a7 | 2011-02-14 17:46:21 +0200 | [diff] [blame] | 2276 | /* disable debug if necessary */ |
| 2277 | if (ralign > __alignof__(unsigned long long)) |
Kevin Hilman | a44b56d | 2006-12-06 20:32:11 -0800 | [diff] [blame] | 2278 | flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER); |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2279 | /* |
Pekka Enberg | ca5f970 | 2006-09-25 23:31:25 -0700 | [diff] [blame] | 2280 | * 4) Store it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | */ |
Christoph Lameter | 8a13a4c | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 2282 | cachep->align = ralign; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 2284 | if (slab_is_available()) |
| 2285 | gfp = GFP_KERNEL; |
| 2286 | else |
| 2287 | gfp = GFP_NOWAIT; |
| 2288 | |
Christoph Lameter | 6a67368 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2289 | setup_node_pointer(cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | #if DEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | |
Pekka Enberg | ca5f970 | 2006-09-25 23:31:25 -0700 | [diff] [blame] | 2292 | /* |
| 2293 | * Both debugging options require word-alignment which is calculated |
| 2294 | * into align above. |
| 2295 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | if (flags & SLAB_RED_ZONE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2297 | /* add space for red zone words */ |
Pekka Enberg | 3ff84a7 | 2011-02-14 17:46:21 +0200 | [diff] [blame] | 2298 | cachep->obj_offset += sizeof(unsigned long long); |
| 2299 | size += 2 * sizeof(unsigned long long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2300 | } |
| 2301 | if (flags & SLAB_STORE_USER) { |
Pekka Enberg | ca5f970 | 2006-09-25 23:31:25 -0700 | [diff] [blame] | 2302 | /* user store requires one word storage behind the end of |
David Woodhouse | 87a927c | 2007-07-04 21:26:44 -0400 | [diff] [blame] | 2303 | * the real object. But if the second red zone needs to be |
| 2304 | * aligned to 64 bits, we must allow that much space. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2305 | */ |
David Woodhouse | 87a927c | 2007-07-04 21:26:44 -0400 | [diff] [blame] | 2306 | if (flags & SLAB_RED_ZONE) |
| 2307 | size += REDZONE_ALIGN; |
| 2308 | else |
| 2309 | size += BYTES_PER_WORD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2310 | } |
| 2311 | #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC) |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2312 | if (size >= kmalloc_size(INDEX_NODE + 1) |
Tetsuo Handa | 608da7e | 2012-09-30 17:28:25 +0900 | [diff] [blame] | 2313 | && cachep->object_size > cache_line_size() |
| 2314 | && ALIGN(size, cachep->align) < PAGE_SIZE) { |
| 2315 | cachep->obj_offset += PAGE_SIZE - ALIGN(size, cachep->align); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2316 | size = PAGE_SIZE; |
| 2317 | } |
| 2318 | #endif |
| 2319 | #endif |
| 2320 | |
Ingo Molnar | e0a4272 | 2006-06-23 02:03:46 -0700 | [diff] [blame] | 2321 | /* |
| 2322 | * Determine if the slab management is 'on' or 'off' slab. |
| 2323 | * (bootstrapping cannot cope with offslab caches so don't do |
Catalin Marinas | e7cb55b | 2009-10-28 13:33:08 +0000 | [diff] [blame] | 2324 | * it too early on. Always use on-slab management when |
| 2325 | * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak) |
Ingo Molnar | e0a4272 | 2006-06-23 02:03:46 -0700 | [diff] [blame] | 2326 | */ |
Joonsoo Kim | 8fc9cf4 | 2013-12-02 17:49:43 +0900 | [diff] [blame] | 2327 | if ((size >= (PAGE_SIZE >> 5)) && !slab_early_init && |
Catalin Marinas | e7cb55b | 2009-10-28 13:33:08 +0000 | [diff] [blame] | 2328 | !(flags & SLAB_NOLEAKTRACE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | /* |
| 2330 | * Size is large, assume best to place the slab management obj |
| 2331 | * off-slab (should allow better packing of objs). |
| 2332 | */ |
| 2333 | flags |= CFLGS_OFF_SLAB; |
| 2334 | |
Christoph Lameter | 8a13a4c | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 2335 | size = ALIGN(size, cachep->align); |
Joonsoo Kim | f315e3f | 2013-12-02 17:49:41 +0900 | [diff] [blame] | 2336 | /* |
| 2337 | * We should restrict the number of objects in a slab to implement |
| 2338 | * byte sized index. Refer comment on SLAB_OBJ_MIN_SIZE definition. |
| 2339 | */ |
| 2340 | if (FREELIST_BYTE_INDEX && size < SLAB_OBJ_MIN_SIZE) |
| 2341 | size = ALIGN(SLAB_OBJ_MIN_SIZE, cachep->align); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2342 | |
Christoph Lameter | 8a13a4c | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 2343 | left_over = calculate_slab_order(cachep, size, cachep->align, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2344 | |
Christoph Lameter | 8a13a4c | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 2345 | if (!cachep->num) |
Christoph Lameter | 278b1bb | 2012-09-05 00:20:34 +0000 | [diff] [blame] | 2346 | return -E2BIG; |
Christoph Lameter | 8a13a4c | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 2347 | |
Joonsoo Kim | 0378730 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 2348 | freelist_size = calculate_freelist_size(cachep->num, cachep->align); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2349 | |
| 2350 | /* |
| 2351 | * If the slab has been placed off-slab, and we have enough space then |
| 2352 | * move it on-slab. This is at the expense of any extra colouring. |
| 2353 | */ |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2354 | if (flags & CFLGS_OFF_SLAB && left_over >= freelist_size) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2355 | flags &= ~CFLGS_OFF_SLAB; |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2356 | left_over -= freelist_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2357 | } |
| 2358 | |
| 2359 | if (flags & CFLGS_OFF_SLAB) { |
| 2360 | /* really off slab. No need for manual alignment */ |
Joonsoo Kim | 0378730 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 2361 | freelist_size = calculate_freelist_size(cachep->num, 0); |
Ron Lee | 6746136 | 2009-05-22 04:58:22 +0930 | [diff] [blame] | 2362 | |
| 2363 | #ifdef CONFIG_PAGE_POISONING |
| 2364 | /* If we're going to use the generic kernel_map_pages() |
| 2365 | * poisoning, then it's going to smash the contents of |
| 2366 | * the redzone and userword anyhow, so switch them off. |
| 2367 | */ |
| 2368 | if (size % PAGE_SIZE == 0 && flags & SLAB_POISON) |
| 2369 | flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER); |
| 2370 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | } |
| 2372 | |
| 2373 | cachep->colour_off = cache_line_size(); |
| 2374 | /* Offset must be a multiple of the alignment. */ |
Christoph Lameter | 8a13a4c | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 2375 | if (cachep->colour_off < cachep->align) |
| 2376 | cachep->colour_off = cachep->align; |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 2377 | cachep->colour = left_over / cachep->colour_off; |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2378 | cachep->freelist_size = freelist_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | cachep->flags = flags; |
Joonsoo Kim | a57a498 | 2013-10-24 10:07:44 +0900 | [diff] [blame] | 2380 | cachep->allocflags = __GFP_COMP; |
Christoph Lameter | 4b51d66 | 2007-02-10 01:43:10 -0800 | [diff] [blame] | 2381 | if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA)) |
Glauber Costa | a618e89 | 2012-06-14 16:17:21 +0400 | [diff] [blame] | 2382 | cachep->allocflags |= GFP_DMA; |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 2383 | cachep->size = size; |
Eric Dumazet | 6a2d7a9 | 2006-12-13 00:34:27 -0800 | [diff] [blame] | 2384 | cachep->reciprocal_buffer_size = reciprocal_value(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2385 | |
Ravikiran G Thirumalai | e5ac9c5 | 2006-09-25 23:31:34 -0700 | [diff] [blame] | 2386 | if (flags & CFLGS_OFF_SLAB) { |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2387 | cachep->freelist_cache = kmalloc_slab(freelist_size, 0u); |
Ravikiran G Thirumalai | e5ac9c5 | 2006-09-25 23:31:34 -0700 | [diff] [blame] | 2388 | /* |
Jianyu Zhan | 5f0985b | 2014-03-30 17:02:20 +0800 | [diff] [blame] | 2389 | * This is a possibility for one of the kmalloc_{dma,}_caches. |
Ravikiran G Thirumalai | e5ac9c5 | 2006-09-25 23:31:34 -0700 | [diff] [blame] | 2390 | * But since we go off slab only for object size greater than |
Jianyu Zhan | 5f0985b | 2014-03-30 17:02:20 +0800 | [diff] [blame] | 2391 | * PAGE_SIZE/8, and kmalloc_{dma,}_caches get created |
| 2392 | * in ascending order,this should not happen at all. |
Ravikiran G Thirumalai | e5ac9c5 | 2006-09-25 23:31:34 -0700 | [diff] [blame] | 2393 | * But leave a BUG_ON for some lucky dude. |
| 2394 | */ |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2395 | BUG_ON(ZERO_OR_NULL_PTR(cachep->freelist_cache)); |
Ravikiran G Thirumalai | e5ac9c5 | 2006-09-25 23:31:34 -0700 | [diff] [blame] | 2396 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | |
Christoph Lameter | 278b1bb | 2012-09-05 00:20:34 +0000 | [diff] [blame] | 2398 | err = setup_cpu_cache(cachep, gfp); |
| 2399 | if (err) { |
Christoph Lameter | 12c3667 | 2012-09-04 23:38:33 +0000 | [diff] [blame] | 2400 | __kmem_cache_shutdown(cachep); |
Christoph Lameter | 278b1bb | 2012-09-05 00:20:34 +0000 | [diff] [blame] | 2401 | return err; |
Christoph Lameter | 2ed3a4e | 2006-09-25 23:31:38 -0700 | [diff] [blame] | 2402 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2403 | |
Peter Zijlstra | 83835b3 | 2011-07-22 15:26:05 +0200 | [diff] [blame] | 2404 | if (flags & SLAB_DEBUG_OBJECTS) { |
| 2405 | /* |
| 2406 | * Would deadlock through slab_destroy()->call_rcu()-> |
| 2407 | * debug_object_activate()->kmem_cache_alloc(). |
| 2408 | */ |
| 2409 | WARN_ON_ONCE(flags & SLAB_DESTROY_BY_RCU); |
| 2410 | |
| 2411 | slab_set_debugobj_lock_classes(cachep); |
Glauber Costa | 6ccfb5b | 2012-12-18 14:22:31 -0800 | [diff] [blame] | 2412 | } else if (!OFF_SLAB(cachep) && !(flags & SLAB_DESTROY_BY_RCU)) |
| 2413 | on_slab_lock_classes(cachep); |
Peter Zijlstra | 83835b3 | 2011-07-22 15:26:05 +0200 | [diff] [blame] | 2414 | |
Christoph Lameter | 278b1bb | 2012-09-05 00:20:34 +0000 | [diff] [blame] | 2415 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2416 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2417 | |
| 2418 | #if DEBUG |
| 2419 | static void check_irq_off(void) |
| 2420 | { |
| 2421 | BUG_ON(!irqs_disabled()); |
| 2422 | } |
| 2423 | |
| 2424 | static void check_irq_on(void) |
| 2425 | { |
| 2426 | BUG_ON(irqs_disabled()); |
| 2427 | } |
| 2428 | |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 2429 | static void check_spinlock_acquired(struct kmem_cache *cachep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2430 | { |
| 2431 | #ifdef CONFIG_SMP |
| 2432 | check_irq_off(); |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 2433 | assert_spin_locked(&get_node(cachep, numa_mem_id())->list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | #endif |
| 2435 | } |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 2436 | |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 2437 | static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node) |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 2438 | { |
| 2439 | #ifdef CONFIG_SMP |
| 2440 | check_irq_off(); |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 2441 | assert_spin_locked(&get_node(cachep, node)->list_lock); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 2442 | #endif |
| 2443 | } |
| 2444 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2445 | #else |
| 2446 | #define check_irq_off() do { } while(0) |
| 2447 | #define check_irq_on() do { } while(0) |
| 2448 | #define check_spinlock_acquired(x) do { } while(0) |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 2449 | #define check_spinlock_acquired_node(x, y) do { } while(0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | #endif |
| 2451 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2452 | static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n, |
Christoph Lameter | aab2207 | 2006-03-22 00:09:06 -0800 | [diff] [blame] | 2453 | struct array_cache *ac, |
| 2454 | int force, int node); |
| 2455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2456 | static void do_drain(void *arg) |
| 2457 | { |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2458 | struct kmem_cache *cachep = arg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | struct array_cache *ac; |
Lee Schermerhorn | 7d6e6d0 | 2010-05-26 14:45:03 -0700 | [diff] [blame] | 2460 | int node = numa_mem_id(); |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 2461 | struct kmem_cache_node *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | |
| 2463 | check_irq_off(); |
Pekka Enberg | 9a2dba4 | 2006-02-01 03:05:49 -0800 | [diff] [blame] | 2464 | ac = cpu_cache_get(cachep); |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 2465 | n = get_node(cachep, node); |
| 2466 | spin_lock(&n->list_lock); |
Christoph Lameter | ff69416 | 2005-09-22 21:44:02 -0700 | [diff] [blame] | 2467 | free_block(cachep, ac->entry, ac->avail, node); |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 2468 | spin_unlock(&n->list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2469 | ac->avail = 0; |
| 2470 | } |
| 2471 | |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 2472 | static void drain_cpu_caches(struct kmem_cache *cachep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | { |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2474 | struct kmem_cache_node *n; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 2475 | int node; |
| 2476 | |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 2477 | on_each_cpu(do_drain, cachep, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2478 | check_irq_on(); |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 2479 | for_each_kmem_cache_node(cachep, node, n) |
| 2480 | if (n->alien) |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2481 | drain_alien_cache(cachep, n->alien); |
Roland Dreier | a4523a8 | 2006-05-15 11:41:00 -0700 | [diff] [blame] | 2482 | |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 2483 | for_each_kmem_cache_node(cachep, node, n) |
| 2484 | drain_array(cachep, n, n->shared, 1, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | } |
| 2486 | |
Christoph Lameter | ed11d9e | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 2487 | /* |
| 2488 | * Remove slabs from the list of free slabs. |
| 2489 | * Specify the number of slabs to drain in tofree. |
| 2490 | * |
| 2491 | * Returns the actual number of slabs released. |
| 2492 | */ |
| 2493 | static int drain_freelist(struct kmem_cache *cache, |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2494 | struct kmem_cache_node *n, int tofree) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | { |
Christoph Lameter | ed11d9e | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 2496 | struct list_head *p; |
| 2497 | int nr_freed; |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2498 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2499 | |
Christoph Lameter | ed11d9e | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 2500 | nr_freed = 0; |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2501 | while (nr_freed < tofree && !list_empty(&n->slabs_free)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2502 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2503 | spin_lock_irq(&n->list_lock); |
| 2504 | p = n->slabs_free.prev; |
| 2505 | if (p == &n->slabs_free) { |
| 2506 | spin_unlock_irq(&n->list_lock); |
Christoph Lameter | ed11d9e | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 2507 | goto out; |
| 2508 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2510 | page = list_entry(p, struct page, lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2511 | #if DEBUG |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2512 | BUG_ON(page->active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | #endif |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2514 | list_del(&page->lru); |
Christoph Lameter | ed11d9e | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 2515 | /* |
| 2516 | * Safe to drop the lock. The slab is no longer linked |
| 2517 | * to the cache. |
| 2518 | */ |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2519 | n->free_objects -= cache->num; |
| 2520 | spin_unlock_irq(&n->list_lock); |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2521 | slab_destroy(cache, page); |
Christoph Lameter | ed11d9e | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 2522 | nr_freed++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2523 | } |
Christoph Lameter | ed11d9e | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 2524 | out: |
| 2525 | return nr_freed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2526 | } |
| 2527 | |
Vladimir Davydov | 03afc0e | 2014-06-04 16:07:20 -0700 | [diff] [blame] | 2528 | int __kmem_cache_shrink(struct kmem_cache *cachep) |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 2529 | { |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 2530 | int ret = 0; |
| 2531 | int node; |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2532 | struct kmem_cache_node *n; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 2533 | |
| 2534 | drain_cpu_caches(cachep); |
| 2535 | |
| 2536 | check_irq_on(); |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 2537 | for_each_kmem_cache_node(cachep, node, n) { |
Wanpeng Li | 0fa8103 | 2013-07-04 08:33:22 +0800 | [diff] [blame] | 2538 | drain_freelist(cachep, n, slabs_tofree(cachep, n)); |
Christoph Lameter | ed11d9e | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 2539 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2540 | ret += !list_empty(&n->slabs_full) || |
| 2541 | !list_empty(&n->slabs_partial); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 2542 | } |
| 2543 | return (ret ? 1 : 0); |
| 2544 | } |
| 2545 | |
Christoph Lameter | 945cf2b | 2012-09-04 23:18:33 +0000 | [diff] [blame] | 2546 | int __kmem_cache_shutdown(struct kmem_cache *cachep) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | { |
Christoph Lameter | 12c3667 | 2012-09-04 23:38:33 +0000 | [diff] [blame] | 2548 | int i; |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2549 | struct kmem_cache_node *n; |
Vladimir Davydov | 03afc0e | 2014-06-04 16:07:20 -0700 | [diff] [blame] | 2550 | int rc = __kmem_cache_shrink(cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2551 | |
Christoph Lameter | 12c3667 | 2012-09-04 23:38:33 +0000 | [diff] [blame] | 2552 | if (rc) |
| 2553 | return rc; |
| 2554 | |
| 2555 | for_each_online_cpu(i) |
| 2556 | kfree(cachep->array[i]); |
| 2557 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2558 | /* NUMA: free the node structures */ |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 2559 | for_each_kmem_cache_node(cachep, i, n) { |
| 2560 | kfree(n->shared); |
| 2561 | free_alien_cache(n->alien); |
| 2562 | kfree(n); |
| 2563 | cachep->node[i] = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2564 | } |
Christoph Lameter | 12c3667 | 2012-09-04 23:38:33 +0000 | [diff] [blame] | 2565 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | |
Ravikiran G Thirumalai | e5ac9c5 | 2006-09-25 23:31:34 -0700 | [diff] [blame] | 2568 | /* |
| 2569 | * Get the memory for a slab management obj. |
Jianyu Zhan | 5f0985b | 2014-03-30 17:02:20 +0800 | [diff] [blame] | 2570 | * |
| 2571 | * For a slab cache when the slab descriptor is off-slab, the |
| 2572 | * slab descriptor can't come from the same cache which is being created, |
| 2573 | * Because if it is the case, that means we defer the creation of |
| 2574 | * the kmalloc_{dma,}_cache of size sizeof(slab descriptor) to this point. |
| 2575 | * And we eventually call down to __kmem_cache_create(), which |
| 2576 | * in turn looks up in the kmalloc_{dma,}_caches for the disired-size one. |
| 2577 | * This is a "chicken-and-egg" problem. |
| 2578 | * |
| 2579 | * So the off-slab slab descriptor shall come from the kmalloc_{dma,}_caches, |
| 2580 | * which are all initialized during kmem_cache_init(). |
Ravikiran G Thirumalai | e5ac9c5 | 2006-09-25 23:31:34 -0700 | [diff] [blame] | 2581 | */ |
Joonsoo Kim | 7e00735 | 2013-10-30 19:04:01 +0900 | [diff] [blame] | 2582 | static void *alloc_slabmgmt(struct kmem_cache *cachep, |
Joonsoo Kim | 0c3aa83 | 2013-10-24 10:07:38 +0900 | [diff] [blame] | 2583 | struct page *page, int colour_off, |
| 2584 | gfp_t local_flags, int nodeid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | { |
Joonsoo Kim | 7e00735 | 2013-10-30 19:04:01 +0900 | [diff] [blame] | 2586 | void *freelist; |
Joonsoo Kim | 0c3aa83 | 2013-10-24 10:07:38 +0900 | [diff] [blame] | 2587 | void *addr = page_address(page); |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 2588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | if (OFF_SLAB(cachep)) { |
| 2590 | /* Slab management obj is off-slab. */ |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2591 | freelist = kmem_cache_alloc_node(cachep->freelist_cache, |
Pekka Enberg | 8759ec5 | 2008-11-26 10:01:31 +0200 | [diff] [blame] | 2592 | local_flags, nodeid); |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2593 | if (!freelist) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2594 | return NULL; |
| 2595 | } else { |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2596 | freelist = addr + colour_off; |
| 2597 | colour_off += cachep->freelist_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | } |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2599 | page->active = 0; |
| 2600 | page->s_mem = addr + colour_off; |
| 2601 | return freelist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | } |
| 2603 | |
Joonsoo Kim | 7cc6897 | 2014-04-18 16:24:09 +0900 | [diff] [blame] | 2604 | static inline freelist_idx_t get_free_obj(struct page *page, unsigned int idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2605 | { |
Joonsoo Kim | a41adfa | 2013-12-02 17:49:42 +0900 | [diff] [blame] | 2606 | return ((freelist_idx_t *)page->freelist)[idx]; |
Joonsoo Kim | e5c58df | 2013-12-02 17:49:40 +0900 | [diff] [blame] | 2607 | } |
| 2608 | |
| 2609 | static inline void set_free_obj(struct page *page, |
Joonsoo Kim | 7cc6897 | 2014-04-18 16:24:09 +0900 | [diff] [blame] | 2610 | unsigned int idx, freelist_idx_t val) |
Joonsoo Kim | e5c58df | 2013-12-02 17:49:40 +0900 | [diff] [blame] | 2611 | { |
Joonsoo Kim | a41adfa | 2013-12-02 17:49:42 +0900 | [diff] [blame] | 2612 | ((freelist_idx_t *)(page->freelist))[idx] = val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | } |
| 2614 | |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 2615 | static void cache_init_objs(struct kmem_cache *cachep, |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2616 | struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2617 | { |
| 2618 | int i; |
| 2619 | |
| 2620 | for (i = 0; i < cachep->num; i++) { |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2621 | void *objp = index_to_obj(cachep, page, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | #if DEBUG |
| 2623 | /* need to poison the objs? */ |
| 2624 | if (cachep->flags & SLAB_POISON) |
| 2625 | poison_obj(cachep, objp, POISON_FREE); |
| 2626 | if (cachep->flags & SLAB_STORE_USER) |
| 2627 | *dbg_userword(cachep, objp) = NULL; |
| 2628 | |
| 2629 | if (cachep->flags & SLAB_RED_ZONE) { |
| 2630 | *dbg_redzone1(cachep, objp) = RED_INACTIVE; |
| 2631 | *dbg_redzone2(cachep, objp) = RED_INACTIVE; |
| 2632 | } |
| 2633 | /* |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2634 | * Constructors are not allowed to allocate memory from the same |
| 2635 | * cache which they are a constructor for. Otherwise, deadlock. |
| 2636 | * They must also be threaded. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2637 | */ |
| 2638 | if (cachep->ctor && !(cachep->flags & SLAB_POISON)) |
Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 2639 | cachep->ctor(objp + obj_offset(cachep)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2640 | |
| 2641 | if (cachep->flags & SLAB_RED_ZONE) { |
| 2642 | if (*dbg_redzone2(cachep, objp) != RED_INACTIVE) |
| 2643 | slab_error(cachep, "constructor overwrote the" |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 2644 | " end of an object"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2645 | if (*dbg_redzone1(cachep, objp) != RED_INACTIVE) |
| 2646 | slab_error(cachep, "constructor overwrote the" |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 2647 | " start of an object"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2648 | } |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 2649 | if ((cachep->size % PAGE_SIZE) == 0 && |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2650 | OFF_SLAB(cachep) && cachep->flags & SLAB_POISON) |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 2651 | kernel_map_pages(virt_to_page(objp), |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 2652 | cachep->size / PAGE_SIZE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2653 | #else |
| 2654 | if (cachep->ctor) |
Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 2655 | cachep->ctor(objp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2656 | #endif |
Joonsoo Kim | 0378730 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 2657 | set_obj_status(page, i, OBJECT_FREE); |
Joonsoo Kim | e5c58df | 2013-12-02 17:49:40 +0900 | [diff] [blame] | 2658 | set_free_obj(page, i, i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | } |
| 2661 | |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 2662 | static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2663 | { |
Christoph Lameter | 4b51d66 | 2007-02-10 01:43:10 -0800 | [diff] [blame] | 2664 | if (CONFIG_ZONE_DMA_FLAG) { |
| 2665 | if (flags & GFP_DMA) |
Glauber Costa | a618e89 | 2012-06-14 16:17:21 +0400 | [diff] [blame] | 2666 | BUG_ON(!(cachep->allocflags & GFP_DMA)); |
Christoph Lameter | 4b51d66 | 2007-02-10 01:43:10 -0800 | [diff] [blame] | 2667 | else |
Glauber Costa | a618e89 | 2012-06-14 16:17:21 +0400 | [diff] [blame] | 2668 | BUG_ON(cachep->allocflags & GFP_DMA); |
Christoph Lameter | 4b51d66 | 2007-02-10 01:43:10 -0800 | [diff] [blame] | 2669 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2670 | } |
| 2671 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2672 | static void *slab_get_obj(struct kmem_cache *cachep, struct page *page, |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2673 | int nodeid) |
Matthew Dobson | 78d382d | 2006-02-01 03:05:47 -0800 | [diff] [blame] | 2674 | { |
Joonsoo Kim | b1cb098 | 2013-10-24 10:07:45 +0900 | [diff] [blame] | 2675 | void *objp; |
Matthew Dobson | 78d382d | 2006-02-01 03:05:47 -0800 | [diff] [blame] | 2676 | |
Joonsoo Kim | e5c58df | 2013-12-02 17:49:40 +0900 | [diff] [blame] | 2677 | objp = index_to_obj(cachep, page, get_free_obj(page, page->active)); |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2678 | page->active++; |
Matthew Dobson | 78d382d | 2006-02-01 03:05:47 -0800 | [diff] [blame] | 2679 | #if DEBUG |
Joonsoo Kim | 1ea991b | 2013-10-24 10:07:40 +0900 | [diff] [blame] | 2680 | WARN_ON(page_to_nid(virt_to_page(objp)) != nodeid); |
Matthew Dobson | 78d382d | 2006-02-01 03:05:47 -0800 | [diff] [blame] | 2681 | #endif |
Matthew Dobson | 78d382d | 2006-02-01 03:05:47 -0800 | [diff] [blame] | 2682 | |
| 2683 | return objp; |
| 2684 | } |
| 2685 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2686 | static void slab_put_obj(struct kmem_cache *cachep, struct page *page, |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2687 | void *objp, int nodeid) |
Matthew Dobson | 78d382d | 2006-02-01 03:05:47 -0800 | [diff] [blame] | 2688 | { |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2689 | unsigned int objnr = obj_to_index(cachep, page, objp); |
Matthew Dobson | 78d382d | 2006-02-01 03:05:47 -0800 | [diff] [blame] | 2690 | #if DEBUG |
Joonsoo Kim | 1602517 | 2013-10-24 10:07:46 +0900 | [diff] [blame] | 2691 | unsigned int i; |
Matthew Dobson | 78d382d | 2006-02-01 03:05:47 -0800 | [diff] [blame] | 2692 | |
Matthew Dobson | 78d382d | 2006-02-01 03:05:47 -0800 | [diff] [blame] | 2693 | /* Verify that the slab belongs to the intended node */ |
Joonsoo Kim | 1ea991b | 2013-10-24 10:07:40 +0900 | [diff] [blame] | 2694 | WARN_ON(page_to_nid(virt_to_page(objp)) != nodeid); |
Matthew Dobson | 78d382d | 2006-02-01 03:05:47 -0800 | [diff] [blame] | 2695 | |
Joonsoo Kim | b1cb098 | 2013-10-24 10:07:45 +0900 | [diff] [blame] | 2696 | /* Verify double free bug */ |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2697 | for (i = page->active; i < cachep->num; i++) { |
Joonsoo Kim | e5c58df | 2013-12-02 17:49:40 +0900 | [diff] [blame] | 2698 | if (get_free_obj(page, i) == objnr) { |
Joonsoo Kim | b1cb098 | 2013-10-24 10:07:45 +0900 | [diff] [blame] | 2699 | printk(KERN_ERR "slab: double free detected in cache " |
| 2700 | "'%s', objp %p\n", cachep->name, objp); |
| 2701 | BUG(); |
| 2702 | } |
Matthew Dobson | 78d382d | 2006-02-01 03:05:47 -0800 | [diff] [blame] | 2703 | } |
| 2704 | #endif |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2705 | page->active--; |
Joonsoo Kim | e5c58df | 2013-12-02 17:49:40 +0900 | [diff] [blame] | 2706 | set_free_obj(page, page->active, objnr); |
Matthew Dobson | 78d382d | 2006-02-01 03:05:47 -0800 | [diff] [blame] | 2707 | } |
| 2708 | |
Pekka Enberg | 4776874 | 2006-06-23 02:03:07 -0700 | [diff] [blame] | 2709 | /* |
| 2710 | * Map pages beginning at addr to the given cache and slab. This is required |
| 2711 | * for the slab allocator to be able to lookup the cache and slab of a |
Nick Piggin | ccd35fb | 2011-01-07 17:49:17 +1100 | [diff] [blame] | 2712 | * virtual address for kfree, ksize, and slab debugging. |
Pekka Enberg | 4776874 | 2006-06-23 02:03:07 -0700 | [diff] [blame] | 2713 | */ |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2714 | static void slab_map_pages(struct kmem_cache *cache, struct page *page, |
Joonsoo Kim | 7e00735 | 2013-10-30 19:04:01 +0900 | [diff] [blame] | 2715 | void *freelist) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | { |
Joonsoo Kim | a57a498 | 2013-10-24 10:07:44 +0900 | [diff] [blame] | 2717 | page->slab_cache = cache; |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2718 | page->freelist = freelist; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2719 | } |
| 2720 | |
| 2721 | /* |
| 2722 | * Grow (by 1) the number of slabs within a cache. This is called by |
| 2723 | * kmem_cache_alloc() when there are no active objs left in a cache. |
| 2724 | */ |
Christoph Lameter | 3c517a6 | 2006-12-06 20:33:29 -0800 | [diff] [blame] | 2725 | static int cache_grow(struct kmem_cache *cachep, |
Joonsoo Kim | 0c3aa83 | 2013-10-24 10:07:38 +0900 | [diff] [blame] | 2726 | gfp_t flags, int nodeid, struct page *page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2727 | { |
Joonsoo Kim | 7e00735 | 2013-10-30 19:04:01 +0900 | [diff] [blame] | 2728 | void *freelist; |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 2729 | size_t offset; |
| 2730 | gfp_t local_flags; |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2731 | struct kmem_cache_node *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2733 | /* |
| 2734 | * Be lazy and only check for valid flags here, keeping it out of the |
| 2735 | * critical path in kmem_cache_alloc(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | */ |
Christoph Lameter | 6cb0622 | 2007-10-16 01:25:41 -0700 | [diff] [blame] | 2737 | BUG_ON(flags & GFP_SLAB_BUG_MASK); |
| 2738 | local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2739 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2740 | /* Take the node list lock to change the colour_next on this node */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2741 | check_irq_off(); |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 2742 | n = get_node(cachep, nodeid); |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2743 | spin_lock(&n->list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2744 | |
| 2745 | /* Get colour for the slab, and cal the next value. */ |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2746 | offset = n->colour_next; |
| 2747 | n->colour_next++; |
| 2748 | if (n->colour_next >= cachep->colour) |
| 2749 | n->colour_next = 0; |
| 2750 | spin_unlock(&n->list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | |
Ravikiran G Thirumalai | 2e1217c | 2006-02-04 23:27:56 -0800 | [diff] [blame] | 2752 | offset *= cachep->colour_off; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2753 | |
| 2754 | if (local_flags & __GFP_WAIT) |
| 2755 | local_irq_enable(); |
| 2756 | |
| 2757 | /* |
| 2758 | * The test for missing atomic flag is performed here, rather than |
| 2759 | * the more obvious place, simply to reduce the critical path length |
| 2760 | * in kmem_cache_alloc(). If a caller is seriously mis-behaving they |
| 2761 | * will eventually be caught here (where it matters). |
| 2762 | */ |
| 2763 | kmem_flagcheck(cachep, flags); |
| 2764 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2765 | /* |
| 2766 | * Get mem for the objs. Attempt to allocate a physical page from |
| 2767 | * 'nodeid'. |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 2768 | */ |
Joonsoo Kim | 0c3aa83 | 2013-10-24 10:07:38 +0900 | [diff] [blame] | 2769 | if (!page) |
| 2770 | page = kmem_getpages(cachep, local_flags, nodeid); |
| 2771 | if (!page) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2772 | goto failed; |
| 2773 | |
| 2774 | /* Get slab management. */ |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2775 | freelist = alloc_slabmgmt(cachep, page, offset, |
Christoph Lameter | 6cb0622 | 2007-10-16 01:25:41 -0700 | [diff] [blame] | 2776 | local_flags & ~GFP_CONSTRAINT_MASK, nodeid); |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2777 | if (!freelist) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | goto opps1; |
| 2779 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2780 | slab_map_pages(cachep, page, freelist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2781 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2782 | cache_init_objs(cachep, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2783 | |
| 2784 | if (local_flags & __GFP_WAIT) |
| 2785 | local_irq_disable(); |
| 2786 | check_irq_off(); |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2787 | spin_lock(&n->list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | |
| 2789 | /* Make slab active. */ |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2790 | list_add_tail(&page->lru, &(n->slabs_free)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2791 | STATS_INC_GROWN(cachep); |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2792 | n->free_objects += cachep->num; |
| 2793 | spin_unlock(&n->list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2794 | return 1; |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2795 | opps1: |
Joonsoo Kim | 0c3aa83 | 2013-10-24 10:07:38 +0900 | [diff] [blame] | 2796 | kmem_freepages(cachep, page); |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2797 | failed: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | if (local_flags & __GFP_WAIT) |
| 2799 | local_irq_disable(); |
| 2800 | return 0; |
| 2801 | } |
| 2802 | |
| 2803 | #if DEBUG |
| 2804 | |
| 2805 | /* |
| 2806 | * Perform extra freeing checks: |
| 2807 | * - detect bad pointers. |
| 2808 | * - POISON/RED_ZONE checking |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2809 | */ |
| 2810 | static void kfree_debugcheck(const void *objp) |
| 2811 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | if (!virt_addr_valid(objp)) { |
| 2813 | printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n", |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 2814 | (unsigned long)objp); |
| 2815 | BUG(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2816 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2817 | } |
| 2818 | |
Pekka Enberg | 58ce1fd | 2006-06-23 02:03:24 -0700 | [diff] [blame] | 2819 | static inline void verify_redzone_free(struct kmem_cache *cache, void *obj) |
| 2820 | { |
David Woodhouse | b46b8f1 | 2007-05-08 00:22:59 -0700 | [diff] [blame] | 2821 | unsigned long long redzone1, redzone2; |
Pekka Enberg | 58ce1fd | 2006-06-23 02:03:24 -0700 | [diff] [blame] | 2822 | |
| 2823 | redzone1 = *dbg_redzone1(cache, obj); |
| 2824 | redzone2 = *dbg_redzone2(cache, obj); |
| 2825 | |
| 2826 | /* |
| 2827 | * Redzone is ok. |
| 2828 | */ |
| 2829 | if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE) |
| 2830 | return; |
| 2831 | |
| 2832 | if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE) |
| 2833 | slab_error(cache, "double free detected"); |
| 2834 | else |
| 2835 | slab_error(cache, "memory outside object was overwritten"); |
| 2836 | |
David Woodhouse | b46b8f1 | 2007-05-08 00:22:59 -0700 | [diff] [blame] | 2837 | printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n", |
Pekka Enberg | 58ce1fd | 2006-06-23 02:03:24 -0700 | [diff] [blame] | 2838 | obj, redzone1, redzone2); |
| 2839 | } |
| 2840 | |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 2841 | static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp, |
Ezequiel Garcia | 7c0cb9c | 2012-09-08 17:47:55 -0300 | [diff] [blame] | 2842 | unsigned long caller) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2843 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2844 | unsigned int objnr; |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2845 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | |
Matthew Wilcox | 80cbd91 | 2007-11-29 12:05:13 -0700 | [diff] [blame] | 2847 | BUG_ON(virt_to_cache(objp) != cachep); |
| 2848 | |
Manfred Spraul | 3dafccf | 2006-02-01 03:05:42 -0800 | [diff] [blame] | 2849 | objp -= obj_offset(cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2850 | kfree_debugcheck(objp); |
Christoph Lameter | b49af68 | 2007-05-06 14:49:41 -0700 | [diff] [blame] | 2851 | page = virt_to_head_page(objp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2852 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | if (cachep->flags & SLAB_RED_ZONE) { |
Pekka Enberg | 58ce1fd | 2006-06-23 02:03:24 -0700 | [diff] [blame] | 2854 | verify_redzone_free(cachep, objp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2855 | *dbg_redzone1(cachep, objp) = RED_INACTIVE; |
| 2856 | *dbg_redzone2(cachep, objp) = RED_INACTIVE; |
| 2857 | } |
| 2858 | if (cachep->flags & SLAB_STORE_USER) |
Ezequiel Garcia | 7c0cb9c | 2012-09-08 17:47:55 -0300 | [diff] [blame] | 2859 | *dbg_userword(cachep, objp) = (void *)caller; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2860 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2861 | objnr = obj_to_index(cachep, page, objp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2862 | |
| 2863 | BUG_ON(objnr >= cachep->num); |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2864 | BUG_ON(objp != index_to_obj(cachep, page, objnr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2865 | |
Joonsoo Kim | 0378730 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 2866 | set_obj_status(page, objnr, OBJECT_FREE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2867 | if (cachep->flags & SLAB_POISON) { |
| 2868 | #ifdef CONFIG_DEBUG_PAGEALLOC |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 2869 | if ((cachep->size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) { |
Ezequiel Garcia | 7c0cb9c | 2012-09-08 17:47:55 -0300 | [diff] [blame] | 2870 | store_stackinfo(cachep, objp, caller); |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 2871 | kernel_map_pages(virt_to_page(objp), |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 2872 | cachep->size / PAGE_SIZE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2873 | } else { |
| 2874 | poison_obj(cachep, objp, POISON_FREE); |
| 2875 | } |
| 2876 | #else |
| 2877 | poison_obj(cachep, objp, POISON_FREE); |
| 2878 | #endif |
| 2879 | } |
| 2880 | return objp; |
| 2881 | } |
| 2882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2883 | #else |
| 2884 | #define kfree_debugcheck(x) do { } while(0) |
| 2885 | #define cache_free_debugcheck(x,objp,z) (objp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2886 | #endif |
| 2887 | |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 2888 | static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags, |
| 2889 | bool force_refill) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2890 | { |
| 2891 | int batchcount; |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2892 | struct kmem_cache_node *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2893 | struct array_cache *ac; |
Pekka Enberg | 1ca4cb2 | 2006-10-06 00:43:52 -0700 | [diff] [blame] | 2894 | int node; |
| 2895 | |
Joe Korty | 6d2144d | 2008-03-05 15:04:59 -0800 | [diff] [blame] | 2896 | check_irq_off(); |
Lee Schermerhorn | 7d6e6d0 | 2010-05-26 14:45:03 -0700 | [diff] [blame] | 2897 | node = numa_mem_id(); |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 2898 | if (unlikely(force_refill)) |
| 2899 | goto force_grow; |
| 2900 | retry: |
Joe Korty | 6d2144d | 2008-03-05 15:04:59 -0800 | [diff] [blame] | 2901 | ac = cpu_cache_get(cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2902 | batchcount = ac->batchcount; |
| 2903 | if (!ac->touched && batchcount > BATCHREFILL_LIMIT) { |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2904 | /* |
| 2905 | * If there was little recent activity on this cache, then |
| 2906 | * perform only a partial refill. Otherwise we could generate |
| 2907 | * refill bouncing. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2908 | */ |
| 2909 | batchcount = BATCHREFILL_LIMIT; |
| 2910 | } |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 2911 | n = get_node(cachep, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2912 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2913 | BUG_ON(ac->avail > 0 || !n); |
| 2914 | spin_lock(&n->list_lock); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 2915 | |
Christoph Lameter | 3ded175 | 2006-03-25 03:06:44 -0800 | [diff] [blame] | 2916 | /* See if we can refill from the shared array */ |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2917 | if (n->shared && transfer_objects(ac, n->shared, batchcount)) { |
| 2918 | n->shared->touched = 1; |
Christoph Lameter | 3ded175 | 2006-03-25 03:06:44 -0800 | [diff] [blame] | 2919 | goto alloc_done; |
Nick Piggin | 44b57f1 | 2010-01-27 22:27:40 +1100 | [diff] [blame] | 2920 | } |
Christoph Lameter | 3ded175 | 2006-03-25 03:06:44 -0800 | [diff] [blame] | 2921 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2922 | while (batchcount > 0) { |
| 2923 | struct list_head *entry; |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2924 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2925 | /* Get slab alloc is to come from. */ |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2926 | entry = n->slabs_partial.next; |
| 2927 | if (entry == &n->slabs_partial) { |
| 2928 | n->free_touched = 1; |
| 2929 | entry = n->slabs_free.next; |
| 2930 | if (entry == &n->slabs_free) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | goto must_grow; |
| 2932 | } |
| 2933 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2934 | page = list_entry(entry, struct page, lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2935 | check_spinlock_acquired(cachep); |
Pekka Enberg | 714b8171 | 2007-05-06 14:49:03 -0700 | [diff] [blame] | 2936 | |
| 2937 | /* |
| 2938 | * The slab was either on partial or free list so |
| 2939 | * there must be at least one object available for |
| 2940 | * allocation. |
| 2941 | */ |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2942 | BUG_ON(page->active >= cachep->num); |
Pekka Enberg | 714b8171 | 2007-05-06 14:49:03 -0700 | [diff] [blame] | 2943 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2944 | while (page->active < cachep->num && batchcount--) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2945 | STATS_INC_ALLOCED(cachep); |
| 2946 | STATS_INC_ACTIVE(cachep); |
| 2947 | STATS_SET_HIGH(cachep); |
| 2948 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2949 | ac_put_obj(cachep, ac, slab_get_obj(cachep, page, |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 2950 | node)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2951 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2952 | |
| 2953 | /* move slabp to correct slabp list: */ |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 2954 | list_del(&page->lru); |
| 2955 | if (page->active == cachep->num) |
Dave Hansen | 34bf6ef | 2014-04-08 13:44:27 -0700 | [diff] [blame] | 2956 | list_add(&page->lru, &n->slabs_full); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2957 | else |
Dave Hansen | 34bf6ef | 2014-04-08 13:44:27 -0700 | [diff] [blame] | 2958 | list_add(&page->lru, &n->slabs_partial); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2959 | } |
| 2960 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2961 | must_grow: |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2962 | n->free_objects -= ac->avail; |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2963 | alloc_done: |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 2964 | spin_unlock(&n->list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2965 | |
| 2966 | if (unlikely(!ac->avail)) { |
| 2967 | int x; |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 2968 | force_grow: |
Christoph Lameter | 3c517a6 | 2006-12-06 20:33:29 -0800 | [diff] [blame] | 2969 | x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 2970 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2971 | /* cache_grow can reenable interrupts, then ac could change. */ |
Pekka Enberg | 9a2dba4 | 2006-02-01 03:05:49 -0800 | [diff] [blame] | 2972 | ac = cpu_cache_get(cachep); |
David Rientjes | 51cd8e6 | 2012-08-28 19:57:21 -0700 | [diff] [blame] | 2973 | node = numa_mem_id(); |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 2974 | |
| 2975 | /* no objects in sight? abort */ |
| 2976 | if (!x && (ac->avail == 0 || force_refill)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2977 | return NULL; |
| 2978 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2979 | if (!ac->avail) /* objects refilled by interrupt? */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2980 | goto retry; |
| 2981 | } |
| 2982 | ac->touched = 1; |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 2983 | |
| 2984 | return ac_get_obj(cachep, ac, flags, force_refill); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2985 | } |
| 2986 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2987 | static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep, |
| 2988 | gfp_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2989 | { |
| 2990 | might_sleep_if(flags & __GFP_WAIT); |
| 2991 | #if DEBUG |
| 2992 | kmem_flagcheck(cachep, flags); |
| 2993 | #endif |
| 2994 | } |
| 2995 | |
| 2996 | #if DEBUG |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 2997 | static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep, |
Ezequiel Garcia | 7c0cb9c | 2012-09-08 17:47:55 -0300 | [diff] [blame] | 2998 | gfp_t flags, void *objp, unsigned long caller) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2999 | { |
Joonsoo Kim | 0378730 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 3000 | struct page *page; |
| 3001 | |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 3002 | if (!objp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3003 | return objp; |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 3004 | if (cachep->flags & SLAB_POISON) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3005 | #ifdef CONFIG_DEBUG_PAGEALLOC |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 3006 | if ((cachep->size % PAGE_SIZE) == 0 && OFF_SLAB(cachep)) |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 3007 | kernel_map_pages(virt_to_page(objp), |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 3008 | cachep->size / PAGE_SIZE, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3009 | else |
| 3010 | check_poison_obj(cachep, objp); |
| 3011 | #else |
| 3012 | check_poison_obj(cachep, objp); |
| 3013 | #endif |
| 3014 | poison_obj(cachep, objp, POISON_INUSE); |
| 3015 | } |
| 3016 | if (cachep->flags & SLAB_STORE_USER) |
Ezequiel Garcia | 7c0cb9c | 2012-09-08 17:47:55 -0300 | [diff] [blame] | 3017 | *dbg_userword(cachep, objp) = (void *)caller; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3018 | |
| 3019 | if (cachep->flags & SLAB_RED_ZONE) { |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 3020 | if (*dbg_redzone1(cachep, objp) != RED_INACTIVE || |
| 3021 | *dbg_redzone2(cachep, objp) != RED_INACTIVE) { |
| 3022 | slab_error(cachep, "double free, or memory outside" |
| 3023 | " object was overwritten"); |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 3024 | printk(KERN_ERR |
David Woodhouse | b46b8f1 | 2007-05-08 00:22:59 -0700 | [diff] [blame] | 3025 | "%p: redzone 1:0x%llx, redzone 2:0x%llx\n", |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 3026 | objp, *dbg_redzone1(cachep, objp), |
| 3027 | *dbg_redzone2(cachep, objp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | } |
| 3029 | *dbg_redzone1(cachep, objp) = RED_ACTIVE; |
| 3030 | *dbg_redzone2(cachep, objp) = RED_ACTIVE; |
| 3031 | } |
Joonsoo Kim | 0378730 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 3032 | |
| 3033 | page = virt_to_head_page(objp); |
| 3034 | set_obj_status(page, obj_to_index(cachep, page, objp), OBJECT_ACTIVE); |
Manfred Spraul | 3dafccf | 2006-02-01 03:05:42 -0800 | [diff] [blame] | 3035 | objp += obj_offset(cachep); |
Christoph Lameter | 4f10493 | 2007-05-06 14:50:17 -0700 | [diff] [blame] | 3036 | if (cachep->ctor && cachep->flags & SLAB_POISON) |
Alexey Dobriyan | 51cc506 | 2008-07-25 19:45:34 -0700 | [diff] [blame] | 3037 | cachep->ctor(objp); |
Tetsuo Handa | 7ea466f | 2011-07-21 09:42:45 +0900 | [diff] [blame] | 3038 | if (ARCH_SLAB_MINALIGN && |
| 3039 | ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) { |
Kevin Hilman | a44b56d | 2006-12-06 20:32:11 -0800 | [diff] [blame] | 3040 | printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n", |
Hugh Dickins | c225150 | 2011-07-11 13:35:08 -0700 | [diff] [blame] | 3041 | objp, (int)ARCH_SLAB_MINALIGN); |
Kevin Hilman | a44b56d | 2006-12-06 20:32:11 -0800 | [diff] [blame] | 3042 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3043 | return objp; |
| 3044 | } |
| 3045 | #else |
| 3046 | #define cache_alloc_debugcheck_after(a,b,objp,d) (objp) |
| 3047 | #endif |
| 3048 | |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 3049 | static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags) |
Akinobu Mita | 8a8b650 | 2006-12-08 02:39:44 -0800 | [diff] [blame] | 3050 | { |
Joonsoo Kim | 8a9c61d | 2014-08-06 16:04:20 -0700 | [diff] [blame^] | 3051 | if (unlikely(cachep == kmem_cache)) |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 3052 | return false; |
Akinobu Mita | 8a8b650 | 2006-12-08 02:39:44 -0800 | [diff] [blame] | 3053 | |
Christoph Lameter | 8c138bc | 2012-06-13 10:24:58 -0500 | [diff] [blame] | 3054 | return should_failslab(cachep->object_size, flags, cachep->flags); |
Akinobu Mita | 8a8b650 | 2006-12-08 02:39:44 -0800 | [diff] [blame] | 3055 | } |
| 3056 | |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 3057 | static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3058 | { |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 3059 | void *objp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3060 | struct array_cache *ac; |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 3061 | bool force_refill = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3062 | |
Alok N Kataria | 5c38230 | 2005-09-27 21:45:46 -0700 | [diff] [blame] | 3063 | check_irq_off(); |
Akinobu Mita | 8a8b650 | 2006-12-08 02:39:44 -0800 | [diff] [blame] | 3064 | |
Pekka Enberg | 9a2dba4 | 2006-02-01 03:05:49 -0800 | [diff] [blame] | 3065 | ac = cpu_cache_get(cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3066 | if (likely(ac->avail)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3067 | ac->touched = 1; |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 3068 | objp = ac_get_obj(cachep, ac, flags, false); |
| 3069 | |
J. R. Okajima | ddbf2e8 | 2009-12-02 16:55:50 +0900 | [diff] [blame] | 3070 | /* |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 3071 | * Allow for the possibility all avail objects are not allowed |
| 3072 | * by the current flags |
J. R. Okajima | ddbf2e8 | 2009-12-02 16:55:50 +0900 | [diff] [blame] | 3073 | */ |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 3074 | if (objp) { |
| 3075 | STATS_INC_ALLOCHIT(cachep); |
| 3076 | goto out; |
| 3077 | } |
| 3078 | force_refill = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3079 | } |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 3080 | |
| 3081 | STATS_INC_ALLOCMISS(cachep); |
| 3082 | objp = cache_alloc_refill(cachep, flags, force_refill); |
| 3083 | /* |
| 3084 | * the 'ac' may be updated by cache_alloc_refill(), |
| 3085 | * and kmemleak_erase() requires its correct value. |
| 3086 | */ |
| 3087 | ac = cpu_cache_get(cachep); |
| 3088 | |
| 3089 | out: |
Catalin Marinas | d5cff63 | 2009-06-11 13:22:40 +0100 | [diff] [blame] | 3090 | /* |
| 3091 | * To avoid a false negative, if an object that is in one of the |
| 3092 | * per-CPU caches is leaked, we need to make sure kmemleak doesn't |
| 3093 | * treat the array pointers as a reference to the object. |
| 3094 | */ |
J. R. Okajima | f3d8b53 | 2009-12-02 16:55:49 +0900 | [diff] [blame] | 3095 | if (objp) |
| 3096 | kmemleak_erase(&ac->entry[ac->avail]); |
Alok N Kataria | 5c38230 | 2005-09-27 21:45:46 -0700 | [diff] [blame] | 3097 | return objp; |
| 3098 | } |
| 3099 | |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3100 | #ifdef CONFIG_NUMA |
| 3101 | /* |
David Rientjes | f0432d1 | 2014-04-07 15:37:30 -0700 | [diff] [blame] | 3102 | * Try allocating on another node if PF_SPREAD_SLAB is a mempolicy is set. |
Paul Jackson | c61afb1 | 2006-03-24 03:16:08 -0800 | [diff] [blame] | 3103 | * |
| 3104 | * If we are in_interrupt, then process context, including cpusets and |
| 3105 | * mempolicy, may not apply and should not be used for allocation policy. |
| 3106 | */ |
| 3107 | static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags) |
| 3108 | { |
| 3109 | int nid_alloc, nid_here; |
| 3110 | |
Christoph Lameter | 765c450 | 2006-09-27 01:50:08 -0700 | [diff] [blame] | 3111 | if (in_interrupt() || (flags & __GFP_THISNODE)) |
Paul Jackson | c61afb1 | 2006-03-24 03:16:08 -0800 | [diff] [blame] | 3112 | return NULL; |
Lee Schermerhorn | 7d6e6d0 | 2010-05-26 14:45:03 -0700 | [diff] [blame] | 3113 | nid_alloc = nid_here = numa_mem_id(); |
Paul Jackson | c61afb1 | 2006-03-24 03:16:08 -0800 | [diff] [blame] | 3114 | if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD)) |
Jack Steiner | 6adef3e | 2010-05-26 14:42:49 -0700 | [diff] [blame] | 3115 | nid_alloc = cpuset_slab_spread_node(); |
Paul Jackson | c61afb1 | 2006-03-24 03:16:08 -0800 | [diff] [blame] | 3116 | else if (current->mempolicy) |
David Rientjes | 2a38961 | 2014-04-07 15:37:29 -0700 | [diff] [blame] | 3117 | nid_alloc = mempolicy_slab_node(); |
Paul Jackson | c61afb1 | 2006-03-24 03:16:08 -0800 | [diff] [blame] | 3118 | if (nid_alloc != nid_here) |
Christoph Hellwig | 8b98c16 | 2006-12-06 20:32:30 -0800 | [diff] [blame] | 3119 | return ____cache_alloc_node(cachep, flags, nid_alloc); |
Paul Jackson | c61afb1 | 2006-03-24 03:16:08 -0800 | [diff] [blame] | 3120 | return NULL; |
| 3121 | } |
| 3122 | |
| 3123 | /* |
Christoph Lameter | 765c450 | 2006-09-27 01:50:08 -0700 | [diff] [blame] | 3124 | * Fallback function if there was no memory available and no objects on a |
Christoph Lameter | 3c517a6 | 2006-12-06 20:33:29 -0800 | [diff] [blame] | 3125 | * certain node and fall back is permitted. First we scan all the |
Christoph Lameter | 6a67368 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3126 | * available node for available objects. If that fails then we |
Christoph Lameter | 3c517a6 | 2006-12-06 20:33:29 -0800 | [diff] [blame] | 3127 | * perform an allocation without specifying a node. This allows the page |
| 3128 | * allocator to do its reclaim / fallback magic. We then insert the |
| 3129 | * slab into the proper nodelist and then allocate from it. |
Christoph Lameter | 765c450 | 2006-09-27 01:50:08 -0700 | [diff] [blame] | 3130 | */ |
Pekka Enberg | 8c8cc2c | 2007-02-10 01:42:53 -0800 | [diff] [blame] | 3131 | static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags) |
Christoph Lameter | 765c450 | 2006-09-27 01:50:08 -0700 | [diff] [blame] | 3132 | { |
Pekka Enberg | 8c8cc2c | 2007-02-10 01:42:53 -0800 | [diff] [blame] | 3133 | struct zonelist *zonelist; |
| 3134 | gfp_t local_flags; |
Mel Gorman | dd1a239 | 2008-04-28 02:12:17 -0700 | [diff] [blame] | 3135 | struct zoneref *z; |
Mel Gorman | 54a6eb5 | 2008-04-28 02:12:16 -0700 | [diff] [blame] | 3136 | struct zone *zone; |
| 3137 | enum zone_type high_zoneidx = gfp_zone(flags); |
Christoph Lameter | 765c450 | 2006-09-27 01:50:08 -0700 | [diff] [blame] | 3138 | void *obj = NULL; |
Christoph Lameter | 3c517a6 | 2006-12-06 20:33:29 -0800 | [diff] [blame] | 3139 | int nid; |
Mel Gorman | cc9a6c8 | 2012-03-21 16:34:11 -0700 | [diff] [blame] | 3140 | unsigned int cpuset_mems_cookie; |
Pekka Enberg | 8c8cc2c | 2007-02-10 01:42:53 -0800 | [diff] [blame] | 3141 | |
| 3142 | if (flags & __GFP_THISNODE) |
| 3143 | return NULL; |
| 3144 | |
Christoph Lameter | 6cb0622 | 2007-10-16 01:25:41 -0700 | [diff] [blame] | 3145 | local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK); |
Christoph Lameter | 765c450 | 2006-09-27 01:50:08 -0700 | [diff] [blame] | 3146 | |
Mel Gorman | cc9a6c8 | 2012-03-21 16:34:11 -0700 | [diff] [blame] | 3147 | retry_cpuset: |
Mel Gorman | d26914d | 2014-04-03 14:47:24 -0700 | [diff] [blame] | 3148 | cpuset_mems_cookie = read_mems_allowed_begin(); |
David Rientjes | 2a38961 | 2014-04-07 15:37:29 -0700 | [diff] [blame] | 3149 | zonelist = node_zonelist(mempolicy_slab_node(), flags); |
Mel Gorman | cc9a6c8 | 2012-03-21 16:34:11 -0700 | [diff] [blame] | 3150 | |
Christoph Lameter | 3c517a6 | 2006-12-06 20:33:29 -0800 | [diff] [blame] | 3151 | retry: |
| 3152 | /* |
| 3153 | * Look through allowed nodes for objects available |
| 3154 | * from existing per node queues. |
| 3155 | */ |
Mel Gorman | 54a6eb5 | 2008-04-28 02:12:16 -0700 | [diff] [blame] | 3156 | for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) { |
| 3157 | nid = zone_to_nid(zone); |
Christoph Lameter | aedb0eb | 2006-10-21 10:24:16 -0700 | [diff] [blame] | 3158 | |
Mel Gorman | 54a6eb5 | 2008-04-28 02:12:16 -0700 | [diff] [blame] | 3159 | if (cpuset_zone_allowed_hardwall(zone, flags) && |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 3160 | get_node(cache, nid) && |
| 3161 | get_node(cache, nid)->free_objects) { |
Christoph Lameter | 3c517a6 | 2006-12-06 20:33:29 -0800 | [diff] [blame] | 3162 | obj = ____cache_alloc_node(cache, |
| 3163 | flags | GFP_THISNODE, nid); |
Christoph Lameter | 481c534 | 2008-06-21 16:46:35 -0700 | [diff] [blame] | 3164 | if (obj) |
| 3165 | break; |
| 3166 | } |
Christoph Lameter | 3c517a6 | 2006-12-06 20:33:29 -0800 | [diff] [blame] | 3167 | } |
| 3168 | |
Christoph Lameter | cfce660 | 2007-05-06 14:50:17 -0700 | [diff] [blame] | 3169 | if (!obj) { |
Christoph Lameter | 3c517a6 | 2006-12-06 20:33:29 -0800 | [diff] [blame] | 3170 | /* |
| 3171 | * This allocation will be performed within the constraints |
| 3172 | * of the current cpuset / memory policy requirements. |
| 3173 | * We may trigger various forms of reclaim on the allowed |
| 3174 | * set and go into memory reserves if necessary. |
| 3175 | */ |
Joonsoo Kim | 0c3aa83 | 2013-10-24 10:07:38 +0900 | [diff] [blame] | 3176 | struct page *page; |
| 3177 | |
Christoph Lameter | dd47ea7 | 2006-12-13 00:34:11 -0800 | [diff] [blame] | 3178 | if (local_flags & __GFP_WAIT) |
| 3179 | local_irq_enable(); |
| 3180 | kmem_flagcheck(cache, flags); |
Joonsoo Kim | 0c3aa83 | 2013-10-24 10:07:38 +0900 | [diff] [blame] | 3181 | page = kmem_getpages(cache, local_flags, numa_mem_id()); |
Christoph Lameter | dd47ea7 | 2006-12-13 00:34:11 -0800 | [diff] [blame] | 3182 | if (local_flags & __GFP_WAIT) |
| 3183 | local_irq_disable(); |
Joonsoo Kim | 0c3aa83 | 2013-10-24 10:07:38 +0900 | [diff] [blame] | 3184 | if (page) { |
Christoph Lameter | 3c517a6 | 2006-12-06 20:33:29 -0800 | [diff] [blame] | 3185 | /* |
| 3186 | * Insert into the appropriate per node queues |
| 3187 | */ |
Joonsoo Kim | 0c3aa83 | 2013-10-24 10:07:38 +0900 | [diff] [blame] | 3188 | nid = page_to_nid(page); |
| 3189 | if (cache_grow(cache, flags, nid, page)) { |
Christoph Lameter | 3c517a6 | 2006-12-06 20:33:29 -0800 | [diff] [blame] | 3190 | obj = ____cache_alloc_node(cache, |
| 3191 | flags | GFP_THISNODE, nid); |
| 3192 | if (!obj) |
| 3193 | /* |
| 3194 | * Another processor may allocate the |
| 3195 | * objects in the slab since we are |
| 3196 | * not holding any locks. |
| 3197 | */ |
| 3198 | goto retry; |
| 3199 | } else { |
Hugh Dickins | b6a6045 | 2007-01-05 16:36:36 -0800 | [diff] [blame] | 3200 | /* cache_grow already freed obj */ |
Christoph Lameter | 3c517a6 | 2006-12-06 20:33:29 -0800 | [diff] [blame] | 3201 | obj = NULL; |
| 3202 | } |
| 3203 | } |
Christoph Lameter | aedb0eb | 2006-10-21 10:24:16 -0700 | [diff] [blame] | 3204 | } |
Mel Gorman | cc9a6c8 | 2012-03-21 16:34:11 -0700 | [diff] [blame] | 3205 | |
Mel Gorman | d26914d | 2014-04-03 14:47:24 -0700 | [diff] [blame] | 3206 | if (unlikely(!obj && read_mems_allowed_retry(cpuset_mems_cookie))) |
Mel Gorman | cc9a6c8 | 2012-03-21 16:34:11 -0700 | [diff] [blame] | 3207 | goto retry_cpuset; |
Christoph Lameter | 765c450 | 2006-09-27 01:50:08 -0700 | [diff] [blame] | 3208 | return obj; |
| 3209 | } |
| 3210 | |
| 3211 | /* |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3212 | * A interface to enable slab creation on nodeid |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3213 | */ |
Christoph Hellwig | 8b98c16 | 2006-12-06 20:32:30 -0800 | [diff] [blame] | 3214 | static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 3215 | int nodeid) |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3216 | { |
| 3217 | struct list_head *entry; |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 3218 | struct page *page; |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3219 | struct kmem_cache_node *n; |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 3220 | void *obj; |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 3221 | int x; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3222 | |
Aaron Tomlin | 14e50c6 | 2013-04-26 16:15:34 +0100 | [diff] [blame] | 3223 | VM_BUG_ON(nodeid > num_online_nodes()); |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 3224 | n = get_node(cachep, nodeid); |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3225 | BUG_ON(!n); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3226 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 3227 | retry: |
Ravikiran G Thirumalai | ca3b9b9 | 2006-02-04 23:27:58 -0800 | [diff] [blame] | 3228 | check_irq_off(); |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3229 | spin_lock(&n->list_lock); |
| 3230 | entry = n->slabs_partial.next; |
| 3231 | if (entry == &n->slabs_partial) { |
| 3232 | n->free_touched = 1; |
| 3233 | entry = n->slabs_free.next; |
| 3234 | if (entry == &n->slabs_free) |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 3235 | goto must_grow; |
| 3236 | } |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3237 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 3238 | page = list_entry(entry, struct page, lru); |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 3239 | check_spinlock_acquired_node(cachep, nodeid); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3240 | |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 3241 | STATS_INC_NODEALLOCS(cachep); |
| 3242 | STATS_INC_ACTIVE(cachep); |
| 3243 | STATS_SET_HIGH(cachep); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3244 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 3245 | BUG_ON(page->active == cachep->num); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3246 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 3247 | obj = slab_get_obj(cachep, page, nodeid); |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3248 | n->free_objects--; |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 3249 | /* move slabp to correct slabp list: */ |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 3250 | list_del(&page->lru); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3251 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 3252 | if (page->active == cachep->num) |
| 3253 | list_add(&page->lru, &n->slabs_full); |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 3254 | else |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 3255 | list_add(&page->lru, &n->slabs_partial); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3256 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3257 | spin_unlock(&n->list_lock); |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 3258 | goto done; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3259 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 3260 | must_grow: |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3261 | spin_unlock(&n->list_lock); |
Christoph Lameter | 3c517a6 | 2006-12-06 20:33:29 -0800 | [diff] [blame] | 3262 | x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL); |
Christoph Lameter | 765c450 | 2006-09-27 01:50:08 -0700 | [diff] [blame] | 3263 | if (x) |
| 3264 | goto retry; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3265 | |
Pekka Enberg | 8c8cc2c | 2007-02-10 01:42:53 -0800 | [diff] [blame] | 3266 | return fallback_alloc(cachep, flags); |
Christoph Lameter | 765c450 | 2006-09-27 01:50:08 -0700 | [diff] [blame] | 3267 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 3268 | done: |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 3269 | return obj; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3270 | } |
Pekka Enberg | 8c8cc2c | 2007-02-10 01:42:53 -0800 | [diff] [blame] | 3271 | |
Pekka Enberg | 8c8cc2c | 2007-02-10 01:42:53 -0800 | [diff] [blame] | 3272 | static __always_inline void * |
Ezequiel Garcia | 4835630 | 2012-09-08 17:47:57 -0300 | [diff] [blame] | 3273 | slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid, |
Ezequiel Garcia | 7c0cb9c | 2012-09-08 17:47:55 -0300 | [diff] [blame] | 3274 | unsigned long caller) |
Pekka Enberg | 8c8cc2c | 2007-02-10 01:42:53 -0800 | [diff] [blame] | 3275 | { |
| 3276 | unsigned long save_flags; |
| 3277 | void *ptr; |
Lee Schermerhorn | 7d6e6d0 | 2010-05-26 14:45:03 -0700 | [diff] [blame] | 3278 | int slab_node = numa_mem_id(); |
Pekka Enberg | 8c8cc2c | 2007-02-10 01:42:53 -0800 | [diff] [blame] | 3279 | |
Benjamin Herrenschmidt | dcce284 | 2009-06-18 13:24:12 +1000 | [diff] [blame] | 3280 | flags &= gfp_allowed_mask; |
Pekka Enberg | 7e85ee0 | 2009-06-12 14:03:06 +0300 | [diff] [blame] | 3281 | |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 3282 | lockdep_trace_alloc(flags); |
| 3283 | |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 3284 | if (slab_should_failslab(cachep, flags)) |
Akinobu Mita | 824ebef | 2007-05-06 14:49:58 -0700 | [diff] [blame] | 3285 | return NULL; |
| 3286 | |
Glauber Costa | d79923f | 2012-12-18 14:22:48 -0800 | [diff] [blame] | 3287 | cachep = memcg_kmem_get_cache(cachep, flags); |
| 3288 | |
Pekka Enberg | 8c8cc2c | 2007-02-10 01:42:53 -0800 | [diff] [blame] | 3289 | cache_alloc_debugcheck_before(cachep, flags); |
| 3290 | local_irq_save(save_flags); |
| 3291 | |
Andrew Morton | eacbbae | 2011-07-28 13:59:49 -0700 | [diff] [blame] | 3292 | if (nodeid == NUMA_NO_NODE) |
Lee Schermerhorn | 7d6e6d0 | 2010-05-26 14:45:03 -0700 | [diff] [blame] | 3293 | nodeid = slab_node; |
Pekka Enberg | 8c8cc2c | 2007-02-10 01:42:53 -0800 | [diff] [blame] | 3294 | |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 3295 | if (unlikely(!get_node(cachep, nodeid))) { |
Pekka Enberg | 8c8cc2c | 2007-02-10 01:42:53 -0800 | [diff] [blame] | 3296 | /* Node not bootstrapped yet */ |
| 3297 | ptr = fallback_alloc(cachep, flags); |
| 3298 | goto out; |
| 3299 | } |
| 3300 | |
Lee Schermerhorn | 7d6e6d0 | 2010-05-26 14:45:03 -0700 | [diff] [blame] | 3301 | if (nodeid == slab_node) { |
Pekka Enberg | 8c8cc2c | 2007-02-10 01:42:53 -0800 | [diff] [blame] | 3302 | /* |
| 3303 | * Use the locally cached objects if possible. |
| 3304 | * However ____cache_alloc does not allow fallback |
| 3305 | * to other nodes. It may fail while we still have |
| 3306 | * objects on other nodes available. |
| 3307 | */ |
| 3308 | ptr = ____cache_alloc(cachep, flags); |
| 3309 | if (ptr) |
| 3310 | goto out; |
| 3311 | } |
| 3312 | /* ___cache_alloc_node can fall back to other nodes */ |
| 3313 | ptr = ____cache_alloc_node(cachep, flags, nodeid); |
| 3314 | out: |
| 3315 | local_irq_restore(save_flags); |
| 3316 | ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller); |
Christoph Lameter | 8c138bc | 2012-06-13 10:24:58 -0500 | [diff] [blame] | 3317 | kmemleak_alloc_recursive(ptr, cachep->object_size, 1, cachep->flags, |
Catalin Marinas | d5cff63 | 2009-06-11 13:22:40 +0100 | [diff] [blame] | 3318 | flags); |
Pekka Enberg | 8c8cc2c | 2007-02-10 01:42:53 -0800 | [diff] [blame] | 3319 | |
Joe Perches | 5087c82 | 2013-09-10 17:02:51 -0700 | [diff] [blame] | 3320 | if (likely(ptr)) { |
Christoph Lameter | 8c138bc | 2012-06-13 10:24:58 -0500 | [diff] [blame] | 3321 | kmemcheck_slab_alloc(cachep, flags, ptr, cachep->object_size); |
Joe Perches | 5087c82 | 2013-09-10 17:02:51 -0700 | [diff] [blame] | 3322 | if (unlikely(flags & __GFP_ZERO)) |
| 3323 | memset(ptr, 0, cachep->object_size); |
| 3324 | } |
Christoph Lameter | d07dbea | 2007-07-17 04:03:23 -0700 | [diff] [blame] | 3325 | |
Pekka Enberg | 8c8cc2c | 2007-02-10 01:42:53 -0800 | [diff] [blame] | 3326 | return ptr; |
| 3327 | } |
| 3328 | |
| 3329 | static __always_inline void * |
| 3330 | __do_cache_alloc(struct kmem_cache *cache, gfp_t flags) |
| 3331 | { |
| 3332 | void *objp; |
| 3333 | |
David Rientjes | f0432d1 | 2014-04-07 15:37:30 -0700 | [diff] [blame] | 3334 | if (current->mempolicy || unlikely(current->flags & PF_SPREAD_SLAB)) { |
Pekka Enberg | 8c8cc2c | 2007-02-10 01:42:53 -0800 | [diff] [blame] | 3335 | objp = alternate_node_alloc(cache, flags); |
| 3336 | if (objp) |
| 3337 | goto out; |
| 3338 | } |
| 3339 | objp = ____cache_alloc(cache, flags); |
| 3340 | |
| 3341 | /* |
| 3342 | * We may just have run out of memory on the local node. |
| 3343 | * ____cache_alloc_node() knows how to locate memory on other nodes |
| 3344 | */ |
Lee Schermerhorn | 7d6e6d0 | 2010-05-26 14:45:03 -0700 | [diff] [blame] | 3345 | if (!objp) |
| 3346 | objp = ____cache_alloc_node(cache, flags, numa_mem_id()); |
Pekka Enberg | 8c8cc2c | 2007-02-10 01:42:53 -0800 | [diff] [blame] | 3347 | |
| 3348 | out: |
| 3349 | return objp; |
| 3350 | } |
| 3351 | #else |
| 3352 | |
| 3353 | static __always_inline void * |
| 3354 | __do_cache_alloc(struct kmem_cache *cachep, gfp_t flags) |
| 3355 | { |
| 3356 | return ____cache_alloc(cachep, flags); |
| 3357 | } |
| 3358 | |
| 3359 | #endif /* CONFIG_NUMA */ |
| 3360 | |
| 3361 | static __always_inline void * |
Ezequiel Garcia | 4835630 | 2012-09-08 17:47:57 -0300 | [diff] [blame] | 3362 | slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller) |
Pekka Enberg | 8c8cc2c | 2007-02-10 01:42:53 -0800 | [diff] [blame] | 3363 | { |
| 3364 | unsigned long save_flags; |
| 3365 | void *objp; |
| 3366 | |
Benjamin Herrenschmidt | dcce284 | 2009-06-18 13:24:12 +1000 | [diff] [blame] | 3367 | flags &= gfp_allowed_mask; |
Pekka Enberg | 7e85ee0 | 2009-06-12 14:03:06 +0300 | [diff] [blame] | 3368 | |
Nick Piggin | cf40bd1 | 2009-01-21 08:12:39 +0100 | [diff] [blame] | 3369 | lockdep_trace_alloc(flags); |
| 3370 | |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 3371 | if (slab_should_failslab(cachep, flags)) |
Akinobu Mita | 824ebef | 2007-05-06 14:49:58 -0700 | [diff] [blame] | 3372 | return NULL; |
| 3373 | |
Glauber Costa | d79923f | 2012-12-18 14:22:48 -0800 | [diff] [blame] | 3374 | cachep = memcg_kmem_get_cache(cachep, flags); |
| 3375 | |
Pekka Enberg | 8c8cc2c | 2007-02-10 01:42:53 -0800 | [diff] [blame] | 3376 | cache_alloc_debugcheck_before(cachep, flags); |
| 3377 | local_irq_save(save_flags); |
| 3378 | objp = __do_cache_alloc(cachep, flags); |
| 3379 | local_irq_restore(save_flags); |
| 3380 | objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller); |
Christoph Lameter | 8c138bc | 2012-06-13 10:24:58 -0500 | [diff] [blame] | 3381 | kmemleak_alloc_recursive(objp, cachep->object_size, 1, cachep->flags, |
Catalin Marinas | d5cff63 | 2009-06-11 13:22:40 +0100 | [diff] [blame] | 3382 | flags); |
Pekka Enberg | 8c8cc2c | 2007-02-10 01:42:53 -0800 | [diff] [blame] | 3383 | prefetchw(objp); |
| 3384 | |
Joe Perches | 5087c82 | 2013-09-10 17:02:51 -0700 | [diff] [blame] | 3385 | if (likely(objp)) { |
Christoph Lameter | 8c138bc | 2012-06-13 10:24:58 -0500 | [diff] [blame] | 3386 | kmemcheck_slab_alloc(cachep, flags, objp, cachep->object_size); |
Joe Perches | 5087c82 | 2013-09-10 17:02:51 -0700 | [diff] [blame] | 3387 | if (unlikely(flags & __GFP_ZERO)) |
| 3388 | memset(objp, 0, cachep->object_size); |
| 3389 | } |
Christoph Lameter | d07dbea | 2007-07-17 04:03:23 -0700 | [diff] [blame] | 3390 | |
Pekka Enberg | 8c8cc2c | 2007-02-10 01:42:53 -0800 | [diff] [blame] | 3391 | return objp; |
| 3392 | } |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3393 | |
| 3394 | /* |
Jianyu Zhan | 5f0985b | 2014-03-30 17:02:20 +0800 | [diff] [blame] | 3395 | * Caller needs to acquire correct kmem_cache_node's list_lock |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3396 | */ |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 3397 | static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects, |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 3398 | int node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3399 | { |
| 3400 | int i; |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3401 | struct kmem_cache_node *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3402 | |
| 3403 | for (i = 0; i < nr_objects; i++) { |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 3404 | void *objp; |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 3405 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3406 | |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 3407 | clear_obj_pfmemalloc(&objpp[i]); |
| 3408 | objp = objpp[i]; |
| 3409 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 3410 | page = virt_to_head_page(objp); |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 3411 | n = get_node(cachep, node); |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 3412 | list_del(&page->lru); |
Christoph Lameter | ff69416 | 2005-09-22 21:44:02 -0700 | [diff] [blame] | 3413 | check_spinlock_acquired_node(cachep, node); |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 3414 | slab_put_obj(cachep, page, objp, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | STATS_DEC_ACTIVE(cachep); |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3416 | n->free_objects++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3417 | |
| 3418 | /* fixup slab chains */ |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 3419 | if (page->active == 0) { |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3420 | if (n->free_objects > n->free_limit) { |
| 3421 | n->free_objects -= cachep->num; |
Ravikiran G Thirumalai | e5ac9c5 | 2006-09-25 23:31:34 -0700 | [diff] [blame] | 3422 | /* No need to drop any previously held |
| 3423 | * lock here, even if we have a off-slab slab |
| 3424 | * descriptor it is guaranteed to come from |
| 3425 | * a different cache, refer to comments before |
| 3426 | * alloc_slabmgmt. |
| 3427 | */ |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 3428 | slab_destroy(cachep, page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3429 | } else { |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 3430 | list_add(&page->lru, &n->slabs_free); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3431 | } |
| 3432 | } else { |
| 3433 | /* Unconditionally move a slab to the end of the |
| 3434 | * partial list on free - maximum time for the |
| 3435 | * other objects to be freed, too. |
| 3436 | */ |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 3437 | list_add_tail(&page->lru, &n->slabs_partial); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3438 | } |
| 3439 | } |
| 3440 | } |
| 3441 | |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 3442 | static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3443 | { |
| 3444 | int batchcount; |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3445 | struct kmem_cache_node *n; |
Lee Schermerhorn | 7d6e6d0 | 2010-05-26 14:45:03 -0700 | [diff] [blame] | 3446 | int node = numa_mem_id(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3447 | |
| 3448 | batchcount = ac->batchcount; |
| 3449 | #if DEBUG |
| 3450 | BUG_ON(!batchcount || batchcount > ac->avail); |
| 3451 | #endif |
| 3452 | check_irq_off(); |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 3453 | n = get_node(cachep, node); |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3454 | spin_lock(&n->list_lock); |
| 3455 | if (n->shared) { |
| 3456 | struct array_cache *shared_array = n->shared; |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 3457 | int max = shared_array->limit - shared_array->avail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3458 | if (max) { |
| 3459 | if (batchcount > max) |
| 3460 | batchcount = max; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3461 | memcpy(&(shared_array->entry[shared_array->avail]), |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 3462 | ac->entry, sizeof(void *) * batchcount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3463 | shared_array->avail += batchcount; |
| 3464 | goto free_done; |
| 3465 | } |
| 3466 | } |
| 3467 | |
Christoph Lameter | ff69416 | 2005-09-22 21:44:02 -0700 | [diff] [blame] | 3468 | free_block(cachep, ac->entry, batchcount, node); |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 3469 | free_done: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3470 | #if STATS |
| 3471 | { |
| 3472 | int i = 0; |
| 3473 | struct list_head *p; |
| 3474 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3475 | p = n->slabs_free.next; |
| 3476 | while (p != &(n->slabs_free)) { |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 3477 | struct page *page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3478 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 3479 | page = list_entry(p, struct page, lru); |
| 3480 | BUG_ON(page->active); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3481 | |
| 3482 | i++; |
| 3483 | p = p->next; |
| 3484 | } |
| 3485 | STATS_SET_FREEABLE(cachep, i); |
| 3486 | } |
| 3487 | #endif |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3488 | spin_unlock(&n->list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3489 | ac->avail -= batchcount; |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 3490 | memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3491 | } |
| 3492 | |
| 3493 | /* |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 3494 | * Release an obj back to its cache. If the obj has a constructed state, it must |
| 3495 | * be in this state _before_ it is released. Called with disabled ints. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3496 | */ |
Suleiman Souhlal | a947eb9 | 2011-06-02 00:16:42 -0700 | [diff] [blame] | 3497 | static inline void __cache_free(struct kmem_cache *cachep, void *objp, |
Ezequiel Garcia | 7c0cb9c | 2012-09-08 17:47:55 -0300 | [diff] [blame] | 3498 | unsigned long caller) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3499 | { |
Pekka Enberg | 9a2dba4 | 2006-02-01 03:05:49 -0800 | [diff] [blame] | 3500 | struct array_cache *ac = cpu_cache_get(cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3501 | |
| 3502 | check_irq_off(); |
Catalin Marinas | d5cff63 | 2009-06-11 13:22:40 +0100 | [diff] [blame] | 3503 | kmemleak_free_recursive(objp, cachep->flags); |
Suleiman Souhlal | a947eb9 | 2011-06-02 00:16:42 -0700 | [diff] [blame] | 3504 | objp = cache_free_debugcheck(cachep, objp, caller); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3505 | |
Christoph Lameter | 8c138bc | 2012-06-13 10:24:58 -0500 | [diff] [blame] | 3506 | kmemcheck_slab_free(cachep, objp, cachep->object_size); |
Pekka Enberg | c175eea | 2008-05-09 20:35:53 +0200 | [diff] [blame] | 3507 | |
Siddha, Suresh B | 1807a1a | 2007-08-22 14:01:49 -0700 | [diff] [blame] | 3508 | /* |
| 3509 | * Skip calling cache_free_alien() when the platform is not numa. |
| 3510 | * This will avoid cache misses that happen while accessing slabp (which |
| 3511 | * is per page memory reference) to get nodeid. Instead use a global |
| 3512 | * variable to skip the call, which is mostly likely to be present in |
| 3513 | * the cache. |
| 3514 | */ |
Mel Gorman | b6e68bc | 2009-06-16 15:32:16 -0700 | [diff] [blame] | 3515 | if (nr_online_nodes > 1 && cache_free_alien(cachep, objp)) |
Pekka Enberg | 729bd0b | 2006-06-23 02:03:05 -0700 | [diff] [blame] | 3516 | return; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3518 | if (likely(ac->avail < ac->limit)) { |
| 3519 | STATS_INC_FREEHIT(cachep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3520 | } else { |
| 3521 | STATS_INC_FREEMISS(cachep); |
| 3522 | cache_flusharray(cachep, ac); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3523 | } |
Zhao Jin | 42c8c99 | 2011-08-27 00:26:17 +0800 | [diff] [blame] | 3524 | |
Mel Gorman | 072bb0a | 2012-07-31 16:43:58 -0700 | [diff] [blame] | 3525 | ac_put_obj(cachep, ac, objp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3526 | } |
| 3527 | |
| 3528 | /** |
| 3529 | * kmem_cache_alloc - Allocate an object |
| 3530 | * @cachep: The cache to allocate from. |
| 3531 | * @flags: See kmalloc(). |
| 3532 | * |
| 3533 | * Allocate an object from this cache. The flags are only relevant |
| 3534 | * if the cache has no available objects. |
| 3535 | */ |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 3536 | void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3537 | { |
Ezequiel Garcia | 4835630 | 2012-09-08 17:47:57 -0300 | [diff] [blame] | 3538 | void *ret = slab_alloc(cachep, flags, _RET_IP_); |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 3539 | |
Eduard - Gabriel Munteanu | ca2b84cb | 2009-03-23 15:12:24 +0200 | [diff] [blame] | 3540 | trace_kmem_cache_alloc(_RET_IP_, ret, |
Christoph Lameter | 8c138bc | 2012-06-13 10:24:58 -0500 | [diff] [blame] | 3541 | cachep->object_size, cachep->size, flags); |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 3542 | |
| 3543 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3544 | } |
| 3545 | EXPORT_SYMBOL(kmem_cache_alloc); |
| 3546 | |
Li Zefan | 0f24f12 | 2009-12-11 15:45:30 +0800 | [diff] [blame] | 3547 | #ifdef CONFIG_TRACING |
Steven Rostedt | 85beb58 | 2010-11-24 16:23:34 -0500 | [diff] [blame] | 3548 | void * |
Ezequiel Garcia | 4052147 | 2012-09-08 17:47:56 -0300 | [diff] [blame] | 3549 | kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size) |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 3550 | { |
Steven Rostedt | 85beb58 | 2010-11-24 16:23:34 -0500 | [diff] [blame] | 3551 | void *ret; |
| 3552 | |
Ezequiel Garcia | 4835630 | 2012-09-08 17:47:57 -0300 | [diff] [blame] | 3553 | ret = slab_alloc(cachep, flags, _RET_IP_); |
Steven Rostedt | 85beb58 | 2010-11-24 16:23:34 -0500 | [diff] [blame] | 3554 | |
| 3555 | trace_kmalloc(_RET_IP_, ret, |
Ezequiel Garcia | ff4fcd0 | 2012-09-08 17:47:52 -0300 | [diff] [blame] | 3556 | size, cachep->size, flags); |
Steven Rostedt | 85beb58 | 2010-11-24 16:23:34 -0500 | [diff] [blame] | 3557 | return ret; |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 3558 | } |
Steven Rostedt | 85beb58 | 2010-11-24 16:23:34 -0500 | [diff] [blame] | 3559 | EXPORT_SYMBOL(kmem_cache_alloc_trace); |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 3560 | #endif |
| 3561 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3562 | #ifdef CONFIG_NUMA |
Zhouping Liu | d0d04b7 | 2013-05-16 11:36:23 +0800 | [diff] [blame] | 3563 | /** |
| 3564 | * kmem_cache_alloc_node - Allocate an object on the specified node |
| 3565 | * @cachep: The cache to allocate from. |
| 3566 | * @flags: See kmalloc(). |
| 3567 | * @nodeid: node number of the target node. |
| 3568 | * |
| 3569 | * Identical to kmem_cache_alloc but it will allocate memory on the given |
| 3570 | * node, which can improve the performance for cpu bound structures. |
| 3571 | * |
| 3572 | * Fallback to other node is possible if __GFP_THISNODE is not set. |
| 3573 | */ |
Christoph Hellwig | 8b98c16 | 2006-12-06 20:32:30 -0800 | [diff] [blame] | 3574 | void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid) |
| 3575 | { |
Ezequiel Garcia | 4835630 | 2012-09-08 17:47:57 -0300 | [diff] [blame] | 3576 | void *ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_); |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 3577 | |
Eduard - Gabriel Munteanu | ca2b84cb | 2009-03-23 15:12:24 +0200 | [diff] [blame] | 3578 | trace_kmem_cache_alloc_node(_RET_IP_, ret, |
Christoph Lameter | 8c138bc | 2012-06-13 10:24:58 -0500 | [diff] [blame] | 3579 | cachep->object_size, cachep->size, |
Eduard - Gabriel Munteanu | ca2b84cb | 2009-03-23 15:12:24 +0200 | [diff] [blame] | 3580 | flags, nodeid); |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 3581 | |
| 3582 | return ret; |
Christoph Hellwig | 8b98c16 | 2006-12-06 20:32:30 -0800 | [diff] [blame] | 3583 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3584 | EXPORT_SYMBOL(kmem_cache_alloc_node); |
| 3585 | |
Li Zefan | 0f24f12 | 2009-12-11 15:45:30 +0800 | [diff] [blame] | 3586 | #ifdef CONFIG_TRACING |
Ezequiel Garcia | 4052147 | 2012-09-08 17:47:56 -0300 | [diff] [blame] | 3587 | void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep, |
Steven Rostedt | 85beb58 | 2010-11-24 16:23:34 -0500 | [diff] [blame] | 3588 | gfp_t flags, |
Ezequiel Garcia | 4052147 | 2012-09-08 17:47:56 -0300 | [diff] [blame] | 3589 | int nodeid, |
| 3590 | size_t size) |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 3591 | { |
Steven Rostedt | 85beb58 | 2010-11-24 16:23:34 -0500 | [diff] [blame] | 3592 | void *ret; |
| 3593 | |
Ezequiel Garcia | 592f414 | 2012-09-25 08:07:08 -0300 | [diff] [blame] | 3594 | ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_); |
Ezequiel Garcia | 7c0cb9c | 2012-09-08 17:47:55 -0300 | [diff] [blame] | 3595 | |
Steven Rostedt | 85beb58 | 2010-11-24 16:23:34 -0500 | [diff] [blame] | 3596 | trace_kmalloc_node(_RET_IP_, ret, |
Ezequiel Garcia | ff4fcd0 | 2012-09-08 17:47:52 -0300 | [diff] [blame] | 3597 | size, cachep->size, |
Steven Rostedt | 85beb58 | 2010-11-24 16:23:34 -0500 | [diff] [blame] | 3598 | flags, nodeid); |
| 3599 | return ret; |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 3600 | } |
Steven Rostedt | 85beb58 | 2010-11-24 16:23:34 -0500 | [diff] [blame] | 3601 | EXPORT_SYMBOL(kmem_cache_alloc_node_trace); |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 3602 | #endif |
| 3603 | |
Christoph Hellwig | 8b98c16 | 2006-12-06 20:32:30 -0800 | [diff] [blame] | 3604 | static __always_inline void * |
Ezequiel Garcia | 7c0cb9c | 2012-09-08 17:47:55 -0300 | [diff] [blame] | 3605 | __do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller) |
Manfred Spraul | 97e2bde | 2005-05-01 08:58:38 -0700 | [diff] [blame] | 3606 | { |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 3607 | struct kmem_cache *cachep; |
Manfred Spraul | 97e2bde | 2005-05-01 08:58:38 -0700 | [diff] [blame] | 3608 | |
Christoph Lameter | 2c59dd6 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3609 | cachep = kmalloc_slab(size, flags); |
Christoph Lameter | 6cb8f91 | 2007-07-17 04:03:22 -0700 | [diff] [blame] | 3610 | if (unlikely(ZERO_OR_NULL_PTR(cachep))) |
| 3611 | return cachep; |
Ezequiel Garcia | 4052147 | 2012-09-08 17:47:56 -0300 | [diff] [blame] | 3612 | return kmem_cache_alloc_node_trace(cachep, flags, node, size); |
Manfred Spraul | 97e2bde | 2005-05-01 08:58:38 -0700 | [diff] [blame] | 3613 | } |
Christoph Hellwig | 8b98c16 | 2006-12-06 20:32:30 -0800 | [diff] [blame] | 3614 | |
Li Zefan | 0bb38a5 | 2009-12-11 15:45:50 +0800 | [diff] [blame] | 3615 | #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING) |
Christoph Hellwig | 8b98c16 | 2006-12-06 20:32:30 -0800 | [diff] [blame] | 3616 | void *__kmalloc_node(size_t size, gfp_t flags, int node) |
| 3617 | { |
Ezequiel Garcia | 7c0cb9c | 2012-09-08 17:47:55 -0300 | [diff] [blame] | 3618 | return __do_kmalloc_node(size, flags, node, _RET_IP_); |
Christoph Hellwig | 8b98c16 | 2006-12-06 20:32:30 -0800 | [diff] [blame] | 3619 | } |
Christoph Hellwig | dbe5e69 | 2006-09-25 23:31:36 -0700 | [diff] [blame] | 3620 | EXPORT_SYMBOL(__kmalloc_node); |
Christoph Hellwig | 8b98c16 | 2006-12-06 20:32:30 -0800 | [diff] [blame] | 3621 | |
| 3622 | void *__kmalloc_node_track_caller(size_t size, gfp_t flags, |
Eduard - Gabriel Munteanu | ce71e27 | 2008-08-19 20:43:25 +0300 | [diff] [blame] | 3623 | int node, unsigned long caller) |
Christoph Hellwig | 8b98c16 | 2006-12-06 20:32:30 -0800 | [diff] [blame] | 3624 | { |
Ezequiel Garcia | 7c0cb9c | 2012-09-08 17:47:55 -0300 | [diff] [blame] | 3625 | return __do_kmalloc_node(size, flags, node, caller); |
Christoph Hellwig | 8b98c16 | 2006-12-06 20:32:30 -0800 | [diff] [blame] | 3626 | } |
| 3627 | EXPORT_SYMBOL(__kmalloc_node_track_caller); |
| 3628 | #else |
| 3629 | void *__kmalloc_node(size_t size, gfp_t flags, int node) |
| 3630 | { |
Ezequiel Garcia | 7c0cb9c | 2012-09-08 17:47:55 -0300 | [diff] [blame] | 3631 | return __do_kmalloc_node(size, flags, node, 0); |
Christoph Hellwig | 8b98c16 | 2006-12-06 20:32:30 -0800 | [diff] [blame] | 3632 | } |
| 3633 | EXPORT_SYMBOL(__kmalloc_node); |
Li Zefan | 0bb38a5 | 2009-12-11 15:45:50 +0800 | [diff] [blame] | 3634 | #endif /* CONFIG_DEBUG_SLAB || CONFIG_TRACING */ |
Christoph Hellwig | 8b98c16 | 2006-12-06 20:32:30 -0800 | [diff] [blame] | 3635 | #endif /* CONFIG_NUMA */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3636 | |
| 3637 | /** |
Paul Drynoff | 800590f | 2006-06-23 02:03:48 -0700 | [diff] [blame] | 3638 | * __do_kmalloc - allocate memory |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3639 | * @size: how many bytes of memory are required. |
Paul Drynoff | 800590f | 2006-06-23 02:03:48 -0700 | [diff] [blame] | 3640 | * @flags: the type of memory to allocate (see kmalloc). |
Randy Dunlap | 911851e | 2006-03-22 00:08:14 -0800 | [diff] [blame] | 3641 | * @caller: function caller for debug tracking of the caller |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3642 | */ |
Pekka Enberg | 7fd6b14 | 2006-02-01 03:05:52 -0800 | [diff] [blame] | 3643 | static __always_inline void *__do_kmalloc(size_t size, gfp_t flags, |
Ezequiel Garcia | 7c0cb9c | 2012-09-08 17:47:55 -0300 | [diff] [blame] | 3644 | unsigned long caller) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3645 | { |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 3646 | struct kmem_cache *cachep; |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 3647 | void *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3648 | |
Christoph Lameter | 2c59dd6 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3649 | cachep = kmalloc_slab(size, flags); |
Linus Torvalds | a5c96d8 | 2007-07-19 13:17:15 -0700 | [diff] [blame] | 3650 | if (unlikely(ZERO_OR_NULL_PTR(cachep))) |
| 3651 | return cachep; |
Ezequiel Garcia | 4835630 | 2012-09-08 17:47:57 -0300 | [diff] [blame] | 3652 | ret = slab_alloc(cachep, flags, caller); |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 3653 | |
Ezequiel Garcia | 7c0cb9c | 2012-09-08 17:47:55 -0300 | [diff] [blame] | 3654 | trace_kmalloc(caller, ret, |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 3655 | size, cachep->size, flags); |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 3656 | |
| 3657 | return ret; |
Pekka Enberg | 7fd6b14 | 2006-02-01 03:05:52 -0800 | [diff] [blame] | 3658 | } |
| 3659 | |
Pekka Enberg | 7fd6b14 | 2006-02-01 03:05:52 -0800 | [diff] [blame] | 3660 | |
Li Zefan | 0bb38a5 | 2009-12-11 15:45:50 +0800 | [diff] [blame] | 3661 | #if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING) |
Pekka Enberg | 7fd6b14 | 2006-02-01 03:05:52 -0800 | [diff] [blame] | 3662 | void *__kmalloc(size_t size, gfp_t flags) |
| 3663 | { |
Ezequiel Garcia | 7c0cb9c | 2012-09-08 17:47:55 -0300 | [diff] [blame] | 3664 | return __do_kmalloc(size, flags, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3665 | } |
| 3666 | EXPORT_SYMBOL(__kmalloc); |
| 3667 | |
Eduard - Gabriel Munteanu | ce71e27 | 2008-08-19 20:43:25 +0300 | [diff] [blame] | 3668 | void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller) |
Pekka Enberg | 7fd6b14 | 2006-02-01 03:05:52 -0800 | [diff] [blame] | 3669 | { |
Ezequiel Garcia | 7c0cb9c | 2012-09-08 17:47:55 -0300 | [diff] [blame] | 3670 | return __do_kmalloc(size, flags, caller); |
Pekka Enberg | 7fd6b14 | 2006-02-01 03:05:52 -0800 | [diff] [blame] | 3671 | } |
| 3672 | EXPORT_SYMBOL(__kmalloc_track_caller); |
Christoph Hellwig | 1d2c8ee | 2006-10-04 02:15:25 -0700 | [diff] [blame] | 3673 | |
| 3674 | #else |
| 3675 | void *__kmalloc(size_t size, gfp_t flags) |
| 3676 | { |
Ezequiel Garcia | 7c0cb9c | 2012-09-08 17:47:55 -0300 | [diff] [blame] | 3677 | return __do_kmalloc(size, flags, 0); |
Christoph Hellwig | 1d2c8ee | 2006-10-04 02:15:25 -0700 | [diff] [blame] | 3678 | } |
| 3679 | EXPORT_SYMBOL(__kmalloc); |
Pekka Enberg | 7fd6b14 | 2006-02-01 03:05:52 -0800 | [diff] [blame] | 3680 | #endif |
| 3681 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3682 | /** |
| 3683 | * kmem_cache_free - Deallocate an object |
| 3684 | * @cachep: The cache the allocation was from. |
| 3685 | * @objp: The previously allocated object. |
| 3686 | * |
| 3687 | * Free an object which was previously allocated from this |
| 3688 | * cache. |
| 3689 | */ |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 3690 | void kmem_cache_free(struct kmem_cache *cachep, void *objp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3691 | { |
| 3692 | unsigned long flags; |
Glauber Costa | b9ce5ef | 2012-12-18 14:22:46 -0800 | [diff] [blame] | 3693 | cachep = cache_from_obj(cachep, objp); |
| 3694 | if (!cachep) |
| 3695 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3696 | |
| 3697 | local_irq_save(flags); |
Feng Tang | d97d476 | 2012-07-02 14:29:10 +0800 | [diff] [blame] | 3698 | debug_check_no_locks_freed(objp, cachep->object_size); |
Thomas Gleixner | 3ac7fe5 | 2008-04-30 00:55:01 -0700 | [diff] [blame] | 3699 | if (!(cachep->flags & SLAB_DEBUG_OBJECTS)) |
Christoph Lameter | 8c138bc | 2012-06-13 10:24:58 -0500 | [diff] [blame] | 3700 | debug_check_no_obj_freed(objp, cachep->object_size); |
Ezequiel Garcia | 7c0cb9c | 2012-09-08 17:47:55 -0300 | [diff] [blame] | 3701 | __cache_free(cachep, objp, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3702 | local_irq_restore(flags); |
Eduard - Gabriel Munteanu | 3655575 | 2008-08-10 20:14:05 +0300 | [diff] [blame] | 3703 | |
Eduard - Gabriel Munteanu | ca2b84cb | 2009-03-23 15:12:24 +0200 | [diff] [blame] | 3704 | trace_kmem_cache_free(_RET_IP_, objp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3705 | } |
| 3706 | EXPORT_SYMBOL(kmem_cache_free); |
| 3707 | |
| 3708 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3709 | * kfree - free previously allocated memory |
| 3710 | * @objp: pointer returned by kmalloc. |
| 3711 | * |
Pekka Enberg | 80e93ef | 2005-09-09 13:10:16 -0700 | [diff] [blame] | 3712 | * If @objp is NULL, no operation is performed. |
| 3713 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3714 | * Don't free memory not originally allocated by kmalloc() |
| 3715 | * or you will run into trouble. |
| 3716 | */ |
| 3717 | void kfree(const void *objp) |
| 3718 | { |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 3719 | struct kmem_cache *c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3720 | unsigned long flags; |
| 3721 | |
Pekka Enberg | 2121db7 | 2009-03-25 11:05:57 +0200 | [diff] [blame] | 3722 | trace_kfree(_RET_IP_, objp); |
| 3723 | |
Christoph Lameter | 6cb8f91 | 2007-07-17 04:03:22 -0700 | [diff] [blame] | 3724 | if (unlikely(ZERO_OR_NULL_PTR(objp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3725 | return; |
| 3726 | local_irq_save(flags); |
| 3727 | kfree_debugcheck(objp); |
Pekka Enberg | 6ed5eb221 | 2006-02-01 03:05:49 -0800 | [diff] [blame] | 3728 | c = virt_to_cache(objp); |
Christoph Lameter | 8c138bc | 2012-06-13 10:24:58 -0500 | [diff] [blame] | 3729 | debug_check_no_locks_freed(objp, c->object_size); |
| 3730 | |
| 3731 | debug_check_no_obj_freed(objp, c->object_size); |
Ezequiel Garcia | 7c0cb9c | 2012-09-08 17:47:55 -0300 | [diff] [blame] | 3732 | __cache_free(c, (void *)objp, _RET_IP_); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3733 | local_irq_restore(flags); |
| 3734 | } |
| 3735 | EXPORT_SYMBOL(kfree); |
| 3736 | |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3737 | /* |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3738 | * This initializes kmem_cache_node or resizes various caches for all nodes. |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3739 | */ |
Jianyu Zhan | 5f0985b | 2014-03-30 17:02:20 +0800 | [diff] [blame] | 3740 | static int alloc_kmem_cache_node(struct kmem_cache *cachep, gfp_t gfp) |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3741 | { |
| 3742 | int node; |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3743 | struct kmem_cache_node *n; |
Christoph Lameter | cafeb02 | 2006-03-25 03:06:46 -0800 | [diff] [blame] | 3744 | struct array_cache *new_shared; |
Paul Menage | 3395ee0 | 2006-12-06 20:32:16 -0800 | [diff] [blame] | 3745 | struct array_cache **new_alien = NULL; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3746 | |
Mel Gorman | 9c09a95 | 2008-01-24 05:49:54 -0800 | [diff] [blame] | 3747 | for_each_online_node(node) { |
Christoph Lameter | cafeb02 | 2006-03-25 03:06:46 -0800 | [diff] [blame] | 3748 | |
Paul Menage | 3395ee0 | 2006-12-06 20:32:16 -0800 | [diff] [blame] | 3749 | if (use_alien_caches) { |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 3750 | new_alien = alloc_alien_cache(node, cachep->limit, gfp); |
Paul Menage | 3395ee0 | 2006-12-06 20:32:16 -0800 | [diff] [blame] | 3751 | if (!new_alien) |
| 3752 | goto fail; |
| 3753 | } |
Christoph Lameter | cafeb02 | 2006-03-25 03:06:46 -0800 | [diff] [blame] | 3754 | |
Eric Dumazet | 6310984 | 2007-05-06 14:49:28 -0700 | [diff] [blame] | 3755 | new_shared = NULL; |
| 3756 | if (cachep->shared) { |
| 3757 | new_shared = alloc_arraycache(node, |
Christoph Lameter | 0718dc2 | 2006-03-25 03:06:47 -0800 | [diff] [blame] | 3758 | cachep->shared*cachep->batchcount, |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 3759 | 0xbaadf00d, gfp); |
Eric Dumazet | 6310984 | 2007-05-06 14:49:28 -0700 | [diff] [blame] | 3760 | if (!new_shared) { |
| 3761 | free_alien_cache(new_alien); |
| 3762 | goto fail; |
| 3763 | } |
Christoph Lameter | 0718dc2 | 2006-03-25 03:06:47 -0800 | [diff] [blame] | 3764 | } |
Christoph Lameter | cafeb02 | 2006-03-25 03:06:46 -0800 | [diff] [blame] | 3765 | |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 3766 | n = get_node(cachep, node); |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3767 | if (n) { |
| 3768 | struct array_cache *shared = n->shared; |
Christoph Lameter | cafeb02 | 2006-03-25 03:06:46 -0800 | [diff] [blame] | 3769 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3770 | spin_lock_irq(&n->list_lock); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3771 | |
Christoph Lameter | cafeb02 | 2006-03-25 03:06:46 -0800 | [diff] [blame] | 3772 | if (shared) |
Christoph Lameter | 0718dc2 | 2006-03-25 03:06:47 -0800 | [diff] [blame] | 3773 | free_block(cachep, shared->entry, |
| 3774 | shared->avail, node); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3775 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3776 | n->shared = new_shared; |
| 3777 | if (!n->alien) { |
| 3778 | n->alien = new_alien; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3779 | new_alien = NULL; |
| 3780 | } |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3781 | n->free_limit = (1 + nr_cpus_node(node)) * |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 3782 | cachep->batchcount + cachep->num; |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3783 | spin_unlock_irq(&n->list_lock); |
Christoph Lameter | cafeb02 | 2006-03-25 03:06:46 -0800 | [diff] [blame] | 3784 | kfree(shared); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3785 | free_alien_cache(new_alien); |
| 3786 | continue; |
| 3787 | } |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3788 | n = kmalloc_node(sizeof(struct kmem_cache_node), gfp, node); |
| 3789 | if (!n) { |
Christoph Lameter | 0718dc2 | 2006-03-25 03:06:47 -0800 | [diff] [blame] | 3790 | free_alien_cache(new_alien); |
| 3791 | kfree(new_shared); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3792 | goto fail; |
Christoph Lameter | 0718dc2 | 2006-03-25 03:06:47 -0800 | [diff] [blame] | 3793 | } |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3794 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3795 | kmem_cache_node_init(n); |
Jianyu Zhan | 5f0985b | 2014-03-30 17:02:20 +0800 | [diff] [blame] | 3796 | n->next_reap = jiffies + REAPTIMEOUT_NODE + |
| 3797 | ((unsigned long)cachep) % REAPTIMEOUT_NODE; |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3798 | n->shared = new_shared; |
| 3799 | n->alien = new_alien; |
| 3800 | n->free_limit = (1 + nr_cpus_node(node)) * |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 3801 | cachep->batchcount + cachep->num; |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3802 | cachep->node[node] = n; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3803 | } |
Christoph Lameter | cafeb02 | 2006-03-25 03:06:46 -0800 | [diff] [blame] | 3804 | return 0; |
Christoph Lameter | 0718dc2 | 2006-03-25 03:06:47 -0800 | [diff] [blame] | 3805 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 3806 | fail: |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 3807 | if (!cachep->list.next) { |
Christoph Lameter | 0718dc2 | 2006-03-25 03:06:47 -0800 | [diff] [blame] | 3808 | /* Cache is not active yet. Roll back what we did */ |
| 3809 | node--; |
| 3810 | while (node >= 0) { |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 3811 | n = get_node(cachep, node); |
| 3812 | if (n) { |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3813 | kfree(n->shared); |
| 3814 | free_alien_cache(n->alien); |
| 3815 | kfree(n); |
Christoph Lameter | 6a67368 | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3816 | cachep->node[node] = NULL; |
Christoph Lameter | 0718dc2 | 2006-03-25 03:06:47 -0800 | [diff] [blame] | 3817 | } |
| 3818 | node--; |
| 3819 | } |
| 3820 | } |
Christoph Lameter | cafeb02 | 2006-03-25 03:06:46 -0800 | [diff] [blame] | 3821 | return -ENOMEM; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3822 | } |
| 3823 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3824 | struct ccupdate_struct { |
Pekka Enberg | 343e0d7 | 2006-02-01 03:05:50 -0800 | [diff] [blame] | 3825 | struct kmem_cache *cachep; |
Eric Dumazet | acfe7d7 | 2011-07-25 08:55:42 +0200 | [diff] [blame] | 3826 | struct array_cache *new[0]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3827 | }; |
| 3828 | |
| 3829 | static void do_ccupdate_local(void *info) |
| 3830 | { |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 3831 | struct ccupdate_struct *new = info; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3832 | struct array_cache *old; |
| 3833 | |
| 3834 | check_irq_off(); |
Pekka Enberg | 9a2dba4 | 2006-02-01 03:05:49 -0800 | [diff] [blame] | 3835 | old = cpu_cache_get(new->cachep); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3836 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3837 | new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()]; |
| 3838 | new->new[smp_processor_id()] = old; |
| 3839 | } |
| 3840 | |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 3841 | /* Always called with the slab_mutex held */ |
Glauber Costa | 943a451 | 2012-12-18 14:23:03 -0800 | [diff] [blame] | 3842 | static int __do_tune_cpucache(struct kmem_cache *cachep, int limit, |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 3843 | int batchcount, int shared, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3844 | { |
Siddha, Suresh B | d2e7b7d | 2006-09-25 23:31:47 -0700 | [diff] [blame] | 3845 | struct ccupdate_struct *new; |
Christoph Lameter | 2ed3a4e | 2006-09-25 23:31:38 -0700 | [diff] [blame] | 3846 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3847 | |
Eric Dumazet | acfe7d7 | 2011-07-25 08:55:42 +0200 | [diff] [blame] | 3848 | new = kzalloc(sizeof(*new) + nr_cpu_ids * sizeof(struct array_cache *), |
| 3849 | gfp); |
Siddha, Suresh B | d2e7b7d | 2006-09-25 23:31:47 -0700 | [diff] [blame] | 3850 | if (!new) |
| 3851 | return -ENOMEM; |
| 3852 | |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3853 | for_each_online_cpu(i) { |
Lee Schermerhorn | 7d6e6d0 | 2010-05-26 14:45:03 -0700 | [diff] [blame] | 3854 | new->new[i] = alloc_arraycache(cpu_to_mem(i), limit, |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 3855 | batchcount, gfp); |
Siddha, Suresh B | d2e7b7d | 2006-09-25 23:31:47 -0700 | [diff] [blame] | 3856 | if (!new->new[i]) { |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 3857 | for (i--; i >= 0; i--) |
Siddha, Suresh B | d2e7b7d | 2006-09-25 23:31:47 -0700 | [diff] [blame] | 3858 | kfree(new->new[i]); |
| 3859 | kfree(new); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3860 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3861 | } |
| 3862 | } |
Siddha, Suresh B | d2e7b7d | 2006-09-25 23:31:47 -0700 | [diff] [blame] | 3863 | new->cachep = cachep; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3864 | |
Jens Axboe | 15c8b6c | 2008-05-09 09:39:44 +0200 | [diff] [blame] | 3865 | on_each_cpu(do_ccupdate_local, (void *)new, 1); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3866 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3867 | check_irq_on(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3868 | cachep->batchcount = batchcount; |
| 3869 | cachep->limit = limit; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3870 | cachep->shared = shared; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3871 | |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 3872 | for_each_online_cpu(i) { |
Siddha, Suresh B | d2e7b7d | 2006-09-25 23:31:47 -0700 | [diff] [blame] | 3873 | struct array_cache *ccold = new->new[i]; |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 3874 | int node; |
| 3875 | struct kmem_cache_node *n; |
| 3876 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3877 | if (!ccold) |
| 3878 | continue; |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 3879 | |
| 3880 | node = cpu_to_mem(i); |
| 3881 | n = get_node(cachep, node); |
| 3882 | spin_lock_irq(&n->list_lock); |
| 3883 | free_block(cachep, ccold->entry, ccold->avail, node); |
| 3884 | spin_unlock_irq(&n->list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3885 | kfree(ccold); |
| 3886 | } |
Siddha, Suresh B | d2e7b7d | 2006-09-25 23:31:47 -0700 | [diff] [blame] | 3887 | kfree(new); |
Jianyu Zhan | 5f0985b | 2014-03-30 17:02:20 +0800 | [diff] [blame] | 3888 | return alloc_kmem_cache_node(cachep, gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3889 | } |
| 3890 | |
Glauber Costa | 943a451 | 2012-12-18 14:23:03 -0800 | [diff] [blame] | 3891 | static int do_tune_cpucache(struct kmem_cache *cachep, int limit, |
| 3892 | int batchcount, int shared, gfp_t gfp) |
| 3893 | { |
| 3894 | int ret; |
| 3895 | struct kmem_cache *c = NULL; |
| 3896 | int i = 0; |
| 3897 | |
| 3898 | ret = __do_tune_cpucache(cachep, limit, batchcount, shared, gfp); |
| 3899 | |
| 3900 | if (slab_state < FULL) |
| 3901 | return ret; |
| 3902 | |
| 3903 | if ((ret < 0) || !is_root_cache(cachep)) |
| 3904 | return ret; |
| 3905 | |
Glauber Costa | ebe945c | 2012-12-18 14:23:10 -0800 | [diff] [blame] | 3906 | VM_BUG_ON(!mutex_is_locked(&slab_mutex)); |
Glauber Costa | 943a451 | 2012-12-18 14:23:03 -0800 | [diff] [blame] | 3907 | for_each_memcg_cache_index(i) { |
Qiang Huang | 2ade4de | 2013-11-12 15:08:23 -0800 | [diff] [blame] | 3908 | c = cache_from_memcg_idx(cachep, i); |
Glauber Costa | 943a451 | 2012-12-18 14:23:03 -0800 | [diff] [blame] | 3909 | if (c) |
| 3910 | /* return value determined by the parent cache only */ |
| 3911 | __do_tune_cpucache(c, limit, batchcount, shared, gfp); |
| 3912 | } |
| 3913 | |
| 3914 | return ret; |
| 3915 | } |
| 3916 | |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 3917 | /* Called with slab_mutex held always */ |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 3918 | static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3919 | { |
| 3920 | int err; |
Glauber Costa | 943a451 | 2012-12-18 14:23:03 -0800 | [diff] [blame] | 3921 | int limit = 0; |
| 3922 | int shared = 0; |
| 3923 | int batchcount = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3924 | |
Glauber Costa | 943a451 | 2012-12-18 14:23:03 -0800 | [diff] [blame] | 3925 | if (!is_root_cache(cachep)) { |
| 3926 | struct kmem_cache *root = memcg_root_cache(cachep); |
| 3927 | limit = root->limit; |
| 3928 | shared = root->shared; |
| 3929 | batchcount = root->batchcount; |
| 3930 | } |
| 3931 | |
| 3932 | if (limit && shared && batchcount) |
| 3933 | goto skip_setup; |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 3934 | /* |
| 3935 | * The head array serves three purposes: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3936 | * - create a LIFO ordering, i.e. return objects that are cache-warm |
| 3937 | * - reduce the number of spinlock operations. |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 3938 | * - reduce the number of linked list operations on the slab and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3939 | * bufctl chains: array operations are cheaper. |
| 3940 | * The numbers are guessed, we should auto-tune as described by |
| 3941 | * Bonwick. |
| 3942 | */ |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 3943 | if (cachep->size > 131072) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3944 | limit = 1; |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 3945 | else if (cachep->size > PAGE_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3946 | limit = 8; |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 3947 | else if (cachep->size > 1024) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3948 | limit = 24; |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 3949 | else if (cachep->size > 256) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3950 | limit = 54; |
| 3951 | else |
| 3952 | limit = 120; |
| 3953 | |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 3954 | /* |
| 3955 | * CPU bound tasks (e.g. network routing) can exhibit cpu bound |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3956 | * allocation behaviour: Most allocs on one cpu, most free operations |
| 3957 | * on another cpu. For these cases, an efficient object passing between |
| 3958 | * cpus is necessary. This is provided by a shared array. The array |
| 3959 | * replaces Bonwick's magazine layer. |
| 3960 | * On uniprocessor, it's functionally equivalent (but less efficient) |
| 3961 | * to a larger limit. Thus disabled by default. |
| 3962 | */ |
| 3963 | shared = 0; |
Christoph Lameter | 3b0efdf | 2012-06-13 10:24:57 -0500 | [diff] [blame] | 3964 | if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3965 | shared = 8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3966 | |
| 3967 | #if DEBUG |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 3968 | /* |
| 3969 | * With debugging enabled, large batchcount lead to excessively long |
| 3970 | * periods with disabled local interrupts. Limit the batchcount |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3971 | */ |
| 3972 | if (limit > 32) |
| 3973 | limit = 32; |
| 3974 | #endif |
Glauber Costa | 943a451 | 2012-12-18 14:23:03 -0800 | [diff] [blame] | 3975 | batchcount = (limit + 1) / 2; |
| 3976 | skip_setup: |
| 3977 | err = do_tune_cpucache(cachep, limit, batchcount, shared, gfp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3978 | if (err) |
| 3979 | printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n", |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 3980 | cachep->name, -err); |
Christoph Lameter | 2ed3a4e | 2006-09-25 23:31:38 -0700 | [diff] [blame] | 3981 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3982 | } |
| 3983 | |
Christoph Lameter | 1b55253 | 2006-03-22 00:09:07 -0800 | [diff] [blame] | 3984 | /* |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3985 | * Drain an array if it contains any elements taking the node lock only if |
| 3986 | * necessary. Note that the node listlock also protects the array_cache |
Christoph Lameter | b18e7e6 | 2006-03-22 00:09:07 -0800 | [diff] [blame] | 3987 | * if drain_array() is used on the shared array. |
Christoph Lameter | 1b55253 | 2006-03-22 00:09:07 -0800 | [diff] [blame] | 3988 | */ |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3989 | static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n, |
Christoph Lameter | 1b55253 | 2006-03-22 00:09:07 -0800 | [diff] [blame] | 3990 | struct array_cache *ac, int force, int node) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3991 | { |
| 3992 | int tofree; |
| 3993 | |
Christoph Lameter | 1b55253 | 2006-03-22 00:09:07 -0800 | [diff] [blame] | 3994 | if (!ac || !ac->avail) |
| 3995 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3996 | if (ac->touched && !force) { |
| 3997 | ac->touched = 0; |
Christoph Lameter | b18e7e6 | 2006-03-22 00:09:07 -0800 | [diff] [blame] | 3998 | } else { |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 3999 | spin_lock_irq(&n->list_lock); |
Christoph Lameter | b18e7e6 | 2006-03-22 00:09:07 -0800 | [diff] [blame] | 4000 | if (ac->avail) { |
| 4001 | tofree = force ? ac->avail : (ac->limit + 4) / 5; |
| 4002 | if (tofree > ac->avail) |
| 4003 | tofree = (ac->avail + 1) / 2; |
| 4004 | free_block(cachep, ac->entry, tofree, node); |
| 4005 | ac->avail -= tofree; |
| 4006 | memmove(ac->entry, &(ac->entry[tofree]), |
| 4007 | sizeof(void *) * ac->avail); |
| 4008 | } |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 4009 | spin_unlock_irq(&n->list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4010 | } |
| 4011 | } |
| 4012 | |
| 4013 | /** |
| 4014 | * cache_reap - Reclaim memory from caches. |
Randy Dunlap | 05fb6bf | 2007-02-28 20:12:13 -0800 | [diff] [blame] | 4015 | * @w: work descriptor |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4016 | * |
| 4017 | * Called from workqueue/eventd every few seconds. |
| 4018 | * Purpose: |
| 4019 | * - clear the per-cpu caches for this CPU. |
| 4020 | * - return freeable pages to the main free memory pool. |
| 4021 | * |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 4022 | * If we cannot acquire the cache chain mutex then just give up - we'll try |
| 4023 | * again on the next iteration. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4024 | */ |
Christoph Lameter | 7c5cae3 | 2007-02-10 01:42:55 -0800 | [diff] [blame] | 4025 | static void cache_reap(struct work_struct *w) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4026 | { |
Christoph Hellwig | 7a7c381 | 2006-06-23 02:03:17 -0700 | [diff] [blame] | 4027 | struct kmem_cache *searchp; |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 4028 | struct kmem_cache_node *n; |
Lee Schermerhorn | 7d6e6d0 | 2010-05-26 14:45:03 -0700 | [diff] [blame] | 4029 | int node = numa_mem_id(); |
Jean Delvare | bf6aede | 2009-04-02 16:56:54 -0700 | [diff] [blame] | 4030 | struct delayed_work *work = to_delayed_work(w); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4031 | |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 4032 | if (!mutex_trylock(&slab_mutex)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4033 | /* Give up. Setup the next iteration. */ |
Christoph Lameter | 7c5cae3 | 2007-02-10 01:42:55 -0800 | [diff] [blame] | 4034 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4035 | |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 4036 | list_for_each_entry(searchp, &slab_caches, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4037 | check_irq_on(); |
| 4038 | |
Christoph Lameter | 35386e3 | 2006-03-22 00:09:05 -0800 | [diff] [blame] | 4039 | /* |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 4040 | * We only take the node lock if absolutely necessary and we |
Christoph Lameter | 35386e3 | 2006-03-22 00:09:05 -0800 | [diff] [blame] | 4041 | * have established with reasonable certainty that |
| 4042 | * we can do some work if the lock was obtained. |
| 4043 | */ |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 4044 | n = get_node(searchp, node); |
Christoph Lameter | 35386e3 | 2006-03-22 00:09:05 -0800 | [diff] [blame] | 4045 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 4046 | reap_alien(searchp, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4047 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 4048 | drain_array(searchp, n, cpu_cache_get(searchp), 0, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4049 | |
Christoph Lameter | 35386e3 | 2006-03-22 00:09:05 -0800 | [diff] [blame] | 4050 | /* |
| 4051 | * These are racy checks but it does not matter |
| 4052 | * if we skip one check or scan twice. |
| 4053 | */ |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 4054 | if (time_after(n->next_reap, jiffies)) |
Christoph Lameter | 35386e3 | 2006-03-22 00:09:05 -0800 | [diff] [blame] | 4055 | goto next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4056 | |
Jianyu Zhan | 5f0985b | 2014-03-30 17:02:20 +0800 | [diff] [blame] | 4057 | n->next_reap = jiffies + REAPTIMEOUT_NODE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4058 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 4059 | drain_array(searchp, n, n->shared, 0, node); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4060 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 4061 | if (n->free_touched) |
| 4062 | n->free_touched = 0; |
Christoph Lameter | ed11d9e | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 4063 | else { |
| 4064 | int freed; |
| 4065 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 4066 | freed = drain_freelist(searchp, n, (n->free_limit + |
Christoph Lameter | ed11d9e | 2006-06-30 01:55:45 -0700 | [diff] [blame] | 4067 | 5 * searchp->num - 1) / (5 * searchp->num)); |
| 4068 | STATS_ADD_REAPED(searchp, freed); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4069 | } |
Christoph Lameter | 35386e3 | 2006-03-22 00:09:05 -0800 | [diff] [blame] | 4070 | next: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4071 | cond_resched(); |
| 4072 | } |
| 4073 | check_irq_on(); |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 4074 | mutex_unlock(&slab_mutex); |
Christoph Lameter | 8fce4d8 | 2006-03-09 17:33:54 -0800 | [diff] [blame] | 4075 | next_reap_node(); |
Christoph Lameter | 7c5cae3 | 2007-02-10 01:42:55 -0800 | [diff] [blame] | 4076 | out: |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 4077 | /* Set up the next iteration */ |
Jianyu Zhan | 5f0985b | 2014-03-30 17:02:20 +0800 | [diff] [blame] | 4078 | schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_AC)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4079 | } |
| 4080 | |
Linus Torvalds | 158a962 | 2008-01-02 13:04:48 -0800 | [diff] [blame] | 4081 | #ifdef CONFIG_SLABINFO |
Glauber Costa | 0d7561c | 2012-10-19 18:20:27 +0400 | [diff] [blame] | 4082 | void get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4083 | { |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 4084 | struct page *page; |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 4085 | unsigned long active_objs; |
| 4086 | unsigned long num_objs; |
| 4087 | unsigned long active_slabs = 0; |
| 4088 | unsigned long num_slabs, free_objects = 0, shared_avail = 0; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 4089 | const char *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4090 | char *error = NULL; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 4091 | int node; |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 4092 | struct kmem_cache_node *n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4093 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4094 | active_objs = 0; |
| 4095 | num_slabs = 0; |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 4096 | for_each_kmem_cache_node(cachep, node, n) { |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 4097 | |
Ravikiran G Thirumalai | ca3b9b9 | 2006-02-04 23:27:58 -0800 | [diff] [blame] | 4098 | check_irq_on(); |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 4099 | spin_lock_irq(&n->list_lock); |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 4100 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 4101 | list_for_each_entry(page, &n->slabs_full, lru) { |
| 4102 | if (page->active != cachep->num && !error) |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 4103 | error = "slabs_full accounting error"; |
| 4104 | active_objs += cachep->num; |
| 4105 | active_slabs++; |
| 4106 | } |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 4107 | list_for_each_entry(page, &n->slabs_partial, lru) { |
| 4108 | if (page->active == cachep->num && !error) |
Joonsoo Kim | 106a74e | 2013-10-24 10:07:48 +0900 | [diff] [blame] | 4109 | error = "slabs_partial accounting error"; |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 4110 | if (!page->active && !error) |
Joonsoo Kim | 106a74e | 2013-10-24 10:07:48 +0900 | [diff] [blame] | 4111 | error = "slabs_partial accounting error"; |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 4112 | active_objs += page->active; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 4113 | active_slabs++; |
| 4114 | } |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 4115 | list_for_each_entry(page, &n->slabs_free, lru) { |
| 4116 | if (page->active && !error) |
Joonsoo Kim | 106a74e | 2013-10-24 10:07:48 +0900 | [diff] [blame] | 4117 | error = "slabs_free accounting error"; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 4118 | num_slabs++; |
| 4119 | } |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 4120 | free_objects += n->free_objects; |
| 4121 | if (n->shared) |
| 4122 | shared_avail += n->shared->avail; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 4123 | |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 4124 | spin_unlock_irq(&n->list_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4125 | } |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 4126 | num_slabs += active_slabs; |
| 4127 | num_objs = num_slabs * cachep->num; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 4128 | if (num_objs - active_objs != free_objects && !error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4129 | error = "free_objects accounting error"; |
| 4130 | |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 4131 | name = cachep->name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4132 | if (error) |
| 4133 | printk(KERN_ERR "slab: cache %s error: %s\n", name, error); |
| 4134 | |
Glauber Costa | 0d7561c | 2012-10-19 18:20:27 +0400 | [diff] [blame] | 4135 | sinfo->active_objs = active_objs; |
| 4136 | sinfo->num_objs = num_objs; |
| 4137 | sinfo->active_slabs = active_slabs; |
| 4138 | sinfo->num_slabs = num_slabs; |
| 4139 | sinfo->shared_avail = shared_avail; |
| 4140 | sinfo->limit = cachep->limit; |
| 4141 | sinfo->batchcount = cachep->batchcount; |
| 4142 | sinfo->shared = cachep->shared; |
| 4143 | sinfo->objects_per_slab = cachep->num; |
| 4144 | sinfo->cache_order = cachep->gfporder; |
| 4145 | } |
| 4146 | |
| 4147 | void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *cachep) |
| 4148 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4149 | #if STATS |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 4150 | { /* node stats */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4151 | unsigned long high = cachep->high_mark; |
| 4152 | unsigned long allocs = cachep->num_allocations; |
| 4153 | unsigned long grown = cachep->grown; |
| 4154 | unsigned long reaped = cachep->reaped; |
| 4155 | unsigned long errors = cachep->errors; |
| 4156 | unsigned long max_freeable = cachep->max_freeable; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4157 | unsigned long node_allocs = cachep->node_allocs; |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 4158 | unsigned long node_frees = cachep->node_frees; |
Ravikiran G Thirumalai | fb7faf3 | 2006-04-10 22:52:54 -0700 | [diff] [blame] | 4159 | unsigned long overflows = cachep->node_overflow; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4160 | |
Joe Perches | e92dd4f | 2010-03-26 19:27:58 -0700 | [diff] [blame] | 4161 | seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu " |
| 4162 | "%4lu %4lu %4lu %4lu %4lu", |
| 4163 | allocs, high, grown, |
| 4164 | reaped, errors, max_freeable, node_allocs, |
| 4165 | node_frees, overflows); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4166 | } |
| 4167 | /* cpu stats */ |
| 4168 | { |
| 4169 | unsigned long allochit = atomic_read(&cachep->allochit); |
| 4170 | unsigned long allocmiss = atomic_read(&cachep->allocmiss); |
| 4171 | unsigned long freehit = atomic_read(&cachep->freehit); |
| 4172 | unsigned long freemiss = atomic_read(&cachep->freemiss); |
| 4173 | |
| 4174 | seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu", |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 4175 | allochit, allocmiss, freehit, freemiss); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4176 | } |
| 4177 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4178 | } |
| 4179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4180 | #define MAX_SLABINFO_WRITE 128 |
| 4181 | /** |
| 4182 | * slabinfo_write - Tuning for the slab allocator |
| 4183 | * @file: unused |
| 4184 | * @buffer: user buffer |
| 4185 | * @count: data length |
| 4186 | * @ppos: unused |
| 4187 | */ |
Glauber Costa | b7454ad | 2012-10-19 18:20:25 +0400 | [diff] [blame] | 4188 | ssize_t slabinfo_write(struct file *file, const char __user *buffer, |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 4189 | size_t count, loff_t *ppos) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4190 | { |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 4191 | char kbuf[MAX_SLABINFO_WRITE + 1], *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4192 | int limit, batchcount, shared, res; |
Christoph Hellwig | 7a7c381 | 2006-06-23 02:03:17 -0700 | [diff] [blame] | 4193 | struct kmem_cache *cachep; |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 4194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4195 | if (count > MAX_SLABINFO_WRITE) |
| 4196 | return -EINVAL; |
| 4197 | if (copy_from_user(&kbuf, buffer, count)) |
| 4198 | return -EFAULT; |
Pekka Enberg | b28a02d | 2006-01-08 01:00:37 -0800 | [diff] [blame] | 4199 | kbuf[MAX_SLABINFO_WRITE] = '\0'; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4200 | |
| 4201 | tmp = strchr(kbuf, ' '); |
| 4202 | if (!tmp) |
| 4203 | return -EINVAL; |
| 4204 | *tmp = '\0'; |
| 4205 | tmp++; |
| 4206 | if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3) |
| 4207 | return -EINVAL; |
| 4208 | |
| 4209 | /* Find the cache in the chain of caches. */ |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 4210 | mutex_lock(&slab_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4211 | res = -EINVAL; |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 4212 | list_for_each_entry(cachep, &slab_caches, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | if (!strcmp(cachep->name, kbuf)) { |
Andrew Morton | a737b3e | 2006-03-22 00:08:11 -0800 | [diff] [blame] | 4214 | if (limit < 1 || batchcount < 1 || |
| 4215 | batchcount > limit || shared < 0) { |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 4216 | res = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4217 | } else { |
Christoph Lameter | e498be7 | 2005-09-09 13:03:32 -0700 | [diff] [blame] | 4218 | res = do_tune_cpucache(cachep, limit, |
Pekka Enberg | 83b519e | 2009-06-10 19:40:04 +0300 | [diff] [blame] | 4219 | batchcount, shared, |
| 4220 | GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4221 | } |
| 4222 | break; |
| 4223 | } |
| 4224 | } |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 4225 | mutex_unlock(&slab_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4226 | if (res >= 0) |
| 4227 | res = count; |
| 4228 | return res; |
| 4229 | } |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4230 | |
| 4231 | #ifdef CONFIG_DEBUG_SLAB_LEAK |
| 4232 | |
| 4233 | static void *leaks_start(struct seq_file *m, loff_t *pos) |
| 4234 | { |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 4235 | mutex_lock(&slab_mutex); |
| 4236 | return seq_list_start(&slab_caches, *pos); |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4237 | } |
| 4238 | |
| 4239 | static inline int add_caller(unsigned long *n, unsigned long v) |
| 4240 | { |
| 4241 | unsigned long *p; |
| 4242 | int l; |
| 4243 | if (!v) |
| 4244 | return 1; |
| 4245 | l = n[1]; |
| 4246 | p = n + 2; |
| 4247 | while (l) { |
| 4248 | int i = l/2; |
| 4249 | unsigned long *q = p + 2 * i; |
| 4250 | if (*q == v) { |
| 4251 | q[1]++; |
| 4252 | return 1; |
| 4253 | } |
| 4254 | if (*q > v) { |
| 4255 | l = i; |
| 4256 | } else { |
| 4257 | p = q + 2; |
| 4258 | l -= i + 1; |
| 4259 | } |
| 4260 | } |
| 4261 | if (++n[1] == n[0]) |
| 4262 | return 0; |
| 4263 | memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n)); |
| 4264 | p[0] = v; |
| 4265 | p[1] = 1; |
| 4266 | return 1; |
| 4267 | } |
| 4268 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 4269 | static void handle_slab(unsigned long *n, struct kmem_cache *c, |
| 4270 | struct page *page) |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4271 | { |
| 4272 | void *p; |
Joonsoo Kim | 0378730 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 4273 | int i; |
Joonsoo Kim | b1cb098 | 2013-10-24 10:07:45 +0900 | [diff] [blame] | 4274 | |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4275 | if (n[0] == n[1]) |
| 4276 | return; |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 4277 | for (i = 0, p = page->s_mem; i < c->num; i++, p += c->size) { |
Joonsoo Kim | 0378730 | 2014-06-23 13:22:06 -0700 | [diff] [blame] | 4278 | if (get_obj_status(page, i) != OBJECT_ACTIVE) |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4279 | continue; |
Joonsoo Kim | b1cb098 | 2013-10-24 10:07:45 +0900 | [diff] [blame] | 4280 | |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4281 | if (!add_caller(n, (unsigned long)*dbg_userword(c, p))) |
| 4282 | return; |
| 4283 | } |
| 4284 | } |
| 4285 | |
| 4286 | static void show_symbol(struct seq_file *m, unsigned long address) |
| 4287 | { |
| 4288 | #ifdef CONFIG_KALLSYMS |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4289 | unsigned long offset, size; |
Tejun Heo | 9281ace | 2007-07-17 04:03:51 -0700 | [diff] [blame] | 4290 | char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN]; |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4291 | |
Alexey Dobriyan | a5c43da | 2007-05-08 00:28:47 -0700 | [diff] [blame] | 4292 | if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) { |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4293 | seq_printf(m, "%s+%#lx/%#lx", name, offset, size); |
Alexey Dobriyan | a5c43da | 2007-05-08 00:28:47 -0700 | [diff] [blame] | 4294 | if (modname[0]) |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4295 | seq_printf(m, " [%s]", modname); |
| 4296 | return; |
| 4297 | } |
| 4298 | #endif |
| 4299 | seq_printf(m, "%p", (void *)address); |
| 4300 | } |
| 4301 | |
| 4302 | static int leaks_show(struct seq_file *m, void *p) |
| 4303 | { |
Thierry Reding | 0672aa7 | 2012-06-22 19:42:49 +0200 | [diff] [blame] | 4304 | struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list); |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 4305 | struct page *page; |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 4306 | struct kmem_cache_node *n; |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4307 | const char *name; |
Christoph Lameter | db84506 | 2013-02-05 18:45:23 +0000 | [diff] [blame] | 4308 | unsigned long *x = m->private; |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4309 | int node; |
| 4310 | int i; |
| 4311 | |
| 4312 | if (!(cachep->flags & SLAB_STORE_USER)) |
| 4313 | return 0; |
| 4314 | if (!(cachep->flags & SLAB_RED_ZONE)) |
| 4315 | return 0; |
| 4316 | |
| 4317 | /* OK, we can do it */ |
| 4318 | |
Christoph Lameter | db84506 | 2013-02-05 18:45:23 +0000 | [diff] [blame] | 4319 | x[1] = 0; |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4320 | |
Christoph Lameter | 18bf854 | 2014-08-06 16:04:11 -0700 | [diff] [blame] | 4321 | for_each_kmem_cache_node(cachep, node, n) { |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4322 | |
| 4323 | check_irq_on(); |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 4324 | spin_lock_irq(&n->list_lock); |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4325 | |
Joonsoo Kim | 8456a64 | 2013-10-24 10:07:49 +0900 | [diff] [blame] | 4326 | list_for_each_entry(page, &n->slabs_full, lru) |
| 4327 | handle_slab(x, cachep, page); |
| 4328 | list_for_each_entry(page, &n->slabs_partial, lru) |
| 4329 | handle_slab(x, cachep, page); |
Christoph Lameter | ce8eb6c | 2013-01-10 19:14:19 +0000 | [diff] [blame] | 4330 | spin_unlock_irq(&n->list_lock); |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4331 | } |
| 4332 | name = cachep->name; |
Christoph Lameter | db84506 | 2013-02-05 18:45:23 +0000 | [diff] [blame] | 4333 | if (x[0] == x[1]) { |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4334 | /* Increase the buffer size */ |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 4335 | mutex_unlock(&slab_mutex); |
Christoph Lameter | db84506 | 2013-02-05 18:45:23 +0000 | [diff] [blame] | 4336 | m->private = kzalloc(x[0] * 4 * sizeof(unsigned long), GFP_KERNEL); |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4337 | if (!m->private) { |
| 4338 | /* Too bad, we are really out */ |
Christoph Lameter | db84506 | 2013-02-05 18:45:23 +0000 | [diff] [blame] | 4339 | m->private = x; |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 4340 | mutex_lock(&slab_mutex); |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4341 | return -ENOMEM; |
| 4342 | } |
Christoph Lameter | db84506 | 2013-02-05 18:45:23 +0000 | [diff] [blame] | 4343 | *(unsigned long *)m->private = x[0] * 2; |
| 4344 | kfree(x); |
Christoph Lameter | 18004c5 | 2012-07-06 15:25:12 -0500 | [diff] [blame] | 4345 | mutex_lock(&slab_mutex); |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4346 | /* Now make sure this entry will be retried */ |
| 4347 | m->count = m->size; |
| 4348 | return 0; |
| 4349 | } |
Christoph Lameter | db84506 | 2013-02-05 18:45:23 +0000 | [diff] [blame] | 4350 | for (i = 0; i < x[1]; i++) { |
| 4351 | seq_printf(m, "%s: %lu ", name, x[2*i+3]); |
| 4352 | show_symbol(m, x[2*i+2]); |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4353 | seq_putc(m, '\n'); |
| 4354 | } |
Siddha, Suresh B | d2e7b7d | 2006-09-25 23:31:47 -0700 | [diff] [blame] | 4355 | |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4356 | return 0; |
| 4357 | } |
| 4358 | |
Alexey Dobriyan | a0ec95a | 2008-10-06 00:59:10 +0400 | [diff] [blame] | 4359 | static const struct seq_operations slabstats_op = { |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4360 | .start = leaks_start, |
Wanpeng Li | 276a243 | 2013-07-08 08:08:28 +0800 | [diff] [blame] | 4361 | .next = slab_next, |
| 4362 | .stop = slab_stop, |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4363 | .show = leaks_show, |
| 4364 | }; |
Alexey Dobriyan | a0ec95a | 2008-10-06 00:59:10 +0400 | [diff] [blame] | 4365 | |
| 4366 | static int slabstats_open(struct inode *inode, struct file *file) |
| 4367 | { |
| 4368 | unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL); |
| 4369 | int ret = -ENOMEM; |
| 4370 | if (n) { |
| 4371 | ret = seq_open(file, &slabstats_op); |
| 4372 | if (!ret) { |
| 4373 | struct seq_file *m = file->private_data; |
| 4374 | *n = PAGE_SIZE / (2 * sizeof(unsigned long)); |
| 4375 | m->private = n; |
| 4376 | n = NULL; |
| 4377 | } |
| 4378 | kfree(n); |
| 4379 | } |
| 4380 | return ret; |
| 4381 | } |
| 4382 | |
| 4383 | static const struct file_operations proc_slabstats_operations = { |
| 4384 | .open = slabstats_open, |
| 4385 | .read = seq_read, |
| 4386 | .llseek = seq_lseek, |
| 4387 | .release = seq_release_private, |
| 4388 | }; |
Al Viro | 871751e | 2006-03-25 03:06:39 -0800 | [diff] [blame] | 4389 | #endif |
Alexey Dobriyan | a0ec95a | 2008-10-06 00:59:10 +0400 | [diff] [blame] | 4390 | |
| 4391 | static int __init slab_proc_init(void) |
| 4392 | { |
| 4393 | #ifdef CONFIG_DEBUG_SLAB_LEAK |
| 4394 | proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations); |
| 4395 | #endif |
| 4396 | return 0; |
| 4397 | } |
| 4398 | module_init(slab_proc_init); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4399 | #endif |
| 4400 | |
Manfred Spraul | 00e145b | 2005-09-03 15:55:07 -0700 | [diff] [blame] | 4401 | /** |
| 4402 | * ksize - get the actual amount of memory allocated for a given object |
| 4403 | * @objp: Pointer to the object |
| 4404 | * |
| 4405 | * kmalloc may internally round up allocations and return more memory |
| 4406 | * than requested. ksize() can be used to determine the actual amount of |
| 4407 | * memory allocated. The caller may use this additional memory, even though |
| 4408 | * a smaller amount of memory was initially specified with the kmalloc call. |
| 4409 | * The caller must guarantee that objp points to a valid object previously |
| 4410 | * allocated with either kmalloc() or kmem_cache_alloc(). The object |
| 4411 | * must not be freed during the duration of the call. |
| 4412 | */ |
Pekka Enberg | fd76bab | 2007-05-06 14:48:40 -0700 | [diff] [blame] | 4413 | size_t ksize(const void *objp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4414 | { |
Christoph Lameter | ef8b452 | 2007-10-16 01:24:46 -0700 | [diff] [blame] | 4415 | BUG_ON(!objp); |
| 4416 | if (unlikely(objp == ZERO_SIZE_PTR)) |
Manfred Spraul | 00e145b | 2005-09-03 15:55:07 -0700 | [diff] [blame] | 4417 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4418 | |
Christoph Lameter | 8c138bc | 2012-06-13 10:24:58 -0500 | [diff] [blame] | 4419 | return virt_to_cache(objp)->object_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4420 | } |
Kirill A. Shutemov | b1aabec | 2009-02-10 15:21:44 +0200 | [diff] [blame] | 4421 | EXPORT_SYMBOL(ksize); |