blob: 4f4e6472db5b893324174f7cddb3890fd30fd812 [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;
Robert P. J. Daybda5b652007-10-16 23:30:05 -0700194 void *entry[]; /*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800195 * Must have this definition in here for the proper
196 * alignment of array_cache. Also simplifies accessing
197 * the entries.
Mel Gorman072bb0a2012-07-31 16:43:58 -0700198 *
199 * Entries should not be directly dereferenced as
200 * entries belonging to slabs marked pfmemalloc will
201 * have the lower bits set SLAB_OBJ_PFMEMALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -0800202 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203};
204
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700205struct alien_cache {
206 spinlock_t lock;
207 struct array_cache ac;
208};
209
Mel Gorman072bb0a2012-07-31 16:43:58 -0700210#define SLAB_OBJ_PFMEMALLOC 1
211static inline bool is_obj_pfmemalloc(void *objp)
212{
213 return (unsigned long)objp & SLAB_OBJ_PFMEMALLOC;
214}
215
216static inline void set_obj_pfmemalloc(void **objp)
217{
218 *objp = (void *)((unsigned long)*objp | SLAB_OBJ_PFMEMALLOC);
219 return;
220}
221
222static inline void clear_obj_pfmemalloc(void **objp)
223{
224 *objp = (void *)((unsigned long)*objp & ~SLAB_OBJ_PFMEMALLOC);
225}
226
Andrew Mortona737b3e2006-03-22 00:08:11 -0800227/*
Christoph Lametere498be72005-09-09 13:03:32 -0700228 * Need this for bootstrapping a per node allocator.
229 */
Joonsoo Kimbf0dea22014-10-09 15:26:27 -0700230#define NUM_INIT_LISTS (2 * MAX_NUMNODES)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000231static struct kmem_cache_node __initdata init_kmem_cache_node[NUM_INIT_LISTS];
Christoph Lametere498be72005-09-09 13:03:32 -0700232#define CACHE_CACHE 0
Joonsoo Kimbf0dea22014-10-09 15:26:27 -0700233#define SIZE_NODE (MAX_NUMNODES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
Christoph Lametered11d9e2006-06-30 01:55:45 -0700235static int drain_freelist(struct kmem_cache *cache,
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000236 struct kmem_cache_node *n, int tofree);
Christoph Lametered11d9e2006-06-30 01:55:45 -0700237static void free_block(struct kmem_cache *cachep, void **objpp, int len,
Joonsoo Kim97654df2014-08-06 16:04:25 -0700238 int node, struct list_head *list);
239static void slabs_destroy(struct kmem_cache *cachep, struct list_head *list);
Pekka Enberg83b519e2009-06-10 19:40:04 +0300240static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp);
David Howells65f27f32006-11-22 14:55:48 +0000241static void cache_reap(struct work_struct *unused);
Christoph Lametered11d9e2006-06-30 01:55:45 -0700242
Ingo Molnare0a42722006-06-23 02:03:46 -0700243static int slab_early_init = 1;
244
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000245#define INDEX_NODE kmalloc_index(sizeof(struct kmem_cache_node))
Christoph Lametere498be72005-09-09 13:03:32 -0700246
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000247static void kmem_cache_node_init(struct kmem_cache_node *parent)
Christoph Lametere498be72005-09-09 13:03:32 -0700248{
249 INIT_LIST_HEAD(&parent->slabs_full);
250 INIT_LIST_HEAD(&parent->slabs_partial);
251 INIT_LIST_HEAD(&parent->slabs_free);
252 parent->shared = NULL;
253 parent->alien = NULL;
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -0800254 parent->colour_next = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700255 spin_lock_init(&parent->list_lock);
256 parent->free_objects = 0;
257 parent->free_touched = 0;
258}
259
Andrew Mortona737b3e2006-03-22 00:08:11 -0800260#define MAKE_LIST(cachep, listp, slab, nodeid) \
261 do { \
262 INIT_LIST_HEAD(listp); \
Christoph Lameter18bf8542014-08-06 16:04:11 -0700263 list_splice(&get_node(cachep, nodeid)->slab, listp); \
Christoph Lametere498be72005-09-09 13:03:32 -0700264 } while (0)
265
Andrew Mortona737b3e2006-03-22 00:08:11 -0800266#define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
267 do { \
Christoph Lametere498be72005-09-09 13:03:32 -0700268 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
269 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
270 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
271 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Joonsoo Kimb03a017b2016-03-15 14:54:50 -0700273#define CFLGS_OBJFREELIST_SLAB (0x40000000UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274#define CFLGS_OFF_SLAB (0x80000000UL)
Joonsoo Kimb03a017b2016-03-15 14:54:50 -0700275#define OBJFREELIST_SLAB(x) ((x)->flags & CFLGS_OBJFREELIST_SLAB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276#define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
277
278#define BATCHREFILL_LIMIT 16
Andrew Mortona737b3e2006-03-22 00:08:11 -0800279/*
280 * Optimization question: fewer reaps means less probability for unnessary
281 * cpucache drain/refill cycles.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 *
Adrian Bunkdc6f3f22005-11-08 16:44:08 +0100283 * OTOH the cpuarrays can contain lots of objects,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 * which could lock up otherwise freeable slabs.
285 */
Jianyu Zhan5f0985b2014-03-30 17:02:20 +0800286#define REAPTIMEOUT_AC (2*HZ)
287#define REAPTIMEOUT_NODE (4*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289#if STATS
290#define STATS_INC_ACTIVE(x) ((x)->num_active++)
291#define STATS_DEC_ACTIVE(x) ((x)->num_active--)
292#define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
293#define STATS_INC_GROWN(x) ((x)->grown++)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700294#define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
Andrew Mortona737b3e2006-03-22 00:08:11 -0800295#define STATS_SET_HIGH(x) \
296 do { \
297 if ((x)->num_active > (x)->high_mark) \
298 (x)->high_mark = (x)->num_active; \
299 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300#define STATS_INC_ERR(x) ((x)->errors++)
301#define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
Christoph Lametere498be72005-09-09 13:03:32 -0700302#define STATS_INC_NODEFREES(x) ((x)->node_frees++)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700303#define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800304#define STATS_SET_FREEABLE(x, i) \
305 do { \
306 if ((x)->max_freeable < i) \
307 (x)->max_freeable = i; \
308 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309#define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
310#define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
311#define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
312#define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
313#else
314#define STATS_INC_ACTIVE(x) do { } while (0)
315#define STATS_DEC_ACTIVE(x) do { } while (0)
316#define STATS_INC_ALLOCED(x) do { } while (0)
317#define STATS_INC_GROWN(x) do { } while (0)
Andi Kleen4e60c862010-08-09 17:19:03 -0700318#define STATS_ADD_REAPED(x,y) do { (void)(y); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319#define STATS_SET_HIGH(x) do { } while (0)
320#define STATS_INC_ERR(x) do { } while (0)
321#define STATS_INC_NODEALLOCS(x) do { } while (0)
Christoph Lametere498be72005-09-09 13:03:32 -0700322#define STATS_INC_NODEFREES(x) do { } while (0)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700323#define STATS_INC_ACOVERFLOW(x) do { } while (0)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800324#define STATS_SET_FREEABLE(x, i) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325#define STATS_INC_ALLOCHIT(x) do { } while (0)
326#define STATS_INC_ALLOCMISS(x) do { } while (0)
327#define STATS_INC_FREEHIT(x) do { } while (0)
328#define STATS_INC_FREEMISS(x) do { } while (0)
329#endif
330
331#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Andrew Mortona737b3e2006-03-22 00:08:11 -0800333/*
334 * memory layout of objects:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 * 0 : objp
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800336 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * the end of an object is aligned with the end of the real
338 * allocation. Catches writes behind the end of the allocation.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800339 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 * redzone word.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800341 * cachep->obj_offset: The real object.
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500342 * cachep->size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
343 * cachep->size - 1* BYTES_PER_WORD: last caller address
Andrew Mortona737b3e2006-03-22 00:08:11 -0800344 * [BYTES_PER_WORD long]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800346static int obj_offset(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800348 return cachep->obj_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
David Woodhouseb46b8f12007-05-08 00:22:59 -0700351static unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
David Woodhouseb46b8f12007-05-08 00:22:59 -0700354 return (unsigned long long*) (objp + obj_offset(cachep) -
355 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
David Woodhouseb46b8f12007-05-08 00:22:59 -0700358static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
360 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
361 if (cachep->flags & SLAB_STORE_USER)
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500362 return (unsigned long long *)(objp + cachep->size -
David Woodhouseb46b8f12007-05-08 00:22:59 -0700363 sizeof(unsigned long long) -
David Woodhouse87a927c2007-07-04 21:26:44 -0400364 REDZONE_ALIGN);
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500365 return (unsigned long long *) (objp + cachep->size -
David Woodhouseb46b8f12007-05-08 00:22:59 -0700366 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Pekka Enberg343e0d72006-02-01 03:05:50 -0800369static void **dbg_userword(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 BUG_ON(!(cachep->flags & SLAB_STORE_USER));
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500372 return (void **)(objp + cachep->size - BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
375#else
376
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800377#define obj_offset(x) 0
David Woodhouseb46b8f12007-05-08 00:22:59 -0700378#define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
379#define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380#define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
381
382#endif
383
Joonsoo Kim03787302014-06-23 13:22:06 -0700384#ifdef CONFIG_DEBUG_SLAB_LEAK
385
Joonsoo Kimd31676d2016-03-15 14:54:24 -0700386static inline bool is_store_user_clean(struct kmem_cache *cachep)
Joonsoo Kim03787302014-06-23 13:22:06 -0700387{
Joonsoo Kimd31676d2016-03-15 14:54:24 -0700388 return atomic_read(&cachep->store_user_clean) == 1;
389}
Joonsoo Kim03787302014-06-23 13:22:06 -0700390
Joonsoo Kimd31676d2016-03-15 14:54:24 -0700391static inline void set_store_user_clean(struct kmem_cache *cachep)
392{
393 atomic_set(&cachep->store_user_clean, 1);
394}
Joonsoo Kim03787302014-06-23 13:22:06 -0700395
Joonsoo Kimd31676d2016-03-15 14:54:24 -0700396static inline void set_store_user_dirty(struct kmem_cache *cachep)
397{
398 if (is_store_user_clean(cachep))
399 atomic_set(&cachep->store_user_clean, 0);
Joonsoo Kim03787302014-06-23 13:22:06 -0700400}
401
402#else
Joonsoo Kimd31676d2016-03-15 14:54:24 -0700403static inline void set_store_user_dirty(struct kmem_cache *cachep) {}
Joonsoo Kim03787302014-06-23 13:22:06 -0700404
405#endif
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407/*
David Rientjes3df1ccc2011-10-18 22:09:28 -0700408 * Do not go above this order unless 0 objects fit into the slab or
409 * overridden on the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 */
David Rientjes543585c2011-10-18 22:09:24 -0700411#define SLAB_MAX_ORDER_HI 1
412#define SLAB_MAX_ORDER_LO 0
413static int slab_max_order = SLAB_MAX_ORDER_LO;
David Rientjes3df1ccc2011-10-18 22:09:28 -0700414static bool slab_max_order_set __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800416static inline struct kmem_cache *virt_to_cache(const void *obj)
417{
Christoph Lameterb49af682007-05-06 14:49:41 -0700418 struct page *page = virt_to_head_page(obj);
Christoph Lameter35026082012-06-13 10:24:56 -0500419 return page->slab_cache;
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800420}
421
Joonsoo Kim8456a642013-10-24 10:07:49 +0900422static inline void *index_to_obj(struct kmem_cache *cache, struct page *page,
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800423 unsigned int idx)
424{
Joonsoo Kim8456a642013-10-24 10:07:49 +0900425 return page->s_mem + cache->size * idx;
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800426}
427
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800428/*
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500429 * We want to avoid an expensive divide : (offset / cache->size)
430 * Using the fact that size is a constant for a particular cache,
431 * we can replace (offset / cache->size) by
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800432 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
433 */
434static inline unsigned int obj_to_index(const struct kmem_cache *cache,
Joonsoo Kim8456a642013-10-24 10:07:49 +0900435 const struct page *page, void *obj)
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800436{
Joonsoo Kim8456a642013-10-24 10:07:49 +0900437 u32 offset = (obj - page->s_mem);
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800438 return reciprocal_divide(offset, cache->reciprocal_buffer_size);
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800439}
440
Joonsoo Kim6fb92432016-03-15 14:54:09 -0700441#define BOOT_CPUCACHE_ENTRIES 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442/* internal cache of cache description objs */
Christoph Lameter9b030cb2012-09-05 00:20:33 +0000443static struct kmem_cache kmem_cache_boot = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800444 .batchcount = 1,
445 .limit = BOOT_CPUCACHE_ENTRIES,
446 .shared = 1,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500447 .size = sizeof(struct kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800448 .name = "kmem_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449};
450
Joonsoo Kimedcad252014-08-08 14:19:15 -0700451#define BAD_ALIEN_MAGIC 0x01020304ul
452
Tejun Heo1871e522009-10-29 22:34:13 +0900453static DEFINE_PER_CPU(struct delayed_work, slab_reap_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Pekka Enberg343e0d72006-02-01 03:05:50 -0800455static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Joonsoo Kimbf0dea22014-10-09 15:26:27 -0700457 return this_cpu_ptr(cachep->cpu_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
Andrew Mortona737b3e2006-03-22 00:08:11 -0800460/*
461 * Calculate the number of objects and left-over bytes for a given buffer size.
462 */
Joonsoo Kim70f75062016-03-15 14:54:53 -0700463static unsigned int cache_estimate(unsigned long gfporder, size_t buffer_size,
464 unsigned long flags, size_t *left_over)
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800465{
Joonsoo Kim70f75062016-03-15 14:54:53 -0700466 unsigned int num;
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800467 size_t slab_size = PAGE_SIZE << gfporder;
468
469 /*
470 * The slab management structure can be either off the slab or
471 * on it. For the latter case, the memory allocated for a
472 * slab is used for:
473 *
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800474 * - @buffer_size bytes for each object
Joonsoo Kim2e6b3602016-03-15 14:54:30 -0700475 * - One freelist_idx_t for each object
476 *
477 * We don't need to consider alignment of freelist because
478 * freelist will be at the end of slab page. The objects will be
479 * at the correct alignment.
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800480 *
481 * If the slab management structure is off the slab, then the
482 * alignment will already be calculated into the size. Because
483 * the slabs are all pages aligned, the objects will be at the
484 * correct alignment when allocated.
485 */
Joonsoo Kimb03a017b2016-03-15 14:54:50 -0700486 if (flags & (CFLGS_OBJFREELIST_SLAB | CFLGS_OFF_SLAB)) {
Joonsoo Kim70f75062016-03-15 14:54:53 -0700487 num = slab_size / buffer_size;
Joonsoo Kim2e6b3602016-03-15 14:54:30 -0700488 *left_over = slab_size % buffer_size;
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800489 } else {
Joonsoo Kim70f75062016-03-15 14:54:53 -0700490 num = slab_size / (buffer_size + sizeof(freelist_idx_t));
Joonsoo Kim2e6b3602016-03-15 14:54:30 -0700491 *left_over = slab_size %
492 (buffer_size + sizeof(freelist_idx_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
Joonsoo Kim70f75062016-03-15 14:54:53 -0700494
495 return num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
497
Christoph Lameterf28510d2012-09-11 19:49:38 +0000498#if DEBUG
Harvey Harrisond40cee22008-04-30 00:55:07 -0700499#define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Andrew Mortona737b3e2006-03-22 00:08:11 -0800501static void __slab_error(const char *function, struct kmem_cache *cachep,
502 char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800505 function, cachep->name, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +1030507 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
Christoph Lameterf28510d2012-09-11 19:49:38 +0000509#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Paul Menage3395ee02006-12-06 20:32:16 -0800511/*
512 * By default on NUMA we use alien caches to stage the freeing of
513 * objects allocated from other nodes. This causes massive memory
514 * inefficiencies when using fake NUMA setup to split memory into a
515 * large number of small nodes, so it can be disabled on the command
516 * line
517 */
518
519static int use_alien_caches __read_mostly = 1;
520static int __init noaliencache_setup(char *s)
521{
522 use_alien_caches = 0;
523 return 1;
524}
525__setup("noaliencache", noaliencache_setup);
526
David Rientjes3df1ccc2011-10-18 22:09:28 -0700527static int __init slab_max_order_setup(char *str)
528{
529 get_option(&str, &slab_max_order);
530 slab_max_order = slab_max_order < 0 ? 0 :
531 min(slab_max_order, MAX_ORDER - 1);
532 slab_max_order_set = true;
533
534 return 1;
535}
536__setup("slab_max_order=", slab_max_order_setup);
537
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800538#ifdef CONFIG_NUMA
539/*
540 * Special reaping functions for NUMA systems called from cache_reap().
541 * These take care of doing round robin flushing of alien caches (containing
542 * objects freed on different nodes from which they were allocated) and the
543 * flushing of remote pcps by calling drain_node_pages.
544 */
Tejun Heo1871e522009-10-29 22:34:13 +0900545static DEFINE_PER_CPU(unsigned long, slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800546
547static void init_reap_node(int cpu)
548{
549 int node;
550
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -0700551 node = next_node(cpu_to_mem(cpu), node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800552 if (node == MAX_NUMNODES)
Paul Jackson442295c2006-03-22 00:09:11 -0800553 node = first_node(node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800554
Tejun Heo1871e522009-10-29 22:34:13 +0900555 per_cpu(slab_reap_node, cpu) = node;
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800556}
557
558static void next_reap_node(void)
559{
Christoph Lameter909ea962010-12-08 16:22:55 +0100560 int node = __this_cpu_read(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800561
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800562 node = next_node(node, node_online_map);
563 if (unlikely(node >= MAX_NUMNODES))
564 node = first_node(node_online_map);
Christoph Lameter909ea962010-12-08 16:22:55 +0100565 __this_cpu_write(slab_reap_node, node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800566}
567
568#else
569#define init_reap_node(cpu) do { } while (0)
570#define next_reap_node(void) do { } while (0)
571#endif
572
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573/*
574 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
575 * via the workqueue/eventd.
576 * Add the CPU number into the expiration time to minimize the possibility of
577 * the CPUs getting into lockstep and contending for the global cache chain
578 * lock.
579 */
Paul Gortmaker0db06282013-06-19 14:53:51 -0400580static void start_cpu_timer(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Tejun Heo1871e522009-10-29 22:34:13 +0900582 struct delayed_work *reap_work = &per_cpu(slab_reap_work, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 /*
585 * When this gets called from do_initcalls via cpucache_init(),
586 * init_workqueues() has already run, so keventd will be setup
587 * at that time.
588 */
David Howells52bad642006-11-22 14:54:01 +0000589 if (keventd_up() && reap_work->work.func == NULL) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800590 init_reap_node(cpu);
Tejun Heo203b42f2012-08-21 13:18:23 -0700591 INIT_DEFERRABLE_WORK(reap_work, cache_reap);
Arjan van de Ven2b284212006-12-10 02:21:28 -0800592 schedule_delayed_work_on(cpu, reap_work,
593 __round_jiffies_relative(HZ, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595}
596
Joonsoo Kim1fe00d52014-08-06 16:04:27 -0700597static void init_arraycache(struct array_cache *ac, int limit, int batch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598{
Catalin Marinasd5cff632009-06-11 13:22:40 +0100599 /*
600 * The array_cache structures contain pointers to free object.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300601 * However, when such objects are allocated or transferred to another
Catalin Marinasd5cff632009-06-11 13:22:40 +0100602 * cache the pointers are not cleared and they could be counted as
603 * valid references during a kmemleak scan. Therefore, kmemleak must
604 * not scan such objects.
605 */
Joonsoo Kim1fe00d52014-08-06 16:04:27 -0700606 kmemleak_no_scan(ac);
607 if (ac) {
608 ac->avail = 0;
609 ac->limit = limit;
610 ac->batchcount = batch;
611 ac->touched = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 }
Joonsoo Kim1fe00d52014-08-06 16:04:27 -0700613}
614
615static struct array_cache *alloc_arraycache(int node, int entries,
616 int batchcount, gfp_t gfp)
617{
Joonsoo Kim5e804782014-08-06 16:04:40 -0700618 size_t memsize = sizeof(void *) * entries + sizeof(struct array_cache);
Joonsoo Kim1fe00d52014-08-06 16:04:27 -0700619 struct array_cache *ac = NULL;
620
621 ac = kmalloc_node(memsize, gfp, node);
622 init_arraycache(ac, entries, batchcount);
623 return ac;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Joonsoo Kim8456a642013-10-24 10:07:49 +0900626static inline bool is_slab_pfmemalloc(struct page *page)
Mel Gorman072bb0a2012-07-31 16:43:58 -0700627{
Mel Gorman072bb0a2012-07-31 16:43:58 -0700628 return PageSlabPfmemalloc(page);
629}
630
631/* Clears pfmemalloc_active if no slabs have pfmalloc set */
632static void recheck_pfmemalloc_active(struct kmem_cache *cachep,
633 struct array_cache *ac)
634{
Christoph Lameter18bf8542014-08-06 16:04:11 -0700635 struct kmem_cache_node *n = get_node(cachep, numa_mem_id());
Joonsoo Kim8456a642013-10-24 10:07:49 +0900636 struct page *page;
Mel Gorman072bb0a2012-07-31 16:43:58 -0700637 unsigned long flags;
638
639 if (!pfmemalloc_active)
640 return;
641
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000642 spin_lock_irqsave(&n->list_lock, flags);
Joonsoo Kim8456a642013-10-24 10:07:49 +0900643 list_for_each_entry(page, &n->slabs_full, lru)
644 if (is_slab_pfmemalloc(page))
Mel Gorman072bb0a2012-07-31 16:43:58 -0700645 goto out;
646
Joonsoo Kim8456a642013-10-24 10:07:49 +0900647 list_for_each_entry(page, &n->slabs_partial, lru)
648 if (is_slab_pfmemalloc(page))
Mel Gorman072bb0a2012-07-31 16:43:58 -0700649 goto out;
650
Joonsoo Kim8456a642013-10-24 10:07:49 +0900651 list_for_each_entry(page, &n->slabs_free, lru)
652 if (is_slab_pfmemalloc(page))
Mel Gorman072bb0a2012-07-31 16:43:58 -0700653 goto out;
654
655 pfmemalloc_active = false;
656out:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000657 spin_unlock_irqrestore(&n->list_lock, flags);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700658}
659
Mel Gorman381760e2012-07-31 16:44:30 -0700660static void *__ac_get_obj(struct kmem_cache *cachep, struct array_cache *ac,
Mel Gorman072bb0a2012-07-31 16:43:58 -0700661 gfp_t flags, bool force_refill)
662{
663 int i;
664 void *objp = ac->entry[--ac->avail];
665
666 /* Ensure the caller is allowed to use objects from PFMEMALLOC slab */
667 if (unlikely(is_obj_pfmemalloc(objp))) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000668 struct kmem_cache_node *n;
Mel Gorman072bb0a2012-07-31 16:43:58 -0700669
670 if (gfp_pfmemalloc_allowed(flags)) {
671 clear_obj_pfmemalloc(&objp);
672 return objp;
673 }
674
675 /* The caller cannot use PFMEMALLOC objects, find another one */
Joonsoo Kimd014dc22012-09-17 14:09:06 -0700676 for (i = 0; i < ac->avail; i++) {
Mel Gorman072bb0a2012-07-31 16:43:58 -0700677 /* If a !PFMEMALLOC object is found, swap them */
678 if (!is_obj_pfmemalloc(ac->entry[i])) {
679 objp = ac->entry[i];
680 ac->entry[i] = ac->entry[ac->avail];
681 ac->entry[ac->avail] = objp;
682 return objp;
683 }
684 }
685
686 /*
687 * If there are empty slabs on the slabs_free list and we are
688 * being forced to refill the cache, mark this one !pfmemalloc.
689 */
Christoph Lameter18bf8542014-08-06 16:04:11 -0700690 n = get_node(cachep, numa_mem_id());
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000691 if (!list_empty(&n->slabs_free) && force_refill) {
Joonsoo Kim8456a642013-10-24 10:07:49 +0900692 struct page *page = virt_to_head_page(objp);
Joonsoo Kim7ecccf92013-10-24 10:07:50 +0900693 ClearPageSlabPfmemalloc(page);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700694 clear_obj_pfmemalloc(&objp);
695 recheck_pfmemalloc_active(cachep, ac);
696 return objp;
697 }
698
699 /* No !PFMEMALLOC objects available */
700 ac->avail++;
701 objp = NULL;
702 }
703
704 return objp;
705}
706
Mel Gorman381760e2012-07-31 16:44:30 -0700707static inline void *ac_get_obj(struct kmem_cache *cachep,
708 struct array_cache *ac, gfp_t flags, bool force_refill)
709{
710 void *objp;
711
712 if (unlikely(sk_memalloc_socks()))
713 objp = __ac_get_obj(cachep, ac, flags, force_refill);
714 else
715 objp = ac->entry[--ac->avail];
716
717 return objp;
718}
719
Joonsoo Kimd3aec342014-10-09 15:26:06 -0700720static noinline void *__ac_put_obj(struct kmem_cache *cachep,
721 struct array_cache *ac, void *objp)
Mel Gorman072bb0a2012-07-31 16:43:58 -0700722{
723 if (unlikely(pfmemalloc_active)) {
724 /* Some pfmemalloc slabs exist, check if this is one */
Mel Gorman30c29be2012-09-17 14:09:03 -0700725 struct page *page = virt_to_head_page(objp);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700726 if (PageSlabPfmemalloc(page))
727 set_obj_pfmemalloc(&objp);
728 }
729
Mel Gorman381760e2012-07-31 16:44:30 -0700730 return objp;
731}
732
733static inline void ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
734 void *objp)
735{
736 if (unlikely(sk_memalloc_socks()))
737 objp = __ac_put_obj(cachep, ac, objp);
738
Mel Gorman072bb0a2012-07-31 16:43:58 -0700739 ac->entry[ac->avail++] = objp;
740}
741
Christoph Lameter3ded1752006-03-25 03:06:44 -0800742/*
743 * Transfer objects in one arraycache to another.
744 * Locking must be handled by the caller.
745 *
746 * Return the number of entries transferred.
747 */
748static int transfer_objects(struct array_cache *to,
749 struct array_cache *from, unsigned int max)
750{
751 /* Figure out how many entries to transfer */
Hagen Paul Pfeifer732eacc2010-10-26 14:22:23 -0700752 int nr = min3(from->avail, max, to->limit - to->avail);
Christoph Lameter3ded1752006-03-25 03:06:44 -0800753
754 if (!nr)
755 return 0;
756
757 memcpy(to->entry + to->avail, from->entry + from->avail -nr,
758 sizeof(void *) *nr);
759
760 from->avail -= nr;
761 to->avail += nr;
Christoph Lameter3ded1752006-03-25 03:06:44 -0800762 return nr;
763}
764
Christoph Lameter765c4502006-09-27 01:50:08 -0700765#ifndef CONFIG_NUMA
766
767#define drain_alien_cache(cachep, alien) do { } while (0)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000768#define reap_alien(cachep, n) do { } while (0)
Christoph Lameter765c4502006-09-27 01:50:08 -0700769
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700770static inline struct alien_cache **alloc_alien_cache(int node,
771 int limit, gfp_t gfp)
Christoph Lameter765c4502006-09-27 01:50:08 -0700772{
Joonsoo Kimedcad252014-08-08 14:19:15 -0700773 return (struct alien_cache **)BAD_ALIEN_MAGIC;
Christoph Lameter765c4502006-09-27 01:50:08 -0700774}
775
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700776static inline void free_alien_cache(struct alien_cache **ac_ptr)
Christoph Lameter765c4502006-09-27 01:50:08 -0700777{
778}
779
780static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
781{
782 return 0;
783}
784
785static inline void *alternate_node_alloc(struct kmem_cache *cachep,
786 gfp_t flags)
787{
788 return NULL;
789}
790
Christoph Hellwig8b98c162006-12-06 20:32:30 -0800791static inline void *____cache_alloc_node(struct kmem_cache *cachep,
Christoph Lameter765c4502006-09-27 01:50:08 -0700792 gfp_t flags, int nodeid)
793{
794 return NULL;
795}
796
David Rientjes4167e9b2015-04-14 15:46:55 -0700797static inline gfp_t gfp_exact_node(gfp_t flags)
798{
799 return flags;
800}
801
Christoph Lameter765c4502006-09-27 01:50:08 -0700802#else /* CONFIG_NUMA */
803
Christoph Hellwig8b98c162006-12-06 20:32:30 -0800804static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
Paul Jacksonc61afb12006-03-24 03:16:08 -0800805static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
Christoph Lameterdc85da12006-01-18 17:42:36 -0800806
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700807static struct alien_cache *__alloc_alien_cache(int node, int entries,
808 int batch, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -0700809{
Joonsoo Kim5e804782014-08-06 16:04:40 -0700810 size_t memsize = sizeof(void *) * entries + sizeof(struct alien_cache);
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700811 struct alien_cache *alc = NULL;
812
813 alc = kmalloc_node(memsize, gfp, node);
814 init_arraycache(&alc->ac, entries, batch);
Joonsoo Kim49dfc302014-08-06 16:04:31 -0700815 spin_lock_init(&alc->lock);
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700816 return alc;
817}
818
819static struct alien_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
820{
821 struct alien_cache **alc_ptr;
Joonsoo Kim5e804782014-08-06 16:04:40 -0700822 size_t memsize = sizeof(void *) * nr_node_ids;
Christoph Lametere498be72005-09-09 13:03:32 -0700823 int i;
824
825 if (limit > 1)
826 limit = 12;
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700827 alc_ptr = kzalloc_node(memsize, gfp, node);
828 if (!alc_ptr)
829 return NULL;
830
831 for_each_node(i) {
832 if (i == node || !node_online(i))
833 continue;
834 alc_ptr[i] = __alloc_alien_cache(node, limit, 0xbaadf00d, gfp);
835 if (!alc_ptr[i]) {
836 for (i--; i >= 0; i--)
837 kfree(alc_ptr[i]);
838 kfree(alc_ptr);
839 return NULL;
Christoph Lametere498be72005-09-09 13:03:32 -0700840 }
841 }
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700842 return alc_ptr;
Christoph Lametere498be72005-09-09 13:03:32 -0700843}
844
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700845static void free_alien_cache(struct alien_cache **alc_ptr)
Christoph Lametere498be72005-09-09 13:03:32 -0700846{
847 int i;
848
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700849 if (!alc_ptr)
Christoph Lametere498be72005-09-09 13:03:32 -0700850 return;
Christoph Lametere498be72005-09-09 13:03:32 -0700851 for_each_node(i)
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700852 kfree(alc_ptr[i]);
853 kfree(alc_ptr);
Christoph Lametere498be72005-09-09 13:03:32 -0700854}
855
Pekka Enberg343e0d72006-02-01 03:05:50 -0800856static void __drain_alien_cache(struct kmem_cache *cachep,
Joonsoo Kim833b7062014-08-06 16:04:33 -0700857 struct array_cache *ac, int node,
858 struct list_head *list)
Christoph Lametere498be72005-09-09 13:03:32 -0700859{
Christoph Lameter18bf8542014-08-06 16:04:11 -0700860 struct kmem_cache_node *n = get_node(cachep, node);
Christoph Lametere498be72005-09-09 13:03:32 -0700861
862 if (ac->avail) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000863 spin_lock(&n->list_lock);
Christoph Lametere00946f2006-03-25 03:06:45 -0800864 /*
865 * Stuff objects into the remote nodes shared array first.
866 * That way we could avoid the overhead of putting the objects
867 * into the free lists and getting them back later.
868 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000869 if (n->shared)
870 transfer_objects(n->shared, ac, ac->limit);
Christoph Lametere00946f2006-03-25 03:06:45 -0800871
Joonsoo Kim833b7062014-08-06 16:04:33 -0700872 free_block(cachep, ac->entry, ac->avail, node, list);
Christoph Lametere498be72005-09-09 13:03:32 -0700873 ac->avail = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000874 spin_unlock(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -0700875 }
876}
877
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800878/*
879 * Called from cache_reap() to regularly drain alien caches round robin.
880 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000881static void reap_alien(struct kmem_cache *cachep, struct kmem_cache_node *n)
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800882{
Christoph Lameter909ea962010-12-08 16:22:55 +0100883 int node = __this_cpu_read(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800884
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000885 if (n->alien) {
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700886 struct alien_cache *alc = n->alien[node];
887 struct array_cache *ac;
Christoph Lametere00946f2006-03-25 03:06:45 -0800888
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700889 if (alc) {
890 ac = &alc->ac;
Joonsoo Kim49dfc302014-08-06 16:04:31 -0700891 if (ac->avail && spin_trylock_irq(&alc->lock)) {
Joonsoo Kim833b7062014-08-06 16:04:33 -0700892 LIST_HEAD(list);
893
894 __drain_alien_cache(cachep, ac, node, &list);
Joonsoo Kim49dfc302014-08-06 16:04:31 -0700895 spin_unlock_irq(&alc->lock);
Joonsoo Kim833b7062014-08-06 16:04:33 -0700896 slabs_destroy(cachep, &list);
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700897 }
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800898 }
899 }
900}
901
Andrew Mortona737b3e2006-03-22 00:08:11 -0800902static void drain_alien_cache(struct kmem_cache *cachep,
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700903 struct alien_cache **alien)
Christoph Lametere498be72005-09-09 13:03:32 -0700904{
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800905 int i = 0;
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700906 struct alien_cache *alc;
Christoph Lametere498be72005-09-09 13:03:32 -0700907 struct array_cache *ac;
908 unsigned long flags;
909
910 for_each_online_node(i) {
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700911 alc = alien[i];
912 if (alc) {
Joonsoo Kim833b7062014-08-06 16:04:33 -0700913 LIST_HEAD(list);
914
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700915 ac = &alc->ac;
Joonsoo Kim49dfc302014-08-06 16:04:31 -0700916 spin_lock_irqsave(&alc->lock, flags);
Joonsoo Kim833b7062014-08-06 16:04:33 -0700917 __drain_alien_cache(cachep, ac, i, &list);
Joonsoo Kim49dfc302014-08-06 16:04:31 -0700918 spin_unlock_irqrestore(&alc->lock, flags);
Joonsoo Kim833b7062014-08-06 16:04:33 -0700919 slabs_destroy(cachep, &list);
Christoph Lametere498be72005-09-09 13:03:32 -0700920 }
921 }
922}
Pekka Enberg729bd0b2006-06-23 02:03:05 -0700923
Joonsoo Kim25c4f302014-10-09 15:26:09 -0700924static int __cache_free_alien(struct kmem_cache *cachep, void *objp,
925 int node, int page_node)
Pekka Enberg729bd0b2006-06-23 02:03:05 -0700926{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000927 struct kmem_cache_node *n;
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700928 struct alien_cache *alien = NULL;
929 struct array_cache *ac;
Joonsoo Kim97654df2014-08-06 16:04:25 -0700930 LIST_HEAD(list);
Pekka Enberg1ca4cb22006-10-06 00:43:52 -0700931
Christoph Lameter18bf8542014-08-06 16:04:11 -0700932 n = get_node(cachep, node);
Pekka Enberg729bd0b2006-06-23 02:03:05 -0700933 STATS_INC_NODEFREES(cachep);
Joonsoo Kim25c4f302014-10-09 15:26:09 -0700934 if (n->alien && n->alien[page_node]) {
935 alien = n->alien[page_node];
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700936 ac = &alien->ac;
Joonsoo Kim49dfc302014-08-06 16:04:31 -0700937 spin_lock(&alien->lock);
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700938 if (unlikely(ac->avail == ac->limit)) {
Pekka Enberg729bd0b2006-06-23 02:03:05 -0700939 STATS_INC_ACOVERFLOW(cachep);
Joonsoo Kim25c4f302014-10-09 15:26:09 -0700940 __drain_alien_cache(cachep, ac, page_node, &list);
Pekka Enberg729bd0b2006-06-23 02:03:05 -0700941 }
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700942 ac_put_obj(cachep, ac, objp);
Joonsoo Kim49dfc302014-08-06 16:04:31 -0700943 spin_unlock(&alien->lock);
Joonsoo Kim833b7062014-08-06 16:04:33 -0700944 slabs_destroy(cachep, &list);
Pekka Enberg729bd0b2006-06-23 02:03:05 -0700945 } else {
Joonsoo Kim25c4f302014-10-09 15:26:09 -0700946 n = get_node(cachep, page_node);
Christoph Lameter18bf8542014-08-06 16:04:11 -0700947 spin_lock(&n->list_lock);
Joonsoo Kim25c4f302014-10-09 15:26:09 -0700948 free_block(cachep, &objp, 1, page_node, &list);
Christoph Lameter18bf8542014-08-06 16:04:11 -0700949 spin_unlock(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -0700950 slabs_destroy(cachep, &list);
Pekka Enberg729bd0b2006-06-23 02:03:05 -0700951 }
952 return 1;
953}
Joonsoo Kim25c4f302014-10-09 15:26:09 -0700954
955static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
956{
957 int page_node = page_to_nid(virt_to_page(objp));
958 int node = numa_mem_id();
959 /*
960 * Make sure we are not freeing a object from another node to the array
961 * cache on this cpu.
962 */
963 if (likely(node == page_node))
964 return 0;
965
966 return __cache_free_alien(cachep, objp, node, page_node);
967}
David Rientjes4167e9b2015-04-14 15:46:55 -0700968
969/*
Mel Gormand0164ad2015-11-06 16:28:21 -0800970 * Construct gfp mask to allocate from a specific node but do not direct reclaim
971 * or warn about failures. kswapd may still wake to reclaim in the background.
David Rientjes4167e9b2015-04-14 15:46:55 -0700972 */
973static inline gfp_t gfp_exact_node(gfp_t flags)
974{
Mel Gormand0164ad2015-11-06 16:28:21 -0800975 return (flags | __GFP_THISNODE | __GFP_NOWARN) & ~__GFP_DIRECT_RECLAIM;
David Rientjes4167e9b2015-04-14 15:46:55 -0700976}
Christoph Lametere498be72005-09-09 13:03:32 -0700977#endif
978
David Rientjes8f9f8d92010-03-27 19:40:47 -0700979/*
Christoph Lameter6a673682013-01-10 19:14:19 +0000980 * Allocates and initializes node for a node on each slab cache, used for
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000981 * either memory or cpu hotplug. If memory is being hot-added, the kmem_cache_node
David Rientjes8f9f8d92010-03-27 19:40:47 -0700982 * will be allocated off-node since memory is not yet online for the new node.
Christoph Lameter6a673682013-01-10 19:14:19 +0000983 * When hotplugging memory or a cpu, existing node are not replaced if
David Rientjes8f9f8d92010-03-27 19:40:47 -0700984 * already in use.
985 *
Christoph Lameter18004c52012-07-06 15:25:12 -0500986 * Must hold slab_mutex.
David Rientjes8f9f8d92010-03-27 19:40:47 -0700987 */
Christoph Lameter6a673682013-01-10 19:14:19 +0000988static int init_cache_node_node(int node)
David Rientjes8f9f8d92010-03-27 19:40:47 -0700989{
990 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000991 struct kmem_cache_node *n;
Joonsoo Kim5e804782014-08-06 16:04:40 -0700992 const size_t memsize = sizeof(struct kmem_cache_node);
David Rientjes8f9f8d92010-03-27 19:40:47 -0700993
Christoph Lameter18004c52012-07-06 15:25:12 -0500994 list_for_each_entry(cachep, &slab_caches, list) {
David Rientjes8f9f8d92010-03-27 19:40:47 -0700995 /*
Jianyu Zhan5f0985b2014-03-30 17:02:20 +0800996 * Set up the kmem_cache_node for cpu before we can
David Rientjes8f9f8d92010-03-27 19:40:47 -0700997 * begin anything. Make sure some other cpu on this
998 * node has not already allocated this
999 */
Christoph Lameter18bf8542014-08-06 16:04:11 -07001000 n = get_node(cachep, node);
1001 if (!n) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001002 n = kmalloc_node(memsize, GFP_KERNEL, node);
1003 if (!n)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001004 return -ENOMEM;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001005 kmem_cache_node_init(n);
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08001006 n->next_reap = jiffies + REAPTIMEOUT_NODE +
1007 ((unsigned long)cachep) % REAPTIMEOUT_NODE;
David Rientjes8f9f8d92010-03-27 19:40:47 -07001008
1009 /*
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08001010 * The kmem_cache_nodes don't come and go as CPUs
1011 * come and go. slab_mutex is sufficient
David Rientjes8f9f8d92010-03-27 19:40:47 -07001012 * protection here.
1013 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001014 cachep->node[node] = n;
David Rientjes8f9f8d92010-03-27 19:40:47 -07001015 }
1016
Christoph Lameter18bf8542014-08-06 16:04:11 -07001017 spin_lock_irq(&n->list_lock);
1018 n->free_limit =
David Rientjes8f9f8d92010-03-27 19:40:47 -07001019 (1 + nr_cpus_node(node)) *
1020 cachep->batchcount + cachep->num;
Christoph Lameter18bf8542014-08-06 16:04:11 -07001021 spin_unlock_irq(&n->list_lock);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001022 }
1023 return 0;
1024}
1025
Wanpeng Li0fa81032013-07-04 08:33:22 +08001026static inline int slabs_tofree(struct kmem_cache *cachep,
1027 struct kmem_cache_node *n)
1028{
1029 return (n->free_objects + cachep->num - 1) / cachep->num;
1030}
1031
Paul Gortmaker0db06282013-06-19 14:53:51 -04001032static void cpuup_canceled(long cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033{
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001034 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001035 struct kmem_cache_node *n = NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001036 int node = cpu_to_mem(cpu);
Rusty Russella70f7302009-03-13 14:49:46 +10301037 const struct cpumask *mask = cpumask_of_node(node);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001038
Christoph Lameter18004c52012-07-06 15:25:12 -05001039 list_for_each_entry(cachep, &slab_caches, list) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001040 struct array_cache *nc;
1041 struct array_cache *shared;
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001042 struct alien_cache **alien;
Joonsoo Kim97654df2014-08-06 16:04:25 -07001043 LIST_HEAD(list);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001044
Christoph Lameter18bf8542014-08-06 16:04:11 -07001045 n = get_node(cachep, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001046 if (!n)
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001047 continue;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001048
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001049 spin_lock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001050
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001051 /* Free limit for this kmem_cache_node */
1052 n->free_limit -= cachep->batchcount;
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001053
1054 /* cpu is dead; no one can alloc from it. */
1055 nc = per_cpu_ptr(cachep->cpu_cache, cpu);
1056 if (nc) {
Joonsoo Kim97654df2014-08-06 16:04:25 -07001057 free_block(cachep, nc->entry, nc->avail, node, &list);
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001058 nc->avail = 0;
1059 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001060
Rusty Russell58463c12009-12-17 11:43:12 -06001061 if (!cpumask_empty(mask)) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001062 spin_unlock_irq(&n->list_lock);
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001063 goto free_slab;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001064 }
1065
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001066 shared = n->shared;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001067 if (shared) {
1068 free_block(cachep, shared->entry,
Joonsoo Kim97654df2014-08-06 16:04:25 -07001069 shared->avail, node, &list);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001070 n->shared = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001071 }
1072
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001073 alien = n->alien;
1074 n->alien = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001075
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001076 spin_unlock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001077
1078 kfree(shared);
1079 if (alien) {
1080 drain_alien_cache(cachep, alien);
1081 free_alien_cache(alien);
1082 }
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001083
1084free_slab:
Joonsoo Kim97654df2014-08-06 16:04:25 -07001085 slabs_destroy(cachep, &list);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001086 }
1087 /*
1088 * In the previous loop, all the objects were freed to
1089 * the respective cache's slabs, now we can go ahead and
1090 * shrink each nodelist to its limit.
1091 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001092 list_for_each_entry(cachep, &slab_caches, list) {
Christoph Lameter18bf8542014-08-06 16:04:11 -07001093 n = get_node(cachep, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001094 if (!n)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001095 continue;
Wanpeng Li0fa81032013-07-04 08:33:22 +08001096 drain_freelist(cachep, n, slabs_tofree(cachep, n));
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001097 }
1098}
1099
Paul Gortmaker0db06282013-06-19 14:53:51 -04001100static int cpuup_prepare(long cpu)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001101{
Pekka Enberg343e0d72006-02-01 03:05:50 -08001102 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001103 struct kmem_cache_node *n = NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001104 int node = cpu_to_mem(cpu);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001105 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001107 /*
1108 * We need to do this right in the beginning since
1109 * alloc_arraycache's are going to use this list.
1110 * kmalloc_node allows us to add the slab to the right
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001111 * kmem_cache_node and not this cpu's kmem_cache_node
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001112 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001113 err = init_cache_node_node(node);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001114 if (err < 0)
1115 goto bad;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001116
1117 /*
1118 * Now we can go ahead with allocating the shared arrays and
1119 * array caches
1120 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001121 list_for_each_entry(cachep, &slab_caches, list) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001122 struct array_cache *shared = NULL;
Joonsoo Kimc8522a32014-08-06 16:04:29 -07001123 struct alien_cache **alien = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001124
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001125 if (cachep->shared) {
1126 shared = alloc_arraycache(node,
1127 cachep->shared * cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001128 0xbaadf00d, GFP_KERNEL);
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001129 if (!shared)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001130 goto bad;
1131 }
1132 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03001133 alien = alloc_alien_cache(node, cachep->limit, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001134 if (!alien) {
1135 kfree(shared);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001136 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001137 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001138 }
Christoph Lameter18bf8542014-08-06 16:04:11 -07001139 n = get_node(cachep, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001140 BUG_ON(!n);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001141
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001142 spin_lock_irq(&n->list_lock);
1143 if (!n->shared) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001144 /*
1145 * We are serialised from CPU_DEAD or
1146 * CPU_UP_CANCELLED by the cpucontrol lock
1147 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001148 n->shared = shared;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001149 shared = NULL;
1150 }
1151#ifdef CONFIG_NUMA
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001152 if (!n->alien) {
1153 n->alien = alien;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001154 alien = NULL;
1155 }
1156#endif
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001157 spin_unlock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001158 kfree(shared);
1159 free_alien_cache(alien);
1160 }
Pekka Enbergce79ddc2009-11-23 22:01:15 +02001161
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001162 return 0;
1163bad:
Akinobu Mita12d00f62007-10-18 03:05:11 -07001164 cpuup_canceled(cpu);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001165 return -ENOMEM;
1166}
1167
Paul Gortmaker0db06282013-06-19 14:53:51 -04001168static int cpuup_callback(struct notifier_block *nfb,
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001169 unsigned long action, void *hcpu)
1170{
1171 long cpu = (long)hcpu;
1172 int err = 0;
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 switch (action) {
Heiko Carstens38c3bd92007-05-09 02:34:05 -07001175 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001176 case CPU_UP_PREPARE_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001177 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001178 err = cpuup_prepare(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001179 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 break;
1181 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001182 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 start_cpu_timer(cpu);
1184 break;
1185#ifdef CONFIG_HOTPLUG_CPU
Christoph Lameter5830c592007-05-09 02:34:22 -07001186 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001187 case CPU_DOWN_PREPARE_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001188 /*
Christoph Lameter18004c52012-07-06 15:25:12 -05001189 * Shutdown cache reaper. Note that the slab_mutex is
Christoph Lameter5830c592007-05-09 02:34:22 -07001190 * held so that if cache_reap() is invoked it cannot do
1191 * anything expensive but will only modify reap_work
1192 * and reschedule the timer.
1193 */
Tejun Heoafe2c512010-12-14 16:21:17 +01001194 cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu));
Christoph Lameter5830c592007-05-09 02:34:22 -07001195 /* Now the cache_reaper is guaranteed to be not running. */
Tejun Heo1871e522009-10-29 22:34:13 +09001196 per_cpu(slab_reap_work, cpu).work.func = NULL;
Christoph Lameter5830c592007-05-09 02:34:22 -07001197 break;
1198 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001199 case CPU_DOWN_FAILED_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001200 start_cpu_timer(cpu);
1201 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001203 case CPU_DEAD_FROZEN:
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001204 /*
1205 * Even if all the cpus of a node are down, we don't free the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001206 * kmem_cache_node of any cache. This to avoid a race between
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001207 * cpu_down, and a kmalloc allocation from another cpu for
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001208 * memory from the node of the cpu going down. The node
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001209 * structure is usually allocated from kmem_cache_create() and
1210 * gets destroyed at kmem_cache_destroy().
1211 */
Simon Arlott183ff222007-10-20 01:27:18 +02001212 /* fall through */
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08001213#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001215 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001216 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001217 cpuup_canceled(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001218 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
Akinobu Mitaeac40682010-05-26 14:43:32 -07001221 return notifier_from_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222}
1223
Paul Gortmaker0db06282013-06-19 14:53:51 -04001224static struct notifier_block cpucache_notifier = {
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001225 &cpuup_callback, NULL, 0
1226};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
David Rientjes8f9f8d92010-03-27 19:40:47 -07001228#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
1229/*
1230 * Drains freelist for a node on each slab cache, used for memory hot-remove.
1231 * Returns -EBUSY if all objects cannot be drained so that the node is not
1232 * removed.
1233 *
Christoph Lameter18004c52012-07-06 15:25:12 -05001234 * Must hold slab_mutex.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001235 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001236static int __meminit drain_cache_node_node(int node)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001237{
1238 struct kmem_cache *cachep;
1239 int ret = 0;
1240
Christoph Lameter18004c52012-07-06 15:25:12 -05001241 list_for_each_entry(cachep, &slab_caches, list) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001242 struct kmem_cache_node *n;
David Rientjes8f9f8d92010-03-27 19:40:47 -07001243
Christoph Lameter18bf8542014-08-06 16:04:11 -07001244 n = get_node(cachep, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001245 if (!n)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001246 continue;
1247
Wanpeng Li0fa81032013-07-04 08:33:22 +08001248 drain_freelist(cachep, n, slabs_tofree(cachep, n));
David Rientjes8f9f8d92010-03-27 19:40:47 -07001249
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001250 if (!list_empty(&n->slabs_full) ||
1251 !list_empty(&n->slabs_partial)) {
David Rientjes8f9f8d92010-03-27 19:40:47 -07001252 ret = -EBUSY;
1253 break;
1254 }
1255 }
1256 return ret;
1257}
1258
1259static int __meminit slab_memory_callback(struct notifier_block *self,
1260 unsigned long action, void *arg)
1261{
1262 struct memory_notify *mnb = arg;
1263 int ret = 0;
1264 int nid;
1265
1266 nid = mnb->status_change_nid;
1267 if (nid < 0)
1268 goto out;
1269
1270 switch (action) {
1271 case MEM_GOING_ONLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001272 mutex_lock(&slab_mutex);
Christoph Lameter6a673682013-01-10 19:14:19 +00001273 ret = init_cache_node_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001274 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001275 break;
1276 case MEM_GOING_OFFLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001277 mutex_lock(&slab_mutex);
Christoph Lameter6a673682013-01-10 19:14:19 +00001278 ret = drain_cache_node_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001279 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001280 break;
1281 case MEM_ONLINE:
1282 case MEM_OFFLINE:
1283 case MEM_CANCEL_ONLINE:
1284 case MEM_CANCEL_OFFLINE:
1285 break;
1286 }
1287out:
Prarit Bhargava5fda1bd2011-03-22 16:30:49 -07001288 return notifier_from_errno(ret);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001289}
1290#endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
1291
Christoph Lametere498be72005-09-09 13:03:32 -07001292/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001293 * swap the static kmem_cache_node with kmalloced memory
Christoph Lametere498be72005-09-09 13:03:32 -07001294 */
Christoph Lameter6744f082013-01-10 19:12:17 +00001295static void __init init_list(struct kmem_cache *cachep, struct kmem_cache_node *list,
David Rientjes8f9f8d92010-03-27 19:40:47 -07001296 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07001297{
Christoph Lameter6744f082013-01-10 19:12:17 +00001298 struct kmem_cache_node *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001299
Christoph Lameter6744f082013-01-10 19:12:17 +00001300 ptr = kmalloc_node(sizeof(struct kmem_cache_node), GFP_NOWAIT, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07001301 BUG_ON(!ptr);
1302
Christoph Lameter6744f082013-01-10 19:12:17 +00001303 memcpy(ptr, list, sizeof(struct kmem_cache_node));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001304 /*
1305 * Do not assume that spinlocks can be initialized via memcpy:
1306 */
1307 spin_lock_init(&ptr->list_lock);
1308
Christoph Lametere498be72005-09-09 13:03:32 -07001309 MAKE_ALL_LISTS(cachep, ptr, nodeid);
Christoph Lameter6a673682013-01-10 19:14:19 +00001310 cachep->node[nodeid] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001311}
1312
Andrew Mortona737b3e2006-03-22 00:08:11 -08001313/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001314 * For setting up all the kmem_cache_node for cache whose buffer_size is same as
1315 * size of kmem_cache_node.
Pekka Enberg556a1692008-01-25 08:20:51 +02001316 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001317static void __init set_up_node(struct kmem_cache *cachep, int index)
Pekka Enberg556a1692008-01-25 08:20:51 +02001318{
1319 int node;
1320
1321 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001322 cachep->node[node] = &init_kmem_cache_node[index + node];
Christoph Lameter6a673682013-01-10 19:14:19 +00001323 cachep->node[node]->next_reap = jiffies +
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08001324 REAPTIMEOUT_NODE +
1325 ((unsigned long)cachep) % REAPTIMEOUT_NODE;
Pekka Enberg556a1692008-01-25 08:20:51 +02001326 }
1327}
1328
1329/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08001330 * Initialisation. Called after the page allocator have been initialised and
1331 * before smp_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 */
1333void __init kmem_cache_init(void)
1334{
Christoph Lametere498be72005-09-09 13:03:32 -07001335 int i;
1336
Joonsoo Kim68126702013-10-24 10:07:42 +09001337 BUILD_BUG_ON(sizeof(((struct page *)NULL)->lru) <
1338 sizeof(struct rcu_head));
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001339 kmem_cache = &kmem_cache_boot;
1340
Mel Gormanb6e68bc2009-06-16 15:32:16 -07001341 if (num_possible_nodes() == 1)
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001342 use_alien_caches = 0;
1343
Christoph Lameter3c583462012-11-28 16:23:01 +00001344 for (i = 0; i < NUM_INIT_LISTS; i++)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001345 kmem_cache_node_init(&init_kmem_cache_node[i]);
Christoph Lameter3c583462012-11-28 16:23:01 +00001346
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 /*
1348 * Fragmentation resistance on low memory - only use bigger
David Rientjes3df1ccc2011-10-18 22:09:28 -07001349 * page orders on machines with more than 32MB of memory if
1350 * not overridden on the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 */
David Rientjes3df1ccc2011-10-18 22:09:28 -07001352 if (!slab_max_order_set && totalram_pages > (32 << 20) >> PAGE_SHIFT)
David Rientjes543585c2011-10-18 22:09:24 -07001353 slab_max_order = SLAB_MAX_ORDER_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 /* Bootstrap is tricky, because several objects are allocated
1356 * from caches that do not exist yet:
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001357 * 1) initialize the kmem_cache cache: it contains the struct
1358 * kmem_cache structures of all caches, except kmem_cache itself:
1359 * kmem_cache is statically allocated.
Christoph Lametere498be72005-09-09 13:03:32 -07001360 * Initially an __init data area is used for the head array and the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001361 * kmem_cache_node structures, it's replaced with a kmalloc allocated
Christoph Lametere498be72005-09-09 13:03:32 -07001362 * array at the end of the bootstrap.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 * 2) Create the first kmalloc cache.
Pekka Enberg343e0d72006-02-01 03:05:50 -08001364 * The struct kmem_cache for the new cache is allocated normally.
Christoph Lametere498be72005-09-09 13:03:32 -07001365 * An __init data area is used for the head array.
1366 * 3) Create the remaining kmalloc caches, with minimally sized
1367 * head arrays.
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001368 * 4) Replace the __init data head arrays for kmem_cache and the first
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 * kmalloc cache with kmalloc allocated arrays.
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001370 * 5) Replace the __init data for kmem_cache_node for kmem_cache and
Christoph Lametere498be72005-09-09 13:03:32 -07001371 * the other cache's with kmalloc allocated memory.
1372 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 */
1374
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001375 /* 1) create the kmem_cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376
Eric Dumazet8da34302007-05-06 14:49:29 -07001377 /*
Eric Dumazetb56efcf2011-07-20 19:04:23 +02001378 * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
Eric Dumazet8da34302007-05-06 14:49:29 -07001379 */
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001380 create_boot_cache(kmem_cache, "kmem_cache",
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001381 offsetof(struct kmem_cache, node) +
Christoph Lameter6744f082013-01-10 19:12:17 +00001382 nr_node_ids * sizeof(struct kmem_cache_node *),
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001383 SLAB_HWCACHE_ALIGN);
1384 list_add(&kmem_cache->list, &slab_caches);
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001385 slab_state = PARTIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386
Andrew Mortona737b3e2006-03-22 00:08:11 -08001387 /*
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001388 * Initialize the caches that provide memory for the kmem_cache_node
1389 * structures first. Without this, further allocations will bug.
Christoph Lametere498be72005-09-09 13:03:32 -07001390 */
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001391 kmalloc_caches[INDEX_NODE] = create_kmalloc_cache("kmalloc-node",
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001392 kmalloc_size(INDEX_NODE), ARCH_KMALLOC_FLAGS);
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001393 slab_state = PARTIAL_NODE;
Daniel Sanders34cc6992015-06-24 16:55:57 -07001394 setup_kmalloc_cache_index_table();
Christoph Lametere498be72005-09-09 13:03:32 -07001395
Ingo Molnare0a42722006-06-23 02:03:46 -07001396 slab_early_init = 0;
1397
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001398 /* 5) Replace the bootstrap kmem_cache_node */
Christoph Lametere498be72005-09-09 13:03:32 -07001399 {
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001400 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
Mel Gorman9c09a952008-01-24 05:49:54 -08001402 for_each_online_node(nid) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001403 init_list(kmem_cache, &init_kmem_cache_node[CACHE_CACHE + nid], nid);
Pekka Enberg556a1692008-01-25 08:20:51 +02001404
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001405 init_list(kmalloc_caches[INDEX_NODE],
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001406 &init_kmem_cache_node[SIZE_NODE + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001407 }
1408 }
1409
Christoph Lameterf97d5f62013-01-10 19:12:17 +00001410 create_kmalloc_caches(ARCH_KMALLOC_FLAGS);
Pekka Enberg8429db52009-06-12 15:58:59 +03001411}
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001412
Pekka Enberg8429db52009-06-12 15:58:59 +03001413void __init kmem_cache_init_late(void)
1414{
1415 struct kmem_cache *cachep;
1416
Christoph Lameter97d06602012-07-06 15:25:11 -05001417 slab_state = UP;
Peter Zijlstra52cef182011-11-28 21:12:40 +01001418
Pekka Enberg8429db52009-06-12 15:58:59 +03001419 /* 6) resize the head arrays to their final sizes */
Christoph Lameter18004c52012-07-06 15:25:12 -05001420 mutex_lock(&slab_mutex);
1421 list_for_each_entry(cachep, &slab_caches, list)
Pekka Enberg8429db52009-06-12 15:58:59 +03001422 if (enable_cpucache(cachep, GFP_NOWAIT))
1423 BUG();
Christoph Lameter18004c52012-07-06 15:25:12 -05001424 mutex_unlock(&slab_mutex);
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001425
Christoph Lameter97d06602012-07-06 15:25:11 -05001426 /* Done! */
1427 slab_state = FULL;
1428
Andrew Mortona737b3e2006-03-22 00:08:11 -08001429 /*
1430 * Register a cpu startup notifier callback that initializes
1431 * cpu_cache_get for all new cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 */
1433 register_cpu_notifier(&cpucache_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434
David Rientjes8f9f8d92010-03-27 19:40:47 -07001435#ifdef CONFIG_NUMA
1436 /*
1437 * Register a memory hotplug callback that initializes and frees
Christoph Lameter6a673682013-01-10 19:14:19 +00001438 * node.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001439 */
1440 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
1441#endif
1442
Andrew Mortona737b3e2006-03-22 00:08:11 -08001443 /*
1444 * The reap timers are started later, with a module init call: That part
1445 * of the kernel is not yet operational.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 */
1447}
1448
1449static int __init cpucache_init(void)
1450{
1451 int cpu;
1452
Andrew Mortona737b3e2006-03-22 00:08:11 -08001453 /*
1454 * Register the timers that return unneeded pages to the page allocator
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 */
Christoph Lametere498be72005-09-09 13:03:32 -07001456 for_each_online_cpu(cpu)
Andrew Mortona737b3e2006-03-22 00:08:11 -08001457 start_cpu_timer(cpu);
Glauber Costaa164f8962012-06-21 00:59:18 +04001458
1459 /* Done! */
Christoph Lameter97d06602012-07-06 15:25:11 -05001460 slab_state = FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 return 0;
1462}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463__initcall(cpucache_init);
1464
Rafael Aquini8bdec192012-03-09 17:27:27 -03001465static noinline void
1466slab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid)
1467{
David Rientjes9a02d692014-06-04 16:06:36 -07001468#if DEBUG
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001469 struct kmem_cache_node *n;
Joonsoo Kim8456a642013-10-24 10:07:49 +09001470 struct page *page;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001471 unsigned long flags;
1472 int node;
David Rientjes9a02d692014-06-04 16:06:36 -07001473 static DEFINE_RATELIMIT_STATE(slab_oom_rs, DEFAULT_RATELIMIT_INTERVAL,
1474 DEFAULT_RATELIMIT_BURST);
1475
1476 if ((gfpflags & __GFP_NOWARN) || !__ratelimit(&slab_oom_rs))
1477 return;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001478
1479 printk(KERN_WARNING
1480 "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n",
1481 nodeid, gfpflags);
1482 printk(KERN_WARNING " cache: %s, object size: %d, order: %d\n",
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05001483 cachep->name, cachep->size, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001484
Christoph Lameter18bf8542014-08-06 16:04:11 -07001485 for_each_kmem_cache_node(cachep, node, n) {
Rafael Aquini8bdec192012-03-09 17:27:27 -03001486 unsigned long active_objs = 0, num_objs = 0, free_objects = 0;
1487 unsigned long active_slabs = 0, num_slabs = 0;
1488
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001489 spin_lock_irqsave(&n->list_lock, flags);
Joonsoo Kim8456a642013-10-24 10:07:49 +09001490 list_for_each_entry(page, &n->slabs_full, lru) {
Rafael Aquini8bdec192012-03-09 17:27:27 -03001491 active_objs += cachep->num;
1492 active_slabs++;
1493 }
Joonsoo Kim8456a642013-10-24 10:07:49 +09001494 list_for_each_entry(page, &n->slabs_partial, lru) {
1495 active_objs += page->active;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001496 active_slabs++;
1497 }
Joonsoo Kim8456a642013-10-24 10:07:49 +09001498 list_for_each_entry(page, &n->slabs_free, lru)
Rafael Aquini8bdec192012-03-09 17:27:27 -03001499 num_slabs++;
1500
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001501 free_objects += n->free_objects;
1502 spin_unlock_irqrestore(&n->list_lock, flags);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001503
1504 num_slabs += active_slabs;
1505 num_objs = num_slabs * cachep->num;
1506 printk(KERN_WARNING
1507 " node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
1508 node, active_slabs, num_slabs, active_objs, num_objs,
1509 free_objects);
1510 }
David Rientjes9a02d692014-06-04 16:06:36 -07001511#endif
Rafael Aquini8bdec192012-03-09 17:27:27 -03001512}
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514/*
Wang Sheng-Hui8a7d9b42014-08-06 16:04:46 -07001515 * Interface to system's page allocator. No need to hold the
1516 * kmem_cache_node ->list_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 *
1518 * If we requested dmaable memory, we will get it. Even if we
1519 * did not request dmaable memory, we might get it, but that
1520 * would be relatively rare and ignorable.
1521 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001522static struct page *kmem_getpages(struct kmem_cache *cachep, gfp_t flags,
1523 int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524{
1525 struct page *page;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001526 int nr_pages;
Christoph Lameter765c4502006-09-27 01:50:08 -07001527
Glauber Costaa618e892012-06-14 16:17:21 +04001528 flags |= cachep->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001529 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1530 flags |= __GFP_RECLAIMABLE;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001531
Vlastimil Babka96db8002015-09-08 15:03:50 -07001532 page = __alloc_pages_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001533 if (!page) {
David Rientjes9a02d692014-06-04 16:06:36 -07001534 slab_out_of_memory(cachep, flags, nodeid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 return NULL;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001536 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08001538 if (memcg_charge_slab(page, flags, cachep->gfporder, cachep)) {
1539 __free_pages(page, cachep->gfporder);
1540 return NULL;
1541 }
1542
Mel Gormanb37f1dd2012-07-31 16:44:03 -07001543 /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
Michal Hocko2f064f32015-08-21 14:11:51 -07001544 if (page_is_pfmemalloc(page))
Mel Gorman072bb0a2012-07-31 16:43:58 -07001545 pfmemalloc_active = true;
1546
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001547 nr_pages = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
Christoph Lameter972d1a72006-09-25 23:31:51 -07001549 add_zone_page_state(page_zone(page),
1550 NR_SLAB_RECLAIMABLE, nr_pages);
1551 else
1552 add_zone_page_state(page_zone(page),
1553 NR_SLAB_UNRECLAIMABLE, nr_pages);
Joonsoo Kima57a4982013-10-24 10:07:44 +09001554 __SetPageSlab(page);
Michal Hocko2f064f32015-08-21 14:11:51 -07001555 if (page_is_pfmemalloc(page))
Joonsoo Kima57a4982013-10-24 10:07:44 +09001556 SetPageSlabPfmemalloc(page);
Mel Gorman072bb0a2012-07-31 16:43:58 -07001557
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001558 if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
1559 kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
1560
1561 if (cachep->ctor)
1562 kmemcheck_mark_uninitialized_pages(page, nr_pages);
1563 else
1564 kmemcheck_mark_unallocated_pages(page, nr_pages);
1565 }
Pekka Enbergc175eea2008-05-09 20:35:53 +02001566
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001567 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568}
1569
1570/*
1571 * Interface to system's page release.
1572 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001573static void kmem_freepages(struct kmem_cache *cachep, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574{
Joonsoo Kima57a4982013-10-24 10:07:44 +09001575 const unsigned long nr_freed = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001577 kmemcheck_free_shadow(page, cachep->gfporder);
Pekka Enbergc175eea2008-05-09 20:35:53 +02001578
Christoph Lameter972d1a72006-09-25 23:31:51 -07001579 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1580 sub_zone_page_state(page_zone(page),
1581 NR_SLAB_RECLAIMABLE, nr_freed);
1582 else
1583 sub_zone_page_state(page_zone(page),
1584 NR_SLAB_UNRECLAIMABLE, nr_freed);
Joonsoo Kim73293c22013-10-24 10:07:37 +09001585
Joonsoo Kima57a4982013-10-24 10:07:44 +09001586 BUG_ON(!PageSlab(page));
Joonsoo Kim73293c22013-10-24 10:07:37 +09001587 __ClearPageSlabPfmemalloc(page);
Joonsoo Kima57a4982013-10-24 10:07:44 +09001588 __ClearPageSlab(page);
Joonsoo Kim8456a642013-10-24 10:07:49 +09001589 page_mapcount_reset(page);
1590 page->mapping = NULL;
Glauber Costa1f458cb2012-12-18 14:22:50 -08001591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 if (current->reclaim_state)
1593 current->reclaim_state->reclaimed_slab += nr_freed;
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08001594 __free_kmem_pages(page, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595}
1596
1597static void kmem_rcu_free(struct rcu_head *head)
1598{
Joonsoo Kim68126702013-10-24 10:07:42 +09001599 struct kmem_cache *cachep;
1600 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Joonsoo Kim68126702013-10-24 10:07:42 +09001602 page = container_of(head, struct page, rcu_head);
1603 cachep = page->slab_cache;
1604
1605 kmem_freepages(cachep, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
1608#if DEBUG
Joonsoo Kim40b44132016-03-15 14:54:21 -07001609static bool is_debug_pagealloc_cache(struct kmem_cache *cachep)
1610{
1611 if (debug_pagealloc_enabled() && OFF_SLAB(cachep) &&
1612 (cachep->size % PAGE_SIZE) == 0)
1613 return true;
1614
1615 return false;
1616}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
1618#ifdef CONFIG_DEBUG_PAGEALLOC
Pekka Enberg343e0d72006-02-01 03:05:50 -08001619static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001620 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001622 int size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001624 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001626 if (size < 5 * sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 return;
1628
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001629 *addr++ = 0x12345678;
1630 *addr++ = caller;
1631 *addr++ = smp_processor_id();
1632 size -= 3 * sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 {
1634 unsigned long *sptr = &caller;
1635 unsigned long svalue;
1636
1637 while (!kstack_end(sptr)) {
1638 svalue = *sptr++;
1639 if (kernel_text_address(svalue)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001640 *addr++ = svalue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 size -= sizeof(unsigned long);
1642 if (size <= sizeof(unsigned long))
1643 break;
1644 }
1645 }
1646
1647 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001648 *addr++ = 0x87654321;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649}
Joonsoo Kim40b44132016-03-15 14:54:21 -07001650
1651static void slab_kernel_map(struct kmem_cache *cachep, void *objp,
1652 int map, unsigned long caller)
1653{
1654 if (!is_debug_pagealloc_cache(cachep))
1655 return;
1656
1657 if (caller)
1658 store_stackinfo(cachep, objp, caller);
1659
1660 kernel_map_pages(virt_to_page(objp), cachep->size / PAGE_SIZE, map);
1661}
1662
1663#else
1664static inline void slab_kernel_map(struct kmem_cache *cachep, void *objp,
1665 int map, unsigned long caller) {}
1666
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667#endif
1668
Pekka Enberg343e0d72006-02-01 03:05:50 -08001669static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001671 int size = cachep->object_size;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001672 addr = &((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673
1674 memset(addr, val, size);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001675 *(unsigned char *)(addr + size - 1) = POISON_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
1677
1678static void dump_line(char *data, int offset, int limit)
1679{
1680 int i;
Dave Jonesaa83aa42006-09-29 01:59:51 -07001681 unsigned char error = 0;
1682 int bad_count = 0;
1683
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02001684 printk(KERN_ERR "%03x: ", offset);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001685 for (i = 0; i < limit; i++) {
1686 if (data[offset + i] != POISON_FREE) {
1687 error = data[offset + i];
1688 bad_count++;
1689 }
Dave Jonesaa83aa42006-09-29 01:59:51 -07001690 }
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02001691 print_hex_dump(KERN_CONT, "", 0, 16, 1,
1692 &data[offset], limit, 1);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001693
1694 if (bad_count == 1) {
1695 error ^= POISON_FREE;
1696 if (!(error & (error - 1))) {
1697 printk(KERN_ERR "Single bit error detected. Probably "
1698 "bad RAM.\n");
1699#ifdef CONFIG_X86
1700 printk(KERN_ERR "Run memtest86+ or a similar memory "
1701 "test tool.\n");
1702#else
1703 printk(KERN_ERR "Run a memory test tool.\n");
1704#endif
1705 }
1706 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707}
1708#endif
1709
1710#if DEBUG
1711
Pekka Enberg343e0d72006-02-01 03:05:50 -08001712static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713{
1714 int i, size;
1715 char *realobj;
1716
1717 if (cachep->flags & SLAB_RED_ZONE) {
David Woodhouseb46b8f12007-05-08 00:22:59 -07001718 printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001719 *dbg_redzone1(cachep, objp),
1720 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 }
1722
1723 if (cachep->flags & SLAB_STORE_USER) {
Joe Perches071361d2012-12-12 10:19:12 -08001724 printk(KERN_ERR "Last user: [<%p>](%pSR)\n",
1725 *dbg_userword(cachep, objp),
1726 *dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 }
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001728 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001729 size = cachep->object_size;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001730 for (i = 0; i < size && lines; i += 16, lines--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 int limit;
1732 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001733 if (i + limit > size)
1734 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 dump_line(realobj, i, limit);
1736 }
1737}
1738
Pekka Enberg343e0d72006-02-01 03:05:50 -08001739static void check_poison_obj(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
1741 char *realobj;
1742 int size, i;
1743 int lines = 0;
1744
Joonsoo Kim40b44132016-03-15 14:54:21 -07001745 if (is_debug_pagealloc_cache(cachep))
1746 return;
1747
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001748 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001749 size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001751 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 char exp = POISON_FREE;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001753 if (i == size - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 exp = POISON_END;
1755 if (realobj[i] != exp) {
1756 int limit;
1757 /* Mismatch ! */
1758 /* Print header */
1759 if (lines == 0) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001760 printk(KERN_ERR
Dave Jonesface37f2011-11-15 15:03:52 -08001761 "Slab corruption (%s): %s start=%p, len=%d\n",
1762 print_tainted(), cachep->name, realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 print_objinfo(cachep, objp, 0);
1764 }
1765 /* Hexdump the affected line */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001766 i = (i / 16) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001768 if (i + limit > size)
1769 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 dump_line(realobj, i, limit);
1771 i += 16;
1772 lines++;
1773 /* Limit to 5 lines */
1774 if (lines > 5)
1775 break;
1776 }
1777 }
1778 if (lines != 0) {
1779 /* Print some data about the neighboring objects, if they
1780 * exist:
1781 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09001782 struct page *page = virt_to_head_page(objp);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001783 unsigned int objnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Joonsoo Kim8456a642013-10-24 10:07:49 +09001785 objnr = obj_to_index(cachep, page, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if (objnr) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09001787 objp = index_to_obj(cachep, page, objnr - 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001788 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001790 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 print_objinfo(cachep, objp, 2);
1792 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001793 if (objnr + 1 < cachep->num) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09001794 objp = index_to_obj(cachep, page, objnr + 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001795 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001797 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 print_objinfo(cachep, objp, 2);
1799 }
1800 }
1801}
1802#endif
1803
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804#if DEBUG
Joonsoo Kim8456a642013-10-24 10:07:49 +09001805static void slab_destroy_debugcheck(struct kmem_cache *cachep,
1806 struct page *page)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001807{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 int i;
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07001809
1810 if (OBJFREELIST_SLAB(cachep) && cachep->flags & SLAB_POISON) {
1811 poison_obj(cachep, page->freelist - obj_offset(cachep),
1812 POISON_FREE);
1813 }
1814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 for (i = 0; i < cachep->num; i++) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09001816 void *objp = index_to_obj(cachep, page, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 check_poison_obj(cachep, objp);
Joonsoo Kim40b44132016-03-15 14:54:21 -07001820 slab_kernel_map(cachep, objp, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 }
1822 if (cachep->flags & SLAB_RED_ZONE) {
1823 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
1824 slab_error(cachep, "start of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001825 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
1827 slab_error(cachep, "end of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001828 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001831}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832#else
Joonsoo Kim8456a642013-10-24 10:07:49 +09001833static void slab_destroy_debugcheck(struct kmem_cache *cachep,
1834 struct page *page)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001835{
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001836}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837#endif
1838
Randy Dunlap911851e2006-03-22 00:08:14 -08001839/**
1840 * slab_destroy - destroy and release all objects in a slab
1841 * @cachep: cache pointer being destroyed
Masanari Iidacb8ee1a2014-01-28 02:57:08 +09001842 * @page: page pointer being destroyed
Randy Dunlap911851e2006-03-22 00:08:14 -08001843 *
Wang Sheng-Hui8a7d9b42014-08-06 16:04:46 -07001844 * Destroy all the objs in a slab page, and release the mem back to the system.
1845 * Before calling the slab page must have been unlinked from the cache. The
1846 * kmem_cache_node ->list_lock is not held/needed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001847 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09001848static void slab_destroy(struct kmem_cache *cachep, struct page *page)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001849{
Joonsoo Kim7e007352013-10-30 19:04:01 +09001850 void *freelist;
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001851
Joonsoo Kim8456a642013-10-24 10:07:49 +09001852 freelist = page->freelist;
1853 slab_destroy_debugcheck(cachep, page);
Kirill A. Shutemovbc4f6102015-11-06 16:29:44 -08001854 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU))
1855 call_rcu(&page->rcu_head, kmem_rcu_free);
1856 else
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001857 kmem_freepages(cachep, page);
Joonsoo Kim68126702013-10-24 10:07:42 +09001858
1859 /*
Joonsoo Kim8456a642013-10-24 10:07:49 +09001860 * From now on, we don't use freelist
Joonsoo Kim68126702013-10-24 10:07:42 +09001861 * although actual page can be freed in rcu context
1862 */
1863 if (OFF_SLAB(cachep))
Joonsoo Kim8456a642013-10-24 10:07:49 +09001864 kmem_cache_free(cachep->freelist_cache, freelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865}
1866
Joonsoo Kim97654df2014-08-06 16:04:25 -07001867static void slabs_destroy(struct kmem_cache *cachep, struct list_head *list)
1868{
1869 struct page *page, *n;
1870
1871 list_for_each_entry_safe(page, n, list, lru) {
1872 list_del(&page->lru);
1873 slab_destroy(cachep, page);
1874 }
1875}
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877/**
Randy.Dunlapa70773d2006-02-01 03:05:52 -08001878 * calculate_slab_order - calculate size (page order) of slabs
1879 * @cachep: pointer to the cache that is being created
1880 * @size: size of objects to be created in this cache.
Randy.Dunlapa70773d2006-02-01 03:05:52 -08001881 * @flags: slab allocation flags
1882 *
1883 * Also calculates the number of objects per slab.
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001884 *
1885 * This could be made much more intelligent. For now, try to avoid using
1886 * high order pages for slabs. When the gfp() functions are more friendly
1887 * towards high-order requests, this should be changed.
1888 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001889static size_t calculate_slab_order(struct kmem_cache *cachep,
Joonsoo Kim2e6b3602016-03-15 14:54:30 -07001890 size_t size, unsigned long flags)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001891{
1892 size_t left_over = 0;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001893 int gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001894
Christoph Lameter0aa817f2007-05-16 22:11:01 -07001895 for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001896 unsigned int num;
1897 size_t remainder;
1898
Joonsoo Kim70f75062016-03-15 14:54:53 -07001899 num = cache_estimate(gfporder, size, flags, &remainder);
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001900 if (!num)
1901 continue;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001902
Joonsoo Kimf315e3f2013-12-02 17:49:41 +09001903 /* Can't handle number of objects more than SLAB_OBJ_MAX_NUM */
1904 if (num > SLAB_OBJ_MAX_NUM)
1905 break;
1906
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02001907 if (flags & CFLGS_OFF_SLAB) {
Joonsoo Kim3217fd92016-03-15 14:54:41 -07001908 struct kmem_cache *freelist_cache;
1909 size_t freelist_size;
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02001910
Joonsoo Kim3217fd92016-03-15 14:54:41 -07001911 freelist_size = num * sizeof(freelist_idx_t);
1912 freelist_cache = kmalloc_slab(freelist_size, 0u);
1913 if (!freelist_cache)
1914 continue;
1915
1916 /*
1917 * Needed to avoid possible looping condition
1918 * in cache_grow()
1919 */
1920 if (OFF_SLAB(freelist_cache))
1921 continue;
1922
1923 /* check if off slab has enough benefit */
1924 if (freelist_cache->size > cachep->size / 2)
1925 continue;
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02001926 }
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001927
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001928 /* Found something acceptable - save it away */
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001929 cachep->num = num;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001930 cachep->gfporder = gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001931 left_over = remainder;
1932
1933 /*
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08001934 * A VFS-reclaimable slab tends to have most allocations
1935 * as GFP_NOFS and we really don't want to have to be allocating
1936 * higher-order pages when we are unable to shrink dcache.
1937 */
1938 if (flags & SLAB_RECLAIM_ACCOUNT)
1939 break;
1940
1941 /*
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001942 * Large number of objects is good, but very large slabs are
1943 * currently bad for the gfp()s.
1944 */
David Rientjes543585c2011-10-18 22:09:24 -07001945 if (gfporder >= slab_max_order)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001946 break;
1947
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001948 /*
1949 * Acceptable internal fragmentation?
1950 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001951 if (left_over * 8 <= (PAGE_SIZE << gfporder))
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001952 break;
1953 }
1954 return left_over;
1955}
1956
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001957static struct array_cache __percpu *alloc_kmem_cache_cpus(
1958 struct kmem_cache *cachep, int entries, int batchcount)
1959{
1960 int cpu;
1961 size_t size;
1962 struct array_cache __percpu *cpu_cache;
1963
1964 size = sizeof(void *) * entries + sizeof(struct array_cache);
Joonsoo Kim85c9f4b2014-10-13 15:51:01 -07001965 cpu_cache = __alloc_percpu(size, sizeof(void *));
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001966
1967 if (!cpu_cache)
1968 return NULL;
1969
1970 for_each_possible_cpu(cpu) {
1971 init_arraycache(per_cpu_ptr(cpu_cache, cpu),
1972 entries, batchcount);
1973 }
1974
1975 return cpu_cache;
1976}
1977
Pekka Enberg83b519e2009-06-10 19:40:04 +03001978static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08001979{
Christoph Lameter97d06602012-07-06 15:25:11 -05001980 if (slab_state >= FULL)
Pekka Enberg83b519e2009-06-10 19:40:04 +03001981 return enable_cpucache(cachep, gfp);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07001982
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001983 cachep->cpu_cache = alloc_kmem_cache_cpus(cachep, 1, 1);
1984 if (!cachep->cpu_cache)
1985 return 1;
1986
Christoph Lameter97d06602012-07-06 15:25:11 -05001987 if (slab_state == DOWN) {
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001988 /* Creation of first cache (kmem_cache). */
1989 set_up_node(kmem_cache, CACHE_CACHE);
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001990 } else if (slab_state == PARTIAL) {
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001991 /* For kmem_cache_node */
1992 set_up_node(cachep, SIZE_NODE);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08001993 } else {
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001994 int node;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08001995
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001996 for_each_online_node(node) {
1997 cachep->node[node] = kmalloc_node(
1998 sizeof(struct kmem_cache_node), gfp, node);
1999 BUG_ON(!cachep->node[node]);
2000 kmem_cache_node_init(cachep->node[node]);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002001 }
2002 }
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07002003
Christoph Lameter6a673682013-01-10 19:14:19 +00002004 cachep->node[numa_mem_id()]->next_reap =
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08002005 jiffies + REAPTIMEOUT_NODE +
2006 ((unsigned long)cachep) % REAPTIMEOUT_NODE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002007
2008 cpu_cache_get(cachep)->avail = 0;
2009 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
2010 cpu_cache_get(cachep)->batchcount = 1;
2011 cpu_cache_get(cachep)->touched = 0;
2012 cachep->batchcount = 1;
2013 cachep->limit = BOOT_CPUCACHE_ENTRIES;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002014 return 0;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002015}
2016
Joonsoo Kim12220de2014-10-09 15:26:24 -07002017unsigned long kmem_cache_flags(unsigned long object_size,
2018 unsigned long flags, const char *name,
2019 void (*ctor)(void *))
2020{
2021 return flags;
2022}
2023
2024struct kmem_cache *
2025__kmem_cache_alias(const char *name, size_t size, size_t align,
2026 unsigned long flags, void (*ctor)(void *))
2027{
2028 struct kmem_cache *cachep;
2029
2030 cachep = find_mergeable(size, align, flags, name, ctor);
2031 if (cachep) {
2032 cachep->refcount++;
2033
2034 /*
2035 * Adjust the object sizes so that we clear
2036 * the complete object on kzalloc.
2037 */
2038 cachep->object_size = max_t(int, cachep->object_size, size);
2039 }
2040 return cachep;
2041}
2042
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002043static bool set_objfreelist_slab_cache(struct kmem_cache *cachep,
2044 size_t size, unsigned long flags)
2045{
2046 size_t left;
2047
2048 cachep->num = 0;
2049
2050 if (cachep->ctor || flags & SLAB_DESTROY_BY_RCU)
2051 return false;
2052
2053 left = calculate_slab_order(cachep, size,
2054 flags | CFLGS_OBJFREELIST_SLAB);
2055 if (!cachep->num)
2056 return false;
2057
2058 if (cachep->num * sizeof(freelist_idx_t) > cachep->object_size)
2059 return false;
2060
2061 cachep->colour = left / cachep->colour_off;
2062
2063 return true;
2064}
2065
Joonsoo Kim158e3192016-03-15 14:54:35 -07002066static bool set_off_slab_cache(struct kmem_cache *cachep,
2067 size_t size, unsigned long flags)
2068{
2069 size_t left;
2070
2071 cachep->num = 0;
2072
2073 /*
Joonsoo Kim3217fd92016-03-15 14:54:41 -07002074 * Always use on-slab management when SLAB_NOLEAKTRACE
2075 * to avoid recursive calls into kmemleak.
Joonsoo Kim158e3192016-03-15 14:54:35 -07002076 */
Joonsoo Kim158e3192016-03-15 14:54:35 -07002077 if (flags & SLAB_NOLEAKTRACE)
2078 return false;
2079
2080 /*
2081 * Size is large, assume best to place the slab management obj
2082 * off-slab (should allow better packing of objs).
2083 */
2084 left = calculate_slab_order(cachep, size, flags | CFLGS_OFF_SLAB);
2085 if (!cachep->num)
2086 return false;
2087
2088 /*
2089 * If the slab has been placed off-slab, and we have enough space then
2090 * move it on-slab. This is at the expense of any extra colouring.
2091 */
2092 if (left >= cachep->num * sizeof(freelist_idx_t))
2093 return false;
2094
2095 cachep->colour = left / cachep->colour_off;
2096
2097 return true;
2098}
2099
2100static bool set_on_slab_cache(struct kmem_cache *cachep,
2101 size_t size, unsigned long flags)
2102{
2103 size_t left;
2104
2105 cachep->num = 0;
2106
2107 left = calculate_slab_order(cachep, size, flags);
2108 if (!cachep->num)
2109 return false;
2110
2111 cachep->colour = left / cachep->colour_off;
2112
2113 return true;
2114}
2115
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002116/**
Christoph Lameter039363f2012-07-06 15:25:10 -05002117 * __kmem_cache_create - Create a cache.
Randy Dunlapa755b762012-11-06 17:10:10 -08002118 * @cachep: cache management descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 * @flags: SLAB flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 *
2121 * Returns a ptr to the cache on success, NULL on failure.
2122 * Cannot be called within a int, but can be interrupted.
Paul Mundt20c2df82007-07-20 10:11:58 +09002123 * The @ctor is run when new pages are allocated by the cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 * The flags are
2126 *
2127 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2128 * to catch references to uninitialised memory.
2129 *
2130 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2131 * for buffer overruns.
2132 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2134 * cacheline. This can be beneficial if you're counting cycles as closely
2135 * as davem.
2136 */
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002137int
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002138__kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
David Rientjesd4a5fca52014-09-25 16:05:20 -07002140 size_t ralign = BYTES_PER_WORD;
Pekka Enberg83b519e2009-06-10 19:40:04 +03002141 gfp_t gfp;
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002142 int err;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002143 size_t size = cachep->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146#if FORCED_DEBUG
2147 /*
2148 * Enable redzoning and last user accounting, except for caches with
2149 * large objects, if the increased size would increase the object size
2150 * above the next power of two: caches with object sizes just above a
2151 * power of two have a significant amount of internal fragmentation.
2152 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002153 if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
2154 2 * sizeof(unsigned long long)))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002155 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 if (!(flags & SLAB_DESTROY_BY_RCU))
2157 flags |= SLAB_POISON;
2158#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
Andrew Mortona737b3e2006-03-22 00:08:11 -08002161 /*
2162 * Check that size is in terms of words. This is needed to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 * unaligned accesses for some archs when redzoning is used, and makes
2164 * sure any on-slab bufctl's are also correctly aligned.
2165 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002166 if (size & (BYTES_PER_WORD - 1)) {
2167 size += (BYTES_PER_WORD - 1);
2168 size &= ~(BYTES_PER_WORD - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 }
2170
David Woodhouse87a927c2007-07-04 21:26:44 -04002171 if (flags & SLAB_RED_ZONE) {
2172 ralign = REDZONE_ALIGN;
2173 /* If redzoning, ensure that the second redzone is suitably
2174 * aligned, by adjusting the object size accordingly. */
2175 size += REDZONE_ALIGN - 1;
2176 size &= ~(REDZONE_ALIGN - 1);
2177 }
Pekka Enbergca5f9702006-09-25 23:31:25 -07002178
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002179 /* 3) caller mandated alignment */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002180 if (ralign < cachep->align) {
2181 ralign = cachep->align;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 }
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002183 /* disable debug if necessary */
2184 if (ralign > __alignof__(unsigned long long))
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002185 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002186 /*
Pekka Enbergca5f9702006-09-25 23:31:25 -07002187 * 4) Store it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002189 cachep->align = ralign;
Joonsoo Kim158e3192016-03-15 14:54:35 -07002190 cachep->colour_off = cache_line_size();
2191 /* Offset must be a multiple of the alignment. */
2192 if (cachep->colour_off < cachep->align)
2193 cachep->colour_off = cachep->align;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Pekka Enberg83b519e2009-06-10 19:40:04 +03002195 if (slab_is_available())
2196 gfp = GFP_KERNEL;
2197 else
2198 gfp = GFP_NOWAIT;
2199
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Pekka Enbergca5f9702006-09-25 23:31:25 -07002202 /*
2203 * Both debugging options require word-alignment which is calculated
2204 * into align above.
2205 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (flags & SLAB_RED_ZONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 /* add space for red zone words */
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002208 cachep->obj_offset += sizeof(unsigned long long);
2209 size += 2 * sizeof(unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 }
2211 if (flags & SLAB_STORE_USER) {
Pekka Enbergca5f9702006-09-25 23:31:25 -07002212 /* user store requires one word storage behind the end of
David Woodhouse87a927c2007-07-04 21:26:44 -04002213 * the real object. But if the second red zone needs to be
2214 * aligned to 64 bits, we must allow that much space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002216 if (flags & SLAB_RED_ZONE)
2217 size += REDZONE_ALIGN;
2218 else
2219 size += BYTES_PER_WORD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
Joonsoo Kim832a15d2016-03-15 14:54:33 -07002221#endif
2222
2223 size = ALIGN(size, cachep->align);
2224 /*
2225 * We should restrict the number of objects in a slab to implement
2226 * byte sized index. Refer comment on SLAB_OBJ_MIN_SIZE definition.
2227 */
2228 if (FREELIST_BYTE_INDEX && size < SLAB_OBJ_MIN_SIZE)
2229 size = ALIGN(SLAB_OBJ_MIN_SIZE, cachep->align);
2230
2231#if DEBUG
Joonsoo Kim03a2d2a2015-10-01 15:36:54 -07002232 /*
2233 * To activate debug pagealloc, off-slab management is necessary
2234 * requirement. In early phase of initialization, small sized slab
2235 * doesn't get initialized so it would not be possible. So, we need
2236 * to check size >= 256. It guarantees that all necessary small
2237 * sized slab is initialized in current slab initialization sequence.
2238 */
Joonsoo Kim40323272016-03-15 14:54:18 -07002239 if (debug_pagealloc_enabled() && (flags & SLAB_POISON) &&
Joonsoo Kimf3a3c322016-03-15 14:54:38 -07002240 size >= 256 && cachep->object_size > cache_line_size()) {
2241 if (size < PAGE_SIZE || size % PAGE_SIZE == 0) {
2242 size_t tmp_size = ALIGN(size, PAGE_SIZE);
2243
2244 if (set_off_slab_cache(cachep, tmp_size, flags)) {
2245 flags |= CFLGS_OFF_SLAB;
2246 cachep->obj_offset += tmp_size - size;
2247 size = tmp_size;
2248 goto done;
2249 }
2250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 }
2252#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002254 if (set_objfreelist_slab_cache(cachep, size, flags)) {
2255 flags |= CFLGS_OBJFREELIST_SLAB;
2256 goto done;
2257 }
2258
Joonsoo Kim158e3192016-03-15 14:54:35 -07002259 if (set_off_slab_cache(cachep, size, flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 flags |= CFLGS_OFF_SLAB;
Joonsoo Kim158e3192016-03-15 14:54:35 -07002261 goto done;
Joonsoo Kim832a15d2016-03-15 14:54:33 -07002262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263
Joonsoo Kim158e3192016-03-15 14:54:35 -07002264 if (set_on_slab_cache(cachep, size, flags))
2265 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Joonsoo Kim158e3192016-03-15 14:54:35 -07002267 return -E2BIG;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002268
Joonsoo Kim158e3192016-03-15 14:54:35 -07002269done:
2270 cachep->freelist_size = cachep->num * sizeof(freelist_idx_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 cachep->flags = flags;
Joonsoo Kima57a4982013-10-24 10:07:44 +09002272 cachep->allocflags = __GFP_COMP;
Christoph Lameter4b51d662007-02-10 01:43:10 -08002273 if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
Glauber Costaa618e892012-06-14 16:17:21 +04002274 cachep->allocflags |= GFP_DMA;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002275 cachep->size = size;
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08002276 cachep->reciprocal_buffer_size = reciprocal_value(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Joonsoo Kim40b44132016-03-15 14:54:21 -07002278#if DEBUG
2279 /*
2280 * If we're going to use the generic kernel_map_pages()
2281 * poisoning, then it's going to smash the contents of
2282 * the redzone and userword anyhow, so switch them off.
2283 */
2284 if (IS_ENABLED(CONFIG_PAGE_POISONING) &&
2285 (cachep->flags & SLAB_POISON) &&
2286 is_debug_pagealloc_cache(cachep))
2287 cachep->flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
2288#endif
2289
2290 if (OFF_SLAB(cachep)) {
Joonsoo Kim158e3192016-03-15 14:54:35 -07002291 cachep->freelist_cache =
2292 kmalloc_slab(cachep->freelist_size, 0u);
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002295 err = setup_cpu_cache(cachep, gfp);
2296 if (err) {
Dmitry Safonov52b4b952016-02-17 13:11:37 -08002297 __kmem_cache_release(cachep);
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002298 return err;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002301 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303
2304#if DEBUG
2305static void check_irq_off(void)
2306{
2307 BUG_ON(!irqs_disabled());
2308}
2309
2310static void check_irq_on(void)
2311{
2312 BUG_ON(irqs_disabled());
2313}
2314
Pekka Enberg343e0d72006-02-01 03:05:50 -08002315static void check_spinlock_acquired(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316{
2317#ifdef CONFIG_SMP
2318 check_irq_off();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002319 assert_spin_locked(&get_node(cachep, numa_mem_id())->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320#endif
2321}
Christoph Lametere498be72005-09-09 13:03:32 -07002322
Pekka Enberg343e0d72006-02-01 03:05:50 -08002323static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07002324{
2325#ifdef CONFIG_SMP
2326 check_irq_off();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002327 assert_spin_locked(&get_node(cachep, node)->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002328#endif
2329}
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331#else
2332#define check_irq_off() do { } while(0)
2333#define check_irq_on() do { } while(0)
2334#define check_spinlock_acquired(x) do { } while(0)
Christoph Lametere498be72005-09-09 13:03:32 -07002335#define check_spinlock_acquired_node(x, y) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336#endif
2337
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002338static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
Christoph Lameteraab22072006-03-22 00:09:06 -08002339 struct array_cache *ac,
2340 int force, int node);
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342static void do_drain(void *arg)
2343{
Andrew Mortona737b3e2006-03-22 00:08:11 -08002344 struct kmem_cache *cachep = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 struct array_cache *ac;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07002346 int node = numa_mem_id();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002347 struct kmem_cache_node *n;
Joonsoo Kim97654df2014-08-06 16:04:25 -07002348 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002351 ac = cpu_cache_get(cachep);
Christoph Lameter18bf8542014-08-06 16:04:11 -07002352 n = get_node(cachep, node);
2353 spin_lock(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07002354 free_block(cachep, ac->entry, ac->avail, node, &list);
Christoph Lameter18bf8542014-08-06 16:04:11 -07002355 spin_unlock(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07002356 slabs_destroy(cachep, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 ac->avail = 0;
2358}
2359
Pekka Enberg343e0d72006-02-01 03:05:50 -08002360static void drain_cpu_caches(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002362 struct kmem_cache_node *n;
Christoph Lametere498be72005-09-09 13:03:32 -07002363 int node;
2364
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002365 on_each_cpu(do_drain, cachep, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 check_irq_on();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002367 for_each_kmem_cache_node(cachep, node, n)
2368 if (n->alien)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002369 drain_alien_cache(cachep, n->alien);
Roland Dreiera4523a82006-05-15 11:41:00 -07002370
Christoph Lameter18bf8542014-08-06 16:04:11 -07002371 for_each_kmem_cache_node(cachep, node, n)
2372 drain_array(cachep, n, n->shared, 1, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373}
2374
Christoph Lametered11d9e2006-06-30 01:55:45 -07002375/*
2376 * Remove slabs from the list of free slabs.
2377 * Specify the number of slabs to drain in tofree.
2378 *
2379 * Returns the actual number of slabs released.
2380 */
2381static int drain_freelist(struct kmem_cache *cache,
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002382 struct kmem_cache_node *n, int tofree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383{
Christoph Lametered11d9e2006-06-30 01:55:45 -07002384 struct list_head *p;
2385 int nr_freed;
Joonsoo Kim8456a642013-10-24 10:07:49 +09002386 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Christoph Lametered11d9e2006-06-30 01:55:45 -07002388 nr_freed = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002389 while (nr_freed < tofree && !list_empty(&n->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002391 spin_lock_irq(&n->list_lock);
2392 p = n->slabs_free.prev;
2393 if (p == &n->slabs_free) {
2394 spin_unlock_irq(&n->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002395 goto out;
2396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397
Joonsoo Kim8456a642013-10-24 10:07:49 +09002398 page = list_entry(p, struct page, lru);
Joonsoo Kim8456a642013-10-24 10:07:49 +09002399 list_del(&page->lru);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002400 /*
2401 * Safe to drop the lock. The slab is no longer linked
2402 * to the cache.
2403 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002404 n->free_objects -= cache->num;
2405 spin_unlock_irq(&n->list_lock);
Joonsoo Kim8456a642013-10-24 10:07:49 +09002406 slab_destroy(cache, page);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002407 nr_freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 }
Christoph Lametered11d9e2006-06-30 01:55:45 -07002409out:
2410 return nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411}
2412
Vladimir Davydovd6e0b7f2015-02-12 14:59:47 -08002413int __kmem_cache_shrink(struct kmem_cache *cachep, bool deactivate)
Christoph Lametere498be72005-09-09 13:03:32 -07002414{
Christoph Lameter18bf8542014-08-06 16:04:11 -07002415 int ret = 0;
2416 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002417 struct kmem_cache_node *n;
Christoph Lametere498be72005-09-09 13:03:32 -07002418
2419 drain_cpu_caches(cachep);
2420
2421 check_irq_on();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002422 for_each_kmem_cache_node(cachep, node, n) {
Wanpeng Li0fa81032013-07-04 08:33:22 +08002423 drain_freelist(cachep, n, slabs_tofree(cachep, n));
Christoph Lametered11d9e2006-06-30 01:55:45 -07002424
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002425 ret += !list_empty(&n->slabs_full) ||
2426 !list_empty(&n->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07002427 }
2428 return (ret ? 1 : 0);
2429}
2430
Christoph Lameter945cf2b2012-09-04 23:18:33 +00002431int __kmem_cache_shutdown(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
Dmitry Safonov52b4b952016-02-17 13:11:37 -08002433 return __kmem_cache_shrink(cachep, false);
2434}
2435
2436void __kmem_cache_release(struct kmem_cache *cachep)
2437{
Christoph Lameter12c36672012-09-04 23:38:33 +00002438 int i;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002439 struct kmem_cache_node *n;
Christoph Lameter12c36672012-09-04 23:38:33 +00002440
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07002441 free_percpu(cachep->cpu_cache);
Christoph Lameter12c36672012-09-04 23:38:33 +00002442
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002443 /* NUMA: free the node structures */
Christoph Lameter18bf8542014-08-06 16:04:11 -07002444 for_each_kmem_cache_node(cachep, i, n) {
2445 kfree(n->shared);
2446 free_alien_cache(n->alien);
2447 kfree(n);
2448 cachep->node[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002452/*
2453 * Get the memory for a slab management obj.
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08002454 *
2455 * For a slab cache when the slab descriptor is off-slab, the
2456 * slab descriptor can't come from the same cache which is being created,
2457 * Because if it is the case, that means we defer the creation of
2458 * the kmalloc_{dma,}_cache of size sizeof(slab descriptor) to this point.
2459 * And we eventually call down to __kmem_cache_create(), which
2460 * in turn looks up in the kmalloc_{dma,}_caches for the disired-size one.
2461 * This is a "chicken-and-egg" problem.
2462 *
2463 * So the off-slab slab descriptor shall come from the kmalloc_{dma,}_caches,
2464 * which are all initialized during kmem_cache_init().
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002465 */
Joonsoo Kim7e007352013-10-30 19:04:01 +09002466static void *alloc_slabmgmt(struct kmem_cache *cachep,
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002467 struct page *page, int colour_off,
2468 gfp_t local_flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469{
Joonsoo Kim7e007352013-10-30 19:04:01 +09002470 void *freelist;
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002471 void *addr = page_address(page);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002472
Joonsoo Kim2e6b3602016-03-15 14:54:30 -07002473 page->s_mem = addr + colour_off;
2474 page->active = 0;
2475
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002476 if (OBJFREELIST_SLAB(cachep))
2477 freelist = NULL;
2478 else if (OFF_SLAB(cachep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 /* Slab management obj is off-slab. */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002480 freelist = kmem_cache_alloc_node(cachep->freelist_cache,
Pekka Enberg8759ec52008-11-26 10:01:31 +02002481 local_flags, nodeid);
Joonsoo Kim8456a642013-10-24 10:07:49 +09002482 if (!freelist)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483 return NULL;
2484 } else {
Joonsoo Kim2e6b3602016-03-15 14:54:30 -07002485 /* We will use last bytes at the slab for freelist */
2486 freelist = addr + (PAGE_SIZE << cachep->gfporder) -
2487 cachep->freelist_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
Joonsoo Kim2e6b3602016-03-15 14:54:30 -07002489
Joonsoo Kim8456a642013-10-24 10:07:49 +09002490 return freelist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491}
2492
Joonsoo Kim7cc68972014-04-18 16:24:09 +09002493static inline freelist_idx_t get_free_obj(struct page *page, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494{
Joonsoo Kima41adfa2013-12-02 17:49:42 +09002495 return ((freelist_idx_t *)page->freelist)[idx];
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002496}
2497
2498static inline void set_free_obj(struct page *page,
Joonsoo Kim7cc68972014-04-18 16:24:09 +09002499 unsigned int idx, freelist_idx_t val)
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002500{
Joonsoo Kima41adfa2013-12-02 17:49:42 +09002501 ((freelist_idx_t *)(page->freelist))[idx] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502}
2503
Joonsoo Kim10b2e9e2016-03-15 14:54:47 -07002504static void cache_init_objs_debug(struct kmem_cache *cachep, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
Joonsoo Kim10b2e9e2016-03-15 14:54:47 -07002506#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 int i;
2508
2509 for (i = 0; i < cachep->num; i++) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09002510 void *objp = index_to_obj(cachep, page, i);
Joonsoo Kim10b2e9e2016-03-15 14:54:47 -07002511
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 if (cachep->flags & SLAB_STORE_USER)
2513 *dbg_userword(cachep, objp) = NULL;
2514
2515 if (cachep->flags & SLAB_RED_ZONE) {
2516 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2517 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2518 }
2519 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002520 * Constructors are not allowed to allocate memory from the same
2521 * cache which they are a constructor for. Otherwise, deadlock.
2522 * They must also be threaded.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 */
2524 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002525 cachep->ctor(objp + obj_offset(cachep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526
2527 if (cachep->flags & SLAB_RED_ZONE) {
2528 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2529 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002530 " end of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2532 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002533 " start of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
Joonsoo Kim40b44132016-03-15 14:54:21 -07002535 /* need to poison the objs? */
2536 if (cachep->flags & SLAB_POISON) {
2537 poison_obj(cachep, objp, POISON_FREE);
2538 slab_kernel_map(cachep, objp, 0, 0);
2539 }
Joonsoo Kim10b2e9e2016-03-15 14:54:47 -07002540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541#endif
Joonsoo Kim10b2e9e2016-03-15 14:54:47 -07002542}
2543
2544static void cache_init_objs(struct kmem_cache *cachep,
2545 struct page *page)
2546{
2547 int i;
2548
2549 cache_init_objs_debug(cachep, page);
2550
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002551 if (OBJFREELIST_SLAB(cachep)) {
2552 page->freelist = index_to_obj(cachep, page, cachep->num - 1) +
2553 obj_offset(cachep);
2554 }
2555
Joonsoo Kim10b2e9e2016-03-15 14:54:47 -07002556 for (i = 0; i < cachep->num; i++) {
2557 /* constructor could break poison info */
2558 if (DEBUG == 0 && cachep->ctor)
2559 cachep->ctor(index_to_obj(cachep, page, i));
2560
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002561 set_free_obj(page, i, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563}
2564
Pekka Enberg343e0d72006-02-01 03:05:50 -08002565static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
Christoph Lameter4b51d662007-02-10 01:43:10 -08002567 if (CONFIG_ZONE_DMA_FLAG) {
2568 if (flags & GFP_DMA)
Glauber Costaa618e892012-06-14 16:17:21 +04002569 BUG_ON(!(cachep->allocflags & GFP_DMA));
Christoph Lameter4b51d662007-02-10 01:43:10 -08002570 else
Glauber Costaa618e892012-06-14 16:17:21 +04002571 BUG_ON(cachep->allocflags & GFP_DMA);
Christoph Lameter4b51d662007-02-10 01:43:10 -08002572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573}
2574
Joonsoo Kim260b61d2016-03-15 14:54:12 -07002575static void *slab_get_obj(struct kmem_cache *cachep, struct page *page)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002576{
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002577 void *objp;
Matthew Dobson78d382d2006-02-01 03:05:47 -08002578
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002579 objp = index_to_obj(cachep, page, get_free_obj(page, page->active));
Joonsoo Kim8456a642013-10-24 10:07:49 +09002580 page->active++;
Matthew Dobson78d382d2006-02-01 03:05:47 -08002581
Joonsoo Kimd31676d2016-03-15 14:54:24 -07002582#if DEBUG
2583 if (cachep->flags & SLAB_STORE_USER)
2584 set_store_user_dirty(cachep);
2585#endif
2586
Matthew Dobson78d382d2006-02-01 03:05:47 -08002587 return objp;
2588}
2589
Joonsoo Kim260b61d2016-03-15 14:54:12 -07002590static void slab_put_obj(struct kmem_cache *cachep,
2591 struct page *page, void *objp)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002592{
Joonsoo Kim8456a642013-10-24 10:07:49 +09002593 unsigned int objnr = obj_to_index(cachep, page, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002594#if DEBUG
Joonsoo Kim16025172013-10-24 10:07:46 +09002595 unsigned int i;
Matthew Dobson78d382d2006-02-01 03:05:47 -08002596
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002597 /* Verify double free bug */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002598 for (i = page->active; i < cachep->num; i++) {
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002599 if (get_free_obj(page, i) == objnr) {
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002600 printk(KERN_ERR "slab: double free detected in cache "
2601 "'%s', objp %p\n", cachep->name, objp);
2602 BUG();
2603 }
Matthew Dobson78d382d2006-02-01 03:05:47 -08002604 }
2605#endif
Joonsoo Kim8456a642013-10-24 10:07:49 +09002606 page->active--;
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002607 if (!page->freelist)
2608 page->freelist = objp + obj_offset(cachep);
2609
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002610 set_free_obj(page, page->active, objnr);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002611}
2612
Pekka Enberg47768742006-06-23 02:03:07 -07002613/*
2614 * Map pages beginning at addr to the given cache and slab. This is required
2615 * for the slab allocator to be able to lookup the cache and slab of a
Nick Pigginccd35fb2011-01-07 17:49:17 +11002616 * virtual address for kfree, ksize, and slab debugging.
Pekka Enberg47768742006-06-23 02:03:07 -07002617 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002618static void slab_map_pages(struct kmem_cache *cache, struct page *page,
Joonsoo Kim7e007352013-10-30 19:04:01 +09002619 void *freelist)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620{
Joonsoo Kima57a4982013-10-24 10:07:44 +09002621 page->slab_cache = cache;
Joonsoo Kim8456a642013-10-24 10:07:49 +09002622 page->freelist = freelist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623}
2624
2625/*
2626 * Grow (by 1) the number of slabs within a cache. This is called by
2627 * kmem_cache_alloc() when there are no active objs left in a cache.
2628 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002629static int cache_grow(struct kmem_cache *cachep,
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002630 gfp_t flags, int nodeid, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
Joonsoo Kim7e007352013-10-30 19:04:01 +09002632 void *freelist;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002633 size_t offset;
2634 gfp_t local_flags;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002635 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Andrew Mortona737b3e2006-03-22 00:08:11 -08002637 /*
2638 * Be lazy and only check for valid flags here, keeping it out of the
2639 * critical path in kmem_cache_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 */
Andrew Mortonc871ac42014-12-10 15:42:25 -08002641 if (unlikely(flags & GFP_SLAB_BUG_MASK)) {
2642 pr_emerg("gfp: %u\n", flags & GFP_SLAB_BUG_MASK);
2643 BUG();
2644 }
Christoph Lameter6cb06222007-10-16 01:25:41 -07002645 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002647 /* Take the node list lock to change the colour_next on this node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 check_irq_off();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002649 n = get_node(cachep, nodeid);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002650 spin_lock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651
2652 /* Get colour for the slab, and cal the next value. */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002653 offset = n->colour_next;
2654 n->colour_next++;
2655 if (n->colour_next >= cachep->colour)
2656 n->colour_next = 0;
2657 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002659 offset *= cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660
Mel Gormand0164ad2015-11-06 16:28:21 -08002661 if (gfpflags_allow_blocking(local_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 local_irq_enable();
2663
2664 /*
2665 * The test for missing atomic flag is performed here, rather than
2666 * the more obvious place, simply to reduce the critical path length
2667 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2668 * will eventually be caught here (where it matters).
2669 */
2670 kmem_flagcheck(cachep, flags);
2671
Andrew Mortona737b3e2006-03-22 00:08:11 -08002672 /*
2673 * Get mem for the objs. Attempt to allocate a physical page from
2674 * 'nodeid'.
Christoph Lametere498be72005-09-09 13:03:32 -07002675 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002676 if (!page)
2677 page = kmem_getpages(cachep, local_flags, nodeid);
2678 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 goto failed;
2680
2681 /* Get slab management. */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002682 freelist = alloc_slabmgmt(cachep, page, offset,
Christoph Lameter6cb06222007-10-16 01:25:41 -07002683 local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002684 if (OFF_SLAB(cachep) && !freelist)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 goto opps1;
2686
Joonsoo Kim8456a642013-10-24 10:07:49 +09002687 slab_map_pages(cachep, page, freelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002688
Joonsoo Kim8456a642013-10-24 10:07:49 +09002689 cache_init_objs(cachep, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Mel Gormand0164ad2015-11-06 16:28:21 -08002691 if (gfpflags_allow_blocking(local_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 local_irq_disable();
2693 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002694 spin_lock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
2696 /* Make slab active. */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002697 list_add_tail(&page->lru, &(n->slabs_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 STATS_INC_GROWN(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002699 n->free_objects += cachep->num;
2700 spin_unlock(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002702opps1:
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002703 kmem_freepages(cachep, page);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002704failed:
Mel Gormand0164ad2015-11-06 16:28:21 -08002705 if (gfpflags_allow_blocking(local_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 local_irq_disable();
2707 return 0;
2708}
2709
2710#if DEBUG
2711
2712/*
2713 * Perform extra freeing checks:
2714 * - detect bad pointers.
2715 * - POISON/RED_ZONE checking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 */
2717static void kfree_debugcheck(const void *objp)
2718{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 if (!virt_addr_valid(objp)) {
2720 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002721 (unsigned long)objp);
2722 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724}
2725
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002726static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2727{
David Woodhouseb46b8f12007-05-08 00:22:59 -07002728 unsigned long long redzone1, redzone2;
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002729
2730 redzone1 = *dbg_redzone1(cache, obj);
2731 redzone2 = *dbg_redzone2(cache, obj);
2732
2733 /*
2734 * Redzone is ok.
2735 */
2736 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2737 return;
2738
2739 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2740 slab_error(cache, "double free detected");
2741 else
2742 slab_error(cache, "memory outside object was overwritten");
2743
David Woodhouseb46b8f12007-05-08 00:22:59 -07002744 printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002745 obj, redzone1, redzone2);
2746}
2747
Pekka Enberg343e0d72006-02-01 03:05:50 -08002748static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002749 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 unsigned int objnr;
Joonsoo Kim8456a642013-10-24 10:07:49 +09002752 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Matthew Wilcox80cbd912007-11-29 12:05:13 -07002754 BUG_ON(virt_to_cache(objp) != cachep);
2755
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002756 objp -= obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 kfree_debugcheck(objp);
Christoph Lameterb49af682007-05-06 14:49:41 -07002758 page = virt_to_head_page(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 if (cachep->flags & SLAB_RED_ZONE) {
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002761 verify_redzone_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2763 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2764 }
Joonsoo Kimd31676d2016-03-15 14:54:24 -07002765 if (cachep->flags & SLAB_STORE_USER) {
2766 set_store_user_dirty(cachep);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002767 *dbg_userword(cachep, objp) = (void *)caller;
Joonsoo Kimd31676d2016-03-15 14:54:24 -07002768 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769
Joonsoo Kim8456a642013-10-24 10:07:49 +09002770 objnr = obj_to_index(cachep, page, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
2772 BUG_ON(objnr >= cachep->num);
Joonsoo Kim8456a642013-10-24 10:07:49 +09002773 BUG_ON(objp != index_to_obj(cachep, page, objnr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 poison_obj(cachep, objp, POISON_FREE);
Joonsoo Kim40b44132016-03-15 14:54:21 -07002777 slab_kernel_map(cachep, objp, 0, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 }
2779 return objp;
2780}
2781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782#else
2783#define kfree_debugcheck(x) do { } while(0)
2784#define cache_free_debugcheck(x,objp,z) (objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785#endif
2786
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002787static inline void fixup_objfreelist_debug(struct kmem_cache *cachep,
2788 void **list)
2789{
2790#if DEBUG
2791 void *next = *list;
2792 void *objp;
2793
2794 while (next) {
2795 objp = next - obj_offset(cachep);
2796 next = *(void **)next;
2797 poison_obj(cachep, objp, POISON_FREE);
2798 }
2799#endif
2800}
2801
Joonsoo Kimd8410232016-03-15 14:54:44 -07002802static inline void fixup_slab_list(struct kmem_cache *cachep,
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002803 struct kmem_cache_node *n, struct page *page,
2804 void **list)
Joonsoo Kimd8410232016-03-15 14:54:44 -07002805{
2806 /* move slabp to correct slabp list: */
2807 list_del(&page->lru);
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002808 if (page->active == cachep->num) {
Joonsoo Kimd8410232016-03-15 14:54:44 -07002809 list_add(&page->lru, &n->slabs_full);
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002810 if (OBJFREELIST_SLAB(cachep)) {
2811#if DEBUG
2812 /* Poisoning will be done without holding the lock */
2813 if (cachep->flags & SLAB_POISON) {
2814 void **objp = page->freelist;
2815
2816 *objp = *list;
2817 *list = objp;
2818 }
2819#endif
2820 page->freelist = NULL;
2821 }
2822 } else
Joonsoo Kimd8410232016-03-15 14:54:44 -07002823 list_add(&page->lru, &n->slabs_partial);
2824}
2825
Geliang Tang7aa0d222016-01-14 15:18:02 -08002826static struct page *get_first_slab(struct kmem_cache_node *n)
2827{
2828 struct page *page;
2829
2830 page = list_first_entry_or_null(&n->slabs_partial,
2831 struct page, lru);
2832 if (!page) {
2833 n->free_touched = 1;
2834 page = list_first_entry_or_null(&n->slabs_free,
2835 struct page, lru);
2836 }
2837
2838 return page;
2839}
2840
Mel Gorman072bb0a2012-07-31 16:43:58 -07002841static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags,
2842 bool force_refill)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
2844 int batchcount;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002845 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 struct array_cache *ac;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002847 int node;
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002848 void *list = NULL;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002849
Joe Korty6d2144d2008-03-05 15:04:59 -08002850 check_irq_off();
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07002851 node = numa_mem_id();
Mel Gorman072bb0a2012-07-31 16:43:58 -07002852 if (unlikely(force_refill))
2853 goto force_grow;
2854retry:
Joe Korty6d2144d2008-03-05 15:04:59 -08002855 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 batchcount = ac->batchcount;
2857 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002858 /*
2859 * If there was little recent activity on this cache, then
2860 * perform only a partial refill. Otherwise we could generate
2861 * refill bouncing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 */
2863 batchcount = BATCHREFILL_LIMIT;
2864 }
Christoph Lameter18bf8542014-08-06 16:04:11 -07002865 n = get_node(cachep, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002867 BUG_ON(ac->avail > 0 || !n);
2868 spin_lock(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002869
Christoph Lameter3ded1752006-03-25 03:06:44 -08002870 /* See if we can refill from the shared array */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002871 if (n->shared && transfer_objects(ac, n->shared, batchcount)) {
2872 n->shared->touched = 1;
Christoph Lameter3ded1752006-03-25 03:06:44 -08002873 goto alloc_done;
Nick Piggin44b57f12010-01-27 22:27:40 +11002874 }
Christoph Lameter3ded1752006-03-25 03:06:44 -08002875
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 while (batchcount > 0) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09002877 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 /* Get slab alloc is to come from. */
Geliang Tang7aa0d222016-01-14 15:18:02 -08002879 page = get_first_slab(n);
2880 if (!page)
2881 goto must_grow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 check_spinlock_acquired(cachep);
Pekka Enberg714b81712007-05-06 14:49:03 -07002884
2885 /*
2886 * The slab was either on partial or free list so
2887 * there must be at least one object available for
2888 * allocation.
2889 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002890 BUG_ON(page->active >= cachep->num);
Pekka Enberg714b81712007-05-06 14:49:03 -07002891
Joonsoo Kim8456a642013-10-24 10:07:49 +09002892 while (page->active < cachep->num && batchcount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 STATS_INC_ALLOCED(cachep);
2894 STATS_INC_ACTIVE(cachep);
2895 STATS_SET_HIGH(cachep);
2896
Joonsoo Kim260b61d2016-03-15 14:54:12 -07002897 ac_put_obj(cachep, ac, slab_get_obj(cachep, page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002900 fixup_slab_list(cachep, n, page, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 }
2902
Andrew Mortona737b3e2006-03-22 00:08:11 -08002903must_grow:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002904 n->free_objects -= ac->avail;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002905alloc_done:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002906 spin_unlock(&n->list_lock);
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002907 fixup_objfreelist_debug(cachep, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908
2909 if (unlikely(!ac->avail)) {
2910 int x;
Mel Gorman072bb0a2012-07-31 16:43:58 -07002911force_grow:
David Rientjes4167e9b2015-04-14 15:46:55 -07002912 x = cache_grow(cachep, gfp_exact_node(flags), node, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07002913
Andrew Mortona737b3e2006-03-22 00:08:11 -08002914 /* cache_grow can reenable interrupts, then ac could change. */
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002915 ac = cpu_cache_get(cachep);
David Rientjes51cd8e62012-08-28 19:57:21 -07002916 node = numa_mem_id();
Mel Gorman072bb0a2012-07-31 16:43:58 -07002917
2918 /* no objects in sight? abort */
2919 if (!x && (ac->avail == 0 || force_refill))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 return NULL;
2921
Andrew Mortona737b3e2006-03-22 00:08:11 -08002922 if (!ac->avail) /* objects refilled by interrupt? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 goto retry;
2924 }
2925 ac->touched = 1;
Mel Gorman072bb0a2012-07-31 16:43:58 -07002926
2927 return ac_get_obj(cachep, ac, flags, force_refill);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928}
2929
Andrew Mortona737b3e2006-03-22 00:08:11 -08002930static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
2931 gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932{
Mel Gormand0164ad2015-11-06 16:28:21 -08002933 might_sleep_if(gfpflags_allow_blocking(flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934#if DEBUG
2935 kmem_flagcheck(cachep, flags);
2936#endif
2937}
2938
2939#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08002940static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002941 gfp_t flags, void *objp, unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002943 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 return objp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002945 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 check_poison_obj(cachep, objp);
Joonsoo Kim40b44132016-03-15 14:54:21 -07002947 slab_kernel_map(cachep, objp, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 poison_obj(cachep, objp, POISON_INUSE);
2949 }
2950 if (cachep->flags & SLAB_STORE_USER)
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002951 *dbg_userword(cachep, objp) = (void *)caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
2953 if (cachep->flags & SLAB_RED_ZONE) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002954 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
2955 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
2956 slab_error(cachep, "double free, or memory outside"
2957 " object was overwritten");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002958 printk(KERN_ERR
David Woodhouseb46b8f12007-05-08 00:22:59 -07002959 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08002960 objp, *dbg_redzone1(cachep, objp),
2961 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 }
2963 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
2964 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
2965 }
Joonsoo Kim03787302014-06-23 13:22:06 -07002966
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002967 objp += obj_offset(cachep);
Christoph Lameter4f104932007-05-06 14:50:17 -07002968 if (cachep->ctor && cachep->flags & SLAB_POISON)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002969 cachep->ctor(objp);
Tetsuo Handa7ea466f2011-07-21 09:42:45 +09002970 if (ARCH_SLAB_MINALIGN &&
2971 ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002972 printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
Hugh Dickinsc2251502011-07-11 13:35:08 -07002973 objp, (int)ARCH_SLAB_MINALIGN);
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 return objp;
2976}
2977#else
2978#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
2979#endif
2980
Pekka Enberg343e0d72006-02-01 03:05:50 -08002981static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002983 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 struct array_cache *ac;
Mel Gorman072bb0a2012-07-31 16:43:58 -07002985 bool force_refill = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
Alok N Kataria5c382302005-09-27 21:45:46 -07002987 check_irq_off();
Akinobu Mita8a8b6502006-12-08 02:39:44 -08002988
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002989 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 if (likely(ac->avail)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 ac->touched = 1;
Mel Gorman072bb0a2012-07-31 16:43:58 -07002992 objp = ac_get_obj(cachep, ac, flags, false);
2993
J. R. Okajimaddbf2e82009-12-02 16:55:50 +09002994 /*
Mel Gorman072bb0a2012-07-31 16:43:58 -07002995 * Allow for the possibility all avail objects are not allowed
2996 * by the current flags
J. R. Okajimaddbf2e82009-12-02 16:55:50 +09002997 */
Mel Gorman072bb0a2012-07-31 16:43:58 -07002998 if (objp) {
2999 STATS_INC_ALLOCHIT(cachep);
3000 goto out;
3001 }
3002 force_refill = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 }
Mel Gorman072bb0a2012-07-31 16:43:58 -07003004
3005 STATS_INC_ALLOCMISS(cachep);
3006 objp = cache_alloc_refill(cachep, flags, force_refill);
3007 /*
3008 * the 'ac' may be updated by cache_alloc_refill(),
3009 * and kmemleak_erase() requires its correct value.
3010 */
3011 ac = cpu_cache_get(cachep);
3012
3013out:
Catalin Marinasd5cff632009-06-11 13:22:40 +01003014 /*
3015 * To avoid a false negative, if an object that is in one of the
3016 * per-CPU caches is leaked, we need to make sure kmemleak doesn't
3017 * treat the array pointers as a reference to the object.
3018 */
J. R. Okajimaf3d8b532009-12-02 16:55:49 +09003019 if (objp)
3020 kmemleak_erase(&ac->entry[ac->avail]);
Alok N Kataria5c382302005-09-27 21:45:46 -07003021 return objp;
3022}
3023
Christoph Lametere498be72005-09-09 13:03:32 -07003024#ifdef CONFIG_NUMA
3025/*
Zefan Li2ad654b2014-09-25 09:41:02 +08003026 * Try allocating on another node if PFA_SPREAD_SLAB is a mempolicy is set.
Paul Jacksonc61afb12006-03-24 03:16:08 -08003027 *
3028 * If we are in_interrupt, then process context, including cpusets and
3029 * mempolicy, may not apply and should not be used for allocation policy.
3030 */
3031static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
3032{
3033 int nid_alloc, nid_here;
3034
Christoph Lameter765c4502006-09-27 01:50:08 -07003035 if (in_interrupt() || (flags & __GFP_THISNODE))
Paul Jacksonc61afb12006-03-24 03:16:08 -08003036 return NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003037 nid_alloc = nid_here = numa_mem_id();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003038 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
Jack Steiner6adef3e2010-05-26 14:42:49 -07003039 nid_alloc = cpuset_slab_spread_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003040 else if (current->mempolicy)
David Rientjes2a389612014-04-07 15:37:29 -07003041 nid_alloc = mempolicy_slab_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003042 if (nid_alloc != nid_here)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003043 return ____cache_alloc_node(cachep, flags, nid_alloc);
Paul Jacksonc61afb12006-03-24 03:16:08 -08003044 return NULL;
3045}
3046
3047/*
Christoph Lameter765c4502006-09-27 01:50:08 -07003048 * Fallback function if there was no memory available and no objects on a
Christoph Lameter3c517a62006-12-06 20:33:29 -08003049 * certain node and fall back is permitted. First we scan all the
Christoph Lameter6a673682013-01-10 19:14:19 +00003050 * available node for available objects. If that fails then we
Christoph Lameter3c517a62006-12-06 20:33:29 -08003051 * perform an allocation without specifying a node. This allows the page
3052 * allocator to do its reclaim / fallback magic. We then insert the
3053 * slab into the proper nodelist and then allocate from it.
Christoph Lameter765c4502006-09-27 01:50:08 -07003054 */
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003055static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
Christoph Lameter765c4502006-09-27 01:50:08 -07003056{
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003057 struct zonelist *zonelist;
3058 gfp_t local_flags;
Mel Gormandd1a2392008-04-28 02:12:17 -07003059 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07003060 struct zone *zone;
3061 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003062 void *obj = NULL;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003063 int nid;
Mel Gormancc9a6c82012-03-21 16:34:11 -07003064 unsigned int cpuset_mems_cookie;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003065
3066 if (flags & __GFP_THISNODE)
3067 return NULL;
3068
Christoph Lameter6cb06222007-10-16 01:25:41 -07003069 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Christoph Lameter765c4502006-09-27 01:50:08 -07003070
Mel Gormancc9a6c82012-03-21 16:34:11 -07003071retry_cpuset:
Mel Gormand26914d2014-04-03 14:47:24 -07003072 cpuset_mems_cookie = read_mems_allowed_begin();
David Rientjes2a389612014-04-07 15:37:29 -07003073 zonelist = node_zonelist(mempolicy_slab_node(), flags);
Mel Gormancc9a6c82012-03-21 16:34:11 -07003074
Christoph Lameter3c517a62006-12-06 20:33:29 -08003075retry:
3076 /*
3077 * Look through allowed nodes for objects available
3078 * from existing per node queues.
3079 */
Mel Gorman54a6eb52008-04-28 02:12:16 -07003080 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
3081 nid = zone_to_nid(zone);
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003082
Vladimir Davydov061d7072014-12-12 16:58:25 -08003083 if (cpuset_zone_allowed(zone, flags) &&
Christoph Lameter18bf8542014-08-06 16:04:11 -07003084 get_node(cache, nid) &&
3085 get_node(cache, nid)->free_objects) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003086 obj = ____cache_alloc_node(cache,
David Rientjes4167e9b2015-04-14 15:46:55 -07003087 gfp_exact_node(flags), nid);
Christoph Lameter481c5342008-06-21 16:46:35 -07003088 if (obj)
3089 break;
3090 }
Christoph Lameter3c517a62006-12-06 20:33:29 -08003091 }
3092
Christoph Lametercfce6602007-05-06 14:50:17 -07003093 if (!obj) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003094 /*
3095 * This allocation will be performed within the constraints
3096 * of the current cpuset / memory policy requirements.
3097 * We may trigger various forms of reclaim on the allowed
3098 * set and go into memory reserves if necessary.
3099 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003100 struct page *page;
3101
Mel Gormand0164ad2015-11-06 16:28:21 -08003102 if (gfpflags_allow_blocking(local_flags))
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003103 local_irq_enable();
3104 kmem_flagcheck(cache, flags);
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003105 page = kmem_getpages(cache, local_flags, numa_mem_id());
Mel Gormand0164ad2015-11-06 16:28:21 -08003106 if (gfpflags_allow_blocking(local_flags))
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003107 local_irq_disable();
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003108 if (page) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003109 /*
3110 * Insert into the appropriate per node queues
3111 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003112 nid = page_to_nid(page);
3113 if (cache_grow(cache, flags, nid, page)) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003114 obj = ____cache_alloc_node(cache,
David Rientjes4167e9b2015-04-14 15:46:55 -07003115 gfp_exact_node(flags), nid);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003116 if (!obj)
3117 /*
3118 * Another processor may allocate the
3119 * objects in the slab since we are
3120 * not holding any locks.
3121 */
3122 goto retry;
3123 } else {
Hugh Dickinsb6a60452007-01-05 16:36:36 -08003124 /* cache_grow already freed obj */
Christoph Lameter3c517a62006-12-06 20:33:29 -08003125 obj = NULL;
3126 }
3127 }
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003128 }
Mel Gormancc9a6c82012-03-21 16:34:11 -07003129
Mel Gormand26914d2014-04-03 14:47:24 -07003130 if (unlikely(!obj && read_mems_allowed_retry(cpuset_mems_cookie)))
Mel Gormancc9a6c82012-03-21 16:34:11 -07003131 goto retry_cpuset;
Christoph Lameter765c4502006-09-27 01:50:08 -07003132 return obj;
3133}
3134
3135/*
Christoph Lametere498be72005-09-09 13:03:32 -07003136 * A interface to enable slab creation on nodeid
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003138static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003139 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07003140{
Joonsoo Kim8456a642013-10-24 10:07:49 +09003141 struct page *page;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003142 struct kmem_cache_node *n;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003143 void *obj;
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07003144 void *list = NULL;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003145 int x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Paul Mackerras7c3fbbd2014-12-02 15:59:48 -08003147 VM_BUG_ON(nodeid < 0 || nodeid >= MAX_NUMNODES);
Christoph Lameter18bf8542014-08-06 16:04:11 -07003148 n = get_node(cachep, nodeid);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003149 BUG_ON(!n);
Christoph Lametere498be72005-09-09 13:03:32 -07003150
Andrew Mortona737b3e2006-03-22 00:08:11 -08003151retry:
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003152 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003153 spin_lock(&n->list_lock);
Geliang Tang7aa0d222016-01-14 15:18:02 -08003154 page = get_first_slab(n);
3155 if (!page)
3156 goto must_grow;
Christoph Lametere498be72005-09-09 13:03:32 -07003157
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003158 check_spinlock_acquired_node(cachep, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07003159
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003160 STATS_INC_NODEALLOCS(cachep);
3161 STATS_INC_ACTIVE(cachep);
3162 STATS_SET_HIGH(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003163
Joonsoo Kim8456a642013-10-24 10:07:49 +09003164 BUG_ON(page->active == cachep->num);
Christoph Lametere498be72005-09-09 13:03:32 -07003165
Joonsoo Kim260b61d2016-03-15 14:54:12 -07003166 obj = slab_get_obj(cachep, page);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003167 n->free_objects--;
Christoph Lametere498be72005-09-09 13:03:32 -07003168
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07003169 fixup_slab_list(cachep, n, page, &list);
Christoph Lametere498be72005-09-09 13:03:32 -07003170
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003171 spin_unlock(&n->list_lock);
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07003172 fixup_objfreelist_debug(cachep, &list);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003173 goto done;
Christoph Lametere498be72005-09-09 13:03:32 -07003174
Andrew Mortona737b3e2006-03-22 00:08:11 -08003175must_grow:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003176 spin_unlock(&n->list_lock);
David Rientjes4167e9b2015-04-14 15:46:55 -07003177 x = cache_grow(cachep, gfp_exact_node(flags), nodeid, NULL);
Christoph Lameter765c4502006-09-27 01:50:08 -07003178 if (x)
3179 goto retry;
Christoph Lametere498be72005-09-09 13:03:32 -07003180
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003181 return fallback_alloc(cachep, flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003182
Andrew Mortona737b3e2006-03-22 00:08:11 -08003183done:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003184 return obj;
Christoph Lametere498be72005-09-09 13:03:32 -07003185}
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003186
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003187static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003188slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003189 unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003190{
3191 unsigned long save_flags;
3192 void *ptr;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003193 int slab_node = numa_mem_id();
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003194
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003195 flags &= gfp_allowed_mask;
Jesper Dangaard Brouer011ecea2016-03-15 14:53:41 -07003196 cachep = slab_pre_alloc_hook(cachep, flags);
3197 if (unlikely(!cachep))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003198 return NULL;
3199
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003200 cache_alloc_debugcheck_before(cachep, flags);
3201 local_irq_save(save_flags);
3202
Andrew Mortoneacbbae2011-07-28 13:59:49 -07003203 if (nodeid == NUMA_NO_NODE)
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003204 nodeid = slab_node;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003205
Christoph Lameter18bf8542014-08-06 16:04:11 -07003206 if (unlikely(!get_node(cachep, nodeid))) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003207 /* Node not bootstrapped yet */
3208 ptr = fallback_alloc(cachep, flags);
3209 goto out;
3210 }
3211
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003212 if (nodeid == slab_node) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003213 /*
3214 * Use the locally cached objects if possible.
3215 * However ____cache_alloc does not allow fallback
3216 * to other nodes. It may fail while we still have
3217 * objects on other nodes available.
3218 */
3219 ptr = ____cache_alloc(cachep, flags);
3220 if (ptr)
3221 goto out;
3222 }
3223 /* ___cache_alloc_node can fall back to other nodes */
3224 ptr = ____cache_alloc_node(cachep, flags, nodeid);
3225 out:
3226 local_irq_restore(save_flags);
3227 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
3228
Jesper Dangaard Brouerd5e3ed62016-03-15 14:53:47 -07003229 if (unlikely(flags & __GFP_ZERO) && ptr)
3230 memset(ptr, 0, cachep->object_size);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003231
Jesper Dangaard Brouerd5e3ed62016-03-15 14:53:47 -07003232 slab_post_alloc_hook(cachep, flags, 1, &ptr);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003233 return ptr;
3234}
3235
3236static __always_inline void *
3237__do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
3238{
3239 void *objp;
3240
Zefan Li2ad654b2014-09-25 09:41:02 +08003241 if (current->mempolicy || cpuset_do_slab_mem_spread()) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003242 objp = alternate_node_alloc(cache, flags);
3243 if (objp)
3244 goto out;
3245 }
3246 objp = ____cache_alloc(cache, flags);
3247
3248 /*
3249 * We may just have run out of memory on the local node.
3250 * ____cache_alloc_node() knows how to locate memory on other nodes
3251 */
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003252 if (!objp)
3253 objp = ____cache_alloc_node(cache, flags, numa_mem_id());
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003254
3255 out:
3256 return objp;
3257}
3258#else
3259
3260static __always_inline void *
3261__do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
3262{
3263 return ____cache_alloc(cachep, flags);
3264}
3265
3266#endif /* CONFIG_NUMA */
3267
3268static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003269slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003270{
3271 unsigned long save_flags;
3272 void *objp;
3273
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003274 flags &= gfp_allowed_mask;
Jesper Dangaard Brouer011ecea2016-03-15 14:53:41 -07003275 cachep = slab_pre_alloc_hook(cachep, flags);
3276 if (unlikely(!cachep))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003277 return NULL;
3278
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003279 cache_alloc_debugcheck_before(cachep, flags);
3280 local_irq_save(save_flags);
3281 objp = __do_cache_alloc(cachep, flags);
3282 local_irq_restore(save_flags);
3283 objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
3284 prefetchw(objp);
3285
Jesper Dangaard Brouerd5e3ed62016-03-15 14:53:47 -07003286 if (unlikely(flags & __GFP_ZERO) && objp)
3287 memset(objp, 0, cachep->object_size);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003288
Jesper Dangaard Brouerd5e3ed62016-03-15 14:53:47 -07003289 slab_post_alloc_hook(cachep, flags, 1, &objp);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003290 return objp;
3291}
Christoph Lametere498be72005-09-09 13:03:32 -07003292
3293/*
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08003294 * Caller needs to acquire correct kmem_cache_node's list_lock
Joonsoo Kim97654df2014-08-06 16:04:25 -07003295 * @list: List of detached free slabs should be freed by caller
Christoph Lametere498be72005-09-09 13:03:32 -07003296 */
Joonsoo Kim97654df2014-08-06 16:04:25 -07003297static void free_block(struct kmem_cache *cachep, void **objpp,
3298 int nr_objects, int node, struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299{
3300 int i;
Joonsoo Kim25c063f2014-08-06 16:04:22 -07003301 struct kmem_cache_node *n = get_node(cachep, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302
3303 for (i = 0; i < nr_objects; i++) {
Mel Gorman072bb0a2012-07-31 16:43:58 -07003304 void *objp;
Joonsoo Kim8456a642013-10-24 10:07:49 +09003305 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306
Mel Gorman072bb0a2012-07-31 16:43:58 -07003307 clear_obj_pfmemalloc(&objpp[i]);
3308 objp = objpp[i];
3309
Joonsoo Kim8456a642013-10-24 10:07:49 +09003310 page = virt_to_head_page(objp);
Joonsoo Kim8456a642013-10-24 10:07:49 +09003311 list_del(&page->lru);
Christoph Lameterff694162005-09-22 21:44:02 -07003312 check_spinlock_acquired_node(cachep, node);
Joonsoo Kim260b61d2016-03-15 14:54:12 -07003313 slab_put_obj(cachep, page, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 STATS_DEC_ACTIVE(cachep);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003315 n->free_objects++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
3317 /* fixup slab chains */
Joonsoo Kim8456a642013-10-24 10:07:49 +09003318 if (page->active == 0) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003319 if (n->free_objects > n->free_limit) {
3320 n->free_objects -= cachep->num;
Joonsoo Kim97654df2014-08-06 16:04:25 -07003321 list_add_tail(&page->lru, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322 } else {
Joonsoo Kim8456a642013-10-24 10:07:49 +09003323 list_add(&page->lru, &n->slabs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 }
3325 } else {
3326 /* Unconditionally move a slab to the end of the
3327 * partial list on free - maximum time for the
3328 * other objects to be freed, too.
3329 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09003330 list_add_tail(&page->lru, &n->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 }
3332 }
3333}
3334
Pekka Enberg343e0d72006-02-01 03:05:50 -08003335static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336{
3337 int batchcount;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003338 struct kmem_cache_node *n;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003339 int node = numa_mem_id();
Joonsoo Kim97654df2014-08-06 16:04:25 -07003340 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341
3342 batchcount = ac->batchcount;
Joonsoo Kim260b61d2016-03-15 14:54:12 -07003343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 check_irq_off();
Christoph Lameter18bf8542014-08-06 16:04:11 -07003345 n = get_node(cachep, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003346 spin_lock(&n->list_lock);
3347 if (n->shared) {
3348 struct array_cache *shared_array = n->shared;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003349 int max = shared_array->limit - shared_array->avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 if (max) {
3351 if (batchcount > max)
3352 batchcount = max;
Christoph Lametere498be72005-09-09 13:03:32 -07003353 memcpy(&(shared_array->entry[shared_array->avail]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003354 ac->entry, sizeof(void *) * batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355 shared_array->avail += batchcount;
3356 goto free_done;
3357 }
3358 }
3359
Joonsoo Kim97654df2014-08-06 16:04:25 -07003360 free_block(cachep, ac->entry, batchcount, node, &list);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003361free_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362#if STATS
3363 {
3364 int i = 0;
Geliang Tang73c02192016-01-14 15:17:59 -08003365 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
Geliang Tang73c02192016-01-14 15:17:59 -08003367 list_for_each_entry(page, &n->slabs_free, lru) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09003368 BUG_ON(page->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369
3370 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 }
3372 STATS_SET_FREEABLE(cachep, i);
3373 }
3374#endif
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003375 spin_unlock(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07003376 slabs_destroy(cachep, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 ac->avail -= batchcount;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003378 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379}
3380
3381/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08003382 * Release an obj back to its cache. If the obj has a constructed state, it must
3383 * be in this state _before_ it is released. Called with disabled ints.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 */
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003385static inline void __cache_free(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003386 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387{
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003388 struct array_cache *ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
3390 check_irq_off();
Catalin Marinasd5cff632009-06-11 13:22:40 +01003391 kmemleak_free_recursive(objp, cachep->flags);
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003392 objp = cache_free_debugcheck(cachep, objp, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003394 kmemcheck_slab_free(cachep, objp, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003395
Siddha, Suresh B1807a1a2007-08-22 14:01:49 -07003396 /*
3397 * Skip calling cache_free_alien() when the platform is not numa.
3398 * This will avoid cache misses that happen while accessing slabp (which
3399 * is per page memory reference) to get nodeid. Instead use a global
3400 * variable to skip the call, which is mostly likely to be present in
3401 * the cache.
3402 */
Mel Gormanb6e68bc2009-06-16 15:32:16 -07003403 if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07003404 return;
Christoph Lametere498be72005-09-09 13:03:32 -07003405
Joonsoo Kim3d880192014-10-09 15:26:04 -07003406 if (ac->avail < ac->limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 STATS_INC_FREEHIT(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 } else {
3409 STATS_INC_FREEMISS(cachep);
3410 cache_flusharray(cachep, ac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003411 }
Zhao Jin42c8c992011-08-27 00:26:17 +08003412
Mel Gorman072bb0a2012-07-31 16:43:58 -07003413 ac_put_obj(cachep, ac, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414}
3415
3416/**
3417 * kmem_cache_alloc - Allocate an object
3418 * @cachep: The cache to allocate from.
3419 * @flags: See kmalloc().
3420 *
3421 * Allocate an object from this cache. The flags are only relevant
3422 * if the cache has no available objects.
3423 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003424void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003426 void *ret = slab_alloc(cachep, flags, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003427
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003428 trace_kmem_cache_alloc(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003429 cachep->object_size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003430
3431 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432}
3433EXPORT_SYMBOL(kmem_cache_alloc);
3434
Jesper Dangaard Brouer7b0501d2016-03-15 14:53:53 -07003435static __always_inline void
3436cache_alloc_debugcheck_after_bulk(struct kmem_cache *s, gfp_t flags,
3437 size_t size, void **p, unsigned long caller)
3438{
3439 size_t i;
3440
3441 for (i = 0; i < size; i++)
3442 p[i] = cache_alloc_debugcheck_after(s, flags, p[i], caller);
3443}
3444
Jesper Dangaard Brouer865762a2015-11-20 15:57:58 -08003445int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
Jesper Dangaard Brouer2a777ea2016-03-15 14:53:50 -07003446 void **p)
Christoph Lameter484748f2015-09-04 15:45:34 -07003447{
Jesper Dangaard Brouer2a777ea2016-03-15 14:53:50 -07003448 size_t i;
3449
3450 s = slab_pre_alloc_hook(s, flags);
3451 if (!s)
3452 return 0;
3453
3454 cache_alloc_debugcheck_before(s, flags);
3455
3456 local_irq_disable();
3457 for (i = 0; i < size; i++) {
3458 void *objp = __do_cache_alloc(s, flags);
3459
Jesper Dangaard Brouer2a777ea2016-03-15 14:53:50 -07003460 if (unlikely(!objp))
3461 goto error;
3462 p[i] = objp;
3463 }
3464 local_irq_enable();
3465
Jesper Dangaard Brouer7b0501d2016-03-15 14:53:53 -07003466 cache_alloc_debugcheck_after_bulk(s, flags, size, p, _RET_IP_);
3467
Jesper Dangaard Brouer2a777ea2016-03-15 14:53:50 -07003468 /* Clear memory outside IRQ disabled section */
3469 if (unlikely(flags & __GFP_ZERO))
3470 for (i = 0; i < size; i++)
3471 memset(p[i], 0, s->object_size);
3472
3473 slab_post_alloc_hook(s, flags, size, p);
3474 /* FIXME: Trace call missing. Christoph would like a bulk variant */
3475 return size;
3476error:
3477 local_irq_enable();
Jesper Dangaard Brouer7b0501d2016-03-15 14:53:53 -07003478 cache_alloc_debugcheck_after_bulk(s, flags, i, p, _RET_IP_);
Jesper Dangaard Brouer2a777ea2016-03-15 14:53:50 -07003479 slab_post_alloc_hook(s, flags, i, p);
3480 __kmem_cache_free_bulk(s, i, p);
3481 return 0;
Christoph Lameter484748f2015-09-04 15:45:34 -07003482}
3483EXPORT_SYMBOL(kmem_cache_alloc_bulk);
3484
Li Zefan0f24f122009-12-11 15:45:30 +08003485#ifdef CONFIG_TRACING
Steven Rostedt85beb582010-11-24 16:23:34 -05003486void *
Ezequiel Garcia40521472012-09-08 17:47:56 -03003487kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003488{
Steven Rostedt85beb582010-11-24 16:23:34 -05003489 void *ret;
3490
Ezequiel Garcia48356302012-09-08 17:47:57 -03003491 ret = slab_alloc(cachep, flags, _RET_IP_);
Steven Rostedt85beb582010-11-24 16:23:34 -05003492
3493 trace_kmalloc(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003494 size, cachep->size, flags);
Steven Rostedt85beb582010-11-24 16:23:34 -05003495 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003496}
Steven Rostedt85beb582010-11-24 16:23:34 -05003497EXPORT_SYMBOL(kmem_cache_alloc_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003498#endif
3499
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500#ifdef CONFIG_NUMA
Zhouping Liud0d04b72013-05-16 11:36:23 +08003501/**
3502 * kmem_cache_alloc_node - Allocate an object on the specified node
3503 * @cachep: The cache to allocate from.
3504 * @flags: See kmalloc().
3505 * @nodeid: node number of the target node.
3506 *
3507 * Identical to kmem_cache_alloc but it will allocate memory on the given
3508 * node, which can improve the performance for cpu bound structures.
3509 *
3510 * Fallback to other node is possible if __GFP_THISNODE is not set.
3511 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003512void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
3513{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003514 void *ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003515
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003516 trace_kmem_cache_alloc_node(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003517 cachep->object_size, cachep->size,
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003518 flags, nodeid);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003519
3520 return ret;
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003521}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522EXPORT_SYMBOL(kmem_cache_alloc_node);
3523
Li Zefan0f24f122009-12-11 15:45:30 +08003524#ifdef CONFIG_TRACING
Ezequiel Garcia40521472012-09-08 17:47:56 -03003525void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
Steven Rostedt85beb582010-11-24 16:23:34 -05003526 gfp_t flags,
Ezequiel Garcia40521472012-09-08 17:47:56 -03003527 int nodeid,
3528 size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003529{
Steven Rostedt85beb582010-11-24 16:23:34 -05003530 void *ret;
3531
Ezequiel Garcia592f4142012-09-25 08:07:08 -03003532 ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003533
Steven Rostedt85beb582010-11-24 16:23:34 -05003534 trace_kmalloc_node(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003535 size, cachep->size,
Steven Rostedt85beb582010-11-24 16:23:34 -05003536 flags, nodeid);
3537 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003538}
Steven Rostedt85beb582010-11-24 16:23:34 -05003539EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003540#endif
3541
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003542static __always_inline void *
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003543__do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller)
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003544{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003545 struct kmem_cache *cachep;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003546
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003547 cachep = kmalloc_slab(size, flags);
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003548 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3549 return cachep;
Ezequiel Garcia40521472012-09-08 17:47:56 -03003550 return kmem_cache_alloc_node_trace(cachep, flags, node, size);
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003551}
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003552
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003553void *__kmalloc_node(size_t size, gfp_t flags, int node)
3554{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003555 return __do_kmalloc_node(size, flags, node, _RET_IP_);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003556}
Christoph Hellwigdbe5e692006-09-25 23:31:36 -07003557EXPORT_SYMBOL(__kmalloc_node);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003558
3559void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003560 int node, unsigned long caller)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003561{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003562 return __do_kmalloc_node(size, flags, node, caller);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003563}
3564EXPORT_SYMBOL(__kmalloc_node_track_caller);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003565#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566
3567/**
Paul Drynoff800590f2006-06-23 02:03:48 -07003568 * __do_kmalloc - allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 * @size: how many bytes of memory are required.
Paul Drynoff800590f2006-06-23 02:03:48 -07003570 * @flags: the type of memory to allocate (see kmalloc).
Randy Dunlap911851e2006-03-22 00:08:14 -08003571 * @caller: function caller for debug tracking of the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 */
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003573static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003574 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003576 struct kmem_cache *cachep;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003577 void *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003579 cachep = kmalloc_slab(size, flags);
Linus Torvaldsa5c96d82007-07-19 13:17:15 -07003580 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3581 return cachep;
Ezequiel Garcia48356302012-09-08 17:47:57 -03003582 ret = slab_alloc(cachep, flags, caller);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003583
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003584 trace_kmalloc(caller, ret,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003585 size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003586
3587 return ret;
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003588}
3589
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003590void *__kmalloc(size_t size, gfp_t flags)
3591{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003592 return __do_kmalloc(size, flags, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593}
3594EXPORT_SYMBOL(__kmalloc);
3595
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003596void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003597{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003598 return __do_kmalloc(size, flags, caller);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003599}
3600EXPORT_SYMBOL(__kmalloc_track_caller);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003601
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602/**
3603 * kmem_cache_free - Deallocate an object
3604 * @cachep: The cache the allocation was from.
3605 * @objp: The previously allocated object.
3606 *
3607 * Free an object which was previously allocated from this
3608 * cache.
3609 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003610void kmem_cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611{
3612 unsigned long flags;
Glauber Costab9ce5ef2012-12-18 14:22:46 -08003613 cachep = cache_from_obj(cachep, objp);
3614 if (!cachep)
3615 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616
3617 local_irq_save(flags);
Feng Tangd97d4762012-07-02 14:29:10 +08003618 debug_check_no_locks_freed(objp, cachep->object_size);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07003619 if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003620 debug_check_no_obj_freed(objp, cachep->object_size);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003621 __cache_free(cachep, objp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 local_irq_restore(flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003623
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003624 trace_kmem_cache_free(_RET_IP_, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625}
3626EXPORT_SYMBOL(kmem_cache_free);
3627
Jesper Dangaard Brouere6cdb582016-03-15 14:53:56 -07003628void kmem_cache_free_bulk(struct kmem_cache *orig_s, size_t size, void **p)
3629{
3630 struct kmem_cache *s;
3631 size_t i;
3632
3633 local_irq_disable();
3634 for (i = 0; i < size; i++) {
3635 void *objp = p[i];
3636
Jesper Dangaard Brouerca257192016-03-15 14:54:00 -07003637 if (!orig_s) /* called via kfree_bulk */
3638 s = virt_to_cache(objp);
3639 else
3640 s = cache_from_obj(orig_s, objp);
Jesper Dangaard Brouere6cdb582016-03-15 14:53:56 -07003641
3642 debug_check_no_locks_freed(objp, s->object_size);
3643 if (!(s->flags & SLAB_DEBUG_OBJECTS))
3644 debug_check_no_obj_freed(objp, s->object_size);
3645
3646 __cache_free(s, objp, _RET_IP_);
3647 }
3648 local_irq_enable();
3649
3650 /* FIXME: add tracing */
3651}
3652EXPORT_SYMBOL(kmem_cache_free_bulk);
3653
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 * kfree - free previously allocated memory
3656 * @objp: pointer returned by kmalloc.
3657 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07003658 * If @objp is NULL, no operation is performed.
3659 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 * Don't free memory not originally allocated by kmalloc()
3661 * or you will run into trouble.
3662 */
3663void kfree(const void *objp)
3664{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003665 struct kmem_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 unsigned long flags;
3667
Pekka Enberg2121db72009-03-25 11:05:57 +02003668 trace_kfree(_RET_IP_, objp);
3669
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003670 if (unlikely(ZERO_OR_NULL_PTR(objp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003671 return;
3672 local_irq_save(flags);
3673 kfree_debugcheck(objp);
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003674 c = virt_to_cache(objp);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003675 debug_check_no_locks_freed(objp, c->object_size);
3676
3677 debug_check_no_obj_freed(objp, c->object_size);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003678 __cache_free(c, (void *)objp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 local_irq_restore(flags);
3680}
3681EXPORT_SYMBOL(kfree);
3682
Christoph Lametere498be72005-09-09 13:03:32 -07003683/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003684 * This initializes kmem_cache_node or resizes various caches for all nodes.
Christoph Lametere498be72005-09-09 13:03:32 -07003685 */
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08003686static int alloc_kmem_cache_node(struct kmem_cache *cachep, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -07003687{
3688 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003689 struct kmem_cache_node *n;
Christoph Lametercafeb022006-03-25 03:06:46 -08003690 struct array_cache *new_shared;
Joonsoo Kimc8522a32014-08-06 16:04:29 -07003691 struct alien_cache **new_alien = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003692
Mel Gorman9c09a952008-01-24 05:49:54 -08003693 for_each_online_node(node) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003694
LQYMGTb455def2014-12-10 15:42:13 -08003695 if (use_alien_caches) {
3696 new_alien = alloc_alien_cache(node, cachep->limit, gfp);
3697 if (!new_alien)
3698 goto fail;
3699 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003700
Eric Dumazet63109842007-05-06 14:49:28 -07003701 new_shared = NULL;
3702 if (cachep->shared) {
3703 new_shared = alloc_arraycache(node,
Christoph Lameter0718dc22006-03-25 03:06:47 -08003704 cachep->shared*cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003705 0xbaadf00d, gfp);
Eric Dumazet63109842007-05-06 14:49:28 -07003706 if (!new_shared) {
3707 free_alien_cache(new_alien);
3708 goto fail;
3709 }
Christoph Lameter0718dc22006-03-25 03:06:47 -08003710 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003711
Christoph Lameter18bf8542014-08-06 16:04:11 -07003712 n = get_node(cachep, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003713 if (n) {
3714 struct array_cache *shared = n->shared;
Joonsoo Kim97654df2014-08-06 16:04:25 -07003715 LIST_HEAD(list);
Christoph Lametercafeb022006-03-25 03:06:46 -08003716
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003717 spin_lock_irq(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07003718
Christoph Lametercafeb022006-03-25 03:06:46 -08003719 if (shared)
Christoph Lameter0718dc22006-03-25 03:06:47 -08003720 free_block(cachep, shared->entry,
Joonsoo Kim97654df2014-08-06 16:04:25 -07003721 shared->avail, node, &list);
Christoph Lametere498be72005-09-09 13:03:32 -07003722
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003723 n->shared = new_shared;
3724 if (!n->alien) {
3725 n->alien = new_alien;
Christoph Lametere498be72005-09-09 13:03:32 -07003726 new_alien = NULL;
3727 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003728 n->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003729 cachep->batchcount + cachep->num;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003730 spin_unlock_irq(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07003731 slabs_destroy(cachep, &list);
Christoph Lametercafeb022006-03-25 03:06:46 -08003732 kfree(shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003733 free_alien_cache(new_alien);
3734 continue;
3735 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003736 n = kmalloc_node(sizeof(struct kmem_cache_node), gfp, node);
3737 if (!n) {
Christoph Lameter0718dc22006-03-25 03:06:47 -08003738 free_alien_cache(new_alien);
3739 kfree(new_shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003740 goto fail;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003741 }
Christoph Lametere498be72005-09-09 13:03:32 -07003742
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003743 kmem_cache_node_init(n);
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08003744 n->next_reap = jiffies + REAPTIMEOUT_NODE +
3745 ((unsigned long)cachep) % REAPTIMEOUT_NODE;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003746 n->shared = new_shared;
3747 n->alien = new_alien;
3748 n->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003749 cachep->batchcount + cachep->num;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003750 cachep->node[node] = n;
Christoph Lametere498be72005-09-09 13:03:32 -07003751 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003752 return 0;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003753
Andrew Mortona737b3e2006-03-22 00:08:11 -08003754fail:
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003755 if (!cachep->list.next) {
Christoph Lameter0718dc22006-03-25 03:06:47 -08003756 /* Cache is not active yet. Roll back what we did */
3757 node--;
3758 while (node >= 0) {
Christoph Lameter18bf8542014-08-06 16:04:11 -07003759 n = get_node(cachep, node);
3760 if (n) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003761 kfree(n->shared);
3762 free_alien_cache(n->alien);
3763 kfree(n);
Christoph Lameter6a673682013-01-10 19:14:19 +00003764 cachep->node[node] = NULL;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003765 }
3766 node--;
3767 }
3768 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003769 return -ENOMEM;
Christoph Lametere498be72005-09-09 13:03:32 -07003770}
3771
Christoph Lameter18004c52012-07-06 15:25:12 -05003772/* Always called with the slab_mutex held */
Glauber Costa943a4512012-12-18 14:23:03 -08003773static int __do_tune_cpucache(struct kmem_cache *cachep, int limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003774 int batchcount, int shared, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003775{
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07003776 struct array_cache __percpu *cpu_cache, *prev;
3777 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07003779 cpu_cache = alloc_kmem_cache_cpus(cachep, limit, batchcount);
3780 if (!cpu_cache)
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003781 return -ENOMEM;
3782
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07003783 prev = cachep->cpu_cache;
3784 cachep->cpu_cache = cpu_cache;
3785 kick_all_cpus_sync();
Christoph Lametere498be72005-09-09 13:03:32 -07003786
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 check_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788 cachep->batchcount = batchcount;
3789 cachep->limit = limit;
Christoph Lametere498be72005-09-09 13:03:32 -07003790 cachep->shared = shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07003792 if (!prev)
3793 goto alloc_node;
3794
3795 for_each_online_cpu(cpu) {
Joonsoo Kim97654df2014-08-06 16:04:25 -07003796 LIST_HEAD(list);
Christoph Lameter18bf8542014-08-06 16:04:11 -07003797 int node;
3798 struct kmem_cache_node *n;
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07003799 struct array_cache *ac = per_cpu_ptr(prev, cpu);
Christoph Lameter18bf8542014-08-06 16:04:11 -07003800
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07003801 node = cpu_to_mem(cpu);
Christoph Lameter18bf8542014-08-06 16:04:11 -07003802 n = get_node(cachep, node);
3803 spin_lock_irq(&n->list_lock);
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07003804 free_block(cachep, ac->entry, ac->avail, node, &list);
Christoph Lameter18bf8542014-08-06 16:04:11 -07003805 spin_unlock_irq(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07003806 slabs_destroy(cachep, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 }
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07003808 free_percpu(prev);
3809
3810alloc_node:
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08003811 return alloc_kmem_cache_node(cachep, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812}
3813
Glauber Costa943a4512012-12-18 14:23:03 -08003814static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
3815 int batchcount, int shared, gfp_t gfp)
3816{
3817 int ret;
Vladimir Davydov426589f2015-02-12 14:59:23 -08003818 struct kmem_cache *c;
Glauber Costa943a4512012-12-18 14:23:03 -08003819
3820 ret = __do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
3821
3822 if (slab_state < FULL)
3823 return ret;
3824
3825 if ((ret < 0) || !is_root_cache(cachep))
3826 return ret;
3827
Vladimir Davydov426589f2015-02-12 14:59:23 -08003828 lockdep_assert_held(&slab_mutex);
3829 for_each_memcg_cache(c, cachep) {
3830 /* return value determined by the root cache only */
3831 __do_tune_cpucache(c, limit, batchcount, shared, gfp);
Glauber Costa943a4512012-12-18 14:23:03 -08003832 }
3833
3834 return ret;
3835}
3836
Christoph Lameter18004c52012-07-06 15:25:12 -05003837/* Called with slab_mutex held always */
Pekka Enberg83b519e2009-06-10 19:40:04 +03003838static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839{
3840 int err;
Glauber Costa943a4512012-12-18 14:23:03 -08003841 int limit = 0;
3842 int shared = 0;
3843 int batchcount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844
Glauber Costa943a4512012-12-18 14:23:03 -08003845 if (!is_root_cache(cachep)) {
3846 struct kmem_cache *root = memcg_root_cache(cachep);
3847 limit = root->limit;
3848 shared = root->shared;
3849 batchcount = root->batchcount;
3850 }
3851
3852 if (limit && shared && batchcount)
3853 goto skip_setup;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003854 /*
3855 * The head array serves three purposes:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 * - create a LIFO ordering, i.e. return objects that are cache-warm
3857 * - reduce the number of spinlock operations.
Andrew Mortona737b3e2006-03-22 00:08:11 -08003858 * - reduce the number of linked list operations on the slab and
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 * bufctl chains: array operations are cheaper.
3860 * The numbers are guessed, we should auto-tune as described by
3861 * Bonwick.
3862 */
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003863 if (cachep->size > 131072)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 limit = 1;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003865 else if (cachep->size > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 limit = 8;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003867 else if (cachep->size > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 limit = 24;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003869 else if (cachep->size > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870 limit = 54;
3871 else
3872 limit = 120;
3873
Andrew Mortona737b3e2006-03-22 00:08:11 -08003874 /*
3875 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
Linus Torvalds1da177e2005-04-16 15:20:36 -07003876 * allocation behaviour: Most allocs on one cpu, most free operations
3877 * on another cpu. For these cases, an efficient object passing between
3878 * cpus is necessary. This is provided by a shared array. The array
3879 * replaces Bonwick's magazine layer.
3880 * On uniprocessor, it's functionally equivalent (but less efficient)
3881 * to a larger limit. Thus disabled by default.
3882 */
3883 shared = 0;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003884 if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003885 shared = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886
3887#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003888 /*
3889 * With debugging enabled, large batchcount lead to excessively long
3890 * periods with disabled local interrupts. Limit the batchcount
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891 */
3892 if (limit > 32)
3893 limit = 32;
3894#endif
Glauber Costa943a4512012-12-18 14:23:03 -08003895 batchcount = (limit + 1) / 2;
3896skip_setup:
3897 err = do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 if (err)
3899 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003900 cachep->name, -err);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003901 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902}
3903
Christoph Lameter1b552532006-03-22 00:09:07 -08003904/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003905 * Drain an array if it contains any elements taking the node lock only if
3906 * necessary. Note that the node listlock also protects the array_cache
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003907 * if drain_array() is used on the shared array.
Christoph Lameter1b552532006-03-22 00:09:07 -08003908 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003909static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
Christoph Lameter1b552532006-03-22 00:09:07 -08003910 struct array_cache *ac, int force, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911{
Joonsoo Kim97654df2014-08-06 16:04:25 -07003912 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 int tofree;
3914
Christoph Lameter1b552532006-03-22 00:09:07 -08003915 if (!ac || !ac->avail)
3916 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 if (ac->touched && !force) {
3918 ac->touched = 0;
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003919 } else {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003920 spin_lock_irq(&n->list_lock);
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003921 if (ac->avail) {
3922 tofree = force ? ac->avail : (ac->limit + 4) / 5;
3923 if (tofree > ac->avail)
3924 tofree = (ac->avail + 1) / 2;
Joonsoo Kim97654df2014-08-06 16:04:25 -07003925 free_block(cachep, ac->entry, tofree, node, &list);
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003926 ac->avail -= tofree;
3927 memmove(ac->entry, &(ac->entry[tofree]),
3928 sizeof(void *) * ac->avail);
3929 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003930 spin_unlock_irq(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07003931 slabs_destroy(cachep, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 }
3933}
3934
3935/**
3936 * cache_reap - Reclaim memory from caches.
Randy Dunlap05fb6bf2007-02-28 20:12:13 -08003937 * @w: work descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 *
3939 * Called from workqueue/eventd every few seconds.
3940 * Purpose:
3941 * - clear the per-cpu caches for this CPU.
3942 * - return freeable pages to the main free memory pool.
3943 *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003944 * If we cannot acquire the cache chain mutex then just give up - we'll try
3945 * again on the next iteration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946 */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08003947static void cache_reap(struct work_struct *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948{
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07003949 struct kmem_cache *searchp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003950 struct kmem_cache_node *n;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003951 int node = numa_mem_id();
Jean Delvarebf6aede2009-04-02 16:56:54 -07003952 struct delayed_work *work = to_delayed_work(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953
Christoph Lameter18004c52012-07-06 15:25:12 -05003954 if (!mutex_trylock(&slab_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 /* Give up. Setup the next iteration. */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08003956 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
Christoph Lameter18004c52012-07-06 15:25:12 -05003958 list_for_each_entry(searchp, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 check_irq_on();
3960
Christoph Lameter35386e32006-03-22 00:09:05 -08003961 /*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003962 * We only take the node lock if absolutely necessary and we
Christoph Lameter35386e32006-03-22 00:09:05 -08003963 * have established with reasonable certainty that
3964 * we can do some work if the lock was obtained.
3965 */
Christoph Lameter18bf8542014-08-06 16:04:11 -07003966 n = get_node(searchp, node);
Christoph Lameter35386e32006-03-22 00:09:05 -08003967
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003968 reap_alien(searchp, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003970 drain_array(searchp, n, cpu_cache_get(searchp), 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971
Christoph Lameter35386e32006-03-22 00:09:05 -08003972 /*
3973 * These are racy checks but it does not matter
3974 * if we skip one check or scan twice.
3975 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003976 if (time_after(n->next_reap, jiffies))
Christoph Lameter35386e32006-03-22 00:09:05 -08003977 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08003979 n->next_reap = jiffies + REAPTIMEOUT_NODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003981 drain_array(searchp, n, n->shared, 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003983 if (n->free_touched)
3984 n->free_touched = 0;
Christoph Lametered11d9e2006-06-30 01:55:45 -07003985 else {
3986 int freed;
3987
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003988 freed = drain_freelist(searchp, n, (n->free_limit +
Christoph Lametered11d9e2006-06-30 01:55:45 -07003989 5 * searchp->num - 1) / (5 * searchp->num));
3990 STATS_ADD_REAPED(searchp, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 }
Christoph Lameter35386e32006-03-22 00:09:05 -08003992next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 cond_resched();
3994 }
3995 check_irq_on();
Christoph Lameter18004c52012-07-06 15:25:12 -05003996 mutex_unlock(&slab_mutex);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08003997 next_reap_node();
Christoph Lameter7c5cae32007-02-10 01:42:55 -08003998out:
Andrew Mortona737b3e2006-03-22 00:08:11 -08003999 /* Set up the next iteration */
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08004000 schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_AC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001}
4002
Linus Torvalds158a9622008-01-02 13:04:48 -08004003#ifdef CONFIG_SLABINFO
Glauber Costa0d7561c2012-10-19 18:20:27 +04004004void get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005{
Joonsoo Kim8456a642013-10-24 10:07:49 +09004006 struct page *page;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004007 unsigned long active_objs;
4008 unsigned long num_objs;
4009 unsigned long active_slabs = 0;
4010 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004011 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 char *error = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07004013 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004014 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 active_objs = 0;
4017 num_slabs = 0;
Christoph Lameter18bf8542014-08-06 16:04:11 -07004018 for_each_kmem_cache_node(cachep, node, n) {
Christoph Lametere498be72005-09-09 13:03:32 -07004019
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004020 check_irq_on();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004021 spin_lock_irq(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07004022
Joonsoo Kim8456a642013-10-24 10:07:49 +09004023 list_for_each_entry(page, &n->slabs_full, lru) {
4024 if (page->active != cachep->num && !error)
Christoph Lametere498be72005-09-09 13:03:32 -07004025 error = "slabs_full accounting error";
4026 active_objs += cachep->num;
4027 active_slabs++;
4028 }
Joonsoo Kim8456a642013-10-24 10:07:49 +09004029 list_for_each_entry(page, &n->slabs_partial, lru) {
4030 if (page->active == cachep->num && !error)
Joonsoo Kim106a74e2013-10-24 10:07:48 +09004031 error = "slabs_partial accounting error";
Joonsoo Kim8456a642013-10-24 10:07:49 +09004032 if (!page->active && !error)
Joonsoo Kim106a74e2013-10-24 10:07:48 +09004033 error = "slabs_partial accounting error";
Joonsoo Kim8456a642013-10-24 10:07:49 +09004034 active_objs += page->active;
Christoph Lametere498be72005-09-09 13:03:32 -07004035 active_slabs++;
4036 }
Joonsoo Kim8456a642013-10-24 10:07:49 +09004037 list_for_each_entry(page, &n->slabs_free, lru) {
4038 if (page->active && !error)
Joonsoo Kim106a74e2013-10-24 10:07:48 +09004039 error = "slabs_free accounting error";
Christoph Lametere498be72005-09-09 13:03:32 -07004040 num_slabs++;
4041 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004042 free_objects += n->free_objects;
4043 if (n->shared)
4044 shared_avail += n->shared->avail;
Christoph Lametere498be72005-09-09 13:03:32 -07004045
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004046 spin_unlock_irq(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004048 num_slabs += active_slabs;
4049 num_objs = num_slabs * cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07004050 if (num_objs - active_objs != free_objects && !error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 error = "free_objects accounting error";
4052
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004053 name = cachep->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 if (error)
4055 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
4056
Glauber Costa0d7561c2012-10-19 18:20:27 +04004057 sinfo->active_objs = active_objs;
4058 sinfo->num_objs = num_objs;
4059 sinfo->active_slabs = active_slabs;
4060 sinfo->num_slabs = num_slabs;
4061 sinfo->shared_avail = shared_avail;
4062 sinfo->limit = cachep->limit;
4063 sinfo->batchcount = cachep->batchcount;
4064 sinfo->shared = cachep->shared;
4065 sinfo->objects_per_slab = cachep->num;
4066 sinfo->cache_order = cachep->gfporder;
4067}
4068
4069void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *cachep)
4070{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071#if STATS
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004072 { /* node stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 unsigned long high = cachep->high_mark;
4074 unsigned long allocs = cachep->num_allocations;
4075 unsigned long grown = cachep->grown;
4076 unsigned long reaped = cachep->reaped;
4077 unsigned long errors = cachep->errors;
4078 unsigned long max_freeable = cachep->max_freeable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 unsigned long node_allocs = cachep->node_allocs;
Christoph Lametere498be72005-09-09 13:03:32 -07004080 unsigned long node_frees = cachep->node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004081 unsigned long overflows = cachep->node_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
Joe Perchese92dd4f2010-03-26 19:27:58 -07004083 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu "
4084 "%4lu %4lu %4lu %4lu %4lu",
4085 allocs, high, grown,
4086 reaped, errors, max_freeable, node_allocs,
4087 node_frees, overflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 }
4089 /* cpu stats */
4090 {
4091 unsigned long allochit = atomic_read(&cachep->allochit);
4092 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
4093 unsigned long freehit = atomic_read(&cachep->freehit);
4094 unsigned long freemiss = atomic_read(&cachep->freemiss);
4095
4096 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004097 allochit, allocmiss, freehit, freemiss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 }
4099#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100}
4101
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102#define MAX_SLABINFO_WRITE 128
4103/**
4104 * slabinfo_write - Tuning for the slab allocator
4105 * @file: unused
4106 * @buffer: user buffer
4107 * @count: data length
4108 * @ppos: unused
4109 */
Glauber Costab7454ad2012-10-19 18:20:25 +04004110ssize_t slabinfo_write(struct file *file, const char __user *buffer,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004111 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004113 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 int limit, batchcount, shared, res;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004115 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004116
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 if (count > MAX_SLABINFO_WRITE)
4118 return -EINVAL;
4119 if (copy_from_user(&kbuf, buffer, count))
4120 return -EFAULT;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004121 kbuf[MAX_SLABINFO_WRITE] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122
4123 tmp = strchr(kbuf, ' ');
4124 if (!tmp)
4125 return -EINVAL;
4126 *tmp = '\0';
4127 tmp++;
4128 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
4129 return -EINVAL;
4130
4131 /* Find the cache in the chain of caches. */
Christoph Lameter18004c52012-07-06 15:25:12 -05004132 mutex_lock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 res = -EINVAL;
Christoph Lameter18004c52012-07-06 15:25:12 -05004134 list_for_each_entry(cachep, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 if (!strcmp(cachep->name, kbuf)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08004136 if (limit < 1 || batchcount < 1 ||
4137 batchcount > limit || shared < 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07004138 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004139 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07004140 res = do_tune_cpucache(cachep, limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03004141 batchcount, shared,
4142 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 }
4144 break;
4145 }
4146 }
Christoph Lameter18004c52012-07-06 15:25:12 -05004147 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 if (res >= 0)
4149 res = count;
4150 return res;
4151}
Al Viro871751e2006-03-25 03:06:39 -08004152
4153#ifdef CONFIG_DEBUG_SLAB_LEAK
4154
Al Viro871751e2006-03-25 03:06:39 -08004155static inline int add_caller(unsigned long *n, unsigned long v)
4156{
4157 unsigned long *p;
4158 int l;
4159 if (!v)
4160 return 1;
4161 l = n[1];
4162 p = n + 2;
4163 while (l) {
4164 int i = l/2;
4165 unsigned long *q = p + 2 * i;
4166 if (*q == v) {
4167 q[1]++;
4168 return 1;
4169 }
4170 if (*q > v) {
4171 l = i;
4172 } else {
4173 p = q + 2;
4174 l -= i + 1;
4175 }
4176 }
4177 if (++n[1] == n[0])
4178 return 0;
4179 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4180 p[0] = v;
4181 p[1] = 1;
4182 return 1;
4183}
4184
Joonsoo Kim8456a642013-10-24 10:07:49 +09004185static void handle_slab(unsigned long *n, struct kmem_cache *c,
4186 struct page *page)
Al Viro871751e2006-03-25 03:06:39 -08004187{
4188 void *p;
Joonsoo Kimd31676d2016-03-15 14:54:24 -07004189 int i, j;
4190 unsigned long v;
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09004191
Al Viro871751e2006-03-25 03:06:39 -08004192 if (n[0] == n[1])
4193 return;
Joonsoo Kim8456a642013-10-24 10:07:49 +09004194 for (i = 0, p = page->s_mem; i < c->num; i++, p += c->size) {
Joonsoo Kimd31676d2016-03-15 14:54:24 -07004195 bool active = true;
4196
4197 for (j = page->active; j < c->num; j++) {
4198 if (get_free_obj(page, j) == i) {
4199 active = false;
4200 break;
4201 }
4202 }
4203
4204 if (!active)
Al Viro871751e2006-03-25 03:06:39 -08004205 continue;
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09004206
Joonsoo Kimd31676d2016-03-15 14:54:24 -07004207 /*
4208 * probe_kernel_read() is used for DEBUG_PAGEALLOC. page table
4209 * mapping is established when actual object allocation and
4210 * we could mistakenly access the unmapped object in the cpu
4211 * cache.
4212 */
4213 if (probe_kernel_read(&v, dbg_userword(c, p), sizeof(v)))
4214 continue;
4215
4216 if (!add_caller(n, v))
Al Viro871751e2006-03-25 03:06:39 -08004217 return;
4218 }
4219}
4220
4221static void show_symbol(struct seq_file *m, unsigned long address)
4222{
4223#ifdef CONFIG_KALLSYMS
Al Viro871751e2006-03-25 03:06:39 -08004224 unsigned long offset, size;
Tejun Heo9281ace2007-07-17 04:03:51 -07004225 char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
Al Viro871751e2006-03-25 03:06:39 -08004226
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004227 if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
Al Viro871751e2006-03-25 03:06:39 -08004228 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004229 if (modname[0])
Al Viro871751e2006-03-25 03:06:39 -08004230 seq_printf(m, " [%s]", modname);
4231 return;
4232 }
4233#endif
4234 seq_printf(m, "%p", (void *)address);
4235}
4236
4237static int leaks_show(struct seq_file *m, void *p)
4238{
Thierry Reding0672aa72012-06-22 19:42:49 +02004239 struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);
Joonsoo Kim8456a642013-10-24 10:07:49 +09004240 struct page *page;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004241 struct kmem_cache_node *n;
Al Viro871751e2006-03-25 03:06:39 -08004242 const char *name;
Christoph Lameterdb845062013-02-05 18:45:23 +00004243 unsigned long *x = m->private;
Al Viro871751e2006-03-25 03:06:39 -08004244 int node;
4245 int i;
4246
4247 if (!(cachep->flags & SLAB_STORE_USER))
4248 return 0;
4249 if (!(cachep->flags & SLAB_RED_ZONE))
4250 return 0;
4251
Joonsoo Kimd31676d2016-03-15 14:54:24 -07004252 /*
4253 * Set store_user_clean and start to grab stored user information
4254 * for all objects on this cache. If some alloc/free requests comes
4255 * during the processing, information would be wrong so restart
4256 * whole processing.
4257 */
4258 do {
4259 set_store_user_clean(cachep);
4260 drain_cpu_caches(cachep);
Al Viro871751e2006-03-25 03:06:39 -08004261
Joonsoo Kimd31676d2016-03-15 14:54:24 -07004262 x[1] = 0;
Al Viro871751e2006-03-25 03:06:39 -08004263
Joonsoo Kimd31676d2016-03-15 14:54:24 -07004264 for_each_kmem_cache_node(cachep, node, n) {
Al Viro871751e2006-03-25 03:06:39 -08004265
Joonsoo Kimd31676d2016-03-15 14:54:24 -07004266 check_irq_on();
4267 spin_lock_irq(&n->list_lock);
Al Viro871751e2006-03-25 03:06:39 -08004268
Joonsoo Kimd31676d2016-03-15 14:54:24 -07004269 list_for_each_entry(page, &n->slabs_full, lru)
4270 handle_slab(x, cachep, page);
4271 list_for_each_entry(page, &n->slabs_partial, lru)
4272 handle_slab(x, cachep, page);
4273 spin_unlock_irq(&n->list_lock);
4274 }
4275 } while (!is_store_user_clean(cachep));
4276
Al Viro871751e2006-03-25 03:06:39 -08004277 name = cachep->name;
Christoph Lameterdb845062013-02-05 18:45:23 +00004278 if (x[0] == x[1]) {
Al Viro871751e2006-03-25 03:06:39 -08004279 /* Increase the buffer size */
Christoph Lameter18004c52012-07-06 15:25:12 -05004280 mutex_unlock(&slab_mutex);
Christoph Lameterdb845062013-02-05 18:45:23 +00004281 m->private = kzalloc(x[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
Al Viro871751e2006-03-25 03:06:39 -08004282 if (!m->private) {
4283 /* Too bad, we are really out */
Christoph Lameterdb845062013-02-05 18:45:23 +00004284 m->private = x;
Christoph Lameter18004c52012-07-06 15:25:12 -05004285 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004286 return -ENOMEM;
4287 }
Christoph Lameterdb845062013-02-05 18:45:23 +00004288 *(unsigned long *)m->private = x[0] * 2;
4289 kfree(x);
Christoph Lameter18004c52012-07-06 15:25:12 -05004290 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004291 /* Now make sure this entry will be retried */
4292 m->count = m->size;
4293 return 0;
4294 }
Christoph Lameterdb845062013-02-05 18:45:23 +00004295 for (i = 0; i < x[1]; i++) {
4296 seq_printf(m, "%s: %lu ", name, x[2*i+3]);
4297 show_symbol(m, x[2*i+2]);
Al Viro871751e2006-03-25 03:06:39 -08004298 seq_putc(m, '\n');
4299 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004300
Al Viro871751e2006-03-25 03:06:39 -08004301 return 0;
4302}
4303
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004304static const struct seq_operations slabstats_op = {
Vladimir Davydov1df3b262014-12-10 15:42:16 -08004305 .start = slab_start,
Wanpeng Li276a2432013-07-08 08:08:28 +08004306 .next = slab_next,
4307 .stop = slab_stop,
Al Viro871751e2006-03-25 03:06:39 -08004308 .show = leaks_show,
4309};
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004310
4311static int slabstats_open(struct inode *inode, struct file *file)
4312{
Rob Jonesb208ce32014-10-09 15:28:03 -07004313 unsigned long *n;
4314
4315 n = __seq_open_private(file, &slabstats_op, PAGE_SIZE);
4316 if (!n)
4317 return -ENOMEM;
4318
4319 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
4320
4321 return 0;
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004322}
4323
4324static const struct file_operations proc_slabstats_operations = {
4325 .open = slabstats_open,
4326 .read = seq_read,
4327 .llseek = seq_lseek,
4328 .release = seq_release_private,
4329};
Al Viro871751e2006-03-25 03:06:39 -08004330#endif
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004331
4332static int __init slab_proc_init(void)
4333{
4334#ifdef CONFIG_DEBUG_SLAB_LEAK
4335 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
4336#endif
4337 return 0;
4338}
4339module_init(slab_proc_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340#endif
4341
Manfred Spraul00e145b2005-09-03 15:55:07 -07004342/**
4343 * ksize - get the actual amount of memory allocated for a given object
4344 * @objp: Pointer to the object
4345 *
4346 * kmalloc may internally round up allocations and return more memory
4347 * than requested. ksize() can be used to determine the actual amount of
4348 * memory allocated. The caller may use this additional memory, even though
4349 * a smaller amount of memory was initially specified with the kmalloc call.
4350 * The caller must guarantee that objp points to a valid object previously
4351 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4352 * must not be freed during the duration of the call.
4353 */
Pekka Enbergfd76bab2007-05-06 14:48:40 -07004354size_t ksize(const void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355{
Christoph Lameteref8b4522007-10-16 01:24:46 -07004356 BUG_ON(!objp);
4357 if (unlikely(objp == ZERO_SIZE_PTR))
Manfred Spraul00e145b2005-09-03 15:55:07 -07004358 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359
Christoph Lameter8c138bc2012-06-13 10:24:58 -05004360 return virt_to_cache(objp)->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361}
Kirill A. Shutemovb1aabec2009-02-10 15:21:44 +02004362EXPORT_SYMBOL(ksize);