blob: e4ce73c32a7a509a3e31206cc10985254ccc4c9a [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
Joonsoo Kimf315e3f2013-12-02 17:49:41 +0900160#define FREELIST_BYTE_INDEX (((PAGE_SIZE >> BITS_PER_BYTE) \
161 <= SLAB_OBJ_MIN_SIZE) ? 1 : 0)
162
163#if FREELIST_BYTE_INDEX
164typedef unsigned char freelist_idx_t;
165#else
166typedef unsigned short freelist_idx_t;
167#endif
168
David Miller30321c72014-05-05 16:20:04 -0400169#define SLAB_OBJ_MAX_NUM ((1 << sizeof(freelist_idx_t) * BITS_PER_BYTE) - 1)
Joonsoo Kimf315e3f2013-12-02 17:49:41 +0900170
Mel Gorman072bb0a2012-07-31 16:43:58 -0700171/*
172 * true if a page was allocated from pfmemalloc reserves for network-based
173 * swap
174 */
175static bool pfmemalloc_active __read_mostly;
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * struct array_cache
179 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * Purpose:
181 * - LIFO ordering, to hand out cache-warm objects from _alloc
182 * - reduce the number of linked list operations
183 * - reduce spinlock operations
184 *
185 * The limit is stored in the per-cpu structure to reduce the data cache
186 * footprint.
187 *
188 */
189struct array_cache {
190 unsigned int avail;
191 unsigned int limit;
192 unsigned int batchcount;
193 unsigned int touched;
Robert P. J. Daybda5b652007-10-16 23:30:05 -0700194 void *entry[]; /*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800195 * Must have this definition in here for the proper
196 * alignment of array_cache. Also simplifies accessing
197 * the entries.
Mel Gorman072bb0a2012-07-31 16:43:58 -0700198 *
199 * Entries should not be directly dereferenced as
200 * entries belonging to slabs marked pfmemalloc will
201 * have the lower bits set SLAB_OBJ_PFMEMALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -0800202 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203};
204
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700205struct alien_cache {
206 spinlock_t lock;
207 struct array_cache ac;
208};
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,
Joonsoo Kim97654df2014-08-06 16:04:25 -0700249 int node, struct list_head *list);
250static void slabs_destroy(struct kmem_cache *cachep, struct list_head *list);
Pekka Enberg83b519e2009-06-10 19:40:04 +0300251static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp);
David Howells65f27f32006-11-22 14:55:48 +0000252static void cache_reap(struct work_struct *unused);
Christoph Lametered11d9e2006-06-30 01:55:45 -0700253
Ingo Molnare0a42722006-06-23 02:03:46 -0700254static int slab_early_init = 1;
255
Christoph Lametere3366012013-01-10 19:14:18 +0000256#define INDEX_AC kmalloc_index(sizeof(struct arraycache_init))
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000257#define INDEX_NODE kmalloc_index(sizeof(struct kmem_cache_node))
Christoph Lametere498be72005-09-09 13:03:32 -0700258
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000259static void kmem_cache_node_init(struct kmem_cache_node *parent)
Christoph Lametere498be72005-09-09 13:03:32 -0700260{
261 INIT_LIST_HEAD(&parent->slabs_full);
262 INIT_LIST_HEAD(&parent->slabs_partial);
263 INIT_LIST_HEAD(&parent->slabs_free);
264 parent->shared = NULL;
265 parent->alien = NULL;
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -0800266 parent->colour_next = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700267 spin_lock_init(&parent->list_lock);
268 parent->free_objects = 0;
269 parent->free_touched = 0;
270}
271
Andrew Mortona737b3e2006-03-22 00:08:11 -0800272#define MAKE_LIST(cachep, listp, slab, nodeid) \
273 do { \
274 INIT_LIST_HEAD(listp); \
Christoph Lameter18bf8542014-08-06 16:04:11 -0700275 list_splice(&get_node(cachep, nodeid)->slab, listp); \
Christoph Lametere498be72005-09-09 13:03:32 -0700276 } while (0)
277
Andrew Mortona737b3e2006-03-22 00:08:11 -0800278#define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
279 do { \
Christoph Lametere498be72005-09-09 13:03:32 -0700280 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
281 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
282 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
283 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285#define CFLGS_OFF_SLAB (0x80000000UL)
286#define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
287
288#define BATCHREFILL_LIMIT 16
Andrew Mortona737b3e2006-03-22 00:08:11 -0800289/*
290 * Optimization question: fewer reaps means less probability for unnessary
291 * cpucache drain/refill cycles.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 *
Adrian Bunkdc6f3f22005-11-08 16:44:08 +0100293 * OTOH the cpuarrays can contain lots of objects,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * which could lock up otherwise freeable slabs.
295 */
Jianyu Zhan5f0985b2014-03-30 17:02:20 +0800296#define REAPTIMEOUT_AC (2*HZ)
297#define REAPTIMEOUT_NODE (4*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
299#if STATS
300#define STATS_INC_ACTIVE(x) ((x)->num_active++)
301#define STATS_DEC_ACTIVE(x) ((x)->num_active--)
302#define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
303#define STATS_INC_GROWN(x) ((x)->grown++)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700304#define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
Andrew Mortona737b3e2006-03-22 00:08:11 -0800305#define STATS_SET_HIGH(x) \
306 do { \
307 if ((x)->num_active > (x)->high_mark) \
308 (x)->high_mark = (x)->num_active; \
309 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310#define STATS_INC_ERR(x) ((x)->errors++)
311#define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
Christoph Lametere498be72005-09-09 13:03:32 -0700312#define STATS_INC_NODEFREES(x) ((x)->node_frees++)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700313#define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800314#define STATS_SET_FREEABLE(x, i) \
315 do { \
316 if ((x)->max_freeable < i) \
317 (x)->max_freeable = i; \
318 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319#define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
320#define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
321#define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
322#define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
323#else
324#define STATS_INC_ACTIVE(x) do { } while (0)
325#define STATS_DEC_ACTIVE(x) do { } while (0)
326#define STATS_INC_ALLOCED(x) do { } while (0)
327#define STATS_INC_GROWN(x) do { } while (0)
Andi Kleen4e60c862010-08-09 17:19:03 -0700328#define STATS_ADD_REAPED(x,y) do { (void)(y); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329#define STATS_SET_HIGH(x) do { } while (0)
330#define STATS_INC_ERR(x) do { } while (0)
331#define STATS_INC_NODEALLOCS(x) do { } while (0)
Christoph Lametere498be72005-09-09 13:03:32 -0700332#define STATS_INC_NODEFREES(x) do { } while (0)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700333#define STATS_INC_ACOVERFLOW(x) do { } while (0)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800334#define STATS_SET_FREEABLE(x, i) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335#define STATS_INC_ALLOCHIT(x) do { } while (0)
336#define STATS_INC_ALLOCMISS(x) do { } while (0)
337#define STATS_INC_FREEHIT(x) do { } while (0)
338#define STATS_INC_FREEMISS(x) do { } while (0)
339#endif
340
341#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Andrew Mortona737b3e2006-03-22 00:08:11 -0800343/*
344 * memory layout of objects:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 * 0 : objp
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800346 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 * the end of an object is aligned with the end of the real
348 * allocation. Catches writes behind the end of the allocation.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800349 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 * redzone word.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800351 * cachep->obj_offset: The real object.
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500352 * cachep->size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
353 * cachep->size - 1* BYTES_PER_WORD: last caller address
Andrew Mortona737b3e2006-03-22 00:08:11 -0800354 * [BYTES_PER_WORD long]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800356static int obj_offset(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800358 return cachep->obj_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
David Woodhouseb46b8f12007-05-08 00:22:59 -0700361static unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
David Woodhouseb46b8f12007-05-08 00:22:59 -0700364 return (unsigned long long*) (objp + obj_offset(cachep) -
365 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366}
367
David Woodhouseb46b8f12007-05-08 00:22:59 -0700368static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
370 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
371 if (cachep->flags & SLAB_STORE_USER)
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500372 return (unsigned long long *)(objp + cachep->size -
David Woodhouseb46b8f12007-05-08 00:22:59 -0700373 sizeof(unsigned long long) -
David Woodhouse87a927c2007-07-04 21:26:44 -0400374 REDZONE_ALIGN);
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500375 return (unsigned long long *) (objp + cachep->size -
David Woodhouseb46b8f12007-05-08 00:22:59 -0700376 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
Pekka Enberg343e0d72006-02-01 03:05:50 -0800379static void **dbg_userword(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380{
381 BUG_ON(!(cachep->flags & SLAB_STORE_USER));
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500382 return (void **)(objp + cachep->size - BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
385#else
386
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800387#define obj_offset(x) 0
David Woodhouseb46b8f12007-05-08 00:22:59 -0700388#define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
389#define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390#define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
391
392#endif
393
Joonsoo Kim03787302014-06-23 13:22:06 -0700394#define OBJECT_FREE (0)
395#define OBJECT_ACTIVE (1)
396
397#ifdef CONFIG_DEBUG_SLAB_LEAK
398
399static void set_obj_status(struct page *page, int idx, int val)
400{
401 int freelist_size;
402 char *status;
403 struct kmem_cache *cachep = page->slab_cache;
404
405 freelist_size = cachep->num * sizeof(freelist_idx_t);
406 status = (char *)page->freelist + freelist_size;
407 status[idx] = val;
408}
409
410static inline unsigned int get_obj_status(struct page *page, int idx)
411{
412 int freelist_size;
413 char *status;
414 struct kmem_cache *cachep = page->slab_cache;
415
416 freelist_size = cachep->num * sizeof(freelist_idx_t);
417 status = (char *)page->freelist + freelist_size;
418
419 return status[idx];
420}
421
422#else
423static inline void set_obj_status(struct page *page, int idx, int val) {}
424
425#endif
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427/*
David Rientjes3df1ccc2011-10-18 22:09:28 -0700428 * Do not go above this order unless 0 objects fit into the slab or
429 * overridden on the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 */
David Rientjes543585c2011-10-18 22:09:24 -0700431#define SLAB_MAX_ORDER_HI 1
432#define SLAB_MAX_ORDER_LO 0
433static int slab_max_order = SLAB_MAX_ORDER_LO;
David Rientjes3df1ccc2011-10-18 22:09:28 -0700434static bool slab_max_order_set __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800436static inline struct kmem_cache *virt_to_cache(const void *obj)
437{
Christoph Lameterb49af682007-05-06 14:49:41 -0700438 struct page *page = virt_to_head_page(obj);
Christoph Lameter35026082012-06-13 10:24:56 -0500439 return page->slab_cache;
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800440}
441
Joonsoo Kim8456a642013-10-24 10:07:49 +0900442static inline void *index_to_obj(struct kmem_cache *cache, struct page *page,
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800443 unsigned int idx)
444{
Joonsoo Kim8456a642013-10-24 10:07:49 +0900445 return page->s_mem + cache->size * idx;
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800446}
447
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800448/*
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500449 * We want to avoid an expensive divide : (offset / cache->size)
450 * Using the fact that size is a constant for a particular cache,
451 * we can replace (offset / cache->size) by
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800452 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
453 */
454static inline unsigned int obj_to_index(const struct kmem_cache *cache,
Joonsoo Kim8456a642013-10-24 10:07:49 +0900455 const struct page *page, void *obj)
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800456{
Joonsoo Kim8456a642013-10-24 10:07:49 +0900457 u32 offset = (obj - page->s_mem);
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800458 return reciprocal_divide(offset, cache->reciprocal_buffer_size);
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800459}
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461static struct arraycache_init initarray_generic =
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800462 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463
464/* internal cache of cache description objs */
Christoph Lameter9b030cb2012-09-05 00:20:33 +0000465static struct kmem_cache kmem_cache_boot = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800466 .batchcount = 1,
467 .limit = BOOT_CPUCACHE_ENTRIES,
468 .shared = 1,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500469 .size = sizeof(struct kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800470 .name = "kmem_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471};
472
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700473#define BAD_ALIEN_MAGIC 0x01020304ul
474
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200475#ifdef CONFIG_LOCKDEP
476
477/*
478 * Slab sometimes uses the kmalloc slabs to store the slab headers
479 * for other slabs "off slab".
480 * The locking for this is tricky in that it nests within the locks
481 * of all other slabs in a few places; to deal with this special
482 * locking we put on-slab caches into a separate lock-class.
483 *
484 * We set lock class for alien array caches which are up during init.
485 * The lock annotation will be lost if all cpus of a node goes down and
486 * then comes back up during hotplug
487 */
488static struct lock_class_key on_slab_l3_key;
489static struct lock_class_key on_slab_alc_key;
490
Peter Zijlstra83835b32011-07-22 15:26:05 +0200491static struct lock_class_key debugobj_l3_key;
492static struct lock_class_key debugobj_alc_key;
493
494static void slab_set_lock_classes(struct kmem_cache *cachep,
495 struct lock_class_key *l3_key, struct lock_class_key *alc_key,
Christoph Lameter18bf8542014-08-06 16:04:11 -0700496 struct kmem_cache_node *n)
Peter Zijlstra83835b32011-07-22 15:26:05 +0200497{
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700498 struct alien_cache **alc;
Peter Zijlstra83835b32011-07-22 15:26:05 +0200499 int r;
500
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000501 lockdep_set_class(&n->list_lock, l3_key);
502 alc = n->alien;
Peter Zijlstra83835b32011-07-22 15:26:05 +0200503 /*
504 * FIXME: This check for BAD_ALIEN_MAGIC
505 * should go away when common slab code is taught to
506 * work even without alien caches.
507 * Currently, non NUMA code returns BAD_ALIEN_MAGIC
508 * for alloc_alien_cache,
509 */
510 if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC)
511 return;
512 for_each_node(r) {
513 if (alc[r])
Joonsoo Kim49dfc302014-08-06 16:04:31 -0700514 lockdep_set_class(&(alc[r]->lock), alc_key);
Peter Zijlstra83835b32011-07-22 15:26:05 +0200515 }
516}
517
Christoph Lameter18bf8542014-08-06 16:04:11 -0700518static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep,
519 struct kmem_cache_node *n)
Peter Zijlstra83835b32011-07-22 15:26:05 +0200520{
Christoph Lameter18bf8542014-08-06 16:04:11 -0700521 slab_set_lock_classes(cachep, &debugobj_l3_key, &debugobj_alc_key, n);
Peter Zijlstra83835b32011-07-22 15:26:05 +0200522}
523
524static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
525{
526 int node;
Christoph Lameter18bf8542014-08-06 16:04:11 -0700527 struct kmem_cache_node *n;
Peter Zijlstra83835b32011-07-22 15:26:05 +0200528
Christoph Lameter18bf8542014-08-06 16:04:11 -0700529 for_each_kmem_cache_node(cachep, node, n)
530 slab_set_debugobj_lock_classes_node(cachep, n);
Peter Zijlstra83835b32011-07-22 15:26:05 +0200531}
532
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200533static void init_node_lock_keys(int q)
534{
Christoph Lametere3366012013-01-10 19:14:18 +0000535 int i;
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200536
Christoph Lameter97d06602012-07-06 15:25:11 -0500537 if (slab_state < UP)
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200538 return;
539
Christoph Lameter0f8f8092013-07-02 12:12:10 -0700540 for (i = 1; i <= KMALLOC_SHIFT_HIGH; i++) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000541 struct kmem_cache_node *n;
Christoph Lametere3366012013-01-10 19:14:18 +0000542 struct kmem_cache *cache = kmalloc_caches[i];
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200543
Christoph Lametere3366012013-01-10 19:14:18 +0000544 if (!cache)
Pekka Enberg00afa752009-12-27 14:33:14 +0200545 continue;
Peter Zijlstra83835b32011-07-22 15:26:05 +0200546
Christoph Lameter18bf8542014-08-06 16:04:11 -0700547 n = get_node(cache, q);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000548 if (!n || OFF_SLAB(cache))
Christoph Lametere3366012013-01-10 19:14:18 +0000549 continue;
550
551 slab_set_lock_classes(cache, &on_slab_l3_key,
Christoph Lameter18bf8542014-08-06 16:04:11 -0700552 &on_slab_alc_key, n);
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200553 }
554}
555
Christoph Lameter18bf8542014-08-06 16:04:11 -0700556static void on_slab_lock_classes_node(struct kmem_cache *cachep,
557 struct kmem_cache_node *n)
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800558{
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800559 slab_set_lock_classes(cachep, &on_slab_l3_key,
Christoph Lameter18bf8542014-08-06 16:04:11 -0700560 &on_slab_alc_key, n);
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800561}
562
563static inline void on_slab_lock_classes(struct kmem_cache *cachep)
564{
565 int node;
Christoph Lameter18bf8542014-08-06 16:04:11 -0700566 struct kmem_cache_node *n;
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800567
568 VM_BUG_ON(OFF_SLAB(cachep));
Christoph Lameter18bf8542014-08-06 16:04:11 -0700569 for_each_kmem_cache_node(cachep, node, n)
570 on_slab_lock_classes_node(cachep, n);
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800571}
572
Fabian Frederick1536cb32014-08-06 16:04:05 -0700573static inline void __init init_lock_keys(void)
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200574{
575 int node;
576
577 for_each_node(node)
578 init_node_lock_keys(node);
579}
580#else
Fabian Frederick1536cb32014-08-06 16:04:05 -0700581static void __init init_node_lock_keys(int q)
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200582{
583}
584
585static inline void init_lock_keys(void)
586{
587}
Peter Zijlstra83835b32011-07-22 15:26:05 +0200588
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800589static inline void on_slab_lock_classes(struct kmem_cache *cachep)
590{
591}
592
Christoph Lameter18bf8542014-08-06 16:04:11 -0700593static inline void on_slab_lock_classes_node(struct kmem_cache *cachep,
594 struct kmem_cache_node *n)
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800595{
596}
597
Christoph Lameter18bf8542014-08-06 16:04:11 -0700598static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep,
599 struct kmem_cache_node *n)
Peter Zijlstra83835b32011-07-22 15:26:05 +0200600{
601}
602
603static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
604{
605}
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200606#endif
607
Tejun Heo1871e522009-10-29 22:34:13 +0900608static DEFINE_PER_CPU(struct delayed_work, slab_reap_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Pekka Enberg343e0d72006-02-01 03:05:50 -0800610static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
612 return cachep->array[smp_processor_id()];
613}
614
Joonsoo Kim03787302014-06-23 13:22:06 -0700615static size_t calculate_freelist_size(int nr_objs, size_t align)
616{
617 size_t freelist_size;
618
619 freelist_size = nr_objs * sizeof(freelist_idx_t);
620 if (IS_ENABLED(CONFIG_DEBUG_SLAB_LEAK))
621 freelist_size += nr_objs * sizeof(char);
622
623 if (align)
624 freelist_size = ALIGN(freelist_size, align);
625
626 return freelist_size;
627}
628
Joonsoo Kim9cef2e22013-12-02 17:49:39 +0900629static int calculate_nr_objs(size_t slab_size, size_t buffer_size,
630 size_t idx_size, size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631{
Joonsoo Kim9cef2e22013-12-02 17:49:39 +0900632 int nr_objs;
Joonsoo Kim03787302014-06-23 13:22:06 -0700633 size_t remained_size;
Joonsoo Kim9cef2e22013-12-02 17:49:39 +0900634 size_t freelist_size;
Joonsoo Kim03787302014-06-23 13:22:06 -0700635 int extra_space = 0;
Joonsoo Kim9cef2e22013-12-02 17:49:39 +0900636
Joonsoo Kim03787302014-06-23 13:22:06 -0700637 if (IS_ENABLED(CONFIG_DEBUG_SLAB_LEAK))
638 extra_space = sizeof(char);
Joonsoo Kim9cef2e22013-12-02 17:49:39 +0900639 /*
640 * Ignore padding for the initial guess. The padding
641 * is at most @align-1 bytes, and @buffer_size is at
642 * least @align. In the worst case, this result will
643 * be one greater than the number of objects that fit
644 * into the memory allocation when taking the padding
645 * into account.
646 */
Joonsoo Kim03787302014-06-23 13:22:06 -0700647 nr_objs = slab_size / (buffer_size + idx_size + extra_space);
Joonsoo Kim9cef2e22013-12-02 17:49:39 +0900648
649 /*
650 * This calculated number will be either the right
651 * amount, or one greater than what we want.
652 */
Joonsoo Kim03787302014-06-23 13:22:06 -0700653 remained_size = slab_size - nr_objs * buffer_size;
654 freelist_size = calculate_freelist_size(nr_objs, align);
655 if (remained_size < freelist_size)
Joonsoo Kim9cef2e22013-12-02 17:49:39 +0900656 nr_objs--;
657
658 return nr_objs;
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800659}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Andrew Mortona737b3e2006-03-22 00:08:11 -0800661/*
662 * Calculate the number of objects and left-over bytes for a given buffer size.
663 */
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800664static void cache_estimate(unsigned long gfporder, size_t buffer_size,
665 size_t align, int flags, size_t *left_over,
666 unsigned int *num)
667{
668 int nr_objs;
669 size_t mgmt_size;
670 size_t slab_size = PAGE_SIZE << gfporder;
671
672 /*
673 * The slab management structure can be either off the slab or
674 * on it. For the latter case, the memory allocated for a
675 * slab is used for:
676 *
Joonsoo Kim16025172013-10-24 10:07:46 +0900677 * - One unsigned int for each object
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800678 * - Padding to respect alignment of @align
679 * - @buffer_size bytes for each object
680 *
681 * If the slab management structure is off the slab, then the
682 * alignment will already be calculated into the size. Because
683 * the slabs are all pages aligned, the objects will be at the
684 * correct alignment when allocated.
685 */
686 if (flags & CFLGS_OFF_SLAB) {
687 mgmt_size = 0;
688 nr_objs = slab_size / buffer_size;
689
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800690 } else {
Joonsoo Kim9cef2e22013-12-02 17:49:39 +0900691 nr_objs = calculate_nr_objs(slab_size, buffer_size,
Joonsoo Kima41adfa2013-12-02 17:49:42 +0900692 sizeof(freelist_idx_t), align);
Joonsoo Kim03787302014-06-23 13:22:06 -0700693 mgmt_size = calculate_freelist_size(nr_objs, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 }
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800695 *num = nr_objs;
696 *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697}
698
Christoph Lameterf28510d2012-09-11 19:49:38 +0000699#if DEBUG
Harvey Harrisond40cee22008-04-30 00:55:07 -0700700#define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Andrew Mortona737b3e2006-03-22 00:08:11 -0800702static void __slab_error(const char *function, struct kmem_cache *cachep,
703 char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800706 function, cachep->name, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +1030708 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
Christoph Lameterf28510d2012-09-11 19:49:38 +0000710#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Paul Menage3395ee02006-12-06 20:32:16 -0800712/*
713 * By default on NUMA we use alien caches to stage the freeing of
714 * objects allocated from other nodes. This causes massive memory
715 * inefficiencies when using fake NUMA setup to split memory into a
716 * large number of small nodes, so it can be disabled on the command
717 * line
718 */
719
720static int use_alien_caches __read_mostly = 1;
721static int __init noaliencache_setup(char *s)
722{
723 use_alien_caches = 0;
724 return 1;
725}
726__setup("noaliencache", noaliencache_setup);
727
David Rientjes3df1ccc2011-10-18 22:09:28 -0700728static int __init slab_max_order_setup(char *str)
729{
730 get_option(&str, &slab_max_order);
731 slab_max_order = slab_max_order < 0 ? 0 :
732 min(slab_max_order, MAX_ORDER - 1);
733 slab_max_order_set = true;
734
735 return 1;
736}
737__setup("slab_max_order=", slab_max_order_setup);
738
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800739#ifdef CONFIG_NUMA
740/*
741 * Special reaping functions for NUMA systems called from cache_reap().
742 * These take care of doing round robin flushing of alien caches (containing
743 * objects freed on different nodes from which they were allocated) and the
744 * flushing of remote pcps by calling drain_node_pages.
745 */
Tejun Heo1871e522009-10-29 22:34:13 +0900746static DEFINE_PER_CPU(unsigned long, slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800747
748static void init_reap_node(int cpu)
749{
750 int node;
751
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -0700752 node = next_node(cpu_to_mem(cpu), node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800753 if (node == MAX_NUMNODES)
Paul Jackson442295c2006-03-22 00:09:11 -0800754 node = first_node(node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800755
Tejun Heo1871e522009-10-29 22:34:13 +0900756 per_cpu(slab_reap_node, cpu) = node;
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800757}
758
759static void next_reap_node(void)
760{
Christoph Lameter909ea962010-12-08 16:22:55 +0100761 int node = __this_cpu_read(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800762
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800763 node = next_node(node, node_online_map);
764 if (unlikely(node >= MAX_NUMNODES))
765 node = first_node(node_online_map);
Christoph Lameter909ea962010-12-08 16:22:55 +0100766 __this_cpu_write(slab_reap_node, node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800767}
768
769#else
770#define init_reap_node(cpu) do { } while (0)
771#define next_reap_node(void) do { } while (0)
772#endif
773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774/*
775 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
776 * via the workqueue/eventd.
777 * Add the CPU number into the expiration time to minimize the possibility of
778 * the CPUs getting into lockstep and contending for the global cache chain
779 * lock.
780 */
Paul Gortmaker0db06282013-06-19 14:53:51 -0400781static void start_cpu_timer(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
Tejun Heo1871e522009-10-29 22:34:13 +0900783 struct delayed_work *reap_work = &per_cpu(slab_reap_work, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /*
786 * When this gets called from do_initcalls via cpucache_init(),
787 * init_workqueues() has already run, so keventd will be setup
788 * at that time.
789 */
David Howells52bad642006-11-22 14:54:01 +0000790 if (keventd_up() && reap_work->work.func == NULL) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800791 init_reap_node(cpu);
Tejun Heo203b42f2012-08-21 13:18:23 -0700792 INIT_DEFERRABLE_WORK(reap_work, cache_reap);
Arjan van de Ven2b284212006-12-10 02:21:28 -0800793 schedule_delayed_work_on(cpu, reap_work,
794 __round_jiffies_relative(HZ, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 }
796}
797
Joonsoo Kim1fe00d52014-08-06 16:04:27 -0700798static void init_arraycache(struct array_cache *ac, int limit, int batch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Catalin Marinasd5cff632009-06-11 13:22:40 +0100800 /*
801 * The array_cache structures contain pointers to free object.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300802 * However, when such objects are allocated or transferred to another
Catalin Marinasd5cff632009-06-11 13:22:40 +0100803 * cache the pointers are not cleared and they could be counted as
804 * valid references during a kmemleak scan. Therefore, kmemleak must
805 * not scan such objects.
806 */
Joonsoo Kim1fe00d52014-08-06 16:04:27 -0700807 kmemleak_no_scan(ac);
808 if (ac) {
809 ac->avail = 0;
810 ac->limit = limit;
811 ac->batchcount = batch;
812 ac->touched = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 }
Joonsoo Kim1fe00d52014-08-06 16:04:27 -0700814}
815
816static struct array_cache *alloc_arraycache(int node, int entries,
817 int batchcount, gfp_t gfp)
818{
819 int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
820 struct array_cache *ac = NULL;
821
822 ac = kmalloc_node(memsize, gfp, node);
823 init_arraycache(ac, entries, batchcount);
824 return ac;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Joonsoo Kim8456a642013-10-24 10:07:49 +0900827static inline bool is_slab_pfmemalloc(struct page *page)
Mel Gorman072bb0a2012-07-31 16:43:58 -0700828{
Mel Gorman072bb0a2012-07-31 16:43:58 -0700829 return PageSlabPfmemalloc(page);
830}
831
832/* Clears pfmemalloc_active if no slabs have pfmalloc set */
833static void recheck_pfmemalloc_active(struct kmem_cache *cachep,
834 struct array_cache *ac)
835{
Christoph Lameter18bf8542014-08-06 16:04:11 -0700836 struct kmem_cache_node *n = get_node(cachep, numa_mem_id());
Joonsoo Kim8456a642013-10-24 10:07:49 +0900837 struct page *page;
Mel Gorman072bb0a2012-07-31 16:43:58 -0700838 unsigned long flags;
839
840 if (!pfmemalloc_active)
841 return;
842
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000843 spin_lock_irqsave(&n->list_lock, flags);
Joonsoo Kim8456a642013-10-24 10:07:49 +0900844 list_for_each_entry(page, &n->slabs_full, lru)
845 if (is_slab_pfmemalloc(page))
Mel Gorman072bb0a2012-07-31 16:43:58 -0700846 goto out;
847
Joonsoo Kim8456a642013-10-24 10:07:49 +0900848 list_for_each_entry(page, &n->slabs_partial, lru)
849 if (is_slab_pfmemalloc(page))
Mel Gorman072bb0a2012-07-31 16:43:58 -0700850 goto out;
851
Joonsoo Kim8456a642013-10-24 10:07:49 +0900852 list_for_each_entry(page, &n->slabs_free, lru)
853 if (is_slab_pfmemalloc(page))
Mel Gorman072bb0a2012-07-31 16:43:58 -0700854 goto out;
855
856 pfmemalloc_active = false;
857out:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000858 spin_unlock_irqrestore(&n->list_lock, flags);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700859}
860
Mel Gorman381760e2012-07-31 16:44:30 -0700861static void *__ac_get_obj(struct kmem_cache *cachep, struct array_cache *ac,
Mel Gorman072bb0a2012-07-31 16:43:58 -0700862 gfp_t flags, bool force_refill)
863{
864 int i;
865 void *objp = ac->entry[--ac->avail];
866
867 /* Ensure the caller is allowed to use objects from PFMEMALLOC slab */
868 if (unlikely(is_obj_pfmemalloc(objp))) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000869 struct kmem_cache_node *n;
Mel Gorman072bb0a2012-07-31 16:43:58 -0700870
871 if (gfp_pfmemalloc_allowed(flags)) {
872 clear_obj_pfmemalloc(&objp);
873 return objp;
874 }
875
876 /* The caller cannot use PFMEMALLOC objects, find another one */
Joonsoo Kimd014dc22012-09-17 14:09:06 -0700877 for (i = 0; i < ac->avail; i++) {
Mel Gorman072bb0a2012-07-31 16:43:58 -0700878 /* If a !PFMEMALLOC object is found, swap them */
879 if (!is_obj_pfmemalloc(ac->entry[i])) {
880 objp = ac->entry[i];
881 ac->entry[i] = ac->entry[ac->avail];
882 ac->entry[ac->avail] = objp;
883 return objp;
884 }
885 }
886
887 /*
888 * If there are empty slabs on the slabs_free list and we are
889 * being forced to refill the cache, mark this one !pfmemalloc.
890 */
Christoph Lameter18bf8542014-08-06 16:04:11 -0700891 n = get_node(cachep, numa_mem_id());
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000892 if (!list_empty(&n->slabs_free) && force_refill) {
Joonsoo Kim8456a642013-10-24 10:07:49 +0900893 struct page *page = virt_to_head_page(objp);
Joonsoo Kim7ecccf92013-10-24 10:07:50 +0900894 ClearPageSlabPfmemalloc(page);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700895 clear_obj_pfmemalloc(&objp);
896 recheck_pfmemalloc_active(cachep, ac);
897 return objp;
898 }
899
900 /* No !PFMEMALLOC objects available */
901 ac->avail++;
902 objp = NULL;
903 }
904
905 return objp;
906}
907
Mel Gorman381760e2012-07-31 16:44:30 -0700908static inline void *ac_get_obj(struct kmem_cache *cachep,
909 struct array_cache *ac, gfp_t flags, bool force_refill)
910{
911 void *objp;
912
913 if (unlikely(sk_memalloc_socks()))
914 objp = __ac_get_obj(cachep, ac, flags, force_refill);
915 else
916 objp = ac->entry[--ac->avail];
917
918 return objp;
919}
920
921static void *__ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
Mel Gorman072bb0a2012-07-31 16:43:58 -0700922 void *objp)
923{
924 if (unlikely(pfmemalloc_active)) {
925 /* Some pfmemalloc slabs exist, check if this is one */
Mel Gorman30c29be2012-09-17 14:09:03 -0700926 struct page *page = virt_to_head_page(objp);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700927 if (PageSlabPfmemalloc(page))
928 set_obj_pfmemalloc(&objp);
929 }
930
Mel Gorman381760e2012-07-31 16:44:30 -0700931 return objp;
932}
933
934static inline void ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
935 void *objp)
936{
937 if (unlikely(sk_memalloc_socks()))
938 objp = __ac_put_obj(cachep, ac, objp);
939
Mel Gorman072bb0a2012-07-31 16:43:58 -0700940 ac->entry[ac->avail++] = objp;
941}
942
Christoph Lameter3ded1752006-03-25 03:06:44 -0800943/*
944 * Transfer objects in one arraycache to another.
945 * Locking must be handled by the caller.
946 *
947 * Return the number of entries transferred.
948 */
949static int transfer_objects(struct array_cache *to,
950 struct array_cache *from, unsigned int max)
951{
952 /* Figure out how many entries to transfer */
Hagen Paul Pfeifer732eacc2010-10-26 14:22:23 -0700953 int nr = min3(from->avail, max, to->limit - to->avail);
Christoph Lameter3ded1752006-03-25 03:06:44 -0800954
955 if (!nr)
956 return 0;
957
958 memcpy(to->entry + to->avail, from->entry + from->avail -nr,
959 sizeof(void *) *nr);
960
961 from->avail -= nr;
962 to->avail += nr;
Christoph Lameter3ded1752006-03-25 03:06:44 -0800963 return nr;
964}
965
Christoph Lameter765c4502006-09-27 01:50:08 -0700966#ifndef CONFIG_NUMA
967
968#define drain_alien_cache(cachep, alien) do { } while (0)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000969#define reap_alien(cachep, n) do { } while (0)
Christoph Lameter765c4502006-09-27 01:50:08 -0700970
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700971static inline struct alien_cache **alloc_alien_cache(int node,
972 int limit, gfp_t gfp)
Christoph Lameter765c4502006-09-27 01:50:08 -0700973{
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700974 return (struct alien_cache **)BAD_ALIEN_MAGIC;
Christoph Lameter765c4502006-09-27 01:50:08 -0700975}
976
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700977static inline void free_alien_cache(struct alien_cache **ac_ptr)
Christoph Lameter765c4502006-09-27 01:50:08 -0700978{
979}
980
981static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
982{
983 return 0;
984}
985
986static inline void *alternate_node_alloc(struct kmem_cache *cachep,
987 gfp_t flags)
988{
989 return NULL;
990}
991
Christoph Hellwig8b98c162006-12-06 20:32:30 -0800992static inline void *____cache_alloc_node(struct kmem_cache *cachep,
Christoph Lameter765c4502006-09-27 01:50:08 -0700993 gfp_t flags, int nodeid)
994{
995 return NULL;
996}
997
998#else /* CONFIG_NUMA */
999
Christoph Hellwig8b98c162006-12-06 20:32:30 -08001000static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
Paul Jacksonc61afb12006-03-24 03:16:08 -08001001static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
Christoph Lameterdc85da12006-01-18 17:42:36 -08001002
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001003static struct alien_cache *__alloc_alien_cache(int node, int entries,
1004 int batch, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -07001005{
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001006 int memsize = sizeof(void *) * entries + sizeof(struct alien_cache);
1007 struct alien_cache *alc = NULL;
1008
1009 alc = kmalloc_node(memsize, gfp, node);
1010 init_arraycache(&alc->ac, entries, batch);
Joonsoo Kim49dfc302014-08-06 16:04:31 -07001011 spin_lock_init(&alc->lock);
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001012 return alc;
1013}
1014
1015static struct alien_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
1016{
1017 struct alien_cache **alc_ptr;
Christoph Lameter8ef82862007-02-20 13:57:52 -08001018 int memsize = sizeof(void *) * nr_node_ids;
Christoph Lametere498be72005-09-09 13:03:32 -07001019 int i;
1020
1021 if (limit > 1)
1022 limit = 12;
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001023 alc_ptr = kzalloc_node(memsize, gfp, node);
1024 if (!alc_ptr)
1025 return NULL;
1026
1027 for_each_node(i) {
1028 if (i == node || !node_online(i))
1029 continue;
1030 alc_ptr[i] = __alloc_alien_cache(node, limit, 0xbaadf00d, gfp);
1031 if (!alc_ptr[i]) {
1032 for (i--; i >= 0; i--)
1033 kfree(alc_ptr[i]);
1034 kfree(alc_ptr);
1035 return NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07001036 }
1037 }
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001038 return alc_ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001039}
1040
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001041static void free_alien_cache(struct alien_cache **alc_ptr)
Christoph Lametere498be72005-09-09 13:03:32 -07001042{
1043 int i;
1044
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001045 if (!alc_ptr)
Christoph Lametere498be72005-09-09 13:03:32 -07001046 return;
Christoph Lametere498be72005-09-09 13:03:32 -07001047 for_each_node(i)
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001048 kfree(alc_ptr[i]);
1049 kfree(alc_ptr);
Christoph Lametere498be72005-09-09 13:03:32 -07001050}
1051
Pekka Enberg343e0d72006-02-01 03:05:50 -08001052static void __drain_alien_cache(struct kmem_cache *cachep,
Pekka Enberg5295a742006-02-01 03:05:48 -08001053 struct array_cache *ac, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07001054{
Christoph Lameter18bf8542014-08-06 16:04:11 -07001055 struct kmem_cache_node *n = get_node(cachep, node);
Joonsoo Kim97654df2014-08-06 16:04:25 -07001056 LIST_HEAD(list);
Christoph Lametere498be72005-09-09 13:03:32 -07001057
1058 if (ac->avail) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001059 spin_lock(&n->list_lock);
Christoph Lametere00946f2006-03-25 03:06:45 -08001060 /*
1061 * Stuff objects into the remote nodes shared array first.
1062 * That way we could avoid the overhead of putting the objects
1063 * into the free lists and getting them back later.
1064 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001065 if (n->shared)
1066 transfer_objects(n->shared, ac, ac->limit);
Christoph Lametere00946f2006-03-25 03:06:45 -08001067
Joonsoo Kim97654df2014-08-06 16:04:25 -07001068 free_block(cachep, ac->entry, ac->avail, node, &list);
Christoph Lametere498be72005-09-09 13:03:32 -07001069 ac->avail = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001070 spin_unlock(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07001071 slabs_destroy(cachep, &list);
Christoph Lametere498be72005-09-09 13:03:32 -07001072 }
1073}
1074
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001075/*
1076 * Called from cache_reap() to regularly drain alien caches round robin.
1077 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001078static void reap_alien(struct kmem_cache *cachep, struct kmem_cache_node *n)
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001079{
Christoph Lameter909ea962010-12-08 16:22:55 +01001080 int node = __this_cpu_read(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001081
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001082 if (n->alien) {
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001083 struct alien_cache *alc = n->alien[node];
1084 struct array_cache *ac;
Christoph Lametere00946f2006-03-25 03:06:45 -08001085
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001086 if (alc) {
1087 ac = &alc->ac;
Joonsoo Kim49dfc302014-08-06 16:04:31 -07001088 if (ac->avail && spin_trylock_irq(&alc->lock)) {
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001089 __drain_alien_cache(cachep, ac, node);
Joonsoo Kim49dfc302014-08-06 16:04:31 -07001090 spin_unlock_irq(&alc->lock);
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001091 }
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001092 }
1093 }
1094}
1095
Andrew Mortona737b3e2006-03-22 00:08:11 -08001096static void drain_alien_cache(struct kmem_cache *cachep,
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001097 struct alien_cache **alien)
Christoph Lametere498be72005-09-09 13:03:32 -07001098{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001099 int i = 0;
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001100 struct alien_cache *alc;
Christoph Lametere498be72005-09-09 13:03:32 -07001101 struct array_cache *ac;
1102 unsigned long flags;
1103
1104 for_each_online_node(i) {
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001105 alc = alien[i];
1106 if (alc) {
1107 ac = &alc->ac;
Joonsoo Kim49dfc302014-08-06 16:04:31 -07001108 spin_lock_irqsave(&alc->lock, flags);
Christoph Lametere498be72005-09-09 13:03:32 -07001109 __drain_alien_cache(cachep, ac, i);
Joonsoo Kim49dfc302014-08-06 16:04:31 -07001110 spin_unlock_irqrestore(&alc->lock, flags);
Christoph Lametere498be72005-09-09 13:03:32 -07001111 }
1112 }
1113}
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001114
Ingo Molnar873623d2006-07-13 14:44:38 +02001115static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001116{
Joonsoo Kim1ea991b2013-10-24 10:07:40 +09001117 int nodeid = page_to_nid(virt_to_page(objp));
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001118 struct kmem_cache_node *n;
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001119 struct alien_cache *alien = NULL;
1120 struct array_cache *ac;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001121 int node;
Joonsoo Kim97654df2014-08-06 16:04:25 -07001122 LIST_HEAD(list);
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001123
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001124 node = numa_mem_id();
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001125
1126 /*
1127 * Make sure we are not freeing a object from another node to the array
1128 * cache on this cpu.
1129 */
Joonsoo Kim1ea991b2013-10-24 10:07:40 +09001130 if (likely(nodeid == node))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001131 return 0;
1132
Christoph Lameter18bf8542014-08-06 16:04:11 -07001133 n = get_node(cachep, node);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001134 STATS_INC_NODEFREES(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001135 if (n->alien && n->alien[nodeid]) {
1136 alien = n->alien[nodeid];
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001137 ac = &alien->ac;
Joonsoo Kim49dfc302014-08-06 16:04:31 -07001138 spin_lock(&alien->lock);
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001139 if (unlikely(ac->avail == ac->limit)) {
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001140 STATS_INC_ACOVERFLOW(cachep);
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001141 __drain_alien_cache(cachep, ac, nodeid);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001142 }
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001143 ac_put_obj(cachep, ac, objp);
Joonsoo Kim49dfc302014-08-06 16:04:31 -07001144 spin_unlock(&alien->lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001145 } else {
Christoph Lameter18bf8542014-08-06 16:04:11 -07001146 n = get_node(cachep, nodeid);
1147 spin_lock(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07001148 free_block(cachep, &objp, 1, nodeid, &list);
Christoph Lameter18bf8542014-08-06 16:04:11 -07001149 spin_unlock(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07001150 slabs_destroy(cachep, &list);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001151 }
1152 return 1;
1153}
Christoph Lametere498be72005-09-09 13:03:32 -07001154#endif
1155
David Rientjes8f9f8d92010-03-27 19:40:47 -07001156/*
Christoph Lameter6a673682013-01-10 19:14:19 +00001157 * Allocates and initializes node for a node on each slab cache, used for
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001158 * either memory or cpu hotplug. If memory is being hot-added, the kmem_cache_node
David Rientjes8f9f8d92010-03-27 19:40:47 -07001159 * will be allocated off-node since memory is not yet online for the new node.
Christoph Lameter6a673682013-01-10 19:14:19 +00001160 * When hotplugging memory or a cpu, existing node are not replaced if
David Rientjes8f9f8d92010-03-27 19:40:47 -07001161 * already in use.
1162 *
Christoph Lameter18004c52012-07-06 15:25:12 -05001163 * Must hold slab_mutex.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001164 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001165static int init_cache_node_node(int node)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001166{
1167 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001168 struct kmem_cache_node *n;
Christoph Lameter6744f0872013-01-10 19:12:17 +00001169 const int memsize = sizeof(struct kmem_cache_node);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001170
Christoph Lameter18004c52012-07-06 15:25:12 -05001171 list_for_each_entry(cachep, &slab_caches, list) {
David Rientjes8f9f8d92010-03-27 19:40:47 -07001172 /*
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08001173 * Set up the kmem_cache_node for cpu before we can
David Rientjes8f9f8d92010-03-27 19:40:47 -07001174 * begin anything. Make sure some other cpu on this
1175 * node has not already allocated this
1176 */
Christoph Lameter18bf8542014-08-06 16:04:11 -07001177 n = get_node(cachep, node);
1178 if (!n) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001179 n = kmalloc_node(memsize, GFP_KERNEL, node);
1180 if (!n)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001181 return -ENOMEM;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001182 kmem_cache_node_init(n);
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08001183 n->next_reap = jiffies + REAPTIMEOUT_NODE +
1184 ((unsigned long)cachep) % REAPTIMEOUT_NODE;
David Rientjes8f9f8d92010-03-27 19:40:47 -07001185
1186 /*
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08001187 * The kmem_cache_nodes don't come and go as CPUs
1188 * come and go. slab_mutex is sufficient
David Rientjes8f9f8d92010-03-27 19:40:47 -07001189 * protection here.
1190 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001191 cachep->node[node] = n;
David Rientjes8f9f8d92010-03-27 19:40:47 -07001192 }
1193
Christoph Lameter18bf8542014-08-06 16:04:11 -07001194 spin_lock_irq(&n->list_lock);
1195 n->free_limit =
David Rientjes8f9f8d92010-03-27 19:40:47 -07001196 (1 + nr_cpus_node(node)) *
1197 cachep->batchcount + cachep->num;
Christoph Lameter18bf8542014-08-06 16:04:11 -07001198 spin_unlock_irq(&n->list_lock);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001199 }
1200 return 0;
1201}
1202
Wanpeng Li0fa81032013-07-04 08:33:22 +08001203static inline int slabs_tofree(struct kmem_cache *cachep,
1204 struct kmem_cache_node *n)
1205{
1206 return (n->free_objects + cachep->num - 1) / cachep->num;
1207}
1208
Paul Gortmaker0db06282013-06-19 14:53:51 -04001209static void cpuup_canceled(long cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001211 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001212 struct kmem_cache_node *n = NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001213 int node = cpu_to_mem(cpu);
Rusty Russella70f7302009-03-13 14:49:46 +10301214 const struct cpumask *mask = cpumask_of_node(node);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001215
Christoph Lameter18004c52012-07-06 15:25:12 -05001216 list_for_each_entry(cachep, &slab_caches, list) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001217 struct array_cache *nc;
1218 struct array_cache *shared;
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001219 struct alien_cache **alien;
Joonsoo Kim97654df2014-08-06 16:04:25 -07001220 LIST_HEAD(list);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001221
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001222 /* cpu is dead; no one can alloc from it. */
1223 nc = cachep->array[cpu];
1224 cachep->array[cpu] = NULL;
Christoph Lameter18bf8542014-08-06 16:04:11 -07001225 n = get_node(cachep, node);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001226
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001227 if (!n)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001228 goto free_array_cache;
1229
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001230 spin_lock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001231
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001232 /* Free limit for this kmem_cache_node */
1233 n->free_limit -= cachep->batchcount;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001234 if (nc)
Joonsoo Kim97654df2014-08-06 16:04:25 -07001235 free_block(cachep, nc->entry, nc->avail, node, &list);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001236
Rusty Russell58463c12009-12-17 11:43:12 -06001237 if (!cpumask_empty(mask)) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001238 spin_unlock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001239 goto free_array_cache;
1240 }
1241
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001242 shared = n->shared;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001243 if (shared) {
1244 free_block(cachep, shared->entry,
Joonsoo Kim97654df2014-08-06 16:04:25 -07001245 shared->avail, node, &list);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001246 n->shared = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001247 }
1248
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001249 alien = n->alien;
1250 n->alien = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001251
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001252 spin_unlock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001253
1254 kfree(shared);
1255 if (alien) {
1256 drain_alien_cache(cachep, alien);
1257 free_alien_cache(alien);
1258 }
1259free_array_cache:
Joonsoo Kim97654df2014-08-06 16:04:25 -07001260 slabs_destroy(cachep, &list);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001261 kfree(nc);
1262 }
1263 /*
1264 * In the previous loop, all the objects were freed to
1265 * the respective cache's slabs, now we can go ahead and
1266 * shrink each nodelist to its limit.
1267 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001268 list_for_each_entry(cachep, &slab_caches, list) {
Christoph Lameter18bf8542014-08-06 16:04:11 -07001269 n = get_node(cachep, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001270 if (!n)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001271 continue;
Wanpeng Li0fa81032013-07-04 08:33:22 +08001272 drain_freelist(cachep, n, slabs_tofree(cachep, n));
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001273 }
1274}
1275
Paul Gortmaker0db06282013-06-19 14:53:51 -04001276static int cpuup_prepare(long cpu)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001277{
Pekka Enberg343e0d72006-02-01 03:05:50 -08001278 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001279 struct kmem_cache_node *n = NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001280 int node = cpu_to_mem(cpu);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001281 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001283 /*
1284 * We need to do this right in the beginning since
1285 * alloc_arraycache's are going to use this list.
1286 * kmalloc_node allows us to add the slab to the right
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001287 * kmem_cache_node and not this cpu's kmem_cache_node
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001288 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001289 err = init_cache_node_node(node);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001290 if (err < 0)
1291 goto bad;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001292
1293 /*
1294 * Now we can go ahead with allocating the shared arrays and
1295 * array caches
1296 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001297 list_for_each_entry(cachep, &slab_caches, list) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001298 struct array_cache *nc;
1299 struct array_cache *shared = NULL;
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001300 struct alien_cache **alien = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001301
1302 nc = alloc_arraycache(node, cachep->limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001303 cachep->batchcount, GFP_KERNEL);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001304 if (!nc)
1305 goto bad;
1306 if (cachep->shared) {
1307 shared = alloc_arraycache(node,
1308 cachep->shared * cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001309 0xbaadf00d, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001310 if (!shared) {
1311 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001312 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001313 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001314 }
1315 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03001316 alien = alloc_alien_cache(node, cachep->limit, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001317 if (!alien) {
1318 kfree(shared);
1319 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001320 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001321 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001322 }
1323 cachep->array[cpu] = nc;
Christoph Lameter18bf8542014-08-06 16:04:11 -07001324 n = get_node(cachep, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001325 BUG_ON(!n);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001326
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001327 spin_lock_irq(&n->list_lock);
1328 if (!n->shared) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001329 /*
1330 * We are serialised from CPU_DEAD or
1331 * CPU_UP_CANCELLED by the cpucontrol lock
1332 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001333 n->shared = shared;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001334 shared = NULL;
1335 }
1336#ifdef CONFIG_NUMA
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001337 if (!n->alien) {
1338 n->alien = alien;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001339 alien = NULL;
1340 }
1341#endif
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001342 spin_unlock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001343 kfree(shared);
1344 free_alien_cache(alien);
Peter Zijlstra83835b32011-07-22 15:26:05 +02001345 if (cachep->flags & SLAB_DEBUG_OBJECTS)
Christoph Lameter18bf8542014-08-06 16:04:11 -07001346 slab_set_debugobj_lock_classes_node(cachep, n);
Glauber Costa6ccfb5b2012-12-18 14:22:31 -08001347 else if (!OFF_SLAB(cachep) &&
1348 !(cachep->flags & SLAB_DESTROY_BY_RCU))
Christoph Lameter18bf8542014-08-06 16:04:11 -07001349 on_slab_lock_classes_node(cachep, n);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001350 }
Pekka Enbergce79ddc2009-11-23 22:01:15 +02001351 init_node_lock_keys(node);
1352
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001353 return 0;
1354bad:
Akinobu Mita12d00f62007-10-18 03:05:11 -07001355 cpuup_canceled(cpu);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001356 return -ENOMEM;
1357}
1358
Paul Gortmaker0db06282013-06-19 14:53:51 -04001359static int cpuup_callback(struct notifier_block *nfb,
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001360 unsigned long action, void *hcpu)
1361{
1362 long cpu = (long)hcpu;
1363 int err = 0;
1364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 switch (action) {
Heiko Carstens38c3bd92007-05-09 02:34:05 -07001366 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001367 case CPU_UP_PREPARE_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001368 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001369 err = cpuup_prepare(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001370 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 break;
1372 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001373 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 start_cpu_timer(cpu);
1375 break;
1376#ifdef CONFIG_HOTPLUG_CPU
Christoph Lameter5830c592007-05-09 02:34:22 -07001377 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001378 case CPU_DOWN_PREPARE_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001379 /*
Christoph Lameter18004c52012-07-06 15:25:12 -05001380 * Shutdown cache reaper. Note that the slab_mutex is
Christoph Lameter5830c592007-05-09 02:34:22 -07001381 * held so that if cache_reap() is invoked it cannot do
1382 * anything expensive but will only modify reap_work
1383 * and reschedule the timer.
1384 */
Tejun Heoafe2c512010-12-14 16:21:17 +01001385 cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu));
Christoph Lameter5830c592007-05-09 02:34:22 -07001386 /* Now the cache_reaper is guaranteed to be not running. */
Tejun Heo1871e522009-10-29 22:34:13 +09001387 per_cpu(slab_reap_work, cpu).work.func = NULL;
Christoph Lameter5830c592007-05-09 02:34:22 -07001388 break;
1389 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001390 case CPU_DOWN_FAILED_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001391 start_cpu_timer(cpu);
1392 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001394 case CPU_DEAD_FROZEN:
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001395 /*
1396 * Even if all the cpus of a node are down, we don't free the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001397 * kmem_cache_node of any cache. This to avoid a race between
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001398 * cpu_down, and a kmalloc allocation from another cpu for
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001399 * memory from the node of the cpu going down. The node
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001400 * structure is usually allocated from kmem_cache_create() and
1401 * gets destroyed at kmem_cache_destroy().
1402 */
Simon Arlott183ff222007-10-20 01:27:18 +02001403 /* fall through */
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08001404#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001406 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001407 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001408 cpuup_canceled(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001409 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 }
Akinobu Mitaeac40682010-05-26 14:43:32 -07001412 return notifier_from_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
Paul Gortmaker0db06282013-06-19 14:53:51 -04001415static struct notifier_block cpucache_notifier = {
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001416 &cpuup_callback, NULL, 0
1417};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418
David Rientjes8f9f8d92010-03-27 19:40:47 -07001419#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
1420/*
1421 * Drains freelist for a node on each slab cache, used for memory hot-remove.
1422 * Returns -EBUSY if all objects cannot be drained so that the node is not
1423 * removed.
1424 *
Christoph Lameter18004c52012-07-06 15:25:12 -05001425 * Must hold slab_mutex.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001426 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001427static int __meminit drain_cache_node_node(int node)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001428{
1429 struct kmem_cache *cachep;
1430 int ret = 0;
1431
Christoph Lameter18004c52012-07-06 15:25:12 -05001432 list_for_each_entry(cachep, &slab_caches, list) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001433 struct kmem_cache_node *n;
David Rientjes8f9f8d92010-03-27 19:40:47 -07001434
Christoph Lameter18bf8542014-08-06 16:04:11 -07001435 n = get_node(cachep, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001436 if (!n)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001437 continue;
1438
Wanpeng Li0fa81032013-07-04 08:33:22 +08001439 drain_freelist(cachep, n, slabs_tofree(cachep, n));
David Rientjes8f9f8d92010-03-27 19:40:47 -07001440
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001441 if (!list_empty(&n->slabs_full) ||
1442 !list_empty(&n->slabs_partial)) {
David Rientjes8f9f8d92010-03-27 19:40:47 -07001443 ret = -EBUSY;
1444 break;
1445 }
1446 }
1447 return ret;
1448}
1449
1450static int __meminit slab_memory_callback(struct notifier_block *self,
1451 unsigned long action, void *arg)
1452{
1453 struct memory_notify *mnb = arg;
1454 int ret = 0;
1455 int nid;
1456
1457 nid = mnb->status_change_nid;
1458 if (nid < 0)
1459 goto out;
1460
1461 switch (action) {
1462 case MEM_GOING_ONLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001463 mutex_lock(&slab_mutex);
Christoph Lameter6a673682013-01-10 19:14:19 +00001464 ret = init_cache_node_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001465 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001466 break;
1467 case MEM_GOING_OFFLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001468 mutex_lock(&slab_mutex);
Christoph Lameter6a673682013-01-10 19:14:19 +00001469 ret = drain_cache_node_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001470 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001471 break;
1472 case MEM_ONLINE:
1473 case MEM_OFFLINE:
1474 case MEM_CANCEL_ONLINE:
1475 case MEM_CANCEL_OFFLINE:
1476 break;
1477 }
1478out:
Prarit Bhargava5fda1bd2011-03-22 16:30:49 -07001479 return notifier_from_errno(ret);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001480}
1481#endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
1482
Christoph Lametere498be72005-09-09 13:03:32 -07001483/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001484 * swap the static kmem_cache_node with kmalloced memory
Christoph Lametere498be72005-09-09 13:03:32 -07001485 */
Christoph Lameter6744f0872013-01-10 19:12:17 +00001486static void __init init_list(struct kmem_cache *cachep, struct kmem_cache_node *list,
David Rientjes8f9f8d92010-03-27 19:40:47 -07001487 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07001488{
Christoph Lameter6744f0872013-01-10 19:12:17 +00001489 struct kmem_cache_node *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001490
Christoph Lameter6744f0872013-01-10 19:12:17 +00001491 ptr = kmalloc_node(sizeof(struct kmem_cache_node), GFP_NOWAIT, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07001492 BUG_ON(!ptr);
1493
Christoph Lameter6744f0872013-01-10 19:12:17 +00001494 memcpy(ptr, list, sizeof(struct kmem_cache_node));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001495 /*
1496 * Do not assume that spinlocks can be initialized via memcpy:
1497 */
1498 spin_lock_init(&ptr->list_lock);
1499
Christoph Lametere498be72005-09-09 13:03:32 -07001500 MAKE_ALL_LISTS(cachep, ptr, nodeid);
Christoph Lameter6a673682013-01-10 19:14:19 +00001501 cachep->node[nodeid] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001502}
1503
Andrew Mortona737b3e2006-03-22 00:08:11 -08001504/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001505 * For setting up all the kmem_cache_node for cache whose buffer_size is same as
1506 * size of kmem_cache_node.
Pekka Enberg556a1692008-01-25 08:20:51 +02001507 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001508static void __init set_up_node(struct kmem_cache *cachep, int index)
Pekka Enberg556a1692008-01-25 08:20:51 +02001509{
1510 int node;
1511
1512 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001513 cachep->node[node] = &init_kmem_cache_node[index + node];
Christoph Lameter6a673682013-01-10 19:14:19 +00001514 cachep->node[node]->next_reap = jiffies +
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08001515 REAPTIMEOUT_NODE +
1516 ((unsigned long)cachep) % REAPTIMEOUT_NODE;
Pekka Enberg556a1692008-01-25 08:20:51 +02001517 }
1518}
1519
1520/*
Christoph Lameter3c583462012-11-28 16:23:01 +00001521 * The memory after the last cpu cache pointer is used for the
Christoph Lameter6a673682013-01-10 19:14:19 +00001522 * the node pointer.
Christoph Lameter3c583462012-11-28 16:23:01 +00001523 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001524static void setup_node_pointer(struct kmem_cache *cachep)
Christoph Lameter3c583462012-11-28 16:23:01 +00001525{
Christoph Lameter6a673682013-01-10 19:14:19 +00001526 cachep->node = (struct kmem_cache_node **)&cachep->array[nr_cpu_ids];
Christoph Lameter3c583462012-11-28 16:23:01 +00001527}
1528
1529/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08001530 * Initialisation. Called after the page allocator have been initialised and
1531 * before smp_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 */
1533void __init kmem_cache_init(void)
1534{
Christoph Lametere498be72005-09-09 13:03:32 -07001535 int i;
1536
Joonsoo Kim68126702013-10-24 10:07:42 +09001537 BUILD_BUG_ON(sizeof(((struct page *)NULL)->lru) <
1538 sizeof(struct rcu_head));
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001539 kmem_cache = &kmem_cache_boot;
Christoph Lameter6a673682013-01-10 19:14:19 +00001540 setup_node_pointer(kmem_cache);
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001541
Mel Gormanb6e68bc2009-06-16 15:32:16 -07001542 if (num_possible_nodes() == 1)
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001543 use_alien_caches = 0;
1544
Christoph Lameter3c583462012-11-28 16:23:01 +00001545 for (i = 0; i < NUM_INIT_LISTS; i++)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001546 kmem_cache_node_init(&init_kmem_cache_node[i]);
Christoph Lameter3c583462012-11-28 16:23:01 +00001547
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001548 set_up_node(kmem_cache, CACHE_CACHE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550 /*
1551 * Fragmentation resistance on low memory - only use bigger
David Rientjes3df1ccc2011-10-18 22:09:28 -07001552 * page orders on machines with more than 32MB of memory if
1553 * not overridden on the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 */
David Rientjes3df1ccc2011-10-18 22:09:28 -07001555 if (!slab_max_order_set && totalram_pages > (32 << 20) >> PAGE_SHIFT)
David Rientjes543585c2011-10-18 22:09:24 -07001556 slab_max_order = SLAB_MAX_ORDER_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 /* Bootstrap is tricky, because several objects are allocated
1559 * from caches that do not exist yet:
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001560 * 1) initialize the kmem_cache cache: it contains the struct
1561 * kmem_cache structures of all caches, except kmem_cache itself:
1562 * kmem_cache is statically allocated.
Christoph Lametere498be72005-09-09 13:03:32 -07001563 * Initially an __init data area is used for the head array and the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001564 * kmem_cache_node structures, it's replaced with a kmalloc allocated
Christoph Lametere498be72005-09-09 13:03:32 -07001565 * array at the end of the bootstrap.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 * 2) Create the first kmalloc cache.
Pekka Enberg343e0d72006-02-01 03:05:50 -08001567 * The struct kmem_cache for the new cache is allocated normally.
Christoph Lametere498be72005-09-09 13:03:32 -07001568 * An __init data area is used for the head array.
1569 * 3) Create the remaining kmalloc caches, with minimally sized
1570 * head arrays.
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001571 * 4) Replace the __init data head arrays for kmem_cache and the first
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 * kmalloc cache with kmalloc allocated arrays.
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001573 * 5) Replace the __init data for kmem_cache_node for kmem_cache and
Christoph Lametere498be72005-09-09 13:03:32 -07001574 * the other cache's with kmalloc allocated memory.
1575 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 */
1577
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001578 /* 1) create the kmem_cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Eric Dumazet8da34302007-05-06 14:49:29 -07001580 /*
Eric Dumazetb56efcf2011-07-20 19:04:23 +02001581 * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
Eric Dumazet8da34302007-05-06 14:49:29 -07001582 */
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001583 create_boot_cache(kmem_cache, "kmem_cache",
1584 offsetof(struct kmem_cache, array[nr_cpu_ids]) +
Christoph Lameter6744f0872013-01-10 19:12:17 +00001585 nr_node_ids * sizeof(struct kmem_cache_node *),
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001586 SLAB_HWCACHE_ALIGN);
1587 list_add(&kmem_cache->list, &slab_caches);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
1589 /* 2+3) create the kmalloc caches */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Andrew Mortona737b3e2006-03-22 00:08:11 -08001591 /*
1592 * Initialize the caches that provide memory for the array cache and the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001593 * kmem_cache_node structures first. Without this, further allocations will
Andrew Mortona737b3e2006-03-22 00:08:11 -08001594 * bug.
Christoph Lametere498be72005-09-09 13:03:32 -07001595 */
1596
Christoph Lametere3366012013-01-10 19:14:18 +00001597 kmalloc_caches[INDEX_AC] = create_kmalloc_cache("kmalloc-ac",
1598 kmalloc_size(INDEX_AC), ARCH_KMALLOC_FLAGS);
Christoph Lametere498be72005-09-09 13:03:32 -07001599
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001600 if (INDEX_AC != INDEX_NODE)
1601 kmalloc_caches[INDEX_NODE] =
1602 create_kmalloc_cache("kmalloc-node",
1603 kmalloc_size(INDEX_NODE), ARCH_KMALLOC_FLAGS);
Christoph Lametere498be72005-09-09 13:03:32 -07001604
Ingo Molnare0a42722006-06-23 02:03:46 -07001605 slab_early_init = 0;
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 /* 4) Replace the bootstrap head arrays */
1608 {
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001609 struct array_cache *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001610
Pekka Enberg83b519e2009-06-10 19:40:04 +03001611 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001612
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001613 memcpy(ptr, cpu_cache_get(kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001614 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001615
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001616 kmem_cache->array[smp_processor_id()] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001617
Pekka Enberg83b519e2009-06-10 19:40:04 +03001618 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001619
Christoph Lametere3366012013-01-10 19:14:18 +00001620 BUG_ON(cpu_cache_get(kmalloc_caches[INDEX_AC])
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001621 != &initarray_generic.cache);
Christoph Lametere3366012013-01-10 19:14:18 +00001622 memcpy(ptr, cpu_cache_get(kmalloc_caches[INDEX_AC]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001623 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001624
Christoph Lametere3366012013-01-10 19:14:18 +00001625 kmalloc_caches[INDEX_AC]->array[smp_processor_id()] = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001627 /* 5) Replace the bootstrap kmem_cache_node */
Christoph Lametere498be72005-09-09 13:03:32 -07001628 {
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001629 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Mel Gorman9c09a952008-01-24 05:49:54 -08001631 for_each_online_node(nid) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001632 init_list(kmem_cache, &init_kmem_cache_node[CACHE_CACHE + nid], nid);
Pekka Enberg556a1692008-01-25 08:20:51 +02001633
Christoph Lametere3366012013-01-10 19:14:18 +00001634 init_list(kmalloc_caches[INDEX_AC],
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001635 &init_kmem_cache_node[SIZE_AC + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001636
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001637 if (INDEX_AC != INDEX_NODE) {
1638 init_list(kmalloc_caches[INDEX_NODE],
1639 &init_kmem_cache_node[SIZE_NODE + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001640 }
1641 }
1642 }
1643
Christoph Lameterf97d5f62013-01-10 19:12:17 +00001644 create_kmalloc_caches(ARCH_KMALLOC_FLAGS);
Pekka Enberg8429db52009-06-12 15:58:59 +03001645}
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001646
Pekka Enberg8429db52009-06-12 15:58:59 +03001647void __init kmem_cache_init_late(void)
1648{
1649 struct kmem_cache *cachep;
1650
Christoph Lameter97d06602012-07-06 15:25:11 -05001651 slab_state = UP;
Peter Zijlstra52cef182011-11-28 21:12:40 +01001652
Pekka Enberg8429db52009-06-12 15:58:59 +03001653 /* 6) resize the head arrays to their final sizes */
Christoph Lameter18004c52012-07-06 15:25:12 -05001654 mutex_lock(&slab_mutex);
1655 list_for_each_entry(cachep, &slab_caches, list)
Pekka Enberg8429db52009-06-12 15:58:59 +03001656 if (enable_cpucache(cachep, GFP_NOWAIT))
1657 BUG();
Christoph Lameter18004c52012-07-06 15:25:12 -05001658 mutex_unlock(&slab_mutex);
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001659
Michael Wang947ca182012-09-05 10:33:18 +08001660 /* Annotate slab for lockdep -- annotate the malloc caches */
1661 init_lock_keys();
1662
Christoph Lameter97d06602012-07-06 15:25:11 -05001663 /* Done! */
1664 slab_state = FULL;
1665
Andrew Mortona737b3e2006-03-22 00:08:11 -08001666 /*
1667 * Register a cpu startup notifier callback that initializes
1668 * cpu_cache_get for all new cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 */
1670 register_cpu_notifier(&cpucache_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671
David Rientjes8f9f8d92010-03-27 19:40:47 -07001672#ifdef CONFIG_NUMA
1673 /*
1674 * Register a memory hotplug callback that initializes and frees
Christoph Lameter6a673682013-01-10 19:14:19 +00001675 * node.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001676 */
1677 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
1678#endif
1679
Andrew Mortona737b3e2006-03-22 00:08:11 -08001680 /*
1681 * The reap timers are started later, with a module init call: That part
1682 * of the kernel is not yet operational.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 */
1684}
1685
1686static int __init cpucache_init(void)
1687{
1688 int cpu;
1689
Andrew Mortona737b3e2006-03-22 00:08:11 -08001690 /*
1691 * Register the timers that return unneeded pages to the page allocator
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 */
Christoph Lametere498be72005-09-09 13:03:32 -07001693 for_each_online_cpu(cpu)
Andrew Mortona737b3e2006-03-22 00:08:11 -08001694 start_cpu_timer(cpu);
Glauber Costaa164f8962012-06-21 00:59:18 +04001695
1696 /* Done! */
Christoph Lameter97d06602012-07-06 15:25:11 -05001697 slab_state = FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 return 0;
1699}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700__initcall(cpucache_init);
1701
Rafael Aquini8bdec192012-03-09 17:27:27 -03001702static noinline void
1703slab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid)
1704{
David Rientjes9a02d692014-06-04 16:06:36 -07001705#if DEBUG
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001706 struct kmem_cache_node *n;
Joonsoo Kim8456a642013-10-24 10:07:49 +09001707 struct page *page;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001708 unsigned long flags;
1709 int node;
David Rientjes9a02d692014-06-04 16:06:36 -07001710 static DEFINE_RATELIMIT_STATE(slab_oom_rs, DEFAULT_RATELIMIT_INTERVAL,
1711 DEFAULT_RATELIMIT_BURST);
1712
1713 if ((gfpflags & __GFP_NOWARN) || !__ratelimit(&slab_oom_rs))
1714 return;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001715
1716 printk(KERN_WARNING
1717 "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n",
1718 nodeid, gfpflags);
1719 printk(KERN_WARNING " cache: %s, object size: %d, order: %d\n",
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05001720 cachep->name, cachep->size, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001721
Christoph Lameter18bf8542014-08-06 16:04:11 -07001722 for_each_kmem_cache_node(cachep, node, n) {
Rafael Aquini8bdec192012-03-09 17:27:27 -03001723 unsigned long active_objs = 0, num_objs = 0, free_objects = 0;
1724 unsigned long active_slabs = 0, num_slabs = 0;
1725
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001726 spin_lock_irqsave(&n->list_lock, flags);
Joonsoo Kim8456a642013-10-24 10:07:49 +09001727 list_for_each_entry(page, &n->slabs_full, lru) {
Rafael Aquini8bdec192012-03-09 17:27:27 -03001728 active_objs += cachep->num;
1729 active_slabs++;
1730 }
Joonsoo Kim8456a642013-10-24 10:07:49 +09001731 list_for_each_entry(page, &n->slabs_partial, lru) {
1732 active_objs += page->active;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001733 active_slabs++;
1734 }
Joonsoo Kim8456a642013-10-24 10:07:49 +09001735 list_for_each_entry(page, &n->slabs_free, lru)
Rafael Aquini8bdec192012-03-09 17:27:27 -03001736 num_slabs++;
1737
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001738 free_objects += n->free_objects;
1739 spin_unlock_irqrestore(&n->list_lock, flags);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001740
1741 num_slabs += active_slabs;
1742 num_objs = num_slabs * cachep->num;
1743 printk(KERN_WARNING
1744 " node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
1745 node, active_slabs, num_slabs, active_objs, num_objs,
1746 free_objects);
1747 }
David Rientjes9a02d692014-06-04 16:06:36 -07001748#endif
Rafael Aquini8bdec192012-03-09 17:27:27 -03001749}
1750
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751/*
1752 * Interface to system's page allocator. No need to hold the cache-lock.
1753 *
1754 * If we requested dmaable memory, we will get it. Even if we
1755 * did not request dmaable memory, we might get it, but that
1756 * would be relatively rare and ignorable.
1757 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001758static struct page *kmem_getpages(struct kmem_cache *cachep, gfp_t flags,
1759 int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760{
1761 struct page *page;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001762 int nr_pages;
Christoph Lameter765c4502006-09-27 01:50:08 -07001763
Glauber Costaa618e892012-06-14 16:17:21 +04001764 flags |= cachep->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001765 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1766 flags |= __GFP_RECLAIMABLE;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001767
Vladimir Davydov5dfb4172014-06-04 16:06:38 -07001768 if (memcg_charge_slab(cachep, flags, cachep->gfporder))
1769 return NULL;
1770
Linus Torvalds517d0862009-06-16 19:50:13 -07001771 page = alloc_pages_exact_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001772 if (!page) {
Vladimir Davydov5dfb4172014-06-04 16:06:38 -07001773 memcg_uncharge_slab(cachep, cachep->gfporder);
David Rientjes9a02d692014-06-04 16:06:36 -07001774 slab_out_of_memory(cachep, flags, nodeid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 return NULL;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Mel Gormanb37f1dd2012-07-31 16:44:03 -07001778 /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
Mel Gorman072bb0a2012-07-31 16:43:58 -07001779 if (unlikely(page->pfmemalloc))
1780 pfmemalloc_active = true;
1781
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001782 nr_pages = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
Christoph Lameter972d1a72006-09-25 23:31:51 -07001784 add_zone_page_state(page_zone(page),
1785 NR_SLAB_RECLAIMABLE, nr_pages);
1786 else
1787 add_zone_page_state(page_zone(page),
1788 NR_SLAB_UNRECLAIMABLE, nr_pages);
Joonsoo Kima57a4982013-10-24 10:07:44 +09001789 __SetPageSlab(page);
1790 if (page->pfmemalloc)
1791 SetPageSlabPfmemalloc(page);
Mel Gorman072bb0a2012-07-31 16:43:58 -07001792
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001793 if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
1794 kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
1795
1796 if (cachep->ctor)
1797 kmemcheck_mark_uninitialized_pages(page, nr_pages);
1798 else
1799 kmemcheck_mark_unallocated_pages(page, nr_pages);
1800 }
Pekka Enbergc175eea2008-05-09 20:35:53 +02001801
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001802 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803}
1804
1805/*
1806 * Interface to system's page release.
1807 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001808static void kmem_freepages(struct kmem_cache *cachep, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Joonsoo Kima57a4982013-10-24 10:07:44 +09001810 const unsigned long nr_freed = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001812 kmemcheck_free_shadow(page, cachep->gfporder);
Pekka Enbergc175eea2008-05-09 20:35:53 +02001813
Christoph Lameter972d1a72006-09-25 23:31:51 -07001814 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1815 sub_zone_page_state(page_zone(page),
1816 NR_SLAB_RECLAIMABLE, nr_freed);
1817 else
1818 sub_zone_page_state(page_zone(page),
1819 NR_SLAB_UNRECLAIMABLE, nr_freed);
Joonsoo Kim73293c22013-10-24 10:07:37 +09001820
Joonsoo Kima57a4982013-10-24 10:07:44 +09001821 BUG_ON(!PageSlab(page));
Joonsoo Kim73293c22013-10-24 10:07:37 +09001822 __ClearPageSlabPfmemalloc(page);
Joonsoo Kima57a4982013-10-24 10:07:44 +09001823 __ClearPageSlab(page);
Joonsoo Kim8456a642013-10-24 10:07:49 +09001824 page_mapcount_reset(page);
1825 page->mapping = NULL;
Glauber Costa1f458cb2012-12-18 14:22:50 -08001826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (current->reclaim_state)
1828 current->reclaim_state->reclaimed_slab += nr_freed;
Vladimir Davydov5dfb4172014-06-04 16:06:38 -07001829 __free_pages(page, cachep->gfporder);
1830 memcg_uncharge_slab(cachep, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831}
1832
1833static void kmem_rcu_free(struct rcu_head *head)
1834{
Joonsoo Kim68126702013-10-24 10:07:42 +09001835 struct kmem_cache *cachep;
1836 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Joonsoo Kim68126702013-10-24 10:07:42 +09001838 page = container_of(head, struct page, rcu_head);
1839 cachep = page->slab_cache;
1840
1841 kmem_freepages(cachep, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842}
1843
1844#if DEBUG
1845
1846#ifdef CONFIG_DEBUG_PAGEALLOC
Pekka Enberg343e0d72006-02-01 03:05:50 -08001847static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001848 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001850 int size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001852 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001854 if (size < 5 * sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 return;
1856
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001857 *addr++ = 0x12345678;
1858 *addr++ = caller;
1859 *addr++ = smp_processor_id();
1860 size -= 3 * sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 {
1862 unsigned long *sptr = &caller;
1863 unsigned long svalue;
1864
1865 while (!kstack_end(sptr)) {
1866 svalue = *sptr++;
1867 if (kernel_text_address(svalue)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001868 *addr++ = svalue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 size -= sizeof(unsigned long);
1870 if (size <= sizeof(unsigned long))
1871 break;
1872 }
1873 }
1874
1875 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001876 *addr++ = 0x87654321;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878#endif
1879
Pekka Enberg343e0d72006-02-01 03:05:50 -08001880static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001882 int size = cachep->object_size;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001883 addr = &((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
1885 memset(addr, val, size);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001886 *(unsigned char *)(addr + size - 1) = POISON_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887}
1888
1889static void dump_line(char *data, int offset, int limit)
1890{
1891 int i;
Dave Jonesaa83aa42006-09-29 01:59:51 -07001892 unsigned char error = 0;
1893 int bad_count = 0;
1894
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02001895 printk(KERN_ERR "%03x: ", offset);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001896 for (i = 0; i < limit; i++) {
1897 if (data[offset + i] != POISON_FREE) {
1898 error = data[offset + i];
1899 bad_count++;
1900 }
Dave Jonesaa83aa42006-09-29 01:59:51 -07001901 }
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02001902 print_hex_dump(KERN_CONT, "", 0, 16, 1,
1903 &data[offset], limit, 1);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001904
1905 if (bad_count == 1) {
1906 error ^= POISON_FREE;
1907 if (!(error & (error - 1))) {
1908 printk(KERN_ERR "Single bit error detected. Probably "
1909 "bad RAM.\n");
1910#ifdef CONFIG_X86
1911 printk(KERN_ERR "Run memtest86+ or a similar memory "
1912 "test tool.\n");
1913#else
1914 printk(KERN_ERR "Run a memory test tool.\n");
1915#endif
1916 }
1917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919#endif
1920
1921#if DEBUG
1922
Pekka Enberg343e0d72006-02-01 03:05:50 -08001923static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 int i, size;
1926 char *realobj;
1927
1928 if (cachep->flags & SLAB_RED_ZONE) {
David Woodhouseb46b8f12007-05-08 00:22:59 -07001929 printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001930 *dbg_redzone1(cachep, objp),
1931 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 }
1933
1934 if (cachep->flags & SLAB_STORE_USER) {
Joe Perches071361d2012-12-12 10:19:12 -08001935 printk(KERN_ERR "Last user: [<%p>](%pSR)\n",
1936 *dbg_userword(cachep, objp),
1937 *dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 }
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001939 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001940 size = cachep->object_size;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001941 for (i = 0; i < size && lines; i += 16, lines--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 int limit;
1943 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001944 if (i + limit > size)
1945 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 dump_line(realobj, i, limit);
1947 }
1948}
1949
Pekka Enberg343e0d72006-02-01 03:05:50 -08001950static void check_poison_obj(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951{
1952 char *realobj;
1953 int size, i;
1954 int lines = 0;
1955
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001956 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001957 size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001959 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 char exp = POISON_FREE;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001961 if (i == size - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 exp = POISON_END;
1963 if (realobj[i] != exp) {
1964 int limit;
1965 /* Mismatch ! */
1966 /* Print header */
1967 if (lines == 0) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001968 printk(KERN_ERR
Dave Jonesface37f2011-11-15 15:03:52 -08001969 "Slab corruption (%s): %s start=%p, len=%d\n",
1970 print_tainted(), cachep->name, realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 print_objinfo(cachep, objp, 0);
1972 }
1973 /* Hexdump the affected line */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001974 i = (i / 16) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001976 if (i + limit > size)
1977 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 dump_line(realobj, i, limit);
1979 i += 16;
1980 lines++;
1981 /* Limit to 5 lines */
1982 if (lines > 5)
1983 break;
1984 }
1985 }
1986 if (lines != 0) {
1987 /* Print some data about the neighboring objects, if they
1988 * exist:
1989 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09001990 struct page *page = virt_to_head_page(objp);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001991 unsigned int objnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Joonsoo Kim8456a642013-10-24 10:07:49 +09001993 objnr = obj_to_index(cachep, page, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 if (objnr) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09001995 objp = index_to_obj(cachep, page, objnr - 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001996 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001998 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 print_objinfo(cachep, objp, 2);
2000 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002001 if (objnr + 1 < cachep->num) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09002002 objp = index_to_obj(cachep, page, objnr + 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002003 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002005 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 print_objinfo(cachep, objp, 2);
2007 }
2008 }
2009}
2010#endif
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012#if DEBUG
Joonsoo Kim8456a642013-10-24 10:07:49 +09002013static void slab_destroy_debugcheck(struct kmem_cache *cachep,
2014 struct page *page)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002015{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 int i;
2017 for (i = 0; i < cachep->num; i++) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09002018 void *objp = index_to_obj(cachep, page, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
2020 if (cachep->flags & SLAB_POISON) {
2021#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002022 if (cachep->size % PAGE_SIZE == 0 &&
Andrew Mortona737b3e2006-03-22 00:08:11 -08002023 OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002024 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002025 cachep->size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 else
2027 check_poison_obj(cachep, objp);
2028#else
2029 check_poison_obj(cachep, objp);
2030#endif
2031 }
2032 if (cachep->flags & SLAB_RED_ZONE) {
2033 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2034 slab_error(cachep, "start of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002035 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2037 slab_error(cachep, "end of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002038 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002041}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042#else
Joonsoo Kim8456a642013-10-24 10:07:49 +09002043static void slab_destroy_debugcheck(struct kmem_cache *cachep,
2044 struct page *page)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002045{
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002046}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047#endif
2048
Randy Dunlap911851e2006-03-22 00:08:14 -08002049/**
2050 * slab_destroy - destroy and release all objects in a slab
2051 * @cachep: cache pointer being destroyed
Masanari Iidacb8ee1a2014-01-28 02:57:08 +09002052 * @page: page pointer being destroyed
Randy Dunlap911851e2006-03-22 00:08:14 -08002053 *
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002054 * Destroy all the objs in a slab, and release the mem back to the system.
Andrew Mortona737b3e2006-03-22 00:08:11 -08002055 * Before calling the slab must have been unlinked from the cache. The
2056 * cache-lock is not held/needed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002057 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002058static void slab_destroy(struct kmem_cache *cachep, struct page *page)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002059{
Joonsoo Kim7e007352013-10-30 19:04:01 +09002060 void *freelist;
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002061
Joonsoo Kim8456a642013-10-24 10:07:49 +09002062 freelist = page->freelist;
2063 slab_destroy_debugcheck(cachep, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
Joonsoo Kim68126702013-10-24 10:07:42 +09002065 struct rcu_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066
Joonsoo Kim68126702013-10-24 10:07:42 +09002067 /*
2068 * RCU free overloads the RCU head over the LRU.
2069 * slab_page has been overloeaded over the LRU,
2070 * however it is not used from now on so that
2071 * we can use it safely.
2072 */
2073 head = (void *)&page->rcu_head;
2074 call_rcu(head, kmem_rcu_free);
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 } else {
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002077 kmem_freepages(cachep, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 }
Joonsoo Kim68126702013-10-24 10:07:42 +09002079
2080 /*
Joonsoo Kim8456a642013-10-24 10:07:49 +09002081 * From now on, we don't use freelist
Joonsoo Kim68126702013-10-24 10:07:42 +09002082 * although actual page can be freed in rcu context
2083 */
2084 if (OFF_SLAB(cachep))
Joonsoo Kim8456a642013-10-24 10:07:49 +09002085 kmem_cache_free(cachep->freelist_cache, freelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086}
2087
Joonsoo Kim97654df2014-08-06 16:04:25 -07002088static void slabs_destroy(struct kmem_cache *cachep, struct list_head *list)
2089{
2090 struct page *page, *n;
2091
2092 list_for_each_entry_safe(page, n, list, lru) {
2093 list_del(&page->lru);
2094 slab_destroy(cachep, page);
2095 }
2096}
2097
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098/**
Randy.Dunlapa70773d2006-02-01 03:05:52 -08002099 * calculate_slab_order - calculate size (page order) of slabs
2100 * @cachep: pointer to the cache that is being created
2101 * @size: size of objects to be created in this cache.
2102 * @align: required alignment for the objects.
2103 * @flags: slab allocation flags
2104 *
2105 * Also calculates the number of objects per slab.
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002106 *
2107 * This could be made much more intelligent. For now, try to avoid using
2108 * high order pages for slabs. When the gfp() functions are more friendly
2109 * towards high-order requests, this should be changed.
2110 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002111static size_t calculate_slab_order(struct kmem_cache *cachep,
Randy Dunlapee13d782006-02-01 03:05:53 -08002112 size_t size, size_t align, unsigned long flags)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002113{
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002114 unsigned long offslab_limit;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002115 size_t left_over = 0;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002116 int gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002117
Christoph Lameter0aa817f2007-05-16 22:11:01 -07002118 for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002119 unsigned int num;
2120 size_t remainder;
2121
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002122 cache_estimate(gfporder, size, align, flags, &remainder, &num);
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002123 if (!num)
2124 continue;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002125
Joonsoo Kimf315e3f2013-12-02 17:49:41 +09002126 /* Can't handle number of objects more than SLAB_OBJ_MAX_NUM */
2127 if (num > SLAB_OBJ_MAX_NUM)
2128 break;
2129
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002130 if (flags & CFLGS_OFF_SLAB) {
Joonsoo Kim03787302014-06-23 13:22:06 -07002131 size_t freelist_size_per_obj = sizeof(freelist_idx_t);
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002132 /*
2133 * Max number of objs-per-slab for caches which
2134 * use off-slab slabs. Needed to avoid a possible
2135 * looping condition in cache_grow().
2136 */
Joonsoo Kim03787302014-06-23 13:22:06 -07002137 if (IS_ENABLED(CONFIG_DEBUG_SLAB_LEAK))
2138 freelist_size_per_obj += sizeof(char);
Joonsoo Kim8456a642013-10-24 10:07:49 +09002139 offslab_limit = size;
Joonsoo Kim03787302014-06-23 13:22:06 -07002140 offslab_limit /= freelist_size_per_obj;
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002141
2142 if (num > offslab_limit)
2143 break;
2144 }
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002145
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002146 /* Found something acceptable - save it away */
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002147 cachep->num = num;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002148 cachep->gfporder = gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002149 left_over = remainder;
2150
2151 /*
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08002152 * A VFS-reclaimable slab tends to have most allocations
2153 * as GFP_NOFS and we really don't want to have to be allocating
2154 * higher-order pages when we are unable to shrink dcache.
2155 */
2156 if (flags & SLAB_RECLAIM_ACCOUNT)
2157 break;
2158
2159 /*
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002160 * Large number of objects is good, but very large slabs are
2161 * currently bad for the gfp()s.
2162 */
David Rientjes543585c2011-10-18 22:09:24 -07002163 if (gfporder >= slab_max_order)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002164 break;
2165
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002166 /*
2167 * Acceptable internal fragmentation?
2168 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002169 if (left_over * 8 <= (PAGE_SIZE << gfporder))
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002170 break;
2171 }
2172 return left_over;
2173}
2174
Pekka Enberg83b519e2009-06-10 19:40:04 +03002175static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002176{
Christoph Lameter97d06602012-07-06 15:25:11 -05002177 if (slab_state >= FULL)
Pekka Enberg83b519e2009-06-10 19:40:04 +03002178 return enable_cpucache(cachep, gfp);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002179
Christoph Lameter97d06602012-07-06 15:25:11 -05002180 if (slab_state == DOWN) {
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002181 /*
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002182 * Note: Creation of first cache (kmem_cache).
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002183 * The setup_node is taken care
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002184 * of by the caller of __kmem_cache_create
2185 */
2186 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2187 slab_state = PARTIAL;
2188 } else if (slab_state == PARTIAL) {
2189 /*
2190 * Note: the second kmem_cache_create must create the cache
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002191 * that's used by kmalloc(24), otherwise the creation of
2192 * further caches will BUG().
2193 */
2194 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2195
2196 /*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002197 * If the cache that's used by kmalloc(sizeof(kmem_cache_node)) is
2198 * the second cache, then we need to set up all its node/,
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002199 * otherwise the creation of further caches will BUG().
2200 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002201 set_up_node(cachep, SIZE_AC);
2202 if (INDEX_AC == INDEX_NODE)
2203 slab_state = PARTIAL_NODE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002204 else
Christoph Lameter97d06602012-07-06 15:25:11 -05002205 slab_state = PARTIAL_ARRAYCACHE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002206 } else {
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002207 /* Remaining boot caches */
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002208 cachep->array[smp_processor_id()] =
Pekka Enberg83b519e2009-06-10 19:40:04 +03002209 kmalloc(sizeof(struct arraycache_init), gfp);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002210
Christoph Lameter97d06602012-07-06 15:25:11 -05002211 if (slab_state == PARTIAL_ARRAYCACHE) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002212 set_up_node(cachep, SIZE_NODE);
2213 slab_state = PARTIAL_NODE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002214 } else {
2215 int node;
Pekka Enberg556a1692008-01-25 08:20:51 +02002216 for_each_online_node(node) {
Christoph Lameter6a673682013-01-10 19:14:19 +00002217 cachep->node[node] =
Christoph Lameter6744f0872013-01-10 19:12:17 +00002218 kmalloc_node(sizeof(struct kmem_cache_node),
Pekka Enbergeb91f1d2009-06-12 14:56:09 +03002219 gfp, node);
Christoph Lameter6a673682013-01-10 19:14:19 +00002220 BUG_ON(!cachep->node[node]);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002221 kmem_cache_node_init(cachep->node[node]);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002222 }
2223 }
2224 }
Christoph Lameter6a673682013-01-10 19:14:19 +00002225 cachep->node[numa_mem_id()]->next_reap =
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08002226 jiffies + REAPTIMEOUT_NODE +
2227 ((unsigned long)cachep) % REAPTIMEOUT_NODE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002228
2229 cpu_cache_get(cachep)->avail = 0;
2230 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
2231 cpu_cache_get(cachep)->batchcount = 1;
2232 cpu_cache_get(cachep)->touched = 0;
2233 cachep->batchcount = 1;
2234 cachep->limit = BOOT_CPUCACHE_ENTRIES;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002235 return 0;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002236}
2237
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002238/**
Christoph Lameter039363f2012-07-06 15:25:10 -05002239 * __kmem_cache_create - Create a cache.
Randy Dunlapa755b762012-11-06 17:10:10 -08002240 * @cachep: cache management descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 * @flags: SLAB flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 *
2243 * Returns a ptr to the cache on success, NULL on failure.
2244 * Cannot be called within a int, but can be interrupted.
Paul Mundt20c2df82007-07-20 10:11:58 +09002245 * The @ctor is run when new pages are allocated by the cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 * The flags are
2248 *
2249 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2250 * to catch references to uninitialised memory.
2251 *
2252 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2253 * for buffer overruns.
2254 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2256 * cacheline. This can be beneficial if you're counting cycles as closely
2257 * as davem.
2258 */
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002259int
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002260__kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
Joonsoo Kim8456a642013-10-24 10:07:49 +09002262 size_t left_over, freelist_size, ralign;
Pekka Enberg83b519e2009-06-10 19:40:04 +03002263 gfp_t gfp;
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002264 int err;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002265 size_t size = cachep->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268#if FORCED_DEBUG
2269 /*
2270 * Enable redzoning and last user accounting, except for caches with
2271 * large objects, if the increased size would increase the object size
2272 * above the next power of two: caches with object sizes just above a
2273 * power of two have a significant amount of internal fragmentation.
2274 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002275 if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
2276 2 * sizeof(unsigned long long)))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002277 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 if (!(flags & SLAB_DESTROY_BY_RCU))
2279 flags |= SLAB_POISON;
2280#endif
2281 if (flags & SLAB_DESTROY_BY_RCU)
2282 BUG_ON(flags & SLAB_POISON);
2283#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
Andrew Mortona737b3e2006-03-22 00:08:11 -08002285 /*
2286 * Check that size is in terms of words. This is needed to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 * unaligned accesses for some archs when redzoning is used, and makes
2288 * sure any on-slab bufctl's are also correctly aligned.
2289 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002290 if (size & (BYTES_PER_WORD - 1)) {
2291 size += (BYTES_PER_WORD - 1);
2292 size &= ~(BYTES_PER_WORD - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 }
2294
Pekka Enbergca5f9702006-09-25 23:31:25 -07002295 /*
David Woodhouse87a927c2007-07-04 21:26:44 -04002296 * Redzoning and user store require word alignment or possibly larger.
2297 * Note this will be overridden by architecture or caller mandated
2298 * alignment if either is greater than BYTES_PER_WORD.
Pekka Enbergca5f9702006-09-25 23:31:25 -07002299 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002300 if (flags & SLAB_STORE_USER)
2301 ralign = BYTES_PER_WORD;
2302
2303 if (flags & SLAB_RED_ZONE) {
2304 ralign = REDZONE_ALIGN;
2305 /* If redzoning, ensure that the second redzone is suitably
2306 * aligned, by adjusting the object size accordingly. */
2307 size += REDZONE_ALIGN - 1;
2308 size &= ~(REDZONE_ALIGN - 1);
2309 }
Pekka Enbergca5f9702006-09-25 23:31:25 -07002310
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002311 /* 3) caller mandated alignment */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002312 if (ralign < cachep->align) {
2313 ralign = cachep->align;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002315 /* disable debug if necessary */
2316 if (ralign > __alignof__(unsigned long long))
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002317 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002318 /*
Pekka Enbergca5f9702006-09-25 23:31:25 -07002319 * 4) Store it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002321 cachep->align = ralign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
Pekka Enberg83b519e2009-06-10 19:40:04 +03002323 if (slab_is_available())
2324 gfp = GFP_KERNEL;
2325 else
2326 gfp = GFP_NOWAIT;
2327
Christoph Lameter6a673682013-01-10 19:14:19 +00002328 setup_node_pointer(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Pekka Enbergca5f9702006-09-25 23:31:25 -07002331 /*
2332 * Both debugging options require word-alignment which is calculated
2333 * into align above.
2334 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 if (flags & SLAB_RED_ZONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 /* add space for red zone words */
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002337 cachep->obj_offset += sizeof(unsigned long long);
2338 size += 2 * sizeof(unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 }
2340 if (flags & SLAB_STORE_USER) {
Pekka Enbergca5f9702006-09-25 23:31:25 -07002341 /* user store requires one word storage behind the end of
David Woodhouse87a927c2007-07-04 21:26:44 -04002342 * the real object. But if the second red zone needs to be
2343 * aligned to 64 bits, we must allow that much space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002345 if (flags & SLAB_RED_ZONE)
2346 size += REDZONE_ALIGN;
2347 else
2348 size += BYTES_PER_WORD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 }
2350#if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002351 if (size >= kmalloc_size(INDEX_NODE + 1)
Tetsuo Handa608da7e2012-09-30 17:28:25 +09002352 && cachep->object_size > cache_line_size()
2353 && ALIGN(size, cachep->align) < PAGE_SIZE) {
2354 cachep->obj_offset += PAGE_SIZE - ALIGN(size, cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 size = PAGE_SIZE;
2356 }
2357#endif
2358#endif
2359
Ingo Molnare0a42722006-06-23 02:03:46 -07002360 /*
2361 * Determine if the slab management is 'on' or 'off' slab.
2362 * (bootstrapping cannot cope with offslab caches so don't do
Catalin Marinase7cb55b2009-10-28 13:33:08 +00002363 * it too early on. Always use on-slab management when
2364 * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
Ingo Molnare0a42722006-06-23 02:03:46 -07002365 */
Joonsoo Kim8fc9cf42013-12-02 17:49:43 +09002366 if ((size >= (PAGE_SIZE >> 5)) && !slab_early_init &&
Catalin Marinase7cb55b2009-10-28 13:33:08 +00002367 !(flags & SLAB_NOLEAKTRACE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 /*
2369 * Size is large, assume best to place the slab management obj
2370 * off-slab (should allow better packing of objs).
2371 */
2372 flags |= CFLGS_OFF_SLAB;
2373
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002374 size = ALIGN(size, cachep->align);
Joonsoo Kimf315e3f2013-12-02 17:49:41 +09002375 /*
2376 * We should restrict the number of objects in a slab to implement
2377 * byte sized index. Refer comment on SLAB_OBJ_MIN_SIZE definition.
2378 */
2379 if (FREELIST_BYTE_INDEX && size < SLAB_OBJ_MIN_SIZE)
2380 size = ALIGN(SLAB_OBJ_MIN_SIZE, cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002382 left_over = calculate_slab_order(cachep, size, cachep->align, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002384 if (!cachep->num)
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002385 return -E2BIG;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002386
Joonsoo Kim03787302014-06-23 13:22:06 -07002387 freelist_size = calculate_freelist_size(cachep->num, cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389 /*
2390 * If the slab has been placed off-slab, and we have enough space then
2391 * move it on-slab. This is at the expense of any extra colouring.
2392 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002393 if (flags & CFLGS_OFF_SLAB && left_over >= freelist_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 flags &= ~CFLGS_OFF_SLAB;
Joonsoo Kim8456a642013-10-24 10:07:49 +09002395 left_over -= freelist_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 }
2397
2398 if (flags & CFLGS_OFF_SLAB) {
2399 /* really off slab. No need for manual alignment */
Joonsoo Kim03787302014-06-23 13:22:06 -07002400 freelist_size = calculate_freelist_size(cachep->num, 0);
Ron Lee67461362009-05-22 04:58:22 +09302401
2402#ifdef CONFIG_PAGE_POISONING
2403 /* If we're going to use the generic kernel_map_pages()
2404 * poisoning, then it's going to smash the contents of
2405 * the redzone and userword anyhow, so switch them off.
2406 */
2407 if (size % PAGE_SIZE == 0 && flags & SLAB_POISON)
2408 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
2409#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 }
2411
2412 cachep->colour_off = cache_line_size();
2413 /* Offset must be a multiple of the alignment. */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002414 if (cachep->colour_off < cachep->align)
2415 cachep->colour_off = cachep->align;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002416 cachep->colour = left_over / cachep->colour_off;
Joonsoo Kim8456a642013-10-24 10:07:49 +09002417 cachep->freelist_size = freelist_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 cachep->flags = flags;
Joonsoo Kima57a4982013-10-24 10:07:44 +09002419 cachep->allocflags = __GFP_COMP;
Christoph Lameter4b51d662007-02-10 01:43:10 -08002420 if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
Glauber Costaa618e892012-06-14 16:17:21 +04002421 cachep->allocflags |= GFP_DMA;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002422 cachep->size = size;
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08002423 cachep->reciprocal_buffer_size = reciprocal_value(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002425 if (flags & CFLGS_OFF_SLAB) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09002426 cachep->freelist_cache = kmalloc_slab(freelist_size, 0u);
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002427 /*
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08002428 * This is a possibility for one of the kmalloc_{dma,}_caches.
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002429 * But since we go off slab only for object size greater than
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08002430 * PAGE_SIZE/8, and kmalloc_{dma,}_caches get created
2431 * in ascending order,this should not happen at all.
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002432 * But leave a BUG_ON for some lucky dude.
2433 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002434 BUG_ON(ZERO_OR_NULL_PTR(cachep->freelist_cache));
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002437 err = setup_cpu_cache(cachep, gfp);
2438 if (err) {
Christoph Lameter12c36672012-09-04 23:38:33 +00002439 __kmem_cache_shutdown(cachep);
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002440 return err;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Peter Zijlstra83835b32011-07-22 15:26:05 +02002443 if (flags & SLAB_DEBUG_OBJECTS) {
2444 /*
2445 * Would deadlock through slab_destroy()->call_rcu()->
2446 * debug_object_activate()->kmem_cache_alloc().
2447 */
2448 WARN_ON_ONCE(flags & SLAB_DESTROY_BY_RCU);
2449
2450 slab_set_debugobj_lock_classes(cachep);
Glauber Costa6ccfb5b2012-12-18 14:22:31 -08002451 } else if (!OFF_SLAB(cachep) && !(flags & SLAB_DESTROY_BY_RCU))
2452 on_slab_lock_classes(cachep);
Peter Zijlstra83835b32011-07-22 15:26:05 +02002453
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002454 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
2457#if DEBUG
2458static void check_irq_off(void)
2459{
2460 BUG_ON(!irqs_disabled());
2461}
2462
2463static void check_irq_on(void)
2464{
2465 BUG_ON(irqs_disabled());
2466}
2467
Pekka Enberg343e0d72006-02-01 03:05:50 -08002468static void check_spinlock_acquired(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
2470#ifdef CONFIG_SMP
2471 check_irq_off();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002472 assert_spin_locked(&get_node(cachep, numa_mem_id())->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473#endif
2474}
Christoph Lametere498be72005-09-09 13:03:32 -07002475
Pekka Enberg343e0d72006-02-01 03:05:50 -08002476static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07002477{
2478#ifdef CONFIG_SMP
2479 check_irq_off();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002480 assert_spin_locked(&get_node(cachep, node)->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002481#endif
2482}
2483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484#else
2485#define check_irq_off() do { } while(0)
2486#define check_irq_on() do { } while(0)
2487#define check_spinlock_acquired(x) do { } while(0)
Christoph Lametere498be72005-09-09 13:03:32 -07002488#define check_spinlock_acquired_node(x, y) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489#endif
2490
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002491static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
Christoph Lameteraab22072006-03-22 00:09:06 -08002492 struct array_cache *ac,
2493 int force, int node);
2494
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495static void do_drain(void *arg)
2496{
Andrew Mortona737b3e2006-03-22 00:08:11 -08002497 struct kmem_cache *cachep = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 struct array_cache *ac;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07002499 int node = numa_mem_id();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002500 struct kmem_cache_node *n;
Joonsoo Kim97654df2014-08-06 16:04:25 -07002501 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
2503 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002504 ac = cpu_cache_get(cachep);
Christoph Lameter18bf8542014-08-06 16:04:11 -07002505 n = get_node(cachep, node);
2506 spin_lock(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07002507 free_block(cachep, ac->entry, ac->avail, node, &list);
Christoph Lameter18bf8542014-08-06 16:04:11 -07002508 spin_unlock(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07002509 slabs_destroy(cachep, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 ac->avail = 0;
2511}
2512
Pekka Enberg343e0d72006-02-01 03:05:50 -08002513static void drain_cpu_caches(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002515 struct kmem_cache_node *n;
Christoph Lametere498be72005-09-09 13:03:32 -07002516 int node;
2517
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002518 on_each_cpu(do_drain, cachep, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 check_irq_on();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002520 for_each_kmem_cache_node(cachep, node, n)
2521 if (n->alien)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002522 drain_alien_cache(cachep, n->alien);
Roland Dreiera4523a82006-05-15 11:41:00 -07002523
Christoph Lameter18bf8542014-08-06 16:04:11 -07002524 for_each_kmem_cache_node(cachep, node, n)
2525 drain_array(cachep, n, n->shared, 1, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526}
2527
Christoph Lametered11d9e2006-06-30 01:55:45 -07002528/*
2529 * Remove slabs from the list of free slabs.
2530 * Specify the number of slabs to drain in tofree.
2531 *
2532 * Returns the actual number of slabs released.
2533 */
2534static int drain_freelist(struct kmem_cache *cache,
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002535 struct kmem_cache_node *n, int tofree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536{
Christoph Lametered11d9e2006-06-30 01:55:45 -07002537 struct list_head *p;
2538 int nr_freed;
Joonsoo Kim8456a642013-10-24 10:07:49 +09002539 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540
Christoph Lametered11d9e2006-06-30 01:55:45 -07002541 nr_freed = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002542 while (nr_freed < tofree && !list_empty(&n->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002544 spin_lock_irq(&n->list_lock);
2545 p = n->slabs_free.prev;
2546 if (p == &n->slabs_free) {
2547 spin_unlock_irq(&n->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002548 goto out;
2549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Joonsoo Kim8456a642013-10-24 10:07:49 +09002551 page = list_entry(p, struct page, lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552#if DEBUG
Joonsoo Kim8456a642013-10-24 10:07:49 +09002553 BUG_ON(page->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554#endif
Joonsoo Kim8456a642013-10-24 10:07:49 +09002555 list_del(&page->lru);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002556 /*
2557 * Safe to drop the lock. The slab is no longer linked
2558 * to the cache.
2559 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002560 n->free_objects -= cache->num;
2561 spin_unlock_irq(&n->list_lock);
Joonsoo Kim8456a642013-10-24 10:07:49 +09002562 slab_destroy(cache, page);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002563 nr_freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 }
Christoph Lametered11d9e2006-06-30 01:55:45 -07002565out:
2566 return nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567}
2568
Vladimir Davydov03afc0e2014-06-04 16:07:20 -07002569int __kmem_cache_shrink(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07002570{
Christoph Lameter18bf8542014-08-06 16:04:11 -07002571 int ret = 0;
2572 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002573 struct kmem_cache_node *n;
Christoph Lametere498be72005-09-09 13:03:32 -07002574
2575 drain_cpu_caches(cachep);
2576
2577 check_irq_on();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002578 for_each_kmem_cache_node(cachep, node, n) {
Wanpeng Li0fa81032013-07-04 08:33:22 +08002579 drain_freelist(cachep, n, slabs_tofree(cachep, n));
Christoph Lametered11d9e2006-06-30 01:55:45 -07002580
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002581 ret += !list_empty(&n->slabs_full) ||
2582 !list_empty(&n->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07002583 }
2584 return (ret ? 1 : 0);
2585}
2586
Christoph Lameter945cf2b2012-09-04 23:18:33 +00002587int __kmem_cache_shutdown(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588{
Christoph Lameter12c36672012-09-04 23:38:33 +00002589 int i;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002590 struct kmem_cache_node *n;
Vladimir Davydov03afc0e2014-06-04 16:07:20 -07002591 int rc = __kmem_cache_shrink(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Christoph Lameter12c36672012-09-04 23:38:33 +00002593 if (rc)
2594 return rc;
2595
2596 for_each_online_cpu(i)
2597 kfree(cachep->array[i]);
2598
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002599 /* NUMA: free the node structures */
Christoph Lameter18bf8542014-08-06 16:04:11 -07002600 for_each_kmem_cache_node(cachep, i, n) {
2601 kfree(n->shared);
2602 free_alien_cache(n->alien);
2603 kfree(n);
2604 cachep->node[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 }
Christoph Lameter12c36672012-09-04 23:38:33 +00002606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002609/*
2610 * Get the memory for a slab management obj.
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08002611 *
2612 * For a slab cache when the slab descriptor is off-slab, the
2613 * slab descriptor can't come from the same cache which is being created,
2614 * Because if it is the case, that means we defer the creation of
2615 * the kmalloc_{dma,}_cache of size sizeof(slab descriptor) to this point.
2616 * And we eventually call down to __kmem_cache_create(), which
2617 * in turn looks up in the kmalloc_{dma,}_caches for the disired-size one.
2618 * This is a "chicken-and-egg" problem.
2619 *
2620 * So the off-slab slab descriptor shall come from the kmalloc_{dma,}_caches,
2621 * which are all initialized during kmem_cache_init().
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002622 */
Joonsoo Kim7e007352013-10-30 19:04:01 +09002623static void *alloc_slabmgmt(struct kmem_cache *cachep,
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002624 struct page *page, int colour_off,
2625 gfp_t local_flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626{
Joonsoo Kim7e007352013-10-30 19:04:01 +09002627 void *freelist;
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002628 void *addr = page_address(page);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002629
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 if (OFF_SLAB(cachep)) {
2631 /* Slab management obj is off-slab. */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002632 freelist = kmem_cache_alloc_node(cachep->freelist_cache,
Pekka Enberg8759ec52008-11-26 10:01:31 +02002633 local_flags, nodeid);
Joonsoo Kim8456a642013-10-24 10:07:49 +09002634 if (!freelist)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 return NULL;
2636 } else {
Joonsoo Kim8456a642013-10-24 10:07:49 +09002637 freelist = addr + colour_off;
2638 colour_off += cachep->freelist_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 }
Joonsoo Kim8456a642013-10-24 10:07:49 +09002640 page->active = 0;
2641 page->s_mem = addr + colour_off;
2642 return freelist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643}
2644
Joonsoo Kim7cc68972014-04-18 16:24:09 +09002645static inline freelist_idx_t get_free_obj(struct page *page, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646{
Joonsoo Kima41adfa2013-12-02 17:49:42 +09002647 return ((freelist_idx_t *)page->freelist)[idx];
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002648}
2649
2650static inline void set_free_obj(struct page *page,
Joonsoo Kim7cc68972014-04-18 16:24:09 +09002651 unsigned int idx, freelist_idx_t val)
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002652{
Joonsoo Kima41adfa2013-12-02 17:49:42 +09002653 ((freelist_idx_t *)(page->freelist))[idx] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654}
2655
Pekka Enberg343e0d72006-02-01 03:05:50 -08002656static void cache_init_objs(struct kmem_cache *cachep,
Joonsoo Kim8456a642013-10-24 10:07:49 +09002657 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
2659 int i;
2660
2661 for (i = 0; i < cachep->num; i++) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09002662 void *objp = index_to_obj(cachep, page, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663#if DEBUG
2664 /* need to poison the objs? */
2665 if (cachep->flags & SLAB_POISON)
2666 poison_obj(cachep, objp, POISON_FREE);
2667 if (cachep->flags & SLAB_STORE_USER)
2668 *dbg_userword(cachep, objp) = NULL;
2669
2670 if (cachep->flags & SLAB_RED_ZONE) {
2671 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2672 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2673 }
2674 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002675 * Constructors are not allowed to allocate memory from the same
2676 * cache which they are a constructor for. Otherwise, deadlock.
2677 * They must also be threaded.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 */
2679 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002680 cachep->ctor(objp + obj_offset(cachep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681
2682 if (cachep->flags & SLAB_RED_ZONE) {
2683 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2684 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002685 " end of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2687 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002688 " start of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 }
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002690 if ((cachep->size % PAGE_SIZE) == 0 &&
Andrew Mortona737b3e2006-03-22 00:08:11 -08002691 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002692 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002693 cachep->size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694#else
2695 if (cachep->ctor)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002696 cachep->ctor(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697#endif
Joonsoo Kim03787302014-06-23 13:22:06 -07002698 set_obj_status(page, i, OBJECT_FREE);
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002699 set_free_obj(page, i, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701}
2702
Pekka Enberg343e0d72006-02-01 03:05:50 -08002703static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704{
Christoph Lameter4b51d662007-02-10 01:43:10 -08002705 if (CONFIG_ZONE_DMA_FLAG) {
2706 if (flags & GFP_DMA)
Glauber Costaa618e892012-06-14 16:17:21 +04002707 BUG_ON(!(cachep->allocflags & GFP_DMA));
Christoph Lameter4b51d662007-02-10 01:43:10 -08002708 else
Glauber Costaa618e892012-06-14 16:17:21 +04002709 BUG_ON(cachep->allocflags & GFP_DMA);
Christoph Lameter4b51d662007-02-10 01:43:10 -08002710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711}
2712
Joonsoo Kim8456a642013-10-24 10:07:49 +09002713static void *slab_get_obj(struct kmem_cache *cachep, struct page *page,
Andrew Mortona737b3e2006-03-22 00:08:11 -08002714 int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002715{
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002716 void *objp;
Matthew Dobson78d382d2006-02-01 03:05:47 -08002717
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002718 objp = index_to_obj(cachep, page, get_free_obj(page, page->active));
Joonsoo Kim8456a642013-10-24 10:07:49 +09002719 page->active++;
Matthew Dobson78d382d2006-02-01 03:05:47 -08002720#if DEBUG
Joonsoo Kim1ea991b2013-10-24 10:07:40 +09002721 WARN_ON(page_to_nid(virt_to_page(objp)) != nodeid);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002722#endif
Matthew Dobson78d382d2006-02-01 03:05:47 -08002723
2724 return objp;
2725}
2726
Joonsoo Kim8456a642013-10-24 10:07:49 +09002727static void slab_put_obj(struct kmem_cache *cachep, struct page *page,
Andrew Mortona737b3e2006-03-22 00:08:11 -08002728 void *objp, int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002729{
Joonsoo Kim8456a642013-10-24 10:07:49 +09002730 unsigned int objnr = obj_to_index(cachep, page, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002731#if DEBUG
Joonsoo Kim16025172013-10-24 10:07:46 +09002732 unsigned int i;
Matthew Dobson78d382d2006-02-01 03:05:47 -08002733
Matthew Dobson78d382d2006-02-01 03:05:47 -08002734 /* Verify that the slab belongs to the intended node */
Joonsoo Kim1ea991b2013-10-24 10:07:40 +09002735 WARN_ON(page_to_nid(virt_to_page(objp)) != nodeid);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002736
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002737 /* Verify double free bug */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002738 for (i = page->active; i < cachep->num; i++) {
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002739 if (get_free_obj(page, i) == objnr) {
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002740 printk(KERN_ERR "slab: double free detected in cache "
2741 "'%s', objp %p\n", cachep->name, objp);
2742 BUG();
2743 }
Matthew Dobson78d382d2006-02-01 03:05:47 -08002744 }
2745#endif
Joonsoo Kim8456a642013-10-24 10:07:49 +09002746 page->active--;
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002747 set_free_obj(page, page->active, objnr);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002748}
2749
Pekka Enberg47768742006-06-23 02:03:07 -07002750/*
2751 * Map pages beginning at addr to the given cache and slab. This is required
2752 * for the slab allocator to be able to lookup the cache and slab of a
Nick Pigginccd35fb2011-01-07 17:49:17 +11002753 * virtual address for kfree, ksize, and slab debugging.
Pekka Enberg47768742006-06-23 02:03:07 -07002754 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002755static void slab_map_pages(struct kmem_cache *cache, struct page *page,
Joonsoo Kim7e007352013-10-30 19:04:01 +09002756 void *freelist)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757{
Joonsoo Kima57a4982013-10-24 10:07:44 +09002758 page->slab_cache = cache;
Joonsoo Kim8456a642013-10-24 10:07:49 +09002759 page->freelist = freelist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760}
2761
2762/*
2763 * Grow (by 1) the number of slabs within a cache. This is called by
2764 * kmem_cache_alloc() when there are no active objs left in a cache.
2765 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002766static int cache_grow(struct kmem_cache *cachep,
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002767 gfp_t flags, int nodeid, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768{
Joonsoo Kim7e007352013-10-30 19:04:01 +09002769 void *freelist;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002770 size_t offset;
2771 gfp_t local_flags;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002772 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Andrew Mortona737b3e2006-03-22 00:08:11 -08002774 /*
2775 * Be lazy and only check for valid flags here, keeping it out of the
2776 * critical path in kmem_cache_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 */
Christoph Lameter6cb06222007-10-16 01:25:41 -07002778 BUG_ON(flags & GFP_SLAB_BUG_MASK);
2779 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002781 /* Take the node list lock to change the colour_next on this node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 check_irq_off();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002783 n = get_node(cachep, nodeid);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002784 spin_lock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
2786 /* Get colour for the slab, and cal the next value. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002787 offset = n->colour_next;
2788 n->colour_next++;
2789 if (n->colour_next >= cachep->colour)
2790 n->colour_next = 0;
2791 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002793 offset *= cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794
2795 if (local_flags & __GFP_WAIT)
2796 local_irq_enable();
2797
2798 /*
2799 * The test for missing atomic flag is performed here, rather than
2800 * the more obvious place, simply to reduce the critical path length
2801 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2802 * will eventually be caught here (where it matters).
2803 */
2804 kmem_flagcheck(cachep, flags);
2805
Andrew Mortona737b3e2006-03-22 00:08:11 -08002806 /*
2807 * Get mem for the objs. Attempt to allocate a physical page from
2808 * 'nodeid'.
Christoph Lametere498be72005-09-09 13:03:32 -07002809 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002810 if (!page)
2811 page = kmem_getpages(cachep, local_flags, nodeid);
2812 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 goto failed;
2814
2815 /* Get slab management. */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002816 freelist = alloc_slabmgmt(cachep, page, offset,
Christoph Lameter6cb06222007-10-16 01:25:41 -07002817 local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
Joonsoo Kim8456a642013-10-24 10:07:49 +09002818 if (!freelist)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 goto opps1;
2820
Joonsoo Kim8456a642013-10-24 10:07:49 +09002821 slab_map_pages(cachep, page, freelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Joonsoo Kim8456a642013-10-24 10:07:49 +09002823 cache_init_objs(cachep, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
2825 if (local_flags & __GFP_WAIT)
2826 local_irq_disable();
2827 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002828 spin_lock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
2830 /* Make slab active. */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002831 list_add_tail(&page->lru, &(n->slabs_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 STATS_INC_GROWN(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002833 n->free_objects += cachep->num;
2834 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002836opps1:
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002837 kmem_freepages(cachep, page);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002838failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 if (local_flags & __GFP_WAIT)
2840 local_irq_disable();
2841 return 0;
2842}
2843
2844#if DEBUG
2845
2846/*
2847 * Perform extra freeing checks:
2848 * - detect bad pointers.
2849 * - POISON/RED_ZONE checking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 */
2851static void kfree_debugcheck(const void *objp)
2852{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 if (!virt_addr_valid(objp)) {
2854 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002855 (unsigned long)objp);
2856 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858}
2859
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002860static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2861{
David Woodhouseb46b8f12007-05-08 00:22:59 -07002862 unsigned long long redzone1, redzone2;
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002863
2864 redzone1 = *dbg_redzone1(cache, obj);
2865 redzone2 = *dbg_redzone2(cache, obj);
2866
2867 /*
2868 * Redzone is ok.
2869 */
2870 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2871 return;
2872
2873 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2874 slab_error(cache, "double free detected");
2875 else
2876 slab_error(cache, "memory outside object was overwritten");
2877
David Woodhouseb46b8f12007-05-08 00:22:59 -07002878 printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002879 obj, redzone1, redzone2);
2880}
2881
Pekka Enberg343e0d72006-02-01 03:05:50 -08002882static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002883 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 unsigned int objnr;
Joonsoo Kim8456a642013-10-24 10:07:49 +09002886 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887
Matthew Wilcox80cbd912007-11-29 12:05:13 -07002888 BUG_ON(virt_to_cache(objp) != cachep);
2889
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002890 objp -= obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 kfree_debugcheck(objp);
Christoph Lameterb49af682007-05-06 14:49:41 -07002892 page = virt_to_head_page(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 if (cachep->flags & SLAB_RED_ZONE) {
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002895 verify_redzone_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2897 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2898 }
2899 if (cachep->flags & SLAB_STORE_USER)
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002900 *dbg_userword(cachep, objp) = (void *)caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901
Joonsoo Kim8456a642013-10-24 10:07:49 +09002902 objnr = obj_to_index(cachep, page, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903
2904 BUG_ON(objnr >= cachep->num);
Joonsoo Kim8456a642013-10-24 10:07:49 +09002905 BUG_ON(objp != index_to_obj(cachep, page, objnr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
Joonsoo Kim03787302014-06-23 13:22:06 -07002907 set_obj_status(page, objnr, OBJECT_FREE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 if (cachep->flags & SLAB_POISON) {
2909#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002910 if ((cachep->size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002911 store_stackinfo(cachep, objp, caller);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002912 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002913 cachep->size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 } else {
2915 poison_obj(cachep, objp, POISON_FREE);
2916 }
2917#else
2918 poison_obj(cachep, objp, POISON_FREE);
2919#endif
2920 }
2921 return objp;
2922}
2923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924#else
2925#define kfree_debugcheck(x) do { } while(0)
2926#define cache_free_debugcheck(x,objp,z) (objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927#endif
2928
Mel Gorman072bb0a2012-07-31 16:43:58 -07002929static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags,
2930 bool force_refill)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931{
2932 int batchcount;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002933 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 struct array_cache *ac;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002935 int node;
2936
Joe Korty6d2144d2008-03-05 15:04:59 -08002937 check_irq_off();
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07002938 node = numa_mem_id();
Mel Gorman072bb0a2012-07-31 16:43:58 -07002939 if (unlikely(force_refill))
2940 goto force_grow;
2941retry:
Joe Korty6d2144d2008-03-05 15:04:59 -08002942 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 batchcount = ac->batchcount;
2944 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002945 /*
2946 * If there was little recent activity on this cache, then
2947 * perform only a partial refill. Otherwise we could generate
2948 * refill bouncing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 */
2950 batchcount = BATCHREFILL_LIMIT;
2951 }
Christoph Lameter18bf8542014-08-06 16:04:11 -07002952 n = get_node(cachep, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002954 BUG_ON(ac->avail > 0 || !n);
2955 spin_lock(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002956
Christoph Lameter3ded1752006-03-25 03:06:44 -08002957 /* See if we can refill from the shared array */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002958 if (n->shared && transfer_objects(ac, n->shared, batchcount)) {
2959 n->shared->touched = 1;
Christoph Lameter3ded1752006-03-25 03:06:44 -08002960 goto alloc_done;
Nick Piggin44b57f12010-01-27 22:27:40 +11002961 }
Christoph Lameter3ded1752006-03-25 03:06:44 -08002962
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 while (batchcount > 0) {
2964 struct list_head *entry;
Joonsoo Kim8456a642013-10-24 10:07:49 +09002965 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 /* Get slab alloc is to come from. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002967 entry = n->slabs_partial.next;
2968 if (entry == &n->slabs_partial) {
2969 n->free_touched = 1;
2970 entry = n->slabs_free.next;
2971 if (entry == &n->slabs_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 goto must_grow;
2973 }
2974
Joonsoo Kim8456a642013-10-24 10:07:49 +09002975 page = list_entry(entry, struct page, lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 check_spinlock_acquired(cachep);
Pekka Enberg714b81712007-05-06 14:49:03 -07002977
2978 /*
2979 * The slab was either on partial or free list so
2980 * there must be at least one object available for
2981 * allocation.
2982 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002983 BUG_ON(page->active >= cachep->num);
Pekka Enberg714b81712007-05-06 14:49:03 -07002984
Joonsoo Kim8456a642013-10-24 10:07:49 +09002985 while (page->active < cachep->num && batchcount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 STATS_INC_ALLOCED(cachep);
2987 STATS_INC_ACTIVE(cachep);
2988 STATS_SET_HIGH(cachep);
2989
Joonsoo Kim8456a642013-10-24 10:07:49 +09002990 ac_put_obj(cachep, ac, slab_get_obj(cachep, page,
Mel Gorman072bb0a2012-07-31 16:43:58 -07002991 node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
2994 /* move slabp to correct slabp list: */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002995 list_del(&page->lru);
2996 if (page->active == cachep->num)
Dave Hansen34bf6ef2014-04-08 13:44:27 -07002997 list_add(&page->lru, &n->slabs_full);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 else
Dave Hansen34bf6ef2014-04-08 13:44:27 -07002999 list_add(&page->lru, &n->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 }
3001
Andrew Mortona737b3e2006-03-22 00:08:11 -08003002must_grow:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003003 n->free_objects -= ac->avail;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003004alloc_done:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003005 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006
3007 if (unlikely(!ac->avail)) {
3008 int x;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003009force_grow:
Christoph Lameter3c517a62006-12-06 20:33:29 -08003010 x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07003011
Andrew Mortona737b3e2006-03-22 00:08:11 -08003012 /* cache_grow can reenable interrupts, then ac could change. */
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003013 ac = cpu_cache_get(cachep);
David Rientjes51cd8e62012-08-28 19:57:21 -07003014 node = numa_mem_id();
Mel Gorman072bb0a2012-07-31 16:43:58 -07003015
3016 /* no objects in sight? abort */
3017 if (!x && (ac->avail == 0 || force_refill))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 return NULL;
3019
Andrew Mortona737b3e2006-03-22 00:08:11 -08003020 if (!ac->avail) /* objects refilled by interrupt? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 goto retry;
3022 }
3023 ac->touched = 1;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003024
3025 return ac_get_obj(cachep, ac, flags, force_refill);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026}
3027
Andrew Mortona737b3e2006-03-22 00:08:11 -08003028static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
3029 gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030{
3031 might_sleep_if(flags & __GFP_WAIT);
3032#if DEBUG
3033 kmem_flagcheck(cachep, flags);
3034#endif
3035}
3036
3037#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003038static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003039 gfp_t flags, void *objp, unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040{
Joonsoo Kim03787302014-06-23 13:22:06 -07003041 struct page *page;
3042
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003043 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044 return objp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003045 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003047 if ((cachep->size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003048 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003049 cachep->size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 else
3051 check_poison_obj(cachep, objp);
3052#else
3053 check_poison_obj(cachep, objp);
3054#endif
3055 poison_obj(cachep, objp, POISON_INUSE);
3056 }
3057 if (cachep->flags & SLAB_STORE_USER)
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003058 *dbg_userword(cachep, objp) = (void *)caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059
3060 if (cachep->flags & SLAB_RED_ZONE) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003061 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
3062 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
3063 slab_error(cachep, "double free, or memory outside"
3064 " object was overwritten");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003065 printk(KERN_ERR
David Woodhouseb46b8f12007-05-08 00:22:59 -07003066 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08003067 objp, *dbg_redzone1(cachep, objp),
3068 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 }
3070 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
3071 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
3072 }
Joonsoo Kim03787302014-06-23 13:22:06 -07003073
3074 page = virt_to_head_page(objp);
3075 set_obj_status(page, obj_to_index(cachep, page, objp), OBJECT_ACTIVE);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003076 objp += obj_offset(cachep);
Christoph Lameter4f104932007-05-06 14:50:17 -07003077 if (cachep->ctor && cachep->flags & SLAB_POISON)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003078 cachep->ctor(objp);
Tetsuo Handa7ea466f2011-07-21 09:42:45 +09003079 if (ARCH_SLAB_MINALIGN &&
3080 ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003081 printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
Hugh Dickinsc2251502011-07-11 13:35:08 -07003082 objp, (int)ARCH_SLAB_MINALIGN);
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 return objp;
3085}
3086#else
3087#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
3088#endif
3089
Akinobu Mita773ff602008-12-23 19:37:01 +09003090static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags)
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003091{
Joonsoo Kim8a9c61d2014-08-06 16:04:20 -07003092 if (unlikely(cachep == kmem_cache))
Akinobu Mita773ff602008-12-23 19:37:01 +09003093 return false;
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003094
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003095 return should_failslab(cachep->object_size, flags, cachep->flags);
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003096}
3097
Pekka Enberg343e0d72006-02-01 03:05:50 -08003098static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003100 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 struct array_cache *ac;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003102 bool force_refill = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103
Alok N Kataria5c382302005-09-27 21:45:46 -07003104 check_irq_off();
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003105
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003106 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 if (likely(ac->avail)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 ac->touched = 1;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003109 objp = ac_get_obj(cachep, ac, flags, false);
3110
J. R. Okajimaddbf2e82009-12-02 16:55:50 +09003111 /*
Mel Gorman072bb0a2012-07-31 16:43:58 -07003112 * Allow for the possibility all avail objects are not allowed
3113 * by the current flags
J. R. Okajimaddbf2e82009-12-02 16:55:50 +09003114 */
Mel Gorman072bb0a2012-07-31 16:43:58 -07003115 if (objp) {
3116 STATS_INC_ALLOCHIT(cachep);
3117 goto out;
3118 }
3119 force_refill = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 }
Mel Gorman072bb0a2012-07-31 16:43:58 -07003121
3122 STATS_INC_ALLOCMISS(cachep);
3123 objp = cache_alloc_refill(cachep, flags, force_refill);
3124 /*
3125 * the 'ac' may be updated by cache_alloc_refill(),
3126 * and kmemleak_erase() requires its correct value.
3127 */
3128 ac = cpu_cache_get(cachep);
3129
3130out:
Catalin Marinasd5cff632009-06-11 13:22:40 +01003131 /*
3132 * To avoid a false negative, if an object that is in one of the
3133 * per-CPU caches is leaked, we need to make sure kmemleak doesn't
3134 * treat the array pointers as a reference to the object.
3135 */
J. R. Okajimaf3d8b532009-12-02 16:55:49 +09003136 if (objp)
3137 kmemleak_erase(&ac->entry[ac->avail]);
Alok N Kataria5c382302005-09-27 21:45:46 -07003138 return objp;
3139}
3140
Christoph Lametere498be72005-09-09 13:03:32 -07003141#ifdef CONFIG_NUMA
3142/*
David Rientjesf0432d12014-04-07 15:37:30 -07003143 * Try allocating on another node if PF_SPREAD_SLAB is a mempolicy is set.
Paul Jacksonc61afb12006-03-24 03:16:08 -08003144 *
3145 * If we are in_interrupt, then process context, including cpusets and
3146 * mempolicy, may not apply and should not be used for allocation policy.
3147 */
3148static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
3149{
3150 int nid_alloc, nid_here;
3151
Christoph Lameter765c4502006-09-27 01:50:08 -07003152 if (in_interrupt() || (flags & __GFP_THISNODE))
Paul Jacksonc61afb12006-03-24 03:16:08 -08003153 return NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003154 nid_alloc = nid_here = numa_mem_id();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003155 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
Jack Steiner6adef3e2010-05-26 14:42:49 -07003156 nid_alloc = cpuset_slab_spread_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003157 else if (current->mempolicy)
David Rientjes2a389612014-04-07 15:37:29 -07003158 nid_alloc = mempolicy_slab_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003159 if (nid_alloc != nid_here)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003160 return ____cache_alloc_node(cachep, flags, nid_alloc);
Paul Jacksonc61afb12006-03-24 03:16:08 -08003161 return NULL;
3162}
3163
3164/*
Christoph Lameter765c4502006-09-27 01:50:08 -07003165 * Fallback function if there was no memory available and no objects on a
Christoph Lameter3c517a62006-12-06 20:33:29 -08003166 * certain node and fall back is permitted. First we scan all the
Christoph Lameter6a673682013-01-10 19:14:19 +00003167 * available node for available objects. If that fails then we
Christoph Lameter3c517a62006-12-06 20:33:29 -08003168 * perform an allocation without specifying a node. This allows the page
3169 * allocator to do its reclaim / fallback magic. We then insert the
3170 * slab into the proper nodelist and then allocate from it.
Christoph Lameter765c4502006-09-27 01:50:08 -07003171 */
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003172static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
Christoph Lameter765c4502006-09-27 01:50:08 -07003173{
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003174 struct zonelist *zonelist;
3175 gfp_t local_flags;
Mel Gormandd1a2392008-04-28 02:12:17 -07003176 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07003177 struct zone *zone;
3178 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003179 void *obj = NULL;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003180 int nid;
Mel Gormancc9a6c82012-03-21 16:34:11 -07003181 unsigned int cpuset_mems_cookie;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003182
3183 if (flags & __GFP_THISNODE)
3184 return NULL;
3185
Christoph Lameter6cb06222007-10-16 01:25:41 -07003186 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Christoph Lameter765c4502006-09-27 01:50:08 -07003187
Mel Gormancc9a6c82012-03-21 16:34:11 -07003188retry_cpuset:
Mel Gormand26914d2014-04-03 14:47:24 -07003189 cpuset_mems_cookie = read_mems_allowed_begin();
David Rientjes2a389612014-04-07 15:37:29 -07003190 zonelist = node_zonelist(mempolicy_slab_node(), flags);
Mel Gormancc9a6c82012-03-21 16:34:11 -07003191
Christoph Lameter3c517a62006-12-06 20:33:29 -08003192retry:
3193 /*
3194 * Look through allowed nodes for objects available
3195 * from existing per node queues.
3196 */
Mel Gorman54a6eb52008-04-28 02:12:16 -07003197 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
3198 nid = zone_to_nid(zone);
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003199
Mel Gorman54a6eb52008-04-28 02:12:16 -07003200 if (cpuset_zone_allowed_hardwall(zone, flags) &&
Christoph Lameter18bf8542014-08-06 16:04:11 -07003201 get_node(cache, nid) &&
3202 get_node(cache, nid)->free_objects) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003203 obj = ____cache_alloc_node(cache,
3204 flags | GFP_THISNODE, nid);
Christoph Lameter481c5342008-06-21 16:46:35 -07003205 if (obj)
3206 break;
3207 }
Christoph Lameter3c517a62006-12-06 20:33:29 -08003208 }
3209
Christoph Lametercfce6602007-05-06 14:50:17 -07003210 if (!obj) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003211 /*
3212 * This allocation will be performed within the constraints
3213 * of the current cpuset / memory policy requirements.
3214 * We may trigger various forms of reclaim on the allowed
3215 * set and go into memory reserves if necessary.
3216 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003217 struct page *page;
3218
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003219 if (local_flags & __GFP_WAIT)
3220 local_irq_enable();
3221 kmem_flagcheck(cache, flags);
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003222 page = kmem_getpages(cache, local_flags, numa_mem_id());
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003223 if (local_flags & __GFP_WAIT)
3224 local_irq_disable();
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003225 if (page) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003226 /*
3227 * Insert into the appropriate per node queues
3228 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003229 nid = page_to_nid(page);
3230 if (cache_grow(cache, flags, nid, page)) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003231 obj = ____cache_alloc_node(cache,
3232 flags | GFP_THISNODE, nid);
3233 if (!obj)
3234 /*
3235 * Another processor may allocate the
3236 * objects in the slab since we are
3237 * not holding any locks.
3238 */
3239 goto retry;
3240 } else {
Hugh Dickinsb6a60452007-01-05 16:36:36 -08003241 /* cache_grow already freed obj */
Christoph Lameter3c517a62006-12-06 20:33:29 -08003242 obj = NULL;
3243 }
3244 }
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003245 }
Mel Gormancc9a6c82012-03-21 16:34:11 -07003246
Mel Gormand26914d2014-04-03 14:47:24 -07003247 if (unlikely(!obj && read_mems_allowed_retry(cpuset_mems_cookie)))
Mel Gormancc9a6c82012-03-21 16:34:11 -07003248 goto retry_cpuset;
Christoph Lameter765c4502006-09-27 01:50:08 -07003249 return obj;
3250}
3251
3252/*
Christoph Lametere498be72005-09-09 13:03:32 -07003253 * A interface to enable slab creation on nodeid
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003255static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003256 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07003257{
3258 struct list_head *entry;
Joonsoo Kim8456a642013-10-24 10:07:49 +09003259 struct page *page;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003260 struct kmem_cache_node *n;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003261 void *obj;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003262 int x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
Aaron Tomlin14e50c62013-04-26 16:15:34 +01003264 VM_BUG_ON(nodeid > num_online_nodes());
Christoph Lameter18bf8542014-08-06 16:04:11 -07003265 n = get_node(cachep, nodeid);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003266 BUG_ON(!n);
Christoph Lametere498be72005-09-09 13:03:32 -07003267
Andrew Mortona737b3e2006-03-22 00:08:11 -08003268retry:
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003269 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003270 spin_lock(&n->list_lock);
3271 entry = n->slabs_partial.next;
3272 if (entry == &n->slabs_partial) {
3273 n->free_touched = 1;
3274 entry = n->slabs_free.next;
3275 if (entry == &n->slabs_free)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003276 goto must_grow;
3277 }
Christoph Lametere498be72005-09-09 13:03:32 -07003278
Joonsoo Kim8456a642013-10-24 10:07:49 +09003279 page = list_entry(entry, struct page, lru);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003280 check_spinlock_acquired_node(cachep, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07003281
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003282 STATS_INC_NODEALLOCS(cachep);
3283 STATS_INC_ACTIVE(cachep);
3284 STATS_SET_HIGH(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003285
Joonsoo Kim8456a642013-10-24 10:07:49 +09003286 BUG_ON(page->active == cachep->num);
Christoph Lametere498be72005-09-09 13:03:32 -07003287
Joonsoo Kim8456a642013-10-24 10:07:49 +09003288 obj = slab_get_obj(cachep, page, nodeid);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003289 n->free_objects--;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003290 /* move slabp to correct slabp list: */
Joonsoo Kim8456a642013-10-24 10:07:49 +09003291 list_del(&page->lru);
Christoph Lametere498be72005-09-09 13:03:32 -07003292
Joonsoo Kim8456a642013-10-24 10:07:49 +09003293 if (page->active == cachep->num)
3294 list_add(&page->lru, &n->slabs_full);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003295 else
Joonsoo Kim8456a642013-10-24 10:07:49 +09003296 list_add(&page->lru, &n->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07003297
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003298 spin_unlock(&n->list_lock);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003299 goto done;
Christoph Lametere498be72005-09-09 13:03:32 -07003300
Andrew Mortona737b3e2006-03-22 00:08:11 -08003301must_grow:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003302 spin_unlock(&n->list_lock);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003303 x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
Christoph Lameter765c4502006-09-27 01:50:08 -07003304 if (x)
3305 goto retry;
Christoph Lametere498be72005-09-09 13:03:32 -07003306
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003307 return fallback_alloc(cachep, flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003308
Andrew Mortona737b3e2006-03-22 00:08:11 -08003309done:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003310 return obj;
Christoph Lametere498be72005-09-09 13:03:32 -07003311}
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003312
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003313static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003314slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003315 unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003316{
3317 unsigned long save_flags;
3318 void *ptr;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003319 int slab_node = numa_mem_id();
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003320
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003321 flags &= gfp_allowed_mask;
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003322
Nick Piggincf40bd12009-01-21 08:12:39 +01003323 lockdep_trace_alloc(flags);
3324
Akinobu Mita773ff602008-12-23 19:37:01 +09003325 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003326 return NULL;
3327
Glauber Costad79923f2012-12-18 14:22:48 -08003328 cachep = memcg_kmem_get_cache(cachep, flags);
3329
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003330 cache_alloc_debugcheck_before(cachep, flags);
3331 local_irq_save(save_flags);
3332
Andrew Mortoneacbbae2011-07-28 13:59:49 -07003333 if (nodeid == NUMA_NO_NODE)
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003334 nodeid = slab_node;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003335
Christoph Lameter18bf8542014-08-06 16:04:11 -07003336 if (unlikely(!get_node(cachep, nodeid))) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003337 /* Node not bootstrapped yet */
3338 ptr = fallback_alloc(cachep, flags);
3339 goto out;
3340 }
3341
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003342 if (nodeid == slab_node) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003343 /*
3344 * Use the locally cached objects if possible.
3345 * However ____cache_alloc does not allow fallback
3346 * to other nodes. It may fail while we still have
3347 * objects on other nodes available.
3348 */
3349 ptr = ____cache_alloc(cachep, flags);
3350 if (ptr)
3351 goto out;
3352 }
3353 /* ___cache_alloc_node can fall back to other nodes */
3354 ptr = ____cache_alloc_node(cachep, flags, nodeid);
3355 out:
3356 local_irq_restore(save_flags);
3357 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003358 kmemleak_alloc_recursive(ptr, cachep->object_size, 1, cachep->flags,
Catalin Marinasd5cff632009-06-11 13:22:40 +01003359 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003360
Joe Perches5087c822013-09-10 17:02:51 -07003361 if (likely(ptr)) {
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003362 kmemcheck_slab_alloc(cachep, flags, ptr, cachep->object_size);
Joe Perches5087c822013-09-10 17:02:51 -07003363 if (unlikely(flags & __GFP_ZERO))
3364 memset(ptr, 0, cachep->object_size);
3365 }
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003366
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003367 return ptr;
3368}
3369
3370static __always_inline void *
3371__do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
3372{
3373 void *objp;
3374
David Rientjesf0432d12014-04-07 15:37:30 -07003375 if (current->mempolicy || unlikely(current->flags & PF_SPREAD_SLAB)) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003376 objp = alternate_node_alloc(cache, flags);
3377 if (objp)
3378 goto out;
3379 }
3380 objp = ____cache_alloc(cache, flags);
3381
3382 /*
3383 * We may just have run out of memory on the local node.
3384 * ____cache_alloc_node() knows how to locate memory on other nodes
3385 */
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003386 if (!objp)
3387 objp = ____cache_alloc_node(cache, flags, numa_mem_id());
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003388
3389 out:
3390 return objp;
3391}
3392#else
3393
3394static __always_inline void *
3395__do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
3396{
3397 return ____cache_alloc(cachep, flags);
3398}
3399
3400#endif /* CONFIG_NUMA */
3401
3402static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003403slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003404{
3405 unsigned long save_flags;
3406 void *objp;
3407
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003408 flags &= gfp_allowed_mask;
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003409
Nick Piggincf40bd12009-01-21 08:12:39 +01003410 lockdep_trace_alloc(flags);
3411
Akinobu Mita773ff602008-12-23 19:37:01 +09003412 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003413 return NULL;
3414
Glauber Costad79923f2012-12-18 14:22:48 -08003415 cachep = memcg_kmem_get_cache(cachep, flags);
3416
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003417 cache_alloc_debugcheck_before(cachep, flags);
3418 local_irq_save(save_flags);
3419 objp = __do_cache_alloc(cachep, flags);
3420 local_irq_restore(save_flags);
3421 objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003422 kmemleak_alloc_recursive(objp, cachep->object_size, 1, cachep->flags,
Catalin Marinasd5cff632009-06-11 13:22:40 +01003423 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003424 prefetchw(objp);
3425
Joe Perches5087c822013-09-10 17:02:51 -07003426 if (likely(objp)) {
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003427 kmemcheck_slab_alloc(cachep, flags, objp, cachep->object_size);
Joe Perches5087c822013-09-10 17:02:51 -07003428 if (unlikely(flags & __GFP_ZERO))
3429 memset(objp, 0, cachep->object_size);
3430 }
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003431
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003432 return objp;
3433}
Christoph Lametere498be72005-09-09 13:03:32 -07003434
3435/*
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08003436 * Caller needs to acquire correct kmem_cache_node's list_lock
Joonsoo Kim97654df2014-08-06 16:04:25 -07003437 * @list: List of detached free slabs should be freed by caller
Christoph Lametere498be72005-09-09 13:03:32 -07003438 */
Joonsoo Kim97654df2014-08-06 16:04:25 -07003439static void free_block(struct kmem_cache *cachep, void **objpp,
3440 int nr_objects, int node, struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441{
3442 int i;
Joonsoo Kim25c063f2014-08-06 16:04:22 -07003443 struct kmem_cache_node *n = get_node(cachep, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
3445 for (i = 0; i < nr_objects; i++) {
Mel Gorman072bb0a2012-07-31 16:43:58 -07003446 void *objp;
Joonsoo Kim8456a642013-10-24 10:07:49 +09003447 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
Mel Gorman072bb0a2012-07-31 16:43:58 -07003449 clear_obj_pfmemalloc(&objpp[i]);
3450 objp = objpp[i];
3451
Joonsoo Kim8456a642013-10-24 10:07:49 +09003452 page = virt_to_head_page(objp);
Joonsoo Kim8456a642013-10-24 10:07:49 +09003453 list_del(&page->lru);
Christoph Lameterff694162005-09-22 21:44:02 -07003454 check_spinlock_acquired_node(cachep, node);
Joonsoo Kim8456a642013-10-24 10:07:49 +09003455 slab_put_obj(cachep, page, objp, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 STATS_DEC_ACTIVE(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003457 n->free_objects++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458
3459 /* fixup slab chains */
Joonsoo Kim8456a642013-10-24 10:07:49 +09003460 if (page->active == 0) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003461 if (n->free_objects > n->free_limit) {
3462 n->free_objects -= cachep->num;
Joonsoo Kim97654df2014-08-06 16:04:25 -07003463 list_add_tail(&page->lru, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 } else {
Joonsoo Kim8456a642013-10-24 10:07:49 +09003465 list_add(&page->lru, &n->slabs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 }
3467 } else {
3468 /* Unconditionally move a slab to the end of the
3469 * partial list on free - maximum time for the
3470 * other objects to be freed, too.
3471 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09003472 list_add_tail(&page->lru, &n->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 }
3474 }
3475}
3476
Pekka Enberg343e0d72006-02-01 03:05:50 -08003477static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478{
3479 int batchcount;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003480 struct kmem_cache_node *n;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003481 int node = numa_mem_id();
Joonsoo Kim97654df2014-08-06 16:04:25 -07003482 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
3484 batchcount = ac->batchcount;
3485#if DEBUG
3486 BUG_ON(!batchcount || batchcount > ac->avail);
3487#endif
3488 check_irq_off();
Christoph Lameter18bf8542014-08-06 16:04:11 -07003489 n = get_node(cachep, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003490 spin_lock(&n->list_lock);
3491 if (n->shared) {
3492 struct array_cache *shared_array = n->shared;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003493 int max = shared_array->limit - shared_array->avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 if (max) {
3495 if (batchcount > max)
3496 batchcount = max;
Christoph Lametere498be72005-09-09 13:03:32 -07003497 memcpy(&(shared_array->entry[shared_array->avail]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003498 ac->entry, sizeof(void *) * batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 shared_array->avail += batchcount;
3500 goto free_done;
3501 }
3502 }
3503
Joonsoo Kim97654df2014-08-06 16:04:25 -07003504 free_block(cachep, ac->entry, batchcount, node, &list);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003505free_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506#if STATS
3507 {
3508 int i = 0;
3509 struct list_head *p;
3510
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003511 p = n->slabs_free.next;
3512 while (p != &(n->slabs_free)) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09003513 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514
Joonsoo Kim8456a642013-10-24 10:07:49 +09003515 page = list_entry(p, struct page, lru);
3516 BUG_ON(page->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517
3518 i++;
3519 p = p->next;
3520 }
3521 STATS_SET_FREEABLE(cachep, i);
3522 }
3523#endif
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003524 spin_unlock(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07003525 slabs_destroy(cachep, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 ac->avail -= batchcount;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003527 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528}
3529
3530/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08003531 * Release an obj back to its cache. If the obj has a constructed state, it must
3532 * be in this state _before_ it is released. Called with disabled ints.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 */
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003534static inline void __cache_free(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003535 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536{
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003537 struct array_cache *ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538
3539 check_irq_off();
Catalin Marinasd5cff632009-06-11 13:22:40 +01003540 kmemleak_free_recursive(objp, cachep->flags);
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003541 objp = cache_free_debugcheck(cachep, objp, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003543 kmemcheck_slab_free(cachep, objp, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003544
Siddha, Suresh B1807a1a2007-08-22 14:01:49 -07003545 /*
3546 * Skip calling cache_free_alien() when the platform is not numa.
3547 * This will avoid cache misses that happen while accessing slabp (which
3548 * is per page memory reference) to get nodeid. Instead use a global
3549 * variable to skip the call, which is mostly likely to be present in
3550 * the cache.
3551 */
Mel Gormanb6e68bc2009-06-16 15:32:16 -07003552 if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07003553 return;
Christoph Lametere498be72005-09-09 13:03:32 -07003554
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 if (likely(ac->avail < ac->limit)) {
3556 STATS_INC_FREEHIT(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 } else {
3558 STATS_INC_FREEMISS(cachep);
3559 cache_flusharray(cachep, ac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 }
Zhao Jin42c8c992011-08-27 00:26:17 +08003561
Mel Gorman072bb0a2012-07-31 16:43:58 -07003562 ac_put_obj(cachep, ac, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563}
3564
3565/**
3566 * kmem_cache_alloc - Allocate an object
3567 * @cachep: The cache to allocate from.
3568 * @flags: See kmalloc().
3569 *
3570 * Allocate an object from this cache. The flags are only relevant
3571 * if the cache has no available objects.
3572 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003573void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003575 void *ret = slab_alloc(cachep, flags, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003576
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003577 trace_kmem_cache_alloc(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003578 cachep->object_size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003579
3580 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581}
3582EXPORT_SYMBOL(kmem_cache_alloc);
3583
Li Zefan0f24f122009-12-11 15:45:30 +08003584#ifdef CONFIG_TRACING
Steven Rostedt85beb582010-11-24 16:23:34 -05003585void *
Ezequiel Garcia40521472012-09-08 17:47:56 -03003586kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003587{
Steven Rostedt85beb582010-11-24 16:23:34 -05003588 void *ret;
3589
Ezequiel Garcia48356302012-09-08 17:47:57 -03003590 ret = slab_alloc(cachep, flags, _RET_IP_);
Steven Rostedt85beb582010-11-24 16:23:34 -05003591
3592 trace_kmalloc(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003593 size, cachep->size, flags);
Steven Rostedt85beb582010-11-24 16:23:34 -05003594 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003595}
Steven Rostedt85beb582010-11-24 16:23:34 -05003596EXPORT_SYMBOL(kmem_cache_alloc_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003597#endif
3598
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599#ifdef CONFIG_NUMA
Zhouping Liud0d04b72013-05-16 11:36:23 +08003600/**
3601 * kmem_cache_alloc_node - Allocate an object on the specified node
3602 * @cachep: The cache to allocate from.
3603 * @flags: See kmalloc().
3604 * @nodeid: node number of the target node.
3605 *
3606 * Identical to kmem_cache_alloc but it will allocate memory on the given
3607 * node, which can improve the performance for cpu bound structures.
3608 *
3609 * Fallback to other node is possible if __GFP_THISNODE is not set.
3610 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003611void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
3612{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003613 void *ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003614
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003615 trace_kmem_cache_alloc_node(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003616 cachep->object_size, cachep->size,
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003617 flags, nodeid);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003618
3619 return ret;
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003620}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621EXPORT_SYMBOL(kmem_cache_alloc_node);
3622
Li Zefan0f24f122009-12-11 15:45:30 +08003623#ifdef CONFIG_TRACING
Ezequiel Garcia40521472012-09-08 17:47:56 -03003624void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
Steven Rostedt85beb582010-11-24 16:23:34 -05003625 gfp_t flags,
Ezequiel Garcia40521472012-09-08 17:47:56 -03003626 int nodeid,
3627 size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003628{
Steven Rostedt85beb582010-11-24 16:23:34 -05003629 void *ret;
3630
Ezequiel Garcia592f4142012-09-25 08:07:08 -03003631 ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003632
Steven Rostedt85beb582010-11-24 16:23:34 -05003633 trace_kmalloc_node(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003634 size, cachep->size,
Steven Rostedt85beb582010-11-24 16:23:34 -05003635 flags, nodeid);
3636 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003637}
Steven Rostedt85beb582010-11-24 16:23:34 -05003638EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003639#endif
3640
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003641static __always_inline void *
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003642__do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller)
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003643{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003644 struct kmem_cache *cachep;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003645
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003646 cachep = kmalloc_slab(size, flags);
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003647 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3648 return cachep;
Ezequiel Garcia40521472012-09-08 17:47:56 -03003649 return kmem_cache_alloc_node_trace(cachep, flags, node, size);
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003650}
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003651
Li Zefan0bb38a52009-12-11 15:45:50 +08003652#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003653void *__kmalloc_node(size_t size, gfp_t flags, int node)
3654{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003655 return __do_kmalloc_node(size, flags, node, _RET_IP_);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003656}
Christoph Hellwigdbe5e692006-09-25 23:31:36 -07003657EXPORT_SYMBOL(__kmalloc_node);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003658
3659void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003660 int node, unsigned long caller)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003661{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003662 return __do_kmalloc_node(size, flags, node, caller);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003663}
3664EXPORT_SYMBOL(__kmalloc_node_track_caller);
3665#else
3666void *__kmalloc_node(size_t size, gfp_t flags, int node)
3667{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003668 return __do_kmalloc_node(size, flags, node, 0);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003669}
3670EXPORT_SYMBOL(__kmalloc_node);
Li Zefan0bb38a52009-12-11 15:45:50 +08003671#endif /* CONFIG_DEBUG_SLAB || CONFIG_TRACING */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003672#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673
3674/**
Paul Drynoff800590f2006-06-23 02:03:48 -07003675 * __do_kmalloc - allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 * @size: how many bytes of memory are required.
Paul Drynoff800590f2006-06-23 02:03:48 -07003677 * @flags: the type of memory to allocate (see kmalloc).
Randy Dunlap911851e2006-03-22 00:08:14 -08003678 * @caller: function caller for debug tracking of the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 */
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003680static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003681 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003683 struct kmem_cache *cachep;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003684 void *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003686 cachep = kmalloc_slab(size, flags);
Linus Torvaldsa5c96d82007-07-19 13:17:15 -07003687 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3688 return cachep;
Ezequiel Garcia48356302012-09-08 17:47:57 -03003689 ret = slab_alloc(cachep, flags, caller);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003690
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003691 trace_kmalloc(caller, ret,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003692 size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003693
3694 return ret;
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003695}
3696
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003697
Li Zefan0bb38a52009-12-11 15:45:50 +08003698#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003699void *__kmalloc(size_t size, gfp_t flags)
3700{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003701 return __do_kmalloc(size, flags, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702}
3703EXPORT_SYMBOL(__kmalloc);
3704
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003705void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003706{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003707 return __do_kmalloc(size, flags, caller);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003708}
3709EXPORT_SYMBOL(__kmalloc_track_caller);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003710
3711#else
3712void *__kmalloc(size_t size, gfp_t flags)
3713{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003714 return __do_kmalloc(size, flags, 0);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003715}
3716EXPORT_SYMBOL(__kmalloc);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003717#endif
3718
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719/**
3720 * kmem_cache_free - Deallocate an object
3721 * @cachep: The cache the allocation was from.
3722 * @objp: The previously allocated object.
3723 *
3724 * Free an object which was previously allocated from this
3725 * cache.
3726 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003727void kmem_cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003728{
3729 unsigned long flags;
Glauber Costab9ce5ef2012-12-18 14:22:46 -08003730 cachep = cache_from_obj(cachep, objp);
3731 if (!cachep)
3732 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
3734 local_irq_save(flags);
Feng Tangd97d4762012-07-02 14:29:10 +08003735 debug_check_no_locks_freed(objp, cachep->object_size);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07003736 if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003737 debug_check_no_obj_freed(objp, cachep->object_size);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003738 __cache_free(cachep, objp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739 local_irq_restore(flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003740
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003741 trace_kmem_cache_free(_RET_IP_, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742}
3743EXPORT_SYMBOL(kmem_cache_free);
3744
3745/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 * kfree - free previously allocated memory
3747 * @objp: pointer returned by kmalloc.
3748 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07003749 * If @objp is NULL, no operation is performed.
3750 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751 * Don't free memory not originally allocated by kmalloc()
3752 * or you will run into trouble.
3753 */
3754void kfree(const void *objp)
3755{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003756 struct kmem_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757 unsigned long flags;
3758
Pekka Enberg2121db72009-03-25 11:05:57 +02003759 trace_kfree(_RET_IP_, objp);
3760
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003761 if (unlikely(ZERO_OR_NULL_PTR(objp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 return;
3763 local_irq_save(flags);
3764 kfree_debugcheck(objp);
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003765 c = virt_to_cache(objp);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003766 debug_check_no_locks_freed(objp, c->object_size);
3767
3768 debug_check_no_obj_freed(objp, c->object_size);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003769 __cache_free(c, (void *)objp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 local_irq_restore(flags);
3771}
3772EXPORT_SYMBOL(kfree);
3773
Christoph Lametere498be72005-09-09 13:03:32 -07003774/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003775 * This initializes kmem_cache_node or resizes various caches for all nodes.
Christoph Lametere498be72005-09-09 13:03:32 -07003776 */
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08003777static int alloc_kmem_cache_node(struct kmem_cache *cachep, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -07003778{
3779 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003780 struct kmem_cache_node *n;
Christoph Lametercafeb022006-03-25 03:06:46 -08003781 struct array_cache *new_shared;
Joonsoo Kimc8522a32014-08-06 16:04:29 -07003782 struct alien_cache **new_alien = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003783
Mel Gorman9c09a952008-01-24 05:49:54 -08003784 for_each_online_node(node) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003785
Paul Menage3395ee02006-12-06 20:32:16 -08003786 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03003787 new_alien = alloc_alien_cache(node, cachep->limit, gfp);
Paul Menage3395ee02006-12-06 20:32:16 -08003788 if (!new_alien)
3789 goto fail;
3790 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003791
Eric Dumazet63109842007-05-06 14:49:28 -07003792 new_shared = NULL;
3793 if (cachep->shared) {
3794 new_shared = alloc_arraycache(node,
Christoph Lameter0718dc22006-03-25 03:06:47 -08003795 cachep->shared*cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003796 0xbaadf00d, gfp);
Eric Dumazet63109842007-05-06 14:49:28 -07003797 if (!new_shared) {
3798 free_alien_cache(new_alien);
3799 goto fail;
3800 }
Christoph Lameter0718dc22006-03-25 03:06:47 -08003801 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003802
Christoph Lameter18bf8542014-08-06 16:04:11 -07003803 n = get_node(cachep, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003804 if (n) {
3805 struct array_cache *shared = n->shared;
Joonsoo Kim97654df2014-08-06 16:04:25 -07003806 LIST_HEAD(list);
Christoph Lametercafeb022006-03-25 03:06:46 -08003807
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003808 spin_lock_irq(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07003809
Christoph Lametercafeb022006-03-25 03:06:46 -08003810 if (shared)
Christoph Lameter0718dc22006-03-25 03:06:47 -08003811 free_block(cachep, shared->entry,
Joonsoo Kim97654df2014-08-06 16:04:25 -07003812 shared->avail, node, &list);
Christoph Lametere498be72005-09-09 13:03:32 -07003813
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003814 n->shared = new_shared;
3815 if (!n->alien) {
3816 n->alien = new_alien;
Christoph Lametere498be72005-09-09 13:03:32 -07003817 new_alien = NULL;
3818 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003819 n->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003820 cachep->batchcount + cachep->num;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003821 spin_unlock_irq(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07003822 slabs_destroy(cachep, &list);
Christoph Lametercafeb022006-03-25 03:06:46 -08003823 kfree(shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003824 free_alien_cache(new_alien);
3825 continue;
3826 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003827 n = kmalloc_node(sizeof(struct kmem_cache_node), gfp, node);
3828 if (!n) {
Christoph Lameter0718dc22006-03-25 03:06:47 -08003829 free_alien_cache(new_alien);
3830 kfree(new_shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003831 goto fail;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003832 }
Christoph Lametere498be72005-09-09 13:03:32 -07003833
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003834 kmem_cache_node_init(n);
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08003835 n->next_reap = jiffies + REAPTIMEOUT_NODE +
3836 ((unsigned long)cachep) % REAPTIMEOUT_NODE;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003837 n->shared = new_shared;
3838 n->alien = new_alien;
3839 n->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003840 cachep->batchcount + cachep->num;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003841 cachep->node[node] = n;
Christoph Lametere498be72005-09-09 13:03:32 -07003842 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003843 return 0;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003844
Andrew Mortona737b3e2006-03-22 00:08:11 -08003845fail:
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003846 if (!cachep->list.next) {
Christoph Lameter0718dc22006-03-25 03:06:47 -08003847 /* Cache is not active yet. Roll back what we did */
3848 node--;
3849 while (node >= 0) {
Christoph Lameter18bf8542014-08-06 16:04:11 -07003850 n = get_node(cachep, node);
3851 if (n) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003852 kfree(n->shared);
3853 free_alien_cache(n->alien);
3854 kfree(n);
Christoph Lameter6a673682013-01-10 19:14:19 +00003855 cachep->node[node] = NULL;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003856 }
3857 node--;
3858 }
3859 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003860 return -ENOMEM;
Christoph Lametere498be72005-09-09 13:03:32 -07003861}
3862
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863struct ccupdate_struct {
Pekka Enberg343e0d72006-02-01 03:05:50 -08003864 struct kmem_cache *cachep;
Eric Dumazetacfe7d72011-07-25 08:55:42 +02003865 struct array_cache *new[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866};
3867
3868static void do_ccupdate_local(void *info)
3869{
Andrew Mortona737b3e2006-03-22 00:08:11 -08003870 struct ccupdate_struct *new = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 struct array_cache *old;
3872
3873 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003874 old = cpu_cache_get(new->cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003875
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
3877 new->new[smp_processor_id()] = old;
3878}
3879
Christoph Lameter18004c52012-07-06 15:25:12 -05003880/* Always called with the slab_mutex held */
Glauber Costa943a4512012-12-18 14:23:03 -08003881static int __do_tune_cpucache(struct kmem_cache *cachep, int limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003882 int batchcount, int shared, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883{
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003884 struct ccupdate_struct *new;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003885 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
Eric Dumazetacfe7d72011-07-25 08:55:42 +02003887 new = kzalloc(sizeof(*new) + nr_cpu_ids * sizeof(struct array_cache *),
3888 gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003889 if (!new)
3890 return -ENOMEM;
3891
Christoph Lametere498be72005-09-09 13:03:32 -07003892 for_each_online_cpu(i) {
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003893 new->new[i] = alloc_arraycache(cpu_to_mem(i), limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003894 batchcount, gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003895 if (!new->new[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003896 for (i--; i >= 0; i--)
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003897 kfree(new->new[i]);
3898 kfree(new);
Christoph Lametere498be72005-09-09 13:03:32 -07003899 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 }
3901 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003902 new->cachep = cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903
Jens Axboe15c8b6c2008-05-09 09:39:44 +02003904 on_each_cpu(do_ccupdate_local, (void *)new, 1);
Christoph Lametere498be72005-09-09 13:03:32 -07003905
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 check_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907 cachep->batchcount = batchcount;
3908 cachep->limit = limit;
Christoph Lametere498be72005-09-09 13:03:32 -07003909 cachep->shared = shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910
Christoph Lametere498be72005-09-09 13:03:32 -07003911 for_each_online_cpu(i) {
Joonsoo Kim97654df2014-08-06 16:04:25 -07003912 LIST_HEAD(list);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003913 struct array_cache *ccold = new->new[i];
Christoph Lameter18bf8542014-08-06 16:04:11 -07003914 int node;
3915 struct kmem_cache_node *n;
3916
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 if (!ccold)
3918 continue;
Christoph Lameter18bf8542014-08-06 16:04:11 -07003919
3920 node = cpu_to_mem(i);
3921 n = get_node(cachep, node);
3922 spin_lock_irq(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07003923 free_block(cachep, ccold->entry, ccold->avail, node, &list);
Christoph Lameter18bf8542014-08-06 16:04:11 -07003924 spin_unlock_irq(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07003925 slabs_destroy(cachep, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 kfree(ccold);
3927 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003928 kfree(new);
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08003929 return alloc_kmem_cache_node(cachep, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930}
3931
Glauber Costa943a4512012-12-18 14:23:03 -08003932static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
3933 int batchcount, int shared, gfp_t gfp)
3934{
3935 int ret;
3936 struct kmem_cache *c = NULL;
3937 int i = 0;
3938
3939 ret = __do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
3940
3941 if (slab_state < FULL)
3942 return ret;
3943
3944 if ((ret < 0) || !is_root_cache(cachep))
3945 return ret;
3946
Glauber Costaebe945c2012-12-18 14:23:10 -08003947 VM_BUG_ON(!mutex_is_locked(&slab_mutex));
Glauber Costa943a4512012-12-18 14:23:03 -08003948 for_each_memcg_cache_index(i) {
Qiang Huang2ade4de2013-11-12 15:08:23 -08003949 c = cache_from_memcg_idx(cachep, i);
Glauber Costa943a4512012-12-18 14:23:03 -08003950 if (c)
3951 /* return value determined by the parent cache only */
3952 __do_tune_cpucache(c, limit, batchcount, shared, gfp);
3953 }
3954
3955 return ret;
3956}
3957
Christoph Lameter18004c52012-07-06 15:25:12 -05003958/* Called with slab_mutex held always */
Pekka Enberg83b519e2009-06-10 19:40:04 +03003959static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960{
3961 int err;
Glauber Costa943a4512012-12-18 14:23:03 -08003962 int limit = 0;
3963 int shared = 0;
3964 int batchcount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965
Glauber Costa943a4512012-12-18 14:23:03 -08003966 if (!is_root_cache(cachep)) {
3967 struct kmem_cache *root = memcg_root_cache(cachep);
3968 limit = root->limit;
3969 shared = root->shared;
3970 batchcount = root->batchcount;
3971 }
3972
3973 if (limit && shared && batchcount)
3974 goto skip_setup;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003975 /*
3976 * The head array serves three purposes:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 * - create a LIFO ordering, i.e. return objects that are cache-warm
3978 * - reduce the number of spinlock operations.
Andrew Mortona737b3e2006-03-22 00:08:11 -08003979 * - reduce the number of linked list operations on the slab and
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980 * bufctl chains: array operations are cheaper.
3981 * The numbers are guessed, we should auto-tune as described by
3982 * Bonwick.
3983 */
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003984 if (cachep->size > 131072)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985 limit = 1;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003986 else if (cachep->size > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 limit = 8;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003988 else if (cachep->size > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 limit = 24;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003990 else if (cachep->size > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 limit = 54;
3992 else
3993 limit = 120;
3994
Andrew Mortona737b3e2006-03-22 00:08:11 -08003995 /*
3996 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 * allocation behaviour: Most allocs on one cpu, most free operations
3998 * on another cpu. For these cases, an efficient object passing between
3999 * cpus is necessary. This is provided by a shared array. The array
4000 * replaces Bonwick's magazine layer.
4001 * On uniprocessor, it's functionally equivalent (but less efficient)
4002 * to a larger limit. Thus disabled by default.
4003 */
4004 shared = 0;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004005 if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 shared = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007
4008#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08004009 /*
4010 * With debugging enabled, large batchcount lead to excessively long
4011 * periods with disabled local interrupts. Limit the batchcount
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 */
4013 if (limit > 32)
4014 limit = 32;
4015#endif
Glauber Costa943a4512012-12-18 14:23:03 -08004016 batchcount = (limit + 1) / 2;
4017skip_setup:
4018 err = do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004019 if (err)
4020 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004021 cachep->name, -err);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07004022 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023}
4024
Christoph Lameter1b552532006-03-22 00:09:07 -08004025/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004026 * Drain an array if it contains any elements taking the node lock only if
4027 * necessary. Note that the node listlock also protects the array_cache
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004028 * if drain_array() is used on the shared array.
Christoph Lameter1b552532006-03-22 00:09:07 -08004029 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004030static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
Christoph Lameter1b552532006-03-22 00:09:07 -08004031 struct array_cache *ac, int force, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032{
Joonsoo Kim97654df2014-08-06 16:04:25 -07004033 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 int tofree;
4035
Christoph Lameter1b552532006-03-22 00:09:07 -08004036 if (!ac || !ac->avail)
4037 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 if (ac->touched && !force) {
4039 ac->touched = 0;
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004040 } else {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004041 spin_lock_irq(&n->list_lock);
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004042 if (ac->avail) {
4043 tofree = force ? ac->avail : (ac->limit + 4) / 5;
4044 if (tofree > ac->avail)
4045 tofree = (ac->avail + 1) / 2;
Joonsoo Kim97654df2014-08-06 16:04:25 -07004046 free_block(cachep, ac->entry, tofree, node, &list);
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004047 ac->avail -= tofree;
4048 memmove(ac->entry, &(ac->entry[tofree]),
4049 sizeof(void *) * ac->avail);
4050 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004051 spin_unlock_irq(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07004052 slabs_destroy(cachep, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 }
4054}
4055
4056/**
4057 * cache_reap - Reclaim memory from caches.
Randy Dunlap05fb6bf2007-02-28 20:12:13 -08004058 * @w: work descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059 *
4060 * Called from workqueue/eventd every few seconds.
4061 * Purpose:
4062 * - clear the per-cpu caches for this CPU.
4063 * - return freeable pages to the main free memory pool.
4064 *
Andrew Mortona737b3e2006-03-22 00:08:11 -08004065 * If we cannot acquire the cache chain mutex then just give up - we'll try
4066 * again on the next iteration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004068static void cache_reap(struct work_struct *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069{
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004070 struct kmem_cache *searchp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004071 struct kmem_cache_node *n;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07004072 int node = numa_mem_id();
Jean Delvarebf6aede2009-04-02 16:56:54 -07004073 struct delayed_work *work = to_delayed_work(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
Christoph Lameter18004c52012-07-06 15:25:12 -05004075 if (!mutex_trylock(&slab_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 /* Give up. Setup the next iteration. */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004077 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
Christoph Lameter18004c52012-07-06 15:25:12 -05004079 list_for_each_entry(searchp, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 check_irq_on();
4081
Christoph Lameter35386e32006-03-22 00:09:05 -08004082 /*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004083 * We only take the node lock if absolutely necessary and we
Christoph Lameter35386e32006-03-22 00:09:05 -08004084 * have established with reasonable certainty that
4085 * we can do some work if the lock was obtained.
4086 */
Christoph Lameter18bf8542014-08-06 16:04:11 -07004087 n = get_node(searchp, node);
Christoph Lameter35386e32006-03-22 00:09:05 -08004088
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004089 reap_alien(searchp, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004091 drain_array(searchp, n, cpu_cache_get(searchp), 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092
Christoph Lameter35386e32006-03-22 00:09:05 -08004093 /*
4094 * These are racy checks but it does not matter
4095 * if we skip one check or scan twice.
4096 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004097 if (time_after(n->next_reap, jiffies))
Christoph Lameter35386e32006-03-22 00:09:05 -08004098 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08004100 n->next_reap = jiffies + REAPTIMEOUT_NODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004102 drain_array(searchp, n, n->shared, 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004104 if (n->free_touched)
4105 n->free_touched = 0;
Christoph Lametered11d9e2006-06-30 01:55:45 -07004106 else {
4107 int freed;
4108
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004109 freed = drain_freelist(searchp, n, (n->free_limit +
Christoph Lametered11d9e2006-06-30 01:55:45 -07004110 5 * searchp->num - 1) / (5 * searchp->num));
4111 STATS_ADD_REAPED(searchp, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 }
Christoph Lameter35386e32006-03-22 00:09:05 -08004113next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 cond_resched();
4115 }
4116 check_irq_on();
Christoph Lameter18004c52012-07-06 15:25:12 -05004117 mutex_unlock(&slab_mutex);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004118 next_reap_node();
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004119out:
Andrew Mortona737b3e2006-03-22 00:08:11 -08004120 /* Set up the next iteration */
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08004121 schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_AC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122}
4123
Linus Torvalds158a9622008-01-02 13:04:48 -08004124#ifdef CONFIG_SLABINFO
Glauber Costa0d7561c2012-10-19 18:20:27 +04004125void get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126{
Joonsoo Kim8456a642013-10-24 10:07:49 +09004127 struct page *page;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004128 unsigned long active_objs;
4129 unsigned long num_objs;
4130 unsigned long active_slabs = 0;
4131 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004132 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 char *error = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07004134 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004135 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 active_objs = 0;
4138 num_slabs = 0;
Christoph Lameter18bf8542014-08-06 16:04:11 -07004139 for_each_kmem_cache_node(cachep, node, n) {
Christoph Lametere498be72005-09-09 13:03:32 -07004140
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004141 check_irq_on();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004142 spin_lock_irq(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07004143
Joonsoo Kim8456a642013-10-24 10:07:49 +09004144 list_for_each_entry(page, &n->slabs_full, lru) {
4145 if (page->active != cachep->num && !error)
Christoph Lametere498be72005-09-09 13:03:32 -07004146 error = "slabs_full accounting error";
4147 active_objs += cachep->num;
4148 active_slabs++;
4149 }
Joonsoo Kim8456a642013-10-24 10:07:49 +09004150 list_for_each_entry(page, &n->slabs_partial, lru) {
4151 if (page->active == cachep->num && !error)
Joonsoo Kim106a74e2013-10-24 10:07:48 +09004152 error = "slabs_partial accounting error";
Joonsoo Kim8456a642013-10-24 10:07:49 +09004153 if (!page->active && !error)
Joonsoo Kim106a74e2013-10-24 10:07:48 +09004154 error = "slabs_partial accounting error";
Joonsoo Kim8456a642013-10-24 10:07:49 +09004155 active_objs += page->active;
Christoph Lametere498be72005-09-09 13:03:32 -07004156 active_slabs++;
4157 }
Joonsoo Kim8456a642013-10-24 10:07:49 +09004158 list_for_each_entry(page, &n->slabs_free, lru) {
4159 if (page->active && !error)
Joonsoo Kim106a74e2013-10-24 10:07:48 +09004160 error = "slabs_free accounting error";
Christoph Lametere498be72005-09-09 13:03:32 -07004161 num_slabs++;
4162 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004163 free_objects += n->free_objects;
4164 if (n->shared)
4165 shared_avail += n->shared->avail;
Christoph Lametere498be72005-09-09 13:03:32 -07004166
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004167 spin_unlock_irq(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004169 num_slabs += active_slabs;
4170 num_objs = num_slabs * cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07004171 if (num_objs - active_objs != free_objects && !error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 error = "free_objects accounting error";
4173
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004174 name = cachep->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 if (error)
4176 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
4177
Glauber Costa0d7561c2012-10-19 18:20:27 +04004178 sinfo->active_objs = active_objs;
4179 sinfo->num_objs = num_objs;
4180 sinfo->active_slabs = active_slabs;
4181 sinfo->num_slabs = num_slabs;
4182 sinfo->shared_avail = shared_avail;
4183 sinfo->limit = cachep->limit;
4184 sinfo->batchcount = cachep->batchcount;
4185 sinfo->shared = cachep->shared;
4186 sinfo->objects_per_slab = cachep->num;
4187 sinfo->cache_order = cachep->gfporder;
4188}
4189
4190void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *cachep)
4191{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192#if STATS
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004193 { /* node stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 unsigned long high = cachep->high_mark;
4195 unsigned long allocs = cachep->num_allocations;
4196 unsigned long grown = cachep->grown;
4197 unsigned long reaped = cachep->reaped;
4198 unsigned long errors = cachep->errors;
4199 unsigned long max_freeable = cachep->max_freeable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 unsigned long node_allocs = cachep->node_allocs;
Christoph Lametere498be72005-09-09 13:03:32 -07004201 unsigned long node_frees = cachep->node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004202 unsigned long overflows = cachep->node_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203
Joe Perchese92dd4f2010-03-26 19:27:58 -07004204 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu "
4205 "%4lu %4lu %4lu %4lu %4lu",
4206 allocs, high, grown,
4207 reaped, errors, max_freeable, node_allocs,
4208 node_frees, overflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 }
4210 /* cpu stats */
4211 {
4212 unsigned long allochit = atomic_read(&cachep->allochit);
4213 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
4214 unsigned long freehit = atomic_read(&cachep->freehit);
4215 unsigned long freemiss = atomic_read(&cachep->freemiss);
4216
4217 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004218 allochit, allocmiss, freehit, freemiss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 }
4220#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221}
4222
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223#define MAX_SLABINFO_WRITE 128
4224/**
4225 * slabinfo_write - Tuning for the slab allocator
4226 * @file: unused
4227 * @buffer: user buffer
4228 * @count: data length
4229 * @ppos: unused
4230 */
Glauber Costab7454ad2012-10-19 18:20:25 +04004231ssize_t slabinfo_write(struct file *file, const char __user *buffer,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004232 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004234 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 int limit, batchcount, shared, res;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004236 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004237
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 if (count > MAX_SLABINFO_WRITE)
4239 return -EINVAL;
4240 if (copy_from_user(&kbuf, buffer, count))
4241 return -EFAULT;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004242 kbuf[MAX_SLABINFO_WRITE] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
4244 tmp = strchr(kbuf, ' ');
4245 if (!tmp)
4246 return -EINVAL;
4247 *tmp = '\0';
4248 tmp++;
4249 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
4250 return -EINVAL;
4251
4252 /* Find the cache in the chain of caches. */
Christoph Lameter18004c52012-07-06 15:25:12 -05004253 mutex_lock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 res = -EINVAL;
Christoph Lameter18004c52012-07-06 15:25:12 -05004255 list_for_each_entry(cachep, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 if (!strcmp(cachep->name, kbuf)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08004257 if (limit < 1 || batchcount < 1 ||
4258 batchcount > limit || shared < 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07004259 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07004261 res = do_tune_cpucache(cachep, limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03004262 batchcount, shared,
4263 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264 }
4265 break;
4266 }
4267 }
Christoph Lameter18004c52012-07-06 15:25:12 -05004268 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 if (res >= 0)
4270 res = count;
4271 return res;
4272}
Al Viro871751e2006-03-25 03:06:39 -08004273
4274#ifdef CONFIG_DEBUG_SLAB_LEAK
4275
4276static void *leaks_start(struct seq_file *m, loff_t *pos)
4277{
Christoph Lameter18004c52012-07-06 15:25:12 -05004278 mutex_lock(&slab_mutex);
4279 return seq_list_start(&slab_caches, *pos);
Al Viro871751e2006-03-25 03:06:39 -08004280}
4281
4282static inline int add_caller(unsigned long *n, unsigned long v)
4283{
4284 unsigned long *p;
4285 int l;
4286 if (!v)
4287 return 1;
4288 l = n[1];
4289 p = n + 2;
4290 while (l) {
4291 int i = l/2;
4292 unsigned long *q = p + 2 * i;
4293 if (*q == v) {
4294 q[1]++;
4295 return 1;
4296 }
4297 if (*q > v) {
4298 l = i;
4299 } else {
4300 p = q + 2;
4301 l -= i + 1;
4302 }
4303 }
4304 if (++n[1] == n[0])
4305 return 0;
4306 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4307 p[0] = v;
4308 p[1] = 1;
4309 return 1;
4310}
4311
Joonsoo Kim8456a642013-10-24 10:07:49 +09004312static void handle_slab(unsigned long *n, struct kmem_cache *c,
4313 struct page *page)
Al Viro871751e2006-03-25 03:06:39 -08004314{
4315 void *p;
Joonsoo Kim03787302014-06-23 13:22:06 -07004316 int i;
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09004317
Al Viro871751e2006-03-25 03:06:39 -08004318 if (n[0] == n[1])
4319 return;
Joonsoo Kim8456a642013-10-24 10:07:49 +09004320 for (i = 0, p = page->s_mem; i < c->num; i++, p += c->size) {
Joonsoo Kim03787302014-06-23 13:22:06 -07004321 if (get_obj_status(page, i) != OBJECT_ACTIVE)
Al Viro871751e2006-03-25 03:06:39 -08004322 continue;
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09004323
Al Viro871751e2006-03-25 03:06:39 -08004324 if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
4325 return;
4326 }
4327}
4328
4329static void show_symbol(struct seq_file *m, unsigned long address)
4330{
4331#ifdef CONFIG_KALLSYMS
Al Viro871751e2006-03-25 03:06:39 -08004332 unsigned long offset, size;
Tejun Heo9281ace2007-07-17 04:03:51 -07004333 char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
Al Viro871751e2006-03-25 03:06:39 -08004334
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004335 if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
Al Viro871751e2006-03-25 03:06:39 -08004336 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004337 if (modname[0])
Al Viro871751e2006-03-25 03:06:39 -08004338 seq_printf(m, " [%s]", modname);
4339 return;
4340 }
4341#endif
4342 seq_printf(m, "%p", (void *)address);
4343}
4344
4345static int leaks_show(struct seq_file *m, void *p)
4346{
Thierry Reding0672aa72012-06-22 19:42:49 +02004347 struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);
Joonsoo Kim8456a642013-10-24 10:07:49 +09004348 struct page *page;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004349 struct kmem_cache_node *n;
Al Viro871751e2006-03-25 03:06:39 -08004350 const char *name;
Christoph Lameterdb845062013-02-05 18:45:23 +00004351 unsigned long *x = m->private;
Al Viro871751e2006-03-25 03:06:39 -08004352 int node;
4353 int i;
4354
4355 if (!(cachep->flags & SLAB_STORE_USER))
4356 return 0;
4357 if (!(cachep->flags & SLAB_RED_ZONE))
4358 return 0;
4359
4360 /* OK, we can do it */
4361
Christoph Lameterdb845062013-02-05 18:45:23 +00004362 x[1] = 0;
Al Viro871751e2006-03-25 03:06:39 -08004363
Christoph Lameter18bf8542014-08-06 16:04:11 -07004364 for_each_kmem_cache_node(cachep, node, n) {
Al Viro871751e2006-03-25 03:06:39 -08004365
4366 check_irq_on();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004367 spin_lock_irq(&n->list_lock);
Al Viro871751e2006-03-25 03:06:39 -08004368
Joonsoo Kim8456a642013-10-24 10:07:49 +09004369 list_for_each_entry(page, &n->slabs_full, lru)
4370 handle_slab(x, cachep, page);
4371 list_for_each_entry(page, &n->slabs_partial, lru)
4372 handle_slab(x, cachep, page);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004373 spin_unlock_irq(&n->list_lock);
Al Viro871751e2006-03-25 03:06:39 -08004374 }
4375 name = cachep->name;
Christoph Lameterdb845062013-02-05 18:45:23 +00004376 if (x[0] == x[1]) {
Al Viro871751e2006-03-25 03:06:39 -08004377 /* Increase the buffer size */
Christoph Lameter18004c52012-07-06 15:25:12 -05004378 mutex_unlock(&slab_mutex);
Christoph Lameterdb845062013-02-05 18:45:23 +00004379 m->private = kzalloc(x[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
Al Viro871751e2006-03-25 03:06:39 -08004380 if (!m->private) {
4381 /* Too bad, we are really out */
Christoph Lameterdb845062013-02-05 18:45:23 +00004382 m->private = x;
Christoph Lameter18004c52012-07-06 15:25:12 -05004383 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004384 return -ENOMEM;
4385 }
Christoph Lameterdb845062013-02-05 18:45:23 +00004386 *(unsigned long *)m->private = x[0] * 2;
4387 kfree(x);
Christoph Lameter18004c52012-07-06 15:25:12 -05004388 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004389 /* Now make sure this entry will be retried */
4390 m->count = m->size;
4391 return 0;
4392 }
Christoph Lameterdb845062013-02-05 18:45:23 +00004393 for (i = 0; i < x[1]; i++) {
4394 seq_printf(m, "%s: %lu ", name, x[2*i+3]);
4395 show_symbol(m, x[2*i+2]);
Al Viro871751e2006-03-25 03:06:39 -08004396 seq_putc(m, '\n');
4397 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004398
Al Viro871751e2006-03-25 03:06:39 -08004399 return 0;
4400}
4401
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004402static const struct seq_operations slabstats_op = {
Al Viro871751e2006-03-25 03:06:39 -08004403 .start = leaks_start,
Wanpeng Li276a2432013-07-08 08:08:28 +08004404 .next = slab_next,
4405 .stop = slab_stop,
Al Viro871751e2006-03-25 03:06:39 -08004406 .show = leaks_show,
4407};
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004408
4409static int slabstats_open(struct inode *inode, struct file *file)
4410{
4411 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
4412 int ret = -ENOMEM;
4413 if (n) {
4414 ret = seq_open(file, &slabstats_op);
4415 if (!ret) {
4416 struct seq_file *m = file->private_data;
4417 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
4418 m->private = n;
4419 n = NULL;
4420 }
4421 kfree(n);
4422 }
4423 return ret;
4424}
4425
4426static const struct file_operations proc_slabstats_operations = {
4427 .open = slabstats_open,
4428 .read = seq_read,
4429 .llseek = seq_lseek,
4430 .release = seq_release_private,
4431};
Al Viro871751e2006-03-25 03:06:39 -08004432#endif
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004433
4434static int __init slab_proc_init(void)
4435{
4436#ifdef CONFIG_DEBUG_SLAB_LEAK
4437 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
4438#endif
4439 return 0;
4440}
4441module_init(slab_proc_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442#endif
4443
Manfred Spraul00e145b2005-09-03 15:55:07 -07004444/**
4445 * ksize - get the actual amount of memory allocated for a given object
4446 * @objp: Pointer to the object
4447 *
4448 * kmalloc may internally round up allocations and return more memory
4449 * than requested. ksize() can be used to determine the actual amount of
4450 * memory allocated. The caller may use this additional memory, even though
4451 * a smaller amount of memory was initially specified with the kmalloc call.
4452 * The caller must guarantee that objp points to a valid object previously
4453 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4454 * must not be freed during the duration of the call.
4455 */
Pekka Enbergfd76bab2007-05-06 14:48:40 -07004456size_t ksize(const void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457{
Christoph Lameteref8b4522007-10-16 01:24:46 -07004458 BUG_ON(!objp);
4459 if (unlikely(objp == ZERO_SIZE_PTR))
Manfred Spraul00e145b2005-09-03 15:55:07 -07004460 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461
Christoph Lameter8c138bc2012-06-13 10:24:58 -05004462 return virt_to_cache(objp)->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463}
Kirill A. Shutemovb1aabec2009-02-10 15:21:44 +02004464EXPORT_SYMBOL(ksize);