blob: 05fe37eb4a5718d009ee91a822811167314d094d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/slab.c
3 * Written by Mark Hemment, 1996/97.
4 * (markhe@nextd.demon.co.uk)
5 *
6 * kmem_cache_destroy() + some cleanup - 1999 Andrea Arcangeli
7 *
8 * Major cleanup, different bufctl logic, per-cpu arrays
9 * (c) 2000 Manfred Spraul
10 *
11 * Cleanup, make the head arrays unconditional, preparation for NUMA
12 * (c) 2002 Manfred Spraul
13 *
14 * An implementation of the Slab Allocator as described in outline in;
15 * UNIX Internals: The New Frontiers by Uresh Vahalia
16 * Pub: Prentice Hall ISBN 0-13-101908-2
17 * or with a little more detail in;
18 * The Slab Allocator: An Object-Caching Kernel Memory Allocator
19 * Jeff Bonwick (Sun Microsystems).
20 * Presented at: USENIX Summer 1994 Technical Conference
21 *
22 * The memory is organized in caches, one cache for each object type.
23 * (e.g. inode_cache, dentry_cache, buffer_head, vm_area_struct)
24 * Each cache consists out of many slabs (they are small (usually one
25 * page long) and always contiguous), and each slab contains multiple
26 * initialized objects.
27 *
28 * This means, that your constructor is used only for newly allocated
Simon Arlott183ff222007-10-20 01:27:18 +020029 * slabs and you must pass objects with the same initializations to
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * kmem_cache_free.
31 *
32 * Each cache can only support one memory type (GFP_DMA, GFP_HIGHMEM,
33 * normal). If you need a special memory type, then must create a new
34 * cache for that memory type.
35 *
36 * In order to reduce fragmentation, the slabs are sorted in 3 groups:
37 * full slabs with 0 free objects
38 * partial slabs
39 * empty slabs with no allocated objects
40 *
41 * If partial slabs exist, then new allocations come from these slabs,
42 * otherwise from empty slabs or new slabs are allocated.
43 *
44 * kmem_cache_destroy() CAN CRASH if you try to allocate from the cache
45 * during kmem_cache_destroy(). The caller must prevent concurrent allocs.
46 *
47 * Each cache has a short per-cpu head array, most allocs
48 * and frees go into that array, and if that array overflows, then 1/2
49 * of the entries in the array are given back into the global cache.
50 * The head array is strictly LIFO and should improve the cache hit rates.
51 * On SMP, it additionally reduces the spinlock operations.
52 *
Andrew Mortona737b3e2006-03-22 00:08:11 -080053 * The c_cpuarray may not be read with enabled local interrupts -
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * it's changed with a smp_call_function().
55 *
56 * SMP synchronization:
57 * constructors and destructors are called without any locking.
Pekka Enberg343e0d72006-02-01 03:05:50 -080058 * Several members in struct kmem_cache and struct slab never change, they
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * are accessed without any locking.
60 * The per-cpu arrays are never accessed from the wrong cpu, no locking,
61 * and local interrupts are disabled so slab code is preempt-safe.
62 * The non-constant members are protected with a per-cache irq spinlock.
63 *
64 * Many thanks to Mark Hemment, who wrote another per-cpu slab patch
65 * in 2000 - many ideas in the current implementation are derived from
66 * his patch.
67 *
68 * Further notes from the original documentation:
69 *
70 * 11 April '97. Started multi-threading - markhe
Christoph Lameter18004c52012-07-06 15:25:12 -050071 * The global cache-chain is protected by the mutex 'slab_mutex'.
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * The sem is only needed when accessing/extending the cache-chain, which
73 * can never happen inside an interrupt (kmem_cache_create(),
74 * kmem_cache_shrink() and kmem_cache_reap()).
75 *
76 * At present, each engine can be growing a cache. This should be blocked.
77 *
Christoph Lametere498be72005-09-09 13:03:32 -070078 * 15 March 2005. NUMA slab allocator.
79 * Shai Fultheim <shai@scalex86.org>.
80 * Shobhit Dayal <shobhit@calsoftinc.com>
81 * Alok N Kataria <alokk@calsoftinc.com>
82 * Christoph Lameter <christoph@lameter.com>
83 *
84 * Modified the slab allocator to be node aware on NUMA systems.
85 * Each node has its own list of partial, free and full slabs.
86 * All object allocations for a node occur from node specific slab lists.
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 */
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <linux/slab.h>
90#include <linux/mm.h>
Randy Dunlapc9cf5522006-06-27 02:53:52 -070091#include <linux/poison.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#include <linux/swap.h>
93#include <linux/cache.h>
94#include <linux/interrupt.h>
95#include <linux/init.h>
96#include <linux/compiler.h>
Paul Jackson101a5002006-03-24 03:16:07 -080097#include <linux/cpuset.h>
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +040098#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#include <linux/seq_file.h>
100#include <linux/notifier.h>
101#include <linux/kallsyms.h>
102#include <linux/cpu.h>
103#include <linux/sysctl.h>
104#include <linux/module.h>
105#include <linux/rcupdate.h>
Paulo Marques543537b2005-06-23 00:09:02 -0700106#include <linux/string.h>
Andrew Morton138ae662006-12-06 20:36:41 -0800107#include <linux/uaccess.h>
Christoph Lametere498be72005-09-09 13:03:32 -0700108#include <linux/nodemask.h>
Catalin Marinasd5cff632009-06-11 13:22:40 +0100109#include <linux/kmemleak.h>
Christoph Lameterdc85da12006-01-18 17:42:36 -0800110#include <linux/mempolicy.h>
Ingo Molnarfc0abb12006-01-18 17:42:33 -0800111#include <linux/mutex.h>
Akinobu Mita8a8b6502006-12-08 02:39:44 -0800112#include <linux/fault-inject.h>
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700113#include <linux/rtmutex.h>
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800114#include <linux/reciprocal_div.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700115#include <linux/debugobjects.h>
Pekka Enbergc175eea2008-05-09 20:35:53 +0200116#include <linux/kmemcheck.h>
David Rientjes8f9f8d92010-03-27 19:40:47 -0700117#include <linux/memory.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -0700118#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Mel Gorman381760e2012-07-31 16:44:30 -0700120#include <net/sock.h>
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#include <asm/cacheflush.h>
123#include <asm/tlbflush.h>
124#include <asm/page.h>
125
Steven Rostedt4dee6b62012-01-09 17:15:42 -0500126#include <trace/events/kmem.h>
127
Mel Gorman072bb0a2012-07-31 16:43:58 -0700128#include "internal.h"
129
Glauber Costab9ce5ef2012-12-18 14:22:46 -0800130#include "slab.h"
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/*
Christoph Lameter50953fe2007-05-06 14:50:16 -0700133 * DEBUG - 1 for kmem_cache_create() to honour; SLAB_RED_ZONE & SLAB_POISON.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * 0 for faster, smaller code (especially in the critical paths).
135 *
136 * STATS - 1 to collect stats for /proc/slabinfo.
137 * 0 for faster, smaller code (especially in the critical paths).
138 *
139 * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
140 */
141
142#ifdef CONFIG_DEBUG_SLAB
143#define DEBUG 1
144#define STATS 1
145#define FORCED_DEBUG 1
146#else
147#define DEBUG 0
148#define STATS 0
149#define FORCED_DEBUG 0
150#endif
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Shouldn't this be in a header file somewhere? */
153#define BYTES_PER_WORD sizeof(void *)
David Woodhouse87a927c2007-07-04 21:26:44 -0400154#define REDZONE_ALIGN max(BYTES_PER_WORD, __alignof__(unsigned long long))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#ifndef ARCH_KMALLOC_FLAGS
157#define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
158#endif
159
Mel Gorman072bb0a2012-07-31 16:43:58 -0700160/*
161 * true if a page was allocated from pfmemalloc reserves for network-based
162 * swap
163 */
164static bool pfmemalloc_active __read_mostly;
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166/*
167 * kmem_bufctl_t:
168 *
169 * Bufctl's are used for linking objs within a slab
170 * linked offsets.
171 *
172 * This implementation relies on "struct page" for locating the cache &
173 * slab an object belongs to.
174 * This allows the bufctl structure to be small (one int), but limits
175 * the number of objects a slab (not a cache) can contain when off-slab
176 * bufctls are used. The limit is the size of the largest general cache
177 * that does not use off-slab slabs.
178 * For 32bit archs with 4 kB pages, is this 56.
179 * This is not serious, as it is only for large objects, when it is unwise
180 * to have too many per slab.
181 * Note: This limit can be raised by introducing a general cache whose size
182 * is less than 512 (PAGE_SIZE<<3), but greater than 256.
183 */
184
Kyle Moffettfa5b08d2005-09-03 15:55:03 -0700185typedef unsigned int kmem_bufctl_t;
Al Viro871751e2006-03-25 03:06:39 -0800186#define SLAB_LIMIT (((kmem_bufctl_t)(~0U))-3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/*
Lai Jiangshan5bfe53a2011-03-10 15:22:24 +0800189 * struct slab
190 *
191 * Manages the objs in a slab. Placed either at the beginning of mem allocated
192 * for a slab, or allocated from an general cache.
193 * Slabs are chained into three list: fully used, partial, fully free slabs.
194 */
195struct slab {
Joonsoo Kim68126702013-10-24 10:07:42 +0900196 struct {
197 struct list_head list;
198 void *s_mem; /* including colour offset */
199 unsigned int inuse; /* num of objs active in slab */
200 kmem_bufctl_t free;
Lai Jiangshan5bfe53a2011-03-10 15:22:24 +0800201 };
202};
203
204/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 * struct array_cache
206 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 * Purpose:
208 * - LIFO ordering, to hand out cache-warm objects from _alloc
209 * - reduce the number of linked list operations
210 * - reduce spinlock operations
211 *
212 * The limit is stored in the per-cpu structure to reduce the data cache
213 * footprint.
214 *
215 */
216struct array_cache {
217 unsigned int avail;
218 unsigned int limit;
219 unsigned int batchcount;
220 unsigned int touched;
Christoph Lametere498be72005-09-09 13:03:32 -0700221 spinlock_t lock;
Robert P. J. Daybda5b652007-10-16 23:30:05 -0700222 void *entry[]; /*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800223 * Must have this definition in here for the proper
224 * alignment of array_cache. Also simplifies accessing
225 * the entries.
Mel Gorman072bb0a2012-07-31 16:43:58 -0700226 *
227 * Entries should not be directly dereferenced as
228 * entries belonging to slabs marked pfmemalloc will
229 * have the lower bits set SLAB_OBJ_PFMEMALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -0800230 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231};
232
Mel Gorman072bb0a2012-07-31 16:43:58 -0700233#define SLAB_OBJ_PFMEMALLOC 1
234static inline bool is_obj_pfmemalloc(void *objp)
235{
236 return (unsigned long)objp & SLAB_OBJ_PFMEMALLOC;
237}
238
239static inline void set_obj_pfmemalloc(void **objp)
240{
241 *objp = (void *)((unsigned long)*objp | SLAB_OBJ_PFMEMALLOC);
242 return;
243}
244
245static inline void clear_obj_pfmemalloc(void **objp)
246{
247 *objp = (void *)((unsigned long)*objp & ~SLAB_OBJ_PFMEMALLOC);
248}
249
Andrew Mortona737b3e2006-03-22 00:08:11 -0800250/*
251 * bootstrap: The caches do not work without cpuarrays anymore, but the
252 * cpuarrays are allocated from the generic caches...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 */
254#define BOOT_CPUCACHE_ENTRIES 1
255struct arraycache_init {
256 struct array_cache cache;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800257 void *entries[BOOT_CPUCACHE_ENTRIES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258};
259
260/*
Christoph Lametere498be72005-09-09 13:03:32 -0700261 * Need this for bootstrapping a per node allocator.
262 */
Pekka Enberg556a1692008-01-25 08:20:51 +0200263#define NUM_INIT_LISTS (3 * MAX_NUMNODES)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000264static struct kmem_cache_node __initdata init_kmem_cache_node[NUM_INIT_LISTS];
Christoph Lametere498be72005-09-09 13:03:32 -0700265#define CACHE_CACHE 0
Pekka Enberg556a1692008-01-25 08:20:51 +0200266#define SIZE_AC MAX_NUMNODES
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000267#define SIZE_NODE (2 * MAX_NUMNODES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268
Christoph Lametered11d9e2006-06-30 01:55:45 -0700269static int drain_freelist(struct kmem_cache *cache,
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000270 struct kmem_cache_node *n, int tofree);
Christoph Lametered11d9e2006-06-30 01:55:45 -0700271static void free_block(struct kmem_cache *cachep, void **objpp, int len,
272 int node);
Pekka Enberg83b519e2009-06-10 19:40:04 +0300273static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp);
David Howells65f27f32006-11-22 14:55:48 +0000274static void cache_reap(struct work_struct *unused);
Christoph Lametered11d9e2006-06-30 01:55:45 -0700275
Ingo Molnare0a42722006-06-23 02:03:46 -0700276static int slab_early_init = 1;
277
Christoph Lametere3366012013-01-10 19:14:18 +0000278#define INDEX_AC kmalloc_index(sizeof(struct arraycache_init))
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000279#define INDEX_NODE kmalloc_index(sizeof(struct kmem_cache_node))
Christoph Lametere498be72005-09-09 13:03:32 -0700280
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000281static void kmem_cache_node_init(struct kmem_cache_node *parent)
Christoph Lametere498be72005-09-09 13:03:32 -0700282{
283 INIT_LIST_HEAD(&parent->slabs_full);
284 INIT_LIST_HEAD(&parent->slabs_partial);
285 INIT_LIST_HEAD(&parent->slabs_free);
286 parent->shared = NULL;
287 parent->alien = NULL;
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -0800288 parent->colour_next = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700289 spin_lock_init(&parent->list_lock);
290 parent->free_objects = 0;
291 parent->free_touched = 0;
292}
293
Andrew Mortona737b3e2006-03-22 00:08:11 -0800294#define MAKE_LIST(cachep, listp, slab, nodeid) \
295 do { \
296 INIT_LIST_HEAD(listp); \
Christoph Lameter6a673682013-01-10 19:14:19 +0000297 list_splice(&(cachep->node[nodeid]->slab), listp); \
Christoph Lametere498be72005-09-09 13:03:32 -0700298 } while (0)
299
Andrew Mortona737b3e2006-03-22 00:08:11 -0800300#define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
301 do { \
Christoph Lametere498be72005-09-09 13:03:32 -0700302 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
303 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
304 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
305 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307#define CFLGS_OFF_SLAB (0x80000000UL)
308#define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
309
310#define BATCHREFILL_LIMIT 16
Andrew Mortona737b3e2006-03-22 00:08:11 -0800311/*
312 * Optimization question: fewer reaps means less probability for unnessary
313 * cpucache drain/refill cycles.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 *
Adrian Bunkdc6f3f22005-11-08 16:44:08 +0100315 * OTOH the cpuarrays can contain lots of objects,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 * which could lock up otherwise freeable slabs.
317 */
318#define REAPTIMEOUT_CPUC (2*HZ)
319#define REAPTIMEOUT_LIST3 (4*HZ)
320
321#if STATS
322#define STATS_INC_ACTIVE(x) ((x)->num_active++)
323#define STATS_DEC_ACTIVE(x) ((x)->num_active--)
324#define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
325#define STATS_INC_GROWN(x) ((x)->grown++)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700326#define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
Andrew Mortona737b3e2006-03-22 00:08:11 -0800327#define STATS_SET_HIGH(x) \
328 do { \
329 if ((x)->num_active > (x)->high_mark) \
330 (x)->high_mark = (x)->num_active; \
331 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332#define STATS_INC_ERR(x) ((x)->errors++)
333#define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
Christoph Lametere498be72005-09-09 13:03:32 -0700334#define STATS_INC_NODEFREES(x) ((x)->node_frees++)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700335#define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800336#define STATS_SET_FREEABLE(x, i) \
337 do { \
338 if ((x)->max_freeable < i) \
339 (x)->max_freeable = i; \
340 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341#define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
342#define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
343#define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
344#define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
345#else
346#define STATS_INC_ACTIVE(x) do { } while (0)
347#define STATS_DEC_ACTIVE(x) do { } while (0)
348#define STATS_INC_ALLOCED(x) do { } while (0)
349#define STATS_INC_GROWN(x) do { } while (0)
Andi Kleen4e60c862010-08-09 17:19:03 -0700350#define STATS_ADD_REAPED(x,y) do { (void)(y); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351#define STATS_SET_HIGH(x) do { } while (0)
352#define STATS_INC_ERR(x) do { } while (0)
353#define STATS_INC_NODEALLOCS(x) do { } while (0)
Christoph Lametere498be72005-09-09 13:03:32 -0700354#define STATS_INC_NODEFREES(x) do { } while (0)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700355#define STATS_INC_ACOVERFLOW(x) do { } while (0)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800356#define STATS_SET_FREEABLE(x, i) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357#define STATS_INC_ALLOCHIT(x) do { } while (0)
358#define STATS_INC_ALLOCMISS(x) do { } while (0)
359#define STATS_INC_FREEHIT(x) do { } while (0)
360#define STATS_INC_FREEMISS(x) do { } while (0)
361#endif
362
363#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
Andrew Mortona737b3e2006-03-22 00:08:11 -0800365/*
366 * memory layout of objects:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 * 0 : objp
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800368 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 * the end of an object is aligned with the end of the real
370 * allocation. Catches writes behind the end of the allocation.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800371 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 * redzone word.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800373 * cachep->obj_offset: The real object.
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500374 * cachep->size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
375 * cachep->size - 1* BYTES_PER_WORD: last caller address
Andrew Mortona737b3e2006-03-22 00:08:11 -0800376 * [BYTES_PER_WORD long]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800378static int obj_offset(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800380 return cachep->obj_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381}
382
David Woodhouseb46b8f12007-05-08 00:22:59 -0700383static unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
385 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
David Woodhouseb46b8f12007-05-08 00:22:59 -0700386 return (unsigned long long*) (objp + obj_offset(cachep) -
387 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
David Woodhouseb46b8f12007-05-08 00:22:59 -0700390static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
393 if (cachep->flags & SLAB_STORE_USER)
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500394 return (unsigned long long *)(objp + cachep->size -
David Woodhouseb46b8f12007-05-08 00:22:59 -0700395 sizeof(unsigned long long) -
David Woodhouse87a927c2007-07-04 21:26:44 -0400396 REDZONE_ALIGN);
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500397 return (unsigned long long *) (objp + cachep->size -
David Woodhouseb46b8f12007-05-08 00:22:59 -0700398 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Pekka Enberg343e0d72006-02-01 03:05:50 -0800401static void **dbg_userword(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402{
403 BUG_ON(!(cachep->flags & SLAB_STORE_USER));
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500404 return (void **)(objp + cachep->size - BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
407#else
408
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800409#define obj_offset(x) 0
David Woodhouseb46b8f12007-05-08 00:22:59 -0700410#define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
411#define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412#define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
413
414#endif
415
416/*
David Rientjes3df1ccc2011-10-18 22:09:28 -0700417 * Do not go above this order unless 0 objects fit into the slab or
418 * overridden on the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 */
David Rientjes543585c2011-10-18 22:09:24 -0700420#define SLAB_MAX_ORDER_HI 1
421#define SLAB_MAX_ORDER_LO 0
422static int slab_max_order = SLAB_MAX_ORDER_LO;
David Rientjes3df1ccc2011-10-18 22:09:28 -0700423static bool slab_max_order_set __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800425static inline struct kmem_cache *virt_to_cache(const void *obj)
426{
Christoph Lameterb49af682007-05-06 14:49:41 -0700427 struct page *page = virt_to_head_page(obj);
Christoph Lameter35026082012-06-13 10:24:56 -0500428 return page->slab_cache;
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800429}
430
431static inline struct slab *virt_to_slab(const void *obj)
432{
Christoph Lameterb49af682007-05-06 14:49:41 -0700433 struct page *page = virt_to_head_page(obj);
Christoph Lameter35026082012-06-13 10:24:56 -0500434
435 VM_BUG_ON(!PageSlab(page));
436 return page->slab_page;
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800437}
438
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800439static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
440 unsigned int idx)
441{
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500442 return slab->s_mem + cache->size * idx;
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800443}
444
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800445/*
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500446 * We want to avoid an expensive divide : (offset / cache->size)
447 * Using the fact that size is a constant for a particular cache,
448 * we can replace (offset / cache->size) by
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800449 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
450 */
451static inline unsigned int obj_to_index(const struct kmem_cache *cache,
452 const struct slab *slab, void *obj)
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800453{
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800454 u32 offset = (obj - slab->s_mem);
455 return reciprocal_divide(offset, cache->reciprocal_buffer_size);
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800456}
457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458static struct arraycache_init initarray_generic =
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800459 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461/* internal cache of cache description objs */
Christoph Lameter9b030cb2012-09-05 00:20:33 +0000462static struct kmem_cache kmem_cache_boot = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800463 .batchcount = 1,
464 .limit = BOOT_CPUCACHE_ENTRIES,
465 .shared = 1,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500466 .size = sizeof(struct kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800467 .name = "kmem_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468};
469
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700470#define BAD_ALIEN_MAGIC 0x01020304ul
471
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200472#ifdef CONFIG_LOCKDEP
473
474/*
475 * Slab sometimes uses the kmalloc slabs to store the slab headers
476 * for other slabs "off slab".
477 * The locking for this is tricky in that it nests within the locks
478 * of all other slabs in a few places; to deal with this special
479 * locking we put on-slab caches into a separate lock-class.
480 *
481 * We set lock class for alien array caches which are up during init.
482 * The lock annotation will be lost if all cpus of a node goes down and
483 * then comes back up during hotplug
484 */
485static struct lock_class_key on_slab_l3_key;
486static struct lock_class_key on_slab_alc_key;
487
Peter Zijlstra83835b32011-07-22 15:26:05 +0200488static struct lock_class_key debugobj_l3_key;
489static struct lock_class_key debugobj_alc_key;
490
491static void slab_set_lock_classes(struct kmem_cache *cachep,
492 struct lock_class_key *l3_key, struct lock_class_key *alc_key,
493 int q)
494{
495 struct array_cache **alc;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000496 struct kmem_cache_node *n;
Peter Zijlstra83835b32011-07-22 15:26:05 +0200497 int r;
498
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000499 n = cachep->node[q];
500 if (!n)
Peter Zijlstra83835b32011-07-22 15:26:05 +0200501 return;
502
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000503 lockdep_set_class(&n->list_lock, l3_key);
504 alc = n->alien;
Peter Zijlstra83835b32011-07-22 15:26:05 +0200505 /*
506 * FIXME: This check for BAD_ALIEN_MAGIC
507 * should go away when common slab code is taught to
508 * work even without alien caches.
509 * Currently, non NUMA code returns BAD_ALIEN_MAGIC
510 * for alloc_alien_cache,
511 */
512 if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC)
513 return;
514 for_each_node(r) {
515 if (alc[r])
516 lockdep_set_class(&alc[r]->lock, alc_key);
517 }
518}
519
520static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
521{
522 slab_set_lock_classes(cachep, &debugobj_l3_key, &debugobj_alc_key, node);
523}
524
525static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
526{
527 int node;
528
529 for_each_online_node(node)
530 slab_set_debugobj_lock_classes_node(cachep, node);
531}
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 Lameterce8eb6c2013-01-10 19:14:19 +0000547 n = cache->node[q];
548 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,
Peter Zijlstra83835b32011-07-22 15:26:05 +0200552 &on_slab_alc_key, q);
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200553 }
554}
555
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800556static void on_slab_lock_classes_node(struct kmem_cache *cachep, int q)
557{
Christoph Lameter6a673682013-01-10 19:14:19 +0000558 if (!cachep->node[q])
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800559 return;
560
561 slab_set_lock_classes(cachep, &on_slab_l3_key,
562 &on_slab_alc_key, q);
563}
564
565static inline void on_slab_lock_classes(struct kmem_cache *cachep)
566{
567 int node;
568
569 VM_BUG_ON(OFF_SLAB(cachep));
570 for_each_node(node)
571 on_slab_lock_classes_node(cachep, node);
572}
573
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200574static inline void init_lock_keys(void)
575{
576 int node;
577
578 for_each_node(node)
579 init_node_lock_keys(node);
580}
581#else
582static void init_node_lock_keys(int q)
583{
584}
585
586static inline void init_lock_keys(void)
587{
588}
Peter Zijlstra83835b32011-07-22 15:26:05 +0200589
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800590static inline void on_slab_lock_classes(struct kmem_cache *cachep)
591{
592}
593
594static inline void on_slab_lock_classes_node(struct kmem_cache *cachep, int node)
595{
596}
597
Peter Zijlstra83835b32011-07-22 15:26:05 +0200598static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
599{
600}
601
602static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
603{
604}
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200605#endif
606
Tejun Heo1871e522009-10-29 22:34:13 +0900607static DEFINE_PER_CPU(struct delayed_work, slab_reap_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Pekka Enberg343e0d72006-02-01 03:05:50 -0800609static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610{
611 return cachep->array[smp_processor_id()];
612}
613
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800614static size_t slab_mgmt_size(size_t nr_objs, size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800616 return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align);
617}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Andrew Mortona737b3e2006-03-22 00:08:11 -0800619/*
620 * Calculate the number of objects and left-over bytes for a given buffer size.
621 */
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800622static void cache_estimate(unsigned long gfporder, size_t buffer_size,
623 size_t align, int flags, size_t *left_over,
624 unsigned int *num)
625{
626 int nr_objs;
627 size_t mgmt_size;
628 size_t slab_size = PAGE_SIZE << gfporder;
629
630 /*
631 * The slab management structure can be either off the slab or
632 * on it. For the latter case, the memory allocated for a
633 * slab is used for:
634 *
635 * - The struct slab
636 * - One kmem_bufctl_t for each object
637 * - Padding to respect alignment of @align
638 * - @buffer_size bytes for each object
639 *
640 * If the slab management structure is off the slab, then the
641 * alignment will already be calculated into the size. Because
642 * the slabs are all pages aligned, the objects will be at the
643 * correct alignment when allocated.
644 */
645 if (flags & CFLGS_OFF_SLAB) {
646 mgmt_size = 0;
647 nr_objs = slab_size / buffer_size;
648
649 if (nr_objs > SLAB_LIMIT)
650 nr_objs = SLAB_LIMIT;
651 } else {
652 /*
653 * Ignore padding for the initial guess. The padding
654 * is at most @align-1 bytes, and @buffer_size is at
655 * least @align. In the worst case, this result will
656 * be one greater than the number of objects that fit
657 * into the memory allocation when taking the padding
658 * into account.
659 */
660 nr_objs = (slab_size - sizeof(struct slab)) /
661 (buffer_size + sizeof(kmem_bufctl_t));
662
663 /*
664 * This calculated number will be either the right
665 * amount, or one greater than what we want.
666 */
667 if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
668 > slab_size)
669 nr_objs--;
670
671 if (nr_objs > SLAB_LIMIT)
672 nr_objs = SLAB_LIMIT;
673
674 mgmt_size = slab_mgmt_size(nr_objs, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 }
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800676 *num = nr_objs;
677 *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678}
679
Christoph Lameterf28510d2012-09-11 19:49:38 +0000680#if DEBUG
Harvey Harrisond40cee22008-04-30 00:55:07 -0700681#define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Andrew Mortona737b3e2006-03-22 00:08:11 -0800683static void __slab_error(const char *function, struct kmem_cache *cachep,
684 char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
686 printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800687 function, cachep->name, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +1030689 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
Christoph Lameterf28510d2012-09-11 19:49:38 +0000691#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Paul Menage3395ee02006-12-06 20:32:16 -0800693/*
694 * By default on NUMA we use alien caches to stage the freeing of
695 * objects allocated from other nodes. This causes massive memory
696 * inefficiencies when using fake NUMA setup to split memory into a
697 * large number of small nodes, so it can be disabled on the command
698 * line
699 */
700
701static int use_alien_caches __read_mostly = 1;
702static int __init noaliencache_setup(char *s)
703{
704 use_alien_caches = 0;
705 return 1;
706}
707__setup("noaliencache", noaliencache_setup);
708
David Rientjes3df1ccc2011-10-18 22:09:28 -0700709static int __init slab_max_order_setup(char *str)
710{
711 get_option(&str, &slab_max_order);
712 slab_max_order = slab_max_order < 0 ? 0 :
713 min(slab_max_order, MAX_ORDER - 1);
714 slab_max_order_set = true;
715
716 return 1;
717}
718__setup("slab_max_order=", slab_max_order_setup);
719
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800720#ifdef CONFIG_NUMA
721/*
722 * Special reaping functions for NUMA systems called from cache_reap().
723 * These take care of doing round robin flushing of alien caches (containing
724 * objects freed on different nodes from which they were allocated) and the
725 * flushing of remote pcps by calling drain_node_pages.
726 */
Tejun Heo1871e522009-10-29 22:34:13 +0900727static DEFINE_PER_CPU(unsigned long, slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800728
729static void init_reap_node(int cpu)
730{
731 int node;
732
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -0700733 node = next_node(cpu_to_mem(cpu), node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800734 if (node == MAX_NUMNODES)
Paul Jackson442295c2006-03-22 00:09:11 -0800735 node = first_node(node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800736
Tejun Heo1871e522009-10-29 22:34:13 +0900737 per_cpu(slab_reap_node, cpu) = node;
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800738}
739
740static void next_reap_node(void)
741{
Christoph Lameter909ea962010-12-08 16:22:55 +0100742 int node = __this_cpu_read(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800743
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800744 node = next_node(node, node_online_map);
745 if (unlikely(node >= MAX_NUMNODES))
746 node = first_node(node_online_map);
Christoph Lameter909ea962010-12-08 16:22:55 +0100747 __this_cpu_write(slab_reap_node, node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800748}
749
750#else
751#define init_reap_node(cpu) do { } while (0)
752#define next_reap_node(void) do { } while (0)
753#endif
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755/*
756 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
757 * via the workqueue/eventd.
758 * Add the CPU number into the expiration time to minimize the possibility of
759 * the CPUs getting into lockstep and contending for the global cache chain
760 * lock.
761 */
Paul Gortmaker0db06282013-06-19 14:53:51 -0400762static void start_cpu_timer(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
Tejun Heo1871e522009-10-29 22:34:13 +0900764 struct delayed_work *reap_work = &per_cpu(slab_reap_work, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 /*
767 * When this gets called from do_initcalls via cpucache_init(),
768 * init_workqueues() has already run, so keventd will be setup
769 * at that time.
770 */
David Howells52bad642006-11-22 14:54:01 +0000771 if (keventd_up() && reap_work->work.func == NULL) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800772 init_reap_node(cpu);
Tejun Heo203b42f2012-08-21 13:18:23 -0700773 INIT_DEFERRABLE_WORK(reap_work, cache_reap);
Arjan van de Ven2b284212006-12-10 02:21:28 -0800774 schedule_delayed_work_on(cpu, reap_work,
775 __round_jiffies_relative(HZ, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777}
778
Christoph Lametere498be72005-09-09 13:03:32 -0700779static struct array_cache *alloc_arraycache(int node, int entries,
Pekka Enberg83b519e2009-06-10 19:40:04 +0300780 int batchcount, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800782 int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 struct array_cache *nc = NULL;
784
Pekka Enberg83b519e2009-06-10 19:40:04 +0300785 nc = kmalloc_node(memsize, gfp, node);
Catalin Marinasd5cff632009-06-11 13:22:40 +0100786 /*
787 * The array_cache structures contain pointers to free object.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300788 * However, when such objects are allocated or transferred to another
Catalin Marinasd5cff632009-06-11 13:22:40 +0100789 * cache the pointers are not cleared and they could be counted as
790 * valid references during a kmemleak scan. Therefore, kmemleak must
791 * not scan such objects.
792 */
793 kmemleak_no_scan(nc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (nc) {
795 nc->avail = 0;
796 nc->limit = entries;
797 nc->batchcount = batchcount;
798 nc->touched = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700799 spin_lock_init(&nc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801 return nc;
802}
803
Mel Gorman072bb0a2012-07-31 16:43:58 -0700804static inline bool is_slab_pfmemalloc(struct slab *slabp)
805{
806 struct page *page = virt_to_page(slabp->s_mem);
807
808 return PageSlabPfmemalloc(page);
809}
810
811/* Clears pfmemalloc_active if no slabs have pfmalloc set */
812static void recheck_pfmemalloc_active(struct kmem_cache *cachep,
813 struct array_cache *ac)
814{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000815 struct kmem_cache_node *n = cachep->node[numa_mem_id()];
Mel Gorman072bb0a2012-07-31 16:43:58 -0700816 struct slab *slabp;
817 unsigned long flags;
818
819 if (!pfmemalloc_active)
820 return;
821
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000822 spin_lock_irqsave(&n->list_lock, flags);
823 list_for_each_entry(slabp, &n->slabs_full, list)
Mel Gorman072bb0a2012-07-31 16:43:58 -0700824 if (is_slab_pfmemalloc(slabp))
825 goto out;
826
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000827 list_for_each_entry(slabp, &n->slabs_partial, list)
Mel Gorman072bb0a2012-07-31 16:43:58 -0700828 if (is_slab_pfmemalloc(slabp))
829 goto out;
830
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000831 list_for_each_entry(slabp, &n->slabs_free, list)
Mel Gorman072bb0a2012-07-31 16:43:58 -0700832 if (is_slab_pfmemalloc(slabp))
833 goto out;
834
835 pfmemalloc_active = false;
836out:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000837 spin_unlock_irqrestore(&n->list_lock, flags);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700838}
839
Mel Gorman381760e2012-07-31 16:44:30 -0700840static void *__ac_get_obj(struct kmem_cache *cachep, struct array_cache *ac,
Mel Gorman072bb0a2012-07-31 16:43:58 -0700841 gfp_t flags, bool force_refill)
842{
843 int i;
844 void *objp = ac->entry[--ac->avail];
845
846 /* Ensure the caller is allowed to use objects from PFMEMALLOC slab */
847 if (unlikely(is_obj_pfmemalloc(objp))) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000848 struct kmem_cache_node *n;
Mel Gorman072bb0a2012-07-31 16:43:58 -0700849
850 if (gfp_pfmemalloc_allowed(flags)) {
851 clear_obj_pfmemalloc(&objp);
852 return objp;
853 }
854
855 /* The caller cannot use PFMEMALLOC objects, find another one */
Joonsoo Kimd014dc22012-09-17 14:09:06 -0700856 for (i = 0; i < ac->avail; i++) {
Mel Gorman072bb0a2012-07-31 16:43:58 -0700857 /* If a !PFMEMALLOC object is found, swap them */
858 if (!is_obj_pfmemalloc(ac->entry[i])) {
859 objp = ac->entry[i];
860 ac->entry[i] = ac->entry[ac->avail];
861 ac->entry[ac->avail] = objp;
862 return objp;
863 }
864 }
865
866 /*
867 * If there are empty slabs on the slabs_free list and we are
868 * being forced to refill the cache, mark this one !pfmemalloc.
869 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000870 n = cachep->node[numa_mem_id()];
871 if (!list_empty(&n->slabs_free) && force_refill) {
Mel Gorman072bb0a2012-07-31 16:43:58 -0700872 struct slab *slabp = virt_to_slab(objp);
Mel Gorman30c29be2012-09-17 14:09:03 -0700873 ClearPageSlabPfmemalloc(virt_to_head_page(slabp->s_mem));
Mel Gorman072bb0a2012-07-31 16:43:58 -0700874 clear_obj_pfmemalloc(&objp);
875 recheck_pfmemalloc_active(cachep, ac);
876 return objp;
877 }
878
879 /* No !PFMEMALLOC objects available */
880 ac->avail++;
881 objp = NULL;
882 }
883
884 return objp;
885}
886
Mel Gorman381760e2012-07-31 16:44:30 -0700887static inline void *ac_get_obj(struct kmem_cache *cachep,
888 struct array_cache *ac, gfp_t flags, bool force_refill)
889{
890 void *objp;
891
892 if (unlikely(sk_memalloc_socks()))
893 objp = __ac_get_obj(cachep, ac, flags, force_refill);
894 else
895 objp = ac->entry[--ac->avail];
896
897 return objp;
898}
899
900static void *__ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
Mel Gorman072bb0a2012-07-31 16:43:58 -0700901 void *objp)
902{
903 if (unlikely(pfmemalloc_active)) {
904 /* Some pfmemalloc slabs exist, check if this is one */
Joonsoo Kim73293c22013-10-24 10:07:37 +0900905 struct slab *slabp = virt_to_slab(objp);
906 struct page *page = virt_to_head_page(slabp->s_mem);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700907 if (PageSlabPfmemalloc(page))
908 set_obj_pfmemalloc(&objp);
909 }
910
Mel Gorman381760e2012-07-31 16:44:30 -0700911 return objp;
912}
913
914static inline void ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
915 void *objp)
916{
917 if (unlikely(sk_memalloc_socks()))
918 objp = __ac_put_obj(cachep, ac, objp);
919
Mel Gorman072bb0a2012-07-31 16:43:58 -0700920 ac->entry[ac->avail++] = objp;
921}
922
Christoph Lameter3ded1752006-03-25 03:06:44 -0800923/*
924 * Transfer objects in one arraycache to another.
925 * Locking must be handled by the caller.
926 *
927 * Return the number of entries transferred.
928 */
929static int transfer_objects(struct array_cache *to,
930 struct array_cache *from, unsigned int max)
931{
932 /* Figure out how many entries to transfer */
Hagen Paul Pfeifer732eacc2010-10-26 14:22:23 -0700933 int nr = min3(from->avail, max, to->limit - to->avail);
Christoph Lameter3ded1752006-03-25 03:06:44 -0800934
935 if (!nr)
936 return 0;
937
938 memcpy(to->entry + to->avail, from->entry + from->avail -nr,
939 sizeof(void *) *nr);
940
941 from->avail -= nr;
942 to->avail += nr;
Christoph Lameter3ded1752006-03-25 03:06:44 -0800943 return nr;
944}
945
Christoph Lameter765c4502006-09-27 01:50:08 -0700946#ifndef CONFIG_NUMA
947
948#define drain_alien_cache(cachep, alien) do { } while (0)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000949#define reap_alien(cachep, n) do { } while (0)
Christoph Lameter765c4502006-09-27 01:50:08 -0700950
Pekka Enberg83b519e2009-06-10 19:40:04 +0300951static inline struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
Christoph Lameter765c4502006-09-27 01:50:08 -0700952{
953 return (struct array_cache **)BAD_ALIEN_MAGIC;
954}
955
956static inline void free_alien_cache(struct array_cache **ac_ptr)
957{
958}
959
960static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
961{
962 return 0;
963}
964
965static inline void *alternate_node_alloc(struct kmem_cache *cachep,
966 gfp_t flags)
967{
968 return NULL;
969}
970
Christoph Hellwig8b98c162006-12-06 20:32:30 -0800971static inline void *____cache_alloc_node(struct kmem_cache *cachep,
Christoph Lameter765c4502006-09-27 01:50:08 -0700972 gfp_t flags, int nodeid)
973{
974 return NULL;
975}
976
977#else /* CONFIG_NUMA */
978
Christoph Hellwig8b98c162006-12-06 20:32:30 -0800979static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
Paul Jacksonc61afb12006-03-24 03:16:08 -0800980static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
Christoph Lameterdc85da12006-01-18 17:42:36 -0800981
Pekka Enberg83b519e2009-06-10 19:40:04 +0300982static struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -0700983{
984 struct array_cache **ac_ptr;
Christoph Lameter8ef82862007-02-20 13:57:52 -0800985 int memsize = sizeof(void *) * nr_node_ids;
Christoph Lametere498be72005-09-09 13:03:32 -0700986 int i;
987
988 if (limit > 1)
989 limit = 12;
Haicheng Lif3186a92010-01-06 15:25:23 +0800990 ac_ptr = kzalloc_node(memsize, gfp, node);
Christoph Lametere498be72005-09-09 13:03:32 -0700991 if (ac_ptr) {
992 for_each_node(i) {
Haicheng Lif3186a92010-01-06 15:25:23 +0800993 if (i == node || !node_online(i))
Christoph Lametere498be72005-09-09 13:03:32 -0700994 continue;
Pekka Enberg83b519e2009-06-10 19:40:04 +0300995 ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d, gfp);
Christoph Lametere498be72005-09-09 13:03:32 -0700996 if (!ac_ptr[i]) {
Akinobu Mitacc550de2007-11-14 16:58:35 -0800997 for (i--; i >= 0; i--)
Christoph Lametere498be72005-09-09 13:03:32 -0700998 kfree(ac_ptr[i]);
999 kfree(ac_ptr);
1000 return NULL;
1001 }
1002 }
1003 }
1004 return ac_ptr;
1005}
1006
Pekka Enberg5295a742006-02-01 03:05:48 -08001007static void free_alien_cache(struct array_cache **ac_ptr)
Christoph Lametere498be72005-09-09 13:03:32 -07001008{
1009 int i;
1010
1011 if (!ac_ptr)
1012 return;
Christoph Lametere498be72005-09-09 13:03:32 -07001013 for_each_node(i)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001014 kfree(ac_ptr[i]);
Christoph Lametere498be72005-09-09 13:03:32 -07001015 kfree(ac_ptr);
1016}
1017
Pekka Enberg343e0d72006-02-01 03:05:50 -08001018static void __drain_alien_cache(struct kmem_cache *cachep,
Pekka Enberg5295a742006-02-01 03:05:48 -08001019 struct array_cache *ac, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07001020{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001021 struct kmem_cache_node *n = cachep->node[node];
Christoph Lametere498be72005-09-09 13:03:32 -07001022
1023 if (ac->avail) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001024 spin_lock(&n->list_lock);
Christoph Lametere00946f2006-03-25 03:06:45 -08001025 /*
1026 * Stuff objects into the remote nodes shared array first.
1027 * That way we could avoid the overhead of putting the objects
1028 * into the free lists and getting them back later.
1029 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001030 if (n->shared)
1031 transfer_objects(n->shared, ac, ac->limit);
Christoph Lametere00946f2006-03-25 03:06:45 -08001032
Christoph Lameterff694162005-09-22 21:44:02 -07001033 free_block(cachep, ac->entry, ac->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001034 ac->avail = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001035 spin_unlock(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07001036 }
1037}
1038
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001039/*
1040 * Called from cache_reap() to regularly drain alien caches round robin.
1041 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001042static void reap_alien(struct kmem_cache *cachep, struct kmem_cache_node *n)
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001043{
Christoph Lameter909ea962010-12-08 16:22:55 +01001044 int node = __this_cpu_read(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001045
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001046 if (n->alien) {
1047 struct array_cache *ac = n->alien[node];
Christoph Lametere00946f2006-03-25 03:06:45 -08001048
1049 if (ac && ac->avail && spin_trylock_irq(&ac->lock)) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001050 __drain_alien_cache(cachep, ac, node);
1051 spin_unlock_irq(&ac->lock);
1052 }
1053 }
1054}
1055
Andrew Mortona737b3e2006-03-22 00:08:11 -08001056static void drain_alien_cache(struct kmem_cache *cachep,
1057 struct array_cache **alien)
Christoph Lametere498be72005-09-09 13:03:32 -07001058{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001059 int i = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07001060 struct array_cache *ac;
1061 unsigned long flags;
1062
1063 for_each_online_node(i) {
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001064 ac = alien[i];
Christoph Lametere498be72005-09-09 13:03:32 -07001065 if (ac) {
1066 spin_lock_irqsave(&ac->lock, flags);
1067 __drain_alien_cache(cachep, ac, i);
1068 spin_unlock_irqrestore(&ac->lock, flags);
1069 }
1070 }
1071}
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001072
Ingo Molnar873623d2006-07-13 14:44:38 +02001073static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001074{
Joonsoo Kim1ea991b2013-10-24 10:07:40 +09001075 int nodeid = page_to_nid(virt_to_page(objp));
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001076 struct kmem_cache_node *n;
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001077 struct array_cache *alien = NULL;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001078 int node;
1079
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001080 node = numa_mem_id();
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001081
1082 /*
1083 * Make sure we are not freeing a object from another node to the array
1084 * cache on this cpu.
1085 */
Joonsoo Kim1ea991b2013-10-24 10:07:40 +09001086 if (likely(nodeid == node))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001087 return 0;
1088
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001089 n = cachep->node[node];
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001090 STATS_INC_NODEFREES(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001091 if (n->alien && n->alien[nodeid]) {
1092 alien = n->alien[nodeid];
Ingo Molnar873623d2006-07-13 14:44:38 +02001093 spin_lock(&alien->lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001094 if (unlikely(alien->avail == alien->limit)) {
1095 STATS_INC_ACOVERFLOW(cachep);
1096 __drain_alien_cache(cachep, alien, nodeid);
1097 }
Mel Gorman072bb0a2012-07-31 16:43:58 -07001098 ac_put_obj(cachep, alien, objp);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001099 spin_unlock(&alien->lock);
1100 } else {
Christoph Lameter6a673682013-01-10 19:14:19 +00001101 spin_lock(&(cachep->node[nodeid])->list_lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001102 free_block(cachep, &objp, 1, nodeid);
Christoph Lameter6a673682013-01-10 19:14:19 +00001103 spin_unlock(&(cachep->node[nodeid])->list_lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001104 }
1105 return 1;
1106}
Christoph Lametere498be72005-09-09 13:03:32 -07001107#endif
1108
David Rientjes8f9f8d92010-03-27 19:40:47 -07001109/*
Christoph Lameter6a673682013-01-10 19:14:19 +00001110 * Allocates and initializes node for a node on each slab cache, used for
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001111 * either memory or cpu hotplug. If memory is being hot-added, the kmem_cache_node
David Rientjes8f9f8d92010-03-27 19:40:47 -07001112 * will be allocated off-node since memory is not yet online for the new node.
Christoph Lameter6a673682013-01-10 19:14:19 +00001113 * When hotplugging memory or a cpu, existing node are not replaced if
David Rientjes8f9f8d92010-03-27 19:40:47 -07001114 * already in use.
1115 *
Christoph Lameter18004c52012-07-06 15:25:12 -05001116 * Must hold slab_mutex.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001117 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001118static int init_cache_node_node(int node)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001119{
1120 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001121 struct kmem_cache_node *n;
Christoph Lameter6744f082013-01-10 19:12:17 +00001122 const int memsize = sizeof(struct kmem_cache_node);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001123
Christoph Lameter18004c52012-07-06 15:25:12 -05001124 list_for_each_entry(cachep, &slab_caches, list) {
David Rientjes8f9f8d92010-03-27 19:40:47 -07001125 /*
1126 * Set up the size64 kmemlist for cpu before we can
1127 * begin anything. Make sure some other cpu on this
1128 * node has not already allocated this
1129 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001130 if (!cachep->node[node]) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001131 n = kmalloc_node(memsize, GFP_KERNEL, node);
1132 if (!n)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001133 return -ENOMEM;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001134 kmem_cache_node_init(n);
1135 n->next_reap = jiffies + REAPTIMEOUT_LIST3 +
David Rientjes8f9f8d92010-03-27 19:40:47 -07001136 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1137
1138 /*
1139 * The l3s don't come and go as CPUs come and
Christoph Lameter18004c52012-07-06 15:25:12 -05001140 * go. slab_mutex is sufficient
David Rientjes8f9f8d92010-03-27 19:40:47 -07001141 * protection here.
1142 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001143 cachep->node[node] = n;
David Rientjes8f9f8d92010-03-27 19:40:47 -07001144 }
1145
Christoph Lameter6a673682013-01-10 19:14:19 +00001146 spin_lock_irq(&cachep->node[node]->list_lock);
1147 cachep->node[node]->free_limit =
David Rientjes8f9f8d92010-03-27 19:40:47 -07001148 (1 + nr_cpus_node(node)) *
1149 cachep->batchcount + cachep->num;
Christoph Lameter6a673682013-01-10 19:14:19 +00001150 spin_unlock_irq(&cachep->node[node]->list_lock);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001151 }
1152 return 0;
1153}
1154
Wanpeng Li0fa81032013-07-04 08:33:22 +08001155static inline int slabs_tofree(struct kmem_cache *cachep,
1156 struct kmem_cache_node *n)
1157{
1158 return (n->free_objects + cachep->num - 1) / cachep->num;
1159}
1160
Paul Gortmaker0db06282013-06-19 14:53:51 -04001161static void cpuup_canceled(long cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001163 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001164 struct kmem_cache_node *n = NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001165 int node = cpu_to_mem(cpu);
Rusty Russella70f7302009-03-13 14:49:46 +10301166 const struct cpumask *mask = cpumask_of_node(node);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001167
Christoph Lameter18004c52012-07-06 15:25:12 -05001168 list_for_each_entry(cachep, &slab_caches, list) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001169 struct array_cache *nc;
1170 struct array_cache *shared;
1171 struct array_cache **alien;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001172
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001173 /* cpu is dead; no one can alloc from it. */
1174 nc = cachep->array[cpu];
1175 cachep->array[cpu] = NULL;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001176 n = cachep->node[node];
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001177
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001178 if (!n)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001179 goto free_array_cache;
1180
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001181 spin_lock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001182
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001183 /* Free limit for this kmem_cache_node */
1184 n->free_limit -= cachep->batchcount;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001185 if (nc)
1186 free_block(cachep, nc->entry, nc->avail, node);
1187
Rusty Russell58463c12009-12-17 11:43:12 -06001188 if (!cpumask_empty(mask)) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001189 spin_unlock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001190 goto free_array_cache;
1191 }
1192
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001193 shared = n->shared;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001194 if (shared) {
1195 free_block(cachep, shared->entry,
1196 shared->avail, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001197 n->shared = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001198 }
1199
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001200 alien = n->alien;
1201 n->alien = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001202
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001203 spin_unlock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001204
1205 kfree(shared);
1206 if (alien) {
1207 drain_alien_cache(cachep, alien);
1208 free_alien_cache(alien);
1209 }
1210free_array_cache:
1211 kfree(nc);
1212 }
1213 /*
1214 * In the previous loop, all the objects were freed to
1215 * the respective cache's slabs, now we can go ahead and
1216 * shrink each nodelist to its limit.
1217 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001218 list_for_each_entry(cachep, &slab_caches, list) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001219 n = cachep->node[node];
1220 if (!n)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001221 continue;
Wanpeng Li0fa81032013-07-04 08:33:22 +08001222 drain_freelist(cachep, n, slabs_tofree(cachep, n));
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001223 }
1224}
1225
Paul Gortmaker0db06282013-06-19 14:53:51 -04001226static int cpuup_prepare(long cpu)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001227{
Pekka Enberg343e0d72006-02-01 03:05:50 -08001228 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001229 struct kmem_cache_node *n = NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001230 int node = cpu_to_mem(cpu);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001231 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001233 /*
1234 * We need to do this right in the beginning since
1235 * alloc_arraycache's are going to use this list.
1236 * kmalloc_node allows us to add the slab to the right
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001237 * kmem_cache_node and not this cpu's kmem_cache_node
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001238 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001239 err = init_cache_node_node(node);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001240 if (err < 0)
1241 goto bad;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001242
1243 /*
1244 * Now we can go ahead with allocating the shared arrays and
1245 * array caches
1246 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001247 list_for_each_entry(cachep, &slab_caches, list) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001248 struct array_cache *nc;
1249 struct array_cache *shared = NULL;
1250 struct array_cache **alien = NULL;
1251
1252 nc = alloc_arraycache(node, cachep->limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001253 cachep->batchcount, GFP_KERNEL);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001254 if (!nc)
1255 goto bad;
1256 if (cachep->shared) {
1257 shared = alloc_arraycache(node,
1258 cachep->shared * cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001259 0xbaadf00d, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001260 if (!shared) {
1261 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001262 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001263 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001264 }
1265 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03001266 alien = alloc_alien_cache(node, cachep->limit, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001267 if (!alien) {
1268 kfree(shared);
1269 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001270 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001271 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001272 }
1273 cachep->array[cpu] = nc;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001274 n = cachep->node[node];
1275 BUG_ON(!n);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001276
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001277 spin_lock_irq(&n->list_lock);
1278 if (!n->shared) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001279 /*
1280 * We are serialised from CPU_DEAD or
1281 * CPU_UP_CANCELLED by the cpucontrol lock
1282 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001283 n->shared = shared;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001284 shared = NULL;
1285 }
1286#ifdef CONFIG_NUMA
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001287 if (!n->alien) {
1288 n->alien = alien;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001289 alien = NULL;
1290 }
1291#endif
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001292 spin_unlock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001293 kfree(shared);
1294 free_alien_cache(alien);
Peter Zijlstra83835b32011-07-22 15:26:05 +02001295 if (cachep->flags & SLAB_DEBUG_OBJECTS)
1296 slab_set_debugobj_lock_classes_node(cachep, node);
Glauber Costa6ccfb5b2012-12-18 14:22:31 -08001297 else if (!OFF_SLAB(cachep) &&
1298 !(cachep->flags & SLAB_DESTROY_BY_RCU))
1299 on_slab_lock_classes_node(cachep, node);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001300 }
Pekka Enbergce79ddc2009-11-23 22:01:15 +02001301 init_node_lock_keys(node);
1302
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001303 return 0;
1304bad:
Akinobu Mita12d00f62007-10-18 03:05:11 -07001305 cpuup_canceled(cpu);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001306 return -ENOMEM;
1307}
1308
Paul Gortmaker0db06282013-06-19 14:53:51 -04001309static int cpuup_callback(struct notifier_block *nfb,
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001310 unsigned long action, void *hcpu)
1311{
1312 long cpu = (long)hcpu;
1313 int err = 0;
1314
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 switch (action) {
Heiko Carstens38c3bd92007-05-09 02:34:05 -07001316 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001317 case CPU_UP_PREPARE_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001318 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001319 err = cpuup_prepare(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001320 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 break;
1322 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001323 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324 start_cpu_timer(cpu);
1325 break;
1326#ifdef CONFIG_HOTPLUG_CPU
Christoph Lameter5830c592007-05-09 02:34:22 -07001327 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001328 case CPU_DOWN_PREPARE_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001329 /*
Christoph Lameter18004c52012-07-06 15:25:12 -05001330 * Shutdown cache reaper. Note that the slab_mutex is
Christoph Lameter5830c592007-05-09 02:34:22 -07001331 * held so that if cache_reap() is invoked it cannot do
1332 * anything expensive but will only modify reap_work
1333 * and reschedule the timer.
1334 */
Tejun Heoafe2c512010-12-14 16:21:17 +01001335 cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu));
Christoph Lameter5830c592007-05-09 02:34:22 -07001336 /* Now the cache_reaper is guaranteed to be not running. */
Tejun Heo1871e522009-10-29 22:34:13 +09001337 per_cpu(slab_reap_work, cpu).work.func = NULL;
Christoph Lameter5830c592007-05-09 02:34:22 -07001338 break;
1339 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001340 case CPU_DOWN_FAILED_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001341 start_cpu_timer(cpu);
1342 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001344 case CPU_DEAD_FROZEN:
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001345 /*
1346 * Even if all the cpus of a node are down, we don't free the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001347 * kmem_cache_node of any cache. This to avoid a race between
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001348 * cpu_down, and a kmalloc allocation from another cpu for
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001349 * memory from the node of the cpu going down. The node
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001350 * structure is usually allocated from kmem_cache_create() and
1351 * gets destroyed at kmem_cache_destroy().
1352 */
Simon Arlott183ff222007-10-20 01:27:18 +02001353 /* fall through */
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08001354#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001356 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001357 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001358 cpuup_canceled(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001359 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 }
Akinobu Mitaeac40682010-05-26 14:43:32 -07001362 return notifier_from_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363}
1364
Paul Gortmaker0db06282013-06-19 14:53:51 -04001365static struct notifier_block cpucache_notifier = {
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001366 &cpuup_callback, NULL, 0
1367};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
David Rientjes8f9f8d92010-03-27 19:40:47 -07001369#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
1370/*
1371 * Drains freelist for a node on each slab cache, used for memory hot-remove.
1372 * Returns -EBUSY if all objects cannot be drained so that the node is not
1373 * removed.
1374 *
Christoph Lameter18004c52012-07-06 15:25:12 -05001375 * Must hold slab_mutex.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001376 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001377static int __meminit drain_cache_node_node(int node)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001378{
1379 struct kmem_cache *cachep;
1380 int ret = 0;
1381
Christoph Lameter18004c52012-07-06 15:25:12 -05001382 list_for_each_entry(cachep, &slab_caches, list) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001383 struct kmem_cache_node *n;
David Rientjes8f9f8d92010-03-27 19:40:47 -07001384
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001385 n = cachep->node[node];
1386 if (!n)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001387 continue;
1388
Wanpeng Li0fa81032013-07-04 08:33:22 +08001389 drain_freelist(cachep, n, slabs_tofree(cachep, n));
David Rientjes8f9f8d92010-03-27 19:40:47 -07001390
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001391 if (!list_empty(&n->slabs_full) ||
1392 !list_empty(&n->slabs_partial)) {
David Rientjes8f9f8d92010-03-27 19:40:47 -07001393 ret = -EBUSY;
1394 break;
1395 }
1396 }
1397 return ret;
1398}
1399
1400static int __meminit slab_memory_callback(struct notifier_block *self,
1401 unsigned long action, void *arg)
1402{
1403 struct memory_notify *mnb = arg;
1404 int ret = 0;
1405 int nid;
1406
1407 nid = mnb->status_change_nid;
1408 if (nid < 0)
1409 goto out;
1410
1411 switch (action) {
1412 case MEM_GOING_ONLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001413 mutex_lock(&slab_mutex);
Christoph Lameter6a673682013-01-10 19:14:19 +00001414 ret = init_cache_node_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001415 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001416 break;
1417 case MEM_GOING_OFFLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001418 mutex_lock(&slab_mutex);
Christoph Lameter6a673682013-01-10 19:14:19 +00001419 ret = drain_cache_node_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001420 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001421 break;
1422 case MEM_ONLINE:
1423 case MEM_OFFLINE:
1424 case MEM_CANCEL_ONLINE:
1425 case MEM_CANCEL_OFFLINE:
1426 break;
1427 }
1428out:
Prarit Bhargava5fda1bd2011-03-22 16:30:49 -07001429 return notifier_from_errno(ret);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001430}
1431#endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
1432
Christoph Lametere498be72005-09-09 13:03:32 -07001433/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001434 * swap the static kmem_cache_node with kmalloced memory
Christoph Lametere498be72005-09-09 13:03:32 -07001435 */
Christoph Lameter6744f082013-01-10 19:12:17 +00001436static void __init init_list(struct kmem_cache *cachep, struct kmem_cache_node *list,
David Rientjes8f9f8d92010-03-27 19:40:47 -07001437 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07001438{
Christoph Lameter6744f082013-01-10 19:12:17 +00001439 struct kmem_cache_node *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001440
Christoph Lameter6744f082013-01-10 19:12:17 +00001441 ptr = kmalloc_node(sizeof(struct kmem_cache_node), GFP_NOWAIT, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07001442 BUG_ON(!ptr);
1443
Christoph Lameter6744f082013-01-10 19:12:17 +00001444 memcpy(ptr, list, sizeof(struct kmem_cache_node));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001445 /*
1446 * Do not assume that spinlocks can be initialized via memcpy:
1447 */
1448 spin_lock_init(&ptr->list_lock);
1449
Christoph Lametere498be72005-09-09 13:03:32 -07001450 MAKE_ALL_LISTS(cachep, ptr, nodeid);
Christoph Lameter6a673682013-01-10 19:14:19 +00001451 cachep->node[nodeid] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001452}
1453
Andrew Mortona737b3e2006-03-22 00:08:11 -08001454/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001455 * For setting up all the kmem_cache_node for cache whose buffer_size is same as
1456 * size of kmem_cache_node.
Pekka Enberg556a1692008-01-25 08:20:51 +02001457 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001458static void __init set_up_node(struct kmem_cache *cachep, int index)
Pekka Enberg556a1692008-01-25 08:20:51 +02001459{
1460 int node;
1461
1462 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001463 cachep->node[node] = &init_kmem_cache_node[index + node];
Christoph Lameter6a673682013-01-10 19:14:19 +00001464 cachep->node[node]->next_reap = jiffies +
Pekka Enberg556a1692008-01-25 08:20:51 +02001465 REAPTIMEOUT_LIST3 +
1466 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1467 }
1468}
1469
1470/*
Christoph Lameter3c583462012-11-28 16:23:01 +00001471 * The memory after the last cpu cache pointer is used for the
Christoph Lameter6a673682013-01-10 19:14:19 +00001472 * the node pointer.
Christoph Lameter3c583462012-11-28 16:23:01 +00001473 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001474static void setup_node_pointer(struct kmem_cache *cachep)
Christoph Lameter3c583462012-11-28 16:23:01 +00001475{
Christoph Lameter6a673682013-01-10 19:14:19 +00001476 cachep->node = (struct kmem_cache_node **)&cachep->array[nr_cpu_ids];
Christoph Lameter3c583462012-11-28 16:23:01 +00001477}
1478
1479/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08001480 * Initialisation. Called after the page allocator have been initialised and
1481 * before smp_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 */
1483void __init kmem_cache_init(void)
1484{
Christoph Lametere498be72005-09-09 13:03:32 -07001485 int i;
1486
Joonsoo Kim68126702013-10-24 10:07:42 +09001487 BUILD_BUG_ON(sizeof(((struct page *)NULL)->lru) <
1488 sizeof(struct rcu_head));
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001489 kmem_cache = &kmem_cache_boot;
Christoph Lameter6a673682013-01-10 19:14:19 +00001490 setup_node_pointer(kmem_cache);
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001491
Mel Gormanb6e68bc2009-06-16 15:32:16 -07001492 if (num_possible_nodes() == 1)
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001493 use_alien_caches = 0;
1494
Christoph Lameter3c583462012-11-28 16:23:01 +00001495 for (i = 0; i < NUM_INIT_LISTS; i++)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001496 kmem_cache_node_init(&init_kmem_cache_node[i]);
Christoph Lameter3c583462012-11-28 16:23:01 +00001497
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001498 set_up_node(kmem_cache, CACHE_CACHE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500 /*
1501 * Fragmentation resistance on low memory - only use bigger
David Rientjes3df1ccc2011-10-18 22:09:28 -07001502 * page orders on machines with more than 32MB of memory if
1503 * not overridden on the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 */
David Rientjes3df1ccc2011-10-18 22:09:28 -07001505 if (!slab_max_order_set && totalram_pages > (32 << 20) >> PAGE_SHIFT)
David Rientjes543585c2011-10-18 22:09:24 -07001506 slab_max_order = SLAB_MAX_ORDER_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 /* Bootstrap is tricky, because several objects are allocated
1509 * from caches that do not exist yet:
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001510 * 1) initialize the kmem_cache cache: it contains the struct
1511 * kmem_cache structures of all caches, except kmem_cache itself:
1512 * kmem_cache is statically allocated.
Christoph Lametere498be72005-09-09 13:03:32 -07001513 * Initially an __init data area is used for the head array and the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001514 * kmem_cache_node structures, it's replaced with a kmalloc allocated
Christoph Lametere498be72005-09-09 13:03:32 -07001515 * array at the end of the bootstrap.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 * 2) Create the first kmalloc cache.
Pekka Enberg343e0d72006-02-01 03:05:50 -08001517 * The struct kmem_cache for the new cache is allocated normally.
Christoph Lametere498be72005-09-09 13:03:32 -07001518 * An __init data area is used for the head array.
1519 * 3) Create the remaining kmalloc caches, with minimally sized
1520 * head arrays.
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001521 * 4) Replace the __init data head arrays for kmem_cache and the first
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 * kmalloc cache with kmalloc allocated arrays.
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001523 * 5) Replace the __init data for kmem_cache_node for kmem_cache and
Christoph Lametere498be72005-09-09 13:03:32 -07001524 * the other cache's with kmalloc allocated memory.
1525 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 */
1527
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001528 /* 1) create the kmem_cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Eric Dumazet8da34302007-05-06 14:49:29 -07001530 /*
Eric Dumazetb56efcf2011-07-20 19:04:23 +02001531 * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
Eric Dumazet8da34302007-05-06 14:49:29 -07001532 */
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001533 create_boot_cache(kmem_cache, "kmem_cache",
1534 offsetof(struct kmem_cache, array[nr_cpu_ids]) +
Christoph Lameter6744f082013-01-10 19:12:17 +00001535 nr_node_ids * sizeof(struct kmem_cache_node *),
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001536 SLAB_HWCACHE_ALIGN);
1537 list_add(&kmem_cache->list, &slab_caches);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
1539 /* 2+3) create the kmalloc caches */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
Andrew Mortona737b3e2006-03-22 00:08:11 -08001541 /*
1542 * Initialize the caches that provide memory for the array cache and the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001543 * kmem_cache_node structures first. Without this, further allocations will
Andrew Mortona737b3e2006-03-22 00:08:11 -08001544 * bug.
Christoph Lametere498be72005-09-09 13:03:32 -07001545 */
1546
Christoph Lametere3366012013-01-10 19:14:18 +00001547 kmalloc_caches[INDEX_AC] = create_kmalloc_cache("kmalloc-ac",
1548 kmalloc_size(INDEX_AC), ARCH_KMALLOC_FLAGS);
Christoph Lametere498be72005-09-09 13:03:32 -07001549
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001550 if (INDEX_AC != INDEX_NODE)
1551 kmalloc_caches[INDEX_NODE] =
1552 create_kmalloc_cache("kmalloc-node",
1553 kmalloc_size(INDEX_NODE), ARCH_KMALLOC_FLAGS);
Christoph Lametere498be72005-09-09 13:03:32 -07001554
Ingo Molnare0a42722006-06-23 02:03:46 -07001555 slab_early_init = 0;
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 /* 4) Replace the bootstrap head arrays */
1558 {
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001559 struct array_cache *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001560
Pekka Enberg83b519e2009-06-10 19:40:04 +03001561 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001562
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001563 memcpy(ptr, cpu_cache_get(kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001564 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001565 /*
1566 * Do not assume that spinlocks can be initialized via memcpy:
1567 */
1568 spin_lock_init(&ptr->lock);
1569
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001570 kmem_cache->array[smp_processor_id()] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001571
Pekka Enberg83b519e2009-06-10 19:40:04 +03001572 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001573
Christoph Lametere3366012013-01-10 19:14:18 +00001574 BUG_ON(cpu_cache_get(kmalloc_caches[INDEX_AC])
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001575 != &initarray_generic.cache);
Christoph Lametere3366012013-01-10 19:14:18 +00001576 memcpy(ptr, cpu_cache_get(kmalloc_caches[INDEX_AC]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001577 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001578 /*
1579 * Do not assume that spinlocks can be initialized via memcpy:
1580 */
1581 spin_lock_init(&ptr->lock);
1582
Christoph Lametere3366012013-01-10 19:14:18 +00001583 kmalloc_caches[INDEX_AC]->array[smp_processor_id()] = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001585 /* 5) Replace the bootstrap kmem_cache_node */
Christoph Lametere498be72005-09-09 13:03:32 -07001586 {
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001587 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Mel Gorman9c09a952008-01-24 05:49:54 -08001589 for_each_online_node(nid) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001590 init_list(kmem_cache, &init_kmem_cache_node[CACHE_CACHE + nid], nid);
Pekka Enberg556a1692008-01-25 08:20:51 +02001591
Christoph Lametere3366012013-01-10 19:14:18 +00001592 init_list(kmalloc_caches[INDEX_AC],
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001593 &init_kmem_cache_node[SIZE_AC + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001594
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001595 if (INDEX_AC != INDEX_NODE) {
1596 init_list(kmalloc_caches[INDEX_NODE],
1597 &init_kmem_cache_node[SIZE_NODE + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001598 }
1599 }
1600 }
1601
Christoph Lameterf97d5f62013-01-10 19:12:17 +00001602 create_kmalloc_caches(ARCH_KMALLOC_FLAGS);
Pekka Enberg8429db52009-06-12 15:58:59 +03001603}
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001604
Pekka Enberg8429db52009-06-12 15:58:59 +03001605void __init kmem_cache_init_late(void)
1606{
1607 struct kmem_cache *cachep;
1608
Christoph Lameter97d06602012-07-06 15:25:11 -05001609 slab_state = UP;
Peter Zijlstra52cef182011-11-28 21:12:40 +01001610
Pekka Enberg8429db52009-06-12 15:58:59 +03001611 /* 6) resize the head arrays to their final sizes */
Christoph Lameter18004c52012-07-06 15:25:12 -05001612 mutex_lock(&slab_mutex);
1613 list_for_each_entry(cachep, &slab_caches, list)
Pekka Enberg8429db52009-06-12 15:58:59 +03001614 if (enable_cpucache(cachep, GFP_NOWAIT))
1615 BUG();
Christoph Lameter18004c52012-07-06 15:25:12 -05001616 mutex_unlock(&slab_mutex);
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001617
Michael Wang947ca182012-09-05 10:33:18 +08001618 /* Annotate slab for lockdep -- annotate the malloc caches */
1619 init_lock_keys();
1620
Christoph Lameter97d06602012-07-06 15:25:11 -05001621 /* Done! */
1622 slab_state = FULL;
1623
Andrew Mortona737b3e2006-03-22 00:08:11 -08001624 /*
1625 * Register a cpu startup notifier callback that initializes
1626 * cpu_cache_get for all new cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 */
1628 register_cpu_notifier(&cpucache_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
David Rientjes8f9f8d92010-03-27 19:40:47 -07001630#ifdef CONFIG_NUMA
1631 /*
1632 * Register a memory hotplug callback that initializes and frees
Christoph Lameter6a673682013-01-10 19:14:19 +00001633 * node.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001634 */
1635 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
1636#endif
1637
Andrew Mortona737b3e2006-03-22 00:08:11 -08001638 /*
1639 * The reap timers are started later, with a module init call: That part
1640 * of the kernel is not yet operational.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 */
1642}
1643
1644static int __init cpucache_init(void)
1645{
1646 int cpu;
1647
Andrew Mortona737b3e2006-03-22 00:08:11 -08001648 /*
1649 * Register the timers that return unneeded pages to the page allocator
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 */
Christoph Lametere498be72005-09-09 13:03:32 -07001651 for_each_online_cpu(cpu)
Andrew Mortona737b3e2006-03-22 00:08:11 -08001652 start_cpu_timer(cpu);
Glauber Costaa164f8962012-06-21 00:59:18 +04001653
1654 /* Done! */
Christoph Lameter97d06602012-07-06 15:25:11 -05001655 slab_state = FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 return 0;
1657}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658__initcall(cpucache_init);
1659
Rafael Aquini8bdec192012-03-09 17:27:27 -03001660static noinline void
1661slab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid)
1662{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001663 struct kmem_cache_node *n;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001664 struct slab *slabp;
1665 unsigned long flags;
1666 int node;
1667
1668 printk(KERN_WARNING
1669 "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n",
1670 nodeid, gfpflags);
1671 printk(KERN_WARNING " cache: %s, object size: %d, order: %d\n",
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05001672 cachep->name, cachep->size, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001673
1674 for_each_online_node(node) {
1675 unsigned long active_objs = 0, num_objs = 0, free_objects = 0;
1676 unsigned long active_slabs = 0, num_slabs = 0;
1677
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001678 n = cachep->node[node];
1679 if (!n)
Rafael Aquini8bdec192012-03-09 17:27:27 -03001680 continue;
1681
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001682 spin_lock_irqsave(&n->list_lock, flags);
1683 list_for_each_entry(slabp, &n->slabs_full, list) {
Rafael Aquini8bdec192012-03-09 17:27:27 -03001684 active_objs += cachep->num;
1685 active_slabs++;
1686 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001687 list_for_each_entry(slabp, &n->slabs_partial, list) {
Rafael Aquini8bdec192012-03-09 17:27:27 -03001688 active_objs += slabp->inuse;
1689 active_slabs++;
1690 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001691 list_for_each_entry(slabp, &n->slabs_free, list)
Rafael Aquini8bdec192012-03-09 17:27:27 -03001692 num_slabs++;
1693
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001694 free_objects += n->free_objects;
1695 spin_unlock_irqrestore(&n->list_lock, flags);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001696
1697 num_slabs += active_slabs;
1698 num_objs = num_slabs * cachep->num;
1699 printk(KERN_WARNING
1700 " node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
1701 node, active_slabs, num_slabs, active_objs, num_objs,
1702 free_objects);
1703 }
1704}
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706/*
1707 * Interface to system's page allocator. No need to hold the cache-lock.
1708 *
1709 * If we requested dmaable memory, we will get it. Even if we
1710 * did not request dmaable memory, we might get it, but that
1711 * would be relatively rare and ignorable.
1712 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001713static struct page *kmem_getpages(struct kmem_cache *cachep, gfp_t flags,
1714 int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715{
1716 struct page *page;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001717 int nr_pages;
Christoph Lameter765c4502006-09-27 01:50:08 -07001718
Glauber Costaa618e892012-06-14 16:17:21 +04001719 flags |= cachep->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001720 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1721 flags |= __GFP_RECLAIMABLE;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001722
Linus Torvalds517d0862009-06-16 19:50:13 -07001723 page = alloc_pages_exact_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001724 if (!page) {
1725 if (!(flags & __GFP_NOWARN) && printk_ratelimit())
1726 slab_out_of_memory(cachep, flags, nodeid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return NULL;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001728 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
Mel Gormanb37f1dd2012-07-31 16:44:03 -07001730 /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
Mel Gorman072bb0a2012-07-31 16:43:58 -07001731 if (unlikely(page->pfmemalloc))
1732 pfmemalloc_active = true;
1733
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001734 nr_pages = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
Christoph Lameter972d1a72006-09-25 23:31:51 -07001736 add_zone_page_state(page_zone(page),
1737 NR_SLAB_RECLAIMABLE, nr_pages);
1738 else
1739 add_zone_page_state(page_zone(page),
1740 NR_SLAB_UNRECLAIMABLE, nr_pages);
Joonsoo Kima57a4982013-10-24 10:07:44 +09001741 __SetPageSlab(page);
1742 if (page->pfmemalloc)
1743 SetPageSlabPfmemalloc(page);
Glauber Costa1f458cb2012-12-18 14:22:50 -08001744 memcg_bind_pages(cachep, cachep->gfporder);
Mel Gorman072bb0a2012-07-31 16:43:58 -07001745
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001746 if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
1747 kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
1748
1749 if (cachep->ctor)
1750 kmemcheck_mark_uninitialized_pages(page, nr_pages);
1751 else
1752 kmemcheck_mark_unallocated_pages(page, nr_pages);
1753 }
Pekka Enbergc175eea2008-05-09 20:35:53 +02001754
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001755 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756}
1757
1758/*
1759 * Interface to system's page release.
1760 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001761static void kmem_freepages(struct kmem_cache *cachep, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762{
Joonsoo Kima57a4982013-10-24 10:07:44 +09001763 const unsigned long nr_freed = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001765 kmemcheck_free_shadow(page, cachep->gfporder);
Pekka Enbergc175eea2008-05-09 20:35:53 +02001766
Christoph Lameter972d1a72006-09-25 23:31:51 -07001767 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1768 sub_zone_page_state(page_zone(page),
1769 NR_SLAB_RECLAIMABLE, nr_freed);
1770 else
1771 sub_zone_page_state(page_zone(page),
1772 NR_SLAB_UNRECLAIMABLE, nr_freed);
Joonsoo Kim73293c22013-10-24 10:07:37 +09001773
Joonsoo Kima57a4982013-10-24 10:07:44 +09001774 BUG_ON(!PageSlab(page));
Joonsoo Kim73293c22013-10-24 10:07:37 +09001775 __ClearPageSlabPfmemalloc(page);
Joonsoo Kima57a4982013-10-24 10:07:44 +09001776 __ClearPageSlab(page);
Glauber Costa1f458cb2012-12-18 14:22:50 -08001777
1778 memcg_release_pages(cachep, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 if (current->reclaim_state)
1780 current->reclaim_state->reclaimed_slab += nr_freed;
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001781 __free_memcg_kmem_pages(page, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782}
1783
1784static void kmem_rcu_free(struct rcu_head *head)
1785{
Joonsoo Kim68126702013-10-24 10:07:42 +09001786 struct kmem_cache *cachep;
1787 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Joonsoo Kim68126702013-10-24 10:07:42 +09001789 page = container_of(head, struct page, rcu_head);
1790 cachep = page->slab_cache;
1791
1792 kmem_freepages(cachep, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793}
1794
1795#if DEBUG
1796
1797#ifdef CONFIG_DEBUG_PAGEALLOC
Pekka Enberg343e0d72006-02-01 03:05:50 -08001798static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001799 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001801 int size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001803 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001805 if (size < 5 * sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 return;
1807
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001808 *addr++ = 0x12345678;
1809 *addr++ = caller;
1810 *addr++ = smp_processor_id();
1811 size -= 3 * sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 {
1813 unsigned long *sptr = &caller;
1814 unsigned long svalue;
1815
1816 while (!kstack_end(sptr)) {
1817 svalue = *sptr++;
1818 if (kernel_text_address(svalue)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001819 *addr++ = svalue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 size -= sizeof(unsigned long);
1821 if (size <= sizeof(unsigned long))
1822 break;
1823 }
1824 }
1825
1826 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001827 *addr++ = 0x87654321;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829#endif
1830
Pekka Enberg343e0d72006-02-01 03:05:50 -08001831static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001833 int size = cachep->object_size;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001834 addr = &((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836 memset(addr, val, size);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001837 *(unsigned char *)(addr + size - 1) = POISON_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838}
1839
1840static void dump_line(char *data, int offset, int limit)
1841{
1842 int i;
Dave Jonesaa83aa42006-09-29 01:59:51 -07001843 unsigned char error = 0;
1844 int bad_count = 0;
1845
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02001846 printk(KERN_ERR "%03x: ", offset);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001847 for (i = 0; i < limit; i++) {
1848 if (data[offset + i] != POISON_FREE) {
1849 error = data[offset + i];
1850 bad_count++;
1851 }
Dave Jonesaa83aa42006-09-29 01:59:51 -07001852 }
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02001853 print_hex_dump(KERN_CONT, "", 0, 16, 1,
1854 &data[offset], limit, 1);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001855
1856 if (bad_count == 1) {
1857 error ^= POISON_FREE;
1858 if (!(error & (error - 1))) {
1859 printk(KERN_ERR "Single bit error detected. Probably "
1860 "bad RAM.\n");
1861#ifdef CONFIG_X86
1862 printk(KERN_ERR "Run memtest86+ or a similar memory "
1863 "test tool.\n");
1864#else
1865 printk(KERN_ERR "Run a memory test tool.\n");
1866#endif
1867 }
1868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869}
1870#endif
1871
1872#if DEBUG
1873
Pekka Enberg343e0d72006-02-01 03:05:50 -08001874static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
1876 int i, size;
1877 char *realobj;
1878
1879 if (cachep->flags & SLAB_RED_ZONE) {
David Woodhouseb46b8f12007-05-08 00:22:59 -07001880 printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001881 *dbg_redzone1(cachep, objp),
1882 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 }
1884
1885 if (cachep->flags & SLAB_STORE_USER) {
Joe Perches071361d2012-12-12 10:19:12 -08001886 printk(KERN_ERR "Last user: [<%p>](%pSR)\n",
1887 *dbg_userword(cachep, objp),
1888 *dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 }
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001890 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001891 size = cachep->object_size;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001892 for (i = 0; i < size && lines; i += 16, lines--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 int limit;
1894 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001895 if (i + limit > size)
1896 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 dump_line(realobj, i, limit);
1898 }
1899}
1900
Pekka Enberg343e0d72006-02-01 03:05:50 -08001901static void check_poison_obj(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
1903 char *realobj;
1904 int size, i;
1905 int lines = 0;
1906
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001907 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001908 size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001910 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 char exp = POISON_FREE;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001912 if (i == size - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 exp = POISON_END;
1914 if (realobj[i] != exp) {
1915 int limit;
1916 /* Mismatch ! */
1917 /* Print header */
1918 if (lines == 0) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001919 printk(KERN_ERR
Dave Jonesface37f2011-11-15 15:03:52 -08001920 "Slab corruption (%s): %s start=%p, len=%d\n",
1921 print_tainted(), cachep->name, realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 print_objinfo(cachep, objp, 0);
1923 }
1924 /* Hexdump the affected line */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001925 i = (i / 16) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001927 if (i + limit > size)
1928 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 dump_line(realobj, i, limit);
1930 i += 16;
1931 lines++;
1932 /* Limit to 5 lines */
1933 if (lines > 5)
1934 break;
1935 }
1936 }
1937 if (lines != 0) {
1938 /* Print some data about the neighboring objects, if they
1939 * exist:
1940 */
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08001941 struct slab *slabp = virt_to_slab(objp);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001942 unsigned int objnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001944 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (objnr) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001946 objp = index_to_obj(cachep, slabp, objnr - 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001947 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001949 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 print_objinfo(cachep, objp, 2);
1951 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001952 if (objnr + 1 < cachep->num) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001953 objp = index_to_obj(cachep, slabp, objnr + 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001954 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001956 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 print_objinfo(cachep, objp, 2);
1958 }
1959 }
1960}
1961#endif
1962
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963#if DEBUG
Rabin Vincente79aec22008-07-04 00:40:32 +05301964static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001965{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 int i;
1967 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001968 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 if (cachep->flags & SLAB_POISON) {
1971#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05001972 if (cachep->size % PAGE_SIZE == 0 &&
Andrew Mortona737b3e2006-03-22 00:08:11 -08001973 OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001974 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05001975 cachep->size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 else
1977 check_poison_obj(cachep, objp);
1978#else
1979 check_poison_obj(cachep, objp);
1980#endif
1981 }
1982 if (cachep->flags & SLAB_RED_ZONE) {
1983 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
1984 slab_error(cachep, "start of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001985 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
1987 slab_error(cachep, "end of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001988 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001991}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992#else
Rabin Vincente79aec22008-07-04 00:40:32 +05301993static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001994{
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001995}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996#endif
1997
Randy Dunlap911851e2006-03-22 00:08:14 -08001998/**
1999 * slab_destroy - destroy and release all objects in a slab
2000 * @cachep: cache pointer being destroyed
2001 * @slabp: slab pointer being destroyed
2002 *
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002003 * Destroy all the objs in a slab, and release the mem back to the system.
Andrew Mortona737b3e2006-03-22 00:08:11 -08002004 * Before calling the slab must have been unlinked from the cache. The
2005 * cache-lock is not held/needed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002006 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002007static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002008{
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002009 struct page *page = virt_to_head_page(slabp->s_mem);
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002010
Rabin Vincente79aec22008-07-04 00:40:32 +05302011 slab_destroy_debugcheck(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
Joonsoo Kim68126702013-10-24 10:07:42 +09002013 struct rcu_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014
Joonsoo Kim68126702013-10-24 10:07:42 +09002015 /*
2016 * RCU free overloads the RCU head over the LRU.
2017 * slab_page has been overloeaded over the LRU,
2018 * however it is not used from now on so that
2019 * we can use it safely.
2020 */
2021 head = (void *)&page->rcu_head;
2022 call_rcu(head, kmem_rcu_free);
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 } else {
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002025 kmem_freepages(cachep, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 }
Joonsoo Kim68126702013-10-24 10:07:42 +09002027
2028 /*
2029 * From now on, we don't use slab management
2030 * although actual page can be freed in rcu context
2031 */
2032 if (OFF_SLAB(cachep))
2033 kmem_cache_free(cachep->slabp_cache, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034}
2035
2036/**
Randy.Dunlapa70773d2006-02-01 03:05:52 -08002037 * calculate_slab_order - calculate size (page order) of slabs
2038 * @cachep: pointer to the cache that is being created
2039 * @size: size of objects to be created in this cache.
2040 * @align: required alignment for the objects.
2041 * @flags: slab allocation flags
2042 *
2043 * Also calculates the number of objects per slab.
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002044 *
2045 * This could be made much more intelligent. For now, try to avoid using
2046 * high order pages for slabs. When the gfp() functions are more friendly
2047 * towards high-order requests, this should be changed.
2048 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002049static size_t calculate_slab_order(struct kmem_cache *cachep,
Randy Dunlapee13d782006-02-01 03:05:53 -08002050 size_t size, size_t align, unsigned long flags)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002051{
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002052 unsigned long offslab_limit;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002053 size_t left_over = 0;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002054 int gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002055
Christoph Lameter0aa817f2007-05-16 22:11:01 -07002056 for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002057 unsigned int num;
2058 size_t remainder;
2059
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002060 cache_estimate(gfporder, size, align, flags, &remainder, &num);
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002061 if (!num)
2062 continue;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002063
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002064 if (flags & CFLGS_OFF_SLAB) {
2065 /*
2066 * Max number of objs-per-slab for caches which
2067 * use off-slab slabs. Needed to avoid a possible
2068 * looping condition in cache_grow().
2069 */
2070 offslab_limit = size - sizeof(struct slab);
2071 offslab_limit /= sizeof(kmem_bufctl_t);
2072
2073 if (num > offslab_limit)
2074 break;
2075 }
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002076
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002077 /* Found something acceptable - save it away */
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002078 cachep->num = num;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002079 cachep->gfporder = gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002080 left_over = remainder;
2081
2082 /*
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08002083 * A VFS-reclaimable slab tends to have most allocations
2084 * as GFP_NOFS and we really don't want to have to be allocating
2085 * higher-order pages when we are unable to shrink dcache.
2086 */
2087 if (flags & SLAB_RECLAIM_ACCOUNT)
2088 break;
2089
2090 /*
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002091 * Large number of objects is good, but very large slabs are
2092 * currently bad for the gfp()s.
2093 */
David Rientjes543585c2011-10-18 22:09:24 -07002094 if (gfporder >= slab_max_order)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002095 break;
2096
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002097 /*
2098 * Acceptable internal fragmentation?
2099 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002100 if (left_over * 8 <= (PAGE_SIZE << gfporder))
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002101 break;
2102 }
2103 return left_over;
2104}
2105
Pekka Enberg83b519e2009-06-10 19:40:04 +03002106static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002107{
Christoph Lameter97d06602012-07-06 15:25:11 -05002108 if (slab_state >= FULL)
Pekka Enberg83b519e2009-06-10 19:40:04 +03002109 return enable_cpucache(cachep, gfp);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002110
Christoph Lameter97d06602012-07-06 15:25:11 -05002111 if (slab_state == DOWN) {
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002112 /*
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002113 * Note: Creation of first cache (kmem_cache).
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002114 * The setup_node is taken care
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002115 * of by the caller of __kmem_cache_create
2116 */
2117 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2118 slab_state = PARTIAL;
2119 } else if (slab_state == PARTIAL) {
2120 /*
2121 * Note: the second kmem_cache_create must create the cache
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002122 * that's used by kmalloc(24), otherwise the creation of
2123 * further caches will BUG().
2124 */
2125 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2126
2127 /*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002128 * If the cache that's used by kmalloc(sizeof(kmem_cache_node)) is
2129 * the second cache, then we need to set up all its node/,
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002130 * otherwise the creation of further caches will BUG().
2131 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002132 set_up_node(cachep, SIZE_AC);
2133 if (INDEX_AC == INDEX_NODE)
2134 slab_state = PARTIAL_NODE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002135 else
Christoph Lameter97d06602012-07-06 15:25:11 -05002136 slab_state = PARTIAL_ARRAYCACHE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002137 } else {
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002138 /* Remaining boot caches */
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002139 cachep->array[smp_processor_id()] =
Pekka Enberg83b519e2009-06-10 19:40:04 +03002140 kmalloc(sizeof(struct arraycache_init), gfp);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002141
Christoph Lameter97d06602012-07-06 15:25:11 -05002142 if (slab_state == PARTIAL_ARRAYCACHE) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002143 set_up_node(cachep, SIZE_NODE);
2144 slab_state = PARTIAL_NODE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002145 } else {
2146 int node;
Pekka Enberg556a1692008-01-25 08:20:51 +02002147 for_each_online_node(node) {
Christoph Lameter6a673682013-01-10 19:14:19 +00002148 cachep->node[node] =
Christoph Lameter6744f082013-01-10 19:12:17 +00002149 kmalloc_node(sizeof(struct kmem_cache_node),
Pekka Enbergeb91f1d2009-06-12 14:56:09 +03002150 gfp, node);
Christoph Lameter6a673682013-01-10 19:14:19 +00002151 BUG_ON(!cachep->node[node]);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002152 kmem_cache_node_init(cachep->node[node]);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002153 }
2154 }
2155 }
Christoph Lameter6a673682013-01-10 19:14:19 +00002156 cachep->node[numa_mem_id()]->next_reap =
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002157 jiffies + REAPTIMEOUT_LIST3 +
2158 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
2159
2160 cpu_cache_get(cachep)->avail = 0;
2161 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
2162 cpu_cache_get(cachep)->batchcount = 1;
2163 cpu_cache_get(cachep)->touched = 0;
2164 cachep->batchcount = 1;
2165 cachep->limit = BOOT_CPUCACHE_ENTRIES;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002166 return 0;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002167}
2168
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002169/**
Christoph Lameter039363f2012-07-06 15:25:10 -05002170 * __kmem_cache_create - Create a cache.
Randy Dunlapa755b762012-11-06 17:10:10 -08002171 * @cachep: cache management descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 * @flags: SLAB flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 *
2174 * Returns a ptr to the cache on success, NULL on failure.
2175 * Cannot be called within a int, but can be interrupted.
Paul Mundt20c2df82007-07-20 10:11:58 +09002176 * The @ctor is run when new pages are allocated by the cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 * The flags are
2179 *
2180 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2181 * to catch references to uninitialised memory.
2182 *
2183 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2184 * for buffer overruns.
2185 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2187 * cacheline. This can be beneficial if you're counting cycles as closely
2188 * as davem.
2189 */
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002190int
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002191__kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192{
2193 size_t left_over, slab_size, ralign;
Pekka Enberg83b519e2009-06-10 19:40:04 +03002194 gfp_t gfp;
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002195 int err;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002196 size_t size = cachep->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199#if FORCED_DEBUG
2200 /*
2201 * Enable redzoning and last user accounting, except for caches with
2202 * large objects, if the increased size would increase the object size
2203 * above the next power of two: caches with object sizes just above a
2204 * power of two have a significant amount of internal fragmentation.
2205 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002206 if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
2207 2 * sizeof(unsigned long long)))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002208 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (!(flags & SLAB_DESTROY_BY_RCU))
2210 flags |= SLAB_POISON;
2211#endif
2212 if (flags & SLAB_DESTROY_BY_RCU)
2213 BUG_ON(flags & SLAB_POISON);
2214#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
Andrew Mortona737b3e2006-03-22 00:08:11 -08002216 /*
2217 * Check that size is in terms of words. This is needed to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 * unaligned accesses for some archs when redzoning is used, and makes
2219 * sure any on-slab bufctl's are also correctly aligned.
2220 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002221 if (size & (BYTES_PER_WORD - 1)) {
2222 size += (BYTES_PER_WORD - 1);
2223 size &= ~(BYTES_PER_WORD - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
2225
Pekka Enbergca5f9702006-09-25 23:31:25 -07002226 /*
David Woodhouse87a927c2007-07-04 21:26:44 -04002227 * Redzoning and user store require word alignment or possibly larger.
2228 * Note this will be overridden by architecture or caller mandated
2229 * alignment if either is greater than BYTES_PER_WORD.
Pekka Enbergca5f9702006-09-25 23:31:25 -07002230 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002231 if (flags & SLAB_STORE_USER)
2232 ralign = BYTES_PER_WORD;
2233
2234 if (flags & SLAB_RED_ZONE) {
2235 ralign = REDZONE_ALIGN;
2236 /* If redzoning, ensure that the second redzone is suitably
2237 * aligned, by adjusting the object size accordingly. */
2238 size += REDZONE_ALIGN - 1;
2239 size &= ~(REDZONE_ALIGN - 1);
2240 }
Pekka Enbergca5f9702006-09-25 23:31:25 -07002241
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002242 /* 3) caller mandated alignment */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002243 if (ralign < cachep->align) {
2244 ralign = cachep->align;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 }
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002246 /* disable debug if necessary */
2247 if (ralign > __alignof__(unsigned long long))
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002248 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002249 /*
Pekka Enbergca5f9702006-09-25 23:31:25 -07002250 * 4) Store it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002252 cachep->align = ralign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Pekka Enberg83b519e2009-06-10 19:40:04 +03002254 if (slab_is_available())
2255 gfp = GFP_KERNEL;
2256 else
2257 gfp = GFP_NOWAIT;
2258
Christoph Lameter6a673682013-01-10 19:14:19 +00002259 setup_node_pointer(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Pekka Enbergca5f9702006-09-25 23:31:25 -07002262 /*
2263 * Both debugging options require word-alignment which is calculated
2264 * into align above.
2265 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 if (flags & SLAB_RED_ZONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 /* add space for red zone words */
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002268 cachep->obj_offset += sizeof(unsigned long long);
2269 size += 2 * sizeof(unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 }
2271 if (flags & SLAB_STORE_USER) {
Pekka Enbergca5f9702006-09-25 23:31:25 -07002272 /* user store requires one word storage behind the end of
David Woodhouse87a927c2007-07-04 21:26:44 -04002273 * the real object. But if the second red zone needs to be
2274 * aligned to 64 bits, we must allow that much space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002276 if (flags & SLAB_RED_ZONE)
2277 size += REDZONE_ALIGN;
2278 else
2279 size += BYTES_PER_WORD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 }
2281#if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002282 if (size >= kmalloc_size(INDEX_NODE + 1)
Tetsuo Handa608da7e2012-09-30 17:28:25 +09002283 && cachep->object_size > cache_line_size()
2284 && ALIGN(size, cachep->align) < PAGE_SIZE) {
2285 cachep->obj_offset += PAGE_SIZE - ALIGN(size, cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 size = PAGE_SIZE;
2287 }
2288#endif
2289#endif
2290
Ingo Molnare0a42722006-06-23 02:03:46 -07002291 /*
2292 * Determine if the slab management is 'on' or 'off' slab.
2293 * (bootstrapping cannot cope with offslab caches so don't do
Catalin Marinase7cb55b2009-10-28 13:33:08 +00002294 * it too early on. Always use on-slab management when
2295 * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
Ingo Molnare0a42722006-06-23 02:03:46 -07002296 */
Catalin Marinase7cb55b2009-10-28 13:33:08 +00002297 if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init &&
2298 !(flags & SLAB_NOLEAKTRACE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 /*
2300 * Size is large, assume best to place the slab management obj
2301 * off-slab (should allow better packing of objs).
2302 */
2303 flags |= CFLGS_OFF_SLAB;
2304
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002305 size = ALIGN(size, cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002307 left_over = calculate_slab_order(cachep, size, cachep->align, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002309 if (!cachep->num)
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002310 return -E2BIG;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002311
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002312 slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002313 + sizeof(struct slab), cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 /*
2316 * If the slab has been placed off-slab, and we have enough space then
2317 * move it on-slab. This is at the expense of any extra colouring.
2318 */
2319 if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
2320 flags &= ~CFLGS_OFF_SLAB;
2321 left_over -= slab_size;
2322 }
2323
2324 if (flags & CFLGS_OFF_SLAB) {
2325 /* really off slab. No need for manual alignment */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002326 slab_size =
2327 cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
Ron Lee67461362009-05-22 04:58:22 +09302328
2329#ifdef CONFIG_PAGE_POISONING
2330 /* If we're going to use the generic kernel_map_pages()
2331 * poisoning, then it's going to smash the contents of
2332 * the redzone and userword anyhow, so switch them off.
2333 */
2334 if (size % PAGE_SIZE == 0 && flags & SLAB_POISON)
2335 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
2336#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 }
2338
2339 cachep->colour_off = cache_line_size();
2340 /* Offset must be a multiple of the alignment. */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002341 if (cachep->colour_off < cachep->align)
2342 cachep->colour_off = cachep->align;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002343 cachep->colour = left_over / cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 cachep->slab_size = slab_size;
2345 cachep->flags = flags;
Joonsoo Kima57a4982013-10-24 10:07:44 +09002346 cachep->allocflags = __GFP_COMP;
Christoph Lameter4b51d662007-02-10 01:43:10 -08002347 if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
Glauber Costaa618e892012-06-14 16:17:21 +04002348 cachep->allocflags |= GFP_DMA;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002349 cachep->size = size;
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08002350 cachep->reciprocal_buffer_size = reciprocal_value(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002352 if (flags & CFLGS_OFF_SLAB) {
Christoph Lameter2c59dd62013-01-10 19:14:19 +00002353 cachep->slabp_cache = kmalloc_slab(slab_size, 0u);
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002354 /*
2355 * This is a possibility for one of the malloc_sizes caches.
2356 * But since we go off slab only for object size greater than
2357 * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
2358 * this should not happen at all.
2359 * But leave a BUG_ON for some lucky dude.
2360 */
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002361 BUG_ON(ZERO_OR_NULL_PTR(cachep->slabp_cache));
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002364 err = setup_cpu_cache(cachep, gfp);
2365 if (err) {
Christoph Lameter12c36672012-09-04 23:38:33 +00002366 __kmem_cache_shutdown(cachep);
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002367 return err;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002368 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369
Peter Zijlstra83835b32011-07-22 15:26:05 +02002370 if (flags & SLAB_DEBUG_OBJECTS) {
2371 /*
2372 * Would deadlock through slab_destroy()->call_rcu()->
2373 * debug_object_activate()->kmem_cache_alloc().
2374 */
2375 WARN_ON_ONCE(flags & SLAB_DESTROY_BY_RCU);
2376
2377 slab_set_debugobj_lock_classes(cachep);
Glauber Costa6ccfb5b2012-12-18 14:22:31 -08002378 } else if (!OFF_SLAB(cachep) && !(flags & SLAB_DESTROY_BY_RCU))
2379 on_slab_lock_classes(cachep);
Peter Zijlstra83835b32011-07-22 15:26:05 +02002380
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002381 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
2384#if DEBUG
2385static void check_irq_off(void)
2386{
2387 BUG_ON(!irqs_disabled());
2388}
2389
2390static void check_irq_on(void)
2391{
2392 BUG_ON(irqs_disabled());
2393}
2394
Pekka Enberg343e0d72006-02-01 03:05:50 -08002395static void check_spinlock_acquired(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
2397#ifdef CONFIG_SMP
2398 check_irq_off();
Christoph Lameter6a673682013-01-10 19:14:19 +00002399 assert_spin_locked(&cachep->node[numa_mem_id()]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400#endif
2401}
Christoph Lametere498be72005-09-09 13:03:32 -07002402
Pekka Enberg343e0d72006-02-01 03:05:50 -08002403static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07002404{
2405#ifdef CONFIG_SMP
2406 check_irq_off();
Christoph Lameter6a673682013-01-10 19:14:19 +00002407 assert_spin_locked(&cachep->node[node]->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002408#endif
2409}
2410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411#else
2412#define check_irq_off() do { } while(0)
2413#define check_irq_on() do { } while(0)
2414#define check_spinlock_acquired(x) do { } while(0)
Christoph Lametere498be72005-09-09 13:03:32 -07002415#define check_spinlock_acquired_node(x, y) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416#endif
2417
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002418static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
Christoph Lameteraab22072006-03-22 00:09:06 -08002419 struct array_cache *ac,
2420 int force, int node);
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422static void do_drain(void *arg)
2423{
Andrew Mortona737b3e2006-03-22 00:08:11 -08002424 struct kmem_cache *cachep = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 struct array_cache *ac;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07002426 int node = numa_mem_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002429 ac = cpu_cache_get(cachep);
Christoph Lameter6a673682013-01-10 19:14:19 +00002430 spin_lock(&cachep->node[node]->list_lock);
Christoph Lameterff694162005-09-22 21:44:02 -07002431 free_block(cachep, ac->entry, ac->avail, node);
Christoph Lameter6a673682013-01-10 19:14:19 +00002432 spin_unlock(&cachep->node[node]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 ac->avail = 0;
2434}
2435
Pekka Enberg343e0d72006-02-01 03:05:50 -08002436static void drain_cpu_caches(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002438 struct kmem_cache_node *n;
Christoph Lametere498be72005-09-09 13:03:32 -07002439 int node;
2440
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002441 on_each_cpu(do_drain, cachep, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 check_irq_on();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002443 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002444 n = cachep->node[node];
2445 if (n && n->alien)
2446 drain_alien_cache(cachep, n->alien);
Roland Dreiera4523a82006-05-15 11:41:00 -07002447 }
2448
2449 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002450 n = cachep->node[node];
2451 if (n)
2452 drain_array(cachep, n, n->shared, 1, node);
Christoph Lametere498be72005-09-09 13:03:32 -07002453 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454}
2455
Christoph Lametered11d9e2006-06-30 01:55:45 -07002456/*
2457 * Remove slabs from the list of free slabs.
2458 * Specify the number of slabs to drain in tofree.
2459 *
2460 * Returns the actual number of slabs released.
2461 */
2462static int drain_freelist(struct kmem_cache *cache,
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002463 struct kmem_cache_node *n, int tofree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464{
Christoph Lametered11d9e2006-06-30 01:55:45 -07002465 struct list_head *p;
2466 int nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468
Christoph Lametered11d9e2006-06-30 01:55:45 -07002469 nr_freed = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002470 while (nr_freed < tofree && !list_empty(&n->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002472 spin_lock_irq(&n->list_lock);
2473 p = n->slabs_free.prev;
2474 if (p == &n->slabs_free) {
2475 spin_unlock_irq(&n->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002476 goto out;
2477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Christoph Lametered11d9e2006-06-30 01:55:45 -07002479 slabp = list_entry(p, struct slab, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480#if DEBUG
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002481 BUG_ON(slabp->inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482#endif
2483 list_del(&slabp->list);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002484 /*
2485 * Safe to drop the lock. The slab is no longer linked
2486 * to the cache.
2487 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002488 n->free_objects -= cache->num;
2489 spin_unlock_irq(&n->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002490 slab_destroy(cache, slabp);
2491 nr_freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 }
Christoph Lametered11d9e2006-06-30 01:55:45 -07002493out:
2494 return nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495}
2496
Christoph Lameter18004c52012-07-06 15:25:12 -05002497/* Called with slab_mutex held to protect against cpu hotplug */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002498static int __cache_shrink(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07002499{
2500 int ret = 0, i = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002501 struct kmem_cache_node *n;
Christoph Lametere498be72005-09-09 13:03:32 -07002502
2503 drain_cpu_caches(cachep);
2504
2505 check_irq_on();
2506 for_each_online_node(i) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002507 n = cachep->node[i];
2508 if (!n)
Christoph Lametered11d9e2006-06-30 01:55:45 -07002509 continue;
2510
Wanpeng Li0fa81032013-07-04 08:33:22 +08002511 drain_freelist(cachep, n, slabs_tofree(cachep, n));
Christoph Lametered11d9e2006-06-30 01:55:45 -07002512
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002513 ret += !list_empty(&n->slabs_full) ||
2514 !list_empty(&n->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07002515 }
2516 return (ret ? 1 : 0);
2517}
2518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519/**
2520 * kmem_cache_shrink - Shrink a cache.
2521 * @cachep: The cache to shrink.
2522 *
2523 * Releases as many slabs as possible for a cache.
2524 * To help debugging, a zero exit status indicates all slabs were released.
2525 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002526int kmem_cache_shrink(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527{
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002528 int ret;
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002529 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002531 get_online_cpus();
Christoph Lameter18004c52012-07-06 15:25:12 -05002532 mutex_lock(&slab_mutex);
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002533 ret = __cache_shrink(cachep);
Christoph Lameter18004c52012-07-06 15:25:12 -05002534 mutex_unlock(&slab_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002535 put_online_cpus();
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002536 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537}
2538EXPORT_SYMBOL(kmem_cache_shrink);
2539
Christoph Lameter945cf2b2012-09-04 23:18:33 +00002540int __kmem_cache_shutdown(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541{
Christoph Lameter12c36672012-09-04 23:38:33 +00002542 int i;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002543 struct kmem_cache_node *n;
Christoph Lameter12c36672012-09-04 23:38:33 +00002544 int rc = __cache_shrink(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545
Christoph Lameter12c36672012-09-04 23:38:33 +00002546 if (rc)
2547 return rc;
2548
2549 for_each_online_cpu(i)
2550 kfree(cachep->array[i]);
2551
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002552 /* NUMA: free the node structures */
Christoph Lameter12c36672012-09-04 23:38:33 +00002553 for_each_online_node(i) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002554 n = cachep->node[i];
2555 if (n) {
2556 kfree(n->shared);
2557 free_alien_cache(n->alien);
2558 kfree(n);
Christoph Lameter12c36672012-09-04 23:38:33 +00002559 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 }
Christoph Lameter12c36672012-09-04 23:38:33 +00002561 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002564/*
2565 * Get the memory for a slab management obj.
2566 * For a slab cache when the slab descriptor is off-slab, slab descriptors
2567 * always come from malloc_sizes caches. The slab descriptor cannot
2568 * come from the same cache which is getting created because,
2569 * when we are searching for an appropriate cache for these
2570 * descriptors in kmem_cache_create, we search through the malloc_sizes array.
2571 * If we are creating a malloc_sizes cache here it would not be visible to
2572 * kmem_find_general_cachep till the initialization is complete.
2573 * Hence we cannot have slabp_cache same as the original cache.
2574 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002575static struct slab *alloc_slabmgmt(struct kmem_cache *cachep,
2576 struct page *page, int colour_off,
2577 gfp_t local_flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578{
2579 struct slab *slabp;
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002580 void *addr = page_address(page);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 if (OFF_SLAB(cachep)) {
2583 /* Slab management obj is off-slab. */
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002584 slabp = kmem_cache_alloc_node(cachep->slabp_cache,
Pekka Enberg8759ec52008-11-26 10:01:31 +02002585 local_flags, nodeid);
Catalin Marinasd5cff632009-06-11 13:22:40 +01002586 /*
2587 * If the first object in the slab is leaked (it's allocated
2588 * but no one has a reference to it), we want to make sure
2589 * kmemleak does not treat the ->s_mem pointer as a reference
2590 * to the object. Otherwise we will not report the leak.
2591 */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002592 kmemleak_scan_area(&slabp->list, sizeof(struct list_head),
2593 local_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 if (!slabp)
2595 return NULL;
2596 } else {
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002597 slabp = addr + colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 colour_off += cachep->slab_size;
2599 }
2600 slabp->inuse = 0;
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002601 slabp->s_mem = addr + colour_off;
Marcin Slusarze51bfd02008-02-10 11:21:54 +01002602 slabp->free = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 return slabp;
2604}
2605
2606static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
2607{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002608 return (kmem_bufctl_t *) (slabp + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609}
2610
Pekka Enberg343e0d72006-02-01 03:05:50 -08002611static void cache_init_objs(struct kmem_cache *cachep,
Christoph Lametera35afb82007-05-16 22:10:57 -07002612 struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613{
2614 int i;
2615
2616 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002617 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618#if DEBUG
2619 /* need to poison the objs? */
2620 if (cachep->flags & SLAB_POISON)
2621 poison_obj(cachep, objp, POISON_FREE);
2622 if (cachep->flags & SLAB_STORE_USER)
2623 *dbg_userword(cachep, objp) = NULL;
2624
2625 if (cachep->flags & SLAB_RED_ZONE) {
2626 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2627 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2628 }
2629 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002630 * Constructors are not allowed to allocate memory from the same
2631 * cache which they are a constructor for. Otherwise, deadlock.
2632 * They must also be threaded.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 */
2634 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002635 cachep->ctor(objp + obj_offset(cachep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
2637 if (cachep->flags & SLAB_RED_ZONE) {
2638 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2639 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002640 " end of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2642 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002643 " start of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 }
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002645 if ((cachep->size % PAGE_SIZE) == 0 &&
Andrew Mortona737b3e2006-03-22 00:08:11 -08002646 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002647 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002648 cachep->size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649#else
2650 if (cachep->ctor)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002651 cachep->ctor(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652#endif
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002653 slab_bufctl(slabp)[i] = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655}
2656
Pekka Enberg343e0d72006-02-01 03:05:50 -08002657static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658{
Christoph Lameter4b51d662007-02-10 01:43:10 -08002659 if (CONFIG_ZONE_DMA_FLAG) {
2660 if (flags & GFP_DMA)
Glauber Costaa618e892012-06-14 16:17:21 +04002661 BUG_ON(!(cachep->allocflags & GFP_DMA));
Christoph Lameter4b51d662007-02-10 01:43:10 -08002662 else
Glauber Costaa618e892012-06-14 16:17:21 +04002663 BUG_ON(cachep->allocflags & GFP_DMA);
Christoph Lameter4b51d662007-02-10 01:43:10 -08002664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665}
2666
Andrew Mortona737b3e2006-03-22 00:08:11 -08002667static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
2668 int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002669{
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002670 void *objp;
Matthew Dobson78d382d2006-02-01 03:05:47 -08002671
2672 slabp->inuse++;
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002673 objp = index_to_obj(cachep, slabp, slab_bufctl(slabp)[slabp->free]);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002674#if DEBUG
Joonsoo Kim1ea991b2013-10-24 10:07:40 +09002675 WARN_ON(page_to_nid(virt_to_page(objp)) != nodeid);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002676#endif
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002677 slabp->free++;
Matthew Dobson78d382d2006-02-01 03:05:47 -08002678
2679 return objp;
2680}
2681
Andrew Mortona737b3e2006-03-22 00:08:11 -08002682static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
2683 void *objp, int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002684{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002685 unsigned int objnr = obj_to_index(cachep, slabp, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002686#if DEBUG
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002687 kmem_bufctl_t i;
2688
Matthew Dobson78d382d2006-02-01 03:05:47 -08002689 /* Verify that the slab belongs to the intended node */
Joonsoo Kim1ea991b2013-10-24 10:07:40 +09002690 WARN_ON(page_to_nid(virt_to_page(objp)) != nodeid);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002691
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002692 /* Verify double free bug */
2693 for (i = slabp->free; i < cachep->num; i++) {
2694 if (slab_bufctl(slabp)[i] == objnr) {
2695 printk(KERN_ERR "slab: double free detected in cache "
2696 "'%s', objp %p\n", cachep->name, objp);
2697 BUG();
2698 }
Matthew Dobson78d382d2006-02-01 03:05:47 -08002699 }
2700#endif
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002701 slabp->free--;
2702 slab_bufctl(slabp)[slabp->free] = objnr;
Matthew Dobson78d382d2006-02-01 03:05:47 -08002703 slabp->inuse--;
2704}
2705
Pekka Enberg47768742006-06-23 02:03:07 -07002706/*
2707 * Map pages beginning at addr to the given cache and slab. This is required
2708 * for the slab allocator to be able to lookup the cache and slab of a
Nick Pigginccd35fb2011-01-07 17:49:17 +11002709 * virtual address for kfree, ksize, and slab debugging.
Pekka Enberg47768742006-06-23 02:03:07 -07002710 */
2711static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002712 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713{
Joonsoo Kima57a4982013-10-24 10:07:44 +09002714 page->slab_cache = cache;
2715 page->slab_page = slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716}
2717
2718/*
2719 * Grow (by 1) the number of slabs within a cache. This is called by
2720 * kmem_cache_alloc() when there are no active objs left in a cache.
2721 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002722static int cache_grow(struct kmem_cache *cachep,
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002723 gfp_t flags, int nodeid, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002725 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002726 size_t offset;
2727 gfp_t local_flags;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002728 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729
Andrew Mortona737b3e2006-03-22 00:08:11 -08002730 /*
2731 * Be lazy and only check for valid flags here, keeping it out of the
2732 * critical path in kmem_cache_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 */
Christoph Lameter6cb06222007-10-16 01:25:41 -07002734 BUG_ON(flags & GFP_SLAB_BUG_MASK);
2735 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002737 /* Take the node list lock to change the colour_next on this node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002739 n = cachep->node[nodeid];
2740 spin_lock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
2742 /* Get colour for the slab, and cal the next value. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002743 offset = n->colour_next;
2744 n->colour_next++;
2745 if (n->colour_next >= cachep->colour)
2746 n->colour_next = 0;
2747 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002749 offset *= cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750
2751 if (local_flags & __GFP_WAIT)
2752 local_irq_enable();
2753
2754 /*
2755 * The test for missing atomic flag is performed here, rather than
2756 * the more obvious place, simply to reduce the critical path length
2757 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2758 * will eventually be caught here (where it matters).
2759 */
2760 kmem_flagcheck(cachep, flags);
2761
Andrew Mortona737b3e2006-03-22 00:08:11 -08002762 /*
2763 * Get mem for the objs. Attempt to allocate a physical page from
2764 * 'nodeid'.
Christoph Lametere498be72005-09-09 13:03:32 -07002765 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002766 if (!page)
2767 page = kmem_getpages(cachep, local_flags, nodeid);
2768 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 goto failed;
2770
2771 /* Get slab management. */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002772 slabp = alloc_slabmgmt(cachep, page, offset,
Christoph Lameter6cb06222007-10-16 01:25:41 -07002773 local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002774 if (!slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 goto opps1;
2776
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002777 slab_map_pages(cachep, slabp, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
Christoph Lametera35afb82007-05-16 22:10:57 -07002779 cache_init_objs(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
2781 if (local_flags & __GFP_WAIT)
2782 local_irq_disable();
2783 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002784 spin_lock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
2786 /* Make slab active. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002787 list_add_tail(&slabp->list, &(n->slabs_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 STATS_INC_GROWN(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002789 n->free_objects += cachep->num;
2790 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002792opps1:
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002793 kmem_freepages(cachep, page);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002794failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 if (local_flags & __GFP_WAIT)
2796 local_irq_disable();
2797 return 0;
2798}
2799
2800#if DEBUG
2801
2802/*
2803 * Perform extra freeing checks:
2804 * - detect bad pointers.
2805 * - POISON/RED_ZONE checking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 */
2807static void kfree_debugcheck(const void *objp)
2808{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 if (!virt_addr_valid(objp)) {
2810 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002811 (unsigned long)objp);
2812 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814}
2815
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002816static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2817{
David Woodhouseb46b8f12007-05-08 00:22:59 -07002818 unsigned long long redzone1, redzone2;
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002819
2820 redzone1 = *dbg_redzone1(cache, obj);
2821 redzone2 = *dbg_redzone2(cache, obj);
2822
2823 /*
2824 * Redzone is ok.
2825 */
2826 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2827 return;
2828
2829 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2830 slab_error(cache, "double free detected");
2831 else
2832 slab_error(cache, "memory outside object was overwritten");
2833
David Woodhouseb46b8f12007-05-08 00:22:59 -07002834 printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002835 obj, redzone1, redzone2);
2836}
2837
Pekka Enberg343e0d72006-02-01 03:05:50 -08002838static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002839 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 unsigned int objnr;
2842 struct slab *slabp;
2843
Matthew Wilcox80cbd912007-11-29 12:05:13 -07002844 BUG_ON(virt_to_cache(objp) != cachep);
2845
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002846 objp -= obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 kfree_debugcheck(objp);
Joonsoo Kim56f295e2013-10-24 10:07:43 +09002848 slabp = virt_to_slab(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 if (cachep->flags & SLAB_RED_ZONE) {
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002851 verify_redzone_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2853 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2854 }
2855 if (cachep->flags & SLAB_STORE_USER)
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002856 *dbg_userword(cachep, objp) = (void *)caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002858 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 BUG_ON(objnr >= cachep->num);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002861 BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 if (cachep->flags & SLAB_POISON) {
2864#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002865 if ((cachep->size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002866 store_stackinfo(cachep, objp, caller);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002867 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002868 cachep->size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 } else {
2870 poison_obj(cachep, objp, POISON_FREE);
2871 }
2872#else
2873 poison_obj(cachep, objp, POISON_FREE);
2874#endif
2875 }
2876 return objp;
2877}
2878
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879#else
2880#define kfree_debugcheck(x) do { } while(0)
2881#define cache_free_debugcheck(x,objp,z) (objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882#endif
2883
Mel Gorman072bb0a2012-07-31 16:43:58 -07002884static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags,
2885 bool force_refill)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886{
2887 int batchcount;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002888 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 struct array_cache *ac;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002890 int node;
2891
Joe Korty6d2144d2008-03-05 15:04:59 -08002892 check_irq_off();
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07002893 node = numa_mem_id();
Mel Gorman072bb0a2012-07-31 16:43:58 -07002894 if (unlikely(force_refill))
2895 goto force_grow;
2896retry:
Joe Korty6d2144d2008-03-05 15:04:59 -08002897 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 batchcount = ac->batchcount;
2899 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002900 /*
2901 * If there was little recent activity on this cache, then
2902 * perform only a partial refill. Otherwise we could generate
2903 * refill bouncing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 */
2905 batchcount = BATCHREFILL_LIMIT;
2906 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002907 n = cachep->node[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002909 BUG_ON(ac->avail > 0 || !n);
2910 spin_lock(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002911
Christoph Lameter3ded1752006-03-25 03:06:44 -08002912 /* See if we can refill from the shared array */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002913 if (n->shared && transfer_objects(ac, n->shared, batchcount)) {
2914 n->shared->touched = 1;
Christoph Lameter3ded1752006-03-25 03:06:44 -08002915 goto alloc_done;
Nick Piggin44b57f12010-01-27 22:27:40 +11002916 }
Christoph Lameter3ded1752006-03-25 03:06:44 -08002917
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 while (batchcount > 0) {
2919 struct list_head *entry;
2920 struct slab *slabp;
2921 /* Get slab alloc is to come from. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002922 entry = n->slabs_partial.next;
2923 if (entry == &n->slabs_partial) {
2924 n->free_touched = 1;
2925 entry = n->slabs_free.next;
2926 if (entry == &n->slabs_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 goto must_grow;
2928 }
2929
2930 slabp = list_entry(entry, struct slab, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 check_spinlock_acquired(cachep);
Pekka Enberg714b81712007-05-06 14:49:03 -07002932
2933 /*
2934 * The slab was either on partial or free list so
2935 * there must be at least one object available for
2936 * allocation.
2937 */
roel kluin249b9f32008-10-29 17:18:07 -04002938 BUG_ON(slabp->inuse >= cachep->num);
Pekka Enberg714b81712007-05-06 14:49:03 -07002939
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 while (slabp->inuse < cachep->num && batchcount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 STATS_INC_ALLOCED(cachep);
2942 STATS_INC_ACTIVE(cachep);
2943 STATS_SET_HIGH(cachep);
2944
Mel Gorman072bb0a2012-07-31 16:43:58 -07002945 ac_put_obj(cachep, ac, slab_get_obj(cachep, slabp,
2946 node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
2949 /* move slabp to correct slabp list: */
2950 list_del(&slabp->list);
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002951 if (slabp->free == cachep->num)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002952 list_add(&slabp->list, &n->slabs_full);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 else
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002954 list_add(&slabp->list, &n->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 }
2956
Andrew Mortona737b3e2006-03-22 00:08:11 -08002957must_grow:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002958 n->free_objects -= ac->avail;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002959alloc_done:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002960 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961
2962 if (unlikely(!ac->avail)) {
2963 int x;
Mel Gorman072bb0a2012-07-31 16:43:58 -07002964force_grow:
Christoph Lameter3c517a62006-12-06 20:33:29 -08002965 x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07002966
Andrew Mortona737b3e2006-03-22 00:08:11 -08002967 /* cache_grow can reenable interrupts, then ac could change. */
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002968 ac = cpu_cache_get(cachep);
David Rientjes51cd8e62012-08-28 19:57:21 -07002969 node = numa_mem_id();
Mel Gorman072bb0a2012-07-31 16:43:58 -07002970
2971 /* no objects in sight? abort */
2972 if (!x && (ac->avail == 0 || force_refill))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 return NULL;
2974
Andrew Mortona737b3e2006-03-22 00:08:11 -08002975 if (!ac->avail) /* objects refilled by interrupt? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 goto retry;
2977 }
2978 ac->touched = 1;
Mel Gorman072bb0a2012-07-31 16:43:58 -07002979
2980 return ac_get_obj(cachep, ac, flags, force_refill);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981}
2982
Andrew Mortona737b3e2006-03-22 00:08:11 -08002983static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
2984 gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
2986 might_sleep_if(flags & __GFP_WAIT);
2987#if DEBUG
2988 kmem_flagcheck(cachep, flags);
2989#endif
2990}
2991
2992#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08002993static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002994 gfp_t flags, void *objp, unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002996 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 return objp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002998 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003000 if ((cachep->size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003001 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003002 cachep->size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 else
3004 check_poison_obj(cachep, objp);
3005#else
3006 check_poison_obj(cachep, objp);
3007#endif
3008 poison_obj(cachep, objp, POISON_INUSE);
3009 }
3010 if (cachep->flags & SLAB_STORE_USER)
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003011 *dbg_userword(cachep, objp) = (void *)caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012
3013 if (cachep->flags & SLAB_RED_ZONE) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003014 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
3015 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
3016 slab_error(cachep, "double free, or memory outside"
3017 " object was overwritten");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003018 printk(KERN_ERR
David Woodhouseb46b8f12007-05-08 00:22:59 -07003019 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08003020 objp, *dbg_redzone1(cachep, objp),
3021 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 }
3023 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
3024 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
3025 }
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003026 objp += obj_offset(cachep);
Christoph Lameter4f104932007-05-06 14:50:17 -07003027 if (cachep->ctor && cachep->flags & SLAB_POISON)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003028 cachep->ctor(objp);
Tetsuo Handa7ea466f2011-07-21 09:42:45 +09003029 if (ARCH_SLAB_MINALIGN &&
3030 ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003031 printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
Hugh Dickinsc2251502011-07-11 13:35:08 -07003032 objp, (int)ARCH_SLAB_MINALIGN);
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003033 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 return objp;
3035}
3036#else
3037#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
3038#endif
3039
Akinobu Mita773ff602008-12-23 19:37:01 +09003040static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags)
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003041{
Christoph Lameter9b030cb2012-09-05 00:20:33 +00003042 if (cachep == kmem_cache)
Akinobu Mita773ff602008-12-23 19:37:01 +09003043 return false;
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003044
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003045 return should_failslab(cachep->object_size, flags, cachep->flags);
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003046}
3047
Pekka Enberg343e0d72006-02-01 03:05:50 -08003048static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003050 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 struct array_cache *ac;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003052 bool force_refill = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
Alok N Kataria5c382302005-09-27 21:45:46 -07003054 check_irq_off();
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003055
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003056 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 if (likely(ac->avail)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 ac->touched = 1;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003059 objp = ac_get_obj(cachep, ac, flags, false);
3060
J. R. Okajimaddbf2e82009-12-02 16:55:50 +09003061 /*
Mel Gorman072bb0a2012-07-31 16:43:58 -07003062 * Allow for the possibility all avail objects are not allowed
3063 * by the current flags
J. R. Okajimaddbf2e82009-12-02 16:55:50 +09003064 */
Mel Gorman072bb0a2012-07-31 16:43:58 -07003065 if (objp) {
3066 STATS_INC_ALLOCHIT(cachep);
3067 goto out;
3068 }
3069 force_refill = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 }
Mel Gorman072bb0a2012-07-31 16:43:58 -07003071
3072 STATS_INC_ALLOCMISS(cachep);
3073 objp = cache_alloc_refill(cachep, flags, force_refill);
3074 /*
3075 * the 'ac' may be updated by cache_alloc_refill(),
3076 * and kmemleak_erase() requires its correct value.
3077 */
3078 ac = cpu_cache_get(cachep);
3079
3080out:
Catalin Marinasd5cff632009-06-11 13:22:40 +01003081 /*
3082 * To avoid a false negative, if an object that is in one of the
3083 * per-CPU caches is leaked, we need to make sure kmemleak doesn't
3084 * treat the array pointers as a reference to the object.
3085 */
J. R. Okajimaf3d8b532009-12-02 16:55:49 +09003086 if (objp)
3087 kmemleak_erase(&ac->entry[ac->avail]);
Alok N Kataria5c382302005-09-27 21:45:46 -07003088 return objp;
3089}
3090
Christoph Lametere498be72005-09-09 13:03:32 -07003091#ifdef CONFIG_NUMA
3092/*
Paul Jacksonb2455392006-03-24 03:16:12 -08003093 * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
Paul Jacksonc61afb12006-03-24 03:16:08 -08003094 *
3095 * If we are in_interrupt, then process context, including cpusets and
3096 * mempolicy, may not apply and should not be used for allocation policy.
3097 */
3098static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
3099{
3100 int nid_alloc, nid_here;
3101
Christoph Lameter765c4502006-09-27 01:50:08 -07003102 if (in_interrupt() || (flags & __GFP_THISNODE))
Paul Jacksonc61afb12006-03-24 03:16:08 -08003103 return NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003104 nid_alloc = nid_here = numa_mem_id();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003105 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
Jack Steiner6adef3e2010-05-26 14:42:49 -07003106 nid_alloc = cpuset_slab_spread_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003107 else if (current->mempolicy)
Andi Kleene7b691b2012-06-09 02:40:03 -07003108 nid_alloc = slab_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003109 if (nid_alloc != nid_here)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003110 return ____cache_alloc_node(cachep, flags, nid_alloc);
Paul Jacksonc61afb12006-03-24 03:16:08 -08003111 return NULL;
3112}
3113
3114/*
Christoph Lameter765c4502006-09-27 01:50:08 -07003115 * Fallback function if there was no memory available and no objects on a
Christoph Lameter3c517a62006-12-06 20:33:29 -08003116 * certain node and fall back is permitted. First we scan all the
Christoph Lameter6a673682013-01-10 19:14:19 +00003117 * available node for available objects. If that fails then we
Christoph Lameter3c517a62006-12-06 20:33:29 -08003118 * perform an allocation without specifying a node. This allows the page
3119 * allocator to do its reclaim / fallback magic. We then insert the
3120 * slab into the proper nodelist and then allocate from it.
Christoph Lameter765c4502006-09-27 01:50:08 -07003121 */
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003122static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
Christoph Lameter765c4502006-09-27 01:50:08 -07003123{
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003124 struct zonelist *zonelist;
3125 gfp_t local_flags;
Mel Gormandd1a2392008-04-28 02:12:17 -07003126 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07003127 struct zone *zone;
3128 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003129 void *obj = NULL;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003130 int nid;
Mel Gormancc9a6c82012-03-21 16:34:11 -07003131 unsigned int cpuset_mems_cookie;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003132
3133 if (flags & __GFP_THISNODE)
3134 return NULL;
3135
Christoph Lameter6cb06222007-10-16 01:25:41 -07003136 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Christoph Lameter765c4502006-09-27 01:50:08 -07003137
Mel Gormancc9a6c82012-03-21 16:34:11 -07003138retry_cpuset:
3139 cpuset_mems_cookie = get_mems_allowed();
Andi Kleene7b691b2012-06-09 02:40:03 -07003140 zonelist = node_zonelist(slab_node(), flags);
Mel Gormancc9a6c82012-03-21 16:34:11 -07003141
Christoph Lameter3c517a62006-12-06 20:33:29 -08003142retry:
3143 /*
3144 * Look through allowed nodes for objects available
3145 * from existing per node queues.
3146 */
Mel Gorman54a6eb52008-04-28 02:12:16 -07003147 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
3148 nid = zone_to_nid(zone);
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003149
Mel Gorman54a6eb52008-04-28 02:12:16 -07003150 if (cpuset_zone_allowed_hardwall(zone, flags) &&
Christoph Lameter6a673682013-01-10 19:14:19 +00003151 cache->node[nid] &&
3152 cache->node[nid]->free_objects) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003153 obj = ____cache_alloc_node(cache,
3154 flags | GFP_THISNODE, nid);
Christoph Lameter481c5342008-06-21 16:46:35 -07003155 if (obj)
3156 break;
3157 }
Christoph Lameter3c517a62006-12-06 20:33:29 -08003158 }
3159
Christoph Lametercfce6602007-05-06 14:50:17 -07003160 if (!obj) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003161 /*
3162 * This allocation will be performed within the constraints
3163 * of the current cpuset / memory policy requirements.
3164 * We may trigger various forms of reclaim on the allowed
3165 * set and go into memory reserves if necessary.
3166 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003167 struct page *page;
3168
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003169 if (local_flags & __GFP_WAIT)
3170 local_irq_enable();
3171 kmem_flagcheck(cache, flags);
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003172 page = kmem_getpages(cache, local_flags, numa_mem_id());
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003173 if (local_flags & __GFP_WAIT)
3174 local_irq_disable();
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003175 if (page) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003176 /*
3177 * Insert into the appropriate per node queues
3178 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003179 nid = page_to_nid(page);
3180 if (cache_grow(cache, flags, nid, page)) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003181 obj = ____cache_alloc_node(cache,
3182 flags | GFP_THISNODE, nid);
3183 if (!obj)
3184 /*
3185 * Another processor may allocate the
3186 * objects in the slab since we are
3187 * not holding any locks.
3188 */
3189 goto retry;
3190 } else {
Hugh Dickinsb6a60452007-01-05 16:36:36 -08003191 /* cache_grow already freed obj */
Christoph Lameter3c517a62006-12-06 20:33:29 -08003192 obj = NULL;
3193 }
3194 }
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003195 }
Mel Gormancc9a6c82012-03-21 16:34:11 -07003196
3197 if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !obj))
3198 goto retry_cpuset;
Christoph Lameter765c4502006-09-27 01:50:08 -07003199 return obj;
3200}
3201
3202/*
Christoph Lametere498be72005-09-09 13:03:32 -07003203 * A interface to enable slab creation on nodeid
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003205static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003206 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07003207{
3208 struct list_head *entry;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003209 struct slab *slabp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003210 struct kmem_cache_node *n;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003211 void *obj;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003212 int x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213
Aaron Tomlin14e50c62013-04-26 16:15:34 +01003214 VM_BUG_ON(nodeid > num_online_nodes());
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003215 n = cachep->node[nodeid];
3216 BUG_ON(!n);
Christoph Lametere498be72005-09-09 13:03:32 -07003217
Andrew Mortona737b3e2006-03-22 00:08:11 -08003218retry:
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003219 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003220 spin_lock(&n->list_lock);
3221 entry = n->slabs_partial.next;
3222 if (entry == &n->slabs_partial) {
3223 n->free_touched = 1;
3224 entry = n->slabs_free.next;
3225 if (entry == &n->slabs_free)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003226 goto must_grow;
3227 }
Christoph Lametere498be72005-09-09 13:03:32 -07003228
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003229 slabp = list_entry(entry, struct slab, list);
3230 check_spinlock_acquired_node(cachep, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07003231
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003232 STATS_INC_NODEALLOCS(cachep);
3233 STATS_INC_ACTIVE(cachep);
3234 STATS_SET_HIGH(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003235
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003236 BUG_ON(slabp->inuse == cachep->num);
Christoph Lametere498be72005-09-09 13:03:32 -07003237
Matthew Dobson78d382d2006-02-01 03:05:47 -08003238 obj = slab_get_obj(cachep, slabp, nodeid);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003239 n->free_objects--;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003240 /* move slabp to correct slabp list: */
3241 list_del(&slabp->list);
Christoph Lametere498be72005-09-09 13:03:32 -07003242
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09003243 if (slabp->free == cachep->num)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003244 list_add(&slabp->list, &n->slabs_full);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003245 else
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003246 list_add(&slabp->list, &n->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07003247
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003248 spin_unlock(&n->list_lock);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003249 goto done;
Christoph Lametere498be72005-09-09 13:03:32 -07003250
Andrew Mortona737b3e2006-03-22 00:08:11 -08003251must_grow:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003252 spin_unlock(&n->list_lock);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003253 x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
Christoph Lameter765c4502006-09-27 01:50:08 -07003254 if (x)
3255 goto retry;
Christoph Lametere498be72005-09-09 13:03:32 -07003256
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003257 return fallback_alloc(cachep, flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003258
Andrew Mortona737b3e2006-03-22 00:08:11 -08003259done:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003260 return obj;
Christoph Lametere498be72005-09-09 13:03:32 -07003261}
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003262
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003263static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003264slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003265 unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003266{
3267 unsigned long save_flags;
3268 void *ptr;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003269 int slab_node = numa_mem_id();
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003270
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003271 flags &= gfp_allowed_mask;
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003272
Nick Piggincf40bd12009-01-21 08:12:39 +01003273 lockdep_trace_alloc(flags);
3274
Akinobu Mita773ff602008-12-23 19:37:01 +09003275 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003276 return NULL;
3277
Glauber Costad79923f2012-12-18 14:22:48 -08003278 cachep = memcg_kmem_get_cache(cachep, flags);
3279
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003280 cache_alloc_debugcheck_before(cachep, flags);
3281 local_irq_save(save_flags);
3282
Andrew Mortoneacbbae2011-07-28 13:59:49 -07003283 if (nodeid == NUMA_NO_NODE)
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003284 nodeid = slab_node;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003285
Christoph Lameter6a673682013-01-10 19:14:19 +00003286 if (unlikely(!cachep->node[nodeid])) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003287 /* Node not bootstrapped yet */
3288 ptr = fallback_alloc(cachep, flags);
3289 goto out;
3290 }
3291
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003292 if (nodeid == slab_node) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003293 /*
3294 * Use the locally cached objects if possible.
3295 * However ____cache_alloc does not allow fallback
3296 * to other nodes. It may fail while we still have
3297 * objects on other nodes available.
3298 */
3299 ptr = ____cache_alloc(cachep, flags);
3300 if (ptr)
3301 goto out;
3302 }
3303 /* ___cache_alloc_node can fall back to other nodes */
3304 ptr = ____cache_alloc_node(cachep, flags, nodeid);
3305 out:
3306 local_irq_restore(save_flags);
3307 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003308 kmemleak_alloc_recursive(ptr, cachep->object_size, 1, cachep->flags,
Catalin Marinasd5cff632009-06-11 13:22:40 +01003309 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003310
Pekka Enbergc175eea2008-05-09 20:35:53 +02003311 if (likely(ptr))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003312 kmemcheck_slab_alloc(cachep, flags, ptr, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003313
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003314 if (unlikely((flags & __GFP_ZERO) && ptr))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003315 memset(ptr, 0, cachep->object_size);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003316
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003317 return ptr;
3318}
3319
3320static __always_inline void *
3321__do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
3322{
3323 void *objp;
3324
3325 if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
3326 objp = alternate_node_alloc(cache, flags);
3327 if (objp)
3328 goto out;
3329 }
3330 objp = ____cache_alloc(cache, flags);
3331
3332 /*
3333 * We may just have run out of memory on the local node.
3334 * ____cache_alloc_node() knows how to locate memory on other nodes
3335 */
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003336 if (!objp)
3337 objp = ____cache_alloc_node(cache, flags, numa_mem_id());
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003338
3339 out:
3340 return objp;
3341}
3342#else
3343
3344static __always_inline void *
3345__do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
3346{
3347 return ____cache_alloc(cachep, flags);
3348}
3349
3350#endif /* CONFIG_NUMA */
3351
3352static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003353slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003354{
3355 unsigned long save_flags;
3356 void *objp;
3357
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003358 flags &= gfp_allowed_mask;
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003359
Nick Piggincf40bd12009-01-21 08:12:39 +01003360 lockdep_trace_alloc(flags);
3361
Akinobu Mita773ff602008-12-23 19:37:01 +09003362 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003363 return NULL;
3364
Glauber Costad79923f2012-12-18 14:22:48 -08003365 cachep = memcg_kmem_get_cache(cachep, flags);
3366
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003367 cache_alloc_debugcheck_before(cachep, flags);
3368 local_irq_save(save_flags);
3369 objp = __do_cache_alloc(cachep, flags);
3370 local_irq_restore(save_flags);
3371 objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003372 kmemleak_alloc_recursive(objp, cachep->object_size, 1, cachep->flags,
Catalin Marinasd5cff632009-06-11 13:22:40 +01003373 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003374 prefetchw(objp);
3375
Pekka Enbergc175eea2008-05-09 20:35:53 +02003376 if (likely(objp))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003377 kmemcheck_slab_alloc(cachep, flags, objp, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003378
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003379 if (unlikely((flags & __GFP_ZERO) && objp))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003380 memset(objp, 0, cachep->object_size);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003381
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003382 return objp;
3383}
Christoph Lametere498be72005-09-09 13:03:32 -07003384
3385/*
3386 * Caller needs to acquire correct kmem_list's list_lock
3387 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003388static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003389 int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390{
3391 int i;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003392 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
3394 for (i = 0; i < nr_objects; i++) {
Mel Gorman072bb0a2012-07-31 16:43:58 -07003395 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397
Mel Gorman072bb0a2012-07-31 16:43:58 -07003398 clear_obj_pfmemalloc(&objpp[i]);
3399 objp = objpp[i];
3400
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003401 slabp = virt_to_slab(objp);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003402 n = cachep->node[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 list_del(&slabp->list);
Christoph Lameterff694162005-09-22 21:44:02 -07003404 check_spinlock_acquired_node(cachep, node);
Matthew Dobson78d382d2006-02-01 03:05:47 -08003405 slab_put_obj(cachep, slabp, objp, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 STATS_DEC_ACTIVE(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003407 n->free_objects++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
3409 /* fixup slab chains */
3410 if (slabp->inuse == 0) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003411 if (n->free_objects > n->free_limit) {
3412 n->free_objects -= cachep->num;
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07003413 /* No need to drop any previously held
3414 * lock here, even if we have a off-slab slab
3415 * descriptor it is guaranteed to come from
3416 * a different cache, refer to comments before
3417 * alloc_slabmgmt.
3418 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 slab_destroy(cachep, slabp);
3420 } else {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003421 list_add(&slabp->list, &n->slabs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 }
3423 } else {
3424 /* Unconditionally move a slab to the end of the
3425 * partial list on free - maximum time for the
3426 * other objects to be freed, too.
3427 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003428 list_add_tail(&slabp->list, &n->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 }
3430 }
3431}
3432
Pekka Enberg343e0d72006-02-01 03:05:50 -08003433static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434{
3435 int batchcount;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003436 struct kmem_cache_node *n;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003437 int node = numa_mem_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
3439 batchcount = ac->batchcount;
3440#if DEBUG
3441 BUG_ON(!batchcount || batchcount > ac->avail);
3442#endif
3443 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003444 n = cachep->node[node];
3445 spin_lock(&n->list_lock);
3446 if (n->shared) {
3447 struct array_cache *shared_array = n->shared;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003448 int max = shared_array->limit - shared_array->avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 if (max) {
3450 if (batchcount > max)
3451 batchcount = max;
Christoph Lametere498be72005-09-09 13:03:32 -07003452 memcpy(&(shared_array->entry[shared_array->avail]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003453 ac->entry, sizeof(void *) * batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 shared_array->avail += batchcount;
3455 goto free_done;
3456 }
3457 }
3458
Christoph Lameterff694162005-09-22 21:44:02 -07003459 free_block(cachep, ac->entry, batchcount, node);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003460free_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461#if STATS
3462 {
3463 int i = 0;
3464 struct list_head *p;
3465
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003466 p = n->slabs_free.next;
3467 while (p != &(n->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 struct slab *slabp;
3469
3470 slabp = list_entry(p, struct slab, list);
3471 BUG_ON(slabp->inuse);
3472
3473 i++;
3474 p = p->next;
3475 }
3476 STATS_SET_FREEABLE(cachep, i);
3477 }
3478#endif
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003479 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 ac->avail -= batchcount;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003481 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482}
3483
3484/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08003485 * Release an obj back to its cache. If the obj has a constructed state, it must
3486 * be in this state _before_ it is released. Called with disabled ints.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 */
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003488static inline void __cache_free(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003489 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490{
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003491 struct array_cache *ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
3493 check_irq_off();
Catalin Marinasd5cff632009-06-11 13:22:40 +01003494 kmemleak_free_recursive(objp, cachep->flags);
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003495 objp = cache_free_debugcheck(cachep, objp, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003497 kmemcheck_slab_free(cachep, objp, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003498
Siddha, Suresh B1807a1a2007-08-22 14:01:49 -07003499 /*
3500 * Skip calling cache_free_alien() when the platform is not numa.
3501 * This will avoid cache misses that happen while accessing slabp (which
3502 * is per page memory reference) to get nodeid. Instead use a global
3503 * variable to skip the call, which is mostly likely to be present in
3504 * the cache.
3505 */
Mel Gormanb6e68bc2009-06-16 15:32:16 -07003506 if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07003507 return;
Christoph Lametere498be72005-09-09 13:03:32 -07003508
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 if (likely(ac->avail < ac->limit)) {
3510 STATS_INC_FREEHIT(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 } else {
3512 STATS_INC_FREEMISS(cachep);
3513 cache_flusharray(cachep, ac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 }
Zhao Jin42c8c992011-08-27 00:26:17 +08003515
Mel Gorman072bb0a2012-07-31 16:43:58 -07003516 ac_put_obj(cachep, ac, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517}
3518
3519/**
3520 * kmem_cache_alloc - Allocate an object
3521 * @cachep: The cache to allocate from.
3522 * @flags: See kmalloc().
3523 *
3524 * Allocate an object from this cache. The flags are only relevant
3525 * if the cache has no available objects.
3526 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003527void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003529 void *ret = slab_alloc(cachep, flags, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003530
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003531 trace_kmem_cache_alloc(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003532 cachep->object_size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003533
3534 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535}
3536EXPORT_SYMBOL(kmem_cache_alloc);
3537
Li Zefan0f24f122009-12-11 15:45:30 +08003538#ifdef CONFIG_TRACING
Steven Rostedt85beb582010-11-24 16:23:34 -05003539void *
Ezequiel Garcia40521472012-09-08 17:47:56 -03003540kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003541{
Steven Rostedt85beb582010-11-24 16:23:34 -05003542 void *ret;
3543
Ezequiel Garcia48356302012-09-08 17:47:57 -03003544 ret = slab_alloc(cachep, flags, _RET_IP_);
Steven Rostedt85beb582010-11-24 16:23:34 -05003545
3546 trace_kmalloc(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003547 size, cachep->size, flags);
Steven Rostedt85beb582010-11-24 16:23:34 -05003548 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003549}
Steven Rostedt85beb582010-11-24 16:23:34 -05003550EXPORT_SYMBOL(kmem_cache_alloc_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003551#endif
3552
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553#ifdef CONFIG_NUMA
Zhouping Liud0d04b72013-05-16 11:36:23 +08003554/**
3555 * kmem_cache_alloc_node - Allocate an object on the specified node
3556 * @cachep: The cache to allocate from.
3557 * @flags: See kmalloc().
3558 * @nodeid: node number of the target node.
3559 *
3560 * Identical to kmem_cache_alloc but it will allocate memory on the given
3561 * node, which can improve the performance for cpu bound structures.
3562 *
3563 * Fallback to other node is possible if __GFP_THISNODE is not set.
3564 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003565void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
3566{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003567 void *ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003568
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003569 trace_kmem_cache_alloc_node(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003570 cachep->object_size, cachep->size,
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003571 flags, nodeid);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003572
3573 return ret;
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003574}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575EXPORT_SYMBOL(kmem_cache_alloc_node);
3576
Li Zefan0f24f122009-12-11 15:45:30 +08003577#ifdef CONFIG_TRACING
Ezequiel Garcia40521472012-09-08 17:47:56 -03003578void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
Steven Rostedt85beb582010-11-24 16:23:34 -05003579 gfp_t flags,
Ezequiel Garcia40521472012-09-08 17:47:56 -03003580 int nodeid,
3581 size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003582{
Steven Rostedt85beb582010-11-24 16:23:34 -05003583 void *ret;
3584
Ezequiel Garcia592f4142012-09-25 08:07:08 -03003585 ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003586
Steven Rostedt85beb582010-11-24 16:23:34 -05003587 trace_kmalloc_node(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003588 size, cachep->size,
Steven Rostedt85beb582010-11-24 16:23:34 -05003589 flags, nodeid);
3590 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003591}
Steven Rostedt85beb582010-11-24 16:23:34 -05003592EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003593#endif
3594
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003595static __always_inline void *
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003596__do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller)
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003597{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003598 struct kmem_cache *cachep;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003599
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003600 cachep = kmalloc_slab(size, flags);
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003601 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3602 return cachep;
Ezequiel Garcia40521472012-09-08 17:47:56 -03003603 return kmem_cache_alloc_node_trace(cachep, flags, node, size);
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003604}
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003605
Li Zefan0bb38a52009-12-11 15:45:50 +08003606#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003607void *__kmalloc_node(size_t size, gfp_t flags, int node)
3608{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003609 return __do_kmalloc_node(size, flags, node, _RET_IP_);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003610}
Christoph Hellwigdbe5e692006-09-25 23:31:36 -07003611EXPORT_SYMBOL(__kmalloc_node);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003612
3613void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003614 int node, unsigned long caller)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003615{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003616 return __do_kmalloc_node(size, flags, node, caller);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003617}
3618EXPORT_SYMBOL(__kmalloc_node_track_caller);
3619#else
3620void *__kmalloc_node(size_t size, gfp_t flags, int node)
3621{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003622 return __do_kmalloc_node(size, flags, node, 0);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003623}
3624EXPORT_SYMBOL(__kmalloc_node);
Li Zefan0bb38a52009-12-11 15:45:50 +08003625#endif /* CONFIG_DEBUG_SLAB || CONFIG_TRACING */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003626#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
3628/**
Paul Drynoff800590f2006-06-23 02:03:48 -07003629 * __do_kmalloc - allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 * @size: how many bytes of memory are required.
Paul Drynoff800590f2006-06-23 02:03:48 -07003631 * @flags: the type of memory to allocate (see kmalloc).
Randy Dunlap911851e2006-03-22 00:08:14 -08003632 * @caller: function caller for debug tracking of the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 */
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003634static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003635 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003637 struct kmem_cache *cachep;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003638 void *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003640 /* If you want to save a few bytes .text space: replace
3641 * __ with kmem_.
3642 * Then kmalloc uses the uninlined functions instead of the inline
3643 * functions.
3644 */
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003645 cachep = kmalloc_slab(size, flags);
Linus Torvaldsa5c96d82007-07-19 13:17:15 -07003646 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3647 return cachep;
Ezequiel Garcia48356302012-09-08 17:47:57 -03003648 ret = slab_alloc(cachep, flags, caller);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003649
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003650 trace_kmalloc(caller, ret,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003651 size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003652
3653 return ret;
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003654}
3655
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003656
Li Zefan0bb38a52009-12-11 15:45:50 +08003657#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003658void *__kmalloc(size_t size, gfp_t flags)
3659{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003660 return __do_kmalloc(size, flags, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661}
3662EXPORT_SYMBOL(__kmalloc);
3663
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003664void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003665{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003666 return __do_kmalloc(size, flags, caller);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003667}
3668EXPORT_SYMBOL(__kmalloc_track_caller);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003669
3670#else
3671void *__kmalloc(size_t size, gfp_t flags)
3672{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003673 return __do_kmalloc(size, flags, 0);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003674}
3675EXPORT_SYMBOL(__kmalloc);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003676#endif
3677
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678/**
3679 * kmem_cache_free - Deallocate an object
3680 * @cachep: The cache the allocation was from.
3681 * @objp: The previously allocated object.
3682 *
3683 * Free an object which was previously allocated from this
3684 * cache.
3685 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003686void kmem_cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687{
3688 unsigned long flags;
Glauber Costab9ce5ef2012-12-18 14:22:46 -08003689 cachep = cache_from_obj(cachep, objp);
3690 if (!cachep)
3691 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692
3693 local_irq_save(flags);
Feng Tangd97d4762012-07-02 14:29:10 +08003694 debug_check_no_locks_freed(objp, cachep->object_size);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07003695 if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003696 debug_check_no_obj_freed(objp, cachep->object_size);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003697 __cache_free(cachep, objp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 local_irq_restore(flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003699
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003700 trace_kmem_cache_free(_RET_IP_, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701}
3702EXPORT_SYMBOL(kmem_cache_free);
3703
3704/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 * kfree - free previously allocated memory
3706 * @objp: pointer returned by kmalloc.
3707 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07003708 * If @objp is NULL, no operation is performed.
3709 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 * Don't free memory not originally allocated by kmalloc()
3711 * or you will run into trouble.
3712 */
3713void kfree(const void *objp)
3714{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003715 struct kmem_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716 unsigned long flags;
3717
Pekka Enberg2121db72009-03-25 11:05:57 +02003718 trace_kfree(_RET_IP_, objp);
3719
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003720 if (unlikely(ZERO_OR_NULL_PTR(objp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 return;
3722 local_irq_save(flags);
3723 kfree_debugcheck(objp);
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003724 c = virt_to_cache(objp);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003725 debug_check_no_locks_freed(objp, c->object_size);
3726
3727 debug_check_no_obj_freed(objp, c->object_size);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003728 __cache_free(c, (void *)objp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 local_irq_restore(flags);
3730}
3731EXPORT_SYMBOL(kfree);
3732
Christoph Lametere498be72005-09-09 13:03:32 -07003733/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003734 * This initializes kmem_cache_node or resizes various caches for all nodes.
Christoph Lametere498be72005-09-09 13:03:32 -07003735 */
Pekka Enberg83b519e2009-06-10 19:40:04 +03003736static int alloc_kmemlist(struct kmem_cache *cachep, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -07003737{
3738 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003739 struct kmem_cache_node *n;
Christoph Lametercafeb022006-03-25 03:06:46 -08003740 struct array_cache *new_shared;
Paul Menage3395ee02006-12-06 20:32:16 -08003741 struct array_cache **new_alien = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003742
Mel Gorman9c09a952008-01-24 05:49:54 -08003743 for_each_online_node(node) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003744
Paul Menage3395ee02006-12-06 20:32:16 -08003745 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03003746 new_alien = alloc_alien_cache(node, cachep->limit, gfp);
Paul Menage3395ee02006-12-06 20:32:16 -08003747 if (!new_alien)
3748 goto fail;
3749 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003750
Eric Dumazet63109842007-05-06 14:49:28 -07003751 new_shared = NULL;
3752 if (cachep->shared) {
3753 new_shared = alloc_arraycache(node,
Christoph Lameter0718dc22006-03-25 03:06:47 -08003754 cachep->shared*cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003755 0xbaadf00d, gfp);
Eric Dumazet63109842007-05-06 14:49:28 -07003756 if (!new_shared) {
3757 free_alien_cache(new_alien);
3758 goto fail;
3759 }
Christoph Lameter0718dc22006-03-25 03:06:47 -08003760 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003761
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003762 n = cachep->node[node];
3763 if (n) {
3764 struct array_cache *shared = n->shared;
Christoph Lametercafeb022006-03-25 03:06:46 -08003765
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003766 spin_lock_irq(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07003767
Christoph Lametercafeb022006-03-25 03:06:46 -08003768 if (shared)
Christoph Lameter0718dc22006-03-25 03:06:47 -08003769 free_block(cachep, shared->entry,
3770 shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07003771
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003772 n->shared = new_shared;
3773 if (!n->alien) {
3774 n->alien = new_alien;
Christoph Lametere498be72005-09-09 13:03:32 -07003775 new_alien = NULL;
3776 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003777 n->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003778 cachep->batchcount + cachep->num;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003779 spin_unlock_irq(&n->list_lock);
Christoph Lametercafeb022006-03-25 03:06:46 -08003780 kfree(shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003781 free_alien_cache(new_alien);
3782 continue;
3783 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003784 n = kmalloc_node(sizeof(struct kmem_cache_node), gfp, node);
3785 if (!n) {
Christoph Lameter0718dc22006-03-25 03:06:47 -08003786 free_alien_cache(new_alien);
3787 kfree(new_shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003788 goto fail;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003789 }
Christoph Lametere498be72005-09-09 13:03:32 -07003790
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003791 kmem_cache_node_init(n);
3792 n->next_reap = jiffies + REAPTIMEOUT_LIST3 +
Andrew Mortona737b3e2006-03-22 00:08:11 -08003793 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003794 n->shared = new_shared;
3795 n->alien = new_alien;
3796 n->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003797 cachep->batchcount + cachep->num;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003798 cachep->node[node] = n;
Christoph Lametere498be72005-09-09 13:03:32 -07003799 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003800 return 0;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003801
Andrew Mortona737b3e2006-03-22 00:08:11 -08003802fail:
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003803 if (!cachep->list.next) {
Christoph Lameter0718dc22006-03-25 03:06:47 -08003804 /* Cache is not active yet. Roll back what we did */
3805 node--;
3806 while (node >= 0) {
Christoph Lameter6a673682013-01-10 19:14:19 +00003807 if (cachep->node[node]) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003808 n = cachep->node[node];
Christoph Lameter0718dc22006-03-25 03:06:47 -08003809
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003810 kfree(n->shared);
3811 free_alien_cache(n->alien);
3812 kfree(n);
Christoph Lameter6a673682013-01-10 19:14:19 +00003813 cachep->node[node] = NULL;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003814 }
3815 node--;
3816 }
3817 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003818 return -ENOMEM;
Christoph Lametere498be72005-09-09 13:03:32 -07003819}
3820
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821struct ccupdate_struct {
Pekka Enberg343e0d72006-02-01 03:05:50 -08003822 struct kmem_cache *cachep;
Eric Dumazetacfe7d72011-07-25 08:55:42 +02003823 struct array_cache *new[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824};
3825
3826static void do_ccupdate_local(void *info)
3827{
Andrew Mortona737b3e2006-03-22 00:08:11 -08003828 struct ccupdate_struct *new = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 struct array_cache *old;
3830
3831 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003832 old = cpu_cache_get(new->cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003833
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834 new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
3835 new->new[smp_processor_id()] = old;
3836}
3837
Christoph Lameter18004c52012-07-06 15:25:12 -05003838/* Always called with the slab_mutex held */
Glauber Costa943a4512012-12-18 14:23:03 -08003839static int __do_tune_cpucache(struct kmem_cache *cachep, int limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003840 int batchcount, int shared, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841{
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003842 struct ccupdate_struct *new;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003843 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
Eric Dumazetacfe7d72011-07-25 08:55:42 +02003845 new = kzalloc(sizeof(*new) + nr_cpu_ids * sizeof(struct array_cache *),
3846 gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003847 if (!new)
3848 return -ENOMEM;
3849
Christoph Lametere498be72005-09-09 13:03:32 -07003850 for_each_online_cpu(i) {
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003851 new->new[i] = alloc_arraycache(cpu_to_mem(i), limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003852 batchcount, gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003853 if (!new->new[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003854 for (i--; i >= 0; i--)
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003855 kfree(new->new[i]);
3856 kfree(new);
Christoph Lametere498be72005-09-09 13:03:32 -07003857 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 }
3859 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003860 new->cachep = cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861
Jens Axboe15c8b6c2008-05-09 09:39:44 +02003862 on_each_cpu(do_ccupdate_local, (void *)new, 1);
Christoph Lametere498be72005-09-09 13:03:32 -07003863
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 check_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 cachep->batchcount = batchcount;
3866 cachep->limit = limit;
Christoph Lametere498be72005-09-09 13:03:32 -07003867 cachep->shared = shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868
Christoph Lametere498be72005-09-09 13:03:32 -07003869 for_each_online_cpu(i) {
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003870 struct array_cache *ccold = new->new[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 if (!ccold)
3872 continue;
Christoph Lameter6a673682013-01-10 19:14:19 +00003873 spin_lock_irq(&cachep->node[cpu_to_mem(i)]->list_lock);
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003874 free_block(cachep, ccold->entry, ccold->avail, cpu_to_mem(i));
Christoph Lameter6a673682013-01-10 19:14:19 +00003875 spin_unlock_irq(&cachep->node[cpu_to_mem(i)]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 kfree(ccold);
3877 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003878 kfree(new);
Pekka Enberg83b519e2009-06-10 19:40:04 +03003879 return alloc_kmemlist(cachep, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880}
3881
Glauber Costa943a4512012-12-18 14:23:03 -08003882static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
3883 int batchcount, int shared, gfp_t gfp)
3884{
3885 int ret;
3886 struct kmem_cache *c = NULL;
3887 int i = 0;
3888
3889 ret = __do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
3890
3891 if (slab_state < FULL)
3892 return ret;
3893
3894 if ((ret < 0) || !is_root_cache(cachep))
3895 return ret;
3896
Glauber Costaebe945c2012-12-18 14:23:10 -08003897 VM_BUG_ON(!mutex_is_locked(&slab_mutex));
Glauber Costa943a4512012-12-18 14:23:03 -08003898 for_each_memcg_cache_index(i) {
3899 c = cache_from_memcg(cachep, i);
3900 if (c)
3901 /* return value determined by the parent cache only */
3902 __do_tune_cpucache(c, limit, batchcount, shared, gfp);
3903 }
3904
3905 return ret;
3906}
3907
Christoph Lameter18004c52012-07-06 15:25:12 -05003908/* Called with slab_mutex held always */
Pekka Enberg83b519e2009-06-10 19:40:04 +03003909static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910{
3911 int err;
Glauber Costa943a4512012-12-18 14:23:03 -08003912 int limit = 0;
3913 int shared = 0;
3914 int batchcount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915
Glauber Costa943a4512012-12-18 14:23:03 -08003916 if (!is_root_cache(cachep)) {
3917 struct kmem_cache *root = memcg_root_cache(cachep);
3918 limit = root->limit;
3919 shared = root->shared;
3920 batchcount = root->batchcount;
3921 }
3922
3923 if (limit && shared && batchcount)
3924 goto skip_setup;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003925 /*
3926 * The head array serves three purposes:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 * - create a LIFO ordering, i.e. return objects that are cache-warm
3928 * - reduce the number of spinlock operations.
Andrew Mortona737b3e2006-03-22 00:08:11 -08003929 * - reduce the number of linked list operations on the slab and
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 * bufctl chains: array operations are cheaper.
3931 * The numbers are guessed, we should auto-tune as described by
3932 * Bonwick.
3933 */
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003934 if (cachep->size > 131072)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935 limit = 1;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003936 else if (cachep->size > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 limit = 8;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003938 else if (cachep->size > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 limit = 24;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003940 else if (cachep->size > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 limit = 54;
3942 else
3943 limit = 120;
3944
Andrew Mortona737b3e2006-03-22 00:08:11 -08003945 /*
3946 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 * allocation behaviour: Most allocs on one cpu, most free operations
3948 * on another cpu. For these cases, an efficient object passing between
3949 * cpus is necessary. This is provided by a shared array. The array
3950 * replaces Bonwick's magazine layer.
3951 * On uniprocessor, it's functionally equivalent (but less efficient)
3952 * to a larger limit. Thus disabled by default.
3953 */
3954 shared = 0;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003955 if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956 shared = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
3958#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003959 /*
3960 * With debugging enabled, large batchcount lead to excessively long
3961 * periods with disabled local interrupts. Limit the batchcount
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 */
3963 if (limit > 32)
3964 limit = 32;
3965#endif
Glauber Costa943a4512012-12-18 14:23:03 -08003966 batchcount = (limit + 1) / 2;
3967skip_setup:
3968 err = do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 if (err)
3970 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003971 cachep->name, -err);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003972 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973}
3974
Christoph Lameter1b552532006-03-22 00:09:07 -08003975/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003976 * Drain an array if it contains any elements taking the node lock only if
3977 * necessary. Note that the node listlock also protects the array_cache
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003978 * if drain_array() is used on the shared array.
Christoph Lameter1b552532006-03-22 00:09:07 -08003979 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003980static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
Christoph Lameter1b552532006-03-22 00:09:07 -08003981 struct array_cache *ac, int force, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982{
3983 int tofree;
3984
Christoph Lameter1b552532006-03-22 00:09:07 -08003985 if (!ac || !ac->avail)
3986 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 if (ac->touched && !force) {
3988 ac->touched = 0;
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003989 } else {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003990 spin_lock_irq(&n->list_lock);
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003991 if (ac->avail) {
3992 tofree = force ? ac->avail : (ac->limit + 4) / 5;
3993 if (tofree > ac->avail)
3994 tofree = (ac->avail + 1) / 2;
3995 free_block(cachep, ac->entry, tofree, node);
3996 ac->avail -= tofree;
3997 memmove(ac->entry, &(ac->entry[tofree]),
3998 sizeof(void *) * ac->avail);
3999 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004000 spin_unlock_irq(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001 }
4002}
4003
4004/**
4005 * cache_reap - Reclaim memory from caches.
Randy Dunlap05fb6bf2007-02-28 20:12:13 -08004006 * @w: work descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 *
4008 * Called from workqueue/eventd every few seconds.
4009 * Purpose:
4010 * - clear the per-cpu caches for this CPU.
4011 * - return freeable pages to the main free memory pool.
4012 *
Andrew Mortona737b3e2006-03-22 00:08:11 -08004013 * If we cannot acquire the cache chain mutex then just give up - we'll try
4014 * again on the next iteration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004016static void cache_reap(struct work_struct *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017{
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004018 struct kmem_cache *searchp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004019 struct kmem_cache_node *n;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07004020 int node = numa_mem_id();
Jean Delvarebf6aede2009-04-02 16:56:54 -07004021 struct delayed_work *work = to_delayed_work(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022
Christoph Lameter18004c52012-07-06 15:25:12 -05004023 if (!mutex_trylock(&slab_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 /* Give up. Setup the next iteration. */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004025 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026
Christoph Lameter18004c52012-07-06 15:25:12 -05004027 list_for_each_entry(searchp, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 check_irq_on();
4029
Christoph Lameter35386e32006-03-22 00:09:05 -08004030 /*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004031 * We only take the node lock if absolutely necessary and we
Christoph Lameter35386e32006-03-22 00:09:05 -08004032 * have established with reasonable certainty that
4033 * we can do some work if the lock was obtained.
4034 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004035 n = searchp->node[node];
Christoph Lameter35386e32006-03-22 00:09:05 -08004036
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004037 reap_alien(searchp, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004039 drain_array(searchp, n, cpu_cache_get(searchp), 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
Christoph Lameter35386e32006-03-22 00:09:05 -08004041 /*
4042 * These are racy checks but it does not matter
4043 * if we skip one check or scan twice.
4044 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004045 if (time_after(n->next_reap, jiffies))
Christoph Lameter35386e32006-03-22 00:09:05 -08004046 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004048 n->next_reap = jiffies + REAPTIMEOUT_LIST3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004050 drain_array(searchp, n, n->shared, 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004052 if (n->free_touched)
4053 n->free_touched = 0;
Christoph Lametered11d9e2006-06-30 01:55:45 -07004054 else {
4055 int freed;
4056
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004057 freed = drain_freelist(searchp, n, (n->free_limit +
Christoph Lametered11d9e2006-06-30 01:55:45 -07004058 5 * searchp->num - 1) / (5 * searchp->num));
4059 STATS_ADD_REAPED(searchp, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 }
Christoph Lameter35386e32006-03-22 00:09:05 -08004061next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 cond_resched();
4063 }
4064 check_irq_on();
Christoph Lameter18004c52012-07-06 15:25:12 -05004065 mutex_unlock(&slab_mutex);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004066 next_reap_node();
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004067out:
Andrew Mortona737b3e2006-03-22 00:08:11 -08004068 /* Set up the next iteration */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004069 schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_CPUC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070}
4071
Linus Torvalds158a9622008-01-02 13:04:48 -08004072#ifdef CONFIG_SLABINFO
Glauber Costa0d7561c2012-10-19 18:20:27 +04004073void get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004075 struct slab *slabp;
4076 unsigned long active_objs;
4077 unsigned long num_objs;
4078 unsigned long active_slabs = 0;
4079 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004080 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 char *error = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07004082 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004083 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 active_objs = 0;
4086 num_slabs = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004087 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004088 n = cachep->node[node];
4089 if (!n)
Christoph Lametere498be72005-09-09 13:03:32 -07004090 continue;
4091
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004092 check_irq_on();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004093 spin_lock_irq(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07004094
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004095 list_for_each_entry(slabp, &n->slabs_full, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004096 if (slabp->inuse != cachep->num && !error)
4097 error = "slabs_full accounting error";
4098 active_objs += cachep->num;
4099 active_slabs++;
4100 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004101 list_for_each_entry(slabp, &n->slabs_partial, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004102 if (slabp->inuse == cachep->num && !error)
4103 error = "slabs_partial inuse accounting error";
4104 if (!slabp->inuse && !error)
4105 error = "slabs_partial/inuse accounting error";
4106 active_objs += slabp->inuse;
4107 active_slabs++;
4108 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004109 list_for_each_entry(slabp, &n->slabs_free, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004110 if (slabp->inuse && !error)
4111 error = "slabs_free/inuse accounting error";
4112 num_slabs++;
4113 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004114 free_objects += n->free_objects;
4115 if (n->shared)
4116 shared_avail += n->shared->avail;
Christoph Lametere498be72005-09-09 13:03:32 -07004117
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004118 spin_unlock_irq(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004120 num_slabs += active_slabs;
4121 num_objs = num_slabs * cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07004122 if (num_objs - active_objs != free_objects && !error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 error = "free_objects accounting error";
4124
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004125 name = cachep->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126 if (error)
4127 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
4128
Glauber Costa0d7561c2012-10-19 18:20:27 +04004129 sinfo->active_objs = active_objs;
4130 sinfo->num_objs = num_objs;
4131 sinfo->active_slabs = active_slabs;
4132 sinfo->num_slabs = num_slabs;
4133 sinfo->shared_avail = shared_avail;
4134 sinfo->limit = cachep->limit;
4135 sinfo->batchcount = cachep->batchcount;
4136 sinfo->shared = cachep->shared;
4137 sinfo->objects_per_slab = cachep->num;
4138 sinfo->cache_order = cachep->gfporder;
4139}
4140
4141void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *cachep)
4142{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143#if STATS
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004144 { /* node stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 unsigned long high = cachep->high_mark;
4146 unsigned long allocs = cachep->num_allocations;
4147 unsigned long grown = cachep->grown;
4148 unsigned long reaped = cachep->reaped;
4149 unsigned long errors = cachep->errors;
4150 unsigned long max_freeable = cachep->max_freeable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 unsigned long node_allocs = cachep->node_allocs;
Christoph Lametere498be72005-09-09 13:03:32 -07004152 unsigned long node_frees = cachep->node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004153 unsigned long overflows = cachep->node_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154
Joe Perchese92dd4f2010-03-26 19:27:58 -07004155 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu "
4156 "%4lu %4lu %4lu %4lu %4lu",
4157 allocs, high, grown,
4158 reaped, errors, max_freeable, node_allocs,
4159 node_frees, overflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 }
4161 /* cpu stats */
4162 {
4163 unsigned long allochit = atomic_read(&cachep->allochit);
4164 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
4165 unsigned long freehit = atomic_read(&cachep->freehit);
4166 unsigned long freemiss = atomic_read(&cachep->freemiss);
4167
4168 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004169 allochit, allocmiss, freehit, freemiss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 }
4171#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172}
4173
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174#define MAX_SLABINFO_WRITE 128
4175/**
4176 * slabinfo_write - Tuning for the slab allocator
4177 * @file: unused
4178 * @buffer: user buffer
4179 * @count: data length
4180 * @ppos: unused
4181 */
Glauber Costab7454ad2012-10-19 18:20:25 +04004182ssize_t slabinfo_write(struct file *file, const char __user *buffer,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004183 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004185 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 int limit, batchcount, shared, res;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004187 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004188
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 if (count > MAX_SLABINFO_WRITE)
4190 return -EINVAL;
4191 if (copy_from_user(&kbuf, buffer, count))
4192 return -EFAULT;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004193 kbuf[MAX_SLABINFO_WRITE] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194
4195 tmp = strchr(kbuf, ' ');
4196 if (!tmp)
4197 return -EINVAL;
4198 *tmp = '\0';
4199 tmp++;
4200 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
4201 return -EINVAL;
4202
4203 /* Find the cache in the chain of caches. */
Christoph Lameter18004c52012-07-06 15:25:12 -05004204 mutex_lock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 res = -EINVAL;
Christoph Lameter18004c52012-07-06 15:25:12 -05004206 list_for_each_entry(cachep, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 if (!strcmp(cachep->name, kbuf)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08004208 if (limit < 1 || batchcount < 1 ||
4209 batchcount > limit || shared < 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07004210 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07004212 res = do_tune_cpucache(cachep, limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03004213 batchcount, shared,
4214 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 }
4216 break;
4217 }
4218 }
Christoph Lameter18004c52012-07-06 15:25:12 -05004219 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 if (res >= 0)
4221 res = count;
4222 return res;
4223}
Al Viro871751e2006-03-25 03:06:39 -08004224
4225#ifdef CONFIG_DEBUG_SLAB_LEAK
4226
4227static void *leaks_start(struct seq_file *m, loff_t *pos)
4228{
Christoph Lameter18004c52012-07-06 15:25:12 -05004229 mutex_lock(&slab_mutex);
4230 return seq_list_start(&slab_caches, *pos);
Al Viro871751e2006-03-25 03:06:39 -08004231}
4232
4233static inline int add_caller(unsigned long *n, unsigned long v)
4234{
4235 unsigned long *p;
4236 int l;
4237 if (!v)
4238 return 1;
4239 l = n[1];
4240 p = n + 2;
4241 while (l) {
4242 int i = l/2;
4243 unsigned long *q = p + 2 * i;
4244 if (*q == v) {
4245 q[1]++;
4246 return 1;
4247 }
4248 if (*q > v) {
4249 l = i;
4250 } else {
4251 p = q + 2;
4252 l -= i + 1;
4253 }
4254 }
4255 if (++n[1] == n[0])
4256 return 0;
4257 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4258 p[0] = v;
4259 p[1] = 1;
4260 return 1;
4261}
4262
4263static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
4264{
4265 void *p;
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09004266 int i, j;
4267
Al Viro871751e2006-03-25 03:06:39 -08004268 if (n[0] == n[1])
4269 return;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004270 for (i = 0, p = s->s_mem; i < c->num; i++, p += c->size) {
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09004271 bool active = true;
4272
4273 for (j = s->free; j < c->num; j++) {
4274 /* Skip freed item */
4275 if (slab_bufctl(s)[j] == i) {
4276 active = false;
4277 break;
4278 }
4279 }
4280 if (!active)
Al Viro871751e2006-03-25 03:06:39 -08004281 continue;
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09004282
Al Viro871751e2006-03-25 03:06:39 -08004283 if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
4284 return;
4285 }
4286}
4287
4288static void show_symbol(struct seq_file *m, unsigned long address)
4289{
4290#ifdef CONFIG_KALLSYMS
Al Viro871751e2006-03-25 03:06:39 -08004291 unsigned long offset, size;
Tejun Heo9281ace2007-07-17 04:03:51 -07004292 char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
Al Viro871751e2006-03-25 03:06:39 -08004293
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004294 if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
Al Viro871751e2006-03-25 03:06:39 -08004295 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004296 if (modname[0])
Al Viro871751e2006-03-25 03:06:39 -08004297 seq_printf(m, " [%s]", modname);
4298 return;
4299 }
4300#endif
4301 seq_printf(m, "%p", (void *)address);
4302}
4303
4304static int leaks_show(struct seq_file *m, void *p)
4305{
Thierry Reding0672aa72012-06-22 19:42:49 +02004306 struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);
Al Viro871751e2006-03-25 03:06:39 -08004307 struct slab *slabp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004308 struct kmem_cache_node *n;
Al Viro871751e2006-03-25 03:06:39 -08004309 const char *name;
Christoph Lameterdb845062013-02-05 18:45:23 +00004310 unsigned long *x = m->private;
Al Viro871751e2006-03-25 03:06:39 -08004311 int node;
4312 int i;
4313
4314 if (!(cachep->flags & SLAB_STORE_USER))
4315 return 0;
4316 if (!(cachep->flags & SLAB_RED_ZONE))
4317 return 0;
4318
4319 /* OK, we can do it */
4320
Christoph Lameterdb845062013-02-05 18:45:23 +00004321 x[1] = 0;
Al Viro871751e2006-03-25 03:06:39 -08004322
4323 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004324 n = cachep->node[node];
4325 if (!n)
Al Viro871751e2006-03-25 03:06:39 -08004326 continue;
4327
4328 check_irq_on();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004329 spin_lock_irq(&n->list_lock);
Al Viro871751e2006-03-25 03:06:39 -08004330
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004331 list_for_each_entry(slabp, &n->slabs_full, list)
Christoph Lameterdb845062013-02-05 18:45:23 +00004332 handle_slab(x, cachep, slabp);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004333 list_for_each_entry(slabp, &n->slabs_partial, list)
Christoph Lameterdb845062013-02-05 18:45:23 +00004334 handle_slab(x, cachep, slabp);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004335 spin_unlock_irq(&n->list_lock);
Al Viro871751e2006-03-25 03:06:39 -08004336 }
4337 name = cachep->name;
Christoph Lameterdb845062013-02-05 18:45:23 +00004338 if (x[0] == x[1]) {
Al Viro871751e2006-03-25 03:06:39 -08004339 /* Increase the buffer size */
Christoph Lameter18004c52012-07-06 15:25:12 -05004340 mutex_unlock(&slab_mutex);
Christoph Lameterdb845062013-02-05 18:45:23 +00004341 m->private = kzalloc(x[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
Al Viro871751e2006-03-25 03:06:39 -08004342 if (!m->private) {
4343 /* Too bad, we are really out */
Christoph Lameterdb845062013-02-05 18:45:23 +00004344 m->private = x;
Christoph Lameter18004c52012-07-06 15:25:12 -05004345 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004346 return -ENOMEM;
4347 }
Christoph Lameterdb845062013-02-05 18:45:23 +00004348 *(unsigned long *)m->private = x[0] * 2;
4349 kfree(x);
Christoph Lameter18004c52012-07-06 15:25:12 -05004350 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004351 /* Now make sure this entry will be retried */
4352 m->count = m->size;
4353 return 0;
4354 }
Christoph Lameterdb845062013-02-05 18:45:23 +00004355 for (i = 0; i < x[1]; i++) {
4356 seq_printf(m, "%s: %lu ", name, x[2*i+3]);
4357 show_symbol(m, x[2*i+2]);
Al Viro871751e2006-03-25 03:06:39 -08004358 seq_putc(m, '\n');
4359 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004360
Al Viro871751e2006-03-25 03:06:39 -08004361 return 0;
4362}
4363
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004364static const struct seq_operations slabstats_op = {
Al Viro871751e2006-03-25 03:06:39 -08004365 .start = leaks_start,
Wanpeng Li276a2432013-07-08 08:08:28 +08004366 .next = slab_next,
4367 .stop = slab_stop,
Al Viro871751e2006-03-25 03:06:39 -08004368 .show = leaks_show,
4369};
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004370
4371static int slabstats_open(struct inode *inode, struct file *file)
4372{
4373 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
4374 int ret = -ENOMEM;
4375 if (n) {
4376 ret = seq_open(file, &slabstats_op);
4377 if (!ret) {
4378 struct seq_file *m = file->private_data;
4379 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
4380 m->private = n;
4381 n = NULL;
4382 }
4383 kfree(n);
4384 }
4385 return ret;
4386}
4387
4388static const struct file_operations proc_slabstats_operations = {
4389 .open = slabstats_open,
4390 .read = seq_read,
4391 .llseek = seq_lseek,
4392 .release = seq_release_private,
4393};
Al Viro871751e2006-03-25 03:06:39 -08004394#endif
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004395
4396static int __init slab_proc_init(void)
4397{
4398#ifdef CONFIG_DEBUG_SLAB_LEAK
4399 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
4400#endif
4401 return 0;
4402}
4403module_init(slab_proc_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404#endif
4405
Manfred Spraul00e145b2005-09-03 15:55:07 -07004406/**
4407 * ksize - get the actual amount of memory allocated for a given object
4408 * @objp: Pointer to the object
4409 *
4410 * kmalloc may internally round up allocations and return more memory
4411 * than requested. ksize() can be used to determine the actual amount of
4412 * memory allocated. The caller may use this additional memory, even though
4413 * a smaller amount of memory was initially specified with the kmalloc call.
4414 * The caller must guarantee that objp points to a valid object previously
4415 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4416 * must not be freed during the duration of the call.
4417 */
Pekka Enbergfd76bab2007-05-06 14:48:40 -07004418size_t ksize(const void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004419{
Christoph Lameteref8b4522007-10-16 01:24:46 -07004420 BUG_ON(!objp);
4421 if (unlikely(objp == ZERO_SIZE_PTR))
Manfred Spraul00e145b2005-09-03 15:55:07 -07004422 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
Christoph Lameter8c138bc2012-06-13 10:24:58 -05004424 return virt_to_cache(objp)->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425}
Kirill A. Shutemovb1aabec2009-02-10 15:21:44 +02004426EXPORT_SYMBOL(ksize);