blob: 2ec2336a1ffcbc8f8a1c4a034000c5783c72b55b [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/*
Lai Jiangshan5bfe53a2011-03-10 15:22:24 +0800167 * struct slab
168 *
169 * Manages the objs in a slab. Placed either at the beginning of mem allocated
170 * for a slab, or allocated from an general cache.
171 * Slabs are chained into three list: fully used, partial, fully free slabs.
172 */
173struct slab {
Joonsoo Kim68126702013-10-24 10:07:42 +0900174 struct {
175 struct list_head list;
176 void *s_mem; /* including colour offset */
Joonsoo Kim106a74e2013-10-24 10:07:48 +0900177 unsigned int active; /* num of objs active in slab */
Lai Jiangshan5bfe53a2011-03-10 15:22:24 +0800178 };
179};
180
181/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 * struct array_cache
183 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * Purpose:
185 * - LIFO ordering, to hand out cache-warm objects from _alloc
186 * - reduce the number of linked list operations
187 * - reduce spinlock operations
188 *
189 * The limit is stored in the per-cpu structure to reduce the data cache
190 * footprint.
191 *
192 */
193struct array_cache {
194 unsigned int avail;
195 unsigned int limit;
196 unsigned int batchcount;
197 unsigned int touched;
Christoph Lametere498be72005-09-09 13:03:32 -0700198 spinlock_t lock;
Robert P. J. Daybda5b652007-10-16 23:30:05 -0700199 void *entry[]; /*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800200 * Must have this definition in here for the proper
201 * alignment of array_cache. Also simplifies accessing
202 * the entries.
Mel Gorman072bb0a2012-07-31 16:43:58 -0700203 *
204 * Entries should not be directly dereferenced as
205 * entries belonging to slabs marked pfmemalloc will
206 * have the lower bits set SLAB_OBJ_PFMEMALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -0800207 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
Mel Gorman072bb0a2012-07-31 16:43:58 -0700210#define SLAB_OBJ_PFMEMALLOC 1
211static inline bool is_obj_pfmemalloc(void *objp)
212{
213 return (unsigned long)objp & SLAB_OBJ_PFMEMALLOC;
214}
215
216static inline void set_obj_pfmemalloc(void **objp)
217{
218 *objp = (void *)((unsigned long)*objp | SLAB_OBJ_PFMEMALLOC);
219 return;
220}
221
222static inline void clear_obj_pfmemalloc(void **objp)
223{
224 *objp = (void *)((unsigned long)*objp & ~SLAB_OBJ_PFMEMALLOC);
225}
226
Andrew Mortona737b3e2006-03-22 00:08:11 -0800227/*
228 * bootstrap: The caches do not work without cpuarrays anymore, but the
229 * cpuarrays are allocated from the generic caches...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 */
231#define BOOT_CPUCACHE_ENTRIES 1
232struct arraycache_init {
233 struct array_cache cache;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800234 void *entries[BOOT_CPUCACHE_ENTRIES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235};
236
237/*
Christoph Lametere498be72005-09-09 13:03:32 -0700238 * Need this for bootstrapping a per node allocator.
239 */
Pekka Enberg556a1692008-01-25 08:20:51 +0200240#define NUM_INIT_LISTS (3 * MAX_NUMNODES)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000241static struct kmem_cache_node __initdata init_kmem_cache_node[NUM_INIT_LISTS];
Christoph Lametere498be72005-09-09 13:03:32 -0700242#define CACHE_CACHE 0
Pekka Enberg556a1692008-01-25 08:20:51 +0200243#define SIZE_AC MAX_NUMNODES
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000244#define SIZE_NODE (2 * MAX_NUMNODES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245
Christoph Lametered11d9e2006-06-30 01:55:45 -0700246static int drain_freelist(struct kmem_cache *cache,
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000247 struct kmem_cache_node *n, int tofree);
Christoph Lametered11d9e2006-06-30 01:55:45 -0700248static void free_block(struct kmem_cache *cachep, void **objpp, int len,
249 int node);
Pekka Enberg83b519e2009-06-10 19:40:04 +0300250static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp);
David Howells65f27f32006-11-22 14:55:48 +0000251static void cache_reap(struct work_struct *unused);
Christoph Lametered11d9e2006-06-30 01:55:45 -0700252
Ingo Molnare0a42722006-06-23 02:03:46 -0700253static int slab_early_init = 1;
254
Christoph Lametere3366012013-01-10 19:14:18 +0000255#define INDEX_AC kmalloc_index(sizeof(struct arraycache_init))
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000256#define INDEX_NODE kmalloc_index(sizeof(struct kmem_cache_node))
Christoph Lametere498be72005-09-09 13:03:32 -0700257
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000258static void kmem_cache_node_init(struct kmem_cache_node *parent)
Christoph Lametere498be72005-09-09 13:03:32 -0700259{
260 INIT_LIST_HEAD(&parent->slabs_full);
261 INIT_LIST_HEAD(&parent->slabs_partial);
262 INIT_LIST_HEAD(&parent->slabs_free);
263 parent->shared = NULL;
264 parent->alien = NULL;
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -0800265 parent->colour_next = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700266 spin_lock_init(&parent->list_lock);
267 parent->free_objects = 0;
268 parent->free_touched = 0;
269}
270
Andrew Mortona737b3e2006-03-22 00:08:11 -0800271#define MAKE_LIST(cachep, listp, slab, nodeid) \
272 do { \
273 INIT_LIST_HEAD(listp); \
Christoph Lameter6a673682013-01-10 19:14:19 +0000274 list_splice(&(cachep->node[nodeid]->slab), listp); \
Christoph Lametere498be72005-09-09 13:03:32 -0700275 } while (0)
276
Andrew Mortona737b3e2006-03-22 00:08:11 -0800277#define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
278 do { \
Christoph Lametere498be72005-09-09 13:03:32 -0700279 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
280 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
281 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
282 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284#define CFLGS_OFF_SLAB (0x80000000UL)
285#define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
286
287#define BATCHREFILL_LIMIT 16
Andrew Mortona737b3e2006-03-22 00:08:11 -0800288/*
289 * Optimization question: fewer reaps means less probability for unnessary
290 * cpucache drain/refill cycles.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 *
Adrian Bunkdc6f3f22005-11-08 16:44:08 +0100292 * OTOH the cpuarrays can contain lots of objects,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * which could lock up otherwise freeable slabs.
294 */
295#define REAPTIMEOUT_CPUC (2*HZ)
296#define REAPTIMEOUT_LIST3 (4*HZ)
297
298#if STATS
299#define STATS_INC_ACTIVE(x) ((x)->num_active++)
300#define STATS_DEC_ACTIVE(x) ((x)->num_active--)
301#define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
302#define STATS_INC_GROWN(x) ((x)->grown++)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700303#define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
Andrew Mortona737b3e2006-03-22 00:08:11 -0800304#define STATS_SET_HIGH(x) \
305 do { \
306 if ((x)->num_active > (x)->high_mark) \
307 (x)->high_mark = (x)->num_active; \
308 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309#define STATS_INC_ERR(x) ((x)->errors++)
310#define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
Christoph Lametere498be72005-09-09 13:03:32 -0700311#define STATS_INC_NODEFREES(x) ((x)->node_frees++)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700312#define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800313#define STATS_SET_FREEABLE(x, i) \
314 do { \
315 if ((x)->max_freeable < i) \
316 (x)->max_freeable = i; \
317 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318#define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
319#define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
320#define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
321#define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
322#else
323#define STATS_INC_ACTIVE(x) do { } while (0)
324#define STATS_DEC_ACTIVE(x) do { } while (0)
325#define STATS_INC_ALLOCED(x) do { } while (0)
326#define STATS_INC_GROWN(x) do { } while (0)
Andi Kleen4e60c862010-08-09 17:19:03 -0700327#define STATS_ADD_REAPED(x,y) do { (void)(y); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328#define STATS_SET_HIGH(x) do { } while (0)
329#define STATS_INC_ERR(x) do { } while (0)
330#define STATS_INC_NODEALLOCS(x) do { } while (0)
Christoph Lametere498be72005-09-09 13:03:32 -0700331#define STATS_INC_NODEFREES(x) do { } while (0)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700332#define STATS_INC_ACOVERFLOW(x) do { } while (0)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800333#define STATS_SET_FREEABLE(x, i) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334#define STATS_INC_ALLOCHIT(x) do { } while (0)
335#define STATS_INC_ALLOCMISS(x) do { } while (0)
336#define STATS_INC_FREEHIT(x) do { } while (0)
337#define STATS_INC_FREEMISS(x) do { } while (0)
338#endif
339
340#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341
Andrew Mortona737b3e2006-03-22 00:08:11 -0800342/*
343 * memory layout of objects:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 * 0 : objp
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800345 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 * the end of an object is aligned with the end of the real
347 * allocation. Catches writes behind the end of the allocation.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800348 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 * redzone word.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800350 * cachep->obj_offset: The real object.
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500351 * cachep->size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
352 * cachep->size - 1* BYTES_PER_WORD: last caller address
Andrew Mortona737b3e2006-03-22 00:08:11 -0800353 * [BYTES_PER_WORD long]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800355static int obj_offset(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800357 return cachep->obj_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
David Woodhouseb46b8f12007-05-08 00:22:59 -0700360static unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
362 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
David Woodhouseb46b8f12007-05-08 00:22:59 -0700363 return (unsigned long long*) (objp + obj_offset(cachep) -
364 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
David Woodhouseb46b8f12007-05-08 00:22:59 -0700367static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368{
369 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
370 if (cachep->flags & SLAB_STORE_USER)
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500371 return (unsigned long long *)(objp + cachep->size -
David Woodhouseb46b8f12007-05-08 00:22:59 -0700372 sizeof(unsigned long long) -
David Woodhouse87a927c2007-07-04 21:26:44 -0400373 REDZONE_ALIGN);
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500374 return (unsigned long long *) (objp + cachep->size -
David Woodhouseb46b8f12007-05-08 00:22:59 -0700375 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
Pekka Enberg343e0d72006-02-01 03:05:50 -0800378static void **dbg_userword(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
380 BUG_ON(!(cachep->flags & SLAB_STORE_USER));
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500381 return (void **)(objp + cachep->size - BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
384#else
385
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800386#define obj_offset(x) 0
David Woodhouseb46b8f12007-05-08 00:22:59 -0700387#define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
388#define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389#define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
390
391#endif
392
393/*
David Rientjes3df1ccc2011-10-18 22:09:28 -0700394 * Do not go above this order unless 0 objects fit into the slab or
395 * overridden on the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 */
David Rientjes543585c2011-10-18 22:09:24 -0700397#define SLAB_MAX_ORDER_HI 1
398#define SLAB_MAX_ORDER_LO 0
399static int slab_max_order = SLAB_MAX_ORDER_LO;
David Rientjes3df1ccc2011-10-18 22:09:28 -0700400static bool slab_max_order_set __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800402static inline struct kmem_cache *virt_to_cache(const void *obj)
403{
Christoph Lameterb49af682007-05-06 14:49:41 -0700404 struct page *page = virt_to_head_page(obj);
Christoph Lameter35026082012-06-13 10:24:56 -0500405 return page->slab_cache;
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800406}
407
408static inline struct slab *virt_to_slab(const void *obj)
409{
Christoph Lameterb49af682007-05-06 14:49:41 -0700410 struct page *page = virt_to_head_page(obj);
Christoph Lameter35026082012-06-13 10:24:56 -0500411
412 VM_BUG_ON(!PageSlab(page));
413 return page->slab_page;
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800414}
415
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800416static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
417 unsigned int idx)
418{
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500419 return slab->s_mem + cache->size * idx;
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800420}
421
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800422/*
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500423 * We want to avoid an expensive divide : (offset / cache->size)
424 * Using the fact that size is a constant for a particular cache,
425 * we can replace (offset / cache->size) by
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800426 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
427 */
428static inline unsigned int obj_to_index(const struct kmem_cache *cache,
429 const struct slab *slab, void *obj)
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800430{
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800431 u32 offset = (obj - slab->s_mem);
432 return reciprocal_divide(offset, cache->reciprocal_buffer_size);
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800433}
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435static struct arraycache_init initarray_generic =
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800436 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438/* internal cache of cache description objs */
Christoph Lameter9b030cb2012-09-05 00:20:33 +0000439static struct kmem_cache kmem_cache_boot = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800440 .batchcount = 1,
441 .limit = BOOT_CPUCACHE_ENTRIES,
442 .shared = 1,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500443 .size = sizeof(struct kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800444 .name = "kmem_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445};
446
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700447#define BAD_ALIEN_MAGIC 0x01020304ul
448
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200449#ifdef CONFIG_LOCKDEP
450
451/*
452 * Slab sometimes uses the kmalloc slabs to store the slab headers
453 * for other slabs "off slab".
454 * The locking for this is tricky in that it nests within the locks
455 * of all other slabs in a few places; to deal with this special
456 * locking we put on-slab caches into a separate lock-class.
457 *
458 * We set lock class for alien array caches which are up during init.
459 * The lock annotation will be lost if all cpus of a node goes down and
460 * then comes back up during hotplug
461 */
462static struct lock_class_key on_slab_l3_key;
463static struct lock_class_key on_slab_alc_key;
464
Peter Zijlstra83835b32011-07-22 15:26:05 +0200465static struct lock_class_key debugobj_l3_key;
466static struct lock_class_key debugobj_alc_key;
467
468static void slab_set_lock_classes(struct kmem_cache *cachep,
469 struct lock_class_key *l3_key, struct lock_class_key *alc_key,
470 int q)
471{
472 struct array_cache **alc;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000473 struct kmem_cache_node *n;
Peter Zijlstra83835b32011-07-22 15:26:05 +0200474 int r;
475
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000476 n = cachep->node[q];
477 if (!n)
Peter Zijlstra83835b32011-07-22 15:26:05 +0200478 return;
479
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000480 lockdep_set_class(&n->list_lock, l3_key);
481 alc = n->alien;
Peter Zijlstra83835b32011-07-22 15:26:05 +0200482 /*
483 * FIXME: This check for BAD_ALIEN_MAGIC
484 * should go away when common slab code is taught to
485 * work even without alien caches.
486 * Currently, non NUMA code returns BAD_ALIEN_MAGIC
487 * for alloc_alien_cache,
488 */
489 if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC)
490 return;
491 for_each_node(r) {
492 if (alc[r])
493 lockdep_set_class(&alc[r]->lock, alc_key);
494 }
495}
496
497static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
498{
499 slab_set_lock_classes(cachep, &debugobj_l3_key, &debugobj_alc_key, node);
500}
501
502static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
503{
504 int node;
505
506 for_each_online_node(node)
507 slab_set_debugobj_lock_classes_node(cachep, node);
508}
509
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200510static void init_node_lock_keys(int q)
511{
Christoph Lametere3366012013-01-10 19:14:18 +0000512 int i;
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200513
Christoph Lameter97d06602012-07-06 15:25:11 -0500514 if (slab_state < UP)
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200515 return;
516
Christoph Lameter0f8f8092013-07-02 12:12:10 -0700517 for (i = 1; i <= KMALLOC_SHIFT_HIGH; i++) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000518 struct kmem_cache_node *n;
Christoph Lametere3366012013-01-10 19:14:18 +0000519 struct kmem_cache *cache = kmalloc_caches[i];
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200520
Christoph Lametere3366012013-01-10 19:14:18 +0000521 if (!cache)
Pekka Enberg00afa752009-12-27 14:33:14 +0200522 continue;
Peter Zijlstra83835b32011-07-22 15:26:05 +0200523
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000524 n = cache->node[q];
525 if (!n || OFF_SLAB(cache))
Christoph Lametere3366012013-01-10 19:14:18 +0000526 continue;
527
528 slab_set_lock_classes(cache, &on_slab_l3_key,
Peter Zijlstra83835b32011-07-22 15:26:05 +0200529 &on_slab_alc_key, q);
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200530 }
531}
532
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800533static void on_slab_lock_classes_node(struct kmem_cache *cachep, int q)
534{
Christoph Lameter6a673682013-01-10 19:14:19 +0000535 if (!cachep->node[q])
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800536 return;
537
538 slab_set_lock_classes(cachep, &on_slab_l3_key,
539 &on_slab_alc_key, q);
540}
541
542static inline void on_slab_lock_classes(struct kmem_cache *cachep)
543{
544 int node;
545
546 VM_BUG_ON(OFF_SLAB(cachep));
547 for_each_node(node)
548 on_slab_lock_classes_node(cachep, node);
549}
550
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200551static inline void init_lock_keys(void)
552{
553 int node;
554
555 for_each_node(node)
556 init_node_lock_keys(node);
557}
558#else
559static void init_node_lock_keys(int q)
560{
561}
562
563static inline void init_lock_keys(void)
564{
565}
Peter Zijlstra83835b32011-07-22 15:26:05 +0200566
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800567static inline void on_slab_lock_classes(struct kmem_cache *cachep)
568{
569}
570
571static inline void on_slab_lock_classes_node(struct kmem_cache *cachep, int node)
572{
573}
574
Peter Zijlstra83835b32011-07-22 15:26:05 +0200575static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
576{
577}
578
579static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
580{
581}
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200582#endif
583
Tejun Heo1871e522009-10-29 22:34:13 +0900584static DEFINE_PER_CPU(struct delayed_work, slab_reap_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Pekka Enberg343e0d72006-02-01 03:05:50 -0800586static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587{
588 return cachep->array[smp_processor_id()];
589}
590
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800591static size_t slab_mgmt_size(size_t nr_objs, size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592{
Joonsoo Kim16025172013-10-24 10:07:46 +0900593 return ALIGN(sizeof(struct slab)+nr_objs*sizeof(unsigned int), align);
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800594}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Andrew Mortona737b3e2006-03-22 00:08:11 -0800596/*
597 * Calculate the number of objects and left-over bytes for a given buffer size.
598 */
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800599static void cache_estimate(unsigned long gfporder, size_t buffer_size,
600 size_t align, int flags, size_t *left_over,
601 unsigned int *num)
602{
603 int nr_objs;
604 size_t mgmt_size;
605 size_t slab_size = PAGE_SIZE << gfporder;
606
607 /*
608 * The slab management structure can be either off the slab or
609 * on it. For the latter case, the memory allocated for a
610 * slab is used for:
611 *
612 * - The struct slab
Joonsoo Kim16025172013-10-24 10:07:46 +0900613 * - One unsigned int for each object
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800614 * - Padding to respect alignment of @align
615 * - @buffer_size bytes for each object
616 *
617 * If the slab management structure is off the slab, then the
618 * alignment will already be calculated into the size. Because
619 * the slabs are all pages aligned, the objects will be at the
620 * correct alignment when allocated.
621 */
622 if (flags & CFLGS_OFF_SLAB) {
623 mgmt_size = 0;
624 nr_objs = slab_size / buffer_size;
625
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800626 } else {
627 /*
628 * Ignore padding for the initial guess. The padding
629 * is at most @align-1 bytes, and @buffer_size is at
630 * least @align. In the worst case, this result will
631 * be one greater than the number of objects that fit
632 * into the memory allocation when taking the padding
633 * into account.
634 */
635 nr_objs = (slab_size - sizeof(struct slab)) /
Joonsoo Kim16025172013-10-24 10:07:46 +0900636 (buffer_size + sizeof(unsigned int));
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800637
638 /*
639 * This calculated number will be either the right
640 * amount, or one greater than what we want.
641 */
642 if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
643 > slab_size)
644 nr_objs--;
645
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800646 mgmt_size = slab_mgmt_size(nr_objs, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800648 *num = nr_objs;
649 *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
Christoph Lameterf28510d2012-09-11 19:49:38 +0000652#if DEBUG
Harvey Harrisond40cee22008-04-30 00:55:07 -0700653#define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Andrew Mortona737b3e2006-03-22 00:08:11 -0800655static void __slab_error(const char *function, struct kmem_cache *cachep,
656 char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657{
658 printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800659 function, cachep->name, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +1030661 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
Christoph Lameterf28510d2012-09-11 19:49:38 +0000663#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Paul Menage3395ee02006-12-06 20:32:16 -0800665/*
666 * By default on NUMA we use alien caches to stage the freeing of
667 * objects allocated from other nodes. This causes massive memory
668 * inefficiencies when using fake NUMA setup to split memory into a
669 * large number of small nodes, so it can be disabled on the command
670 * line
671 */
672
673static int use_alien_caches __read_mostly = 1;
674static int __init noaliencache_setup(char *s)
675{
676 use_alien_caches = 0;
677 return 1;
678}
679__setup("noaliencache", noaliencache_setup);
680
David Rientjes3df1ccc2011-10-18 22:09:28 -0700681static int __init slab_max_order_setup(char *str)
682{
683 get_option(&str, &slab_max_order);
684 slab_max_order = slab_max_order < 0 ? 0 :
685 min(slab_max_order, MAX_ORDER - 1);
686 slab_max_order_set = true;
687
688 return 1;
689}
690__setup("slab_max_order=", slab_max_order_setup);
691
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800692#ifdef CONFIG_NUMA
693/*
694 * Special reaping functions for NUMA systems called from cache_reap().
695 * These take care of doing round robin flushing of alien caches (containing
696 * objects freed on different nodes from which they were allocated) and the
697 * flushing of remote pcps by calling drain_node_pages.
698 */
Tejun Heo1871e522009-10-29 22:34:13 +0900699static DEFINE_PER_CPU(unsigned long, slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800700
701static void init_reap_node(int cpu)
702{
703 int node;
704
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -0700705 node = next_node(cpu_to_mem(cpu), node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800706 if (node == MAX_NUMNODES)
Paul Jackson442295c2006-03-22 00:09:11 -0800707 node = first_node(node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800708
Tejun Heo1871e522009-10-29 22:34:13 +0900709 per_cpu(slab_reap_node, cpu) = node;
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800710}
711
712static void next_reap_node(void)
713{
Christoph Lameter909ea962010-12-08 16:22:55 +0100714 int node = __this_cpu_read(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800715
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800716 node = next_node(node, node_online_map);
717 if (unlikely(node >= MAX_NUMNODES))
718 node = first_node(node_online_map);
Christoph Lameter909ea962010-12-08 16:22:55 +0100719 __this_cpu_write(slab_reap_node, node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800720}
721
722#else
723#define init_reap_node(cpu) do { } while (0)
724#define next_reap_node(void) do { } while (0)
725#endif
726
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727/*
728 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
729 * via the workqueue/eventd.
730 * Add the CPU number into the expiration time to minimize the possibility of
731 * the CPUs getting into lockstep and contending for the global cache chain
732 * lock.
733 */
Paul Gortmaker0db06282013-06-19 14:53:51 -0400734static void start_cpu_timer(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Tejun Heo1871e522009-10-29 22:34:13 +0900736 struct delayed_work *reap_work = &per_cpu(slab_reap_work, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 /*
739 * When this gets called from do_initcalls via cpucache_init(),
740 * init_workqueues() has already run, so keventd will be setup
741 * at that time.
742 */
David Howells52bad642006-11-22 14:54:01 +0000743 if (keventd_up() && reap_work->work.func == NULL) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800744 init_reap_node(cpu);
Tejun Heo203b42f2012-08-21 13:18:23 -0700745 INIT_DEFERRABLE_WORK(reap_work, cache_reap);
Arjan van de Ven2b284212006-12-10 02:21:28 -0800746 schedule_delayed_work_on(cpu, reap_work,
747 __round_jiffies_relative(HZ, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 }
749}
750
Christoph Lametere498be72005-09-09 13:03:32 -0700751static struct array_cache *alloc_arraycache(int node, int entries,
Pekka Enberg83b519e2009-06-10 19:40:04 +0300752 int batchcount, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800754 int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 struct array_cache *nc = NULL;
756
Pekka Enberg83b519e2009-06-10 19:40:04 +0300757 nc = kmalloc_node(memsize, gfp, node);
Catalin Marinasd5cff632009-06-11 13:22:40 +0100758 /*
759 * The array_cache structures contain pointers to free object.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300760 * However, when such objects are allocated or transferred to another
Catalin Marinasd5cff632009-06-11 13:22:40 +0100761 * cache the pointers are not cleared and they could be counted as
762 * valid references during a kmemleak scan. Therefore, kmemleak must
763 * not scan such objects.
764 */
765 kmemleak_no_scan(nc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (nc) {
767 nc->avail = 0;
768 nc->limit = entries;
769 nc->batchcount = batchcount;
770 nc->touched = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700771 spin_lock_init(&nc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773 return nc;
774}
775
Mel Gorman072bb0a2012-07-31 16:43:58 -0700776static inline bool is_slab_pfmemalloc(struct slab *slabp)
777{
778 struct page *page = virt_to_page(slabp->s_mem);
779
780 return PageSlabPfmemalloc(page);
781}
782
783/* Clears pfmemalloc_active if no slabs have pfmalloc set */
784static void recheck_pfmemalloc_active(struct kmem_cache *cachep,
785 struct array_cache *ac)
786{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000787 struct kmem_cache_node *n = cachep->node[numa_mem_id()];
Mel Gorman072bb0a2012-07-31 16:43:58 -0700788 struct slab *slabp;
789 unsigned long flags;
790
791 if (!pfmemalloc_active)
792 return;
793
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000794 spin_lock_irqsave(&n->list_lock, flags);
795 list_for_each_entry(slabp, &n->slabs_full, list)
Mel Gorman072bb0a2012-07-31 16:43:58 -0700796 if (is_slab_pfmemalloc(slabp))
797 goto out;
798
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000799 list_for_each_entry(slabp, &n->slabs_partial, list)
Mel Gorman072bb0a2012-07-31 16:43:58 -0700800 if (is_slab_pfmemalloc(slabp))
801 goto out;
802
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000803 list_for_each_entry(slabp, &n->slabs_free, list)
Mel Gorman072bb0a2012-07-31 16:43:58 -0700804 if (is_slab_pfmemalloc(slabp))
805 goto out;
806
807 pfmemalloc_active = false;
808out:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000809 spin_unlock_irqrestore(&n->list_lock, flags);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700810}
811
Mel Gorman381760e2012-07-31 16:44:30 -0700812static void *__ac_get_obj(struct kmem_cache *cachep, struct array_cache *ac,
Mel Gorman072bb0a2012-07-31 16:43:58 -0700813 gfp_t flags, bool force_refill)
814{
815 int i;
816 void *objp = ac->entry[--ac->avail];
817
818 /* Ensure the caller is allowed to use objects from PFMEMALLOC slab */
819 if (unlikely(is_obj_pfmemalloc(objp))) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000820 struct kmem_cache_node *n;
Mel Gorman072bb0a2012-07-31 16:43:58 -0700821
822 if (gfp_pfmemalloc_allowed(flags)) {
823 clear_obj_pfmemalloc(&objp);
824 return objp;
825 }
826
827 /* The caller cannot use PFMEMALLOC objects, find another one */
Joonsoo Kimd014dc22012-09-17 14:09:06 -0700828 for (i = 0; i < ac->avail; i++) {
Mel Gorman072bb0a2012-07-31 16:43:58 -0700829 /* If a !PFMEMALLOC object is found, swap them */
830 if (!is_obj_pfmemalloc(ac->entry[i])) {
831 objp = ac->entry[i];
832 ac->entry[i] = ac->entry[ac->avail];
833 ac->entry[ac->avail] = objp;
834 return objp;
835 }
836 }
837
838 /*
839 * If there are empty slabs on the slabs_free list and we are
840 * being forced to refill the cache, mark this one !pfmemalloc.
841 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000842 n = cachep->node[numa_mem_id()];
843 if (!list_empty(&n->slabs_free) && force_refill) {
Mel Gorman072bb0a2012-07-31 16:43:58 -0700844 struct slab *slabp = virt_to_slab(objp);
Mel Gorman30c29be2012-09-17 14:09:03 -0700845 ClearPageSlabPfmemalloc(virt_to_head_page(slabp->s_mem));
Mel Gorman072bb0a2012-07-31 16:43:58 -0700846 clear_obj_pfmemalloc(&objp);
847 recheck_pfmemalloc_active(cachep, ac);
848 return objp;
849 }
850
851 /* No !PFMEMALLOC objects available */
852 ac->avail++;
853 objp = NULL;
854 }
855
856 return objp;
857}
858
Mel Gorman381760e2012-07-31 16:44:30 -0700859static inline void *ac_get_obj(struct kmem_cache *cachep,
860 struct array_cache *ac, gfp_t flags, bool force_refill)
861{
862 void *objp;
863
864 if (unlikely(sk_memalloc_socks()))
865 objp = __ac_get_obj(cachep, ac, flags, force_refill);
866 else
867 objp = ac->entry[--ac->avail];
868
869 return objp;
870}
871
872static void *__ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
Mel Gorman072bb0a2012-07-31 16:43:58 -0700873 void *objp)
874{
875 if (unlikely(pfmemalloc_active)) {
876 /* Some pfmemalloc slabs exist, check if this is one */
Joonsoo Kim73293c22013-10-24 10:07:37 +0900877 struct slab *slabp = virt_to_slab(objp);
878 struct page *page = virt_to_head_page(slabp->s_mem);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700879 if (PageSlabPfmemalloc(page))
880 set_obj_pfmemalloc(&objp);
881 }
882
Mel Gorman381760e2012-07-31 16:44:30 -0700883 return objp;
884}
885
886static inline void ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
887 void *objp)
888{
889 if (unlikely(sk_memalloc_socks()))
890 objp = __ac_put_obj(cachep, ac, objp);
891
Mel Gorman072bb0a2012-07-31 16:43:58 -0700892 ac->entry[ac->avail++] = objp;
893}
894
Christoph Lameter3ded1752006-03-25 03:06:44 -0800895/*
896 * Transfer objects in one arraycache to another.
897 * Locking must be handled by the caller.
898 *
899 * Return the number of entries transferred.
900 */
901static int transfer_objects(struct array_cache *to,
902 struct array_cache *from, unsigned int max)
903{
904 /* Figure out how many entries to transfer */
Hagen Paul Pfeifer732eacc2010-10-26 14:22:23 -0700905 int nr = min3(from->avail, max, to->limit - to->avail);
Christoph Lameter3ded1752006-03-25 03:06:44 -0800906
907 if (!nr)
908 return 0;
909
910 memcpy(to->entry + to->avail, from->entry + from->avail -nr,
911 sizeof(void *) *nr);
912
913 from->avail -= nr;
914 to->avail += nr;
Christoph Lameter3ded1752006-03-25 03:06:44 -0800915 return nr;
916}
917
Christoph Lameter765c4502006-09-27 01:50:08 -0700918#ifndef CONFIG_NUMA
919
920#define drain_alien_cache(cachep, alien) do { } while (0)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000921#define reap_alien(cachep, n) do { } while (0)
Christoph Lameter765c4502006-09-27 01:50:08 -0700922
Pekka Enberg83b519e2009-06-10 19:40:04 +0300923static inline struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
Christoph Lameter765c4502006-09-27 01:50:08 -0700924{
925 return (struct array_cache **)BAD_ALIEN_MAGIC;
926}
927
928static inline void free_alien_cache(struct array_cache **ac_ptr)
929{
930}
931
932static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
933{
934 return 0;
935}
936
937static inline void *alternate_node_alloc(struct kmem_cache *cachep,
938 gfp_t flags)
939{
940 return NULL;
941}
942
Christoph Hellwig8b98c162006-12-06 20:32:30 -0800943static inline void *____cache_alloc_node(struct kmem_cache *cachep,
Christoph Lameter765c4502006-09-27 01:50:08 -0700944 gfp_t flags, int nodeid)
945{
946 return NULL;
947}
948
949#else /* CONFIG_NUMA */
950
Christoph Hellwig8b98c162006-12-06 20:32:30 -0800951static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
Paul Jacksonc61afb12006-03-24 03:16:08 -0800952static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
Christoph Lameterdc85da12006-01-18 17:42:36 -0800953
Pekka Enberg83b519e2009-06-10 19:40:04 +0300954static struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -0700955{
956 struct array_cache **ac_ptr;
Christoph Lameter8ef82862007-02-20 13:57:52 -0800957 int memsize = sizeof(void *) * nr_node_ids;
Christoph Lametere498be72005-09-09 13:03:32 -0700958 int i;
959
960 if (limit > 1)
961 limit = 12;
Haicheng Lif3186a92010-01-06 15:25:23 +0800962 ac_ptr = kzalloc_node(memsize, gfp, node);
Christoph Lametere498be72005-09-09 13:03:32 -0700963 if (ac_ptr) {
964 for_each_node(i) {
Haicheng Lif3186a92010-01-06 15:25:23 +0800965 if (i == node || !node_online(i))
Christoph Lametere498be72005-09-09 13:03:32 -0700966 continue;
Pekka Enberg83b519e2009-06-10 19:40:04 +0300967 ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d, gfp);
Christoph Lametere498be72005-09-09 13:03:32 -0700968 if (!ac_ptr[i]) {
Akinobu Mitacc550de2007-11-14 16:58:35 -0800969 for (i--; i >= 0; i--)
Christoph Lametere498be72005-09-09 13:03:32 -0700970 kfree(ac_ptr[i]);
971 kfree(ac_ptr);
972 return NULL;
973 }
974 }
975 }
976 return ac_ptr;
977}
978
Pekka Enberg5295a742006-02-01 03:05:48 -0800979static void free_alien_cache(struct array_cache **ac_ptr)
Christoph Lametere498be72005-09-09 13:03:32 -0700980{
981 int i;
982
983 if (!ac_ptr)
984 return;
Christoph Lametere498be72005-09-09 13:03:32 -0700985 for_each_node(i)
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800986 kfree(ac_ptr[i]);
Christoph Lametere498be72005-09-09 13:03:32 -0700987 kfree(ac_ptr);
988}
989
Pekka Enberg343e0d72006-02-01 03:05:50 -0800990static void __drain_alien_cache(struct kmem_cache *cachep,
Pekka Enberg5295a742006-02-01 03:05:48 -0800991 struct array_cache *ac, int node)
Christoph Lametere498be72005-09-09 13:03:32 -0700992{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000993 struct kmem_cache_node *n = cachep->node[node];
Christoph Lametere498be72005-09-09 13:03:32 -0700994
995 if (ac->avail) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000996 spin_lock(&n->list_lock);
Christoph Lametere00946f2006-03-25 03:06:45 -0800997 /*
998 * Stuff objects into the remote nodes shared array first.
999 * That way we could avoid the overhead of putting the objects
1000 * into the free lists and getting them back later.
1001 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001002 if (n->shared)
1003 transfer_objects(n->shared, ac, ac->limit);
Christoph Lametere00946f2006-03-25 03:06:45 -08001004
Christoph Lameterff694162005-09-22 21:44:02 -07001005 free_block(cachep, ac->entry, ac->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001006 ac->avail = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001007 spin_unlock(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07001008 }
1009}
1010
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001011/*
1012 * Called from cache_reap() to regularly drain alien caches round robin.
1013 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001014static void reap_alien(struct kmem_cache *cachep, struct kmem_cache_node *n)
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001015{
Christoph Lameter909ea962010-12-08 16:22:55 +01001016 int node = __this_cpu_read(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001017
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001018 if (n->alien) {
1019 struct array_cache *ac = n->alien[node];
Christoph Lametere00946f2006-03-25 03:06:45 -08001020
1021 if (ac && ac->avail && spin_trylock_irq(&ac->lock)) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001022 __drain_alien_cache(cachep, ac, node);
1023 spin_unlock_irq(&ac->lock);
1024 }
1025 }
1026}
1027
Andrew Mortona737b3e2006-03-22 00:08:11 -08001028static void drain_alien_cache(struct kmem_cache *cachep,
1029 struct array_cache **alien)
Christoph Lametere498be72005-09-09 13:03:32 -07001030{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001031 int i = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07001032 struct array_cache *ac;
1033 unsigned long flags;
1034
1035 for_each_online_node(i) {
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001036 ac = alien[i];
Christoph Lametere498be72005-09-09 13:03:32 -07001037 if (ac) {
1038 spin_lock_irqsave(&ac->lock, flags);
1039 __drain_alien_cache(cachep, ac, i);
1040 spin_unlock_irqrestore(&ac->lock, flags);
1041 }
1042 }
1043}
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001044
Ingo Molnar873623d2006-07-13 14:44:38 +02001045static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001046{
Joonsoo Kim1ea991b2013-10-24 10:07:40 +09001047 int nodeid = page_to_nid(virt_to_page(objp));
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001048 struct kmem_cache_node *n;
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001049 struct array_cache *alien = NULL;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001050 int node;
1051
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001052 node = numa_mem_id();
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001053
1054 /*
1055 * Make sure we are not freeing a object from another node to the array
1056 * cache on this cpu.
1057 */
Joonsoo Kim1ea991b2013-10-24 10:07:40 +09001058 if (likely(nodeid == node))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001059 return 0;
1060
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001061 n = cachep->node[node];
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001062 STATS_INC_NODEFREES(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001063 if (n->alien && n->alien[nodeid]) {
1064 alien = n->alien[nodeid];
Ingo Molnar873623d2006-07-13 14:44:38 +02001065 spin_lock(&alien->lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001066 if (unlikely(alien->avail == alien->limit)) {
1067 STATS_INC_ACOVERFLOW(cachep);
1068 __drain_alien_cache(cachep, alien, nodeid);
1069 }
Mel Gorman072bb0a2012-07-31 16:43:58 -07001070 ac_put_obj(cachep, alien, objp);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001071 spin_unlock(&alien->lock);
1072 } else {
Christoph Lameter6a673682013-01-10 19:14:19 +00001073 spin_lock(&(cachep->node[nodeid])->list_lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001074 free_block(cachep, &objp, 1, nodeid);
Christoph Lameter6a673682013-01-10 19:14:19 +00001075 spin_unlock(&(cachep->node[nodeid])->list_lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001076 }
1077 return 1;
1078}
Christoph Lametere498be72005-09-09 13:03:32 -07001079#endif
1080
David Rientjes8f9f8d92010-03-27 19:40:47 -07001081/*
Christoph Lameter6a673682013-01-10 19:14:19 +00001082 * Allocates and initializes node for a node on each slab cache, used for
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001083 * either memory or cpu hotplug. If memory is being hot-added, the kmem_cache_node
David Rientjes8f9f8d92010-03-27 19:40:47 -07001084 * will be allocated off-node since memory is not yet online for the new node.
Christoph Lameter6a673682013-01-10 19:14:19 +00001085 * When hotplugging memory or a cpu, existing node are not replaced if
David Rientjes8f9f8d92010-03-27 19:40:47 -07001086 * already in use.
1087 *
Christoph Lameter18004c52012-07-06 15:25:12 -05001088 * Must hold slab_mutex.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001089 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001090static int init_cache_node_node(int node)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001091{
1092 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001093 struct kmem_cache_node *n;
Christoph Lameter6744f082013-01-10 19:12:17 +00001094 const int memsize = sizeof(struct kmem_cache_node);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001095
Christoph Lameter18004c52012-07-06 15:25:12 -05001096 list_for_each_entry(cachep, &slab_caches, list) {
David Rientjes8f9f8d92010-03-27 19:40:47 -07001097 /*
1098 * Set up the size64 kmemlist for cpu before we can
1099 * begin anything. Make sure some other cpu on this
1100 * node has not already allocated this
1101 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001102 if (!cachep->node[node]) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001103 n = kmalloc_node(memsize, GFP_KERNEL, node);
1104 if (!n)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001105 return -ENOMEM;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001106 kmem_cache_node_init(n);
1107 n->next_reap = jiffies + REAPTIMEOUT_LIST3 +
David Rientjes8f9f8d92010-03-27 19:40:47 -07001108 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1109
1110 /*
1111 * The l3s don't come and go as CPUs come and
Christoph Lameter18004c52012-07-06 15:25:12 -05001112 * go. slab_mutex is sufficient
David Rientjes8f9f8d92010-03-27 19:40:47 -07001113 * protection here.
1114 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001115 cachep->node[node] = n;
David Rientjes8f9f8d92010-03-27 19:40:47 -07001116 }
1117
Christoph Lameter6a673682013-01-10 19:14:19 +00001118 spin_lock_irq(&cachep->node[node]->list_lock);
1119 cachep->node[node]->free_limit =
David Rientjes8f9f8d92010-03-27 19:40:47 -07001120 (1 + nr_cpus_node(node)) *
1121 cachep->batchcount + cachep->num;
Christoph Lameter6a673682013-01-10 19:14:19 +00001122 spin_unlock_irq(&cachep->node[node]->list_lock);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001123 }
1124 return 0;
1125}
1126
Wanpeng Li0fa81032013-07-04 08:33:22 +08001127static inline int slabs_tofree(struct kmem_cache *cachep,
1128 struct kmem_cache_node *n)
1129{
1130 return (n->free_objects + cachep->num - 1) / cachep->num;
1131}
1132
Paul Gortmaker0db06282013-06-19 14:53:51 -04001133static void cpuup_canceled(long cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134{
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001135 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001136 struct kmem_cache_node *n = NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001137 int node = cpu_to_mem(cpu);
Rusty Russella70f7302009-03-13 14:49:46 +10301138 const struct cpumask *mask = cpumask_of_node(node);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001139
Christoph Lameter18004c52012-07-06 15:25:12 -05001140 list_for_each_entry(cachep, &slab_caches, list) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001141 struct array_cache *nc;
1142 struct array_cache *shared;
1143 struct array_cache **alien;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001144
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001145 /* cpu is dead; no one can alloc from it. */
1146 nc = cachep->array[cpu];
1147 cachep->array[cpu] = NULL;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001148 n = cachep->node[node];
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001149
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001150 if (!n)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001151 goto free_array_cache;
1152
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001153 spin_lock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001154
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001155 /* Free limit for this kmem_cache_node */
1156 n->free_limit -= cachep->batchcount;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001157 if (nc)
1158 free_block(cachep, nc->entry, nc->avail, node);
1159
Rusty Russell58463c12009-12-17 11:43:12 -06001160 if (!cpumask_empty(mask)) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001161 spin_unlock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001162 goto free_array_cache;
1163 }
1164
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001165 shared = n->shared;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001166 if (shared) {
1167 free_block(cachep, shared->entry,
1168 shared->avail, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001169 n->shared = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001170 }
1171
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001172 alien = n->alien;
1173 n->alien = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001174
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001175 spin_unlock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001176
1177 kfree(shared);
1178 if (alien) {
1179 drain_alien_cache(cachep, alien);
1180 free_alien_cache(alien);
1181 }
1182free_array_cache:
1183 kfree(nc);
1184 }
1185 /*
1186 * In the previous loop, all the objects were freed to
1187 * the respective cache's slabs, now we can go ahead and
1188 * shrink each nodelist to its limit.
1189 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001190 list_for_each_entry(cachep, &slab_caches, list) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001191 n = cachep->node[node];
1192 if (!n)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001193 continue;
Wanpeng Li0fa81032013-07-04 08:33:22 +08001194 drain_freelist(cachep, n, slabs_tofree(cachep, n));
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001195 }
1196}
1197
Paul Gortmaker0db06282013-06-19 14:53:51 -04001198static int cpuup_prepare(long cpu)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001199{
Pekka Enberg343e0d72006-02-01 03:05:50 -08001200 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001201 struct kmem_cache_node *n = NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001202 int node = cpu_to_mem(cpu);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001203 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001205 /*
1206 * We need to do this right in the beginning since
1207 * alloc_arraycache's are going to use this list.
1208 * kmalloc_node allows us to add the slab to the right
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001209 * kmem_cache_node and not this cpu's kmem_cache_node
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001210 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001211 err = init_cache_node_node(node);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001212 if (err < 0)
1213 goto bad;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001214
1215 /*
1216 * Now we can go ahead with allocating the shared arrays and
1217 * array caches
1218 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001219 list_for_each_entry(cachep, &slab_caches, list) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001220 struct array_cache *nc;
1221 struct array_cache *shared = NULL;
1222 struct array_cache **alien = NULL;
1223
1224 nc = alloc_arraycache(node, cachep->limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001225 cachep->batchcount, GFP_KERNEL);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001226 if (!nc)
1227 goto bad;
1228 if (cachep->shared) {
1229 shared = alloc_arraycache(node,
1230 cachep->shared * cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001231 0xbaadf00d, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001232 if (!shared) {
1233 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001234 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001235 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001236 }
1237 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03001238 alien = alloc_alien_cache(node, cachep->limit, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001239 if (!alien) {
1240 kfree(shared);
1241 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001242 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001243 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001244 }
1245 cachep->array[cpu] = nc;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001246 n = cachep->node[node];
1247 BUG_ON(!n);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001248
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001249 spin_lock_irq(&n->list_lock);
1250 if (!n->shared) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001251 /*
1252 * We are serialised from CPU_DEAD or
1253 * CPU_UP_CANCELLED by the cpucontrol lock
1254 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001255 n->shared = shared;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001256 shared = NULL;
1257 }
1258#ifdef CONFIG_NUMA
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001259 if (!n->alien) {
1260 n->alien = alien;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001261 alien = NULL;
1262 }
1263#endif
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001264 spin_unlock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001265 kfree(shared);
1266 free_alien_cache(alien);
Peter Zijlstra83835b32011-07-22 15:26:05 +02001267 if (cachep->flags & SLAB_DEBUG_OBJECTS)
1268 slab_set_debugobj_lock_classes_node(cachep, node);
Glauber Costa6ccfb5b2012-12-18 14:22:31 -08001269 else if (!OFF_SLAB(cachep) &&
1270 !(cachep->flags & SLAB_DESTROY_BY_RCU))
1271 on_slab_lock_classes_node(cachep, node);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001272 }
Pekka Enbergce79ddc2009-11-23 22:01:15 +02001273 init_node_lock_keys(node);
1274
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001275 return 0;
1276bad:
Akinobu Mita12d00f62007-10-18 03:05:11 -07001277 cpuup_canceled(cpu);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001278 return -ENOMEM;
1279}
1280
Paul Gortmaker0db06282013-06-19 14:53:51 -04001281static int cpuup_callback(struct notifier_block *nfb,
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001282 unsigned long action, void *hcpu)
1283{
1284 long cpu = (long)hcpu;
1285 int err = 0;
1286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 switch (action) {
Heiko Carstens38c3bd92007-05-09 02:34:05 -07001288 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001289 case CPU_UP_PREPARE_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001290 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001291 err = cpuup_prepare(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001292 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 break;
1294 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001295 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 start_cpu_timer(cpu);
1297 break;
1298#ifdef CONFIG_HOTPLUG_CPU
Christoph Lameter5830c592007-05-09 02:34:22 -07001299 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001300 case CPU_DOWN_PREPARE_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001301 /*
Christoph Lameter18004c52012-07-06 15:25:12 -05001302 * Shutdown cache reaper. Note that the slab_mutex is
Christoph Lameter5830c592007-05-09 02:34:22 -07001303 * held so that if cache_reap() is invoked it cannot do
1304 * anything expensive but will only modify reap_work
1305 * and reschedule the timer.
1306 */
Tejun Heoafe2c512010-12-14 16:21:17 +01001307 cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu));
Christoph Lameter5830c592007-05-09 02:34:22 -07001308 /* Now the cache_reaper is guaranteed to be not running. */
Tejun Heo1871e522009-10-29 22:34:13 +09001309 per_cpu(slab_reap_work, cpu).work.func = NULL;
Christoph Lameter5830c592007-05-09 02:34:22 -07001310 break;
1311 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001312 case CPU_DOWN_FAILED_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001313 start_cpu_timer(cpu);
1314 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001316 case CPU_DEAD_FROZEN:
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001317 /*
1318 * Even if all the cpus of a node are down, we don't free the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001319 * kmem_cache_node of any cache. This to avoid a race between
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001320 * cpu_down, and a kmalloc allocation from another cpu for
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001321 * memory from the node of the cpu going down. The node
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001322 * structure is usually allocated from kmem_cache_create() and
1323 * gets destroyed at kmem_cache_destroy().
1324 */
Simon Arlott183ff222007-10-20 01:27:18 +02001325 /* fall through */
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08001326#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001328 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001329 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001330 cpuup_canceled(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001331 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 }
Akinobu Mitaeac40682010-05-26 14:43:32 -07001334 return notifier_from_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335}
1336
Paul Gortmaker0db06282013-06-19 14:53:51 -04001337static struct notifier_block cpucache_notifier = {
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001338 &cpuup_callback, NULL, 0
1339};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340
David Rientjes8f9f8d92010-03-27 19:40:47 -07001341#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
1342/*
1343 * Drains freelist for a node on each slab cache, used for memory hot-remove.
1344 * Returns -EBUSY if all objects cannot be drained so that the node is not
1345 * removed.
1346 *
Christoph Lameter18004c52012-07-06 15:25:12 -05001347 * Must hold slab_mutex.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001348 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001349static int __meminit drain_cache_node_node(int node)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001350{
1351 struct kmem_cache *cachep;
1352 int ret = 0;
1353
Christoph Lameter18004c52012-07-06 15:25:12 -05001354 list_for_each_entry(cachep, &slab_caches, list) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001355 struct kmem_cache_node *n;
David Rientjes8f9f8d92010-03-27 19:40:47 -07001356
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001357 n = cachep->node[node];
1358 if (!n)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001359 continue;
1360
Wanpeng Li0fa81032013-07-04 08:33:22 +08001361 drain_freelist(cachep, n, slabs_tofree(cachep, n));
David Rientjes8f9f8d92010-03-27 19:40:47 -07001362
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001363 if (!list_empty(&n->slabs_full) ||
1364 !list_empty(&n->slabs_partial)) {
David Rientjes8f9f8d92010-03-27 19:40:47 -07001365 ret = -EBUSY;
1366 break;
1367 }
1368 }
1369 return ret;
1370}
1371
1372static int __meminit slab_memory_callback(struct notifier_block *self,
1373 unsigned long action, void *arg)
1374{
1375 struct memory_notify *mnb = arg;
1376 int ret = 0;
1377 int nid;
1378
1379 nid = mnb->status_change_nid;
1380 if (nid < 0)
1381 goto out;
1382
1383 switch (action) {
1384 case MEM_GOING_ONLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001385 mutex_lock(&slab_mutex);
Christoph Lameter6a673682013-01-10 19:14:19 +00001386 ret = init_cache_node_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001387 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001388 break;
1389 case MEM_GOING_OFFLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001390 mutex_lock(&slab_mutex);
Christoph Lameter6a673682013-01-10 19:14:19 +00001391 ret = drain_cache_node_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001392 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001393 break;
1394 case MEM_ONLINE:
1395 case MEM_OFFLINE:
1396 case MEM_CANCEL_ONLINE:
1397 case MEM_CANCEL_OFFLINE:
1398 break;
1399 }
1400out:
Prarit Bhargava5fda1bd2011-03-22 16:30:49 -07001401 return notifier_from_errno(ret);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001402}
1403#endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
1404
Christoph Lametere498be72005-09-09 13:03:32 -07001405/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001406 * swap the static kmem_cache_node with kmalloced memory
Christoph Lametere498be72005-09-09 13:03:32 -07001407 */
Christoph Lameter6744f082013-01-10 19:12:17 +00001408static void __init init_list(struct kmem_cache *cachep, struct kmem_cache_node *list,
David Rientjes8f9f8d92010-03-27 19:40:47 -07001409 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07001410{
Christoph Lameter6744f082013-01-10 19:12:17 +00001411 struct kmem_cache_node *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001412
Christoph Lameter6744f082013-01-10 19:12:17 +00001413 ptr = kmalloc_node(sizeof(struct kmem_cache_node), GFP_NOWAIT, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07001414 BUG_ON(!ptr);
1415
Christoph Lameter6744f082013-01-10 19:12:17 +00001416 memcpy(ptr, list, sizeof(struct kmem_cache_node));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001417 /*
1418 * Do not assume that spinlocks can be initialized via memcpy:
1419 */
1420 spin_lock_init(&ptr->list_lock);
1421
Christoph Lametere498be72005-09-09 13:03:32 -07001422 MAKE_ALL_LISTS(cachep, ptr, nodeid);
Christoph Lameter6a673682013-01-10 19:14:19 +00001423 cachep->node[nodeid] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001424}
1425
Andrew Mortona737b3e2006-03-22 00:08:11 -08001426/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001427 * For setting up all the kmem_cache_node for cache whose buffer_size is same as
1428 * size of kmem_cache_node.
Pekka Enberg556a1692008-01-25 08:20:51 +02001429 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001430static void __init set_up_node(struct kmem_cache *cachep, int index)
Pekka Enberg556a1692008-01-25 08:20:51 +02001431{
1432 int node;
1433
1434 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001435 cachep->node[node] = &init_kmem_cache_node[index + node];
Christoph Lameter6a673682013-01-10 19:14:19 +00001436 cachep->node[node]->next_reap = jiffies +
Pekka Enberg556a1692008-01-25 08:20:51 +02001437 REAPTIMEOUT_LIST3 +
1438 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1439 }
1440}
1441
1442/*
Christoph Lameter3c583462012-11-28 16:23:01 +00001443 * The memory after the last cpu cache pointer is used for the
Christoph Lameter6a673682013-01-10 19:14:19 +00001444 * the node pointer.
Christoph Lameter3c583462012-11-28 16:23:01 +00001445 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001446static void setup_node_pointer(struct kmem_cache *cachep)
Christoph Lameter3c583462012-11-28 16:23:01 +00001447{
Christoph Lameter6a673682013-01-10 19:14:19 +00001448 cachep->node = (struct kmem_cache_node **)&cachep->array[nr_cpu_ids];
Christoph Lameter3c583462012-11-28 16:23:01 +00001449}
1450
1451/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08001452 * Initialisation. Called after the page allocator have been initialised and
1453 * before smp_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 */
1455void __init kmem_cache_init(void)
1456{
Christoph Lametere498be72005-09-09 13:03:32 -07001457 int i;
1458
Joonsoo Kim68126702013-10-24 10:07:42 +09001459 BUILD_BUG_ON(sizeof(((struct page *)NULL)->lru) <
1460 sizeof(struct rcu_head));
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001461 kmem_cache = &kmem_cache_boot;
Christoph Lameter6a673682013-01-10 19:14:19 +00001462 setup_node_pointer(kmem_cache);
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001463
Mel Gormanb6e68bc2009-06-16 15:32:16 -07001464 if (num_possible_nodes() == 1)
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001465 use_alien_caches = 0;
1466
Christoph Lameter3c583462012-11-28 16:23:01 +00001467 for (i = 0; i < NUM_INIT_LISTS; i++)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001468 kmem_cache_node_init(&init_kmem_cache_node[i]);
Christoph Lameter3c583462012-11-28 16:23:01 +00001469
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001470 set_up_node(kmem_cache, CACHE_CACHE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 /*
1473 * Fragmentation resistance on low memory - only use bigger
David Rientjes3df1ccc2011-10-18 22:09:28 -07001474 * page orders on machines with more than 32MB of memory if
1475 * not overridden on the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 */
David Rientjes3df1ccc2011-10-18 22:09:28 -07001477 if (!slab_max_order_set && totalram_pages > (32 << 20) >> PAGE_SHIFT)
David Rientjes543585c2011-10-18 22:09:24 -07001478 slab_max_order = SLAB_MAX_ORDER_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 /* Bootstrap is tricky, because several objects are allocated
1481 * from caches that do not exist yet:
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001482 * 1) initialize the kmem_cache cache: it contains the struct
1483 * kmem_cache structures of all caches, except kmem_cache itself:
1484 * kmem_cache is statically allocated.
Christoph Lametere498be72005-09-09 13:03:32 -07001485 * Initially an __init data area is used for the head array and the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001486 * kmem_cache_node structures, it's replaced with a kmalloc allocated
Christoph Lametere498be72005-09-09 13:03:32 -07001487 * array at the end of the bootstrap.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 * 2) Create the first kmalloc cache.
Pekka Enberg343e0d72006-02-01 03:05:50 -08001489 * The struct kmem_cache for the new cache is allocated normally.
Christoph Lametere498be72005-09-09 13:03:32 -07001490 * An __init data area is used for the head array.
1491 * 3) Create the remaining kmalloc caches, with minimally sized
1492 * head arrays.
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001493 * 4) Replace the __init data head arrays for kmem_cache and the first
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 * kmalloc cache with kmalloc allocated arrays.
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001495 * 5) Replace the __init data for kmem_cache_node for kmem_cache and
Christoph Lametere498be72005-09-09 13:03:32 -07001496 * the other cache's with kmalloc allocated memory.
1497 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 */
1499
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001500 /* 1) create the kmem_cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Eric Dumazet8da34302007-05-06 14:49:29 -07001502 /*
Eric Dumazetb56efcf2011-07-20 19:04:23 +02001503 * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
Eric Dumazet8da34302007-05-06 14:49:29 -07001504 */
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001505 create_boot_cache(kmem_cache, "kmem_cache",
1506 offsetof(struct kmem_cache, array[nr_cpu_ids]) +
Christoph Lameter6744f082013-01-10 19:12:17 +00001507 nr_node_ids * sizeof(struct kmem_cache_node *),
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001508 SLAB_HWCACHE_ALIGN);
1509 list_add(&kmem_cache->list, &slab_caches);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510
1511 /* 2+3) create the kmalloc caches */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
Andrew Mortona737b3e2006-03-22 00:08:11 -08001513 /*
1514 * Initialize the caches that provide memory for the array cache and the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001515 * kmem_cache_node structures first. Without this, further allocations will
Andrew Mortona737b3e2006-03-22 00:08:11 -08001516 * bug.
Christoph Lametere498be72005-09-09 13:03:32 -07001517 */
1518
Christoph Lametere3366012013-01-10 19:14:18 +00001519 kmalloc_caches[INDEX_AC] = create_kmalloc_cache("kmalloc-ac",
1520 kmalloc_size(INDEX_AC), ARCH_KMALLOC_FLAGS);
Christoph Lametere498be72005-09-09 13:03:32 -07001521
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001522 if (INDEX_AC != INDEX_NODE)
1523 kmalloc_caches[INDEX_NODE] =
1524 create_kmalloc_cache("kmalloc-node",
1525 kmalloc_size(INDEX_NODE), ARCH_KMALLOC_FLAGS);
Christoph Lametere498be72005-09-09 13:03:32 -07001526
Ingo Molnare0a42722006-06-23 02:03:46 -07001527 slab_early_init = 0;
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 /* 4) Replace the bootstrap head arrays */
1530 {
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001531 struct array_cache *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001532
Pekka Enberg83b519e2009-06-10 19:40:04 +03001533 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001534
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001535 memcpy(ptr, cpu_cache_get(kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001536 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001537 /*
1538 * Do not assume that spinlocks can be initialized via memcpy:
1539 */
1540 spin_lock_init(&ptr->lock);
1541
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001542 kmem_cache->array[smp_processor_id()] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001543
Pekka Enberg83b519e2009-06-10 19:40:04 +03001544 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001545
Christoph Lametere3366012013-01-10 19:14:18 +00001546 BUG_ON(cpu_cache_get(kmalloc_caches[INDEX_AC])
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001547 != &initarray_generic.cache);
Christoph Lametere3366012013-01-10 19:14:18 +00001548 memcpy(ptr, cpu_cache_get(kmalloc_caches[INDEX_AC]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001549 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001550 /*
1551 * Do not assume that spinlocks can be initialized via memcpy:
1552 */
1553 spin_lock_init(&ptr->lock);
1554
Christoph Lametere3366012013-01-10 19:14:18 +00001555 kmalloc_caches[INDEX_AC]->array[smp_processor_id()] = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001557 /* 5) Replace the bootstrap kmem_cache_node */
Christoph Lametere498be72005-09-09 13:03:32 -07001558 {
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001559 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Mel Gorman9c09a952008-01-24 05:49:54 -08001561 for_each_online_node(nid) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001562 init_list(kmem_cache, &init_kmem_cache_node[CACHE_CACHE + nid], nid);
Pekka Enberg556a1692008-01-25 08:20:51 +02001563
Christoph Lametere3366012013-01-10 19:14:18 +00001564 init_list(kmalloc_caches[INDEX_AC],
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001565 &init_kmem_cache_node[SIZE_AC + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001566
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001567 if (INDEX_AC != INDEX_NODE) {
1568 init_list(kmalloc_caches[INDEX_NODE],
1569 &init_kmem_cache_node[SIZE_NODE + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001570 }
1571 }
1572 }
1573
Christoph Lameterf97d5f62013-01-10 19:12:17 +00001574 create_kmalloc_caches(ARCH_KMALLOC_FLAGS);
Pekka Enberg8429db52009-06-12 15:58:59 +03001575}
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001576
Pekka Enberg8429db52009-06-12 15:58:59 +03001577void __init kmem_cache_init_late(void)
1578{
1579 struct kmem_cache *cachep;
1580
Christoph Lameter97d06602012-07-06 15:25:11 -05001581 slab_state = UP;
Peter Zijlstra52cef182011-11-28 21:12:40 +01001582
Pekka Enberg8429db52009-06-12 15:58:59 +03001583 /* 6) resize the head arrays to their final sizes */
Christoph Lameter18004c52012-07-06 15:25:12 -05001584 mutex_lock(&slab_mutex);
1585 list_for_each_entry(cachep, &slab_caches, list)
Pekka Enberg8429db52009-06-12 15:58:59 +03001586 if (enable_cpucache(cachep, GFP_NOWAIT))
1587 BUG();
Christoph Lameter18004c52012-07-06 15:25:12 -05001588 mutex_unlock(&slab_mutex);
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001589
Michael Wang947ca182012-09-05 10:33:18 +08001590 /* Annotate slab for lockdep -- annotate the malloc caches */
1591 init_lock_keys();
1592
Christoph Lameter97d06602012-07-06 15:25:11 -05001593 /* Done! */
1594 slab_state = FULL;
1595
Andrew Mortona737b3e2006-03-22 00:08:11 -08001596 /*
1597 * Register a cpu startup notifier callback that initializes
1598 * cpu_cache_get for all new cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 */
1600 register_cpu_notifier(&cpucache_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
David Rientjes8f9f8d92010-03-27 19:40:47 -07001602#ifdef CONFIG_NUMA
1603 /*
1604 * Register a memory hotplug callback that initializes and frees
Christoph Lameter6a673682013-01-10 19:14:19 +00001605 * node.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001606 */
1607 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
1608#endif
1609
Andrew Mortona737b3e2006-03-22 00:08:11 -08001610 /*
1611 * The reap timers are started later, with a module init call: That part
1612 * of the kernel is not yet operational.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 */
1614}
1615
1616static int __init cpucache_init(void)
1617{
1618 int cpu;
1619
Andrew Mortona737b3e2006-03-22 00:08:11 -08001620 /*
1621 * Register the timers that return unneeded pages to the page allocator
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 */
Christoph Lametere498be72005-09-09 13:03:32 -07001623 for_each_online_cpu(cpu)
Andrew Mortona737b3e2006-03-22 00:08:11 -08001624 start_cpu_timer(cpu);
Glauber Costaa164f8962012-06-21 00:59:18 +04001625
1626 /* Done! */
Christoph Lameter97d06602012-07-06 15:25:11 -05001627 slab_state = FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return 0;
1629}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630__initcall(cpucache_init);
1631
Rafael Aquini8bdec192012-03-09 17:27:27 -03001632static noinline void
1633slab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid)
1634{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001635 struct kmem_cache_node *n;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001636 struct slab *slabp;
1637 unsigned long flags;
1638 int node;
1639
1640 printk(KERN_WARNING
1641 "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n",
1642 nodeid, gfpflags);
1643 printk(KERN_WARNING " cache: %s, object size: %d, order: %d\n",
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05001644 cachep->name, cachep->size, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001645
1646 for_each_online_node(node) {
1647 unsigned long active_objs = 0, num_objs = 0, free_objects = 0;
1648 unsigned long active_slabs = 0, num_slabs = 0;
1649
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001650 n = cachep->node[node];
1651 if (!n)
Rafael Aquini8bdec192012-03-09 17:27:27 -03001652 continue;
1653
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001654 spin_lock_irqsave(&n->list_lock, flags);
1655 list_for_each_entry(slabp, &n->slabs_full, list) {
Rafael Aquini8bdec192012-03-09 17:27:27 -03001656 active_objs += cachep->num;
1657 active_slabs++;
1658 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001659 list_for_each_entry(slabp, &n->slabs_partial, list) {
Joonsoo Kim106a74e2013-10-24 10:07:48 +09001660 active_objs += slabp->active;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001661 active_slabs++;
1662 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001663 list_for_each_entry(slabp, &n->slabs_free, list)
Rafael Aquini8bdec192012-03-09 17:27:27 -03001664 num_slabs++;
1665
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001666 free_objects += n->free_objects;
1667 spin_unlock_irqrestore(&n->list_lock, flags);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001668
1669 num_slabs += active_slabs;
1670 num_objs = num_slabs * cachep->num;
1671 printk(KERN_WARNING
1672 " node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
1673 node, active_slabs, num_slabs, active_objs, num_objs,
1674 free_objects);
1675 }
1676}
1677
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678/*
1679 * Interface to system's page allocator. No need to hold the cache-lock.
1680 *
1681 * If we requested dmaable memory, we will get it. Even if we
1682 * did not request dmaable memory, we might get it, but that
1683 * would be relatively rare and ignorable.
1684 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001685static struct page *kmem_getpages(struct kmem_cache *cachep, gfp_t flags,
1686 int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
1688 struct page *page;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001689 int nr_pages;
Christoph Lameter765c4502006-09-27 01:50:08 -07001690
Glauber Costaa618e892012-06-14 16:17:21 +04001691 flags |= cachep->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001692 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1693 flags |= __GFP_RECLAIMABLE;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001694
Linus Torvalds517d0862009-06-16 19:50:13 -07001695 page = alloc_pages_exact_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001696 if (!page) {
1697 if (!(flags & __GFP_NOWARN) && printk_ratelimit())
1698 slab_out_of_memory(cachep, flags, nodeid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 return NULL;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Mel Gormanb37f1dd2012-07-31 16:44:03 -07001702 /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
Mel Gorman072bb0a2012-07-31 16:43:58 -07001703 if (unlikely(page->pfmemalloc))
1704 pfmemalloc_active = true;
1705
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001706 nr_pages = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
Christoph Lameter972d1a72006-09-25 23:31:51 -07001708 add_zone_page_state(page_zone(page),
1709 NR_SLAB_RECLAIMABLE, nr_pages);
1710 else
1711 add_zone_page_state(page_zone(page),
1712 NR_SLAB_UNRECLAIMABLE, nr_pages);
Joonsoo Kima57a4982013-10-24 10:07:44 +09001713 __SetPageSlab(page);
1714 if (page->pfmemalloc)
1715 SetPageSlabPfmemalloc(page);
Glauber Costa1f458cb2012-12-18 14:22:50 -08001716 memcg_bind_pages(cachep, cachep->gfporder);
Mel Gorman072bb0a2012-07-31 16:43:58 -07001717
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001718 if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
1719 kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
1720
1721 if (cachep->ctor)
1722 kmemcheck_mark_uninitialized_pages(page, nr_pages);
1723 else
1724 kmemcheck_mark_unallocated_pages(page, nr_pages);
1725 }
Pekka Enbergc175eea2008-05-09 20:35:53 +02001726
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001727 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728}
1729
1730/*
1731 * Interface to system's page release.
1732 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001733static void kmem_freepages(struct kmem_cache *cachep, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734{
Joonsoo Kima57a4982013-10-24 10:07:44 +09001735 const unsigned long nr_freed = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001737 kmemcheck_free_shadow(page, cachep->gfporder);
Pekka Enbergc175eea2008-05-09 20:35:53 +02001738
Christoph Lameter972d1a72006-09-25 23:31:51 -07001739 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1740 sub_zone_page_state(page_zone(page),
1741 NR_SLAB_RECLAIMABLE, nr_freed);
1742 else
1743 sub_zone_page_state(page_zone(page),
1744 NR_SLAB_UNRECLAIMABLE, nr_freed);
Joonsoo Kim73293c22013-10-24 10:07:37 +09001745
Joonsoo Kima57a4982013-10-24 10:07:44 +09001746 BUG_ON(!PageSlab(page));
Joonsoo Kim73293c22013-10-24 10:07:37 +09001747 __ClearPageSlabPfmemalloc(page);
Joonsoo Kima57a4982013-10-24 10:07:44 +09001748 __ClearPageSlab(page);
Glauber Costa1f458cb2012-12-18 14:22:50 -08001749
1750 memcg_release_pages(cachep, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 if (current->reclaim_state)
1752 current->reclaim_state->reclaimed_slab += nr_freed;
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001753 __free_memcg_kmem_pages(page, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754}
1755
1756static void kmem_rcu_free(struct rcu_head *head)
1757{
Joonsoo Kim68126702013-10-24 10:07:42 +09001758 struct kmem_cache *cachep;
1759 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
Joonsoo Kim68126702013-10-24 10:07:42 +09001761 page = container_of(head, struct page, rcu_head);
1762 cachep = page->slab_cache;
1763
1764 kmem_freepages(cachep, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
1766
1767#if DEBUG
1768
1769#ifdef CONFIG_DEBUG_PAGEALLOC
Pekka Enberg343e0d72006-02-01 03:05:50 -08001770static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001771 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001773 int size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001775 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001777 if (size < 5 * sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return;
1779
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001780 *addr++ = 0x12345678;
1781 *addr++ = caller;
1782 *addr++ = smp_processor_id();
1783 size -= 3 * sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 {
1785 unsigned long *sptr = &caller;
1786 unsigned long svalue;
1787
1788 while (!kstack_end(sptr)) {
1789 svalue = *sptr++;
1790 if (kernel_text_address(svalue)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001791 *addr++ = svalue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 size -= sizeof(unsigned long);
1793 if (size <= sizeof(unsigned long))
1794 break;
1795 }
1796 }
1797
1798 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001799 *addr++ = 0x87654321;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800}
1801#endif
1802
Pekka Enberg343e0d72006-02-01 03:05:50 -08001803static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001805 int size = cachep->object_size;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001806 addr = &((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 memset(addr, val, size);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001809 *(unsigned char *)(addr + size - 1) = POISON_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810}
1811
1812static void dump_line(char *data, int offset, int limit)
1813{
1814 int i;
Dave Jonesaa83aa42006-09-29 01:59:51 -07001815 unsigned char error = 0;
1816 int bad_count = 0;
1817
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02001818 printk(KERN_ERR "%03x: ", offset);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001819 for (i = 0; i < limit; i++) {
1820 if (data[offset + i] != POISON_FREE) {
1821 error = data[offset + i];
1822 bad_count++;
1823 }
Dave Jonesaa83aa42006-09-29 01:59:51 -07001824 }
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02001825 print_hex_dump(KERN_CONT, "", 0, 16, 1,
1826 &data[offset], limit, 1);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001827
1828 if (bad_count == 1) {
1829 error ^= POISON_FREE;
1830 if (!(error & (error - 1))) {
1831 printk(KERN_ERR "Single bit error detected. Probably "
1832 "bad RAM.\n");
1833#ifdef CONFIG_X86
1834 printk(KERN_ERR "Run memtest86+ or a similar memory "
1835 "test tool.\n");
1836#else
1837 printk(KERN_ERR "Run a memory test tool.\n");
1838#endif
1839 }
1840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842#endif
1843
1844#if DEBUG
1845
Pekka Enberg343e0d72006-02-01 03:05:50 -08001846static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847{
1848 int i, size;
1849 char *realobj;
1850
1851 if (cachep->flags & SLAB_RED_ZONE) {
David Woodhouseb46b8f12007-05-08 00:22:59 -07001852 printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001853 *dbg_redzone1(cachep, objp),
1854 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 }
1856
1857 if (cachep->flags & SLAB_STORE_USER) {
Joe Perches071361d2012-12-12 10:19:12 -08001858 printk(KERN_ERR "Last user: [<%p>](%pSR)\n",
1859 *dbg_userword(cachep, objp),
1860 *dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001862 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001863 size = cachep->object_size;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001864 for (i = 0; i < size && lines; i += 16, lines--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 int limit;
1866 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001867 if (i + limit > size)
1868 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 dump_line(realobj, i, limit);
1870 }
1871}
1872
Pekka Enberg343e0d72006-02-01 03:05:50 -08001873static void check_poison_obj(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874{
1875 char *realobj;
1876 int size, i;
1877 int lines = 0;
1878
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001879 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001880 size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001882 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 char exp = POISON_FREE;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001884 if (i == size - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 exp = POISON_END;
1886 if (realobj[i] != exp) {
1887 int limit;
1888 /* Mismatch ! */
1889 /* Print header */
1890 if (lines == 0) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001891 printk(KERN_ERR
Dave Jonesface37f2011-11-15 15:03:52 -08001892 "Slab corruption (%s): %s start=%p, len=%d\n",
1893 print_tainted(), cachep->name, realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 print_objinfo(cachep, objp, 0);
1895 }
1896 /* Hexdump the affected line */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001897 i = (i / 16) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001899 if (i + limit > size)
1900 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 dump_line(realobj, i, limit);
1902 i += 16;
1903 lines++;
1904 /* Limit to 5 lines */
1905 if (lines > 5)
1906 break;
1907 }
1908 }
1909 if (lines != 0) {
1910 /* Print some data about the neighboring objects, if they
1911 * exist:
1912 */
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08001913 struct slab *slabp = virt_to_slab(objp);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001914 unsigned int objnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001916 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (objnr) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001918 objp = index_to_obj(cachep, slabp, objnr - 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001919 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001921 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 print_objinfo(cachep, objp, 2);
1923 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001924 if (objnr + 1 < cachep->num) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001925 objp = index_to_obj(cachep, slabp, objnr + 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001926 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001928 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 print_objinfo(cachep, objp, 2);
1930 }
1931 }
1932}
1933#endif
1934
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935#if DEBUG
Rabin Vincente79aec22008-07-04 00:40:32 +05301936static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001937{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 int i;
1939 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001940 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 if (cachep->flags & SLAB_POISON) {
1943#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05001944 if (cachep->size % PAGE_SIZE == 0 &&
Andrew Mortona737b3e2006-03-22 00:08:11 -08001945 OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001946 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05001947 cachep->size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 else
1949 check_poison_obj(cachep, objp);
1950#else
1951 check_poison_obj(cachep, objp);
1952#endif
1953 }
1954 if (cachep->flags & SLAB_RED_ZONE) {
1955 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
1956 slab_error(cachep, "start of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001957 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
1959 slab_error(cachep, "end of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001960 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001963}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964#else
Rabin Vincente79aec22008-07-04 00:40:32 +05301965static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001966{
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001967}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968#endif
1969
Randy Dunlap911851e2006-03-22 00:08:14 -08001970/**
1971 * slab_destroy - destroy and release all objects in a slab
1972 * @cachep: cache pointer being destroyed
1973 * @slabp: slab pointer being destroyed
1974 *
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001975 * Destroy all the objs in a slab, and release the mem back to the system.
Andrew Mortona737b3e2006-03-22 00:08:11 -08001976 * Before calling the slab must have been unlinked from the cache. The
1977 * cache-lock is not held/needed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001978 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001979static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001980{
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001981 struct page *page = virt_to_head_page(slabp->s_mem);
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001982
Rabin Vincente79aec22008-07-04 00:40:32 +05301983 slab_destroy_debugcheck(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
Joonsoo Kim68126702013-10-24 10:07:42 +09001985 struct rcu_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Joonsoo Kim68126702013-10-24 10:07:42 +09001987 /*
1988 * RCU free overloads the RCU head over the LRU.
1989 * slab_page has been overloeaded over the LRU,
1990 * however it is not used from now on so that
1991 * we can use it safely.
1992 */
1993 head = (void *)&page->rcu_head;
1994 call_rcu(head, kmem_rcu_free);
1995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 } else {
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001997 kmem_freepages(cachep, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 }
Joonsoo Kim68126702013-10-24 10:07:42 +09001999
2000 /*
2001 * From now on, we don't use slab management
2002 * although actual page can be freed in rcu context
2003 */
2004 if (OFF_SLAB(cachep))
2005 kmem_cache_free(cachep->slabp_cache, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
2007
2008/**
Randy.Dunlapa70773d2006-02-01 03:05:52 -08002009 * calculate_slab_order - calculate size (page order) of slabs
2010 * @cachep: pointer to the cache that is being created
2011 * @size: size of objects to be created in this cache.
2012 * @align: required alignment for the objects.
2013 * @flags: slab allocation flags
2014 *
2015 * Also calculates the number of objects per slab.
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002016 *
2017 * This could be made much more intelligent. For now, try to avoid using
2018 * high order pages for slabs. When the gfp() functions are more friendly
2019 * towards high-order requests, this should be changed.
2020 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002021static size_t calculate_slab_order(struct kmem_cache *cachep,
Randy Dunlapee13d782006-02-01 03:05:53 -08002022 size_t size, size_t align, unsigned long flags)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002023{
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002024 unsigned long offslab_limit;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002025 size_t left_over = 0;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002026 int gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002027
Christoph Lameter0aa817f2007-05-16 22:11:01 -07002028 for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002029 unsigned int num;
2030 size_t remainder;
2031
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002032 cache_estimate(gfporder, size, align, flags, &remainder, &num);
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002033 if (!num)
2034 continue;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002035
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002036 if (flags & CFLGS_OFF_SLAB) {
2037 /*
2038 * Max number of objs-per-slab for caches which
2039 * use off-slab slabs. Needed to avoid a possible
2040 * looping condition in cache_grow().
2041 */
2042 offslab_limit = size - sizeof(struct slab);
Joonsoo Kim16025172013-10-24 10:07:46 +09002043 offslab_limit /= sizeof(unsigned int);
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002044
2045 if (num > offslab_limit)
2046 break;
2047 }
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002048
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002049 /* Found something acceptable - save it away */
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002050 cachep->num = num;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002051 cachep->gfporder = gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002052 left_over = remainder;
2053
2054 /*
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08002055 * A VFS-reclaimable slab tends to have most allocations
2056 * as GFP_NOFS and we really don't want to have to be allocating
2057 * higher-order pages when we are unable to shrink dcache.
2058 */
2059 if (flags & SLAB_RECLAIM_ACCOUNT)
2060 break;
2061
2062 /*
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002063 * Large number of objects is good, but very large slabs are
2064 * currently bad for the gfp()s.
2065 */
David Rientjes543585c2011-10-18 22:09:24 -07002066 if (gfporder >= slab_max_order)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002067 break;
2068
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002069 /*
2070 * Acceptable internal fragmentation?
2071 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002072 if (left_over * 8 <= (PAGE_SIZE << gfporder))
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002073 break;
2074 }
2075 return left_over;
2076}
2077
Pekka Enberg83b519e2009-06-10 19:40:04 +03002078static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002079{
Christoph Lameter97d06602012-07-06 15:25:11 -05002080 if (slab_state >= FULL)
Pekka Enberg83b519e2009-06-10 19:40:04 +03002081 return enable_cpucache(cachep, gfp);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002082
Christoph Lameter97d06602012-07-06 15:25:11 -05002083 if (slab_state == DOWN) {
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002084 /*
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002085 * Note: Creation of first cache (kmem_cache).
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002086 * The setup_node is taken care
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002087 * of by the caller of __kmem_cache_create
2088 */
2089 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2090 slab_state = PARTIAL;
2091 } else if (slab_state == PARTIAL) {
2092 /*
2093 * Note: the second kmem_cache_create must create the cache
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002094 * that's used by kmalloc(24), otherwise the creation of
2095 * further caches will BUG().
2096 */
2097 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2098
2099 /*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002100 * If the cache that's used by kmalloc(sizeof(kmem_cache_node)) is
2101 * the second cache, then we need to set up all its node/,
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002102 * otherwise the creation of further caches will BUG().
2103 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002104 set_up_node(cachep, SIZE_AC);
2105 if (INDEX_AC == INDEX_NODE)
2106 slab_state = PARTIAL_NODE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002107 else
Christoph Lameter97d06602012-07-06 15:25:11 -05002108 slab_state = PARTIAL_ARRAYCACHE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002109 } else {
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002110 /* Remaining boot caches */
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002111 cachep->array[smp_processor_id()] =
Pekka Enberg83b519e2009-06-10 19:40:04 +03002112 kmalloc(sizeof(struct arraycache_init), gfp);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002113
Christoph Lameter97d06602012-07-06 15:25:11 -05002114 if (slab_state == PARTIAL_ARRAYCACHE) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002115 set_up_node(cachep, SIZE_NODE);
2116 slab_state = PARTIAL_NODE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002117 } else {
2118 int node;
Pekka Enberg556a1692008-01-25 08:20:51 +02002119 for_each_online_node(node) {
Christoph Lameter6a673682013-01-10 19:14:19 +00002120 cachep->node[node] =
Christoph Lameter6744f082013-01-10 19:12:17 +00002121 kmalloc_node(sizeof(struct kmem_cache_node),
Pekka Enbergeb91f1d2009-06-12 14:56:09 +03002122 gfp, node);
Christoph Lameter6a673682013-01-10 19:14:19 +00002123 BUG_ON(!cachep->node[node]);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002124 kmem_cache_node_init(cachep->node[node]);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002125 }
2126 }
2127 }
Christoph Lameter6a673682013-01-10 19:14:19 +00002128 cachep->node[numa_mem_id()]->next_reap =
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002129 jiffies + REAPTIMEOUT_LIST3 +
2130 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
2131
2132 cpu_cache_get(cachep)->avail = 0;
2133 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
2134 cpu_cache_get(cachep)->batchcount = 1;
2135 cpu_cache_get(cachep)->touched = 0;
2136 cachep->batchcount = 1;
2137 cachep->limit = BOOT_CPUCACHE_ENTRIES;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002138 return 0;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002139}
2140
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002141/**
Christoph Lameter039363f2012-07-06 15:25:10 -05002142 * __kmem_cache_create - Create a cache.
Randy Dunlapa755b762012-11-06 17:10:10 -08002143 * @cachep: cache management descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 * @flags: SLAB flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 *
2146 * Returns a ptr to the cache on success, NULL on failure.
2147 * Cannot be called within a int, but can be interrupted.
Paul Mundt20c2df82007-07-20 10:11:58 +09002148 * The @ctor is run when new pages are allocated by the cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 * The flags are
2151 *
2152 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2153 * to catch references to uninitialised memory.
2154 *
2155 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2156 * for buffer overruns.
2157 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2159 * cacheline. This can be beneficial if you're counting cycles as closely
2160 * as davem.
2161 */
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002162int
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002163__kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164{
2165 size_t left_over, slab_size, ralign;
Pekka Enberg83b519e2009-06-10 19:40:04 +03002166 gfp_t gfp;
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002167 int err;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002168 size_t size = cachep->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171#if FORCED_DEBUG
2172 /*
2173 * Enable redzoning and last user accounting, except for caches with
2174 * large objects, if the increased size would increase the object size
2175 * above the next power of two: caches with object sizes just above a
2176 * power of two have a significant amount of internal fragmentation.
2177 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002178 if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
2179 2 * sizeof(unsigned long long)))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002180 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 if (!(flags & SLAB_DESTROY_BY_RCU))
2182 flags |= SLAB_POISON;
2183#endif
2184 if (flags & SLAB_DESTROY_BY_RCU)
2185 BUG_ON(flags & SLAB_POISON);
2186#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Andrew Mortona737b3e2006-03-22 00:08:11 -08002188 /*
2189 * Check that size is in terms of words. This is needed to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 * unaligned accesses for some archs when redzoning is used, and makes
2191 * sure any on-slab bufctl's are also correctly aligned.
2192 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002193 if (size & (BYTES_PER_WORD - 1)) {
2194 size += (BYTES_PER_WORD - 1);
2195 size &= ~(BYTES_PER_WORD - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 }
2197
Pekka Enbergca5f9702006-09-25 23:31:25 -07002198 /*
David Woodhouse87a927c2007-07-04 21:26:44 -04002199 * Redzoning and user store require word alignment or possibly larger.
2200 * Note this will be overridden by architecture or caller mandated
2201 * alignment if either is greater than BYTES_PER_WORD.
Pekka Enbergca5f9702006-09-25 23:31:25 -07002202 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002203 if (flags & SLAB_STORE_USER)
2204 ralign = BYTES_PER_WORD;
2205
2206 if (flags & SLAB_RED_ZONE) {
2207 ralign = REDZONE_ALIGN;
2208 /* If redzoning, ensure that the second redzone is suitably
2209 * aligned, by adjusting the object size accordingly. */
2210 size += REDZONE_ALIGN - 1;
2211 size &= ~(REDZONE_ALIGN - 1);
2212 }
Pekka Enbergca5f9702006-09-25 23:31:25 -07002213
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002214 /* 3) caller mandated alignment */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002215 if (ralign < cachep->align) {
2216 ralign = cachep->align;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 }
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002218 /* disable debug if necessary */
2219 if (ralign > __alignof__(unsigned long long))
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002220 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002221 /*
Pekka Enbergca5f9702006-09-25 23:31:25 -07002222 * 4) Store it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002224 cachep->align = ralign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Pekka Enberg83b519e2009-06-10 19:40:04 +03002226 if (slab_is_available())
2227 gfp = GFP_KERNEL;
2228 else
2229 gfp = GFP_NOWAIT;
2230
Christoph Lameter6a673682013-01-10 19:14:19 +00002231 setup_node_pointer(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233
Pekka Enbergca5f9702006-09-25 23:31:25 -07002234 /*
2235 * Both debugging options require word-alignment which is calculated
2236 * into align above.
2237 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 if (flags & SLAB_RED_ZONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 /* add space for red zone words */
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002240 cachep->obj_offset += sizeof(unsigned long long);
2241 size += 2 * sizeof(unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 }
2243 if (flags & SLAB_STORE_USER) {
Pekka Enbergca5f9702006-09-25 23:31:25 -07002244 /* user store requires one word storage behind the end of
David Woodhouse87a927c2007-07-04 21:26:44 -04002245 * the real object. But if the second red zone needs to be
2246 * aligned to 64 bits, we must allow that much space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002248 if (flags & SLAB_RED_ZONE)
2249 size += REDZONE_ALIGN;
2250 else
2251 size += BYTES_PER_WORD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 }
2253#if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002254 if (size >= kmalloc_size(INDEX_NODE + 1)
Tetsuo Handa608da7e2012-09-30 17:28:25 +09002255 && cachep->object_size > cache_line_size()
2256 && ALIGN(size, cachep->align) < PAGE_SIZE) {
2257 cachep->obj_offset += PAGE_SIZE - ALIGN(size, cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 size = PAGE_SIZE;
2259 }
2260#endif
2261#endif
2262
Ingo Molnare0a42722006-06-23 02:03:46 -07002263 /*
2264 * Determine if the slab management is 'on' or 'off' slab.
2265 * (bootstrapping cannot cope with offslab caches so don't do
Catalin Marinase7cb55b2009-10-28 13:33:08 +00002266 * it too early on. Always use on-slab management when
2267 * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
Ingo Molnare0a42722006-06-23 02:03:46 -07002268 */
Catalin Marinase7cb55b2009-10-28 13:33:08 +00002269 if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init &&
2270 !(flags & SLAB_NOLEAKTRACE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 /*
2272 * Size is large, assume best to place the slab management obj
2273 * off-slab (should allow better packing of objs).
2274 */
2275 flags |= CFLGS_OFF_SLAB;
2276
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002277 size = ALIGN(size, cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002279 left_over = calculate_slab_order(cachep, size, cachep->align, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002281 if (!cachep->num)
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002282 return -E2BIG;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002283
Joonsoo Kim16025172013-10-24 10:07:46 +09002284 slab_size = ALIGN(cachep->num * sizeof(unsigned int)
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002285 + sizeof(struct slab), cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
2287 /*
2288 * If the slab has been placed off-slab, and we have enough space then
2289 * move it on-slab. This is at the expense of any extra colouring.
2290 */
2291 if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
2292 flags &= ~CFLGS_OFF_SLAB;
2293 left_over -= slab_size;
2294 }
2295
2296 if (flags & CFLGS_OFF_SLAB) {
2297 /* really off slab. No need for manual alignment */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002298 slab_size =
Joonsoo Kim16025172013-10-24 10:07:46 +09002299 cachep->num * sizeof(unsigned int) + sizeof(struct slab);
Ron Lee67461362009-05-22 04:58:22 +09302300
2301#ifdef CONFIG_PAGE_POISONING
2302 /* If we're going to use the generic kernel_map_pages()
2303 * poisoning, then it's going to smash the contents of
2304 * the redzone and userword anyhow, so switch them off.
2305 */
2306 if (size % PAGE_SIZE == 0 && flags & SLAB_POISON)
2307 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
2308#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 }
2310
2311 cachep->colour_off = cache_line_size();
2312 /* Offset must be a multiple of the alignment. */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002313 if (cachep->colour_off < cachep->align)
2314 cachep->colour_off = cachep->align;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002315 cachep->colour = left_over / cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 cachep->slab_size = slab_size;
2317 cachep->flags = flags;
Joonsoo Kima57a4982013-10-24 10:07:44 +09002318 cachep->allocflags = __GFP_COMP;
Christoph Lameter4b51d662007-02-10 01:43:10 -08002319 if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
Glauber Costaa618e892012-06-14 16:17:21 +04002320 cachep->allocflags |= GFP_DMA;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002321 cachep->size = size;
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08002322 cachep->reciprocal_buffer_size = reciprocal_value(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002324 if (flags & CFLGS_OFF_SLAB) {
Christoph Lameter2c59dd62013-01-10 19:14:19 +00002325 cachep->slabp_cache = kmalloc_slab(slab_size, 0u);
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002326 /*
2327 * This is a possibility for one of the malloc_sizes caches.
2328 * But since we go off slab only for object size greater than
2329 * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
2330 * this should not happen at all.
2331 * But leave a BUG_ON for some lucky dude.
2332 */
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002333 BUG_ON(ZERO_OR_NULL_PTR(cachep->slabp_cache));
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002336 err = setup_cpu_cache(cachep, gfp);
2337 if (err) {
Christoph Lameter12c36672012-09-04 23:38:33 +00002338 __kmem_cache_shutdown(cachep);
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002339 return err;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
Peter Zijlstra83835b32011-07-22 15:26:05 +02002342 if (flags & SLAB_DEBUG_OBJECTS) {
2343 /*
2344 * Would deadlock through slab_destroy()->call_rcu()->
2345 * debug_object_activate()->kmem_cache_alloc().
2346 */
2347 WARN_ON_ONCE(flags & SLAB_DESTROY_BY_RCU);
2348
2349 slab_set_debugobj_lock_classes(cachep);
Glauber Costa6ccfb5b2012-12-18 14:22:31 -08002350 } else if (!OFF_SLAB(cachep) && !(flags & SLAB_DESTROY_BY_RCU))
2351 on_slab_lock_classes(cachep);
Peter Zijlstra83835b32011-07-22 15:26:05 +02002352
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
2356#if DEBUG
2357static void check_irq_off(void)
2358{
2359 BUG_ON(!irqs_disabled());
2360}
2361
2362static void check_irq_on(void)
2363{
2364 BUG_ON(irqs_disabled());
2365}
2366
Pekka Enberg343e0d72006-02-01 03:05:50 -08002367static void check_spinlock_acquired(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
2369#ifdef CONFIG_SMP
2370 check_irq_off();
Christoph Lameter6a673682013-01-10 19:14:19 +00002371 assert_spin_locked(&cachep->node[numa_mem_id()]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372#endif
2373}
Christoph Lametere498be72005-09-09 13:03:32 -07002374
Pekka Enberg343e0d72006-02-01 03:05:50 -08002375static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07002376{
2377#ifdef CONFIG_SMP
2378 check_irq_off();
Christoph Lameter6a673682013-01-10 19:14:19 +00002379 assert_spin_locked(&cachep->node[node]->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002380#endif
2381}
2382
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383#else
2384#define check_irq_off() do { } while(0)
2385#define check_irq_on() do { } while(0)
2386#define check_spinlock_acquired(x) do { } while(0)
Christoph Lametere498be72005-09-09 13:03:32 -07002387#define check_spinlock_acquired_node(x, y) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388#endif
2389
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002390static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
Christoph Lameteraab22072006-03-22 00:09:06 -08002391 struct array_cache *ac,
2392 int force, int node);
2393
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394static void do_drain(void *arg)
2395{
Andrew Mortona737b3e2006-03-22 00:08:11 -08002396 struct kmem_cache *cachep = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 struct array_cache *ac;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07002398 int node = numa_mem_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399
2400 check_irq_off();
Pekka Enberg9a2dba4b2006-02-01 03:05:49 -08002401 ac = cpu_cache_get(cachep);
Christoph Lameter6a673682013-01-10 19:14:19 +00002402 spin_lock(&cachep->node[node]->list_lock);
Christoph Lameterff694162005-09-22 21:44:02 -07002403 free_block(cachep, ac->entry, ac->avail, node);
Christoph Lameter6a673682013-01-10 19:14:19 +00002404 spin_unlock(&cachep->node[node]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 ac->avail = 0;
2406}
2407
Pekka Enberg343e0d72006-02-01 03:05:50 -08002408static void drain_cpu_caches(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002410 struct kmem_cache_node *n;
Christoph Lametere498be72005-09-09 13:03:32 -07002411 int node;
2412
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002413 on_each_cpu(do_drain, cachep, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 check_irq_on();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002415 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002416 n = cachep->node[node];
2417 if (n && n->alien)
2418 drain_alien_cache(cachep, n->alien);
Roland Dreiera4523a82006-05-15 11:41:00 -07002419 }
2420
2421 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002422 n = cachep->node[node];
2423 if (n)
2424 drain_array(cachep, n, n->shared, 1, node);
Christoph Lametere498be72005-09-09 13:03:32 -07002425 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426}
2427
Christoph Lametered11d9e2006-06-30 01:55:45 -07002428/*
2429 * Remove slabs from the list of free slabs.
2430 * Specify the number of slabs to drain in tofree.
2431 *
2432 * Returns the actual number of slabs released.
2433 */
2434static int drain_freelist(struct kmem_cache *cache,
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002435 struct kmem_cache_node *n, int tofree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436{
Christoph Lametered11d9e2006-06-30 01:55:45 -07002437 struct list_head *p;
2438 int nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Christoph Lametered11d9e2006-06-30 01:55:45 -07002441 nr_freed = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002442 while (nr_freed < tofree && !list_empty(&n->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002444 spin_lock_irq(&n->list_lock);
2445 p = n->slabs_free.prev;
2446 if (p == &n->slabs_free) {
2447 spin_unlock_irq(&n->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002448 goto out;
2449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Christoph Lametered11d9e2006-06-30 01:55:45 -07002451 slabp = list_entry(p, struct slab, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452#if DEBUG
Joonsoo Kim106a74e2013-10-24 10:07:48 +09002453 BUG_ON(slabp->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454#endif
2455 list_del(&slabp->list);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002456 /*
2457 * Safe to drop the lock. The slab is no longer linked
2458 * to the cache.
2459 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002460 n->free_objects -= cache->num;
2461 spin_unlock_irq(&n->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002462 slab_destroy(cache, slabp);
2463 nr_freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 }
Christoph Lametered11d9e2006-06-30 01:55:45 -07002465out:
2466 return nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467}
2468
Christoph Lameter18004c52012-07-06 15:25:12 -05002469/* Called with slab_mutex held to protect against cpu hotplug */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002470static int __cache_shrink(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07002471{
2472 int ret = 0, i = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002473 struct kmem_cache_node *n;
Christoph Lametere498be72005-09-09 13:03:32 -07002474
2475 drain_cpu_caches(cachep);
2476
2477 check_irq_on();
2478 for_each_online_node(i) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002479 n = cachep->node[i];
2480 if (!n)
Christoph Lametered11d9e2006-06-30 01:55:45 -07002481 continue;
2482
Wanpeng Li0fa81032013-07-04 08:33:22 +08002483 drain_freelist(cachep, n, slabs_tofree(cachep, n));
Christoph Lametered11d9e2006-06-30 01:55:45 -07002484
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002485 ret += !list_empty(&n->slabs_full) ||
2486 !list_empty(&n->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07002487 }
2488 return (ret ? 1 : 0);
2489}
2490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491/**
2492 * kmem_cache_shrink - Shrink a cache.
2493 * @cachep: The cache to shrink.
2494 *
2495 * Releases as many slabs as possible for a cache.
2496 * To help debugging, a zero exit status indicates all slabs were released.
2497 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002498int kmem_cache_shrink(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499{
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002500 int ret;
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002501 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002503 get_online_cpus();
Christoph Lameter18004c52012-07-06 15:25:12 -05002504 mutex_lock(&slab_mutex);
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002505 ret = __cache_shrink(cachep);
Christoph Lameter18004c52012-07-06 15:25:12 -05002506 mutex_unlock(&slab_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002507 put_online_cpus();
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002508 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509}
2510EXPORT_SYMBOL(kmem_cache_shrink);
2511
Christoph Lameter945cf2b2012-09-04 23:18:33 +00002512int __kmem_cache_shutdown(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
Christoph Lameter12c36672012-09-04 23:38:33 +00002514 int i;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002515 struct kmem_cache_node *n;
Christoph Lameter12c36672012-09-04 23:38:33 +00002516 int rc = __cache_shrink(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
Christoph Lameter12c36672012-09-04 23:38:33 +00002518 if (rc)
2519 return rc;
2520
2521 for_each_online_cpu(i)
2522 kfree(cachep->array[i]);
2523
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002524 /* NUMA: free the node structures */
Christoph Lameter12c36672012-09-04 23:38:33 +00002525 for_each_online_node(i) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002526 n = cachep->node[i];
2527 if (n) {
2528 kfree(n->shared);
2529 free_alien_cache(n->alien);
2530 kfree(n);
Christoph Lameter12c36672012-09-04 23:38:33 +00002531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 }
Christoph Lameter12c36672012-09-04 23:38:33 +00002533 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002536/*
2537 * Get the memory for a slab management obj.
2538 * For a slab cache when the slab descriptor is off-slab, slab descriptors
2539 * always come from malloc_sizes caches. The slab descriptor cannot
2540 * come from the same cache which is getting created because,
2541 * when we are searching for an appropriate cache for these
2542 * descriptors in kmem_cache_create, we search through the malloc_sizes array.
2543 * If we are creating a malloc_sizes cache here it would not be visible to
2544 * kmem_find_general_cachep till the initialization is complete.
2545 * Hence we cannot have slabp_cache same as the original cache.
2546 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002547static struct slab *alloc_slabmgmt(struct kmem_cache *cachep,
2548 struct page *page, int colour_off,
2549 gfp_t local_flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550{
2551 struct slab *slabp;
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002552 void *addr = page_address(page);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 if (OFF_SLAB(cachep)) {
2555 /* Slab management obj is off-slab. */
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002556 slabp = kmem_cache_alloc_node(cachep->slabp_cache,
Pekka Enberg8759ec52008-11-26 10:01:31 +02002557 local_flags, nodeid);
Catalin Marinasd5cff632009-06-11 13:22:40 +01002558 /*
2559 * If the first object in the slab is leaked (it's allocated
2560 * but no one has a reference to it), we want to make sure
2561 * kmemleak does not treat the ->s_mem pointer as a reference
2562 * to the object. Otherwise we will not report the leak.
2563 */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002564 kmemleak_scan_area(&slabp->list, sizeof(struct list_head),
2565 local_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 if (!slabp)
2567 return NULL;
2568 } else {
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002569 slabp = addr + colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570 colour_off += cachep->slab_size;
2571 }
Joonsoo Kim106a74e2013-10-24 10:07:48 +09002572 slabp->active = 0;
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002573 slabp->s_mem = addr + colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 return slabp;
2575}
2576
Joonsoo Kim16025172013-10-24 10:07:46 +09002577static inline unsigned int *slab_bufctl(struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
Joonsoo Kim16025172013-10-24 10:07:46 +09002579 return (unsigned int *) (slabp + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580}
2581
Pekka Enberg343e0d72006-02-01 03:05:50 -08002582static void cache_init_objs(struct kmem_cache *cachep,
Christoph Lametera35afb82007-05-16 22:10:57 -07002583 struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584{
2585 int i;
2586
2587 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002588 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589#if DEBUG
2590 /* need to poison the objs? */
2591 if (cachep->flags & SLAB_POISON)
2592 poison_obj(cachep, objp, POISON_FREE);
2593 if (cachep->flags & SLAB_STORE_USER)
2594 *dbg_userword(cachep, objp) = NULL;
2595
2596 if (cachep->flags & SLAB_RED_ZONE) {
2597 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2598 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2599 }
2600 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002601 * Constructors are not allowed to allocate memory from the same
2602 * cache which they are a constructor for. Otherwise, deadlock.
2603 * They must also be threaded.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 */
2605 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002606 cachep->ctor(objp + obj_offset(cachep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
2608 if (cachep->flags & SLAB_RED_ZONE) {
2609 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2610 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002611 " end of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2613 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002614 " start of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 }
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002616 if ((cachep->size % PAGE_SIZE) == 0 &&
Andrew Mortona737b3e2006-03-22 00:08:11 -08002617 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002618 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002619 cachep->size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620#else
2621 if (cachep->ctor)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002622 cachep->ctor(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623#endif
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002624 slab_bufctl(slabp)[i] = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626}
2627
Pekka Enberg343e0d72006-02-01 03:05:50 -08002628static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
Christoph Lameter4b51d662007-02-10 01:43:10 -08002630 if (CONFIG_ZONE_DMA_FLAG) {
2631 if (flags & GFP_DMA)
Glauber Costaa618e892012-06-14 16:17:21 +04002632 BUG_ON(!(cachep->allocflags & GFP_DMA));
Christoph Lameter4b51d662007-02-10 01:43:10 -08002633 else
Glauber Costaa618e892012-06-14 16:17:21 +04002634 BUG_ON(cachep->allocflags & GFP_DMA);
Christoph Lameter4b51d662007-02-10 01:43:10 -08002635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636}
2637
Andrew Mortona737b3e2006-03-22 00:08:11 -08002638static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
2639 int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002640{
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002641 void *objp;
Matthew Dobson78d382d2006-02-01 03:05:47 -08002642
Joonsoo Kim106a74e2013-10-24 10:07:48 +09002643 objp = index_to_obj(cachep, slabp, slab_bufctl(slabp)[slabp->active]);
2644 slabp->active++;
Matthew Dobson78d382d2006-02-01 03:05:47 -08002645#if DEBUG
Joonsoo Kim1ea991b2013-10-24 10:07:40 +09002646 WARN_ON(page_to_nid(virt_to_page(objp)) != nodeid);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002647#endif
Matthew Dobson78d382d2006-02-01 03:05:47 -08002648
2649 return objp;
2650}
2651
Andrew Mortona737b3e2006-03-22 00:08:11 -08002652static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
2653 void *objp, int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002654{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002655 unsigned int objnr = obj_to_index(cachep, slabp, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002656#if DEBUG
Joonsoo Kim16025172013-10-24 10:07:46 +09002657 unsigned int i;
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002658
Matthew Dobson78d382d2006-02-01 03:05:47 -08002659 /* Verify that the slab belongs to the intended node */
Joonsoo Kim1ea991b2013-10-24 10:07:40 +09002660 WARN_ON(page_to_nid(virt_to_page(objp)) != nodeid);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002661
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002662 /* Verify double free bug */
Joonsoo Kim106a74e2013-10-24 10:07:48 +09002663 for (i = slabp->active; i < cachep->num; i++) {
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002664 if (slab_bufctl(slabp)[i] == objnr) {
2665 printk(KERN_ERR "slab: double free detected in cache "
2666 "'%s', objp %p\n", cachep->name, objp);
2667 BUG();
2668 }
Matthew Dobson78d382d2006-02-01 03:05:47 -08002669 }
2670#endif
Joonsoo Kim106a74e2013-10-24 10:07:48 +09002671 slabp->active--;
2672 slab_bufctl(slabp)[slabp->active] = objnr;
Matthew Dobson78d382d2006-02-01 03:05:47 -08002673}
2674
Pekka Enberg47768742006-06-23 02:03:07 -07002675/*
2676 * Map pages beginning at addr to the given cache and slab. This is required
2677 * for the slab allocator to be able to lookup the cache and slab of a
Nick Pigginccd35fb2011-01-07 17:49:17 +11002678 * virtual address for kfree, ksize, and slab debugging.
Pekka Enberg47768742006-06-23 02:03:07 -07002679 */
2680static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002681 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682{
Joonsoo Kima57a4982013-10-24 10:07:44 +09002683 page->slab_cache = cache;
2684 page->slab_page = slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685}
2686
2687/*
2688 * Grow (by 1) the number of slabs within a cache. This is called by
2689 * kmem_cache_alloc() when there are no active objs left in a cache.
2690 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002691static int cache_grow(struct kmem_cache *cachep,
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002692 gfp_t flags, int nodeid, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002694 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002695 size_t offset;
2696 gfp_t local_flags;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002697 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698
Andrew Mortona737b3e2006-03-22 00:08:11 -08002699 /*
2700 * Be lazy and only check for valid flags here, keeping it out of the
2701 * critical path in kmem_cache_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 */
Christoph Lameter6cb06222007-10-16 01:25:41 -07002703 BUG_ON(flags & GFP_SLAB_BUG_MASK);
2704 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002706 /* Take the node list lock to change the colour_next on this node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002708 n = cachep->node[nodeid];
2709 spin_lock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
2711 /* Get colour for the slab, and cal the next value. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002712 offset = n->colour_next;
2713 n->colour_next++;
2714 if (n->colour_next >= cachep->colour)
2715 n->colour_next = 0;
2716 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002718 offset *= cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
2720 if (local_flags & __GFP_WAIT)
2721 local_irq_enable();
2722
2723 /*
2724 * The test for missing atomic flag is performed here, rather than
2725 * the more obvious place, simply to reduce the critical path length
2726 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2727 * will eventually be caught here (where it matters).
2728 */
2729 kmem_flagcheck(cachep, flags);
2730
Andrew Mortona737b3e2006-03-22 00:08:11 -08002731 /*
2732 * Get mem for the objs. Attempt to allocate a physical page from
2733 * 'nodeid'.
Christoph Lametere498be72005-09-09 13:03:32 -07002734 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002735 if (!page)
2736 page = kmem_getpages(cachep, local_flags, nodeid);
2737 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 goto failed;
2739
2740 /* Get slab management. */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002741 slabp = alloc_slabmgmt(cachep, page, offset,
Christoph Lameter6cb06222007-10-16 01:25:41 -07002742 local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002743 if (!slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 goto opps1;
2745
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002746 slab_map_pages(cachep, slabp, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Christoph Lametera35afb82007-05-16 22:10:57 -07002748 cache_init_objs(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
2750 if (local_flags & __GFP_WAIT)
2751 local_irq_disable();
2752 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002753 spin_lock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754
2755 /* Make slab active. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002756 list_add_tail(&slabp->list, &(n->slabs_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 STATS_INC_GROWN(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002758 n->free_objects += cachep->num;
2759 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002761opps1:
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002762 kmem_freepages(cachep, page);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002763failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 if (local_flags & __GFP_WAIT)
2765 local_irq_disable();
2766 return 0;
2767}
2768
2769#if DEBUG
2770
2771/*
2772 * Perform extra freeing checks:
2773 * - detect bad pointers.
2774 * - POISON/RED_ZONE checking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 */
2776static void kfree_debugcheck(const void *objp)
2777{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 if (!virt_addr_valid(objp)) {
2779 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002780 (unsigned long)objp);
2781 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783}
2784
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002785static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2786{
David Woodhouseb46b8f12007-05-08 00:22:59 -07002787 unsigned long long redzone1, redzone2;
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002788
2789 redzone1 = *dbg_redzone1(cache, obj);
2790 redzone2 = *dbg_redzone2(cache, obj);
2791
2792 /*
2793 * Redzone is ok.
2794 */
2795 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2796 return;
2797
2798 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2799 slab_error(cache, "double free detected");
2800 else
2801 slab_error(cache, "memory outside object was overwritten");
2802
David Woodhouseb46b8f12007-05-08 00:22:59 -07002803 printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002804 obj, redzone1, redzone2);
2805}
2806
Pekka Enberg343e0d72006-02-01 03:05:50 -08002807static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002808 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 unsigned int objnr;
2811 struct slab *slabp;
2812
Matthew Wilcox80cbd912007-11-29 12:05:13 -07002813 BUG_ON(virt_to_cache(objp) != cachep);
2814
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002815 objp -= obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 kfree_debugcheck(objp);
Joonsoo Kim56f295e2013-10-24 10:07:43 +09002817 slabp = virt_to_slab(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
2819 if (cachep->flags & SLAB_RED_ZONE) {
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002820 verify_redzone_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2822 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2823 }
2824 if (cachep->flags & SLAB_STORE_USER)
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002825 *dbg_userword(cachep, objp) = (void *)caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002827 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829 BUG_ON(objnr >= cachep->num);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002830 BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 if (cachep->flags & SLAB_POISON) {
2833#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002834 if ((cachep->size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002835 store_stackinfo(cachep, objp, caller);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002836 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002837 cachep->size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 } else {
2839 poison_obj(cachep, objp, POISON_FREE);
2840 }
2841#else
2842 poison_obj(cachep, objp, POISON_FREE);
2843#endif
2844 }
2845 return objp;
2846}
2847
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848#else
2849#define kfree_debugcheck(x) do { } while(0)
2850#define cache_free_debugcheck(x,objp,z) (objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851#endif
2852
Mel Gorman072bb0a2012-07-31 16:43:58 -07002853static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags,
2854 bool force_refill)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
2856 int batchcount;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002857 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 struct array_cache *ac;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002859 int node;
2860
Joe Korty6d2144d2008-03-05 15:04:59 -08002861 check_irq_off();
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07002862 node = numa_mem_id();
Mel Gorman072bb0a2012-07-31 16:43:58 -07002863 if (unlikely(force_refill))
2864 goto force_grow;
2865retry:
Joe Korty6d2144d2008-03-05 15:04:59 -08002866 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 batchcount = ac->batchcount;
2868 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002869 /*
2870 * If there was little recent activity on this cache, then
2871 * perform only a partial refill. Otherwise we could generate
2872 * refill bouncing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 */
2874 batchcount = BATCHREFILL_LIMIT;
2875 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002876 n = cachep->node[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002878 BUG_ON(ac->avail > 0 || !n);
2879 spin_lock(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002880
Christoph Lameter3ded1752006-03-25 03:06:44 -08002881 /* See if we can refill from the shared array */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002882 if (n->shared && transfer_objects(ac, n->shared, batchcount)) {
2883 n->shared->touched = 1;
Christoph Lameter3ded1752006-03-25 03:06:44 -08002884 goto alloc_done;
Nick Piggin44b57f12010-01-27 22:27:40 +11002885 }
Christoph Lameter3ded1752006-03-25 03:06:44 -08002886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 while (batchcount > 0) {
2888 struct list_head *entry;
2889 struct slab *slabp;
2890 /* Get slab alloc is to come from. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002891 entry = n->slabs_partial.next;
2892 if (entry == &n->slabs_partial) {
2893 n->free_touched = 1;
2894 entry = n->slabs_free.next;
2895 if (entry == &n->slabs_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 goto must_grow;
2897 }
2898
2899 slabp = list_entry(entry, struct slab, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 check_spinlock_acquired(cachep);
Pekka Enberg714b81712007-05-06 14:49:03 -07002901
2902 /*
2903 * The slab was either on partial or free list so
2904 * there must be at least one object available for
2905 * allocation.
2906 */
Joonsoo Kim106a74e2013-10-24 10:07:48 +09002907 BUG_ON(slabp->active >= cachep->num);
Pekka Enberg714b81712007-05-06 14:49:03 -07002908
Joonsoo Kim106a74e2013-10-24 10:07:48 +09002909 while (slabp->active < cachep->num && batchcount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 STATS_INC_ALLOCED(cachep);
2911 STATS_INC_ACTIVE(cachep);
2912 STATS_SET_HIGH(cachep);
2913
Mel Gorman072bb0a2012-07-31 16:43:58 -07002914 ac_put_obj(cachep, ac, slab_get_obj(cachep, slabp,
2915 node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
2918 /* move slabp to correct slabp list: */
2919 list_del(&slabp->list);
Joonsoo Kim106a74e2013-10-24 10:07:48 +09002920 if (slabp->active == cachep->num)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002921 list_add(&slabp->list, &n->slabs_full);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 else
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002923 list_add(&slabp->list, &n->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 }
2925
Andrew Mortona737b3e2006-03-22 00:08:11 -08002926must_grow:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002927 n->free_objects -= ac->avail;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002928alloc_done:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002929 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
2931 if (unlikely(!ac->avail)) {
2932 int x;
Mel Gorman072bb0a2012-07-31 16:43:58 -07002933force_grow:
Christoph Lameter3c517a62006-12-06 20:33:29 -08002934 x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07002935
Andrew Mortona737b3e2006-03-22 00:08:11 -08002936 /* cache_grow can reenable interrupts, then ac could change. */
Pekka Enberg9a2dba4b2006-02-01 03:05:49 -08002937 ac = cpu_cache_get(cachep);
David Rientjes51cd8e62012-08-28 19:57:21 -07002938 node = numa_mem_id();
Mel Gorman072bb0a2012-07-31 16:43:58 -07002939
2940 /* no objects in sight? abort */
2941 if (!x && (ac->avail == 0 || force_refill))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 return NULL;
2943
Andrew Mortona737b3e2006-03-22 00:08:11 -08002944 if (!ac->avail) /* objects refilled by interrupt? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 goto retry;
2946 }
2947 ac->touched = 1;
Mel Gorman072bb0a2012-07-31 16:43:58 -07002948
2949 return ac_get_obj(cachep, ac, flags, force_refill);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950}
2951
Andrew Mortona737b3e2006-03-22 00:08:11 -08002952static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
2953 gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954{
2955 might_sleep_if(flags & __GFP_WAIT);
2956#if DEBUG
2957 kmem_flagcheck(cachep, flags);
2958#endif
2959}
2960
2961#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08002962static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002963 gfp_t flags, void *objp, unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002965 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 return objp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002967 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002969 if ((cachep->size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002970 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002971 cachep->size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 else
2973 check_poison_obj(cachep, objp);
2974#else
2975 check_poison_obj(cachep, objp);
2976#endif
2977 poison_obj(cachep, objp, POISON_INUSE);
2978 }
2979 if (cachep->flags & SLAB_STORE_USER)
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002980 *dbg_userword(cachep, objp) = (void *)caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981
2982 if (cachep->flags & SLAB_RED_ZONE) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002983 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
2984 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
2985 slab_error(cachep, "double free, or memory outside"
2986 " object was overwritten");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002987 printk(KERN_ERR
David Woodhouseb46b8f12007-05-08 00:22:59 -07002988 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08002989 objp, *dbg_redzone1(cachep, objp),
2990 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 }
2992 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
2993 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
2994 }
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002995 objp += obj_offset(cachep);
Christoph Lameter4f104932007-05-06 14:50:17 -07002996 if (cachep->ctor && cachep->flags & SLAB_POISON)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002997 cachep->ctor(objp);
Tetsuo Handa7ea466f2011-07-21 09:42:45 +09002998 if (ARCH_SLAB_MINALIGN &&
2999 ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003000 printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
Hugh Dickinsc2251502011-07-11 13:35:08 -07003001 objp, (int)ARCH_SLAB_MINALIGN);
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 return objp;
3004}
3005#else
3006#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
3007#endif
3008
Akinobu Mita773ff602008-12-23 19:37:01 +09003009static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags)
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003010{
Christoph Lameter9b030cb2012-09-05 00:20:33 +00003011 if (cachep == kmem_cache)
Akinobu Mita773ff602008-12-23 19:37:01 +09003012 return false;
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003013
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003014 return should_failslab(cachep->object_size, flags, cachep->flags);
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003015}
3016
Pekka Enberg343e0d72006-02-01 03:05:50 -08003017static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003019 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 struct array_cache *ac;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003021 bool force_refill = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
Alok N Kataria5c382302005-09-27 21:45:46 -07003023 check_irq_off();
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003024
Pekka Enberg9a2dba4b2006-02-01 03:05:49 -08003025 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026 if (likely(ac->avail)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 ac->touched = 1;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003028 objp = ac_get_obj(cachep, ac, flags, false);
3029
J. R. Okajimaddbf2e82009-12-02 16:55:50 +09003030 /*
Mel Gorman072bb0a2012-07-31 16:43:58 -07003031 * Allow for the possibility all avail objects are not allowed
3032 * by the current flags
J. R. Okajimaddbf2e82009-12-02 16:55:50 +09003033 */
Mel Gorman072bb0a2012-07-31 16:43:58 -07003034 if (objp) {
3035 STATS_INC_ALLOCHIT(cachep);
3036 goto out;
3037 }
3038 force_refill = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 }
Mel Gorman072bb0a2012-07-31 16:43:58 -07003040
3041 STATS_INC_ALLOCMISS(cachep);
3042 objp = cache_alloc_refill(cachep, flags, force_refill);
3043 /*
3044 * the 'ac' may be updated by cache_alloc_refill(),
3045 * and kmemleak_erase() requires its correct value.
3046 */
3047 ac = cpu_cache_get(cachep);
3048
3049out:
Catalin Marinasd5cff632009-06-11 13:22:40 +01003050 /*
3051 * To avoid a false negative, if an object that is in one of the
3052 * per-CPU caches is leaked, we need to make sure kmemleak doesn't
3053 * treat the array pointers as a reference to the object.
3054 */
J. R. Okajimaf3d8b532009-12-02 16:55:49 +09003055 if (objp)
3056 kmemleak_erase(&ac->entry[ac->avail]);
Alok N Kataria5c382302005-09-27 21:45:46 -07003057 return objp;
3058}
3059
Christoph Lametere498be72005-09-09 13:03:32 -07003060#ifdef CONFIG_NUMA
3061/*
Paul Jacksonb2455392006-03-24 03:16:12 -08003062 * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
Paul Jacksonc61afb12006-03-24 03:16:08 -08003063 *
3064 * If we are in_interrupt, then process context, including cpusets and
3065 * mempolicy, may not apply and should not be used for allocation policy.
3066 */
3067static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
3068{
3069 int nid_alloc, nid_here;
3070
Christoph Lameter765c4502006-09-27 01:50:08 -07003071 if (in_interrupt() || (flags & __GFP_THISNODE))
Paul Jacksonc61afb12006-03-24 03:16:08 -08003072 return NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003073 nid_alloc = nid_here = numa_mem_id();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003074 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
Jack Steiner6adef3e2010-05-26 14:42:49 -07003075 nid_alloc = cpuset_slab_spread_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003076 else if (current->mempolicy)
Andi Kleene7b691b2012-06-09 02:40:03 -07003077 nid_alloc = slab_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003078 if (nid_alloc != nid_here)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003079 return ____cache_alloc_node(cachep, flags, nid_alloc);
Paul Jacksonc61afb12006-03-24 03:16:08 -08003080 return NULL;
3081}
3082
3083/*
Christoph Lameter765c4502006-09-27 01:50:08 -07003084 * Fallback function if there was no memory available and no objects on a
Christoph Lameter3c517a62006-12-06 20:33:29 -08003085 * certain node and fall back is permitted. First we scan all the
Christoph Lameter6a673682013-01-10 19:14:19 +00003086 * available node for available objects. If that fails then we
Christoph Lameter3c517a62006-12-06 20:33:29 -08003087 * perform an allocation without specifying a node. This allows the page
3088 * allocator to do its reclaim / fallback magic. We then insert the
3089 * slab into the proper nodelist and then allocate from it.
Christoph Lameter765c4502006-09-27 01:50:08 -07003090 */
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003091static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
Christoph Lameter765c4502006-09-27 01:50:08 -07003092{
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003093 struct zonelist *zonelist;
3094 gfp_t local_flags;
Mel Gormandd1a2392008-04-28 02:12:17 -07003095 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07003096 struct zone *zone;
3097 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003098 void *obj = NULL;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003099 int nid;
Mel Gormancc9a6c82012-03-21 16:34:11 -07003100 unsigned int cpuset_mems_cookie;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003101
3102 if (flags & __GFP_THISNODE)
3103 return NULL;
3104
Christoph Lameter6cb06222007-10-16 01:25:41 -07003105 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Christoph Lameter765c4502006-09-27 01:50:08 -07003106
Mel Gormancc9a6c82012-03-21 16:34:11 -07003107retry_cpuset:
3108 cpuset_mems_cookie = get_mems_allowed();
Andi Kleene7b691b2012-06-09 02:40:03 -07003109 zonelist = node_zonelist(slab_node(), flags);
Mel Gormancc9a6c82012-03-21 16:34:11 -07003110
Christoph Lameter3c517a62006-12-06 20:33:29 -08003111retry:
3112 /*
3113 * Look through allowed nodes for objects available
3114 * from existing per node queues.
3115 */
Mel Gorman54a6eb52008-04-28 02:12:16 -07003116 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
3117 nid = zone_to_nid(zone);
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003118
Mel Gorman54a6eb52008-04-28 02:12:16 -07003119 if (cpuset_zone_allowed_hardwall(zone, flags) &&
Christoph Lameter6a673682013-01-10 19:14:19 +00003120 cache->node[nid] &&
3121 cache->node[nid]->free_objects) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003122 obj = ____cache_alloc_node(cache,
3123 flags | GFP_THISNODE, nid);
Christoph Lameter481c5342008-06-21 16:46:35 -07003124 if (obj)
3125 break;
3126 }
Christoph Lameter3c517a62006-12-06 20:33:29 -08003127 }
3128
Christoph Lametercfce6602007-05-06 14:50:17 -07003129 if (!obj) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003130 /*
3131 * This allocation will be performed within the constraints
3132 * of the current cpuset / memory policy requirements.
3133 * We may trigger various forms of reclaim on the allowed
3134 * set and go into memory reserves if necessary.
3135 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003136 struct page *page;
3137
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003138 if (local_flags & __GFP_WAIT)
3139 local_irq_enable();
3140 kmem_flagcheck(cache, flags);
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003141 page = kmem_getpages(cache, local_flags, numa_mem_id());
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003142 if (local_flags & __GFP_WAIT)
3143 local_irq_disable();
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003144 if (page) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003145 /*
3146 * Insert into the appropriate per node queues
3147 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003148 nid = page_to_nid(page);
3149 if (cache_grow(cache, flags, nid, page)) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003150 obj = ____cache_alloc_node(cache,
3151 flags | GFP_THISNODE, nid);
3152 if (!obj)
3153 /*
3154 * Another processor may allocate the
3155 * objects in the slab since we are
3156 * not holding any locks.
3157 */
3158 goto retry;
3159 } else {
Hugh Dickinsb6a60452007-01-05 16:36:36 -08003160 /* cache_grow already freed obj */
Christoph Lameter3c517a62006-12-06 20:33:29 -08003161 obj = NULL;
3162 }
3163 }
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003164 }
Mel Gormancc9a6c82012-03-21 16:34:11 -07003165
3166 if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !obj))
3167 goto retry_cpuset;
Christoph Lameter765c4502006-09-27 01:50:08 -07003168 return obj;
3169}
3170
3171/*
Christoph Lametere498be72005-09-09 13:03:32 -07003172 * A interface to enable slab creation on nodeid
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003174static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003175 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07003176{
3177 struct list_head *entry;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003178 struct slab *slabp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003179 struct kmem_cache_node *n;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003180 void *obj;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003181 int x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
Aaron Tomlin14e50c62013-04-26 16:15:34 +01003183 VM_BUG_ON(nodeid > num_online_nodes());
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003184 n = cachep->node[nodeid];
3185 BUG_ON(!n);
Christoph Lametere498be72005-09-09 13:03:32 -07003186
Andrew Mortona737b3e2006-03-22 00:08:11 -08003187retry:
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003188 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003189 spin_lock(&n->list_lock);
3190 entry = n->slabs_partial.next;
3191 if (entry == &n->slabs_partial) {
3192 n->free_touched = 1;
3193 entry = n->slabs_free.next;
3194 if (entry == &n->slabs_free)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003195 goto must_grow;
3196 }
Christoph Lametere498be72005-09-09 13:03:32 -07003197
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003198 slabp = list_entry(entry, struct slab, list);
3199 check_spinlock_acquired_node(cachep, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07003200
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003201 STATS_INC_NODEALLOCS(cachep);
3202 STATS_INC_ACTIVE(cachep);
3203 STATS_SET_HIGH(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003204
Joonsoo Kim106a74e2013-10-24 10:07:48 +09003205 BUG_ON(slabp->active == cachep->num);
Christoph Lametere498be72005-09-09 13:03:32 -07003206
Matthew Dobson78d382d2006-02-01 03:05:47 -08003207 obj = slab_get_obj(cachep, slabp, nodeid);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003208 n->free_objects--;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003209 /* move slabp to correct slabp list: */
3210 list_del(&slabp->list);
Christoph Lametere498be72005-09-09 13:03:32 -07003211
Joonsoo Kim106a74e2013-10-24 10:07:48 +09003212 if (slabp->active == cachep->num)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003213 list_add(&slabp->list, &n->slabs_full);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003214 else
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003215 list_add(&slabp->list, &n->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07003216
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003217 spin_unlock(&n->list_lock);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003218 goto done;
Christoph Lametere498be72005-09-09 13:03:32 -07003219
Andrew Mortona737b3e2006-03-22 00:08:11 -08003220must_grow:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003221 spin_unlock(&n->list_lock);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003222 x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
Christoph Lameter765c4502006-09-27 01:50:08 -07003223 if (x)
3224 goto retry;
Christoph Lametere498be72005-09-09 13:03:32 -07003225
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003226 return fallback_alloc(cachep, flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003227
Andrew Mortona737b3e2006-03-22 00:08:11 -08003228done:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003229 return obj;
Christoph Lametere498be72005-09-09 13:03:32 -07003230}
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003231
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003232static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003233slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003234 unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003235{
3236 unsigned long save_flags;
3237 void *ptr;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003238 int slab_node = numa_mem_id();
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003239
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003240 flags &= gfp_allowed_mask;
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003241
Nick Piggincf40bd12009-01-21 08:12:39 +01003242 lockdep_trace_alloc(flags);
3243
Akinobu Mita773ff602008-12-23 19:37:01 +09003244 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003245 return NULL;
3246
Glauber Costad79923f2012-12-18 14:22:48 -08003247 cachep = memcg_kmem_get_cache(cachep, flags);
3248
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003249 cache_alloc_debugcheck_before(cachep, flags);
3250 local_irq_save(save_flags);
3251
Andrew Mortoneacbbae2011-07-28 13:59:49 -07003252 if (nodeid == NUMA_NO_NODE)
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003253 nodeid = slab_node;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003254
Christoph Lameter6a673682013-01-10 19:14:19 +00003255 if (unlikely(!cachep->node[nodeid])) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003256 /* Node not bootstrapped yet */
3257 ptr = fallback_alloc(cachep, flags);
3258 goto out;
3259 }
3260
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003261 if (nodeid == slab_node) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003262 /*
3263 * Use the locally cached objects if possible.
3264 * However ____cache_alloc does not allow fallback
3265 * to other nodes. It may fail while we still have
3266 * objects on other nodes available.
3267 */
3268 ptr = ____cache_alloc(cachep, flags);
3269 if (ptr)
3270 goto out;
3271 }
3272 /* ___cache_alloc_node can fall back to other nodes */
3273 ptr = ____cache_alloc_node(cachep, flags, nodeid);
3274 out:
3275 local_irq_restore(save_flags);
3276 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003277 kmemleak_alloc_recursive(ptr, cachep->object_size, 1, cachep->flags,
Catalin Marinasd5cff632009-06-11 13:22:40 +01003278 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003279
Pekka Enbergc175eea2008-05-09 20:35:53 +02003280 if (likely(ptr))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003281 kmemcheck_slab_alloc(cachep, flags, ptr, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003282
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003283 if (unlikely((flags & __GFP_ZERO) && ptr))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003284 memset(ptr, 0, cachep->object_size);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003285
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003286 return ptr;
3287}
3288
3289static __always_inline void *
3290__do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
3291{
3292 void *objp;
3293
3294 if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
3295 objp = alternate_node_alloc(cache, flags);
3296 if (objp)
3297 goto out;
3298 }
3299 objp = ____cache_alloc(cache, flags);
3300
3301 /*
3302 * We may just have run out of memory on the local node.
3303 * ____cache_alloc_node() knows how to locate memory on other nodes
3304 */
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003305 if (!objp)
3306 objp = ____cache_alloc_node(cache, flags, numa_mem_id());
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003307
3308 out:
3309 return objp;
3310}
3311#else
3312
3313static __always_inline void *
3314__do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
3315{
3316 return ____cache_alloc(cachep, flags);
3317}
3318
3319#endif /* CONFIG_NUMA */
3320
3321static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003322slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003323{
3324 unsigned long save_flags;
3325 void *objp;
3326
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003327 flags &= gfp_allowed_mask;
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003328
Nick Piggincf40bd12009-01-21 08:12:39 +01003329 lockdep_trace_alloc(flags);
3330
Akinobu Mita773ff602008-12-23 19:37:01 +09003331 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003332 return NULL;
3333
Glauber Costad79923f2012-12-18 14:22:48 -08003334 cachep = memcg_kmem_get_cache(cachep, flags);
3335
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003336 cache_alloc_debugcheck_before(cachep, flags);
3337 local_irq_save(save_flags);
3338 objp = __do_cache_alloc(cachep, flags);
3339 local_irq_restore(save_flags);
3340 objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003341 kmemleak_alloc_recursive(objp, cachep->object_size, 1, cachep->flags,
Catalin Marinasd5cff632009-06-11 13:22:40 +01003342 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003343 prefetchw(objp);
3344
Pekka Enbergc175eea2008-05-09 20:35:53 +02003345 if (likely(objp))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003346 kmemcheck_slab_alloc(cachep, flags, objp, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003347
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003348 if (unlikely((flags & __GFP_ZERO) && objp))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003349 memset(objp, 0, cachep->object_size);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003350
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003351 return objp;
3352}
Christoph Lametere498be72005-09-09 13:03:32 -07003353
3354/*
3355 * Caller needs to acquire correct kmem_list's list_lock
3356 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003357static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003358 int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359{
3360 int i;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003361 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
3363 for (i = 0; i < nr_objects; i++) {
Mel Gorman072bb0a2012-07-31 16:43:58 -07003364 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
Mel Gorman072bb0a2012-07-31 16:43:58 -07003367 clear_obj_pfmemalloc(&objpp[i]);
3368 objp = objpp[i];
3369
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003370 slabp = virt_to_slab(objp);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003371 n = cachep->node[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 list_del(&slabp->list);
Christoph Lameterff694162005-09-22 21:44:02 -07003373 check_spinlock_acquired_node(cachep, node);
Matthew Dobson78d382d2006-02-01 03:05:47 -08003374 slab_put_obj(cachep, slabp, objp, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 STATS_DEC_ACTIVE(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003376 n->free_objects++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377
3378 /* fixup slab chains */
Joonsoo Kim106a74e2013-10-24 10:07:48 +09003379 if (slabp->active == 0) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003380 if (n->free_objects > n->free_limit) {
3381 n->free_objects -= cachep->num;
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07003382 /* No need to drop any previously held
3383 * lock here, even if we have a off-slab slab
3384 * descriptor it is guaranteed to come from
3385 * a different cache, refer to comments before
3386 * alloc_slabmgmt.
3387 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 slab_destroy(cachep, slabp);
3389 } else {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003390 list_add(&slabp->list, &n->slabs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 }
3392 } else {
3393 /* Unconditionally move a slab to the end of the
3394 * partial list on free - maximum time for the
3395 * other objects to be freed, too.
3396 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003397 list_add_tail(&slabp->list, &n->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 }
3399 }
3400}
3401
Pekka Enberg343e0d72006-02-01 03:05:50 -08003402static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403{
3404 int batchcount;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003405 struct kmem_cache_node *n;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003406 int node = numa_mem_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407
3408 batchcount = ac->batchcount;
3409#if DEBUG
3410 BUG_ON(!batchcount || batchcount > ac->avail);
3411#endif
3412 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003413 n = cachep->node[node];
3414 spin_lock(&n->list_lock);
3415 if (n->shared) {
3416 struct array_cache *shared_array = n->shared;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003417 int max = shared_array->limit - shared_array->avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418 if (max) {
3419 if (batchcount > max)
3420 batchcount = max;
Christoph Lametere498be72005-09-09 13:03:32 -07003421 memcpy(&(shared_array->entry[shared_array->avail]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003422 ac->entry, sizeof(void *) * batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 shared_array->avail += batchcount;
3424 goto free_done;
3425 }
3426 }
3427
Christoph Lameterff694162005-09-22 21:44:02 -07003428 free_block(cachep, ac->entry, batchcount, node);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003429free_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430#if STATS
3431 {
3432 int i = 0;
3433 struct list_head *p;
3434
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003435 p = n->slabs_free.next;
3436 while (p != &(n->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 struct slab *slabp;
3438
3439 slabp = list_entry(p, struct slab, list);
Joonsoo Kim106a74e2013-10-24 10:07:48 +09003440 BUG_ON(slabp->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441
3442 i++;
3443 p = p->next;
3444 }
3445 STATS_SET_FREEABLE(cachep, i);
3446 }
3447#endif
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003448 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 ac->avail -= batchcount;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003450 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451}
3452
3453/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08003454 * Release an obj back to its cache. If the obj has a constructed state, it must
3455 * be in this state _before_ it is released. Called with disabled ints.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 */
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003457static inline void __cache_free(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003458 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459{
Pekka Enberg9a2dba4b2006-02-01 03:05:49 -08003460 struct array_cache *ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
3462 check_irq_off();
Catalin Marinasd5cff632009-06-11 13:22:40 +01003463 kmemleak_free_recursive(objp, cachep->flags);
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003464 objp = cache_free_debugcheck(cachep, objp, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003466 kmemcheck_slab_free(cachep, objp, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003467
Siddha, Suresh B1807a1a2007-08-22 14:01:49 -07003468 /*
3469 * Skip calling cache_free_alien() when the platform is not numa.
3470 * This will avoid cache misses that happen while accessing slabp (which
3471 * is per page memory reference) to get nodeid. Instead use a global
3472 * variable to skip the call, which is mostly likely to be present in
3473 * the cache.
3474 */
Mel Gormanb6e68bc2009-06-16 15:32:16 -07003475 if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07003476 return;
Christoph Lametere498be72005-09-09 13:03:32 -07003477
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 if (likely(ac->avail < ac->limit)) {
3479 STATS_INC_FREEHIT(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 } else {
3481 STATS_INC_FREEMISS(cachep);
3482 cache_flusharray(cachep, ac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483 }
Zhao Jin42c8c992011-08-27 00:26:17 +08003484
Mel Gorman072bb0a2012-07-31 16:43:58 -07003485 ac_put_obj(cachep, ac, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003486}
3487
3488/**
3489 * kmem_cache_alloc - Allocate an object
3490 * @cachep: The cache to allocate from.
3491 * @flags: See kmalloc().
3492 *
3493 * Allocate an object from this cache. The flags are only relevant
3494 * if the cache has no available objects.
3495 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003496void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003498 void *ret = slab_alloc(cachep, flags, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003499
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003500 trace_kmem_cache_alloc(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003501 cachep->object_size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003502
3503 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504}
3505EXPORT_SYMBOL(kmem_cache_alloc);
3506
Li Zefan0f24f122009-12-11 15:45:30 +08003507#ifdef CONFIG_TRACING
Steven Rostedt85beb582010-11-24 16:23:34 -05003508void *
Ezequiel Garcia40521472012-09-08 17:47:56 -03003509kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003510{
Steven Rostedt85beb582010-11-24 16:23:34 -05003511 void *ret;
3512
Ezequiel Garcia48356302012-09-08 17:47:57 -03003513 ret = slab_alloc(cachep, flags, _RET_IP_);
Steven Rostedt85beb582010-11-24 16:23:34 -05003514
3515 trace_kmalloc(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003516 size, cachep->size, flags);
Steven Rostedt85beb582010-11-24 16:23:34 -05003517 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003518}
Steven Rostedt85beb582010-11-24 16:23:34 -05003519EXPORT_SYMBOL(kmem_cache_alloc_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003520#endif
3521
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522#ifdef CONFIG_NUMA
Zhouping Liud0d04b72013-05-16 11:36:23 +08003523/**
3524 * kmem_cache_alloc_node - Allocate an object on the specified node
3525 * @cachep: The cache to allocate from.
3526 * @flags: See kmalloc().
3527 * @nodeid: node number of the target node.
3528 *
3529 * Identical to kmem_cache_alloc but it will allocate memory on the given
3530 * node, which can improve the performance for cpu bound structures.
3531 *
3532 * Fallback to other node is possible if __GFP_THISNODE is not set.
3533 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003534void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
3535{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003536 void *ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003537
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003538 trace_kmem_cache_alloc_node(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003539 cachep->object_size, cachep->size,
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003540 flags, nodeid);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003541
3542 return ret;
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003543}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544EXPORT_SYMBOL(kmem_cache_alloc_node);
3545
Li Zefan0f24f122009-12-11 15:45:30 +08003546#ifdef CONFIG_TRACING
Ezequiel Garcia40521472012-09-08 17:47:56 -03003547void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
Steven Rostedt85beb582010-11-24 16:23:34 -05003548 gfp_t flags,
Ezequiel Garcia40521472012-09-08 17:47:56 -03003549 int nodeid,
3550 size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003551{
Steven Rostedt85beb582010-11-24 16:23:34 -05003552 void *ret;
3553
Ezequiel Garcia592f4142012-09-25 08:07:08 -03003554 ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003555
Steven Rostedt85beb582010-11-24 16:23:34 -05003556 trace_kmalloc_node(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003557 size, cachep->size,
Steven Rostedt85beb582010-11-24 16:23:34 -05003558 flags, nodeid);
3559 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003560}
Steven Rostedt85beb582010-11-24 16:23:34 -05003561EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003562#endif
3563
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003564static __always_inline void *
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003565__do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller)
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003566{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003567 struct kmem_cache *cachep;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003568
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003569 cachep = kmalloc_slab(size, flags);
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003570 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3571 return cachep;
Ezequiel Garcia40521472012-09-08 17:47:56 -03003572 return kmem_cache_alloc_node_trace(cachep, flags, node, size);
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003573}
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003574
Li Zefan0bb38a52009-12-11 15:45:50 +08003575#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003576void *__kmalloc_node(size_t size, gfp_t flags, int node)
3577{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003578 return __do_kmalloc_node(size, flags, node, _RET_IP_);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003579}
Christoph Hellwigdbe5e692006-09-25 23:31:36 -07003580EXPORT_SYMBOL(__kmalloc_node);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003581
3582void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003583 int node, unsigned long caller)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003584{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003585 return __do_kmalloc_node(size, flags, node, caller);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003586}
3587EXPORT_SYMBOL(__kmalloc_node_track_caller);
3588#else
3589void *__kmalloc_node(size_t size, gfp_t flags, int node)
3590{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003591 return __do_kmalloc_node(size, flags, node, 0);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003592}
3593EXPORT_SYMBOL(__kmalloc_node);
Li Zefan0bb38a52009-12-11 15:45:50 +08003594#endif /* CONFIG_DEBUG_SLAB || CONFIG_TRACING */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003595#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596
3597/**
Paul Drynoff800590f2006-06-23 02:03:48 -07003598 * __do_kmalloc - allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 * @size: how many bytes of memory are required.
Paul Drynoff800590f2006-06-23 02:03:48 -07003600 * @flags: the type of memory to allocate (see kmalloc).
Randy Dunlap911851e2006-03-22 00:08:14 -08003601 * @caller: function caller for debug tracking of the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 */
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003603static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003604 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003606 struct kmem_cache *cachep;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003607 void *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003609 /* If you want to save a few bytes .text space: replace
3610 * __ with kmem_.
3611 * Then kmalloc uses the uninlined functions instead of the inline
3612 * functions.
3613 */
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003614 cachep = kmalloc_slab(size, flags);
Linus Torvaldsa5c96d82007-07-19 13:17:15 -07003615 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3616 return cachep;
Ezequiel Garcia48356302012-09-08 17:47:57 -03003617 ret = slab_alloc(cachep, flags, caller);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003618
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003619 trace_kmalloc(caller, ret,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003620 size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003621
3622 return ret;
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003623}
3624
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003625
Li Zefan0bb38a52009-12-11 15:45:50 +08003626#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003627void *__kmalloc(size_t size, gfp_t flags)
3628{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003629 return __do_kmalloc(size, flags, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630}
3631EXPORT_SYMBOL(__kmalloc);
3632
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003633void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003634{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003635 return __do_kmalloc(size, flags, caller);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003636}
3637EXPORT_SYMBOL(__kmalloc_track_caller);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003638
3639#else
3640void *__kmalloc(size_t size, gfp_t flags)
3641{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003642 return __do_kmalloc(size, flags, 0);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003643}
3644EXPORT_SYMBOL(__kmalloc);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003645#endif
3646
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647/**
3648 * kmem_cache_free - Deallocate an object
3649 * @cachep: The cache the allocation was from.
3650 * @objp: The previously allocated object.
3651 *
3652 * Free an object which was previously allocated from this
3653 * cache.
3654 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003655void kmem_cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656{
3657 unsigned long flags;
Glauber Costab9ce5ef2012-12-18 14:22:46 -08003658 cachep = cache_from_obj(cachep, objp);
3659 if (!cachep)
3660 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661
3662 local_irq_save(flags);
Feng Tangd97d4762012-07-02 14:29:10 +08003663 debug_check_no_locks_freed(objp, cachep->object_size);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07003664 if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003665 debug_check_no_obj_freed(objp, cachep->object_size);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003666 __cache_free(cachep, objp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 local_irq_restore(flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003668
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003669 trace_kmem_cache_free(_RET_IP_, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670}
3671EXPORT_SYMBOL(kmem_cache_free);
3672
3673/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674 * kfree - free previously allocated memory
3675 * @objp: pointer returned by kmalloc.
3676 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07003677 * If @objp is NULL, no operation is performed.
3678 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 * Don't free memory not originally allocated by kmalloc()
3680 * or you will run into trouble.
3681 */
3682void kfree(const void *objp)
3683{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003684 struct kmem_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 unsigned long flags;
3686
Pekka Enberg2121db72009-03-25 11:05:57 +02003687 trace_kfree(_RET_IP_, objp);
3688
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003689 if (unlikely(ZERO_OR_NULL_PTR(objp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 return;
3691 local_irq_save(flags);
3692 kfree_debugcheck(objp);
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003693 c = virt_to_cache(objp);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003694 debug_check_no_locks_freed(objp, c->object_size);
3695
3696 debug_check_no_obj_freed(objp, c->object_size);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003697 __cache_free(c, (void *)objp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 local_irq_restore(flags);
3699}
3700EXPORT_SYMBOL(kfree);
3701
Christoph Lametere498be72005-09-09 13:03:32 -07003702/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003703 * This initializes kmem_cache_node or resizes various caches for all nodes.
Christoph Lametere498be72005-09-09 13:03:32 -07003704 */
Pekka Enberg83b519e2009-06-10 19:40:04 +03003705static int alloc_kmemlist(struct kmem_cache *cachep, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -07003706{
3707 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003708 struct kmem_cache_node *n;
Christoph Lametercafeb022006-03-25 03:06:46 -08003709 struct array_cache *new_shared;
Paul Menage3395ee02006-12-06 20:32:16 -08003710 struct array_cache **new_alien = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003711
Mel Gorman9c09a952008-01-24 05:49:54 -08003712 for_each_online_node(node) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003713
Paul Menage3395ee02006-12-06 20:32:16 -08003714 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03003715 new_alien = alloc_alien_cache(node, cachep->limit, gfp);
Paul Menage3395ee02006-12-06 20:32:16 -08003716 if (!new_alien)
3717 goto fail;
3718 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003719
Eric Dumazet63109842007-05-06 14:49:28 -07003720 new_shared = NULL;
3721 if (cachep->shared) {
3722 new_shared = alloc_arraycache(node,
Christoph Lameter0718dc22006-03-25 03:06:47 -08003723 cachep->shared*cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003724 0xbaadf00d, gfp);
Eric Dumazet63109842007-05-06 14:49:28 -07003725 if (!new_shared) {
3726 free_alien_cache(new_alien);
3727 goto fail;
3728 }
Christoph Lameter0718dc22006-03-25 03:06:47 -08003729 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003730
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003731 n = cachep->node[node];
3732 if (n) {
3733 struct array_cache *shared = n->shared;
Christoph Lametercafeb022006-03-25 03:06:46 -08003734
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003735 spin_lock_irq(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07003736
Christoph Lametercafeb022006-03-25 03:06:46 -08003737 if (shared)
Christoph Lameter0718dc22006-03-25 03:06:47 -08003738 free_block(cachep, shared->entry,
3739 shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07003740
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003741 n->shared = new_shared;
3742 if (!n->alien) {
3743 n->alien = new_alien;
Christoph Lametere498be72005-09-09 13:03:32 -07003744 new_alien = NULL;
3745 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003746 n->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003747 cachep->batchcount + cachep->num;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003748 spin_unlock_irq(&n->list_lock);
Christoph Lametercafeb022006-03-25 03:06:46 -08003749 kfree(shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003750 free_alien_cache(new_alien);
3751 continue;
3752 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003753 n = kmalloc_node(sizeof(struct kmem_cache_node), gfp, node);
3754 if (!n) {
Christoph Lameter0718dc22006-03-25 03:06:47 -08003755 free_alien_cache(new_alien);
3756 kfree(new_shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003757 goto fail;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003758 }
Christoph Lametere498be72005-09-09 13:03:32 -07003759
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003760 kmem_cache_node_init(n);
3761 n->next_reap = jiffies + REAPTIMEOUT_LIST3 +
Andrew Mortona737b3e2006-03-22 00:08:11 -08003762 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003763 n->shared = new_shared;
3764 n->alien = new_alien;
3765 n->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003766 cachep->batchcount + cachep->num;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003767 cachep->node[node] = n;
Christoph Lametere498be72005-09-09 13:03:32 -07003768 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003769 return 0;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003770
Andrew Mortona737b3e2006-03-22 00:08:11 -08003771fail:
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003772 if (!cachep->list.next) {
Christoph Lameter0718dc22006-03-25 03:06:47 -08003773 /* Cache is not active yet. Roll back what we did */
3774 node--;
3775 while (node >= 0) {
Christoph Lameter6a673682013-01-10 19:14:19 +00003776 if (cachep->node[node]) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003777 n = cachep->node[node];
Christoph Lameter0718dc22006-03-25 03:06:47 -08003778
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003779 kfree(n->shared);
3780 free_alien_cache(n->alien);
3781 kfree(n);
Christoph Lameter6a673682013-01-10 19:14:19 +00003782 cachep->node[node] = NULL;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003783 }
3784 node--;
3785 }
3786 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003787 return -ENOMEM;
Christoph Lametere498be72005-09-09 13:03:32 -07003788}
3789
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790struct ccupdate_struct {
Pekka Enberg343e0d72006-02-01 03:05:50 -08003791 struct kmem_cache *cachep;
Eric Dumazetacfe7d72011-07-25 08:55:42 +02003792 struct array_cache *new[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793};
3794
3795static void do_ccupdate_local(void *info)
3796{
Andrew Mortona737b3e2006-03-22 00:08:11 -08003797 struct ccupdate_struct *new = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 struct array_cache *old;
3799
3800 check_irq_off();
Pekka Enberg9a2dba4b2006-02-01 03:05:49 -08003801 old = cpu_cache_get(new->cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003802
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
3804 new->new[smp_processor_id()] = old;
3805}
3806
Christoph Lameter18004c52012-07-06 15:25:12 -05003807/* Always called with the slab_mutex held */
Glauber Costa943a4512012-12-18 14:23:03 -08003808static int __do_tune_cpucache(struct kmem_cache *cachep, int limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003809 int batchcount, int shared, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810{
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003811 struct ccupdate_struct *new;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003812 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813
Eric Dumazetacfe7d72011-07-25 08:55:42 +02003814 new = kzalloc(sizeof(*new) + nr_cpu_ids * sizeof(struct array_cache *),
3815 gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003816 if (!new)
3817 return -ENOMEM;
3818
Christoph Lametere498be72005-09-09 13:03:32 -07003819 for_each_online_cpu(i) {
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003820 new->new[i] = alloc_arraycache(cpu_to_mem(i), limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003821 batchcount, gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003822 if (!new->new[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003823 for (i--; i >= 0; i--)
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003824 kfree(new->new[i]);
3825 kfree(new);
Christoph Lametere498be72005-09-09 13:03:32 -07003826 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 }
3828 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003829 new->cachep = cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830
Jens Axboe15c8b6c2008-05-09 09:39:44 +02003831 on_each_cpu(do_ccupdate_local, (void *)new, 1);
Christoph Lametere498be72005-09-09 13:03:32 -07003832
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 check_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 cachep->batchcount = batchcount;
3835 cachep->limit = limit;
Christoph Lametere498be72005-09-09 13:03:32 -07003836 cachep->shared = shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837
Christoph Lametere498be72005-09-09 13:03:32 -07003838 for_each_online_cpu(i) {
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003839 struct array_cache *ccold = new->new[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 if (!ccold)
3841 continue;
Christoph Lameter6a673682013-01-10 19:14:19 +00003842 spin_lock_irq(&cachep->node[cpu_to_mem(i)]->list_lock);
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003843 free_block(cachep, ccold->entry, ccold->avail, cpu_to_mem(i));
Christoph Lameter6a673682013-01-10 19:14:19 +00003844 spin_unlock_irq(&cachep->node[cpu_to_mem(i)]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845 kfree(ccold);
3846 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003847 kfree(new);
Pekka Enberg83b519e2009-06-10 19:40:04 +03003848 return alloc_kmemlist(cachep, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849}
3850
Glauber Costa943a4512012-12-18 14:23:03 -08003851static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
3852 int batchcount, int shared, gfp_t gfp)
3853{
3854 int ret;
3855 struct kmem_cache *c = NULL;
3856 int i = 0;
3857
3858 ret = __do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
3859
3860 if (slab_state < FULL)
3861 return ret;
3862
3863 if ((ret < 0) || !is_root_cache(cachep))
3864 return ret;
3865
Glauber Costaebe945c2012-12-18 14:23:10 -08003866 VM_BUG_ON(!mutex_is_locked(&slab_mutex));
Glauber Costa943a4512012-12-18 14:23:03 -08003867 for_each_memcg_cache_index(i) {
3868 c = cache_from_memcg(cachep, i);
3869 if (c)
3870 /* return value determined by the parent cache only */
3871 __do_tune_cpucache(c, limit, batchcount, shared, gfp);
3872 }
3873
3874 return ret;
3875}
3876
Christoph Lameter18004c52012-07-06 15:25:12 -05003877/* Called with slab_mutex held always */
Pekka Enberg83b519e2009-06-10 19:40:04 +03003878static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879{
3880 int err;
Glauber Costa943a4512012-12-18 14:23:03 -08003881 int limit = 0;
3882 int shared = 0;
3883 int batchcount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
Glauber Costa943a4512012-12-18 14:23:03 -08003885 if (!is_root_cache(cachep)) {
3886 struct kmem_cache *root = memcg_root_cache(cachep);
3887 limit = root->limit;
3888 shared = root->shared;
3889 batchcount = root->batchcount;
3890 }
3891
3892 if (limit && shared && batchcount)
3893 goto skip_setup;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003894 /*
3895 * The head array serves three purposes:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 * - create a LIFO ordering, i.e. return objects that are cache-warm
3897 * - reduce the number of spinlock operations.
Andrew Mortona737b3e2006-03-22 00:08:11 -08003898 * - reduce the number of linked list operations on the slab and
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 * bufctl chains: array operations are cheaper.
3900 * The numbers are guessed, we should auto-tune as described by
3901 * Bonwick.
3902 */
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003903 if (cachep->size > 131072)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 limit = 1;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003905 else if (cachep->size > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 limit = 8;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003907 else if (cachep->size > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 limit = 24;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003909 else if (cachep->size > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910 limit = 54;
3911 else
3912 limit = 120;
3913
Andrew Mortona737b3e2006-03-22 00:08:11 -08003914 /*
3915 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 * allocation behaviour: Most allocs on one cpu, most free operations
3917 * on another cpu. For these cases, an efficient object passing between
3918 * cpus is necessary. This is provided by a shared array. The array
3919 * replaces Bonwick's magazine layer.
3920 * On uniprocessor, it's functionally equivalent (but less efficient)
3921 * to a larger limit. Thus disabled by default.
3922 */
3923 shared = 0;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003924 if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 shared = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926
3927#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003928 /*
3929 * With debugging enabled, large batchcount lead to excessively long
3930 * periods with disabled local interrupts. Limit the batchcount
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931 */
3932 if (limit > 32)
3933 limit = 32;
3934#endif
Glauber Costa943a4512012-12-18 14:23:03 -08003935 batchcount = (limit + 1) / 2;
3936skip_setup:
3937 err = do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 if (err)
3939 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003940 cachep->name, -err);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003941 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942}
3943
Christoph Lameter1b552532006-03-22 00:09:07 -08003944/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003945 * Drain an array if it contains any elements taking the node lock only if
3946 * necessary. Note that the node listlock also protects the array_cache
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003947 * if drain_array() is used on the shared array.
Christoph Lameter1b552532006-03-22 00:09:07 -08003948 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003949static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
Christoph Lameter1b552532006-03-22 00:09:07 -08003950 struct array_cache *ac, int force, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951{
3952 int tofree;
3953
Christoph Lameter1b552532006-03-22 00:09:07 -08003954 if (!ac || !ac->avail)
3955 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 if (ac->touched && !force) {
3957 ac->touched = 0;
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003958 } else {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003959 spin_lock_irq(&n->list_lock);
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003960 if (ac->avail) {
3961 tofree = force ? ac->avail : (ac->limit + 4) / 5;
3962 if (tofree > ac->avail)
3963 tofree = (ac->avail + 1) / 2;
3964 free_block(cachep, ac->entry, tofree, node);
3965 ac->avail -= tofree;
3966 memmove(ac->entry, &(ac->entry[tofree]),
3967 sizeof(void *) * ac->avail);
3968 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003969 spin_unlock_irq(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 }
3971}
3972
3973/**
3974 * cache_reap - Reclaim memory from caches.
Randy Dunlap05fb6bf2007-02-28 20:12:13 -08003975 * @w: work descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 *
3977 * Called from workqueue/eventd every few seconds.
3978 * Purpose:
3979 * - clear the per-cpu caches for this CPU.
3980 * - return freeable pages to the main free memory pool.
3981 *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003982 * If we cannot acquire the cache chain mutex then just give up - we'll try
3983 * again on the next iteration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08003985static void cache_reap(struct work_struct *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986{
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07003987 struct kmem_cache *searchp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003988 struct kmem_cache_node *n;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003989 int node = numa_mem_id();
Jean Delvarebf6aede2009-04-02 16:56:54 -07003990 struct delayed_work *work = to_delayed_work(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Christoph Lameter18004c52012-07-06 15:25:12 -05003992 if (!mutex_trylock(&slab_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 /* Give up. Setup the next iteration. */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08003994 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995
Christoph Lameter18004c52012-07-06 15:25:12 -05003996 list_for_each_entry(searchp, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 check_irq_on();
3998
Christoph Lameter35386e32006-03-22 00:09:05 -08003999 /*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004000 * We only take the node lock if absolutely necessary and we
Christoph Lameter35386e32006-03-22 00:09:05 -08004001 * have established with reasonable certainty that
4002 * we can do some work if the lock was obtained.
4003 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004004 n = searchp->node[node];
Christoph Lameter35386e32006-03-22 00:09:05 -08004005
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004006 reap_alien(searchp, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004008 drain_array(searchp, n, cpu_cache_get(searchp), 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009
Christoph Lameter35386e32006-03-22 00:09:05 -08004010 /*
4011 * These are racy checks but it does not matter
4012 * if we skip one check or scan twice.
4013 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004014 if (time_after(n->next_reap, jiffies))
Christoph Lameter35386e32006-03-22 00:09:05 -08004015 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004017 n->next_reap = jiffies + REAPTIMEOUT_LIST3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004019 drain_array(searchp, n, n->shared, 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004021 if (n->free_touched)
4022 n->free_touched = 0;
Christoph Lametered11d9e2006-06-30 01:55:45 -07004023 else {
4024 int freed;
4025
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004026 freed = drain_freelist(searchp, n, (n->free_limit +
Christoph Lametered11d9e2006-06-30 01:55:45 -07004027 5 * searchp->num - 1) / (5 * searchp->num));
4028 STATS_ADD_REAPED(searchp, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029 }
Christoph Lameter35386e32006-03-22 00:09:05 -08004030next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 cond_resched();
4032 }
4033 check_irq_on();
Christoph Lameter18004c52012-07-06 15:25:12 -05004034 mutex_unlock(&slab_mutex);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004035 next_reap_node();
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004036out:
Andrew Mortona737b3e2006-03-22 00:08:11 -08004037 /* Set up the next iteration */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004038 schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_CPUC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039}
4040
Linus Torvalds158a9622008-01-02 13:04:48 -08004041#ifdef CONFIG_SLABINFO
Glauber Costa0d7561c2012-10-19 18:20:27 +04004042void get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004044 struct slab *slabp;
4045 unsigned long active_objs;
4046 unsigned long num_objs;
4047 unsigned long active_slabs = 0;
4048 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004049 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 char *error = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07004051 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004052 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 active_objs = 0;
4055 num_slabs = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004056 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004057 n = cachep->node[node];
4058 if (!n)
Christoph Lametere498be72005-09-09 13:03:32 -07004059 continue;
4060
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004061 check_irq_on();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004062 spin_lock_irq(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07004063
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004064 list_for_each_entry(slabp, &n->slabs_full, list) {
Joonsoo Kim106a74e2013-10-24 10:07:48 +09004065 if (slabp->active != cachep->num && !error)
Christoph Lametere498be72005-09-09 13:03:32 -07004066 error = "slabs_full accounting error";
4067 active_objs += cachep->num;
4068 active_slabs++;
4069 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004070 list_for_each_entry(slabp, &n->slabs_partial, list) {
Joonsoo Kim106a74e2013-10-24 10:07:48 +09004071 if (slabp->active == cachep->num && !error)
4072 error = "slabs_partial accounting error";
4073 if (!slabp->active && !error)
4074 error = "slabs_partial accounting error";
4075 active_objs += slabp->active;
Christoph Lametere498be72005-09-09 13:03:32 -07004076 active_slabs++;
4077 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004078 list_for_each_entry(slabp, &n->slabs_free, list) {
Joonsoo Kim106a74e2013-10-24 10:07:48 +09004079 if (slabp->active && !error)
4080 error = "slabs_free accounting error";
Christoph Lametere498be72005-09-09 13:03:32 -07004081 num_slabs++;
4082 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004083 free_objects += n->free_objects;
4084 if (n->shared)
4085 shared_avail += n->shared->avail;
Christoph Lametere498be72005-09-09 13:03:32 -07004086
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004087 spin_unlock_irq(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004089 num_slabs += active_slabs;
4090 num_objs = num_slabs * cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07004091 if (num_objs - active_objs != free_objects && !error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 error = "free_objects accounting error";
4093
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004094 name = cachep->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 if (error)
4096 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
4097
Glauber Costa0d7561c2012-10-19 18:20:27 +04004098 sinfo->active_objs = active_objs;
4099 sinfo->num_objs = num_objs;
4100 sinfo->active_slabs = active_slabs;
4101 sinfo->num_slabs = num_slabs;
4102 sinfo->shared_avail = shared_avail;
4103 sinfo->limit = cachep->limit;
4104 sinfo->batchcount = cachep->batchcount;
4105 sinfo->shared = cachep->shared;
4106 sinfo->objects_per_slab = cachep->num;
4107 sinfo->cache_order = cachep->gfporder;
4108}
4109
4110void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *cachep)
4111{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112#if STATS
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004113 { /* node stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 unsigned long high = cachep->high_mark;
4115 unsigned long allocs = cachep->num_allocations;
4116 unsigned long grown = cachep->grown;
4117 unsigned long reaped = cachep->reaped;
4118 unsigned long errors = cachep->errors;
4119 unsigned long max_freeable = cachep->max_freeable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 unsigned long node_allocs = cachep->node_allocs;
Christoph Lametere498be72005-09-09 13:03:32 -07004121 unsigned long node_frees = cachep->node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004122 unsigned long overflows = cachep->node_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
Joe Perchese92dd4f2010-03-26 19:27:58 -07004124 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu "
4125 "%4lu %4lu %4lu %4lu %4lu",
4126 allocs, high, grown,
4127 reaped, errors, max_freeable, node_allocs,
4128 node_frees, overflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 }
4130 /* cpu stats */
4131 {
4132 unsigned long allochit = atomic_read(&cachep->allochit);
4133 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
4134 unsigned long freehit = atomic_read(&cachep->freehit);
4135 unsigned long freemiss = atomic_read(&cachep->freemiss);
4136
4137 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004138 allochit, allocmiss, freehit, freemiss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 }
4140#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141}
4142
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143#define MAX_SLABINFO_WRITE 128
4144/**
4145 * slabinfo_write - Tuning for the slab allocator
4146 * @file: unused
4147 * @buffer: user buffer
4148 * @count: data length
4149 * @ppos: unused
4150 */
Glauber Costab7454ad2012-10-19 18:20:25 +04004151ssize_t slabinfo_write(struct file *file, const char __user *buffer,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004152 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004154 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 int limit, batchcount, shared, res;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004156 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004157
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 if (count > MAX_SLABINFO_WRITE)
4159 return -EINVAL;
4160 if (copy_from_user(&kbuf, buffer, count))
4161 return -EFAULT;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004162 kbuf[MAX_SLABINFO_WRITE] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163
4164 tmp = strchr(kbuf, ' ');
4165 if (!tmp)
4166 return -EINVAL;
4167 *tmp = '\0';
4168 tmp++;
4169 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
4170 return -EINVAL;
4171
4172 /* Find the cache in the chain of caches. */
Christoph Lameter18004c52012-07-06 15:25:12 -05004173 mutex_lock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 res = -EINVAL;
Christoph Lameter18004c52012-07-06 15:25:12 -05004175 list_for_each_entry(cachep, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 if (!strcmp(cachep->name, kbuf)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08004177 if (limit < 1 || batchcount < 1 ||
4178 batchcount > limit || shared < 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07004179 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07004181 res = do_tune_cpucache(cachep, limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03004182 batchcount, shared,
4183 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 }
4185 break;
4186 }
4187 }
Christoph Lameter18004c52012-07-06 15:25:12 -05004188 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 if (res >= 0)
4190 res = count;
4191 return res;
4192}
Al Viro871751e2006-03-25 03:06:39 -08004193
4194#ifdef CONFIG_DEBUG_SLAB_LEAK
4195
4196static void *leaks_start(struct seq_file *m, loff_t *pos)
4197{
Christoph Lameter18004c52012-07-06 15:25:12 -05004198 mutex_lock(&slab_mutex);
4199 return seq_list_start(&slab_caches, *pos);
Al Viro871751e2006-03-25 03:06:39 -08004200}
4201
4202static inline int add_caller(unsigned long *n, unsigned long v)
4203{
4204 unsigned long *p;
4205 int l;
4206 if (!v)
4207 return 1;
4208 l = n[1];
4209 p = n + 2;
4210 while (l) {
4211 int i = l/2;
4212 unsigned long *q = p + 2 * i;
4213 if (*q == v) {
4214 q[1]++;
4215 return 1;
4216 }
4217 if (*q > v) {
4218 l = i;
4219 } else {
4220 p = q + 2;
4221 l -= i + 1;
4222 }
4223 }
4224 if (++n[1] == n[0])
4225 return 0;
4226 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4227 p[0] = v;
4228 p[1] = 1;
4229 return 1;
4230}
4231
4232static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
4233{
4234 void *p;
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09004235 int i, j;
4236
Al Viro871751e2006-03-25 03:06:39 -08004237 if (n[0] == n[1])
4238 return;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004239 for (i = 0, p = s->s_mem; i < c->num; i++, p += c->size) {
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09004240 bool active = true;
4241
Joonsoo Kim106a74e2013-10-24 10:07:48 +09004242 for (j = s->active; j < c->num; j++) {
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09004243 /* Skip freed item */
4244 if (slab_bufctl(s)[j] == i) {
4245 active = false;
4246 break;
4247 }
4248 }
4249 if (!active)
Al Viro871751e2006-03-25 03:06:39 -08004250 continue;
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09004251
Al Viro871751e2006-03-25 03:06:39 -08004252 if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
4253 return;
4254 }
4255}
4256
4257static void show_symbol(struct seq_file *m, unsigned long address)
4258{
4259#ifdef CONFIG_KALLSYMS
Al Viro871751e2006-03-25 03:06:39 -08004260 unsigned long offset, size;
Tejun Heo9281ace2007-07-17 04:03:51 -07004261 char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
Al Viro871751e2006-03-25 03:06:39 -08004262
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004263 if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
Al Viro871751e2006-03-25 03:06:39 -08004264 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004265 if (modname[0])
Al Viro871751e2006-03-25 03:06:39 -08004266 seq_printf(m, " [%s]", modname);
4267 return;
4268 }
4269#endif
4270 seq_printf(m, "%p", (void *)address);
4271}
4272
4273static int leaks_show(struct seq_file *m, void *p)
4274{
Thierry Reding0672aa72012-06-22 19:42:49 +02004275 struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);
Al Viro871751e2006-03-25 03:06:39 -08004276 struct slab *slabp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004277 struct kmem_cache_node *n;
Al Viro871751e2006-03-25 03:06:39 -08004278 const char *name;
Christoph Lameterdb845062013-02-05 18:45:23 +00004279 unsigned long *x = m->private;
Al Viro871751e2006-03-25 03:06:39 -08004280 int node;
4281 int i;
4282
4283 if (!(cachep->flags & SLAB_STORE_USER))
4284 return 0;
4285 if (!(cachep->flags & SLAB_RED_ZONE))
4286 return 0;
4287
4288 /* OK, we can do it */
4289
Christoph Lameterdb845062013-02-05 18:45:23 +00004290 x[1] = 0;
Al Viro871751e2006-03-25 03:06:39 -08004291
4292 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004293 n = cachep->node[node];
4294 if (!n)
Al Viro871751e2006-03-25 03:06:39 -08004295 continue;
4296
4297 check_irq_on();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004298 spin_lock_irq(&n->list_lock);
Al Viro871751e2006-03-25 03:06:39 -08004299
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004300 list_for_each_entry(slabp, &n->slabs_full, list)
Christoph Lameterdb845062013-02-05 18:45:23 +00004301 handle_slab(x, cachep, slabp);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004302 list_for_each_entry(slabp, &n->slabs_partial, list)
Christoph Lameterdb845062013-02-05 18:45:23 +00004303 handle_slab(x, cachep, slabp);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004304 spin_unlock_irq(&n->list_lock);
Al Viro871751e2006-03-25 03:06:39 -08004305 }
4306 name = cachep->name;
Christoph Lameterdb845062013-02-05 18:45:23 +00004307 if (x[0] == x[1]) {
Al Viro871751e2006-03-25 03:06:39 -08004308 /* Increase the buffer size */
Christoph Lameter18004c52012-07-06 15:25:12 -05004309 mutex_unlock(&slab_mutex);
Christoph Lameterdb845062013-02-05 18:45:23 +00004310 m->private = kzalloc(x[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
Al Viro871751e2006-03-25 03:06:39 -08004311 if (!m->private) {
4312 /* Too bad, we are really out */
Christoph Lameterdb845062013-02-05 18:45:23 +00004313 m->private = x;
Christoph Lameter18004c52012-07-06 15:25:12 -05004314 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004315 return -ENOMEM;
4316 }
Christoph Lameterdb845062013-02-05 18:45:23 +00004317 *(unsigned long *)m->private = x[0] * 2;
4318 kfree(x);
Christoph Lameter18004c52012-07-06 15:25:12 -05004319 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004320 /* Now make sure this entry will be retried */
4321 m->count = m->size;
4322 return 0;
4323 }
Christoph Lameterdb845062013-02-05 18:45:23 +00004324 for (i = 0; i < x[1]; i++) {
4325 seq_printf(m, "%s: %lu ", name, x[2*i+3]);
4326 show_symbol(m, x[2*i+2]);
Al Viro871751e2006-03-25 03:06:39 -08004327 seq_putc(m, '\n');
4328 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004329
Al Viro871751e2006-03-25 03:06:39 -08004330 return 0;
4331}
4332
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004333static const struct seq_operations slabstats_op = {
Al Viro871751e2006-03-25 03:06:39 -08004334 .start = leaks_start,
Wanpeng Li276a2432013-07-08 08:08:28 +08004335 .next = slab_next,
4336 .stop = slab_stop,
Al Viro871751e2006-03-25 03:06:39 -08004337 .show = leaks_show,
4338};
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004339
4340static int slabstats_open(struct inode *inode, struct file *file)
4341{
4342 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
4343 int ret = -ENOMEM;
4344 if (n) {
4345 ret = seq_open(file, &slabstats_op);
4346 if (!ret) {
4347 struct seq_file *m = file->private_data;
4348 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
4349 m->private = n;
4350 n = NULL;
4351 }
4352 kfree(n);
4353 }
4354 return ret;
4355}
4356
4357static const struct file_operations proc_slabstats_operations = {
4358 .open = slabstats_open,
4359 .read = seq_read,
4360 .llseek = seq_lseek,
4361 .release = seq_release_private,
4362};
Al Viro871751e2006-03-25 03:06:39 -08004363#endif
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004364
4365static int __init slab_proc_init(void)
4366{
4367#ifdef CONFIG_DEBUG_SLAB_LEAK
4368 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
4369#endif
4370 return 0;
4371}
4372module_init(slab_proc_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373#endif
4374
Manfred Spraul00e145b2005-09-03 15:55:07 -07004375/**
4376 * ksize - get the actual amount of memory allocated for a given object
4377 * @objp: Pointer to the object
4378 *
4379 * kmalloc may internally round up allocations and return more memory
4380 * than requested. ksize() can be used to determine the actual amount of
4381 * memory allocated. The caller may use this additional memory, even though
4382 * a smaller amount of memory was initially specified with the kmalloc call.
4383 * The caller must guarantee that objp points to a valid object previously
4384 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4385 * must not be freed during the duration of the call.
4386 */
Pekka Enbergfd76bab2007-05-06 14:48:40 -07004387size_t ksize(const void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388{
Christoph Lameteref8b4522007-10-16 01:24:46 -07004389 BUG_ON(!objp);
4390 if (unlikely(objp == ZERO_SIZE_PTR))
Manfred Spraul00e145b2005-09-03 15:55:07 -07004391 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392
Christoph Lameter8c138bc2012-06-13 10:24:58 -05004393 return virt_to_cache(objp)->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394}
Kirill A. Shutemovb1aabec2009-02-10 15:21:44 +02004395EXPORT_SYMBOL(ksize);