blob: 698bf71ec3c25198d71adc67b2e05f30455093db [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);
Thomas Gleixner4a621b32011-06-18 19:44:43 +0200637static DEFINE_LOCAL_IRQ_LOCK(slab_lock);
638
639#ifndef CONFIG_PREEMPT_RT_BASE
640# define slab_on_each_cpu(func, cp) on_each_cpu(func, cp, 1)
641#else
642/*
643 * execute func() for all CPUs. On PREEMPT_RT we dont actually have
644 * to run on the remote CPUs - we only have to take their CPU-locks.
645 * (This is a rare operation, so cacheline bouncing is not an issue.)
646 */
647static void
648slab_on_each_cpu(void (*func)(void *arg, int this_cpu), void *arg)
649{
650 unsigned int i;
651
652 get_cpu_light();
653 for_each_online_cpu(i)
654 func(arg, i);
655 put_cpu_light();
656}
657
658static void lock_slab_on(unsigned int cpu)
659{
660 local_lock_irq_on(slab_lock, cpu);
661}
662
663static void unlock_slab_on(unsigned int cpu)
664{
665 local_unlock_irq_on(slab_lock, cpu);
666}
667#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Pekka Enberg343e0d72006-02-01 03:05:50 -0800669static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 return cachep->array[smp_processor_id()];
672}
673
Thomas Gleixner4a621b32011-06-18 19:44:43 +0200674static inline struct array_cache *cpu_cache_get_on_cpu(struct kmem_cache *cachep,
675 int cpu)
676{
677 return cachep->array[cpu];
678}
679
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800680static size_t slab_mgmt_size(size_t nr_objs, size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681{
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800682 return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align);
683}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
Andrew Mortona737b3e2006-03-22 00:08:11 -0800685/*
686 * Calculate the number of objects and left-over bytes for a given buffer size.
687 */
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800688static void cache_estimate(unsigned long gfporder, size_t buffer_size,
689 size_t align, int flags, size_t *left_over,
690 unsigned int *num)
691{
692 int nr_objs;
693 size_t mgmt_size;
694 size_t slab_size = PAGE_SIZE << gfporder;
695
696 /*
697 * The slab management structure can be either off the slab or
698 * on it. For the latter case, the memory allocated for a
699 * slab is used for:
700 *
701 * - The struct slab
702 * - One kmem_bufctl_t for each object
703 * - Padding to respect alignment of @align
704 * - @buffer_size bytes for each object
705 *
706 * If the slab management structure is off the slab, then the
707 * alignment will already be calculated into the size. Because
708 * the slabs are all pages aligned, the objects will be at the
709 * correct alignment when allocated.
710 */
711 if (flags & CFLGS_OFF_SLAB) {
712 mgmt_size = 0;
713 nr_objs = slab_size / buffer_size;
714
715 if (nr_objs > SLAB_LIMIT)
716 nr_objs = SLAB_LIMIT;
717 } else {
718 /*
719 * Ignore padding for the initial guess. The padding
720 * is at most @align-1 bytes, and @buffer_size is at
721 * least @align. In the worst case, this result will
722 * be one greater than the number of objects that fit
723 * into the memory allocation when taking the padding
724 * into account.
725 */
726 nr_objs = (slab_size - sizeof(struct slab)) /
727 (buffer_size + sizeof(kmem_bufctl_t));
728
729 /*
730 * This calculated number will be either the right
731 * amount, or one greater than what we want.
732 */
733 if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
734 > slab_size)
735 nr_objs--;
736
737 if (nr_objs > SLAB_LIMIT)
738 nr_objs = SLAB_LIMIT;
739
740 mgmt_size = slab_mgmt_size(nr_objs, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800742 *num = nr_objs;
743 *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Christoph Lameterf28510d2012-09-11 19:49:38 +0000746#if DEBUG
Harvey Harrisond40cee22008-04-30 00:55:07 -0700747#define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Andrew Mortona737b3e2006-03-22 00:08:11 -0800749static void __slab_error(const char *function, struct kmem_cache *cachep,
750 char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751{
752 printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800753 function, cachep->name, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +1030755 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
Christoph Lameterf28510d2012-09-11 19:49:38 +0000757#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Paul Menage3395ee02006-12-06 20:32:16 -0800759/*
760 * By default on NUMA we use alien caches to stage the freeing of
761 * objects allocated from other nodes. This causes massive memory
762 * inefficiencies when using fake NUMA setup to split memory into a
763 * large number of small nodes, so it can be disabled on the command
764 * line
765 */
766
767static int use_alien_caches __read_mostly = 1;
768static int __init noaliencache_setup(char *s)
769{
770 use_alien_caches = 0;
771 return 1;
772}
773__setup("noaliencache", noaliencache_setup);
774
David Rientjes3df1ccc2011-10-18 22:09:28 -0700775static int __init slab_max_order_setup(char *str)
776{
777 get_option(&str, &slab_max_order);
778 slab_max_order = slab_max_order < 0 ? 0 :
779 min(slab_max_order, MAX_ORDER - 1);
780 slab_max_order_set = true;
781
782 return 1;
783}
784__setup("slab_max_order=", slab_max_order_setup);
785
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800786#ifdef CONFIG_NUMA
787/*
788 * Special reaping functions for NUMA systems called from cache_reap().
789 * These take care of doing round robin flushing of alien caches (containing
790 * objects freed on different nodes from which they were allocated) and the
791 * flushing of remote pcps by calling drain_node_pages.
792 */
Tejun Heo1871e522009-10-29 22:34:13 +0900793static DEFINE_PER_CPU(unsigned long, slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800794
795static void init_reap_node(int cpu)
796{
797 int node;
798
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -0700799 node = next_node(cpu_to_mem(cpu), node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800800 if (node == MAX_NUMNODES)
Paul Jackson442295c2006-03-22 00:09:11 -0800801 node = first_node(node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800802
Tejun Heo1871e522009-10-29 22:34:13 +0900803 per_cpu(slab_reap_node, cpu) = node;
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800804}
805
806static void next_reap_node(void)
807{
Christoph Lameter909ea962010-12-08 16:22:55 +0100808 int node = __this_cpu_read(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800809
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800810 node = next_node(node, node_online_map);
811 if (unlikely(node >= MAX_NUMNODES))
812 node = first_node(node_online_map);
Christoph Lameter909ea962010-12-08 16:22:55 +0100813 __this_cpu_write(slab_reap_node, node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800814}
815
816#else
817#define init_reap_node(cpu) do { } while (0)
818#define next_reap_node(void) do { } while (0)
819#endif
820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821/*
822 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
823 * via the workqueue/eventd.
824 * Add the CPU number into the expiration time to minimize the possibility of
825 * the CPUs getting into lockstep and contending for the global cache chain
826 * lock.
827 */
Adrian Bunk897e6792007-07-15 23:38:20 -0700828static void __cpuinit start_cpu_timer(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Tejun Heo1871e522009-10-29 22:34:13 +0900830 struct delayed_work *reap_work = &per_cpu(slab_reap_work, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832 /*
833 * When this gets called from do_initcalls via cpucache_init(),
834 * init_workqueues() has already run, so keventd will be setup
835 * at that time.
836 */
David Howells52bad642006-11-22 14:54:01 +0000837 if (keventd_up() && reap_work->work.func == NULL) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800838 init_reap_node(cpu);
Tejun Heo203b42f2012-08-21 13:18:23 -0700839 INIT_DEFERRABLE_WORK(reap_work, cache_reap);
Arjan van de Ven2b284212006-12-10 02:21:28 -0800840 schedule_delayed_work_on(cpu, reap_work,
841 __round_jiffies_relative(HZ, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 }
843}
844
Christoph Lametere498be72005-09-09 13:03:32 -0700845static struct array_cache *alloc_arraycache(int node, int entries,
Pekka Enberg83b519e2009-06-10 19:40:04 +0300846 int batchcount, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800848 int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 struct array_cache *nc = NULL;
850
Pekka Enberg83b519e2009-06-10 19:40:04 +0300851 nc = kmalloc_node(memsize, gfp, node);
Catalin Marinasd5cff632009-06-11 13:22:40 +0100852 /*
853 * The array_cache structures contain pointers to free object.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300854 * However, when such objects are allocated or transferred to another
Catalin Marinasd5cff632009-06-11 13:22:40 +0100855 * cache the pointers are not cleared and they could be counted as
856 * valid references during a kmemleak scan. Therefore, kmemleak must
857 * not scan such objects.
858 */
859 kmemleak_no_scan(nc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 if (nc) {
861 nc->avail = 0;
862 nc->limit = entries;
863 nc->batchcount = batchcount;
864 nc->touched = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700865 spin_lock_init(&nc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
867 return nc;
868}
869
Mel Gorman072bb0a2012-07-31 16:43:58 -0700870static inline bool is_slab_pfmemalloc(struct slab *slabp)
871{
872 struct page *page = virt_to_page(slabp->s_mem);
873
874 return PageSlabPfmemalloc(page);
875}
876
877/* Clears pfmemalloc_active if no slabs have pfmalloc set */
878static void recheck_pfmemalloc_active(struct kmem_cache *cachep,
879 struct array_cache *ac)
880{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000881 struct kmem_cache_node *n = cachep->node[numa_mem_id()];
Mel Gorman072bb0a2012-07-31 16:43:58 -0700882 struct slab *slabp;
883 unsigned long flags;
884
885 if (!pfmemalloc_active)
886 return;
887
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000888 spin_lock_irqsave(&n->list_lock, flags);
889 list_for_each_entry(slabp, &n->slabs_full, list)
Mel Gorman072bb0a2012-07-31 16:43:58 -0700890 if (is_slab_pfmemalloc(slabp))
891 goto out;
892
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000893 list_for_each_entry(slabp, &n->slabs_partial, list)
Mel Gorman072bb0a2012-07-31 16:43:58 -0700894 if (is_slab_pfmemalloc(slabp))
895 goto out;
896
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000897 list_for_each_entry(slabp, &n->slabs_free, list)
Mel Gorman072bb0a2012-07-31 16:43:58 -0700898 if (is_slab_pfmemalloc(slabp))
899 goto out;
900
901 pfmemalloc_active = false;
902out:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000903 spin_unlock_irqrestore(&n->list_lock, flags);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700904}
905
Mel Gorman381760e2012-07-31 16:44:30 -0700906static void *__ac_get_obj(struct kmem_cache *cachep, struct array_cache *ac,
Mel Gorman072bb0a2012-07-31 16:43:58 -0700907 gfp_t flags, bool force_refill)
908{
909 int i;
910 void *objp = ac->entry[--ac->avail];
911
912 /* Ensure the caller is allowed to use objects from PFMEMALLOC slab */
913 if (unlikely(is_obj_pfmemalloc(objp))) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000914 struct kmem_cache_node *n;
Mel Gorman072bb0a2012-07-31 16:43:58 -0700915
916 if (gfp_pfmemalloc_allowed(flags)) {
917 clear_obj_pfmemalloc(&objp);
918 return objp;
919 }
920
921 /* The caller cannot use PFMEMALLOC objects, find another one */
Joonsoo Kimd014dc22012-09-17 14:09:06 -0700922 for (i = 0; i < ac->avail; i++) {
Mel Gorman072bb0a2012-07-31 16:43:58 -0700923 /* If a !PFMEMALLOC object is found, swap them */
924 if (!is_obj_pfmemalloc(ac->entry[i])) {
925 objp = ac->entry[i];
926 ac->entry[i] = ac->entry[ac->avail];
927 ac->entry[ac->avail] = objp;
928 return objp;
929 }
930 }
931
932 /*
933 * If there are empty slabs on the slabs_free list and we are
934 * being forced to refill the cache, mark this one !pfmemalloc.
935 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000936 n = cachep->node[numa_mem_id()];
937 if (!list_empty(&n->slabs_free) && force_refill) {
Mel Gorman072bb0a2012-07-31 16:43:58 -0700938 struct slab *slabp = virt_to_slab(objp);
Mel Gorman30c29be2012-09-17 14:09:03 -0700939 ClearPageSlabPfmemalloc(virt_to_head_page(slabp->s_mem));
Mel Gorman072bb0a2012-07-31 16:43:58 -0700940 clear_obj_pfmemalloc(&objp);
941 recheck_pfmemalloc_active(cachep, ac);
942 return objp;
943 }
944
945 /* No !PFMEMALLOC objects available */
946 ac->avail++;
947 objp = NULL;
948 }
949
950 return objp;
951}
952
Mel Gorman381760e2012-07-31 16:44:30 -0700953static inline void *ac_get_obj(struct kmem_cache *cachep,
954 struct array_cache *ac, gfp_t flags, bool force_refill)
955{
956 void *objp;
957
958 if (unlikely(sk_memalloc_socks()))
959 objp = __ac_get_obj(cachep, ac, flags, force_refill);
960 else
961 objp = ac->entry[--ac->avail];
962
963 return objp;
964}
965
966static void *__ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
Mel Gorman072bb0a2012-07-31 16:43:58 -0700967 void *objp)
968{
969 if (unlikely(pfmemalloc_active)) {
970 /* Some pfmemalloc slabs exist, check if this is one */
Mel Gorman30c29be2012-09-17 14:09:03 -0700971 struct page *page = virt_to_head_page(objp);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700972 if (PageSlabPfmemalloc(page))
973 set_obj_pfmemalloc(&objp);
974 }
975
Mel Gorman381760e2012-07-31 16:44:30 -0700976 return objp;
977}
978
979static inline void ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
980 void *objp)
981{
982 if (unlikely(sk_memalloc_socks()))
983 objp = __ac_put_obj(cachep, ac, objp);
984
Mel Gorman072bb0a2012-07-31 16:43:58 -0700985 ac->entry[ac->avail++] = objp;
986}
987
Christoph Lameter3ded1752006-03-25 03:06:44 -0800988/*
989 * Transfer objects in one arraycache to another.
990 * Locking must be handled by the caller.
991 *
992 * Return the number of entries transferred.
993 */
994static int transfer_objects(struct array_cache *to,
995 struct array_cache *from, unsigned int max)
996{
997 /* Figure out how many entries to transfer */
Hagen Paul Pfeifer732eacc2010-10-26 14:22:23 -0700998 int nr = min3(from->avail, max, to->limit - to->avail);
Christoph Lameter3ded1752006-03-25 03:06:44 -0800999
1000 if (!nr)
1001 return 0;
1002
1003 memcpy(to->entry + to->avail, from->entry + from->avail -nr,
1004 sizeof(void *) *nr);
1005
1006 from->avail -= nr;
1007 to->avail += nr;
Christoph Lameter3ded1752006-03-25 03:06:44 -08001008 return nr;
1009}
1010
Christoph Lameter765c4502006-09-27 01:50:08 -07001011#ifndef CONFIG_NUMA
1012
1013#define drain_alien_cache(cachep, alien) do { } while (0)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001014#define reap_alien(cachep, n) do { } while (0)
Christoph Lameter765c4502006-09-27 01:50:08 -07001015
Pekka Enberg83b519e2009-06-10 19:40:04 +03001016static inline struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
Christoph Lameter765c4502006-09-27 01:50:08 -07001017{
1018 return (struct array_cache **)BAD_ALIEN_MAGIC;
1019}
1020
1021static inline void free_alien_cache(struct array_cache **ac_ptr)
1022{
1023}
1024
1025static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
1026{
1027 return 0;
1028}
1029
1030static inline void *alternate_node_alloc(struct kmem_cache *cachep,
1031 gfp_t flags)
1032{
1033 return NULL;
1034}
1035
Christoph Hellwig8b98c162006-12-06 20:32:30 -08001036static inline void *____cache_alloc_node(struct kmem_cache *cachep,
Christoph Lameter765c4502006-09-27 01:50:08 -07001037 gfp_t flags, int nodeid)
1038{
1039 return NULL;
1040}
1041
1042#else /* CONFIG_NUMA */
1043
Christoph Hellwig8b98c162006-12-06 20:32:30 -08001044static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
Paul Jacksonc61afb12006-03-24 03:16:08 -08001045static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
Christoph Lameterdc85da12006-01-18 17:42:36 -08001046
Pekka Enberg83b519e2009-06-10 19:40:04 +03001047static struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -07001048{
1049 struct array_cache **ac_ptr;
Christoph Lameter8ef82862007-02-20 13:57:52 -08001050 int memsize = sizeof(void *) * nr_node_ids;
Christoph Lametere498be72005-09-09 13:03:32 -07001051 int i;
1052
1053 if (limit > 1)
1054 limit = 12;
Haicheng Lif3186a92010-01-06 15:25:23 +08001055 ac_ptr = kzalloc_node(memsize, gfp, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001056 if (ac_ptr) {
1057 for_each_node(i) {
Haicheng Lif3186a92010-01-06 15:25:23 +08001058 if (i == node || !node_online(i))
Christoph Lametere498be72005-09-09 13:03:32 -07001059 continue;
Pekka Enberg83b519e2009-06-10 19:40:04 +03001060 ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d, gfp);
Christoph Lametere498be72005-09-09 13:03:32 -07001061 if (!ac_ptr[i]) {
Akinobu Mitacc550de2007-11-14 16:58:35 -08001062 for (i--; i >= 0; i--)
Christoph Lametere498be72005-09-09 13:03:32 -07001063 kfree(ac_ptr[i]);
1064 kfree(ac_ptr);
1065 return NULL;
1066 }
1067 }
1068 }
1069 return ac_ptr;
1070}
1071
Pekka Enberg5295a742006-02-01 03:05:48 -08001072static void free_alien_cache(struct array_cache **ac_ptr)
Christoph Lametere498be72005-09-09 13:03:32 -07001073{
1074 int i;
1075
1076 if (!ac_ptr)
1077 return;
Christoph Lametere498be72005-09-09 13:03:32 -07001078 for_each_node(i)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001079 kfree(ac_ptr[i]);
Christoph Lametere498be72005-09-09 13:03:32 -07001080 kfree(ac_ptr);
1081}
1082
Pekka Enberg343e0d72006-02-01 03:05:50 -08001083static void __drain_alien_cache(struct kmem_cache *cachep,
Pekka Enberg5295a742006-02-01 03:05:48 -08001084 struct array_cache *ac, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07001085{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001086 struct kmem_cache_node *n = cachep->node[node];
Christoph Lametere498be72005-09-09 13:03:32 -07001087
1088 if (ac->avail) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001089 spin_lock(&n->list_lock);
Christoph Lametere00946f2006-03-25 03:06:45 -08001090 /*
1091 * Stuff objects into the remote nodes shared array first.
1092 * That way we could avoid the overhead of putting the objects
1093 * into the free lists and getting them back later.
1094 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001095 if (n->shared)
1096 transfer_objects(n->shared, ac, ac->limit);
Christoph Lametere00946f2006-03-25 03:06:45 -08001097
Christoph Lameterff694162005-09-22 21:44:02 -07001098 free_block(cachep, ac->entry, ac->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001099 ac->avail = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001100 spin_unlock(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07001101 }
1102}
1103
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001104/*
1105 * Called from cache_reap() to regularly drain alien caches round robin.
1106 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001107static void reap_alien(struct kmem_cache *cachep, struct kmem_cache_node *n)
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001108{
Christoph Lameter909ea962010-12-08 16:22:55 +01001109 int node = __this_cpu_read(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001110
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001111 if (n->alien) {
1112 struct array_cache *ac = n->alien[node];
Christoph Lametere00946f2006-03-25 03:06:45 -08001113
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001114 if (ac && ac->avail &&
1115 local_spin_trylock_irq(slab_lock, &ac->lock)) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001116 __drain_alien_cache(cachep, ac, node);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001117 local_spin_unlock_irq(slab_lock, &ac->lock);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001118 }
1119 }
1120}
1121
Andrew Mortona737b3e2006-03-22 00:08:11 -08001122static void drain_alien_cache(struct kmem_cache *cachep,
1123 struct array_cache **alien)
Christoph Lametere498be72005-09-09 13:03:32 -07001124{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001125 int i = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07001126 struct array_cache *ac;
1127 unsigned long flags;
1128
1129 for_each_online_node(i) {
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001130 ac = alien[i];
Christoph Lametere498be72005-09-09 13:03:32 -07001131 if (ac) {
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001132 local_spin_lock_irqsave(slab_lock, &ac->lock, flags);
Christoph Lametere498be72005-09-09 13:03:32 -07001133 __drain_alien_cache(cachep, ac, i);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001134 local_spin_unlock_irqrestore(slab_lock, &ac->lock, flags);
Christoph Lametere498be72005-09-09 13:03:32 -07001135 }
1136 }
1137}
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001138
Ingo Molnar873623d2006-07-13 14:44:38 +02001139static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001140{
1141 struct slab *slabp = virt_to_slab(objp);
1142 int nodeid = slabp->nodeid;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001143 struct kmem_cache_node *n;
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001144 struct array_cache *alien = NULL;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001145 int node;
1146
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001147 node = numa_mem_id();
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001148
1149 /*
1150 * Make sure we are not freeing a object from another node to the array
1151 * cache on this cpu.
1152 */
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001153 if (likely(slabp->nodeid == node))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001154 return 0;
1155
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001156 n = cachep->node[node];
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001157 STATS_INC_NODEFREES(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001158 if (n->alien && n->alien[nodeid]) {
1159 alien = n->alien[nodeid];
Ingo Molnar873623d2006-07-13 14:44:38 +02001160 spin_lock(&alien->lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001161 if (unlikely(alien->avail == alien->limit)) {
1162 STATS_INC_ACOVERFLOW(cachep);
1163 __drain_alien_cache(cachep, alien, nodeid);
1164 }
Mel Gorman072bb0a2012-07-31 16:43:58 -07001165 ac_put_obj(cachep, alien, objp);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001166 spin_unlock(&alien->lock);
1167 } else {
Christoph Lameter6a673682013-01-10 19:14:19 +00001168 spin_lock(&(cachep->node[nodeid])->list_lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001169 free_block(cachep, &objp, 1, nodeid);
Christoph Lameter6a673682013-01-10 19:14:19 +00001170 spin_unlock(&(cachep->node[nodeid])->list_lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001171 }
1172 return 1;
1173}
Christoph Lametere498be72005-09-09 13:03:32 -07001174#endif
1175
David Rientjes8f9f8d92010-03-27 19:40:47 -07001176/*
Christoph Lameter6a673682013-01-10 19:14:19 +00001177 * Allocates and initializes node for a node on each slab cache, used for
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001178 * either memory or cpu hotplug. If memory is being hot-added, the kmem_cache_node
David Rientjes8f9f8d92010-03-27 19:40:47 -07001179 * will be allocated off-node since memory is not yet online for the new node.
Christoph Lameter6a673682013-01-10 19:14:19 +00001180 * When hotplugging memory or a cpu, existing node are not replaced if
David Rientjes8f9f8d92010-03-27 19:40:47 -07001181 * already in use.
1182 *
Christoph Lameter18004c52012-07-06 15:25:12 -05001183 * Must hold slab_mutex.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001184 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001185static int init_cache_node_node(int node)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001186{
1187 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001188 struct kmem_cache_node *n;
Christoph Lameter6744f0872013-01-10 19:12:17 +00001189 const int memsize = sizeof(struct kmem_cache_node);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001190
Christoph Lameter18004c52012-07-06 15:25:12 -05001191 list_for_each_entry(cachep, &slab_caches, list) {
David Rientjes8f9f8d92010-03-27 19:40:47 -07001192 /*
1193 * Set up the size64 kmemlist for cpu before we can
1194 * begin anything. Make sure some other cpu on this
1195 * node has not already allocated this
1196 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001197 if (!cachep->node[node]) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001198 n = kmalloc_node(memsize, GFP_KERNEL, node);
1199 if (!n)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001200 return -ENOMEM;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001201 kmem_cache_node_init(n);
1202 n->next_reap = jiffies + REAPTIMEOUT_LIST3 +
David Rientjes8f9f8d92010-03-27 19:40:47 -07001203 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1204
1205 /*
1206 * The l3s don't come and go as CPUs come and
Christoph Lameter18004c52012-07-06 15:25:12 -05001207 * go. slab_mutex is sufficient
David Rientjes8f9f8d92010-03-27 19:40:47 -07001208 * protection here.
1209 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001210 cachep->node[node] = n;
David Rientjes8f9f8d92010-03-27 19:40:47 -07001211 }
1212
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001213 local_spin_lock_irq(slab_lock, &cachep->nodelists[node]->list_lock);
Christoph Lameter6a673682013-01-10 19:14:19 +00001214 cachep->node[node]->free_limit =
David Rientjes8f9f8d92010-03-27 19:40:47 -07001215 (1 + nr_cpus_node(node)) *
1216 cachep->batchcount + cachep->num;
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001217 local_spin_unlock_irq(slab_lock, &cachep->nodelists[node]->list_lock);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001218 }
1219 return 0;
1220}
1221
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001222static void __cpuinit cpuup_canceled(long cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001224 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001225 struct kmem_cache_node *n = NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001226 int node = cpu_to_mem(cpu);
Rusty Russella70f7302009-03-13 14:49:46 +10301227 const struct cpumask *mask = cpumask_of_node(node);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001228
Christoph Lameter18004c52012-07-06 15:25:12 -05001229 list_for_each_entry(cachep, &slab_caches, list) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001230 struct array_cache *nc;
1231 struct array_cache *shared;
1232 struct array_cache **alien;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001233
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001234 /* cpu is dead; no one can alloc from it. */
1235 nc = cachep->array[cpu];
1236 cachep->array[cpu] = NULL;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001237 n = cachep->node[node];
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001238
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001239 if (!n)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001240 goto free_array_cache;
1241
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001242 local_spin_lock_irq(slab_lock, &n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001243
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001244 /* Free limit for this kmem_cache_node */
1245 n->free_limit -= cachep->batchcount;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001246 if (nc)
1247 free_block(cachep, nc->entry, nc->avail, node);
1248
Rusty Russell58463c12009-12-17 11:43:12 -06001249 if (!cpumask_empty(mask)) {
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001250 local_spin_unlock_irq(slab_lock, &n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001251 goto free_array_cache;
1252 }
1253
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001254 shared = n->shared;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001255 if (shared) {
1256 free_block(cachep, shared->entry,
1257 shared->avail, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001258 n->shared = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001259 }
1260
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001261 alien = n->alien;
1262 n->alien = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001263
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001264 local_spin_unlock_irq(slab_lock, &n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001265
1266 kfree(shared);
1267 if (alien) {
1268 drain_alien_cache(cachep, alien);
1269 free_alien_cache(alien);
1270 }
1271free_array_cache:
1272 kfree(nc);
1273 }
1274 /*
1275 * In the previous loop, all the objects were freed to
1276 * the respective cache's slabs, now we can go ahead and
1277 * shrink each nodelist to its limit.
1278 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001279 list_for_each_entry(cachep, &slab_caches, list) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001280 n = cachep->node[node];
1281 if (!n)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001282 continue;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001283 drain_freelist(cachep, n, n->free_objects);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001284 }
1285}
1286
1287static int __cpuinit cpuup_prepare(long cpu)
1288{
Pekka Enberg343e0d72006-02-01 03:05:50 -08001289 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001290 struct kmem_cache_node *n = NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001291 int node = cpu_to_mem(cpu);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001292 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001294 /*
1295 * We need to do this right in the beginning since
1296 * alloc_arraycache's are going to use this list.
1297 * kmalloc_node allows us to add the slab to the right
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001298 * kmem_cache_node and not this cpu's kmem_cache_node
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001299 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001300 err = init_cache_node_node(node);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001301 if (err < 0)
1302 goto bad;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001303
1304 /*
1305 * Now we can go ahead with allocating the shared arrays and
1306 * array caches
1307 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001308 list_for_each_entry(cachep, &slab_caches, list) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001309 struct array_cache *nc;
1310 struct array_cache *shared = NULL;
1311 struct array_cache **alien = NULL;
1312
1313 nc = alloc_arraycache(node, cachep->limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001314 cachep->batchcount, GFP_KERNEL);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001315 if (!nc)
1316 goto bad;
1317 if (cachep->shared) {
1318 shared = alloc_arraycache(node,
1319 cachep->shared * cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001320 0xbaadf00d, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001321 if (!shared) {
1322 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001323 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001324 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001325 }
1326 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03001327 alien = alloc_alien_cache(node, cachep->limit, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001328 if (!alien) {
1329 kfree(shared);
1330 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001331 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001332 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001333 }
1334 cachep->array[cpu] = nc;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001335 n = cachep->node[node];
1336 BUG_ON(!n);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001337
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001338 local_spin_lock_irq(slab_lock, &n->list_lock);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001339 if (!n->shared) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001340 /*
1341 * We are serialised from CPU_DEAD or
1342 * CPU_UP_CANCELLED by the cpucontrol lock
1343 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001344 n->shared = shared;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001345 shared = NULL;
1346 }
1347#ifdef CONFIG_NUMA
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001348 if (!n->alien) {
1349 n->alien = alien;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001350 alien = NULL;
1351 }
1352#endif
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001353 local_spin_unlock_irq(slab_lock, &n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001354 kfree(shared);
1355 free_alien_cache(alien);
Peter Zijlstra83835b32011-07-22 15:26:05 +02001356 if (cachep->flags & SLAB_DEBUG_OBJECTS)
1357 slab_set_debugobj_lock_classes_node(cachep, node);
Glauber Costa6ccfb5b2012-12-18 14:22:31 -08001358 else if (!OFF_SLAB(cachep) &&
1359 !(cachep->flags & SLAB_DESTROY_BY_RCU))
1360 on_slab_lock_classes_node(cachep, node);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001361 }
Pekka Enbergce79ddc2009-11-23 22:01:15 +02001362 init_node_lock_keys(node);
1363
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001364 return 0;
1365bad:
Akinobu Mita12d00f62007-10-18 03:05:11 -07001366 cpuup_canceled(cpu);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001367 return -ENOMEM;
1368}
1369
1370static int __cpuinit cpuup_callback(struct notifier_block *nfb,
1371 unsigned long action, void *hcpu)
1372{
1373 long cpu = (long)hcpu;
1374 int err = 0;
1375
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 switch (action) {
Heiko Carstens38c3bd92007-05-09 02:34:05 -07001377 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001378 case CPU_UP_PREPARE_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001379 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001380 err = cpuup_prepare(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001381 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 break;
1383 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001384 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385 start_cpu_timer(cpu);
1386 break;
1387#ifdef CONFIG_HOTPLUG_CPU
Christoph Lameter5830c592007-05-09 02:34:22 -07001388 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001389 case CPU_DOWN_PREPARE_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001390 /*
Christoph Lameter18004c52012-07-06 15:25:12 -05001391 * Shutdown cache reaper. Note that the slab_mutex is
Christoph Lameter5830c592007-05-09 02:34:22 -07001392 * held so that if cache_reap() is invoked it cannot do
1393 * anything expensive but will only modify reap_work
1394 * and reschedule the timer.
1395 */
Tejun Heoafe2c512010-12-14 16:21:17 +01001396 cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu));
Christoph Lameter5830c592007-05-09 02:34:22 -07001397 /* Now the cache_reaper is guaranteed to be not running. */
Tejun Heo1871e522009-10-29 22:34:13 +09001398 per_cpu(slab_reap_work, cpu).work.func = NULL;
Christoph Lameter5830c592007-05-09 02:34:22 -07001399 break;
1400 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001401 case CPU_DOWN_FAILED_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001402 start_cpu_timer(cpu);
1403 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001405 case CPU_DEAD_FROZEN:
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001406 /*
1407 * Even if all the cpus of a node are down, we don't free the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001408 * kmem_cache_node of any cache. This to avoid a race between
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001409 * cpu_down, and a kmalloc allocation from another cpu for
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001410 * memory from the node of the cpu going down. The node
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001411 * structure is usually allocated from kmem_cache_create() and
1412 * gets destroyed at kmem_cache_destroy().
1413 */
Simon Arlott183ff222007-10-20 01:27:18 +02001414 /* fall through */
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08001415#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001417 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001418 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001419 cpuup_canceled(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001420 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
Akinobu Mitaeac40682010-05-26 14:43:32 -07001423 return notifier_from_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001426static struct notifier_block __cpuinitdata cpucache_notifier = {
1427 &cpuup_callback, NULL, 0
1428};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
David Rientjes8f9f8d92010-03-27 19:40:47 -07001430#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
1431/*
1432 * Drains freelist for a node on each slab cache, used for memory hot-remove.
1433 * Returns -EBUSY if all objects cannot be drained so that the node is not
1434 * removed.
1435 *
Christoph Lameter18004c52012-07-06 15:25:12 -05001436 * Must hold slab_mutex.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001437 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001438static int __meminit drain_cache_node_node(int node)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001439{
1440 struct kmem_cache *cachep;
1441 int ret = 0;
1442
Christoph Lameter18004c52012-07-06 15:25:12 -05001443 list_for_each_entry(cachep, &slab_caches, list) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001444 struct kmem_cache_node *n;
David Rientjes8f9f8d92010-03-27 19:40:47 -07001445
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001446 n = cachep->node[node];
1447 if (!n)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001448 continue;
1449
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001450 drain_freelist(cachep, n, n->free_objects);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001451
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001452 if (!list_empty(&n->slabs_full) ||
1453 !list_empty(&n->slabs_partial)) {
David Rientjes8f9f8d92010-03-27 19:40:47 -07001454 ret = -EBUSY;
1455 break;
1456 }
1457 }
1458 return ret;
1459}
1460
1461static int __meminit slab_memory_callback(struct notifier_block *self,
1462 unsigned long action, void *arg)
1463{
1464 struct memory_notify *mnb = arg;
1465 int ret = 0;
1466 int nid;
1467
1468 nid = mnb->status_change_nid;
1469 if (nid < 0)
1470 goto out;
1471
1472 switch (action) {
1473 case MEM_GOING_ONLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001474 mutex_lock(&slab_mutex);
Christoph Lameter6a673682013-01-10 19:14:19 +00001475 ret = init_cache_node_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001476 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001477 break;
1478 case MEM_GOING_OFFLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001479 mutex_lock(&slab_mutex);
Christoph Lameter6a673682013-01-10 19:14:19 +00001480 ret = drain_cache_node_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001481 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001482 break;
1483 case MEM_ONLINE:
1484 case MEM_OFFLINE:
1485 case MEM_CANCEL_ONLINE:
1486 case MEM_CANCEL_OFFLINE:
1487 break;
1488 }
1489out:
Prarit Bhargava5fda1bd2011-03-22 16:30:49 -07001490 return notifier_from_errno(ret);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001491}
1492#endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
1493
Christoph Lametere498be72005-09-09 13:03:32 -07001494/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001495 * swap the static kmem_cache_node with kmalloced memory
Christoph Lametere498be72005-09-09 13:03:32 -07001496 */
Christoph Lameter6744f0872013-01-10 19:12:17 +00001497static void __init init_list(struct kmem_cache *cachep, struct kmem_cache_node *list,
David Rientjes8f9f8d92010-03-27 19:40:47 -07001498 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07001499{
Christoph Lameter6744f0872013-01-10 19:12:17 +00001500 struct kmem_cache_node *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001501
Christoph Lameter6744f0872013-01-10 19:12:17 +00001502 ptr = kmalloc_node(sizeof(struct kmem_cache_node), GFP_NOWAIT, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07001503 BUG_ON(!ptr);
1504
Christoph Lameter6744f0872013-01-10 19:12:17 +00001505 memcpy(ptr, list, sizeof(struct kmem_cache_node));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001506 /*
1507 * Do not assume that spinlocks can be initialized via memcpy:
1508 */
1509 spin_lock_init(&ptr->list_lock);
1510
Christoph Lametere498be72005-09-09 13:03:32 -07001511 MAKE_ALL_LISTS(cachep, ptr, nodeid);
Christoph Lameter6a673682013-01-10 19:14:19 +00001512 cachep->node[nodeid] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001513}
1514
Andrew Mortona737b3e2006-03-22 00:08:11 -08001515/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001516 * For setting up all the kmem_cache_node for cache whose buffer_size is same as
1517 * size of kmem_cache_node.
Pekka Enberg556a1692008-01-25 08:20:51 +02001518 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001519static void __init set_up_node(struct kmem_cache *cachep, int index)
Pekka Enberg556a1692008-01-25 08:20:51 +02001520{
1521 int node;
1522
1523 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001524 cachep->node[node] = &init_kmem_cache_node[index + node];
Christoph Lameter6a673682013-01-10 19:14:19 +00001525 cachep->node[node]->next_reap = jiffies +
Pekka Enberg556a1692008-01-25 08:20:51 +02001526 REAPTIMEOUT_LIST3 +
1527 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1528 }
1529}
1530
1531/*
Christoph Lameter3c583462012-11-28 16:23:01 +00001532 * The memory after the last cpu cache pointer is used for the
Christoph Lameter6a673682013-01-10 19:14:19 +00001533 * the node pointer.
Christoph Lameter3c583462012-11-28 16:23:01 +00001534 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001535static void setup_node_pointer(struct kmem_cache *cachep)
Christoph Lameter3c583462012-11-28 16:23:01 +00001536{
Christoph Lameter6a673682013-01-10 19:14:19 +00001537 cachep->node = (struct kmem_cache_node **)&cachep->array[nr_cpu_ids];
Christoph Lameter3c583462012-11-28 16:23:01 +00001538}
1539
1540/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08001541 * Initialisation. Called after the page allocator have been initialised and
1542 * before smp_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 */
1544void __init kmem_cache_init(void)
1545{
Christoph Lametere498be72005-09-09 13:03:32 -07001546 int i;
1547
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001548 kmem_cache = &kmem_cache_boot;
Christoph Lameter6a673682013-01-10 19:14:19 +00001549 setup_node_pointer(kmem_cache);
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001550
Mel Gormanb6e68bc2009-06-16 15:32:16 -07001551 if (num_possible_nodes() == 1)
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001552 use_alien_caches = 0;
1553
Thomas Gleixner4a621b32011-06-18 19:44:43 +02001554 local_irq_lock_init(slab_lock);
1555
Christoph Lameter3c583462012-11-28 16:23:01 +00001556 for (i = 0; i < NUM_INIT_LISTS; i++)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001557 kmem_cache_node_init(&init_kmem_cache_node[i]);
Christoph Lameter3c583462012-11-28 16:23:01 +00001558
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001559 set_up_node(kmem_cache, CACHE_CACHE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
1561 /*
1562 * Fragmentation resistance on low memory - only use bigger
David Rientjes3df1ccc2011-10-18 22:09:28 -07001563 * page orders on machines with more than 32MB of memory if
1564 * not overridden on the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 */
David Rientjes3df1ccc2011-10-18 22:09:28 -07001566 if (!slab_max_order_set && totalram_pages > (32 << 20) >> PAGE_SHIFT)
David Rientjes543585c2011-10-18 22:09:24 -07001567 slab_max_order = SLAB_MAX_ORDER_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 /* Bootstrap is tricky, because several objects are allocated
1570 * from caches that do not exist yet:
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001571 * 1) initialize the kmem_cache cache: it contains the struct
1572 * kmem_cache structures of all caches, except kmem_cache itself:
1573 * kmem_cache is statically allocated.
Christoph Lametere498be72005-09-09 13:03:32 -07001574 * Initially an __init data area is used for the head array and the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001575 * kmem_cache_node structures, it's replaced with a kmalloc allocated
Christoph Lametere498be72005-09-09 13:03:32 -07001576 * array at the end of the bootstrap.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 * 2) Create the first kmalloc cache.
Pekka Enberg343e0d72006-02-01 03:05:50 -08001578 * The struct kmem_cache for the new cache is allocated normally.
Christoph Lametere498be72005-09-09 13:03:32 -07001579 * An __init data area is used for the head array.
1580 * 3) Create the remaining kmalloc caches, with minimally sized
1581 * head arrays.
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001582 * 4) Replace the __init data head arrays for kmem_cache and the first
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 * kmalloc cache with kmalloc allocated arrays.
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001584 * 5) Replace the __init data for kmem_cache_node for kmem_cache and
Christoph Lametere498be72005-09-09 13:03:32 -07001585 * the other cache's with kmalloc allocated memory.
1586 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 */
1588
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001589 /* 1) create the kmem_cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Eric Dumazet8da34302007-05-06 14:49:29 -07001591 /*
Eric Dumazetb56efcf2011-07-20 19:04:23 +02001592 * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
Eric Dumazet8da34302007-05-06 14:49:29 -07001593 */
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001594 create_boot_cache(kmem_cache, "kmem_cache",
1595 offsetof(struct kmem_cache, array[nr_cpu_ids]) +
Christoph Lameter6744f0872013-01-10 19:12:17 +00001596 nr_node_ids * sizeof(struct kmem_cache_node *),
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001597 SLAB_HWCACHE_ALIGN);
1598 list_add(&kmem_cache->list, &slab_caches);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
1600 /* 2+3) create the kmalloc caches */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Andrew Mortona737b3e2006-03-22 00:08:11 -08001602 /*
1603 * Initialize the caches that provide memory for the array cache and the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001604 * kmem_cache_node structures first. Without this, further allocations will
Andrew Mortona737b3e2006-03-22 00:08:11 -08001605 * bug.
Christoph Lametere498be72005-09-09 13:03:32 -07001606 */
1607
Christoph Lametere3366012013-01-10 19:14:18 +00001608 kmalloc_caches[INDEX_AC] = create_kmalloc_cache("kmalloc-ac",
1609 kmalloc_size(INDEX_AC), ARCH_KMALLOC_FLAGS);
Christoph Lametere498be72005-09-09 13:03:32 -07001610
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001611 if (INDEX_AC != INDEX_NODE)
1612 kmalloc_caches[INDEX_NODE] =
1613 create_kmalloc_cache("kmalloc-node",
1614 kmalloc_size(INDEX_NODE), ARCH_KMALLOC_FLAGS);
Christoph Lametere498be72005-09-09 13:03:32 -07001615
Ingo Molnare0a42722006-06-23 02:03:46 -07001616 slab_early_init = 0;
1617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 /* 4) Replace the bootstrap head arrays */
1619 {
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001620 struct array_cache *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001621
Pekka Enberg83b519e2009-06-10 19:40:04 +03001622 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001623
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001624 memcpy(ptr, cpu_cache_get(kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001625 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001626 /*
1627 * Do not assume that spinlocks can be initialized via memcpy:
1628 */
1629 spin_lock_init(&ptr->lock);
1630
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001631 kmem_cache->array[smp_processor_id()] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001632
Pekka Enberg83b519e2009-06-10 19:40:04 +03001633 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001634
Christoph Lametere3366012013-01-10 19:14:18 +00001635 BUG_ON(cpu_cache_get(kmalloc_caches[INDEX_AC])
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001636 != &initarray_generic.cache);
Christoph Lametere3366012013-01-10 19:14:18 +00001637 memcpy(ptr, cpu_cache_get(kmalloc_caches[INDEX_AC]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001638 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001639 /*
1640 * Do not assume that spinlocks can be initialized via memcpy:
1641 */
1642 spin_lock_init(&ptr->lock);
1643
Christoph Lametere3366012013-01-10 19:14:18 +00001644 kmalloc_caches[INDEX_AC]->array[smp_processor_id()] = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001646 /* 5) Replace the bootstrap kmem_cache_node */
Christoph Lametere498be72005-09-09 13:03:32 -07001647 {
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001648 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Mel Gorman9c09a952008-01-24 05:49:54 -08001650 for_each_online_node(nid) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001651 init_list(kmem_cache, &init_kmem_cache_node[CACHE_CACHE + nid], nid);
Pekka Enberg556a1692008-01-25 08:20:51 +02001652
Christoph Lametere3366012013-01-10 19:14:18 +00001653 init_list(kmalloc_caches[INDEX_AC],
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001654 &init_kmem_cache_node[SIZE_AC + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001655
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001656 if (INDEX_AC != INDEX_NODE) {
1657 init_list(kmalloc_caches[INDEX_NODE],
1658 &init_kmem_cache_node[SIZE_NODE + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001659 }
1660 }
1661 }
1662
Christoph Lameterf97d5f62013-01-10 19:12:17 +00001663 create_kmalloc_caches(ARCH_KMALLOC_FLAGS);
Pekka Enberg8429db52009-06-12 15:58:59 +03001664}
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001665
Pekka Enberg8429db52009-06-12 15:58:59 +03001666void __init kmem_cache_init_late(void)
1667{
1668 struct kmem_cache *cachep;
1669
Christoph Lameter97d06602012-07-06 15:25:11 -05001670 slab_state = UP;
Peter Zijlstra52cef182011-11-28 21:12:40 +01001671
Pekka Enberg8429db52009-06-12 15:58:59 +03001672 /* 6) resize the head arrays to their final sizes */
Christoph Lameter18004c52012-07-06 15:25:12 -05001673 mutex_lock(&slab_mutex);
1674 list_for_each_entry(cachep, &slab_caches, list)
Pekka Enberg8429db52009-06-12 15:58:59 +03001675 if (enable_cpucache(cachep, GFP_NOWAIT))
1676 BUG();
Christoph Lameter18004c52012-07-06 15:25:12 -05001677 mutex_unlock(&slab_mutex);
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001678
Michael Wang947ca182012-09-05 10:33:18 +08001679 /* Annotate slab for lockdep -- annotate the malloc caches */
1680 init_lock_keys();
1681
Christoph Lameter97d06602012-07-06 15:25:11 -05001682 /* Done! */
1683 slab_state = FULL;
1684
Andrew Mortona737b3e2006-03-22 00:08:11 -08001685 /*
1686 * Register a cpu startup notifier callback that initializes
1687 * cpu_cache_get for all new cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 */
1689 register_cpu_notifier(&cpucache_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690
David Rientjes8f9f8d92010-03-27 19:40:47 -07001691#ifdef CONFIG_NUMA
1692 /*
1693 * Register a memory hotplug callback that initializes and frees
Christoph Lameter6a673682013-01-10 19:14:19 +00001694 * node.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001695 */
1696 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
1697#endif
1698
Andrew Mortona737b3e2006-03-22 00:08:11 -08001699 /*
1700 * The reap timers are started later, with a module init call: That part
1701 * of the kernel is not yet operational.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 */
1703}
1704
1705static int __init cpucache_init(void)
1706{
1707 int cpu;
1708
Andrew Mortona737b3e2006-03-22 00:08:11 -08001709 /*
1710 * Register the timers that return unneeded pages to the page allocator
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 */
Christoph Lametere498be72005-09-09 13:03:32 -07001712 for_each_online_cpu(cpu)
Andrew Mortona737b3e2006-03-22 00:08:11 -08001713 start_cpu_timer(cpu);
Glauber Costaa164f8962012-06-21 00:59:18 +04001714
1715 /* Done! */
Christoph Lameter97d06602012-07-06 15:25:11 -05001716 slab_state = FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 return 0;
1718}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719__initcall(cpucache_init);
1720
Rafael Aquini8bdec192012-03-09 17:27:27 -03001721static noinline void
1722slab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid)
1723{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001724 struct kmem_cache_node *n;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001725 struct slab *slabp;
1726 unsigned long flags;
1727 int node;
1728
1729 printk(KERN_WARNING
1730 "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n",
1731 nodeid, gfpflags);
1732 printk(KERN_WARNING " cache: %s, object size: %d, order: %d\n",
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05001733 cachep->name, cachep->size, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001734
1735 for_each_online_node(node) {
1736 unsigned long active_objs = 0, num_objs = 0, free_objects = 0;
1737 unsigned long active_slabs = 0, num_slabs = 0;
1738
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001739 n = cachep->node[node];
1740 if (!n)
Rafael Aquini8bdec192012-03-09 17:27:27 -03001741 continue;
1742
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001743 spin_lock_irqsave(&n->list_lock, flags);
1744 list_for_each_entry(slabp, &n->slabs_full, list) {
Rafael Aquini8bdec192012-03-09 17:27:27 -03001745 active_objs += cachep->num;
1746 active_slabs++;
1747 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001748 list_for_each_entry(slabp, &n->slabs_partial, list) {
Rafael Aquini8bdec192012-03-09 17:27:27 -03001749 active_objs += slabp->inuse;
1750 active_slabs++;
1751 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001752 list_for_each_entry(slabp, &n->slabs_free, list)
Rafael Aquini8bdec192012-03-09 17:27:27 -03001753 num_slabs++;
1754
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001755 free_objects += n->free_objects;
1756 spin_unlock_irqrestore(&n->list_lock, flags);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001757
1758 num_slabs += active_slabs;
1759 num_objs = num_slabs * cachep->num;
1760 printk(KERN_WARNING
1761 " node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
1762 node, active_slabs, num_slabs, active_objs, num_objs,
1763 free_objects);
1764 }
1765}
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767/*
1768 * Interface to system's page allocator. No need to hold the cache-lock.
1769 *
1770 * If we requested dmaable memory, we will get it. Even if we
1771 * did not request dmaable memory, we might get it, but that
1772 * would be relatively rare and ignorable.
1773 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001774static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
1776 struct page *page;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001777 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 int i;
1779
Luke Yangd6fef9d2006-04-10 22:52:56 -07001780#ifndef CONFIG_MMU
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001781 /*
1782 * Nommu uses slab's for process anonymous memory allocations, and thus
1783 * requires __GFP_COMP to properly refcount higher order allocations
Luke Yangd6fef9d2006-04-10 22:52:56 -07001784 */
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001785 flags |= __GFP_COMP;
Luke Yangd6fef9d2006-04-10 22:52:56 -07001786#endif
Christoph Lameter765c4502006-09-27 01:50:08 -07001787
Glauber Costaa618e892012-06-14 16:17:21 +04001788 flags |= cachep->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001789 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1790 flags |= __GFP_RECLAIMABLE;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001791
Linus Torvalds517d0862009-06-16 19:50:13 -07001792 page = alloc_pages_exact_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001793 if (!page) {
1794 if (!(flags & __GFP_NOWARN) && printk_ratelimit())
1795 slab_out_of_memory(cachep, flags, nodeid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 return NULL;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
Mel Gormanb37f1dd2012-07-31 16:44:03 -07001799 /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
Mel Gorman072bb0a2012-07-31 16:43:58 -07001800 if (unlikely(page->pfmemalloc))
1801 pfmemalloc_active = true;
1802
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001803 nr_pages = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
Christoph Lameter972d1a72006-09-25 23:31:51 -07001805 add_zone_page_state(page_zone(page),
1806 NR_SLAB_RECLAIMABLE, nr_pages);
1807 else
1808 add_zone_page_state(page_zone(page),
1809 NR_SLAB_UNRECLAIMABLE, nr_pages);
Mel Gorman072bb0a2012-07-31 16:43:58 -07001810 for (i = 0; i < nr_pages; i++) {
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001811 __SetPageSlab(page + i);
Pekka Enbergc175eea2008-05-09 20:35:53 +02001812
Mel Gorman072bb0a2012-07-31 16:43:58 -07001813 if (page->pfmemalloc)
1814 SetPageSlabPfmemalloc(page + i);
1815 }
Glauber Costa1f458cb2012-12-18 14:22:50 -08001816 memcg_bind_pages(cachep, cachep->gfporder);
Mel Gorman072bb0a2012-07-31 16:43:58 -07001817
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001818 if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
1819 kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
1820
1821 if (cachep->ctor)
1822 kmemcheck_mark_uninitialized_pages(page, nr_pages);
1823 else
1824 kmemcheck_mark_unallocated_pages(page, nr_pages);
1825 }
Pekka Enbergc175eea2008-05-09 20:35:53 +02001826
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001827 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828}
1829
1830/*
1831 * Interface to system's page release.
1832 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001833static void kmem_freepages(struct kmem_cache *cachep, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001835 unsigned long i = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 struct page *page = virt_to_page(addr);
1837 const unsigned long nr_freed = i;
1838
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001839 kmemcheck_free_shadow(page, cachep->gfporder);
Pekka Enbergc175eea2008-05-09 20:35:53 +02001840
Christoph Lameter972d1a72006-09-25 23:31:51 -07001841 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1842 sub_zone_page_state(page_zone(page),
1843 NR_SLAB_RECLAIMABLE, nr_freed);
1844 else
1845 sub_zone_page_state(page_zone(page),
1846 NR_SLAB_UNRECLAIMABLE, nr_freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 while (i--) {
Nick Pigginf205b2f2006-03-22 00:08:02 -08001848 BUG_ON(!PageSlab(page));
Mel Gorman072bb0a2012-07-31 16:43:58 -07001849 __ClearPageSlabPfmemalloc(page);
Nick Pigginf205b2f2006-03-22 00:08:02 -08001850 __ClearPageSlab(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 page++;
1852 }
Glauber Costa1f458cb2012-12-18 14:22:50 -08001853
1854 memcg_release_pages(cachep, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (current->reclaim_state)
1856 current->reclaim_state->reclaimed_slab += nr_freed;
Glauber Costad79923f2012-12-18 14:22:48 -08001857 free_memcg_kmem_pages((unsigned long)addr, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858}
1859
1860static void kmem_rcu_free(struct rcu_head *head)
1861{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001862 struct slab_rcu *slab_rcu = (struct slab_rcu *)head;
Pekka Enberg343e0d72006-02-01 03:05:50 -08001863 struct kmem_cache *cachep = slab_rcu->cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865 kmem_freepages(cachep, slab_rcu->addr);
1866 if (OFF_SLAB(cachep))
1867 kmem_cache_free(cachep->slabp_cache, slab_rcu);
1868}
1869
1870#if DEBUG
1871
1872#ifdef CONFIG_DEBUG_PAGEALLOC
Pekka Enberg343e0d72006-02-01 03:05:50 -08001873static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001874 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001876 int size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001878 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001880 if (size < 5 * sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 return;
1882
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001883 *addr++ = 0x12345678;
1884 *addr++ = caller;
1885 *addr++ = smp_processor_id();
1886 size -= 3 * sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 {
1888 unsigned long *sptr = &caller;
1889 unsigned long svalue;
1890
1891 while (!kstack_end(sptr)) {
1892 svalue = *sptr++;
1893 if (kernel_text_address(svalue)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001894 *addr++ = svalue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 size -= sizeof(unsigned long);
1896 if (size <= sizeof(unsigned long))
1897 break;
1898 }
1899 }
1900
1901 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001902 *addr++ = 0x87654321;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903}
1904#endif
1905
Pekka Enberg343e0d72006-02-01 03:05:50 -08001906static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001908 int size = cachep->object_size;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001909 addr = &((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 memset(addr, val, size);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001912 *(unsigned char *)(addr + size - 1) = POISON_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913}
1914
1915static void dump_line(char *data, int offset, int limit)
1916{
1917 int i;
Dave Jonesaa83aa42006-09-29 01:59:51 -07001918 unsigned char error = 0;
1919 int bad_count = 0;
1920
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02001921 printk(KERN_ERR "%03x: ", offset);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001922 for (i = 0; i < limit; i++) {
1923 if (data[offset + i] != POISON_FREE) {
1924 error = data[offset + i];
1925 bad_count++;
1926 }
Dave Jonesaa83aa42006-09-29 01:59:51 -07001927 }
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02001928 print_hex_dump(KERN_CONT, "", 0, 16, 1,
1929 &data[offset], limit, 1);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001930
1931 if (bad_count == 1) {
1932 error ^= POISON_FREE;
1933 if (!(error & (error - 1))) {
1934 printk(KERN_ERR "Single bit error detected. Probably "
1935 "bad RAM.\n");
1936#ifdef CONFIG_X86
1937 printk(KERN_ERR "Run memtest86+ or a similar memory "
1938 "test tool.\n");
1939#else
1940 printk(KERN_ERR "Run a memory test tool.\n");
1941#endif
1942 }
1943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944}
1945#endif
1946
1947#if DEBUG
1948
Pekka Enberg343e0d72006-02-01 03:05:50 -08001949static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950{
1951 int i, size;
1952 char *realobj;
1953
1954 if (cachep->flags & SLAB_RED_ZONE) {
David Woodhouseb46b8f12007-05-08 00:22:59 -07001955 printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001956 *dbg_redzone1(cachep, objp),
1957 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959
1960 if (cachep->flags & SLAB_STORE_USER) {
Joe Perches071361d2012-12-12 10:19:12 -08001961 printk(KERN_ERR "Last user: [<%p>](%pSR)\n",
1962 *dbg_userword(cachep, objp),
1963 *dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001965 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001966 size = cachep->object_size;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001967 for (i = 0; i < size && lines; i += 16, lines--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 int limit;
1969 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001970 if (i + limit > size)
1971 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 dump_line(realobj, i, limit);
1973 }
1974}
1975
Pekka Enberg343e0d72006-02-01 03:05:50 -08001976static void check_poison_obj(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977{
1978 char *realobj;
1979 int size, i;
1980 int lines = 0;
1981
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001982 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001983 size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001985 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 char exp = POISON_FREE;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001987 if (i == size - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 exp = POISON_END;
1989 if (realobj[i] != exp) {
1990 int limit;
1991 /* Mismatch ! */
1992 /* Print header */
1993 if (lines == 0) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001994 printk(KERN_ERR
Dave Jonesface37f2011-11-15 15:03:52 -08001995 "Slab corruption (%s): %s start=%p, len=%d\n",
1996 print_tainted(), cachep->name, realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 print_objinfo(cachep, objp, 0);
1998 }
1999 /* Hexdump the affected line */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002000 i = (i / 16) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002002 if (i + limit > size)
2003 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 dump_line(realobj, i, limit);
2005 i += 16;
2006 lines++;
2007 /* Limit to 5 lines */
2008 if (lines > 5)
2009 break;
2010 }
2011 }
2012 if (lines != 0) {
2013 /* Print some data about the neighboring objects, if they
2014 * exist:
2015 */
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08002016 struct slab *slabp = virt_to_slab(objp);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002017 unsigned int objnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002019 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if (objnr) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002021 objp = index_to_obj(cachep, slabp, objnr - 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002022 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002024 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 print_objinfo(cachep, objp, 2);
2026 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002027 if (objnr + 1 < cachep->num) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002028 objp = index_to_obj(cachep, slabp, objnr + 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002029 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002031 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 print_objinfo(cachep, objp, 2);
2033 }
2034 }
2035}
2036#endif
2037
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038#if DEBUG
Rabin Vincente79aec22008-07-04 00:40:32 +05302039static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002040{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 int i;
2042 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002043 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
2045 if (cachep->flags & SLAB_POISON) {
2046#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002047 if (cachep->size % PAGE_SIZE == 0 &&
Andrew Mortona737b3e2006-03-22 00:08:11 -08002048 OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002049 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002050 cachep->size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 else
2052 check_poison_obj(cachep, objp);
2053#else
2054 check_poison_obj(cachep, objp);
2055#endif
2056 }
2057 if (cachep->flags & SLAB_RED_ZONE) {
2058 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2059 slab_error(cachep, "start of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002060 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2062 slab_error(cachep, "end of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002063 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002066}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067#else
Rabin Vincente79aec22008-07-04 00:40:32 +05302068static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002069{
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002070}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071#endif
2072
Randy Dunlap911851e2006-03-22 00:08:14 -08002073/**
2074 * slab_destroy - destroy and release all objects in a slab
2075 * @cachep: cache pointer being destroyed
2076 * @slabp: slab pointer being destroyed
2077 *
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002078 * Destroy all the objs in a slab, and release the mem back to the system.
Andrew Mortona737b3e2006-03-22 00:08:11 -08002079 * Before calling the slab must have been unlinked from the cache. The
2080 * cache-lock is not held/needed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002081 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002082static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002083{
2084 void *addr = slabp->s_mem - slabp->colouroff;
2085
Rabin Vincente79aec22008-07-04 00:40:32 +05302086 slab_destroy_debugcheck(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
2088 struct slab_rcu *slab_rcu;
2089
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002090 slab_rcu = (struct slab_rcu *)slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 slab_rcu->cachep = cachep;
2092 slab_rcu->addr = addr;
2093 call_rcu(&slab_rcu->head, kmem_rcu_free);
2094 } else {
2095 kmem_freepages(cachep, addr);
Ingo Molnar873623d2006-07-13 14:44:38 +02002096 if (OFF_SLAB(cachep))
2097 kmem_cache_free(cachep->slabp_cache, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098 }
2099}
2100
2101/**
Randy.Dunlapa70773d2006-02-01 03:05:52 -08002102 * calculate_slab_order - calculate size (page order) of slabs
2103 * @cachep: pointer to the cache that is being created
2104 * @size: size of objects to be created in this cache.
2105 * @align: required alignment for the objects.
2106 * @flags: slab allocation flags
2107 *
2108 * Also calculates the number of objects per slab.
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002109 *
2110 * This could be made much more intelligent. For now, try to avoid using
2111 * high order pages for slabs. When the gfp() functions are more friendly
2112 * towards high-order requests, this should be changed.
2113 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002114static size_t calculate_slab_order(struct kmem_cache *cachep,
Randy Dunlapee13d782006-02-01 03:05:53 -08002115 size_t size, size_t align, unsigned long flags)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002116{
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002117 unsigned long offslab_limit;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002118 size_t left_over = 0;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002119 int gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002120
Christoph Lameter0aa817f2007-05-16 22:11:01 -07002121 for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002122 unsigned int num;
2123 size_t remainder;
2124
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002125 cache_estimate(gfporder, size, align, flags, &remainder, &num);
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002126 if (!num)
2127 continue;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002128
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002129 if (flags & CFLGS_OFF_SLAB) {
2130 /*
2131 * Max number of objs-per-slab for caches which
2132 * use off-slab slabs. Needed to avoid a possible
2133 * looping condition in cache_grow().
2134 */
2135 offslab_limit = size - sizeof(struct slab);
2136 offslab_limit /= sizeof(kmem_bufctl_t);
2137
2138 if (num > offslab_limit)
2139 break;
2140 }
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002141
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002142 /* Found something acceptable - save it away */
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002143 cachep->num = num;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002144 cachep->gfporder = gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002145 left_over = remainder;
2146
2147 /*
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08002148 * A VFS-reclaimable slab tends to have most allocations
2149 * as GFP_NOFS and we really don't want to have to be allocating
2150 * higher-order pages when we are unable to shrink dcache.
2151 */
2152 if (flags & SLAB_RECLAIM_ACCOUNT)
2153 break;
2154
2155 /*
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002156 * Large number of objects is good, but very large slabs are
2157 * currently bad for the gfp()s.
2158 */
David Rientjes543585c2011-10-18 22:09:24 -07002159 if (gfporder >= slab_max_order)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002160 break;
2161
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002162 /*
2163 * Acceptable internal fragmentation?
2164 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002165 if (left_over * 8 <= (PAGE_SIZE << gfporder))
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002166 break;
2167 }
2168 return left_over;
2169}
2170
Pekka Enberg83b519e2009-06-10 19:40:04 +03002171static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002172{
Christoph Lameter97d06602012-07-06 15:25:11 -05002173 if (slab_state >= FULL)
Pekka Enberg83b519e2009-06-10 19:40:04 +03002174 return enable_cpucache(cachep, gfp);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002175
Christoph Lameter97d06602012-07-06 15:25:11 -05002176 if (slab_state == DOWN) {
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002177 /*
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002178 * Note: Creation of first cache (kmem_cache).
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002179 * The setup_node is taken care
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002180 * of by the caller of __kmem_cache_create
2181 */
2182 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2183 slab_state = PARTIAL;
2184 } else if (slab_state == PARTIAL) {
2185 /*
2186 * Note: the second kmem_cache_create must create the cache
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002187 * that's used by kmalloc(24), otherwise the creation of
2188 * further caches will BUG().
2189 */
2190 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2191
2192 /*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002193 * If the cache that's used by kmalloc(sizeof(kmem_cache_node)) is
2194 * the second cache, then we need to set up all its node/,
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002195 * otherwise the creation of further caches will BUG().
2196 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002197 set_up_node(cachep, SIZE_AC);
2198 if (INDEX_AC == INDEX_NODE)
2199 slab_state = PARTIAL_NODE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002200 else
Christoph Lameter97d06602012-07-06 15:25:11 -05002201 slab_state = PARTIAL_ARRAYCACHE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002202 } else {
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002203 /* Remaining boot caches */
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002204 cachep->array[smp_processor_id()] =
Pekka Enberg83b519e2009-06-10 19:40:04 +03002205 kmalloc(sizeof(struct arraycache_init), gfp);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002206
Christoph Lameter97d06602012-07-06 15:25:11 -05002207 if (slab_state == PARTIAL_ARRAYCACHE) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002208 set_up_node(cachep, SIZE_NODE);
2209 slab_state = PARTIAL_NODE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002210 } else {
2211 int node;
Pekka Enberg556a1692008-01-25 08:20:51 +02002212 for_each_online_node(node) {
Christoph Lameter6a673682013-01-10 19:14:19 +00002213 cachep->node[node] =
Christoph Lameter6744f0872013-01-10 19:12:17 +00002214 kmalloc_node(sizeof(struct kmem_cache_node),
Pekka Enbergeb91f1d2009-06-12 14:56:09 +03002215 gfp, node);
Christoph Lameter6a673682013-01-10 19:14:19 +00002216 BUG_ON(!cachep->node[node]);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002217 kmem_cache_node_init(cachep->node[node]);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002218 }
2219 }
2220 }
Christoph Lameter6a673682013-01-10 19:14:19 +00002221 cachep->node[numa_mem_id()]->next_reap =
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002222 jiffies + REAPTIMEOUT_LIST3 +
2223 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
2224
2225 cpu_cache_get(cachep)->avail = 0;
2226 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
2227 cpu_cache_get(cachep)->batchcount = 1;
2228 cpu_cache_get(cachep)->touched = 0;
2229 cachep->batchcount = 1;
2230 cachep->limit = BOOT_CPUCACHE_ENTRIES;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002231 return 0;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002232}
2233
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002234/**
Christoph Lameter039363f2012-07-06 15:25:10 -05002235 * __kmem_cache_create - Create a cache.
Randy Dunlapa755b762012-11-06 17:10:10 -08002236 * @cachep: cache management descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 * @flags: SLAB flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 *
2239 * Returns a ptr to the cache on success, NULL on failure.
2240 * Cannot be called within a int, but can be interrupted.
Paul Mundt20c2df82007-07-20 10:11:58 +09002241 * The @ctor is run when new pages are allocated by the cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 * The flags are
2244 *
2245 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2246 * to catch references to uninitialised memory.
2247 *
2248 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2249 * for buffer overruns.
2250 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2252 * cacheline. This can be beneficial if you're counting cycles as closely
2253 * as davem.
2254 */
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002255int
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002256__kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
2258 size_t left_over, slab_size, ralign;
Pekka Enberg83b519e2009-06-10 19:40:04 +03002259 gfp_t gfp;
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002260 int err;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002261 size_t size = cachep->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264#if FORCED_DEBUG
2265 /*
2266 * Enable redzoning and last user accounting, except for caches with
2267 * large objects, if the increased size would increase the object size
2268 * above the next power of two: caches with object sizes just above a
2269 * power of two have a significant amount of internal fragmentation.
2270 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002271 if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
2272 2 * sizeof(unsigned long long)))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002273 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 if (!(flags & SLAB_DESTROY_BY_RCU))
2275 flags |= SLAB_POISON;
2276#endif
2277 if (flags & SLAB_DESTROY_BY_RCU)
2278 BUG_ON(flags & SLAB_POISON);
2279#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Andrew Mortona737b3e2006-03-22 00:08:11 -08002281 /*
2282 * Check that size is in terms of words. This is needed to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 * unaligned accesses for some archs when redzoning is used, and makes
2284 * sure any on-slab bufctl's are also correctly aligned.
2285 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002286 if (size & (BYTES_PER_WORD - 1)) {
2287 size += (BYTES_PER_WORD - 1);
2288 size &= ~(BYTES_PER_WORD - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 }
2290
Pekka Enbergca5f9702006-09-25 23:31:25 -07002291 /*
David Woodhouse87a927c2007-07-04 21:26:44 -04002292 * Redzoning and user store require word alignment or possibly larger.
2293 * Note this will be overridden by architecture or caller mandated
2294 * alignment if either is greater than BYTES_PER_WORD.
Pekka Enbergca5f9702006-09-25 23:31:25 -07002295 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002296 if (flags & SLAB_STORE_USER)
2297 ralign = BYTES_PER_WORD;
2298
2299 if (flags & SLAB_RED_ZONE) {
2300 ralign = REDZONE_ALIGN;
2301 /* If redzoning, ensure that the second redzone is suitably
2302 * aligned, by adjusting the object size accordingly. */
2303 size += REDZONE_ALIGN - 1;
2304 size &= ~(REDZONE_ALIGN - 1);
2305 }
Pekka Enbergca5f9702006-09-25 23:31:25 -07002306
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002307 /* 3) caller mandated alignment */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002308 if (ralign < cachep->align) {
2309 ralign = cachep->align;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 }
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002311 /* disable debug if necessary */
2312 if (ralign > __alignof__(unsigned long long))
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002313 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002314 /*
Pekka Enbergca5f9702006-09-25 23:31:25 -07002315 * 4) Store it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002317 cachep->align = ralign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318
Pekka Enberg83b519e2009-06-10 19:40:04 +03002319 if (slab_is_available())
2320 gfp = GFP_KERNEL;
2321 else
2322 gfp = GFP_NOWAIT;
2323
Christoph Lameter6a673682013-01-10 19:14:19 +00002324 setup_node_pointer(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Pekka Enbergca5f9702006-09-25 23:31:25 -07002327 /*
2328 * Both debugging options require word-alignment which is calculated
2329 * into align above.
2330 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 if (flags & SLAB_RED_ZONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 /* add space for red zone words */
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002333 cachep->obj_offset += sizeof(unsigned long long);
2334 size += 2 * sizeof(unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
2336 if (flags & SLAB_STORE_USER) {
Pekka Enbergca5f9702006-09-25 23:31:25 -07002337 /* user store requires one word storage behind the end of
David Woodhouse87a927c2007-07-04 21:26:44 -04002338 * the real object. But if the second red zone needs to be
2339 * aligned to 64 bits, we must allow that much space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002341 if (flags & SLAB_RED_ZONE)
2342 size += REDZONE_ALIGN;
2343 else
2344 size += BYTES_PER_WORD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 }
2346#if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002347 if (size >= kmalloc_size(INDEX_NODE + 1)
Tetsuo Handa608da7e2012-09-30 17:28:25 +09002348 && cachep->object_size > cache_line_size()
2349 && ALIGN(size, cachep->align) < PAGE_SIZE) {
2350 cachep->obj_offset += PAGE_SIZE - ALIGN(size, cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 size = PAGE_SIZE;
2352 }
2353#endif
2354#endif
2355
Ingo Molnare0a42722006-06-23 02:03:46 -07002356 /*
2357 * Determine if the slab management is 'on' or 'off' slab.
2358 * (bootstrapping cannot cope with offslab caches so don't do
Catalin Marinase7cb55b2009-10-28 13:33:08 +00002359 * it too early on. Always use on-slab management when
2360 * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
Ingo Molnare0a42722006-06-23 02:03:46 -07002361 */
Catalin Marinase7cb55b2009-10-28 13:33:08 +00002362 if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init &&
2363 !(flags & SLAB_NOLEAKTRACE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 /*
2365 * Size is large, assume best to place the slab management obj
2366 * off-slab (should allow better packing of objs).
2367 */
2368 flags |= CFLGS_OFF_SLAB;
2369
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002370 size = ALIGN(size, cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002372 left_over = calculate_slab_order(cachep, size, cachep->align, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002374 if (!cachep->num)
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002375 return -E2BIG;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002376
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002377 slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002378 + sizeof(struct slab), cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
2380 /*
2381 * If the slab has been placed off-slab, and we have enough space then
2382 * move it on-slab. This is at the expense of any extra colouring.
2383 */
2384 if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
2385 flags &= ~CFLGS_OFF_SLAB;
2386 left_over -= slab_size;
2387 }
2388
2389 if (flags & CFLGS_OFF_SLAB) {
2390 /* really off slab. No need for manual alignment */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002391 slab_size =
2392 cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
Ron Lee67461362009-05-22 04:58:22 +09302393
2394#ifdef CONFIG_PAGE_POISONING
2395 /* If we're going to use the generic kernel_map_pages()
2396 * poisoning, then it's going to smash the contents of
2397 * the redzone and userword anyhow, so switch them off.
2398 */
2399 if (size % PAGE_SIZE == 0 && flags & SLAB_POISON)
2400 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
2401#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 }
2403
2404 cachep->colour_off = cache_line_size();
2405 /* Offset must be a multiple of the alignment. */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002406 if (cachep->colour_off < cachep->align)
2407 cachep->colour_off = cachep->align;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002408 cachep->colour = left_over / cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 cachep->slab_size = slab_size;
2410 cachep->flags = flags;
Glauber Costaa618e892012-06-14 16:17:21 +04002411 cachep->allocflags = 0;
Christoph Lameter4b51d662007-02-10 01:43:10 -08002412 if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
Glauber Costaa618e892012-06-14 16:17:21 +04002413 cachep->allocflags |= GFP_DMA;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002414 cachep->size = size;
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08002415 cachep->reciprocal_buffer_size = reciprocal_value(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002417 if (flags & CFLGS_OFF_SLAB) {
Christoph Lameter2c59dd62013-01-10 19:14:19 +00002418 cachep->slabp_cache = kmalloc_slab(slab_size, 0u);
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002419 /*
2420 * This is a possibility for one of the malloc_sizes caches.
2421 * But since we go off slab only for object size greater than
2422 * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
2423 * this should not happen at all.
2424 * But leave a BUG_ON for some lucky dude.
2425 */
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002426 BUG_ON(ZERO_OR_NULL_PTR(cachep->slabp_cache));
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002429 err = setup_cpu_cache(cachep, gfp);
2430 if (err) {
Christoph Lameter12c36672012-09-04 23:38:33 +00002431 __kmem_cache_shutdown(cachep);
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002432 return err;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
Peter Zijlstra83835b32011-07-22 15:26:05 +02002435 if (flags & SLAB_DEBUG_OBJECTS) {
2436 /*
2437 * Would deadlock through slab_destroy()->call_rcu()->
2438 * debug_object_activate()->kmem_cache_alloc().
2439 */
2440 WARN_ON_ONCE(flags & SLAB_DESTROY_BY_RCU);
2441
2442 slab_set_debugobj_lock_classes(cachep);
Glauber Costa6ccfb5b2012-12-18 14:22:31 -08002443 } else if (!OFF_SLAB(cachep) && !(flags & SLAB_DESTROY_BY_RCU))
2444 on_slab_lock_classes(cachep);
Peter Zijlstra83835b32011-07-22 15:26:05 +02002445
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002446 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
2449#if DEBUG
2450static void check_irq_off(void)
2451{
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002452 BUG_ON_NONRT(!irqs_disabled());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
2454
2455static void check_irq_on(void)
2456{
2457 BUG_ON(irqs_disabled());
2458}
2459
Pekka Enberg343e0d72006-02-01 03:05:50 -08002460static void check_spinlock_acquired(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461{
2462#ifdef CONFIG_SMP
2463 check_irq_off();
Christoph Lameter6a673682013-01-10 19:14:19 +00002464 assert_spin_locked(&cachep->node[numa_mem_id()]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465#endif
2466}
Christoph Lametere498be72005-09-09 13:03:32 -07002467
Pekka Enberg343e0d72006-02-01 03:05:50 -08002468static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07002469{
2470#ifdef CONFIG_SMP
2471 check_irq_off();
Christoph Lameter6a673682013-01-10 19:14:19 +00002472 assert_spin_locked(&cachep->node[node]->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002473#endif
2474}
2475
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476#else
2477#define check_irq_off() do { } while(0)
2478#define check_irq_on() do { } while(0)
2479#define check_spinlock_acquired(x) do { } while(0)
Christoph Lametere498be72005-09-09 13:03:32 -07002480#define check_spinlock_acquired_node(x, y) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481#endif
2482
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002483static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
Christoph Lameteraab22072006-03-22 00:09:06 -08002484 struct array_cache *ac,
2485 int force, int node);
2486
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002487static void __do_drain(void *arg, unsigned int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
Andrew Mortona737b3e2006-03-22 00:08:11 -08002489 struct kmem_cache *cachep = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 struct array_cache *ac;
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002491 int node = cpu_to_mem(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002493 ac = cpu_cache_get_on_cpu(cachep, cpu);
Christoph Lameter6a673682013-01-10 19:14:19 +00002494 spin_lock(&cachep->node[node]->list_lock);
Christoph Lameterff694162005-09-22 21:44:02 -07002495 free_block(cachep, ac->entry, ac->avail, node);
Christoph Lameter6a673682013-01-10 19:14:19 +00002496 spin_unlock(&cachep->node[node]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 ac->avail = 0;
2498}
2499
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002500#ifndef CONFIG_PREEMPT_RT_BASE
2501static void do_drain(void *arg)
2502{
2503 __do_drain(arg, smp_processor_id());
2504}
2505#else
2506static void do_drain(void *arg, int this_cpu)
2507{
2508 __do_drain(arg, this_cpu);
2509}
2510#endif
2511
Pekka Enberg343e0d72006-02-01 03:05:50 -08002512static void drain_cpu_caches(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002514 struct kmem_cache_node *n;
Christoph Lametere498be72005-09-09 13:03:32 -07002515 int node;
2516
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002517 slab_on_each_cpu(do_drain, cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 check_irq_on();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002519 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002520 n = cachep->node[node];
2521 if (n && n->alien)
2522 drain_alien_cache(cachep, n->alien);
Roland Dreiera4523a82006-05-15 11:41:00 -07002523 }
2524
2525 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002526 n = cachep->node[node];
2527 if (n)
2528 drain_array(cachep, n, n->shared, 1, node);
Christoph Lametere498be72005-09-09 13:03:32 -07002529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530}
2531
Christoph Lametered11d9e2006-06-30 01:55:45 -07002532/*
2533 * Remove slabs from the list of free slabs.
2534 * Specify the number of slabs to drain in tofree.
2535 *
2536 * Returns the actual number of slabs released.
2537 */
2538static int drain_freelist(struct kmem_cache *cache,
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002539 struct kmem_cache_node *n, int tofree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540{
Christoph Lametered11d9e2006-06-30 01:55:45 -07002541 struct list_head *p;
2542 int nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Christoph Lametered11d9e2006-06-30 01:55:45 -07002545 nr_freed = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002546 while (nr_freed < tofree && !list_empty(&n->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002548 local_spin_lock_irq(slab_lock, &n->list_lock);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002549 p = n->slabs_free.prev;
2550 if (p == &n->slabs_free) {
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002551 local_spin_unlock_irq(slab_lock, &n->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002552 goto out;
2553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554
Christoph Lametered11d9e2006-06-30 01:55:45 -07002555 slabp = list_entry(p, struct slab, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556#if DEBUG
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002557 BUG_ON(slabp->inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558#endif
2559 list_del(&slabp->list);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002560 /*
2561 * Safe to drop the lock. The slab is no longer linked
2562 * to the cache.
2563 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002564 n->free_objects -= cache->num;
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002565 local_spin_unlock_irq(slab_lock, &n->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002566 slab_destroy(cache, slabp);
2567 nr_freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 }
Christoph Lametered11d9e2006-06-30 01:55:45 -07002569out:
2570 return nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571}
2572
Christoph Lameter18004c52012-07-06 15:25:12 -05002573/* Called with slab_mutex held to protect against cpu hotplug */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002574static int __cache_shrink(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07002575{
2576 int ret = 0, i = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002577 struct kmem_cache_node *n;
Christoph Lametere498be72005-09-09 13:03:32 -07002578
2579 drain_cpu_caches(cachep);
2580
2581 check_irq_on();
2582 for_each_online_node(i) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002583 n = cachep->node[i];
2584 if (!n)
Christoph Lametered11d9e2006-06-30 01:55:45 -07002585 continue;
2586
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002587 drain_freelist(cachep, n, n->free_objects);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002588
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002589 ret += !list_empty(&n->slabs_full) ||
2590 !list_empty(&n->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07002591 }
2592 return (ret ? 1 : 0);
2593}
2594
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595/**
2596 * kmem_cache_shrink - Shrink a cache.
2597 * @cachep: The cache to shrink.
2598 *
2599 * Releases as many slabs as possible for a cache.
2600 * To help debugging, a zero exit status indicates all slabs were released.
2601 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002602int kmem_cache_shrink(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002604 int ret;
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002605 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002607 get_online_cpus();
Christoph Lameter18004c52012-07-06 15:25:12 -05002608 mutex_lock(&slab_mutex);
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002609 ret = __cache_shrink(cachep);
Christoph Lameter18004c52012-07-06 15:25:12 -05002610 mutex_unlock(&slab_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002611 put_online_cpus();
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002612 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613}
2614EXPORT_SYMBOL(kmem_cache_shrink);
2615
Christoph Lameter945cf2b2012-09-04 23:18:33 +00002616int __kmem_cache_shutdown(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617{
Christoph Lameter12c36672012-09-04 23:38:33 +00002618 int i;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002619 struct kmem_cache_node *n;
Christoph Lameter12c36672012-09-04 23:38:33 +00002620 int rc = __cache_shrink(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
Christoph Lameter12c36672012-09-04 23:38:33 +00002622 if (rc)
2623 return rc;
2624
2625 for_each_online_cpu(i)
2626 kfree(cachep->array[i]);
2627
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002628 /* NUMA: free the node structures */
Christoph Lameter12c36672012-09-04 23:38:33 +00002629 for_each_online_node(i) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002630 n = cachep->node[i];
2631 if (n) {
2632 kfree(n->shared);
2633 free_alien_cache(n->alien);
2634 kfree(n);
Christoph Lameter12c36672012-09-04 23:38:33 +00002635 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 }
Christoph Lameter12c36672012-09-04 23:38:33 +00002637 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002640/*
2641 * Get the memory for a slab management obj.
2642 * For a slab cache when the slab descriptor is off-slab, slab descriptors
2643 * always come from malloc_sizes caches. The slab descriptor cannot
2644 * come from the same cache which is getting created because,
2645 * when we are searching for an appropriate cache for these
2646 * descriptors in kmem_cache_create, we search through the malloc_sizes array.
2647 * If we are creating a malloc_sizes cache here it would not be visible to
2648 * kmem_find_general_cachep till the initialization is complete.
2649 * Hence we cannot have slabp_cache same as the original cache.
2650 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002651static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002652 int colour_off, gfp_t local_flags,
2653 int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654{
2655 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002656
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657 if (OFF_SLAB(cachep)) {
2658 /* Slab management obj is off-slab. */
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002659 slabp = kmem_cache_alloc_node(cachep->slabp_cache,
Pekka Enberg8759ec52008-11-26 10:01:31 +02002660 local_flags, nodeid);
Catalin Marinasd5cff632009-06-11 13:22:40 +01002661 /*
2662 * If the first object in the slab is leaked (it's allocated
2663 * but no one has a reference to it), we want to make sure
2664 * kmemleak does not treat the ->s_mem pointer as a reference
2665 * to the object. Otherwise we will not report the leak.
2666 */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002667 kmemleak_scan_area(&slabp->list, sizeof(struct list_head),
2668 local_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 if (!slabp)
2670 return NULL;
2671 } else {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002672 slabp = objp + colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 colour_off += cachep->slab_size;
2674 }
2675 slabp->inuse = 0;
2676 slabp->colouroff = colour_off;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002677 slabp->s_mem = objp + colour_off;
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002678 slabp->nodeid = nodeid;
Marcin Slusarze51bfd02008-02-10 11:21:54 +01002679 slabp->free = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 return slabp;
2681}
2682
2683static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
2684{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002685 return (kmem_bufctl_t *) (slabp + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686}
2687
Pekka Enberg343e0d72006-02-01 03:05:50 -08002688static void cache_init_objs(struct kmem_cache *cachep,
Christoph Lametera35afb82007-05-16 22:10:57 -07002689 struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690{
2691 int i;
2692
2693 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002694 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695#if DEBUG
2696 /* need to poison the objs? */
2697 if (cachep->flags & SLAB_POISON)
2698 poison_obj(cachep, objp, POISON_FREE);
2699 if (cachep->flags & SLAB_STORE_USER)
2700 *dbg_userword(cachep, objp) = NULL;
2701
2702 if (cachep->flags & SLAB_RED_ZONE) {
2703 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2704 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2705 }
2706 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002707 * Constructors are not allowed to allocate memory from the same
2708 * cache which they are a constructor for. Otherwise, deadlock.
2709 * They must also be threaded.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 */
2711 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002712 cachep->ctor(objp + obj_offset(cachep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
2714 if (cachep->flags & SLAB_RED_ZONE) {
2715 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2716 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002717 " end of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2719 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002720 " start of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 }
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002722 if ((cachep->size % PAGE_SIZE) == 0 &&
Andrew Mortona737b3e2006-03-22 00:08:11 -08002723 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002724 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002725 cachep->size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726#else
2727 if (cachep->ctor)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002728 cachep->ctor(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729#endif
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002730 slab_bufctl(slabp)[i] = i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002732 slab_bufctl(slabp)[i - 1] = BUFCTL_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733}
2734
Pekka Enberg343e0d72006-02-01 03:05:50 -08002735static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736{
Christoph Lameter4b51d662007-02-10 01:43:10 -08002737 if (CONFIG_ZONE_DMA_FLAG) {
2738 if (flags & GFP_DMA)
Glauber Costaa618e892012-06-14 16:17:21 +04002739 BUG_ON(!(cachep->allocflags & GFP_DMA));
Christoph Lameter4b51d662007-02-10 01:43:10 -08002740 else
Glauber Costaa618e892012-06-14 16:17:21 +04002741 BUG_ON(cachep->allocflags & GFP_DMA);
Christoph Lameter4b51d662007-02-10 01:43:10 -08002742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743}
2744
Andrew Mortona737b3e2006-03-22 00:08:11 -08002745static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
2746 int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002747{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002748 void *objp = index_to_obj(cachep, slabp, slabp->free);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002749 kmem_bufctl_t next;
2750
2751 slabp->inuse++;
2752 next = slab_bufctl(slabp)[slabp->free];
2753#if DEBUG
2754 slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
2755 WARN_ON(slabp->nodeid != nodeid);
2756#endif
2757 slabp->free = next;
2758
2759 return objp;
2760}
2761
Andrew Mortona737b3e2006-03-22 00:08:11 -08002762static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
2763 void *objp, int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002764{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002765 unsigned int objnr = obj_to_index(cachep, slabp, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002766
2767#if DEBUG
2768 /* Verify that the slab belongs to the intended node */
2769 WARN_ON(slabp->nodeid != nodeid);
2770
Al Viro871751e2006-03-25 03:06:39 -08002771 if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) {
Matthew Dobson78d382d2006-02-01 03:05:47 -08002772 printk(KERN_ERR "slab: double free detected in cache "
Andrew Mortona737b3e2006-03-22 00:08:11 -08002773 "'%s', objp %p\n", cachep->name, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002774 BUG();
2775 }
2776#endif
2777 slab_bufctl(slabp)[objnr] = slabp->free;
2778 slabp->free = objnr;
2779 slabp->inuse--;
2780}
2781
Pekka Enberg47768742006-06-23 02:03:07 -07002782/*
2783 * Map pages beginning at addr to the given cache and slab. This is required
2784 * for the slab allocator to be able to lookup the cache and slab of a
Nick Pigginccd35fb2011-01-07 17:49:17 +11002785 * virtual address for kfree, ksize, and slab debugging.
Pekka Enberg47768742006-06-23 02:03:07 -07002786 */
2787static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
2788 void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789{
Pekka Enberg47768742006-06-23 02:03:07 -07002790 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 struct page *page;
2792
Pekka Enberg47768742006-06-23 02:03:07 -07002793 page = virt_to_page(addr);
Nick Piggin84097512006-03-22 00:08:34 -08002794
Pekka Enberg47768742006-06-23 02:03:07 -07002795 nr_pages = 1;
Nick Piggin84097512006-03-22 00:08:34 -08002796 if (likely(!PageCompound(page)))
Pekka Enberg47768742006-06-23 02:03:07 -07002797 nr_pages <<= cache->gfporder;
2798
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 do {
Christoph Lameter35026082012-06-13 10:24:56 -05002800 page->slab_cache = cache;
2801 page->slab_page = slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 page++;
Pekka Enberg47768742006-06-23 02:03:07 -07002803 } while (--nr_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804}
2805
2806/*
2807 * Grow (by 1) the number of slabs within a cache. This is called by
2808 * kmem_cache_alloc() when there are no active objs left in a cache.
2809 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002810static int cache_grow(struct kmem_cache *cachep,
2811 gfp_t flags, int nodeid, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002813 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002814 size_t offset;
2815 gfp_t local_flags;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002816 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Andrew Mortona737b3e2006-03-22 00:08:11 -08002818 /*
2819 * Be lazy and only check for valid flags here, keeping it out of the
2820 * critical path in kmem_cache_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002821 */
Christoph Lameter6cb06222007-10-16 01:25:41 -07002822 BUG_ON(flags & GFP_SLAB_BUG_MASK);
2823 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002825 /* Take the node list lock to change the colour_next on this node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002827 n = cachep->node[nodeid];
2828 spin_lock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829
2830 /* Get colour for the slab, and cal the next value. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002831 offset = n->colour_next;
2832 n->colour_next++;
2833 if (n->colour_next >= cachep->colour)
2834 n->colour_next = 0;
2835 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002837 offset *= cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
2839 if (local_flags & __GFP_WAIT)
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002840 local_unlock_irq(slab_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
2842 /*
2843 * The test for missing atomic flag is performed here, rather than
2844 * the more obvious place, simply to reduce the critical path length
2845 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2846 * will eventually be caught here (where it matters).
2847 */
2848 kmem_flagcheck(cachep, flags);
2849
Andrew Mortona737b3e2006-03-22 00:08:11 -08002850 /*
2851 * Get mem for the objs. Attempt to allocate a physical page from
2852 * 'nodeid'.
Christoph Lametere498be72005-09-09 13:03:32 -07002853 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002854 if (!objp)
Andrew Mortonb8c1c5d2007-07-24 12:02:40 -07002855 objp = kmem_getpages(cachep, local_flags, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002856 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 goto failed;
2858
2859 /* Get slab management. */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002860 slabp = alloc_slabmgmt(cachep, objp, offset,
Christoph Lameter6cb06222007-10-16 01:25:41 -07002861 local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002862 if (!slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 goto opps1;
2864
Pekka Enberg47768742006-06-23 02:03:07 -07002865 slab_map_pages(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Christoph Lametera35afb82007-05-16 22:10:57 -07002867 cache_init_objs(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
2869 if (local_flags & __GFP_WAIT)
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002870 local_lock_irq(slab_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002872 spin_lock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873
2874 /* Make slab active. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002875 list_add_tail(&slabp->list, &(n->slabs_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 STATS_INC_GROWN(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002877 n->free_objects += cachep->num;
2878 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002880opps1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 kmem_freepages(cachep, objp);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002882failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 if (local_flags & __GFP_WAIT)
Thomas Gleixner4a621b32011-06-18 19:44:43 +02002884 local_lock_irq(slab_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 return 0;
2886}
2887
2888#if DEBUG
2889
2890/*
2891 * Perform extra freeing checks:
2892 * - detect bad pointers.
2893 * - POISON/RED_ZONE checking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894 */
2895static void kfree_debugcheck(const void *objp)
2896{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 if (!virt_addr_valid(objp)) {
2898 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002899 (unsigned long)objp);
2900 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902}
2903
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002904static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2905{
David Woodhouseb46b8f12007-05-08 00:22:59 -07002906 unsigned long long redzone1, redzone2;
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002907
2908 redzone1 = *dbg_redzone1(cache, obj);
2909 redzone2 = *dbg_redzone2(cache, obj);
2910
2911 /*
2912 * Redzone is ok.
2913 */
2914 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2915 return;
2916
2917 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2918 slab_error(cache, "double free detected");
2919 else
2920 slab_error(cache, "memory outside object was overwritten");
2921
David Woodhouseb46b8f12007-05-08 00:22:59 -07002922 printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002923 obj, redzone1, redzone2);
2924}
2925
Pekka Enberg343e0d72006-02-01 03:05:50 -08002926static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002927 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
2929 struct page *page;
2930 unsigned int objnr;
2931 struct slab *slabp;
2932
Matthew Wilcox80cbd912007-11-29 12:05:13 -07002933 BUG_ON(virt_to_cache(objp) != cachep);
2934
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002935 objp -= obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 kfree_debugcheck(objp);
Christoph Lameterb49af682007-05-06 14:49:41 -07002937 page = virt_to_head_page(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Christoph Lameter35026082012-06-13 10:24:56 -05002939 slabp = page->slab_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
2941 if (cachep->flags & SLAB_RED_ZONE) {
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002942 verify_redzone_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2944 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2945 }
2946 if (cachep->flags & SLAB_STORE_USER)
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002947 *dbg_userword(cachep, objp) = (void *)caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002949 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
2951 BUG_ON(objnr >= cachep->num);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002952 BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Al Viro871751e2006-03-25 03:06:39 -08002954#ifdef CONFIG_DEBUG_SLAB_LEAK
2955 slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
2956#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 if (cachep->flags & SLAB_POISON) {
2958#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002959 if ((cachep->size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002960 store_stackinfo(cachep, objp, caller);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002961 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002962 cachep->size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 } else {
2964 poison_obj(cachep, objp, POISON_FREE);
2965 }
2966#else
2967 poison_obj(cachep, objp, POISON_FREE);
2968#endif
2969 }
2970 return objp;
2971}
2972
Pekka Enberg343e0d72006-02-01 03:05:50 -08002973static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974{
2975 kmem_bufctl_t i;
2976 int entries = 0;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002977
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 /* Check slab's freelist to see if this obj is there. */
2979 for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
2980 entries++;
2981 if (entries > cachep->num || i >= cachep->num)
2982 goto bad;
2983 }
2984 if (entries != cachep->num - slabp->inuse) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002985bad:
2986 printk(KERN_ERR "slab: Internal list corruption detected in "
Dave Jonesface37f2011-11-15 15:03:52 -08002987 "cache '%s'(%d), slabp %p(%d). Tainted(%s). Hexdump:\n",
2988 cachep->name, cachep->num, slabp, slabp->inuse,
2989 print_tainted());
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02002990 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, slabp,
2991 sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t),
2992 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 BUG();
2994 }
2995}
2996#else
2997#define kfree_debugcheck(x) do { } while(0)
2998#define cache_free_debugcheck(x,objp,z) (objp)
2999#define check_slabp(x,y) do { } while(0)
3000#endif
3001
Mel Gorman072bb0a2012-07-31 16:43:58 -07003002static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags,
3003 bool force_refill)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004{
3005 int batchcount;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003006 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 struct array_cache *ac;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07003008 int node;
3009
Joe Korty6d2144d2008-03-05 15:04:59 -08003010 check_irq_off();
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003011 node = numa_mem_id();
Mel Gorman072bb0a2012-07-31 16:43:58 -07003012 if (unlikely(force_refill))
3013 goto force_grow;
3014retry:
Joe Korty6d2144d2008-03-05 15:04:59 -08003015 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 batchcount = ac->batchcount;
3017 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003018 /*
3019 * If there was little recent activity on this cache, then
3020 * perform only a partial refill. Otherwise we could generate
3021 * refill bouncing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 */
3023 batchcount = BATCHREFILL_LIMIT;
3024 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003025 n = cachep->node[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003027 BUG_ON(ac->avail > 0 || !n);
3028 spin_lock(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07003029
Christoph Lameter3ded1752006-03-25 03:06:44 -08003030 /* See if we can refill from the shared array */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003031 if (n->shared && transfer_objects(ac, n->shared, batchcount)) {
3032 n->shared->touched = 1;
Christoph Lameter3ded1752006-03-25 03:06:44 -08003033 goto alloc_done;
Nick Piggin44b57f12010-01-27 22:27:40 +11003034 }
Christoph Lameter3ded1752006-03-25 03:06:44 -08003035
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 while (batchcount > 0) {
3037 struct list_head *entry;
3038 struct slab *slabp;
3039 /* Get slab alloc is to come from. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003040 entry = n->slabs_partial.next;
3041 if (entry == &n->slabs_partial) {
3042 n->free_touched = 1;
3043 entry = n->slabs_free.next;
3044 if (entry == &n->slabs_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 goto must_grow;
3046 }
3047
3048 slabp = list_entry(entry, struct slab, list);
3049 check_slabp(cachep, slabp);
3050 check_spinlock_acquired(cachep);
Pekka Enberg714b81712007-05-06 14:49:03 -07003051
3052 /*
3053 * The slab was either on partial or free list so
3054 * there must be at least one object available for
3055 * allocation.
3056 */
roel kluin249b9f32008-10-29 17:18:07 -04003057 BUG_ON(slabp->inuse >= cachep->num);
Pekka Enberg714b81712007-05-06 14:49:03 -07003058
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 while (slabp->inuse < cachep->num && batchcount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 STATS_INC_ALLOCED(cachep);
3061 STATS_INC_ACTIVE(cachep);
3062 STATS_SET_HIGH(cachep);
3063
Mel Gorman072bb0a2012-07-31 16:43:58 -07003064 ac_put_obj(cachep, ac, slab_get_obj(cachep, slabp,
3065 node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 }
3067 check_slabp(cachep, slabp);
3068
3069 /* move slabp to correct slabp list: */
3070 list_del(&slabp->list);
3071 if (slabp->free == BUFCTL_END)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003072 list_add(&slabp->list, &n->slabs_full);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 else
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003074 list_add(&slabp->list, &n->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 }
3076
Andrew Mortona737b3e2006-03-22 00:08:11 -08003077must_grow:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003078 n->free_objects -= ac->avail;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003079alloc_done:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003080 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
3082 if (unlikely(!ac->avail)) {
3083 int x;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003084force_grow:
Christoph Lameter3c517a62006-12-06 20:33:29 -08003085 x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07003086
Andrew Mortona737b3e2006-03-22 00:08:11 -08003087 /* cache_grow can reenable interrupts, then ac could change. */
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003088 ac = cpu_cache_get(cachep);
David Rientjes51cd8e62012-08-28 19:57:21 -07003089 node = numa_mem_id();
Mel Gorman072bb0a2012-07-31 16:43:58 -07003090
3091 /* no objects in sight? abort */
3092 if (!x && (ac->avail == 0 || force_refill))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 return NULL;
3094
Andrew Mortona737b3e2006-03-22 00:08:11 -08003095 if (!ac->avail) /* objects refilled by interrupt? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 goto retry;
3097 }
3098 ac->touched = 1;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003099
3100 return ac_get_obj(cachep, ac, flags, force_refill);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101}
3102
Andrew Mortona737b3e2006-03-22 00:08:11 -08003103static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
3104 gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105{
3106 might_sleep_if(flags & __GFP_WAIT);
3107#if DEBUG
3108 kmem_flagcheck(cachep, flags);
3109#endif
3110}
3111
3112#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003113static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003114 gfp_t flags, void *objp, unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003116 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 return objp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003118 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003120 if ((cachep->size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003121 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003122 cachep->size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 else
3124 check_poison_obj(cachep, objp);
3125#else
3126 check_poison_obj(cachep, objp);
3127#endif
3128 poison_obj(cachep, objp, POISON_INUSE);
3129 }
3130 if (cachep->flags & SLAB_STORE_USER)
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003131 *dbg_userword(cachep, objp) = (void *)caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
3133 if (cachep->flags & SLAB_RED_ZONE) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003134 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
3135 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
3136 slab_error(cachep, "double free, or memory outside"
3137 " object was overwritten");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003138 printk(KERN_ERR
David Woodhouseb46b8f12007-05-08 00:22:59 -07003139 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08003140 objp, *dbg_redzone1(cachep, objp),
3141 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 }
3143 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
3144 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
3145 }
Al Viro871751e2006-03-25 03:06:39 -08003146#ifdef CONFIG_DEBUG_SLAB_LEAK
3147 {
3148 struct slab *slabp;
3149 unsigned objnr;
3150
Christoph Lameter35026082012-06-13 10:24:56 -05003151 slabp = virt_to_head_page(objp)->slab_page;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003152 objnr = (unsigned)(objp - slabp->s_mem) / cachep->size;
Al Viro871751e2006-03-25 03:06:39 -08003153 slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE;
3154 }
3155#endif
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003156 objp += obj_offset(cachep);
Christoph Lameter4f104932007-05-06 14:50:17 -07003157 if (cachep->ctor && cachep->flags & SLAB_POISON)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003158 cachep->ctor(objp);
Tetsuo Handa7ea466f2011-07-21 09:42:45 +09003159 if (ARCH_SLAB_MINALIGN &&
3160 ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003161 printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
Hugh Dickinsc2251502011-07-11 13:35:08 -07003162 objp, (int)ARCH_SLAB_MINALIGN);
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003163 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164 return objp;
3165}
3166#else
3167#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
3168#endif
3169
Akinobu Mita773ff602008-12-23 19:37:01 +09003170static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags)
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003171{
Christoph Lameter9b030cb2012-09-05 00:20:33 +00003172 if (cachep == kmem_cache)
Akinobu Mita773ff602008-12-23 19:37:01 +09003173 return false;
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003174
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003175 return should_failslab(cachep->object_size, flags, cachep->flags);
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003176}
3177
Pekka Enberg343e0d72006-02-01 03:05:50 -08003178static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003180 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 struct array_cache *ac;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003182 bool force_refill = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
Alok N Kataria5c382302005-09-27 21:45:46 -07003184 check_irq_off();
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003185
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003186 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 if (likely(ac->avail)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 ac->touched = 1;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003189 objp = ac_get_obj(cachep, ac, flags, false);
3190
J. R. Okajimaddbf2e82009-12-02 16:55:50 +09003191 /*
Mel Gorman072bb0a2012-07-31 16:43:58 -07003192 * Allow for the possibility all avail objects are not allowed
3193 * by the current flags
J. R. Okajimaddbf2e82009-12-02 16:55:50 +09003194 */
Mel Gorman072bb0a2012-07-31 16:43:58 -07003195 if (objp) {
3196 STATS_INC_ALLOCHIT(cachep);
3197 goto out;
3198 }
3199 force_refill = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 }
Mel Gorman072bb0a2012-07-31 16:43:58 -07003201
3202 STATS_INC_ALLOCMISS(cachep);
3203 objp = cache_alloc_refill(cachep, flags, force_refill);
3204 /*
3205 * the 'ac' may be updated by cache_alloc_refill(),
3206 * and kmemleak_erase() requires its correct value.
3207 */
3208 ac = cpu_cache_get(cachep);
3209
3210out:
Catalin Marinasd5cff632009-06-11 13:22:40 +01003211 /*
3212 * To avoid a false negative, if an object that is in one of the
3213 * per-CPU caches is leaked, we need to make sure kmemleak doesn't
3214 * treat the array pointers as a reference to the object.
3215 */
J. R. Okajimaf3d8b532009-12-02 16:55:49 +09003216 if (objp)
3217 kmemleak_erase(&ac->entry[ac->avail]);
Alok N Kataria5c382302005-09-27 21:45:46 -07003218 return objp;
3219}
3220
Christoph Lametere498be72005-09-09 13:03:32 -07003221#ifdef CONFIG_NUMA
3222/*
Paul Jacksonb2455392006-03-24 03:16:12 -08003223 * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
Paul Jacksonc61afb12006-03-24 03:16:08 -08003224 *
3225 * If we are in_interrupt, then process context, including cpusets and
3226 * mempolicy, may not apply and should not be used for allocation policy.
3227 */
3228static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
3229{
3230 int nid_alloc, nid_here;
3231
Christoph Lameter765c4502006-09-27 01:50:08 -07003232 if (in_interrupt() || (flags & __GFP_THISNODE))
Paul Jacksonc61afb12006-03-24 03:16:08 -08003233 return NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003234 nid_alloc = nid_here = numa_mem_id();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003235 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
Jack Steiner6adef3e2010-05-26 14:42:49 -07003236 nid_alloc = cpuset_slab_spread_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003237 else if (current->mempolicy)
Andi Kleene7b691b2012-06-09 02:40:03 -07003238 nid_alloc = slab_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003239 if (nid_alloc != nid_here)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003240 return ____cache_alloc_node(cachep, flags, nid_alloc);
Paul Jacksonc61afb12006-03-24 03:16:08 -08003241 return NULL;
3242}
3243
3244/*
Christoph Lameter765c4502006-09-27 01:50:08 -07003245 * Fallback function if there was no memory available and no objects on a
Christoph Lameter3c517a62006-12-06 20:33:29 -08003246 * certain node and fall back is permitted. First we scan all the
Christoph Lameter6a673682013-01-10 19:14:19 +00003247 * available node for available objects. If that fails then we
Christoph Lameter3c517a62006-12-06 20:33:29 -08003248 * perform an allocation without specifying a node. This allows the page
3249 * allocator to do its reclaim / fallback magic. We then insert the
3250 * slab into the proper nodelist and then allocate from it.
Christoph Lameter765c4502006-09-27 01:50:08 -07003251 */
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003252static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
Christoph Lameter765c4502006-09-27 01:50:08 -07003253{
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003254 struct zonelist *zonelist;
3255 gfp_t local_flags;
Mel Gormandd1a2392008-04-28 02:12:17 -07003256 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07003257 struct zone *zone;
3258 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003259 void *obj = NULL;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003260 int nid;
Mel Gormancc9a6c82012-03-21 16:34:11 -07003261 unsigned int cpuset_mems_cookie;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003262
3263 if (flags & __GFP_THISNODE)
3264 return NULL;
3265
Christoph Lameter6cb06222007-10-16 01:25:41 -07003266 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Christoph Lameter765c4502006-09-27 01:50:08 -07003267
Mel Gormancc9a6c82012-03-21 16:34:11 -07003268retry_cpuset:
3269 cpuset_mems_cookie = get_mems_allowed();
Andi Kleene7b691b2012-06-09 02:40:03 -07003270 zonelist = node_zonelist(slab_node(), flags);
Mel Gormancc9a6c82012-03-21 16:34:11 -07003271
Christoph Lameter3c517a62006-12-06 20:33:29 -08003272retry:
3273 /*
3274 * Look through allowed nodes for objects available
3275 * from existing per node queues.
3276 */
Mel Gorman54a6eb52008-04-28 02:12:16 -07003277 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
3278 nid = zone_to_nid(zone);
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003279
Mel Gorman54a6eb52008-04-28 02:12:16 -07003280 if (cpuset_zone_allowed_hardwall(zone, flags) &&
Christoph Lameter6a673682013-01-10 19:14:19 +00003281 cache->node[nid] &&
3282 cache->node[nid]->free_objects) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003283 obj = ____cache_alloc_node(cache,
3284 flags | GFP_THISNODE, nid);
Christoph Lameter481c5342008-06-21 16:46:35 -07003285 if (obj)
3286 break;
3287 }
Christoph Lameter3c517a62006-12-06 20:33:29 -08003288 }
3289
Christoph Lametercfce6602007-05-06 14:50:17 -07003290 if (!obj) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003291 /*
3292 * This allocation will be performed within the constraints
3293 * of the current cpuset / memory policy requirements.
3294 * We may trigger various forms of reclaim on the allowed
3295 * set and go into memory reserves if necessary.
3296 */
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003297 if (local_flags & __GFP_WAIT)
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003298 local_unlock_irq(slab_lock);
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003299 kmem_flagcheck(cache, flags);
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003300 obj = kmem_getpages(cache, local_flags, numa_mem_id());
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003301 if (local_flags & __GFP_WAIT)
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003302 local_lock_irq(slab_lock);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003303 if (obj) {
3304 /*
3305 * Insert into the appropriate per node queues
3306 */
3307 nid = page_to_nid(virt_to_page(obj));
3308 if (cache_grow(cache, flags, nid, obj)) {
3309 obj = ____cache_alloc_node(cache,
3310 flags | GFP_THISNODE, nid);
3311 if (!obj)
3312 /*
3313 * Another processor may allocate the
3314 * objects in the slab since we are
3315 * not holding any locks.
3316 */
3317 goto retry;
3318 } else {
Hugh Dickinsb6a60452007-01-05 16:36:36 -08003319 /* cache_grow already freed obj */
Christoph Lameter3c517a62006-12-06 20:33:29 -08003320 obj = NULL;
3321 }
3322 }
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003323 }
Mel Gormancc9a6c82012-03-21 16:34:11 -07003324
3325 if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !obj))
3326 goto retry_cpuset;
Christoph Lameter765c4502006-09-27 01:50:08 -07003327 return obj;
3328}
3329
3330/*
Christoph Lametere498be72005-09-09 13:03:32 -07003331 * A interface to enable slab creation on nodeid
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003333static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003334 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07003335{
3336 struct list_head *entry;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003337 struct slab *slabp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003338 struct kmem_cache_node *n;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003339 void *obj;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003340 int x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
Aaron Tomlin14e50c62013-04-26 16:15:34 +01003342 VM_BUG_ON(nodeid > num_online_nodes());
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003343 n = cachep->node[nodeid];
3344 BUG_ON(!n);
Christoph Lametere498be72005-09-09 13:03:32 -07003345
Andrew Mortona737b3e2006-03-22 00:08:11 -08003346retry:
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003347 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003348 spin_lock(&n->list_lock);
3349 entry = n->slabs_partial.next;
3350 if (entry == &n->slabs_partial) {
3351 n->free_touched = 1;
3352 entry = n->slabs_free.next;
3353 if (entry == &n->slabs_free)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003354 goto must_grow;
3355 }
Christoph Lametere498be72005-09-09 13:03:32 -07003356
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003357 slabp = list_entry(entry, struct slab, list);
3358 check_spinlock_acquired_node(cachep, nodeid);
3359 check_slabp(cachep, slabp);
Christoph Lametere498be72005-09-09 13:03:32 -07003360
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003361 STATS_INC_NODEALLOCS(cachep);
3362 STATS_INC_ACTIVE(cachep);
3363 STATS_SET_HIGH(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003364
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003365 BUG_ON(slabp->inuse == cachep->num);
Christoph Lametere498be72005-09-09 13:03:32 -07003366
Matthew Dobson78d382d2006-02-01 03:05:47 -08003367 obj = slab_get_obj(cachep, slabp, nodeid);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003368 check_slabp(cachep, slabp);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003369 n->free_objects--;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003370 /* move slabp to correct slabp list: */
3371 list_del(&slabp->list);
Christoph Lametere498be72005-09-09 13:03:32 -07003372
Andrew Mortona737b3e2006-03-22 00:08:11 -08003373 if (slabp->free == BUFCTL_END)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003374 list_add(&slabp->list, &n->slabs_full);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003375 else
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003376 list_add(&slabp->list, &n->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07003377
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003378 spin_unlock(&n->list_lock);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003379 goto done;
Christoph Lametere498be72005-09-09 13:03:32 -07003380
Andrew Mortona737b3e2006-03-22 00:08:11 -08003381must_grow:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003382 spin_unlock(&n->list_lock);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003383 x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
Christoph Lameter765c4502006-09-27 01:50:08 -07003384 if (x)
3385 goto retry;
Christoph Lametere498be72005-09-09 13:03:32 -07003386
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003387 return fallback_alloc(cachep, flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003388
Andrew Mortona737b3e2006-03-22 00:08:11 -08003389done:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003390 return obj;
Christoph Lametere498be72005-09-09 13:03:32 -07003391}
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003392
3393/**
3394 * kmem_cache_alloc_node - Allocate an object on the specified node
3395 * @cachep: The cache to allocate from.
3396 * @flags: See kmalloc().
3397 * @nodeid: node number of the target node.
3398 * @caller: return address of caller, used for debug information
3399 *
3400 * Identical to kmem_cache_alloc but it will allocate memory on the given
3401 * node, which can improve the performance for cpu bound structures.
3402 *
3403 * Fallback to other node is possible if __GFP_THISNODE is not set.
3404 */
3405static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003406slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003407 unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003408{
3409 unsigned long save_flags;
3410 void *ptr;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003411 int slab_node = numa_mem_id();
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003412
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003413 flags &= gfp_allowed_mask;
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003414
Nick Piggincf40bd12009-01-21 08:12:39 +01003415 lockdep_trace_alloc(flags);
3416
Akinobu Mita773ff602008-12-23 19:37:01 +09003417 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003418 return NULL;
3419
Glauber Costad79923f2012-12-18 14:22:48 -08003420 cachep = memcg_kmem_get_cache(cachep, flags);
3421
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003422 cache_alloc_debugcheck_before(cachep, flags);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003423 local_lock_irqsave(slab_lock, save_flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003424
Andrew Mortoneacbbae2011-07-28 13:59:49 -07003425 if (nodeid == NUMA_NO_NODE)
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003426 nodeid = slab_node;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003427
Christoph Lameter6a673682013-01-10 19:14:19 +00003428 if (unlikely(!cachep->node[nodeid])) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003429 /* Node not bootstrapped yet */
3430 ptr = fallback_alloc(cachep, flags);
3431 goto out;
3432 }
3433
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003434 if (nodeid == slab_node) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003435 /*
3436 * Use the locally cached objects if possible.
3437 * However ____cache_alloc does not allow fallback
3438 * to other nodes. It may fail while we still have
3439 * objects on other nodes available.
3440 */
3441 ptr = ____cache_alloc(cachep, flags);
3442 if (ptr)
3443 goto out;
3444 }
3445 /* ___cache_alloc_node can fall back to other nodes */
3446 ptr = ____cache_alloc_node(cachep, flags, nodeid);
3447 out:
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003448 local_unlock_irqrestore(slab_lock, save_flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003449 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003450 kmemleak_alloc_recursive(ptr, cachep->object_size, 1, cachep->flags,
Catalin Marinasd5cff632009-06-11 13:22:40 +01003451 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003452
Pekka Enbergc175eea2008-05-09 20:35:53 +02003453 if (likely(ptr))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003454 kmemcheck_slab_alloc(cachep, flags, ptr, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003455
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003456 if (unlikely((flags & __GFP_ZERO) && ptr))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003457 memset(ptr, 0, cachep->object_size);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003458
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003459 return ptr;
3460}
3461
3462static __always_inline void *
3463__do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
3464{
3465 void *objp;
3466
3467 if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
3468 objp = alternate_node_alloc(cache, flags);
3469 if (objp)
3470 goto out;
3471 }
3472 objp = ____cache_alloc(cache, flags);
3473
3474 /*
3475 * We may just have run out of memory on the local node.
3476 * ____cache_alloc_node() knows how to locate memory on other nodes
3477 */
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003478 if (!objp)
3479 objp = ____cache_alloc_node(cache, flags, numa_mem_id());
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003480
3481 out:
3482 return objp;
3483}
3484#else
3485
3486static __always_inline void *
3487__do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
3488{
3489 return ____cache_alloc(cachep, flags);
3490}
3491
3492#endif /* CONFIG_NUMA */
3493
3494static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003495slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003496{
3497 unsigned long save_flags;
3498 void *objp;
3499
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003500 flags &= gfp_allowed_mask;
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003501
Nick Piggincf40bd12009-01-21 08:12:39 +01003502 lockdep_trace_alloc(flags);
3503
Akinobu Mita773ff602008-12-23 19:37:01 +09003504 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003505 return NULL;
3506
Glauber Costad79923f2012-12-18 14:22:48 -08003507 cachep = memcg_kmem_get_cache(cachep, flags);
3508
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003509 cache_alloc_debugcheck_before(cachep, flags);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003510 local_lock_irqsave(slab_lock, save_flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003511 objp = __do_cache_alloc(cachep, flags);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003512 local_unlock_irqrestore(slab_lock, save_flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003513 objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003514 kmemleak_alloc_recursive(objp, cachep->object_size, 1, cachep->flags,
Catalin Marinasd5cff632009-06-11 13:22:40 +01003515 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003516 prefetchw(objp);
3517
Pekka Enbergc175eea2008-05-09 20:35:53 +02003518 if (likely(objp))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003519 kmemcheck_slab_alloc(cachep, flags, objp, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003520
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003521 if (unlikely((flags & __GFP_ZERO) && objp))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003522 memset(objp, 0, cachep->object_size);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003523
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003524 return objp;
3525}
Christoph Lametere498be72005-09-09 13:03:32 -07003526
3527/*
3528 * Caller needs to acquire correct kmem_list's list_lock
3529 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003530static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003531 int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532{
3533 int i;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003534 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
3536 for (i = 0; i < nr_objects; i++) {
Mel Gorman072bb0a2012-07-31 16:43:58 -07003537 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539
Mel Gorman072bb0a2012-07-31 16:43:58 -07003540 clear_obj_pfmemalloc(&objpp[i]);
3541 objp = objpp[i];
3542
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003543 slabp = virt_to_slab(objp);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003544 n = cachep->node[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545 list_del(&slabp->list);
Christoph Lameterff694162005-09-22 21:44:02 -07003546 check_spinlock_acquired_node(cachep, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 check_slabp(cachep, slabp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08003548 slab_put_obj(cachep, slabp, objp, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 STATS_DEC_ACTIVE(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003550 n->free_objects++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 check_slabp(cachep, slabp);
3552
3553 /* fixup slab chains */
3554 if (slabp->inuse == 0) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003555 if (n->free_objects > n->free_limit) {
3556 n->free_objects -= cachep->num;
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07003557 /* No need to drop any previously held
3558 * lock here, even if we have a off-slab slab
3559 * descriptor it is guaranteed to come from
3560 * a different cache, refer to comments before
3561 * alloc_slabmgmt.
3562 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 slab_destroy(cachep, slabp);
3564 } else {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003565 list_add(&slabp->list, &n->slabs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 }
3567 } else {
3568 /* Unconditionally move a slab to the end of the
3569 * partial list on free - maximum time for the
3570 * other objects to be freed, too.
3571 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003572 list_add_tail(&slabp->list, &n->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 }
3574 }
3575}
3576
Pekka Enberg343e0d72006-02-01 03:05:50 -08003577static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578{
3579 int batchcount;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003580 struct kmem_cache_node *n;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003581 int node = numa_mem_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582
3583 batchcount = ac->batchcount;
3584#if DEBUG
3585 BUG_ON(!batchcount || batchcount > ac->avail);
3586#endif
3587 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003588 n = cachep->node[node];
3589 spin_lock(&n->list_lock);
3590 if (n->shared) {
3591 struct array_cache *shared_array = n->shared;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003592 int max = shared_array->limit - shared_array->avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 if (max) {
3594 if (batchcount > max)
3595 batchcount = max;
Christoph Lametere498be72005-09-09 13:03:32 -07003596 memcpy(&(shared_array->entry[shared_array->avail]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003597 ac->entry, sizeof(void *) * batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598 shared_array->avail += batchcount;
3599 goto free_done;
3600 }
3601 }
3602
Christoph Lameterff694162005-09-22 21:44:02 -07003603 free_block(cachep, ac->entry, batchcount, node);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003604free_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605#if STATS
3606 {
3607 int i = 0;
3608 struct list_head *p;
3609
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003610 p = n->slabs_free.next;
3611 while (p != &(n->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003612 struct slab *slabp;
3613
3614 slabp = list_entry(p, struct slab, list);
3615 BUG_ON(slabp->inuse);
3616
3617 i++;
3618 p = p->next;
3619 }
3620 STATS_SET_FREEABLE(cachep, i);
3621 }
3622#endif
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003623 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 ac->avail -= batchcount;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003625 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626}
3627
3628/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08003629 * Release an obj back to its cache. If the obj has a constructed state, it must
3630 * be in this state _before_ it is released. Called with disabled ints.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 */
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003632static inline void __cache_free(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003633 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634{
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003635 struct array_cache *ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003636
3637 check_irq_off();
Catalin Marinasd5cff632009-06-11 13:22:40 +01003638 kmemleak_free_recursive(objp, cachep->flags);
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003639 objp = cache_free_debugcheck(cachep, objp, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003641 kmemcheck_slab_free(cachep, objp, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003642
Siddha, Suresh B1807a1a2007-08-22 14:01:49 -07003643 /*
3644 * Skip calling cache_free_alien() when the platform is not numa.
3645 * This will avoid cache misses that happen while accessing slabp (which
3646 * is per page memory reference) to get nodeid. Instead use a global
3647 * variable to skip the call, which is mostly likely to be present in
3648 * the cache.
3649 */
Mel Gormanb6e68bc2009-06-16 15:32:16 -07003650 if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07003651 return;
Christoph Lametere498be72005-09-09 13:03:32 -07003652
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 if (likely(ac->avail < ac->limit)) {
3654 STATS_INC_FREEHIT(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 } else {
3656 STATS_INC_FREEMISS(cachep);
3657 cache_flusharray(cachep, ac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 }
Zhao Jin42c8c992011-08-27 00:26:17 +08003659
Mel Gorman072bb0a2012-07-31 16:43:58 -07003660 ac_put_obj(cachep, ac, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661}
3662
3663/**
3664 * kmem_cache_alloc - Allocate an object
3665 * @cachep: The cache to allocate from.
3666 * @flags: See kmalloc().
3667 *
3668 * Allocate an object from this cache. The flags are only relevant
3669 * if the cache has no available objects.
3670 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003671void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003673 void *ret = slab_alloc(cachep, flags, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003674
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003675 trace_kmem_cache_alloc(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003676 cachep->object_size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003677
3678 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679}
3680EXPORT_SYMBOL(kmem_cache_alloc);
3681
Li Zefan0f24f122009-12-11 15:45:30 +08003682#ifdef CONFIG_TRACING
Steven Rostedt85beb582010-11-24 16:23:34 -05003683void *
Ezequiel Garcia40521472012-09-08 17:47:56 -03003684kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003685{
Steven Rostedt85beb582010-11-24 16:23:34 -05003686 void *ret;
3687
Ezequiel Garcia48356302012-09-08 17:47:57 -03003688 ret = slab_alloc(cachep, flags, _RET_IP_);
Steven Rostedt85beb582010-11-24 16:23:34 -05003689
3690 trace_kmalloc(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003691 size, cachep->size, flags);
Steven Rostedt85beb582010-11-24 16:23:34 -05003692 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003693}
Steven Rostedt85beb582010-11-24 16:23:34 -05003694EXPORT_SYMBOL(kmem_cache_alloc_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003695#endif
3696
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697#ifdef CONFIG_NUMA
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003698void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
3699{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003700 void *ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003701
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003702 trace_kmem_cache_alloc_node(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003703 cachep->object_size, cachep->size,
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003704 flags, nodeid);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003705
3706 return ret;
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003707}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708EXPORT_SYMBOL(kmem_cache_alloc_node);
3709
Li Zefan0f24f122009-12-11 15:45:30 +08003710#ifdef CONFIG_TRACING
Ezequiel Garcia40521472012-09-08 17:47:56 -03003711void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
Steven Rostedt85beb582010-11-24 16:23:34 -05003712 gfp_t flags,
Ezequiel Garcia40521472012-09-08 17:47:56 -03003713 int nodeid,
3714 size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003715{
Steven Rostedt85beb582010-11-24 16:23:34 -05003716 void *ret;
3717
Ezequiel Garcia592f4142012-09-25 08:07:08 -03003718 ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003719
Steven Rostedt85beb582010-11-24 16:23:34 -05003720 trace_kmalloc_node(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003721 size, cachep->size,
Steven Rostedt85beb582010-11-24 16:23:34 -05003722 flags, nodeid);
3723 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003724}
Steven Rostedt85beb582010-11-24 16:23:34 -05003725EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003726#endif
3727
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003728static __always_inline void *
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003729__do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller)
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003730{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003731 struct kmem_cache *cachep;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003732
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003733 cachep = kmalloc_slab(size, flags);
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003734 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3735 return cachep;
Ezequiel Garcia40521472012-09-08 17:47:56 -03003736 return kmem_cache_alloc_node_trace(cachep, flags, node, size);
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003737}
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003738
Li Zefan0bb38a52009-12-11 15:45:50 +08003739#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003740void *__kmalloc_node(size_t size, gfp_t flags, int node)
3741{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003742 return __do_kmalloc_node(size, flags, node, _RET_IP_);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003743}
Christoph Hellwigdbe5e692006-09-25 23:31:36 -07003744EXPORT_SYMBOL(__kmalloc_node);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003745
3746void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003747 int node, unsigned long caller)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003748{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003749 return __do_kmalloc_node(size, flags, node, caller);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003750}
3751EXPORT_SYMBOL(__kmalloc_node_track_caller);
3752#else
3753void *__kmalloc_node(size_t size, gfp_t flags, int node)
3754{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003755 return __do_kmalloc_node(size, flags, node, 0);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003756}
3757EXPORT_SYMBOL(__kmalloc_node);
Li Zefan0bb38a52009-12-11 15:45:50 +08003758#endif /* CONFIG_DEBUG_SLAB || CONFIG_TRACING */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003759#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
3761/**
Paul Drynoff800590f2006-06-23 02:03:48 -07003762 * __do_kmalloc - allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07003763 * @size: how many bytes of memory are required.
Paul Drynoff800590f2006-06-23 02:03:48 -07003764 * @flags: the type of memory to allocate (see kmalloc).
Randy Dunlap911851e2006-03-22 00:08:14 -08003765 * @caller: function caller for debug tracking of the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 */
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003767static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003768 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003770 struct kmem_cache *cachep;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003771 void *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003773 /* If you want to save a few bytes .text space: replace
3774 * __ with kmem_.
3775 * Then kmalloc uses the uninlined functions instead of the inline
3776 * functions.
3777 */
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003778 cachep = kmalloc_slab(size, flags);
Linus Torvaldsa5c96d82007-07-19 13:17:15 -07003779 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3780 return cachep;
Ezequiel Garcia48356302012-09-08 17:47:57 -03003781 ret = slab_alloc(cachep, flags, caller);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003782
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003783 trace_kmalloc(caller, ret,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003784 size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003785
3786 return ret;
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003787}
3788
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003789
Li Zefan0bb38a52009-12-11 15:45:50 +08003790#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003791void *__kmalloc(size_t size, gfp_t flags)
3792{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003793 return __do_kmalloc(size, flags, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794}
3795EXPORT_SYMBOL(__kmalloc);
3796
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003797void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003798{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003799 return __do_kmalloc(size, flags, caller);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003800}
3801EXPORT_SYMBOL(__kmalloc_track_caller);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003802
3803#else
3804void *__kmalloc(size_t size, gfp_t flags)
3805{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003806 return __do_kmalloc(size, flags, 0);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003807}
3808EXPORT_SYMBOL(__kmalloc);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003809#endif
3810
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811/**
3812 * kmem_cache_free - Deallocate an object
3813 * @cachep: The cache the allocation was from.
3814 * @objp: The previously allocated object.
3815 *
3816 * Free an object which was previously allocated from this
3817 * cache.
3818 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003819void kmem_cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820{
3821 unsigned long flags;
Glauber Costab9ce5ef2012-12-18 14:22:46 -08003822 cachep = cache_from_obj(cachep, objp);
3823 if (!cachep)
3824 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825
Feng Tangd97d4762012-07-02 14:29:10 +08003826 debug_check_no_locks_freed(objp, cachep->object_size);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07003827 if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003828 debug_check_no_obj_freed(objp, cachep->object_size);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003829 local_lock_irqsave(slab_lock, flags);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003830 __cache_free(cachep, objp, _RET_IP_);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003831 local_unlock_irqrestore(slab_lock, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003832
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003833 trace_kmem_cache_free(_RET_IP_, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003834}
3835EXPORT_SYMBOL(kmem_cache_free);
3836
3837/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003838 * kfree - free previously allocated memory
3839 * @objp: pointer returned by kmalloc.
3840 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07003841 * If @objp is NULL, no operation is performed.
3842 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 * Don't free memory not originally allocated by kmalloc()
3844 * or you will run into trouble.
3845 */
3846void kfree(const void *objp)
3847{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003848 struct kmem_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 unsigned long flags;
3850
Pekka Enberg2121db72009-03-25 11:05:57 +02003851 trace_kfree(_RET_IP_, objp);
3852
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003853 if (unlikely(ZERO_OR_NULL_PTR(objp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 kfree_debugcheck(objp);
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003856 c = virt_to_cache(objp);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003857 debug_check_no_locks_freed(objp, c->object_size);
3858
3859 debug_check_no_obj_freed(objp, c->object_size);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003860 local_lock_irqsave(slab_lock, flags);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003861 __cache_free(c, (void *)objp, _RET_IP_);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003862 local_unlock_irqrestore(slab_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863}
3864EXPORT_SYMBOL(kfree);
3865
Christoph Lametere498be72005-09-09 13:03:32 -07003866/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003867 * This initializes kmem_cache_node or resizes various caches for all nodes.
Christoph Lametere498be72005-09-09 13:03:32 -07003868 */
Pekka Enberg83b519e2009-06-10 19:40:04 +03003869static int alloc_kmemlist(struct kmem_cache *cachep, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -07003870{
3871 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003872 struct kmem_cache_node *n;
Christoph Lametercafeb022006-03-25 03:06:46 -08003873 struct array_cache *new_shared;
Paul Menage3395ee02006-12-06 20:32:16 -08003874 struct array_cache **new_alien = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003875
Mel Gorman9c09a952008-01-24 05:49:54 -08003876 for_each_online_node(node) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003877
Paul Menage3395ee02006-12-06 20:32:16 -08003878 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03003879 new_alien = alloc_alien_cache(node, cachep->limit, gfp);
Paul Menage3395ee02006-12-06 20:32:16 -08003880 if (!new_alien)
3881 goto fail;
3882 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003883
Eric Dumazet63109842007-05-06 14:49:28 -07003884 new_shared = NULL;
3885 if (cachep->shared) {
3886 new_shared = alloc_arraycache(node,
Christoph Lameter0718dc22006-03-25 03:06:47 -08003887 cachep->shared*cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003888 0xbaadf00d, gfp);
Eric Dumazet63109842007-05-06 14:49:28 -07003889 if (!new_shared) {
3890 free_alien_cache(new_alien);
3891 goto fail;
3892 }
Christoph Lameter0718dc22006-03-25 03:06:47 -08003893 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003894
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003895 n = cachep->node[node];
3896 if (n) {
3897 struct array_cache *shared = n->shared;
Christoph Lametercafeb022006-03-25 03:06:46 -08003898
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003899 local_spin_lock_irq(slab_lock, &n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07003900
Christoph Lametercafeb022006-03-25 03:06:46 -08003901 if (shared)
Christoph Lameter0718dc22006-03-25 03:06:47 -08003902 free_block(cachep, shared->entry,
3903 shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07003904
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003905 n->shared = new_shared;
3906 if (!n->alien) {
3907 n->alien = new_alien;
Christoph Lametere498be72005-09-09 13:03:32 -07003908 new_alien = NULL;
3909 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003910 n->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003911 cachep->batchcount + cachep->num;
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003912 local_spin_unlock_irq(slab_lock, &n->list_lock);
Christoph Lametercafeb022006-03-25 03:06:46 -08003913 kfree(shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003914 free_alien_cache(new_alien);
3915 continue;
3916 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003917 n = kmalloc_node(sizeof(struct kmem_cache_node), gfp, node);
3918 if (!n) {
Christoph Lameter0718dc22006-03-25 03:06:47 -08003919 free_alien_cache(new_alien);
3920 kfree(new_shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003921 goto fail;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003922 }
Christoph Lametere498be72005-09-09 13:03:32 -07003923
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003924 kmem_cache_node_init(n);
3925 n->next_reap = jiffies + REAPTIMEOUT_LIST3 +
Andrew Mortona737b3e2006-03-22 00:08:11 -08003926 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003927 n->shared = new_shared;
3928 n->alien = new_alien;
3929 n->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003930 cachep->batchcount + cachep->num;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003931 cachep->node[node] = n;
Christoph Lametere498be72005-09-09 13:03:32 -07003932 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003933 return 0;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003934
Andrew Mortona737b3e2006-03-22 00:08:11 -08003935fail:
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003936 if (!cachep->list.next) {
Christoph Lameter0718dc22006-03-25 03:06:47 -08003937 /* Cache is not active yet. Roll back what we did */
3938 node--;
3939 while (node >= 0) {
Christoph Lameter6a673682013-01-10 19:14:19 +00003940 if (cachep->node[node]) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003941 n = cachep->node[node];
Christoph Lameter0718dc22006-03-25 03:06:47 -08003942
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003943 kfree(n->shared);
3944 free_alien_cache(n->alien);
3945 kfree(n);
Christoph Lameter6a673682013-01-10 19:14:19 +00003946 cachep->node[node] = NULL;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003947 }
3948 node--;
3949 }
3950 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003951 return -ENOMEM;
Christoph Lametere498be72005-09-09 13:03:32 -07003952}
3953
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954struct ccupdate_struct {
Pekka Enberg343e0d72006-02-01 03:05:50 -08003955 struct kmem_cache *cachep;
Eric Dumazetacfe7d72011-07-25 08:55:42 +02003956 struct array_cache *new[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957};
3958
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003959static void __do_ccupdate_local(void *info, int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960{
Andrew Mortona737b3e2006-03-22 00:08:11 -08003961 struct ccupdate_struct *new = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 struct array_cache *old;
3963
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003964 old = cpu_cache_get_on_cpu(new->cachep, cpu);
Christoph Lametere498be72005-09-09 13:03:32 -07003965
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003966 new->cachep->array[cpu] = new->new[cpu];
3967 new->new[cpu] = old;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968}
3969
Thomas Gleixner4a621b32011-06-18 19:44:43 +02003970#ifndef CONFIG_PREEMPT_RT_BASE
3971static void do_ccupdate_local(void *info)
3972{
3973 __do_ccupdate_local(info, smp_processor_id());
3974}
3975#else
3976static void do_ccupdate_local(void *info, int cpu)
3977{
3978 __do_ccupdate_local(info, cpu);
3979}
3980#endif
3981
Christoph Lameter18004c52012-07-06 15:25:12 -05003982/* Always called with the slab_mutex held */
Glauber Costa943a4512012-12-18 14:23:03 -08003983static int __do_tune_cpucache(struct kmem_cache *cachep, int limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003984 int batchcount, int shared, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985{
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003986 struct ccupdate_struct *new;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003987 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988
Eric Dumazetacfe7d72011-07-25 08:55:42 +02003989 new = kzalloc(sizeof(*new) + nr_cpu_ids * sizeof(struct array_cache *),
3990 gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003991 if (!new)
3992 return -ENOMEM;
3993
Christoph Lametere498be72005-09-09 13:03:32 -07003994 for_each_online_cpu(i) {
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003995 new->new[i] = alloc_arraycache(cpu_to_mem(i), limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003996 batchcount, gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003997 if (!new->new[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003998 for (i--; i >= 0; i--)
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003999 kfree(new->new[i]);
4000 kfree(new);
Christoph Lametere498be72005-09-09 13:03:32 -07004001 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 }
4003 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004004 new->cachep = cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004006 slab_on_each_cpu(do_ccupdate_local, (void *)new);
Christoph Lametere498be72005-09-09 13:03:32 -07004007
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 check_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 cachep->batchcount = batchcount;
4010 cachep->limit = limit;
Christoph Lametere498be72005-09-09 13:03:32 -07004011 cachep->shared = shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
Christoph Lametere498be72005-09-09 13:03:32 -07004013 for_each_online_cpu(i) {
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004014 struct array_cache *ccold = new->new[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 if (!ccold)
4016 continue;
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004017 local_spin_lock_irq(slab_lock,
4018 &cachep->node[cpu_to_mem(i)]->list_lock);
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07004019 free_block(cachep, ccold->entry, ccold->avail, cpu_to_mem(i));
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004020 local_spin_unlock_irq(slab_lock,
4021x &cachep->node[cpu_to_mem(i)]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 kfree(ccold);
4023 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004024 kfree(new);
Pekka Enberg83b519e2009-06-10 19:40:04 +03004025 return alloc_kmemlist(cachep, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026}
4027
Glauber Costa943a4512012-12-18 14:23:03 -08004028static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
4029 int batchcount, int shared, gfp_t gfp)
4030{
4031 int ret;
4032 struct kmem_cache *c = NULL;
4033 int i = 0;
4034
4035 ret = __do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
4036
4037 if (slab_state < FULL)
4038 return ret;
4039
4040 if ((ret < 0) || !is_root_cache(cachep))
4041 return ret;
4042
Glauber Costaebe945c2012-12-18 14:23:10 -08004043 VM_BUG_ON(!mutex_is_locked(&slab_mutex));
Glauber Costa943a4512012-12-18 14:23:03 -08004044 for_each_memcg_cache_index(i) {
4045 c = cache_from_memcg(cachep, i);
4046 if (c)
4047 /* return value determined by the parent cache only */
4048 __do_tune_cpucache(c, limit, batchcount, shared, gfp);
4049 }
4050
4051 return ret;
4052}
4053
Christoph Lameter18004c52012-07-06 15:25:12 -05004054/* Called with slab_mutex held always */
Pekka Enberg83b519e2009-06-10 19:40:04 +03004055static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056{
4057 int err;
Glauber Costa943a4512012-12-18 14:23:03 -08004058 int limit = 0;
4059 int shared = 0;
4060 int batchcount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061
Glauber Costa943a4512012-12-18 14:23:03 -08004062 if (!is_root_cache(cachep)) {
4063 struct kmem_cache *root = memcg_root_cache(cachep);
4064 limit = root->limit;
4065 shared = root->shared;
4066 batchcount = root->batchcount;
4067 }
4068
4069 if (limit && shared && batchcount)
4070 goto skip_setup;
Andrew Mortona737b3e2006-03-22 00:08:11 -08004071 /*
4072 * The head array serves three purposes:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 * - create a LIFO ordering, i.e. return objects that are cache-warm
4074 * - reduce the number of spinlock operations.
Andrew Mortona737b3e2006-03-22 00:08:11 -08004075 * - reduce the number of linked list operations on the slab and
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 * bufctl chains: array operations are cheaper.
4077 * The numbers are guessed, we should auto-tune as described by
4078 * Bonwick.
4079 */
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004080 if (cachep->size > 131072)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 limit = 1;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004082 else if (cachep->size > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 limit = 8;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004084 else if (cachep->size > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085 limit = 24;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004086 else if (cachep->size > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 limit = 54;
4088 else
4089 limit = 120;
4090
Andrew Mortona737b3e2006-03-22 00:08:11 -08004091 /*
4092 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 * allocation behaviour: Most allocs on one cpu, most free operations
4094 * on another cpu. For these cases, an efficient object passing between
4095 * cpus is necessary. This is provided by a shared array. The array
4096 * replaces Bonwick's magazine layer.
4097 * On uniprocessor, it's functionally equivalent (but less efficient)
4098 * to a larger limit. Thus disabled by default.
4099 */
4100 shared = 0;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004101 if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 shared = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103
4104#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08004105 /*
4106 * With debugging enabled, large batchcount lead to excessively long
4107 * periods with disabled local interrupts. Limit the batchcount
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 */
4109 if (limit > 32)
4110 limit = 32;
4111#endif
Glauber Costa943a4512012-12-18 14:23:03 -08004112 batchcount = (limit + 1) / 2;
4113skip_setup:
4114 err = do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 if (err)
4116 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004117 cachep->name, -err);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07004118 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119}
4120
Christoph Lameter1b552532006-03-22 00:09:07 -08004121/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004122 * Drain an array if it contains any elements taking the node lock only if
4123 * necessary. Note that the node listlock also protects the array_cache
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004124 * if drain_array() is used on the shared array.
Christoph Lameter1b552532006-03-22 00:09:07 -08004125 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004126static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
Christoph Lameter1b552532006-03-22 00:09:07 -08004127 struct array_cache *ac, int force, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128{
4129 int tofree;
4130
Christoph Lameter1b552532006-03-22 00:09:07 -08004131 if (!ac || !ac->avail)
4132 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 if (ac->touched && !force) {
4134 ac->touched = 0;
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004135 } else {
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004136 local_spin_lock_irq(slab_lock, &n->list_lock);
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004137 if (ac->avail) {
4138 tofree = force ? ac->avail : (ac->limit + 4) / 5;
4139 if (tofree > ac->avail)
4140 tofree = (ac->avail + 1) / 2;
4141 free_block(cachep, ac->entry, tofree, node);
4142 ac->avail -= tofree;
4143 memmove(ac->entry, &(ac->entry[tofree]),
4144 sizeof(void *) * ac->avail);
4145 }
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004146 local_spin_unlock_irq(slab_lock, &n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 }
4148}
4149
4150/**
4151 * cache_reap - Reclaim memory from caches.
Randy Dunlap05fb6bf2007-02-28 20:12:13 -08004152 * @w: work descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 *
4154 * Called from workqueue/eventd every few seconds.
4155 * Purpose:
4156 * - clear the per-cpu caches for this CPU.
4157 * - return freeable pages to the main free memory pool.
4158 *
Andrew Mortona737b3e2006-03-22 00:08:11 -08004159 * If we cannot acquire the cache chain mutex then just give up - we'll try
4160 * again on the next iteration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004162static void cache_reap(struct work_struct *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163{
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004164 struct kmem_cache *searchp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004165 struct kmem_cache_node *n;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07004166 int node = numa_mem_id();
Jean Delvarebf6aede2009-04-02 16:56:54 -07004167 struct delayed_work *work = to_delayed_work(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168
Christoph Lameter18004c52012-07-06 15:25:12 -05004169 if (!mutex_trylock(&slab_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 /* Give up. Setup the next iteration. */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004171 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
Christoph Lameter18004c52012-07-06 15:25:12 -05004173 list_for_each_entry(searchp, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 check_irq_on();
4175
Christoph Lameter35386e32006-03-22 00:09:05 -08004176 /*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004177 * We only take the node lock if absolutely necessary and we
Christoph Lameter35386e32006-03-22 00:09:05 -08004178 * have established with reasonable certainty that
4179 * we can do some work if the lock was obtained.
4180 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004181 n = searchp->node[node];
Christoph Lameter35386e32006-03-22 00:09:05 -08004182
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004183 reap_alien(searchp, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004185 drain_array(searchp, n, cpu_cache_get(searchp), 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
Christoph Lameter35386e32006-03-22 00:09:05 -08004187 /*
4188 * These are racy checks but it does not matter
4189 * if we skip one check or scan twice.
4190 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004191 if (time_after(n->next_reap, jiffies))
Christoph Lameter35386e32006-03-22 00:09:05 -08004192 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004194 n->next_reap = jiffies + REAPTIMEOUT_LIST3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004196 drain_array(searchp, n, n->shared, 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004198 if (n->free_touched)
4199 n->free_touched = 0;
Christoph Lametered11d9e2006-06-30 01:55:45 -07004200 else {
4201 int freed;
4202
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004203 freed = drain_freelist(searchp, n, (n->free_limit +
Christoph Lametered11d9e2006-06-30 01:55:45 -07004204 5 * searchp->num - 1) / (5 * searchp->num));
4205 STATS_ADD_REAPED(searchp, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 }
Christoph Lameter35386e32006-03-22 00:09:05 -08004207next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 cond_resched();
4209 }
4210 check_irq_on();
Christoph Lameter18004c52012-07-06 15:25:12 -05004211 mutex_unlock(&slab_mutex);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004212 next_reap_node();
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004213out:
Andrew Mortona737b3e2006-03-22 00:08:11 -08004214 /* Set up the next iteration */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004215 schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_CPUC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216}
4217
Linus Torvalds158a9622008-01-02 13:04:48 -08004218#ifdef CONFIG_SLABINFO
Glauber Costa0d7561c2012-10-19 18:20:27 +04004219void get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004221 struct slab *slabp;
4222 unsigned long active_objs;
4223 unsigned long num_objs;
4224 unsigned long active_slabs = 0;
4225 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004226 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 char *error = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07004228 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004229 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 active_objs = 0;
4232 num_slabs = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004233 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004234 n = cachep->node[node];
4235 if (!n)
Christoph Lametere498be72005-09-09 13:03:32 -07004236 continue;
4237
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004238 check_irq_on();
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004239 local_spin_lock_irq(slab_lock, &n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07004240
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004241 list_for_each_entry(slabp, &n->slabs_full, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004242 if (slabp->inuse != cachep->num && !error)
4243 error = "slabs_full accounting error";
4244 active_objs += cachep->num;
4245 active_slabs++;
4246 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004247 list_for_each_entry(slabp, &n->slabs_partial, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004248 if (slabp->inuse == cachep->num && !error)
4249 error = "slabs_partial inuse accounting error";
4250 if (!slabp->inuse && !error)
4251 error = "slabs_partial/inuse accounting error";
4252 active_objs += slabp->inuse;
4253 active_slabs++;
4254 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004255 list_for_each_entry(slabp, &n->slabs_free, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004256 if (slabp->inuse && !error)
4257 error = "slabs_free/inuse accounting error";
4258 num_slabs++;
4259 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004260 free_objects += n->free_objects;
4261 if (n->shared)
4262 shared_avail += n->shared->avail;
Christoph Lametere498be72005-09-09 13:03:32 -07004263
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004264 local_spin_unlock_irq(slab_lock, &n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004266 num_slabs += active_slabs;
4267 num_objs = num_slabs * cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07004268 if (num_objs - active_objs != free_objects && !error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 error = "free_objects accounting error";
4270
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004271 name = cachep->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 if (error)
4273 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
4274
Glauber Costa0d7561c2012-10-19 18:20:27 +04004275 sinfo->active_objs = active_objs;
4276 sinfo->num_objs = num_objs;
4277 sinfo->active_slabs = active_slabs;
4278 sinfo->num_slabs = num_slabs;
4279 sinfo->shared_avail = shared_avail;
4280 sinfo->limit = cachep->limit;
4281 sinfo->batchcount = cachep->batchcount;
4282 sinfo->shared = cachep->shared;
4283 sinfo->objects_per_slab = cachep->num;
4284 sinfo->cache_order = cachep->gfporder;
4285}
4286
4287void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *cachep)
4288{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289#if STATS
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004290 { /* node stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291 unsigned long high = cachep->high_mark;
4292 unsigned long allocs = cachep->num_allocations;
4293 unsigned long grown = cachep->grown;
4294 unsigned long reaped = cachep->reaped;
4295 unsigned long errors = cachep->errors;
4296 unsigned long max_freeable = cachep->max_freeable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 unsigned long node_allocs = cachep->node_allocs;
Christoph Lametere498be72005-09-09 13:03:32 -07004298 unsigned long node_frees = cachep->node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004299 unsigned long overflows = cachep->node_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300
Joe Perchese92dd4f2010-03-26 19:27:58 -07004301 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu "
4302 "%4lu %4lu %4lu %4lu %4lu",
4303 allocs, high, grown,
4304 reaped, errors, max_freeable, node_allocs,
4305 node_frees, overflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004306 }
4307 /* cpu stats */
4308 {
4309 unsigned long allochit = atomic_read(&cachep->allochit);
4310 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
4311 unsigned long freehit = atomic_read(&cachep->freehit);
4312 unsigned long freemiss = atomic_read(&cachep->freemiss);
4313
4314 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004315 allochit, allocmiss, freehit, freemiss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 }
4317#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318}
4319
Linus Torvalds1da177e2005-04-16 15:20:36 -07004320#define MAX_SLABINFO_WRITE 128
4321/**
4322 * slabinfo_write - Tuning for the slab allocator
4323 * @file: unused
4324 * @buffer: user buffer
4325 * @count: data length
4326 * @ppos: unused
4327 */
Glauber Costab7454ad2012-10-19 18:20:25 +04004328ssize_t slabinfo_write(struct file *file, const char __user *buffer,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004329 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004331 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 int limit, batchcount, shared, res;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004333 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004334
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 if (count > MAX_SLABINFO_WRITE)
4336 return -EINVAL;
4337 if (copy_from_user(&kbuf, buffer, count))
4338 return -EFAULT;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004339 kbuf[MAX_SLABINFO_WRITE] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340
4341 tmp = strchr(kbuf, ' ');
4342 if (!tmp)
4343 return -EINVAL;
4344 *tmp = '\0';
4345 tmp++;
4346 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
4347 return -EINVAL;
4348
4349 /* Find the cache in the chain of caches. */
Christoph Lameter18004c52012-07-06 15:25:12 -05004350 mutex_lock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351 res = -EINVAL;
Christoph Lameter18004c52012-07-06 15:25:12 -05004352 list_for_each_entry(cachep, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 if (!strcmp(cachep->name, kbuf)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08004354 if (limit < 1 || batchcount < 1 ||
4355 batchcount > limit || shared < 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07004356 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07004358 res = do_tune_cpucache(cachep, limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03004359 batchcount, shared,
4360 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 }
4362 break;
4363 }
4364 }
Christoph Lameter18004c52012-07-06 15:25:12 -05004365 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 if (res >= 0)
4367 res = count;
4368 return res;
4369}
Al Viro871751e2006-03-25 03:06:39 -08004370
4371#ifdef CONFIG_DEBUG_SLAB_LEAK
4372
4373static void *leaks_start(struct seq_file *m, loff_t *pos)
4374{
Christoph Lameter18004c52012-07-06 15:25:12 -05004375 mutex_lock(&slab_mutex);
4376 return seq_list_start(&slab_caches, *pos);
Al Viro871751e2006-03-25 03:06:39 -08004377}
4378
4379static inline int add_caller(unsigned long *n, unsigned long v)
4380{
4381 unsigned long *p;
4382 int l;
4383 if (!v)
4384 return 1;
4385 l = n[1];
4386 p = n + 2;
4387 while (l) {
4388 int i = l/2;
4389 unsigned long *q = p + 2 * i;
4390 if (*q == v) {
4391 q[1]++;
4392 return 1;
4393 }
4394 if (*q > v) {
4395 l = i;
4396 } else {
4397 p = q + 2;
4398 l -= i + 1;
4399 }
4400 }
4401 if (++n[1] == n[0])
4402 return 0;
4403 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4404 p[0] = v;
4405 p[1] = 1;
4406 return 1;
4407}
4408
4409static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
4410{
4411 void *p;
4412 int i;
4413 if (n[0] == n[1])
4414 return;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004415 for (i = 0, p = s->s_mem; i < c->num; i++, p += c->size) {
Al Viro871751e2006-03-25 03:06:39 -08004416 if (slab_bufctl(s)[i] != BUFCTL_ACTIVE)
4417 continue;
4418 if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
4419 return;
4420 }
4421}
4422
4423static void show_symbol(struct seq_file *m, unsigned long address)
4424{
4425#ifdef CONFIG_KALLSYMS
Al Viro871751e2006-03-25 03:06:39 -08004426 unsigned long offset, size;
Tejun Heo9281ace2007-07-17 04:03:51 -07004427 char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
Al Viro871751e2006-03-25 03:06:39 -08004428
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004429 if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
Al Viro871751e2006-03-25 03:06:39 -08004430 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004431 if (modname[0])
Al Viro871751e2006-03-25 03:06:39 -08004432 seq_printf(m, " [%s]", modname);
4433 return;
4434 }
4435#endif
4436 seq_printf(m, "%p", (void *)address);
4437}
4438
4439static int leaks_show(struct seq_file *m, void *p)
4440{
Thierry Reding0672aa72012-06-22 19:42:49 +02004441 struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);
Al Viro871751e2006-03-25 03:06:39 -08004442 struct slab *slabp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004443 struct kmem_cache_node *n;
Al Viro871751e2006-03-25 03:06:39 -08004444 const char *name;
Christoph Lameterdb845062013-02-05 18:45:23 +00004445 unsigned long *x = m->private;
Al Viro871751e2006-03-25 03:06:39 -08004446 int node;
4447 int i;
4448
4449 if (!(cachep->flags & SLAB_STORE_USER))
4450 return 0;
4451 if (!(cachep->flags & SLAB_RED_ZONE))
4452 return 0;
4453
4454 /* OK, we can do it */
4455
Christoph Lameterdb845062013-02-05 18:45:23 +00004456 x[1] = 0;
Al Viro871751e2006-03-25 03:06:39 -08004457
4458 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004459 n = cachep->node[node];
4460 if (!n)
Al Viro871751e2006-03-25 03:06:39 -08004461 continue;
4462
4463 check_irq_on();
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004464 local_spin_lock_irq(slab_lock, &n->list_lock);
Al Viro871751e2006-03-25 03:06:39 -08004465
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004466 list_for_each_entry(slabp, &n->slabs_full, list)
Christoph Lameterdb845062013-02-05 18:45:23 +00004467 handle_slab(x, cachep, slabp);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004468 list_for_each_entry(slabp, &n->slabs_partial, list)
Christoph Lameterdb845062013-02-05 18:45:23 +00004469 handle_slab(x, cachep, slabp);
Thomas Gleixner4a621b32011-06-18 19:44:43 +02004470 local_spin_unlock_irq(slab_lock, &n->list_lock);
Al Viro871751e2006-03-25 03:06:39 -08004471 }
4472 name = cachep->name;
Christoph Lameterdb845062013-02-05 18:45:23 +00004473 if (x[0] == x[1]) {
Al Viro871751e2006-03-25 03:06:39 -08004474 /* Increase the buffer size */
Christoph Lameter18004c52012-07-06 15:25:12 -05004475 mutex_unlock(&slab_mutex);
Christoph Lameterdb845062013-02-05 18:45:23 +00004476 m->private = kzalloc(x[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
Al Viro871751e2006-03-25 03:06:39 -08004477 if (!m->private) {
4478 /* Too bad, we are really out */
Christoph Lameterdb845062013-02-05 18:45:23 +00004479 m->private = x;
Christoph Lameter18004c52012-07-06 15:25:12 -05004480 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004481 return -ENOMEM;
4482 }
Christoph Lameterdb845062013-02-05 18:45:23 +00004483 *(unsigned long *)m->private = x[0] * 2;
4484 kfree(x);
Christoph Lameter18004c52012-07-06 15:25:12 -05004485 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004486 /* Now make sure this entry will be retried */
4487 m->count = m->size;
4488 return 0;
4489 }
Christoph Lameterdb845062013-02-05 18:45:23 +00004490 for (i = 0; i < x[1]; i++) {
4491 seq_printf(m, "%s: %lu ", name, x[2*i+3]);
4492 show_symbol(m, x[2*i+2]);
Al Viro871751e2006-03-25 03:06:39 -08004493 seq_putc(m, '\n');
4494 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004495
Al Viro871751e2006-03-25 03:06:39 -08004496 return 0;
4497}
4498
Glauber Costab7454ad2012-10-19 18:20:25 +04004499static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4500{
4501 return seq_list_next(p, &slab_caches, pos);
4502}
4503
4504static void s_stop(struct seq_file *m, void *p)
4505{
4506 mutex_unlock(&slab_mutex);
4507}
4508
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004509static const struct seq_operations slabstats_op = {
Al Viro871751e2006-03-25 03:06:39 -08004510 .start = leaks_start,
4511 .next = s_next,
4512 .stop = s_stop,
4513 .show = leaks_show,
4514};
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004515
4516static int slabstats_open(struct inode *inode, struct file *file)
4517{
4518 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
4519 int ret = -ENOMEM;
4520 if (n) {
4521 ret = seq_open(file, &slabstats_op);
4522 if (!ret) {
4523 struct seq_file *m = file->private_data;
4524 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
4525 m->private = n;
4526 n = NULL;
4527 }
4528 kfree(n);
4529 }
4530 return ret;
4531}
4532
4533static const struct file_operations proc_slabstats_operations = {
4534 .open = slabstats_open,
4535 .read = seq_read,
4536 .llseek = seq_lseek,
4537 .release = seq_release_private,
4538};
Al Viro871751e2006-03-25 03:06:39 -08004539#endif
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004540
4541static int __init slab_proc_init(void)
4542{
4543#ifdef CONFIG_DEBUG_SLAB_LEAK
4544 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
4545#endif
4546 return 0;
4547}
4548module_init(slab_proc_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549#endif
4550
Manfred Spraul00e145b2005-09-03 15:55:07 -07004551/**
4552 * ksize - get the actual amount of memory allocated for a given object
4553 * @objp: Pointer to the object
4554 *
4555 * kmalloc may internally round up allocations and return more memory
4556 * than requested. ksize() can be used to determine the actual amount of
4557 * memory allocated. The caller may use this additional memory, even though
4558 * a smaller amount of memory was initially specified with the kmalloc call.
4559 * The caller must guarantee that objp points to a valid object previously
4560 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4561 * must not be freed during the duration of the call.
4562 */
Pekka Enbergfd76bab2007-05-06 14:48:40 -07004563size_t ksize(const void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564{
Christoph Lameteref8b4522007-10-16 01:24:46 -07004565 BUG_ON(!objp);
4566 if (unlikely(objp == ZERO_SIZE_PTR))
Manfred Spraul00e145b2005-09-03 15:55:07 -07004567 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568
Christoph Lameter8c138bc2012-06-13 10:24:58 -05004569 return virt_to_cache(objp)->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570}
Kirill A. Shutemovb1aabec2009-02-10 15:21:44 +02004571EXPORT_SYMBOL(ksize);