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