blob: 1e9330f43f46df015d2ee0ed3d733de816cbfe7a [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)) {
Peter Zijlstra696ac522009-07-03 08:44:43 -05001279 unlock_l3_and_free_delayed(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001280 goto free_array_cache;
1281 }
1282
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001283 shared = n->shared;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001284 if (shared) {
1285 free_block(cachep, shared->entry,
1286 shared->avail, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001287 n->shared = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001288 }
1289
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001290 alien = n->alien;
1291 n->alien = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001292
Peter Zijlstra696ac522009-07-03 08:44:43 -05001293 unlock_l3_and_free_delayed(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001294
1295 kfree(shared);
1296 if (alien) {
1297 drain_alien_cache(cachep, alien);
1298 free_alien_cache(alien);
1299 }
1300free_array_cache:
1301 kfree(nc);
1302 }
1303 /*
1304 * In the previous loop, all the objects were freed to
1305 * the respective cache's slabs, now we can go ahead and
1306 * shrink each nodelist to its limit.
1307 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001308 list_for_each_entry(cachep, &slab_caches, list) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001309 n = cachep->node[node];
1310 if (!n)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001311 continue;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001312 drain_freelist(cachep, n, n->free_objects);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001313 }
1314}
1315
1316static int __cpuinit cpuup_prepare(long cpu)
1317{
Pekka Enberg343e0d72006-02-01 03:05:50 -08001318 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001319 struct kmem_cache_node *n = NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001320 int node = cpu_to_mem(cpu);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001321 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001323 /*
1324 * We need to do this right in the beginning since
1325 * alloc_arraycache's are going to use this list.
1326 * kmalloc_node allows us to add the slab to the right
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001327 * kmem_cache_node and not this cpu's kmem_cache_node
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001328 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001329 err = init_cache_node_node(node);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001330 if (err < 0)
1331 goto bad;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001332
1333 /*
1334 * Now we can go ahead with allocating the shared arrays and
1335 * array caches
1336 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001337 list_for_each_entry(cachep, &slab_caches, list) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001338 struct array_cache *nc;
1339 struct array_cache *shared = NULL;
1340 struct array_cache **alien = NULL;
1341
1342 nc = alloc_arraycache(node, cachep->limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001343 cachep->batchcount, GFP_KERNEL);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001344 if (!nc)
1345 goto bad;
1346 if (cachep->shared) {
1347 shared = alloc_arraycache(node,
1348 cachep->shared * cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001349 0xbaadf00d, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001350 if (!shared) {
1351 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001352 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001353 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001354 }
1355 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03001356 alien = alloc_alien_cache(node, cachep->limit, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001357 if (!alien) {
1358 kfree(shared);
1359 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001360 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001361 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001362 }
1363 cachep->array[cpu] = nc;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001364 n = cachep->node[node];
1365 BUG_ON(!n);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001366
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001367 local_spin_lock_irq(slab_lock, &n->list_lock);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001368 if (!n->shared) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001369 /*
1370 * We are serialised from CPU_DEAD or
1371 * CPU_UP_CANCELLED by the cpucontrol lock
1372 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001373 n->shared = shared;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001374 shared = NULL;
1375 }
1376#ifdef CONFIG_NUMA
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001377 if (!n->alien) {
1378 n->alien = alien;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001379 alien = NULL;
1380 }
1381#endif
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001382 local_spin_unlock_irq(slab_lock, &n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001383 kfree(shared);
1384 free_alien_cache(alien);
Peter Zijlstra83835b32011-07-22 15:26:05 +02001385 if (cachep->flags & SLAB_DEBUG_OBJECTS)
1386 slab_set_debugobj_lock_classes_node(cachep, node);
Glauber Costa6ccfb5b2012-12-18 14:22:31 -08001387 else if (!OFF_SLAB(cachep) &&
1388 !(cachep->flags & SLAB_DESTROY_BY_RCU))
1389 on_slab_lock_classes_node(cachep, node);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001390 }
Pekka Enbergce79ddc2009-11-23 22:01:15 +02001391 init_node_lock_keys(node);
1392
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001393 return 0;
1394bad:
Akinobu Mita12d00f62007-10-18 03:05:11 -07001395 cpuup_canceled(cpu);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001396 return -ENOMEM;
1397}
1398
1399static int __cpuinit cpuup_callback(struct notifier_block *nfb,
1400 unsigned long action, void *hcpu)
1401{
1402 long cpu = (long)hcpu;
1403 int err = 0;
1404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 switch (action) {
Heiko Carstens38c3bd92007-05-09 02:34:05 -07001406 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001407 case CPU_UP_PREPARE_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001408 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001409 err = cpuup_prepare(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001410 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411 break;
1412 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001413 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 start_cpu_timer(cpu);
1415 break;
1416#ifdef CONFIG_HOTPLUG_CPU
Christoph Lameter5830c592007-05-09 02:34:22 -07001417 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001418 case CPU_DOWN_PREPARE_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001419 /*
Christoph Lameter18004c52012-07-06 15:25:12 -05001420 * Shutdown cache reaper. Note that the slab_mutex is
Christoph Lameter5830c592007-05-09 02:34:22 -07001421 * held so that if cache_reap() is invoked it cannot do
1422 * anything expensive but will only modify reap_work
1423 * and reschedule the timer.
1424 */
Tejun Heoafe2c512010-12-14 16:21:17 +01001425 cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu));
Christoph Lameter5830c592007-05-09 02:34:22 -07001426 /* Now the cache_reaper is guaranteed to be not running. */
Tejun Heo1871e522009-10-29 22:34:13 +09001427 per_cpu(slab_reap_work, cpu).work.func = NULL;
Christoph Lameter5830c592007-05-09 02:34:22 -07001428 break;
1429 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001430 case CPU_DOWN_FAILED_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001431 start_cpu_timer(cpu);
1432 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001434 case CPU_DEAD_FROZEN:
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001435 /*
1436 * Even if all the cpus of a node are down, we don't free the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001437 * kmem_cache_node of any cache. This to avoid a race between
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001438 * cpu_down, and a kmalloc allocation from another cpu for
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001439 * memory from the node of the cpu going down. The node
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001440 * structure is usually allocated from kmem_cache_create() and
1441 * gets destroyed at kmem_cache_destroy().
1442 */
Simon Arlott183ff222007-10-20 01:27:18 +02001443 /* fall through */
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08001444#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001446 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001447 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001448 cpuup_canceled(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001449 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 }
Akinobu Mitaeac40682010-05-26 14:43:32 -07001452 return notifier_from_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453}
1454
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001455static struct notifier_block __cpuinitdata cpucache_notifier = {
1456 &cpuup_callback, NULL, 0
1457};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458
David Rientjes8f9f8d92010-03-27 19:40:47 -07001459#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
1460/*
1461 * Drains freelist for a node on each slab cache, used for memory hot-remove.
1462 * Returns -EBUSY if all objects cannot be drained so that the node is not
1463 * removed.
1464 *
Christoph Lameter18004c52012-07-06 15:25:12 -05001465 * Must hold slab_mutex.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001466 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001467static int __meminit drain_cache_node_node(int node)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001468{
1469 struct kmem_cache *cachep;
1470 int ret = 0;
1471
Christoph Lameter18004c52012-07-06 15:25:12 -05001472 list_for_each_entry(cachep, &slab_caches, list) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001473 struct kmem_cache_node *n;
David Rientjes8f9f8d92010-03-27 19:40:47 -07001474
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001475 n = cachep->node[node];
1476 if (!n)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001477 continue;
1478
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001479 drain_freelist(cachep, n, n->free_objects);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001480
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001481 if (!list_empty(&n->slabs_full) ||
1482 !list_empty(&n->slabs_partial)) {
David Rientjes8f9f8d92010-03-27 19:40:47 -07001483 ret = -EBUSY;
1484 break;
1485 }
1486 }
1487 return ret;
1488}
1489
1490static int __meminit slab_memory_callback(struct notifier_block *self,
1491 unsigned long action, void *arg)
1492{
1493 struct memory_notify *mnb = arg;
1494 int ret = 0;
1495 int nid;
1496
1497 nid = mnb->status_change_nid;
1498 if (nid < 0)
1499 goto out;
1500
1501 switch (action) {
1502 case MEM_GOING_ONLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001503 mutex_lock(&slab_mutex);
Christoph Lameter6a673682013-01-10 19:14:19 +00001504 ret = init_cache_node_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001505 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001506 break;
1507 case MEM_GOING_OFFLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001508 mutex_lock(&slab_mutex);
Christoph Lameter6a673682013-01-10 19:14:19 +00001509 ret = drain_cache_node_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001510 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001511 break;
1512 case MEM_ONLINE:
1513 case MEM_OFFLINE:
1514 case MEM_CANCEL_ONLINE:
1515 case MEM_CANCEL_OFFLINE:
1516 break;
1517 }
1518out:
Prarit Bhargava5fda1bd2011-03-22 16:30:49 -07001519 return notifier_from_errno(ret);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001520}
1521#endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
1522
Christoph Lametere498be72005-09-09 13:03:32 -07001523/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001524 * swap the static kmem_cache_node with kmalloced memory
Christoph Lametere498be72005-09-09 13:03:32 -07001525 */
Christoph Lameter6744f0872013-01-10 19:12:17 +00001526static void __init init_list(struct kmem_cache *cachep, struct kmem_cache_node *list,
David Rientjes8f9f8d92010-03-27 19:40:47 -07001527 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07001528{
Christoph Lameter6744f0872013-01-10 19:12:17 +00001529 struct kmem_cache_node *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001530
Christoph Lameter6744f0872013-01-10 19:12:17 +00001531 ptr = kmalloc_node(sizeof(struct kmem_cache_node), GFP_NOWAIT, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07001532 BUG_ON(!ptr);
1533
Christoph Lameter6744f0872013-01-10 19:12:17 +00001534 memcpy(ptr, list, sizeof(struct kmem_cache_node));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001535 /*
1536 * Do not assume that spinlocks can be initialized via memcpy:
1537 */
1538 spin_lock_init(&ptr->list_lock);
1539
Christoph Lametere498be72005-09-09 13:03:32 -07001540 MAKE_ALL_LISTS(cachep, ptr, nodeid);
Christoph Lameter6a673682013-01-10 19:14:19 +00001541 cachep->node[nodeid] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001542}
1543
Andrew Mortona737b3e2006-03-22 00:08:11 -08001544/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001545 * For setting up all the kmem_cache_node for cache whose buffer_size is same as
1546 * size of kmem_cache_node.
Pekka Enberg556a1692008-01-25 08:20:51 +02001547 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001548static void __init set_up_node(struct kmem_cache *cachep, int index)
Pekka Enberg556a1692008-01-25 08:20:51 +02001549{
1550 int node;
1551
1552 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001553 cachep->node[node] = &init_kmem_cache_node[index + node];
Christoph Lameter6a673682013-01-10 19:14:19 +00001554 cachep->node[node]->next_reap = jiffies +
Pekka Enberg556a1692008-01-25 08:20:51 +02001555 REAPTIMEOUT_LIST3 +
1556 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1557 }
1558}
1559
1560/*
Christoph Lameter3c583462012-11-28 16:23:01 +00001561 * The memory after the last cpu cache pointer is used for the
Christoph Lameter6a673682013-01-10 19:14:19 +00001562 * the node pointer.
Christoph Lameter3c583462012-11-28 16:23:01 +00001563 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001564static void setup_node_pointer(struct kmem_cache *cachep)
Christoph Lameter3c583462012-11-28 16:23:01 +00001565{
Christoph Lameter6a673682013-01-10 19:14:19 +00001566 cachep->node = (struct kmem_cache_node **)&cachep->array[nr_cpu_ids];
Christoph Lameter3c583462012-11-28 16:23:01 +00001567}
1568
1569/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08001570 * Initialisation. Called after the page allocator have been initialised and
1571 * before smp_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 */
1573void __init kmem_cache_init(void)
1574{
Christoph Lametere498be72005-09-09 13:03:32 -07001575 int i;
1576
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001577 kmem_cache = &kmem_cache_boot;
Christoph Lameter6a673682013-01-10 19:14:19 +00001578 setup_node_pointer(kmem_cache);
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001579
Mel Gormanb6e68bc2009-06-16 15:32:16 -07001580 if (num_possible_nodes() == 1)
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001581 use_alien_caches = 0;
1582
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001583 local_irq_lock_init(slab_lock);
Peter Zijlstra696ac522009-07-03 08:44:43 -05001584 for_each_possible_cpu(i)
1585 INIT_LIST_HEAD(&per_cpu(slab_free_list, i));
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001586
Christoph Lameter3c583462012-11-28 16:23:01 +00001587 for (i = 0; i < NUM_INIT_LISTS; i++)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001588 kmem_cache_node_init(&init_kmem_cache_node[i]);
Christoph Lameter3c583462012-11-28 16:23:01 +00001589
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001590 set_up_node(kmem_cache, CACHE_CACHE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 /*
1593 * Fragmentation resistance on low memory - only use bigger
David Rientjes3df1ccc2011-10-18 22:09:28 -07001594 * page orders on machines with more than 32MB of memory if
1595 * not overridden on the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 */
David Rientjes3df1ccc2011-10-18 22:09:28 -07001597 if (!slab_max_order_set && totalram_pages > (32 << 20) >> PAGE_SHIFT)
David Rientjes543585c2011-10-18 22:09:24 -07001598 slab_max_order = SLAB_MAX_ORDER_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 /* Bootstrap is tricky, because several objects are allocated
1601 * from caches that do not exist yet:
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001602 * 1) initialize the kmem_cache cache: it contains the struct
1603 * kmem_cache structures of all caches, except kmem_cache itself:
1604 * kmem_cache is statically allocated.
Christoph Lametere498be72005-09-09 13:03:32 -07001605 * Initially an __init data area is used for the head array and the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001606 * kmem_cache_node structures, it's replaced with a kmalloc allocated
Christoph Lametere498be72005-09-09 13:03:32 -07001607 * array at the end of the bootstrap.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 * 2) Create the first kmalloc cache.
Pekka Enberg343e0d72006-02-01 03:05:50 -08001609 * The struct kmem_cache for the new cache is allocated normally.
Christoph Lametere498be72005-09-09 13:03:32 -07001610 * An __init data area is used for the head array.
1611 * 3) Create the remaining kmalloc caches, with minimally sized
1612 * head arrays.
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001613 * 4) Replace the __init data head arrays for kmem_cache and the first
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 * kmalloc cache with kmalloc allocated arrays.
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001615 * 5) Replace the __init data for kmem_cache_node for kmem_cache and
Christoph Lametere498be72005-09-09 13:03:32 -07001616 * the other cache's with kmalloc allocated memory.
1617 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 */
1619
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001620 /* 1) create the kmem_cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Eric Dumazet8da34302007-05-06 14:49:29 -07001622 /*
Eric Dumazetb56efcf2011-07-20 19:04:23 +02001623 * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
Eric Dumazet8da34302007-05-06 14:49:29 -07001624 */
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001625 create_boot_cache(kmem_cache, "kmem_cache",
1626 offsetof(struct kmem_cache, array[nr_cpu_ids]) +
Christoph Lameter6744f0872013-01-10 19:12:17 +00001627 nr_node_ids * sizeof(struct kmem_cache_node *),
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001628 SLAB_HWCACHE_ALIGN);
1629 list_add(&kmem_cache->list, &slab_caches);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
1631 /* 2+3) create the kmalloc caches */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Andrew Mortona737b3e2006-03-22 00:08:11 -08001633 /*
1634 * Initialize the caches that provide memory for the array cache and the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001635 * kmem_cache_node structures first. Without this, further allocations will
Andrew Mortona737b3e2006-03-22 00:08:11 -08001636 * bug.
Christoph Lametere498be72005-09-09 13:03:32 -07001637 */
1638
Christoph Lametere3366012013-01-10 19:14:18 +00001639 kmalloc_caches[INDEX_AC] = create_kmalloc_cache("kmalloc-ac",
1640 kmalloc_size(INDEX_AC), ARCH_KMALLOC_FLAGS);
Christoph Lametere498be72005-09-09 13:03:32 -07001641
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001642 if (INDEX_AC != INDEX_NODE)
1643 kmalloc_caches[INDEX_NODE] =
1644 create_kmalloc_cache("kmalloc-node",
1645 kmalloc_size(INDEX_NODE), ARCH_KMALLOC_FLAGS);
Christoph Lametere498be72005-09-09 13:03:32 -07001646
Ingo Molnare0a42722006-06-23 02:03:46 -07001647 slab_early_init = 0;
1648
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 /* 4) Replace the bootstrap head arrays */
1650 {
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001651 struct array_cache *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001652
Pekka Enberg83b519e2009-06-10 19:40:04 +03001653 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001654
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001655 memcpy(ptr, cpu_cache_get(kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001656 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001657 /*
1658 * Do not assume that spinlocks can be initialized via memcpy:
1659 */
1660 spin_lock_init(&ptr->lock);
1661
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001662 kmem_cache->array[smp_processor_id()] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001663
Pekka Enberg83b519e2009-06-10 19:40:04 +03001664 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001665
Christoph Lametere3366012013-01-10 19:14:18 +00001666 BUG_ON(cpu_cache_get(kmalloc_caches[INDEX_AC])
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001667 != &initarray_generic.cache);
Christoph Lametere3366012013-01-10 19:14:18 +00001668 memcpy(ptr, cpu_cache_get(kmalloc_caches[INDEX_AC]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001669 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001670 /*
1671 * Do not assume that spinlocks can be initialized via memcpy:
1672 */
1673 spin_lock_init(&ptr->lock);
1674
Christoph Lametere3366012013-01-10 19:14:18 +00001675 kmalloc_caches[INDEX_AC]->array[smp_processor_id()] = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001677 /* 5) Replace the bootstrap kmem_cache_node */
Christoph Lametere498be72005-09-09 13:03:32 -07001678 {
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001679 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Mel Gorman9c09a952008-01-24 05:49:54 -08001681 for_each_online_node(nid) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001682 init_list(kmem_cache, &init_kmem_cache_node[CACHE_CACHE + nid], nid);
Pekka Enberg556a1692008-01-25 08:20:51 +02001683
Christoph Lametere3366012013-01-10 19:14:18 +00001684 init_list(kmalloc_caches[INDEX_AC],
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001685 &init_kmem_cache_node[SIZE_AC + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001686
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001687 if (INDEX_AC != INDEX_NODE) {
1688 init_list(kmalloc_caches[INDEX_NODE],
1689 &init_kmem_cache_node[SIZE_NODE + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001690 }
1691 }
1692 }
1693
Christoph Lameterf97d5f62013-01-10 19:12:17 +00001694 create_kmalloc_caches(ARCH_KMALLOC_FLAGS);
Pekka Enberg8429db52009-06-12 15:58:59 +03001695}
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001696
Pekka Enberg8429db52009-06-12 15:58:59 +03001697void __init kmem_cache_init_late(void)
1698{
1699 struct kmem_cache *cachep;
1700
Christoph Lameter97d06602012-07-06 15:25:11 -05001701 slab_state = UP;
Peter Zijlstra52cef182011-11-28 21:12:40 +01001702
Pekka Enberg8429db52009-06-12 15:58:59 +03001703 /* 6) resize the head arrays to their final sizes */
Christoph Lameter18004c52012-07-06 15:25:12 -05001704 mutex_lock(&slab_mutex);
1705 list_for_each_entry(cachep, &slab_caches, list)
Pekka Enberg8429db52009-06-12 15:58:59 +03001706 if (enable_cpucache(cachep, GFP_NOWAIT))
1707 BUG();
Christoph Lameter18004c52012-07-06 15:25:12 -05001708 mutex_unlock(&slab_mutex);
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001709
Michael Wang947ca182012-09-05 10:33:18 +08001710 /* Annotate slab for lockdep -- annotate the malloc caches */
1711 init_lock_keys();
1712
Christoph Lameter97d06602012-07-06 15:25:11 -05001713 /* Done! */
1714 slab_state = FULL;
1715
Andrew Mortona737b3e2006-03-22 00:08:11 -08001716 /*
1717 * Register a cpu startup notifier callback that initializes
1718 * cpu_cache_get for all new cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 */
1720 register_cpu_notifier(&cpucache_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721
David Rientjes8f9f8d92010-03-27 19:40:47 -07001722#ifdef CONFIG_NUMA
1723 /*
1724 * Register a memory hotplug callback that initializes and frees
Christoph Lameter6a673682013-01-10 19:14:19 +00001725 * node.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001726 */
1727 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
1728#endif
1729
Andrew Mortona737b3e2006-03-22 00:08:11 -08001730 /*
1731 * The reap timers are started later, with a module init call: That part
1732 * of the kernel is not yet operational.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 */
1734}
1735
1736static int __init cpucache_init(void)
1737{
1738 int cpu;
1739
Andrew Mortona737b3e2006-03-22 00:08:11 -08001740 /*
1741 * Register the timers that return unneeded pages to the page allocator
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 */
Christoph Lametere498be72005-09-09 13:03:32 -07001743 for_each_online_cpu(cpu)
Andrew Mortona737b3e2006-03-22 00:08:11 -08001744 start_cpu_timer(cpu);
Glauber Costaa164f8962012-06-21 00:59:18 +04001745
1746 /* Done! */
Christoph Lameter97d06602012-07-06 15:25:11 -05001747 slab_state = FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 return 0;
1749}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750__initcall(cpucache_init);
1751
Rafael Aquini8bdec192012-03-09 17:27:27 -03001752static noinline void
1753slab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid)
1754{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001755 struct kmem_cache_node *n;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001756 struct slab *slabp;
1757 unsigned long flags;
1758 int node;
1759
1760 printk(KERN_WARNING
1761 "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n",
1762 nodeid, gfpflags);
1763 printk(KERN_WARNING " cache: %s, object size: %d, order: %d\n",
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05001764 cachep->name, cachep->size, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001765
1766 for_each_online_node(node) {
1767 unsigned long active_objs = 0, num_objs = 0, free_objects = 0;
1768 unsigned long active_slabs = 0, num_slabs = 0;
1769
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001770 n = cachep->node[node];
1771 if (!n)
Rafael Aquini8bdec192012-03-09 17:27:27 -03001772 continue;
1773
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001774 spin_lock_irqsave(&n->list_lock, flags);
1775 list_for_each_entry(slabp, &n->slabs_full, list) {
Rafael Aquini8bdec192012-03-09 17:27:27 -03001776 active_objs += cachep->num;
1777 active_slabs++;
1778 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001779 list_for_each_entry(slabp, &n->slabs_partial, list) {
Rafael Aquini8bdec192012-03-09 17:27:27 -03001780 active_objs += slabp->inuse;
1781 active_slabs++;
1782 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001783 list_for_each_entry(slabp, &n->slabs_free, list)
Rafael Aquini8bdec192012-03-09 17:27:27 -03001784 num_slabs++;
1785
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001786 free_objects += n->free_objects;
1787 spin_unlock_irqrestore(&n->list_lock, flags);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001788
1789 num_slabs += active_slabs;
1790 num_objs = num_slabs * cachep->num;
1791 printk(KERN_WARNING
1792 " node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
1793 node, active_slabs, num_slabs, active_objs, num_objs,
1794 free_objects);
1795 }
1796}
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798/*
1799 * Interface to system's page allocator. No need to hold the cache-lock.
1800 *
1801 * If we requested dmaable memory, we will get it. Even if we
1802 * did not request dmaable memory, we might get it, but that
1803 * would be relatively rare and ignorable.
1804 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001805static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
1807 struct page *page;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001808 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 int i;
1810
Luke Yangd6fef9d2006-04-10 22:52:56 -07001811#ifndef CONFIG_MMU
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001812 /*
1813 * Nommu uses slab's for process anonymous memory allocations, and thus
1814 * requires __GFP_COMP to properly refcount higher order allocations
Luke Yangd6fef9d2006-04-10 22:52:56 -07001815 */
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001816 flags |= __GFP_COMP;
Luke Yangd6fef9d2006-04-10 22:52:56 -07001817#endif
Christoph Lameter765c4502006-09-27 01:50:08 -07001818
Glauber Costaa618e892012-06-14 16:17:21 +04001819 flags |= cachep->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001820 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1821 flags |= __GFP_RECLAIMABLE;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001822
Linus Torvalds517d0862009-06-16 19:50:13 -07001823 page = alloc_pages_exact_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001824 if (!page) {
1825 if (!(flags & __GFP_NOWARN) && printk_ratelimit())
1826 slab_out_of_memory(cachep, flags, nodeid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 return NULL;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Mel Gormanb37f1dd2012-07-31 16:44:03 -07001830 /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
Mel Gorman072bb0a2012-07-31 16:43:58 -07001831 if (unlikely(page->pfmemalloc))
1832 pfmemalloc_active = true;
1833
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001834 nr_pages = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
Christoph Lameter972d1a72006-09-25 23:31:51 -07001836 add_zone_page_state(page_zone(page),
1837 NR_SLAB_RECLAIMABLE, nr_pages);
1838 else
1839 add_zone_page_state(page_zone(page),
1840 NR_SLAB_UNRECLAIMABLE, nr_pages);
Mel Gorman072bb0a2012-07-31 16:43:58 -07001841 for (i = 0; i < nr_pages; i++) {
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001842 __SetPageSlab(page + i);
Pekka Enbergc175eea2008-05-09 20:35:53 +02001843
Mel Gorman072bb0a2012-07-31 16:43:58 -07001844 if (page->pfmemalloc)
1845 SetPageSlabPfmemalloc(page + i);
1846 }
Glauber Costa1f458cb2012-12-18 14:22:50 -08001847 memcg_bind_pages(cachep, cachep->gfporder);
Mel Gorman072bb0a2012-07-31 16:43:58 -07001848
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001849 if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
1850 kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
1851
1852 if (cachep->ctor)
1853 kmemcheck_mark_uninitialized_pages(page, nr_pages);
1854 else
1855 kmemcheck_mark_unallocated_pages(page, nr_pages);
1856 }
Pekka Enbergc175eea2008-05-09 20:35:53 +02001857
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001858 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859}
1860
1861/*
1862 * Interface to system's page release.
1863 */
Peter Zijlstra696ac522009-07-03 08:44:43 -05001864static void kmem_freepages(struct kmem_cache *cachep, void *addr, bool delayed)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001866 unsigned long i = (1 << cachep->gfporder);
Peter Zijlstra696ac522009-07-03 08:44:43 -05001867 struct page *page, *basepage = virt_to_page(addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 const unsigned long nr_freed = i;
1869
Peter Zijlstra696ac522009-07-03 08:44:43 -05001870 page = basepage;
1871
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001872 kmemcheck_free_shadow(page, cachep->gfporder);
Pekka Enbergc175eea2008-05-09 20:35:53 +02001873
Christoph Lameter972d1a72006-09-25 23:31:51 -07001874 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1875 sub_zone_page_state(page_zone(page),
1876 NR_SLAB_RECLAIMABLE, nr_freed);
1877 else
1878 sub_zone_page_state(page_zone(page),
1879 NR_SLAB_UNRECLAIMABLE, nr_freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 while (i--) {
Nick Pigginf205b2f2006-03-22 00:08:02 -08001881 BUG_ON(!PageSlab(page));
Mel Gorman072bb0a2012-07-31 16:43:58 -07001882 __ClearPageSlabPfmemalloc(page);
Nick Pigginf205b2f2006-03-22 00:08:02 -08001883 __ClearPageSlab(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 page++;
1885 }
Glauber Costa1f458cb2012-12-18 14:22:50 -08001886
1887 memcg_release_pages(cachep, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (current->reclaim_state)
1889 current->reclaim_state->reclaimed_slab += nr_freed;
Peter Zijlstra696ac522009-07-03 08:44:43 -05001890 if (!delayed) {
1891 free_memcg_kmem_pages((unsigned long)addr, cachep->gfporder);
1892 } else {
1893 basepage->index = cachep->gfporder;
1894 list_add(&basepage->lru, &__get_cpu_var(slab_free_list));
1895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896}
1897
1898static void kmem_rcu_free(struct rcu_head *head)
1899{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001900 struct slab_rcu *slab_rcu = (struct slab_rcu *)head;
Pekka Enberg343e0d72006-02-01 03:05:50 -08001901 struct kmem_cache *cachep = slab_rcu->cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Peter Zijlstra696ac522009-07-03 08:44:43 -05001903 kmem_freepages(cachep, slab_rcu->addr, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (OFF_SLAB(cachep))
1905 kmem_cache_free(cachep->slabp_cache, slab_rcu);
1906}
1907
1908#if DEBUG
1909
1910#ifdef CONFIG_DEBUG_PAGEALLOC
Pekka Enberg343e0d72006-02-01 03:05:50 -08001911static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001912 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001914 int size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001916 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001918 if (size < 5 * sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 return;
1920
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001921 *addr++ = 0x12345678;
1922 *addr++ = caller;
1923 *addr++ = smp_processor_id();
1924 size -= 3 * sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 {
1926 unsigned long *sptr = &caller;
1927 unsigned long svalue;
1928
1929 while (!kstack_end(sptr)) {
1930 svalue = *sptr++;
1931 if (kernel_text_address(svalue)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001932 *addr++ = svalue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 size -= sizeof(unsigned long);
1934 if (size <= sizeof(unsigned long))
1935 break;
1936 }
1937 }
1938
1939 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001940 *addr++ = 0x87654321;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942#endif
1943
Pekka Enberg343e0d72006-02-01 03:05:50 -08001944static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001946 int size = cachep->object_size;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001947 addr = &((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
1949 memset(addr, val, size);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001950 *(unsigned char *)(addr + size - 1) = POISON_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
1952
1953static void dump_line(char *data, int offset, int limit)
1954{
1955 int i;
Dave Jonesaa83aa42006-09-29 01:59:51 -07001956 unsigned char error = 0;
1957 int bad_count = 0;
1958
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02001959 printk(KERN_ERR "%03x: ", offset);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001960 for (i = 0; i < limit; i++) {
1961 if (data[offset + i] != POISON_FREE) {
1962 error = data[offset + i];
1963 bad_count++;
1964 }
Dave Jonesaa83aa42006-09-29 01:59:51 -07001965 }
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02001966 print_hex_dump(KERN_CONT, "", 0, 16, 1,
1967 &data[offset], limit, 1);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001968
1969 if (bad_count == 1) {
1970 error ^= POISON_FREE;
1971 if (!(error & (error - 1))) {
1972 printk(KERN_ERR "Single bit error detected. Probably "
1973 "bad RAM.\n");
1974#ifdef CONFIG_X86
1975 printk(KERN_ERR "Run memtest86+ or a similar memory "
1976 "test tool.\n");
1977#else
1978 printk(KERN_ERR "Run a memory test tool.\n");
1979#endif
1980 }
1981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982}
1983#endif
1984
1985#if DEBUG
1986
Pekka Enberg343e0d72006-02-01 03:05:50 -08001987static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 int i, size;
1990 char *realobj;
1991
1992 if (cachep->flags & SLAB_RED_ZONE) {
David Woodhouseb46b8f12007-05-08 00:22:59 -07001993 printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001994 *dbg_redzone1(cachep, objp),
1995 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 }
1997
1998 if (cachep->flags & SLAB_STORE_USER) {
Joe Perches071361d2012-12-12 10:19:12 -08001999 printk(KERN_ERR "Last user: [<%p>](%pSR)\n",
2000 *dbg_userword(cachep, objp),
2001 *dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 }
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002003 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05002004 size = cachep->object_size;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002005 for (i = 0; i < size && lines; i += 16, lines--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 int limit;
2007 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002008 if (i + limit > size)
2009 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 dump_line(realobj, i, limit);
2011 }
2012}
2013
Pekka Enberg343e0d72006-02-01 03:05:50 -08002014static void check_poison_obj(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015{
2016 char *realobj;
2017 int size, i;
2018 int lines = 0;
2019
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002020 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05002021 size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002023 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 char exp = POISON_FREE;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002025 if (i == size - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 exp = POISON_END;
2027 if (realobj[i] != exp) {
2028 int limit;
2029 /* Mismatch ! */
2030 /* Print header */
2031 if (lines == 0) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002032 printk(KERN_ERR
Dave Jonesface37f2011-11-15 15:03:52 -08002033 "Slab corruption (%s): %s start=%p, len=%d\n",
2034 print_tainted(), cachep->name, realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 print_objinfo(cachep, objp, 0);
2036 }
2037 /* Hexdump the affected line */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002038 i = (i / 16) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002040 if (i + limit > size)
2041 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 dump_line(realobj, i, limit);
2043 i += 16;
2044 lines++;
2045 /* Limit to 5 lines */
2046 if (lines > 5)
2047 break;
2048 }
2049 }
2050 if (lines != 0) {
2051 /* Print some data about the neighboring objects, if they
2052 * exist:
2053 */
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08002054 struct slab *slabp = virt_to_slab(objp);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002055 unsigned int objnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002057 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 if (objnr) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002059 objp = index_to_obj(cachep, slabp, objnr - 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002060 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002062 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 print_objinfo(cachep, objp, 2);
2064 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002065 if (objnr + 1 < cachep->num) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002066 objp = index_to_obj(cachep, slabp, objnr + 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002067 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002069 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 print_objinfo(cachep, objp, 2);
2071 }
2072 }
2073}
2074#endif
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076#if DEBUG
Rabin Vincente79aec22008-07-04 00:40:32 +05302077static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002078{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 int i;
2080 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002081 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
2083 if (cachep->flags & SLAB_POISON) {
2084#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002085 if (cachep->size % PAGE_SIZE == 0 &&
Andrew Mortona737b3e2006-03-22 00:08:11 -08002086 OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002087 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002088 cachep->size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 else
2090 check_poison_obj(cachep, objp);
2091#else
2092 check_poison_obj(cachep, objp);
2093#endif
2094 }
2095 if (cachep->flags & SLAB_RED_ZONE) {
2096 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2097 slab_error(cachep, "start of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002098 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2100 slab_error(cachep, "end of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002101 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002104}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105#else
Rabin Vincente79aec22008-07-04 00:40:32 +05302106static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002107{
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002108}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109#endif
2110
Randy Dunlap911851e2006-03-22 00:08:14 -08002111/**
2112 * slab_destroy - destroy and release all objects in a slab
2113 * @cachep: cache pointer being destroyed
2114 * @slabp: slab pointer being destroyed
2115 *
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002116 * Destroy all the objs in a slab, and release the mem back to the system.
Andrew Mortona737b3e2006-03-22 00:08:11 -08002117 * Before calling the slab must have been unlinked from the cache. The
2118 * cache-lock is not held/needed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002119 */
Peter Zijlstra696ac522009-07-03 08:44:43 -05002120static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp,
2121 bool delayed)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002122{
2123 void *addr = slabp->s_mem - slabp->colouroff;
2124
Rabin Vincente79aec22008-07-04 00:40:32 +05302125 slab_destroy_debugcheck(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
2127 struct slab_rcu *slab_rcu;
2128
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002129 slab_rcu = (struct slab_rcu *)slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 slab_rcu->cachep = cachep;
2131 slab_rcu->addr = addr;
2132 call_rcu(&slab_rcu->head, kmem_rcu_free);
2133 } else {
Peter Zijlstra696ac522009-07-03 08:44:43 -05002134 kmem_freepages(cachep, addr, delayed);
Ingo Molnar873623d2006-07-13 14:44:38 +02002135 if (OFF_SLAB(cachep))
2136 kmem_cache_free(cachep->slabp_cache, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 }
2138}
2139
2140/**
Randy.Dunlapa70773d2006-02-01 03:05:52 -08002141 * calculate_slab_order - calculate size (page order) of slabs
2142 * @cachep: pointer to the cache that is being created
2143 * @size: size of objects to be created in this cache.
2144 * @align: required alignment for the objects.
2145 * @flags: slab allocation flags
2146 *
2147 * Also calculates the number of objects per slab.
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002148 *
2149 * This could be made much more intelligent. For now, try to avoid using
2150 * high order pages for slabs. When the gfp() functions are more friendly
2151 * towards high-order requests, this should be changed.
2152 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002153static size_t calculate_slab_order(struct kmem_cache *cachep,
Randy Dunlapee13d782006-02-01 03:05:53 -08002154 size_t size, size_t align, unsigned long flags)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002155{
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002156 unsigned long offslab_limit;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002157 size_t left_over = 0;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002158 int gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002159
Christoph Lameter0aa817f2007-05-16 22:11:01 -07002160 for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002161 unsigned int num;
2162 size_t remainder;
2163
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002164 cache_estimate(gfporder, size, align, flags, &remainder, &num);
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002165 if (!num)
2166 continue;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002167
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002168 if (flags & CFLGS_OFF_SLAB) {
2169 /*
2170 * Max number of objs-per-slab for caches which
2171 * use off-slab slabs. Needed to avoid a possible
2172 * looping condition in cache_grow().
2173 */
2174 offslab_limit = size - sizeof(struct slab);
2175 offslab_limit /= sizeof(kmem_bufctl_t);
2176
2177 if (num > offslab_limit)
2178 break;
2179 }
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002180
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002181 /* Found something acceptable - save it away */
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002182 cachep->num = num;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002183 cachep->gfporder = gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002184 left_over = remainder;
2185
2186 /*
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08002187 * A VFS-reclaimable slab tends to have most allocations
2188 * as GFP_NOFS and we really don't want to have to be allocating
2189 * higher-order pages when we are unable to shrink dcache.
2190 */
2191 if (flags & SLAB_RECLAIM_ACCOUNT)
2192 break;
2193
2194 /*
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002195 * Large number of objects is good, but very large slabs are
2196 * currently bad for the gfp()s.
2197 */
David Rientjes543585c2011-10-18 22:09:24 -07002198 if (gfporder >= slab_max_order)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002199 break;
2200
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002201 /*
2202 * Acceptable internal fragmentation?
2203 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002204 if (left_over * 8 <= (PAGE_SIZE << gfporder))
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002205 break;
2206 }
2207 return left_over;
2208}
2209
Pekka Enberg83b519e2009-06-10 19:40:04 +03002210static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002211{
Christoph Lameter97d06602012-07-06 15:25:11 -05002212 if (slab_state >= FULL)
Pekka Enberg83b519e2009-06-10 19:40:04 +03002213 return enable_cpucache(cachep, gfp);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002214
Christoph Lameter97d06602012-07-06 15:25:11 -05002215 if (slab_state == DOWN) {
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002216 /*
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002217 * Note: Creation of first cache (kmem_cache).
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002218 * The setup_node is taken care
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002219 * of by the caller of __kmem_cache_create
2220 */
2221 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2222 slab_state = PARTIAL;
2223 } else if (slab_state == PARTIAL) {
2224 /*
2225 * Note: the second kmem_cache_create must create the cache
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002226 * that's used by kmalloc(24), otherwise the creation of
2227 * further caches will BUG().
2228 */
2229 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2230
2231 /*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002232 * If the cache that's used by kmalloc(sizeof(kmem_cache_node)) is
2233 * the second cache, then we need to set up all its node/,
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002234 * otherwise the creation of further caches will BUG().
2235 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002236 set_up_node(cachep, SIZE_AC);
2237 if (INDEX_AC == INDEX_NODE)
2238 slab_state = PARTIAL_NODE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002239 else
Christoph Lameter97d06602012-07-06 15:25:11 -05002240 slab_state = PARTIAL_ARRAYCACHE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002241 } else {
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002242 /* Remaining boot caches */
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002243 cachep->array[smp_processor_id()] =
Pekka Enberg83b519e2009-06-10 19:40:04 +03002244 kmalloc(sizeof(struct arraycache_init), gfp);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002245
Christoph Lameter97d06602012-07-06 15:25:11 -05002246 if (slab_state == PARTIAL_ARRAYCACHE) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002247 set_up_node(cachep, SIZE_NODE);
2248 slab_state = PARTIAL_NODE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002249 } else {
2250 int node;
Pekka Enberg556a1692008-01-25 08:20:51 +02002251 for_each_online_node(node) {
Christoph Lameter6a673682013-01-10 19:14:19 +00002252 cachep->node[node] =
Christoph Lameter6744f0872013-01-10 19:12:17 +00002253 kmalloc_node(sizeof(struct kmem_cache_node),
Pekka Enbergeb91f1d2009-06-12 14:56:09 +03002254 gfp, node);
Christoph Lameter6a673682013-01-10 19:14:19 +00002255 BUG_ON(!cachep->node[node]);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002256 kmem_cache_node_init(cachep->node[node]);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002257 }
2258 }
2259 }
Christoph Lameter6a673682013-01-10 19:14:19 +00002260 cachep->node[numa_mem_id()]->next_reap =
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002261 jiffies + REAPTIMEOUT_LIST3 +
2262 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
2263
2264 cpu_cache_get(cachep)->avail = 0;
2265 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
2266 cpu_cache_get(cachep)->batchcount = 1;
2267 cpu_cache_get(cachep)->touched = 0;
2268 cachep->batchcount = 1;
2269 cachep->limit = BOOT_CPUCACHE_ENTRIES;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002270 return 0;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002271}
2272
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002273/**
Christoph Lameter039363f2012-07-06 15:25:10 -05002274 * __kmem_cache_create - Create a cache.
Randy Dunlapa755b762012-11-06 17:10:10 -08002275 * @cachep: cache management descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 * @flags: SLAB flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 *
2278 * Returns a ptr to the cache on success, NULL on failure.
2279 * Cannot be called within a int, but can be interrupted.
Paul Mundt20c2df82007-07-20 10:11:58 +09002280 * The @ctor is run when new pages are allocated by the cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 * The flags are
2283 *
2284 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2285 * to catch references to uninitialised memory.
2286 *
2287 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2288 * for buffer overruns.
2289 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2291 * cacheline. This can be beneficial if you're counting cycles as closely
2292 * as davem.
2293 */
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002294int
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002295__kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296{
2297 size_t left_over, slab_size, ralign;
Pekka Enberg83b519e2009-06-10 19:40:04 +03002298 gfp_t gfp;
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002299 int err;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002300 size_t size = cachep->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303#if FORCED_DEBUG
2304 /*
2305 * Enable redzoning and last user accounting, except for caches with
2306 * large objects, if the increased size would increase the object size
2307 * above the next power of two: caches with object sizes just above a
2308 * power of two have a significant amount of internal fragmentation.
2309 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002310 if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
2311 2 * sizeof(unsigned long long)))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002312 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 if (!(flags & SLAB_DESTROY_BY_RCU))
2314 flags |= SLAB_POISON;
2315#endif
2316 if (flags & SLAB_DESTROY_BY_RCU)
2317 BUG_ON(flags & SLAB_POISON);
2318#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
Andrew Mortona737b3e2006-03-22 00:08:11 -08002320 /*
2321 * Check that size is in terms of words. This is needed to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 * unaligned accesses for some archs when redzoning is used, and makes
2323 * sure any on-slab bufctl's are also correctly aligned.
2324 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002325 if (size & (BYTES_PER_WORD - 1)) {
2326 size += (BYTES_PER_WORD - 1);
2327 size &= ~(BYTES_PER_WORD - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 }
2329
Pekka Enbergca5f9702006-09-25 23:31:25 -07002330 /*
David Woodhouse87a927c2007-07-04 21:26:44 -04002331 * Redzoning and user store require word alignment or possibly larger.
2332 * Note this will be overridden by architecture or caller mandated
2333 * alignment if either is greater than BYTES_PER_WORD.
Pekka Enbergca5f9702006-09-25 23:31:25 -07002334 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002335 if (flags & SLAB_STORE_USER)
2336 ralign = BYTES_PER_WORD;
2337
2338 if (flags & SLAB_RED_ZONE) {
2339 ralign = REDZONE_ALIGN;
2340 /* If redzoning, ensure that the second redzone is suitably
2341 * aligned, by adjusting the object size accordingly. */
2342 size += REDZONE_ALIGN - 1;
2343 size &= ~(REDZONE_ALIGN - 1);
2344 }
Pekka Enbergca5f9702006-09-25 23:31:25 -07002345
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002346 /* 3) caller mandated alignment */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002347 if (ralign < cachep->align) {
2348 ralign = cachep->align;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 }
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002350 /* disable debug if necessary */
2351 if (ralign > __alignof__(unsigned long long))
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002352 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002353 /*
Pekka Enbergca5f9702006-09-25 23:31:25 -07002354 * 4) Store it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002356 cachep->align = ralign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
Pekka Enberg83b519e2009-06-10 19:40:04 +03002358 if (slab_is_available())
2359 gfp = GFP_KERNEL;
2360 else
2361 gfp = GFP_NOWAIT;
2362
Christoph Lameter6a673682013-01-10 19:14:19 +00002363 setup_node_pointer(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Pekka Enbergca5f9702006-09-25 23:31:25 -07002366 /*
2367 * Both debugging options require word-alignment which is calculated
2368 * into align above.
2369 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (flags & SLAB_RED_ZONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 /* add space for red zone words */
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002372 cachep->obj_offset += sizeof(unsigned long long);
2373 size += 2 * sizeof(unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
2375 if (flags & SLAB_STORE_USER) {
Pekka Enbergca5f9702006-09-25 23:31:25 -07002376 /* user store requires one word storage behind the end of
David Woodhouse87a927c2007-07-04 21:26:44 -04002377 * the real object. But if the second red zone needs to be
2378 * aligned to 64 bits, we must allow that much space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002380 if (flags & SLAB_RED_ZONE)
2381 size += REDZONE_ALIGN;
2382 else
2383 size += BYTES_PER_WORD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 }
2385#if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002386 if (size >= kmalloc_size(INDEX_NODE + 1)
Tetsuo Handa608da7e2012-09-30 17:28:25 +09002387 && cachep->object_size > cache_line_size()
2388 && ALIGN(size, cachep->align) < PAGE_SIZE) {
2389 cachep->obj_offset += PAGE_SIZE - ALIGN(size, cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 size = PAGE_SIZE;
2391 }
2392#endif
2393#endif
2394
Ingo Molnare0a42722006-06-23 02:03:46 -07002395 /*
2396 * Determine if the slab management is 'on' or 'off' slab.
2397 * (bootstrapping cannot cope with offslab caches so don't do
Catalin Marinase7cb55b2009-10-28 13:33:08 +00002398 * it too early on. Always use on-slab management when
2399 * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
Ingo Molnare0a42722006-06-23 02:03:46 -07002400 */
Catalin Marinase7cb55b2009-10-28 13:33:08 +00002401 if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init &&
2402 !(flags & SLAB_NOLEAKTRACE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 /*
2404 * Size is large, assume best to place the slab management obj
2405 * off-slab (should allow better packing of objs).
2406 */
2407 flags |= CFLGS_OFF_SLAB;
2408
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002409 size = ALIGN(size, cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002411 left_over = calculate_slab_order(cachep, size, cachep->align, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002413 if (!cachep->num)
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002414 return -E2BIG;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002415
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002416 slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002417 + sizeof(struct slab), cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
2419 /*
2420 * If the slab has been placed off-slab, and we have enough space then
2421 * move it on-slab. This is at the expense of any extra colouring.
2422 */
2423 if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
2424 flags &= ~CFLGS_OFF_SLAB;
2425 left_over -= slab_size;
2426 }
2427
2428 if (flags & CFLGS_OFF_SLAB) {
2429 /* really off slab. No need for manual alignment */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002430 slab_size =
2431 cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
Ron Lee67461362009-05-22 04:58:22 +09302432
2433#ifdef CONFIG_PAGE_POISONING
2434 /* If we're going to use the generic kernel_map_pages()
2435 * poisoning, then it's going to smash the contents of
2436 * the redzone and userword anyhow, so switch them off.
2437 */
2438 if (size % PAGE_SIZE == 0 && flags & SLAB_POISON)
2439 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
2440#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 }
2442
2443 cachep->colour_off = cache_line_size();
2444 /* Offset must be a multiple of the alignment. */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002445 if (cachep->colour_off < cachep->align)
2446 cachep->colour_off = cachep->align;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002447 cachep->colour = left_over / cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448 cachep->slab_size = slab_size;
2449 cachep->flags = flags;
Glauber Costaa618e892012-06-14 16:17:21 +04002450 cachep->allocflags = 0;
Christoph Lameter4b51d662007-02-10 01:43:10 -08002451 if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
Glauber Costaa618e892012-06-14 16:17:21 +04002452 cachep->allocflags |= GFP_DMA;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002453 cachep->size = size;
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08002454 cachep->reciprocal_buffer_size = reciprocal_value(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002456 if (flags & CFLGS_OFF_SLAB) {
Christoph Lameter2c59dd62013-01-10 19:14:19 +00002457 cachep->slabp_cache = kmalloc_slab(slab_size, 0u);
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002458 /*
2459 * This is a possibility for one of the malloc_sizes caches.
2460 * But since we go off slab only for object size greater than
2461 * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
2462 * this should not happen at all.
2463 * But leave a BUG_ON for some lucky dude.
2464 */
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002465 BUG_ON(ZERO_OR_NULL_PTR(cachep->slabp_cache));
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002468 err = setup_cpu_cache(cachep, gfp);
2469 if (err) {
Christoph Lameter12c36672012-09-04 23:38:33 +00002470 __kmem_cache_shutdown(cachep);
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002471 return err;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Peter Zijlstra83835b32011-07-22 15:26:05 +02002474 if (flags & SLAB_DEBUG_OBJECTS) {
2475 /*
2476 * Would deadlock through slab_destroy()->call_rcu()->
2477 * debug_object_activate()->kmem_cache_alloc().
2478 */
2479 WARN_ON_ONCE(flags & SLAB_DESTROY_BY_RCU);
2480
2481 slab_set_debugobj_lock_classes(cachep);
Glauber Costa6ccfb5b2012-12-18 14:22:31 -08002482 } else if (!OFF_SLAB(cachep) && !(flags & SLAB_DESTROY_BY_RCU))
2483 on_slab_lock_classes(cachep);
Peter Zijlstra83835b32011-07-22 15:26:05 +02002484
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488#if DEBUG
2489static void check_irq_off(void)
2490{
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002491 BUG_ON_NONRT(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492}
2493
2494static void check_irq_on(void)
2495{
2496 BUG_ON(irqs_disabled());
2497}
2498
Pekka Enberg343e0d72006-02-01 03:05:50 -08002499static void check_spinlock_acquired(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500{
2501#ifdef CONFIG_SMP
2502 check_irq_off();
Christoph Lameter6a673682013-01-10 19:14:19 +00002503 assert_spin_locked(&cachep->node[numa_mem_id()]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504#endif
2505}
Christoph Lametere498be72005-09-09 13:03:32 -07002506
Pekka Enberg343e0d72006-02-01 03:05:50 -08002507static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07002508{
2509#ifdef CONFIG_SMP
2510 check_irq_off();
Christoph Lameter6a673682013-01-10 19:14:19 +00002511 assert_spin_locked(&cachep->node[node]->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002512#endif
2513}
2514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515#else
2516#define check_irq_off() do { } while(0)
2517#define check_irq_on() do { } while(0)
2518#define check_spinlock_acquired(x) do { } while(0)
Christoph Lametere498be72005-09-09 13:03:32 -07002519#define check_spinlock_acquired_node(x, y) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520#endif
2521
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002522static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
Christoph Lameteraab22072006-03-22 00:09:06 -08002523 struct array_cache *ac,
2524 int force, int node);
2525
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002526static void __do_drain(void *arg, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527{
Andrew Mortona737b3e2006-03-22 00:08:11 -08002528 struct kmem_cache *cachep = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 struct array_cache *ac;
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002530 int node = cpu_to_mem(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002532 ac = cpu_cache_get_on_cpu(cachep, cpu);
Christoph Lameter6a673682013-01-10 19:14:19 +00002533 spin_lock(&cachep->node[node]->list_lock);
Christoph Lameterff694162005-09-22 21:44:02 -07002534 free_block(cachep, ac->entry, ac->avail, node);
Christoph Lameter6a673682013-01-10 19:14:19 +00002535 spin_unlock(&cachep->node[node]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 ac->avail = 0;
2537}
2538
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002539#ifndef CONFIG_PREEMPT_RT_BASE
2540static void do_drain(void *arg)
2541{
2542 __do_drain(arg, smp_processor_id());
2543}
2544#else
Peter Zijlstra696ac522009-07-03 08:44:43 -05002545static void do_drain(void *arg, int cpu)
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002546{
Peter Zijlstra696ac522009-07-03 08:44:43 -05002547 LIST_HEAD(tmp);
2548
2549 lock_slab_on(cpu);
2550 __do_drain(arg, cpu);
2551 list_splice_init(&per_cpu(slab_free_list, cpu), &tmp);
2552 unlock_slab_on(cpu);
2553 free_delayed(&tmp);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002554}
2555#endif
2556
Pekka Enberg343e0d72006-02-01 03:05:50 -08002557static void drain_cpu_caches(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002559 struct kmem_cache_node *n;
Christoph Lametere498be72005-09-09 13:03:32 -07002560 int node;
2561
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002562 slab_on_each_cpu(do_drain, cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 check_irq_on();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002564 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002565 n = cachep->node[node];
2566 if (n && n->alien)
2567 drain_alien_cache(cachep, n->alien);
Roland Dreiera4523a82006-05-15 11:41:00 -07002568 }
2569
2570 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002571 n = cachep->node[node];
2572 if (n)
2573 drain_array(cachep, n, n->shared, 1, node);
Christoph Lametere498be72005-09-09 13:03:32 -07002574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
2576
Christoph Lametered11d9e2006-06-30 01:55:45 -07002577/*
2578 * Remove slabs from the list of free slabs.
2579 * Specify the number of slabs to drain in tofree.
2580 *
2581 * Returns the actual number of slabs released.
2582 */
2583static int drain_freelist(struct kmem_cache *cache,
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002584 struct kmem_cache_node *n, int tofree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585{
Christoph Lametered11d9e2006-06-30 01:55:45 -07002586 struct list_head *p;
2587 int nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Christoph Lametered11d9e2006-06-30 01:55:45 -07002590 nr_freed = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002591 while (nr_freed < tofree && !list_empty(&n->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002593 local_spin_lock_irq(slab_lock, &n->list_lock);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002594 p = n->slabs_free.prev;
2595 if (p == &n->slabs_free) {
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002596 local_spin_unlock_irq(slab_lock, &n->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002597 goto out;
2598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
Christoph Lametered11d9e2006-06-30 01:55:45 -07002600 slabp = list_entry(p, struct slab, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601#if DEBUG
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002602 BUG_ON(slabp->inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603#endif
2604 list_del(&slabp->list);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002605 /*
2606 * Safe to drop the lock. The slab is no longer linked
2607 * to the cache.
2608 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002609 n->free_objects -= cache->num;
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002610 local_spin_unlock_irq(slab_lock, &n->list_lock);
Peter Zijlstra696ac522009-07-03 08:44:43 -05002611 slab_destroy(cache, slabp, false);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002612 nr_freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 }
Christoph Lametered11d9e2006-06-30 01:55:45 -07002614out:
2615 return nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616}
2617
Christoph Lameter18004c52012-07-06 15:25:12 -05002618/* Called with slab_mutex held to protect against cpu hotplug */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002619static int __cache_shrink(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07002620{
2621 int ret = 0, i = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002622 struct kmem_cache_node *n;
Christoph Lametere498be72005-09-09 13:03:32 -07002623
2624 drain_cpu_caches(cachep);
2625
2626 check_irq_on();
2627 for_each_online_node(i) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002628 n = cachep->node[i];
2629 if (!n)
Christoph Lametered11d9e2006-06-30 01:55:45 -07002630 continue;
2631
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002632 drain_freelist(cachep, n, n->free_objects);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002633
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002634 ret += !list_empty(&n->slabs_full) ||
2635 !list_empty(&n->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07002636 }
2637 return (ret ? 1 : 0);
2638}
2639
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640/**
2641 * kmem_cache_shrink - Shrink a cache.
2642 * @cachep: The cache to shrink.
2643 *
2644 * Releases as many slabs as possible for a cache.
2645 * To help debugging, a zero exit status indicates all slabs were released.
2646 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002647int kmem_cache_shrink(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648{
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002649 int ret;
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002650 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002652 get_online_cpus();
Christoph Lameter18004c52012-07-06 15:25:12 -05002653 mutex_lock(&slab_mutex);
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002654 ret = __cache_shrink(cachep);
Christoph Lameter18004c52012-07-06 15:25:12 -05002655 mutex_unlock(&slab_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002656 put_online_cpus();
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002657 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658}
2659EXPORT_SYMBOL(kmem_cache_shrink);
2660
Christoph Lameter945cf2b2012-09-04 23:18:33 +00002661int __kmem_cache_shutdown(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662{
Christoph Lameter12c36672012-09-04 23:38:33 +00002663 int i;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002664 struct kmem_cache_node *n;
Christoph Lameter12c36672012-09-04 23:38:33 +00002665 int rc = __cache_shrink(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Christoph Lameter12c36672012-09-04 23:38:33 +00002667 if (rc)
2668 return rc;
2669
2670 for_each_online_cpu(i)
2671 kfree(cachep->array[i]);
2672
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002673 /* NUMA: free the node structures */
Christoph Lameter12c36672012-09-04 23:38:33 +00002674 for_each_online_node(i) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002675 n = cachep->node[i];
2676 if (n) {
2677 kfree(n->shared);
2678 free_alien_cache(n->alien);
2679 kfree(n);
Christoph Lameter12c36672012-09-04 23:38:33 +00002680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 }
Christoph Lameter12c36672012-09-04 23:38:33 +00002682 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002685/*
2686 * Get the memory for a slab management obj.
2687 * For a slab cache when the slab descriptor is off-slab, slab descriptors
2688 * always come from malloc_sizes caches. The slab descriptor cannot
2689 * come from the same cache which is getting created because,
2690 * when we are searching for an appropriate cache for these
2691 * descriptors in kmem_cache_create, we search through the malloc_sizes array.
2692 * If we are creating a malloc_sizes cache here it would not be visible to
2693 * kmem_find_general_cachep till the initialization is complete.
2694 * Hence we cannot have slabp_cache same as the original cache.
2695 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002696static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002697 int colour_off, gfp_t local_flags,
2698 int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
2700 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002701
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 if (OFF_SLAB(cachep)) {
2703 /* Slab management obj is off-slab. */
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002704 slabp = kmem_cache_alloc_node(cachep->slabp_cache,
Pekka Enberg8759ec52008-11-26 10:01:31 +02002705 local_flags, nodeid);
Catalin Marinasd5cff632009-06-11 13:22:40 +01002706 /*
2707 * If the first object in the slab is leaked (it's allocated
2708 * but no one has a reference to it), we want to make sure
2709 * kmemleak does not treat the ->s_mem pointer as a reference
2710 * to the object. Otherwise we will not report the leak.
2711 */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002712 kmemleak_scan_area(&slabp->list, sizeof(struct list_head),
2713 local_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 if (!slabp)
2715 return NULL;
2716 } else {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002717 slabp = objp + colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 colour_off += cachep->slab_size;
2719 }
2720 slabp->inuse = 0;
2721 slabp->colouroff = colour_off;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002722 slabp->s_mem = objp + colour_off;
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002723 slabp->nodeid = nodeid;
Marcin Slusarze51bfd02008-02-10 11:21:54 +01002724 slabp->free = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 return slabp;
2726}
2727
2728static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
2729{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002730 return (kmem_bufctl_t *) (slabp + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731}
2732
Pekka Enberg343e0d72006-02-01 03:05:50 -08002733static void cache_init_objs(struct kmem_cache *cachep,
Christoph Lametera35afb82007-05-16 22:10:57 -07002734 struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
2736 int i;
2737
2738 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002739 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740#if DEBUG
2741 /* need to poison the objs? */
2742 if (cachep->flags & SLAB_POISON)
2743 poison_obj(cachep, objp, POISON_FREE);
2744 if (cachep->flags & SLAB_STORE_USER)
2745 *dbg_userword(cachep, objp) = NULL;
2746
2747 if (cachep->flags & SLAB_RED_ZONE) {
2748 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2749 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2750 }
2751 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002752 * Constructors are not allowed to allocate memory from the same
2753 * cache which they are a constructor for. Otherwise, deadlock.
2754 * They must also be threaded.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 */
2756 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002757 cachep->ctor(objp + obj_offset(cachep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
2759 if (cachep->flags & SLAB_RED_ZONE) {
2760 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2761 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002762 " end of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2764 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002765 " start of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766 }
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002767 if ((cachep->size % PAGE_SIZE) == 0 &&
Andrew Mortona737b3e2006-03-22 00:08:11 -08002768 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002769 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002770 cachep->size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771#else
2772 if (cachep->ctor)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002773 cachep->ctor(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774#endif
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002775 slab_bufctl(slabp)[i] = i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002777 slab_bufctl(slabp)[i - 1] = BUFCTL_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778}
2779
Pekka Enberg343e0d72006-02-01 03:05:50 -08002780static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781{
Christoph Lameter4b51d662007-02-10 01:43:10 -08002782 if (CONFIG_ZONE_DMA_FLAG) {
2783 if (flags & GFP_DMA)
Glauber Costaa618e892012-06-14 16:17:21 +04002784 BUG_ON(!(cachep->allocflags & GFP_DMA));
Christoph Lameter4b51d662007-02-10 01:43:10 -08002785 else
Glauber Costaa618e892012-06-14 16:17:21 +04002786 BUG_ON(cachep->allocflags & GFP_DMA);
Christoph Lameter4b51d662007-02-10 01:43:10 -08002787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788}
2789
Andrew Mortona737b3e2006-03-22 00:08:11 -08002790static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
2791 int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002792{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002793 void *objp = index_to_obj(cachep, slabp, slabp->free);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002794 kmem_bufctl_t next;
2795
2796 slabp->inuse++;
2797 next = slab_bufctl(slabp)[slabp->free];
2798#if DEBUG
2799 slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
2800 WARN_ON(slabp->nodeid != nodeid);
2801#endif
2802 slabp->free = next;
2803
2804 return objp;
2805}
2806
Andrew Mortona737b3e2006-03-22 00:08:11 -08002807static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
2808 void *objp, int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002809{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002810 unsigned int objnr = obj_to_index(cachep, slabp, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002811
2812#if DEBUG
2813 /* Verify that the slab belongs to the intended node */
2814 WARN_ON(slabp->nodeid != nodeid);
2815
Al Viro871751e2006-03-25 03:06:39 -08002816 if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) {
Matthew Dobson78d382d2006-02-01 03:05:47 -08002817 printk(KERN_ERR "slab: double free detected in cache "
Andrew Mortona737b3e2006-03-22 00:08:11 -08002818 "'%s', objp %p\n", cachep->name, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002819 BUG();
2820 }
2821#endif
2822 slab_bufctl(slabp)[objnr] = slabp->free;
2823 slabp->free = objnr;
2824 slabp->inuse--;
2825}
2826
Pekka Enberg47768742006-06-23 02:03:07 -07002827/*
2828 * Map pages beginning at addr to the given cache and slab. This is required
2829 * for the slab allocator to be able to lookup the cache and slab of a
Nick Pigginccd35fb2011-01-07 17:49:17 +11002830 * virtual address for kfree, ksize, and slab debugging.
Pekka Enberg47768742006-06-23 02:03:07 -07002831 */
2832static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
2833 void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834{
Pekka Enberg47768742006-06-23 02:03:07 -07002835 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 struct page *page;
2837
Pekka Enberg47768742006-06-23 02:03:07 -07002838 page = virt_to_page(addr);
Nick Piggin84097512006-03-22 00:08:34 -08002839
Pekka Enberg47768742006-06-23 02:03:07 -07002840 nr_pages = 1;
Nick Piggin84097512006-03-22 00:08:34 -08002841 if (likely(!PageCompound(page)))
Pekka Enberg47768742006-06-23 02:03:07 -07002842 nr_pages <<= cache->gfporder;
2843
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 do {
Christoph Lameter35026082012-06-13 10:24:56 -05002845 page->slab_cache = cache;
2846 page->slab_page = slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 page++;
Pekka Enberg47768742006-06-23 02:03:07 -07002848 } while (--nr_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849}
2850
2851/*
2852 * Grow (by 1) the number of slabs within a cache. This is called by
2853 * kmem_cache_alloc() when there are no active objs left in a cache.
2854 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002855static int cache_grow(struct kmem_cache *cachep,
2856 gfp_t flags, int nodeid, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002858 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002859 size_t offset;
2860 gfp_t local_flags;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002861 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Andrew Mortona737b3e2006-03-22 00:08:11 -08002863 /*
2864 * Be lazy and only check for valid flags here, keeping it out of the
2865 * critical path in kmem_cache_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 */
Christoph Lameter6cb06222007-10-16 01:25:41 -07002867 BUG_ON(flags & GFP_SLAB_BUG_MASK);
2868 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002870 /* Take the node list lock to change the colour_next on this node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002872 n = cachep->node[nodeid];
2873 spin_lock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
2875 /* Get colour for the slab, and cal the next value. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002876 offset = n->colour_next;
2877 n->colour_next++;
2878 if (n->colour_next >= cachep->colour)
2879 n->colour_next = 0;
2880 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002882 offset *= cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
2884 if (local_flags & __GFP_WAIT)
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002885 local_unlock_irq(slab_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
2887 /*
2888 * The test for missing atomic flag is performed here, rather than
2889 * the more obvious place, simply to reduce the critical path length
2890 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2891 * will eventually be caught here (where it matters).
2892 */
2893 kmem_flagcheck(cachep, flags);
2894
Andrew Mortona737b3e2006-03-22 00:08:11 -08002895 /*
2896 * Get mem for the objs. Attempt to allocate a physical page from
2897 * 'nodeid'.
Christoph Lametere498be72005-09-09 13:03:32 -07002898 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002899 if (!objp)
Andrew Mortonb8c1c5d2007-07-24 12:02:40 -07002900 objp = kmem_getpages(cachep, local_flags, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002901 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 goto failed;
2903
2904 /* Get slab management. */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002905 slabp = alloc_slabmgmt(cachep, objp, offset,
Christoph Lameter6cb06222007-10-16 01:25:41 -07002906 local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002907 if (!slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 goto opps1;
2909
Pekka Enberg47768742006-06-23 02:03:07 -07002910 slab_map_pages(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911
Christoph Lametera35afb82007-05-16 22:10:57 -07002912 cache_init_objs(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
2914 if (local_flags & __GFP_WAIT)
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002915 local_lock_irq(slab_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002917 spin_lock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918
2919 /* Make slab active. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002920 list_add_tail(&slabp->list, &(n->slabs_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 STATS_INC_GROWN(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002922 n->free_objects += cachep->num;
2923 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002925opps1:
Peter Zijlstra696ac522009-07-03 08:44:43 -05002926 kmem_freepages(cachep, objp, false);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002927failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 if (local_flags & __GFP_WAIT)
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002929 local_lock_irq(slab_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 return 0;
2931}
2932
2933#if DEBUG
2934
2935/*
2936 * Perform extra freeing checks:
2937 * - detect bad pointers.
2938 * - POISON/RED_ZONE checking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 */
2940static void kfree_debugcheck(const void *objp)
2941{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 if (!virt_addr_valid(objp)) {
2943 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002944 (unsigned long)objp);
2945 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947}
2948
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002949static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2950{
David Woodhouseb46b8f12007-05-08 00:22:59 -07002951 unsigned long long redzone1, redzone2;
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002952
2953 redzone1 = *dbg_redzone1(cache, obj);
2954 redzone2 = *dbg_redzone2(cache, obj);
2955
2956 /*
2957 * Redzone is ok.
2958 */
2959 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2960 return;
2961
2962 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2963 slab_error(cache, "double free detected");
2964 else
2965 slab_error(cache, "memory outside object was overwritten");
2966
David Woodhouseb46b8f12007-05-08 00:22:59 -07002967 printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002968 obj, redzone1, redzone2);
2969}
2970
Pekka Enberg343e0d72006-02-01 03:05:50 -08002971static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002972 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973{
2974 struct page *page;
2975 unsigned int objnr;
2976 struct slab *slabp;
2977
Matthew Wilcox80cbd912007-11-29 12:05:13 -07002978 BUG_ON(virt_to_cache(objp) != cachep);
2979
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002980 objp -= obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 kfree_debugcheck(objp);
Christoph Lameterb49af682007-05-06 14:49:41 -07002982 page = virt_to_head_page(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983
Christoph Lameter35026082012-06-13 10:24:56 -05002984 slabp = page->slab_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985
2986 if (cachep->flags & SLAB_RED_ZONE) {
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002987 verify_redzone_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2989 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2990 }
2991 if (cachep->flags & SLAB_STORE_USER)
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002992 *dbg_userword(cachep, objp) = (void *)caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002994 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
2996 BUG_ON(objnr >= cachep->num);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002997 BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998
Al Viro871751e2006-03-25 03:06:39 -08002999#ifdef CONFIG_DEBUG_SLAB_LEAK
3000 slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
3001#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 if (cachep->flags & SLAB_POISON) {
3003#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003004 if ((cachep->size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003005 store_stackinfo(cachep, objp, caller);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003006 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003007 cachep->size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 } else {
3009 poison_obj(cachep, objp, POISON_FREE);
3010 }
3011#else
3012 poison_obj(cachep, objp, POISON_FREE);
3013#endif
3014 }
3015 return objp;
3016}
3017
Pekka Enberg343e0d72006-02-01 03:05:50 -08003018static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
3020 kmem_bufctl_t i;
3021 int entries = 0;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003022
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 /* Check slab's freelist to see if this obj is there. */
3024 for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
3025 entries++;
3026 if (entries > cachep->num || i >= cachep->num)
3027 goto bad;
3028 }
3029 if (entries != cachep->num - slabp->inuse) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003030bad:
3031 printk(KERN_ERR "slab: Internal list corruption detected in "
Dave Jonesface37f2011-11-15 15:03:52 -08003032 "cache '%s'(%d), slabp %p(%d). Tainted(%s). Hexdump:\n",
3033 cachep->name, cachep->num, slabp, slabp->inuse,
3034 print_tainted());
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02003035 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, slabp,
3036 sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t),
3037 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 BUG();
3039 }
3040}
3041#else
3042#define kfree_debugcheck(x) do { } while(0)
3043#define cache_free_debugcheck(x,objp,z) (objp)
3044#define check_slabp(x,y) do { } while(0)
3045#endif
3046
Mel Gorman072bb0a2012-07-31 16:43:58 -07003047static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags,
3048 bool force_refill)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
3050 int batchcount;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003051 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 struct array_cache *ac;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07003053 int node;
3054
Joe Korty6d2144d2008-03-05 15:04:59 -08003055 check_irq_off();
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003056 node = numa_mem_id();
Mel Gorman072bb0a2012-07-31 16:43:58 -07003057 if (unlikely(force_refill))
3058 goto force_grow;
3059retry:
Joe Korty6d2144d2008-03-05 15:04:59 -08003060 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 batchcount = ac->batchcount;
3062 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003063 /*
3064 * If there was little recent activity on this cache, then
3065 * perform only a partial refill. Otherwise we could generate
3066 * refill bouncing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 */
3068 batchcount = BATCHREFILL_LIMIT;
3069 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003070 n = cachep->node[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003072 BUG_ON(ac->avail > 0 || !n);
3073 spin_lock(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07003074
Christoph Lameter3ded1752006-03-25 03:06:44 -08003075 /* See if we can refill from the shared array */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003076 if (n->shared && transfer_objects(ac, n->shared, batchcount)) {
3077 n->shared->touched = 1;
Christoph Lameter3ded1752006-03-25 03:06:44 -08003078 goto alloc_done;
Nick Piggin44b57f12010-01-27 22:27:40 +11003079 }
Christoph Lameter3ded1752006-03-25 03:06:44 -08003080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 while (batchcount > 0) {
3082 struct list_head *entry;
3083 struct slab *slabp;
3084 /* Get slab alloc is to come from. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003085 entry = n->slabs_partial.next;
3086 if (entry == &n->slabs_partial) {
3087 n->free_touched = 1;
3088 entry = n->slabs_free.next;
3089 if (entry == &n->slabs_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 goto must_grow;
3091 }
3092
3093 slabp = list_entry(entry, struct slab, list);
3094 check_slabp(cachep, slabp);
3095 check_spinlock_acquired(cachep);
Pekka Enberg714b81712007-05-06 14:49:03 -07003096
3097 /*
3098 * The slab was either on partial or free list so
3099 * there must be at least one object available for
3100 * allocation.
3101 */
roel kluin249b9f32008-10-29 17:18:07 -04003102 BUG_ON(slabp->inuse >= cachep->num);
Pekka Enberg714b81712007-05-06 14:49:03 -07003103
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104 while (slabp->inuse < cachep->num && batchcount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 STATS_INC_ALLOCED(cachep);
3106 STATS_INC_ACTIVE(cachep);
3107 STATS_SET_HIGH(cachep);
3108
Mel Gorman072bb0a2012-07-31 16:43:58 -07003109 ac_put_obj(cachep, ac, slab_get_obj(cachep, slabp,
3110 node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 }
3112 check_slabp(cachep, slabp);
3113
3114 /* move slabp to correct slabp list: */
3115 list_del(&slabp->list);
3116 if (slabp->free == BUFCTL_END)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003117 list_add(&slabp->list, &n->slabs_full);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 else
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003119 list_add(&slabp->list, &n->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 }
3121
Andrew Mortona737b3e2006-03-22 00:08:11 -08003122must_grow:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003123 n->free_objects -= ac->avail;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003124alloc_done:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003125 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
3127 if (unlikely(!ac->avail)) {
3128 int x;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003129force_grow:
Christoph Lameter3c517a62006-12-06 20:33:29 -08003130 x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07003131
Andrew Mortona737b3e2006-03-22 00:08:11 -08003132 /* cache_grow can reenable interrupts, then ac could change. */
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003133 ac = cpu_cache_get(cachep);
David Rientjes51cd8e62012-08-28 19:57:21 -07003134 node = numa_mem_id();
Mel Gorman072bb0a2012-07-31 16:43:58 -07003135
3136 /* no objects in sight? abort */
3137 if (!x && (ac->avail == 0 || force_refill))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 return NULL;
3139
Andrew Mortona737b3e2006-03-22 00:08:11 -08003140 if (!ac->avail) /* objects refilled by interrupt? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 goto retry;
3142 }
3143 ac->touched = 1;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003144
3145 return ac_get_obj(cachep, ac, flags, force_refill);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146}
3147
Andrew Mortona737b3e2006-03-22 00:08:11 -08003148static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
3149 gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150{
3151 might_sleep_if(flags & __GFP_WAIT);
3152#if DEBUG
3153 kmem_flagcheck(cachep, flags);
3154#endif
3155}
3156
3157#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003158static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003159 gfp_t flags, void *objp, unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003161 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 return objp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003163 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003165 if ((cachep->size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003166 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003167 cachep->size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 else
3169 check_poison_obj(cachep, objp);
3170#else
3171 check_poison_obj(cachep, objp);
3172#endif
3173 poison_obj(cachep, objp, POISON_INUSE);
3174 }
3175 if (cachep->flags & SLAB_STORE_USER)
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003176 *dbg_userword(cachep, objp) = (void *)caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
3178 if (cachep->flags & SLAB_RED_ZONE) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003179 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
3180 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
3181 slab_error(cachep, "double free, or memory outside"
3182 " object was overwritten");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003183 printk(KERN_ERR
David Woodhouseb46b8f12007-05-08 00:22:59 -07003184 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08003185 objp, *dbg_redzone1(cachep, objp),
3186 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 }
3188 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
3189 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
3190 }
Al Viro871751e2006-03-25 03:06:39 -08003191#ifdef CONFIG_DEBUG_SLAB_LEAK
3192 {
3193 struct slab *slabp;
3194 unsigned objnr;
3195
Christoph Lameter35026082012-06-13 10:24:56 -05003196 slabp = virt_to_head_page(objp)->slab_page;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003197 objnr = (unsigned)(objp - slabp->s_mem) / cachep->size;
Al Viro871751e2006-03-25 03:06:39 -08003198 slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE;
3199 }
3200#endif
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003201 objp += obj_offset(cachep);
Christoph Lameter4f104932007-05-06 14:50:17 -07003202 if (cachep->ctor && cachep->flags & SLAB_POISON)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003203 cachep->ctor(objp);
Tetsuo Handa7ea466f2011-07-21 09:42:45 +09003204 if (ARCH_SLAB_MINALIGN &&
3205 ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003206 printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
Hugh Dickinsc2251502011-07-11 13:35:08 -07003207 objp, (int)ARCH_SLAB_MINALIGN);
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 return objp;
3210}
3211#else
3212#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
3213#endif
3214
Akinobu Mita773ff602008-12-23 19:37:01 +09003215static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags)
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003216{
Christoph Lameter9b030cb2012-09-05 00:20:33 +00003217 if (cachep == kmem_cache)
Akinobu Mita773ff602008-12-23 19:37:01 +09003218 return false;
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003219
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003220 return should_failslab(cachep->object_size, flags, cachep->flags);
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003221}
3222
Pekka Enberg343e0d72006-02-01 03:05:50 -08003223static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003225 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 struct array_cache *ac;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003227 bool force_refill = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
Alok N Kataria5c382302005-09-27 21:45:46 -07003229 check_irq_off();
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003230
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003231 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 if (likely(ac->avail)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 ac->touched = 1;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003234 objp = ac_get_obj(cachep, ac, flags, false);
3235
J. R. Okajimaddbf2e82009-12-02 16:55:50 +09003236 /*
Mel Gorman072bb0a2012-07-31 16:43:58 -07003237 * Allow for the possibility all avail objects are not allowed
3238 * by the current flags
J. R. Okajimaddbf2e82009-12-02 16:55:50 +09003239 */
Mel Gorman072bb0a2012-07-31 16:43:58 -07003240 if (objp) {
3241 STATS_INC_ALLOCHIT(cachep);
3242 goto out;
3243 }
3244 force_refill = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 }
Mel Gorman072bb0a2012-07-31 16:43:58 -07003246
3247 STATS_INC_ALLOCMISS(cachep);
3248 objp = cache_alloc_refill(cachep, flags, force_refill);
3249 /*
3250 * the 'ac' may be updated by cache_alloc_refill(),
3251 * and kmemleak_erase() requires its correct value.
3252 */
3253 ac = cpu_cache_get(cachep);
3254
3255out:
Catalin Marinasd5cff632009-06-11 13:22:40 +01003256 /*
3257 * To avoid a false negative, if an object that is in one of the
3258 * per-CPU caches is leaked, we need to make sure kmemleak doesn't
3259 * treat the array pointers as a reference to the object.
3260 */
J. R. Okajimaf3d8b532009-12-02 16:55:49 +09003261 if (objp)
3262 kmemleak_erase(&ac->entry[ac->avail]);
Alok N Kataria5c382302005-09-27 21:45:46 -07003263 return objp;
3264}
3265
Christoph Lametere498be72005-09-09 13:03:32 -07003266#ifdef CONFIG_NUMA
3267/*
Paul Jacksonb2455392006-03-24 03:16:12 -08003268 * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
Paul Jacksonc61afb12006-03-24 03:16:08 -08003269 *
3270 * If we are in_interrupt, then process context, including cpusets and
3271 * mempolicy, may not apply and should not be used for allocation policy.
3272 */
3273static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
3274{
3275 int nid_alloc, nid_here;
3276
Christoph Lameter765c4502006-09-27 01:50:08 -07003277 if (in_interrupt() || (flags & __GFP_THISNODE))
Paul Jacksonc61afb12006-03-24 03:16:08 -08003278 return NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003279 nid_alloc = nid_here = numa_mem_id();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003280 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
Jack Steiner6adef3e2010-05-26 14:42:49 -07003281 nid_alloc = cpuset_slab_spread_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003282 else if (current->mempolicy)
Andi Kleene7b691b2012-06-09 02:40:03 -07003283 nid_alloc = slab_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003284 if (nid_alloc != nid_here)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003285 return ____cache_alloc_node(cachep, flags, nid_alloc);
Paul Jacksonc61afb12006-03-24 03:16:08 -08003286 return NULL;
3287}
3288
3289/*
Christoph Lameter765c4502006-09-27 01:50:08 -07003290 * Fallback function if there was no memory available and no objects on a
Christoph Lameter3c517a62006-12-06 20:33:29 -08003291 * certain node and fall back is permitted. First we scan all the
Christoph Lameter6a673682013-01-10 19:14:19 +00003292 * available node for available objects. If that fails then we
Christoph Lameter3c517a62006-12-06 20:33:29 -08003293 * perform an allocation without specifying a node. This allows the page
3294 * allocator to do its reclaim / fallback magic. We then insert the
3295 * slab into the proper nodelist and then allocate from it.
Christoph Lameter765c4502006-09-27 01:50:08 -07003296 */
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003297static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
Christoph Lameter765c4502006-09-27 01:50:08 -07003298{
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003299 struct zonelist *zonelist;
3300 gfp_t local_flags;
Mel Gormandd1a2392008-04-28 02:12:17 -07003301 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07003302 struct zone *zone;
3303 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003304 void *obj = NULL;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003305 int nid;
Mel Gormancc9a6c82012-03-21 16:34:11 -07003306 unsigned int cpuset_mems_cookie;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003307
3308 if (flags & __GFP_THISNODE)
3309 return NULL;
3310
Christoph Lameter6cb06222007-10-16 01:25:41 -07003311 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Christoph Lameter765c4502006-09-27 01:50:08 -07003312
Mel Gormancc9a6c82012-03-21 16:34:11 -07003313retry_cpuset:
3314 cpuset_mems_cookie = get_mems_allowed();
Andi Kleene7b691b2012-06-09 02:40:03 -07003315 zonelist = node_zonelist(slab_node(), flags);
Mel Gormancc9a6c82012-03-21 16:34:11 -07003316
Christoph Lameter3c517a62006-12-06 20:33:29 -08003317retry:
3318 /*
3319 * Look through allowed nodes for objects available
3320 * from existing per node queues.
3321 */
Mel Gorman54a6eb52008-04-28 02:12:16 -07003322 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
3323 nid = zone_to_nid(zone);
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003324
Mel Gorman54a6eb52008-04-28 02:12:16 -07003325 if (cpuset_zone_allowed_hardwall(zone, flags) &&
Christoph Lameter6a673682013-01-10 19:14:19 +00003326 cache->node[nid] &&
3327 cache->node[nid]->free_objects) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003328 obj = ____cache_alloc_node(cache,
3329 flags | GFP_THISNODE, nid);
Christoph Lameter481c5342008-06-21 16:46:35 -07003330 if (obj)
3331 break;
3332 }
Christoph Lameter3c517a62006-12-06 20:33:29 -08003333 }
3334
Christoph Lametercfce6602007-05-06 14:50:17 -07003335 if (!obj) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003336 /*
3337 * This allocation will be performed within the constraints
3338 * of the current cpuset / memory policy requirements.
3339 * We may trigger various forms of reclaim on the allowed
3340 * set and go into memory reserves if necessary.
3341 */
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003342 if (local_flags & __GFP_WAIT)
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003343 local_unlock_irq(slab_lock);
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003344 kmem_flagcheck(cache, flags);
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003345 obj = kmem_getpages(cache, local_flags, numa_mem_id());
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003346 if (local_flags & __GFP_WAIT)
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003347 local_lock_irq(slab_lock);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003348 if (obj) {
3349 /*
3350 * Insert into the appropriate per node queues
3351 */
3352 nid = page_to_nid(virt_to_page(obj));
3353 if (cache_grow(cache, flags, nid, obj)) {
3354 obj = ____cache_alloc_node(cache,
3355 flags | GFP_THISNODE, nid);
3356 if (!obj)
3357 /*
3358 * Another processor may allocate the
3359 * objects in the slab since we are
3360 * not holding any locks.
3361 */
3362 goto retry;
3363 } else {
Hugh Dickinsb6a60452007-01-05 16:36:36 -08003364 /* cache_grow already freed obj */
Christoph Lameter3c517a62006-12-06 20:33:29 -08003365 obj = NULL;
3366 }
3367 }
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003368 }
Mel Gormancc9a6c82012-03-21 16:34:11 -07003369
3370 if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !obj))
3371 goto retry_cpuset;
Christoph Lameter765c4502006-09-27 01:50:08 -07003372 return obj;
3373}
3374
3375/*
Christoph Lametere498be72005-09-09 13:03:32 -07003376 * A interface to enable slab creation on nodeid
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003378static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003379 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07003380{
3381 struct list_head *entry;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003382 struct slab *slabp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003383 struct kmem_cache_node *n;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003384 void *obj;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003385 int x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386
Aaron Tomlin14e50c62013-04-26 16:15:34 +01003387 VM_BUG_ON(nodeid > num_online_nodes());
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003388 n = cachep->node[nodeid];
3389 BUG_ON(!n);
Christoph Lametere498be72005-09-09 13:03:32 -07003390
Andrew Mortona737b3e2006-03-22 00:08:11 -08003391retry:
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003392 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003393 spin_lock(&n->list_lock);
3394 entry = n->slabs_partial.next;
3395 if (entry == &n->slabs_partial) {
3396 n->free_touched = 1;
3397 entry = n->slabs_free.next;
3398 if (entry == &n->slabs_free)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003399 goto must_grow;
3400 }
Christoph Lametere498be72005-09-09 13:03:32 -07003401
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003402 slabp = list_entry(entry, struct slab, list);
3403 check_spinlock_acquired_node(cachep, nodeid);
3404 check_slabp(cachep, slabp);
Christoph Lametere498be72005-09-09 13:03:32 -07003405
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003406 STATS_INC_NODEALLOCS(cachep);
3407 STATS_INC_ACTIVE(cachep);
3408 STATS_SET_HIGH(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003409
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003410 BUG_ON(slabp->inuse == cachep->num);
Christoph Lametere498be72005-09-09 13:03:32 -07003411
Matthew Dobson78d382d2006-02-01 03:05:47 -08003412 obj = slab_get_obj(cachep, slabp, nodeid);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003413 check_slabp(cachep, slabp);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003414 n->free_objects--;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003415 /* move slabp to correct slabp list: */
3416 list_del(&slabp->list);
Christoph Lametere498be72005-09-09 13:03:32 -07003417
Andrew Mortona737b3e2006-03-22 00:08:11 -08003418 if (slabp->free == BUFCTL_END)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003419 list_add(&slabp->list, &n->slabs_full);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003420 else
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003421 list_add(&slabp->list, &n->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07003422
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003423 spin_unlock(&n->list_lock);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003424 goto done;
Christoph Lametere498be72005-09-09 13:03:32 -07003425
Andrew Mortona737b3e2006-03-22 00:08:11 -08003426must_grow:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003427 spin_unlock(&n->list_lock);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003428 x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
Christoph Lameter765c4502006-09-27 01:50:08 -07003429 if (x)
3430 goto retry;
Christoph Lametere498be72005-09-09 13:03:32 -07003431
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003432 return fallback_alloc(cachep, flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003433
Andrew Mortona737b3e2006-03-22 00:08:11 -08003434done:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003435 return obj;
Christoph Lametere498be72005-09-09 13:03:32 -07003436}
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003437
3438/**
3439 * kmem_cache_alloc_node - Allocate an object on the specified node
3440 * @cachep: The cache to allocate from.
3441 * @flags: See kmalloc().
3442 * @nodeid: node number of the target node.
3443 * @caller: return address of caller, used for debug information
3444 *
3445 * Identical to kmem_cache_alloc but it will allocate memory on the given
3446 * node, which can improve the performance for cpu bound structures.
3447 *
3448 * Fallback to other node is possible if __GFP_THISNODE is not set.
3449 */
3450static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003451slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003452 unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003453{
3454 unsigned long save_flags;
3455 void *ptr;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003456 int slab_node = numa_mem_id();
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003457
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003458 flags &= gfp_allowed_mask;
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003459
Nick Piggincf40bd12009-01-21 08:12:39 +01003460 lockdep_trace_alloc(flags);
3461
Akinobu Mita773ff602008-12-23 19:37:01 +09003462 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003463 return NULL;
3464
Glauber Costad79923f2012-12-18 14:22:48 -08003465 cachep = memcg_kmem_get_cache(cachep, flags);
3466
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003467 cache_alloc_debugcheck_before(cachep, flags);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003468 local_lock_irqsave(slab_lock, save_flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003469
Andrew Mortoneacbbae2011-07-28 13:59:49 -07003470 if (nodeid == NUMA_NO_NODE)
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003471 nodeid = slab_node;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003472
Christoph Lameter6a673682013-01-10 19:14:19 +00003473 if (unlikely(!cachep->node[nodeid])) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003474 /* Node not bootstrapped yet */
3475 ptr = fallback_alloc(cachep, flags);
3476 goto out;
3477 }
3478
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003479 if (nodeid == slab_node) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003480 /*
3481 * Use the locally cached objects if possible.
3482 * However ____cache_alloc does not allow fallback
3483 * to other nodes. It may fail while we still have
3484 * objects on other nodes available.
3485 */
3486 ptr = ____cache_alloc(cachep, flags);
3487 if (ptr)
3488 goto out;
3489 }
3490 /* ___cache_alloc_node can fall back to other nodes */
3491 ptr = ____cache_alloc_node(cachep, flags, nodeid);
3492 out:
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003493 local_unlock_irqrestore(slab_lock, save_flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003494 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003495 kmemleak_alloc_recursive(ptr, cachep->object_size, 1, cachep->flags,
Catalin Marinasd5cff632009-06-11 13:22:40 +01003496 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003497
Pekka Enbergc175eea2008-05-09 20:35:53 +02003498 if (likely(ptr))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003499 kmemcheck_slab_alloc(cachep, flags, ptr, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003500
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003501 if (unlikely((flags & __GFP_ZERO) && ptr))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003502 memset(ptr, 0, cachep->object_size);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003503
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003504 return ptr;
3505}
3506
3507static __always_inline void *
3508__do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
3509{
3510 void *objp;
3511
3512 if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
3513 objp = alternate_node_alloc(cache, flags);
3514 if (objp)
3515 goto out;
3516 }
3517 objp = ____cache_alloc(cache, flags);
3518
3519 /*
3520 * We may just have run out of memory on the local node.
3521 * ____cache_alloc_node() knows how to locate memory on other nodes
3522 */
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003523 if (!objp)
3524 objp = ____cache_alloc_node(cache, flags, numa_mem_id());
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003525
3526 out:
3527 return objp;
3528}
3529#else
3530
3531static __always_inline void *
3532__do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
3533{
3534 return ____cache_alloc(cachep, flags);
3535}
3536
3537#endif /* CONFIG_NUMA */
3538
3539static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003540slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003541{
3542 unsigned long save_flags;
3543 void *objp;
3544
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003545 flags &= gfp_allowed_mask;
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003546
Nick Piggincf40bd12009-01-21 08:12:39 +01003547 lockdep_trace_alloc(flags);
3548
Akinobu Mita773ff602008-12-23 19:37:01 +09003549 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003550 return NULL;
3551
Glauber Costad79923f2012-12-18 14:22:48 -08003552 cachep = memcg_kmem_get_cache(cachep, flags);
3553
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003554 cache_alloc_debugcheck_before(cachep, flags);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003555 local_lock_irqsave(slab_lock, save_flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003556 objp = __do_cache_alloc(cachep, flags);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003557 local_unlock_irqrestore(slab_lock, save_flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003558 objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003559 kmemleak_alloc_recursive(objp, cachep->object_size, 1, cachep->flags,
Catalin Marinasd5cff632009-06-11 13:22:40 +01003560 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003561 prefetchw(objp);
3562
Pekka Enbergc175eea2008-05-09 20:35:53 +02003563 if (likely(objp))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003564 kmemcheck_slab_alloc(cachep, flags, objp, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003565
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003566 if (unlikely((flags & __GFP_ZERO) && objp))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003567 memset(objp, 0, cachep->object_size);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003568
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003569 return objp;
3570}
Christoph Lametere498be72005-09-09 13:03:32 -07003571
3572/*
3573 * Caller needs to acquire correct kmem_list's list_lock
3574 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003575static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003576 int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577{
3578 int i;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003579 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
3581 for (i = 0; i < nr_objects; i++) {
Mel Gorman072bb0a2012-07-31 16:43:58 -07003582 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
Mel Gorman072bb0a2012-07-31 16:43:58 -07003585 clear_obj_pfmemalloc(&objpp[i]);
3586 objp = objpp[i];
3587
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003588 slabp = virt_to_slab(objp);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003589 n = cachep->node[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 list_del(&slabp->list);
Christoph Lameterff694162005-09-22 21:44:02 -07003591 check_spinlock_acquired_node(cachep, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 check_slabp(cachep, slabp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08003593 slab_put_obj(cachep, slabp, objp, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 STATS_DEC_ACTIVE(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003595 n->free_objects++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 check_slabp(cachep, slabp);
3597
3598 /* fixup slab chains */
3599 if (slabp->inuse == 0) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003600 if (n->free_objects > n->free_limit) {
3601 n->free_objects -= cachep->num;
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07003602 /* No need to drop any previously held
3603 * lock here, even if we have a off-slab slab
3604 * descriptor it is guaranteed to come from
3605 * a different cache, refer to comments before
3606 * alloc_slabmgmt.
3607 */
Peter Zijlstra696ac522009-07-03 08:44:43 -05003608 slab_destroy(cachep, slabp, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609 } else {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003610 list_add(&slabp->list, &n->slabs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 }
3612 } else {
3613 /* Unconditionally move a slab to the end of the
3614 * partial list on free - maximum time for the
3615 * other objects to be freed, too.
3616 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003617 list_add_tail(&slabp->list, &n->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 }
3619 }
3620}
3621
Pekka Enberg343e0d72006-02-01 03:05:50 -08003622static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003623{
3624 int batchcount;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003625 struct kmem_cache_node *n;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003626 int node = numa_mem_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
3628 batchcount = ac->batchcount;
3629#if DEBUG
3630 BUG_ON(!batchcount || batchcount > ac->avail);
3631#endif
3632 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003633 n = cachep->node[node];
3634 spin_lock(&n->list_lock);
3635 if (n->shared) {
3636 struct array_cache *shared_array = n->shared;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003637 int max = shared_array->limit - shared_array->avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 if (max) {
3639 if (batchcount > max)
3640 batchcount = max;
Christoph Lametere498be72005-09-09 13:03:32 -07003641 memcpy(&(shared_array->entry[shared_array->avail]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003642 ac->entry, sizeof(void *) * batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 shared_array->avail += batchcount;
3644 goto free_done;
3645 }
3646 }
3647
Christoph Lameterff694162005-09-22 21:44:02 -07003648 free_block(cachep, ac->entry, batchcount, node);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003649free_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650#if STATS
3651 {
3652 int i = 0;
3653 struct list_head *p;
3654
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003655 p = n->slabs_free.next;
3656 while (p != &(n->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 struct slab *slabp;
3658
3659 slabp = list_entry(p, struct slab, list);
3660 BUG_ON(slabp->inuse);
3661
3662 i++;
3663 p = p->next;
3664 }
3665 STATS_SET_FREEABLE(cachep, i);
3666 }
3667#endif
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003668 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 ac->avail -= batchcount;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003670 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671}
3672
3673/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08003674 * Release an obj back to its cache. If the obj has a constructed state, it must
3675 * be in this state _before_ it is released. Called with disabled ints.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 */
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003677static inline void __cache_free(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003678 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679{
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003680 struct array_cache *ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681
3682 check_irq_off();
Catalin Marinasd5cff632009-06-11 13:22:40 +01003683 kmemleak_free_recursive(objp, cachep->flags);
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003684 objp = cache_free_debugcheck(cachep, objp, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003686 kmemcheck_slab_free(cachep, objp, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003687
Siddha, Suresh B1807a1a2007-08-22 14:01:49 -07003688 /*
3689 * Skip calling cache_free_alien() when the platform is not numa.
3690 * This will avoid cache misses that happen while accessing slabp (which
3691 * is per page memory reference) to get nodeid. Instead use a global
3692 * variable to skip the call, which is mostly likely to be present in
3693 * the cache.
3694 */
Mel Gormanb6e68bc2009-06-16 15:32:16 -07003695 if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07003696 return;
Christoph Lametere498be72005-09-09 13:03:32 -07003697
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 if (likely(ac->avail < ac->limit)) {
3699 STATS_INC_FREEHIT(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700 } else {
3701 STATS_INC_FREEMISS(cachep);
3702 cache_flusharray(cachep, ac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 }
Zhao Jin42c8c992011-08-27 00:26:17 +08003704
Mel Gorman072bb0a2012-07-31 16:43:58 -07003705 ac_put_obj(cachep, ac, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706}
3707
3708/**
3709 * kmem_cache_alloc - Allocate an object
3710 * @cachep: The cache to allocate from.
3711 * @flags: See kmalloc().
3712 *
3713 * Allocate an object from this cache. The flags are only relevant
3714 * if the cache has no available objects.
3715 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003716void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003718 void *ret = slab_alloc(cachep, flags, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003719
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003720 trace_kmem_cache_alloc(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003721 cachep->object_size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003722
3723 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724}
3725EXPORT_SYMBOL(kmem_cache_alloc);
3726
Li Zefan0f24f122009-12-11 15:45:30 +08003727#ifdef CONFIG_TRACING
Steven Rostedt85beb582010-11-24 16:23:34 -05003728void *
Ezequiel Garcia40521472012-09-08 17:47:56 -03003729kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003730{
Steven Rostedt85beb582010-11-24 16:23:34 -05003731 void *ret;
3732
Ezequiel Garcia48356302012-09-08 17:47:57 -03003733 ret = slab_alloc(cachep, flags, _RET_IP_);
Steven Rostedt85beb582010-11-24 16:23:34 -05003734
3735 trace_kmalloc(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003736 size, cachep->size, flags);
Steven Rostedt85beb582010-11-24 16:23:34 -05003737 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003738}
Steven Rostedt85beb582010-11-24 16:23:34 -05003739EXPORT_SYMBOL(kmem_cache_alloc_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003740#endif
3741
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742#ifdef CONFIG_NUMA
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003743void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
3744{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003745 void *ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003746
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003747 trace_kmem_cache_alloc_node(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003748 cachep->object_size, cachep->size,
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003749 flags, nodeid);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003750
3751 return ret;
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003752}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753EXPORT_SYMBOL(kmem_cache_alloc_node);
3754
Li Zefan0f24f122009-12-11 15:45:30 +08003755#ifdef CONFIG_TRACING
Ezequiel Garcia40521472012-09-08 17:47:56 -03003756void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
Steven Rostedt85beb582010-11-24 16:23:34 -05003757 gfp_t flags,
Ezequiel Garcia40521472012-09-08 17:47:56 -03003758 int nodeid,
3759 size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003760{
Steven Rostedt85beb582010-11-24 16:23:34 -05003761 void *ret;
3762
Ezequiel Garcia592f4142012-09-25 08:07:08 -03003763 ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003764
Steven Rostedt85beb582010-11-24 16:23:34 -05003765 trace_kmalloc_node(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003766 size, cachep->size,
Steven Rostedt85beb582010-11-24 16:23:34 -05003767 flags, nodeid);
3768 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003769}
Steven Rostedt85beb582010-11-24 16:23:34 -05003770EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003771#endif
3772
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003773static __always_inline void *
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003774__do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller)
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003775{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003776 struct kmem_cache *cachep;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003777
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003778 cachep = kmalloc_slab(size, flags);
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003779 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3780 return cachep;
Ezequiel Garcia40521472012-09-08 17:47:56 -03003781 return kmem_cache_alloc_node_trace(cachep, flags, node, size);
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003782}
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003783
Li Zefan0bb38a52009-12-11 15:45:50 +08003784#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003785void *__kmalloc_node(size_t size, gfp_t flags, int node)
3786{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003787 return __do_kmalloc_node(size, flags, node, _RET_IP_);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003788}
Christoph Hellwigdbe5e692006-09-25 23:31:36 -07003789EXPORT_SYMBOL(__kmalloc_node);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003790
3791void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003792 int node, unsigned long caller)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003793{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003794 return __do_kmalloc_node(size, flags, node, caller);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003795}
3796EXPORT_SYMBOL(__kmalloc_node_track_caller);
3797#else
3798void *__kmalloc_node(size_t size, gfp_t flags, int node)
3799{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003800 return __do_kmalloc_node(size, flags, node, 0);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003801}
3802EXPORT_SYMBOL(__kmalloc_node);
Li Zefan0bb38a52009-12-11 15:45:50 +08003803#endif /* CONFIG_DEBUG_SLAB || CONFIG_TRACING */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003804#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805
3806/**
Paul Drynoff800590f2006-06-23 02:03:48 -07003807 * __do_kmalloc - allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808 * @size: how many bytes of memory are required.
Paul Drynoff800590f2006-06-23 02:03:48 -07003809 * @flags: the type of memory to allocate (see kmalloc).
Randy Dunlap911851e2006-03-22 00:08:14 -08003810 * @caller: function caller for debug tracking of the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 */
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003812static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003813 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003815 struct kmem_cache *cachep;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003816 void *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003818 /* If you want to save a few bytes .text space: replace
3819 * __ with kmem_.
3820 * Then kmalloc uses the uninlined functions instead of the inline
3821 * functions.
3822 */
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003823 cachep = kmalloc_slab(size, flags);
Linus Torvaldsa5c96d82007-07-19 13:17:15 -07003824 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3825 return cachep;
Ezequiel Garcia48356302012-09-08 17:47:57 -03003826 ret = slab_alloc(cachep, flags, caller);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003827
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003828 trace_kmalloc(caller, ret,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003829 size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003830
3831 return ret;
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003832}
3833
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003834
Li Zefan0bb38a52009-12-11 15:45:50 +08003835#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003836void *__kmalloc(size_t size, gfp_t flags)
3837{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003838 return __do_kmalloc(size, flags, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839}
3840EXPORT_SYMBOL(__kmalloc);
3841
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003842void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003843{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003844 return __do_kmalloc(size, flags, caller);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003845}
3846EXPORT_SYMBOL(__kmalloc_track_caller);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003847
3848#else
3849void *__kmalloc(size_t size, gfp_t flags)
3850{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003851 return __do_kmalloc(size, flags, 0);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003852}
3853EXPORT_SYMBOL(__kmalloc);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003854#endif
3855
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856/**
3857 * kmem_cache_free - Deallocate an object
3858 * @cachep: The cache the allocation was from.
3859 * @objp: The previously allocated object.
3860 *
3861 * Free an object which was previously allocated from this
3862 * cache.
3863 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003864void kmem_cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865{
3866 unsigned long flags;
Glauber Costab9ce5ef2012-12-18 14:22:46 -08003867 cachep = cache_from_obj(cachep, objp);
3868 if (!cachep)
3869 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870
Feng Tangd97d4762012-07-02 14:29:10 +08003871 debug_check_no_locks_freed(objp, cachep->object_size);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07003872 if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003873 debug_check_no_obj_freed(objp, cachep->object_size);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003874 local_lock_irqsave(slab_lock, flags);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003875 __cache_free(cachep, objp, _RET_IP_);
Peter Zijlstra696ac522009-07-03 08:44:43 -05003876 unlock_slab_and_free_delayed(flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003877
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003878 trace_kmem_cache_free(_RET_IP_, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879}
3880EXPORT_SYMBOL(kmem_cache_free);
3881
3882/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 * kfree - free previously allocated memory
3884 * @objp: pointer returned by kmalloc.
3885 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07003886 * If @objp is NULL, no operation is performed.
3887 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 * Don't free memory not originally allocated by kmalloc()
3889 * or you will run into trouble.
3890 */
3891void kfree(const void *objp)
3892{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003893 struct kmem_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 unsigned long flags;
3895
Pekka Enberg2121db72009-03-25 11:05:57 +02003896 trace_kfree(_RET_IP_, objp);
3897
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003898 if (unlikely(ZERO_OR_NULL_PTR(objp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 kfree_debugcheck(objp);
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003901 c = virt_to_cache(objp);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003902 debug_check_no_locks_freed(objp, c->object_size);
3903
3904 debug_check_no_obj_freed(objp, c->object_size);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003905 local_lock_irqsave(slab_lock, flags);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003906 __cache_free(c, (void *)objp, _RET_IP_);
Peter Zijlstra696ac522009-07-03 08:44:43 -05003907 unlock_slab_and_free_delayed(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908}
3909EXPORT_SYMBOL(kfree);
3910
Christoph Lametere498be72005-09-09 13:03:32 -07003911/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003912 * This initializes kmem_cache_node or resizes various caches for all nodes.
Christoph Lametere498be72005-09-09 13:03:32 -07003913 */
Pekka Enberg83b519e2009-06-10 19:40:04 +03003914static int alloc_kmemlist(struct kmem_cache *cachep, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -07003915{
3916 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003917 struct kmem_cache_node *n;
Christoph Lametercafeb022006-03-25 03:06:46 -08003918 struct array_cache *new_shared;
Paul Menage3395ee02006-12-06 20:32:16 -08003919 struct array_cache **new_alien = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003920
Mel Gorman9c09a952008-01-24 05:49:54 -08003921 for_each_online_node(node) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003922
Paul Menage3395ee02006-12-06 20:32:16 -08003923 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03003924 new_alien = alloc_alien_cache(node, cachep->limit, gfp);
Paul Menage3395ee02006-12-06 20:32:16 -08003925 if (!new_alien)
3926 goto fail;
3927 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003928
Eric Dumazet63109842007-05-06 14:49:28 -07003929 new_shared = NULL;
3930 if (cachep->shared) {
3931 new_shared = alloc_arraycache(node,
Christoph Lameter0718dc22006-03-25 03:06:47 -08003932 cachep->shared*cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003933 0xbaadf00d, gfp);
Eric Dumazet63109842007-05-06 14:49:28 -07003934 if (!new_shared) {
3935 free_alien_cache(new_alien);
3936 goto fail;
3937 }
Christoph Lameter0718dc22006-03-25 03:06:47 -08003938 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003939
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003940 n = cachep->node[node];
3941 if (n) {
3942 struct array_cache *shared = n->shared;
Christoph Lametercafeb022006-03-25 03:06:46 -08003943
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003944 local_spin_lock_irq(slab_lock, &n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07003945
Christoph Lametercafeb022006-03-25 03:06:46 -08003946 if (shared)
Christoph Lameter0718dc22006-03-25 03:06:47 -08003947 free_block(cachep, shared->entry,
3948 shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07003949
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003950 n->shared = new_shared;
3951 if (!n->alien) {
3952 n->alien = new_alien;
Christoph Lametere498be72005-09-09 13:03:32 -07003953 new_alien = NULL;
3954 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003955 n->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003956 cachep->batchcount + cachep->num;
Peter Zijlstra696ac522009-07-03 08:44:43 -05003957 unlock_l3_and_free_delayed(&n->list_lock);
3958
Christoph Lametercafeb022006-03-25 03:06:46 -08003959 kfree(shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003960 free_alien_cache(new_alien);
3961 continue;
3962 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003963 n = kmalloc_node(sizeof(struct kmem_cache_node), gfp, node);
3964 if (!n) {
Christoph Lameter0718dc22006-03-25 03:06:47 -08003965 free_alien_cache(new_alien);
3966 kfree(new_shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003967 goto fail;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003968 }
Christoph Lametere498be72005-09-09 13:03:32 -07003969
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003970 kmem_cache_node_init(n);
3971 n->next_reap = jiffies + REAPTIMEOUT_LIST3 +
Andrew Mortona737b3e2006-03-22 00:08:11 -08003972 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003973 n->shared = new_shared;
3974 n->alien = new_alien;
3975 n->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003976 cachep->batchcount + cachep->num;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003977 cachep->node[node] = n;
Christoph Lametere498be72005-09-09 13:03:32 -07003978 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003979 return 0;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003980
Andrew Mortona737b3e2006-03-22 00:08:11 -08003981fail:
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003982 if (!cachep->list.next) {
Christoph Lameter0718dc22006-03-25 03:06:47 -08003983 /* Cache is not active yet. Roll back what we did */
3984 node--;
3985 while (node >= 0) {
Christoph Lameter6a673682013-01-10 19:14:19 +00003986 if (cachep->node[node]) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003987 n = cachep->node[node];
Christoph Lameter0718dc22006-03-25 03:06:47 -08003988
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003989 kfree(n->shared);
3990 free_alien_cache(n->alien);
3991 kfree(n);
Christoph Lameter6a673682013-01-10 19:14:19 +00003992 cachep->node[node] = NULL;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003993 }
3994 node--;
3995 }
3996 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003997 return -ENOMEM;
Christoph Lametere498be72005-09-09 13:03:32 -07003998}
3999
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000struct ccupdate_struct {
Pekka Enberg343e0d72006-02-01 03:05:50 -08004001 struct kmem_cache *cachep;
Eric Dumazetacfe7d72011-07-25 08:55:42 +02004002 struct array_cache *new[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003};
4004
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004005static void __do_ccupdate_local(void *info, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006{
Andrew Mortona737b3e2006-03-22 00:08:11 -08004007 struct ccupdate_struct *new = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 struct array_cache *old;
4009
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004010 old = cpu_cache_get_on_cpu(new->cachep, cpu);
Christoph Lametere498be72005-09-09 13:03:32 -07004011
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004012 new->cachep->array[cpu] = new->new[cpu];
4013 new->new[cpu] = old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014}
4015
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004016#ifndef CONFIG_PREEMPT_RT_BASE
4017static void do_ccupdate_local(void *info)
4018{
4019 __do_ccupdate_local(info, smp_processor_id());
4020}
4021#else
4022static void do_ccupdate_local(void *info, int cpu)
4023{
4024 __do_ccupdate_local(info, cpu);
4025}
4026#endif
4027
Christoph Lameter18004c52012-07-06 15:25:12 -05004028/* Always called with the slab_mutex held */
Glauber Costa943a4512012-12-18 14:23:03 -08004029static int __do_tune_cpucache(struct kmem_cache *cachep, int limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03004030 int batchcount, int shared, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031{
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004032 struct ccupdate_struct *new;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07004033 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034
Eric Dumazetacfe7d72011-07-25 08:55:42 +02004035 new = kzalloc(sizeof(*new) + nr_cpu_ids * sizeof(struct array_cache *),
4036 gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004037 if (!new)
4038 return -ENOMEM;
4039
Christoph Lametere498be72005-09-09 13:03:32 -07004040 for_each_online_cpu(i) {
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07004041 new->new[i] = alloc_arraycache(cpu_to_mem(i), limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03004042 batchcount, gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004043 if (!new->new[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004044 for (i--; i >= 0; i--)
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004045 kfree(new->new[i]);
4046 kfree(new);
Christoph Lametere498be72005-09-09 13:03:32 -07004047 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 }
4049 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004050 new->cachep = cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004052 slab_on_each_cpu(do_ccupdate_local, (void *)new);
Christoph Lametere498be72005-09-09 13:03:32 -07004053
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 check_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 cachep->batchcount = batchcount;
4056 cachep->limit = limit;
Christoph Lametere498be72005-09-09 13:03:32 -07004057 cachep->shared = shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058
Christoph Lametere498be72005-09-09 13:03:32 -07004059 for_each_online_cpu(i) {
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004060 struct array_cache *ccold = new->new[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 if (!ccold)
4062 continue;
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004063 local_spin_lock_irq(slab_lock,
4064 &cachep->node[cpu_to_mem(i)]->list_lock);
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07004065 free_block(cachep, ccold->entry, ccold->avail, cpu_to_mem(i));
Peter Zijlstra696ac522009-07-03 08:44:43 -05004066
4067 unlock_l3_and_free_delayed(&cachep->node[cpu_to_mem(i)]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 kfree(ccold);
4069 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004070 kfree(new);
Pekka Enberg83b519e2009-06-10 19:40:04 +03004071 return alloc_kmemlist(cachep, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072}
4073
Glauber Costa943a4512012-12-18 14:23:03 -08004074static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
4075 int batchcount, int shared, gfp_t gfp)
4076{
4077 int ret;
4078 struct kmem_cache *c = NULL;
4079 int i = 0;
4080
4081 ret = __do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
4082
4083 if (slab_state < FULL)
4084 return ret;
4085
4086 if ((ret < 0) || !is_root_cache(cachep))
4087 return ret;
4088
Glauber Costaebe945c2012-12-18 14:23:10 -08004089 VM_BUG_ON(!mutex_is_locked(&slab_mutex));
Glauber Costa943a4512012-12-18 14:23:03 -08004090 for_each_memcg_cache_index(i) {
4091 c = cache_from_memcg(cachep, i);
4092 if (c)
4093 /* return value determined by the parent cache only */
4094 __do_tune_cpucache(c, limit, batchcount, shared, gfp);
4095 }
4096
4097 return ret;
4098}
4099
Christoph Lameter18004c52012-07-06 15:25:12 -05004100/* Called with slab_mutex held always */
Pekka Enberg83b519e2009-06-10 19:40:04 +03004101static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102{
4103 int err;
Glauber Costa943a4512012-12-18 14:23:03 -08004104 int limit = 0;
4105 int shared = 0;
4106 int batchcount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107
Glauber Costa943a4512012-12-18 14:23:03 -08004108 if (!is_root_cache(cachep)) {
4109 struct kmem_cache *root = memcg_root_cache(cachep);
4110 limit = root->limit;
4111 shared = root->shared;
4112 batchcount = root->batchcount;
4113 }
4114
4115 if (limit && shared && batchcount)
4116 goto skip_setup;
Andrew Mortona737b3e2006-03-22 00:08:11 -08004117 /*
4118 * The head array serves three purposes:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 * - create a LIFO ordering, i.e. return objects that are cache-warm
4120 * - reduce the number of spinlock operations.
Andrew Mortona737b3e2006-03-22 00:08:11 -08004121 * - reduce the number of linked list operations on the slab and
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 * bufctl chains: array operations are cheaper.
4123 * The numbers are guessed, we should auto-tune as described by
4124 * Bonwick.
4125 */
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004126 if (cachep->size > 131072)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 limit = 1;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004128 else if (cachep->size > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 limit = 8;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004130 else if (cachep->size > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 limit = 24;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004132 else if (cachep->size > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 limit = 54;
4134 else
4135 limit = 120;
4136
Andrew Mortona737b3e2006-03-22 00:08:11 -08004137 /*
4138 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 * allocation behaviour: Most allocs on one cpu, most free operations
4140 * on another cpu. For these cases, an efficient object passing between
4141 * cpus is necessary. This is provided by a shared array. The array
4142 * replaces Bonwick's magazine layer.
4143 * On uniprocessor, it's functionally equivalent (but less efficient)
4144 * to a larger limit. Thus disabled by default.
4145 */
4146 shared = 0;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004147 if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 shared = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
4150#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08004151 /*
4152 * With debugging enabled, large batchcount lead to excessively long
4153 * periods with disabled local interrupts. Limit the batchcount
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 */
4155 if (limit > 32)
4156 limit = 32;
4157#endif
Glauber Costa943a4512012-12-18 14:23:03 -08004158 batchcount = (limit + 1) / 2;
4159skip_setup:
4160 err = do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 if (err)
4162 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004163 cachep->name, -err);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07004164 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165}
4166
Christoph Lameter1b552532006-03-22 00:09:07 -08004167/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004168 * Drain an array if it contains any elements taking the node lock only if
4169 * necessary. Note that the node listlock also protects the array_cache
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004170 * if drain_array() is used on the shared array.
Christoph Lameter1b552532006-03-22 00:09:07 -08004171 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004172static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
Christoph Lameter1b552532006-03-22 00:09:07 -08004173 struct array_cache *ac, int force, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174{
4175 int tofree;
4176
Christoph Lameter1b552532006-03-22 00:09:07 -08004177 if (!ac || !ac->avail)
4178 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 if (ac->touched && !force) {
4180 ac->touched = 0;
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004181 } else {
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004182 local_spin_lock_irq(slab_lock, &n->list_lock);
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004183 if (ac->avail) {
4184 tofree = force ? ac->avail : (ac->limit + 4) / 5;
4185 if (tofree > ac->avail)
4186 tofree = (ac->avail + 1) / 2;
4187 free_block(cachep, ac->entry, tofree, node);
4188 ac->avail -= tofree;
4189 memmove(ac->entry, &(ac->entry[tofree]),
4190 sizeof(void *) * ac->avail);
4191 }
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004192 local_spin_unlock_irq(slab_lock, &n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 }
4194}
4195
4196/**
4197 * cache_reap - Reclaim memory from caches.
Randy Dunlap05fb6bf2007-02-28 20:12:13 -08004198 * @w: work descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 *
4200 * Called from workqueue/eventd every few seconds.
4201 * Purpose:
4202 * - clear the per-cpu caches for this CPU.
4203 * - return freeable pages to the main free memory pool.
4204 *
Andrew Mortona737b3e2006-03-22 00:08:11 -08004205 * If we cannot acquire the cache chain mutex then just give up - we'll try
4206 * again on the next iteration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207 */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004208static void cache_reap(struct work_struct *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209{
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004210 struct kmem_cache *searchp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004211 struct kmem_cache_node *n;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07004212 int node = numa_mem_id();
Jean Delvarebf6aede2009-04-02 16:56:54 -07004213 struct delayed_work *work = to_delayed_work(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
Christoph Lameter18004c52012-07-06 15:25:12 -05004215 if (!mutex_trylock(&slab_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 /* Give up. Setup the next iteration. */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004217 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Christoph Lameter18004c52012-07-06 15:25:12 -05004219 list_for_each_entry(searchp, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 check_irq_on();
4221
Christoph Lameter35386e32006-03-22 00:09:05 -08004222 /*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004223 * We only take the node lock if absolutely necessary and we
Christoph Lameter35386e32006-03-22 00:09:05 -08004224 * have established with reasonable certainty that
4225 * we can do some work if the lock was obtained.
4226 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004227 n = searchp->node[node];
Christoph Lameter35386e32006-03-22 00:09:05 -08004228
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004229 reap_alien(searchp, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004231 drain_array(searchp, n, cpu_cache_get(searchp), 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232
Christoph Lameter35386e32006-03-22 00:09:05 -08004233 /*
4234 * These are racy checks but it does not matter
4235 * if we skip one check or scan twice.
4236 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004237 if (time_after(n->next_reap, jiffies))
Christoph Lameter35386e32006-03-22 00:09:05 -08004238 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004240 n->next_reap = jiffies + REAPTIMEOUT_LIST3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004242 drain_array(searchp, n, n->shared, 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004244 if (n->free_touched)
4245 n->free_touched = 0;
Christoph Lametered11d9e2006-06-30 01:55:45 -07004246 else {
4247 int freed;
4248
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004249 freed = drain_freelist(searchp, n, (n->free_limit +
Christoph Lametered11d9e2006-06-30 01:55:45 -07004250 5 * searchp->num - 1) / (5 * searchp->num));
4251 STATS_ADD_REAPED(searchp, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 }
Christoph Lameter35386e32006-03-22 00:09:05 -08004253next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 cond_resched();
4255 }
4256 check_irq_on();
Christoph Lameter18004c52012-07-06 15:25:12 -05004257 mutex_unlock(&slab_mutex);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004258 next_reap_node();
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004259out:
Andrew Mortona737b3e2006-03-22 00:08:11 -08004260 /* Set up the next iteration */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004261 schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_CPUC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262}
4263
Linus Torvalds158a9622008-01-02 13:04:48 -08004264#ifdef CONFIG_SLABINFO
Glauber Costa0d7561c2012-10-19 18:20:27 +04004265void get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004267 struct slab *slabp;
4268 unsigned long active_objs;
4269 unsigned long num_objs;
4270 unsigned long active_slabs = 0;
4271 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004272 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273 char *error = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07004274 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004275 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 active_objs = 0;
4278 num_slabs = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004279 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004280 n = cachep->node[node];
4281 if (!n)
Christoph Lametere498be72005-09-09 13:03:32 -07004282 continue;
4283
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004284 check_irq_on();
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004285 local_spin_lock_irq(slab_lock, &n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07004286
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004287 list_for_each_entry(slabp, &n->slabs_full, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004288 if (slabp->inuse != cachep->num && !error)
4289 error = "slabs_full accounting error";
4290 active_objs += cachep->num;
4291 active_slabs++;
4292 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004293 list_for_each_entry(slabp, &n->slabs_partial, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004294 if (slabp->inuse == cachep->num && !error)
4295 error = "slabs_partial inuse accounting error";
4296 if (!slabp->inuse && !error)
4297 error = "slabs_partial/inuse accounting error";
4298 active_objs += slabp->inuse;
4299 active_slabs++;
4300 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004301 list_for_each_entry(slabp, &n->slabs_free, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004302 if (slabp->inuse && !error)
4303 error = "slabs_free/inuse accounting error";
4304 num_slabs++;
4305 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004306 free_objects += n->free_objects;
4307 if (n->shared)
4308 shared_avail += n->shared->avail;
Christoph Lametere498be72005-09-09 13:03:32 -07004309
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004310 local_spin_unlock_irq(slab_lock, &n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004312 num_slabs += active_slabs;
4313 num_objs = num_slabs * cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07004314 if (num_objs - active_objs != free_objects && !error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315 error = "free_objects accounting error";
4316
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004317 name = cachep->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 if (error)
4319 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
4320
Glauber Costa0d7561c2012-10-19 18:20:27 +04004321 sinfo->active_objs = active_objs;
4322 sinfo->num_objs = num_objs;
4323 sinfo->active_slabs = active_slabs;
4324 sinfo->num_slabs = num_slabs;
4325 sinfo->shared_avail = shared_avail;
4326 sinfo->limit = cachep->limit;
4327 sinfo->batchcount = cachep->batchcount;
4328 sinfo->shared = cachep->shared;
4329 sinfo->objects_per_slab = cachep->num;
4330 sinfo->cache_order = cachep->gfporder;
4331}
4332
4333void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *cachep)
4334{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335#if STATS
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004336 { /* node stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 unsigned long high = cachep->high_mark;
4338 unsigned long allocs = cachep->num_allocations;
4339 unsigned long grown = cachep->grown;
4340 unsigned long reaped = cachep->reaped;
4341 unsigned long errors = cachep->errors;
4342 unsigned long max_freeable = cachep->max_freeable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 unsigned long node_allocs = cachep->node_allocs;
Christoph Lametere498be72005-09-09 13:03:32 -07004344 unsigned long node_frees = cachep->node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004345 unsigned long overflows = cachep->node_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346
Joe Perchese92dd4f2010-03-26 19:27:58 -07004347 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu "
4348 "%4lu %4lu %4lu %4lu %4lu",
4349 allocs, high, grown,
4350 reaped, errors, max_freeable, node_allocs,
4351 node_frees, overflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 }
4353 /* cpu stats */
4354 {
4355 unsigned long allochit = atomic_read(&cachep->allochit);
4356 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
4357 unsigned long freehit = atomic_read(&cachep->freehit);
4358 unsigned long freemiss = atomic_read(&cachep->freemiss);
4359
4360 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004361 allochit, allocmiss, freehit, freemiss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 }
4363#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364}
4365
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366#define MAX_SLABINFO_WRITE 128
4367/**
4368 * slabinfo_write - Tuning for the slab allocator
4369 * @file: unused
4370 * @buffer: user buffer
4371 * @count: data length
4372 * @ppos: unused
4373 */
Glauber Costab7454ad2012-10-19 18:20:25 +04004374ssize_t slabinfo_write(struct file *file, const char __user *buffer,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004375 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004377 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378 int limit, batchcount, shared, res;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004379 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004380
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 if (count > MAX_SLABINFO_WRITE)
4382 return -EINVAL;
4383 if (copy_from_user(&kbuf, buffer, count))
4384 return -EFAULT;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004385 kbuf[MAX_SLABINFO_WRITE] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386
4387 tmp = strchr(kbuf, ' ');
4388 if (!tmp)
4389 return -EINVAL;
4390 *tmp = '\0';
4391 tmp++;
4392 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
4393 return -EINVAL;
4394
4395 /* Find the cache in the chain of caches. */
Christoph Lameter18004c52012-07-06 15:25:12 -05004396 mutex_lock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 res = -EINVAL;
Christoph Lameter18004c52012-07-06 15:25:12 -05004398 list_for_each_entry(cachep, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 if (!strcmp(cachep->name, kbuf)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08004400 if (limit < 1 || batchcount < 1 ||
4401 batchcount > limit || shared < 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07004402 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07004404 res = do_tune_cpucache(cachep, limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03004405 batchcount, shared,
4406 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 }
4408 break;
4409 }
4410 }
Christoph Lameter18004c52012-07-06 15:25:12 -05004411 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412 if (res >= 0)
4413 res = count;
4414 return res;
4415}
Al Viro871751e2006-03-25 03:06:39 -08004416
4417#ifdef CONFIG_DEBUG_SLAB_LEAK
4418
4419static void *leaks_start(struct seq_file *m, loff_t *pos)
4420{
Christoph Lameter18004c52012-07-06 15:25:12 -05004421 mutex_lock(&slab_mutex);
4422 return seq_list_start(&slab_caches, *pos);
Al Viro871751e2006-03-25 03:06:39 -08004423}
4424
4425static inline int add_caller(unsigned long *n, unsigned long v)
4426{
4427 unsigned long *p;
4428 int l;
4429 if (!v)
4430 return 1;
4431 l = n[1];
4432 p = n + 2;
4433 while (l) {
4434 int i = l/2;
4435 unsigned long *q = p + 2 * i;
4436 if (*q == v) {
4437 q[1]++;
4438 return 1;
4439 }
4440 if (*q > v) {
4441 l = i;
4442 } else {
4443 p = q + 2;
4444 l -= i + 1;
4445 }
4446 }
4447 if (++n[1] == n[0])
4448 return 0;
4449 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4450 p[0] = v;
4451 p[1] = 1;
4452 return 1;
4453}
4454
4455static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
4456{
4457 void *p;
4458 int i;
4459 if (n[0] == n[1])
4460 return;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004461 for (i = 0, p = s->s_mem; i < c->num; i++, p += c->size) {
Al Viro871751e2006-03-25 03:06:39 -08004462 if (slab_bufctl(s)[i] != BUFCTL_ACTIVE)
4463 continue;
4464 if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
4465 return;
4466 }
4467}
4468
4469static void show_symbol(struct seq_file *m, unsigned long address)
4470{
4471#ifdef CONFIG_KALLSYMS
Al Viro871751e2006-03-25 03:06:39 -08004472 unsigned long offset, size;
Tejun Heo9281ace2007-07-17 04:03:51 -07004473 char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
Al Viro871751e2006-03-25 03:06:39 -08004474
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004475 if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
Al Viro871751e2006-03-25 03:06:39 -08004476 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004477 if (modname[0])
Al Viro871751e2006-03-25 03:06:39 -08004478 seq_printf(m, " [%s]", modname);
4479 return;
4480 }
4481#endif
4482 seq_printf(m, "%p", (void *)address);
4483}
4484
4485static int leaks_show(struct seq_file *m, void *p)
4486{
Thierry Reding0672aa72012-06-22 19:42:49 +02004487 struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);
Al Viro871751e2006-03-25 03:06:39 -08004488 struct slab *slabp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004489 struct kmem_cache_node *n;
Al Viro871751e2006-03-25 03:06:39 -08004490 const char *name;
Christoph Lameterdb845062013-02-05 18:45:23 +00004491 unsigned long *x = m->private;
Al Viro871751e2006-03-25 03:06:39 -08004492 int node;
4493 int i;
4494
4495 if (!(cachep->flags & SLAB_STORE_USER))
4496 return 0;
4497 if (!(cachep->flags & SLAB_RED_ZONE))
4498 return 0;
4499
4500 /* OK, we can do it */
4501
Christoph Lameterdb845062013-02-05 18:45:23 +00004502 x[1] = 0;
Al Viro871751e2006-03-25 03:06:39 -08004503
4504 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004505 n = cachep->node[node];
4506 if (!n)
Al Viro871751e2006-03-25 03:06:39 -08004507 continue;
4508
4509 check_irq_on();
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004510 local_spin_lock_irq(slab_lock, &n->list_lock);
Al Viro871751e2006-03-25 03:06:39 -08004511
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004512 list_for_each_entry(slabp, &n->slabs_full, list)
Christoph Lameterdb845062013-02-05 18:45:23 +00004513 handle_slab(x, cachep, slabp);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004514 list_for_each_entry(slabp, &n->slabs_partial, list)
Christoph Lameterdb845062013-02-05 18:45:23 +00004515 handle_slab(x, cachep, slabp);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004516 local_spin_unlock_irq(slab_lock, &n->list_lock);
Al Viro871751e2006-03-25 03:06:39 -08004517 }
4518 name = cachep->name;
Christoph Lameterdb845062013-02-05 18:45:23 +00004519 if (x[0] == x[1]) {
Al Viro871751e2006-03-25 03:06:39 -08004520 /* Increase the buffer size */
Christoph Lameter18004c52012-07-06 15:25:12 -05004521 mutex_unlock(&slab_mutex);
Christoph Lameterdb845062013-02-05 18:45:23 +00004522 m->private = kzalloc(x[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
Al Viro871751e2006-03-25 03:06:39 -08004523 if (!m->private) {
4524 /* Too bad, we are really out */
Christoph Lameterdb845062013-02-05 18:45:23 +00004525 m->private = x;
Christoph Lameter18004c52012-07-06 15:25:12 -05004526 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004527 return -ENOMEM;
4528 }
Christoph Lameterdb845062013-02-05 18:45:23 +00004529 *(unsigned long *)m->private = x[0] * 2;
4530 kfree(x);
Christoph Lameter18004c52012-07-06 15:25:12 -05004531 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004532 /* Now make sure this entry will be retried */
4533 m->count = m->size;
4534 return 0;
4535 }
Christoph Lameterdb845062013-02-05 18:45:23 +00004536 for (i = 0; i < x[1]; i++) {
4537 seq_printf(m, "%s: %lu ", name, x[2*i+3]);
4538 show_symbol(m, x[2*i+2]);
Al Viro871751e2006-03-25 03:06:39 -08004539 seq_putc(m, '\n');
4540 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004541
Al Viro871751e2006-03-25 03:06:39 -08004542 return 0;
4543}
4544
Glauber Costab7454ad2012-10-19 18:20:25 +04004545static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4546{
4547 return seq_list_next(p, &slab_caches, pos);
4548}
4549
4550static void s_stop(struct seq_file *m, void *p)
4551{
4552 mutex_unlock(&slab_mutex);
4553}
4554
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004555static const struct seq_operations slabstats_op = {
Al Viro871751e2006-03-25 03:06:39 -08004556 .start = leaks_start,
4557 .next = s_next,
4558 .stop = s_stop,
4559 .show = leaks_show,
4560};
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004561
4562static int slabstats_open(struct inode *inode, struct file *file)
4563{
4564 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
4565 int ret = -ENOMEM;
4566 if (n) {
4567 ret = seq_open(file, &slabstats_op);
4568 if (!ret) {
4569 struct seq_file *m = file->private_data;
4570 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
4571 m->private = n;
4572 n = NULL;
4573 }
4574 kfree(n);
4575 }
4576 return ret;
4577}
4578
4579static const struct file_operations proc_slabstats_operations = {
4580 .open = slabstats_open,
4581 .read = seq_read,
4582 .llseek = seq_lseek,
4583 .release = seq_release_private,
4584};
Al Viro871751e2006-03-25 03:06:39 -08004585#endif
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004586
4587static int __init slab_proc_init(void)
4588{
4589#ifdef CONFIG_DEBUG_SLAB_LEAK
4590 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
4591#endif
4592 return 0;
4593}
4594module_init(slab_proc_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595#endif
4596
Manfred Spraul00e145b2005-09-03 15:55:07 -07004597/**
4598 * ksize - get the actual amount of memory allocated for a given object
4599 * @objp: Pointer to the object
4600 *
4601 * kmalloc may internally round up allocations and return more memory
4602 * than requested. ksize() can be used to determine the actual amount of
4603 * memory allocated. The caller may use this additional memory, even though
4604 * a smaller amount of memory was initially specified with the kmalloc call.
4605 * The caller must guarantee that objp points to a valid object previously
4606 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4607 * must not be freed during the duration of the call.
4608 */
Pekka Enbergfd76bab2007-05-06 14:48:40 -07004609size_t ksize(const void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610{
Christoph Lameteref8b4522007-10-16 01:24:46 -07004611 BUG_ON(!objp);
4612 if (unlikely(objp == ZERO_SIZE_PTR))
Manfred Spraul00e145b2005-09-03 15:55:07 -07004613 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614
Christoph Lameter8c138bc2012-06-13 10:24:58 -05004615 return virt_to_cache(objp)->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616}
Kirill A. Shutemovb1aabec2009-02-10 15:21:44 +02004617EXPORT_SYMBOL(ksize);