blob: f6ad8d335be7ea55b150cd230bbeeecd0ab2bbe3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/mm/slab.c
3 * Written by Mark Hemment, 1996/97.
4 * (markhe@nextd.demon.co.uk)
5 *
6 * kmem_cache_destroy() + some cleanup - 1999 Andrea Arcangeli
7 *
8 * Major cleanup, different bufctl logic, per-cpu arrays
9 * (c) 2000 Manfred Spraul
10 *
11 * Cleanup, make the head arrays unconditional, preparation for NUMA
12 * (c) 2002 Manfred Spraul
13 *
14 * An implementation of the Slab Allocator as described in outline in;
15 * UNIX Internals: The New Frontiers by Uresh Vahalia
16 * Pub: Prentice Hall ISBN 0-13-101908-2
17 * or with a little more detail in;
18 * The Slab Allocator: An Object-Caching Kernel Memory Allocator
19 * Jeff Bonwick (Sun Microsystems).
20 * Presented at: USENIX Summer 1994 Technical Conference
21 *
22 * The memory is organized in caches, one cache for each object type.
23 * (e.g. inode_cache, dentry_cache, buffer_head, vm_area_struct)
24 * Each cache consists out of many slabs (they are small (usually one
25 * page long) and always contiguous), and each slab contains multiple
26 * initialized objects.
27 *
28 * This means, that your constructor is used only for newly allocated
Simon Arlott183ff222007-10-20 01:27:18 +020029 * slabs and you must pass objects with the same initializations to
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * kmem_cache_free.
31 *
32 * Each cache can only support one memory type (GFP_DMA, GFP_HIGHMEM,
33 * normal). If you need a special memory type, then must create a new
34 * cache for that memory type.
35 *
36 * In order to reduce fragmentation, the slabs are sorted in 3 groups:
37 * full slabs with 0 free objects
38 * partial slabs
39 * empty slabs with no allocated objects
40 *
41 * If partial slabs exist, then new allocations come from these slabs,
42 * otherwise from empty slabs or new slabs are allocated.
43 *
44 * kmem_cache_destroy() CAN CRASH if you try to allocate from the cache
45 * during kmem_cache_destroy(). The caller must prevent concurrent allocs.
46 *
47 * Each cache has a short per-cpu head array, most allocs
48 * and frees go into that array, and if that array overflows, then 1/2
49 * of the entries in the array are given back into the global cache.
50 * The head array is strictly LIFO and should improve the cache hit rates.
51 * On SMP, it additionally reduces the spinlock operations.
52 *
Andrew Mortona737b3e2006-03-22 00:08:11 -080053 * The c_cpuarray may not be read with enabled local interrupts -
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * it's changed with a smp_call_function().
55 *
56 * SMP synchronization:
57 * constructors and destructors are called without any locking.
Pekka Enberg343e0d72006-02-01 03:05:50 -080058 * Several members in struct kmem_cache and struct slab never change, they
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * are accessed without any locking.
60 * The per-cpu arrays are never accessed from the wrong cpu, no locking,
61 * and local interrupts are disabled so slab code is preempt-safe.
62 * The non-constant members are protected with a per-cache irq spinlock.
63 *
64 * Many thanks to Mark Hemment, who wrote another per-cpu slab patch
65 * in 2000 - many ideas in the current implementation are derived from
66 * his patch.
67 *
68 * Further notes from the original documentation:
69 *
70 * 11 April '97. Started multi-threading - markhe
Christoph Lameter18004c52012-07-06 15:25:12 -050071 * The global cache-chain is protected by the mutex 'slab_mutex'.
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * The sem is only needed when accessing/extending the cache-chain, which
73 * can never happen inside an interrupt (kmem_cache_create(),
74 * kmem_cache_shrink() and kmem_cache_reap()).
75 *
76 * At present, each engine can be growing a cache. This should be blocked.
77 *
Christoph Lametere498be72005-09-09 13:03:32 -070078 * 15 March 2005. NUMA slab allocator.
79 * Shai Fultheim <shai@scalex86.org>.
80 * Shobhit Dayal <shobhit@calsoftinc.com>
81 * Alok N Kataria <alokk@calsoftinc.com>
82 * Christoph Lameter <christoph@lameter.com>
83 *
84 * Modified the slab allocator to be node aware on NUMA systems.
85 * Each node has its own list of partial, free and full slabs.
86 * All object allocations for a node occur from node specific slab lists.
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 */
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <linux/slab.h>
90#include <linux/mm.h>
Randy Dunlapc9cf5522006-06-27 02:53:52 -070091#include <linux/poison.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#include <linux/swap.h>
93#include <linux/cache.h>
94#include <linux/interrupt.h>
95#include <linux/init.h>
96#include <linux/compiler.h>
Paul Jackson101a5002006-03-24 03:16:07 -080097#include <linux/cpuset.h>
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +040098#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#include <linux/seq_file.h>
100#include <linux/notifier.h>
101#include <linux/kallsyms.h>
102#include <linux/cpu.h>
103#include <linux/sysctl.h>
104#include <linux/module.h>
105#include <linux/rcupdate.h>
Paulo Marques543537b2005-06-23 00:09:02 -0700106#include <linux/string.h>
Andrew Morton138ae662006-12-06 20:36:41 -0800107#include <linux/uaccess.h>
Christoph Lametere498be72005-09-09 13:03:32 -0700108#include <linux/nodemask.h>
Catalin Marinasd5cff632009-06-11 13:22:40 +0100109#include <linux/kmemleak.h>
Christoph Lameterdc85da12006-01-18 17:42:36 -0800110#include <linux/mempolicy.h>
Ingo Molnarfc0abb12006-01-18 17:42:33 -0800111#include <linux/mutex.h>
Akinobu Mita8a8b6502006-12-08 02:39:44 -0800112#include <linux/fault-inject.h>
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700113#include <linux/rtmutex.h>
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800114#include <linux/reciprocal_div.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700115#include <linux/debugobjects.h>
Pekka Enbergc175eea2008-05-09 20:35:53 +0200116#include <linux/kmemcheck.h>
David Rientjes8f9f8d92010-03-27 19:40:47 -0700117#include <linux/memory.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -0700118#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Mel Gorman381760e2012-07-31 16:44:30 -0700120#include <net/sock.h>
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122#include <asm/cacheflush.h>
123#include <asm/tlbflush.h>
124#include <asm/page.h>
125
Steven Rostedt4dee6b62012-01-09 17:15:42 -0500126#include <trace/events/kmem.h>
127
Mel Gorman072bb0a2012-07-31 16:43:58 -0700128#include "internal.h"
129
Glauber Costab9ce5ef2012-12-18 14:22:46 -0800130#include "slab.h"
131
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132/*
Christoph Lameter50953fe2007-05-06 14:50:16 -0700133 * DEBUG - 1 for kmem_cache_create() to honour; SLAB_RED_ZONE & SLAB_POISON.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 * 0 for faster, smaller code (especially in the critical paths).
135 *
136 * STATS - 1 to collect stats for /proc/slabinfo.
137 * 0 for faster, smaller code (especially in the critical paths).
138 *
139 * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
140 */
141
142#ifdef CONFIG_DEBUG_SLAB
143#define DEBUG 1
144#define STATS 1
145#define FORCED_DEBUG 1
146#else
147#define DEBUG 0
148#define STATS 0
149#define FORCED_DEBUG 0
150#endif
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152/* Shouldn't this be in a header file somewhere? */
153#define BYTES_PER_WORD sizeof(void *)
David Woodhouse87a927c2007-07-04 21:26:44 -0400154#define REDZONE_ALIGN max(BYTES_PER_WORD, __alignof__(unsigned long long))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#ifndef ARCH_KMALLOC_FLAGS
157#define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
158#endif
159
Joonsoo Kimf315e3f2013-12-02 17:49:41 +0900160#define FREELIST_BYTE_INDEX (((PAGE_SIZE >> BITS_PER_BYTE) \
161 <= SLAB_OBJ_MIN_SIZE) ? 1 : 0)
162
163#if FREELIST_BYTE_INDEX
164typedef unsigned char freelist_idx_t;
165#else
166typedef unsigned short freelist_idx_t;
167#endif
168
David Miller30321c72014-05-05 16:20:04 -0400169#define SLAB_OBJ_MAX_NUM ((1 << sizeof(freelist_idx_t) * BITS_PER_BYTE) - 1)
Joonsoo Kimf315e3f2013-12-02 17:49:41 +0900170
Mel Gorman072bb0a2012-07-31 16:43:58 -0700171/*
172 * true if a page was allocated from pfmemalloc reserves for network-based
173 * swap
174 */
175static bool pfmemalloc_active __read_mostly;
176
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 * struct array_cache
179 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 * Purpose:
181 * - LIFO ordering, to hand out cache-warm objects from _alloc
182 * - reduce the number of linked list operations
183 * - reduce spinlock operations
184 *
185 * The limit is stored in the per-cpu structure to reduce the data cache
186 * footprint.
187 *
188 */
189struct array_cache {
190 unsigned int avail;
191 unsigned int limit;
192 unsigned int batchcount;
193 unsigned int touched;
Christoph Lametere498be72005-09-09 13:03:32 -0700194 spinlock_t lock;
Robert P. J. Daybda5b652007-10-16 23:30:05 -0700195 void *entry[]; /*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800196 * Must have this definition in here for the proper
197 * alignment of array_cache. Also simplifies accessing
198 * the entries.
Mel Gorman072bb0a2012-07-31 16:43:58 -0700199 *
200 * Entries should not be directly dereferenced as
201 * entries belonging to slabs marked pfmemalloc will
202 * have the lower bits set SLAB_OBJ_PFMEMALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -0800203 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204};
205
Mel Gorman072bb0a2012-07-31 16:43:58 -0700206#define SLAB_OBJ_PFMEMALLOC 1
207static inline bool is_obj_pfmemalloc(void *objp)
208{
209 return (unsigned long)objp & SLAB_OBJ_PFMEMALLOC;
210}
211
212static inline void set_obj_pfmemalloc(void **objp)
213{
214 *objp = (void *)((unsigned long)*objp | SLAB_OBJ_PFMEMALLOC);
215 return;
216}
217
218static inline void clear_obj_pfmemalloc(void **objp)
219{
220 *objp = (void *)((unsigned long)*objp & ~SLAB_OBJ_PFMEMALLOC);
221}
222
Andrew Mortona737b3e2006-03-22 00:08:11 -0800223/*
224 * bootstrap: The caches do not work without cpuarrays anymore, but the
225 * cpuarrays are allocated from the generic caches...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 */
227#define BOOT_CPUCACHE_ENTRIES 1
228struct arraycache_init {
229 struct array_cache cache;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800230 void *entries[BOOT_CPUCACHE_ENTRIES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231};
232
233/*
Christoph Lametere498be72005-09-09 13:03:32 -0700234 * Need this for bootstrapping a per node allocator.
235 */
Pekka Enberg556a1692008-01-25 08:20:51 +0200236#define NUM_INIT_LISTS (3 * MAX_NUMNODES)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000237static struct kmem_cache_node __initdata init_kmem_cache_node[NUM_INIT_LISTS];
Christoph Lametere498be72005-09-09 13:03:32 -0700238#define CACHE_CACHE 0
Pekka Enberg556a1692008-01-25 08:20:51 +0200239#define SIZE_AC MAX_NUMNODES
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000240#define SIZE_NODE (2 * MAX_NUMNODES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Christoph Lametered11d9e2006-06-30 01:55:45 -0700242static int drain_freelist(struct kmem_cache *cache,
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000243 struct kmem_cache_node *n, int tofree);
Christoph Lametered11d9e2006-06-30 01:55:45 -0700244static void free_block(struct kmem_cache *cachep, void **objpp, int len,
Joonsoo Kim97654df2014-08-06 16:04:25 -0700245 int node, struct list_head *list);
246static void slabs_destroy(struct kmem_cache *cachep, struct list_head *list);
Pekka Enberg83b519e2009-06-10 19:40:04 +0300247static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp);
David Howells65f27f32006-11-22 14:55:48 +0000248static void cache_reap(struct work_struct *unused);
Christoph Lametered11d9e2006-06-30 01:55:45 -0700249
Ingo Molnare0a42722006-06-23 02:03:46 -0700250static int slab_early_init = 1;
251
Christoph Lametere3366012013-01-10 19:14:18 +0000252#define INDEX_AC kmalloc_index(sizeof(struct arraycache_init))
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000253#define INDEX_NODE kmalloc_index(sizeof(struct kmem_cache_node))
Christoph Lametere498be72005-09-09 13:03:32 -0700254
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000255static void kmem_cache_node_init(struct kmem_cache_node *parent)
Christoph Lametere498be72005-09-09 13:03:32 -0700256{
257 INIT_LIST_HEAD(&parent->slabs_full);
258 INIT_LIST_HEAD(&parent->slabs_partial);
259 INIT_LIST_HEAD(&parent->slabs_free);
260 parent->shared = NULL;
261 parent->alien = NULL;
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -0800262 parent->colour_next = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700263 spin_lock_init(&parent->list_lock);
264 parent->free_objects = 0;
265 parent->free_touched = 0;
266}
267
Andrew Mortona737b3e2006-03-22 00:08:11 -0800268#define MAKE_LIST(cachep, listp, slab, nodeid) \
269 do { \
270 INIT_LIST_HEAD(listp); \
Christoph Lameter18bf8542014-08-06 16:04:11 -0700271 list_splice(&get_node(cachep, nodeid)->slab, listp); \
Christoph Lametere498be72005-09-09 13:03:32 -0700272 } while (0)
273
Andrew Mortona737b3e2006-03-22 00:08:11 -0800274#define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
275 do { \
Christoph Lametere498be72005-09-09 13:03:32 -0700276 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
277 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
278 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
279 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281#define CFLGS_OFF_SLAB (0x80000000UL)
282#define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
283
284#define BATCHREFILL_LIMIT 16
Andrew Mortona737b3e2006-03-22 00:08:11 -0800285/*
286 * Optimization question: fewer reaps means less probability for unnessary
287 * cpucache drain/refill cycles.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 *
Adrian Bunkdc6f3f22005-11-08 16:44:08 +0100289 * OTOH the cpuarrays can contain lots of objects,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 * which could lock up otherwise freeable slabs.
291 */
Jianyu Zhan5f0985b2014-03-30 17:02:20 +0800292#define REAPTIMEOUT_AC (2*HZ)
293#define REAPTIMEOUT_NODE (4*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
295#if STATS
296#define STATS_INC_ACTIVE(x) ((x)->num_active++)
297#define STATS_DEC_ACTIVE(x) ((x)->num_active--)
298#define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
299#define STATS_INC_GROWN(x) ((x)->grown++)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700300#define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
Andrew Mortona737b3e2006-03-22 00:08:11 -0800301#define STATS_SET_HIGH(x) \
302 do { \
303 if ((x)->num_active > (x)->high_mark) \
304 (x)->high_mark = (x)->num_active; \
305 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306#define STATS_INC_ERR(x) ((x)->errors++)
307#define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
Christoph Lametere498be72005-09-09 13:03:32 -0700308#define STATS_INC_NODEFREES(x) ((x)->node_frees++)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700309#define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800310#define STATS_SET_FREEABLE(x, i) \
311 do { \
312 if ((x)->max_freeable < i) \
313 (x)->max_freeable = i; \
314 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315#define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
316#define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
317#define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
318#define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
319#else
320#define STATS_INC_ACTIVE(x) do { } while (0)
321#define STATS_DEC_ACTIVE(x) do { } while (0)
322#define STATS_INC_ALLOCED(x) do { } while (0)
323#define STATS_INC_GROWN(x) do { } while (0)
Andi Kleen4e60c862010-08-09 17:19:03 -0700324#define STATS_ADD_REAPED(x,y) do { (void)(y); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325#define STATS_SET_HIGH(x) do { } while (0)
326#define STATS_INC_ERR(x) do { } while (0)
327#define STATS_INC_NODEALLOCS(x) do { } while (0)
Christoph Lametere498be72005-09-09 13:03:32 -0700328#define STATS_INC_NODEFREES(x) do { } while (0)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700329#define STATS_INC_ACOVERFLOW(x) do { } while (0)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800330#define STATS_SET_FREEABLE(x, i) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331#define STATS_INC_ALLOCHIT(x) do { } while (0)
332#define STATS_INC_ALLOCMISS(x) do { } while (0)
333#define STATS_INC_FREEHIT(x) do { } while (0)
334#define STATS_INC_FREEMISS(x) do { } while (0)
335#endif
336
337#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Andrew Mortona737b3e2006-03-22 00:08:11 -0800339/*
340 * memory layout of objects:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 * 0 : objp
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800342 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 * the end of an object is aligned with the end of the real
344 * allocation. Catches writes behind the end of the allocation.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800345 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 * redzone word.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800347 * cachep->obj_offset: The real object.
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500348 * cachep->size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
349 * cachep->size - 1* BYTES_PER_WORD: last caller address
Andrew Mortona737b3e2006-03-22 00:08:11 -0800350 * [BYTES_PER_WORD long]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800352static int obj_offset(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800354 return cachep->obj_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
David Woodhouseb46b8f12007-05-08 00:22:59 -0700357static unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
David Woodhouseb46b8f12007-05-08 00:22:59 -0700360 return (unsigned long long*) (objp + obj_offset(cachep) -
361 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
David Woodhouseb46b8f12007-05-08 00:22:59 -0700364static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
367 if (cachep->flags & SLAB_STORE_USER)
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500368 return (unsigned long long *)(objp + cachep->size -
David Woodhouseb46b8f12007-05-08 00:22:59 -0700369 sizeof(unsigned long long) -
David Woodhouse87a927c2007-07-04 21:26:44 -0400370 REDZONE_ALIGN);
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500371 return (unsigned long long *) (objp + cachep->size -
David Woodhouseb46b8f12007-05-08 00:22:59 -0700372 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Pekka Enberg343e0d72006-02-01 03:05:50 -0800375static void **dbg_userword(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
377 BUG_ON(!(cachep->flags & SLAB_STORE_USER));
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500378 return (void **)(objp + cachep->size - BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
381#else
382
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800383#define obj_offset(x) 0
David Woodhouseb46b8f12007-05-08 00:22:59 -0700384#define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
385#define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386#define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
387
388#endif
389
Joonsoo Kim03787302014-06-23 13:22:06 -0700390#define OBJECT_FREE (0)
391#define OBJECT_ACTIVE (1)
392
393#ifdef CONFIG_DEBUG_SLAB_LEAK
394
395static void set_obj_status(struct page *page, int idx, int val)
396{
397 int freelist_size;
398 char *status;
399 struct kmem_cache *cachep = page->slab_cache;
400
401 freelist_size = cachep->num * sizeof(freelist_idx_t);
402 status = (char *)page->freelist + freelist_size;
403 status[idx] = val;
404}
405
406static inline unsigned int get_obj_status(struct page *page, int idx)
407{
408 int freelist_size;
409 char *status;
410 struct kmem_cache *cachep = page->slab_cache;
411
412 freelist_size = cachep->num * sizeof(freelist_idx_t);
413 status = (char *)page->freelist + freelist_size;
414
415 return status[idx];
416}
417
418#else
419static inline void set_obj_status(struct page *page, int idx, int val) {}
420
421#endif
422
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423/*
David Rientjes3df1ccc2011-10-18 22:09:28 -0700424 * Do not go above this order unless 0 objects fit into the slab or
425 * overridden on the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 */
David Rientjes543585c2011-10-18 22:09:24 -0700427#define SLAB_MAX_ORDER_HI 1
428#define SLAB_MAX_ORDER_LO 0
429static int slab_max_order = SLAB_MAX_ORDER_LO;
David Rientjes3df1ccc2011-10-18 22:09:28 -0700430static bool slab_max_order_set __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800432static inline struct kmem_cache *virt_to_cache(const void *obj)
433{
Christoph Lameterb49af682007-05-06 14:49:41 -0700434 struct page *page = virt_to_head_page(obj);
Christoph Lameter35026082012-06-13 10:24:56 -0500435 return page->slab_cache;
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800436}
437
Joonsoo Kim8456a642013-10-24 10:07:49 +0900438static inline void *index_to_obj(struct kmem_cache *cache, struct page *page,
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800439 unsigned int idx)
440{
Joonsoo Kim8456a642013-10-24 10:07:49 +0900441 return page->s_mem + cache->size * idx;
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800442}
443
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800444/*
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500445 * We want to avoid an expensive divide : (offset / cache->size)
446 * Using the fact that size is a constant for a particular cache,
447 * we can replace (offset / cache->size) by
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800448 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
449 */
450static inline unsigned int obj_to_index(const struct kmem_cache *cache,
Joonsoo Kim8456a642013-10-24 10:07:49 +0900451 const struct page *page, void *obj)
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800452{
Joonsoo Kim8456a642013-10-24 10:07:49 +0900453 u32 offset = (obj - page->s_mem);
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800454 return reciprocal_divide(offset, cache->reciprocal_buffer_size);
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800455}
456
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457static struct arraycache_init initarray_generic =
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800458 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
460/* internal cache of cache description objs */
Christoph Lameter9b030cb2012-09-05 00:20:33 +0000461static struct kmem_cache kmem_cache_boot = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800462 .batchcount = 1,
463 .limit = BOOT_CPUCACHE_ENTRIES,
464 .shared = 1,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500465 .size = sizeof(struct kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800466 .name = "kmem_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467};
468
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700469#define BAD_ALIEN_MAGIC 0x01020304ul
470
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200471#ifdef CONFIG_LOCKDEP
472
473/*
474 * Slab sometimes uses the kmalloc slabs to store the slab headers
475 * for other slabs "off slab".
476 * The locking for this is tricky in that it nests within the locks
477 * of all other slabs in a few places; to deal with this special
478 * locking we put on-slab caches into a separate lock-class.
479 *
480 * We set lock class for alien array caches which are up during init.
481 * The lock annotation will be lost if all cpus of a node goes down and
482 * then comes back up during hotplug
483 */
484static struct lock_class_key on_slab_l3_key;
485static struct lock_class_key on_slab_alc_key;
486
Peter Zijlstra83835b32011-07-22 15:26:05 +0200487static struct lock_class_key debugobj_l3_key;
488static struct lock_class_key debugobj_alc_key;
489
490static void slab_set_lock_classes(struct kmem_cache *cachep,
491 struct lock_class_key *l3_key, struct lock_class_key *alc_key,
Christoph Lameter18bf8542014-08-06 16:04:11 -0700492 struct kmem_cache_node *n)
Peter Zijlstra83835b32011-07-22 15:26:05 +0200493{
494 struct array_cache **alc;
Peter Zijlstra83835b32011-07-22 15:26:05 +0200495 int r;
496
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000497 lockdep_set_class(&n->list_lock, l3_key);
498 alc = n->alien;
Peter Zijlstra83835b32011-07-22 15:26:05 +0200499 /*
500 * FIXME: This check for BAD_ALIEN_MAGIC
501 * should go away when common slab code is taught to
502 * work even without alien caches.
503 * Currently, non NUMA code returns BAD_ALIEN_MAGIC
504 * for alloc_alien_cache,
505 */
506 if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC)
507 return;
508 for_each_node(r) {
509 if (alc[r])
510 lockdep_set_class(&alc[r]->lock, alc_key);
511 }
512}
513
Christoph Lameter18bf8542014-08-06 16:04:11 -0700514static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep,
515 struct kmem_cache_node *n)
Peter Zijlstra83835b32011-07-22 15:26:05 +0200516{
Christoph Lameter18bf8542014-08-06 16:04:11 -0700517 slab_set_lock_classes(cachep, &debugobj_l3_key, &debugobj_alc_key, n);
Peter Zijlstra83835b32011-07-22 15:26:05 +0200518}
519
520static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
521{
522 int node;
Christoph Lameter18bf8542014-08-06 16:04:11 -0700523 struct kmem_cache_node *n;
Peter Zijlstra83835b32011-07-22 15:26:05 +0200524
Christoph Lameter18bf8542014-08-06 16:04:11 -0700525 for_each_kmem_cache_node(cachep, node, n)
526 slab_set_debugobj_lock_classes_node(cachep, n);
Peter Zijlstra83835b32011-07-22 15:26:05 +0200527}
528
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200529static void init_node_lock_keys(int q)
530{
Christoph Lametere3366012013-01-10 19:14:18 +0000531 int i;
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200532
Christoph Lameter97d06602012-07-06 15:25:11 -0500533 if (slab_state < UP)
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200534 return;
535
Christoph Lameter0f8f8092013-07-02 12:12:10 -0700536 for (i = 1; i <= KMALLOC_SHIFT_HIGH; i++) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000537 struct kmem_cache_node *n;
Christoph Lametere3366012013-01-10 19:14:18 +0000538 struct kmem_cache *cache = kmalloc_caches[i];
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200539
Christoph Lametere3366012013-01-10 19:14:18 +0000540 if (!cache)
Pekka Enberg00afa752009-12-27 14:33:14 +0200541 continue;
Peter Zijlstra83835b32011-07-22 15:26:05 +0200542
Christoph Lameter18bf8542014-08-06 16:04:11 -0700543 n = get_node(cache, q);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000544 if (!n || OFF_SLAB(cache))
Christoph Lametere3366012013-01-10 19:14:18 +0000545 continue;
546
547 slab_set_lock_classes(cache, &on_slab_l3_key,
Christoph Lameter18bf8542014-08-06 16:04:11 -0700548 &on_slab_alc_key, n);
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200549 }
550}
551
Christoph Lameter18bf8542014-08-06 16:04:11 -0700552static void on_slab_lock_classes_node(struct kmem_cache *cachep,
553 struct kmem_cache_node *n)
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800554{
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800555 slab_set_lock_classes(cachep, &on_slab_l3_key,
Christoph Lameter18bf8542014-08-06 16:04:11 -0700556 &on_slab_alc_key, n);
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800557}
558
559static inline void on_slab_lock_classes(struct kmem_cache *cachep)
560{
561 int node;
Christoph Lameter18bf8542014-08-06 16:04:11 -0700562 struct kmem_cache_node *n;
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800563
564 VM_BUG_ON(OFF_SLAB(cachep));
Christoph Lameter18bf8542014-08-06 16:04:11 -0700565 for_each_kmem_cache_node(cachep, node, n)
566 on_slab_lock_classes_node(cachep, n);
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800567}
568
Fabian Frederick1536cb32014-08-06 16:04:05 -0700569static inline void __init init_lock_keys(void)
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200570{
571 int node;
572
573 for_each_node(node)
574 init_node_lock_keys(node);
575}
576#else
Fabian Frederick1536cb32014-08-06 16:04:05 -0700577static void __init init_node_lock_keys(int q)
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200578{
579}
580
581static inline void init_lock_keys(void)
582{
583}
Peter Zijlstra83835b32011-07-22 15:26:05 +0200584
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800585static inline void on_slab_lock_classes(struct kmem_cache *cachep)
586{
587}
588
Christoph Lameter18bf8542014-08-06 16:04:11 -0700589static inline void on_slab_lock_classes_node(struct kmem_cache *cachep,
590 struct kmem_cache_node *n)
Glauber Costa6ccfb5b2012-12-18 14:22:31 -0800591{
592}
593
Christoph Lameter18bf8542014-08-06 16:04:11 -0700594static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep,
595 struct kmem_cache_node *n)
Peter Zijlstra83835b32011-07-22 15:26:05 +0200596{
597}
598
599static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
600{
601}
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200602#endif
603
Tejun Heo1871e522009-10-29 22:34:13 +0900604static DEFINE_PER_CPU(struct delayed_work, slab_reap_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Pekka Enberg343e0d72006-02-01 03:05:50 -0800606static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607{
608 return cachep->array[smp_processor_id()];
609}
610
Joonsoo Kim03787302014-06-23 13:22:06 -0700611static size_t calculate_freelist_size(int nr_objs, size_t align)
612{
613 size_t freelist_size;
614
615 freelist_size = nr_objs * sizeof(freelist_idx_t);
616 if (IS_ENABLED(CONFIG_DEBUG_SLAB_LEAK))
617 freelist_size += nr_objs * sizeof(char);
618
619 if (align)
620 freelist_size = ALIGN(freelist_size, align);
621
622 return freelist_size;
623}
624
Joonsoo Kim9cef2e22013-12-02 17:49:39 +0900625static int calculate_nr_objs(size_t slab_size, size_t buffer_size,
626 size_t idx_size, size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Joonsoo Kim9cef2e22013-12-02 17:49:39 +0900628 int nr_objs;
Joonsoo Kim03787302014-06-23 13:22:06 -0700629 size_t remained_size;
Joonsoo Kim9cef2e22013-12-02 17:49:39 +0900630 size_t freelist_size;
Joonsoo Kim03787302014-06-23 13:22:06 -0700631 int extra_space = 0;
Joonsoo Kim9cef2e22013-12-02 17:49:39 +0900632
Joonsoo Kim03787302014-06-23 13:22:06 -0700633 if (IS_ENABLED(CONFIG_DEBUG_SLAB_LEAK))
634 extra_space = sizeof(char);
Joonsoo Kim9cef2e22013-12-02 17:49:39 +0900635 /*
636 * Ignore padding for the initial guess. The padding
637 * is at most @align-1 bytes, and @buffer_size is at
638 * least @align. In the worst case, this result will
639 * be one greater than the number of objects that fit
640 * into the memory allocation when taking the padding
641 * into account.
642 */
Joonsoo Kim03787302014-06-23 13:22:06 -0700643 nr_objs = slab_size / (buffer_size + idx_size + extra_space);
Joonsoo Kim9cef2e22013-12-02 17:49:39 +0900644
645 /*
646 * This calculated number will be either the right
647 * amount, or one greater than what we want.
648 */
Joonsoo Kim03787302014-06-23 13:22:06 -0700649 remained_size = slab_size - nr_objs * buffer_size;
650 freelist_size = calculate_freelist_size(nr_objs, align);
651 if (remained_size < freelist_size)
Joonsoo Kim9cef2e22013-12-02 17:49:39 +0900652 nr_objs--;
653
654 return nr_objs;
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800655}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Andrew Mortona737b3e2006-03-22 00:08:11 -0800657/*
658 * Calculate the number of objects and left-over bytes for a given buffer size.
659 */
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800660static void cache_estimate(unsigned long gfporder, size_t buffer_size,
661 size_t align, int flags, size_t *left_over,
662 unsigned int *num)
663{
664 int nr_objs;
665 size_t mgmt_size;
666 size_t slab_size = PAGE_SIZE << gfporder;
667
668 /*
669 * The slab management structure can be either off the slab or
670 * on it. For the latter case, the memory allocated for a
671 * slab is used for:
672 *
Joonsoo Kim16025172013-10-24 10:07:46 +0900673 * - One unsigned int for each object
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800674 * - Padding to respect alignment of @align
675 * - @buffer_size bytes for each object
676 *
677 * If the slab management structure is off the slab, then the
678 * alignment will already be calculated into the size. Because
679 * the slabs are all pages aligned, the objects will be at the
680 * correct alignment when allocated.
681 */
682 if (flags & CFLGS_OFF_SLAB) {
683 mgmt_size = 0;
684 nr_objs = slab_size / buffer_size;
685
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800686 } else {
Joonsoo Kim9cef2e22013-12-02 17:49:39 +0900687 nr_objs = calculate_nr_objs(slab_size, buffer_size,
Joonsoo Kima41adfa2013-12-02 17:49:42 +0900688 sizeof(freelist_idx_t), align);
Joonsoo Kim03787302014-06-23 13:22:06 -0700689 mgmt_size = calculate_freelist_size(nr_objs, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800691 *num = nr_objs;
692 *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
694
Christoph Lameterf28510d2012-09-11 19:49:38 +0000695#if DEBUG
Harvey Harrisond40cee22008-04-30 00:55:07 -0700696#define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Andrew Mortona737b3e2006-03-22 00:08:11 -0800698static void __slab_error(const char *function, struct kmem_cache *cachep,
699 char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800702 function, cachep->name, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +1030704 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
Christoph Lameterf28510d2012-09-11 19:49:38 +0000706#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
Paul Menage3395ee02006-12-06 20:32:16 -0800708/*
709 * By default on NUMA we use alien caches to stage the freeing of
710 * objects allocated from other nodes. This causes massive memory
711 * inefficiencies when using fake NUMA setup to split memory into a
712 * large number of small nodes, so it can be disabled on the command
713 * line
714 */
715
716static int use_alien_caches __read_mostly = 1;
717static int __init noaliencache_setup(char *s)
718{
719 use_alien_caches = 0;
720 return 1;
721}
722__setup("noaliencache", noaliencache_setup);
723
David Rientjes3df1ccc2011-10-18 22:09:28 -0700724static int __init slab_max_order_setup(char *str)
725{
726 get_option(&str, &slab_max_order);
727 slab_max_order = slab_max_order < 0 ? 0 :
728 min(slab_max_order, MAX_ORDER - 1);
729 slab_max_order_set = true;
730
731 return 1;
732}
733__setup("slab_max_order=", slab_max_order_setup);
734
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800735#ifdef CONFIG_NUMA
736/*
737 * Special reaping functions for NUMA systems called from cache_reap().
738 * These take care of doing round robin flushing of alien caches (containing
739 * objects freed on different nodes from which they were allocated) and the
740 * flushing of remote pcps by calling drain_node_pages.
741 */
Tejun Heo1871e522009-10-29 22:34:13 +0900742static DEFINE_PER_CPU(unsigned long, slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800743
744static void init_reap_node(int cpu)
745{
746 int node;
747
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -0700748 node = next_node(cpu_to_mem(cpu), node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800749 if (node == MAX_NUMNODES)
Paul Jackson442295c2006-03-22 00:09:11 -0800750 node = first_node(node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800751
Tejun Heo1871e522009-10-29 22:34:13 +0900752 per_cpu(slab_reap_node, cpu) = node;
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800753}
754
755static void next_reap_node(void)
756{
Christoph Lameter909ea962010-12-08 16:22:55 +0100757 int node = __this_cpu_read(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800758
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800759 node = next_node(node, node_online_map);
760 if (unlikely(node >= MAX_NUMNODES))
761 node = first_node(node_online_map);
Christoph Lameter909ea962010-12-08 16:22:55 +0100762 __this_cpu_write(slab_reap_node, node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800763}
764
765#else
766#define init_reap_node(cpu) do { } while (0)
767#define next_reap_node(void) do { } while (0)
768#endif
769
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770/*
771 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
772 * via the workqueue/eventd.
773 * Add the CPU number into the expiration time to minimize the possibility of
774 * the CPUs getting into lockstep and contending for the global cache chain
775 * lock.
776 */
Paul Gortmaker0db06282013-06-19 14:53:51 -0400777static void start_cpu_timer(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Tejun Heo1871e522009-10-29 22:34:13 +0900779 struct delayed_work *reap_work = &per_cpu(slab_reap_work, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 /*
782 * When this gets called from do_initcalls via cpucache_init(),
783 * init_workqueues() has already run, so keventd will be setup
784 * at that time.
785 */
David Howells52bad642006-11-22 14:54:01 +0000786 if (keventd_up() && reap_work->work.func == NULL) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800787 init_reap_node(cpu);
Tejun Heo203b42f2012-08-21 13:18:23 -0700788 INIT_DEFERRABLE_WORK(reap_work, cache_reap);
Arjan van de Ven2b284212006-12-10 02:21:28 -0800789 schedule_delayed_work_on(cpu, reap_work,
790 __round_jiffies_relative(HZ, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 }
792}
793
Christoph Lametere498be72005-09-09 13:03:32 -0700794static struct array_cache *alloc_arraycache(int node, int entries,
Pekka Enberg83b519e2009-06-10 19:40:04 +0300795 int batchcount, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800797 int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 struct array_cache *nc = NULL;
799
Pekka Enberg83b519e2009-06-10 19:40:04 +0300800 nc = kmalloc_node(memsize, gfp, node);
Catalin Marinasd5cff632009-06-11 13:22:40 +0100801 /*
802 * The array_cache structures contain pointers to free object.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300803 * However, when such objects are allocated or transferred to another
Catalin Marinasd5cff632009-06-11 13:22:40 +0100804 * cache the pointers are not cleared and they could be counted as
805 * valid references during a kmemleak scan. Therefore, kmemleak must
806 * not scan such objects.
807 */
808 kmemleak_no_scan(nc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 if (nc) {
810 nc->avail = 0;
811 nc->limit = entries;
812 nc->batchcount = batchcount;
813 nc->touched = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700814 spin_lock_init(&nc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 }
816 return nc;
817}
818
Joonsoo Kim8456a642013-10-24 10:07:49 +0900819static inline bool is_slab_pfmemalloc(struct page *page)
Mel Gorman072bb0a2012-07-31 16:43:58 -0700820{
Mel Gorman072bb0a2012-07-31 16:43:58 -0700821 return PageSlabPfmemalloc(page);
822}
823
824/* Clears pfmemalloc_active if no slabs have pfmalloc set */
825static void recheck_pfmemalloc_active(struct kmem_cache *cachep,
826 struct array_cache *ac)
827{
Christoph Lameter18bf8542014-08-06 16:04:11 -0700828 struct kmem_cache_node *n = get_node(cachep, numa_mem_id());
Joonsoo Kim8456a642013-10-24 10:07:49 +0900829 struct page *page;
Mel Gorman072bb0a2012-07-31 16:43:58 -0700830 unsigned long flags;
831
832 if (!pfmemalloc_active)
833 return;
834
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000835 spin_lock_irqsave(&n->list_lock, flags);
Joonsoo Kim8456a642013-10-24 10:07:49 +0900836 list_for_each_entry(page, &n->slabs_full, lru)
837 if (is_slab_pfmemalloc(page))
Mel Gorman072bb0a2012-07-31 16:43:58 -0700838 goto out;
839
Joonsoo Kim8456a642013-10-24 10:07:49 +0900840 list_for_each_entry(page, &n->slabs_partial, lru)
841 if (is_slab_pfmemalloc(page))
Mel Gorman072bb0a2012-07-31 16:43:58 -0700842 goto out;
843
Joonsoo Kim8456a642013-10-24 10:07:49 +0900844 list_for_each_entry(page, &n->slabs_free, lru)
845 if (is_slab_pfmemalloc(page))
Mel Gorman072bb0a2012-07-31 16:43:58 -0700846 goto out;
847
848 pfmemalloc_active = false;
849out:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000850 spin_unlock_irqrestore(&n->list_lock, flags);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700851}
852
Mel Gorman381760e2012-07-31 16:44:30 -0700853static void *__ac_get_obj(struct kmem_cache *cachep, struct array_cache *ac,
Mel Gorman072bb0a2012-07-31 16:43:58 -0700854 gfp_t flags, bool force_refill)
855{
856 int i;
857 void *objp = ac->entry[--ac->avail];
858
859 /* Ensure the caller is allowed to use objects from PFMEMALLOC slab */
860 if (unlikely(is_obj_pfmemalloc(objp))) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000861 struct kmem_cache_node *n;
Mel Gorman072bb0a2012-07-31 16:43:58 -0700862
863 if (gfp_pfmemalloc_allowed(flags)) {
864 clear_obj_pfmemalloc(&objp);
865 return objp;
866 }
867
868 /* The caller cannot use PFMEMALLOC objects, find another one */
Joonsoo Kimd014dc22012-09-17 14:09:06 -0700869 for (i = 0; i < ac->avail; i++) {
Mel Gorman072bb0a2012-07-31 16:43:58 -0700870 /* If a !PFMEMALLOC object is found, swap them */
871 if (!is_obj_pfmemalloc(ac->entry[i])) {
872 objp = ac->entry[i];
873 ac->entry[i] = ac->entry[ac->avail];
874 ac->entry[ac->avail] = objp;
875 return objp;
876 }
877 }
878
879 /*
880 * If there are empty slabs on the slabs_free list and we are
881 * being forced to refill the cache, mark this one !pfmemalloc.
882 */
Christoph Lameter18bf8542014-08-06 16:04:11 -0700883 n = get_node(cachep, numa_mem_id());
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000884 if (!list_empty(&n->slabs_free) && force_refill) {
Joonsoo Kim8456a642013-10-24 10:07:49 +0900885 struct page *page = virt_to_head_page(objp);
Joonsoo Kim7ecccf92013-10-24 10:07:50 +0900886 ClearPageSlabPfmemalloc(page);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700887 clear_obj_pfmemalloc(&objp);
888 recheck_pfmemalloc_active(cachep, ac);
889 return objp;
890 }
891
892 /* No !PFMEMALLOC objects available */
893 ac->avail++;
894 objp = NULL;
895 }
896
897 return objp;
898}
899
Mel Gorman381760e2012-07-31 16:44:30 -0700900static inline void *ac_get_obj(struct kmem_cache *cachep,
901 struct array_cache *ac, gfp_t flags, bool force_refill)
902{
903 void *objp;
904
905 if (unlikely(sk_memalloc_socks()))
906 objp = __ac_get_obj(cachep, ac, flags, force_refill);
907 else
908 objp = ac->entry[--ac->avail];
909
910 return objp;
911}
912
913static void *__ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
Mel Gorman072bb0a2012-07-31 16:43:58 -0700914 void *objp)
915{
916 if (unlikely(pfmemalloc_active)) {
917 /* Some pfmemalloc slabs exist, check if this is one */
Mel Gorman30c29be2012-09-17 14:09:03 -0700918 struct page *page = virt_to_head_page(objp);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700919 if (PageSlabPfmemalloc(page))
920 set_obj_pfmemalloc(&objp);
921 }
922
Mel Gorman381760e2012-07-31 16:44:30 -0700923 return objp;
924}
925
926static inline void ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
927 void *objp)
928{
929 if (unlikely(sk_memalloc_socks()))
930 objp = __ac_put_obj(cachep, ac, objp);
931
Mel Gorman072bb0a2012-07-31 16:43:58 -0700932 ac->entry[ac->avail++] = objp;
933}
934
Christoph Lameter3ded1752006-03-25 03:06:44 -0800935/*
936 * Transfer objects in one arraycache to another.
937 * Locking must be handled by the caller.
938 *
939 * Return the number of entries transferred.
940 */
941static int transfer_objects(struct array_cache *to,
942 struct array_cache *from, unsigned int max)
943{
944 /* Figure out how many entries to transfer */
Hagen Paul Pfeifer732eacc2010-10-26 14:22:23 -0700945 int nr = min3(from->avail, max, to->limit - to->avail);
Christoph Lameter3ded1752006-03-25 03:06:44 -0800946
947 if (!nr)
948 return 0;
949
950 memcpy(to->entry + to->avail, from->entry + from->avail -nr,
951 sizeof(void *) *nr);
952
953 from->avail -= nr;
954 to->avail += nr;
Christoph Lameter3ded1752006-03-25 03:06:44 -0800955 return nr;
956}
957
Christoph Lameter765c4502006-09-27 01:50:08 -0700958#ifndef CONFIG_NUMA
959
960#define drain_alien_cache(cachep, alien) do { } while (0)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000961#define reap_alien(cachep, n) do { } while (0)
Christoph Lameter765c4502006-09-27 01:50:08 -0700962
Pekka Enberg83b519e2009-06-10 19:40:04 +0300963static inline struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
Christoph Lameter765c4502006-09-27 01:50:08 -0700964{
965 return (struct array_cache **)BAD_ALIEN_MAGIC;
966}
967
968static inline void free_alien_cache(struct array_cache **ac_ptr)
969{
970}
971
972static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
973{
974 return 0;
975}
976
977static inline void *alternate_node_alloc(struct kmem_cache *cachep,
978 gfp_t flags)
979{
980 return NULL;
981}
982
Christoph Hellwig8b98c162006-12-06 20:32:30 -0800983static inline void *____cache_alloc_node(struct kmem_cache *cachep,
Christoph Lameter765c4502006-09-27 01:50:08 -0700984 gfp_t flags, int nodeid)
985{
986 return NULL;
987}
988
989#else /* CONFIG_NUMA */
990
Christoph Hellwig8b98c162006-12-06 20:32:30 -0800991static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
Paul Jacksonc61afb12006-03-24 03:16:08 -0800992static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
Christoph Lameterdc85da12006-01-18 17:42:36 -0800993
Pekka Enberg83b519e2009-06-10 19:40:04 +0300994static struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -0700995{
996 struct array_cache **ac_ptr;
Christoph Lameter8ef82862007-02-20 13:57:52 -0800997 int memsize = sizeof(void *) * nr_node_ids;
Christoph Lametere498be72005-09-09 13:03:32 -0700998 int i;
999
1000 if (limit > 1)
1001 limit = 12;
Haicheng Lif3186a92010-01-06 15:25:23 +08001002 ac_ptr = kzalloc_node(memsize, gfp, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001003 if (ac_ptr) {
1004 for_each_node(i) {
Haicheng Lif3186a92010-01-06 15:25:23 +08001005 if (i == node || !node_online(i))
Christoph Lametere498be72005-09-09 13:03:32 -07001006 continue;
Pekka Enberg83b519e2009-06-10 19:40:04 +03001007 ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d, gfp);
Christoph Lametere498be72005-09-09 13:03:32 -07001008 if (!ac_ptr[i]) {
Akinobu Mitacc550de2007-11-14 16:58:35 -08001009 for (i--; i >= 0; i--)
Christoph Lametere498be72005-09-09 13:03:32 -07001010 kfree(ac_ptr[i]);
1011 kfree(ac_ptr);
1012 return NULL;
1013 }
1014 }
1015 }
1016 return ac_ptr;
1017}
1018
Pekka Enberg5295a742006-02-01 03:05:48 -08001019static void free_alien_cache(struct array_cache **ac_ptr)
Christoph Lametere498be72005-09-09 13:03:32 -07001020{
1021 int i;
1022
1023 if (!ac_ptr)
1024 return;
Christoph Lametere498be72005-09-09 13:03:32 -07001025 for_each_node(i)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001026 kfree(ac_ptr[i]);
Christoph Lametere498be72005-09-09 13:03:32 -07001027 kfree(ac_ptr);
1028}
1029
Pekka Enberg343e0d72006-02-01 03:05:50 -08001030static void __drain_alien_cache(struct kmem_cache *cachep,
Pekka Enberg5295a742006-02-01 03:05:48 -08001031 struct array_cache *ac, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07001032{
Christoph Lameter18bf8542014-08-06 16:04:11 -07001033 struct kmem_cache_node *n = get_node(cachep, node);
Joonsoo Kim97654df2014-08-06 16:04:25 -07001034 LIST_HEAD(list);
Christoph Lametere498be72005-09-09 13:03:32 -07001035
1036 if (ac->avail) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001037 spin_lock(&n->list_lock);
Christoph Lametere00946f2006-03-25 03:06:45 -08001038 /*
1039 * Stuff objects into the remote nodes shared array first.
1040 * That way we could avoid the overhead of putting the objects
1041 * into the free lists and getting them back later.
1042 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001043 if (n->shared)
1044 transfer_objects(n->shared, ac, ac->limit);
Christoph Lametere00946f2006-03-25 03:06:45 -08001045
Joonsoo Kim97654df2014-08-06 16:04:25 -07001046 free_block(cachep, ac->entry, ac->avail, node, &list);
Christoph Lametere498be72005-09-09 13:03:32 -07001047 ac->avail = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001048 spin_unlock(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07001049 slabs_destroy(cachep, &list);
Christoph Lametere498be72005-09-09 13:03:32 -07001050 }
1051}
1052
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001053/*
1054 * Called from cache_reap() to regularly drain alien caches round robin.
1055 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001056static void reap_alien(struct kmem_cache *cachep, struct kmem_cache_node *n)
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001057{
Christoph Lameter909ea962010-12-08 16:22:55 +01001058 int node = __this_cpu_read(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001059
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001060 if (n->alien) {
1061 struct array_cache *ac = n->alien[node];
Christoph Lametere00946f2006-03-25 03:06:45 -08001062
1063 if (ac && ac->avail && spin_trylock_irq(&ac->lock)) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001064 __drain_alien_cache(cachep, ac, node);
1065 spin_unlock_irq(&ac->lock);
1066 }
1067 }
1068}
1069
Andrew Mortona737b3e2006-03-22 00:08:11 -08001070static void drain_alien_cache(struct kmem_cache *cachep,
1071 struct array_cache **alien)
Christoph Lametere498be72005-09-09 13:03:32 -07001072{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001073 int i = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07001074 struct array_cache *ac;
1075 unsigned long flags;
1076
1077 for_each_online_node(i) {
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001078 ac = alien[i];
Christoph Lametere498be72005-09-09 13:03:32 -07001079 if (ac) {
1080 spin_lock_irqsave(&ac->lock, flags);
1081 __drain_alien_cache(cachep, ac, i);
1082 spin_unlock_irqrestore(&ac->lock, flags);
1083 }
1084 }
1085}
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001086
Ingo Molnar873623d2006-07-13 14:44:38 +02001087static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001088{
Joonsoo Kim1ea991b2013-10-24 10:07:40 +09001089 int nodeid = page_to_nid(virt_to_page(objp));
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001090 struct kmem_cache_node *n;
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001091 struct array_cache *alien = NULL;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001092 int node;
Joonsoo Kim97654df2014-08-06 16:04:25 -07001093 LIST_HEAD(list);
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001094
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001095 node = numa_mem_id();
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001096
1097 /*
1098 * Make sure we are not freeing a object from another node to the array
1099 * cache on this cpu.
1100 */
Joonsoo Kim1ea991b2013-10-24 10:07:40 +09001101 if (likely(nodeid == node))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001102 return 0;
1103
Christoph Lameter18bf8542014-08-06 16:04:11 -07001104 n = get_node(cachep, node);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001105 STATS_INC_NODEFREES(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001106 if (n->alien && n->alien[nodeid]) {
1107 alien = n->alien[nodeid];
Ingo Molnar873623d2006-07-13 14:44:38 +02001108 spin_lock(&alien->lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001109 if (unlikely(alien->avail == alien->limit)) {
1110 STATS_INC_ACOVERFLOW(cachep);
1111 __drain_alien_cache(cachep, alien, nodeid);
1112 }
Mel Gorman072bb0a2012-07-31 16:43:58 -07001113 ac_put_obj(cachep, alien, objp);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001114 spin_unlock(&alien->lock);
1115 } else {
Christoph Lameter18bf8542014-08-06 16:04:11 -07001116 n = get_node(cachep, nodeid);
1117 spin_lock(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07001118 free_block(cachep, &objp, 1, nodeid, &list);
Christoph Lameter18bf8542014-08-06 16:04:11 -07001119 spin_unlock(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07001120 slabs_destroy(cachep, &list);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001121 }
1122 return 1;
1123}
Christoph Lametere498be72005-09-09 13:03:32 -07001124#endif
1125
David Rientjes8f9f8d92010-03-27 19:40:47 -07001126/*
Christoph Lameter6a673682013-01-10 19:14:19 +00001127 * Allocates and initializes node for a node on each slab cache, used for
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001128 * either memory or cpu hotplug. If memory is being hot-added, the kmem_cache_node
David Rientjes8f9f8d92010-03-27 19:40:47 -07001129 * will be allocated off-node since memory is not yet online for the new node.
Christoph Lameter6a673682013-01-10 19:14:19 +00001130 * When hotplugging memory or a cpu, existing node are not replaced if
David Rientjes8f9f8d92010-03-27 19:40:47 -07001131 * already in use.
1132 *
Christoph Lameter18004c52012-07-06 15:25:12 -05001133 * Must hold slab_mutex.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001134 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001135static int init_cache_node_node(int node)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001136{
1137 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001138 struct kmem_cache_node *n;
Christoph Lameter6744f082013-01-10 19:12:17 +00001139 const int memsize = sizeof(struct kmem_cache_node);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001140
Christoph Lameter18004c52012-07-06 15:25:12 -05001141 list_for_each_entry(cachep, &slab_caches, list) {
David Rientjes8f9f8d92010-03-27 19:40:47 -07001142 /*
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08001143 * Set up the kmem_cache_node for cpu before we can
David Rientjes8f9f8d92010-03-27 19:40:47 -07001144 * begin anything. Make sure some other cpu on this
1145 * node has not already allocated this
1146 */
Christoph Lameter18bf8542014-08-06 16:04:11 -07001147 n = get_node(cachep, node);
1148 if (!n) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001149 n = kmalloc_node(memsize, GFP_KERNEL, node);
1150 if (!n)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001151 return -ENOMEM;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001152 kmem_cache_node_init(n);
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08001153 n->next_reap = jiffies + REAPTIMEOUT_NODE +
1154 ((unsigned long)cachep) % REAPTIMEOUT_NODE;
David Rientjes8f9f8d92010-03-27 19:40:47 -07001155
1156 /*
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08001157 * The kmem_cache_nodes don't come and go as CPUs
1158 * come and go. slab_mutex is sufficient
David Rientjes8f9f8d92010-03-27 19:40:47 -07001159 * protection here.
1160 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001161 cachep->node[node] = n;
David Rientjes8f9f8d92010-03-27 19:40:47 -07001162 }
1163
Christoph Lameter18bf8542014-08-06 16:04:11 -07001164 spin_lock_irq(&n->list_lock);
1165 n->free_limit =
David Rientjes8f9f8d92010-03-27 19:40:47 -07001166 (1 + nr_cpus_node(node)) *
1167 cachep->batchcount + cachep->num;
Christoph Lameter18bf8542014-08-06 16:04:11 -07001168 spin_unlock_irq(&n->list_lock);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001169 }
1170 return 0;
1171}
1172
Wanpeng Li0fa81032013-07-04 08:33:22 +08001173static inline int slabs_tofree(struct kmem_cache *cachep,
1174 struct kmem_cache_node *n)
1175{
1176 return (n->free_objects + cachep->num - 1) / cachep->num;
1177}
1178
Paul Gortmaker0db06282013-06-19 14:53:51 -04001179static void cpuup_canceled(long cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180{
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001181 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001182 struct kmem_cache_node *n = NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001183 int node = cpu_to_mem(cpu);
Rusty Russella70f7302009-03-13 14:49:46 +10301184 const struct cpumask *mask = cpumask_of_node(node);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001185
Christoph Lameter18004c52012-07-06 15:25:12 -05001186 list_for_each_entry(cachep, &slab_caches, list) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001187 struct array_cache *nc;
1188 struct array_cache *shared;
1189 struct array_cache **alien;
Joonsoo Kim97654df2014-08-06 16:04:25 -07001190 LIST_HEAD(list);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001191
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001192 /* cpu is dead; no one can alloc from it. */
1193 nc = cachep->array[cpu];
1194 cachep->array[cpu] = NULL;
Christoph Lameter18bf8542014-08-06 16:04:11 -07001195 n = get_node(cachep, node);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001196
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001197 if (!n)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001198 goto free_array_cache;
1199
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001200 spin_lock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001201
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001202 /* Free limit for this kmem_cache_node */
1203 n->free_limit -= cachep->batchcount;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001204 if (nc)
Joonsoo Kim97654df2014-08-06 16:04:25 -07001205 free_block(cachep, nc->entry, nc->avail, node, &list);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001206
Rusty Russell58463c12009-12-17 11:43:12 -06001207 if (!cpumask_empty(mask)) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001208 spin_unlock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001209 goto free_array_cache;
1210 }
1211
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001212 shared = n->shared;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001213 if (shared) {
1214 free_block(cachep, shared->entry,
Joonsoo Kim97654df2014-08-06 16:04:25 -07001215 shared->avail, node, &list);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001216 n->shared = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001217 }
1218
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001219 alien = n->alien;
1220 n->alien = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001221
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001222 spin_unlock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001223
1224 kfree(shared);
1225 if (alien) {
1226 drain_alien_cache(cachep, alien);
1227 free_alien_cache(alien);
1228 }
1229free_array_cache:
Joonsoo Kim97654df2014-08-06 16:04:25 -07001230 slabs_destroy(cachep, &list);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001231 kfree(nc);
1232 }
1233 /*
1234 * In the previous loop, all the objects were freed to
1235 * the respective cache's slabs, now we can go ahead and
1236 * shrink each nodelist to its limit.
1237 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001238 list_for_each_entry(cachep, &slab_caches, list) {
Christoph Lameter18bf8542014-08-06 16:04:11 -07001239 n = get_node(cachep, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001240 if (!n)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001241 continue;
Wanpeng Li0fa81032013-07-04 08:33:22 +08001242 drain_freelist(cachep, n, slabs_tofree(cachep, n));
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001243 }
1244}
1245
Paul Gortmaker0db06282013-06-19 14:53:51 -04001246static int cpuup_prepare(long cpu)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001247{
Pekka Enberg343e0d72006-02-01 03:05:50 -08001248 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001249 struct kmem_cache_node *n = NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001250 int node = cpu_to_mem(cpu);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001251 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001253 /*
1254 * We need to do this right in the beginning since
1255 * alloc_arraycache's are going to use this list.
1256 * kmalloc_node allows us to add the slab to the right
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001257 * kmem_cache_node and not this cpu's kmem_cache_node
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001258 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001259 err = init_cache_node_node(node);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001260 if (err < 0)
1261 goto bad;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001262
1263 /*
1264 * Now we can go ahead with allocating the shared arrays and
1265 * array caches
1266 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001267 list_for_each_entry(cachep, &slab_caches, list) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001268 struct array_cache *nc;
1269 struct array_cache *shared = NULL;
1270 struct array_cache **alien = NULL;
1271
1272 nc = alloc_arraycache(node, cachep->limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001273 cachep->batchcount, GFP_KERNEL);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001274 if (!nc)
1275 goto bad;
1276 if (cachep->shared) {
1277 shared = alloc_arraycache(node,
1278 cachep->shared * cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001279 0xbaadf00d, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001280 if (!shared) {
1281 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001282 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001283 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001284 }
1285 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03001286 alien = alloc_alien_cache(node, cachep->limit, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001287 if (!alien) {
1288 kfree(shared);
1289 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001290 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001291 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001292 }
1293 cachep->array[cpu] = nc;
Christoph Lameter18bf8542014-08-06 16:04:11 -07001294 n = get_node(cachep, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001295 BUG_ON(!n);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001296
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001297 spin_lock_irq(&n->list_lock);
1298 if (!n->shared) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001299 /*
1300 * We are serialised from CPU_DEAD or
1301 * CPU_UP_CANCELLED by the cpucontrol lock
1302 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001303 n->shared = shared;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001304 shared = NULL;
1305 }
1306#ifdef CONFIG_NUMA
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001307 if (!n->alien) {
1308 n->alien = alien;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001309 alien = NULL;
1310 }
1311#endif
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001312 spin_unlock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001313 kfree(shared);
1314 free_alien_cache(alien);
Peter Zijlstra83835b32011-07-22 15:26:05 +02001315 if (cachep->flags & SLAB_DEBUG_OBJECTS)
Christoph Lameter18bf8542014-08-06 16:04:11 -07001316 slab_set_debugobj_lock_classes_node(cachep, n);
Glauber Costa6ccfb5b2012-12-18 14:22:31 -08001317 else if (!OFF_SLAB(cachep) &&
1318 !(cachep->flags & SLAB_DESTROY_BY_RCU))
Christoph Lameter18bf8542014-08-06 16:04:11 -07001319 on_slab_lock_classes_node(cachep, n);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001320 }
Pekka Enbergce79ddc2009-11-23 22:01:15 +02001321 init_node_lock_keys(node);
1322
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001323 return 0;
1324bad:
Akinobu Mita12d00f62007-10-18 03:05:11 -07001325 cpuup_canceled(cpu);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001326 return -ENOMEM;
1327}
1328
Paul Gortmaker0db06282013-06-19 14:53:51 -04001329static int cpuup_callback(struct notifier_block *nfb,
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001330 unsigned long action, void *hcpu)
1331{
1332 long cpu = (long)hcpu;
1333 int err = 0;
1334
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 switch (action) {
Heiko Carstens38c3bd92007-05-09 02:34:05 -07001336 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001337 case CPU_UP_PREPARE_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001338 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001339 err = cpuup_prepare(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001340 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 break;
1342 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001343 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 start_cpu_timer(cpu);
1345 break;
1346#ifdef CONFIG_HOTPLUG_CPU
Christoph Lameter5830c592007-05-09 02:34:22 -07001347 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001348 case CPU_DOWN_PREPARE_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001349 /*
Christoph Lameter18004c52012-07-06 15:25:12 -05001350 * Shutdown cache reaper. Note that the slab_mutex is
Christoph Lameter5830c592007-05-09 02:34:22 -07001351 * held so that if cache_reap() is invoked it cannot do
1352 * anything expensive but will only modify reap_work
1353 * and reschedule the timer.
1354 */
Tejun Heoafe2c512010-12-14 16:21:17 +01001355 cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu));
Christoph Lameter5830c592007-05-09 02:34:22 -07001356 /* Now the cache_reaper is guaranteed to be not running. */
Tejun Heo1871e522009-10-29 22:34:13 +09001357 per_cpu(slab_reap_work, cpu).work.func = NULL;
Christoph Lameter5830c592007-05-09 02:34:22 -07001358 break;
1359 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001360 case CPU_DOWN_FAILED_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001361 start_cpu_timer(cpu);
1362 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001364 case CPU_DEAD_FROZEN:
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001365 /*
1366 * Even if all the cpus of a node are down, we don't free the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001367 * kmem_cache_node of any cache. This to avoid a race between
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001368 * cpu_down, and a kmalloc allocation from another cpu for
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001369 * memory from the node of the cpu going down. The node
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001370 * structure is usually allocated from kmem_cache_create() and
1371 * gets destroyed at kmem_cache_destroy().
1372 */
Simon Arlott183ff222007-10-20 01:27:18 +02001373 /* fall through */
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08001374#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001376 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001377 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001378 cpuup_canceled(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001379 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 }
Akinobu Mitaeac40682010-05-26 14:43:32 -07001382 return notifier_from_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383}
1384
Paul Gortmaker0db06282013-06-19 14:53:51 -04001385static struct notifier_block cpucache_notifier = {
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001386 &cpuup_callback, NULL, 0
1387};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388
David Rientjes8f9f8d92010-03-27 19:40:47 -07001389#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
1390/*
1391 * Drains freelist for a node on each slab cache, used for memory hot-remove.
1392 * Returns -EBUSY if all objects cannot be drained so that the node is not
1393 * removed.
1394 *
Christoph Lameter18004c52012-07-06 15:25:12 -05001395 * Must hold slab_mutex.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001396 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001397static int __meminit drain_cache_node_node(int node)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001398{
1399 struct kmem_cache *cachep;
1400 int ret = 0;
1401
Christoph Lameter18004c52012-07-06 15:25:12 -05001402 list_for_each_entry(cachep, &slab_caches, list) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001403 struct kmem_cache_node *n;
David Rientjes8f9f8d92010-03-27 19:40:47 -07001404
Christoph Lameter18bf8542014-08-06 16:04:11 -07001405 n = get_node(cachep, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001406 if (!n)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001407 continue;
1408
Wanpeng Li0fa81032013-07-04 08:33:22 +08001409 drain_freelist(cachep, n, slabs_tofree(cachep, n));
David Rientjes8f9f8d92010-03-27 19:40:47 -07001410
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001411 if (!list_empty(&n->slabs_full) ||
1412 !list_empty(&n->slabs_partial)) {
David Rientjes8f9f8d92010-03-27 19:40:47 -07001413 ret = -EBUSY;
1414 break;
1415 }
1416 }
1417 return ret;
1418}
1419
1420static int __meminit slab_memory_callback(struct notifier_block *self,
1421 unsigned long action, void *arg)
1422{
1423 struct memory_notify *mnb = arg;
1424 int ret = 0;
1425 int nid;
1426
1427 nid = mnb->status_change_nid;
1428 if (nid < 0)
1429 goto out;
1430
1431 switch (action) {
1432 case MEM_GOING_ONLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001433 mutex_lock(&slab_mutex);
Christoph Lameter6a673682013-01-10 19:14:19 +00001434 ret = init_cache_node_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001435 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001436 break;
1437 case MEM_GOING_OFFLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001438 mutex_lock(&slab_mutex);
Christoph Lameter6a673682013-01-10 19:14:19 +00001439 ret = drain_cache_node_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001440 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001441 break;
1442 case MEM_ONLINE:
1443 case MEM_OFFLINE:
1444 case MEM_CANCEL_ONLINE:
1445 case MEM_CANCEL_OFFLINE:
1446 break;
1447 }
1448out:
Prarit Bhargava5fda1bd2011-03-22 16:30:49 -07001449 return notifier_from_errno(ret);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001450}
1451#endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
1452
Christoph Lametere498be72005-09-09 13:03:32 -07001453/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001454 * swap the static kmem_cache_node with kmalloced memory
Christoph Lametere498be72005-09-09 13:03:32 -07001455 */
Christoph Lameter6744f082013-01-10 19:12:17 +00001456static void __init init_list(struct kmem_cache *cachep, struct kmem_cache_node *list,
David Rientjes8f9f8d92010-03-27 19:40:47 -07001457 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07001458{
Christoph Lameter6744f082013-01-10 19:12:17 +00001459 struct kmem_cache_node *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001460
Christoph Lameter6744f082013-01-10 19:12:17 +00001461 ptr = kmalloc_node(sizeof(struct kmem_cache_node), GFP_NOWAIT, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07001462 BUG_ON(!ptr);
1463
Christoph Lameter6744f082013-01-10 19:12:17 +00001464 memcpy(ptr, list, sizeof(struct kmem_cache_node));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001465 /*
1466 * Do not assume that spinlocks can be initialized via memcpy:
1467 */
1468 spin_lock_init(&ptr->list_lock);
1469
Christoph Lametere498be72005-09-09 13:03:32 -07001470 MAKE_ALL_LISTS(cachep, ptr, nodeid);
Christoph Lameter6a673682013-01-10 19:14:19 +00001471 cachep->node[nodeid] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001472}
1473
Andrew Mortona737b3e2006-03-22 00:08:11 -08001474/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001475 * For setting up all the kmem_cache_node for cache whose buffer_size is same as
1476 * size of kmem_cache_node.
Pekka Enberg556a1692008-01-25 08:20:51 +02001477 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001478static void __init set_up_node(struct kmem_cache *cachep, int index)
Pekka Enberg556a1692008-01-25 08:20:51 +02001479{
1480 int node;
1481
1482 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001483 cachep->node[node] = &init_kmem_cache_node[index + node];
Christoph Lameter6a673682013-01-10 19:14:19 +00001484 cachep->node[node]->next_reap = jiffies +
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08001485 REAPTIMEOUT_NODE +
1486 ((unsigned long)cachep) % REAPTIMEOUT_NODE;
Pekka Enberg556a1692008-01-25 08:20:51 +02001487 }
1488}
1489
1490/*
Christoph Lameter3c583462012-11-28 16:23:01 +00001491 * The memory after the last cpu cache pointer is used for the
Christoph Lameter6a673682013-01-10 19:14:19 +00001492 * the node pointer.
Christoph Lameter3c583462012-11-28 16:23:01 +00001493 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001494static void setup_node_pointer(struct kmem_cache *cachep)
Christoph Lameter3c583462012-11-28 16:23:01 +00001495{
Christoph Lameter6a673682013-01-10 19:14:19 +00001496 cachep->node = (struct kmem_cache_node **)&cachep->array[nr_cpu_ids];
Christoph Lameter3c583462012-11-28 16:23:01 +00001497}
1498
1499/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08001500 * Initialisation. Called after the page allocator have been initialised and
1501 * before smp_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 */
1503void __init kmem_cache_init(void)
1504{
Christoph Lametere498be72005-09-09 13:03:32 -07001505 int i;
1506
Joonsoo Kim68126702013-10-24 10:07:42 +09001507 BUILD_BUG_ON(sizeof(((struct page *)NULL)->lru) <
1508 sizeof(struct rcu_head));
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001509 kmem_cache = &kmem_cache_boot;
Christoph Lameter6a673682013-01-10 19:14:19 +00001510 setup_node_pointer(kmem_cache);
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001511
Mel Gormanb6e68bc2009-06-16 15:32:16 -07001512 if (num_possible_nodes() == 1)
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001513 use_alien_caches = 0;
1514
Christoph Lameter3c583462012-11-28 16:23:01 +00001515 for (i = 0; i < NUM_INIT_LISTS; i++)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001516 kmem_cache_node_init(&init_kmem_cache_node[i]);
Christoph Lameter3c583462012-11-28 16:23:01 +00001517
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001518 set_up_node(kmem_cache, CACHE_CACHE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
1520 /*
1521 * Fragmentation resistance on low memory - only use bigger
David Rientjes3df1ccc2011-10-18 22:09:28 -07001522 * page orders on machines with more than 32MB of memory if
1523 * not overridden on the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 */
David Rientjes3df1ccc2011-10-18 22:09:28 -07001525 if (!slab_max_order_set && totalram_pages > (32 << 20) >> PAGE_SHIFT)
David Rientjes543585c2011-10-18 22:09:24 -07001526 slab_max_order = SLAB_MAX_ORDER_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 /* Bootstrap is tricky, because several objects are allocated
1529 * from caches that do not exist yet:
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001530 * 1) initialize the kmem_cache cache: it contains the struct
1531 * kmem_cache structures of all caches, except kmem_cache itself:
1532 * kmem_cache is statically allocated.
Christoph Lametere498be72005-09-09 13:03:32 -07001533 * Initially an __init data area is used for the head array and the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001534 * kmem_cache_node structures, it's replaced with a kmalloc allocated
Christoph Lametere498be72005-09-09 13:03:32 -07001535 * array at the end of the bootstrap.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 * 2) Create the first kmalloc cache.
Pekka Enberg343e0d72006-02-01 03:05:50 -08001537 * The struct kmem_cache for the new cache is allocated normally.
Christoph Lametere498be72005-09-09 13:03:32 -07001538 * An __init data area is used for the head array.
1539 * 3) Create the remaining kmalloc caches, with minimally sized
1540 * head arrays.
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001541 * 4) Replace the __init data head arrays for kmem_cache and the first
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 * kmalloc cache with kmalloc allocated arrays.
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001543 * 5) Replace the __init data for kmem_cache_node for kmem_cache and
Christoph Lametere498be72005-09-09 13:03:32 -07001544 * the other cache's with kmalloc allocated memory.
1545 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 */
1547
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001548 /* 1) create the kmem_cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Eric Dumazet8da34302007-05-06 14:49:29 -07001550 /*
Eric Dumazetb56efcf2011-07-20 19:04:23 +02001551 * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
Eric Dumazet8da34302007-05-06 14:49:29 -07001552 */
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001553 create_boot_cache(kmem_cache, "kmem_cache",
1554 offsetof(struct kmem_cache, array[nr_cpu_ids]) +
Christoph Lameter6744f082013-01-10 19:12:17 +00001555 nr_node_ids * sizeof(struct kmem_cache_node *),
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001556 SLAB_HWCACHE_ALIGN);
1557 list_add(&kmem_cache->list, &slab_caches);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 /* 2+3) create the kmalloc caches */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Andrew Mortona737b3e2006-03-22 00:08:11 -08001561 /*
1562 * Initialize the caches that provide memory for the array cache and the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001563 * kmem_cache_node structures first. Without this, further allocations will
Andrew Mortona737b3e2006-03-22 00:08:11 -08001564 * bug.
Christoph Lametere498be72005-09-09 13:03:32 -07001565 */
1566
Christoph Lametere3366012013-01-10 19:14:18 +00001567 kmalloc_caches[INDEX_AC] = create_kmalloc_cache("kmalloc-ac",
1568 kmalloc_size(INDEX_AC), ARCH_KMALLOC_FLAGS);
Christoph Lametere498be72005-09-09 13:03:32 -07001569
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001570 if (INDEX_AC != INDEX_NODE)
1571 kmalloc_caches[INDEX_NODE] =
1572 create_kmalloc_cache("kmalloc-node",
1573 kmalloc_size(INDEX_NODE), ARCH_KMALLOC_FLAGS);
Christoph Lametere498be72005-09-09 13:03:32 -07001574
Ingo Molnare0a42722006-06-23 02:03:46 -07001575 slab_early_init = 0;
1576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 /* 4) Replace the bootstrap head arrays */
1578 {
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001579 struct array_cache *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001580
Pekka Enberg83b519e2009-06-10 19:40:04 +03001581 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001582
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001583 memcpy(ptr, cpu_cache_get(kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001584 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001585 /*
1586 * Do not assume that spinlocks can be initialized via memcpy:
1587 */
1588 spin_lock_init(&ptr->lock);
1589
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001590 kmem_cache->array[smp_processor_id()] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001591
Pekka Enberg83b519e2009-06-10 19:40:04 +03001592 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001593
Christoph Lametere3366012013-01-10 19:14:18 +00001594 BUG_ON(cpu_cache_get(kmalloc_caches[INDEX_AC])
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001595 != &initarray_generic.cache);
Christoph Lametere3366012013-01-10 19:14:18 +00001596 memcpy(ptr, cpu_cache_get(kmalloc_caches[INDEX_AC]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001597 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001598 /*
1599 * Do not assume that spinlocks can be initialized via memcpy:
1600 */
1601 spin_lock_init(&ptr->lock);
1602
Christoph Lametere3366012013-01-10 19:14:18 +00001603 kmalloc_caches[INDEX_AC]->array[smp_processor_id()] = ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001605 /* 5) Replace the bootstrap kmem_cache_node */
Christoph Lametere498be72005-09-09 13:03:32 -07001606 {
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001607 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
Mel Gorman9c09a952008-01-24 05:49:54 -08001609 for_each_online_node(nid) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001610 init_list(kmem_cache, &init_kmem_cache_node[CACHE_CACHE + nid], nid);
Pekka Enberg556a1692008-01-25 08:20:51 +02001611
Christoph Lametere3366012013-01-10 19:14:18 +00001612 init_list(kmalloc_caches[INDEX_AC],
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001613 &init_kmem_cache_node[SIZE_AC + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001614
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001615 if (INDEX_AC != INDEX_NODE) {
1616 init_list(kmalloc_caches[INDEX_NODE],
1617 &init_kmem_cache_node[SIZE_NODE + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001618 }
1619 }
1620 }
1621
Christoph Lameterf97d5f62013-01-10 19:12:17 +00001622 create_kmalloc_caches(ARCH_KMALLOC_FLAGS);
Pekka Enberg8429db52009-06-12 15:58:59 +03001623}
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001624
Pekka Enberg8429db52009-06-12 15:58:59 +03001625void __init kmem_cache_init_late(void)
1626{
1627 struct kmem_cache *cachep;
1628
Christoph Lameter97d06602012-07-06 15:25:11 -05001629 slab_state = UP;
Peter Zijlstra52cef182011-11-28 21:12:40 +01001630
Pekka Enberg8429db52009-06-12 15:58:59 +03001631 /* 6) resize the head arrays to their final sizes */
Christoph Lameter18004c52012-07-06 15:25:12 -05001632 mutex_lock(&slab_mutex);
1633 list_for_each_entry(cachep, &slab_caches, list)
Pekka Enberg8429db52009-06-12 15:58:59 +03001634 if (enable_cpucache(cachep, GFP_NOWAIT))
1635 BUG();
Christoph Lameter18004c52012-07-06 15:25:12 -05001636 mutex_unlock(&slab_mutex);
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001637
Michael Wang947ca182012-09-05 10:33:18 +08001638 /* Annotate slab for lockdep -- annotate the malloc caches */
1639 init_lock_keys();
1640
Christoph Lameter97d06602012-07-06 15:25:11 -05001641 /* Done! */
1642 slab_state = FULL;
1643
Andrew Mortona737b3e2006-03-22 00:08:11 -08001644 /*
1645 * Register a cpu startup notifier callback that initializes
1646 * cpu_cache_get for all new cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 */
1648 register_cpu_notifier(&cpucache_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
David Rientjes8f9f8d92010-03-27 19:40:47 -07001650#ifdef CONFIG_NUMA
1651 /*
1652 * Register a memory hotplug callback that initializes and frees
Christoph Lameter6a673682013-01-10 19:14:19 +00001653 * node.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001654 */
1655 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
1656#endif
1657
Andrew Mortona737b3e2006-03-22 00:08:11 -08001658 /*
1659 * The reap timers are started later, with a module init call: That part
1660 * of the kernel is not yet operational.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 */
1662}
1663
1664static int __init cpucache_init(void)
1665{
1666 int cpu;
1667
Andrew Mortona737b3e2006-03-22 00:08:11 -08001668 /*
1669 * Register the timers that return unneeded pages to the page allocator
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 */
Christoph Lametere498be72005-09-09 13:03:32 -07001671 for_each_online_cpu(cpu)
Andrew Mortona737b3e2006-03-22 00:08:11 -08001672 start_cpu_timer(cpu);
Glauber Costaa164f8962012-06-21 00:59:18 +04001673
1674 /* Done! */
Christoph Lameter97d06602012-07-06 15:25:11 -05001675 slab_state = FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return 0;
1677}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678__initcall(cpucache_init);
1679
Rafael Aquini8bdec192012-03-09 17:27:27 -03001680static noinline void
1681slab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid)
1682{
David Rientjes9a02d692014-06-04 16:06:36 -07001683#if DEBUG
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001684 struct kmem_cache_node *n;
Joonsoo Kim8456a642013-10-24 10:07:49 +09001685 struct page *page;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001686 unsigned long flags;
1687 int node;
David Rientjes9a02d692014-06-04 16:06:36 -07001688 static DEFINE_RATELIMIT_STATE(slab_oom_rs, DEFAULT_RATELIMIT_INTERVAL,
1689 DEFAULT_RATELIMIT_BURST);
1690
1691 if ((gfpflags & __GFP_NOWARN) || !__ratelimit(&slab_oom_rs))
1692 return;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001693
1694 printk(KERN_WARNING
1695 "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n",
1696 nodeid, gfpflags);
1697 printk(KERN_WARNING " cache: %s, object size: %d, order: %d\n",
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05001698 cachep->name, cachep->size, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001699
Christoph Lameter18bf8542014-08-06 16:04:11 -07001700 for_each_kmem_cache_node(cachep, node, n) {
Rafael Aquini8bdec192012-03-09 17:27:27 -03001701 unsigned long active_objs = 0, num_objs = 0, free_objects = 0;
1702 unsigned long active_slabs = 0, num_slabs = 0;
1703
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001704 spin_lock_irqsave(&n->list_lock, flags);
Joonsoo Kim8456a642013-10-24 10:07:49 +09001705 list_for_each_entry(page, &n->slabs_full, lru) {
Rafael Aquini8bdec192012-03-09 17:27:27 -03001706 active_objs += cachep->num;
1707 active_slabs++;
1708 }
Joonsoo Kim8456a642013-10-24 10:07:49 +09001709 list_for_each_entry(page, &n->slabs_partial, lru) {
1710 active_objs += page->active;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001711 active_slabs++;
1712 }
Joonsoo Kim8456a642013-10-24 10:07:49 +09001713 list_for_each_entry(page, &n->slabs_free, lru)
Rafael Aquini8bdec192012-03-09 17:27:27 -03001714 num_slabs++;
1715
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001716 free_objects += n->free_objects;
1717 spin_unlock_irqrestore(&n->list_lock, flags);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001718
1719 num_slabs += active_slabs;
1720 num_objs = num_slabs * cachep->num;
1721 printk(KERN_WARNING
1722 " node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
1723 node, active_slabs, num_slabs, active_objs, num_objs,
1724 free_objects);
1725 }
David Rientjes9a02d692014-06-04 16:06:36 -07001726#endif
Rafael Aquini8bdec192012-03-09 17:27:27 -03001727}
1728
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729/*
1730 * Interface to system's page allocator. No need to hold the cache-lock.
1731 *
1732 * If we requested dmaable memory, we will get it. Even if we
1733 * did not request dmaable memory, we might get it, but that
1734 * would be relatively rare and ignorable.
1735 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001736static struct page *kmem_getpages(struct kmem_cache *cachep, gfp_t flags,
1737 int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738{
1739 struct page *page;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001740 int nr_pages;
Christoph Lameter765c4502006-09-27 01:50:08 -07001741
Glauber Costaa618e892012-06-14 16:17:21 +04001742 flags |= cachep->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001743 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1744 flags |= __GFP_RECLAIMABLE;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001745
Vladimir Davydov5dfb4172014-06-04 16:06:38 -07001746 if (memcg_charge_slab(cachep, flags, cachep->gfporder))
1747 return NULL;
1748
Linus Torvalds517d0862009-06-16 19:50:13 -07001749 page = alloc_pages_exact_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001750 if (!page) {
Vladimir Davydov5dfb4172014-06-04 16:06:38 -07001751 memcg_uncharge_slab(cachep, cachep->gfporder);
David Rientjes9a02d692014-06-04 16:06:36 -07001752 slab_out_of_memory(cachep, flags, nodeid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 return NULL;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Mel Gormanb37f1dd2012-07-31 16:44:03 -07001756 /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
Mel Gorman072bb0a2012-07-31 16:43:58 -07001757 if (unlikely(page->pfmemalloc))
1758 pfmemalloc_active = true;
1759
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001760 nr_pages = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
Christoph Lameter972d1a72006-09-25 23:31:51 -07001762 add_zone_page_state(page_zone(page),
1763 NR_SLAB_RECLAIMABLE, nr_pages);
1764 else
1765 add_zone_page_state(page_zone(page),
1766 NR_SLAB_UNRECLAIMABLE, nr_pages);
Joonsoo Kima57a4982013-10-24 10:07:44 +09001767 __SetPageSlab(page);
1768 if (page->pfmemalloc)
1769 SetPageSlabPfmemalloc(page);
Mel Gorman072bb0a2012-07-31 16:43:58 -07001770
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001771 if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
1772 kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
1773
1774 if (cachep->ctor)
1775 kmemcheck_mark_uninitialized_pages(page, nr_pages);
1776 else
1777 kmemcheck_mark_unallocated_pages(page, nr_pages);
1778 }
Pekka Enbergc175eea2008-05-09 20:35:53 +02001779
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001780 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781}
1782
1783/*
1784 * Interface to system's page release.
1785 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001786static void kmem_freepages(struct kmem_cache *cachep, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
Joonsoo Kima57a4982013-10-24 10:07:44 +09001788 const unsigned long nr_freed = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001790 kmemcheck_free_shadow(page, cachep->gfporder);
Pekka Enbergc175eea2008-05-09 20:35:53 +02001791
Christoph Lameter972d1a72006-09-25 23:31:51 -07001792 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1793 sub_zone_page_state(page_zone(page),
1794 NR_SLAB_RECLAIMABLE, nr_freed);
1795 else
1796 sub_zone_page_state(page_zone(page),
1797 NR_SLAB_UNRECLAIMABLE, nr_freed);
Joonsoo Kim73293c22013-10-24 10:07:37 +09001798
Joonsoo Kima57a4982013-10-24 10:07:44 +09001799 BUG_ON(!PageSlab(page));
Joonsoo Kim73293c22013-10-24 10:07:37 +09001800 __ClearPageSlabPfmemalloc(page);
Joonsoo Kima57a4982013-10-24 10:07:44 +09001801 __ClearPageSlab(page);
Joonsoo Kim8456a642013-10-24 10:07:49 +09001802 page_mapcount_reset(page);
1803 page->mapping = NULL;
Glauber Costa1f458cb2012-12-18 14:22:50 -08001804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 if (current->reclaim_state)
1806 current->reclaim_state->reclaimed_slab += nr_freed;
Vladimir Davydov5dfb4172014-06-04 16:06:38 -07001807 __free_pages(page, cachep->gfporder);
1808 memcg_uncharge_slab(cachep, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809}
1810
1811static void kmem_rcu_free(struct rcu_head *head)
1812{
Joonsoo Kim68126702013-10-24 10:07:42 +09001813 struct kmem_cache *cachep;
1814 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Joonsoo Kim68126702013-10-24 10:07:42 +09001816 page = container_of(head, struct page, rcu_head);
1817 cachep = page->slab_cache;
1818
1819 kmem_freepages(cachep, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820}
1821
1822#if DEBUG
1823
1824#ifdef CONFIG_DEBUG_PAGEALLOC
Pekka Enberg343e0d72006-02-01 03:05:50 -08001825static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001826 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001828 int size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001830 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001832 if (size < 5 * sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 return;
1834
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001835 *addr++ = 0x12345678;
1836 *addr++ = caller;
1837 *addr++ = smp_processor_id();
1838 size -= 3 * sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 {
1840 unsigned long *sptr = &caller;
1841 unsigned long svalue;
1842
1843 while (!kstack_end(sptr)) {
1844 svalue = *sptr++;
1845 if (kernel_text_address(svalue)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001846 *addr++ = svalue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 size -= sizeof(unsigned long);
1848 if (size <= sizeof(unsigned long))
1849 break;
1850 }
1851 }
1852
1853 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001854 *addr++ = 0x87654321;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
1856#endif
1857
Pekka Enberg343e0d72006-02-01 03:05:50 -08001858static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001860 int size = cachep->object_size;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001861 addr = &((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 memset(addr, val, size);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001864 *(unsigned char *)(addr + size - 1) = POISON_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
1867static void dump_line(char *data, int offset, int limit)
1868{
1869 int i;
Dave Jonesaa83aa42006-09-29 01:59:51 -07001870 unsigned char error = 0;
1871 int bad_count = 0;
1872
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02001873 printk(KERN_ERR "%03x: ", offset);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001874 for (i = 0; i < limit; i++) {
1875 if (data[offset + i] != POISON_FREE) {
1876 error = data[offset + i];
1877 bad_count++;
1878 }
Dave Jonesaa83aa42006-09-29 01:59:51 -07001879 }
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02001880 print_hex_dump(KERN_CONT, "", 0, 16, 1,
1881 &data[offset], limit, 1);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001882
1883 if (bad_count == 1) {
1884 error ^= POISON_FREE;
1885 if (!(error & (error - 1))) {
1886 printk(KERN_ERR "Single bit error detected. Probably "
1887 "bad RAM.\n");
1888#ifdef CONFIG_X86
1889 printk(KERN_ERR "Run memtest86+ or a similar memory "
1890 "test tool.\n");
1891#else
1892 printk(KERN_ERR "Run a memory test tool.\n");
1893#endif
1894 }
1895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896}
1897#endif
1898
1899#if DEBUG
1900
Pekka Enberg343e0d72006-02-01 03:05:50 -08001901static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902{
1903 int i, size;
1904 char *realobj;
1905
1906 if (cachep->flags & SLAB_RED_ZONE) {
David Woodhouseb46b8f12007-05-08 00:22:59 -07001907 printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001908 *dbg_redzone1(cachep, objp),
1909 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911
1912 if (cachep->flags & SLAB_STORE_USER) {
Joe Perches071361d2012-12-12 10:19:12 -08001913 printk(KERN_ERR "Last user: [<%p>](%pSR)\n",
1914 *dbg_userword(cachep, objp),
1915 *dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001917 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001918 size = cachep->object_size;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001919 for (i = 0; i < size && lines; i += 16, lines--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 int limit;
1921 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001922 if (i + limit > size)
1923 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 dump_line(realobj, i, limit);
1925 }
1926}
1927
Pekka Enberg343e0d72006-02-01 03:05:50 -08001928static void check_poison_obj(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
1930 char *realobj;
1931 int size, i;
1932 int lines = 0;
1933
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001934 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001935 size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001937 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 char exp = POISON_FREE;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001939 if (i == size - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 exp = POISON_END;
1941 if (realobj[i] != exp) {
1942 int limit;
1943 /* Mismatch ! */
1944 /* Print header */
1945 if (lines == 0) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001946 printk(KERN_ERR
Dave Jonesface37f2011-11-15 15:03:52 -08001947 "Slab corruption (%s): %s start=%p, len=%d\n",
1948 print_tainted(), cachep->name, realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 print_objinfo(cachep, objp, 0);
1950 }
1951 /* Hexdump the affected line */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001952 i = (i / 16) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001954 if (i + limit > size)
1955 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 dump_line(realobj, i, limit);
1957 i += 16;
1958 lines++;
1959 /* Limit to 5 lines */
1960 if (lines > 5)
1961 break;
1962 }
1963 }
1964 if (lines != 0) {
1965 /* Print some data about the neighboring objects, if they
1966 * exist:
1967 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09001968 struct page *page = virt_to_head_page(objp);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001969 unsigned int objnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Joonsoo Kim8456a642013-10-24 10:07:49 +09001971 objnr = obj_to_index(cachep, page, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 if (objnr) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09001973 objp = index_to_obj(cachep, page, objnr - 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001974 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001976 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 print_objinfo(cachep, objp, 2);
1978 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001979 if (objnr + 1 < cachep->num) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09001980 objp = index_to_obj(cachep, page, objnr + 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001981 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001983 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 print_objinfo(cachep, objp, 2);
1985 }
1986 }
1987}
1988#endif
1989
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990#if DEBUG
Joonsoo Kim8456a642013-10-24 10:07:49 +09001991static void slab_destroy_debugcheck(struct kmem_cache *cachep,
1992 struct page *page)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001993{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 int i;
1995 for (i = 0; i < cachep->num; i++) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09001996 void *objp = index_to_obj(cachep, page, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 if (cachep->flags & SLAB_POISON) {
1999#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002000 if (cachep->size % PAGE_SIZE == 0 &&
Andrew Mortona737b3e2006-03-22 00:08:11 -08002001 OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002002 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002003 cachep->size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 else
2005 check_poison_obj(cachep, objp);
2006#else
2007 check_poison_obj(cachep, objp);
2008#endif
2009 }
2010 if (cachep->flags & SLAB_RED_ZONE) {
2011 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2012 slab_error(cachep, "start of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002013 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2015 slab_error(cachep, "end of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002016 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002019}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020#else
Joonsoo Kim8456a642013-10-24 10:07:49 +09002021static void slab_destroy_debugcheck(struct kmem_cache *cachep,
2022 struct page *page)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002023{
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002024}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025#endif
2026
Randy Dunlap911851e2006-03-22 00:08:14 -08002027/**
2028 * slab_destroy - destroy and release all objects in a slab
2029 * @cachep: cache pointer being destroyed
Masanari Iidacb8ee1a2014-01-28 02:57:08 +09002030 * @page: page pointer being destroyed
Randy Dunlap911851e2006-03-22 00:08:14 -08002031 *
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002032 * Destroy all the objs in a slab, and release the mem back to the system.
Andrew Mortona737b3e2006-03-22 00:08:11 -08002033 * Before calling the slab must have been unlinked from the cache. The
2034 * cache-lock is not held/needed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002035 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002036static void slab_destroy(struct kmem_cache *cachep, struct page *page)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002037{
Joonsoo Kim7e007352013-10-30 19:04:01 +09002038 void *freelist;
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002039
Joonsoo Kim8456a642013-10-24 10:07:49 +09002040 freelist = page->freelist;
2041 slab_destroy_debugcheck(cachep, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
Joonsoo Kim68126702013-10-24 10:07:42 +09002043 struct rcu_head *head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Joonsoo Kim68126702013-10-24 10:07:42 +09002045 /*
2046 * RCU free overloads the RCU head over the LRU.
2047 * slab_page has been overloeaded over the LRU,
2048 * however it is not used from now on so that
2049 * we can use it safely.
2050 */
2051 head = (void *)&page->rcu_head;
2052 call_rcu(head, kmem_rcu_free);
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 } else {
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002055 kmem_freepages(cachep, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 }
Joonsoo Kim68126702013-10-24 10:07:42 +09002057
2058 /*
Joonsoo Kim8456a642013-10-24 10:07:49 +09002059 * From now on, we don't use freelist
Joonsoo Kim68126702013-10-24 10:07:42 +09002060 * although actual page can be freed in rcu context
2061 */
2062 if (OFF_SLAB(cachep))
Joonsoo Kim8456a642013-10-24 10:07:49 +09002063 kmem_cache_free(cachep->freelist_cache, freelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
Joonsoo Kim97654df2014-08-06 16:04:25 -07002066static void slabs_destroy(struct kmem_cache *cachep, struct list_head *list)
2067{
2068 struct page *page, *n;
2069
2070 list_for_each_entry_safe(page, n, list, lru) {
2071 list_del(&page->lru);
2072 slab_destroy(cachep, page);
2073 }
2074}
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076/**
Randy.Dunlapa70773d2006-02-01 03:05:52 -08002077 * calculate_slab_order - calculate size (page order) of slabs
2078 * @cachep: pointer to the cache that is being created
2079 * @size: size of objects to be created in this cache.
2080 * @align: required alignment for the objects.
2081 * @flags: slab allocation flags
2082 *
2083 * Also calculates the number of objects per slab.
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002084 *
2085 * This could be made much more intelligent. For now, try to avoid using
2086 * high order pages for slabs. When the gfp() functions are more friendly
2087 * towards high-order requests, this should be changed.
2088 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002089static size_t calculate_slab_order(struct kmem_cache *cachep,
Randy Dunlapee13d782006-02-01 03:05:53 -08002090 size_t size, size_t align, unsigned long flags)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002091{
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002092 unsigned long offslab_limit;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002093 size_t left_over = 0;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002094 int gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002095
Christoph Lameter0aa817f2007-05-16 22:11:01 -07002096 for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002097 unsigned int num;
2098 size_t remainder;
2099
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002100 cache_estimate(gfporder, size, align, flags, &remainder, &num);
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002101 if (!num)
2102 continue;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002103
Joonsoo Kimf315e3f2013-12-02 17:49:41 +09002104 /* Can't handle number of objects more than SLAB_OBJ_MAX_NUM */
2105 if (num > SLAB_OBJ_MAX_NUM)
2106 break;
2107
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002108 if (flags & CFLGS_OFF_SLAB) {
Joonsoo Kim03787302014-06-23 13:22:06 -07002109 size_t freelist_size_per_obj = sizeof(freelist_idx_t);
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002110 /*
2111 * Max number of objs-per-slab for caches which
2112 * use off-slab slabs. Needed to avoid a possible
2113 * looping condition in cache_grow().
2114 */
Joonsoo Kim03787302014-06-23 13:22:06 -07002115 if (IS_ENABLED(CONFIG_DEBUG_SLAB_LEAK))
2116 freelist_size_per_obj += sizeof(char);
Joonsoo Kim8456a642013-10-24 10:07:49 +09002117 offslab_limit = size;
Joonsoo Kim03787302014-06-23 13:22:06 -07002118 offslab_limit /= freelist_size_per_obj;
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002119
2120 if (num > offslab_limit)
2121 break;
2122 }
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002123
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002124 /* Found something acceptable - save it away */
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002125 cachep->num = num;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002126 cachep->gfporder = gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002127 left_over = remainder;
2128
2129 /*
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08002130 * A VFS-reclaimable slab tends to have most allocations
2131 * as GFP_NOFS and we really don't want to have to be allocating
2132 * higher-order pages when we are unable to shrink dcache.
2133 */
2134 if (flags & SLAB_RECLAIM_ACCOUNT)
2135 break;
2136
2137 /*
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002138 * Large number of objects is good, but very large slabs are
2139 * currently bad for the gfp()s.
2140 */
David Rientjes543585c2011-10-18 22:09:24 -07002141 if (gfporder >= slab_max_order)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002142 break;
2143
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002144 /*
2145 * Acceptable internal fragmentation?
2146 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002147 if (left_over * 8 <= (PAGE_SIZE << gfporder))
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002148 break;
2149 }
2150 return left_over;
2151}
2152
Pekka Enberg83b519e2009-06-10 19:40:04 +03002153static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002154{
Christoph Lameter97d06602012-07-06 15:25:11 -05002155 if (slab_state >= FULL)
Pekka Enberg83b519e2009-06-10 19:40:04 +03002156 return enable_cpucache(cachep, gfp);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002157
Christoph Lameter97d06602012-07-06 15:25:11 -05002158 if (slab_state == DOWN) {
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002159 /*
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002160 * Note: Creation of first cache (kmem_cache).
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002161 * The setup_node is taken care
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002162 * of by the caller of __kmem_cache_create
2163 */
2164 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2165 slab_state = PARTIAL;
2166 } else if (slab_state == PARTIAL) {
2167 /*
2168 * Note: the second kmem_cache_create must create the cache
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002169 * that's used by kmalloc(24), otherwise the creation of
2170 * further caches will BUG().
2171 */
2172 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2173
2174 /*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002175 * If the cache that's used by kmalloc(sizeof(kmem_cache_node)) is
2176 * the second cache, then we need to set up all its node/,
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002177 * otherwise the creation of further caches will BUG().
2178 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002179 set_up_node(cachep, SIZE_AC);
2180 if (INDEX_AC == INDEX_NODE)
2181 slab_state = PARTIAL_NODE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002182 else
Christoph Lameter97d06602012-07-06 15:25:11 -05002183 slab_state = PARTIAL_ARRAYCACHE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002184 } else {
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002185 /* Remaining boot caches */
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002186 cachep->array[smp_processor_id()] =
Pekka Enberg83b519e2009-06-10 19:40:04 +03002187 kmalloc(sizeof(struct arraycache_init), gfp);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002188
Christoph Lameter97d06602012-07-06 15:25:11 -05002189 if (slab_state == PARTIAL_ARRAYCACHE) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002190 set_up_node(cachep, SIZE_NODE);
2191 slab_state = PARTIAL_NODE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002192 } else {
2193 int node;
Pekka Enberg556a1692008-01-25 08:20:51 +02002194 for_each_online_node(node) {
Christoph Lameter6a673682013-01-10 19:14:19 +00002195 cachep->node[node] =
Christoph Lameter6744f082013-01-10 19:12:17 +00002196 kmalloc_node(sizeof(struct kmem_cache_node),
Pekka Enbergeb91f1d2009-06-12 14:56:09 +03002197 gfp, node);
Christoph Lameter6a673682013-01-10 19:14:19 +00002198 BUG_ON(!cachep->node[node]);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002199 kmem_cache_node_init(cachep->node[node]);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002200 }
2201 }
2202 }
Christoph Lameter6a673682013-01-10 19:14:19 +00002203 cachep->node[numa_mem_id()]->next_reap =
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08002204 jiffies + REAPTIMEOUT_NODE +
2205 ((unsigned long)cachep) % REAPTIMEOUT_NODE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002206
2207 cpu_cache_get(cachep)->avail = 0;
2208 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
2209 cpu_cache_get(cachep)->batchcount = 1;
2210 cpu_cache_get(cachep)->touched = 0;
2211 cachep->batchcount = 1;
2212 cachep->limit = BOOT_CPUCACHE_ENTRIES;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002213 return 0;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002214}
2215
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002216/**
Christoph Lameter039363f2012-07-06 15:25:10 -05002217 * __kmem_cache_create - Create a cache.
Randy Dunlapa755b762012-11-06 17:10:10 -08002218 * @cachep: cache management descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 * @flags: SLAB flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 *
2221 * Returns a ptr to the cache on success, NULL on failure.
2222 * Cannot be called within a int, but can be interrupted.
Paul Mundt20c2df82007-07-20 10:11:58 +09002223 * The @ctor is run when new pages are allocated by the cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 * The flags are
2226 *
2227 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2228 * to catch references to uninitialised memory.
2229 *
2230 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2231 * for buffer overruns.
2232 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2234 * cacheline. This can be beneficial if you're counting cycles as closely
2235 * as davem.
2236 */
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002237int
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002238__kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
Joonsoo Kim8456a642013-10-24 10:07:49 +09002240 size_t left_over, freelist_size, ralign;
Pekka Enberg83b519e2009-06-10 19:40:04 +03002241 gfp_t gfp;
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002242 int err;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002243 size_t size = cachep->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246#if FORCED_DEBUG
2247 /*
2248 * Enable redzoning and last user accounting, except for caches with
2249 * large objects, if the increased size would increase the object size
2250 * above the next power of two: caches with object sizes just above a
2251 * power of two have a significant amount of internal fragmentation.
2252 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002253 if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
2254 2 * sizeof(unsigned long long)))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002255 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 if (!(flags & SLAB_DESTROY_BY_RCU))
2257 flags |= SLAB_POISON;
2258#endif
2259 if (flags & SLAB_DESTROY_BY_RCU)
2260 BUG_ON(flags & SLAB_POISON);
2261#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
Andrew Mortona737b3e2006-03-22 00:08:11 -08002263 /*
2264 * Check that size is in terms of words. This is needed to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 * unaligned accesses for some archs when redzoning is used, and makes
2266 * sure any on-slab bufctl's are also correctly aligned.
2267 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002268 if (size & (BYTES_PER_WORD - 1)) {
2269 size += (BYTES_PER_WORD - 1);
2270 size &= ~(BYTES_PER_WORD - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 }
2272
Pekka Enbergca5f9702006-09-25 23:31:25 -07002273 /*
David Woodhouse87a927c2007-07-04 21:26:44 -04002274 * Redzoning and user store require word alignment or possibly larger.
2275 * Note this will be overridden by architecture or caller mandated
2276 * alignment if either is greater than BYTES_PER_WORD.
Pekka Enbergca5f9702006-09-25 23:31:25 -07002277 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002278 if (flags & SLAB_STORE_USER)
2279 ralign = BYTES_PER_WORD;
2280
2281 if (flags & SLAB_RED_ZONE) {
2282 ralign = REDZONE_ALIGN;
2283 /* If redzoning, ensure that the second redzone is suitably
2284 * aligned, by adjusting the object size accordingly. */
2285 size += REDZONE_ALIGN - 1;
2286 size &= ~(REDZONE_ALIGN - 1);
2287 }
Pekka Enbergca5f9702006-09-25 23:31:25 -07002288
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002289 /* 3) caller mandated alignment */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002290 if (ralign < cachep->align) {
2291 ralign = cachep->align;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 }
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002293 /* disable debug if necessary */
2294 if (ralign > __alignof__(unsigned long long))
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002295 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002296 /*
Pekka Enbergca5f9702006-09-25 23:31:25 -07002297 * 4) Store it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002299 cachep->align = ralign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Pekka Enberg83b519e2009-06-10 19:40:04 +03002301 if (slab_is_available())
2302 gfp = GFP_KERNEL;
2303 else
2304 gfp = GFP_NOWAIT;
2305
Christoph Lameter6a673682013-01-10 19:14:19 +00002306 setup_node_pointer(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308
Pekka Enbergca5f9702006-09-25 23:31:25 -07002309 /*
2310 * Both debugging options require word-alignment which is calculated
2311 * into align above.
2312 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 if (flags & SLAB_RED_ZONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 /* add space for red zone words */
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002315 cachep->obj_offset += sizeof(unsigned long long);
2316 size += 2 * sizeof(unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 }
2318 if (flags & SLAB_STORE_USER) {
Pekka Enbergca5f9702006-09-25 23:31:25 -07002319 /* user store requires one word storage behind the end of
David Woodhouse87a927c2007-07-04 21:26:44 -04002320 * the real object. But if the second red zone needs to be
2321 * aligned to 64 bits, we must allow that much space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002323 if (flags & SLAB_RED_ZONE)
2324 size += REDZONE_ALIGN;
2325 else
2326 size += BYTES_PER_WORD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 }
2328#if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002329 if (size >= kmalloc_size(INDEX_NODE + 1)
Tetsuo Handa608da7e2012-09-30 17:28:25 +09002330 && cachep->object_size > cache_line_size()
2331 && ALIGN(size, cachep->align) < PAGE_SIZE) {
2332 cachep->obj_offset += PAGE_SIZE - ALIGN(size, cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 size = PAGE_SIZE;
2334 }
2335#endif
2336#endif
2337
Ingo Molnare0a42722006-06-23 02:03:46 -07002338 /*
2339 * Determine if the slab management is 'on' or 'off' slab.
2340 * (bootstrapping cannot cope with offslab caches so don't do
Catalin Marinase7cb55b2009-10-28 13:33:08 +00002341 * it too early on. Always use on-slab management when
2342 * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
Ingo Molnare0a42722006-06-23 02:03:46 -07002343 */
Joonsoo Kim8fc9cf42013-12-02 17:49:43 +09002344 if ((size >= (PAGE_SIZE >> 5)) && !slab_early_init &&
Catalin Marinase7cb55b2009-10-28 13:33:08 +00002345 !(flags & SLAB_NOLEAKTRACE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 /*
2347 * Size is large, assume best to place the slab management obj
2348 * off-slab (should allow better packing of objs).
2349 */
2350 flags |= CFLGS_OFF_SLAB;
2351
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002352 size = ALIGN(size, cachep->align);
Joonsoo Kimf315e3f2013-12-02 17:49:41 +09002353 /*
2354 * We should restrict the number of objects in a slab to implement
2355 * byte sized index. Refer comment on SLAB_OBJ_MIN_SIZE definition.
2356 */
2357 if (FREELIST_BYTE_INDEX && size < SLAB_OBJ_MIN_SIZE)
2358 size = ALIGN(SLAB_OBJ_MIN_SIZE, cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002360 left_over = calculate_slab_order(cachep, size, cachep->align, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002362 if (!cachep->num)
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002363 return -E2BIG;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002364
Joonsoo Kim03787302014-06-23 13:22:06 -07002365 freelist_size = calculate_freelist_size(cachep->num, cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
2367 /*
2368 * If the slab has been placed off-slab, and we have enough space then
2369 * move it on-slab. This is at the expense of any extra colouring.
2370 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002371 if (flags & CFLGS_OFF_SLAB && left_over >= freelist_size) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 flags &= ~CFLGS_OFF_SLAB;
Joonsoo Kim8456a642013-10-24 10:07:49 +09002373 left_over -= freelist_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 }
2375
2376 if (flags & CFLGS_OFF_SLAB) {
2377 /* really off slab. No need for manual alignment */
Joonsoo Kim03787302014-06-23 13:22:06 -07002378 freelist_size = calculate_freelist_size(cachep->num, 0);
Ron Lee67461362009-05-22 04:58:22 +09302379
2380#ifdef CONFIG_PAGE_POISONING
2381 /* If we're going to use the generic kernel_map_pages()
2382 * poisoning, then it's going to smash the contents of
2383 * the redzone and userword anyhow, so switch them off.
2384 */
2385 if (size % PAGE_SIZE == 0 && flags & SLAB_POISON)
2386 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
2387#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 }
2389
2390 cachep->colour_off = cache_line_size();
2391 /* Offset must be a multiple of the alignment. */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002392 if (cachep->colour_off < cachep->align)
2393 cachep->colour_off = cachep->align;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002394 cachep->colour = left_over / cachep->colour_off;
Joonsoo Kim8456a642013-10-24 10:07:49 +09002395 cachep->freelist_size = freelist_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 cachep->flags = flags;
Joonsoo Kima57a4982013-10-24 10:07:44 +09002397 cachep->allocflags = __GFP_COMP;
Christoph Lameter4b51d662007-02-10 01:43:10 -08002398 if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
Glauber Costaa618e892012-06-14 16:17:21 +04002399 cachep->allocflags |= GFP_DMA;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002400 cachep->size = size;
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08002401 cachep->reciprocal_buffer_size = reciprocal_value(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002403 if (flags & CFLGS_OFF_SLAB) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09002404 cachep->freelist_cache = kmalloc_slab(freelist_size, 0u);
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002405 /*
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08002406 * This is a possibility for one of the kmalloc_{dma,}_caches.
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002407 * But since we go off slab only for object size greater than
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08002408 * PAGE_SIZE/8, and kmalloc_{dma,}_caches get created
2409 * in ascending order,this should not happen at all.
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002410 * But leave a BUG_ON for some lucky dude.
2411 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002412 BUG_ON(ZERO_OR_NULL_PTR(cachep->freelist_cache));
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002415 err = setup_cpu_cache(cachep, gfp);
2416 if (err) {
Christoph Lameter12c36672012-09-04 23:38:33 +00002417 __kmem_cache_shutdown(cachep);
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002418 return err;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
Peter Zijlstra83835b32011-07-22 15:26:05 +02002421 if (flags & SLAB_DEBUG_OBJECTS) {
2422 /*
2423 * Would deadlock through slab_destroy()->call_rcu()->
2424 * debug_object_activate()->kmem_cache_alloc().
2425 */
2426 WARN_ON_ONCE(flags & SLAB_DESTROY_BY_RCU);
2427
2428 slab_set_debugobj_lock_classes(cachep);
Glauber Costa6ccfb5b2012-12-18 14:22:31 -08002429 } else if (!OFF_SLAB(cachep) && !(flags & SLAB_DESTROY_BY_RCU))
2430 on_slab_lock_classes(cachep);
Peter Zijlstra83835b32011-07-22 15:26:05 +02002431
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002432 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
2435#if DEBUG
2436static void check_irq_off(void)
2437{
2438 BUG_ON(!irqs_disabled());
2439}
2440
2441static void check_irq_on(void)
2442{
2443 BUG_ON(irqs_disabled());
2444}
2445
Pekka Enberg343e0d72006-02-01 03:05:50 -08002446static void check_spinlock_acquired(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447{
2448#ifdef CONFIG_SMP
2449 check_irq_off();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002450 assert_spin_locked(&get_node(cachep, numa_mem_id())->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451#endif
2452}
Christoph Lametere498be72005-09-09 13:03:32 -07002453
Pekka Enberg343e0d72006-02-01 03:05:50 -08002454static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07002455{
2456#ifdef CONFIG_SMP
2457 check_irq_off();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002458 assert_spin_locked(&get_node(cachep, node)->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002459#endif
2460}
2461
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462#else
2463#define check_irq_off() do { } while(0)
2464#define check_irq_on() do { } while(0)
2465#define check_spinlock_acquired(x) do { } while(0)
Christoph Lametere498be72005-09-09 13:03:32 -07002466#define check_spinlock_acquired_node(x, y) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002467#endif
2468
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002469static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
Christoph Lameteraab22072006-03-22 00:09:06 -08002470 struct array_cache *ac,
2471 int force, int node);
2472
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473static void do_drain(void *arg)
2474{
Andrew Mortona737b3e2006-03-22 00:08:11 -08002475 struct kmem_cache *cachep = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 struct array_cache *ac;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07002477 int node = numa_mem_id();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002478 struct kmem_cache_node *n;
Joonsoo Kim97654df2014-08-06 16:04:25 -07002479 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002482 ac = cpu_cache_get(cachep);
Christoph Lameter18bf8542014-08-06 16:04:11 -07002483 n = get_node(cachep, node);
2484 spin_lock(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07002485 free_block(cachep, ac->entry, ac->avail, node, &list);
Christoph Lameter18bf8542014-08-06 16:04:11 -07002486 spin_unlock(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07002487 slabs_destroy(cachep, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 ac->avail = 0;
2489}
2490
Pekka Enberg343e0d72006-02-01 03:05:50 -08002491static void drain_cpu_caches(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002493 struct kmem_cache_node *n;
Christoph Lametere498be72005-09-09 13:03:32 -07002494 int node;
2495
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002496 on_each_cpu(do_drain, cachep, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 check_irq_on();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002498 for_each_kmem_cache_node(cachep, node, n)
2499 if (n->alien)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002500 drain_alien_cache(cachep, n->alien);
Roland Dreiera4523a82006-05-15 11:41:00 -07002501
Christoph Lameter18bf8542014-08-06 16:04:11 -07002502 for_each_kmem_cache_node(cachep, node, n)
2503 drain_array(cachep, n, n->shared, 1, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
2505
Christoph Lametered11d9e2006-06-30 01:55:45 -07002506/*
2507 * Remove slabs from the list of free slabs.
2508 * Specify the number of slabs to drain in tofree.
2509 *
2510 * Returns the actual number of slabs released.
2511 */
2512static int drain_freelist(struct kmem_cache *cache,
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002513 struct kmem_cache_node *n, int tofree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514{
Christoph Lametered11d9e2006-06-30 01:55:45 -07002515 struct list_head *p;
2516 int nr_freed;
Joonsoo Kim8456a642013-10-24 10:07:49 +09002517 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518
Christoph Lametered11d9e2006-06-30 01:55:45 -07002519 nr_freed = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002520 while (nr_freed < tofree && !list_empty(&n->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002522 spin_lock_irq(&n->list_lock);
2523 p = n->slabs_free.prev;
2524 if (p == &n->slabs_free) {
2525 spin_unlock_irq(&n->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002526 goto out;
2527 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Joonsoo Kim8456a642013-10-24 10:07:49 +09002529 page = list_entry(p, struct page, lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530#if DEBUG
Joonsoo Kim8456a642013-10-24 10:07:49 +09002531 BUG_ON(page->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532#endif
Joonsoo Kim8456a642013-10-24 10:07:49 +09002533 list_del(&page->lru);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002534 /*
2535 * Safe to drop the lock. The slab is no longer linked
2536 * to the cache.
2537 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002538 n->free_objects -= cache->num;
2539 spin_unlock_irq(&n->list_lock);
Joonsoo Kim8456a642013-10-24 10:07:49 +09002540 slab_destroy(cache, page);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002541 nr_freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 }
Christoph Lametered11d9e2006-06-30 01:55:45 -07002543out:
2544 return nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545}
2546
Vladimir Davydov03afc0e2014-06-04 16:07:20 -07002547int __kmem_cache_shrink(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07002548{
Christoph Lameter18bf8542014-08-06 16:04:11 -07002549 int ret = 0;
2550 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002551 struct kmem_cache_node *n;
Christoph Lametere498be72005-09-09 13:03:32 -07002552
2553 drain_cpu_caches(cachep);
2554
2555 check_irq_on();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002556 for_each_kmem_cache_node(cachep, node, n) {
Wanpeng Li0fa81032013-07-04 08:33:22 +08002557 drain_freelist(cachep, n, slabs_tofree(cachep, n));
Christoph Lametered11d9e2006-06-30 01:55:45 -07002558
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002559 ret += !list_empty(&n->slabs_full) ||
2560 !list_empty(&n->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07002561 }
2562 return (ret ? 1 : 0);
2563}
2564
Christoph Lameter945cf2b2012-09-04 23:18:33 +00002565int __kmem_cache_shutdown(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
Christoph Lameter12c36672012-09-04 23:38:33 +00002567 int i;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002568 struct kmem_cache_node *n;
Vladimir Davydov03afc0e2014-06-04 16:07:20 -07002569 int rc = __kmem_cache_shrink(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Christoph Lameter12c36672012-09-04 23:38:33 +00002571 if (rc)
2572 return rc;
2573
2574 for_each_online_cpu(i)
2575 kfree(cachep->array[i]);
2576
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002577 /* NUMA: free the node structures */
Christoph Lameter18bf8542014-08-06 16:04:11 -07002578 for_each_kmem_cache_node(cachep, i, n) {
2579 kfree(n->shared);
2580 free_alien_cache(n->alien);
2581 kfree(n);
2582 cachep->node[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 }
Christoph Lameter12c36672012-09-04 23:38:33 +00002584 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002587/*
2588 * Get the memory for a slab management obj.
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08002589 *
2590 * For a slab cache when the slab descriptor is off-slab, the
2591 * slab descriptor can't come from the same cache which is being created,
2592 * Because if it is the case, that means we defer the creation of
2593 * the kmalloc_{dma,}_cache of size sizeof(slab descriptor) to this point.
2594 * And we eventually call down to __kmem_cache_create(), which
2595 * in turn looks up in the kmalloc_{dma,}_caches for the disired-size one.
2596 * This is a "chicken-and-egg" problem.
2597 *
2598 * So the off-slab slab descriptor shall come from the kmalloc_{dma,}_caches,
2599 * which are all initialized during kmem_cache_init().
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002600 */
Joonsoo Kim7e007352013-10-30 19:04:01 +09002601static void *alloc_slabmgmt(struct kmem_cache *cachep,
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002602 struct page *page, int colour_off,
2603 gfp_t local_flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604{
Joonsoo Kim7e007352013-10-30 19:04:01 +09002605 void *freelist;
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002606 void *addr = page_address(page);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002607
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 if (OFF_SLAB(cachep)) {
2609 /* Slab management obj is off-slab. */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002610 freelist = kmem_cache_alloc_node(cachep->freelist_cache,
Pekka Enberg8759ec52008-11-26 10:01:31 +02002611 local_flags, nodeid);
Joonsoo Kim8456a642013-10-24 10:07:49 +09002612 if (!freelist)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 return NULL;
2614 } else {
Joonsoo Kim8456a642013-10-24 10:07:49 +09002615 freelist = addr + colour_off;
2616 colour_off += cachep->freelist_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
Joonsoo Kim8456a642013-10-24 10:07:49 +09002618 page->active = 0;
2619 page->s_mem = addr + colour_off;
2620 return freelist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621}
2622
Joonsoo Kim7cc689732014-04-18 16:24:09 +09002623static inline freelist_idx_t get_free_obj(struct page *page, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624{
Joonsoo Kima41adfa2013-12-02 17:49:42 +09002625 return ((freelist_idx_t *)page->freelist)[idx];
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002626}
2627
2628static inline void set_free_obj(struct page *page,
Joonsoo Kim7cc689732014-04-18 16:24:09 +09002629 unsigned int idx, freelist_idx_t val)
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002630{
Joonsoo Kima41adfa2013-12-02 17:49:42 +09002631 ((freelist_idx_t *)(page->freelist))[idx] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632}
2633
Pekka Enberg343e0d72006-02-01 03:05:50 -08002634static void cache_init_objs(struct kmem_cache *cachep,
Joonsoo Kim8456a642013-10-24 10:07:49 +09002635 struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636{
2637 int i;
2638
2639 for (i = 0; i < cachep->num; i++) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09002640 void *objp = index_to_obj(cachep, page, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641#if DEBUG
2642 /* need to poison the objs? */
2643 if (cachep->flags & SLAB_POISON)
2644 poison_obj(cachep, objp, POISON_FREE);
2645 if (cachep->flags & SLAB_STORE_USER)
2646 *dbg_userword(cachep, objp) = NULL;
2647
2648 if (cachep->flags & SLAB_RED_ZONE) {
2649 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2650 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2651 }
2652 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002653 * Constructors are not allowed to allocate memory from the same
2654 * cache which they are a constructor for. Otherwise, deadlock.
2655 * They must also be threaded.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 */
2657 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002658 cachep->ctor(objp + obj_offset(cachep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
2660 if (cachep->flags & SLAB_RED_ZONE) {
2661 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2662 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002663 " end of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2665 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002666 " start of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 }
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002668 if ((cachep->size % PAGE_SIZE) == 0 &&
Andrew Mortona737b3e2006-03-22 00:08:11 -08002669 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002670 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002671 cachep->size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672#else
2673 if (cachep->ctor)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002674 cachep->ctor(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675#endif
Joonsoo Kim03787302014-06-23 13:22:06 -07002676 set_obj_status(page, i, OBJECT_FREE);
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002677 set_free_obj(page, i, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679}
2680
Pekka Enberg343e0d72006-02-01 03:05:50 -08002681static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682{
Christoph Lameter4b51d662007-02-10 01:43:10 -08002683 if (CONFIG_ZONE_DMA_FLAG) {
2684 if (flags & GFP_DMA)
Glauber Costaa618e892012-06-14 16:17:21 +04002685 BUG_ON(!(cachep->allocflags & GFP_DMA));
Christoph Lameter4b51d662007-02-10 01:43:10 -08002686 else
Glauber Costaa618e892012-06-14 16:17:21 +04002687 BUG_ON(cachep->allocflags & GFP_DMA);
Christoph Lameter4b51d662007-02-10 01:43:10 -08002688 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689}
2690
Joonsoo Kim8456a642013-10-24 10:07:49 +09002691static void *slab_get_obj(struct kmem_cache *cachep, struct page *page,
Andrew Mortona737b3e2006-03-22 00:08:11 -08002692 int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002693{
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002694 void *objp;
Matthew Dobson78d382d2006-02-01 03:05:47 -08002695
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002696 objp = index_to_obj(cachep, page, get_free_obj(page, page->active));
Joonsoo Kim8456a642013-10-24 10:07:49 +09002697 page->active++;
Matthew Dobson78d382d2006-02-01 03:05:47 -08002698#if DEBUG
Joonsoo Kim1ea991b2013-10-24 10:07:40 +09002699 WARN_ON(page_to_nid(virt_to_page(objp)) != nodeid);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002700#endif
Matthew Dobson78d382d2006-02-01 03:05:47 -08002701
2702 return objp;
2703}
2704
Joonsoo Kim8456a642013-10-24 10:07:49 +09002705static void slab_put_obj(struct kmem_cache *cachep, struct page *page,
Andrew Mortona737b3e2006-03-22 00:08:11 -08002706 void *objp, int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002707{
Joonsoo Kim8456a642013-10-24 10:07:49 +09002708 unsigned int objnr = obj_to_index(cachep, page, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002709#if DEBUG
Joonsoo Kim16025172013-10-24 10:07:46 +09002710 unsigned int i;
Matthew Dobson78d382d2006-02-01 03:05:47 -08002711
Matthew Dobson78d382d2006-02-01 03:05:47 -08002712 /* Verify that the slab belongs to the intended node */
Joonsoo Kim1ea991b2013-10-24 10:07:40 +09002713 WARN_ON(page_to_nid(virt_to_page(objp)) != nodeid);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002714
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002715 /* Verify double free bug */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002716 for (i = page->active; i < cachep->num; i++) {
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002717 if (get_free_obj(page, i) == objnr) {
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002718 printk(KERN_ERR "slab: double free detected in cache "
2719 "'%s', objp %p\n", cachep->name, objp);
2720 BUG();
2721 }
Matthew Dobson78d382d2006-02-01 03:05:47 -08002722 }
2723#endif
Joonsoo Kim8456a642013-10-24 10:07:49 +09002724 page->active--;
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002725 set_free_obj(page, page->active, objnr);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002726}
2727
Pekka Enberg47768742006-06-23 02:03:07 -07002728/*
2729 * Map pages beginning at addr to the given cache and slab. This is required
2730 * for the slab allocator to be able to lookup the cache and slab of a
Nick Pigginccd35fb2011-01-07 17:49:17 +11002731 * virtual address for kfree, ksize, and slab debugging.
Pekka Enberg47768742006-06-23 02:03:07 -07002732 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002733static void slab_map_pages(struct kmem_cache *cache, struct page *page,
Joonsoo Kim7e007352013-10-30 19:04:01 +09002734 void *freelist)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735{
Joonsoo Kima57a4982013-10-24 10:07:44 +09002736 page->slab_cache = cache;
Joonsoo Kim8456a642013-10-24 10:07:49 +09002737 page->freelist = freelist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
2739
2740/*
2741 * Grow (by 1) the number of slabs within a cache. This is called by
2742 * kmem_cache_alloc() when there are no active objs left in a cache.
2743 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002744static int cache_grow(struct kmem_cache *cachep,
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002745 gfp_t flags, int nodeid, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746{
Joonsoo Kim7e007352013-10-30 19:04:01 +09002747 void *freelist;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002748 size_t offset;
2749 gfp_t local_flags;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002750 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Andrew Mortona737b3e2006-03-22 00:08:11 -08002752 /*
2753 * Be lazy and only check for valid flags here, keeping it out of the
2754 * critical path in kmem_cache_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 */
Christoph Lameter6cb06222007-10-16 01:25:41 -07002756 BUG_ON(flags & GFP_SLAB_BUG_MASK);
2757 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002759 /* Take the node list lock to change the colour_next on this node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 check_irq_off();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002761 n = get_node(cachep, nodeid);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002762 spin_lock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
2764 /* Get colour for the slab, and cal the next value. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002765 offset = n->colour_next;
2766 n->colour_next++;
2767 if (n->colour_next >= cachep->colour)
2768 n->colour_next = 0;
2769 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002771 offset *= cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
2773 if (local_flags & __GFP_WAIT)
2774 local_irq_enable();
2775
2776 /*
2777 * The test for missing atomic flag is performed here, rather than
2778 * the more obvious place, simply to reduce the critical path length
2779 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2780 * will eventually be caught here (where it matters).
2781 */
2782 kmem_flagcheck(cachep, flags);
2783
Andrew Mortona737b3e2006-03-22 00:08:11 -08002784 /*
2785 * Get mem for the objs. Attempt to allocate a physical page from
2786 * 'nodeid'.
Christoph Lametere498be72005-09-09 13:03:32 -07002787 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002788 if (!page)
2789 page = kmem_getpages(cachep, local_flags, nodeid);
2790 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 goto failed;
2792
2793 /* Get slab management. */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002794 freelist = alloc_slabmgmt(cachep, page, offset,
Christoph Lameter6cb06222007-10-16 01:25:41 -07002795 local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
Joonsoo Kim8456a642013-10-24 10:07:49 +09002796 if (!freelist)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 goto opps1;
2798
Joonsoo Kim8456a642013-10-24 10:07:49 +09002799 slab_map_pages(cachep, page, freelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
Joonsoo Kim8456a642013-10-24 10:07:49 +09002801 cache_init_objs(cachep, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802
2803 if (local_flags & __GFP_WAIT)
2804 local_irq_disable();
2805 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002806 spin_lock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807
2808 /* Make slab active. */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002809 list_add_tail(&page->lru, &(n->slabs_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 STATS_INC_GROWN(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002811 n->free_objects += cachep->num;
2812 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002814opps1:
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002815 kmem_freepages(cachep, page);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002816failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 if (local_flags & __GFP_WAIT)
2818 local_irq_disable();
2819 return 0;
2820}
2821
2822#if DEBUG
2823
2824/*
2825 * Perform extra freeing checks:
2826 * - detect bad pointers.
2827 * - POISON/RED_ZONE checking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 */
2829static void kfree_debugcheck(const void *objp)
2830{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 if (!virt_addr_valid(objp)) {
2832 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002833 (unsigned long)objp);
2834 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836}
2837
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002838static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2839{
David Woodhouseb46b8f12007-05-08 00:22:59 -07002840 unsigned long long redzone1, redzone2;
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002841
2842 redzone1 = *dbg_redzone1(cache, obj);
2843 redzone2 = *dbg_redzone2(cache, obj);
2844
2845 /*
2846 * Redzone is ok.
2847 */
2848 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2849 return;
2850
2851 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2852 slab_error(cache, "double free detected");
2853 else
2854 slab_error(cache, "memory outside object was overwritten");
2855
David Woodhouseb46b8f12007-05-08 00:22:59 -07002856 printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002857 obj, redzone1, redzone2);
2858}
2859
Pekka Enberg343e0d72006-02-01 03:05:50 -08002860static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002861 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 unsigned int objnr;
Joonsoo Kim8456a642013-10-24 10:07:49 +09002864 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865
Matthew Wilcox80cbd912007-11-29 12:05:13 -07002866 BUG_ON(virt_to_cache(objp) != cachep);
2867
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002868 objp -= obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 kfree_debugcheck(objp);
Christoph Lameterb49af682007-05-06 14:49:41 -07002870 page = virt_to_head_page(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 if (cachep->flags & SLAB_RED_ZONE) {
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002873 verify_redzone_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2875 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2876 }
2877 if (cachep->flags & SLAB_STORE_USER)
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002878 *dbg_userword(cachep, objp) = (void *)caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
Joonsoo Kim8456a642013-10-24 10:07:49 +09002880 objnr = obj_to_index(cachep, page, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
2882 BUG_ON(objnr >= cachep->num);
Joonsoo Kim8456a642013-10-24 10:07:49 +09002883 BUG_ON(objp != index_to_obj(cachep, page, objnr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
Joonsoo Kim03787302014-06-23 13:22:06 -07002885 set_obj_status(page, objnr, OBJECT_FREE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 if (cachep->flags & SLAB_POISON) {
2887#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002888 if ((cachep->size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002889 store_stackinfo(cachep, objp, caller);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002890 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002891 cachep->size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 } else {
2893 poison_obj(cachep, objp, POISON_FREE);
2894 }
2895#else
2896 poison_obj(cachep, objp, POISON_FREE);
2897#endif
2898 }
2899 return objp;
2900}
2901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902#else
2903#define kfree_debugcheck(x) do { } while(0)
2904#define cache_free_debugcheck(x,objp,z) (objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905#endif
2906
Mel Gorman072bb0a2012-07-31 16:43:58 -07002907static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags,
2908 bool force_refill)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909{
2910 int batchcount;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002911 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 struct array_cache *ac;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002913 int node;
2914
Joe Korty6d2144d2008-03-05 15:04:59 -08002915 check_irq_off();
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07002916 node = numa_mem_id();
Mel Gorman072bb0a2012-07-31 16:43:58 -07002917 if (unlikely(force_refill))
2918 goto force_grow;
2919retry:
Joe Korty6d2144d2008-03-05 15:04:59 -08002920 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 batchcount = ac->batchcount;
2922 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002923 /*
2924 * If there was little recent activity on this cache, then
2925 * perform only a partial refill. Otherwise we could generate
2926 * refill bouncing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 */
2928 batchcount = BATCHREFILL_LIMIT;
2929 }
Christoph Lameter18bf8542014-08-06 16:04:11 -07002930 n = get_node(cachep, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002932 BUG_ON(ac->avail > 0 || !n);
2933 spin_lock(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002934
Christoph Lameter3ded1752006-03-25 03:06:44 -08002935 /* See if we can refill from the shared array */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002936 if (n->shared && transfer_objects(ac, n->shared, batchcount)) {
2937 n->shared->touched = 1;
Christoph Lameter3ded1752006-03-25 03:06:44 -08002938 goto alloc_done;
Nick Piggin44b57f12010-01-27 22:27:40 +11002939 }
Christoph Lameter3ded1752006-03-25 03:06:44 -08002940
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 while (batchcount > 0) {
2942 struct list_head *entry;
Joonsoo Kim8456a642013-10-24 10:07:49 +09002943 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 /* Get slab alloc is to come from. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002945 entry = n->slabs_partial.next;
2946 if (entry == &n->slabs_partial) {
2947 n->free_touched = 1;
2948 entry = n->slabs_free.next;
2949 if (entry == &n->slabs_free)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 goto must_grow;
2951 }
2952
Joonsoo Kim8456a642013-10-24 10:07:49 +09002953 page = list_entry(entry, struct page, lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 check_spinlock_acquired(cachep);
Pekka Enberg714b81712007-05-06 14:49:03 -07002955
2956 /*
2957 * The slab was either on partial or free list so
2958 * there must be at least one object available for
2959 * allocation.
2960 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002961 BUG_ON(page->active >= cachep->num);
Pekka Enberg714b81712007-05-06 14:49:03 -07002962
Joonsoo Kim8456a642013-10-24 10:07:49 +09002963 while (page->active < cachep->num && batchcount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 STATS_INC_ALLOCED(cachep);
2965 STATS_INC_ACTIVE(cachep);
2966 STATS_SET_HIGH(cachep);
2967
Joonsoo Kim8456a642013-10-24 10:07:49 +09002968 ac_put_obj(cachep, ac, slab_get_obj(cachep, page,
Mel Gorman072bb0a2012-07-31 16:43:58 -07002969 node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971
2972 /* move slabp to correct slabp list: */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002973 list_del(&page->lru);
2974 if (page->active == cachep->num)
Dave Hansen34bf6ef2014-04-08 13:44:27 -07002975 list_add(&page->lru, &n->slabs_full);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002976 else
Dave Hansen34bf6ef2014-04-08 13:44:27 -07002977 list_add(&page->lru, &n->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 }
2979
Andrew Mortona737b3e2006-03-22 00:08:11 -08002980must_grow:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002981 n->free_objects -= ac->avail;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002982alloc_done:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002983 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
2985 if (unlikely(!ac->avail)) {
2986 int x;
Mel Gorman072bb0a2012-07-31 16:43:58 -07002987force_grow:
Christoph Lameter3c517a62006-12-06 20:33:29 -08002988 x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07002989
Andrew Mortona737b3e2006-03-22 00:08:11 -08002990 /* cache_grow can reenable interrupts, then ac could change. */
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002991 ac = cpu_cache_get(cachep);
David Rientjes51cd8e62012-08-28 19:57:21 -07002992 node = numa_mem_id();
Mel Gorman072bb0a2012-07-31 16:43:58 -07002993
2994 /* no objects in sight? abort */
2995 if (!x && (ac->avail == 0 || force_refill))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 return NULL;
2997
Andrew Mortona737b3e2006-03-22 00:08:11 -08002998 if (!ac->avail) /* objects refilled by interrupt? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 goto retry;
3000 }
3001 ac->touched = 1;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003002
3003 return ac_get_obj(cachep, ac, flags, force_refill);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004}
3005
Andrew Mortona737b3e2006-03-22 00:08:11 -08003006static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
3007 gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008{
3009 might_sleep_if(flags & __GFP_WAIT);
3010#if DEBUG
3011 kmem_flagcheck(cachep, flags);
3012#endif
3013}
3014
3015#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003016static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003017 gfp_t flags, void *objp, unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018{
Joonsoo Kim03787302014-06-23 13:22:06 -07003019 struct page *page;
3020
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003021 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 return objp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003023 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003025 if ((cachep->size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003026 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003027 cachep->size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 else
3029 check_poison_obj(cachep, objp);
3030#else
3031 check_poison_obj(cachep, objp);
3032#endif
3033 poison_obj(cachep, objp, POISON_INUSE);
3034 }
3035 if (cachep->flags & SLAB_STORE_USER)
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003036 *dbg_userword(cachep, objp) = (void *)caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037
3038 if (cachep->flags & SLAB_RED_ZONE) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003039 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
3040 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
3041 slab_error(cachep, "double free, or memory outside"
3042 " object was overwritten");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003043 printk(KERN_ERR
David Woodhouseb46b8f12007-05-08 00:22:59 -07003044 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08003045 objp, *dbg_redzone1(cachep, objp),
3046 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 }
3048 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
3049 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
3050 }
Joonsoo Kim03787302014-06-23 13:22:06 -07003051
3052 page = virt_to_head_page(objp);
3053 set_obj_status(page, obj_to_index(cachep, page, objp), OBJECT_ACTIVE);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003054 objp += obj_offset(cachep);
Christoph Lameter4f104932007-05-06 14:50:17 -07003055 if (cachep->ctor && cachep->flags & SLAB_POISON)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003056 cachep->ctor(objp);
Tetsuo Handa7ea466f2011-07-21 09:42:45 +09003057 if (ARCH_SLAB_MINALIGN &&
3058 ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003059 printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
Hugh Dickinsc2251502011-07-11 13:35:08 -07003060 objp, (int)ARCH_SLAB_MINALIGN);
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 return objp;
3063}
3064#else
3065#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
3066#endif
3067
Akinobu Mita773ff602008-12-23 19:37:01 +09003068static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags)
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003069{
Joonsoo Kim8a9c61d2014-08-06 16:04:20 -07003070 if (unlikely(cachep == kmem_cache))
Akinobu Mita773ff602008-12-23 19:37:01 +09003071 return false;
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003072
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003073 return should_failslab(cachep->object_size, flags, cachep->flags);
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003074}
3075
Pekka Enberg343e0d72006-02-01 03:05:50 -08003076static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003078 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 struct array_cache *ac;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003080 bool force_refill = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081
Alok N Kataria5c382302005-09-27 21:45:46 -07003082 check_irq_off();
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003083
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003084 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 if (likely(ac->avail)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 ac->touched = 1;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003087 objp = ac_get_obj(cachep, ac, flags, false);
3088
J. R. Okajimaddbf2e82009-12-02 16:55:50 +09003089 /*
Mel Gorman072bb0a2012-07-31 16:43:58 -07003090 * Allow for the possibility all avail objects are not allowed
3091 * by the current flags
J. R. Okajimaddbf2e82009-12-02 16:55:50 +09003092 */
Mel Gorman072bb0a2012-07-31 16:43:58 -07003093 if (objp) {
3094 STATS_INC_ALLOCHIT(cachep);
3095 goto out;
3096 }
3097 force_refill = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 }
Mel Gorman072bb0a2012-07-31 16:43:58 -07003099
3100 STATS_INC_ALLOCMISS(cachep);
3101 objp = cache_alloc_refill(cachep, flags, force_refill);
3102 /*
3103 * the 'ac' may be updated by cache_alloc_refill(),
3104 * and kmemleak_erase() requires its correct value.
3105 */
3106 ac = cpu_cache_get(cachep);
3107
3108out:
Catalin Marinasd5cff632009-06-11 13:22:40 +01003109 /*
3110 * To avoid a false negative, if an object that is in one of the
3111 * per-CPU caches is leaked, we need to make sure kmemleak doesn't
3112 * treat the array pointers as a reference to the object.
3113 */
J. R. Okajimaf3d8b532009-12-02 16:55:49 +09003114 if (objp)
3115 kmemleak_erase(&ac->entry[ac->avail]);
Alok N Kataria5c382302005-09-27 21:45:46 -07003116 return objp;
3117}
3118
Christoph Lametere498be72005-09-09 13:03:32 -07003119#ifdef CONFIG_NUMA
3120/*
David Rientjesf0432d12014-04-07 15:37:30 -07003121 * Try allocating on another node if PF_SPREAD_SLAB is a mempolicy is set.
Paul Jacksonc61afb12006-03-24 03:16:08 -08003122 *
3123 * If we are in_interrupt, then process context, including cpusets and
3124 * mempolicy, may not apply and should not be used for allocation policy.
3125 */
3126static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
3127{
3128 int nid_alloc, nid_here;
3129
Christoph Lameter765c4502006-09-27 01:50:08 -07003130 if (in_interrupt() || (flags & __GFP_THISNODE))
Paul Jacksonc61afb12006-03-24 03:16:08 -08003131 return NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003132 nid_alloc = nid_here = numa_mem_id();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003133 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
Jack Steiner6adef3e2010-05-26 14:42:49 -07003134 nid_alloc = cpuset_slab_spread_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003135 else if (current->mempolicy)
David Rientjes2a389612014-04-07 15:37:29 -07003136 nid_alloc = mempolicy_slab_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003137 if (nid_alloc != nid_here)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003138 return ____cache_alloc_node(cachep, flags, nid_alloc);
Paul Jacksonc61afb12006-03-24 03:16:08 -08003139 return NULL;
3140}
3141
3142/*
Christoph Lameter765c4502006-09-27 01:50:08 -07003143 * Fallback function if there was no memory available and no objects on a
Christoph Lameter3c517a62006-12-06 20:33:29 -08003144 * certain node and fall back is permitted. First we scan all the
Christoph Lameter6a673682013-01-10 19:14:19 +00003145 * available node for available objects. If that fails then we
Christoph Lameter3c517a62006-12-06 20:33:29 -08003146 * perform an allocation without specifying a node. This allows the page
3147 * allocator to do its reclaim / fallback magic. We then insert the
3148 * slab into the proper nodelist and then allocate from it.
Christoph Lameter765c4502006-09-27 01:50:08 -07003149 */
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003150static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
Christoph Lameter765c4502006-09-27 01:50:08 -07003151{
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003152 struct zonelist *zonelist;
3153 gfp_t local_flags;
Mel Gormandd1a2392008-04-28 02:12:17 -07003154 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07003155 struct zone *zone;
3156 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003157 void *obj = NULL;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003158 int nid;
Mel Gormancc9a6c82012-03-21 16:34:11 -07003159 unsigned int cpuset_mems_cookie;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003160
3161 if (flags & __GFP_THISNODE)
3162 return NULL;
3163
Christoph Lameter6cb06222007-10-16 01:25:41 -07003164 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Christoph Lameter765c4502006-09-27 01:50:08 -07003165
Mel Gormancc9a6c82012-03-21 16:34:11 -07003166retry_cpuset:
Mel Gormand26914d2014-04-03 14:47:24 -07003167 cpuset_mems_cookie = read_mems_allowed_begin();
David Rientjes2a389612014-04-07 15:37:29 -07003168 zonelist = node_zonelist(mempolicy_slab_node(), flags);
Mel Gormancc9a6c82012-03-21 16:34:11 -07003169
Christoph Lameter3c517a62006-12-06 20:33:29 -08003170retry:
3171 /*
3172 * Look through allowed nodes for objects available
3173 * from existing per node queues.
3174 */
Mel Gorman54a6eb52008-04-28 02:12:16 -07003175 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
3176 nid = zone_to_nid(zone);
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003177
Mel Gorman54a6eb52008-04-28 02:12:16 -07003178 if (cpuset_zone_allowed_hardwall(zone, flags) &&
Christoph Lameter18bf8542014-08-06 16:04:11 -07003179 get_node(cache, nid) &&
3180 get_node(cache, nid)->free_objects) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003181 obj = ____cache_alloc_node(cache,
3182 flags | GFP_THISNODE, nid);
Christoph Lameter481c5342008-06-21 16:46:35 -07003183 if (obj)
3184 break;
3185 }
Christoph Lameter3c517a62006-12-06 20:33:29 -08003186 }
3187
Christoph Lametercfce6602007-05-06 14:50:17 -07003188 if (!obj) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003189 /*
3190 * This allocation will be performed within the constraints
3191 * of the current cpuset / memory policy requirements.
3192 * We may trigger various forms of reclaim on the allowed
3193 * set and go into memory reserves if necessary.
3194 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003195 struct page *page;
3196
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003197 if (local_flags & __GFP_WAIT)
3198 local_irq_enable();
3199 kmem_flagcheck(cache, flags);
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003200 page = kmem_getpages(cache, local_flags, numa_mem_id());
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003201 if (local_flags & __GFP_WAIT)
3202 local_irq_disable();
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003203 if (page) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003204 /*
3205 * Insert into the appropriate per node queues
3206 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003207 nid = page_to_nid(page);
3208 if (cache_grow(cache, flags, nid, page)) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003209 obj = ____cache_alloc_node(cache,
3210 flags | GFP_THISNODE, nid);
3211 if (!obj)
3212 /*
3213 * Another processor may allocate the
3214 * objects in the slab since we are
3215 * not holding any locks.
3216 */
3217 goto retry;
3218 } else {
Hugh Dickinsb6a60452007-01-05 16:36:36 -08003219 /* cache_grow already freed obj */
Christoph Lameter3c517a62006-12-06 20:33:29 -08003220 obj = NULL;
3221 }
3222 }
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003223 }
Mel Gormancc9a6c82012-03-21 16:34:11 -07003224
Mel Gormand26914d2014-04-03 14:47:24 -07003225 if (unlikely(!obj && read_mems_allowed_retry(cpuset_mems_cookie)))
Mel Gormancc9a6c82012-03-21 16:34:11 -07003226 goto retry_cpuset;
Christoph Lameter765c4502006-09-27 01:50:08 -07003227 return obj;
3228}
3229
3230/*
Christoph Lametere498be72005-09-09 13:03:32 -07003231 * A interface to enable slab creation on nodeid
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003233static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003234 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07003235{
3236 struct list_head *entry;
Joonsoo Kim8456a642013-10-24 10:07:49 +09003237 struct page *page;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003238 struct kmem_cache_node *n;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003239 void *obj;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003240 int x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
Aaron Tomlin14e50c62013-04-26 16:15:34 +01003242 VM_BUG_ON(nodeid > num_online_nodes());
Christoph Lameter18bf8542014-08-06 16:04:11 -07003243 n = get_node(cachep, nodeid);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003244 BUG_ON(!n);
Christoph Lametere498be72005-09-09 13:03:32 -07003245
Andrew Mortona737b3e2006-03-22 00:08:11 -08003246retry:
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003247 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003248 spin_lock(&n->list_lock);
3249 entry = n->slabs_partial.next;
3250 if (entry == &n->slabs_partial) {
3251 n->free_touched = 1;
3252 entry = n->slabs_free.next;
3253 if (entry == &n->slabs_free)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003254 goto must_grow;
3255 }
Christoph Lametere498be72005-09-09 13:03:32 -07003256
Joonsoo Kim8456a642013-10-24 10:07:49 +09003257 page = list_entry(entry, struct page, lru);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003258 check_spinlock_acquired_node(cachep, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07003259
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003260 STATS_INC_NODEALLOCS(cachep);
3261 STATS_INC_ACTIVE(cachep);
3262 STATS_SET_HIGH(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003263
Joonsoo Kim8456a642013-10-24 10:07:49 +09003264 BUG_ON(page->active == cachep->num);
Christoph Lametere498be72005-09-09 13:03:32 -07003265
Joonsoo Kim8456a642013-10-24 10:07:49 +09003266 obj = slab_get_obj(cachep, page, nodeid);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003267 n->free_objects--;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003268 /* move slabp to correct slabp list: */
Joonsoo Kim8456a642013-10-24 10:07:49 +09003269 list_del(&page->lru);
Christoph Lametere498be72005-09-09 13:03:32 -07003270
Joonsoo Kim8456a642013-10-24 10:07:49 +09003271 if (page->active == cachep->num)
3272 list_add(&page->lru, &n->slabs_full);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003273 else
Joonsoo Kim8456a642013-10-24 10:07:49 +09003274 list_add(&page->lru, &n->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07003275
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003276 spin_unlock(&n->list_lock);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003277 goto done;
Christoph Lametere498be72005-09-09 13:03:32 -07003278
Andrew Mortona737b3e2006-03-22 00:08:11 -08003279must_grow:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003280 spin_unlock(&n->list_lock);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003281 x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
Christoph Lameter765c4502006-09-27 01:50:08 -07003282 if (x)
3283 goto retry;
Christoph Lametere498be72005-09-09 13:03:32 -07003284
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003285 return fallback_alloc(cachep, flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003286
Andrew Mortona737b3e2006-03-22 00:08:11 -08003287done:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003288 return obj;
Christoph Lametere498be72005-09-09 13:03:32 -07003289}
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003290
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003291static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003292slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003293 unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003294{
3295 unsigned long save_flags;
3296 void *ptr;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003297 int slab_node = numa_mem_id();
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003298
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003299 flags &= gfp_allowed_mask;
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003300
Nick Piggincf40bd12009-01-21 08:12:39 +01003301 lockdep_trace_alloc(flags);
3302
Akinobu Mita773ff602008-12-23 19:37:01 +09003303 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003304 return NULL;
3305
Glauber Costad79923f2012-12-18 14:22:48 -08003306 cachep = memcg_kmem_get_cache(cachep, flags);
3307
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003308 cache_alloc_debugcheck_before(cachep, flags);
3309 local_irq_save(save_flags);
3310
Andrew Mortoneacbbae2011-07-28 13:59:49 -07003311 if (nodeid == NUMA_NO_NODE)
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003312 nodeid = slab_node;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003313
Christoph Lameter18bf8542014-08-06 16:04:11 -07003314 if (unlikely(!get_node(cachep, nodeid))) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003315 /* Node not bootstrapped yet */
3316 ptr = fallback_alloc(cachep, flags);
3317 goto out;
3318 }
3319
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003320 if (nodeid == slab_node) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003321 /*
3322 * Use the locally cached objects if possible.
3323 * However ____cache_alloc does not allow fallback
3324 * to other nodes. It may fail while we still have
3325 * objects on other nodes available.
3326 */
3327 ptr = ____cache_alloc(cachep, flags);
3328 if (ptr)
3329 goto out;
3330 }
3331 /* ___cache_alloc_node can fall back to other nodes */
3332 ptr = ____cache_alloc_node(cachep, flags, nodeid);
3333 out:
3334 local_irq_restore(save_flags);
3335 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003336 kmemleak_alloc_recursive(ptr, cachep->object_size, 1, cachep->flags,
Catalin Marinasd5cff632009-06-11 13:22:40 +01003337 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003338
Joe Perches5087c822013-09-10 17:02:51 -07003339 if (likely(ptr)) {
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003340 kmemcheck_slab_alloc(cachep, flags, ptr, cachep->object_size);
Joe Perches5087c822013-09-10 17:02:51 -07003341 if (unlikely(flags & __GFP_ZERO))
3342 memset(ptr, 0, cachep->object_size);
3343 }
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003344
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003345 return ptr;
3346}
3347
3348static __always_inline void *
3349__do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
3350{
3351 void *objp;
3352
David Rientjesf0432d12014-04-07 15:37:30 -07003353 if (current->mempolicy || unlikely(current->flags & PF_SPREAD_SLAB)) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003354 objp = alternate_node_alloc(cache, flags);
3355 if (objp)
3356 goto out;
3357 }
3358 objp = ____cache_alloc(cache, flags);
3359
3360 /*
3361 * We may just have run out of memory on the local node.
3362 * ____cache_alloc_node() knows how to locate memory on other nodes
3363 */
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003364 if (!objp)
3365 objp = ____cache_alloc_node(cache, flags, numa_mem_id());
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003366
3367 out:
3368 return objp;
3369}
3370#else
3371
3372static __always_inline void *
3373__do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
3374{
3375 return ____cache_alloc(cachep, flags);
3376}
3377
3378#endif /* CONFIG_NUMA */
3379
3380static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003381slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003382{
3383 unsigned long save_flags;
3384 void *objp;
3385
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003386 flags &= gfp_allowed_mask;
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003387
Nick Piggincf40bd12009-01-21 08:12:39 +01003388 lockdep_trace_alloc(flags);
3389
Akinobu Mita773ff602008-12-23 19:37:01 +09003390 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003391 return NULL;
3392
Glauber Costad79923f2012-12-18 14:22:48 -08003393 cachep = memcg_kmem_get_cache(cachep, flags);
3394
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003395 cache_alloc_debugcheck_before(cachep, flags);
3396 local_irq_save(save_flags);
3397 objp = __do_cache_alloc(cachep, flags);
3398 local_irq_restore(save_flags);
3399 objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003400 kmemleak_alloc_recursive(objp, cachep->object_size, 1, cachep->flags,
Catalin Marinasd5cff632009-06-11 13:22:40 +01003401 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003402 prefetchw(objp);
3403
Joe Perches5087c822013-09-10 17:02:51 -07003404 if (likely(objp)) {
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003405 kmemcheck_slab_alloc(cachep, flags, objp, cachep->object_size);
Joe Perches5087c822013-09-10 17:02:51 -07003406 if (unlikely(flags & __GFP_ZERO))
3407 memset(objp, 0, cachep->object_size);
3408 }
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003409
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003410 return objp;
3411}
Christoph Lametere498be72005-09-09 13:03:32 -07003412
3413/*
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08003414 * Caller needs to acquire correct kmem_cache_node's list_lock
Joonsoo Kim97654df2014-08-06 16:04:25 -07003415 * @list: List of detached free slabs should be freed by caller
Christoph Lametere498be72005-09-09 13:03:32 -07003416 */
Joonsoo Kim97654df2014-08-06 16:04:25 -07003417static void free_block(struct kmem_cache *cachep, void **objpp,
3418 int nr_objects, int node, struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419{
3420 int i;
Joonsoo Kim25c063f2014-08-06 16:04:22 -07003421 struct kmem_cache_node *n = get_node(cachep, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422
3423 for (i = 0; i < nr_objects; i++) {
Mel Gorman072bb0a2012-07-31 16:43:58 -07003424 void *objp;
Joonsoo Kim8456a642013-10-24 10:07:49 +09003425 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
Mel Gorman072bb0a2012-07-31 16:43:58 -07003427 clear_obj_pfmemalloc(&objpp[i]);
3428 objp = objpp[i];
3429
Joonsoo Kim8456a642013-10-24 10:07:49 +09003430 page = virt_to_head_page(objp);
Joonsoo Kim8456a642013-10-24 10:07:49 +09003431 list_del(&page->lru);
Christoph Lameterff694162005-09-22 21:44:02 -07003432 check_spinlock_acquired_node(cachep, node);
Joonsoo Kim8456a642013-10-24 10:07:49 +09003433 slab_put_obj(cachep, page, objp, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 STATS_DEC_ACTIVE(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003435 n->free_objects++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
3437 /* fixup slab chains */
Joonsoo Kim8456a642013-10-24 10:07:49 +09003438 if (page->active == 0) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003439 if (n->free_objects > n->free_limit) {
3440 n->free_objects -= cachep->num;
Joonsoo Kim97654df2014-08-06 16:04:25 -07003441 list_add_tail(&page->lru, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442 } else {
Joonsoo Kim8456a642013-10-24 10:07:49 +09003443 list_add(&page->lru, &n->slabs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 }
3445 } else {
3446 /* Unconditionally move a slab to the end of the
3447 * partial list on free - maximum time for the
3448 * other objects to be freed, too.
3449 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09003450 list_add_tail(&page->lru, &n->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 }
3452 }
3453}
3454
Pekka Enberg343e0d72006-02-01 03:05:50 -08003455static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456{
3457 int batchcount;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003458 struct kmem_cache_node *n;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003459 int node = numa_mem_id();
Joonsoo Kim97654df2014-08-06 16:04:25 -07003460 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
3462 batchcount = ac->batchcount;
3463#if DEBUG
3464 BUG_ON(!batchcount || batchcount > ac->avail);
3465#endif
3466 check_irq_off();
Christoph Lameter18bf8542014-08-06 16:04:11 -07003467 n = get_node(cachep, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003468 spin_lock(&n->list_lock);
3469 if (n->shared) {
3470 struct array_cache *shared_array = n->shared;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003471 int max = shared_array->limit - shared_array->avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 if (max) {
3473 if (batchcount > max)
3474 batchcount = max;
Christoph Lametere498be72005-09-09 13:03:32 -07003475 memcpy(&(shared_array->entry[shared_array->avail]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003476 ac->entry, sizeof(void *) * batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 shared_array->avail += batchcount;
3478 goto free_done;
3479 }
3480 }
3481
Joonsoo Kim97654df2014-08-06 16:04:25 -07003482 free_block(cachep, ac->entry, batchcount, node, &list);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003483free_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484#if STATS
3485 {
3486 int i = 0;
3487 struct list_head *p;
3488
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003489 p = n->slabs_free.next;
3490 while (p != &(n->slabs_free)) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09003491 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
Joonsoo Kim8456a642013-10-24 10:07:49 +09003493 page = list_entry(p, struct page, lru);
3494 BUG_ON(page->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495
3496 i++;
3497 p = p->next;
3498 }
3499 STATS_SET_FREEABLE(cachep, i);
3500 }
3501#endif
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003502 spin_unlock(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07003503 slabs_destroy(cachep, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 ac->avail -= batchcount;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003505 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506}
3507
3508/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08003509 * Release an obj back to its cache. If the obj has a constructed state, it must
3510 * be in this state _before_ it is released. Called with disabled ints.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 */
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003512static inline void __cache_free(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003513 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514{
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003515 struct array_cache *ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
3517 check_irq_off();
Catalin Marinasd5cff632009-06-11 13:22:40 +01003518 kmemleak_free_recursive(objp, cachep->flags);
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003519 objp = cache_free_debugcheck(cachep, objp, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003521 kmemcheck_slab_free(cachep, objp, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003522
Siddha, Suresh B1807a1a2007-08-22 14:01:49 -07003523 /*
3524 * Skip calling cache_free_alien() when the platform is not numa.
3525 * This will avoid cache misses that happen while accessing slabp (which
3526 * is per page memory reference) to get nodeid. Instead use a global
3527 * variable to skip the call, which is mostly likely to be present in
3528 * the cache.
3529 */
Mel Gormanb6e68bc2009-06-16 15:32:16 -07003530 if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07003531 return;
Christoph Lametere498be72005-09-09 13:03:32 -07003532
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 if (likely(ac->avail < ac->limit)) {
3534 STATS_INC_FREEHIT(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 } else {
3536 STATS_INC_FREEMISS(cachep);
3537 cache_flusharray(cachep, ac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 }
Zhao Jin42c8c992011-08-27 00:26:17 +08003539
Mel Gorman072bb0a2012-07-31 16:43:58 -07003540 ac_put_obj(cachep, ac, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541}
3542
3543/**
3544 * kmem_cache_alloc - Allocate an object
3545 * @cachep: The cache to allocate from.
3546 * @flags: See kmalloc().
3547 *
3548 * Allocate an object from this cache. The flags are only relevant
3549 * if the cache has no available objects.
3550 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003551void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003553 void *ret = slab_alloc(cachep, flags, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003554
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003555 trace_kmem_cache_alloc(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003556 cachep->object_size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003557
3558 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559}
3560EXPORT_SYMBOL(kmem_cache_alloc);
3561
Li Zefan0f24f122009-12-11 15:45:30 +08003562#ifdef CONFIG_TRACING
Steven Rostedt85beb582010-11-24 16:23:34 -05003563void *
Ezequiel Garcia40521472012-09-08 17:47:56 -03003564kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003565{
Steven Rostedt85beb582010-11-24 16:23:34 -05003566 void *ret;
3567
Ezequiel Garcia48356302012-09-08 17:47:57 -03003568 ret = slab_alloc(cachep, flags, _RET_IP_);
Steven Rostedt85beb582010-11-24 16:23:34 -05003569
3570 trace_kmalloc(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003571 size, cachep->size, flags);
Steven Rostedt85beb582010-11-24 16:23:34 -05003572 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003573}
Steven Rostedt85beb582010-11-24 16:23:34 -05003574EXPORT_SYMBOL(kmem_cache_alloc_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003575#endif
3576
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577#ifdef CONFIG_NUMA
Zhouping Liud0d04b72013-05-16 11:36:23 +08003578/**
3579 * kmem_cache_alloc_node - Allocate an object on the specified node
3580 * @cachep: The cache to allocate from.
3581 * @flags: See kmalloc().
3582 * @nodeid: node number of the target node.
3583 *
3584 * Identical to kmem_cache_alloc but it will allocate memory on the given
3585 * node, which can improve the performance for cpu bound structures.
3586 *
3587 * Fallback to other node is possible if __GFP_THISNODE is not set.
3588 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003589void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
3590{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003591 void *ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003592
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003593 trace_kmem_cache_alloc_node(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003594 cachep->object_size, cachep->size,
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003595 flags, nodeid);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003596
3597 return ret;
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003598}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599EXPORT_SYMBOL(kmem_cache_alloc_node);
3600
Li Zefan0f24f122009-12-11 15:45:30 +08003601#ifdef CONFIG_TRACING
Ezequiel Garcia40521472012-09-08 17:47:56 -03003602void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
Steven Rostedt85beb582010-11-24 16:23:34 -05003603 gfp_t flags,
Ezequiel Garcia40521472012-09-08 17:47:56 -03003604 int nodeid,
3605 size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003606{
Steven Rostedt85beb582010-11-24 16:23:34 -05003607 void *ret;
3608
Ezequiel Garcia592f4142012-09-25 08:07:08 -03003609 ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003610
Steven Rostedt85beb582010-11-24 16:23:34 -05003611 trace_kmalloc_node(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003612 size, cachep->size,
Steven Rostedt85beb582010-11-24 16:23:34 -05003613 flags, nodeid);
3614 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003615}
Steven Rostedt85beb582010-11-24 16:23:34 -05003616EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003617#endif
3618
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003619static __always_inline void *
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003620__do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller)
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003621{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003622 struct kmem_cache *cachep;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003623
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003624 cachep = kmalloc_slab(size, flags);
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003625 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3626 return cachep;
Ezequiel Garcia40521472012-09-08 17:47:56 -03003627 return kmem_cache_alloc_node_trace(cachep, flags, node, size);
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003628}
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003629
Li Zefan0bb38a52009-12-11 15:45:50 +08003630#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003631void *__kmalloc_node(size_t size, gfp_t flags, int node)
3632{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003633 return __do_kmalloc_node(size, flags, node, _RET_IP_);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003634}
Christoph Hellwigdbe5e692006-09-25 23:31:36 -07003635EXPORT_SYMBOL(__kmalloc_node);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003636
3637void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003638 int node, unsigned long caller)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003639{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003640 return __do_kmalloc_node(size, flags, node, caller);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003641}
3642EXPORT_SYMBOL(__kmalloc_node_track_caller);
3643#else
3644void *__kmalloc_node(size_t size, gfp_t flags, int node)
3645{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003646 return __do_kmalloc_node(size, flags, node, 0);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003647}
3648EXPORT_SYMBOL(__kmalloc_node);
Li Zefan0bb38a52009-12-11 15:45:50 +08003649#endif /* CONFIG_DEBUG_SLAB || CONFIG_TRACING */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003650#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651
3652/**
Paul Drynoff800590f2006-06-23 02:03:48 -07003653 * __do_kmalloc - allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654 * @size: how many bytes of memory are required.
Paul Drynoff800590f2006-06-23 02:03:48 -07003655 * @flags: the type of memory to allocate (see kmalloc).
Randy Dunlap911851e2006-03-22 00:08:14 -08003656 * @caller: function caller for debug tracking of the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 */
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003658static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003659 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003661 struct kmem_cache *cachep;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003662 void *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003664 cachep = kmalloc_slab(size, flags);
Linus Torvaldsa5c96d82007-07-19 13:17:15 -07003665 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3666 return cachep;
Ezequiel Garcia48356302012-09-08 17:47:57 -03003667 ret = slab_alloc(cachep, flags, caller);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003668
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003669 trace_kmalloc(caller, ret,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003670 size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003671
3672 return ret;
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003673}
3674
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003675
Li Zefan0bb38a52009-12-11 15:45:50 +08003676#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003677void *__kmalloc(size_t size, gfp_t flags)
3678{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003679 return __do_kmalloc(size, flags, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680}
3681EXPORT_SYMBOL(__kmalloc);
3682
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003683void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003684{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003685 return __do_kmalloc(size, flags, caller);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003686}
3687EXPORT_SYMBOL(__kmalloc_track_caller);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003688
3689#else
3690void *__kmalloc(size_t size, gfp_t flags)
3691{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003692 return __do_kmalloc(size, flags, 0);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003693}
3694EXPORT_SYMBOL(__kmalloc);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003695#endif
3696
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697/**
3698 * kmem_cache_free - Deallocate an object
3699 * @cachep: The cache the allocation was from.
3700 * @objp: The previously allocated object.
3701 *
3702 * Free an object which was previously allocated from this
3703 * cache.
3704 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003705void kmem_cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706{
3707 unsigned long flags;
Glauber Costab9ce5ef2012-12-18 14:22:46 -08003708 cachep = cache_from_obj(cachep, objp);
3709 if (!cachep)
3710 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711
3712 local_irq_save(flags);
Feng Tangd97d4762012-07-02 14:29:10 +08003713 debug_check_no_locks_freed(objp, cachep->object_size);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07003714 if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003715 debug_check_no_obj_freed(objp, cachep->object_size);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003716 __cache_free(cachep, objp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 local_irq_restore(flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003718
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003719 trace_kmem_cache_free(_RET_IP_, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720}
3721EXPORT_SYMBOL(kmem_cache_free);
3722
3723/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 * kfree - free previously allocated memory
3725 * @objp: pointer returned by kmalloc.
3726 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07003727 * If @objp is NULL, no operation is performed.
3728 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 * Don't free memory not originally allocated by kmalloc()
3730 * or you will run into trouble.
3731 */
3732void kfree(const void *objp)
3733{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003734 struct kmem_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 unsigned long flags;
3736
Pekka Enberg2121db72009-03-25 11:05:57 +02003737 trace_kfree(_RET_IP_, objp);
3738
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003739 if (unlikely(ZERO_OR_NULL_PTR(objp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 return;
3741 local_irq_save(flags);
3742 kfree_debugcheck(objp);
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003743 c = virt_to_cache(objp);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003744 debug_check_no_locks_freed(objp, c->object_size);
3745
3746 debug_check_no_obj_freed(objp, c->object_size);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003747 __cache_free(c, (void *)objp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748 local_irq_restore(flags);
3749}
3750EXPORT_SYMBOL(kfree);
3751
Christoph Lametere498be72005-09-09 13:03:32 -07003752/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003753 * This initializes kmem_cache_node or resizes various caches for all nodes.
Christoph Lametere498be72005-09-09 13:03:32 -07003754 */
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08003755static int alloc_kmem_cache_node(struct kmem_cache *cachep, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -07003756{
3757 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003758 struct kmem_cache_node *n;
Christoph Lametercafeb022006-03-25 03:06:46 -08003759 struct array_cache *new_shared;
Paul Menage3395ee02006-12-06 20:32:16 -08003760 struct array_cache **new_alien = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003761
Mel Gorman9c09a952008-01-24 05:49:54 -08003762 for_each_online_node(node) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003763
Paul Menage3395ee02006-12-06 20:32:16 -08003764 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03003765 new_alien = alloc_alien_cache(node, cachep->limit, gfp);
Paul Menage3395ee02006-12-06 20:32:16 -08003766 if (!new_alien)
3767 goto fail;
3768 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003769
Eric Dumazet63109842007-05-06 14:49:28 -07003770 new_shared = NULL;
3771 if (cachep->shared) {
3772 new_shared = alloc_arraycache(node,
Christoph Lameter0718dc22006-03-25 03:06:47 -08003773 cachep->shared*cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003774 0xbaadf00d, gfp);
Eric Dumazet63109842007-05-06 14:49:28 -07003775 if (!new_shared) {
3776 free_alien_cache(new_alien);
3777 goto fail;
3778 }
Christoph Lameter0718dc22006-03-25 03:06:47 -08003779 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003780
Christoph Lameter18bf8542014-08-06 16:04:11 -07003781 n = get_node(cachep, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003782 if (n) {
3783 struct array_cache *shared = n->shared;
Joonsoo Kim97654df2014-08-06 16:04:25 -07003784 LIST_HEAD(list);
Christoph Lametercafeb022006-03-25 03:06:46 -08003785
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003786 spin_lock_irq(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07003787
Christoph Lametercafeb022006-03-25 03:06:46 -08003788 if (shared)
Christoph Lameter0718dc22006-03-25 03:06:47 -08003789 free_block(cachep, shared->entry,
Joonsoo Kim97654df2014-08-06 16:04:25 -07003790 shared->avail, node, &list);
Christoph Lametere498be72005-09-09 13:03:32 -07003791
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003792 n->shared = new_shared;
3793 if (!n->alien) {
3794 n->alien = new_alien;
Christoph Lametere498be72005-09-09 13:03:32 -07003795 new_alien = NULL;
3796 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003797 n->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003798 cachep->batchcount + cachep->num;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003799 spin_unlock_irq(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07003800 slabs_destroy(cachep, &list);
Christoph Lametercafeb022006-03-25 03:06:46 -08003801 kfree(shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003802 free_alien_cache(new_alien);
3803 continue;
3804 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003805 n = kmalloc_node(sizeof(struct kmem_cache_node), gfp, node);
3806 if (!n) {
Christoph Lameter0718dc22006-03-25 03:06:47 -08003807 free_alien_cache(new_alien);
3808 kfree(new_shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003809 goto fail;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003810 }
Christoph Lametere498be72005-09-09 13:03:32 -07003811
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003812 kmem_cache_node_init(n);
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08003813 n->next_reap = jiffies + REAPTIMEOUT_NODE +
3814 ((unsigned long)cachep) % REAPTIMEOUT_NODE;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003815 n->shared = new_shared;
3816 n->alien = new_alien;
3817 n->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003818 cachep->batchcount + cachep->num;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003819 cachep->node[node] = n;
Christoph Lametere498be72005-09-09 13:03:32 -07003820 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003821 return 0;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003822
Andrew Mortona737b3e2006-03-22 00:08:11 -08003823fail:
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003824 if (!cachep->list.next) {
Christoph Lameter0718dc22006-03-25 03:06:47 -08003825 /* Cache is not active yet. Roll back what we did */
3826 node--;
3827 while (node >= 0) {
Christoph Lameter18bf8542014-08-06 16:04:11 -07003828 n = get_node(cachep, node);
3829 if (n) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003830 kfree(n->shared);
3831 free_alien_cache(n->alien);
3832 kfree(n);
Christoph Lameter6a673682013-01-10 19:14:19 +00003833 cachep->node[node] = NULL;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003834 }
3835 node--;
3836 }
3837 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003838 return -ENOMEM;
Christoph Lametere498be72005-09-09 13:03:32 -07003839}
3840
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841struct ccupdate_struct {
Pekka Enberg343e0d72006-02-01 03:05:50 -08003842 struct kmem_cache *cachep;
Eric Dumazetacfe7d72011-07-25 08:55:42 +02003843 struct array_cache *new[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844};
3845
3846static void do_ccupdate_local(void *info)
3847{
Andrew Mortona737b3e2006-03-22 00:08:11 -08003848 struct ccupdate_struct *new = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 struct array_cache *old;
3850
3851 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003852 old = cpu_cache_get(new->cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003853
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
3855 new->new[smp_processor_id()] = old;
3856}
3857
Christoph Lameter18004c52012-07-06 15:25:12 -05003858/* Always called with the slab_mutex held */
Glauber Costa943a4512012-12-18 14:23:03 -08003859static int __do_tune_cpucache(struct kmem_cache *cachep, int limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003860 int batchcount, int shared, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861{
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003862 struct ccupdate_struct *new;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003863 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864
Eric Dumazetacfe7d72011-07-25 08:55:42 +02003865 new = kzalloc(sizeof(*new) + nr_cpu_ids * sizeof(struct array_cache *),
3866 gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003867 if (!new)
3868 return -ENOMEM;
3869
Christoph Lametere498be72005-09-09 13:03:32 -07003870 for_each_online_cpu(i) {
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003871 new->new[i] = alloc_arraycache(cpu_to_mem(i), limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003872 batchcount, gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003873 if (!new->new[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003874 for (i--; i >= 0; i--)
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003875 kfree(new->new[i]);
3876 kfree(new);
Christoph Lametere498be72005-09-09 13:03:32 -07003877 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 }
3879 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003880 new->cachep = cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881
Jens Axboe15c8b6c2008-05-09 09:39:44 +02003882 on_each_cpu(do_ccupdate_local, (void *)new, 1);
Christoph Lametere498be72005-09-09 13:03:32 -07003883
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 check_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 cachep->batchcount = batchcount;
3886 cachep->limit = limit;
Christoph Lametere498be72005-09-09 13:03:32 -07003887 cachep->shared = shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888
Christoph Lametere498be72005-09-09 13:03:32 -07003889 for_each_online_cpu(i) {
Joonsoo Kim97654df2014-08-06 16:04:25 -07003890 LIST_HEAD(list);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003891 struct array_cache *ccold = new->new[i];
Christoph Lameter18bf8542014-08-06 16:04:11 -07003892 int node;
3893 struct kmem_cache_node *n;
3894
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 if (!ccold)
3896 continue;
Christoph Lameter18bf8542014-08-06 16:04:11 -07003897
3898 node = cpu_to_mem(i);
3899 n = get_node(cachep, node);
3900 spin_lock_irq(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07003901 free_block(cachep, ccold->entry, ccold->avail, node, &list);
Christoph Lameter18bf8542014-08-06 16:04:11 -07003902 spin_unlock_irq(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07003903 slabs_destroy(cachep, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 kfree(ccold);
3905 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003906 kfree(new);
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08003907 return alloc_kmem_cache_node(cachep, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908}
3909
Glauber Costa943a4512012-12-18 14:23:03 -08003910static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
3911 int batchcount, int shared, gfp_t gfp)
3912{
3913 int ret;
3914 struct kmem_cache *c = NULL;
3915 int i = 0;
3916
3917 ret = __do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
3918
3919 if (slab_state < FULL)
3920 return ret;
3921
3922 if ((ret < 0) || !is_root_cache(cachep))
3923 return ret;
3924
Glauber Costaebe945c2012-12-18 14:23:10 -08003925 VM_BUG_ON(!mutex_is_locked(&slab_mutex));
Glauber Costa943a4512012-12-18 14:23:03 -08003926 for_each_memcg_cache_index(i) {
Qiang Huang2ade4de2013-11-12 15:08:23 -08003927 c = cache_from_memcg_idx(cachep, i);
Glauber Costa943a4512012-12-18 14:23:03 -08003928 if (c)
3929 /* return value determined by the parent cache only */
3930 __do_tune_cpucache(c, limit, batchcount, shared, gfp);
3931 }
3932
3933 return ret;
3934}
3935
Christoph Lameter18004c52012-07-06 15:25:12 -05003936/* Called with slab_mutex held always */
Pekka Enberg83b519e2009-06-10 19:40:04 +03003937static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938{
3939 int err;
Glauber Costa943a4512012-12-18 14:23:03 -08003940 int limit = 0;
3941 int shared = 0;
3942 int batchcount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943
Glauber Costa943a4512012-12-18 14:23:03 -08003944 if (!is_root_cache(cachep)) {
3945 struct kmem_cache *root = memcg_root_cache(cachep);
3946 limit = root->limit;
3947 shared = root->shared;
3948 batchcount = root->batchcount;
3949 }
3950
3951 if (limit && shared && batchcount)
3952 goto skip_setup;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003953 /*
3954 * The head array serves three purposes:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 * - create a LIFO ordering, i.e. return objects that are cache-warm
3956 * - reduce the number of spinlock operations.
Andrew Mortona737b3e2006-03-22 00:08:11 -08003957 * - reduce the number of linked list operations on the slab and
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958 * bufctl chains: array operations are cheaper.
3959 * The numbers are guessed, we should auto-tune as described by
3960 * Bonwick.
3961 */
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003962 if (cachep->size > 131072)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 limit = 1;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003964 else if (cachep->size > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 limit = 8;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003966 else if (cachep->size > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 limit = 24;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003968 else if (cachep->size > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 limit = 54;
3970 else
3971 limit = 120;
3972
Andrew Mortona737b3e2006-03-22 00:08:11 -08003973 /*
3974 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 * allocation behaviour: Most allocs on one cpu, most free operations
3976 * on another cpu. For these cases, an efficient object passing between
3977 * cpus is necessary. This is provided by a shared array. The array
3978 * replaces Bonwick's magazine layer.
3979 * On uniprocessor, it's functionally equivalent (but less efficient)
3980 * to a larger limit. Thus disabled by default.
3981 */
3982 shared = 0;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003983 if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 shared = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
3986#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003987 /*
3988 * With debugging enabled, large batchcount lead to excessively long
3989 * periods with disabled local interrupts. Limit the batchcount
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 */
3991 if (limit > 32)
3992 limit = 32;
3993#endif
Glauber Costa943a4512012-12-18 14:23:03 -08003994 batchcount = (limit + 1) / 2;
3995skip_setup:
3996 err = do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997 if (err)
3998 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003999 cachep->name, -err);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07004000 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001}
4002
Christoph Lameter1b552532006-03-22 00:09:07 -08004003/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004004 * Drain an array if it contains any elements taking the node lock only if
4005 * necessary. Note that the node listlock also protects the array_cache
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004006 * if drain_array() is used on the shared array.
Christoph Lameter1b552532006-03-22 00:09:07 -08004007 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004008static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
Christoph Lameter1b552532006-03-22 00:09:07 -08004009 struct array_cache *ac, int force, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010{
Joonsoo Kim97654df2014-08-06 16:04:25 -07004011 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 int tofree;
4013
Christoph Lameter1b552532006-03-22 00:09:07 -08004014 if (!ac || !ac->avail)
4015 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 if (ac->touched && !force) {
4017 ac->touched = 0;
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004018 } else {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004019 spin_lock_irq(&n->list_lock);
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004020 if (ac->avail) {
4021 tofree = force ? ac->avail : (ac->limit + 4) / 5;
4022 if (tofree > ac->avail)
4023 tofree = (ac->avail + 1) / 2;
Joonsoo Kim97654df2014-08-06 16:04:25 -07004024 free_block(cachep, ac->entry, tofree, node, &list);
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004025 ac->avail -= tofree;
4026 memmove(ac->entry, &(ac->entry[tofree]),
4027 sizeof(void *) * ac->avail);
4028 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004029 spin_unlock_irq(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07004030 slabs_destroy(cachep, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 }
4032}
4033
4034/**
4035 * cache_reap - Reclaim memory from caches.
Randy Dunlap05fb6bf2007-02-28 20:12:13 -08004036 * @w: work descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 *
4038 * Called from workqueue/eventd every few seconds.
4039 * Purpose:
4040 * - clear the per-cpu caches for this CPU.
4041 * - return freeable pages to the main free memory pool.
4042 *
Andrew Mortona737b3e2006-03-22 00:08:11 -08004043 * If we cannot acquire the cache chain mutex then just give up - we'll try
4044 * again on the next iteration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004046static void cache_reap(struct work_struct *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047{
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004048 struct kmem_cache *searchp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004049 struct kmem_cache_node *n;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07004050 int node = numa_mem_id();
Jean Delvarebf6aede2009-04-02 16:56:54 -07004051 struct delayed_work *work = to_delayed_work(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052
Christoph Lameter18004c52012-07-06 15:25:12 -05004053 if (!mutex_trylock(&slab_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 /* Give up. Setup the next iteration. */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004055 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056
Christoph Lameter18004c52012-07-06 15:25:12 -05004057 list_for_each_entry(searchp, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 check_irq_on();
4059
Christoph Lameter35386e32006-03-22 00:09:05 -08004060 /*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004061 * We only take the node lock if absolutely necessary and we
Christoph Lameter35386e32006-03-22 00:09:05 -08004062 * have established with reasonable certainty that
4063 * we can do some work if the lock was obtained.
4064 */
Christoph Lameter18bf8542014-08-06 16:04:11 -07004065 n = get_node(searchp, node);
Christoph Lameter35386e32006-03-22 00:09:05 -08004066
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004067 reap_alien(searchp, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004069 drain_array(searchp, n, cpu_cache_get(searchp), 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
Christoph Lameter35386e32006-03-22 00:09:05 -08004071 /*
4072 * These are racy checks but it does not matter
4073 * if we skip one check or scan twice.
4074 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004075 if (time_after(n->next_reap, jiffies))
Christoph Lameter35386e32006-03-22 00:09:05 -08004076 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08004078 n->next_reap = jiffies + REAPTIMEOUT_NODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004080 drain_array(searchp, n, n->shared, 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004082 if (n->free_touched)
4083 n->free_touched = 0;
Christoph Lametered11d9e2006-06-30 01:55:45 -07004084 else {
4085 int freed;
4086
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004087 freed = drain_freelist(searchp, n, (n->free_limit +
Christoph Lametered11d9e2006-06-30 01:55:45 -07004088 5 * searchp->num - 1) / (5 * searchp->num));
4089 STATS_ADD_REAPED(searchp, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 }
Christoph Lameter35386e32006-03-22 00:09:05 -08004091next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 cond_resched();
4093 }
4094 check_irq_on();
Christoph Lameter18004c52012-07-06 15:25:12 -05004095 mutex_unlock(&slab_mutex);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004096 next_reap_node();
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004097out:
Andrew Mortona737b3e2006-03-22 00:08:11 -08004098 /* Set up the next iteration */
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08004099 schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_AC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100}
4101
Linus Torvalds158a9622008-01-02 13:04:48 -08004102#ifdef CONFIG_SLABINFO
Glauber Costa0d7561c2012-10-19 18:20:27 +04004103void get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104{
Joonsoo Kim8456a642013-10-24 10:07:49 +09004105 struct page *page;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004106 unsigned long active_objs;
4107 unsigned long num_objs;
4108 unsigned long active_slabs = 0;
4109 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004110 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 char *error = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07004112 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004113 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 active_objs = 0;
4116 num_slabs = 0;
Christoph Lameter18bf8542014-08-06 16:04:11 -07004117 for_each_kmem_cache_node(cachep, node, n) {
Christoph Lametere498be72005-09-09 13:03:32 -07004118
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004119 check_irq_on();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004120 spin_lock_irq(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07004121
Joonsoo Kim8456a642013-10-24 10:07:49 +09004122 list_for_each_entry(page, &n->slabs_full, lru) {
4123 if (page->active != cachep->num && !error)
Christoph Lametere498be72005-09-09 13:03:32 -07004124 error = "slabs_full accounting error";
4125 active_objs += cachep->num;
4126 active_slabs++;
4127 }
Joonsoo Kim8456a642013-10-24 10:07:49 +09004128 list_for_each_entry(page, &n->slabs_partial, lru) {
4129 if (page->active == cachep->num && !error)
Joonsoo Kim106a74e2013-10-24 10:07:48 +09004130 error = "slabs_partial accounting error";
Joonsoo Kim8456a642013-10-24 10:07:49 +09004131 if (!page->active && !error)
Joonsoo Kim106a74e2013-10-24 10:07:48 +09004132 error = "slabs_partial accounting error";
Joonsoo Kim8456a642013-10-24 10:07:49 +09004133 active_objs += page->active;
Christoph Lametere498be72005-09-09 13:03:32 -07004134 active_slabs++;
4135 }
Joonsoo Kim8456a642013-10-24 10:07:49 +09004136 list_for_each_entry(page, &n->slabs_free, lru) {
4137 if (page->active && !error)
Joonsoo Kim106a74e2013-10-24 10:07:48 +09004138 error = "slabs_free accounting error";
Christoph Lametere498be72005-09-09 13:03:32 -07004139 num_slabs++;
4140 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004141 free_objects += n->free_objects;
4142 if (n->shared)
4143 shared_avail += n->shared->avail;
Christoph Lametere498be72005-09-09 13:03:32 -07004144
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004145 spin_unlock_irq(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004147 num_slabs += active_slabs;
4148 num_objs = num_slabs * cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07004149 if (num_objs - active_objs != free_objects && !error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 error = "free_objects accounting error";
4151
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004152 name = cachep->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 if (error)
4154 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
4155
Glauber Costa0d7561c2012-10-19 18:20:27 +04004156 sinfo->active_objs = active_objs;
4157 sinfo->num_objs = num_objs;
4158 sinfo->active_slabs = active_slabs;
4159 sinfo->num_slabs = num_slabs;
4160 sinfo->shared_avail = shared_avail;
4161 sinfo->limit = cachep->limit;
4162 sinfo->batchcount = cachep->batchcount;
4163 sinfo->shared = cachep->shared;
4164 sinfo->objects_per_slab = cachep->num;
4165 sinfo->cache_order = cachep->gfporder;
4166}
4167
4168void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *cachep)
4169{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170#if STATS
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004171 { /* node stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 unsigned long high = cachep->high_mark;
4173 unsigned long allocs = cachep->num_allocations;
4174 unsigned long grown = cachep->grown;
4175 unsigned long reaped = cachep->reaped;
4176 unsigned long errors = cachep->errors;
4177 unsigned long max_freeable = cachep->max_freeable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 unsigned long node_allocs = cachep->node_allocs;
Christoph Lametere498be72005-09-09 13:03:32 -07004179 unsigned long node_frees = cachep->node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004180 unsigned long overflows = cachep->node_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
Joe Perchese92dd4f2010-03-26 19:27:58 -07004182 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu "
4183 "%4lu %4lu %4lu %4lu %4lu",
4184 allocs, high, grown,
4185 reaped, errors, max_freeable, node_allocs,
4186 node_frees, overflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 }
4188 /* cpu stats */
4189 {
4190 unsigned long allochit = atomic_read(&cachep->allochit);
4191 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
4192 unsigned long freehit = atomic_read(&cachep->freehit);
4193 unsigned long freemiss = atomic_read(&cachep->freemiss);
4194
4195 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004196 allochit, allocmiss, freehit, freemiss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 }
4198#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199}
4200
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201#define MAX_SLABINFO_WRITE 128
4202/**
4203 * slabinfo_write - Tuning for the slab allocator
4204 * @file: unused
4205 * @buffer: user buffer
4206 * @count: data length
4207 * @ppos: unused
4208 */
Glauber Costab7454ad2012-10-19 18:20:25 +04004209ssize_t slabinfo_write(struct file *file, const char __user *buffer,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004210 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004212 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 int limit, batchcount, shared, res;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004214 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004215
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 if (count > MAX_SLABINFO_WRITE)
4217 return -EINVAL;
4218 if (copy_from_user(&kbuf, buffer, count))
4219 return -EFAULT;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004220 kbuf[MAX_SLABINFO_WRITE] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221
4222 tmp = strchr(kbuf, ' ');
4223 if (!tmp)
4224 return -EINVAL;
4225 *tmp = '\0';
4226 tmp++;
4227 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
4228 return -EINVAL;
4229
4230 /* Find the cache in the chain of caches. */
Christoph Lameter18004c52012-07-06 15:25:12 -05004231 mutex_lock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 res = -EINVAL;
Christoph Lameter18004c52012-07-06 15:25:12 -05004233 list_for_each_entry(cachep, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 if (!strcmp(cachep->name, kbuf)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08004235 if (limit < 1 || batchcount < 1 ||
4236 batchcount > limit || shared < 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07004237 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07004239 res = do_tune_cpucache(cachep, limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03004240 batchcount, shared,
4241 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 }
4243 break;
4244 }
4245 }
Christoph Lameter18004c52012-07-06 15:25:12 -05004246 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247 if (res >= 0)
4248 res = count;
4249 return res;
4250}
Al Viro871751e2006-03-25 03:06:39 -08004251
4252#ifdef CONFIG_DEBUG_SLAB_LEAK
4253
4254static void *leaks_start(struct seq_file *m, loff_t *pos)
4255{
Christoph Lameter18004c52012-07-06 15:25:12 -05004256 mutex_lock(&slab_mutex);
4257 return seq_list_start(&slab_caches, *pos);
Al Viro871751e2006-03-25 03:06:39 -08004258}
4259
4260static inline int add_caller(unsigned long *n, unsigned long v)
4261{
4262 unsigned long *p;
4263 int l;
4264 if (!v)
4265 return 1;
4266 l = n[1];
4267 p = n + 2;
4268 while (l) {
4269 int i = l/2;
4270 unsigned long *q = p + 2 * i;
4271 if (*q == v) {
4272 q[1]++;
4273 return 1;
4274 }
4275 if (*q > v) {
4276 l = i;
4277 } else {
4278 p = q + 2;
4279 l -= i + 1;
4280 }
4281 }
4282 if (++n[1] == n[0])
4283 return 0;
4284 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4285 p[0] = v;
4286 p[1] = 1;
4287 return 1;
4288}
4289
Joonsoo Kim8456a642013-10-24 10:07:49 +09004290static void handle_slab(unsigned long *n, struct kmem_cache *c,
4291 struct page *page)
Al Viro871751e2006-03-25 03:06:39 -08004292{
4293 void *p;
Joonsoo Kim03787302014-06-23 13:22:06 -07004294 int i;
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09004295
Al Viro871751e2006-03-25 03:06:39 -08004296 if (n[0] == n[1])
4297 return;
Joonsoo Kim8456a642013-10-24 10:07:49 +09004298 for (i = 0, p = page->s_mem; i < c->num; i++, p += c->size) {
Joonsoo Kim03787302014-06-23 13:22:06 -07004299 if (get_obj_status(page, i) != OBJECT_ACTIVE)
Al Viro871751e2006-03-25 03:06:39 -08004300 continue;
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09004301
Al Viro871751e2006-03-25 03:06:39 -08004302 if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
4303 return;
4304 }
4305}
4306
4307static void show_symbol(struct seq_file *m, unsigned long address)
4308{
4309#ifdef CONFIG_KALLSYMS
Al Viro871751e2006-03-25 03:06:39 -08004310 unsigned long offset, size;
Tejun Heo9281ace2007-07-17 04:03:51 -07004311 char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
Al Viro871751e2006-03-25 03:06:39 -08004312
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004313 if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
Al Viro871751e2006-03-25 03:06:39 -08004314 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004315 if (modname[0])
Al Viro871751e2006-03-25 03:06:39 -08004316 seq_printf(m, " [%s]", modname);
4317 return;
4318 }
4319#endif
4320 seq_printf(m, "%p", (void *)address);
4321}
4322
4323static int leaks_show(struct seq_file *m, void *p)
4324{
Thierry Reding0672aa72012-06-22 19:42:49 +02004325 struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);
Joonsoo Kim8456a642013-10-24 10:07:49 +09004326 struct page *page;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004327 struct kmem_cache_node *n;
Al Viro871751e2006-03-25 03:06:39 -08004328 const char *name;
Christoph Lameterdb845062013-02-05 18:45:23 +00004329 unsigned long *x = m->private;
Al Viro871751e2006-03-25 03:06:39 -08004330 int node;
4331 int i;
4332
4333 if (!(cachep->flags & SLAB_STORE_USER))
4334 return 0;
4335 if (!(cachep->flags & SLAB_RED_ZONE))
4336 return 0;
4337
4338 /* OK, we can do it */
4339
Christoph Lameterdb845062013-02-05 18:45:23 +00004340 x[1] = 0;
Al Viro871751e2006-03-25 03:06:39 -08004341
Christoph Lameter18bf8542014-08-06 16:04:11 -07004342 for_each_kmem_cache_node(cachep, node, n) {
Al Viro871751e2006-03-25 03:06:39 -08004343
4344 check_irq_on();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004345 spin_lock_irq(&n->list_lock);
Al Viro871751e2006-03-25 03:06:39 -08004346
Joonsoo Kim8456a642013-10-24 10:07:49 +09004347 list_for_each_entry(page, &n->slabs_full, lru)
4348 handle_slab(x, cachep, page);
4349 list_for_each_entry(page, &n->slabs_partial, lru)
4350 handle_slab(x, cachep, page);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004351 spin_unlock_irq(&n->list_lock);
Al Viro871751e2006-03-25 03:06:39 -08004352 }
4353 name = cachep->name;
Christoph Lameterdb845062013-02-05 18:45:23 +00004354 if (x[0] == x[1]) {
Al Viro871751e2006-03-25 03:06:39 -08004355 /* Increase the buffer size */
Christoph Lameter18004c52012-07-06 15:25:12 -05004356 mutex_unlock(&slab_mutex);
Christoph Lameterdb845062013-02-05 18:45:23 +00004357 m->private = kzalloc(x[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
Al Viro871751e2006-03-25 03:06:39 -08004358 if (!m->private) {
4359 /* Too bad, we are really out */
Christoph Lameterdb845062013-02-05 18:45:23 +00004360 m->private = x;
Christoph Lameter18004c52012-07-06 15:25:12 -05004361 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004362 return -ENOMEM;
4363 }
Christoph Lameterdb845062013-02-05 18:45:23 +00004364 *(unsigned long *)m->private = x[0] * 2;
4365 kfree(x);
Christoph Lameter18004c52012-07-06 15:25:12 -05004366 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004367 /* Now make sure this entry will be retried */
4368 m->count = m->size;
4369 return 0;
4370 }
Christoph Lameterdb845062013-02-05 18:45:23 +00004371 for (i = 0; i < x[1]; i++) {
4372 seq_printf(m, "%s: %lu ", name, x[2*i+3]);
4373 show_symbol(m, x[2*i+2]);
Al Viro871751e2006-03-25 03:06:39 -08004374 seq_putc(m, '\n');
4375 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004376
Al Viro871751e2006-03-25 03:06:39 -08004377 return 0;
4378}
4379
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004380static const struct seq_operations slabstats_op = {
Al Viro871751e2006-03-25 03:06:39 -08004381 .start = leaks_start,
Wanpeng Li276a2432013-07-08 08:08:28 +08004382 .next = slab_next,
4383 .stop = slab_stop,
Al Viro871751e2006-03-25 03:06:39 -08004384 .show = leaks_show,
4385};
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004386
4387static int slabstats_open(struct inode *inode, struct file *file)
4388{
4389 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
4390 int ret = -ENOMEM;
4391 if (n) {
4392 ret = seq_open(file, &slabstats_op);
4393 if (!ret) {
4394 struct seq_file *m = file->private_data;
4395 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
4396 m->private = n;
4397 n = NULL;
4398 }
4399 kfree(n);
4400 }
4401 return ret;
4402}
4403
4404static const struct file_operations proc_slabstats_operations = {
4405 .open = slabstats_open,
4406 .read = seq_read,
4407 .llseek = seq_lseek,
4408 .release = seq_release_private,
4409};
Al Viro871751e2006-03-25 03:06:39 -08004410#endif
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004411
4412static int __init slab_proc_init(void)
4413{
4414#ifdef CONFIG_DEBUG_SLAB_LEAK
4415 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
4416#endif
4417 return 0;
4418}
4419module_init(slab_proc_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420#endif
4421
Manfred Spraul00e145b2005-09-03 15:55:07 -07004422/**
4423 * ksize - get the actual amount of memory allocated for a given object
4424 * @objp: Pointer to the object
4425 *
4426 * kmalloc may internally round up allocations and return more memory
4427 * than requested. ksize() can be used to determine the actual amount of
4428 * memory allocated. The caller may use this additional memory, even though
4429 * a smaller amount of memory was initially specified with the kmalloc call.
4430 * The caller must guarantee that objp points to a valid object previously
4431 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4432 * must not be freed during the duration of the call.
4433 */
Pekka Enbergfd76bab2007-05-06 14:48:40 -07004434size_t ksize(const void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435{
Christoph Lameteref8b4522007-10-16 01:24:46 -07004436 BUG_ON(!objp);
4437 if (unlikely(objp == ZERO_SIZE_PTR))
Manfred Spraul00e145b2005-09-03 15:55:07 -07004438 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439
Christoph Lameter8c138bc2012-06-13 10:24:58 -05004440 return virt_to_cache(objp)->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004441}
Kirill A. Shutemovb1aabec2009-02-10 15:21:44 +02004442EXPORT_SYMBOL(ksize);