blob: fa94c0b04265388cae4ae84438087dcad0fc322e [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>
Thomas Gleixner4a621b32011-06-18 19:44:43 +0200119#include <linux/locallock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Mel Gorman381760e2012-07-31 16:44:30 -0700121#include <net/sock.h>
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#include <asm/cacheflush.h>
124#include <asm/tlbflush.h>
125#include <asm/page.h>
126
Steven Rostedt4dee6b62012-01-09 17:15:42 -0500127#include <trace/events/kmem.h>
128
Mel Gorman072bb0a2012-07-31 16:43:58 -0700129#include "internal.h"
130
Glauber Costab9ce5ef2012-12-18 14:22:46 -0800131#include "slab.h"
132
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133/*
Christoph Lameter50953fe2007-05-06 14:50:16 -0700134 * DEBUG - 1 for kmem_cache_create() to honour; SLAB_RED_ZONE & SLAB_POISON.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 * 0 for faster, smaller code (especially in the critical paths).
136 *
137 * STATS - 1 to collect stats for /proc/slabinfo.
138 * 0 for faster, smaller code (especially in the critical paths).
139 *
140 * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
141 */
142
143#ifdef CONFIG_DEBUG_SLAB
144#define DEBUG 1
145#define STATS 1
146#define FORCED_DEBUG 1
147#else
148#define DEBUG 0
149#define STATS 0
150#define FORCED_DEBUG 0
151#endif
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153/* Shouldn't this be in a header file somewhere? */
154#define BYTES_PER_WORD sizeof(void *)
David Woodhouse87a927c2007-07-04 21:26:44 -0400155#define REDZONE_ALIGN max(BYTES_PER_WORD, __alignof__(unsigned long long))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#ifndef ARCH_KMALLOC_FLAGS
158#define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
159#endif
160
Mel Gorman072bb0a2012-07-31 16:43:58 -0700161/*
162 * true if a page was allocated from pfmemalloc reserves for network-based
163 * swap
164 */
165static bool pfmemalloc_active __read_mostly;
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/*
168 * kmem_bufctl_t:
169 *
170 * Bufctl's are used for linking objs within a slab
171 * linked offsets.
172 *
173 * This implementation relies on "struct page" for locating the cache &
174 * slab an object belongs to.
175 * This allows the bufctl structure to be small (one int), but limits
176 * the number of objects a slab (not a cache) can contain when off-slab
177 * bufctls are used. The limit is the size of the largest general cache
178 * that does not use off-slab slabs.
179 * For 32bit archs with 4 kB pages, is this 56.
180 * This is not serious, as it is only for large objects, when it is unwise
181 * to have too many per slab.
182 * Note: This limit can be raised by introducing a general cache whose size
183 * is less than 512 (PAGE_SIZE<<3), but greater than 256.
184 */
185
Kyle Moffettfa5b08d2005-09-03 15:55:03 -0700186typedef unsigned int kmem_bufctl_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#define BUFCTL_END (((kmem_bufctl_t)(~0U))-0)
188#define BUFCTL_FREE (((kmem_bufctl_t)(~0U))-1)
Al Viro871751e2006-03-25 03:06:39 -0800189#define BUFCTL_ACTIVE (((kmem_bufctl_t)(~0U))-2)
190#define SLAB_LIMIT (((kmem_bufctl_t)(~0U))-3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 * struct slab_rcu
194 *
195 * slab_destroy on a SLAB_DESTROY_BY_RCU cache uses this structure to
196 * arrange for kmem_freepages to be called via RCU. This is useful if
197 * we need to approach a kernel structure obliquely, from its address
198 * obtained without the usual locking. We can lock the structure to
199 * stabilize it and check it's still at the given address, only if we
200 * can be sure that the memory has not been meanwhile reused for some
201 * other kind of object (which our subsystem's lock might corrupt).
202 *
203 * rcu_read_lock before reading the address, then rcu_read_unlock after
204 * taking the spinlock within the structure expected at that address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 */
206struct slab_rcu {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800207 struct rcu_head head;
Pekka Enberg343e0d72006-02-01 03:05:50 -0800208 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800209 void *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210};
211
212/*
Lai Jiangshan5bfe53a2011-03-10 15:22:24 +0800213 * struct slab
214 *
215 * Manages the objs in a slab. Placed either at the beginning of mem allocated
216 * for a slab, or allocated from an general cache.
217 * Slabs are chained into three list: fully used, partial, fully free slabs.
218 */
219struct slab {
220 union {
221 struct {
222 struct list_head list;
223 unsigned long colouroff;
224 void *s_mem; /* including colour offset */
225 unsigned int inuse; /* num of objs active in slab */
226 kmem_bufctl_t free;
227 unsigned short nodeid;
228 };
229 struct slab_rcu __slab_cover_slab_rcu;
230 };
231};
232
233/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * struct array_cache
235 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 * Purpose:
237 * - LIFO ordering, to hand out cache-warm objects from _alloc
238 * - reduce the number of linked list operations
239 * - reduce spinlock operations
240 *
241 * The limit is stored in the per-cpu structure to reduce the data cache
242 * footprint.
243 *
244 */
245struct array_cache {
246 unsigned int avail;
247 unsigned int limit;
248 unsigned int batchcount;
249 unsigned int touched;
Christoph Lametere498be72005-09-09 13:03:32 -0700250 spinlock_t lock;
Robert P. J. Daybda5b652007-10-16 23:30:05 -0700251 void *entry[]; /*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800252 * Must have this definition in here for the proper
253 * alignment of array_cache. Also simplifies accessing
254 * the entries.
Mel Gorman072bb0a2012-07-31 16:43:58 -0700255 *
256 * Entries should not be directly dereferenced as
257 * entries belonging to slabs marked pfmemalloc will
258 * have the lower bits set SLAB_OBJ_PFMEMALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -0800259 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260};
261
Mel Gorman072bb0a2012-07-31 16:43:58 -0700262#define SLAB_OBJ_PFMEMALLOC 1
263static inline bool is_obj_pfmemalloc(void *objp)
264{
265 return (unsigned long)objp & SLAB_OBJ_PFMEMALLOC;
266}
267
268static inline void set_obj_pfmemalloc(void **objp)
269{
270 *objp = (void *)((unsigned long)*objp | SLAB_OBJ_PFMEMALLOC);
271 return;
272}
273
274static inline void clear_obj_pfmemalloc(void **objp)
275{
276 *objp = (void *)((unsigned long)*objp & ~SLAB_OBJ_PFMEMALLOC);
277}
278
Andrew Mortona737b3e2006-03-22 00:08:11 -0800279/*
280 * bootstrap: The caches do not work without cpuarrays anymore, but the
281 * cpuarrays are allocated from the generic caches...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 */
283#define BOOT_CPUCACHE_ENTRIES 1
284struct arraycache_init {
285 struct array_cache cache;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800286 void *entries[BOOT_CPUCACHE_ENTRIES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287};
288
289/*
Christoph Lametere498be72005-09-09 13:03:32 -0700290 * Need this for bootstrapping a per node allocator.
291 */
Pekka Enberg556a1692008-01-25 08:20:51 +0200292#define NUM_INIT_LISTS (3 * MAX_NUMNODES)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000293static struct kmem_cache_node __initdata init_kmem_cache_node[NUM_INIT_LISTS];
Christoph Lametere498be72005-09-09 13:03:32 -0700294#define CACHE_CACHE 0
Pekka Enberg556a1692008-01-25 08:20:51 +0200295#define SIZE_AC MAX_NUMNODES
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000296#define SIZE_NODE (2 * MAX_NUMNODES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
Christoph Lametered11d9e2006-06-30 01:55:45 -0700298static int drain_freelist(struct kmem_cache *cache,
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000299 struct kmem_cache_node *n, int tofree);
Christoph Lametered11d9e2006-06-30 01:55:45 -0700300static void free_block(struct kmem_cache *cachep, void **objpp, int len,
301 int node);
Pekka Enberg83b519e2009-06-10 19:40:04 +0300302static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp);
David Howells65f27f32006-11-22 14:55:48 +0000303static void cache_reap(struct work_struct *unused);
Christoph Lametered11d9e2006-06-30 01:55:45 -0700304
Ingo Molnare0a42722006-06-23 02:03:46 -0700305static int slab_early_init = 1;
306
Christoph Lametere3366012013-01-10 19:14:18 +0000307#define INDEX_AC kmalloc_index(sizeof(struct arraycache_init))
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000308#define INDEX_NODE kmalloc_index(sizeof(struct kmem_cache_node))
Christoph Lametere498be72005-09-09 13:03:32 -0700309
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000310static void kmem_cache_node_init(struct kmem_cache_node *parent)
Christoph Lametere498be72005-09-09 13:03:32 -0700311{
312 INIT_LIST_HEAD(&parent->slabs_full);
313 INIT_LIST_HEAD(&parent->slabs_partial);
314 INIT_LIST_HEAD(&parent->slabs_free);
315 parent->shared = NULL;
316 parent->alien = NULL;
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -0800317 parent->colour_next = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700318 spin_lock_init(&parent->list_lock);
319 parent->free_objects = 0;
320 parent->free_touched = 0;
321}
322
Andrew Mortona737b3e2006-03-22 00:08:11 -0800323#define MAKE_LIST(cachep, listp, slab, nodeid) \
324 do { \
325 INIT_LIST_HEAD(listp); \
Christoph Lameter6a673682013-01-10 19:14:19 +0000326 list_splice(&(cachep->node[nodeid]->slab), listp); \
Christoph Lametere498be72005-09-09 13:03:32 -0700327 } while (0)
328
Andrew Mortona737b3e2006-03-22 00:08:11 -0800329#define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
330 do { \
Christoph Lametere498be72005-09-09 13:03:32 -0700331 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
332 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
333 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
334 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336#define CFLGS_OFF_SLAB (0x80000000UL)
337#define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
338
339#define BATCHREFILL_LIMIT 16
Andrew Mortona737b3e2006-03-22 00:08:11 -0800340/*
341 * Optimization question: fewer reaps means less probability for unnessary
342 * cpucache drain/refill cycles.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 *
Adrian Bunkdc6f3f22005-11-08 16:44:08 +0100344 * OTOH the cpuarrays can contain lots of objects,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 * which could lock up otherwise freeable slabs.
346 */
347#define REAPTIMEOUT_CPUC (2*HZ)
348#define REAPTIMEOUT_LIST3 (4*HZ)
349
350#if STATS
351#define STATS_INC_ACTIVE(x) ((x)->num_active++)
352#define STATS_DEC_ACTIVE(x) ((x)->num_active--)
353#define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
354#define STATS_INC_GROWN(x) ((x)->grown++)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700355#define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
Andrew Mortona737b3e2006-03-22 00:08:11 -0800356#define STATS_SET_HIGH(x) \
357 do { \
358 if ((x)->num_active > (x)->high_mark) \
359 (x)->high_mark = (x)->num_active; \
360 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361#define STATS_INC_ERR(x) ((x)->errors++)
362#define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
Christoph Lametere498be72005-09-09 13:03:32 -0700363#define STATS_INC_NODEFREES(x) ((x)->node_frees++)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700364#define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800365#define STATS_SET_FREEABLE(x, i) \
366 do { \
367 if ((x)->max_freeable < i) \
368 (x)->max_freeable = i; \
369 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370#define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
371#define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
372#define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
373#define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
374#else
375#define STATS_INC_ACTIVE(x) do { } while (0)
376#define STATS_DEC_ACTIVE(x) do { } while (0)
377#define STATS_INC_ALLOCED(x) do { } while (0)
378#define STATS_INC_GROWN(x) do { } while (0)
Andi Kleen4e60c862010-08-09 17:19:03 -0700379#define STATS_ADD_REAPED(x,y) do { (void)(y); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380#define STATS_SET_HIGH(x) do { } while (0)
381#define STATS_INC_ERR(x) do { } while (0)
382#define STATS_INC_NODEALLOCS(x) do { } while (0)
Christoph Lametere498be72005-09-09 13:03:32 -0700383#define STATS_INC_NODEFREES(x) do { } while (0)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700384#define STATS_INC_ACOVERFLOW(x) do { } while (0)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800385#define STATS_SET_FREEABLE(x, i) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386#define STATS_INC_ALLOCHIT(x) do { } while (0)
387#define STATS_INC_ALLOCMISS(x) do { } while (0)
388#define STATS_INC_FREEHIT(x) do { } while (0)
389#define STATS_INC_FREEMISS(x) do { } while (0)
390#endif
391
392#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Andrew Mortona737b3e2006-03-22 00:08:11 -0800394/*
395 * memory layout of objects:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 * 0 : objp
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800397 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 * the end of an object is aligned with the end of the real
399 * allocation. Catches writes behind the end of the allocation.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800400 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 * redzone word.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800402 * cachep->obj_offset: The real object.
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500403 * cachep->size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
404 * cachep->size - 1* BYTES_PER_WORD: last caller address
Andrew Mortona737b3e2006-03-22 00:08:11 -0800405 * [BYTES_PER_WORD long]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800407static int obj_offset(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800409 return cachep->obj_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
David Woodhouseb46b8f12007-05-08 00:22:59 -0700412static unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
David Woodhouseb46b8f12007-05-08 00:22:59 -0700415 return (unsigned long long*) (objp + obj_offset(cachep) -
416 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417}
418
David Woodhouseb46b8f12007-05-08 00:22:59 -0700419static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
422 if (cachep->flags & SLAB_STORE_USER)
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500423 return (unsigned long long *)(objp + cachep->size -
David Woodhouseb46b8f12007-05-08 00:22:59 -0700424 sizeof(unsigned long long) -
David Woodhouse87a927c2007-07-04 21:26:44 -0400425 REDZONE_ALIGN);
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500426 return (unsigned long long *) (objp + cachep->size -
David Woodhouseb46b8f12007-05-08 00:22:59 -0700427 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Pekka Enberg343e0d72006-02-01 03:05:50 -0800430static void **dbg_userword(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
432 BUG_ON(!(cachep->flags & SLAB_STORE_USER));
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500433 return (void **)(objp + cachep->size - BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436#else
437
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800438#define obj_offset(x) 0
David Woodhouseb46b8f12007-05-08 00:22:59 -0700439#define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
440#define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441#define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
442
443#endif
444
445/*
David Rientjes3df1ccc2011-10-18 22:09:28 -0700446 * Do not go above this order unless 0 objects fit into the slab or
447 * overridden on the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 */
David Rientjes543585c2011-10-18 22:09:24 -0700449#define SLAB_MAX_ORDER_HI 1
450#define SLAB_MAX_ORDER_LO 0
451static int slab_max_order = SLAB_MAX_ORDER_LO;
David Rientjes3df1ccc2011-10-18 22:09:28 -0700452static bool slab_max_order_set __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800454static inline struct kmem_cache *virt_to_cache(const void *obj)
455{
Christoph Lameterb49af682007-05-06 14:49:41 -0700456 struct page *page = virt_to_head_page(obj);
Christoph Lameter35026082012-06-13 10:24:56 -0500457 return page->slab_cache;
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800458}
459
460static inline struct slab *virt_to_slab(const void *obj)
461{
Christoph Lameterb49af682007-05-06 14:49:41 -0700462 struct page *page = virt_to_head_page(obj);
Christoph Lameter35026082012-06-13 10:24:56 -0500463
464 VM_BUG_ON(!PageSlab(page));
465 return page->slab_page;
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800466}
467
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800468static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
469 unsigned int idx)
470{
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500471 return slab->s_mem + cache->size * idx;
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800472}
473
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800474/*
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500475 * We want to avoid an expensive divide : (offset / cache->size)
476 * Using the fact that size is a constant for a particular cache,
477 * we can replace (offset / cache->size) by
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800478 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
479 */
480static inline unsigned int obj_to_index(const struct kmem_cache *cache,
481 const struct slab *slab, void *obj)
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800482{
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800483 u32 offset = (obj - slab->s_mem);
484 return reciprocal_divide(offset, cache->reciprocal_buffer_size);
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800485}
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487static struct arraycache_init initarray_generic =
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800488 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
490/* internal cache of cache description objs */
Christoph Lameter9b030cb2012-09-05 00:20:33 +0000491static struct kmem_cache kmem_cache_boot = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800492 .batchcount = 1,
493 .limit = BOOT_CPUCACHE_ENTRIES,
494 .shared = 1,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500495 .size = sizeof(struct kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800496 .name = "kmem_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497};
498
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700499#define BAD_ALIEN_MAGIC 0x01020304ul
500
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200501#ifdef CONFIG_LOCKDEP
502
503/*
504 * Slab sometimes uses the kmalloc slabs to store the slab headers
505 * for other slabs "off slab".
506 * The locking for this is tricky in that it nests within the locks
507 * of all other slabs in a few places; to deal with this special
508 * locking we put on-slab caches into a separate lock-class.
509 *
510 * We set lock class for alien array caches which are up during init.
511 * The lock annotation will be lost if all cpus of a node goes down and
512 * then comes back up during hotplug
513 */
514static struct lock_class_key on_slab_l3_key;
515static struct lock_class_key on_slab_alc_key;
516
Peter Zijlstra83835b32011-07-22 15:26:05 +0200517static struct lock_class_key debugobj_l3_key;
518static struct lock_class_key debugobj_alc_key;
519
520static void slab_set_lock_classes(struct kmem_cache *cachep,
521 struct lock_class_key *l3_key, struct lock_class_key *alc_key,
522 int q)
523{
524 struct array_cache **alc;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000525 struct kmem_cache_node *n;
Peter Zijlstra83835b32011-07-22 15:26:05 +0200526 int r;
527
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000528 n = cachep->node[q];
529 if (!n)
Peter Zijlstra83835b32011-07-22 15:26:05 +0200530 return;
531
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000532 lockdep_set_class(&n->list_lock, l3_key);
533 alc = n->alien;
Peter Zijlstra83835b32011-07-22 15:26:05 +0200534 /*
535 * FIXME: This check for BAD_ALIEN_MAGIC
536 * should go away when common slab code is taught to
537 * work even without alien caches.
538 * Currently, non NUMA code returns BAD_ALIEN_MAGIC
539 * for alloc_alien_cache,
540 */
541 if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC)
542 return;
543 for_each_node(r) {
544 if (alc[r])
545 lockdep_set_class(&alc[r]->lock, alc_key);
546 }
547}
548
549static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
550{
551 slab_set_lock_classes(cachep, &debugobj_l3_key, &debugobj_alc_key, node);
552}
553
554static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
555{
556 int node;
557
558 for_each_online_node(node)
559 slab_set_debugobj_lock_classes_node(cachep, node);
560}
561
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200562static void init_node_lock_keys(int q)
563{
Christoph Lametere3366012013-01-10 19:14:18 +0000564 int i;
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200565
Christoph Lameter97d06602012-07-06 15:25:11 -0500566 if (slab_state < UP)
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200567 return;
568
Christoph Lameter002b98a2013-07-02 12:12:10 -0700569 for (i = 1; i <= KMALLOC_SHIFT_HIGH; i++) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000570 struct kmem_cache_node *n;
Christoph Lametere3366012013-01-10 19:14:18 +0000571 struct kmem_cache *cache = kmalloc_caches[i];
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200572
Christoph Lametere3366012013-01-10 19:14:18 +0000573 if (!cache)
Pekka Enberg00afa752009-12-27 14:33:14 +0200574 continue;
Peter Zijlstra83835b32011-07-22 15:26:05 +0200575
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000576 n = cache->node[q];
577 if (!n || OFF_SLAB(cache))
Christoph Lametere3366012013-01-10 19:14:18 +0000578 continue;
579
580 slab_set_lock_classes(cache, &on_slab_l3_key,
Peter Zijlstra83835b32011-07-22 15:26:05 +0200581 &on_slab_alc_key, q);
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200582 }
583}
584
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800585static void on_slab_lock_classes_node(struct kmem_cache *cachep, int q)
586{
Christoph Lameter6a673682013-01-10 19:14:19 +0000587 if (!cachep->node[q])
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800588 return;
589
590 slab_set_lock_classes(cachep, &on_slab_l3_key,
591 &on_slab_alc_key, q);
592}
593
594static inline void on_slab_lock_classes(struct kmem_cache *cachep)
595{
596 int node;
597
598 VM_BUG_ON(OFF_SLAB(cachep));
599 for_each_node(node)
600 on_slab_lock_classes_node(cachep, node);
601}
602
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200603static inline void init_lock_keys(void)
604{
605 int node;
606
607 for_each_node(node)
608 init_node_lock_keys(node);
609}
610#else
611static void init_node_lock_keys(int q)
612{
613}
614
615static inline void init_lock_keys(void)
616{
617}
Peter Zijlstra83835b32011-07-22 15:26:05 +0200618
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800619static inline void on_slab_lock_classes(struct kmem_cache *cachep)
620{
621}
622
623static inline void on_slab_lock_classes_node(struct kmem_cache *cachep, int node)
624{
625}
626
Peter Zijlstra83835b32011-07-22 15:26:05 +0200627static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
628{
629}
630
631static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
632{
633}
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200634#endif
635
Tejun Heo1871e522009-10-29 22:34:13 +0900636static DEFINE_PER_CPU(struct delayed_work, slab_reap_work);
Peter Zijlstra696ac522009-07-03 08:44:43 -0500637static DEFINE_PER_CPU(struct list_head, slab_free_list);
Thomas Gleixner4a621b32011-06-18 19:44:43 +0200638static DEFINE_LOCAL_IRQ_LOCK(slab_lock);
639
640#ifndef CONFIG_PREEMPT_RT_BASE
641# define slab_on_each_cpu(func, cp) on_each_cpu(func, cp, 1)
642#else
643/*
644 * execute func() for all CPUs. On PREEMPT_RT we dont actually have
645 * to run on the remote CPUs - we only have to take their CPU-locks.
646 * (This is a rare operation, so cacheline bouncing is not an issue.)
647 */
648static void
649slab_on_each_cpu(void (*func)(void *arg, int this_cpu), void *arg)
650{
651 unsigned int i;
652
653 get_cpu_light();
654 for_each_online_cpu(i)
655 func(arg, i);
656 put_cpu_light();
657}
658
659static void lock_slab_on(unsigned int cpu)
660{
661 local_lock_irq_on(slab_lock, cpu);
662}
663
664static void unlock_slab_on(unsigned int cpu)
665{
666 local_unlock_irq_on(slab_lock, cpu);
667}
668#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Peter Zijlstra696ac522009-07-03 08:44:43 -0500670static void free_delayed(struct list_head *h)
671{
672 while(!list_empty(h)) {
673 struct page *page = list_first_entry(h, struct page, lru);
674
675 list_del(&page->lru);
676 __free_pages(page, page->index);
677 }
678}
679
680static void unlock_l3_and_free_delayed(spinlock_t *list_lock)
681{
682 LIST_HEAD(tmp);
683
684 list_splice_init(&__get_cpu_var(slab_free_list), &tmp);
685 local_spin_unlock_irq(slab_lock, list_lock);
686 free_delayed(&tmp);
687}
688
689static void unlock_slab_and_free_delayed(unsigned long flags)
690{
691 LIST_HEAD(tmp);
692
693 list_splice_init(&__get_cpu_var(slab_free_list), &tmp);
694 local_unlock_irqrestore(slab_lock, flags);
695 free_delayed(&tmp);
696}
697
Pekka Enberg343e0d72006-02-01 03:05:50 -0800698static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
700 return cachep->array[smp_processor_id()];
701}
702
Thomas Gleixner4a621b32011-06-18 19:44:43 +0200703static inline struct array_cache *cpu_cache_get_on_cpu(struct kmem_cache *cachep,
704 int cpu)
705{
706 return cachep->array[cpu];
707}
708
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800709static size_t slab_mgmt_size(size_t nr_objs, size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800711 return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align);
712}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Andrew Mortona737b3e2006-03-22 00:08:11 -0800714/*
715 * Calculate the number of objects and left-over bytes for a given buffer size.
716 */
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800717static void cache_estimate(unsigned long gfporder, size_t buffer_size,
718 size_t align, int flags, size_t *left_over,
719 unsigned int *num)
720{
721 int nr_objs;
722 size_t mgmt_size;
723 size_t slab_size = PAGE_SIZE << gfporder;
724
725 /*
726 * The slab management structure can be either off the slab or
727 * on it. For the latter case, the memory allocated for a
728 * slab is used for:
729 *
730 * - The struct slab
731 * - One kmem_bufctl_t for each object
732 * - Padding to respect alignment of @align
733 * - @buffer_size bytes for each object
734 *
735 * If the slab management structure is off the slab, then the
736 * alignment will already be calculated into the size. Because
737 * the slabs are all pages aligned, the objects will be at the
738 * correct alignment when allocated.
739 */
740 if (flags & CFLGS_OFF_SLAB) {
741 mgmt_size = 0;
742 nr_objs = slab_size / buffer_size;
743
744 if (nr_objs > SLAB_LIMIT)
745 nr_objs = SLAB_LIMIT;
746 } else {
747 /*
748 * Ignore padding for the initial guess. The padding
749 * is at most @align-1 bytes, and @buffer_size is at
750 * least @align. In the worst case, this result will
751 * be one greater than the number of objects that fit
752 * into the memory allocation when taking the padding
753 * into account.
754 */
755 nr_objs = (slab_size - sizeof(struct slab)) /
756 (buffer_size + sizeof(kmem_bufctl_t));
757
758 /*
759 * This calculated number will be either the right
760 * amount, or one greater than what we want.
761 */
762 if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
763 > slab_size)
764 nr_objs--;
765
766 if (nr_objs > SLAB_LIMIT)
767 nr_objs = SLAB_LIMIT;
768
769 mgmt_size = slab_mgmt_size(nr_objs, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800771 *num = nr_objs;
772 *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Christoph Lameterf28510d2012-09-11 19:49:38 +0000775#if DEBUG
Harvey Harrisond40cee22008-04-30 00:55:07 -0700776#define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Andrew Mortona737b3e2006-03-22 00:08:11 -0800778static void __slab_error(const char *function, struct kmem_cache *cachep,
779 char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
781 printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800782 function, cachep->name, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +1030784 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
Christoph Lameterf28510d2012-09-11 19:49:38 +0000786#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Paul Menage3395ee02006-12-06 20:32:16 -0800788/*
789 * By default on NUMA we use alien caches to stage the freeing of
790 * objects allocated from other nodes. This causes massive memory
791 * inefficiencies when using fake NUMA setup to split memory into a
792 * large number of small nodes, so it can be disabled on the command
793 * line
794 */
795
796static int use_alien_caches __read_mostly = 1;
797static int __init noaliencache_setup(char *s)
798{
799 use_alien_caches = 0;
800 return 1;
801}
802__setup("noaliencache", noaliencache_setup);
803
David Rientjes3df1ccc2011-10-18 22:09:28 -0700804static int __init slab_max_order_setup(char *str)
805{
806 get_option(&str, &slab_max_order);
807 slab_max_order = slab_max_order < 0 ? 0 :
808 min(slab_max_order, MAX_ORDER - 1);
809 slab_max_order_set = true;
810
811 return 1;
812}
813__setup("slab_max_order=", slab_max_order_setup);
814
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800815#ifdef CONFIG_NUMA
816/*
817 * Special reaping functions for NUMA systems called from cache_reap().
818 * These take care of doing round robin flushing of alien caches (containing
819 * objects freed on different nodes from which they were allocated) and the
820 * flushing of remote pcps by calling drain_node_pages.
821 */
Tejun Heo1871e522009-10-29 22:34:13 +0900822static DEFINE_PER_CPU(unsigned long, slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800823
824static void init_reap_node(int cpu)
825{
826 int node;
827
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -0700828 node = next_node(cpu_to_mem(cpu), node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800829 if (node == MAX_NUMNODES)
Paul Jackson442295c2006-03-22 00:09:11 -0800830 node = first_node(node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800831
Tejun Heo1871e522009-10-29 22:34:13 +0900832 per_cpu(slab_reap_node, cpu) = node;
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800833}
834
835static void next_reap_node(void)
836{
Christoph Lameter909ea962010-12-08 16:22:55 +0100837 int node = __this_cpu_read(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800838
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800839 node = next_node(node, node_online_map);
840 if (unlikely(node >= MAX_NUMNODES))
841 node = first_node(node_online_map);
Christoph Lameter909ea962010-12-08 16:22:55 +0100842 __this_cpu_write(slab_reap_node, node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800843}
844
845#else
846#define init_reap_node(cpu) do { } while (0)
847#define next_reap_node(void) do { } while (0)
848#endif
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850/*
851 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
852 * via the workqueue/eventd.
853 * Add the CPU number into the expiration time to minimize the possibility of
854 * the CPUs getting into lockstep and contending for the global cache chain
855 * lock.
856 */
Adrian Bunk897e6792007-07-15 23:38:20 -0700857static void __cpuinit start_cpu_timer(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Tejun Heo1871e522009-10-29 22:34:13 +0900859 struct delayed_work *reap_work = &per_cpu(slab_reap_work, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 /*
862 * When this gets called from do_initcalls via cpucache_init(),
863 * init_workqueues() has already run, so keventd will be setup
864 * at that time.
865 */
David Howells52bad642006-11-22 14:54:01 +0000866 if (keventd_up() && reap_work->work.func == NULL) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800867 init_reap_node(cpu);
Tejun Heo203b42f2012-08-21 13:18:23 -0700868 INIT_DEFERRABLE_WORK(reap_work, cache_reap);
Arjan van de Ven2b284212006-12-10 02:21:28 -0800869 schedule_delayed_work_on(cpu, reap_work,
870 __round_jiffies_relative(HZ, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
872}
873
Christoph Lametere498be72005-09-09 13:03:32 -0700874static struct array_cache *alloc_arraycache(int node, int entries,
Pekka Enberg83b519e2009-06-10 19:40:04 +0300875 int batchcount, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876{
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800877 int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 struct array_cache *nc = NULL;
879
Pekka Enberg83b519e2009-06-10 19:40:04 +0300880 nc = kmalloc_node(memsize, gfp, node);
Catalin Marinasd5cff632009-06-11 13:22:40 +0100881 /*
882 * The array_cache structures contain pointers to free object.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300883 * However, when such objects are allocated or transferred to another
Catalin Marinasd5cff632009-06-11 13:22:40 +0100884 * cache the pointers are not cleared and they could be counted as
885 * valid references during a kmemleak scan. Therefore, kmemleak must
886 * not scan such objects.
887 */
888 kmemleak_no_scan(nc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (nc) {
890 nc->avail = 0;
891 nc->limit = entries;
892 nc->batchcount = batchcount;
893 nc->touched = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700894 spin_lock_init(&nc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 }
896 return nc;
897}
898
Mel Gorman072bb0a2012-07-31 16:43:58 -0700899static inline bool is_slab_pfmemalloc(struct slab *slabp)
900{
901 struct page *page = virt_to_page(slabp->s_mem);
902
903 return PageSlabPfmemalloc(page);
904}
905
906/* Clears pfmemalloc_active if no slabs have pfmalloc set */
907static void recheck_pfmemalloc_active(struct kmem_cache *cachep,
908 struct array_cache *ac)
909{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000910 struct kmem_cache_node *n = cachep->node[numa_mem_id()];
Mel Gorman072bb0a2012-07-31 16:43:58 -0700911 struct slab *slabp;
912 unsigned long flags;
913
914 if (!pfmemalloc_active)
915 return;
916
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000917 spin_lock_irqsave(&n->list_lock, flags);
918 list_for_each_entry(slabp, &n->slabs_full, list)
Mel Gorman072bb0a2012-07-31 16:43:58 -0700919 if (is_slab_pfmemalloc(slabp))
920 goto out;
921
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000922 list_for_each_entry(slabp, &n->slabs_partial, list)
Mel Gorman072bb0a2012-07-31 16:43:58 -0700923 if (is_slab_pfmemalloc(slabp))
924 goto out;
925
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000926 list_for_each_entry(slabp, &n->slabs_free, list)
Mel Gorman072bb0a2012-07-31 16:43:58 -0700927 if (is_slab_pfmemalloc(slabp))
928 goto out;
929
930 pfmemalloc_active = false;
931out:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000932 spin_unlock_irqrestore(&n->list_lock, flags);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700933}
934
Mel Gorman381760e2012-07-31 16:44:30 -0700935static void *__ac_get_obj(struct kmem_cache *cachep, struct array_cache *ac,
Mel Gorman072bb0a2012-07-31 16:43:58 -0700936 gfp_t flags, bool force_refill)
937{
938 int i;
939 void *objp = ac->entry[--ac->avail];
940
941 /* Ensure the caller is allowed to use objects from PFMEMALLOC slab */
942 if (unlikely(is_obj_pfmemalloc(objp))) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000943 struct kmem_cache_node *n;
Mel Gorman072bb0a2012-07-31 16:43:58 -0700944
945 if (gfp_pfmemalloc_allowed(flags)) {
946 clear_obj_pfmemalloc(&objp);
947 return objp;
948 }
949
950 /* The caller cannot use PFMEMALLOC objects, find another one */
Joonsoo Kimd014dc22012-09-17 14:09:06 -0700951 for (i = 0; i < ac->avail; i++) {
Mel Gorman072bb0a2012-07-31 16:43:58 -0700952 /* If a !PFMEMALLOC object is found, swap them */
953 if (!is_obj_pfmemalloc(ac->entry[i])) {
954 objp = ac->entry[i];
955 ac->entry[i] = ac->entry[ac->avail];
956 ac->entry[ac->avail] = objp;
957 return objp;
958 }
959 }
960
961 /*
962 * If there are empty slabs on the slabs_free list and we are
963 * being forced to refill the cache, mark this one !pfmemalloc.
964 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000965 n = cachep->node[numa_mem_id()];
966 if (!list_empty(&n->slabs_free) && force_refill) {
Mel Gorman072bb0a2012-07-31 16:43:58 -0700967 struct slab *slabp = virt_to_slab(objp);
Mel Gorman30c29be2012-09-17 14:09:03 -0700968 ClearPageSlabPfmemalloc(virt_to_head_page(slabp->s_mem));
Mel Gorman072bb0a2012-07-31 16:43:58 -0700969 clear_obj_pfmemalloc(&objp);
970 recheck_pfmemalloc_active(cachep, ac);
971 return objp;
972 }
973
974 /* No !PFMEMALLOC objects available */
975 ac->avail++;
976 objp = NULL;
977 }
978
979 return objp;
980}
981
Mel Gorman381760e2012-07-31 16:44:30 -0700982static inline void *ac_get_obj(struct kmem_cache *cachep,
983 struct array_cache *ac, gfp_t flags, bool force_refill)
984{
985 void *objp;
986
987 if (unlikely(sk_memalloc_socks()))
988 objp = __ac_get_obj(cachep, ac, flags, force_refill);
989 else
990 objp = ac->entry[--ac->avail];
991
992 return objp;
993}
994
995static void *__ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
Mel Gorman072bb0a2012-07-31 16:43:58 -0700996 void *objp)
997{
998 if (unlikely(pfmemalloc_active)) {
999 /* Some pfmemalloc slabs exist, check if this is one */
Mel Gorman30c29be2012-09-17 14:09:03 -07001000 struct page *page = virt_to_head_page(objp);
Mel Gorman072bb0a2012-07-31 16:43:58 -07001001 if (PageSlabPfmemalloc(page))
1002 set_obj_pfmemalloc(&objp);
1003 }
1004
Mel Gorman381760e2012-07-31 16:44:30 -07001005 return objp;
1006}
1007
1008static inline void ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
1009 void *objp)
1010{
1011 if (unlikely(sk_memalloc_socks()))
1012 objp = __ac_put_obj(cachep, ac, objp);
1013
Mel Gorman072bb0a2012-07-31 16:43:58 -07001014 ac->entry[ac->avail++] = objp;
1015}
1016
Christoph Lameter3ded1752006-03-25 03:06:44 -08001017/*
1018 * Transfer objects in one arraycache to another.
1019 * Locking must be handled by the caller.
1020 *
1021 * Return the number of entries transferred.
1022 */
1023static int transfer_objects(struct array_cache *to,
1024 struct array_cache *from, unsigned int max)
1025{
1026 /* Figure out how many entries to transfer */
Hagen Paul Pfeifer732eacc2010-10-26 14:22:23 -07001027 int nr = min3(from->avail, max, to->limit - to->avail);
Christoph Lameter3ded1752006-03-25 03:06:44 -08001028
1029 if (!nr)
1030 return 0;
1031
1032 memcpy(to->entry + to->avail, from->entry + from->avail -nr,
1033 sizeof(void *) *nr);
1034
1035 from->avail -= nr;
1036 to->avail += nr;
Christoph Lameter3ded1752006-03-25 03:06:44 -08001037 return nr;
1038}
1039
Christoph Lameter765c4502006-09-27 01:50:08 -07001040#ifndef CONFIG_NUMA
1041
1042#define drain_alien_cache(cachep, alien) do { } while (0)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001043#define reap_alien(cachep, n) do { } while (0)
Christoph Lameter765c4502006-09-27 01:50:08 -07001044
Pekka Enberg83b519e2009-06-10 19:40:04 +03001045static inline struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
Christoph Lameter765c4502006-09-27 01:50:08 -07001046{
1047 return (struct array_cache **)BAD_ALIEN_MAGIC;
1048}
1049
1050static inline void free_alien_cache(struct array_cache **ac_ptr)
1051{
1052}
1053
1054static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
1055{
1056 return 0;
1057}
1058
1059static inline void *alternate_node_alloc(struct kmem_cache *cachep,
1060 gfp_t flags)
1061{
1062 return NULL;
1063}
1064
Christoph Hellwig8b98c162006-12-06 20:32:30 -08001065static inline void *____cache_alloc_node(struct kmem_cache *cachep,
Christoph Lameter765c4502006-09-27 01:50:08 -07001066 gfp_t flags, int nodeid)
1067{
1068 return NULL;
1069}
1070
1071#else /* CONFIG_NUMA */
1072
Christoph Hellwig8b98c162006-12-06 20:32:30 -08001073static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
Paul Jacksonc61afb12006-03-24 03:16:08 -08001074static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
Christoph Lameterdc85da12006-01-18 17:42:36 -08001075
Pekka Enberg83b519e2009-06-10 19:40:04 +03001076static struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -07001077{
1078 struct array_cache **ac_ptr;
Christoph Lameter8ef82862007-02-20 13:57:52 -08001079 int memsize = sizeof(void *) * nr_node_ids;
Christoph Lametere498be72005-09-09 13:03:32 -07001080 int i;
1081
1082 if (limit > 1)
1083 limit = 12;
Haicheng Lif3186a92010-01-06 15:25:23 +08001084 ac_ptr = kzalloc_node(memsize, gfp, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001085 if (ac_ptr) {
1086 for_each_node(i) {
Haicheng Lif3186a92010-01-06 15:25:23 +08001087 if (i == node || !node_online(i))
Christoph Lametere498be72005-09-09 13:03:32 -07001088 continue;
Pekka Enberg83b519e2009-06-10 19:40:04 +03001089 ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d, gfp);
Christoph Lametere498be72005-09-09 13:03:32 -07001090 if (!ac_ptr[i]) {
Akinobu Mitacc550de2007-11-14 16:58:35 -08001091 for (i--; i >= 0; i--)
Christoph Lametere498be72005-09-09 13:03:32 -07001092 kfree(ac_ptr[i]);
1093 kfree(ac_ptr);
1094 return NULL;
1095 }
1096 }
1097 }
1098 return ac_ptr;
1099}
1100
Pekka Enberg5295a742006-02-01 03:05:48 -08001101static void free_alien_cache(struct array_cache **ac_ptr)
Christoph Lametere498be72005-09-09 13:03:32 -07001102{
1103 int i;
1104
1105 if (!ac_ptr)
1106 return;
Christoph Lametere498be72005-09-09 13:03:32 -07001107 for_each_node(i)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001108 kfree(ac_ptr[i]);
Christoph Lametere498be72005-09-09 13:03:32 -07001109 kfree(ac_ptr);
1110}
1111
Pekka Enberg343e0d72006-02-01 03:05:50 -08001112static void __drain_alien_cache(struct kmem_cache *cachep,
Pekka Enberg5295a742006-02-01 03:05:48 -08001113 struct array_cache *ac, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07001114{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001115 struct kmem_cache_node *n = cachep->node[node];
Christoph Lametere498be72005-09-09 13:03:32 -07001116
1117 if (ac->avail) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001118 spin_lock(&n->list_lock);
Christoph Lametere00946f2006-03-25 03:06:45 -08001119 /*
1120 * Stuff objects into the remote nodes shared array first.
1121 * That way we could avoid the overhead of putting the objects
1122 * into the free lists and getting them back later.
1123 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001124 if (n->shared)
1125 transfer_objects(n->shared, ac, ac->limit);
Christoph Lametere00946f2006-03-25 03:06:45 -08001126
Christoph Lameterff694162005-09-22 21:44:02 -07001127 free_block(cachep, ac->entry, ac->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001128 ac->avail = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001129 spin_unlock(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07001130 }
1131}
1132
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001133/*
1134 * Called from cache_reap() to regularly drain alien caches round robin.
1135 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001136static void reap_alien(struct kmem_cache *cachep, struct kmem_cache_node *n)
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001137{
Christoph Lameter909ea962010-12-08 16:22:55 +01001138 int node = __this_cpu_read(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001139
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001140 if (n->alien) {
1141 struct array_cache *ac = n->alien[node];
Christoph Lametere00946f2006-03-25 03:06:45 -08001142
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001143 if (ac && ac->avail &&
1144 local_spin_trylock_irq(slab_lock, &ac->lock)) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001145 __drain_alien_cache(cachep, ac, node);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001146 local_spin_unlock_irq(slab_lock, &ac->lock);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001147 }
1148 }
1149}
1150
Andrew Mortona737b3e2006-03-22 00:08:11 -08001151static void drain_alien_cache(struct kmem_cache *cachep,
1152 struct array_cache **alien)
Christoph Lametere498be72005-09-09 13:03:32 -07001153{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001154 int i = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07001155 struct array_cache *ac;
1156 unsigned long flags;
1157
1158 for_each_online_node(i) {
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001159 ac = alien[i];
Christoph Lametere498be72005-09-09 13:03:32 -07001160 if (ac) {
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001161 local_spin_lock_irqsave(slab_lock, &ac->lock, flags);
Christoph Lametere498be72005-09-09 13:03:32 -07001162 __drain_alien_cache(cachep, ac, i);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001163 local_spin_unlock_irqrestore(slab_lock, &ac->lock, flags);
Christoph Lametere498be72005-09-09 13:03:32 -07001164 }
1165 }
1166}
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001167
Ingo Molnar873623d2006-07-13 14:44:38 +02001168static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001169{
1170 struct slab *slabp = virt_to_slab(objp);
1171 int nodeid = slabp->nodeid;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001172 struct kmem_cache_node *n;
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001173 struct array_cache *alien = NULL;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001174 int node;
1175
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001176 node = numa_mem_id();
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001177
1178 /*
1179 * Make sure we are not freeing a object from another node to the array
1180 * cache on this cpu.
1181 */
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001182 if (likely(slabp->nodeid == node))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001183 return 0;
1184
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001185 n = cachep->node[node];
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001186 STATS_INC_NODEFREES(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001187 if (n->alien && n->alien[nodeid]) {
1188 alien = n->alien[nodeid];
Ingo Molnar873623d2006-07-13 14:44:38 +02001189 spin_lock(&alien->lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001190 if (unlikely(alien->avail == alien->limit)) {
1191 STATS_INC_ACOVERFLOW(cachep);
1192 __drain_alien_cache(cachep, alien, nodeid);
1193 }
Mel Gorman072bb0a2012-07-31 16:43:58 -07001194 ac_put_obj(cachep, alien, objp);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001195 spin_unlock(&alien->lock);
1196 } else {
Christoph Lameter6a673682013-01-10 19:14:19 +00001197 spin_lock(&(cachep->node[nodeid])->list_lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001198 free_block(cachep, &objp, 1, nodeid);
Christoph Lameter6a673682013-01-10 19:14:19 +00001199 spin_unlock(&(cachep->node[nodeid])->list_lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001200 }
1201 return 1;
1202}
Christoph Lametere498be72005-09-09 13:03:32 -07001203#endif
1204
David Rientjes8f9f8d92010-03-27 19:40:47 -07001205/*
Christoph Lameter6a673682013-01-10 19:14:19 +00001206 * Allocates and initializes node for a node on each slab cache, used for
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001207 * either memory or cpu hotplug. If memory is being hot-added, the kmem_cache_node
David Rientjes8f9f8d92010-03-27 19:40:47 -07001208 * will be allocated off-node since memory is not yet online for the new node.
Christoph Lameter6a673682013-01-10 19:14:19 +00001209 * When hotplugging memory or a cpu, existing node are not replaced if
David Rientjes8f9f8d92010-03-27 19:40:47 -07001210 * already in use.
1211 *
Christoph Lameter18004c52012-07-06 15:25:12 -05001212 * Must hold slab_mutex.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001213 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001214static int init_cache_node_node(int node)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001215{
1216 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001217 struct kmem_cache_node *n;
Christoph Lameter6744f0872013-01-10 19:12:17 +00001218 const int memsize = sizeof(struct kmem_cache_node);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001219
Christoph Lameter18004c52012-07-06 15:25:12 -05001220 list_for_each_entry(cachep, &slab_caches, list) {
David Rientjes8f9f8d92010-03-27 19:40:47 -07001221 /*
1222 * Set up the size64 kmemlist for cpu before we can
1223 * begin anything. Make sure some other cpu on this
1224 * node has not already allocated this
1225 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001226 if (!cachep->node[node]) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001227 n = kmalloc_node(memsize, GFP_KERNEL, node);
1228 if (!n)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001229 return -ENOMEM;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001230 kmem_cache_node_init(n);
1231 n->next_reap = jiffies + REAPTIMEOUT_LIST3 +
David Rientjes8f9f8d92010-03-27 19:40:47 -07001232 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1233
1234 /*
1235 * The l3s don't come and go as CPUs come and
Christoph Lameter18004c52012-07-06 15:25:12 -05001236 * go. slab_mutex is sufficient
David Rientjes8f9f8d92010-03-27 19:40:47 -07001237 * protection here.
1238 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001239 cachep->node[node] = n;
David Rientjes8f9f8d92010-03-27 19:40:47 -07001240 }
1241
Peter Zijlstra696ac522009-07-03 08:44:43 -05001242 local_spin_lock_irq(slab_lock, &cachep->node[node]->list_lock);
Christoph Lameter6a673682013-01-10 19:14:19 +00001243 cachep->node[node]->free_limit =
David Rientjes8f9f8d92010-03-27 19:40:47 -07001244 (1 + nr_cpus_node(node)) *
1245 cachep->batchcount + cachep->num;
Peter Zijlstra696ac522009-07-03 08:44:43 -05001246 local_spin_unlock_irq(slab_lock, &cachep->node[node]->list_lock);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001247 }
1248 return 0;
1249}
1250
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001251static void __cpuinit cpuup_canceled(long cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001253 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001254 struct kmem_cache_node *n = NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001255 int node = cpu_to_mem(cpu);
Rusty Russella70f7302009-03-13 14:49:46 +10301256 const struct cpumask *mask = cpumask_of_node(node);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001257
Christoph Lameter18004c52012-07-06 15:25:12 -05001258 list_for_each_entry(cachep, &slab_caches, list) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001259 struct array_cache *nc;
1260 struct array_cache *shared;
1261 struct array_cache **alien;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001262
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001263 /* cpu is dead; no one can alloc from it. */
1264 nc = cachep->array[cpu];
1265 cachep->array[cpu] = NULL;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001266 n = cachep->node[node];
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001267
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001268 if (!n)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001269 goto free_array_cache;
1270
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001271 local_spin_lock_irq(slab_lock, &n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001272
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001273 /* Free limit for this kmem_cache_node */
1274 n->free_limit -= cachep->batchcount;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001275 if (nc)
1276 free_block(cachep, nc->entry, nc->avail, node);
1277
Rusty Russell58463c12009-12-17 11:43:12 -06001278 if (!cpumask_empty(mask)) {
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001279 local_spin_unlock_irq(slab_lock, &n->list_lock);
Peter Zijlstra696ac522009-07-03 08:44:43 -05001280 unlock_l3_and_free_delayed(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001281 goto free_array_cache;
1282 }
1283
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001284 shared = n->shared;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001285 if (shared) {
1286 free_block(cachep, shared->entry,
1287 shared->avail, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001288 n->shared = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001289 }
1290
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001291 alien = n->alien;
1292 n->alien = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001293
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001294 local_spin_unlock_irq(slab_lock, &n->list_lock);
Peter Zijlstra696ac522009-07-03 08:44:43 -05001295 unlock_l3_and_free_delayed(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001296
1297 kfree(shared);
1298 if (alien) {
1299 drain_alien_cache(cachep, alien);
1300 free_alien_cache(alien);
1301 }
1302free_array_cache:
1303 kfree(nc);
1304 }
1305 /*
1306 * In the previous loop, all the objects were freed to
1307 * the respective cache's slabs, now we can go ahead and
1308 * shrink each nodelist to its limit.
1309 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001310 list_for_each_entry(cachep, &slab_caches, list) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001311 n = cachep->node[node];
1312 if (!n)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001313 continue;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001314 drain_freelist(cachep, n, n->free_objects);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001315 }
1316}
1317
1318static int __cpuinit cpuup_prepare(long cpu)
1319{
Pekka Enberg343e0d72006-02-01 03:05:50 -08001320 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001321 struct kmem_cache_node *n = NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001322 int node = cpu_to_mem(cpu);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001323 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001325 /*
1326 * We need to do this right in the beginning since
1327 * alloc_arraycache's are going to use this list.
1328 * kmalloc_node allows us to add the slab to the right
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001329 * kmem_cache_node and not this cpu's kmem_cache_node
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001330 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001331 err = init_cache_node_node(node);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001332 if (err < 0)
1333 goto bad;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001334
1335 /*
1336 * Now we can go ahead with allocating the shared arrays and
1337 * array caches
1338 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001339 list_for_each_entry(cachep, &slab_caches, list) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001340 struct array_cache *nc;
1341 struct array_cache *shared = NULL;
1342 struct array_cache **alien = NULL;
1343
1344 nc = alloc_arraycache(node, cachep->limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001345 cachep->batchcount, GFP_KERNEL);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001346 if (!nc)
1347 goto bad;
1348 if (cachep->shared) {
1349 shared = alloc_arraycache(node,
1350 cachep->shared * cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001351 0xbaadf00d, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001352 if (!shared) {
1353 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001354 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001355 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001356 }
1357 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03001358 alien = alloc_alien_cache(node, cachep->limit, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001359 if (!alien) {
1360 kfree(shared);
1361 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001362 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001363 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001364 }
1365 cachep->array[cpu] = nc;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001366 n = cachep->node[node];
1367 BUG_ON(!n);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001368
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001369 local_spin_lock_irq(slab_lock, &n->list_lock);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001370 if (!n->shared) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001371 /*
1372 * We are serialised from CPU_DEAD or
1373 * CPU_UP_CANCELLED by the cpucontrol lock
1374 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001375 n->shared = shared;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001376 shared = NULL;
1377 }
1378#ifdef CONFIG_NUMA
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001379 if (!n->alien) {
1380 n->alien = alien;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001381 alien = NULL;
1382 }
1383#endif
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001384 local_spin_unlock_irq(slab_lock, &n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001385 kfree(shared);
1386 free_alien_cache(alien);
Peter Zijlstra83835b32011-07-22 15:26:05 +02001387 if (cachep->flags & SLAB_DEBUG_OBJECTS)
1388 slab_set_debugobj_lock_classes_node(cachep, node);
Glauber Costa6ccfb5b2012-12-18 14:22:31 -08001389 else if (!OFF_SLAB(cachep) &&
1390 !(cachep->flags & SLAB_DESTROY_BY_RCU))
1391 on_slab_lock_classes_node(cachep, node);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001392 }
Pekka Enbergce79ddc2009-11-23 22:01:15 +02001393 init_node_lock_keys(node);
1394
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001395 return 0;
1396bad:
Akinobu Mita12d00f62007-10-18 03:05:11 -07001397 cpuup_canceled(cpu);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001398 return -ENOMEM;
1399}
1400
1401static int __cpuinit cpuup_callback(struct notifier_block *nfb,
1402 unsigned long action, void *hcpu)
1403{
1404 long cpu = (long)hcpu;
1405 int err = 0;
1406
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 switch (action) {
Heiko Carstens38c3bd92007-05-09 02:34:05 -07001408 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001409 case CPU_UP_PREPARE_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001410 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001411 err = cpuup_prepare(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001412 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 break;
1414 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001415 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 start_cpu_timer(cpu);
1417 break;
1418#ifdef CONFIG_HOTPLUG_CPU
Christoph Lameter5830c592007-05-09 02:34:22 -07001419 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001420 case CPU_DOWN_PREPARE_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001421 /*
Christoph Lameter18004c52012-07-06 15:25:12 -05001422 * Shutdown cache reaper. Note that the slab_mutex is
Christoph Lameter5830c592007-05-09 02:34:22 -07001423 * held so that if cache_reap() is invoked it cannot do
1424 * anything expensive but will only modify reap_work
1425 * and reschedule the timer.
1426 */
Tejun Heoafe2c512010-12-14 16:21:17 +01001427 cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu));
Christoph Lameter5830c592007-05-09 02:34:22 -07001428 /* Now the cache_reaper is guaranteed to be not running. */
Tejun Heo1871e522009-10-29 22:34:13 +09001429 per_cpu(slab_reap_work, cpu).work.func = NULL;
Christoph Lameter5830c592007-05-09 02:34:22 -07001430 break;
1431 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001432 case CPU_DOWN_FAILED_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001433 start_cpu_timer(cpu);
1434 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001436 case CPU_DEAD_FROZEN:
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001437 /*
1438 * Even if all the cpus of a node are down, we don't free the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001439 * kmem_cache_node of any cache. This to avoid a race between
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001440 * cpu_down, and a kmalloc allocation from another cpu for
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001441 * memory from the node of the cpu going down. The node
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001442 * structure is usually allocated from kmem_cache_create() and
1443 * gets destroyed at kmem_cache_destroy().
1444 */
Simon Arlott183ff222007-10-20 01:27:18 +02001445 /* fall through */
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08001446#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001448 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001449 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001450 cpuup_canceled(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001451 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
Akinobu Mitaeac40682010-05-26 14:43:32 -07001454 return notifier_from_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455}
1456
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001457static struct notifier_block __cpuinitdata cpucache_notifier = {
1458 &cpuup_callback, NULL, 0
1459};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
David Rientjes8f9f8d92010-03-27 19:40:47 -07001461#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
1462/*
1463 * Drains freelist for a node on each slab cache, used for memory hot-remove.
1464 * Returns -EBUSY if all objects cannot be drained so that the node is not
1465 * removed.
1466 *
Christoph Lameter18004c52012-07-06 15:25:12 -05001467 * Must hold slab_mutex.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001468 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001469static int __meminit drain_cache_node_node(int node)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001470{
1471 struct kmem_cache *cachep;
1472 int ret = 0;
1473
Christoph Lameter18004c52012-07-06 15:25:12 -05001474 list_for_each_entry(cachep, &slab_caches, list) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001475 struct kmem_cache_node *n;
David Rientjes8f9f8d92010-03-27 19:40:47 -07001476
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001477 n = cachep->node[node];
1478 if (!n)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001479 continue;
1480
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001481 drain_freelist(cachep, n, n->free_objects);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001482
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001483 if (!list_empty(&n->slabs_full) ||
1484 !list_empty(&n->slabs_partial)) {
David Rientjes8f9f8d92010-03-27 19:40:47 -07001485 ret = -EBUSY;
1486 break;
1487 }
1488 }
1489 return ret;
1490}
1491
1492static int __meminit slab_memory_callback(struct notifier_block *self,
1493 unsigned long action, void *arg)
1494{
1495 struct memory_notify *mnb = arg;
1496 int ret = 0;
1497 int nid;
1498
1499 nid = mnb->status_change_nid;
1500 if (nid < 0)
1501 goto out;
1502
1503 switch (action) {
1504 case MEM_GOING_ONLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001505 mutex_lock(&slab_mutex);
Christoph Lameter6a673682013-01-10 19:14:19 +00001506 ret = init_cache_node_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001507 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001508 break;
1509 case MEM_GOING_OFFLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001510 mutex_lock(&slab_mutex);
Christoph Lameter6a673682013-01-10 19:14:19 +00001511 ret = drain_cache_node_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001512 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001513 break;
1514 case MEM_ONLINE:
1515 case MEM_OFFLINE:
1516 case MEM_CANCEL_ONLINE:
1517 case MEM_CANCEL_OFFLINE:
1518 break;
1519 }
1520out:
Prarit Bhargava5fda1bd2011-03-22 16:30:49 -07001521 return notifier_from_errno(ret);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001522}
1523#endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
1524
Christoph Lametere498be72005-09-09 13:03:32 -07001525/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001526 * swap the static kmem_cache_node with kmalloced memory
Christoph Lametere498be72005-09-09 13:03:32 -07001527 */
Christoph Lameter6744f0872013-01-10 19:12:17 +00001528static void __init init_list(struct kmem_cache *cachep, struct kmem_cache_node *list,
David Rientjes8f9f8d92010-03-27 19:40:47 -07001529 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07001530{
Christoph Lameter6744f0872013-01-10 19:12:17 +00001531 struct kmem_cache_node *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001532
Christoph Lameter6744f0872013-01-10 19:12:17 +00001533 ptr = kmalloc_node(sizeof(struct kmem_cache_node), GFP_NOWAIT, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07001534 BUG_ON(!ptr);
1535
Christoph Lameter6744f0872013-01-10 19:12:17 +00001536 memcpy(ptr, list, sizeof(struct kmem_cache_node));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001537 /*
1538 * Do not assume that spinlocks can be initialized via memcpy:
1539 */
1540 spin_lock_init(&ptr->list_lock);
1541
Christoph Lametere498be72005-09-09 13:03:32 -07001542 MAKE_ALL_LISTS(cachep, ptr, nodeid);
Christoph Lameter6a673682013-01-10 19:14:19 +00001543 cachep->node[nodeid] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001544}
1545
Andrew Mortona737b3e2006-03-22 00:08:11 -08001546/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001547 * For setting up all the kmem_cache_node for cache whose buffer_size is same as
1548 * size of kmem_cache_node.
Pekka Enberg556a1692008-01-25 08:20:51 +02001549 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001550static void __init set_up_node(struct kmem_cache *cachep, int index)
Pekka Enberg556a1692008-01-25 08:20:51 +02001551{
1552 int node;
1553
1554 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001555 cachep->node[node] = &init_kmem_cache_node[index + node];
Christoph Lameter6a673682013-01-10 19:14:19 +00001556 cachep->node[node]->next_reap = jiffies +
Pekka Enberg556a1692008-01-25 08:20:51 +02001557 REAPTIMEOUT_LIST3 +
1558 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1559 }
1560}
1561
1562/*
Christoph Lameter3c583462012-11-28 16:23:01 +00001563 * The memory after the last cpu cache pointer is used for the
Christoph Lameter6a673682013-01-10 19:14:19 +00001564 * the node pointer.
Christoph Lameter3c583462012-11-28 16:23:01 +00001565 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001566static void setup_node_pointer(struct kmem_cache *cachep)
Christoph Lameter3c583462012-11-28 16:23:01 +00001567{
Christoph Lameter6a673682013-01-10 19:14:19 +00001568 cachep->node = (struct kmem_cache_node **)&cachep->array[nr_cpu_ids];
Christoph Lameter3c583462012-11-28 16:23:01 +00001569}
1570
1571/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08001572 * Initialisation. Called after the page allocator have been initialised and
1573 * before smp_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 */
1575void __init kmem_cache_init(void)
1576{
Christoph Lametere498be72005-09-09 13:03:32 -07001577 int i;
1578
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001579 kmem_cache = &kmem_cache_boot;
Christoph Lameter6a673682013-01-10 19:14:19 +00001580 setup_node_pointer(kmem_cache);
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001581
Mel Gormanb6e68bc2009-06-16 15:32:16 -07001582 if (num_possible_nodes() == 1)
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001583 use_alien_caches = 0;
1584
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001585 local_irq_lock_init(slab_lock);
Peter Zijlstra696ac522009-07-03 08:44:43 -05001586 for_each_possible_cpu(i)
1587 INIT_LIST_HEAD(&per_cpu(slab_free_list, i));
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001588
Christoph Lameter3c583462012-11-28 16:23:01 +00001589 for (i = 0; i < NUM_INIT_LISTS; i++)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001590 kmem_cache_node_init(&init_kmem_cache_node[i]);
Christoph Lameter3c583462012-11-28 16:23:01 +00001591
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001592 set_up_node(kmem_cache, CACHE_CACHE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593
1594 /*
1595 * Fragmentation resistance on low memory - only use bigger
David Rientjes3df1ccc2011-10-18 22:09:28 -07001596 * page orders on machines with more than 32MB of memory if
1597 * not overridden on the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 */
David Rientjes3df1ccc2011-10-18 22:09:28 -07001599 if (!slab_max_order_set && totalram_pages > (32 << 20) >> PAGE_SHIFT)
David Rientjes543585c2011-10-18 22:09:24 -07001600 slab_max_order = SLAB_MAX_ORDER_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 /* Bootstrap is tricky, because several objects are allocated
1603 * from caches that do not exist yet:
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001604 * 1) initialize the kmem_cache cache: it contains the struct
1605 * kmem_cache structures of all caches, except kmem_cache itself:
1606 * kmem_cache is statically allocated.
Christoph Lametere498be72005-09-09 13:03:32 -07001607 * Initially an __init data area is used for the head array and the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001608 * kmem_cache_node structures, it's replaced with a kmalloc allocated
Christoph Lametere498be72005-09-09 13:03:32 -07001609 * array at the end of the bootstrap.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 * 2) Create the first kmalloc cache.
Pekka Enberg343e0d72006-02-01 03:05:50 -08001611 * The struct kmem_cache for the new cache is allocated normally.
Christoph Lametere498be72005-09-09 13:03:32 -07001612 * An __init data area is used for the head array.
1613 * 3) Create the remaining kmalloc caches, with minimally sized
1614 * head arrays.
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001615 * 4) Replace the __init data head arrays for kmem_cache and the first
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 * kmalloc cache with kmalloc allocated arrays.
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001617 * 5) Replace the __init data for kmem_cache_node for kmem_cache and
Christoph Lametere498be72005-09-09 13:03:32 -07001618 * the other cache's with kmalloc allocated memory.
1619 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 */
1621
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001622 /* 1) create the kmem_cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Eric Dumazet8da34302007-05-06 14:49:29 -07001624 /*
Eric Dumazetb56efcf2011-07-20 19:04:23 +02001625 * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
Eric Dumazet8da34302007-05-06 14:49:29 -07001626 */
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001627 create_boot_cache(kmem_cache, "kmem_cache",
1628 offsetof(struct kmem_cache, array[nr_cpu_ids]) +
Christoph Lameter6744f0872013-01-10 19:12:17 +00001629 nr_node_ids * sizeof(struct kmem_cache_node *),
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001630 SLAB_HWCACHE_ALIGN);
1631 list_add(&kmem_cache->list, &slab_caches);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
1633 /* 2+3) create the kmalloc caches */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Andrew Mortona737b3e2006-03-22 00:08:11 -08001635 /*
1636 * Initialize the caches that provide memory for the array cache and the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001637 * kmem_cache_node structures first. Without this, further allocations will
Andrew Mortona737b3e2006-03-22 00:08:11 -08001638 * bug.
Christoph Lametere498be72005-09-09 13:03:32 -07001639 */
1640
Christoph Lametere3366012013-01-10 19:14:18 +00001641 kmalloc_caches[INDEX_AC] = create_kmalloc_cache("kmalloc-ac",
1642 kmalloc_size(INDEX_AC), ARCH_KMALLOC_FLAGS);
Christoph Lametere498be72005-09-09 13:03:32 -07001643
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001644 if (INDEX_AC != INDEX_NODE)
1645 kmalloc_caches[INDEX_NODE] =
1646 create_kmalloc_cache("kmalloc-node",
1647 kmalloc_size(INDEX_NODE), ARCH_KMALLOC_FLAGS);
Christoph Lametere498be72005-09-09 13:03:32 -07001648
Ingo Molnare0a42722006-06-23 02:03:46 -07001649 slab_early_init = 0;
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 /* 4) Replace the bootstrap head arrays */
1652 {
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001653 struct array_cache *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001654
Pekka Enberg83b519e2009-06-10 19:40:04 +03001655 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001656
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001657 memcpy(ptr, cpu_cache_get(kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001658 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001659 /*
1660 * Do not assume that spinlocks can be initialized via memcpy:
1661 */
1662 spin_lock_init(&ptr->lock);
1663
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001664 kmem_cache->array[smp_processor_id()] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001665
Pekka Enberg83b519e2009-06-10 19:40:04 +03001666 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001667
Christoph Lametere3366012013-01-10 19:14:18 +00001668 BUG_ON(cpu_cache_get(kmalloc_caches[INDEX_AC])
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001669 != &initarray_generic.cache);
Christoph Lametere3366012013-01-10 19:14:18 +00001670 memcpy(ptr, cpu_cache_get(kmalloc_caches[INDEX_AC]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001671 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001672 /*
1673 * Do not assume that spinlocks can be initialized via memcpy:
1674 */
1675 spin_lock_init(&ptr->lock);
1676
Christoph Lametere3366012013-01-10 19:14:18 +00001677 kmalloc_caches[INDEX_AC]->array[smp_processor_id()] = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001679 /* 5) Replace the bootstrap kmem_cache_node */
Christoph Lametere498be72005-09-09 13:03:32 -07001680 {
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001681 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682
Mel Gorman9c09a952008-01-24 05:49:54 -08001683 for_each_online_node(nid) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001684 init_list(kmem_cache, &init_kmem_cache_node[CACHE_CACHE + nid], nid);
Pekka Enberg556a1692008-01-25 08:20:51 +02001685
Christoph Lametere3366012013-01-10 19:14:18 +00001686 init_list(kmalloc_caches[INDEX_AC],
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001687 &init_kmem_cache_node[SIZE_AC + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001688
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001689 if (INDEX_AC != INDEX_NODE) {
1690 init_list(kmalloc_caches[INDEX_NODE],
1691 &init_kmem_cache_node[SIZE_NODE + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001692 }
1693 }
1694 }
1695
Christoph Lameterf97d5f62013-01-10 19:12:17 +00001696 create_kmalloc_caches(ARCH_KMALLOC_FLAGS);
Pekka Enberg8429db52009-06-12 15:58:59 +03001697}
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001698
Pekka Enberg8429db52009-06-12 15:58:59 +03001699void __init kmem_cache_init_late(void)
1700{
1701 struct kmem_cache *cachep;
1702
Christoph Lameter97d06602012-07-06 15:25:11 -05001703 slab_state = UP;
Peter Zijlstra52cef182011-11-28 21:12:40 +01001704
Pekka Enberg8429db52009-06-12 15:58:59 +03001705 /* 6) resize the head arrays to their final sizes */
Christoph Lameter18004c52012-07-06 15:25:12 -05001706 mutex_lock(&slab_mutex);
1707 list_for_each_entry(cachep, &slab_caches, list)
Pekka Enberg8429db52009-06-12 15:58:59 +03001708 if (enable_cpucache(cachep, GFP_NOWAIT))
1709 BUG();
Christoph Lameter18004c52012-07-06 15:25:12 -05001710 mutex_unlock(&slab_mutex);
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001711
Michael Wang947ca182012-09-05 10:33:18 +08001712 /* Annotate slab for lockdep -- annotate the malloc caches */
1713 init_lock_keys();
1714
Christoph Lameter97d06602012-07-06 15:25:11 -05001715 /* Done! */
1716 slab_state = FULL;
1717
Andrew Mortona737b3e2006-03-22 00:08:11 -08001718 /*
1719 * Register a cpu startup notifier callback that initializes
1720 * cpu_cache_get for all new cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 */
1722 register_cpu_notifier(&cpucache_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
David Rientjes8f9f8d92010-03-27 19:40:47 -07001724#ifdef CONFIG_NUMA
1725 /*
1726 * Register a memory hotplug callback that initializes and frees
Christoph Lameter6a673682013-01-10 19:14:19 +00001727 * node.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001728 */
1729 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
1730#endif
1731
Andrew Mortona737b3e2006-03-22 00:08:11 -08001732 /*
1733 * The reap timers are started later, with a module init call: That part
1734 * of the kernel is not yet operational.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 */
1736}
1737
1738static int __init cpucache_init(void)
1739{
1740 int cpu;
1741
Andrew Mortona737b3e2006-03-22 00:08:11 -08001742 /*
1743 * Register the timers that return unneeded pages to the page allocator
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 */
Christoph Lametere498be72005-09-09 13:03:32 -07001745 for_each_online_cpu(cpu)
Andrew Mortona737b3e2006-03-22 00:08:11 -08001746 start_cpu_timer(cpu);
Glauber Costaa164f8962012-06-21 00:59:18 +04001747
1748 /* Done! */
Christoph Lameter97d06602012-07-06 15:25:11 -05001749 slab_state = FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 return 0;
1751}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752__initcall(cpucache_init);
1753
Rafael Aquini8bdec192012-03-09 17:27:27 -03001754static noinline void
1755slab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid)
1756{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001757 struct kmem_cache_node *n;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001758 struct slab *slabp;
1759 unsigned long flags;
1760 int node;
1761
1762 printk(KERN_WARNING
1763 "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n",
1764 nodeid, gfpflags);
1765 printk(KERN_WARNING " cache: %s, object size: %d, order: %d\n",
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05001766 cachep->name, cachep->size, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001767
1768 for_each_online_node(node) {
1769 unsigned long active_objs = 0, num_objs = 0, free_objects = 0;
1770 unsigned long active_slabs = 0, num_slabs = 0;
1771
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001772 n = cachep->node[node];
1773 if (!n)
Rafael Aquini8bdec192012-03-09 17:27:27 -03001774 continue;
1775
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001776 spin_lock_irqsave(&n->list_lock, flags);
1777 list_for_each_entry(slabp, &n->slabs_full, list) {
Rafael Aquini8bdec192012-03-09 17:27:27 -03001778 active_objs += cachep->num;
1779 active_slabs++;
1780 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001781 list_for_each_entry(slabp, &n->slabs_partial, list) {
Rafael Aquini8bdec192012-03-09 17:27:27 -03001782 active_objs += slabp->inuse;
1783 active_slabs++;
1784 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001785 list_for_each_entry(slabp, &n->slabs_free, list)
Rafael Aquini8bdec192012-03-09 17:27:27 -03001786 num_slabs++;
1787
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001788 free_objects += n->free_objects;
1789 spin_unlock_irqrestore(&n->list_lock, flags);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001790
1791 num_slabs += active_slabs;
1792 num_objs = num_slabs * cachep->num;
1793 printk(KERN_WARNING
1794 " node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
1795 node, active_slabs, num_slabs, active_objs, num_objs,
1796 free_objects);
1797 }
1798}
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800/*
1801 * Interface to system's page allocator. No need to hold the cache-lock.
1802 *
1803 * If we requested dmaable memory, we will get it. Even if we
1804 * did not request dmaable memory, we might get it, but that
1805 * would be relatively rare and ignorable.
1806 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001807static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808{
1809 struct page *page;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001810 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 int i;
1812
Luke Yangd6fef9d2006-04-10 22:52:56 -07001813#ifndef CONFIG_MMU
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001814 /*
1815 * Nommu uses slab's for process anonymous memory allocations, and thus
1816 * requires __GFP_COMP to properly refcount higher order allocations
Luke Yangd6fef9d2006-04-10 22:52:56 -07001817 */
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001818 flags |= __GFP_COMP;
Luke Yangd6fef9d2006-04-10 22:52:56 -07001819#endif
Christoph Lameter765c4502006-09-27 01:50:08 -07001820
Glauber Costaa618e892012-06-14 16:17:21 +04001821 flags |= cachep->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001822 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1823 flags |= __GFP_RECLAIMABLE;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001824
Linus Torvalds517d0862009-06-16 19:50:13 -07001825 page = alloc_pages_exact_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001826 if (!page) {
1827 if (!(flags & __GFP_NOWARN) && printk_ratelimit())
1828 slab_out_of_memory(cachep, flags, nodeid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 return NULL;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Mel Gormanb37f1dd2012-07-31 16:44:03 -07001832 /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
Mel Gorman072bb0a2012-07-31 16:43:58 -07001833 if (unlikely(page->pfmemalloc))
1834 pfmemalloc_active = true;
1835
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001836 nr_pages = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
Christoph Lameter972d1a72006-09-25 23:31:51 -07001838 add_zone_page_state(page_zone(page),
1839 NR_SLAB_RECLAIMABLE, nr_pages);
1840 else
1841 add_zone_page_state(page_zone(page),
1842 NR_SLAB_UNRECLAIMABLE, nr_pages);
Mel Gorman072bb0a2012-07-31 16:43:58 -07001843 for (i = 0; i < nr_pages; i++) {
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001844 __SetPageSlab(page + i);
Pekka Enbergc175eea2008-05-09 20:35:53 +02001845
Mel Gorman072bb0a2012-07-31 16:43:58 -07001846 if (page->pfmemalloc)
1847 SetPageSlabPfmemalloc(page + i);
1848 }
Glauber Costa1f458cb2012-12-18 14:22:50 -08001849 memcg_bind_pages(cachep, cachep->gfporder);
Mel Gorman072bb0a2012-07-31 16:43:58 -07001850
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001851 if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
1852 kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
1853
1854 if (cachep->ctor)
1855 kmemcheck_mark_uninitialized_pages(page, nr_pages);
1856 else
1857 kmemcheck_mark_unallocated_pages(page, nr_pages);
1858 }
Pekka Enbergc175eea2008-05-09 20:35:53 +02001859
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001860 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861}
1862
1863/*
1864 * Interface to system's page release.
1865 */
Peter Zijlstra696ac522009-07-03 08:44:43 -05001866static void kmem_freepages(struct kmem_cache *cachep, void *addr, bool delayed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001868 unsigned long i = (1 << cachep->gfporder);
Peter Zijlstra696ac522009-07-03 08:44:43 -05001869 struct page *page, *basepage = virt_to_page(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 const unsigned long nr_freed = i;
1871
Peter Zijlstra696ac522009-07-03 08:44:43 -05001872 page = basepage;
1873
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001874 kmemcheck_free_shadow(page, cachep->gfporder);
Pekka Enbergc175eea2008-05-09 20:35:53 +02001875
Christoph Lameter972d1a72006-09-25 23:31:51 -07001876 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1877 sub_zone_page_state(page_zone(page),
1878 NR_SLAB_RECLAIMABLE, nr_freed);
1879 else
1880 sub_zone_page_state(page_zone(page),
1881 NR_SLAB_UNRECLAIMABLE, nr_freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 while (i--) {
Nick Pigginf205b2f2006-03-22 00:08:02 -08001883 BUG_ON(!PageSlab(page));
Mel Gorman072bb0a2012-07-31 16:43:58 -07001884 __ClearPageSlabPfmemalloc(page);
Nick Pigginf205b2f2006-03-22 00:08:02 -08001885 __ClearPageSlab(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 page++;
1887 }
Glauber Costa1f458cb2012-12-18 14:22:50 -08001888
1889 memcg_release_pages(cachep, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 if (current->reclaim_state)
1891 current->reclaim_state->reclaimed_slab += nr_freed;
Peter Zijlstra696ac522009-07-03 08:44:43 -05001892 if (!delayed) {
1893 free_memcg_kmem_pages((unsigned long)addr, cachep->gfporder);
1894 } else {
1895 basepage->index = cachep->gfporder;
1896 list_add(&basepage->lru, &__get_cpu_var(slab_free_list));
1897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898}
1899
1900static void kmem_rcu_free(struct rcu_head *head)
1901{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001902 struct slab_rcu *slab_rcu = (struct slab_rcu *)head;
Pekka Enberg343e0d72006-02-01 03:05:50 -08001903 struct kmem_cache *cachep = slab_rcu->cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904
Peter Zijlstra696ac522009-07-03 08:44:43 -05001905 kmem_freepages(cachep, slab_rcu->addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (OFF_SLAB(cachep))
1907 kmem_cache_free(cachep->slabp_cache, slab_rcu);
1908}
1909
1910#if DEBUG
1911
1912#ifdef CONFIG_DEBUG_PAGEALLOC
Pekka Enberg343e0d72006-02-01 03:05:50 -08001913static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001914 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001916 int size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001918 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001920 if (size < 5 * sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 return;
1922
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001923 *addr++ = 0x12345678;
1924 *addr++ = caller;
1925 *addr++ = smp_processor_id();
1926 size -= 3 * sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 {
1928 unsigned long *sptr = &caller;
1929 unsigned long svalue;
1930
1931 while (!kstack_end(sptr)) {
1932 svalue = *sptr++;
1933 if (kernel_text_address(svalue)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001934 *addr++ = svalue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 size -= sizeof(unsigned long);
1936 if (size <= sizeof(unsigned long))
1937 break;
1938 }
1939 }
1940
1941 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001942 *addr++ = 0x87654321;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943}
1944#endif
1945
Pekka Enberg343e0d72006-02-01 03:05:50 -08001946static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001948 int size = cachep->object_size;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001949 addr = &((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 memset(addr, val, size);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001952 *(unsigned char *)(addr + size - 1) = POISON_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953}
1954
1955static void dump_line(char *data, int offset, int limit)
1956{
1957 int i;
Dave Jonesaa83aa42006-09-29 01:59:51 -07001958 unsigned char error = 0;
1959 int bad_count = 0;
1960
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02001961 printk(KERN_ERR "%03x: ", offset);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001962 for (i = 0; i < limit; i++) {
1963 if (data[offset + i] != POISON_FREE) {
1964 error = data[offset + i];
1965 bad_count++;
1966 }
Dave Jonesaa83aa42006-09-29 01:59:51 -07001967 }
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02001968 print_hex_dump(KERN_CONT, "", 0, 16, 1,
1969 &data[offset], limit, 1);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001970
1971 if (bad_count == 1) {
1972 error ^= POISON_FREE;
1973 if (!(error & (error - 1))) {
1974 printk(KERN_ERR "Single bit error detected. Probably "
1975 "bad RAM.\n");
1976#ifdef CONFIG_X86
1977 printk(KERN_ERR "Run memtest86+ or a similar memory "
1978 "test tool.\n");
1979#else
1980 printk(KERN_ERR "Run a memory test tool.\n");
1981#endif
1982 }
1983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984}
1985#endif
1986
1987#if DEBUG
1988
Pekka Enberg343e0d72006-02-01 03:05:50 -08001989static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990{
1991 int i, size;
1992 char *realobj;
1993
1994 if (cachep->flags & SLAB_RED_ZONE) {
David Woodhouseb46b8f12007-05-08 00:22:59 -07001995 printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001996 *dbg_redzone1(cachep, objp),
1997 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 }
1999
2000 if (cachep->flags & SLAB_STORE_USER) {
Joe Perches071361d2012-12-12 10:19:12 -08002001 printk(KERN_ERR "Last user: [<%p>](%pSR)\n",
2002 *dbg_userword(cachep, objp),
2003 *dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 }
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002005 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05002006 size = cachep->object_size;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002007 for (i = 0; i < size && lines; i += 16, lines--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 int limit;
2009 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002010 if (i + limit > size)
2011 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 dump_line(realobj, i, limit);
2013 }
2014}
2015
Pekka Enberg343e0d72006-02-01 03:05:50 -08002016static void check_poison_obj(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017{
2018 char *realobj;
2019 int size, i;
2020 int lines = 0;
2021
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002022 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05002023 size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002025 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 char exp = POISON_FREE;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002027 if (i == size - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 exp = POISON_END;
2029 if (realobj[i] != exp) {
2030 int limit;
2031 /* Mismatch ! */
2032 /* Print header */
2033 if (lines == 0) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002034 printk(KERN_ERR
Dave Jonesface37f2011-11-15 15:03:52 -08002035 "Slab corruption (%s): %s start=%p, len=%d\n",
2036 print_tainted(), cachep->name, realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 print_objinfo(cachep, objp, 0);
2038 }
2039 /* Hexdump the affected line */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002040 i = (i / 16) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002042 if (i + limit > size)
2043 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 dump_line(realobj, i, limit);
2045 i += 16;
2046 lines++;
2047 /* Limit to 5 lines */
2048 if (lines > 5)
2049 break;
2050 }
2051 }
2052 if (lines != 0) {
2053 /* Print some data about the neighboring objects, if they
2054 * exist:
2055 */
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08002056 struct slab *slabp = virt_to_slab(objp);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002057 unsigned int objnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002059 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 if (objnr) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002061 objp = index_to_obj(cachep, slabp, objnr - 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002062 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002064 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 print_objinfo(cachep, objp, 2);
2066 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002067 if (objnr + 1 < cachep->num) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002068 objp = index_to_obj(cachep, slabp, objnr + 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002069 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002071 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 print_objinfo(cachep, objp, 2);
2073 }
2074 }
2075}
2076#endif
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078#if DEBUG
Rabin Vincente79aec22008-07-04 00:40:32 +05302079static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002080{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 int i;
2082 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002083 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
2085 if (cachep->flags & SLAB_POISON) {
2086#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002087 if (cachep->size % PAGE_SIZE == 0 &&
Andrew Mortona737b3e2006-03-22 00:08:11 -08002088 OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002089 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002090 cachep->size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 else
2092 check_poison_obj(cachep, objp);
2093#else
2094 check_poison_obj(cachep, objp);
2095#endif
2096 }
2097 if (cachep->flags & SLAB_RED_ZONE) {
2098 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2099 slab_error(cachep, "start of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002100 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2102 slab_error(cachep, "end of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002103 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002106}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107#else
Rabin Vincente79aec22008-07-04 00:40:32 +05302108static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002109{
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002110}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111#endif
2112
Randy Dunlap911851e2006-03-22 00:08:14 -08002113/**
2114 * slab_destroy - destroy and release all objects in a slab
2115 * @cachep: cache pointer being destroyed
2116 * @slabp: slab pointer being destroyed
2117 *
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002118 * Destroy all the objs in a slab, and release the mem back to the system.
Andrew Mortona737b3e2006-03-22 00:08:11 -08002119 * Before calling the slab must have been unlinked from the cache. The
2120 * cache-lock is not held/needed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002121 */
Peter Zijlstra696ac522009-07-03 08:44:43 -05002122static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp,
2123 bool delayed)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002124{
2125 void *addr = slabp->s_mem - slabp->colouroff;
2126
Rabin Vincente79aec22008-07-04 00:40:32 +05302127 slab_destroy_debugcheck(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
2129 struct slab_rcu *slab_rcu;
2130
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002131 slab_rcu = (struct slab_rcu *)slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 slab_rcu->cachep = cachep;
2133 slab_rcu->addr = addr;
2134 call_rcu(&slab_rcu->head, kmem_rcu_free);
2135 } else {
Peter Zijlstra696ac522009-07-03 08:44:43 -05002136 kmem_freepages(cachep, addr, delayed);
Ingo Molnar873623d2006-07-13 14:44:38 +02002137 if (OFF_SLAB(cachep))
2138 kmem_cache_free(cachep->slabp_cache, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 }
2140}
2141
2142/**
Randy.Dunlapa70773d2006-02-01 03:05:52 -08002143 * calculate_slab_order - calculate size (page order) of slabs
2144 * @cachep: pointer to the cache that is being created
2145 * @size: size of objects to be created in this cache.
2146 * @align: required alignment for the objects.
2147 * @flags: slab allocation flags
2148 *
2149 * Also calculates the number of objects per slab.
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002150 *
2151 * This could be made much more intelligent. For now, try to avoid using
2152 * high order pages for slabs. When the gfp() functions are more friendly
2153 * towards high-order requests, this should be changed.
2154 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002155static size_t calculate_slab_order(struct kmem_cache *cachep,
Randy Dunlapee13d782006-02-01 03:05:53 -08002156 size_t size, size_t align, unsigned long flags)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002157{
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002158 unsigned long offslab_limit;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002159 size_t left_over = 0;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002160 int gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002161
Christoph Lameter0aa817f2007-05-16 22:11:01 -07002162 for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002163 unsigned int num;
2164 size_t remainder;
2165
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002166 cache_estimate(gfporder, size, align, flags, &remainder, &num);
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002167 if (!num)
2168 continue;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002169
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002170 if (flags & CFLGS_OFF_SLAB) {
2171 /*
2172 * Max number of objs-per-slab for caches which
2173 * use off-slab slabs. Needed to avoid a possible
2174 * looping condition in cache_grow().
2175 */
2176 offslab_limit = size - sizeof(struct slab);
2177 offslab_limit /= sizeof(kmem_bufctl_t);
2178
2179 if (num > offslab_limit)
2180 break;
2181 }
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002182
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002183 /* Found something acceptable - save it away */
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002184 cachep->num = num;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002185 cachep->gfporder = gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002186 left_over = remainder;
2187
2188 /*
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08002189 * A VFS-reclaimable slab tends to have most allocations
2190 * as GFP_NOFS and we really don't want to have to be allocating
2191 * higher-order pages when we are unable to shrink dcache.
2192 */
2193 if (flags & SLAB_RECLAIM_ACCOUNT)
2194 break;
2195
2196 /*
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002197 * Large number of objects is good, but very large slabs are
2198 * currently bad for the gfp()s.
2199 */
David Rientjes543585c2011-10-18 22:09:24 -07002200 if (gfporder >= slab_max_order)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002201 break;
2202
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002203 /*
2204 * Acceptable internal fragmentation?
2205 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002206 if (left_over * 8 <= (PAGE_SIZE << gfporder))
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002207 break;
2208 }
2209 return left_over;
2210}
2211
Pekka Enberg83b519e2009-06-10 19:40:04 +03002212static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002213{
Christoph Lameter97d06602012-07-06 15:25:11 -05002214 if (slab_state >= FULL)
Pekka Enberg83b519e2009-06-10 19:40:04 +03002215 return enable_cpucache(cachep, gfp);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002216
Christoph Lameter97d06602012-07-06 15:25:11 -05002217 if (slab_state == DOWN) {
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002218 /*
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002219 * Note: Creation of first cache (kmem_cache).
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002220 * The setup_node is taken care
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002221 * of by the caller of __kmem_cache_create
2222 */
2223 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2224 slab_state = PARTIAL;
2225 } else if (slab_state == PARTIAL) {
2226 /*
2227 * Note: the second kmem_cache_create must create the cache
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002228 * that's used by kmalloc(24), otherwise the creation of
2229 * further caches will BUG().
2230 */
2231 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2232
2233 /*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002234 * If the cache that's used by kmalloc(sizeof(kmem_cache_node)) is
2235 * the second cache, then we need to set up all its node/,
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002236 * otherwise the creation of further caches will BUG().
2237 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002238 set_up_node(cachep, SIZE_AC);
2239 if (INDEX_AC == INDEX_NODE)
2240 slab_state = PARTIAL_NODE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002241 else
Christoph Lameter97d06602012-07-06 15:25:11 -05002242 slab_state = PARTIAL_ARRAYCACHE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002243 } else {
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002244 /* Remaining boot caches */
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002245 cachep->array[smp_processor_id()] =
Pekka Enberg83b519e2009-06-10 19:40:04 +03002246 kmalloc(sizeof(struct arraycache_init), gfp);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002247
Christoph Lameter97d06602012-07-06 15:25:11 -05002248 if (slab_state == PARTIAL_ARRAYCACHE) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002249 set_up_node(cachep, SIZE_NODE);
2250 slab_state = PARTIAL_NODE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002251 } else {
2252 int node;
Pekka Enberg556a1692008-01-25 08:20:51 +02002253 for_each_online_node(node) {
Christoph Lameter6a673682013-01-10 19:14:19 +00002254 cachep->node[node] =
Christoph Lameter6744f0872013-01-10 19:12:17 +00002255 kmalloc_node(sizeof(struct kmem_cache_node),
Pekka Enbergeb91f1d2009-06-12 14:56:09 +03002256 gfp, node);
Christoph Lameter6a673682013-01-10 19:14:19 +00002257 BUG_ON(!cachep->node[node]);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002258 kmem_cache_node_init(cachep->node[node]);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002259 }
2260 }
2261 }
Christoph Lameter6a673682013-01-10 19:14:19 +00002262 cachep->node[numa_mem_id()]->next_reap =
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002263 jiffies + REAPTIMEOUT_LIST3 +
2264 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
2265
2266 cpu_cache_get(cachep)->avail = 0;
2267 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
2268 cpu_cache_get(cachep)->batchcount = 1;
2269 cpu_cache_get(cachep)->touched = 0;
2270 cachep->batchcount = 1;
2271 cachep->limit = BOOT_CPUCACHE_ENTRIES;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002272 return 0;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002273}
2274
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002275/**
Christoph Lameter039363f2012-07-06 15:25:10 -05002276 * __kmem_cache_create - Create a cache.
Randy Dunlapa755b762012-11-06 17:10:10 -08002277 * @cachep: cache management descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 * @flags: SLAB flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 *
2280 * Returns a ptr to the cache on success, NULL on failure.
2281 * Cannot be called within a int, but can be interrupted.
Paul Mundt20c2df82007-07-20 10:11:58 +09002282 * The @ctor is run when new pages are allocated by the cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 * The flags are
2285 *
2286 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2287 * to catch references to uninitialised memory.
2288 *
2289 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2290 * for buffer overruns.
2291 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2293 * cacheline. This can be beneficial if you're counting cycles as closely
2294 * as davem.
2295 */
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002296int
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002297__kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
2299 size_t left_over, slab_size, ralign;
Pekka Enberg83b519e2009-06-10 19:40:04 +03002300 gfp_t gfp;
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002301 int err;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002302 size_t size = cachep->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305#if FORCED_DEBUG
2306 /*
2307 * Enable redzoning and last user accounting, except for caches with
2308 * large objects, if the increased size would increase the object size
2309 * above the next power of two: caches with object sizes just above a
2310 * power of two have a significant amount of internal fragmentation.
2311 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002312 if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
2313 2 * sizeof(unsigned long long)))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002314 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 if (!(flags & SLAB_DESTROY_BY_RCU))
2316 flags |= SLAB_POISON;
2317#endif
2318 if (flags & SLAB_DESTROY_BY_RCU)
2319 BUG_ON(flags & SLAB_POISON);
2320#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Andrew Mortona737b3e2006-03-22 00:08:11 -08002322 /*
2323 * Check that size is in terms of words. This is needed to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 * unaligned accesses for some archs when redzoning is used, and makes
2325 * sure any on-slab bufctl's are also correctly aligned.
2326 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002327 if (size & (BYTES_PER_WORD - 1)) {
2328 size += (BYTES_PER_WORD - 1);
2329 size &= ~(BYTES_PER_WORD - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 }
2331
Pekka Enbergca5f9702006-09-25 23:31:25 -07002332 /*
David Woodhouse87a927c2007-07-04 21:26:44 -04002333 * Redzoning and user store require word alignment or possibly larger.
2334 * Note this will be overridden by architecture or caller mandated
2335 * alignment if either is greater than BYTES_PER_WORD.
Pekka Enbergca5f9702006-09-25 23:31:25 -07002336 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002337 if (flags & SLAB_STORE_USER)
2338 ralign = BYTES_PER_WORD;
2339
2340 if (flags & SLAB_RED_ZONE) {
2341 ralign = REDZONE_ALIGN;
2342 /* If redzoning, ensure that the second redzone is suitably
2343 * aligned, by adjusting the object size accordingly. */
2344 size += REDZONE_ALIGN - 1;
2345 size &= ~(REDZONE_ALIGN - 1);
2346 }
Pekka Enbergca5f9702006-09-25 23:31:25 -07002347
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002348 /* 3) caller mandated alignment */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002349 if (ralign < cachep->align) {
2350 ralign = cachep->align;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 }
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002352 /* disable debug if necessary */
2353 if (ralign > __alignof__(unsigned long long))
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002354 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002355 /*
Pekka Enbergca5f9702006-09-25 23:31:25 -07002356 * 4) Store it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002358 cachep->align = ralign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Pekka Enberg83b519e2009-06-10 19:40:04 +03002360 if (slab_is_available())
2361 gfp = GFP_KERNEL;
2362 else
2363 gfp = GFP_NOWAIT;
2364
Christoph Lameter6a673682013-01-10 19:14:19 +00002365 setup_node_pointer(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Pekka Enbergca5f9702006-09-25 23:31:25 -07002368 /*
2369 * Both debugging options require word-alignment which is calculated
2370 * into align above.
2371 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 if (flags & SLAB_RED_ZONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 /* add space for red zone words */
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002374 cachep->obj_offset += sizeof(unsigned long long);
2375 size += 2 * sizeof(unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
2377 if (flags & SLAB_STORE_USER) {
Pekka Enbergca5f9702006-09-25 23:31:25 -07002378 /* user store requires one word storage behind the end of
David Woodhouse87a927c2007-07-04 21:26:44 -04002379 * the real object. But if the second red zone needs to be
2380 * aligned to 64 bits, we must allow that much space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002382 if (flags & SLAB_RED_ZONE)
2383 size += REDZONE_ALIGN;
2384 else
2385 size += BYTES_PER_WORD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 }
2387#if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002388 if (size >= kmalloc_size(INDEX_NODE + 1)
Tetsuo Handa608da7e2012-09-30 17:28:25 +09002389 && cachep->object_size > cache_line_size()
2390 && ALIGN(size, cachep->align) < PAGE_SIZE) {
2391 cachep->obj_offset += PAGE_SIZE - ALIGN(size, cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 size = PAGE_SIZE;
2393 }
2394#endif
2395#endif
2396
Ingo Molnare0a42722006-06-23 02:03:46 -07002397 /*
2398 * Determine if the slab management is 'on' or 'off' slab.
2399 * (bootstrapping cannot cope with offslab caches so don't do
Catalin Marinase7cb55b2009-10-28 13:33:08 +00002400 * it too early on. Always use on-slab management when
2401 * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
Ingo Molnare0a42722006-06-23 02:03:46 -07002402 */
Catalin Marinase7cb55b2009-10-28 13:33:08 +00002403 if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init &&
2404 !(flags & SLAB_NOLEAKTRACE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 /*
2406 * Size is large, assume best to place the slab management obj
2407 * off-slab (should allow better packing of objs).
2408 */
2409 flags |= CFLGS_OFF_SLAB;
2410
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002411 size = ALIGN(size, cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002413 left_over = calculate_slab_order(cachep, size, cachep->align, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002415 if (!cachep->num)
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002416 return -E2BIG;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002417
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002418 slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002419 + sizeof(struct slab), cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
2421 /*
2422 * If the slab has been placed off-slab, and we have enough space then
2423 * move it on-slab. This is at the expense of any extra colouring.
2424 */
2425 if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
2426 flags &= ~CFLGS_OFF_SLAB;
2427 left_over -= slab_size;
2428 }
2429
2430 if (flags & CFLGS_OFF_SLAB) {
2431 /* really off slab. No need for manual alignment */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002432 slab_size =
2433 cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
Ron Lee67461362009-05-22 04:58:22 +09302434
2435#ifdef CONFIG_PAGE_POISONING
2436 /* If we're going to use the generic kernel_map_pages()
2437 * poisoning, then it's going to smash the contents of
2438 * the redzone and userword anyhow, so switch them off.
2439 */
2440 if (size % PAGE_SIZE == 0 && flags & SLAB_POISON)
2441 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
2442#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 }
2444
2445 cachep->colour_off = cache_line_size();
2446 /* Offset must be a multiple of the alignment. */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002447 if (cachep->colour_off < cachep->align)
2448 cachep->colour_off = cachep->align;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002449 cachep->colour = left_over / cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 cachep->slab_size = slab_size;
2451 cachep->flags = flags;
Glauber Costaa618e892012-06-14 16:17:21 +04002452 cachep->allocflags = 0;
Christoph Lameter4b51d662007-02-10 01:43:10 -08002453 if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
Glauber Costaa618e892012-06-14 16:17:21 +04002454 cachep->allocflags |= GFP_DMA;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002455 cachep->size = size;
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08002456 cachep->reciprocal_buffer_size = reciprocal_value(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002458 if (flags & CFLGS_OFF_SLAB) {
Christoph Lameter2c59dd62013-01-10 19:14:19 +00002459 cachep->slabp_cache = kmalloc_slab(slab_size, 0u);
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002460 /*
2461 * This is a possibility for one of the malloc_sizes caches.
2462 * But since we go off slab only for object size greater than
2463 * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
2464 * this should not happen at all.
2465 * But leave a BUG_ON for some lucky dude.
2466 */
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002467 BUG_ON(ZERO_OR_NULL_PTR(cachep->slabp_cache));
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002468 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002470 err = setup_cpu_cache(cachep, gfp);
2471 if (err) {
Christoph Lameter12c36672012-09-04 23:38:33 +00002472 __kmem_cache_shutdown(cachep);
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002473 return err;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Peter Zijlstra83835b32011-07-22 15:26:05 +02002476 if (flags & SLAB_DEBUG_OBJECTS) {
2477 /*
2478 * Would deadlock through slab_destroy()->call_rcu()->
2479 * debug_object_activate()->kmem_cache_alloc().
2480 */
2481 WARN_ON_ONCE(flags & SLAB_DESTROY_BY_RCU);
2482
2483 slab_set_debugobj_lock_classes(cachep);
Glauber Costa6ccfb5b2012-12-18 14:22:31 -08002484 } else if (!OFF_SLAB(cachep) && !(flags & SLAB_DESTROY_BY_RCU))
2485 on_slab_lock_classes(cachep);
Peter Zijlstra83835b32011-07-22 15:26:05 +02002486
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002487 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489
2490#if DEBUG
2491static void check_irq_off(void)
2492{
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002493 BUG_ON_NONRT(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494}
2495
2496static void check_irq_on(void)
2497{
2498 BUG_ON(irqs_disabled());
2499}
2500
Pekka Enberg343e0d72006-02-01 03:05:50 -08002501static void check_spinlock_acquired(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502{
2503#ifdef CONFIG_SMP
2504 check_irq_off();
Christoph Lameter6a673682013-01-10 19:14:19 +00002505 assert_spin_locked(&cachep->node[numa_mem_id()]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506#endif
2507}
Christoph Lametere498be72005-09-09 13:03:32 -07002508
Pekka Enberg343e0d72006-02-01 03:05:50 -08002509static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07002510{
2511#ifdef CONFIG_SMP
2512 check_irq_off();
Christoph Lameter6a673682013-01-10 19:14:19 +00002513 assert_spin_locked(&cachep->node[node]->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002514#endif
2515}
2516
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517#else
2518#define check_irq_off() do { } while(0)
2519#define check_irq_on() do { } while(0)
2520#define check_spinlock_acquired(x) do { } while(0)
Christoph Lametere498be72005-09-09 13:03:32 -07002521#define check_spinlock_acquired_node(x, y) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522#endif
2523
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002524static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
Christoph Lameteraab22072006-03-22 00:09:06 -08002525 struct array_cache *ac,
2526 int force, int node);
2527
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002528static void __do_drain(void *arg, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
Andrew Mortona737b3e2006-03-22 00:08:11 -08002530 struct kmem_cache *cachep = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 struct array_cache *ac;
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002532 int node = cpu_to_mem(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002534 ac = cpu_cache_get_on_cpu(cachep, cpu);
Christoph Lameter6a673682013-01-10 19:14:19 +00002535 spin_lock(&cachep->node[node]->list_lock);
Christoph Lameterff694162005-09-22 21:44:02 -07002536 free_block(cachep, ac->entry, ac->avail, node);
Christoph Lameter6a673682013-01-10 19:14:19 +00002537 spin_unlock(&cachep->node[node]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 ac->avail = 0;
2539}
2540
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002541#ifndef CONFIG_PREEMPT_RT_BASE
2542static void do_drain(void *arg)
2543{
2544 __do_drain(arg, smp_processor_id());
2545}
2546#else
Peter Zijlstra696ac522009-07-03 08:44:43 -05002547static void do_drain(void *arg, int cpu)
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002548{
Peter Zijlstra696ac522009-07-03 08:44:43 -05002549 LIST_HEAD(tmp);
2550
2551 lock_slab_on(cpu);
2552 __do_drain(arg, cpu);
2553 list_splice_init(&per_cpu(slab_free_list, cpu), &tmp);
2554 unlock_slab_on(cpu);
2555 free_delayed(&tmp);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002556}
2557#endif
2558
Pekka Enberg343e0d72006-02-01 03:05:50 -08002559static void drain_cpu_caches(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002561 struct kmem_cache_node *n;
Christoph Lametere498be72005-09-09 13:03:32 -07002562 int node;
2563
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002564 slab_on_each_cpu(do_drain, cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 check_irq_on();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002566 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002567 n = cachep->node[node];
2568 if (n && n->alien)
2569 drain_alien_cache(cachep, n->alien);
Roland Dreiera4523a82006-05-15 11:41:00 -07002570 }
2571
2572 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002573 n = cachep->node[node];
2574 if (n)
2575 drain_array(cachep, n, n->shared, 1, node);
Christoph Lametere498be72005-09-09 13:03:32 -07002576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577}
2578
Christoph Lametered11d9e2006-06-30 01:55:45 -07002579/*
2580 * Remove slabs from the list of free slabs.
2581 * Specify the number of slabs to drain in tofree.
2582 *
2583 * Returns the actual number of slabs released.
2584 */
2585static int drain_freelist(struct kmem_cache *cache,
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002586 struct kmem_cache_node *n, int tofree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
Christoph Lametered11d9e2006-06-30 01:55:45 -07002588 struct list_head *p;
2589 int nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
Christoph Lametered11d9e2006-06-30 01:55:45 -07002592 nr_freed = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002593 while (nr_freed < tofree && !list_empty(&n->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002595 local_spin_lock_irq(slab_lock, &n->list_lock);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002596 p = n->slabs_free.prev;
2597 if (p == &n->slabs_free) {
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002598 local_spin_unlock_irq(slab_lock, &n->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002599 goto out;
2600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Christoph Lametered11d9e2006-06-30 01:55:45 -07002602 slabp = list_entry(p, struct slab, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603#if DEBUG
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002604 BUG_ON(slabp->inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605#endif
2606 list_del(&slabp->list);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002607 /*
2608 * Safe to drop the lock. The slab is no longer linked
2609 * to the cache.
2610 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002611 n->free_objects -= cache->num;
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002612 local_spin_unlock_irq(slab_lock, &n->list_lock);
Peter Zijlstra696ac522009-07-03 08:44:43 -05002613 slab_destroy(cache, slabp, false);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002614 nr_freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 }
Christoph Lametered11d9e2006-06-30 01:55:45 -07002616out:
2617 return nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618}
2619
Christoph Lameter18004c52012-07-06 15:25:12 -05002620/* Called with slab_mutex held to protect against cpu hotplug */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002621static int __cache_shrink(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07002622{
2623 int ret = 0, i = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002624 struct kmem_cache_node *n;
Christoph Lametere498be72005-09-09 13:03:32 -07002625
2626 drain_cpu_caches(cachep);
2627
2628 check_irq_on();
2629 for_each_online_node(i) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002630 n = cachep->node[i];
2631 if (!n)
Christoph Lametered11d9e2006-06-30 01:55:45 -07002632 continue;
2633
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002634 drain_freelist(cachep, n, n->free_objects);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002635
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002636 ret += !list_empty(&n->slabs_full) ||
2637 !list_empty(&n->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07002638 }
2639 return (ret ? 1 : 0);
2640}
2641
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642/**
2643 * kmem_cache_shrink - Shrink a cache.
2644 * @cachep: The cache to shrink.
2645 *
2646 * Releases as many slabs as possible for a cache.
2647 * To help debugging, a zero exit status indicates all slabs were released.
2648 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002649int kmem_cache_shrink(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002651 int ret;
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002652 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002654 get_online_cpus();
Christoph Lameter18004c52012-07-06 15:25:12 -05002655 mutex_lock(&slab_mutex);
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002656 ret = __cache_shrink(cachep);
Christoph Lameter18004c52012-07-06 15:25:12 -05002657 mutex_unlock(&slab_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002658 put_online_cpus();
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002659 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660}
2661EXPORT_SYMBOL(kmem_cache_shrink);
2662
Christoph Lameter945cf2b2012-09-04 23:18:33 +00002663int __kmem_cache_shutdown(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664{
Christoph Lameter12c36672012-09-04 23:38:33 +00002665 int i;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002666 struct kmem_cache_node *n;
Christoph Lameter12c36672012-09-04 23:38:33 +00002667 int rc = __cache_shrink(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Christoph Lameter12c36672012-09-04 23:38:33 +00002669 if (rc)
2670 return rc;
2671
2672 for_each_online_cpu(i)
2673 kfree(cachep->array[i]);
2674
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002675 /* NUMA: free the node structures */
Christoph Lameter12c36672012-09-04 23:38:33 +00002676 for_each_online_node(i) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002677 n = cachep->node[i];
2678 if (n) {
2679 kfree(n->shared);
2680 free_alien_cache(n->alien);
2681 kfree(n);
Christoph Lameter12c36672012-09-04 23:38:33 +00002682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 }
Christoph Lameter12c36672012-09-04 23:38:33 +00002684 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002687/*
2688 * Get the memory for a slab management obj.
2689 * For a slab cache when the slab descriptor is off-slab, slab descriptors
2690 * always come from malloc_sizes caches. The slab descriptor cannot
2691 * come from the same cache which is getting created because,
2692 * when we are searching for an appropriate cache for these
2693 * descriptors in kmem_cache_create, we search through the malloc_sizes array.
2694 * If we are creating a malloc_sizes cache here it would not be visible to
2695 * kmem_find_general_cachep till the initialization is complete.
2696 * Hence we cannot have slabp_cache same as the original cache.
2697 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002698static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002699 int colour_off, gfp_t local_flags,
2700 int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701{
2702 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002703
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 if (OFF_SLAB(cachep)) {
2705 /* Slab management obj is off-slab. */
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002706 slabp = kmem_cache_alloc_node(cachep->slabp_cache,
Pekka Enberg8759ec52008-11-26 10:01:31 +02002707 local_flags, nodeid);
Catalin Marinasd5cff632009-06-11 13:22:40 +01002708 /*
2709 * If the first object in the slab is leaked (it's allocated
2710 * but no one has a reference to it), we want to make sure
2711 * kmemleak does not treat the ->s_mem pointer as a reference
2712 * to the object. Otherwise we will not report the leak.
2713 */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002714 kmemleak_scan_area(&slabp->list, sizeof(struct list_head),
2715 local_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 if (!slabp)
2717 return NULL;
2718 } else {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002719 slabp = objp + colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 colour_off += cachep->slab_size;
2721 }
2722 slabp->inuse = 0;
2723 slabp->colouroff = colour_off;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002724 slabp->s_mem = objp + colour_off;
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002725 slabp->nodeid = nodeid;
Marcin Slusarze51bfd02008-02-10 11:21:54 +01002726 slabp->free = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 return slabp;
2728}
2729
2730static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
2731{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002732 return (kmem_bufctl_t *) (slabp + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733}
2734
Pekka Enberg343e0d72006-02-01 03:05:50 -08002735static void cache_init_objs(struct kmem_cache *cachep,
Christoph Lametera35afb82007-05-16 22:10:57 -07002736 struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737{
2738 int i;
2739
2740 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002741 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742#if DEBUG
2743 /* need to poison the objs? */
2744 if (cachep->flags & SLAB_POISON)
2745 poison_obj(cachep, objp, POISON_FREE);
2746 if (cachep->flags & SLAB_STORE_USER)
2747 *dbg_userword(cachep, objp) = NULL;
2748
2749 if (cachep->flags & SLAB_RED_ZONE) {
2750 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2751 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2752 }
2753 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002754 * Constructors are not allowed to allocate memory from the same
2755 * cache which they are a constructor for. Otherwise, deadlock.
2756 * They must also be threaded.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 */
2758 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002759 cachep->ctor(objp + obj_offset(cachep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
2761 if (cachep->flags & SLAB_RED_ZONE) {
2762 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2763 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002764 " end of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2766 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002767 " start of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 }
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002769 if ((cachep->size % PAGE_SIZE) == 0 &&
Andrew Mortona737b3e2006-03-22 00:08:11 -08002770 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002771 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002772 cachep->size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773#else
2774 if (cachep->ctor)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002775 cachep->ctor(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776#endif
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002777 slab_bufctl(slabp)[i] = i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002779 slab_bufctl(slabp)[i - 1] = BUFCTL_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780}
2781
Pekka Enberg343e0d72006-02-01 03:05:50 -08002782static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783{
Christoph Lameter4b51d662007-02-10 01:43:10 -08002784 if (CONFIG_ZONE_DMA_FLAG) {
2785 if (flags & GFP_DMA)
Glauber Costaa618e892012-06-14 16:17:21 +04002786 BUG_ON(!(cachep->allocflags & GFP_DMA));
Christoph Lameter4b51d662007-02-10 01:43:10 -08002787 else
Glauber Costaa618e892012-06-14 16:17:21 +04002788 BUG_ON(cachep->allocflags & GFP_DMA);
Christoph Lameter4b51d662007-02-10 01:43:10 -08002789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790}
2791
Andrew Mortona737b3e2006-03-22 00:08:11 -08002792static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
2793 int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002794{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002795 void *objp = index_to_obj(cachep, slabp, slabp->free);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002796 kmem_bufctl_t next;
2797
2798 slabp->inuse++;
2799 next = slab_bufctl(slabp)[slabp->free];
2800#if DEBUG
2801 slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
2802 WARN_ON(slabp->nodeid != nodeid);
2803#endif
2804 slabp->free = next;
2805
2806 return objp;
2807}
2808
Andrew Mortona737b3e2006-03-22 00:08:11 -08002809static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
2810 void *objp, int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002811{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002812 unsigned int objnr = obj_to_index(cachep, slabp, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002813
2814#if DEBUG
2815 /* Verify that the slab belongs to the intended node */
2816 WARN_ON(slabp->nodeid != nodeid);
2817
Al Viro871751e2006-03-25 03:06:39 -08002818 if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) {
Matthew Dobson78d382d2006-02-01 03:05:47 -08002819 printk(KERN_ERR "slab: double free detected in cache "
Andrew Mortona737b3e2006-03-22 00:08:11 -08002820 "'%s', objp %p\n", cachep->name, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002821 BUG();
2822 }
2823#endif
2824 slab_bufctl(slabp)[objnr] = slabp->free;
2825 slabp->free = objnr;
2826 slabp->inuse--;
2827}
2828
Pekka Enberg47768742006-06-23 02:03:07 -07002829/*
2830 * Map pages beginning at addr to the given cache and slab. This is required
2831 * for the slab allocator to be able to lookup the cache and slab of a
Nick Pigginccd35fb2011-01-07 17:49:17 +11002832 * virtual address for kfree, ksize, and slab debugging.
Pekka Enberg47768742006-06-23 02:03:07 -07002833 */
2834static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
2835 void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
Pekka Enberg47768742006-06-23 02:03:07 -07002837 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 struct page *page;
2839
Pekka Enberg47768742006-06-23 02:03:07 -07002840 page = virt_to_page(addr);
Nick Piggin84097512006-03-22 00:08:34 -08002841
Pekka Enberg47768742006-06-23 02:03:07 -07002842 nr_pages = 1;
Nick Piggin84097512006-03-22 00:08:34 -08002843 if (likely(!PageCompound(page)))
Pekka Enberg47768742006-06-23 02:03:07 -07002844 nr_pages <<= cache->gfporder;
2845
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 do {
Christoph Lameter35026082012-06-13 10:24:56 -05002847 page->slab_cache = cache;
2848 page->slab_page = slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 page++;
Pekka Enberg47768742006-06-23 02:03:07 -07002850 } while (--nr_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851}
2852
2853/*
2854 * Grow (by 1) the number of slabs within a cache. This is called by
2855 * kmem_cache_alloc() when there are no active objs left in a cache.
2856 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002857static int cache_grow(struct kmem_cache *cachep,
2858 gfp_t flags, int nodeid, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002860 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002861 size_t offset;
2862 gfp_t local_flags;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002863 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Andrew Mortona737b3e2006-03-22 00:08:11 -08002865 /*
2866 * Be lazy and only check for valid flags here, keeping it out of the
2867 * critical path in kmem_cache_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868 */
Christoph Lameter6cb06222007-10-16 01:25:41 -07002869 BUG_ON(flags & GFP_SLAB_BUG_MASK);
2870 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002872 /* Take the node list lock to change the colour_next on this node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002874 n = cachep->node[nodeid];
2875 spin_lock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
2877 /* Get colour for the slab, and cal the next value. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002878 offset = n->colour_next;
2879 n->colour_next++;
2880 if (n->colour_next >= cachep->colour)
2881 n->colour_next = 0;
2882 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002884 offset *= cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885
2886 if (local_flags & __GFP_WAIT)
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002887 local_unlock_irq(slab_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
2889 /*
2890 * The test for missing atomic flag is performed here, rather than
2891 * the more obvious place, simply to reduce the critical path length
2892 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2893 * will eventually be caught here (where it matters).
2894 */
2895 kmem_flagcheck(cachep, flags);
2896
Andrew Mortona737b3e2006-03-22 00:08:11 -08002897 /*
2898 * Get mem for the objs. Attempt to allocate a physical page from
2899 * 'nodeid'.
Christoph Lametere498be72005-09-09 13:03:32 -07002900 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002901 if (!objp)
Andrew Mortonb8c1c5d2007-07-24 12:02:40 -07002902 objp = kmem_getpages(cachep, local_flags, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002903 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 goto failed;
2905
2906 /* Get slab management. */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002907 slabp = alloc_slabmgmt(cachep, objp, offset,
Christoph Lameter6cb06222007-10-16 01:25:41 -07002908 local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002909 if (!slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 goto opps1;
2911
Pekka Enberg47768742006-06-23 02:03:07 -07002912 slab_map_pages(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Christoph Lametera35afb82007-05-16 22:10:57 -07002914 cache_init_objs(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915
2916 if (local_flags & __GFP_WAIT)
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002917 local_lock_irq(slab_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002919 spin_lock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920
2921 /* Make slab active. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002922 list_add_tail(&slabp->list, &(n->slabs_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 STATS_INC_GROWN(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002924 n->free_objects += cachep->num;
2925 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002927opps1:
Peter Zijlstra696ac522009-07-03 08:44:43 -05002928 kmem_freepages(cachep, objp, false);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002929failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 if (local_flags & __GFP_WAIT)
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002931 local_lock_irq(slab_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 return 0;
2933}
2934
2935#if DEBUG
2936
2937/*
2938 * Perform extra freeing checks:
2939 * - detect bad pointers.
2940 * - POISON/RED_ZONE checking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 */
2942static void kfree_debugcheck(const void *objp)
2943{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 if (!virt_addr_valid(objp)) {
2945 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002946 (unsigned long)objp);
2947 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949}
2950
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002951static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2952{
David Woodhouseb46b8f12007-05-08 00:22:59 -07002953 unsigned long long redzone1, redzone2;
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002954
2955 redzone1 = *dbg_redzone1(cache, obj);
2956 redzone2 = *dbg_redzone2(cache, obj);
2957
2958 /*
2959 * Redzone is ok.
2960 */
2961 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2962 return;
2963
2964 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2965 slab_error(cache, "double free detected");
2966 else
2967 slab_error(cache, "memory outside object was overwritten");
2968
David Woodhouseb46b8f12007-05-08 00:22:59 -07002969 printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002970 obj, redzone1, redzone2);
2971}
2972
Pekka Enberg343e0d72006-02-01 03:05:50 -08002973static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002974 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975{
2976 struct page *page;
2977 unsigned int objnr;
2978 struct slab *slabp;
2979
Matthew Wilcox80cbd912007-11-29 12:05:13 -07002980 BUG_ON(virt_to_cache(objp) != cachep);
2981
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002982 objp -= obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 kfree_debugcheck(objp);
Christoph Lameterb49af682007-05-06 14:49:41 -07002984 page = virt_to_head_page(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
Christoph Lameter35026082012-06-13 10:24:56 -05002986 slabp = page->slab_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
2988 if (cachep->flags & SLAB_RED_ZONE) {
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002989 verify_redzone_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2991 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2992 }
2993 if (cachep->flags & SLAB_STORE_USER)
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002994 *dbg_userword(cachep, objp) = (void *)caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002996 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
2998 BUG_ON(objnr >= cachep->num);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002999 BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000
Al Viro871751e2006-03-25 03:06:39 -08003001#ifdef CONFIG_DEBUG_SLAB_LEAK
3002 slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
3003#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 if (cachep->flags & SLAB_POISON) {
3005#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003006 if ((cachep->size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003007 store_stackinfo(cachep, objp, caller);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003008 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003009 cachep->size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 } else {
3011 poison_obj(cachep, objp, POISON_FREE);
3012 }
3013#else
3014 poison_obj(cachep, objp, POISON_FREE);
3015#endif
3016 }
3017 return objp;
3018}
3019
Pekka Enberg343e0d72006-02-01 03:05:50 -08003020static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021{
3022 kmem_bufctl_t i;
3023 int entries = 0;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 /* Check slab's freelist to see if this obj is there. */
3026 for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
3027 entries++;
3028 if (entries > cachep->num || i >= cachep->num)
3029 goto bad;
3030 }
3031 if (entries != cachep->num - slabp->inuse) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003032bad:
3033 printk(KERN_ERR "slab: Internal list corruption detected in "
Dave Jonesface37f2011-11-15 15:03:52 -08003034 "cache '%s'(%d), slabp %p(%d). Tainted(%s). Hexdump:\n",
3035 cachep->name, cachep->num, slabp, slabp->inuse,
3036 print_tainted());
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02003037 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, slabp,
3038 sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t),
3039 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 BUG();
3041 }
3042}
3043#else
3044#define kfree_debugcheck(x) do { } while(0)
3045#define cache_free_debugcheck(x,objp,z) (objp)
3046#define check_slabp(x,y) do { } while(0)
3047#endif
3048
Mel Gorman072bb0a2012-07-31 16:43:58 -07003049static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags,
3050 bool force_refill)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051{
3052 int batchcount;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003053 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 struct array_cache *ac;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07003055 int node;
3056
Joe Korty6d2144d2008-03-05 15:04:59 -08003057 check_irq_off();
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003058 node = numa_mem_id();
Mel Gorman072bb0a2012-07-31 16:43:58 -07003059 if (unlikely(force_refill))
3060 goto force_grow;
3061retry:
Joe Korty6d2144d2008-03-05 15:04:59 -08003062 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 batchcount = ac->batchcount;
3064 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003065 /*
3066 * If there was little recent activity on this cache, then
3067 * perform only a partial refill. Otherwise we could generate
3068 * refill bouncing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 */
3070 batchcount = BATCHREFILL_LIMIT;
3071 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003072 n = cachep->node[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003074 BUG_ON(ac->avail > 0 || !n);
3075 spin_lock(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07003076
Christoph Lameter3ded1752006-03-25 03:06:44 -08003077 /* See if we can refill from the shared array */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003078 if (n->shared && transfer_objects(ac, n->shared, batchcount)) {
3079 n->shared->touched = 1;
Christoph Lameter3ded1752006-03-25 03:06:44 -08003080 goto alloc_done;
Nick Piggin44b57f12010-01-27 22:27:40 +11003081 }
Christoph Lameter3ded1752006-03-25 03:06:44 -08003082
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 while (batchcount > 0) {
3084 struct list_head *entry;
3085 struct slab *slabp;
3086 /* Get slab alloc is to come from. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003087 entry = n->slabs_partial.next;
3088 if (entry == &n->slabs_partial) {
3089 n->free_touched = 1;
3090 entry = n->slabs_free.next;
3091 if (entry == &n->slabs_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 goto must_grow;
3093 }
3094
3095 slabp = list_entry(entry, struct slab, list);
3096 check_slabp(cachep, slabp);
3097 check_spinlock_acquired(cachep);
Pekka Enberg714b81712007-05-06 14:49:03 -07003098
3099 /*
3100 * The slab was either on partial or free list so
3101 * there must be at least one object available for
3102 * allocation.
3103 */
roel kluin249b9f32008-10-29 17:18:07 -04003104 BUG_ON(slabp->inuse >= cachep->num);
Pekka Enberg714b81712007-05-06 14:49:03 -07003105
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 while (slabp->inuse < cachep->num && batchcount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 STATS_INC_ALLOCED(cachep);
3108 STATS_INC_ACTIVE(cachep);
3109 STATS_SET_HIGH(cachep);
3110
Mel Gorman072bb0a2012-07-31 16:43:58 -07003111 ac_put_obj(cachep, ac, slab_get_obj(cachep, slabp,
3112 node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 }
3114 check_slabp(cachep, slabp);
3115
3116 /* move slabp to correct slabp list: */
3117 list_del(&slabp->list);
3118 if (slabp->free == BUFCTL_END)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003119 list_add(&slabp->list, &n->slabs_full);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 else
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003121 list_add(&slabp->list, &n->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 }
3123
Andrew Mortona737b3e2006-03-22 00:08:11 -08003124must_grow:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003125 n->free_objects -= ac->avail;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003126alloc_done:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003127 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
3129 if (unlikely(!ac->avail)) {
3130 int x;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003131force_grow:
Christoph Lameter3c517a62006-12-06 20:33:29 -08003132 x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07003133
Andrew Mortona737b3e2006-03-22 00:08:11 -08003134 /* cache_grow can reenable interrupts, then ac could change. */
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003135 ac = cpu_cache_get(cachep);
David Rientjes51cd8e62012-08-28 19:57:21 -07003136 node = numa_mem_id();
Mel Gorman072bb0a2012-07-31 16:43:58 -07003137
3138 /* no objects in sight? abort */
3139 if (!x && (ac->avail == 0 || force_refill))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 return NULL;
3141
Andrew Mortona737b3e2006-03-22 00:08:11 -08003142 if (!ac->avail) /* objects refilled by interrupt? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 goto retry;
3144 }
3145 ac->touched = 1;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003146
3147 return ac_get_obj(cachep, ac, flags, force_refill);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148}
3149
Andrew Mortona737b3e2006-03-22 00:08:11 -08003150static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
3151 gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152{
3153 might_sleep_if(flags & __GFP_WAIT);
3154#if DEBUG
3155 kmem_flagcheck(cachep, flags);
3156#endif
3157}
3158
3159#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003160static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003161 gfp_t flags, void *objp, unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003163 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 return objp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003165 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003167 if ((cachep->size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003168 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003169 cachep->size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 else
3171 check_poison_obj(cachep, objp);
3172#else
3173 check_poison_obj(cachep, objp);
3174#endif
3175 poison_obj(cachep, objp, POISON_INUSE);
3176 }
3177 if (cachep->flags & SLAB_STORE_USER)
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003178 *dbg_userword(cachep, objp) = (void *)caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
3180 if (cachep->flags & SLAB_RED_ZONE) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003181 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
3182 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
3183 slab_error(cachep, "double free, or memory outside"
3184 " object was overwritten");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003185 printk(KERN_ERR
David Woodhouseb46b8f12007-05-08 00:22:59 -07003186 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08003187 objp, *dbg_redzone1(cachep, objp),
3188 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 }
3190 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
3191 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
3192 }
Al Viro871751e2006-03-25 03:06:39 -08003193#ifdef CONFIG_DEBUG_SLAB_LEAK
3194 {
3195 struct slab *slabp;
3196 unsigned objnr;
3197
Christoph Lameter35026082012-06-13 10:24:56 -05003198 slabp = virt_to_head_page(objp)->slab_page;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003199 objnr = (unsigned)(objp - slabp->s_mem) / cachep->size;
Al Viro871751e2006-03-25 03:06:39 -08003200 slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE;
3201 }
3202#endif
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003203 objp += obj_offset(cachep);
Christoph Lameter4f104932007-05-06 14:50:17 -07003204 if (cachep->ctor && cachep->flags & SLAB_POISON)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003205 cachep->ctor(objp);
Tetsuo Handa7ea466f2011-07-21 09:42:45 +09003206 if (ARCH_SLAB_MINALIGN &&
3207 ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003208 printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
Hugh Dickinsc2251502011-07-11 13:35:08 -07003209 objp, (int)ARCH_SLAB_MINALIGN);
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003210 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 return objp;
3212}
3213#else
3214#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
3215#endif
3216
Akinobu Mita773ff602008-12-23 19:37:01 +09003217static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags)
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003218{
Christoph Lameter9b030cb2012-09-05 00:20:33 +00003219 if (cachep == kmem_cache)
Akinobu Mita773ff602008-12-23 19:37:01 +09003220 return false;
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003221
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003222 return should_failslab(cachep->object_size, flags, cachep->flags);
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003223}
3224
Pekka Enberg343e0d72006-02-01 03:05:50 -08003225static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003227 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 struct array_cache *ac;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003229 bool force_refill = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230
Alok N Kataria5c382302005-09-27 21:45:46 -07003231 check_irq_off();
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003232
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003233 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 if (likely(ac->avail)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 ac->touched = 1;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003236 objp = ac_get_obj(cachep, ac, flags, false);
3237
J. R. Okajimaddbf2e82009-12-02 16:55:50 +09003238 /*
Mel Gorman072bb0a2012-07-31 16:43:58 -07003239 * Allow for the possibility all avail objects are not allowed
3240 * by the current flags
J. R. Okajimaddbf2e82009-12-02 16:55:50 +09003241 */
Mel Gorman072bb0a2012-07-31 16:43:58 -07003242 if (objp) {
3243 STATS_INC_ALLOCHIT(cachep);
3244 goto out;
3245 }
3246 force_refill = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 }
Mel Gorman072bb0a2012-07-31 16:43:58 -07003248
3249 STATS_INC_ALLOCMISS(cachep);
3250 objp = cache_alloc_refill(cachep, flags, force_refill);
3251 /*
3252 * the 'ac' may be updated by cache_alloc_refill(),
3253 * and kmemleak_erase() requires its correct value.
3254 */
3255 ac = cpu_cache_get(cachep);
3256
3257out:
Catalin Marinasd5cff632009-06-11 13:22:40 +01003258 /*
3259 * To avoid a false negative, if an object that is in one of the
3260 * per-CPU caches is leaked, we need to make sure kmemleak doesn't
3261 * treat the array pointers as a reference to the object.
3262 */
J. R. Okajimaf3d8b532009-12-02 16:55:49 +09003263 if (objp)
3264 kmemleak_erase(&ac->entry[ac->avail]);
Alok N Kataria5c382302005-09-27 21:45:46 -07003265 return objp;
3266}
3267
Christoph Lametere498be72005-09-09 13:03:32 -07003268#ifdef CONFIG_NUMA
3269/*
Paul Jacksonb2455392006-03-24 03:16:12 -08003270 * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
Paul Jacksonc61afb12006-03-24 03:16:08 -08003271 *
3272 * If we are in_interrupt, then process context, including cpusets and
3273 * mempolicy, may not apply and should not be used for allocation policy.
3274 */
3275static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
3276{
3277 int nid_alloc, nid_here;
3278
Christoph Lameter765c4502006-09-27 01:50:08 -07003279 if (in_interrupt() || (flags & __GFP_THISNODE))
Paul Jacksonc61afb12006-03-24 03:16:08 -08003280 return NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003281 nid_alloc = nid_here = numa_mem_id();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003282 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
Jack Steiner6adef3e2010-05-26 14:42:49 -07003283 nid_alloc = cpuset_slab_spread_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003284 else if (current->mempolicy)
Andi Kleene7b691b2012-06-09 02:40:03 -07003285 nid_alloc = slab_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003286 if (nid_alloc != nid_here)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003287 return ____cache_alloc_node(cachep, flags, nid_alloc);
Paul Jacksonc61afb12006-03-24 03:16:08 -08003288 return NULL;
3289}
3290
3291/*
Christoph Lameter765c4502006-09-27 01:50:08 -07003292 * Fallback function if there was no memory available and no objects on a
Christoph Lameter3c517a62006-12-06 20:33:29 -08003293 * certain node and fall back is permitted. First we scan all the
Christoph Lameter6a673682013-01-10 19:14:19 +00003294 * available node for available objects. If that fails then we
Christoph Lameter3c517a62006-12-06 20:33:29 -08003295 * perform an allocation without specifying a node. This allows the page
3296 * allocator to do its reclaim / fallback magic. We then insert the
3297 * slab into the proper nodelist and then allocate from it.
Christoph Lameter765c4502006-09-27 01:50:08 -07003298 */
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003299static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
Christoph Lameter765c4502006-09-27 01:50:08 -07003300{
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003301 struct zonelist *zonelist;
3302 gfp_t local_flags;
Mel Gormandd1a2392008-04-28 02:12:17 -07003303 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07003304 struct zone *zone;
3305 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003306 void *obj = NULL;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003307 int nid;
Mel Gormancc9a6c82012-03-21 16:34:11 -07003308 unsigned int cpuset_mems_cookie;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003309
3310 if (flags & __GFP_THISNODE)
3311 return NULL;
3312
Christoph Lameter6cb06222007-10-16 01:25:41 -07003313 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Christoph Lameter765c4502006-09-27 01:50:08 -07003314
Mel Gormancc9a6c82012-03-21 16:34:11 -07003315retry_cpuset:
3316 cpuset_mems_cookie = get_mems_allowed();
Andi Kleene7b691b2012-06-09 02:40:03 -07003317 zonelist = node_zonelist(slab_node(), flags);
Mel Gormancc9a6c82012-03-21 16:34:11 -07003318
Christoph Lameter3c517a62006-12-06 20:33:29 -08003319retry:
3320 /*
3321 * Look through allowed nodes for objects available
3322 * from existing per node queues.
3323 */
Mel Gorman54a6eb52008-04-28 02:12:16 -07003324 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
3325 nid = zone_to_nid(zone);
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003326
Mel Gorman54a6eb52008-04-28 02:12:16 -07003327 if (cpuset_zone_allowed_hardwall(zone, flags) &&
Christoph Lameter6a673682013-01-10 19:14:19 +00003328 cache->node[nid] &&
3329 cache->node[nid]->free_objects) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003330 obj = ____cache_alloc_node(cache,
3331 flags | GFP_THISNODE, nid);
Christoph Lameter481c5342008-06-21 16:46:35 -07003332 if (obj)
3333 break;
3334 }
Christoph Lameter3c517a62006-12-06 20:33:29 -08003335 }
3336
Christoph Lametercfce6602007-05-06 14:50:17 -07003337 if (!obj) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003338 /*
3339 * This allocation will be performed within the constraints
3340 * of the current cpuset / memory policy requirements.
3341 * We may trigger various forms of reclaim on the allowed
3342 * set and go into memory reserves if necessary.
3343 */
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003344 if (local_flags & __GFP_WAIT)
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003345 local_unlock_irq(slab_lock);
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003346 kmem_flagcheck(cache, flags);
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003347 obj = kmem_getpages(cache, local_flags, numa_mem_id());
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003348 if (local_flags & __GFP_WAIT)
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003349 local_lock_irq(slab_lock);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003350 if (obj) {
3351 /*
3352 * Insert into the appropriate per node queues
3353 */
3354 nid = page_to_nid(virt_to_page(obj));
3355 if (cache_grow(cache, flags, nid, obj)) {
3356 obj = ____cache_alloc_node(cache,
3357 flags | GFP_THISNODE, nid);
3358 if (!obj)
3359 /*
3360 * Another processor may allocate the
3361 * objects in the slab since we are
3362 * not holding any locks.
3363 */
3364 goto retry;
3365 } else {
Hugh Dickinsb6a60452007-01-05 16:36:36 -08003366 /* cache_grow already freed obj */
Christoph Lameter3c517a62006-12-06 20:33:29 -08003367 obj = NULL;
3368 }
3369 }
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003370 }
Mel Gormancc9a6c82012-03-21 16:34:11 -07003371
3372 if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !obj))
3373 goto retry_cpuset;
Christoph Lameter765c4502006-09-27 01:50:08 -07003374 return obj;
3375}
3376
3377/*
Christoph Lametere498be72005-09-09 13:03:32 -07003378 * A interface to enable slab creation on nodeid
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003380static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003381 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07003382{
3383 struct list_head *entry;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003384 struct slab *slabp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003385 struct kmem_cache_node *n;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003386 void *obj;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003387 int x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
Aaron Tomlin14e50c62013-04-26 16:15:34 +01003389 VM_BUG_ON(nodeid > num_online_nodes());
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003390 n = cachep->node[nodeid];
3391 BUG_ON(!n);
Christoph Lametere498be72005-09-09 13:03:32 -07003392
Andrew Mortona737b3e2006-03-22 00:08:11 -08003393retry:
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003394 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003395 spin_lock(&n->list_lock);
3396 entry = n->slabs_partial.next;
3397 if (entry == &n->slabs_partial) {
3398 n->free_touched = 1;
3399 entry = n->slabs_free.next;
3400 if (entry == &n->slabs_free)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003401 goto must_grow;
3402 }
Christoph Lametere498be72005-09-09 13:03:32 -07003403
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003404 slabp = list_entry(entry, struct slab, list);
3405 check_spinlock_acquired_node(cachep, nodeid);
3406 check_slabp(cachep, slabp);
Christoph Lametere498be72005-09-09 13:03:32 -07003407
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003408 STATS_INC_NODEALLOCS(cachep);
3409 STATS_INC_ACTIVE(cachep);
3410 STATS_SET_HIGH(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003411
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003412 BUG_ON(slabp->inuse == cachep->num);
Christoph Lametere498be72005-09-09 13:03:32 -07003413
Matthew Dobson78d382d2006-02-01 03:05:47 -08003414 obj = slab_get_obj(cachep, slabp, nodeid);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003415 check_slabp(cachep, slabp);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003416 n->free_objects--;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003417 /* move slabp to correct slabp list: */
3418 list_del(&slabp->list);
Christoph Lametere498be72005-09-09 13:03:32 -07003419
Andrew Mortona737b3e2006-03-22 00:08:11 -08003420 if (slabp->free == BUFCTL_END)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003421 list_add(&slabp->list, &n->slabs_full);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003422 else
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003423 list_add(&slabp->list, &n->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07003424
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003425 spin_unlock(&n->list_lock);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003426 goto done;
Christoph Lametere498be72005-09-09 13:03:32 -07003427
Andrew Mortona737b3e2006-03-22 00:08:11 -08003428must_grow:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003429 spin_unlock(&n->list_lock);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003430 x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
Christoph Lameter765c4502006-09-27 01:50:08 -07003431 if (x)
3432 goto retry;
Christoph Lametere498be72005-09-09 13:03:32 -07003433
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003434 return fallback_alloc(cachep, flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003435
Andrew Mortona737b3e2006-03-22 00:08:11 -08003436done:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003437 return obj;
Christoph Lametere498be72005-09-09 13:03:32 -07003438}
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003439
3440/**
3441 * kmem_cache_alloc_node - Allocate an object on the specified node
3442 * @cachep: The cache to allocate from.
3443 * @flags: See kmalloc().
3444 * @nodeid: node number of the target node.
3445 * @caller: return address of caller, used for debug information
3446 *
3447 * Identical to kmem_cache_alloc but it will allocate memory on the given
3448 * node, which can improve the performance for cpu bound structures.
3449 *
3450 * Fallback to other node is possible if __GFP_THISNODE is not set.
3451 */
3452static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003453slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003454 unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003455{
3456 unsigned long save_flags;
3457 void *ptr;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003458 int slab_node = numa_mem_id();
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003459
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003460 flags &= gfp_allowed_mask;
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003461
Nick Piggincf40bd12009-01-21 08:12:39 +01003462 lockdep_trace_alloc(flags);
3463
Akinobu Mita773ff602008-12-23 19:37:01 +09003464 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003465 return NULL;
3466
Glauber Costad79923f2012-12-18 14:22:48 -08003467 cachep = memcg_kmem_get_cache(cachep, flags);
3468
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003469 cache_alloc_debugcheck_before(cachep, flags);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003470 local_lock_irqsave(slab_lock, save_flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003471
Andrew Mortoneacbbae2011-07-28 13:59:49 -07003472 if (nodeid == NUMA_NO_NODE)
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003473 nodeid = slab_node;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003474
Christoph Lameter6a673682013-01-10 19:14:19 +00003475 if (unlikely(!cachep->node[nodeid])) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003476 /* Node not bootstrapped yet */
3477 ptr = fallback_alloc(cachep, flags);
3478 goto out;
3479 }
3480
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003481 if (nodeid == slab_node) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003482 /*
3483 * Use the locally cached objects if possible.
3484 * However ____cache_alloc does not allow fallback
3485 * to other nodes. It may fail while we still have
3486 * objects on other nodes available.
3487 */
3488 ptr = ____cache_alloc(cachep, flags);
3489 if (ptr)
3490 goto out;
3491 }
3492 /* ___cache_alloc_node can fall back to other nodes */
3493 ptr = ____cache_alloc_node(cachep, flags, nodeid);
3494 out:
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003495 local_unlock_irqrestore(slab_lock, save_flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003496 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003497 kmemleak_alloc_recursive(ptr, cachep->object_size, 1, cachep->flags,
Catalin Marinasd5cff632009-06-11 13:22:40 +01003498 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003499
Pekka Enbergc175eea2008-05-09 20:35:53 +02003500 if (likely(ptr))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003501 kmemcheck_slab_alloc(cachep, flags, ptr, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003502
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003503 if (unlikely((flags & __GFP_ZERO) && ptr))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003504 memset(ptr, 0, cachep->object_size);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003505
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003506 return ptr;
3507}
3508
3509static __always_inline void *
3510__do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
3511{
3512 void *objp;
3513
3514 if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
3515 objp = alternate_node_alloc(cache, flags);
3516 if (objp)
3517 goto out;
3518 }
3519 objp = ____cache_alloc(cache, flags);
3520
3521 /*
3522 * We may just have run out of memory on the local node.
3523 * ____cache_alloc_node() knows how to locate memory on other nodes
3524 */
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003525 if (!objp)
3526 objp = ____cache_alloc_node(cache, flags, numa_mem_id());
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003527
3528 out:
3529 return objp;
3530}
3531#else
3532
3533static __always_inline void *
3534__do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
3535{
3536 return ____cache_alloc(cachep, flags);
3537}
3538
3539#endif /* CONFIG_NUMA */
3540
3541static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003542slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003543{
3544 unsigned long save_flags;
3545 void *objp;
3546
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003547 flags &= gfp_allowed_mask;
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003548
Nick Piggincf40bd12009-01-21 08:12:39 +01003549 lockdep_trace_alloc(flags);
3550
Akinobu Mita773ff602008-12-23 19:37:01 +09003551 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003552 return NULL;
3553
Glauber Costad79923f2012-12-18 14:22:48 -08003554 cachep = memcg_kmem_get_cache(cachep, flags);
3555
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003556 cache_alloc_debugcheck_before(cachep, flags);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003557 local_lock_irqsave(slab_lock, save_flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003558 objp = __do_cache_alloc(cachep, flags);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003559 local_unlock_irqrestore(slab_lock, save_flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003560 objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003561 kmemleak_alloc_recursive(objp, cachep->object_size, 1, cachep->flags,
Catalin Marinasd5cff632009-06-11 13:22:40 +01003562 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003563 prefetchw(objp);
3564
Pekka Enbergc175eea2008-05-09 20:35:53 +02003565 if (likely(objp))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003566 kmemcheck_slab_alloc(cachep, flags, objp, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003567
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003568 if (unlikely((flags & __GFP_ZERO) && objp))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003569 memset(objp, 0, cachep->object_size);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003570
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003571 return objp;
3572}
Christoph Lametere498be72005-09-09 13:03:32 -07003573
3574/*
3575 * Caller needs to acquire correct kmem_list's list_lock
3576 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003577static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003578 int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579{
3580 int i;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003581 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
3583 for (i = 0; i < nr_objects; i++) {
Mel Gorman072bb0a2012-07-31 16:43:58 -07003584 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586
Mel Gorman072bb0a2012-07-31 16:43:58 -07003587 clear_obj_pfmemalloc(&objpp[i]);
3588 objp = objpp[i];
3589
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003590 slabp = virt_to_slab(objp);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003591 n = cachep->node[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 list_del(&slabp->list);
Christoph Lameterff694162005-09-22 21:44:02 -07003593 check_spinlock_acquired_node(cachep, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 check_slabp(cachep, slabp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08003595 slab_put_obj(cachep, slabp, objp, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 STATS_DEC_ACTIVE(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003597 n->free_objects++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 check_slabp(cachep, slabp);
3599
3600 /* fixup slab chains */
3601 if (slabp->inuse == 0) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003602 if (n->free_objects > n->free_limit) {
3603 n->free_objects -= cachep->num;
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07003604 /* No need to drop any previously held
3605 * lock here, even if we have a off-slab slab
3606 * descriptor it is guaranteed to come from
3607 * a different cache, refer to comments before
3608 * alloc_slabmgmt.
3609 */
Peter Zijlstra696ac522009-07-03 08:44:43 -05003610 slab_destroy(cachep, slabp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 } else {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003612 list_add(&slabp->list, &n->slabs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 }
3614 } else {
3615 /* Unconditionally move a slab to the end of the
3616 * partial list on free - maximum time for the
3617 * other objects to be freed, too.
3618 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003619 list_add_tail(&slabp->list, &n->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 }
3621 }
3622}
3623
Pekka Enberg343e0d72006-02-01 03:05:50 -08003624static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625{
3626 int batchcount;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003627 struct kmem_cache_node *n;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003628 int node = numa_mem_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629
3630 batchcount = ac->batchcount;
3631#if DEBUG
3632 BUG_ON(!batchcount || batchcount > ac->avail);
3633#endif
3634 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003635 n = cachep->node[node];
3636 spin_lock(&n->list_lock);
3637 if (n->shared) {
3638 struct array_cache *shared_array = n->shared;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003639 int max = shared_array->limit - shared_array->avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640 if (max) {
3641 if (batchcount > max)
3642 batchcount = max;
Christoph Lametere498be72005-09-09 13:03:32 -07003643 memcpy(&(shared_array->entry[shared_array->avail]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003644 ac->entry, sizeof(void *) * batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 shared_array->avail += batchcount;
3646 goto free_done;
3647 }
3648 }
3649
Christoph Lameterff694162005-09-22 21:44:02 -07003650 free_block(cachep, ac->entry, batchcount, node);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003651free_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652#if STATS
3653 {
3654 int i = 0;
3655 struct list_head *p;
3656
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003657 p = n->slabs_free.next;
3658 while (p != &(n->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 struct slab *slabp;
3660
3661 slabp = list_entry(p, struct slab, list);
3662 BUG_ON(slabp->inuse);
3663
3664 i++;
3665 p = p->next;
3666 }
3667 STATS_SET_FREEABLE(cachep, i);
3668 }
3669#endif
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003670 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 ac->avail -= batchcount;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003672 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673}
3674
3675/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08003676 * Release an obj back to its cache. If the obj has a constructed state, it must
3677 * be in this state _before_ it is released. Called with disabled ints.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 */
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003679static inline void __cache_free(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003680 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681{
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003682 struct array_cache *ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683
3684 check_irq_off();
Catalin Marinasd5cff632009-06-11 13:22:40 +01003685 kmemleak_free_recursive(objp, cachep->flags);
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003686 objp = cache_free_debugcheck(cachep, objp, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003688 kmemcheck_slab_free(cachep, objp, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003689
Siddha, Suresh B1807a1a2007-08-22 14:01:49 -07003690 /*
3691 * Skip calling cache_free_alien() when the platform is not numa.
3692 * This will avoid cache misses that happen while accessing slabp (which
3693 * is per page memory reference) to get nodeid. Instead use a global
3694 * variable to skip the call, which is mostly likely to be present in
3695 * the cache.
3696 */
Mel Gormanb6e68bc2009-06-16 15:32:16 -07003697 if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07003698 return;
Christoph Lametere498be72005-09-09 13:03:32 -07003699
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 if (likely(ac->avail < ac->limit)) {
3701 STATS_INC_FREEHIT(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 } else {
3703 STATS_INC_FREEMISS(cachep);
3704 cache_flusharray(cachep, ac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 }
Zhao Jin42c8c992011-08-27 00:26:17 +08003706
Mel Gorman072bb0a2012-07-31 16:43:58 -07003707 ac_put_obj(cachep, ac, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708}
3709
3710/**
3711 * kmem_cache_alloc - Allocate an object
3712 * @cachep: The cache to allocate from.
3713 * @flags: See kmalloc().
3714 *
3715 * Allocate an object from this cache. The flags are only relevant
3716 * if the cache has no available objects.
3717 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003718void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003720 void *ret = slab_alloc(cachep, flags, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003721
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003722 trace_kmem_cache_alloc(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003723 cachep->object_size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003724
3725 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726}
3727EXPORT_SYMBOL(kmem_cache_alloc);
3728
Li Zefan0f24f122009-12-11 15:45:30 +08003729#ifdef CONFIG_TRACING
Steven Rostedt85beb582010-11-24 16:23:34 -05003730void *
Ezequiel Garcia40521472012-09-08 17:47:56 -03003731kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003732{
Steven Rostedt85beb582010-11-24 16:23:34 -05003733 void *ret;
3734
Ezequiel Garcia48356302012-09-08 17:47:57 -03003735 ret = slab_alloc(cachep, flags, _RET_IP_);
Steven Rostedt85beb582010-11-24 16:23:34 -05003736
3737 trace_kmalloc(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003738 size, cachep->size, flags);
Steven Rostedt85beb582010-11-24 16:23:34 -05003739 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003740}
Steven Rostedt85beb582010-11-24 16:23:34 -05003741EXPORT_SYMBOL(kmem_cache_alloc_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003742#endif
3743
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744#ifdef CONFIG_NUMA
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003745void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
3746{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003747 void *ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003748
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003749 trace_kmem_cache_alloc_node(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003750 cachep->object_size, cachep->size,
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003751 flags, nodeid);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003752
3753 return ret;
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003754}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755EXPORT_SYMBOL(kmem_cache_alloc_node);
3756
Li Zefan0f24f122009-12-11 15:45:30 +08003757#ifdef CONFIG_TRACING
Ezequiel Garcia40521472012-09-08 17:47:56 -03003758void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
Steven Rostedt85beb582010-11-24 16:23:34 -05003759 gfp_t flags,
Ezequiel Garcia40521472012-09-08 17:47:56 -03003760 int nodeid,
3761 size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003762{
Steven Rostedt85beb582010-11-24 16:23:34 -05003763 void *ret;
3764
Ezequiel Garcia592f4142012-09-25 08:07:08 -03003765 ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003766
Steven Rostedt85beb582010-11-24 16:23:34 -05003767 trace_kmalloc_node(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003768 size, cachep->size,
Steven Rostedt85beb582010-11-24 16:23:34 -05003769 flags, nodeid);
3770 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003771}
Steven Rostedt85beb582010-11-24 16:23:34 -05003772EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003773#endif
3774
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003775static __always_inline void *
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003776__do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller)
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003777{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003778 struct kmem_cache *cachep;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003779
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003780 cachep = kmalloc_slab(size, flags);
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003781 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3782 return cachep;
Ezequiel Garcia40521472012-09-08 17:47:56 -03003783 return kmem_cache_alloc_node_trace(cachep, flags, node, size);
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003784}
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003785
Li Zefan0bb38a52009-12-11 15:45:50 +08003786#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003787void *__kmalloc_node(size_t size, gfp_t flags, int node)
3788{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003789 return __do_kmalloc_node(size, flags, node, _RET_IP_);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003790}
Christoph Hellwigdbe5e692006-09-25 23:31:36 -07003791EXPORT_SYMBOL(__kmalloc_node);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003792
3793void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003794 int node, unsigned long caller)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003795{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003796 return __do_kmalloc_node(size, flags, node, caller);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003797}
3798EXPORT_SYMBOL(__kmalloc_node_track_caller);
3799#else
3800void *__kmalloc_node(size_t size, gfp_t flags, int node)
3801{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003802 return __do_kmalloc_node(size, flags, node, 0);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003803}
3804EXPORT_SYMBOL(__kmalloc_node);
Li Zefan0bb38a52009-12-11 15:45:50 +08003805#endif /* CONFIG_DEBUG_SLAB || CONFIG_TRACING */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003806#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807
3808/**
Paul Drynoff800590f2006-06-23 02:03:48 -07003809 * __do_kmalloc - allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 * @size: how many bytes of memory are required.
Paul Drynoff800590f2006-06-23 02:03:48 -07003811 * @flags: the type of memory to allocate (see kmalloc).
Randy Dunlap911851e2006-03-22 00:08:14 -08003812 * @caller: function caller for debug tracking of the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 */
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003814static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003815 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003817 struct kmem_cache *cachep;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003818 void *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003820 /* If you want to save a few bytes .text space: replace
3821 * __ with kmem_.
3822 * Then kmalloc uses the uninlined functions instead of the inline
3823 * functions.
3824 */
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003825 cachep = kmalloc_slab(size, flags);
Linus Torvaldsa5c96d82007-07-19 13:17:15 -07003826 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3827 return cachep;
Ezequiel Garcia48356302012-09-08 17:47:57 -03003828 ret = slab_alloc(cachep, flags, caller);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003829
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003830 trace_kmalloc(caller, ret,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003831 size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003832
3833 return ret;
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003834}
3835
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003836
Li Zefan0bb38a52009-12-11 15:45:50 +08003837#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003838void *__kmalloc(size_t size, gfp_t flags)
3839{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003840 return __do_kmalloc(size, flags, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841}
3842EXPORT_SYMBOL(__kmalloc);
3843
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003844void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003845{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003846 return __do_kmalloc(size, flags, caller);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003847}
3848EXPORT_SYMBOL(__kmalloc_track_caller);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003849
3850#else
3851void *__kmalloc(size_t size, gfp_t flags)
3852{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003853 return __do_kmalloc(size, flags, 0);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003854}
3855EXPORT_SYMBOL(__kmalloc);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003856#endif
3857
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858/**
3859 * kmem_cache_free - Deallocate an object
3860 * @cachep: The cache the allocation was from.
3861 * @objp: The previously allocated object.
3862 *
3863 * Free an object which was previously allocated from this
3864 * cache.
3865 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003866void kmem_cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867{
3868 unsigned long flags;
Glauber Costab9ce5ef2012-12-18 14:22:46 -08003869 cachep = cache_from_obj(cachep, objp);
3870 if (!cachep)
3871 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872
Feng Tangd97d4762012-07-02 14:29:10 +08003873 debug_check_no_locks_freed(objp, cachep->object_size);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07003874 if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003875 debug_check_no_obj_freed(objp, cachep->object_size);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003876 local_lock_irqsave(slab_lock, flags);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003877 __cache_free(cachep, objp, _RET_IP_);
Peter Zijlstra696ac522009-07-03 08:44:43 -05003878 unlock_slab_and_free_delayed(flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003879
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003880 trace_kmem_cache_free(_RET_IP_, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881}
3882EXPORT_SYMBOL(kmem_cache_free);
3883
3884/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 * kfree - free previously allocated memory
3886 * @objp: pointer returned by kmalloc.
3887 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07003888 * If @objp is NULL, no operation is performed.
3889 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 * Don't free memory not originally allocated by kmalloc()
3891 * or you will run into trouble.
3892 */
3893void kfree(const void *objp)
3894{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003895 struct kmem_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 unsigned long flags;
3897
Pekka Enberg2121db72009-03-25 11:05:57 +02003898 trace_kfree(_RET_IP_, objp);
3899
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003900 if (unlikely(ZERO_OR_NULL_PTR(objp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 kfree_debugcheck(objp);
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003903 c = virt_to_cache(objp);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003904 debug_check_no_locks_freed(objp, c->object_size);
3905
3906 debug_check_no_obj_freed(objp, c->object_size);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003907 local_lock_irqsave(slab_lock, flags);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003908 __cache_free(c, (void *)objp, _RET_IP_);
Peter Zijlstra696ac522009-07-03 08:44:43 -05003909 unlock_slab_and_free_delayed(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910}
3911EXPORT_SYMBOL(kfree);
3912
Christoph Lametere498be72005-09-09 13:03:32 -07003913/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003914 * This initializes kmem_cache_node or resizes various caches for all nodes.
Christoph Lametere498be72005-09-09 13:03:32 -07003915 */
Pekka Enberg83b519e2009-06-10 19:40:04 +03003916static int alloc_kmemlist(struct kmem_cache *cachep, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -07003917{
3918 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003919 struct kmem_cache_node *n;
Christoph Lametercafeb022006-03-25 03:06:46 -08003920 struct array_cache *new_shared;
Paul Menage3395ee02006-12-06 20:32:16 -08003921 struct array_cache **new_alien = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003922
Mel Gorman9c09a952008-01-24 05:49:54 -08003923 for_each_online_node(node) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003924
Paul Menage3395ee02006-12-06 20:32:16 -08003925 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03003926 new_alien = alloc_alien_cache(node, cachep->limit, gfp);
Paul Menage3395ee02006-12-06 20:32:16 -08003927 if (!new_alien)
3928 goto fail;
3929 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003930
Eric Dumazet63109842007-05-06 14:49:28 -07003931 new_shared = NULL;
3932 if (cachep->shared) {
3933 new_shared = alloc_arraycache(node,
Christoph Lameter0718dc22006-03-25 03:06:47 -08003934 cachep->shared*cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003935 0xbaadf00d, gfp);
Eric Dumazet63109842007-05-06 14:49:28 -07003936 if (!new_shared) {
3937 free_alien_cache(new_alien);
3938 goto fail;
3939 }
Christoph Lameter0718dc22006-03-25 03:06:47 -08003940 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003941
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003942 n = cachep->node[node];
3943 if (n) {
3944 struct array_cache *shared = n->shared;
Christoph Lametercafeb022006-03-25 03:06:46 -08003945
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003946 local_spin_lock_irq(slab_lock, &n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07003947
Christoph Lametercafeb022006-03-25 03:06:46 -08003948 if (shared)
Christoph Lameter0718dc22006-03-25 03:06:47 -08003949 free_block(cachep, shared->entry,
3950 shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07003951
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003952 n->shared = new_shared;
3953 if (!n->alien) {
3954 n->alien = new_alien;
Christoph Lametere498be72005-09-09 13:03:32 -07003955 new_alien = NULL;
3956 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003957 n->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003958 cachep->batchcount + cachep->num;
Peter Zijlstra696ac522009-07-03 08:44:43 -05003959 unlock_l3_and_free_delayed(&n->list_lock);
3960
Christoph Lametercafeb022006-03-25 03:06:46 -08003961 kfree(shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003962 free_alien_cache(new_alien);
3963 continue;
3964 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003965 n = kmalloc_node(sizeof(struct kmem_cache_node), gfp, node);
3966 if (!n) {
Christoph Lameter0718dc22006-03-25 03:06:47 -08003967 free_alien_cache(new_alien);
3968 kfree(new_shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003969 goto fail;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003970 }
Christoph Lametere498be72005-09-09 13:03:32 -07003971
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003972 kmem_cache_node_init(n);
3973 n->next_reap = jiffies + REAPTIMEOUT_LIST3 +
Andrew Mortona737b3e2006-03-22 00:08:11 -08003974 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003975 n->shared = new_shared;
3976 n->alien = new_alien;
3977 n->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003978 cachep->batchcount + cachep->num;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003979 cachep->node[node] = n;
Christoph Lametere498be72005-09-09 13:03:32 -07003980 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003981 return 0;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003982
Andrew Mortona737b3e2006-03-22 00:08:11 -08003983fail:
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003984 if (!cachep->list.next) {
Christoph Lameter0718dc22006-03-25 03:06:47 -08003985 /* Cache is not active yet. Roll back what we did */
3986 node--;
3987 while (node >= 0) {
Christoph Lameter6a673682013-01-10 19:14:19 +00003988 if (cachep->node[node]) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003989 n = cachep->node[node];
Christoph Lameter0718dc22006-03-25 03:06:47 -08003990
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003991 kfree(n->shared);
3992 free_alien_cache(n->alien);
3993 kfree(n);
Christoph Lameter6a673682013-01-10 19:14:19 +00003994 cachep->node[node] = NULL;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003995 }
3996 node--;
3997 }
3998 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003999 return -ENOMEM;
Christoph Lametere498be72005-09-09 13:03:32 -07004000}
4001
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002struct ccupdate_struct {
Pekka Enberg343e0d72006-02-01 03:05:50 -08004003 struct kmem_cache *cachep;
Eric Dumazetacfe7d72011-07-25 08:55:42 +02004004 struct array_cache *new[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005};
4006
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004007static void __do_ccupdate_local(void *info, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008{
Andrew Mortona737b3e2006-03-22 00:08:11 -08004009 struct ccupdate_struct *new = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 struct array_cache *old;
4011
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004012 old = cpu_cache_get_on_cpu(new->cachep, cpu);
Christoph Lametere498be72005-09-09 13:03:32 -07004013
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004014 new->cachep->array[cpu] = new->new[cpu];
4015 new->new[cpu] = old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016}
4017
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004018#ifndef CONFIG_PREEMPT_RT_BASE
4019static void do_ccupdate_local(void *info)
4020{
4021 __do_ccupdate_local(info, smp_processor_id());
4022}
4023#else
4024static void do_ccupdate_local(void *info, int cpu)
4025{
4026 __do_ccupdate_local(info, cpu);
4027}
4028#endif
4029
Christoph Lameter18004c52012-07-06 15:25:12 -05004030/* Always called with the slab_mutex held */
Glauber Costa943a4512012-12-18 14:23:03 -08004031static int __do_tune_cpucache(struct kmem_cache *cachep, int limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03004032 int batchcount, int shared, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033{
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004034 struct ccupdate_struct *new;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07004035 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
Eric Dumazetacfe7d72011-07-25 08:55:42 +02004037 new = kzalloc(sizeof(*new) + nr_cpu_ids * sizeof(struct array_cache *),
4038 gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004039 if (!new)
4040 return -ENOMEM;
4041
Christoph Lametere498be72005-09-09 13:03:32 -07004042 for_each_online_cpu(i) {
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07004043 new->new[i] = alloc_arraycache(cpu_to_mem(i), limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03004044 batchcount, gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004045 if (!new->new[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004046 for (i--; i >= 0; i--)
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004047 kfree(new->new[i]);
4048 kfree(new);
Christoph Lametere498be72005-09-09 13:03:32 -07004049 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 }
4051 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004052 new->cachep = cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004054 slab_on_each_cpu(do_ccupdate_local, (void *)new);
Christoph Lametere498be72005-09-09 13:03:32 -07004055
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 check_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 cachep->batchcount = batchcount;
4058 cachep->limit = limit;
Christoph Lametere498be72005-09-09 13:03:32 -07004059 cachep->shared = shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
Christoph Lametere498be72005-09-09 13:03:32 -07004061 for_each_online_cpu(i) {
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004062 struct array_cache *ccold = new->new[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 if (!ccold)
4064 continue;
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004065 local_spin_lock_irq(slab_lock,
4066 &cachep->node[cpu_to_mem(i)]->list_lock);
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07004067 free_block(cachep, ccold->entry, ccold->avail, cpu_to_mem(i));
Peter Zijlstra696ac522009-07-03 08:44:43 -05004068
4069 unlock_l3_and_free_delayed(&cachep->node[cpu_to_mem(i)]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 kfree(ccold);
4071 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004072 kfree(new);
Pekka Enberg83b519e2009-06-10 19:40:04 +03004073 return alloc_kmemlist(cachep, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074}
4075
Glauber Costa943a4512012-12-18 14:23:03 -08004076static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
4077 int batchcount, int shared, gfp_t gfp)
4078{
4079 int ret;
4080 struct kmem_cache *c = NULL;
4081 int i = 0;
4082
4083 ret = __do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
4084
4085 if (slab_state < FULL)
4086 return ret;
4087
4088 if ((ret < 0) || !is_root_cache(cachep))
4089 return ret;
4090
Glauber Costaebe945c2012-12-18 14:23:10 -08004091 VM_BUG_ON(!mutex_is_locked(&slab_mutex));
Glauber Costa943a4512012-12-18 14:23:03 -08004092 for_each_memcg_cache_index(i) {
4093 c = cache_from_memcg(cachep, i);
4094 if (c)
4095 /* return value determined by the parent cache only */
4096 __do_tune_cpucache(c, limit, batchcount, shared, gfp);
4097 }
4098
4099 return ret;
4100}
4101
Christoph Lameter18004c52012-07-06 15:25:12 -05004102/* Called with slab_mutex held always */
Pekka Enberg83b519e2009-06-10 19:40:04 +03004103static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104{
4105 int err;
Glauber Costa943a4512012-12-18 14:23:03 -08004106 int limit = 0;
4107 int shared = 0;
4108 int batchcount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109
Glauber Costa943a4512012-12-18 14:23:03 -08004110 if (!is_root_cache(cachep)) {
4111 struct kmem_cache *root = memcg_root_cache(cachep);
4112 limit = root->limit;
4113 shared = root->shared;
4114 batchcount = root->batchcount;
4115 }
4116
4117 if (limit && shared && batchcount)
4118 goto skip_setup;
Andrew Mortona737b3e2006-03-22 00:08:11 -08004119 /*
4120 * The head array serves three purposes:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121 * - create a LIFO ordering, i.e. return objects that are cache-warm
4122 * - reduce the number of spinlock operations.
Andrew Mortona737b3e2006-03-22 00:08:11 -08004123 * - reduce the number of linked list operations on the slab and
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 * bufctl chains: array operations are cheaper.
4125 * The numbers are guessed, we should auto-tune as described by
4126 * Bonwick.
4127 */
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004128 if (cachep->size > 131072)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 limit = 1;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004130 else if (cachep->size > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 limit = 8;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004132 else if (cachep->size > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 limit = 24;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004134 else if (cachep->size > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 limit = 54;
4136 else
4137 limit = 120;
4138
Andrew Mortona737b3e2006-03-22 00:08:11 -08004139 /*
4140 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 * allocation behaviour: Most allocs on one cpu, most free operations
4142 * on another cpu. For these cases, an efficient object passing between
4143 * cpus is necessary. This is provided by a shared array. The array
4144 * replaces Bonwick's magazine layer.
4145 * On uniprocessor, it's functionally equivalent (but less efficient)
4146 * to a larger limit. Thus disabled by default.
4147 */
4148 shared = 0;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004149 if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 shared = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
4152#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08004153 /*
4154 * With debugging enabled, large batchcount lead to excessively long
4155 * periods with disabled local interrupts. Limit the batchcount
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 */
4157 if (limit > 32)
4158 limit = 32;
4159#endif
Glauber Costa943a4512012-12-18 14:23:03 -08004160 batchcount = (limit + 1) / 2;
4161skip_setup:
4162 err = do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 if (err)
4164 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004165 cachep->name, -err);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07004166 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167}
4168
Christoph Lameter1b552532006-03-22 00:09:07 -08004169/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004170 * Drain an array if it contains any elements taking the node lock only if
4171 * necessary. Note that the node listlock also protects the array_cache
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004172 * if drain_array() is used on the shared array.
Christoph Lameter1b552532006-03-22 00:09:07 -08004173 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004174static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
Christoph Lameter1b552532006-03-22 00:09:07 -08004175 struct array_cache *ac, int force, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176{
4177 int tofree;
4178
Christoph Lameter1b552532006-03-22 00:09:07 -08004179 if (!ac || !ac->avail)
4180 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 if (ac->touched && !force) {
4182 ac->touched = 0;
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004183 } else {
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004184 local_spin_lock_irq(slab_lock, &n->list_lock);
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004185 if (ac->avail) {
4186 tofree = force ? ac->avail : (ac->limit + 4) / 5;
4187 if (tofree > ac->avail)
4188 tofree = (ac->avail + 1) / 2;
4189 free_block(cachep, ac->entry, tofree, node);
4190 ac->avail -= tofree;
4191 memmove(ac->entry, &(ac->entry[tofree]),
4192 sizeof(void *) * ac->avail);
4193 }
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004194 local_spin_unlock_irq(slab_lock, &n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 }
4196}
4197
4198/**
4199 * cache_reap - Reclaim memory from caches.
Randy Dunlap05fb6bf2007-02-28 20:12:13 -08004200 * @w: work descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 *
4202 * Called from workqueue/eventd every few seconds.
4203 * Purpose:
4204 * - clear the per-cpu caches for this CPU.
4205 * - return freeable pages to the main free memory pool.
4206 *
Andrew Mortona737b3e2006-03-22 00:08:11 -08004207 * If we cannot acquire the cache chain mutex then just give up - we'll try
4208 * again on the next iteration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209 */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004210static void cache_reap(struct work_struct *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211{
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004212 struct kmem_cache *searchp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004213 struct kmem_cache_node *n;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07004214 int node = numa_mem_id();
Jean Delvarebf6aede2009-04-02 16:56:54 -07004215 struct delayed_work *work = to_delayed_work(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216
Christoph Lameter18004c52012-07-06 15:25:12 -05004217 if (!mutex_trylock(&slab_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 /* Give up. Setup the next iteration. */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004219 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220
Christoph Lameter18004c52012-07-06 15:25:12 -05004221 list_for_each_entry(searchp, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222 check_irq_on();
4223
Christoph Lameter35386e32006-03-22 00:09:05 -08004224 /*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004225 * We only take the node lock if absolutely necessary and we
Christoph Lameter35386e32006-03-22 00:09:05 -08004226 * have established with reasonable certainty that
4227 * we can do some work if the lock was obtained.
4228 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004229 n = searchp->node[node];
Christoph Lameter35386e32006-03-22 00:09:05 -08004230
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004231 reap_alien(searchp, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004233 drain_array(searchp, n, cpu_cache_get(searchp), 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234
Christoph Lameter35386e32006-03-22 00:09:05 -08004235 /*
4236 * These are racy checks but it does not matter
4237 * if we skip one check or scan twice.
4238 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004239 if (time_after(n->next_reap, jiffies))
Christoph Lameter35386e32006-03-22 00:09:05 -08004240 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004242 n->next_reap = jiffies + REAPTIMEOUT_LIST3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004244 drain_array(searchp, n, n->shared, 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004246 if (n->free_touched)
4247 n->free_touched = 0;
Christoph Lametered11d9e2006-06-30 01:55:45 -07004248 else {
4249 int freed;
4250
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004251 freed = drain_freelist(searchp, n, (n->free_limit +
Christoph Lametered11d9e2006-06-30 01:55:45 -07004252 5 * searchp->num - 1) / (5 * searchp->num));
4253 STATS_ADD_REAPED(searchp, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 }
Christoph Lameter35386e32006-03-22 00:09:05 -08004255next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 cond_resched();
4257 }
4258 check_irq_on();
Christoph Lameter18004c52012-07-06 15:25:12 -05004259 mutex_unlock(&slab_mutex);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004260 next_reap_node();
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004261out:
Andrew Mortona737b3e2006-03-22 00:08:11 -08004262 /* Set up the next iteration */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004263 schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_CPUC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264}
4265
Linus Torvalds158a9622008-01-02 13:04:48 -08004266#ifdef CONFIG_SLABINFO
Glauber Costa0d7561c2012-10-19 18:20:27 +04004267void get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004269 struct slab *slabp;
4270 unsigned long active_objs;
4271 unsigned long num_objs;
4272 unsigned long active_slabs = 0;
4273 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004274 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 char *error = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07004276 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004277 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 active_objs = 0;
4280 num_slabs = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004281 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004282 n = cachep->node[node];
4283 if (!n)
Christoph Lametere498be72005-09-09 13:03:32 -07004284 continue;
4285
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004286 check_irq_on();
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004287 local_spin_lock_irq(slab_lock, &n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07004288
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004289 list_for_each_entry(slabp, &n->slabs_full, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004290 if (slabp->inuse != cachep->num && !error)
4291 error = "slabs_full accounting error";
4292 active_objs += cachep->num;
4293 active_slabs++;
4294 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004295 list_for_each_entry(slabp, &n->slabs_partial, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004296 if (slabp->inuse == cachep->num && !error)
4297 error = "slabs_partial inuse accounting error";
4298 if (!slabp->inuse && !error)
4299 error = "slabs_partial/inuse accounting error";
4300 active_objs += slabp->inuse;
4301 active_slabs++;
4302 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004303 list_for_each_entry(slabp, &n->slabs_free, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004304 if (slabp->inuse && !error)
4305 error = "slabs_free/inuse accounting error";
4306 num_slabs++;
4307 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004308 free_objects += n->free_objects;
4309 if (n->shared)
4310 shared_avail += n->shared->avail;
Christoph Lametere498be72005-09-09 13:03:32 -07004311
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004312 local_spin_unlock_irq(slab_lock, &n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004314 num_slabs += active_slabs;
4315 num_objs = num_slabs * cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07004316 if (num_objs - active_objs != free_objects && !error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317 error = "free_objects accounting error";
4318
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004319 name = cachep->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320 if (error)
4321 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
4322
Glauber Costa0d7561c2012-10-19 18:20:27 +04004323 sinfo->active_objs = active_objs;
4324 sinfo->num_objs = num_objs;
4325 sinfo->active_slabs = active_slabs;
4326 sinfo->num_slabs = num_slabs;
4327 sinfo->shared_avail = shared_avail;
4328 sinfo->limit = cachep->limit;
4329 sinfo->batchcount = cachep->batchcount;
4330 sinfo->shared = cachep->shared;
4331 sinfo->objects_per_slab = cachep->num;
4332 sinfo->cache_order = cachep->gfporder;
4333}
4334
4335void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *cachep)
4336{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337#if STATS
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004338 { /* node stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 unsigned long high = cachep->high_mark;
4340 unsigned long allocs = cachep->num_allocations;
4341 unsigned long grown = cachep->grown;
4342 unsigned long reaped = cachep->reaped;
4343 unsigned long errors = cachep->errors;
4344 unsigned long max_freeable = cachep->max_freeable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 unsigned long node_allocs = cachep->node_allocs;
Christoph Lametere498be72005-09-09 13:03:32 -07004346 unsigned long node_frees = cachep->node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004347 unsigned long overflows = cachep->node_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
Joe Perchese92dd4f2010-03-26 19:27:58 -07004349 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu "
4350 "%4lu %4lu %4lu %4lu %4lu",
4351 allocs, high, grown,
4352 reaped, errors, max_freeable, node_allocs,
4353 node_frees, overflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 }
4355 /* cpu stats */
4356 {
4357 unsigned long allochit = atomic_read(&cachep->allochit);
4358 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
4359 unsigned long freehit = atomic_read(&cachep->freehit);
4360 unsigned long freemiss = atomic_read(&cachep->freemiss);
4361
4362 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004363 allochit, allocmiss, freehit, freemiss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 }
4365#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366}
4367
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368#define MAX_SLABINFO_WRITE 128
4369/**
4370 * slabinfo_write - Tuning for the slab allocator
4371 * @file: unused
4372 * @buffer: user buffer
4373 * @count: data length
4374 * @ppos: unused
4375 */
Glauber Costab7454ad2012-10-19 18:20:25 +04004376ssize_t slabinfo_write(struct file *file, const char __user *buffer,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004377 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004379 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 int limit, batchcount, shared, res;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004381 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004382
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 if (count > MAX_SLABINFO_WRITE)
4384 return -EINVAL;
4385 if (copy_from_user(&kbuf, buffer, count))
4386 return -EFAULT;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004387 kbuf[MAX_SLABINFO_WRITE] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
4389 tmp = strchr(kbuf, ' ');
4390 if (!tmp)
4391 return -EINVAL;
4392 *tmp = '\0';
4393 tmp++;
4394 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
4395 return -EINVAL;
4396
4397 /* Find the cache in the chain of caches. */
Christoph Lameter18004c52012-07-06 15:25:12 -05004398 mutex_lock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 res = -EINVAL;
Christoph Lameter18004c52012-07-06 15:25:12 -05004400 list_for_each_entry(cachep, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 if (!strcmp(cachep->name, kbuf)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08004402 if (limit < 1 || batchcount < 1 ||
4403 batchcount > limit || shared < 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07004404 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07004406 res = do_tune_cpucache(cachep, limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03004407 batchcount, shared,
4408 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409 }
4410 break;
4411 }
4412 }
Christoph Lameter18004c52012-07-06 15:25:12 -05004413 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 if (res >= 0)
4415 res = count;
4416 return res;
4417}
Al Viro871751e2006-03-25 03:06:39 -08004418
4419#ifdef CONFIG_DEBUG_SLAB_LEAK
4420
4421static void *leaks_start(struct seq_file *m, loff_t *pos)
4422{
Christoph Lameter18004c52012-07-06 15:25:12 -05004423 mutex_lock(&slab_mutex);
4424 return seq_list_start(&slab_caches, *pos);
Al Viro871751e2006-03-25 03:06:39 -08004425}
4426
4427static inline int add_caller(unsigned long *n, unsigned long v)
4428{
4429 unsigned long *p;
4430 int l;
4431 if (!v)
4432 return 1;
4433 l = n[1];
4434 p = n + 2;
4435 while (l) {
4436 int i = l/2;
4437 unsigned long *q = p + 2 * i;
4438 if (*q == v) {
4439 q[1]++;
4440 return 1;
4441 }
4442 if (*q > v) {
4443 l = i;
4444 } else {
4445 p = q + 2;
4446 l -= i + 1;
4447 }
4448 }
4449 if (++n[1] == n[0])
4450 return 0;
4451 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4452 p[0] = v;
4453 p[1] = 1;
4454 return 1;
4455}
4456
4457static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
4458{
4459 void *p;
4460 int i;
4461 if (n[0] == n[1])
4462 return;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004463 for (i = 0, p = s->s_mem; i < c->num; i++, p += c->size) {
Al Viro871751e2006-03-25 03:06:39 -08004464 if (slab_bufctl(s)[i] != BUFCTL_ACTIVE)
4465 continue;
4466 if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
4467 return;
4468 }
4469}
4470
4471static void show_symbol(struct seq_file *m, unsigned long address)
4472{
4473#ifdef CONFIG_KALLSYMS
Al Viro871751e2006-03-25 03:06:39 -08004474 unsigned long offset, size;
Tejun Heo9281ace2007-07-17 04:03:51 -07004475 char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
Al Viro871751e2006-03-25 03:06:39 -08004476
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004477 if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
Al Viro871751e2006-03-25 03:06:39 -08004478 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004479 if (modname[0])
Al Viro871751e2006-03-25 03:06:39 -08004480 seq_printf(m, " [%s]", modname);
4481 return;
4482 }
4483#endif
4484 seq_printf(m, "%p", (void *)address);
4485}
4486
4487static int leaks_show(struct seq_file *m, void *p)
4488{
Thierry Reding0672aa72012-06-22 19:42:49 +02004489 struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);
Al Viro871751e2006-03-25 03:06:39 -08004490 struct slab *slabp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004491 struct kmem_cache_node *n;
Al Viro871751e2006-03-25 03:06:39 -08004492 const char *name;
Christoph Lameterdb845062013-02-05 18:45:23 +00004493 unsigned long *x = m->private;
Al Viro871751e2006-03-25 03:06:39 -08004494 int node;
4495 int i;
4496
4497 if (!(cachep->flags & SLAB_STORE_USER))
4498 return 0;
4499 if (!(cachep->flags & SLAB_RED_ZONE))
4500 return 0;
4501
4502 /* OK, we can do it */
4503
Christoph Lameterdb845062013-02-05 18:45:23 +00004504 x[1] = 0;
Al Viro871751e2006-03-25 03:06:39 -08004505
4506 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004507 n = cachep->node[node];
4508 if (!n)
Al Viro871751e2006-03-25 03:06:39 -08004509 continue;
4510
4511 check_irq_on();
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004512 local_spin_lock_irq(slab_lock, &n->list_lock);
Al Viro871751e2006-03-25 03:06:39 -08004513
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004514 list_for_each_entry(slabp, &n->slabs_full, list)
Christoph Lameterdb845062013-02-05 18:45:23 +00004515 handle_slab(x, cachep, slabp);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004516 list_for_each_entry(slabp, &n->slabs_partial, list)
Christoph Lameterdb845062013-02-05 18:45:23 +00004517 handle_slab(x, cachep, slabp);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004518 local_spin_unlock_irq(slab_lock, &n->list_lock);
Al Viro871751e2006-03-25 03:06:39 -08004519 }
4520 name = cachep->name;
Christoph Lameterdb845062013-02-05 18:45:23 +00004521 if (x[0] == x[1]) {
Al Viro871751e2006-03-25 03:06:39 -08004522 /* Increase the buffer size */
Christoph Lameter18004c52012-07-06 15:25:12 -05004523 mutex_unlock(&slab_mutex);
Christoph Lameterdb845062013-02-05 18:45:23 +00004524 m->private = kzalloc(x[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
Al Viro871751e2006-03-25 03:06:39 -08004525 if (!m->private) {
4526 /* Too bad, we are really out */
Christoph Lameterdb845062013-02-05 18:45:23 +00004527 m->private = x;
Christoph Lameter18004c52012-07-06 15:25:12 -05004528 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004529 return -ENOMEM;
4530 }
Christoph Lameterdb845062013-02-05 18:45:23 +00004531 *(unsigned long *)m->private = x[0] * 2;
4532 kfree(x);
Christoph Lameter18004c52012-07-06 15:25:12 -05004533 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004534 /* Now make sure this entry will be retried */
4535 m->count = m->size;
4536 return 0;
4537 }
Christoph Lameterdb845062013-02-05 18:45:23 +00004538 for (i = 0; i < x[1]; i++) {
4539 seq_printf(m, "%s: %lu ", name, x[2*i+3]);
4540 show_symbol(m, x[2*i+2]);
Al Viro871751e2006-03-25 03:06:39 -08004541 seq_putc(m, '\n');
4542 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004543
Al Viro871751e2006-03-25 03:06:39 -08004544 return 0;
4545}
4546
Glauber Costab7454ad2012-10-19 18:20:25 +04004547static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4548{
4549 return seq_list_next(p, &slab_caches, pos);
4550}
4551
4552static void s_stop(struct seq_file *m, void *p)
4553{
4554 mutex_unlock(&slab_mutex);
4555}
4556
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004557static const struct seq_operations slabstats_op = {
Al Viro871751e2006-03-25 03:06:39 -08004558 .start = leaks_start,
4559 .next = s_next,
4560 .stop = s_stop,
4561 .show = leaks_show,
4562};
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004563
4564static int slabstats_open(struct inode *inode, struct file *file)
4565{
4566 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
4567 int ret = -ENOMEM;
4568 if (n) {
4569 ret = seq_open(file, &slabstats_op);
4570 if (!ret) {
4571 struct seq_file *m = file->private_data;
4572 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
4573 m->private = n;
4574 n = NULL;
4575 }
4576 kfree(n);
4577 }
4578 return ret;
4579}
4580
4581static const struct file_operations proc_slabstats_operations = {
4582 .open = slabstats_open,
4583 .read = seq_read,
4584 .llseek = seq_lseek,
4585 .release = seq_release_private,
4586};
Al Viro871751e2006-03-25 03:06:39 -08004587#endif
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004588
4589static int __init slab_proc_init(void)
4590{
4591#ifdef CONFIG_DEBUG_SLAB_LEAK
4592 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
4593#endif
4594 return 0;
4595}
4596module_init(slab_proc_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597#endif
4598
Manfred Spraul00e145b2005-09-03 15:55:07 -07004599/**
4600 * ksize - get the actual amount of memory allocated for a given object
4601 * @objp: Pointer to the object
4602 *
4603 * kmalloc may internally round up allocations and return more memory
4604 * than requested. ksize() can be used to determine the actual amount of
4605 * memory allocated. The caller may use this additional memory, even though
4606 * a smaller amount of memory was initially specified with the kmalloc call.
4607 * The caller must guarantee that objp points to a valid object previously
4608 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4609 * must not be freed during the duration of the call.
4610 */
Pekka Enbergfd76bab2007-05-06 14:48:40 -07004611size_t ksize(const void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612{
Christoph Lameteref8b4522007-10-16 01:24:46 -07004613 BUG_ON(!objp);
4614 if (unlikely(objp == ZERO_SIZE_PTR))
Manfred Spraul00e145b2005-09-03 15:55:07 -07004615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
Christoph Lameter8c138bc2012-06-13 10:24:58 -05004617 return virt_to_cache(objp)->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618}
Kirill A. Shutemovb1aabec2009-02-10 15:21:44 +02004619EXPORT_SYMBOL(ksize);