blob: 75c60821e38225d477d7ff1bfa7ac8ca261191e1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Arlott183ff222007-10-20 01:27:18 +020029 * slabs and you must pass objects with the same initializations to
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * 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 Mortona737b3e2006-03-22 00:08:11 -080053 * The c_cpuarray may not be read with enabled local interrupts -
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * it's changed with a smp_call_function().
55 *
56 * SMP synchronization:
57 * constructors and destructors are called without any locking.
Pekka Enberg343e0d72006-02-01 03:05:50 -080058 * Several members in struct kmem_cache and struct slab never change, they
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * 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 Lameter18004c52012-07-06 15:25:12 -050071 * The global cache-chain is protected by the mutex 'slab_mutex'.
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * 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 Lametere498be72005-09-09 13:03:32 -070078 * 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 Torvalds1da177e2005-04-16 15:20:36 -070087 */
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <linux/slab.h>
90#include <linux/mm.h>
Randy Dunlapc9cf5522006-06-27 02:53:52 -070091#include <linux/poison.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#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 Jackson101a5002006-03-24 03:16:07 -080097#include <linux/cpuset.h>
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +040098#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#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 Marques543537b2005-06-23 00:09:02 -0700106#include <linux/string.h>
Andrew Morton138ae662006-12-06 20:36:41 -0800107#include <linux/uaccess.h>
Christoph Lametere498be72005-09-09 13:03:32 -0700108#include <linux/nodemask.h>
Catalin Marinasd5cff632009-06-11 13:22:40 +0100109#include <linux/kmemleak.h>
Christoph Lameterdc85da12006-01-18 17:42:36 -0800110#include <linux/mempolicy.h>
Ingo Molnarfc0abb12006-01-18 17:42:33 -0800111#include <linux/mutex.h>
Akinobu Mita8a8b6502006-12-08 02:39:44 -0800112#include <linux/fault-inject.h>
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700113#include <linux/rtmutex.h>
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800114#include <linux/reciprocal_div.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700115#include <linux/debugobjects.h>
Pekka Enbergc175eea2008-05-09 20:35:53 +0200116#include <linux/kmemcheck.h>
David Rientjes8f9f8d92010-03-27 19:40:47 -0700117#include <linux/memory.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -0700118#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Mel Gorman381760e2012-07-31 16:44:30 -0700120#include <net/sock.h>
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#include <asm/cacheflush.h>
123#include <asm/tlbflush.h>
124#include <asm/page.h>
125
Steven Rostedt4dee6b62012-01-09 17:15:42 -0500126#include <trace/events/kmem.h>
127
Mel Gorman072bb0a2012-07-31 16:43:58 -0700128#include "internal.h"
129
Glauber Costab9ce5ef2012-12-18 14:22:46 -0800130#include "slab.h"
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/*
Christoph Lameter50953fe2007-05-06 14:50:16 -0700133 * DEBUG - 1 for kmem_cache_create() to honour; SLAB_RED_ZONE & SLAB_POISON.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * 0 for faster, smaller code (especially in the critical paths).
135 *
136 * STATS - 1 to collect stats for /proc/slabinfo.
137 * 0 for faster, smaller code (especially in the critical paths).
138 *
139 * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
140 */
141
142#ifdef CONFIG_DEBUG_SLAB
143#define DEBUG 1
144#define STATS 1
145#define FORCED_DEBUG 1
146#else
147#define DEBUG 0
148#define STATS 0
149#define FORCED_DEBUG 0
150#endif
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Shouldn't this be in a header file somewhere? */
153#define BYTES_PER_WORD sizeof(void *)
David Woodhouse87a927c2007-07-04 21:26:44 -0400154#define REDZONE_ALIGN max(BYTES_PER_WORD, __alignof__(unsigned long long))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#ifndef ARCH_KMALLOC_FLAGS
157#define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
158#endif
159
Mel Gorman072bb0a2012-07-31 16:43:58 -0700160/*
161 * true if a page was allocated from pfmemalloc reserves for network-based
162 * swap
163 */
164static bool pfmemalloc_active __read_mostly;
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/*
167 * kmem_bufctl_t:
168 *
169 * Bufctl's are used for linking objs within a slab
170 * linked offsets.
171 *
172 * This implementation relies on "struct page" for locating the cache &
173 * slab an object belongs to.
174 * This allows the bufctl structure to be small (one int), but limits
175 * the number of objects a slab (not a cache) can contain when off-slab
176 * bufctls are used. The limit is the size of the largest general cache
177 * that does not use off-slab slabs.
178 * For 32bit archs with 4 kB pages, is this 56.
179 * This is not serious, as it is only for large objects, when it is unwise
180 * to have too many per slab.
181 * Note: This limit can be raised by introducing a general cache whose size
182 * is less than 512 (PAGE_SIZE<<3), but greater than 256.
183 */
184
Kyle Moffettfa5b08d2005-09-03 15:55:03 -0700185typedef unsigned int kmem_bufctl_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#define BUFCTL_END (((kmem_bufctl_t)(~0U))-0)
187#define BUFCTL_FREE (((kmem_bufctl_t)(~0U))-1)
Al Viro871751e2006-03-25 03:06:39 -0800188#define BUFCTL_ACTIVE (((kmem_bufctl_t)(~0U))-2)
189#define SLAB_LIMIT (((kmem_bufctl_t)(~0U))-3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/*
Lai Jiangshan5bfe53a2011-03-10 15:22:24 +0800192 * struct slab
193 *
194 * Manages the objs in a slab. Placed either at the beginning of mem allocated
195 * for a slab, or allocated from an general cache.
196 * Slabs are chained into three list: fully used, partial, fully free slabs.
197 */
198struct slab {
Joonsoo Kim68126702013-10-24 10:07:42 +0900199 struct {
200 struct list_head list;
201 void *s_mem; /* including colour offset */
202 unsigned int inuse; /* num of objs active in slab */
203 kmem_bufctl_t free;
Lai Jiangshan5bfe53a2011-03-10 15:22:24 +0800204 };
205};
206
207/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 * struct array_cache
209 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 * Purpose:
211 * - LIFO ordering, to hand out cache-warm objects from _alloc
212 * - reduce the number of linked list operations
213 * - reduce spinlock operations
214 *
215 * The limit is stored in the per-cpu structure to reduce the data cache
216 * footprint.
217 *
218 */
219struct array_cache {
220 unsigned int avail;
221 unsigned int limit;
222 unsigned int batchcount;
223 unsigned int touched;
Christoph Lametere498be72005-09-09 13:03:32 -0700224 spinlock_t lock;
Robert P. J. Daybda5b652007-10-16 23:30:05 -0700225 void *entry[]; /*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800226 * Must have this definition in here for the proper
227 * alignment of array_cache. Also simplifies accessing
228 * the entries.
Mel Gorman072bb0a2012-07-31 16:43:58 -0700229 *
230 * Entries should not be directly dereferenced as
231 * entries belonging to slabs marked pfmemalloc will
232 * have the lower bits set SLAB_OBJ_PFMEMALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -0800233 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234};
235
Mel Gorman072bb0a2012-07-31 16:43:58 -0700236#define SLAB_OBJ_PFMEMALLOC 1
237static inline bool is_obj_pfmemalloc(void *objp)
238{
239 return (unsigned long)objp & SLAB_OBJ_PFMEMALLOC;
240}
241
242static inline void set_obj_pfmemalloc(void **objp)
243{
244 *objp = (void *)((unsigned long)*objp | SLAB_OBJ_PFMEMALLOC);
245 return;
246}
247
248static inline void clear_obj_pfmemalloc(void **objp)
249{
250 *objp = (void *)((unsigned long)*objp & ~SLAB_OBJ_PFMEMALLOC);
251}
252
Andrew Mortona737b3e2006-03-22 00:08:11 -0800253/*
254 * bootstrap: The caches do not work without cpuarrays anymore, but the
255 * cpuarrays are allocated from the generic caches...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 */
257#define BOOT_CPUCACHE_ENTRIES 1
258struct arraycache_init {
259 struct array_cache cache;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800260 void *entries[BOOT_CPUCACHE_ENTRIES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261};
262
263/*
Christoph Lametere498be72005-09-09 13:03:32 -0700264 * Need this for bootstrapping a per node allocator.
265 */
Pekka Enberg556a1692008-01-25 08:20:51 +0200266#define NUM_INIT_LISTS (3 * MAX_NUMNODES)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000267static struct kmem_cache_node __initdata init_kmem_cache_node[NUM_INIT_LISTS];
Christoph Lametere498be72005-09-09 13:03:32 -0700268#define CACHE_CACHE 0
Pekka Enberg556a1692008-01-25 08:20:51 +0200269#define SIZE_AC MAX_NUMNODES
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000270#define SIZE_NODE (2 * MAX_NUMNODES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Christoph Lametered11d9e2006-06-30 01:55:45 -0700272static int drain_freelist(struct kmem_cache *cache,
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000273 struct kmem_cache_node *n, int tofree);
Christoph Lametered11d9e2006-06-30 01:55:45 -0700274static void free_block(struct kmem_cache *cachep, void **objpp, int len,
275 int node);
Pekka Enberg83b519e2009-06-10 19:40:04 +0300276static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp);
David Howells65f27f32006-11-22 14:55:48 +0000277static void cache_reap(struct work_struct *unused);
Christoph Lametered11d9e2006-06-30 01:55:45 -0700278
Ingo Molnare0a42722006-06-23 02:03:46 -0700279static int slab_early_init = 1;
280
Christoph Lametere3366012013-01-10 19:14:18 +0000281#define INDEX_AC kmalloc_index(sizeof(struct arraycache_init))
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000282#define INDEX_NODE kmalloc_index(sizeof(struct kmem_cache_node))
Christoph Lametere498be72005-09-09 13:03:32 -0700283
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000284static void kmem_cache_node_init(struct kmem_cache_node *parent)
Christoph Lametere498be72005-09-09 13:03:32 -0700285{
286 INIT_LIST_HEAD(&parent->slabs_full);
287 INIT_LIST_HEAD(&parent->slabs_partial);
288 INIT_LIST_HEAD(&parent->slabs_free);
289 parent->shared = NULL;
290 parent->alien = NULL;
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -0800291 parent->colour_next = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700292 spin_lock_init(&parent->list_lock);
293 parent->free_objects = 0;
294 parent->free_touched = 0;
295}
296
Andrew Mortona737b3e2006-03-22 00:08:11 -0800297#define MAKE_LIST(cachep, listp, slab, nodeid) \
298 do { \
299 INIT_LIST_HEAD(listp); \
Christoph Lameter6a673682013-01-10 19:14:19 +0000300 list_splice(&(cachep->node[nodeid]->slab), listp); \
Christoph Lametere498be72005-09-09 13:03:32 -0700301 } while (0)
302
Andrew Mortona737b3e2006-03-22 00:08:11 -0800303#define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
304 do { \
Christoph Lametere498be72005-09-09 13:03:32 -0700305 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
306 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
307 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
308 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310#define CFLGS_OFF_SLAB (0x80000000UL)
311#define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
312
313#define BATCHREFILL_LIMIT 16
Andrew Mortona737b3e2006-03-22 00:08:11 -0800314/*
315 * Optimization question: fewer reaps means less probability for unnessary
316 * cpucache drain/refill cycles.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 *
Adrian Bunkdc6f3f22005-11-08 16:44:08 +0100318 * OTOH the cpuarrays can contain lots of objects,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * which could lock up otherwise freeable slabs.
320 */
321#define REAPTIMEOUT_CPUC (2*HZ)
322#define REAPTIMEOUT_LIST3 (4*HZ)
323
324#if STATS
325#define STATS_INC_ACTIVE(x) ((x)->num_active++)
326#define STATS_DEC_ACTIVE(x) ((x)->num_active--)
327#define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
328#define STATS_INC_GROWN(x) ((x)->grown++)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700329#define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
Andrew Mortona737b3e2006-03-22 00:08:11 -0800330#define STATS_SET_HIGH(x) \
331 do { \
332 if ((x)->num_active > (x)->high_mark) \
333 (x)->high_mark = (x)->num_active; \
334 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335#define STATS_INC_ERR(x) ((x)->errors++)
336#define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
Christoph Lametere498be72005-09-09 13:03:32 -0700337#define STATS_INC_NODEFREES(x) ((x)->node_frees++)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700338#define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800339#define STATS_SET_FREEABLE(x, i) \
340 do { \
341 if ((x)->max_freeable < i) \
342 (x)->max_freeable = i; \
343 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344#define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
345#define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
346#define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
347#define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
348#else
349#define STATS_INC_ACTIVE(x) do { } while (0)
350#define STATS_DEC_ACTIVE(x) do { } while (0)
351#define STATS_INC_ALLOCED(x) do { } while (0)
352#define STATS_INC_GROWN(x) do { } while (0)
Andi Kleen4e60c862010-08-09 17:19:03 -0700353#define STATS_ADD_REAPED(x,y) do { (void)(y); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354#define STATS_SET_HIGH(x) do { } while (0)
355#define STATS_INC_ERR(x) do { } while (0)
356#define STATS_INC_NODEALLOCS(x) do { } while (0)
Christoph Lametere498be72005-09-09 13:03:32 -0700357#define STATS_INC_NODEFREES(x) do { } while (0)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700358#define STATS_INC_ACOVERFLOW(x) do { } while (0)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800359#define STATS_SET_FREEABLE(x, i) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360#define STATS_INC_ALLOCHIT(x) do { } while (0)
361#define STATS_INC_ALLOCMISS(x) do { } while (0)
362#define STATS_INC_FREEHIT(x) do { } while (0)
363#define STATS_INC_FREEMISS(x) do { } while (0)
364#endif
365
366#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Andrew Mortona737b3e2006-03-22 00:08:11 -0800368/*
369 * memory layout of objects:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 * 0 : objp
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800371 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 * the end of an object is aligned with the end of the real
373 * allocation. Catches writes behind the end of the allocation.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800374 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 * redzone word.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800376 * cachep->obj_offset: The real object.
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500377 * cachep->size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
378 * cachep->size - 1* BYTES_PER_WORD: last caller address
Andrew Mortona737b3e2006-03-22 00:08:11 -0800379 * [BYTES_PER_WORD long]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800381static int obj_offset(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800383 return cachep->obj_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
385
David Woodhouseb46b8f12007-05-08 00:22:59 -0700386static unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
388 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
David Woodhouseb46b8f12007-05-08 00:22:59 -0700389 return (unsigned long long*) (objp + obj_offset(cachep) -
390 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
David Woodhouseb46b8f12007-05-08 00:22:59 -0700393static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
396 if (cachep->flags & SLAB_STORE_USER)
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500397 return (unsigned long long *)(objp + cachep->size -
David Woodhouseb46b8f12007-05-08 00:22:59 -0700398 sizeof(unsigned long long) -
David Woodhouse87a927c2007-07-04 21:26:44 -0400399 REDZONE_ALIGN);
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500400 return (unsigned long long *) (objp + cachep->size -
David Woodhouseb46b8f12007-05-08 00:22:59 -0700401 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
Pekka Enberg343e0d72006-02-01 03:05:50 -0800404static void **dbg_userword(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405{
406 BUG_ON(!(cachep->flags & SLAB_STORE_USER));
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500407 return (void **)(objp + cachep->size - BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
409
410#else
411
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800412#define obj_offset(x) 0
David Woodhouseb46b8f12007-05-08 00:22:59 -0700413#define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
414#define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415#define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
416
417#endif
418
419/*
David Rientjes3df1ccc2011-10-18 22:09:28 -0700420 * Do not go above this order unless 0 objects fit into the slab or
421 * overridden on the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 */
David Rientjes543585c2011-10-18 22:09:24 -0700423#define SLAB_MAX_ORDER_HI 1
424#define SLAB_MAX_ORDER_LO 0
425static int slab_max_order = SLAB_MAX_ORDER_LO;
David Rientjes3df1ccc2011-10-18 22:09:28 -0700426static bool slab_max_order_set __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800428static inline struct kmem_cache *virt_to_cache(const void *obj)
429{
Christoph Lameterb49af682007-05-06 14:49:41 -0700430 struct page *page = virt_to_head_page(obj);
Christoph Lameter35026082012-06-13 10:24:56 -0500431 return page->slab_cache;
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800432}
433
434static inline struct slab *virt_to_slab(const void *obj)
435{
Christoph Lameterb49af682007-05-06 14:49:41 -0700436 struct page *page = virt_to_head_page(obj);
Christoph Lameter35026082012-06-13 10:24:56 -0500437
438 VM_BUG_ON(!PageSlab(page));
439 return page->slab_page;
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800440}
441
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800442static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
443 unsigned int idx)
444{
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500445 return slab->s_mem + cache->size * idx;
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800446}
447
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800448/*
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500449 * We want to avoid an expensive divide : (offset / cache->size)
450 * Using the fact that size is a constant for a particular cache,
451 * we can replace (offset / cache->size) by
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800452 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
453 */
454static inline unsigned int obj_to_index(const struct kmem_cache *cache,
455 const struct slab *slab, void *obj)
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800456{
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800457 u32 offset = (obj - slab->s_mem);
458 return reciprocal_divide(offset, cache->reciprocal_buffer_size);
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461static struct arraycache_init initarray_generic =
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800462 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464/* internal cache of cache description objs */
Christoph Lameter9b030cb2012-09-05 00:20:33 +0000465static struct kmem_cache kmem_cache_boot = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800466 .batchcount = 1,
467 .limit = BOOT_CPUCACHE_ENTRIES,
468 .shared = 1,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500469 .size = sizeof(struct kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800470 .name = "kmem_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471};
472
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700473#define BAD_ALIEN_MAGIC 0x01020304ul
474
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200475#ifdef CONFIG_LOCKDEP
476
477/*
478 * Slab sometimes uses the kmalloc slabs to store the slab headers
479 * for other slabs "off slab".
480 * The locking for this is tricky in that it nests within the locks
481 * of all other slabs in a few places; to deal with this special
482 * locking we put on-slab caches into a separate lock-class.
483 *
484 * We set lock class for alien array caches which are up during init.
485 * The lock annotation will be lost if all cpus of a node goes down and
486 * then comes back up during hotplug
487 */
488static struct lock_class_key on_slab_l3_key;
489static struct lock_class_key on_slab_alc_key;
490
Peter Zijlstra83835b32011-07-22 15:26:05 +0200491static struct lock_class_key debugobj_l3_key;
492static struct lock_class_key debugobj_alc_key;
493
494static void slab_set_lock_classes(struct kmem_cache *cachep,
495 struct lock_class_key *l3_key, struct lock_class_key *alc_key,
496 int q)
497{
498 struct array_cache **alc;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000499 struct kmem_cache_node *n;
Peter Zijlstra83835b32011-07-22 15:26:05 +0200500 int r;
501
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000502 n = cachep->node[q];
503 if (!n)
Peter Zijlstra83835b32011-07-22 15:26:05 +0200504 return;
505
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000506 lockdep_set_class(&n->list_lock, l3_key);
507 alc = n->alien;
Peter Zijlstra83835b32011-07-22 15:26:05 +0200508 /*
509 * FIXME: This check for BAD_ALIEN_MAGIC
510 * should go away when common slab code is taught to
511 * work even without alien caches.
512 * Currently, non NUMA code returns BAD_ALIEN_MAGIC
513 * for alloc_alien_cache,
514 */
515 if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC)
516 return;
517 for_each_node(r) {
518 if (alc[r])
519 lockdep_set_class(&alc[r]->lock, alc_key);
520 }
521}
522
523static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
524{
525 slab_set_lock_classes(cachep, &debugobj_l3_key, &debugobj_alc_key, node);
526}
527
528static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
529{
530 int node;
531
532 for_each_online_node(node)
533 slab_set_debugobj_lock_classes_node(cachep, node);
534}
535
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200536static void init_node_lock_keys(int q)
537{
Christoph Lametere3366012013-01-10 19:14:18 +0000538 int i;
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200539
Christoph Lameter97d06602012-07-06 15:25:11 -0500540 if (slab_state < UP)
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200541 return;
542
Christoph Lameter0f8f8092013-07-02 12:12:10 -0700543 for (i = 1; i <= KMALLOC_SHIFT_HIGH; i++) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000544 struct kmem_cache_node *n;
Christoph Lametere3366012013-01-10 19:14:18 +0000545 struct kmem_cache *cache = kmalloc_caches[i];
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200546
Christoph Lametere3366012013-01-10 19:14:18 +0000547 if (!cache)
Pekka Enberg00afa752009-12-27 14:33:14 +0200548 continue;
Peter Zijlstra83835b32011-07-22 15:26:05 +0200549
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000550 n = cache->node[q];
551 if (!n || OFF_SLAB(cache))
Christoph Lametere3366012013-01-10 19:14:18 +0000552 continue;
553
554 slab_set_lock_classes(cache, &on_slab_l3_key,
Peter Zijlstra83835b32011-07-22 15:26:05 +0200555 &on_slab_alc_key, q);
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200556 }
557}
558
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800559static void on_slab_lock_classes_node(struct kmem_cache *cachep, int q)
560{
Christoph Lameter6a673682013-01-10 19:14:19 +0000561 if (!cachep->node[q])
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800562 return;
563
564 slab_set_lock_classes(cachep, &on_slab_l3_key,
565 &on_slab_alc_key, q);
566}
567
568static inline void on_slab_lock_classes(struct kmem_cache *cachep)
569{
570 int node;
571
572 VM_BUG_ON(OFF_SLAB(cachep));
573 for_each_node(node)
574 on_slab_lock_classes_node(cachep, node);
575}
576
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200577static inline void init_lock_keys(void)
578{
579 int node;
580
581 for_each_node(node)
582 init_node_lock_keys(node);
583}
584#else
585static void init_node_lock_keys(int q)
586{
587}
588
589static inline void init_lock_keys(void)
590{
591}
Peter Zijlstra83835b32011-07-22 15:26:05 +0200592
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800593static inline void on_slab_lock_classes(struct kmem_cache *cachep)
594{
595}
596
597static inline void on_slab_lock_classes_node(struct kmem_cache *cachep, int node)
598{
599}
600
Peter Zijlstra83835b32011-07-22 15:26:05 +0200601static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
602{
603}
604
605static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
606{
607}
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200608#endif
609
Tejun Heo1871e522009-10-29 22:34:13 +0900610static DEFINE_PER_CPU(struct delayed_work, slab_reap_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Pekka Enberg343e0d72006-02-01 03:05:50 -0800612static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613{
614 return cachep->array[smp_processor_id()];
615}
616
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800617static size_t slab_mgmt_size(size_t nr_objs, size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800619 return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align);
620}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Andrew Mortona737b3e2006-03-22 00:08:11 -0800622/*
623 * Calculate the number of objects and left-over bytes for a given buffer size.
624 */
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800625static void cache_estimate(unsigned long gfporder, size_t buffer_size,
626 size_t align, int flags, size_t *left_over,
627 unsigned int *num)
628{
629 int nr_objs;
630 size_t mgmt_size;
631 size_t slab_size = PAGE_SIZE << gfporder;
632
633 /*
634 * The slab management structure can be either off the slab or
635 * on it. For the latter case, the memory allocated for a
636 * slab is used for:
637 *
638 * - The struct slab
639 * - One kmem_bufctl_t for each object
640 * - Padding to respect alignment of @align
641 * - @buffer_size bytes for each object
642 *
643 * If the slab management structure is off the slab, then the
644 * alignment will already be calculated into the size. Because
645 * the slabs are all pages aligned, the objects will be at the
646 * correct alignment when allocated.
647 */
648 if (flags & CFLGS_OFF_SLAB) {
649 mgmt_size = 0;
650 nr_objs = slab_size / buffer_size;
651
652 if (nr_objs > SLAB_LIMIT)
653 nr_objs = SLAB_LIMIT;
654 } else {
655 /*
656 * Ignore padding for the initial guess. The padding
657 * is at most @align-1 bytes, and @buffer_size is at
658 * least @align. In the worst case, this result will
659 * be one greater than the number of objects that fit
660 * into the memory allocation when taking the padding
661 * into account.
662 */
663 nr_objs = (slab_size - sizeof(struct slab)) /
664 (buffer_size + sizeof(kmem_bufctl_t));
665
666 /*
667 * This calculated number will be either the right
668 * amount, or one greater than what we want.
669 */
670 if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
671 > slab_size)
672 nr_objs--;
673
674 if (nr_objs > SLAB_LIMIT)
675 nr_objs = SLAB_LIMIT;
676
677 mgmt_size = slab_mgmt_size(nr_objs, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800679 *num = nr_objs;
680 *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
Christoph Lameterf28510d2012-09-11 19:49:38 +0000683#if DEBUG
Harvey Harrisond40cee22008-04-30 00:55:07 -0700684#define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Andrew Mortona737b3e2006-03-22 00:08:11 -0800686static void __slab_error(const char *function, struct kmem_cache *cachep,
687 char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800690 function, cachep->name, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +1030692 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
Christoph Lameterf28510d2012-09-11 19:49:38 +0000694#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Paul Menage3395ee02006-12-06 20:32:16 -0800696/*
697 * By default on NUMA we use alien caches to stage the freeing of
698 * objects allocated from other nodes. This causes massive memory
699 * inefficiencies when using fake NUMA setup to split memory into a
700 * large number of small nodes, so it can be disabled on the command
701 * line
702 */
703
704static int use_alien_caches __read_mostly = 1;
705static int __init noaliencache_setup(char *s)
706{
707 use_alien_caches = 0;
708 return 1;
709}
710__setup("noaliencache", noaliencache_setup);
711
David Rientjes3df1ccc2011-10-18 22:09:28 -0700712static int __init slab_max_order_setup(char *str)
713{
714 get_option(&str, &slab_max_order);
715 slab_max_order = slab_max_order < 0 ? 0 :
716 min(slab_max_order, MAX_ORDER - 1);
717 slab_max_order_set = true;
718
719 return 1;
720}
721__setup("slab_max_order=", slab_max_order_setup);
722
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800723#ifdef CONFIG_NUMA
724/*
725 * Special reaping functions for NUMA systems called from cache_reap().
726 * These take care of doing round robin flushing of alien caches (containing
727 * objects freed on different nodes from which they were allocated) and the
728 * flushing of remote pcps by calling drain_node_pages.
729 */
Tejun Heo1871e522009-10-29 22:34:13 +0900730static DEFINE_PER_CPU(unsigned long, slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800731
732static void init_reap_node(int cpu)
733{
734 int node;
735
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -0700736 node = next_node(cpu_to_mem(cpu), node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800737 if (node == MAX_NUMNODES)
Paul Jackson442295c2006-03-22 00:09:11 -0800738 node = first_node(node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800739
Tejun Heo1871e522009-10-29 22:34:13 +0900740 per_cpu(slab_reap_node, cpu) = node;
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800741}
742
743static void next_reap_node(void)
744{
Christoph Lameter909ea962010-12-08 16:22:55 +0100745 int node = __this_cpu_read(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800746
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800747 node = next_node(node, node_online_map);
748 if (unlikely(node >= MAX_NUMNODES))
749 node = first_node(node_online_map);
Christoph Lameter909ea962010-12-08 16:22:55 +0100750 __this_cpu_write(slab_reap_node, node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800751}
752
753#else
754#define init_reap_node(cpu) do { } while (0)
755#define next_reap_node(void) do { } while (0)
756#endif
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758/*
759 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
760 * via the workqueue/eventd.
761 * Add the CPU number into the expiration time to minimize the possibility of
762 * the CPUs getting into lockstep and contending for the global cache chain
763 * lock.
764 */
Paul Gortmaker0db06282013-06-19 14:53:51 -0400765static void start_cpu_timer(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Tejun Heo1871e522009-10-29 22:34:13 +0900767 struct delayed_work *reap_work = &per_cpu(slab_reap_work, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
769 /*
770 * When this gets called from do_initcalls via cpucache_init(),
771 * init_workqueues() has already run, so keventd will be setup
772 * at that time.
773 */
David Howells52bad642006-11-22 14:54:01 +0000774 if (keventd_up() && reap_work->work.func == NULL) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800775 init_reap_node(cpu);
Tejun Heo203b42f2012-08-21 13:18:23 -0700776 INIT_DEFERRABLE_WORK(reap_work, cache_reap);
Arjan van de Ven2b284212006-12-10 02:21:28 -0800777 schedule_delayed_work_on(cpu, reap_work,
778 __round_jiffies_relative(HZ, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 }
780}
781
Christoph Lametere498be72005-09-09 13:03:32 -0700782static struct array_cache *alloc_arraycache(int node, int entries,
Pekka Enberg83b519e2009-06-10 19:40:04 +0300783 int batchcount, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800785 int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 struct array_cache *nc = NULL;
787
Pekka Enberg83b519e2009-06-10 19:40:04 +0300788 nc = kmalloc_node(memsize, gfp, node);
Catalin Marinasd5cff632009-06-11 13:22:40 +0100789 /*
790 * The array_cache structures contain pointers to free object.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300791 * However, when such objects are allocated or transferred to another
Catalin Marinasd5cff632009-06-11 13:22:40 +0100792 * cache the pointers are not cleared and they could be counted as
793 * valid references during a kmemleak scan. Therefore, kmemleak must
794 * not scan such objects.
795 */
796 kmemleak_no_scan(nc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 if (nc) {
798 nc->avail = 0;
799 nc->limit = entries;
800 nc->batchcount = batchcount;
801 nc->touched = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700802 spin_lock_init(&nc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804 return nc;
805}
806
Mel Gorman072bb0a2012-07-31 16:43:58 -0700807static inline bool is_slab_pfmemalloc(struct slab *slabp)
808{
809 struct page *page = virt_to_page(slabp->s_mem);
810
811 return PageSlabPfmemalloc(page);
812}
813
814/* Clears pfmemalloc_active if no slabs have pfmalloc set */
815static void recheck_pfmemalloc_active(struct kmem_cache *cachep,
816 struct array_cache *ac)
817{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000818 struct kmem_cache_node *n = cachep->node[numa_mem_id()];
Mel Gorman072bb0a2012-07-31 16:43:58 -0700819 struct slab *slabp;
820 unsigned long flags;
821
822 if (!pfmemalloc_active)
823 return;
824
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000825 spin_lock_irqsave(&n->list_lock, flags);
826 list_for_each_entry(slabp, &n->slabs_full, list)
Mel Gorman072bb0a2012-07-31 16:43:58 -0700827 if (is_slab_pfmemalloc(slabp))
828 goto out;
829
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000830 list_for_each_entry(slabp, &n->slabs_partial, list)
Mel Gorman072bb0a2012-07-31 16:43:58 -0700831 if (is_slab_pfmemalloc(slabp))
832 goto out;
833
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000834 list_for_each_entry(slabp, &n->slabs_free, list)
Mel Gorman072bb0a2012-07-31 16:43:58 -0700835 if (is_slab_pfmemalloc(slabp))
836 goto out;
837
838 pfmemalloc_active = false;
839out:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000840 spin_unlock_irqrestore(&n->list_lock, flags);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700841}
842
Mel Gorman381760e2012-07-31 16:44:30 -0700843static void *__ac_get_obj(struct kmem_cache *cachep, struct array_cache *ac,
Mel Gorman072bb0a2012-07-31 16:43:58 -0700844 gfp_t flags, bool force_refill)
845{
846 int i;
847 void *objp = ac->entry[--ac->avail];
848
849 /* Ensure the caller is allowed to use objects from PFMEMALLOC slab */
850 if (unlikely(is_obj_pfmemalloc(objp))) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000851 struct kmem_cache_node *n;
Mel Gorman072bb0a2012-07-31 16:43:58 -0700852
853 if (gfp_pfmemalloc_allowed(flags)) {
854 clear_obj_pfmemalloc(&objp);
855 return objp;
856 }
857
858 /* The caller cannot use PFMEMALLOC objects, find another one */
Joonsoo Kimd014dc22012-09-17 14:09:06 -0700859 for (i = 0; i < ac->avail; i++) {
Mel Gorman072bb0a2012-07-31 16:43:58 -0700860 /* If a !PFMEMALLOC object is found, swap them */
861 if (!is_obj_pfmemalloc(ac->entry[i])) {
862 objp = ac->entry[i];
863 ac->entry[i] = ac->entry[ac->avail];
864 ac->entry[ac->avail] = objp;
865 return objp;
866 }
867 }
868
869 /*
870 * If there are empty slabs on the slabs_free list and we are
871 * being forced to refill the cache, mark this one !pfmemalloc.
872 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000873 n = cachep->node[numa_mem_id()];
874 if (!list_empty(&n->slabs_free) && force_refill) {
Mel Gorman072bb0a2012-07-31 16:43:58 -0700875 struct slab *slabp = virt_to_slab(objp);
Mel Gorman30c29be2012-09-17 14:09:03 -0700876 ClearPageSlabPfmemalloc(virt_to_head_page(slabp->s_mem));
Mel Gorman072bb0a2012-07-31 16:43:58 -0700877 clear_obj_pfmemalloc(&objp);
878 recheck_pfmemalloc_active(cachep, ac);
879 return objp;
880 }
881
882 /* No !PFMEMALLOC objects available */
883 ac->avail++;
884 objp = NULL;
885 }
886
887 return objp;
888}
889
Mel Gorman381760e2012-07-31 16:44:30 -0700890static inline void *ac_get_obj(struct kmem_cache *cachep,
891 struct array_cache *ac, gfp_t flags, bool force_refill)
892{
893 void *objp;
894
895 if (unlikely(sk_memalloc_socks()))
896 objp = __ac_get_obj(cachep, ac, flags, force_refill);
897 else
898 objp = ac->entry[--ac->avail];
899
900 return objp;
901}
902
903static void *__ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
Mel Gorman072bb0a2012-07-31 16:43:58 -0700904 void *objp)
905{
906 if (unlikely(pfmemalloc_active)) {
907 /* Some pfmemalloc slabs exist, check if this is one */
Joonsoo Kim73293c22013-10-24 10:07:37 +0900908 struct slab *slabp = virt_to_slab(objp);
909 struct page *page = virt_to_head_page(slabp->s_mem);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700910 if (PageSlabPfmemalloc(page))
911 set_obj_pfmemalloc(&objp);
912 }
913
Mel Gorman381760e2012-07-31 16:44:30 -0700914 return objp;
915}
916
917static inline void ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
918 void *objp)
919{
920 if (unlikely(sk_memalloc_socks()))
921 objp = __ac_put_obj(cachep, ac, objp);
922
Mel Gorman072bb0a2012-07-31 16:43:58 -0700923 ac->entry[ac->avail++] = objp;
924}
925
Christoph Lameter3ded1752006-03-25 03:06:44 -0800926/*
927 * Transfer objects in one arraycache to another.
928 * Locking must be handled by the caller.
929 *
930 * Return the number of entries transferred.
931 */
932static int transfer_objects(struct array_cache *to,
933 struct array_cache *from, unsigned int max)
934{
935 /* Figure out how many entries to transfer */
Hagen Paul Pfeifer732eacc2010-10-26 14:22:23 -0700936 int nr = min3(from->avail, max, to->limit - to->avail);
Christoph Lameter3ded1752006-03-25 03:06:44 -0800937
938 if (!nr)
939 return 0;
940
941 memcpy(to->entry + to->avail, from->entry + from->avail -nr,
942 sizeof(void *) *nr);
943
944 from->avail -= nr;
945 to->avail += nr;
Christoph Lameter3ded1752006-03-25 03:06:44 -0800946 return nr;
947}
948
Christoph Lameter765c4502006-09-27 01:50:08 -0700949#ifndef CONFIG_NUMA
950
951#define drain_alien_cache(cachep, alien) do { } while (0)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000952#define reap_alien(cachep, n) do { } while (0)
Christoph Lameter765c4502006-09-27 01:50:08 -0700953
Pekka Enberg83b519e2009-06-10 19:40:04 +0300954static inline struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
Christoph Lameter765c4502006-09-27 01:50:08 -0700955{
956 return (struct array_cache **)BAD_ALIEN_MAGIC;
957}
958
959static inline void free_alien_cache(struct array_cache **ac_ptr)
960{
961}
962
963static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
964{
965 return 0;
966}
967
968static inline void *alternate_node_alloc(struct kmem_cache *cachep,
969 gfp_t flags)
970{
971 return NULL;
972}
973
Christoph Hellwig8b98c162006-12-06 20:32:30 -0800974static inline void *____cache_alloc_node(struct kmem_cache *cachep,
Christoph Lameter765c4502006-09-27 01:50:08 -0700975 gfp_t flags, int nodeid)
976{
977 return NULL;
978}
979
980#else /* CONFIG_NUMA */
981
Christoph Hellwig8b98c162006-12-06 20:32:30 -0800982static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
Paul Jacksonc61afb12006-03-24 03:16:08 -0800983static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
Christoph Lameterdc85da12006-01-18 17:42:36 -0800984
Pekka Enberg83b519e2009-06-10 19:40:04 +0300985static struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -0700986{
987 struct array_cache **ac_ptr;
Christoph Lameter8ef82862007-02-20 13:57:52 -0800988 int memsize = sizeof(void *) * nr_node_ids;
Christoph Lametere498be72005-09-09 13:03:32 -0700989 int i;
990
991 if (limit > 1)
992 limit = 12;
Haicheng Lif3186a92010-01-06 15:25:23 +0800993 ac_ptr = kzalloc_node(memsize, gfp, node);
Christoph Lametere498be72005-09-09 13:03:32 -0700994 if (ac_ptr) {
995 for_each_node(i) {
Haicheng Lif3186a92010-01-06 15:25:23 +0800996 if (i == node || !node_online(i))
Christoph Lametere498be72005-09-09 13:03:32 -0700997 continue;
Pekka Enberg83b519e2009-06-10 19:40:04 +0300998 ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d, gfp);
Christoph Lametere498be72005-09-09 13:03:32 -0700999 if (!ac_ptr[i]) {
Akinobu Mitacc550de2007-11-14 16:58:35 -08001000 for (i--; i >= 0; i--)
Christoph Lametere498be72005-09-09 13:03:32 -07001001 kfree(ac_ptr[i]);
1002 kfree(ac_ptr);
1003 return NULL;
1004 }
1005 }
1006 }
1007 return ac_ptr;
1008}
1009
Pekka Enberg5295a742006-02-01 03:05:48 -08001010static void free_alien_cache(struct array_cache **ac_ptr)
Christoph Lametere498be72005-09-09 13:03:32 -07001011{
1012 int i;
1013
1014 if (!ac_ptr)
1015 return;
Christoph Lametere498be72005-09-09 13:03:32 -07001016 for_each_node(i)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001017 kfree(ac_ptr[i]);
Christoph Lametere498be72005-09-09 13:03:32 -07001018 kfree(ac_ptr);
1019}
1020
Pekka Enberg343e0d72006-02-01 03:05:50 -08001021static void __drain_alien_cache(struct kmem_cache *cachep,
Pekka Enberg5295a742006-02-01 03:05:48 -08001022 struct array_cache *ac, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07001023{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001024 struct kmem_cache_node *n = cachep->node[node];
Christoph Lametere498be72005-09-09 13:03:32 -07001025
1026 if (ac->avail) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001027 spin_lock(&n->list_lock);
Christoph Lametere00946f2006-03-25 03:06:45 -08001028 /*
1029 * Stuff objects into the remote nodes shared array first.
1030 * That way we could avoid the overhead of putting the objects
1031 * into the free lists and getting them back later.
1032 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001033 if (n->shared)
1034 transfer_objects(n->shared, ac, ac->limit);
Christoph Lametere00946f2006-03-25 03:06:45 -08001035
Christoph Lameterff694162005-09-22 21:44:02 -07001036 free_block(cachep, ac->entry, ac->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001037 ac->avail = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001038 spin_unlock(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07001039 }
1040}
1041
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001042/*
1043 * Called from cache_reap() to regularly drain alien caches round robin.
1044 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001045static void reap_alien(struct kmem_cache *cachep, struct kmem_cache_node *n)
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001046{
Christoph Lameter909ea962010-12-08 16:22:55 +01001047 int node = __this_cpu_read(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001048
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001049 if (n->alien) {
1050 struct array_cache *ac = n->alien[node];
Christoph Lametere00946f2006-03-25 03:06:45 -08001051
1052 if (ac && ac->avail && spin_trylock_irq(&ac->lock)) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001053 __drain_alien_cache(cachep, ac, node);
1054 spin_unlock_irq(&ac->lock);
1055 }
1056 }
1057}
1058
Andrew Mortona737b3e2006-03-22 00:08:11 -08001059static void drain_alien_cache(struct kmem_cache *cachep,
1060 struct array_cache **alien)
Christoph Lametere498be72005-09-09 13:03:32 -07001061{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001062 int i = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07001063 struct array_cache *ac;
1064 unsigned long flags;
1065
1066 for_each_online_node(i) {
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001067 ac = alien[i];
Christoph Lametere498be72005-09-09 13:03:32 -07001068 if (ac) {
1069 spin_lock_irqsave(&ac->lock, flags);
1070 __drain_alien_cache(cachep, ac, i);
1071 spin_unlock_irqrestore(&ac->lock, flags);
1072 }
1073 }
1074}
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001075
Ingo Molnar873623d2006-07-13 14:44:38 +02001076static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001077{
Joonsoo Kim1ea991b2013-10-24 10:07:40 +09001078 int nodeid = page_to_nid(virt_to_page(objp));
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001079 struct kmem_cache_node *n;
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001080 struct array_cache *alien = NULL;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001081 int node;
1082
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001083 node = numa_mem_id();
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001084
1085 /*
1086 * Make sure we are not freeing a object from another node to the array
1087 * cache on this cpu.
1088 */
Joonsoo Kim1ea991b2013-10-24 10:07:40 +09001089 if (likely(nodeid == node))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001090 return 0;
1091
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001092 n = cachep->node[node];
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001093 STATS_INC_NODEFREES(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001094 if (n->alien && n->alien[nodeid]) {
1095 alien = n->alien[nodeid];
Ingo Molnar873623d2006-07-13 14:44:38 +02001096 spin_lock(&alien->lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001097 if (unlikely(alien->avail == alien->limit)) {
1098 STATS_INC_ACOVERFLOW(cachep);
1099 __drain_alien_cache(cachep, alien, nodeid);
1100 }
Mel Gorman072bb0a2012-07-31 16:43:58 -07001101 ac_put_obj(cachep, alien, objp);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001102 spin_unlock(&alien->lock);
1103 } else {
Christoph Lameter6a673682013-01-10 19:14:19 +00001104 spin_lock(&(cachep->node[nodeid])->list_lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001105 free_block(cachep, &objp, 1, nodeid);
Christoph Lameter6a673682013-01-10 19:14:19 +00001106 spin_unlock(&(cachep->node[nodeid])->list_lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001107 }
1108 return 1;
1109}
Christoph Lametere498be72005-09-09 13:03:32 -07001110#endif
1111
David Rientjes8f9f8d92010-03-27 19:40:47 -07001112/*
Christoph Lameter6a673682013-01-10 19:14:19 +00001113 * Allocates and initializes node for a node on each slab cache, used for
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001114 * either memory or cpu hotplug. If memory is being hot-added, the kmem_cache_node
David Rientjes8f9f8d92010-03-27 19:40:47 -07001115 * will be allocated off-node since memory is not yet online for the new node.
Christoph Lameter6a673682013-01-10 19:14:19 +00001116 * When hotplugging memory or a cpu, existing node are not replaced if
David Rientjes8f9f8d92010-03-27 19:40:47 -07001117 * already in use.
1118 *
Christoph Lameter18004c52012-07-06 15:25:12 -05001119 * Must hold slab_mutex.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001120 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001121static int init_cache_node_node(int node)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001122{
1123 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001124 struct kmem_cache_node *n;
Christoph Lameter6744f082013-01-10 19:12:17 +00001125 const int memsize = sizeof(struct kmem_cache_node);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001126
Christoph Lameter18004c52012-07-06 15:25:12 -05001127 list_for_each_entry(cachep, &slab_caches, list) {
David Rientjes8f9f8d92010-03-27 19:40:47 -07001128 /*
1129 * Set up the size64 kmemlist for cpu before we can
1130 * begin anything. Make sure some other cpu on this
1131 * node has not already allocated this
1132 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001133 if (!cachep->node[node]) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001134 n = kmalloc_node(memsize, GFP_KERNEL, node);
1135 if (!n)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001136 return -ENOMEM;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001137 kmem_cache_node_init(n);
1138 n->next_reap = jiffies + REAPTIMEOUT_LIST3 +
David Rientjes8f9f8d92010-03-27 19:40:47 -07001139 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1140
1141 /*
1142 * The l3s don't come and go as CPUs come and
Christoph Lameter18004c52012-07-06 15:25:12 -05001143 * go. slab_mutex is sufficient
David Rientjes8f9f8d92010-03-27 19:40:47 -07001144 * protection here.
1145 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001146 cachep->node[node] = n;
David Rientjes8f9f8d92010-03-27 19:40:47 -07001147 }
1148
Christoph Lameter6a673682013-01-10 19:14:19 +00001149 spin_lock_irq(&cachep->node[node]->list_lock);
1150 cachep->node[node]->free_limit =
David Rientjes8f9f8d92010-03-27 19:40:47 -07001151 (1 + nr_cpus_node(node)) *
1152 cachep->batchcount + cachep->num;
Christoph Lameter6a673682013-01-10 19:14:19 +00001153 spin_unlock_irq(&cachep->node[node]->list_lock);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001154 }
1155 return 0;
1156}
1157
Wanpeng Li0fa81032013-07-04 08:33:22 +08001158static inline int slabs_tofree(struct kmem_cache *cachep,
1159 struct kmem_cache_node *n)
1160{
1161 return (n->free_objects + cachep->num - 1) / cachep->num;
1162}
1163
Paul Gortmaker0db06282013-06-19 14:53:51 -04001164static void cpuup_canceled(long cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165{
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001166 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001167 struct kmem_cache_node *n = NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001168 int node = cpu_to_mem(cpu);
Rusty Russella70f7302009-03-13 14:49:46 +10301169 const struct cpumask *mask = cpumask_of_node(node);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001170
Christoph Lameter18004c52012-07-06 15:25:12 -05001171 list_for_each_entry(cachep, &slab_caches, list) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001172 struct array_cache *nc;
1173 struct array_cache *shared;
1174 struct array_cache **alien;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001175
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001176 /* cpu is dead; no one can alloc from it. */
1177 nc = cachep->array[cpu];
1178 cachep->array[cpu] = NULL;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001179 n = cachep->node[node];
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001180
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001181 if (!n)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001182 goto free_array_cache;
1183
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001184 spin_lock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001185
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001186 /* Free limit for this kmem_cache_node */
1187 n->free_limit -= cachep->batchcount;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001188 if (nc)
1189 free_block(cachep, nc->entry, nc->avail, node);
1190
Rusty Russell58463c12009-12-17 11:43:12 -06001191 if (!cpumask_empty(mask)) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001192 spin_unlock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001193 goto free_array_cache;
1194 }
1195
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001196 shared = n->shared;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001197 if (shared) {
1198 free_block(cachep, shared->entry,
1199 shared->avail, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001200 n->shared = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001201 }
1202
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001203 alien = n->alien;
1204 n->alien = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001205
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001206 spin_unlock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001207
1208 kfree(shared);
1209 if (alien) {
1210 drain_alien_cache(cachep, alien);
1211 free_alien_cache(alien);
1212 }
1213free_array_cache:
1214 kfree(nc);
1215 }
1216 /*
1217 * In the previous loop, all the objects were freed to
1218 * the respective cache's slabs, now we can go ahead and
1219 * shrink each nodelist to its limit.
1220 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001221 list_for_each_entry(cachep, &slab_caches, list) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001222 n = cachep->node[node];
1223 if (!n)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001224 continue;
Wanpeng Li0fa81032013-07-04 08:33:22 +08001225 drain_freelist(cachep, n, slabs_tofree(cachep, n));
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001226 }
1227}
1228
Paul Gortmaker0db06282013-06-19 14:53:51 -04001229static int cpuup_prepare(long cpu)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001230{
Pekka Enberg343e0d72006-02-01 03:05:50 -08001231 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001232 struct kmem_cache_node *n = NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001233 int node = cpu_to_mem(cpu);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001234 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001236 /*
1237 * We need to do this right in the beginning since
1238 * alloc_arraycache's are going to use this list.
1239 * kmalloc_node allows us to add the slab to the right
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001240 * kmem_cache_node and not this cpu's kmem_cache_node
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001241 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001242 err = init_cache_node_node(node);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001243 if (err < 0)
1244 goto bad;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001245
1246 /*
1247 * Now we can go ahead with allocating the shared arrays and
1248 * array caches
1249 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001250 list_for_each_entry(cachep, &slab_caches, list) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001251 struct array_cache *nc;
1252 struct array_cache *shared = NULL;
1253 struct array_cache **alien = NULL;
1254
1255 nc = alloc_arraycache(node, cachep->limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001256 cachep->batchcount, GFP_KERNEL);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001257 if (!nc)
1258 goto bad;
1259 if (cachep->shared) {
1260 shared = alloc_arraycache(node,
1261 cachep->shared * cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001262 0xbaadf00d, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001263 if (!shared) {
1264 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001265 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001266 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001267 }
1268 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03001269 alien = alloc_alien_cache(node, cachep->limit, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001270 if (!alien) {
1271 kfree(shared);
1272 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001273 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001274 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001275 }
1276 cachep->array[cpu] = nc;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001277 n = cachep->node[node];
1278 BUG_ON(!n);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001279
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001280 spin_lock_irq(&n->list_lock);
1281 if (!n->shared) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001282 /*
1283 * We are serialised from CPU_DEAD or
1284 * CPU_UP_CANCELLED by the cpucontrol lock
1285 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001286 n->shared = shared;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001287 shared = NULL;
1288 }
1289#ifdef CONFIG_NUMA
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001290 if (!n->alien) {
1291 n->alien = alien;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001292 alien = NULL;
1293 }
1294#endif
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001295 spin_unlock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001296 kfree(shared);
1297 free_alien_cache(alien);
Peter Zijlstra83835b32011-07-22 15:26:05 +02001298 if (cachep->flags & SLAB_DEBUG_OBJECTS)
1299 slab_set_debugobj_lock_classes_node(cachep, node);
Glauber Costa6ccfb5b2012-12-18 14:22:31 -08001300 else if (!OFF_SLAB(cachep) &&
1301 !(cachep->flags & SLAB_DESTROY_BY_RCU))
1302 on_slab_lock_classes_node(cachep, node);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001303 }
Pekka Enbergce79ddc2009-11-23 22:01:15 +02001304 init_node_lock_keys(node);
1305
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001306 return 0;
1307bad:
Akinobu Mita12d00f62007-10-18 03:05:11 -07001308 cpuup_canceled(cpu);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001309 return -ENOMEM;
1310}
1311
Paul Gortmaker0db06282013-06-19 14:53:51 -04001312static int cpuup_callback(struct notifier_block *nfb,
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001313 unsigned long action, void *hcpu)
1314{
1315 long cpu = (long)hcpu;
1316 int err = 0;
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 switch (action) {
Heiko Carstens38c3bd92007-05-09 02:34:05 -07001319 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001320 case CPU_UP_PREPARE_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001321 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001322 err = cpuup_prepare(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001323 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 break;
1325 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001326 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 start_cpu_timer(cpu);
1328 break;
1329#ifdef CONFIG_HOTPLUG_CPU
Christoph Lameter5830c592007-05-09 02:34:22 -07001330 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001331 case CPU_DOWN_PREPARE_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001332 /*
Christoph Lameter18004c52012-07-06 15:25:12 -05001333 * Shutdown cache reaper. Note that the slab_mutex is
Christoph Lameter5830c592007-05-09 02:34:22 -07001334 * held so that if cache_reap() is invoked it cannot do
1335 * anything expensive but will only modify reap_work
1336 * and reschedule the timer.
1337 */
Tejun Heoafe2c512010-12-14 16:21:17 +01001338 cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu));
Christoph Lameter5830c592007-05-09 02:34:22 -07001339 /* Now the cache_reaper is guaranteed to be not running. */
Tejun Heo1871e522009-10-29 22:34:13 +09001340 per_cpu(slab_reap_work, cpu).work.func = NULL;
Christoph Lameter5830c592007-05-09 02:34:22 -07001341 break;
1342 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001343 case CPU_DOWN_FAILED_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001344 start_cpu_timer(cpu);
1345 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001347 case CPU_DEAD_FROZEN:
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001348 /*
1349 * Even if all the cpus of a node are down, we don't free the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001350 * kmem_cache_node of any cache. This to avoid a race between
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001351 * cpu_down, and a kmalloc allocation from another cpu for
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001352 * memory from the node of the cpu going down. The node
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001353 * structure is usually allocated from kmem_cache_create() and
1354 * gets destroyed at kmem_cache_destroy().
1355 */
Simon Arlott183ff222007-10-20 01:27:18 +02001356 /* fall through */
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08001357#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001359 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001360 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001361 cpuup_canceled(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001362 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
Akinobu Mitaeac40682010-05-26 14:43:32 -07001365 return notifier_from_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}
1367
Paul Gortmaker0db06282013-06-19 14:53:51 -04001368static struct notifier_block cpucache_notifier = {
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001369 &cpuup_callback, NULL, 0
1370};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
David Rientjes8f9f8d92010-03-27 19:40:47 -07001372#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
1373/*
1374 * Drains freelist for a node on each slab cache, used for memory hot-remove.
1375 * Returns -EBUSY if all objects cannot be drained so that the node is not
1376 * removed.
1377 *
Christoph Lameter18004c52012-07-06 15:25:12 -05001378 * Must hold slab_mutex.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001379 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001380static int __meminit drain_cache_node_node(int node)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001381{
1382 struct kmem_cache *cachep;
1383 int ret = 0;
1384
Christoph Lameter18004c52012-07-06 15:25:12 -05001385 list_for_each_entry(cachep, &slab_caches, list) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001386 struct kmem_cache_node *n;
David Rientjes8f9f8d92010-03-27 19:40:47 -07001387
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001388 n = cachep->node[node];
1389 if (!n)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001390 continue;
1391
Wanpeng Li0fa81032013-07-04 08:33:22 +08001392 drain_freelist(cachep, n, slabs_tofree(cachep, n));
David Rientjes8f9f8d92010-03-27 19:40:47 -07001393
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001394 if (!list_empty(&n->slabs_full) ||
1395 !list_empty(&n->slabs_partial)) {
David Rientjes8f9f8d92010-03-27 19:40:47 -07001396 ret = -EBUSY;
1397 break;
1398 }
1399 }
1400 return ret;
1401}
1402
1403static int __meminit slab_memory_callback(struct notifier_block *self,
1404 unsigned long action, void *arg)
1405{
1406 struct memory_notify *mnb = arg;
1407 int ret = 0;
1408 int nid;
1409
1410 nid = mnb->status_change_nid;
1411 if (nid < 0)
1412 goto out;
1413
1414 switch (action) {
1415 case MEM_GOING_ONLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001416 mutex_lock(&slab_mutex);
Christoph Lameter6a673682013-01-10 19:14:19 +00001417 ret = init_cache_node_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001418 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001419 break;
1420 case MEM_GOING_OFFLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001421 mutex_lock(&slab_mutex);
Christoph Lameter6a673682013-01-10 19:14:19 +00001422 ret = drain_cache_node_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001423 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001424 break;
1425 case MEM_ONLINE:
1426 case MEM_OFFLINE:
1427 case MEM_CANCEL_ONLINE:
1428 case MEM_CANCEL_OFFLINE:
1429 break;
1430 }
1431out:
Prarit Bhargava5fda1bd2011-03-22 16:30:49 -07001432 return notifier_from_errno(ret);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001433}
1434#endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
1435
Christoph Lametere498be72005-09-09 13:03:32 -07001436/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001437 * swap the static kmem_cache_node with kmalloced memory
Christoph Lametere498be72005-09-09 13:03:32 -07001438 */
Christoph Lameter6744f082013-01-10 19:12:17 +00001439static void __init init_list(struct kmem_cache *cachep, struct kmem_cache_node *list,
David Rientjes8f9f8d92010-03-27 19:40:47 -07001440 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07001441{
Christoph Lameter6744f082013-01-10 19:12:17 +00001442 struct kmem_cache_node *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001443
Christoph Lameter6744f082013-01-10 19:12:17 +00001444 ptr = kmalloc_node(sizeof(struct kmem_cache_node), GFP_NOWAIT, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07001445 BUG_ON(!ptr);
1446
Christoph Lameter6744f082013-01-10 19:12:17 +00001447 memcpy(ptr, list, sizeof(struct kmem_cache_node));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001448 /*
1449 * Do not assume that spinlocks can be initialized via memcpy:
1450 */
1451 spin_lock_init(&ptr->list_lock);
1452
Christoph Lametere498be72005-09-09 13:03:32 -07001453 MAKE_ALL_LISTS(cachep, ptr, nodeid);
Christoph Lameter6a673682013-01-10 19:14:19 +00001454 cachep->node[nodeid] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001455}
1456
Andrew Mortona737b3e2006-03-22 00:08:11 -08001457/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001458 * For setting up all the kmem_cache_node for cache whose buffer_size is same as
1459 * size of kmem_cache_node.
Pekka Enberg556a1692008-01-25 08:20:51 +02001460 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001461static void __init set_up_node(struct kmem_cache *cachep, int index)
Pekka Enberg556a1692008-01-25 08:20:51 +02001462{
1463 int node;
1464
1465 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001466 cachep->node[node] = &init_kmem_cache_node[index + node];
Christoph Lameter6a673682013-01-10 19:14:19 +00001467 cachep->node[node]->next_reap = jiffies +
Pekka Enberg556a1692008-01-25 08:20:51 +02001468 REAPTIMEOUT_LIST3 +
1469 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1470 }
1471}
1472
1473/*
Christoph Lameter3c583462012-11-28 16:23:01 +00001474 * The memory after the last cpu cache pointer is used for the
Christoph Lameter6a673682013-01-10 19:14:19 +00001475 * the node pointer.
Christoph Lameter3c583462012-11-28 16:23:01 +00001476 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001477static void setup_node_pointer(struct kmem_cache *cachep)
Christoph Lameter3c583462012-11-28 16:23:01 +00001478{
Christoph Lameter6a673682013-01-10 19:14:19 +00001479 cachep->node = (struct kmem_cache_node **)&cachep->array[nr_cpu_ids];
Christoph Lameter3c583462012-11-28 16:23:01 +00001480}
1481
1482/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08001483 * Initialisation. Called after the page allocator have been initialised and
1484 * before smp_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 */
1486void __init kmem_cache_init(void)
1487{
Christoph Lametere498be72005-09-09 13:03:32 -07001488 int i;
1489
Joonsoo Kim68126702013-10-24 10:07:42 +09001490 BUILD_BUG_ON(sizeof(((struct page *)NULL)->lru) <
1491 sizeof(struct rcu_head));
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001492 kmem_cache = &kmem_cache_boot;
Christoph Lameter6a673682013-01-10 19:14:19 +00001493 setup_node_pointer(kmem_cache);
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001494
Mel Gormanb6e68bc2009-06-16 15:32:16 -07001495 if (num_possible_nodes() == 1)
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001496 use_alien_caches = 0;
1497
Christoph Lameter3c583462012-11-28 16:23:01 +00001498 for (i = 0; i < NUM_INIT_LISTS; i++)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001499 kmem_cache_node_init(&init_kmem_cache_node[i]);
Christoph Lameter3c583462012-11-28 16:23:01 +00001500
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001501 set_up_node(kmem_cache, CACHE_CACHE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 /*
1504 * Fragmentation resistance on low memory - only use bigger
David Rientjes3df1ccc2011-10-18 22:09:28 -07001505 * page orders on machines with more than 32MB of memory if
1506 * not overridden on the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 */
David Rientjes3df1ccc2011-10-18 22:09:28 -07001508 if (!slab_max_order_set && totalram_pages > (32 << 20) >> PAGE_SHIFT)
David Rientjes543585c2011-10-18 22:09:24 -07001509 slab_max_order = SLAB_MAX_ORDER_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 /* Bootstrap is tricky, because several objects are allocated
1512 * from caches that do not exist yet:
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001513 * 1) initialize the kmem_cache cache: it contains the struct
1514 * kmem_cache structures of all caches, except kmem_cache itself:
1515 * kmem_cache is statically allocated.
Christoph Lametere498be72005-09-09 13:03:32 -07001516 * Initially an __init data area is used for the head array and the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001517 * kmem_cache_node structures, it's replaced with a kmalloc allocated
Christoph Lametere498be72005-09-09 13:03:32 -07001518 * array at the end of the bootstrap.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 * 2) Create the first kmalloc cache.
Pekka Enberg343e0d72006-02-01 03:05:50 -08001520 * The struct kmem_cache for the new cache is allocated normally.
Christoph Lametere498be72005-09-09 13:03:32 -07001521 * An __init data area is used for the head array.
1522 * 3) Create the remaining kmalloc caches, with minimally sized
1523 * head arrays.
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001524 * 4) Replace the __init data head arrays for kmem_cache and the first
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 * kmalloc cache with kmalloc allocated arrays.
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001526 * 5) Replace the __init data for kmem_cache_node for kmem_cache and
Christoph Lametere498be72005-09-09 13:03:32 -07001527 * the other cache's with kmalloc allocated memory.
1528 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 */
1530
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001531 /* 1) create the kmem_cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Eric Dumazet8da34302007-05-06 14:49:29 -07001533 /*
Eric Dumazetb56efcf2011-07-20 19:04:23 +02001534 * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
Eric Dumazet8da34302007-05-06 14:49:29 -07001535 */
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001536 create_boot_cache(kmem_cache, "kmem_cache",
1537 offsetof(struct kmem_cache, array[nr_cpu_ids]) +
Christoph Lameter6744f082013-01-10 19:12:17 +00001538 nr_node_ids * sizeof(struct kmem_cache_node *),
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001539 SLAB_HWCACHE_ALIGN);
1540 list_add(&kmem_cache->list, &slab_caches);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541
1542 /* 2+3) create the kmalloc caches */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Andrew Mortona737b3e2006-03-22 00:08:11 -08001544 /*
1545 * Initialize the caches that provide memory for the array cache and the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001546 * kmem_cache_node structures first. Without this, further allocations will
Andrew Mortona737b3e2006-03-22 00:08:11 -08001547 * bug.
Christoph Lametere498be72005-09-09 13:03:32 -07001548 */
1549
Christoph Lametere3366012013-01-10 19:14:18 +00001550 kmalloc_caches[INDEX_AC] = create_kmalloc_cache("kmalloc-ac",
1551 kmalloc_size(INDEX_AC), ARCH_KMALLOC_FLAGS);
Christoph Lametere498be72005-09-09 13:03:32 -07001552
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001553 if (INDEX_AC != INDEX_NODE)
1554 kmalloc_caches[INDEX_NODE] =
1555 create_kmalloc_cache("kmalloc-node",
1556 kmalloc_size(INDEX_NODE), ARCH_KMALLOC_FLAGS);
Christoph Lametere498be72005-09-09 13:03:32 -07001557
Ingo Molnare0a42722006-06-23 02:03:46 -07001558 slab_early_init = 0;
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 /* 4) Replace the bootstrap head arrays */
1561 {
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001562 struct array_cache *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001563
Pekka Enberg83b519e2009-06-10 19:40:04 +03001564 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001565
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001566 memcpy(ptr, cpu_cache_get(kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001567 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001568 /*
1569 * Do not assume that spinlocks can be initialized via memcpy:
1570 */
1571 spin_lock_init(&ptr->lock);
1572
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001573 kmem_cache->array[smp_processor_id()] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001574
Pekka Enberg83b519e2009-06-10 19:40:04 +03001575 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001576
Christoph Lametere3366012013-01-10 19:14:18 +00001577 BUG_ON(cpu_cache_get(kmalloc_caches[INDEX_AC])
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001578 != &initarray_generic.cache);
Christoph Lametere3366012013-01-10 19:14:18 +00001579 memcpy(ptr, cpu_cache_get(kmalloc_caches[INDEX_AC]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001580 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001581 /*
1582 * Do not assume that spinlocks can be initialized via memcpy:
1583 */
1584 spin_lock_init(&ptr->lock);
1585
Christoph Lametere3366012013-01-10 19:14:18 +00001586 kmalloc_caches[INDEX_AC]->array[smp_processor_id()] = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001588 /* 5) Replace the bootstrap kmem_cache_node */
Christoph Lametere498be72005-09-09 13:03:32 -07001589 {
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001590 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
Mel Gorman9c09a952008-01-24 05:49:54 -08001592 for_each_online_node(nid) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001593 init_list(kmem_cache, &init_kmem_cache_node[CACHE_CACHE + nid], nid);
Pekka Enberg556a1692008-01-25 08:20:51 +02001594
Christoph Lametere3366012013-01-10 19:14:18 +00001595 init_list(kmalloc_caches[INDEX_AC],
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001596 &init_kmem_cache_node[SIZE_AC + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001597
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001598 if (INDEX_AC != INDEX_NODE) {
1599 init_list(kmalloc_caches[INDEX_NODE],
1600 &init_kmem_cache_node[SIZE_NODE + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001601 }
1602 }
1603 }
1604
Christoph Lameterf97d5f62013-01-10 19:12:17 +00001605 create_kmalloc_caches(ARCH_KMALLOC_FLAGS);
Pekka Enberg8429db52009-06-12 15:58:59 +03001606}
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001607
Pekka Enberg8429db52009-06-12 15:58:59 +03001608void __init kmem_cache_init_late(void)
1609{
1610 struct kmem_cache *cachep;
1611
Christoph Lameter97d06602012-07-06 15:25:11 -05001612 slab_state = UP;
Peter Zijlstra52cef182011-11-28 21:12:40 +01001613
Pekka Enberg8429db52009-06-12 15:58:59 +03001614 /* 6) resize the head arrays to their final sizes */
Christoph Lameter18004c52012-07-06 15:25:12 -05001615 mutex_lock(&slab_mutex);
1616 list_for_each_entry(cachep, &slab_caches, list)
Pekka Enberg8429db52009-06-12 15:58:59 +03001617 if (enable_cpucache(cachep, GFP_NOWAIT))
1618 BUG();
Christoph Lameter18004c52012-07-06 15:25:12 -05001619 mutex_unlock(&slab_mutex);
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001620
Michael Wang947ca182012-09-05 10:33:18 +08001621 /* Annotate slab for lockdep -- annotate the malloc caches */
1622 init_lock_keys();
1623
Christoph Lameter97d06602012-07-06 15:25:11 -05001624 /* Done! */
1625 slab_state = FULL;
1626
Andrew Mortona737b3e2006-03-22 00:08:11 -08001627 /*
1628 * Register a cpu startup notifier callback that initializes
1629 * cpu_cache_get for all new cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 */
1631 register_cpu_notifier(&cpucache_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
David Rientjes8f9f8d92010-03-27 19:40:47 -07001633#ifdef CONFIG_NUMA
1634 /*
1635 * Register a memory hotplug callback that initializes and frees
Christoph Lameter6a673682013-01-10 19:14:19 +00001636 * node.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001637 */
1638 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
1639#endif
1640
Andrew Mortona737b3e2006-03-22 00:08:11 -08001641 /*
1642 * The reap timers are started later, with a module init call: That part
1643 * of the kernel is not yet operational.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 */
1645}
1646
1647static int __init cpucache_init(void)
1648{
1649 int cpu;
1650
Andrew Mortona737b3e2006-03-22 00:08:11 -08001651 /*
1652 * Register the timers that return unneeded pages to the page allocator
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 */
Christoph Lametere498be72005-09-09 13:03:32 -07001654 for_each_online_cpu(cpu)
Andrew Mortona737b3e2006-03-22 00:08:11 -08001655 start_cpu_timer(cpu);
Glauber Costaa164f8962012-06-21 00:59:18 +04001656
1657 /* Done! */
Christoph Lameter97d06602012-07-06 15:25:11 -05001658 slab_state = FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 return 0;
1660}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661__initcall(cpucache_init);
1662
Rafael Aquini8bdec192012-03-09 17:27:27 -03001663static noinline void
1664slab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid)
1665{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001666 struct kmem_cache_node *n;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001667 struct slab *slabp;
1668 unsigned long flags;
1669 int node;
1670
1671 printk(KERN_WARNING
1672 "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n",
1673 nodeid, gfpflags);
1674 printk(KERN_WARNING " cache: %s, object size: %d, order: %d\n",
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05001675 cachep->name, cachep->size, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001676
1677 for_each_online_node(node) {
1678 unsigned long active_objs = 0, num_objs = 0, free_objects = 0;
1679 unsigned long active_slabs = 0, num_slabs = 0;
1680
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001681 n = cachep->node[node];
1682 if (!n)
Rafael Aquini8bdec192012-03-09 17:27:27 -03001683 continue;
1684
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001685 spin_lock_irqsave(&n->list_lock, flags);
1686 list_for_each_entry(slabp, &n->slabs_full, list) {
Rafael Aquini8bdec192012-03-09 17:27:27 -03001687 active_objs += cachep->num;
1688 active_slabs++;
1689 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001690 list_for_each_entry(slabp, &n->slabs_partial, list) {
Rafael Aquini8bdec192012-03-09 17:27:27 -03001691 active_objs += slabp->inuse;
1692 active_slabs++;
1693 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001694 list_for_each_entry(slabp, &n->slabs_free, list)
Rafael Aquini8bdec192012-03-09 17:27:27 -03001695 num_slabs++;
1696
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001697 free_objects += n->free_objects;
1698 spin_unlock_irqrestore(&n->list_lock, flags);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001699
1700 num_slabs += active_slabs;
1701 num_objs = num_slabs * cachep->num;
1702 printk(KERN_WARNING
1703 " node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
1704 node, active_slabs, num_slabs, active_objs, num_objs,
1705 free_objects);
1706 }
1707}
1708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709/*
1710 * Interface to system's page allocator. No need to hold the cache-lock.
1711 *
1712 * If we requested dmaable memory, we will get it. Even if we
1713 * did not request dmaable memory, we might get it, but that
1714 * would be relatively rare and ignorable.
1715 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001716static struct page *kmem_getpages(struct kmem_cache *cachep, gfp_t flags,
1717 int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718{
1719 struct page *page;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001720 int nr_pages;
Christoph Lameter765c4502006-09-27 01:50:08 -07001721
Glauber Costaa618e892012-06-14 16:17:21 +04001722 flags |= cachep->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001723 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1724 flags |= __GFP_RECLAIMABLE;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001725
Linus Torvalds517d0862009-06-16 19:50:13 -07001726 page = alloc_pages_exact_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001727 if (!page) {
1728 if (!(flags & __GFP_NOWARN) && printk_ratelimit())
1729 slab_out_of_memory(cachep, flags, nodeid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 return NULL;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
Mel Gormanb37f1dd2012-07-31 16:44:03 -07001733 /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
Mel Gorman072bb0a2012-07-31 16:43:58 -07001734 if (unlikely(page->pfmemalloc))
1735 pfmemalloc_active = true;
1736
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001737 nr_pages = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
Christoph Lameter972d1a72006-09-25 23:31:51 -07001739 add_zone_page_state(page_zone(page),
1740 NR_SLAB_RECLAIMABLE, nr_pages);
1741 else
1742 add_zone_page_state(page_zone(page),
1743 NR_SLAB_UNRECLAIMABLE, nr_pages);
Joonsoo Kima57a4982013-10-24 10:07:44 +09001744 __SetPageSlab(page);
1745 if (page->pfmemalloc)
1746 SetPageSlabPfmemalloc(page);
Glauber Costa1f458cb2012-12-18 14:22:50 -08001747 memcg_bind_pages(cachep, cachep->gfporder);
Mel Gorman072bb0a2012-07-31 16:43:58 -07001748
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001749 if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
1750 kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
1751
1752 if (cachep->ctor)
1753 kmemcheck_mark_uninitialized_pages(page, nr_pages);
1754 else
1755 kmemcheck_mark_unallocated_pages(page, nr_pages);
1756 }
Pekka Enbergc175eea2008-05-09 20:35:53 +02001757
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001758 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759}
1760
1761/*
1762 * Interface to system's page release.
1763 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001764static void kmem_freepages(struct kmem_cache *cachep, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765{
Joonsoo Kima57a4982013-10-24 10:07:44 +09001766 const unsigned long nr_freed = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001768 kmemcheck_free_shadow(page, cachep->gfporder);
Pekka Enbergc175eea2008-05-09 20:35:53 +02001769
Christoph Lameter972d1a72006-09-25 23:31:51 -07001770 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1771 sub_zone_page_state(page_zone(page),
1772 NR_SLAB_RECLAIMABLE, nr_freed);
1773 else
1774 sub_zone_page_state(page_zone(page),
1775 NR_SLAB_UNRECLAIMABLE, nr_freed);
Joonsoo Kim73293c22013-10-24 10:07:37 +09001776
Joonsoo Kima57a4982013-10-24 10:07:44 +09001777 BUG_ON(!PageSlab(page));
Joonsoo Kim73293c22013-10-24 10:07:37 +09001778 __ClearPageSlabPfmemalloc(page);
Joonsoo Kima57a4982013-10-24 10:07:44 +09001779 __ClearPageSlab(page);
Glauber Costa1f458cb2012-12-18 14:22:50 -08001780
1781 memcg_release_pages(cachep, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 if (current->reclaim_state)
1783 current->reclaim_state->reclaimed_slab += nr_freed;
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001784 __free_memcg_kmem_pages(page, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
1787static void kmem_rcu_free(struct rcu_head *head)
1788{
Joonsoo Kim68126702013-10-24 10:07:42 +09001789 struct kmem_cache *cachep;
1790 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
Joonsoo Kim68126702013-10-24 10:07:42 +09001792 page = container_of(head, struct page, rcu_head);
1793 cachep = page->slab_cache;
1794
1795 kmem_freepages(cachep, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
1797
1798#if DEBUG
1799
1800#ifdef CONFIG_DEBUG_PAGEALLOC
Pekka Enberg343e0d72006-02-01 03:05:50 -08001801static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001802 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001804 int size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001806 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001808 if (size < 5 * sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 return;
1810
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001811 *addr++ = 0x12345678;
1812 *addr++ = caller;
1813 *addr++ = smp_processor_id();
1814 size -= 3 * sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 {
1816 unsigned long *sptr = &caller;
1817 unsigned long svalue;
1818
1819 while (!kstack_end(sptr)) {
1820 svalue = *sptr++;
1821 if (kernel_text_address(svalue)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001822 *addr++ = svalue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 size -= sizeof(unsigned long);
1824 if (size <= sizeof(unsigned long))
1825 break;
1826 }
1827 }
1828
1829 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001830 *addr++ = 0x87654321;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832#endif
1833
Pekka Enberg343e0d72006-02-01 03:05:50 -08001834static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001836 int size = cachep->object_size;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001837 addr = &((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
1839 memset(addr, val, size);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001840 *(unsigned char *)(addr + size - 1) = POISON_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
1843static void dump_line(char *data, int offset, int limit)
1844{
1845 int i;
Dave Jonesaa83aa42006-09-29 01:59:51 -07001846 unsigned char error = 0;
1847 int bad_count = 0;
1848
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02001849 printk(KERN_ERR "%03x: ", offset);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001850 for (i = 0; i < limit; i++) {
1851 if (data[offset + i] != POISON_FREE) {
1852 error = data[offset + i];
1853 bad_count++;
1854 }
Dave Jonesaa83aa42006-09-29 01:59:51 -07001855 }
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02001856 print_hex_dump(KERN_CONT, "", 0, 16, 1,
1857 &data[offset], limit, 1);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001858
1859 if (bad_count == 1) {
1860 error ^= POISON_FREE;
1861 if (!(error & (error - 1))) {
1862 printk(KERN_ERR "Single bit error detected. Probably "
1863 "bad RAM.\n");
1864#ifdef CONFIG_X86
1865 printk(KERN_ERR "Run memtest86+ or a similar memory "
1866 "test tool.\n");
1867#else
1868 printk(KERN_ERR "Run a memory test tool.\n");
1869#endif
1870 }
1871 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872}
1873#endif
1874
1875#if DEBUG
1876
Pekka Enberg343e0d72006-02-01 03:05:50 -08001877static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
1879 int i, size;
1880 char *realobj;
1881
1882 if (cachep->flags & SLAB_RED_ZONE) {
David Woodhouseb46b8f12007-05-08 00:22:59 -07001883 printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001884 *dbg_redzone1(cachep, objp),
1885 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 }
1887
1888 if (cachep->flags & SLAB_STORE_USER) {
Joe Perches071361d2012-12-12 10:19:12 -08001889 printk(KERN_ERR "Last user: [<%p>](%pSR)\n",
1890 *dbg_userword(cachep, objp),
1891 *dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 }
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001893 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001894 size = cachep->object_size;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001895 for (i = 0; i < size && lines; i += 16, lines--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 int limit;
1897 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001898 if (i + limit > size)
1899 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 dump_line(realobj, i, limit);
1901 }
1902}
1903
Pekka Enberg343e0d72006-02-01 03:05:50 -08001904static void check_poison_obj(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905{
1906 char *realobj;
1907 int size, i;
1908 int lines = 0;
1909
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001910 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001911 size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001913 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 char exp = POISON_FREE;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001915 if (i == size - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 exp = POISON_END;
1917 if (realobj[i] != exp) {
1918 int limit;
1919 /* Mismatch ! */
1920 /* Print header */
1921 if (lines == 0) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001922 printk(KERN_ERR
Dave Jonesface37f2011-11-15 15:03:52 -08001923 "Slab corruption (%s): %s start=%p, len=%d\n",
1924 print_tainted(), cachep->name, realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 print_objinfo(cachep, objp, 0);
1926 }
1927 /* Hexdump the affected line */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001928 i = (i / 16) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001930 if (i + limit > size)
1931 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 dump_line(realobj, i, limit);
1933 i += 16;
1934 lines++;
1935 /* Limit to 5 lines */
1936 if (lines > 5)
1937 break;
1938 }
1939 }
1940 if (lines != 0) {
1941 /* Print some data about the neighboring objects, if they
1942 * exist:
1943 */
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08001944 struct slab *slabp = virt_to_slab(objp);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001945 unsigned int objnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001947 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (objnr) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001949 objp = index_to_obj(cachep, slabp, objnr - 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001950 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001952 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 print_objinfo(cachep, objp, 2);
1954 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001955 if (objnr + 1 < cachep->num) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001956 objp = index_to_obj(cachep, slabp, objnr + 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001957 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001959 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 print_objinfo(cachep, objp, 2);
1961 }
1962 }
1963}
1964#endif
1965
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966#if DEBUG
Rabin Vincente79aec22008-07-04 00:40:32 +05301967static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001968{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 int i;
1970 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001971 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
1973 if (cachep->flags & SLAB_POISON) {
1974#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05001975 if (cachep->size % PAGE_SIZE == 0 &&
Andrew Mortona737b3e2006-03-22 00:08:11 -08001976 OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001977 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05001978 cachep->size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 else
1980 check_poison_obj(cachep, objp);
1981#else
1982 check_poison_obj(cachep, objp);
1983#endif
1984 }
1985 if (cachep->flags & SLAB_RED_ZONE) {
1986 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
1987 slab_error(cachep, "start of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001988 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
1990 slab_error(cachep, "end of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001991 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001994}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995#else
Rabin Vincente79aec22008-07-04 00:40:32 +05301996static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001997{
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001998}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999#endif
2000
Randy Dunlap911851e2006-03-22 00:08:14 -08002001/**
2002 * slab_destroy - destroy and release all objects in a slab
2003 * @cachep: cache pointer being destroyed
2004 * @slabp: slab pointer being destroyed
2005 *
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002006 * Destroy all the objs in a slab, and release the mem back to the system.
Andrew Mortona737b3e2006-03-22 00:08:11 -08002007 * Before calling the slab must have been unlinked from the cache. The
2008 * cache-lock is not held/needed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002009 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002010static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002011{
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002012 struct page *page = virt_to_head_page(slabp->s_mem);
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002013
Rabin Vincente79aec22008-07-04 00:40:32 +05302014 slab_destroy_debugcheck(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
Joonsoo Kim68126702013-10-24 10:07:42 +09002016 struct rcu_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Joonsoo Kim68126702013-10-24 10:07:42 +09002018 /*
2019 * RCU free overloads the RCU head over the LRU.
2020 * slab_page has been overloeaded over the LRU,
2021 * however it is not used from now on so that
2022 * we can use it safely.
2023 */
2024 head = (void *)&page->rcu_head;
2025 call_rcu(head, kmem_rcu_free);
2026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 } else {
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002028 kmem_freepages(cachep, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029 }
Joonsoo Kim68126702013-10-24 10:07:42 +09002030
2031 /*
2032 * From now on, we don't use slab management
2033 * although actual page can be freed in rcu context
2034 */
2035 if (OFF_SLAB(cachep))
2036 kmem_cache_free(cachep->slabp_cache, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037}
2038
2039/**
Randy.Dunlapa70773d2006-02-01 03:05:52 -08002040 * calculate_slab_order - calculate size (page order) of slabs
2041 * @cachep: pointer to the cache that is being created
2042 * @size: size of objects to be created in this cache.
2043 * @align: required alignment for the objects.
2044 * @flags: slab allocation flags
2045 *
2046 * Also calculates the number of objects per slab.
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002047 *
2048 * This could be made much more intelligent. For now, try to avoid using
2049 * high order pages for slabs. When the gfp() functions are more friendly
2050 * towards high-order requests, this should be changed.
2051 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002052static size_t calculate_slab_order(struct kmem_cache *cachep,
Randy Dunlapee13d782006-02-01 03:05:53 -08002053 size_t size, size_t align, unsigned long flags)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002054{
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002055 unsigned long offslab_limit;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002056 size_t left_over = 0;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002057 int gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002058
Christoph Lameter0aa817f2007-05-16 22:11:01 -07002059 for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002060 unsigned int num;
2061 size_t remainder;
2062
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002063 cache_estimate(gfporder, size, align, flags, &remainder, &num);
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002064 if (!num)
2065 continue;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002066
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002067 if (flags & CFLGS_OFF_SLAB) {
2068 /*
2069 * Max number of objs-per-slab for caches which
2070 * use off-slab slabs. Needed to avoid a possible
2071 * looping condition in cache_grow().
2072 */
2073 offslab_limit = size - sizeof(struct slab);
2074 offslab_limit /= sizeof(kmem_bufctl_t);
2075
2076 if (num > offslab_limit)
2077 break;
2078 }
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002079
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002080 /* Found something acceptable - save it away */
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002081 cachep->num = num;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002082 cachep->gfporder = gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002083 left_over = remainder;
2084
2085 /*
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08002086 * A VFS-reclaimable slab tends to have most allocations
2087 * as GFP_NOFS and we really don't want to have to be allocating
2088 * higher-order pages when we are unable to shrink dcache.
2089 */
2090 if (flags & SLAB_RECLAIM_ACCOUNT)
2091 break;
2092
2093 /*
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002094 * Large number of objects is good, but very large slabs are
2095 * currently bad for the gfp()s.
2096 */
David Rientjes543585c2011-10-18 22:09:24 -07002097 if (gfporder >= slab_max_order)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002098 break;
2099
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002100 /*
2101 * Acceptable internal fragmentation?
2102 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002103 if (left_over * 8 <= (PAGE_SIZE << gfporder))
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002104 break;
2105 }
2106 return left_over;
2107}
2108
Pekka Enberg83b519e2009-06-10 19:40:04 +03002109static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002110{
Christoph Lameter97d06602012-07-06 15:25:11 -05002111 if (slab_state >= FULL)
Pekka Enberg83b519e2009-06-10 19:40:04 +03002112 return enable_cpucache(cachep, gfp);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002113
Christoph Lameter97d06602012-07-06 15:25:11 -05002114 if (slab_state == DOWN) {
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002115 /*
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002116 * Note: Creation of first cache (kmem_cache).
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002117 * The setup_node is taken care
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002118 * of by the caller of __kmem_cache_create
2119 */
2120 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2121 slab_state = PARTIAL;
2122 } else if (slab_state == PARTIAL) {
2123 /*
2124 * Note: the second kmem_cache_create must create the cache
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002125 * that's used by kmalloc(24), otherwise the creation of
2126 * further caches will BUG().
2127 */
2128 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2129
2130 /*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002131 * If the cache that's used by kmalloc(sizeof(kmem_cache_node)) is
2132 * the second cache, then we need to set up all its node/,
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002133 * otherwise the creation of further caches will BUG().
2134 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002135 set_up_node(cachep, SIZE_AC);
2136 if (INDEX_AC == INDEX_NODE)
2137 slab_state = PARTIAL_NODE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002138 else
Christoph Lameter97d06602012-07-06 15:25:11 -05002139 slab_state = PARTIAL_ARRAYCACHE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002140 } else {
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002141 /* Remaining boot caches */
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002142 cachep->array[smp_processor_id()] =
Pekka Enberg83b519e2009-06-10 19:40:04 +03002143 kmalloc(sizeof(struct arraycache_init), gfp);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002144
Christoph Lameter97d06602012-07-06 15:25:11 -05002145 if (slab_state == PARTIAL_ARRAYCACHE) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002146 set_up_node(cachep, SIZE_NODE);
2147 slab_state = PARTIAL_NODE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002148 } else {
2149 int node;
Pekka Enberg556a1692008-01-25 08:20:51 +02002150 for_each_online_node(node) {
Christoph Lameter6a673682013-01-10 19:14:19 +00002151 cachep->node[node] =
Christoph Lameter6744f082013-01-10 19:12:17 +00002152 kmalloc_node(sizeof(struct kmem_cache_node),
Pekka Enbergeb91f1d2009-06-12 14:56:09 +03002153 gfp, node);
Christoph Lameter6a673682013-01-10 19:14:19 +00002154 BUG_ON(!cachep->node[node]);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002155 kmem_cache_node_init(cachep->node[node]);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002156 }
2157 }
2158 }
Christoph Lameter6a673682013-01-10 19:14:19 +00002159 cachep->node[numa_mem_id()]->next_reap =
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002160 jiffies + REAPTIMEOUT_LIST3 +
2161 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
2162
2163 cpu_cache_get(cachep)->avail = 0;
2164 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
2165 cpu_cache_get(cachep)->batchcount = 1;
2166 cpu_cache_get(cachep)->touched = 0;
2167 cachep->batchcount = 1;
2168 cachep->limit = BOOT_CPUCACHE_ENTRIES;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002169 return 0;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002170}
2171
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002172/**
Christoph Lameter039363f2012-07-06 15:25:10 -05002173 * __kmem_cache_create - Create a cache.
Randy Dunlapa755b762012-11-06 17:10:10 -08002174 * @cachep: cache management descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 * @flags: SLAB flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 *
2177 * Returns a ptr to the cache on success, NULL on failure.
2178 * Cannot be called within a int, but can be interrupted.
Paul Mundt20c2df82007-07-20 10:11:58 +09002179 * The @ctor is run when new pages are allocated by the cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 * The flags are
2182 *
2183 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2184 * to catch references to uninitialised memory.
2185 *
2186 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2187 * for buffer overruns.
2188 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2190 * cacheline. This can be beneficial if you're counting cycles as closely
2191 * as davem.
2192 */
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002193int
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002194__kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195{
2196 size_t left_over, slab_size, ralign;
Pekka Enberg83b519e2009-06-10 19:40:04 +03002197 gfp_t gfp;
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002198 int err;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002199 size_t size = cachep->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202#if FORCED_DEBUG
2203 /*
2204 * Enable redzoning and last user accounting, except for caches with
2205 * large objects, if the increased size would increase the object size
2206 * above the next power of two: caches with object sizes just above a
2207 * power of two have a significant amount of internal fragmentation.
2208 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002209 if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
2210 2 * sizeof(unsigned long long)))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002211 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 if (!(flags & SLAB_DESTROY_BY_RCU))
2213 flags |= SLAB_POISON;
2214#endif
2215 if (flags & SLAB_DESTROY_BY_RCU)
2216 BUG_ON(flags & SLAB_POISON);
2217#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Andrew Mortona737b3e2006-03-22 00:08:11 -08002219 /*
2220 * Check that size is in terms of words. This is needed to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 * unaligned accesses for some archs when redzoning is used, and makes
2222 * sure any on-slab bufctl's are also correctly aligned.
2223 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002224 if (size & (BYTES_PER_WORD - 1)) {
2225 size += (BYTES_PER_WORD - 1);
2226 size &= ~(BYTES_PER_WORD - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 }
2228
Pekka Enbergca5f9702006-09-25 23:31:25 -07002229 /*
David Woodhouse87a927c2007-07-04 21:26:44 -04002230 * Redzoning and user store require word alignment or possibly larger.
2231 * Note this will be overridden by architecture or caller mandated
2232 * alignment if either is greater than BYTES_PER_WORD.
Pekka Enbergca5f9702006-09-25 23:31:25 -07002233 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002234 if (flags & SLAB_STORE_USER)
2235 ralign = BYTES_PER_WORD;
2236
2237 if (flags & SLAB_RED_ZONE) {
2238 ralign = REDZONE_ALIGN;
2239 /* If redzoning, ensure that the second redzone is suitably
2240 * aligned, by adjusting the object size accordingly. */
2241 size += REDZONE_ALIGN - 1;
2242 size &= ~(REDZONE_ALIGN - 1);
2243 }
Pekka Enbergca5f9702006-09-25 23:31:25 -07002244
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002245 /* 3) caller mandated alignment */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002246 if (ralign < cachep->align) {
2247 ralign = cachep->align;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 }
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002249 /* disable debug if necessary */
2250 if (ralign > __alignof__(unsigned long long))
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002251 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002252 /*
Pekka Enbergca5f9702006-09-25 23:31:25 -07002253 * 4) Store it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002255 cachep->align = ralign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256
Pekka Enberg83b519e2009-06-10 19:40:04 +03002257 if (slab_is_available())
2258 gfp = GFP_KERNEL;
2259 else
2260 gfp = GFP_NOWAIT;
2261
Christoph Lameter6a673682013-01-10 19:14:19 +00002262 setup_node_pointer(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Pekka Enbergca5f9702006-09-25 23:31:25 -07002265 /*
2266 * Both debugging options require word-alignment which is calculated
2267 * into align above.
2268 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 if (flags & SLAB_RED_ZONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 /* add space for red zone words */
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002271 cachep->obj_offset += sizeof(unsigned long long);
2272 size += 2 * sizeof(unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 }
2274 if (flags & SLAB_STORE_USER) {
Pekka Enbergca5f9702006-09-25 23:31:25 -07002275 /* user store requires one word storage behind the end of
David Woodhouse87a927c2007-07-04 21:26:44 -04002276 * the real object. But if the second red zone needs to be
2277 * aligned to 64 bits, we must allow that much space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002279 if (flags & SLAB_RED_ZONE)
2280 size += REDZONE_ALIGN;
2281 else
2282 size += BYTES_PER_WORD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 }
2284#if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002285 if (size >= kmalloc_size(INDEX_NODE + 1)
Tetsuo Handa608da7e2012-09-30 17:28:25 +09002286 && cachep->object_size > cache_line_size()
2287 && ALIGN(size, cachep->align) < PAGE_SIZE) {
2288 cachep->obj_offset += PAGE_SIZE - ALIGN(size, cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 size = PAGE_SIZE;
2290 }
2291#endif
2292#endif
2293
Ingo Molnare0a42722006-06-23 02:03:46 -07002294 /*
2295 * Determine if the slab management is 'on' or 'off' slab.
2296 * (bootstrapping cannot cope with offslab caches so don't do
Catalin Marinase7cb55b2009-10-28 13:33:08 +00002297 * it too early on. Always use on-slab management when
2298 * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
Ingo Molnare0a42722006-06-23 02:03:46 -07002299 */
Catalin Marinase7cb55b2009-10-28 13:33:08 +00002300 if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init &&
2301 !(flags & SLAB_NOLEAKTRACE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 /*
2303 * Size is large, assume best to place the slab management obj
2304 * off-slab (should allow better packing of objs).
2305 */
2306 flags |= CFLGS_OFF_SLAB;
2307
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002308 size = ALIGN(size, cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002310 left_over = calculate_slab_order(cachep, size, cachep->align, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002312 if (!cachep->num)
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002313 return -E2BIG;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002314
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002315 slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002316 + sizeof(struct slab), cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 /*
2319 * If the slab has been placed off-slab, and we have enough space then
2320 * move it on-slab. This is at the expense of any extra colouring.
2321 */
2322 if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
2323 flags &= ~CFLGS_OFF_SLAB;
2324 left_over -= slab_size;
2325 }
2326
2327 if (flags & CFLGS_OFF_SLAB) {
2328 /* really off slab. No need for manual alignment */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002329 slab_size =
2330 cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
Ron Lee67461362009-05-22 04:58:22 +09302331
2332#ifdef CONFIG_PAGE_POISONING
2333 /* If we're going to use the generic kernel_map_pages()
2334 * poisoning, then it's going to smash the contents of
2335 * the redzone and userword anyhow, so switch them off.
2336 */
2337 if (size % PAGE_SIZE == 0 && flags & SLAB_POISON)
2338 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
2339#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 }
2341
2342 cachep->colour_off = cache_line_size();
2343 /* Offset must be a multiple of the alignment. */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002344 if (cachep->colour_off < cachep->align)
2345 cachep->colour_off = cachep->align;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002346 cachep->colour = left_over / cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 cachep->slab_size = slab_size;
2348 cachep->flags = flags;
Joonsoo Kima57a4982013-10-24 10:07:44 +09002349 cachep->allocflags = __GFP_COMP;
Christoph Lameter4b51d662007-02-10 01:43:10 -08002350 if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
Glauber Costaa618e892012-06-14 16:17:21 +04002351 cachep->allocflags |= GFP_DMA;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002352 cachep->size = size;
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08002353 cachep->reciprocal_buffer_size = reciprocal_value(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002355 if (flags & CFLGS_OFF_SLAB) {
Christoph Lameter2c59dd62013-01-10 19:14:19 +00002356 cachep->slabp_cache = kmalloc_slab(slab_size, 0u);
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002357 /*
2358 * This is a possibility for one of the malloc_sizes caches.
2359 * But since we go off slab only for object size greater than
2360 * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
2361 * this should not happen at all.
2362 * But leave a BUG_ON for some lucky dude.
2363 */
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002364 BUG_ON(ZERO_OR_NULL_PTR(cachep->slabp_cache));
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002367 err = setup_cpu_cache(cachep, gfp);
2368 if (err) {
Christoph Lameter12c36672012-09-04 23:38:33 +00002369 __kmem_cache_shutdown(cachep);
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002370 return err;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Peter Zijlstra83835b32011-07-22 15:26:05 +02002373 if (flags & SLAB_DEBUG_OBJECTS) {
2374 /*
2375 * Would deadlock through slab_destroy()->call_rcu()->
2376 * debug_object_activate()->kmem_cache_alloc().
2377 */
2378 WARN_ON_ONCE(flags & SLAB_DESTROY_BY_RCU);
2379
2380 slab_set_debugobj_lock_classes(cachep);
Glauber Costa6ccfb5b2012-12-18 14:22:31 -08002381 } else if (!OFF_SLAB(cachep) && !(flags & SLAB_DESTROY_BY_RCU))
2382 on_slab_lock_classes(cachep);
Peter Zijlstra83835b32011-07-22 15:26:05 +02002383
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002384 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386
2387#if DEBUG
2388static void check_irq_off(void)
2389{
2390 BUG_ON(!irqs_disabled());
2391}
2392
2393static void check_irq_on(void)
2394{
2395 BUG_ON(irqs_disabled());
2396}
2397
Pekka Enberg343e0d72006-02-01 03:05:50 -08002398static void check_spinlock_acquired(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
2400#ifdef CONFIG_SMP
2401 check_irq_off();
Christoph Lameter6a673682013-01-10 19:14:19 +00002402 assert_spin_locked(&cachep->node[numa_mem_id()]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403#endif
2404}
Christoph Lametere498be72005-09-09 13:03:32 -07002405
Pekka Enberg343e0d72006-02-01 03:05:50 -08002406static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07002407{
2408#ifdef CONFIG_SMP
2409 check_irq_off();
Christoph Lameter6a673682013-01-10 19:14:19 +00002410 assert_spin_locked(&cachep->node[node]->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002411#endif
2412}
2413
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414#else
2415#define check_irq_off() do { } while(0)
2416#define check_irq_on() do { } while(0)
2417#define check_spinlock_acquired(x) do { } while(0)
Christoph Lametere498be72005-09-09 13:03:32 -07002418#define check_spinlock_acquired_node(x, y) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419#endif
2420
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002421static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
Christoph Lameteraab22072006-03-22 00:09:06 -08002422 struct array_cache *ac,
2423 int force, int node);
2424
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425static void do_drain(void *arg)
2426{
Andrew Mortona737b3e2006-03-22 00:08:11 -08002427 struct kmem_cache *cachep = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 struct array_cache *ac;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07002429 int node = numa_mem_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430
2431 check_irq_off();
Pekka Enberg9a2dba4b2006-02-01 03:05:49 -08002432 ac = cpu_cache_get(cachep);
Christoph Lameter6a673682013-01-10 19:14:19 +00002433 spin_lock(&cachep->node[node]->list_lock);
Christoph Lameterff694162005-09-22 21:44:02 -07002434 free_block(cachep, ac->entry, ac->avail, node);
Christoph Lameter6a673682013-01-10 19:14:19 +00002435 spin_unlock(&cachep->node[node]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 ac->avail = 0;
2437}
2438
Pekka Enberg343e0d72006-02-01 03:05:50 -08002439static void drain_cpu_caches(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002441 struct kmem_cache_node *n;
Christoph Lametere498be72005-09-09 13:03:32 -07002442 int node;
2443
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002444 on_each_cpu(do_drain, cachep, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 check_irq_on();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002446 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002447 n = cachep->node[node];
2448 if (n && n->alien)
2449 drain_alien_cache(cachep, n->alien);
Roland Dreiera4523a82006-05-15 11:41:00 -07002450 }
2451
2452 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002453 n = cachep->node[node];
2454 if (n)
2455 drain_array(cachep, n, n->shared, 1, node);
Christoph Lametere498be72005-09-09 13:03:32 -07002456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
2458
Christoph Lametered11d9e2006-06-30 01:55:45 -07002459/*
2460 * Remove slabs from the list of free slabs.
2461 * Specify the number of slabs to drain in tofree.
2462 *
2463 * Returns the actual number of slabs released.
2464 */
2465static int drain_freelist(struct kmem_cache *cache,
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002466 struct kmem_cache_node *n, int tofree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467{
Christoph Lametered11d9e2006-06-30 01:55:45 -07002468 struct list_head *p;
2469 int nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Christoph Lametered11d9e2006-06-30 01:55:45 -07002472 nr_freed = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002473 while (nr_freed < tofree && !list_empty(&n->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002475 spin_lock_irq(&n->list_lock);
2476 p = n->slabs_free.prev;
2477 if (p == &n->slabs_free) {
2478 spin_unlock_irq(&n->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002479 goto out;
2480 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Christoph Lametered11d9e2006-06-30 01:55:45 -07002482 slabp = list_entry(p, struct slab, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483#if DEBUG
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002484 BUG_ON(slabp->inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485#endif
2486 list_del(&slabp->list);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002487 /*
2488 * Safe to drop the lock. The slab is no longer linked
2489 * to the cache.
2490 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002491 n->free_objects -= cache->num;
2492 spin_unlock_irq(&n->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002493 slab_destroy(cache, slabp);
2494 nr_freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 }
Christoph Lametered11d9e2006-06-30 01:55:45 -07002496out:
2497 return nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498}
2499
Christoph Lameter18004c52012-07-06 15:25:12 -05002500/* Called with slab_mutex held to protect against cpu hotplug */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002501static int __cache_shrink(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07002502{
2503 int ret = 0, i = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002504 struct kmem_cache_node *n;
Christoph Lametere498be72005-09-09 13:03:32 -07002505
2506 drain_cpu_caches(cachep);
2507
2508 check_irq_on();
2509 for_each_online_node(i) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002510 n = cachep->node[i];
2511 if (!n)
Christoph Lametered11d9e2006-06-30 01:55:45 -07002512 continue;
2513
Wanpeng Li0fa81032013-07-04 08:33:22 +08002514 drain_freelist(cachep, n, slabs_tofree(cachep, n));
Christoph Lametered11d9e2006-06-30 01:55:45 -07002515
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002516 ret += !list_empty(&n->slabs_full) ||
2517 !list_empty(&n->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07002518 }
2519 return (ret ? 1 : 0);
2520}
2521
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522/**
2523 * kmem_cache_shrink - Shrink a cache.
2524 * @cachep: The cache to shrink.
2525 *
2526 * Releases as many slabs as possible for a cache.
2527 * To help debugging, a zero exit status indicates all slabs were released.
2528 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002529int kmem_cache_shrink(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530{
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002531 int ret;
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002532 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002534 get_online_cpus();
Christoph Lameter18004c52012-07-06 15:25:12 -05002535 mutex_lock(&slab_mutex);
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002536 ret = __cache_shrink(cachep);
Christoph Lameter18004c52012-07-06 15:25:12 -05002537 mutex_unlock(&slab_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002538 put_online_cpus();
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002539 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540}
2541EXPORT_SYMBOL(kmem_cache_shrink);
2542
Christoph Lameter945cf2b2012-09-04 23:18:33 +00002543int __kmem_cache_shutdown(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544{
Christoph Lameter12c36672012-09-04 23:38:33 +00002545 int i;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002546 struct kmem_cache_node *n;
Christoph Lameter12c36672012-09-04 23:38:33 +00002547 int rc = __cache_shrink(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Christoph Lameter12c36672012-09-04 23:38:33 +00002549 if (rc)
2550 return rc;
2551
2552 for_each_online_cpu(i)
2553 kfree(cachep->array[i]);
2554
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002555 /* NUMA: free the node structures */
Christoph Lameter12c36672012-09-04 23:38:33 +00002556 for_each_online_node(i) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002557 n = cachep->node[i];
2558 if (n) {
2559 kfree(n->shared);
2560 free_alien_cache(n->alien);
2561 kfree(n);
Christoph Lameter12c36672012-09-04 23:38:33 +00002562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 }
Christoph Lameter12c36672012-09-04 23:38:33 +00002564 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002567/*
2568 * Get the memory for a slab management obj.
2569 * For a slab cache when the slab descriptor is off-slab, slab descriptors
2570 * always come from malloc_sizes caches. The slab descriptor cannot
2571 * come from the same cache which is getting created because,
2572 * when we are searching for an appropriate cache for these
2573 * descriptors in kmem_cache_create, we search through the malloc_sizes array.
2574 * If we are creating a malloc_sizes cache here it would not be visible to
2575 * kmem_find_general_cachep till the initialization is complete.
2576 * Hence we cannot have slabp_cache same as the original cache.
2577 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002578static struct slab *alloc_slabmgmt(struct kmem_cache *cachep,
2579 struct page *page, int colour_off,
2580 gfp_t local_flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581{
2582 struct slab *slabp;
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002583 void *addr = page_address(page);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002584
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 if (OFF_SLAB(cachep)) {
2586 /* Slab management obj is off-slab. */
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002587 slabp = kmem_cache_alloc_node(cachep->slabp_cache,
Pekka Enberg8759ec52008-11-26 10:01:31 +02002588 local_flags, nodeid);
Catalin Marinasd5cff632009-06-11 13:22:40 +01002589 /*
2590 * If the first object in the slab is leaked (it's allocated
2591 * but no one has a reference to it), we want to make sure
2592 * kmemleak does not treat the ->s_mem pointer as a reference
2593 * to the object. Otherwise we will not report the leak.
2594 */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002595 kmemleak_scan_area(&slabp->list, sizeof(struct list_head),
2596 local_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 if (!slabp)
2598 return NULL;
2599 } else {
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002600 slabp = addr + colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601 colour_off += cachep->slab_size;
2602 }
2603 slabp->inuse = 0;
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002604 slabp->s_mem = addr + colour_off;
Marcin Slusarze51bfd02008-02-10 11:21:54 +01002605 slabp->free = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606 return slabp;
2607}
2608
2609static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
2610{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002611 return (kmem_bufctl_t *) (slabp + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612}
2613
Pekka Enberg343e0d72006-02-01 03:05:50 -08002614static void cache_init_objs(struct kmem_cache *cachep,
Christoph Lametera35afb82007-05-16 22:10:57 -07002615 struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616{
2617 int i;
2618
2619 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002620 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621#if DEBUG
2622 /* need to poison the objs? */
2623 if (cachep->flags & SLAB_POISON)
2624 poison_obj(cachep, objp, POISON_FREE);
2625 if (cachep->flags & SLAB_STORE_USER)
2626 *dbg_userword(cachep, objp) = NULL;
2627
2628 if (cachep->flags & SLAB_RED_ZONE) {
2629 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2630 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2631 }
2632 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002633 * Constructors are not allowed to allocate memory from the same
2634 * cache which they are a constructor for. Otherwise, deadlock.
2635 * They must also be threaded.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 */
2637 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002638 cachep->ctor(objp + obj_offset(cachep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
2640 if (cachep->flags & SLAB_RED_ZONE) {
2641 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2642 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002643 " end of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2645 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002646 " start of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 }
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002648 if ((cachep->size % PAGE_SIZE) == 0 &&
Andrew Mortona737b3e2006-03-22 00:08:11 -08002649 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002650 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002651 cachep->size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652#else
2653 if (cachep->ctor)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002654 cachep->ctor(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655#endif
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002656 slab_bufctl(slabp)[i] = i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002658 slab_bufctl(slabp)[i - 1] = BUFCTL_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659}
2660
Pekka Enberg343e0d72006-02-01 03:05:50 -08002661static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662{
Christoph Lameter4b51d662007-02-10 01:43:10 -08002663 if (CONFIG_ZONE_DMA_FLAG) {
2664 if (flags & GFP_DMA)
Glauber Costaa618e892012-06-14 16:17:21 +04002665 BUG_ON(!(cachep->allocflags & GFP_DMA));
Christoph Lameter4b51d662007-02-10 01:43:10 -08002666 else
Glauber Costaa618e892012-06-14 16:17:21 +04002667 BUG_ON(cachep->allocflags & GFP_DMA);
Christoph Lameter4b51d662007-02-10 01:43:10 -08002668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669}
2670
Andrew Mortona737b3e2006-03-22 00:08:11 -08002671static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
2672 int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002673{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002674 void *objp = index_to_obj(cachep, slabp, slabp->free);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002675 kmem_bufctl_t next;
2676
2677 slabp->inuse++;
2678 next = slab_bufctl(slabp)[slabp->free];
2679#if DEBUG
2680 slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
Joonsoo Kim1ea991b2013-10-24 10:07:40 +09002681 WARN_ON(page_to_nid(virt_to_page(objp)) != nodeid);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002682#endif
2683 slabp->free = next;
2684
2685 return objp;
2686}
2687
Andrew Mortona737b3e2006-03-22 00:08:11 -08002688static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
2689 void *objp, int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002690{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002691 unsigned int objnr = obj_to_index(cachep, slabp, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002692
2693#if DEBUG
2694 /* Verify that the slab belongs to the intended node */
Joonsoo Kim1ea991b2013-10-24 10:07:40 +09002695 WARN_ON(page_to_nid(virt_to_page(objp)) != nodeid);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002696
Al Viro871751e2006-03-25 03:06:39 -08002697 if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) {
Matthew Dobson78d382d2006-02-01 03:05:47 -08002698 printk(KERN_ERR "slab: double free detected in cache "
Andrew Mortona737b3e2006-03-22 00:08:11 -08002699 "'%s', objp %p\n", cachep->name, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002700 BUG();
2701 }
2702#endif
2703 slab_bufctl(slabp)[objnr] = slabp->free;
2704 slabp->free = objnr;
2705 slabp->inuse--;
2706}
2707
Pekka Enberg47768742006-06-23 02:03:07 -07002708/*
2709 * Map pages beginning at addr to the given cache and slab. This is required
2710 * for the slab allocator to be able to lookup the cache and slab of a
Nick Pigginccd35fb2011-01-07 17:49:17 +11002711 * virtual address for kfree, ksize, and slab debugging.
Pekka Enberg47768742006-06-23 02:03:07 -07002712 */
2713static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002714 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
Joonsoo Kima57a4982013-10-24 10:07:44 +09002716 page->slab_cache = cache;
2717 page->slab_page = slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718}
2719
2720/*
2721 * Grow (by 1) the number of slabs within a cache. This is called by
2722 * kmem_cache_alloc() when there are no active objs left in a cache.
2723 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002724static int cache_grow(struct kmem_cache *cachep,
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002725 gfp_t flags, int nodeid, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002727 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002728 size_t offset;
2729 gfp_t local_flags;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002730 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
Andrew Mortona737b3e2006-03-22 00:08:11 -08002732 /*
2733 * Be lazy and only check for valid flags here, keeping it out of the
2734 * critical path in kmem_cache_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 */
Christoph Lameter6cb06222007-10-16 01:25:41 -07002736 BUG_ON(flags & GFP_SLAB_BUG_MASK);
2737 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002739 /* Take the node list lock to change the colour_next on this node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002741 n = cachep->node[nodeid];
2742 spin_lock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743
2744 /* Get colour for the slab, and cal the next value. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002745 offset = n->colour_next;
2746 n->colour_next++;
2747 if (n->colour_next >= cachep->colour)
2748 n->colour_next = 0;
2749 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002751 offset *= cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752
2753 if (local_flags & __GFP_WAIT)
2754 local_irq_enable();
2755
2756 /*
2757 * The test for missing atomic flag is performed here, rather than
2758 * the more obvious place, simply to reduce the critical path length
2759 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2760 * will eventually be caught here (where it matters).
2761 */
2762 kmem_flagcheck(cachep, flags);
2763
Andrew Mortona737b3e2006-03-22 00:08:11 -08002764 /*
2765 * Get mem for the objs. Attempt to allocate a physical page from
2766 * 'nodeid'.
Christoph Lametere498be72005-09-09 13:03:32 -07002767 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002768 if (!page)
2769 page = kmem_getpages(cachep, local_flags, nodeid);
2770 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 goto failed;
2772
2773 /* Get slab management. */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002774 slabp = alloc_slabmgmt(cachep, page, offset,
Christoph Lameter6cb06222007-10-16 01:25:41 -07002775 local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002776 if (!slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 goto opps1;
2778
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002779 slab_map_pages(cachep, slabp, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Christoph Lametera35afb82007-05-16 22:10:57 -07002781 cache_init_objs(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782
2783 if (local_flags & __GFP_WAIT)
2784 local_irq_disable();
2785 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002786 spin_lock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787
2788 /* Make slab active. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002789 list_add_tail(&slabp->list, &(n->slabs_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 STATS_INC_GROWN(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002791 n->free_objects += cachep->num;
2792 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002794opps1:
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002795 kmem_freepages(cachep, page);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002796failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 if (local_flags & __GFP_WAIT)
2798 local_irq_disable();
2799 return 0;
2800}
2801
2802#if DEBUG
2803
2804/*
2805 * Perform extra freeing checks:
2806 * - detect bad pointers.
2807 * - POISON/RED_ZONE checking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 */
2809static void kfree_debugcheck(const void *objp)
2810{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 if (!virt_addr_valid(objp)) {
2812 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002813 (unsigned long)objp);
2814 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816}
2817
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002818static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2819{
David Woodhouseb46b8f12007-05-08 00:22:59 -07002820 unsigned long long redzone1, redzone2;
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002821
2822 redzone1 = *dbg_redzone1(cache, obj);
2823 redzone2 = *dbg_redzone2(cache, obj);
2824
2825 /*
2826 * Redzone is ok.
2827 */
2828 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2829 return;
2830
2831 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2832 slab_error(cache, "double free detected");
2833 else
2834 slab_error(cache, "memory outside object was overwritten");
2835
David Woodhouseb46b8f12007-05-08 00:22:59 -07002836 printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002837 obj, redzone1, redzone2);
2838}
2839
Pekka Enberg343e0d72006-02-01 03:05:50 -08002840static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002841 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843 unsigned int objnr;
2844 struct slab *slabp;
2845
Matthew Wilcox80cbd912007-11-29 12:05:13 -07002846 BUG_ON(virt_to_cache(objp) != cachep);
2847
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002848 objp -= obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 kfree_debugcheck(objp);
Joonsoo Kim56f295e2013-10-24 10:07:43 +09002850 slabp = virt_to_slab(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
2852 if (cachep->flags & SLAB_RED_ZONE) {
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002853 verify_redzone_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2855 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2856 }
2857 if (cachep->flags & SLAB_STORE_USER)
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002858 *dbg_userword(cachep, objp) = (void *)caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002860 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
2862 BUG_ON(objnr >= cachep->num);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002863 BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Al Viro871751e2006-03-25 03:06:39 -08002865#ifdef CONFIG_DEBUG_SLAB_LEAK
2866 slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
2867#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 if (cachep->flags & SLAB_POISON) {
2869#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002870 if ((cachep->size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002871 store_stackinfo(cachep, objp, caller);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002872 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002873 cachep->size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 } else {
2875 poison_obj(cachep, objp, POISON_FREE);
2876 }
2877#else
2878 poison_obj(cachep, objp, POISON_FREE);
2879#endif
2880 }
2881 return objp;
2882}
2883
Pekka Enberg343e0d72006-02-01 03:05:50 -08002884static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885{
2886 kmem_bufctl_t i;
2887 int entries = 0;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002888
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 /* Check slab's freelist to see if this obj is there. */
2890 for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
2891 entries++;
2892 if (entries > cachep->num || i >= cachep->num)
2893 goto bad;
2894 }
2895 if (entries != cachep->num - slabp->inuse) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002896bad:
2897 printk(KERN_ERR "slab: Internal list corruption detected in "
Dave Jonesface37f2011-11-15 15:03:52 -08002898 "cache '%s'(%d), slabp %p(%d). Tainted(%s). Hexdump:\n",
2899 cachep->name, cachep->num, slabp, slabp->inuse,
2900 print_tainted());
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02002901 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, slabp,
2902 sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t),
2903 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 BUG();
2905 }
2906}
2907#else
2908#define kfree_debugcheck(x) do { } while(0)
2909#define cache_free_debugcheck(x,objp,z) (objp)
2910#define check_slabp(x,y) do { } while(0)
2911#endif
2912
Mel Gorman072bb0a2012-07-31 16:43:58 -07002913static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags,
2914 bool force_refill)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915{
2916 int batchcount;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002917 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 struct array_cache *ac;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002919 int node;
2920
Joe Korty6d2144d2008-03-05 15:04:59 -08002921 check_irq_off();
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07002922 node = numa_mem_id();
Mel Gorman072bb0a2012-07-31 16:43:58 -07002923 if (unlikely(force_refill))
2924 goto force_grow;
2925retry:
Joe Korty6d2144d2008-03-05 15:04:59 -08002926 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 batchcount = ac->batchcount;
2928 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002929 /*
2930 * If there was little recent activity on this cache, then
2931 * perform only a partial refill. Otherwise we could generate
2932 * refill bouncing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 */
2934 batchcount = BATCHREFILL_LIMIT;
2935 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002936 n = cachep->node[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002938 BUG_ON(ac->avail > 0 || !n);
2939 spin_lock(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002940
Christoph Lameter3ded1752006-03-25 03:06:44 -08002941 /* See if we can refill from the shared array */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002942 if (n->shared && transfer_objects(ac, n->shared, batchcount)) {
2943 n->shared->touched = 1;
Christoph Lameter3ded1752006-03-25 03:06:44 -08002944 goto alloc_done;
Nick Piggin44b57f12010-01-27 22:27:40 +11002945 }
Christoph Lameter3ded1752006-03-25 03:06:44 -08002946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 while (batchcount > 0) {
2948 struct list_head *entry;
2949 struct slab *slabp;
2950 /* Get slab alloc is to come from. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002951 entry = n->slabs_partial.next;
2952 if (entry == &n->slabs_partial) {
2953 n->free_touched = 1;
2954 entry = n->slabs_free.next;
2955 if (entry == &n->slabs_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 goto must_grow;
2957 }
2958
2959 slabp = list_entry(entry, struct slab, list);
2960 check_slabp(cachep, slabp);
2961 check_spinlock_acquired(cachep);
Pekka Enberg714b81712007-05-06 14:49:03 -07002962
2963 /*
2964 * The slab was either on partial or free list so
2965 * there must be at least one object available for
2966 * allocation.
2967 */
roel kluin249b9f32008-10-29 17:18:07 -04002968 BUG_ON(slabp->inuse >= cachep->num);
Pekka Enberg714b81712007-05-06 14:49:03 -07002969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 while (slabp->inuse < cachep->num && batchcount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 STATS_INC_ALLOCED(cachep);
2972 STATS_INC_ACTIVE(cachep);
2973 STATS_SET_HIGH(cachep);
2974
Mel Gorman072bb0a2012-07-31 16:43:58 -07002975 ac_put_obj(cachep, ac, slab_get_obj(cachep, slabp,
2976 node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 }
2978 check_slabp(cachep, slabp);
2979
2980 /* move slabp to correct slabp list: */
2981 list_del(&slabp->list);
2982 if (slabp->free == BUFCTL_END)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002983 list_add(&slabp->list, &n->slabs_full);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 else
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002985 list_add(&slabp->list, &n->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 }
2987
Andrew Mortona737b3e2006-03-22 00:08:11 -08002988must_grow:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002989 n->free_objects -= ac->avail;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002990alloc_done:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002991 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992
2993 if (unlikely(!ac->avail)) {
2994 int x;
Mel Gorman072bb0a2012-07-31 16:43:58 -07002995force_grow:
Christoph Lameter3c517a62006-12-06 20:33:29 -08002996 x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07002997
Andrew Mortona737b3e2006-03-22 00:08:11 -08002998 /* cache_grow can reenable interrupts, then ac could change. */
Pekka Enberg9a2dba4b2006-02-01 03:05:49 -08002999 ac = cpu_cache_get(cachep);
David Rientjes51cd8e62012-08-28 19:57:21 -07003000 node = numa_mem_id();
Mel Gorman072bb0a2012-07-31 16:43:58 -07003001
3002 /* no objects in sight? abort */
3003 if (!x && (ac->avail == 0 || force_refill))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 return NULL;
3005
Andrew Mortona737b3e2006-03-22 00:08:11 -08003006 if (!ac->avail) /* objects refilled by interrupt? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 goto retry;
3008 }
3009 ac->touched = 1;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003010
3011 return ac_get_obj(cachep, ac, flags, force_refill);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012}
3013
Andrew Mortona737b3e2006-03-22 00:08:11 -08003014static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
3015 gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016{
3017 might_sleep_if(flags & __GFP_WAIT);
3018#if DEBUG
3019 kmem_flagcheck(cachep, flags);
3020#endif
3021}
3022
3023#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003024static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003025 gfp_t flags, void *objp, unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003027 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 return objp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003029 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003031 if ((cachep->size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003032 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003033 cachep->size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 else
3035 check_poison_obj(cachep, objp);
3036#else
3037 check_poison_obj(cachep, objp);
3038#endif
3039 poison_obj(cachep, objp, POISON_INUSE);
3040 }
3041 if (cachep->flags & SLAB_STORE_USER)
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003042 *dbg_userword(cachep, objp) = (void *)caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043
3044 if (cachep->flags & SLAB_RED_ZONE) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003045 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
3046 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
3047 slab_error(cachep, "double free, or memory outside"
3048 " object was overwritten");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003049 printk(KERN_ERR
David Woodhouseb46b8f12007-05-08 00:22:59 -07003050 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08003051 objp, *dbg_redzone1(cachep, objp),
3052 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053 }
3054 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
3055 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
3056 }
Al Viro871751e2006-03-25 03:06:39 -08003057#ifdef CONFIG_DEBUG_SLAB_LEAK
3058 {
3059 struct slab *slabp;
3060 unsigned objnr;
3061
Joonsoo Kim56f295e2013-10-24 10:07:43 +09003062 slabp = virt_to_slab(objp);
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003063 objnr = (unsigned)(objp - slabp->s_mem) / cachep->size;
Al Viro871751e2006-03-25 03:06:39 -08003064 slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE;
3065 }
3066#endif
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003067 objp += obj_offset(cachep);
Christoph Lameter4f104932007-05-06 14:50:17 -07003068 if (cachep->ctor && cachep->flags & SLAB_POISON)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003069 cachep->ctor(objp);
Tetsuo Handa7ea466f2011-07-21 09:42:45 +09003070 if (ARCH_SLAB_MINALIGN &&
3071 ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003072 printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
Hugh Dickinsc2251502011-07-11 13:35:08 -07003073 objp, (int)ARCH_SLAB_MINALIGN);
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003074 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 return objp;
3076}
3077#else
3078#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
3079#endif
3080
Akinobu Mita773ff602008-12-23 19:37:01 +09003081static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags)
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003082{
Christoph Lameter9b030cb2012-09-05 00:20:33 +00003083 if (cachep == kmem_cache)
Akinobu Mita773ff602008-12-23 19:37:01 +09003084 return false;
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003085
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003086 return should_failslab(cachep->object_size, flags, cachep->flags);
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003087}
3088
Pekka Enberg343e0d72006-02-01 03:05:50 -08003089static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003091 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 struct array_cache *ac;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003093 bool force_refill = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
Alok N Kataria5c382302005-09-27 21:45:46 -07003095 check_irq_off();
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003096
Pekka Enberg9a2dba4b2006-02-01 03:05:49 -08003097 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 if (likely(ac->avail)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 ac->touched = 1;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003100 objp = ac_get_obj(cachep, ac, flags, false);
3101
J. R. Okajimaddbf2e82009-12-02 16:55:50 +09003102 /*
Mel Gorman072bb0a2012-07-31 16:43:58 -07003103 * Allow for the possibility all avail objects are not allowed
3104 * by the current flags
J. R. Okajimaddbf2e82009-12-02 16:55:50 +09003105 */
Mel Gorman072bb0a2012-07-31 16:43:58 -07003106 if (objp) {
3107 STATS_INC_ALLOCHIT(cachep);
3108 goto out;
3109 }
3110 force_refill = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 }
Mel Gorman072bb0a2012-07-31 16:43:58 -07003112
3113 STATS_INC_ALLOCMISS(cachep);
3114 objp = cache_alloc_refill(cachep, flags, force_refill);
3115 /*
3116 * the 'ac' may be updated by cache_alloc_refill(),
3117 * and kmemleak_erase() requires its correct value.
3118 */
3119 ac = cpu_cache_get(cachep);
3120
3121out:
Catalin Marinasd5cff632009-06-11 13:22:40 +01003122 /*
3123 * To avoid a false negative, if an object that is in one of the
3124 * per-CPU caches is leaked, we need to make sure kmemleak doesn't
3125 * treat the array pointers as a reference to the object.
3126 */
J. R. Okajimaf3d8b532009-12-02 16:55:49 +09003127 if (objp)
3128 kmemleak_erase(&ac->entry[ac->avail]);
Alok N Kataria5c382302005-09-27 21:45:46 -07003129 return objp;
3130}
3131
Christoph Lametere498be72005-09-09 13:03:32 -07003132#ifdef CONFIG_NUMA
3133/*
Paul Jacksonb2455392006-03-24 03:16:12 -08003134 * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
Paul Jacksonc61afb12006-03-24 03:16:08 -08003135 *
3136 * If we are in_interrupt, then process context, including cpusets and
3137 * mempolicy, may not apply and should not be used for allocation policy.
3138 */
3139static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
3140{
3141 int nid_alloc, nid_here;
3142
Christoph Lameter765c4502006-09-27 01:50:08 -07003143 if (in_interrupt() || (flags & __GFP_THISNODE))
Paul Jacksonc61afb12006-03-24 03:16:08 -08003144 return NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003145 nid_alloc = nid_here = numa_mem_id();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003146 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
Jack Steiner6adef3e2010-05-26 14:42:49 -07003147 nid_alloc = cpuset_slab_spread_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003148 else if (current->mempolicy)
Andi Kleene7b691b2012-06-09 02:40:03 -07003149 nid_alloc = slab_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003150 if (nid_alloc != nid_here)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003151 return ____cache_alloc_node(cachep, flags, nid_alloc);
Paul Jacksonc61afb12006-03-24 03:16:08 -08003152 return NULL;
3153}
3154
3155/*
Christoph Lameter765c4502006-09-27 01:50:08 -07003156 * Fallback function if there was no memory available and no objects on a
Christoph Lameter3c517a62006-12-06 20:33:29 -08003157 * certain node and fall back is permitted. First we scan all the
Christoph Lameter6a673682013-01-10 19:14:19 +00003158 * available node for available objects. If that fails then we
Christoph Lameter3c517a62006-12-06 20:33:29 -08003159 * perform an allocation without specifying a node. This allows the page
3160 * allocator to do its reclaim / fallback magic. We then insert the
3161 * slab into the proper nodelist and then allocate from it.
Christoph Lameter765c4502006-09-27 01:50:08 -07003162 */
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003163static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
Christoph Lameter765c4502006-09-27 01:50:08 -07003164{
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003165 struct zonelist *zonelist;
3166 gfp_t local_flags;
Mel Gormandd1a2392008-04-28 02:12:17 -07003167 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07003168 struct zone *zone;
3169 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003170 void *obj = NULL;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003171 int nid;
Mel Gormancc9a6c82012-03-21 16:34:11 -07003172 unsigned int cpuset_mems_cookie;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003173
3174 if (flags & __GFP_THISNODE)
3175 return NULL;
3176
Christoph Lameter6cb06222007-10-16 01:25:41 -07003177 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Christoph Lameter765c4502006-09-27 01:50:08 -07003178
Mel Gormancc9a6c82012-03-21 16:34:11 -07003179retry_cpuset:
3180 cpuset_mems_cookie = get_mems_allowed();
Andi Kleene7b691b2012-06-09 02:40:03 -07003181 zonelist = node_zonelist(slab_node(), flags);
Mel Gormancc9a6c82012-03-21 16:34:11 -07003182
Christoph Lameter3c517a62006-12-06 20:33:29 -08003183retry:
3184 /*
3185 * Look through allowed nodes for objects available
3186 * from existing per node queues.
3187 */
Mel Gorman54a6eb52008-04-28 02:12:16 -07003188 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
3189 nid = zone_to_nid(zone);
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003190
Mel Gorman54a6eb52008-04-28 02:12:16 -07003191 if (cpuset_zone_allowed_hardwall(zone, flags) &&
Christoph Lameter6a673682013-01-10 19:14:19 +00003192 cache->node[nid] &&
3193 cache->node[nid]->free_objects) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003194 obj = ____cache_alloc_node(cache,
3195 flags | GFP_THISNODE, nid);
Christoph Lameter481c5342008-06-21 16:46:35 -07003196 if (obj)
3197 break;
3198 }
Christoph Lameter3c517a62006-12-06 20:33:29 -08003199 }
3200
Christoph Lametercfce6602007-05-06 14:50:17 -07003201 if (!obj) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003202 /*
3203 * This allocation will be performed within the constraints
3204 * of the current cpuset / memory policy requirements.
3205 * We may trigger various forms of reclaim on the allowed
3206 * set and go into memory reserves if necessary.
3207 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003208 struct page *page;
3209
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003210 if (local_flags & __GFP_WAIT)
3211 local_irq_enable();
3212 kmem_flagcheck(cache, flags);
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003213 page = kmem_getpages(cache, local_flags, numa_mem_id());
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003214 if (local_flags & __GFP_WAIT)
3215 local_irq_disable();
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003216 if (page) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003217 /*
3218 * Insert into the appropriate per node queues
3219 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003220 nid = page_to_nid(page);
3221 if (cache_grow(cache, flags, nid, page)) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003222 obj = ____cache_alloc_node(cache,
3223 flags | GFP_THISNODE, nid);
3224 if (!obj)
3225 /*
3226 * Another processor may allocate the
3227 * objects in the slab since we are
3228 * not holding any locks.
3229 */
3230 goto retry;
3231 } else {
Hugh Dickinsb6a60452007-01-05 16:36:36 -08003232 /* cache_grow already freed obj */
Christoph Lameter3c517a62006-12-06 20:33:29 -08003233 obj = NULL;
3234 }
3235 }
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003236 }
Mel Gormancc9a6c82012-03-21 16:34:11 -07003237
3238 if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !obj))
3239 goto retry_cpuset;
Christoph Lameter765c4502006-09-27 01:50:08 -07003240 return obj;
3241}
3242
3243/*
Christoph Lametere498be72005-09-09 13:03:32 -07003244 * A interface to enable slab creation on nodeid
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003246static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003247 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07003248{
3249 struct list_head *entry;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003250 struct slab *slabp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003251 struct kmem_cache_node *n;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003252 void *obj;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003253 int x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254
Aaron Tomlin14e50c62013-04-26 16:15:34 +01003255 VM_BUG_ON(nodeid > num_online_nodes());
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003256 n = cachep->node[nodeid];
3257 BUG_ON(!n);
Christoph Lametere498be72005-09-09 13:03:32 -07003258
Andrew Mortona737b3e2006-03-22 00:08:11 -08003259retry:
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003260 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003261 spin_lock(&n->list_lock);
3262 entry = n->slabs_partial.next;
3263 if (entry == &n->slabs_partial) {
3264 n->free_touched = 1;
3265 entry = n->slabs_free.next;
3266 if (entry == &n->slabs_free)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003267 goto must_grow;
3268 }
Christoph Lametere498be72005-09-09 13:03:32 -07003269
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003270 slabp = list_entry(entry, struct slab, list);
3271 check_spinlock_acquired_node(cachep, nodeid);
3272 check_slabp(cachep, slabp);
Christoph Lametere498be72005-09-09 13:03:32 -07003273
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003274 STATS_INC_NODEALLOCS(cachep);
3275 STATS_INC_ACTIVE(cachep);
3276 STATS_SET_HIGH(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003277
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003278 BUG_ON(slabp->inuse == cachep->num);
Christoph Lametere498be72005-09-09 13:03:32 -07003279
Matthew Dobson78d382d2006-02-01 03:05:47 -08003280 obj = slab_get_obj(cachep, slabp, nodeid);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003281 check_slabp(cachep, slabp);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003282 n->free_objects--;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003283 /* move slabp to correct slabp list: */
3284 list_del(&slabp->list);
Christoph Lametere498be72005-09-09 13:03:32 -07003285
Andrew Mortona737b3e2006-03-22 00:08:11 -08003286 if (slabp->free == BUFCTL_END)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003287 list_add(&slabp->list, &n->slabs_full);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003288 else
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003289 list_add(&slabp->list, &n->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07003290
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003291 spin_unlock(&n->list_lock);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003292 goto done;
Christoph Lametere498be72005-09-09 13:03:32 -07003293
Andrew Mortona737b3e2006-03-22 00:08:11 -08003294must_grow:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003295 spin_unlock(&n->list_lock);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003296 x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
Christoph Lameter765c4502006-09-27 01:50:08 -07003297 if (x)
3298 goto retry;
Christoph Lametere498be72005-09-09 13:03:32 -07003299
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003300 return fallback_alloc(cachep, flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003301
Andrew Mortona737b3e2006-03-22 00:08:11 -08003302done:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003303 return obj;
Christoph Lametere498be72005-09-09 13:03:32 -07003304}
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003305
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003306static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003307slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003308 unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003309{
3310 unsigned long save_flags;
3311 void *ptr;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003312 int slab_node = numa_mem_id();
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003313
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003314 flags &= gfp_allowed_mask;
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003315
Nick Piggincf40bd12009-01-21 08:12:39 +01003316 lockdep_trace_alloc(flags);
3317
Akinobu Mita773ff602008-12-23 19:37:01 +09003318 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003319 return NULL;
3320
Glauber Costad79923f2012-12-18 14:22:48 -08003321 cachep = memcg_kmem_get_cache(cachep, flags);
3322
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003323 cache_alloc_debugcheck_before(cachep, flags);
3324 local_irq_save(save_flags);
3325
Andrew Mortoneacbbae2011-07-28 13:59:49 -07003326 if (nodeid == NUMA_NO_NODE)
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003327 nodeid = slab_node;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003328
Christoph Lameter6a673682013-01-10 19:14:19 +00003329 if (unlikely(!cachep->node[nodeid])) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003330 /* Node not bootstrapped yet */
3331 ptr = fallback_alloc(cachep, flags);
3332 goto out;
3333 }
3334
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003335 if (nodeid == slab_node) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003336 /*
3337 * Use the locally cached objects if possible.
3338 * However ____cache_alloc does not allow fallback
3339 * to other nodes. It may fail while we still have
3340 * objects on other nodes available.
3341 */
3342 ptr = ____cache_alloc(cachep, flags);
3343 if (ptr)
3344 goto out;
3345 }
3346 /* ___cache_alloc_node can fall back to other nodes */
3347 ptr = ____cache_alloc_node(cachep, flags, nodeid);
3348 out:
3349 local_irq_restore(save_flags);
3350 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003351 kmemleak_alloc_recursive(ptr, cachep->object_size, 1, cachep->flags,
Catalin Marinasd5cff632009-06-11 13:22:40 +01003352 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003353
Pekka Enbergc175eea2008-05-09 20:35:53 +02003354 if (likely(ptr))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003355 kmemcheck_slab_alloc(cachep, flags, ptr, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003356
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003357 if (unlikely((flags & __GFP_ZERO) && ptr))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003358 memset(ptr, 0, cachep->object_size);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003359
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003360 return ptr;
3361}
3362
3363static __always_inline void *
3364__do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
3365{
3366 void *objp;
3367
3368 if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
3369 objp = alternate_node_alloc(cache, flags);
3370 if (objp)
3371 goto out;
3372 }
3373 objp = ____cache_alloc(cache, flags);
3374
3375 /*
3376 * We may just have run out of memory on the local node.
3377 * ____cache_alloc_node() knows how to locate memory on other nodes
3378 */
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003379 if (!objp)
3380 objp = ____cache_alloc_node(cache, flags, numa_mem_id());
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003381
3382 out:
3383 return objp;
3384}
3385#else
3386
3387static __always_inline void *
3388__do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
3389{
3390 return ____cache_alloc(cachep, flags);
3391}
3392
3393#endif /* CONFIG_NUMA */
3394
3395static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003396slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003397{
3398 unsigned long save_flags;
3399 void *objp;
3400
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003401 flags &= gfp_allowed_mask;
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003402
Nick Piggincf40bd12009-01-21 08:12:39 +01003403 lockdep_trace_alloc(flags);
3404
Akinobu Mita773ff602008-12-23 19:37:01 +09003405 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003406 return NULL;
3407
Glauber Costad79923f2012-12-18 14:22:48 -08003408 cachep = memcg_kmem_get_cache(cachep, flags);
3409
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003410 cache_alloc_debugcheck_before(cachep, flags);
3411 local_irq_save(save_flags);
3412 objp = __do_cache_alloc(cachep, flags);
3413 local_irq_restore(save_flags);
3414 objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003415 kmemleak_alloc_recursive(objp, cachep->object_size, 1, cachep->flags,
Catalin Marinasd5cff632009-06-11 13:22:40 +01003416 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003417 prefetchw(objp);
3418
Pekka Enbergc175eea2008-05-09 20:35:53 +02003419 if (likely(objp))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003420 kmemcheck_slab_alloc(cachep, flags, objp, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003421
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003422 if (unlikely((flags & __GFP_ZERO) && objp))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003423 memset(objp, 0, cachep->object_size);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003424
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003425 return objp;
3426}
Christoph Lametere498be72005-09-09 13:03:32 -07003427
3428/*
3429 * Caller needs to acquire correct kmem_list's list_lock
3430 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003431static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003432 int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433{
3434 int i;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003435 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
3437 for (i = 0; i < nr_objects; i++) {
Mel Gorman072bb0a2012-07-31 16:43:58 -07003438 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
Mel Gorman072bb0a2012-07-31 16:43:58 -07003441 clear_obj_pfmemalloc(&objpp[i]);
3442 objp = objpp[i];
3443
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003444 slabp = virt_to_slab(objp);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003445 n = cachep->node[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446 list_del(&slabp->list);
Christoph Lameterff694162005-09-22 21:44:02 -07003447 check_spinlock_acquired_node(cachep, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 check_slabp(cachep, slabp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08003449 slab_put_obj(cachep, slabp, objp, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 STATS_DEC_ACTIVE(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003451 n->free_objects++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 check_slabp(cachep, slabp);
3453
3454 /* fixup slab chains */
3455 if (slabp->inuse == 0) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003456 if (n->free_objects > n->free_limit) {
3457 n->free_objects -= cachep->num;
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07003458 /* No need to drop any previously held
3459 * lock here, even if we have a off-slab slab
3460 * descriptor it is guaranteed to come from
3461 * a different cache, refer to comments before
3462 * alloc_slabmgmt.
3463 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 slab_destroy(cachep, slabp);
3465 } else {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003466 list_add(&slabp->list, &n->slabs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 }
3468 } else {
3469 /* Unconditionally move a slab to the end of the
3470 * partial list on free - maximum time for the
3471 * other objects to be freed, too.
3472 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003473 list_add_tail(&slabp->list, &n->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 }
3475 }
3476}
3477
Pekka Enberg343e0d72006-02-01 03:05:50 -08003478static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479{
3480 int batchcount;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003481 struct kmem_cache_node *n;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003482 int node = numa_mem_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
3484 batchcount = ac->batchcount;
3485#if DEBUG
3486 BUG_ON(!batchcount || batchcount > ac->avail);
3487#endif
3488 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003489 n = cachep->node[node];
3490 spin_lock(&n->list_lock);
3491 if (n->shared) {
3492 struct array_cache *shared_array = n->shared;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003493 int max = shared_array->limit - shared_array->avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 if (max) {
3495 if (batchcount > max)
3496 batchcount = max;
Christoph Lametere498be72005-09-09 13:03:32 -07003497 memcpy(&(shared_array->entry[shared_array->avail]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003498 ac->entry, sizeof(void *) * batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 shared_array->avail += batchcount;
3500 goto free_done;
3501 }
3502 }
3503
Christoph Lameterff694162005-09-22 21:44:02 -07003504 free_block(cachep, ac->entry, batchcount, node);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003505free_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506#if STATS
3507 {
3508 int i = 0;
3509 struct list_head *p;
3510
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003511 p = n->slabs_free.next;
3512 while (p != &(n->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 struct slab *slabp;
3514
3515 slabp = list_entry(p, struct slab, list);
3516 BUG_ON(slabp->inuse);
3517
3518 i++;
3519 p = p->next;
3520 }
3521 STATS_SET_FREEABLE(cachep, i);
3522 }
3523#endif
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003524 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 ac->avail -= batchcount;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003526 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527}
3528
3529/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08003530 * Release an obj back to its cache. If the obj has a constructed state, it must
3531 * be in this state _before_ it is released. Called with disabled ints.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532 */
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003533static inline void __cache_free(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003534 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535{
Pekka Enberg9a2dba4b2006-02-01 03:05:49 -08003536 struct array_cache *ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
3538 check_irq_off();
Catalin Marinasd5cff632009-06-11 13:22:40 +01003539 kmemleak_free_recursive(objp, cachep->flags);
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003540 objp = cache_free_debugcheck(cachep, objp, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003542 kmemcheck_slab_free(cachep, objp, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003543
Siddha, Suresh B1807a1a2007-08-22 14:01:49 -07003544 /*
3545 * Skip calling cache_free_alien() when the platform is not numa.
3546 * This will avoid cache misses that happen while accessing slabp (which
3547 * is per page memory reference) to get nodeid. Instead use a global
3548 * variable to skip the call, which is mostly likely to be present in
3549 * the cache.
3550 */
Mel Gormanb6e68bc2009-06-16 15:32:16 -07003551 if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07003552 return;
Christoph Lametere498be72005-09-09 13:03:32 -07003553
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 if (likely(ac->avail < ac->limit)) {
3555 STATS_INC_FREEHIT(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 } else {
3557 STATS_INC_FREEMISS(cachep);
3558 cache_flusharray(cachep, ac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 }
Zhao Jin42c8c992011-08-27 00:26:17 +08003560
Mel Gorman072bb0a2012-07-31 16:43:58 -07003561 ac_put_obj(cachep, ac, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562}
3563
3564/**
3565 * kmem_cache_alloc - Allocate an object
3566 * @cachep: The cache to allocate from.
3567 * @flags: See kmalloc().
3568 *
3569 * Allocate an object from this cache. The flags are only relevant
3570 * if the cache has no available objects.
3571 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003572void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003574 void *ret = slab_alloc(cachep, flags, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003575
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003576 trace_kmem_cache_alloc(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003577 cachep->object_size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003578
3579 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580}
3581EXPORT_SYMBOL(kmem_cache_alloc);
3582
Li Zefan0f24f122009-12-11 15:45:30 +08003583#ifdef CONFIG_TRACING
Steven Rostedt85beb582010-11-24 16:23:34 -05003584void *
Ezequiel Garcia40521472012-09-08 17:47:56 -03003585kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003586{
Steven Rostedt85beb582010-11-24 16:23:34 -05003587 void *ret;
3588
Ezequiel Garcia48356302012-09-08 17:47:57 -03003589 ret = slab_alloc(cachep, flags, _RET_IP_);
Steven Rostedt85beb582010-11-24 16:23:34 -05003590
3591 trace_kmalloc(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003592 size, cachep->size, flags);
Steven Rostedt85beb582010-11-24 16:23:34 -05003593 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003594}
Steven Rostedt85beb582010-11-24 16:23:34 -05003595EXPORT_SYMBOL(kmem_cache_alloc_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003596#endif
3597
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598#ifdef CONFIG_NUMA
Zhouping Liud0d04b72013-05-16 11:36:23 +08003599/**
3600 * kmem_cache_alloc_node - Allocate an object on the specified node
3601 * @cachep: The cache to allocate from.
3602 * @flags: See kmalloc().
3603 * @nodeid: node number of the target node.
3604 *
3605 * Identical to kmem_cache_alloc but it will allocate memory on the given
3606 * node, which can improve the performance for cpu bound structures.
3607 *
3608 * Fallback to other node is possible if __GFP_THISNODE is not set.
3609 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003610void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
3611{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003612 void *ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003613
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003614 trace_kmem_cache_alloc_node(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003615 cachep->object_size, cachep->size,
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003616 flags, nodeid);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003617
3618 return ret;
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003619}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620EXPORT_SYMBOL(kmem_cache_alloc_node);
3621
Li Zefan0f24f122009-12-11 15:45:30 +08003622#ifdef CONFIG_TRACING
Ezequiel Garcia40521472012-09-08 17:47:56 -03003623void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
Steven Rostedt85beb582010-11-24 16:23:34 -05003624 gfp_t flags,
Ezequiel Garcia40521472012-09-08 17:47:56 -03003625 int nodeid,
3626 size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003627{
Steven Rostedt85beb582010-11-24 16:23:34 -05003628 void *ret;
3629
Ezequiel Garcia592f4142012-09-25 08:07:08 -03003630 ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003631
Steven Rostedt85beb582010-11-24 16:23:34 -05003632 trace_kmalloc_node(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003633 size, cachep->size,
Steven Rostedt85beb582010-11-24 16:23:34 -05003634 flags, nodeid);
3635 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003636}
Steven Rostedt85beb582010-11-24 16:23:34 -05003637EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003638#endif
3639
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003640static __always_inline void *
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003641__do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller)
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003642{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003643 struct kmem_cache *cachep;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003644
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003645 cachep = kmalloc_slab(size, flags);
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003646 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3647 return cachep;
Ezequiel Garcia40521472012-09-08 17:47:56 -03003648 return kmem_cache_alloc_node_trace(cachep, flags, node, size);
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003649}
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003650
Li Zefan0bb38a52009-12-11 15:45:50 +08003651#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003652void *__kmalloc_node(size_t size, gfp_t flags, int node)
3653{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003654 return __do_kmalloc_node(size, flags, node, _RET_IP_);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003655}
Christoph Hellwigdbe5e692006-09-25 23:31:36 -07003656EXPORT_SYMBOL(__kmalloc_node);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003657
3658void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003659 int node, unsigned long caller)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003660{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003661 return __do_kmalloc_node(size, flags, node, caller);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003662}
3663EXPORT_SYMBOL(__kmalloc_node_track_caller);
3664#else
3665void *__kmalloc_node(size_t size, gfp_t flags, int node)
3666{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003667 return __do_kmalloc_node(size, flags, node, 0);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003668}
3669EXPORT_SYMBOL(__kmalloc_node);
Li Zefan0bb38a52009-12-11 15:45:50 +08003670#endif /* CONFIG_DEBUG_SLAB || CONFIG_TRACING */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003671#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672
3673/**
Paul Drynoff800590f2006-06-23 02:03:48 -07003674 * __do_kmalloc - allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 * @size: how many bytes of memory are required.
Paul Drynoff800590f2006-06-23 02:03:48 -07003676 * @flags: the type of memory to allocate (see kmalloc).
Randy Dunlap911851e2006-03-22 00:08:14 -08003677 * @caller: function caller for debug tracking of the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 */
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003679static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003680 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003682 struct kmem_cache *cachep;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003683 void *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003685 /* If you want to save a few bytes .text space: replace
3686 * __ with kmem_.
3687 * Then kmalloc uses the uninlined functions instead of the inline
3688 * functions.
3689 */
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003690 cachep = kmalloc_slab(size, flags);
Linus Torvaldsa5c96d82007-07-19 13:17:15 -07003691 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3692 return cachep;
Ezequiel Garcia48356302012-09-08 17:47:57 -03003693 ret = slab_alloc(cachep, flags, caller);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003694
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003695 trace_kmalloc(caller, ret,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003696 size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003697
3698 return ret;
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003699}
3700
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003701
Li Zefan0bb38a52009-12-11 15:45:50 +08003702#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003703void *__kmalloc(size_t size, gfp_t flags)
3704{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003705 return __do_kmalloc(size, flags, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706}
3707EXPORT_SYMBOL(__kmalloc);
3708
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003709void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003710{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003711 return __do_kmalloc(size, flags, caller);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003712}
3713EXPORT_SYMBOL(__kmalloc_track_caller);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003714
3715#else
3716void *__kmalloc(size_t size, gfp_t flags)
3717{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003718 return __do_kmalloc(size, flags, 0);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003719}
3720EXPORT_SYMBOL(__kmalloc);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003721#endif
3722
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723/**
3724 * kmem_cache_free - Deallocate an object
3725 * @cachep: The cache the allocation was from.
3726 * @objp: The previously allocated object.
3727 *
3728 * Free an object which was previously allocated from this
3729 * cache.
3730 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003731void kmem_cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732{
3733 unsigned long flags;
Glauber Costab9ce5ef2012-12-18 14:22:46 -08003734 cachep = cache_from_obj(cachep, objp);
3735 if (!cachep)
3736 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737
3738 local_irq_save(flags);
Feng Tangd97d4762012-07-02 14:29:10 +08003739 debug_check_no_locks_freed(objp, cachep->object_size);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07003740 if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003741 debug_check_no_obj_freed(objp, cachep->object_size);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003742 __cache_free(cachep, objp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 local_irq_restore(flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003744
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003745 trace_kmem_cache_free(_RET_IP_, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746}
3747EXPORT_SYMBOL(kmem_cache_free);
3748
3749/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 * kfree - free previously allocated memory
3751 * @objp: pointer returned by kmalloc.
3752 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07003753 * If @objp is NULL, no operation is performed.
3754 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 * Don't free memory not originally allocated by kmalloc()
3756 * or you will run into trouble.
3757 */
3758void kfree(const void *objp)
3759{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003760 struct kmem_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 unsigned long flags;
3762
Pekka Enberg2121db72009-03-25 11:05:57 +02003763 trace_kfree(_RET_IP_, objp);
3764
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003765 if (unlikely(ZERO_OR_NULL_PTR(objp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 return;
3767 local_irq_save(flags);
3768 kfree_debugcheck(objp);
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003769 c = virt_to_cache(objp);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003770 debug_check_no_locks_freed(objp, c->object_size);
3771
3772 debug_check_no_obj_freed(objp, c->object_size);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003773 __cache_free(c, (void *)objp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 local_irq_restore(flags);
3775}
3776EXPORT_SYMBOL(kfree);
3777
Christoph Lametere498be72005-09-09 13:03:32 -07003778/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003779 * This initializes kmem_cache_node or resizes various caches for all nodes.
Christoph Lametere498be72005-09-09 13:03:32 -07003780 */
Pekka Enberg83b519e2009-06-10 19:40:04 +03003781static int alloc_kmemlist(struct kmem_cache *cachep, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -07003782{
3783 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003784 struct kmem_cache_node *n;
Christoph Lametercafeb022006-03-25 03:06:46 -08003785 struct array_cache *new_shared;
Paul Menage3395ee02006-12-06 20:32:16 -08003786 struct array_cache **new_alien = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003787
Mel Gorman9c09a952008-01-24 05:49:54 -08003788 for_each_online_node(node) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003789
Paul Menage3395ee02006-12-06 20:32:16 -08003790 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03003791 new_alien = alloc_alien_cache(node, cachep->limit, gfp);
Paul Menage3395ee02006-12-06 20:32:16 -08003792 if (!new_alien)
3793 goto fail;
3794 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003795
Eric Dumazet63109842007-05-06 14:49:28 -07003796 new_shared = NULL;
3797 if (cachep->shared) {
3798 new_shared = alloc_arraycache(node,
Christoph Lameter0718dc22006-03-25 03:06:47 -08003799 cachep->shared*cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003800 0xbaadf00d, gfp);
Eric Dumazet63109842007-05-06 14:49:28 -07003801 if (!new_shared) {
3802 free_alien_cache(new_alien);
3803 goto fail;
3804 }
Christoph Lameter0718dc22006-03-25 03:06:47 -08003805 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003806
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003807 n = cachep->node[node];
3808 if (n) {
3809 struct array_cache *shared = n->shared;
Christoph Lametercafeb022006-03-25 03:06:46 -08003810
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003811 spin_lock_irq(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07003812
Christoph Lametercafeb022006-03-25 03:06:46 -08003813 if (shared)
Christoph Lameter0718dc22006-03-25 03:06:47 -08003814 free_block(cachep, shared->entry,
3815 shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07003816
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003817 n->shared = new_shared;
3818 if (!n->alien) {
3819 n->alien = new_alien;
Christoph Lametere498be72005-09-09 13:03:32 -07003820 new_alien = NULL;
3821 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003822 n->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003823 cachep->batchcount + cachep->num;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003824 spin_unlock_irq(&n->list_lock);
Christoph Lametercafeb022006-03-25 03:06:46 -08003825 kfree(shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003826 free_alien_cache(new_alien);
3827 continue;
3828 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003829 n = kmalloc_node(sizeof(struct kmem_cache_node), gfp, node);
3830 if (!n) {
Christoph Lameter0718dc22006-03-25 03:06:47 -08003831 free_alien_cache(new_alien);
3832 kfree(new_shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003833 goto fail;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003834 }
Christoph Lametere498be72005-09-09 13:03:32 -07003835
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003836 kmem_cache_node_init(n);
3837 n->next_reap = jiffies + REAPTIMEOUT_LIST3 +
Andrew Mortona737b3e2006-03-22 00:08:11 -08003838 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003839 n->shared = new_shared;
3840 n->alien = new_alien;
3841 n->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003842 cachep->batchcount + cachep->num;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003843 cachep->node[node] = n;
Christoph Lametere498be72005-09-09 13:03:32 -07003844 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003845 return 0;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003846
Andrew Mortona737b3e2006-03-22 00:08:11 -08003847fail:
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003848 if (!cachep->list.next) {
Christoph Lameter0718dc22006-03-25 03:06:47 -08003849 /* Cache is not active yet. Roll back what we did */
3850 node--;
3851 while (node >= 0) {
Christoph Lameter6a673682013-01-10 19:14:19 +00003852 if (cachep->node[node]) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003853 n = cachep->node[node];
Christoph Lameter0718dc22006-03-25 03:06:47 -08003854
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003855 kfree(n->shared);
3856 free_alien_cache(n->alien);
3857 kfree(n);
Christoph Lameter6a673682013-01-10 19:14:19 +00003858 cachep->node[node] = NULL;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003859 }
3860 node--;
3861 }
3862 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003863 return -ENOMEM;
Christoph Lametere498be72005-09-09 13:03:32 -07003864}
3865
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866struct ccupdate_struct {
Pekka Enberg343e0d72006-02-01 03:05:50 -08003867 struct kmem_cache *cachep;
Eric Dumazetacfe7d72011-07-25 08:55:42 +02003868 struct array_cache *new[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869};
3870
3871static void do_ccupdate_local(void *info)
3872{
Andrew Mortona737b3e2006-03-22 00:08:11 -08003873 struct ccupdate_struct *new = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 struct array_cache *old;
3875
3876 check_irq_off();
Pekka Enberg9a2dba4b2006-02-01 03:05:49 -08003877 old = cpu_cache_get(new->cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003878
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
3880 new->new[smp_processor_id()] = old;
3881}
3882
Christoph Lameter18004c52012-07-06 15:25:12 -05003883/* Always called with the slab_mutex held */
Glauber Costa943a4512012-12-18 14:23:03 -08003884static int __do_tune_cpucache(struct kmem_cache *cachep, int limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003885 int batchcount, int shared, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886{
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003887 struct ccupdate_struct *new;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003888 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
Eric Dumazetacfe7d72011-07-25 08:55:42 +02003890 new = kzalloc(sizeof(*new) + nr_cpu_ids * sizeof(struct array_cache *),
3891 gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003892 if (!new)
3893 return -ENOMEM;
3894
Christoph Lametere498be72005-09-09 13:03:32 -07003895 for_each_online_cpu(i) {
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003896 new->new[i] = alloc_arraycache(cpu_to_mem(i), limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003897 batchcount, gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003898 if (!new->new[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003899 for (i--; i >= 0; i--)
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003900 kfree(new->new[i]);
3901 kfree(new);
Christoph Lametere498be72005-09-09 13:03:32 -07003902 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 }
3904 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003905 new->cachep = cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906
Jens Axboe15c8b6c2008-05-09 09:39:44 +02003907 on_each_cpu(do_ccupdate_local, (void *)new, 1);
Christoph Lametere498be72005-09-09 13:03:32 -07003908
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 check_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 cachep->batchcount = batchcount;
3911 cachep->limit = limit;
Christoph Lametere498be72005-09-09 13:03:32 -07003912 cachep->shared = shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
Christoph Lametere498be72005-09-09 13:03:32 -07003914 for_each_online_cpu(i) {
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003915 struct array_cache *ccold = new->new[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 if (!ccold)
3917 continue;
Christoph Lameter6a673682013-01-10 19:14:19 +00003918 spin_lock_irq(&cachep->node[cpu_to_mem(i)]->list_lock);
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003919 free_block(cachep, ccold->entry, ccold->avail, cpu_to_mem(i));
Christoph Lameter6a673682013-01-10 19:14:19 +00003920 spin_unlock_irq(&cachep->node[cpu_to_mem(i)]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 kfree(ccold);
3922 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003923 kfree(new);
Pekka Enberg83b519e2009-06-10 19:40:04 +03003924 return alloc_kmemlist(cachep, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925}
3926
Glauber Costa943a4512012-12-18 14:23:03 -08003927static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
3928 int batchcount, int shared, gfp_t gfp)
3929{
3930 int ret;
3931 struct kmem_cache *c = NULL;
3932 int i = 0;
3933
3934 ret = __do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
3935
3936 if (slab_state < FULL)
3937 return ret;
3938
3939 if ((ret < 0) || !is_root_cache(cachep))
3940 return ret;
3941
Glauber Costaebe945c2012-12-18 14:23:10 -08003942 VM_BUG_ON(!mutex_is_locked(&slab_mutex));
Glauber Costa943a4512012-12-18 14:23:03 -08003943 for_each_memcg_cache_index(i) {
3944 c = cache_from_memcg(cachep, i);
3945 if (c)
3946 /* return value determined by the parent cache only */
3947 __do_tune_cpucache(c, limit, batchcount, shared, gfp);
3948 }
3949
3950 return ret;
3951}
3952
Christoph Lameter18004c52012-07-06 15:25:12 -05003953/* Called with slab_mutex held always */
Pekka Enberg83b519e2009-06-10 19:40:04 +03003954static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955{
3956 int err;
Glauber Costa943a4512012-12-18 14:23:03 -08003957 int limit = 0;
3958 int shared = 0;
3959 int batchcount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960
Glauber Costa943a4512012-12-18 14:23:03 -08003961 if (!is_root_cache(cachep)) {
3962 struct kmem_cache *root = memcg_root_cache(cachep);
3963 limit = root->limit;
3964 shared = root->shared;
3965 batchcount = root->batchcount;
3966 }
3967
3968 if (limit && shared && batchcount)
3969 goto skip_setup;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003970 /*
3971 * The head array serves three purposes:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 * - create a LIFO ordering, i.e. return objects that are cache-warm
3973 * - reduce the number of spinlock operations.
Andrew Mortona737b3e2006-03-22 00:08:11 -08003974 * - reduce the number of linked list operations on the slab and
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 * bufctl chains: array operations are cheaper.
3976 * The numbers are guessed, we should auto-tune as described by
3977 * Bonwick.
3978 */
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003979 if (cachep->size > 131072)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 limit = 1;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003981 else if (cachep->size > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 limit = 8;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003983 else if (cachep->size > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 limit = 24;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003985 else if (cachep->size > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 limit = 54;
3987 else
3988 limit = 120;
3989
Andrew Mortona737b3e2006-03-22 00:08:11 -08003990 /*
3991 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 * allocation behaviour: Most allocs on one cpu, most free operations
3993 * on another cpu. For these cases, an efficient object passing between
3994 * cpus is necessary. This is provided by a shared array. The array
3995 * replaces Bonwick's magazine layer.
3996 * On uniprocessor, it's functionally equivalent (but less efficient)
3997 * to a larger limit. Thus disabled by default.
3998 */
3999 shared = 0;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004000 if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 shared = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002
4003#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08004004 /*
4005 * With debugging enabled, large batchcount lead to excessively long
4006 * periods with disabled local interrupts. Limit the batchcount
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 */
4008 if (limit > 32)
4009 limit = 32;
4010#endif
Glauber Costa943a4512012-12-18 14:23:03 -08004011 batchcount = (limit + 1) / 2;
4012skip_setup:
4013 err = do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 if (err)
4015 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004016 cachep->name, -err);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07004017 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018}
4019
Christoph Lameter1b552532006-03-22 00:09:07 -08004020/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004021 * Drain an array if it contains any elements taking the node lock only if
4022 * necessary. Note that the node listlock also protects the array_cache
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004023 * if drain_array() is used on the shared array.
Christoph Lameter1b552532006-03-22 00:09:07 -08004024 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004025static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
Christoph Lameter1b552532006-03-22 00:09:07 -08004026 struct array_cache *ac, int force, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027{
4028 int tofree;
4029
Christoph Lameter1b552532006-03-22 00:09:07 -08004030 if (!ac || !ac->avail)
4031 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 if (ac->touched && !force) {
4033 ac->touched = 0;
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004034 } else {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004035 spin_lock_irq(&n->list_lock);
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004036 if (ac->avail) {
4037 tofree = force ? ac->avail : (ac->limit + 4) / 5;
4038 if (tofree > ac->avail)
4039 tofree = (ac->avail + 1) / 2;
4040 free_block(cachep, ac->entry, tofree, node);
4041 ac->avail -= tofree;
4042 memmove(ac->entry, &(ac->entry[tofree]),
4043 sizeof(void *) * ac->avail);
4044 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004045 spin_unlock_irq(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 }
4047}
4048
4049/**
4050 * cache_reap - Reclaim memory from caches.
Randy Dunlap05fb6bf2007-02-28 20:12:13 -08004051 * @w: work descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 *
4053 * Called from workqueue/eventd every few seconds.
4054 * Purpose:
4055 * - clear the per-cpu caches for this CPU.
4056 * - return freeable pages to the main free memory pool.
4057 *
Andrew Mortona737b3e2006-03-22 00:08:11 -08004058 * If we cannot acquire the cache chain mutex then just give up - we'll try
4059 * again on the next iteration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004061static void cache_reap(struct work_struct *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062{
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004063 struct kmem_cache *searchp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004064 struct kmem_cache_node *n;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07004065 int node = numa_mem_id();
Jean Delvarebf6aede2009-04-02 16:56:54 -07004066 struct delayed_work *work = to_delayed_work(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067
Christoph Lameter18004c52012-07-06 15:25:12 -05004068 if (!mutex_trylock(&slab_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 /* Give up. Setup the next iteration. */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004070 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071
Christoph Lameter18004c52012-07-06 15:25:12 -05004072 list_for_each_entry(searchp, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 check_irq_on();
4074
Christoph Lameter35386e32006-03-22 00:09:05 -08004075 /*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004076 * We only take the node lock if absolutely necessary and we
Christoph Lameter35386e32006-03-22 00:09:05 -08004077 * have established with reasonable certainty that
4078 * we can do some work if the lock was obtained.
4079 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004080 n = searchp->node[node];
Christoph Lameter35386e32006-03-22 00:09:05 -08004081
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004082 reap_alien(searchp, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004084 drain_array(searchp, n, cpu_cache_get(searchp), 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085
Christoph Lameter35386e32006-03-22 00:09:05 -08004086 /*
4087 * These are racy checks but it does not matter
4088 * if we skip one check or scan twice.
4089 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004090 if (time_after(n->next_reap, jiffies))
Christoph Lameter35386e32006-03-22 00:09:05 -08004091 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004093 n->next_reap = jiffies + REAPTIMEOUT_LIST3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004095 drain_array(searchp, n, n->shared, 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004097 if (n->free_touched)
4098 n->free_touched = 0;
Christoph Lametered11d9e2006-06-30 01:55:45 -07004099 else {
4100 int freed;
4101
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004102 freed = drain_freelist(searchp, n, (n->free_limit +
Christoph Lametered11d9e2006-06-30 01:55:45 -07004103 5 * searchp->num - 1) / (5 * searchp->num));
4104 STATS_ADD_REAPED(searchp, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105 }
Christoph Lameter35386e32006-03-22 00:09:05 -08004106next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 cond_resched();
4108 }
4109 check_irq_on();
Christoph Lameter18004c52012-07-06 15:25:12 -05004110 mutex_unlock(&slab_mutex);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004111 next_reap_node();
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004112out:
Andrew Mortona737b3e2006-03-22 00:08:11 -08004113 /* Set up the next iteration */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004114 schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_CPUC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115}
4116
Linus Torvalds158a9622008-01-02 13:04:48 -08004117#ifdef CONFIG_SLABINFO
Glauber Costa0d7561c2012-10-19 18:20:27 +04004118void get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004120 struct slab *slabp;
4121 unsigned long active_objs;
4122 unsigned long num_objs;
4123 unsigned long active_slabs = 0;
4124 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004125 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 char *error = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07004127 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004128 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 active_objs = 0;
4131 num_slabs = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004132 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004133 n = cachep->node[node];
4134 if (!n)
Christoph Lametere498be72005-09-09 13:03:32 -07004135 continue;
4136
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004137 check_irq_on();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004138 spin_lock_irq(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07004139
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004140 list_for_each_entry(slabp, &n->slabs_full, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004141 if (slabp->inuse != cachep->num && !error)
4142 error = "slabs_full accounting error";
4143 active_objs += cachep->num;
4144 active_slabs++;
4145 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004146 list_for_each_entry(slabp, &n->slabs_partial, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004147 if (slabp->inuse == cachep->num && !error)
4148 error = "slabs_partial inuse accounting error";
4149 if (!slabp->inuse && !error)
4150 error = "slabs_partial/inuse accounting error";
4151 active_objs += slabp->inuse;
4152 active_slabs++;
4153 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004154 list_for_each_entry(slabp, &n->slabs_free, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004155 if (slabp->inuse && !error)
4156 error = "slabs_free/inuse accounting error";
4157 num_slabs++;
4158 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004159 free_objects += n->free_objects;
4160 if (n->shared)
4161 shared_avail += n->shared->avail;
Christoph Lametere498be72005-09-09 13:03:32 -07004162
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004163 spin_unlock_irq(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004165 num_slabs += active_slabs;
4166 num_objs = num_slabs * cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07004167 if (num_objs - active_objs != free_objects && !error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 error = "free_objects accounting error";
4169
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004170 name = cachep->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 if (error)
4172 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
4173
Glauber Costa0d7561c2012-10-19 18:20:27 +04004174 sinfo->active_objs = active_objs;
4175 sinfo->num_objs = num_objs;
4176 sinfo->active_slabs = active_slabs;
4177 sinfo->num_slabs = num_slabs;
4178 sinfo->shared_avail = shared_avail;
4179 sinfo->limit = cachep->limit;
4180 sinfo->batchcount = cachep->batchcount;
4181 sinfo->shared = cachep->shared;
4182 sinfo->objects_per_slab = cachep->num;
4183 sinfo->cache_order = cachep->gfporder;
4184}
4185
4186void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *cachep)
4187{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188#if STATS
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004189 { /* node stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 unsigned long high = cachep->high_mark;
4191 unsigned long allocs = cachep->num_allocations;
4192 unsigned long grown = cachep->grown;
4193 unsigned long reaped = cachep->reaped;
4194 unsigned long errors = cachep->errors;
4195 unsigned long max_freeable = cachep->max_freeable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196 unsigned long node_allocs = cachep->node_allocs;
Christoph Lametere498be72005-09-09 13:03:32 -07004197 unsigned long node_frees = cachep->node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004198 unsigned long overflows = cachep->node_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199
Joe Perchese92dd4f2010-03-26 19:27:58 -07004200 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu "
4201 "%4lu %4lu %4lu %4lu %4lu",
4202 allocs, high, grown,
4203 reaped, errors, max_freeable, node_allocs,
4204 node_frees, overflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 }
4206 /* cpu stats */
4207 {
4208 unsigned long allochit = atomic_read(&cachep->allochit);
4209 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
4210 unsigned long freehit = atomic_read(&cachep->freehit);
4211 unsigned long freemiss = atomic_read(&cachep->freemiss);
4212
4213 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004214 allochit, allocmiss, freehit, freemiss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 }
4216#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217}
4218
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219#define MAX_SLABINFO_WRITE 128
4220/**
4221 * slabinfo_write - Tuning for the slab allocator
4222 * @file: unused
4223 * @buffer: user buffer
4224 * @count: data length
4225 * @ppos: unused
4226 */
Glauber Costab7454ad2012-10-19 18:20:25 +04004227ssize_t slabinfo_write(struct file *file, const char __user *buffer,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004228 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004230 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 int limit, batchcount, shared, res;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004232 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004233
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 if (count > MAX_SLABINFO_WRITE)
4235 return -EINVAL;
4236 if (copy_from_user(&kbuf, buffer, count))
4237 return -EFAULT;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004238 kbuf[MAX_SLABINFO_WRITE] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
4240 tmp = strchr(kbuf, ' ');
4241 if (!tmp)
4242 return -EINVAL;
4243 *tmp = '\0';
4244 tmp++;
4245 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
4246 return -EINVAL;
4247
4248 /* Find the cache in the chain of caches. */
Christoph Lameter18004c52012-07-06 15:25:12 -05004249 mutex_lock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 res = -EINVAL;
Christoph Lameter18004c52012-07-06 15:25:12 -05004251 list_for_each_entry(cachep, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 if (!strcmp(cachep->name, kbuf)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08004253 if (limit < 1 || batchcount < 1 ||
4254 batchcount > limit || shared < 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07004255 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07004257 res = do_tune_cpucache(cachep, limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03004258 batchcount, shared,
4259 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 }
4261 break;
4262 }
4263 }
Christoph Lameter18004c52012-07-06 15:25:12 -05004264 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 if (res >= 0)
4266 res = count;
4267 return res;
4268}
Al Viro871751e2006-03-25 03:06:39 -08004269
4270#ifdef CONFIG_DEBUG_SLAB_LEAK
4271
4272static void *leaks_start(struct seq_file *m, loff_t *pos)
4273{
Christoph Lameter18004c52012-07-06 15:25:12 -05004274 mutex_lock(&slab_mutex);
4275 return seq_list_start(&slab_caches, *pos);
Al Viro871751e2006-03-25 03:06:39 -08004276}
4277
4278static inline int add_caller(unsigned long *n, unsigned long v)
4279{
4280 unsigned long *p;
4281 int l;
4282 if (!v)
4283 return 1;
4284 l = n[1];
4285 p = n + 2;
4286 while (l) {
4287 int i = l/2;
4288 unsigned long *q = p + 2 * i;
4289 if (*q == v) {
4290 q[1]++;
4291 return 1;
4292 }
4293 if (*q > v) {
4294 l = i;
4295 } else {
4296 p = q + 2;
4297 l -= i + 1;
4298 }
4299 }
4300 if (++n[1] == n[0])
4301 return 0;
4302 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4303 p[0] = v;
4304 p[1] = 1;
4305 return 1;
4306}
4307
4308static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
4309{
4310 void *p;
4311 int i;
4312 if (n[0] == n[1])
4313 return;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004314 for (i = 0, p = s->s_mem; i < c->num; i++, p += c->size) {
Al Viro871751e2006-03-25 03:06:39 -08004315 if (slab_bufctl(s)[i] != BUFCTL_ACTIVE)
4316 continue;
4317 if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
4318 return;
4319 }
4320}
4321
4322static void show_symbol(struct seq_file *m, unsigned long address)
4323{
4324#ifdef CONFIG_KALLSYMS
Al Viro871751e2006-03-25 03:06:39 -08004325 unsigned long offset, size;
Tejun Heo9281ace2007-07-17 04:03:51 -07004326 char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
Al Viro871751e2006-03-25 03:06:39 -08004327
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004328 if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
Al Viro871751e2006-03-25 03:06:39 -08004329 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004330 if (modname[0])
Al Viro871751e2006-03-25 03:06:39 -08004331 seq_printf(m, " [%s]", modname);
4332 return;
4333 }
4334#endif
4335 seq_printf(m, "%p", (void *)address);
4336}
4337
4338static int leaks_show(struct seq_file *m, void *p)
4339{
Thierry Reding0672aa72012-06-22 19:42:49 +02004340 struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);
Al Viro871751e2006-03-25 03:06:39 -08004341 struct slab *slabp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004342 struct kmem_cache_node *n;
Al Viro871751e2006-03-25 03:06:39 -08004343 const char *name;
Christoph Lameterdb845062013-02-05 18:45:23 +00004344 unsigned long *x = m->private;
Al Viro871751e2006-03-25 03:06:39 -08004345 int node;
4346 int i;
4347
4348 if (!(cachep->flags & SLAB_STORE_USER))
4349 return 0;
4350 if (!(cachep->flags & SLAB_RED_ZONE))
4351 return 0;
4352
4353 /* OK, we can do it */
4354
Christoph Lameterdb845062013-02-05 18:45:23 +00004355 x[1] = 0;
Al Viro871751e2006-03-25 03:06:39 -08004356
4357 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004358 n = cachep->node[node];
4359 if (!n)
Al Viro871751e2006-03-25 03:06:39 -08004360 continue;
4361
4362 check_irq_on();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004363 spin_lock_irq(&n->list_lock);
Al Viro871751e2006-03-25 03:06:39 -08004364
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004365 list_for_each_entry(slabp, &n->slabs_full, list)
Christoph Lameterdb845062013-02-05 18:45:23 +00004366 handle_slab(x, cachep, slabp);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004367 list_for_each_entry(slabp, &n->slabs_partial, list)
Christoph Lameterdb845062013-02-05 18:45:23 +00004368 handle_slab(x, cachep, slabp);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004369 spin_unlock_irq(&n->list_lock);
Al Viro871751e2006-03-25 03:06:39 -08004370 }
4371 name = cachep->name;
Christoph Lameterdb845062013-02-05 18:45:23 +00004372 if (x[0] == x[1]) {
Al Viro871751e2006-03-25 03:06:39 -08004373 /* Increase the buffer size */
Christoph Lameter18004c52012-07-06 15:25:12 -05004374 mutex_unlock(&slab_mutex);
Christoph Lameterdb845062013-02-05 18:45:23 +00004375 m->private = kzalloc(x[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
Al Viro871751e2006-03-25 03:06:39 -08004376 if (!m->private) {
4377 /* Too bad, we are really out */
Christoph Lameterdb845062013-02-05 18:45:23 +00004378 m->private = x;
Christoph Lameter18004c52012-07-06 15:25:12 -05004379 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004380 return -ENOMEM;
4381 }
Christoph Lameterdb845062013-02-05 18:45:23 +00004382 *(unsigned long *)m->private = x[0] * 2;
4383 kfree(x);
Christoph Lameter18004c52012-07-06 15:25:12 -05004384 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004385 /* Now make sure this entry will be retried */
4386 m->count = m->size;
4387 return 0;
4388 }
Christoph Lameterdb845062013-02-05 18:45:23 +00004389 for (i = 0; i < x[1]; i++) {
4390 seq_printf(m, "%s: %lu ", name, x[2*i+3]);
4391 show_symbol(m, x[2*i+2]);
Al Viro871751e2006-03-25 03:06:39 -08004392 seq_putc(m, '\n');
4393 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004394
Al Viro871751e2006-03-25 03:06:39 -08004395 return 0;
4396}
4397
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004398static const struct seq_operations slabstats_op = {
Al Viro871751e2006-03-25 03:06:39 -08004399 .start = leaks_start,
Wanpeng Li276a2432013-07-08 08:08:28 +08004400 .next = slab_next,
4401 .stop = slab_stop,
Al Viro871751e2006-03-25 03:06:39 -08004402 .show = leaks_show,
4403};
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004404
4405static int slabstats_open(struct inode *inode, struct file *file)
4406{
4407 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
4408 int ret = -ENOMEM;
4409 if (n) {
4410 ret = seq_open(file, &slabstats_op);
4411 if (!ret) {
4412 struct seq_file *m = file->private_data;
4413 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
4414 m->private = n;
4415 n = NULL;
4416 }
4417 kfree(n);
4418 }
4419 return ret;
4420}
4421
4422static const struct file_operations proc_slabstats_operations = {
4423 .open = slabstats_open,
4424 .read = seq_read,
4425 .llseek = seq_lseek,
4426 .release = seq_release_private,
4427};
Al Viro871751e2006-03-25 03:06:39 -08004428#endif
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004429
4430static int __init slab_proc_init(void)
4431{
4432#ifdef CONFIG_DEBUG_SLAB_LEAK
4433 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
4434#endif
4435 return 0;
4436}
4437module_init(slab_proc_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438#endif
4439
Manfred Spraul00e145b2005-09-03 15:55:07 -07004440/**
4441 * ksize - get the actual amount of memory allocated for a given object
4442 * @objp: Pointer to the object
4443 *
4444 * kmalloc may internally round up allocations and return more memory
4445 * than requested. ksize() can be used to determine the actual amount of
4446 * memory allocated. The caller may use this additional memory, even though
4447 * a smaller amount of memory was initially specified with the kmalloc call.
4448 * The caller must guarantee that objp points to a valid object previously
4449 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4450 * must not be freed during the duration of the call.
4451 */
Pekka Enbergfd76bab2007-05-06 14:48:40 -07004452size_t ksize(const void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453{
Christoph Lameteref8b4522007-10-16 01:24:46 -07004454 BUG_ON(!objp);
4455 if (unlikely(objp == ZERO_SIZE_PTR))
Manfred Spraul00e145b2005-09-03 15:55:07 -07004456 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
Christoph Lameter8c138bc2012-06-13 10:24:58 -05004458 return virt_to_cache(objp)->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459}
Kirill A. Shutemovb1aabec2009-02-10 15:21:44 +02004460EXPORT_SYMBOL(ksize);