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