blob: 8c4db214b05bce27df81fa876033b56225773ecd [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/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 * struct array_cache
173 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 * Purpose:
175 * - LIFO ordering, to hand out cache-warm objects from _alloc
176 * - reduce the number of linked list operations
177 * - reduce spinlock operations
178 *
179 * The limit is stored in the per-cpu structure to reduce the data cache
180 * footprint.
181 *
182 */
183struct array_cache {
184 unsigned int avail;
185 unsigned int limit;
186 unsigned int batchcount;
187 unsigned int touched;
Robert P. J. Daybda5b652007-10-16 23:30:05 -0700188 void *entry[]; /*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800189 * Must have this definition in here for the proper
190 * alignment of array_cache. Also simplifies accessing
191 * the entries.
Andrew Mortona737b3e2006-03-22 00:08:11 -0800192 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193};
194
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700195struct alien_cache {
196 spinlock_t lock;
197 struct array_cache ac;
198};
199
Andrew Mortona737b3e2006-03-22 00:08:11 -0800200/*
Christoph Lametere498be72005-09-09 13:03:32 -0700201 * Need this for bootstrapping a per node allocator.
202 */
Joonsoo Kimbf0dea22014-10-09 15:26:27 -0700203#define NUM_INIT_LISTS (2 * MAX_NUMNODES)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000204static struct kmem_cache_node __initdata init_kmem_cache_node[NUM_INIT_LISTS];
Christoph Lametere498be72005-09-09 13:03:32 -0700205#define CACHE_CACHE 0
Joonsoo Kimbf0dea22014-10-09 15:26:27 -0700206#define SIZE_NODE (MAX_NUMNODES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Christoph Lametered11d9e2006-06-30 01:55:45 -0700208static int drain_freelist(struct kmem_cache *cache,
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000209 struct kmem_cache_node *n, int tofree);
Christoph Lametered11d9e2006-06-30 01:55:45 -0700210static void free_block(struct kmem_cache *cachep, void **objpp, int len,
Joonsoo Kim97654df2014-08-06 16:04:25 -0700211 int node, struct list_head *list);
212static void slabs_destroy(struct kmem_cache *cachep, struct list_head *list);
Pekka Enberg83b519e2009-06-10 19:40:04 +0300213static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp);
David Howells65f27f32006-11-22 14:55:48 +0000214static void cache_reap(struct work_struct *unused);
Christoph Lametered11d9e2006-06-30 01:55:45 -0700215
Joonsoo Kim76b342b2016-05-19 17:10:26 -0700216static inline void fixup_objfreelist_debug(struct kmem_cache *cachep,
217 void **list);
218static inline void fixup_slab_list(struct kmem_cache *cachep,
219 struct kmem_cache_node *n, struct page *page,
220 void **list);
Ingo Molnare0a42722006-06-23 02:03:46 -0700221static int slab_early_init = 1;
222
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000223#define INDEX_NODE kmalloc_index(sizeof(struct kmem_cache_node))
Christoph Lametere498be72005-09-09 13:03:32 -0700224
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000225static void kmem_cache_node_init(struct kmem_cache_node *parent)
Christoph Lametere498be72005-09-09 13:03:32 -0700226{
227 INIT_LIST_HEAD(&parent->slabs_full);
228 INIT_LIST_HEAD(&parent->slabs_partial);
229 INIT_LIST_HEAD(&parent->slabs_free);
230 parent->shared = NULL;
231 parent->alien = NULL;
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -0800232 parent->colour_next = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700233 spin_lock_init(&parent->list_lock);
234 parent->free_objects = 0;
235 parent->free_touched = 0;
236}
237
Andrew Mortona737b3e2006-03-22 00:08:11 -0800238#define MAKE_LIST(cachep, listp, slab, nodeid) \
239 do { \
240 INIT_LIST_HEAD(listp); \
Christoph Lameter18bf8542014-08-06 16:04:11 -0700241 list_splice(&get_node(cachep, nodeid)->slab, listp); \
Christoph Lametere498be72005-09-09 13:03:32 -0700242 } while (0)
243
Andrew Mortona737b3e2006-03-22 00:08:11 -0800244#define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
245 do { \
Christoph Lametere498be72005-09-09 13:03:32 -0700246 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
247 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
248 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
249 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Joonsoo Kimb03a017b2016-03-15 14:54:50 -0700251#define CFLGS_OBJFREELIST_SLAB (0x40000000UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252#define CFLGS_OFF_SLAB (0x80000000UL)
Joonsoo Kimb03a017b2016-03-15 14:54:50 -0700253#define OBJFREELIST_SLAB(x) ((x)->flags & CFLGS_OBJFREELIST_SLAB)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254#define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
255
256#define BATCHREFILL_LIMIT 16
Andrew Mortona737b3e2006-03-22 00:08:11 -0800257/*
258 * Optimization question: fewer reaps means less probability for unnessary
259 * cpucache drain/refill cycles.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 *
Adrian Bunkdc6f3f22005-11-08 16:44:08 +0100261 * OTOH the cpuarrays can contain lots of objects,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 * which could lock up otherwise freeable slabs.
263 */
Jianyu Zhan5f0985b2014-03-30 17:02:20 +0800264#define REAPTIMEOUT_AC (2*HZ)
265#define REAPTIMEOUT_NODE (4*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267#if STATS
268#define STATS_INC_ACTIVE(x) ((x)->num_active++)
269#define STATS_DEC_ACTIVE(x) ((x)->num_active--)
270#define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
271#define STATS_INC_GROWN(x) ((x)->grown++)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700272#define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
Andrew Mortona737b3e2006-03-22 00:08:11 -0800273#define STATS_SET_HIGH(x) \
274 do { \
275 if ((x)->num_active > (x)->high_mark) \
276 (x)->high_mark = (x)->num_active; \
277 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278#define STATS_INC_ERR(x) ((x)->errors++)
279#define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
Christoph Lametere498be72005-09-09 13:03:32 -0700280#define STATS_INC_NODEFREES(x) ((x)->node_frees++)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700281#define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800282#define STATS_SET_FREEABLE(x, i) \
283 do { \
284 if ((x)->max_freeable < i) \
285 (x)->max_freeable = i; \
286 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287#define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
288#define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
289#define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
290#define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
291#else
292#define STATS_INC_ACTIVE(x) do { } while (0)
293#define STATS_DEC_ACTIVE(x) do { } while (0)
294#define STATS_INC_ALLOCED(x) do { } while (0)
295#define STATS_INC_GROWN(x) do { } while (0)
Andi Kleen4e60c862010-08-09 17:19:03 -0700296#define STATS_ADD_REAPED(x,y) do { (void)(y); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297#define STATS_SET_HIGH(x) do { } while (0)
298#define STATS_INC_ERR(x) do { } while (0)
299#define STATS_INC_NODEALLOCS(x) do { } while (0)
Christoph Lametere498be72005-09-09 13:03:32 -0700300#define STATS_INC_NODEFREES(x) do { } while (0)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700301#define STATS_INC_ACOVERFLOW(x) do { } while (0)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800302#define STATS_SET_FREEABLE(x, i) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303#define STATS_INC_ALLOCHIT(x) do { } while (0)
304#define STATS_INC_ALLOCMISS(x) do { } while (0)
305#define STATS_INC_FREEHIT(x) do { } while (0)
306#define STATS_INC_FREEMISS(x) do { } while (0)
307#endif
308
309#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Andrew Mortona737b3e2006-03-22 00:08:11 -0800311/*
312 * memory layout of objects:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * 0 : objp
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800314 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 * the end of an object is aligned with the end of the real
316 * allocation. Catches writes behind the end of the allocation.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800317 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 * redzone word.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800319 * cachep->obj_offset: The real object.
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500320 * cachep->size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
321 * cachep->size - 1* BYTES_PER_WORD: last caller address
Andrew Mortona737b3e2006-03-22 00:08:11 -0800322 * [BYTES_PER_WORD long]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800324static int obj_offset(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800326 return cachep->obj_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327}
328
David Woodhouseb46b8f12007-05-08 00:22:59 -0700329static unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
David Woodhouseb46b8f12007-05-08 00:22:59 -0700332 return (unsigned long long*) (objp + obj_offset(cachep) -
333 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335
David Woodhouseb46b8f12007-05-08 00:22:59 -0700336static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
338 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
339 if (cachep->flags & SLAB_STORE_USER)
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500340 return (unsigned long long *)(objp + cachep->size -
David Woodhouseb46b8f12007-05-08 00:22:59 -0700341 sizeof(unsigned long long) -
David Woodhouse87a927c2007-07-04 21:26:44 -0400342 REDZONE_ALIGN);
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500343 return (unsigned long long *) (objp + cachep->size -
David Woodhouseb46b8f12007-05-08 00:22:59 -0700344 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345}
346
Pekka Enberg343e0d72006-02-01 03:05:50 -0800347static void **dbg_userword(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 BUG_ON(!(cachep->flags & SLAB_STORE_USER));
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500350 return (void **)(objp + cachep->size - BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353#else
354
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800355#define obj_offset(x) 0
David Woodhouseb46b8f12007-05-08 00:22:59 -0700356#define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
357#define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358#define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
359
360#endif
361
Joonsoo Kim03787302014-06-23 13:22:06 -0700362#ifdef CONFIG_DEBUG_SLAB_LEAK
363
Joonsoo Kimd31676d2016-03-15 14:54:24 -0700364static inline bool is_store_user_clean(struct kmem_cache *cachep)
Joonsoo Kim03787302014-06-23 13:22:06 -0700365{
Joonsoo Kimd31676d2016-03-15 14:54:24 -0700366 return atomic_read(&cachep->store_user_clean) == 1;
367}
Joonsoo Kim03787302014-06-23 13:22:06 -0700368
Joonsoo Kimd31676d2016-03-15 14:54:24 -0700369static inline void set_store_user_clean(struct kmem_cache *cachep)
370{
371 atomic_set(&cachep->store_user_clean, 1);
372}
Joonsoo Kim03787302014-06-23 13:22:06 -0700373
Joonsoo Kimd31676d2016-03-15 14:54:24 -0700374static inline void set_store_user_dirty(struct kmem_cache *cachep)
375{
376 if (is_store_user_clean(cachep))
377 atomic_set(&cachep->store_user_clean, 0);
Joonsoo Kim03787302014-06-23 13:22:06 -0700378}
379
380#else
Joonsoo Kimd31676d2016-03-15 14:54:24 -0700381static inline void set_store_user_dirty(struct kmem_cache *cachep) {}
Joonsoo Kim03787302014-06-23 13:22:06 -0700382
383#endif
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/*
David Rientjes3df1ccc2011-10-18 22:09:28 -0700386 * Do not go above this order unless 0 objects fit into the slab or
387 * overridden on the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 */
David Rientjes543585c2011-10-18 22:09:24 -0700389#define SLAB_MAX_ORDER_HI 1
390#define SLAB_MAX_ORDER_LO 0
391static int slab_max_order = SLAB_MAX_ORDER_LO;
David Rientjes3df1ccc2011-10-18 22:09:28 -0700392static bool slab_max_order_set __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800394static inline struct kmem_cache *virt_to_cache(const void *obj)
395{
Christoph Lameterb49af682007-05-06 14:49:41 -0700396 struct page *page = virt_to_head_page(obj);
Christoph Lameter35026082012-06-13 10:24:56 -0500397 return page->slab_cache;
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800398}
399
Joonsoo Kim8456a642013-10-24 10:07:49 +0900400static inline void *index_to_obj(struct kmem_cache *cache, struct page *page,
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800401 unsigned int idx)
402{
Joonsoo Kim8456a642013-10-24 10:07:49 +0900403 return page->s_mem + cache->size * idx;
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800404}
405
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800406/*
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500407 * We want to avoid an expensive divide : (offset / cache->size)
408 * Using the fact that size is a constant for a particular cache,
409 * we can replace (offset / cache->size) by
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800410 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
411 */
412static inline unsigned int obj_to_index(const struct kmem_cache *cache,
Joonsoo Kim8456a642013-10-24 10:07:49 +0900413 const struct page *page, void *obj)
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800414{
Joonsoo Kim8456a642013-10-24 10:07:49 +0900415 u32 offset = (obj - page->s_mem);
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800416 return reciprocal_divide(offset, cache->reciprocal_buffer_size);
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800417}
418
Joonsoo Kim6fb92432016-03-15 14:54:09 -0700419#define BOOT_CPUCACHE_ENTRIES 1
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/* internal cache of cache description objs */
Christoph Lameter9b030cb2012-09-05 00:20:33 +0000421static struct kmem_cache kmem_cache_boot = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800422 .batchcount = 1,
423 .limit = BOOT_CPUCACHE_ENTRIES,
424 .shared = 1,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500425 .size = sizeof(struct kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800426 .name = "kmem_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427};
428
Tejun Heo1871e522009-10-29 22:34:13 +0900429static DEFINE_PER_CPU(struct delayed_work, slab_reap_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Pekka Enberg343e0d72006-02-01 03:05:50 -0800431static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432{
Joonsoo Kimbf0dea22014-10-09 15:26:27 -0700433 return this_cpu_ptr(cachep->cpu_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Andrew Mortona737b3e2006-03-22 00:08:11 -0800436/*
437 * Calculate the number of objects and left-over bytes for a given buffer size.
438 */
Joonsoo Kim70f75062016-03-15 14:54:53 -0700439static unsigned int cache_estimate(unsigned long gfporder, size_t buffer_size,
440 unsigned long flags, size_t *left_over)
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800441{
Joonsoo Kim70f75062016-03-15 14:54:53 -0700442 unsigned int num;
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800443 size_t slab_size = PAGE_SIZE << gfporder;
444
445 /*
446 * The slab management structure can be either off the slab or
447 * on it. For the latter case, the memory allocated for a
448 * slab is used for:
449 *
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800450 * - @buffer_size bytes for each object
Joonsoo Kim2e6b3602016-03-15 14:54:30 -0700451 * - One freelist_idx_t for each object
452 *
453 * We don't need to consider alignment of freelist because
454 * freelist will be at the end of slab page. The objects will be
455 * at the correct alignment.
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800456 *
457 * If the slab management structure is off the slab, then the
458 * alignment will already be calculated into the size. Because
459 * the slabs are all pages aligned, the objects will be at the
460 * correct alignment when allocated.
461 */
Joonsoo Kimb03a017b2016-03-15 14:54:50 -0700462 if (flags & (CFLGS_OBJFREELIST_SLAB | CFLGS_OFF_SLAB)) {
Joonsoo Kim70f75062016-03-15 14:54:53 -0700463 num = slab_size / buffer_size;
Joonsoo Kim2e6b3602016-03-15 14:54:30 -0700464 *left_over = slab_size % buffer_size;
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800465 } else {
Joonsoo Kim70f75062016-03-15 14:54:53 -0700466 num = slab_size / (buffer_size + sizeof(freelist_idx_t));
Joonsoo Kim2e6b3602016-03-15 14:54:30 -0700467 *left_over = slab_size %
468 (buffer_size + sizeof(freelist_idx_t));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 }
Joonsoo Kim70f75062016-03-15 14:54:53 -0700470
471 return num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472}
473
Christoph Lameterf28510d2012-09-11 19:49:38 +0000474#if DEBUG
Harvey Harrisond40cee22008-04-30 00:55:07 -0700475#define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Andrew Mortona737b3e2006-03-22 00:08:11 -0800477static void __slab_error(const char *function, struct kmem_cache *cachep,
478 char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
Joe Perches11705322016-03-17 14:19:50 -0700480 pr_err("slab error in %s(): cache `%s': %s\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800481 function, cachep->name, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 dump_stack();
Rusty Russell373d4d02013-01-21 17:17:39 +1030483 add_taint(TAINT_BAD_PAGE, LOCKDEP_NOW_UNRELIABLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484}
Christoph Lameterf28510d2012-09-11 19:49:38 +0000485#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Paul Menage3395ee02006-12-06 20:32:16 -0800487/*
488 * By default on NUMA we use alien caches to stage the freeing of
489 * objects allocated from other nodes. This causes massive memory
490 * inefficiencies when using fake NUMA setup to split memory into a
491 * large number of small nodes, so it can be disabled on the command
492 * line
493 */
494
495static int use_alien_caches __read_mostly = 1;
496static int __init noaliencache_setup(char *s)
497{
498 use_alien_caches = 0;
499 return 1;
500}
501__setup("noaliencache", noaliencache_setup);
502
David Rientjes3df1ccc2011-10-18 22:09:28 -0700503static int __init slab_max_order_setup(char *str)
504{
505 get_option(&str, &slab_max_order);
506 slab_max_order = slab_max_order < 0 ? 0 :
507 min(slab_max_order, MAX_ORDER - 1);
508 slab_max_order_set = true;
509
510 return 1;
511}
512__setup("slab_max_order=", slab_max_order_setup);
513
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800514#ifdef CONFIG_NUMA
515/*
516 * Special reaping functions for NUMA systems called from cache_reap().
517 * These take care of doing round robin flushing of alien caches (containing
518 * objects freed on different nodes from which they were allocated) and the
519 * flushing of remote pcps by calling drain_node_pages.
520 */
Tejun Heo1871e522009-10-29 22:34:13 +0900521static DEFINE_PER_CPU(unsigned long, slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800522
523static void init_reap_node(int cpu)
524{
525 int node;
526
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -0700527 node = next_node(cpu_to_mem(cpu), node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800528 if (node == MAX_NUMNODES)
Paul Jackson442295c2006-03-22 00:09:11 -0800529 node = first_node(node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800530
Tejun Heo1871e522009-10-29 22:34:13 +0900531 per_cpu(slab_reap_node, cpu) = node;
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800532}
533
534static void next_reap_node(void)
535{
Christoph Lameter909ea962010-12-08 16:22:55 +0100536 int node = __this_cpu_read(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800537
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800538 node = next_node(node, node_online_map);
539 if (unlikely(node >= MAX_NUMNODES))
540 node = first_node(node_online_map);
Christoph Lameter909ea962010-12-08 16:22:55 +0100541 __this_cpu_write(slab_reap_node, node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800542}
543
544#else
545#define init_reap_node(cpu) do { } while (0)
546#define next_reap_node(void) do { } while (0)
547#endif
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549/*
550 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
551 * via the workqueue/eventd.
552 * Add the CPU number into the expiration time to minimize the possibility of
553 * the CPUs getting into lockstep and contending for the global cache chain
554 * lock.
555 */
Paul Gortmaker0db06282013-06-19 14:53:51 -0400556static void start_cpu_timer(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Tejun Heo1871e522009-10-29 22:34:13 +0900558 struct delayed_work *reap_work = &per_cpu(slab_reap_work, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 /*
561 * When this gets called from do_initcalls via cpucache_init(),
562 * init_workqueues() has already run, so keventd will be setup
563 * at that time.
564 */
David Howells52bad642006-11-22 14:54:01 +0000565 if (keventd_up() && reap_work->work.func == NULL) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800566 init_reap_node(cpu);
Tejun Heo203b42f2012-08-21 13:18:23 -0700567 INIT_DEFERRABLE_WORK(reap_work, cache_reap);
Arjan van de Ven2b284212006-12-10 02:21:28 -0800568 schedule_delayed_work_on(cpu, reap_work,
569 __round_jiffies_relative(HZ, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 }
571}
572
Joonsoo Kim1fe00d52014-08-06 16:04:27 -0700573static void init_arraycache(struct array_cache *ac, int limit, int batch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
Catalin Marinasd5cff632009-06-11 13:22:40 +0100575 /*
576 * The array_cache structures contain pointers to free object.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300577 * However, when such objects are allocated or transferred to another
Catalin Marinasd5cff632009-06-11 13:22:40 +0100578 * cache the pointers are not cleared and they could be counted as
579 * valid references during a kmemleak scan. Therefore, kmemleak must
580 * not scan such objects.
581 */
Joonsoo Kim1fe00d52014-08-06 16:04:27 -0700582 kmemleak_no_scan(ac);
583 if (ac) {
584 ac->avail = 0;
585 ac->limit = limit;
586 ac->batchcount = batch;
587 ac->touched = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
Joonsoo Kim1fe00d52014-08-06 16:04:27 -0700589}
590
591static struct array_cache *alloc_arraycache(int node, int entries,
592 int batchcount, gfp_t gfp)
593{
Joonsoo Kim5e804782014-08-06 16:04:40 -0700594 size_t memsize = sizeof(void *) * entries + sizeof(struct array_cache);
Joonsoo Kim1fe00d52014-08-06 16:04:27 -0700595 struct array_cache *ac = NULL;
596
597 ac = kmalloc_node(memsize, gfp, node);
598 init_arraycache(ac, entries, batchcount);
599 return ac;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600}
601
Joonsoo Kimf68f8dd2016-03-15 14:54:56 -0700602static noinline void cache_free_pfmemalloc(struct kmem_cache *cachep,
603 struct page *page, void *objp)
Mel Gorman072bb0a2012-07-31 16:43:58 -0700604{
Joonsoo Kimf68f8dd2016-03-15 14:54:56 -0700605 struct kmem_cache_node *n;
606 int page_node;
607 LIST_HEAD(list);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700608
Joonsoo Kimf68f8dd2016-03-15 14:54:56 -0700609 page_node = page_to_nid(page);
610 n = get_node(cachep, page_node);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700611
Joonsoo Kimf68f8dd2016-03-15 14:54:56 -0700612 spin_lock(&n->list_lock);
613 free_block(cachep, &objp, 1, page_node, &list);
614 spin_unlock(&n->list_lock);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700615
Joonsoo Kimf68f8dd2016-03-15 14:54:56 -0700616 slabs_destroy(cachep, &list);
Mel Gorman072bb0a2012-07-31 16:43:58 -0700617}
618
Christoph Lameter3ded1752006-03-25 03:06:44 -0800619/*
620 * Transfer objects in one arraycache to another.
621 * Locking must be handled by the caller.
622 *
623 * Return the number of entries transferred.
624 */
625static int transfer_objects(struct array_cache *to,
626 struct array_cache *from, unsigned int max)
627{
628 /* Figure out how many entries to transfer */
Hagen Paul Pfeifer732eacc2010-10-26 14:22:23 -0700629 int nr = min3(from->avail, max, to->limit - to->avail);
Christoph Lameter3ded1752006-03-25 03:06:44 -0800630
631 if (!nr)
632 return 0;
633
634 memcpy(to->entry + to->avail, from->entry + from->avail -nr,
635 sizeof(void *) *nr);
636
637 from->avail -= nr;
638 to->avail += nr;
Christoph Lameter3ded1752006-03-25 03:06:44 -0800639 return nr;
640}
641
Christoph Lameter765c4502006-09-27 01:50:08 -0700642#ifndef CONFIG_NUMA
643
644#define drain_alien_cache(cachep, alien) do { } while (0)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000645#define reap_alien(cachep, n) do { } while (0)
Christoph Lameter765c4502006-09-27 01:50:08 -0700646
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700647static inline struct alien_cache **alloc_alien_cache(int node,
648 int limit, gfp_t gfp)
Christoph Lameter765c4502006-09-27 01:50:08 -0700649{
Joonsoo Kim88881772016-05-19 17:10:05 -0700650 return NULL;
Christoph Lameter765c4502006-09-27 01:50:08 -0700651}
652
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700653static inline void free_alien_cache(struct alien_cache **ac_ptr)
Christoph Lameter765c4502006-09-27 01:50:08 -0700654{
655}
656
657static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
658{
659 return 0;
660}
661
662static inline void *alternate_node_alloc(struct kmem_cache *cachep,
663 gfp_t flags)
664{
665 return NULL;
666}
667
Christoph Hellwig8b98c162006-12-06 20:32:30 -0800668static inline void *____cache_alloc_node(struct kmem_cache *cachep,
Christoph Lameter765c4502006-09-27 01:50:08 -0700669 gfp_t flags, int nodeid)
670{
671 return NULL;
672}
673
David Rientjes4167e9b2015-04-14 15:46:55 -0700674static inline gfp_t gfp_exact_node(gfp_t flags)
675{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700676 return flags & ~__GFP_NOFAIL;
David Rientjes4167e9b2015-04-14 15:46:55 -0700677}
678
Christoph Lameter765c4502006-09-27 01:50:08 -0700679#else /* CONFIG_NUMA */
680
Christoph Hellwig8b98c162006-12-06 20:32:30 -0800681static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
Paul Jacksonc61afb12006-03-24 03:16:08 -0800682static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
Christoph Lameterdc85da12006-01-18 17:42:36 -0800683
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700684static struct alien_cache *__alloc_alien_cache(int node, int entries,
685 int batch, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -0700686{
Joonsoo Kim5e804782014-08-06 16:04:40 -0700687 size_t memsize = sizeof(void *) * entries + sizeof(struct alien_cache);
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700688 struct alien_cache *alc = NULL;
689
690 alc = kmalloc_node(memsize, gfp, node);
691 init_arraycache(&alc->ac, entries, batch);
Joonsoo Kim49dfc302014-08-06 16:04:31 -0700692 spin_lock_init(&alc->lock);
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700693 return alc;
694}
695
696static struct alien_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
697{
698 struct alien_cache **alc_ptr;
Joonsoo Kim5e804782014-08-06 16:04:40 -0700699 size_t memsize = sizeof(void *) * nr_node_ids;
Christoph Lametere498be72005-09-09 13:03:32 -0700700 int i;
701
702 if (limit > 1)
703 limit = 12;
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700704 alc_ptr = kzalloc_node(memsize, gfp, node);
705 if (!alc_ptr)
706 return NULL;
707
708 for_each_node(i) {
709 if (i == node || !node_online(i))
710 continue;
711 alc_ptr[i] = __alloc_alien_cache(node, limit, 0xbaadf00d, gfp);
712 if (!alc_ptr[i]) {
713 for (i--; i >= 0; i--)
714 kfree(alc_ptr[i]);
715 kfree(alc_ptr);
716 return NULL;
Christoph Lametere498be72005-09-09 13:03:32 -0700717 }
718 }
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700719 return alc_ptr;
Christoph Lametere498be72005-09-09 13:03:32 -0700720}
721
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700722static void free_alien_cache(struct alien_cache **alc_ptr)
Christoph Lametere498be72005-09-09 13:03:32 -0700723{
724 int i;
725
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700726 if (!alc_ptr)
Christoph Lametere498be72005-09-09 13:03:32 -0700727 return;
Christoph Lametere498be72005-09-09 13:03:32 -0700728 for_each_node(i)
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700729 kfree(alc_ptr[i]);
730 kfree(alc_ptr);
Christoph Lametere498be72005-09-09 13:03:32 -0700731}
732
Pekka Enberg343e0d72006-02-01 03:05:50 -0800733static void __drain_alien_cache(struct kmem_cache *cachep,
Joonsoo Kim833b7062014-08-06 16:04:33 -0700734 struct array_cache *ac, int node,
735 struct list_head *list)
Christoph Lametere498be72005-09-09 13:03:32 -0700736{
Christoph Lameter18bf8542014-08-06 16:04:11 -0700737 struct kmem_cache_node *n = get_node(cachep, node);
Christoph Lametere498be72005-09-09 13:03:32 -0700738
739 if (ac->avail) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000740 spin_lock(&n->list_lock);
Christoph Lametere00946f2006-03-25 03:06:45 -0800741 /*
742 * Stuff objects into the remote nodes shared array first.
743 * That way we could avoid the overhead of putting the objects
744 * into the free lists and getting them back later.
745 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000746 if (n->shared)
747 transfer_objects(n->shared, ac, ac->limit);
Christoph Lametere00946f2006-03-25 03:06:45 -0800748
Joonsoo Kim833b7062014-08-06 16:04:33 -0700749 free_block(cachep, ac->entry, ac->avail, node, list);
Christoph Lametere498be72005-09-09 13:03:32 -0700750 ac->avail = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000751 spin_unlock(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -0700752 }
753}
754
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800755/*
756 * Called from cache_reap() to regularly drain alien caches round robin.
757 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000758static void reap_alien(struct kmem_cache *cachep, struct kmem_cache_node *n)
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800759{
Christoph Lameter909ea962010-12-08 16:22:55 +0100760 int node = __this_cpu_read(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800761
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000762 if (n->alien) {
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700763 struct alien_cache *alc = n->alien[node];
764 struct array_cache *ac;
Christoph Lametere00946f2006-03-25 03:06:45 -0800765
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700766 if (alc) {
767 ac = &alc->ac;
Joonsoo Kim49dfc302014-08-06 16:04:31 -0700768 if (ac->avail && spin_trylock_irq(&alc->lock)) {
Joonsoo Kim833b7062014-08-06 16:04:33 -0700769 LIST_HEAD(list);
770
771 __drain_alien_cache(cachep, ac, node, &list);
Joonsoo Kim49dfc302014-08-06 16:04:31 -0700772 spin_unlock_irq(&alc->lock);
Joonsoo Kim833b7062014-08-06 16:04:33 -0700773 slabs_destroy(cachep, &list);
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700774 }
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800775 }
776 }
777}
778
Andrew Mortona737b3e2006-03-22 00:08:11 -0800779static void drain_alien_cache(struct kmem_cache *cachep,
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700780 struct alien_cache **alien)
Christoph Lametere498be72005-09-09 13:03:32 -0700781{
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800782 int i = 0;
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700783 struct alien_cache *alc;
Christoph Lametere498be72005-09-09 13:03:32 -0700784 struct array_cache *ac;
785 unsigned long flags;
786
787 for_each_online_node(i) {
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700788 alc = alien[i];
789 if (alc) {
Joonsoo Kim833b7062014-08-06 16:04:33 -0700790 LIST_HEAD(list);
791
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700792 ac = &alc->ac;
Joonsoo Kim49dfc302014-08-06 16:04:31 -0700793 spin_lock_irqsave(&alc->lock, flags);
Joonsoo Kim833b7062014-08-06 16:04:33 -0700794 __drain_alien_cache(cachep, ac, i, &list);
Joonsoo Kim49dfc302014-08-06 16:04:31 -0700795 spin_unlock_irqrestore(&alc->lock, flags);
Joonsoo Kim833b7062014-08-06 16:04:33 -0700796 slabs_destroy(cachep, &list);
Christoph Lametere498be72005-09-09 13:03:32 -0700797 }
798 }
799}
Pekka Enberg729bd0b2006-06-23 02:03:05 -0700800
Joonsoo Kim25c4f302014-10-09 15:26:09 -0700801static int __cache_free_alien(struct kmem_cache *cachep, void *objp,
802 int node, int page_node)
Pekka Enberg729bd0b2006-06-23 02:03:05 -0700803{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000804 struct kmem_cache_node *n;
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700805 struct alien_cache *alien = NULL;
806 struct array_cache *ac;
Joonsoo Kim97654df2014-08-06 16:04:25 -0700807 LIST_HEAD(list);
Pekka Enberg1ca4cb22006-10-06 00:43:52 -0700808
Christoph Lameter18bf8542014-08-06 16:04:11 -0700809 n = get_node(cachep, node);
Pekka Enberg729bd0b2006-06-23 02:03:05 -0700810 STATS_INC_NODEFREES(cachep);
Joonsoo Kim25c4f302014-10-09 15:26:09 -0700811 if (n->alien && n->alien[page_node]) {
812 alien = n->alien[page_node];
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700813 ac = &alien->ac;
Joonsoo Kim49dfc302014-08-06 16:04:31 -0700814 spin_lock(&alien->lock);
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700815 if (unlikely(ac->avail == ac->limit)) {
Pekka Enberg729bd0b2006-06-23 02:03:05 -0700816 STATS_INC_ACOVERFLOW(cachep);
Joonsoo Kim25c4f302014-10-09 15:26:09 -0700817 __drain_alien_cache(cachep, ac, page_node, &list);
Pekka Enberg729bd0b2006-06-23 02:03:05 -0700818 }
Joonsoo Kimf68f8dd2016-03-15 14:54:56 -0700819 ac->entry[ac->avail++] = objp;
Joonsoo Kim49dfc302014-08-06 16:04:31 -0700820 spin_unlock(&alien->lock);
Joonsoo Kim833b7062014-08-06 16:04:33 -0700821 slabs_destroy(cachep, &list);
Pekka Enberg729bd0b2006-06-23 02:03:05 -0700822 } else {
Joonsoo Kim25c4f302014-10-09 15:26:09 -0700823 n = get_node(cachep, page_node);
Christoph Lameter18bf8542014-08-06 16:04:11 -0700824 spin_lock(&n->list_lock);
Joonsoo Kim25c4f302014-10-09 15:26:09 -0700825 free_block(cachep, &objp, 1, page_node, &list);
Christoph Lameter18bf8542014-08-06 16:04:11 -0700826 spin_unlock(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -0700827 slabs_destroy(cachep, &list);
Pekka Enberg729bd0b2006-06-23 02:03:05 -0700828 }
829 return 1;
830}
Joonsoo Kim25c4f302014-10-09 15:26:09 -0700831
832static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
833{
834 int page_node = page_to_nid(virt_to_page(objp));
835 int node = numa_mem_id();
836 /*
837 * Make sure we are not freeing a object from another node to the array
838 * cache on this cpu.
839 */
840 if (likely(node == page_node))
841 return 0;
842
843 return __cache_free_alien(cachep, objp, node, page_node);
844}
David Rientjes4167e9b2015-04-14 15:46:55 -0700845
846/*
Mel Gorman444eb2a42016-03-17 14:19:23 -0700847 * Construct gfp mask to allocate from a specific node but do not reclaim or
848 * warn about failures.
David Rientjes4167e9b2015-04-14 15:46:55 -0700849 */
850static inline gfp_t gfp_exact_node(gfp_t flags)
851{
Mel Gorman444eb2a42016-03-17 14:19:23 -0700852 return (flags | __GFP_THISNODE | __GFP_NOWARN) & ~(__GFP_RECLAIM|__GFP_NOFAIL);
David Rientjes4167e9b2015-04-14 15:46:55 -0700853}
Christoph Lametere498be72005-09-09 13:03:32 -0700854#endif
855
Joonsoo Kimded0ecf2016-05-19 17:10:11 -0700856static int init_cache_node(struct kmem_cache *cachep, int node, gfp_t gfp)
857{
858 struct kmem_cache_node *n;
859
860 /*
861 * Set up the kmem_cache_node for cpu before we can
862 * begin anything. Make sure some other cpu on this
863 * node has not already allocated this
864 */
865 n = get_node(cachep, node);
866 if (n) {
867 spin_lock_irq(&n->list_lock);
868 n->free_limit = (1 + nr_cpus_node(node)) * cachep->batchcount +
869 cachep->num;
870 spin_unlock_irq(&n->list_lock);
871
872 return 0;
873 }
874
875 n = kmalloc_node(sizeof(struct kmem_cache_node), gfp, node);
876 if (!n)
877 return -ENOMEM;
878
879 kmem_cache_node_init(n);
880 n->next_reap = jiffies + REAPTIMEOUT_NODE +
881 ((unsigned long)cachep) % REAPTIMEOUT_NODE;
882
883 n->free_limit =
884 (1 + nr_cpus_node(node)) * cachep->batchcount + cachep->num;
885
886 /*
887 * The kmem_cache_nodes don't come and go as CPUs
888 * come and go. slab_mutex is sufficient
889 * protection here.
890 */
891 cachep->node[node] = n;
892
893 return 0;
894}
895
David Rientjes8f9f8d92010-03-27 19:40:47 -0700896/*
Christoph Lameter6a673682013-01-10 19:14:19 +0000897 * Allocates and initializes node for a node on each slab cache, used for
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000898 * either memory or cpu hotplug. If memory is being hot-added, the kmem_cache_node
David Rientjes8f9f8d92010-03-27 19:40:47 -0700899 * will be allocated off-node since memory is not yet online for the new node.
Christoph Lameter6a673682013-01-10 19:14:19 +0000900 * When hotplugging memory or a cpu, existing node are not replaced if
David Rientjes8f9f8d92010-03-27 19:40:47 -0700901 * already in use.
902 *
Christoph Lameter18004c52012-07-06 15:25:12 -0500903 * Must hold slab_mutex.
David Rientjes8f9f8d92010-03-27 19:40:47 -0700904 */
Christoph Lameter6a673682013-01-10 19:14:19 +0000905static int init_cache_node_node(int node)
David Rientjes8f9f8d92010-03-27 19:40:47 -0700906{
Joonsoo Kimded0ecf2016-05-19 17:10:11 -0700907 int ret;
David Rientjes8f9f8d92010-03-27 19:40:47 -0700908 struct kmem_cache *cachep;
David Rientjes8f9f8d92010-03-27 19:40:47 -0700909
Christoph Lameter18004c52012-07-06 15:25:12 -0500910 list_for_each_entry(cachep, &slab_caches, list) {
Joonsoo Kimded0ecf2016-05-19 17:10:11 -0700911 ret = init_cache_node(cachep, node, GFP_KERNEL);
912 if (ret)
913 return ret;
David Rientjes8f9f8d92010-03-27 19:40:47 -0700914 }
Joonsoo Kimded0ecf2016-05-19 17:10:11 -0700915
David Rientjes8f9f8d92010-03-27 19:40:47 -0700916 return 0;
917}
918
Joonsoo Kimc3d332b2016-05-19 17:10:14 -0700919static int setup_kmem_cache_node(struct kmem_cache *cachep,
920 int node, gfp_t gfp, bool force_change)
921{
922 int ret = -ENOMEM;
923 struct kmem_cache_node *n;
924 struct array_cache *old_shared = NULL;
925 struct array_cache *new_shared = NULL;
926 struct alien_cache **new_alien = NULL;
927 LIST_HEAD(list);
928
929 if (use_alien_caches) {
930 new_alien = alloc_alien_cache(node, cachep->limit, gfp);
931 if (!new_alien)
932 goto fail;
933 }
934
935 if (cachep->shared) {
936 new_shared = alloc_arraycache(node,
937 cachep->shared * cachep->batchcount, 0xbaadf00d, gfp);
938 if (!new_shared)
939 goto fail;
940 }
941
942 ret = init_cache_node(cachep, node, gfp);
943 if (ret)
944 goto fail;
945
946 n = get_node(cachep, node);
947 spin_lock_irq(&n->list_lock);
948 if (n->shared && force_change) {
949 free_block(cachep, n->shared->entry,
950 n->shared->avail, node, &list);
951 n->shared->avail = 0;
952 }
953
954 if (!n->shared || force_change) {
955 old_shared = n->shared;
956 n->shared = new_shared;
957 new_shared = NULL;
958 }
959
960 if (!n->alien) {
961 n->alien = new_alien;
962 new_alien = NULL;
963 }
964
965 spin_unlock_irq(&n->list_lock);
966 slabs_destroy(cachep, &list);
967
968fail:
969 kfree(old_shared);
970 kfree(new_shared);
971 free_alien_cache(new_alien);
972
973 return ret;
974}
975
Paul Gortmaker0db06282013-06-19 14:53:51 -0400976static void cpuup_canceled(long cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
Akinobu Mitafbf1e472007-10-18 03:05:09 -0700978 struct kmem_cache *cachep;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000979 struct kmem_cache_node *n = NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -0700980 int node = cpu_to_mem(cpu);
Rusty Russella70f7302009-03-13 14:49:46 +1030981 const struct cpumask *mask = cpumask_of_node(node);
Akinobu Mitafbf1e472007-10-18 03:05:09 -0700982
Christoph Lameter18004c52012-07-06 15:25:12 -0500983 list_for_each_entry(cachep, &slab_caches, list) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -0700984 struct array_cache *nc;
985 struct array_cache *shared;
Joonsoo Kimc8522a32014-08-06 16:04:29 -0700986 struct alien_cache **alien;
Joonsoo Kim97654df2014-08-06 16:04:25 -0700987 LIST_HEAD(list);
Akinobu Mitafbf1e472007-10-18 03:05:09 -0700988
Christoph Lameter18bf8542014-08-06 16:04:11 -0700989 n = get_node(cachep, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000990 if (!n)
Joonsoo Kimbf0dea22014-10-09 15:26:27 -0700991 continue;
Akinobu Mitafbf1e472007-10-18 03:05:09 -0700992
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000993 spin_lock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -0700994
Christoph Lameterce8eb6c2013-01-10 19:14:19 +0000995 /* Free limit for this kmem_cache_node */
996 n->free_limit -= cachep->batchcount;
Joonsoo Kimbf0dea22014-10-09 15:26:27 -0700997
998 /* cpu is dead; no one can alloc from it. */
999 nc = per_cpu_ptr(cachep->cpu_cache, cpu);
1000 if (nc) {
Joonsoo Kim97654df2014-08-06 16:04:25 -07001001 free_block(cachep, nc->entry, nc->avail, node, &list);
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001002 nc->avail = 0;
1003 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001004
Rusty Russell58463c12009-12-17 11:43:12 -06001005 if (!cpumask_empty(mask)) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001006 spin_unlock_irq(&n->list_lock);
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001007 goto free_slab;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001008 }
1009
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001010 shared = n->shared;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001011 if (shared) {
1012 free_block(cachep, shared->entry,
Joonsoo Kim97654df2014-08-06 16:04:25 -07001013 shared->avail, node, &list);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001014 n->shared = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001015 }
1016
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001017 alien = n->alien;
1018 n->alien = NULL;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001019
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001020 spin_unlock_irq(&n->list_lock);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001021
1022 kfree(shared);
1023 if (alien) {
1024 drain_alien_cache(cachep, alien);
1025 free_alien_cache(alien);
1026 }
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001027
1028free_slab:
Joonsoo Kim97654df2014-08-06 16:04:25 -07001029 slabs_destroy(cachep, &list);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001030 }
1031 /*
1032 * In the previous loop, all the objects were freed to
1033 * the respective cache's slabs, now we can go ahead and
1034 * shrink each nodelist to its limit.
1035 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001036 list_for_each_entry(cachep, &slab_caches, list) {
Christoph Lameter18bf8542014-08-06 16:04:11 -07001037 n = get_node(cachep, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001038 if (!n)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001039 continue;
Joonsoo Kima5aa63a2016-05-19 17:10:08 -07001040 drain_freelist(cachep, n, INT_MAX);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001041 }
1042}
1043
Paul Gortmaker0db06282013-06-19 14:53:51 -04001044static int cpuup_prepare(long cpu)
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001045{
Pekka Enberg343e0d72006-02-01 03:05:50 -08001046 struct kmem_cache *cachep;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001047 int node = cpu_to_mem(cpu);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001048 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001050 /*
1051 * We need to do this right in the beginning since
1052 * alloc_arraycache's are going to use this list.
1053 * kmalloc_node allows us to add the slab to the right
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001054 * kmem_cache_node and not this cpu's kmem_cache_node
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001055 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001056 err = init_cache_node_node(node);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001057 if (err < 0)
1058 goto bad;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001059
1060 /*
1061 * Now we can go ahead with allocating the shared arrays and
1062 * array caches
1063 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001064 list_for_each_entry(cachep, &slab_caches, list) {
Joonsoo Kimc3d332b2016-05-19 17:10:14 -07001065 err = setup_kmem_cache_node(cachep, node, GFP_KERNEL, false);
1066 if (err)
1067 goto bad;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001068 }
Pekka Enbergce79ddc2009-11-23 22:01:15 +02001069
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001070 return 0;
1071bad:
Akinobu Mita12d00f62007-10-18 03:05:11 -07001072 cpuup_canceled(cpu);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001073 return -ENOMEM;
1074}
1075
Paul Gortmaker0db06282013-06-19 14:53:51 -04001076static int cpuup_callback(struct notifier_block *nfb,
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001077 unsigned long action, void *hcpu)
1078{
1079 long cpu = (long)hcpu;
1080 int err = 0;
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 switch (action) {
Heiko Carstens38c3bd92007-05-09 02:34:05 -07001083 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001084 case CPU_UP_PREPARE_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001085 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001086 err = cpuup_prepare(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001087 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 break;
1089 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001090 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 start_cpu_timer(cpu);
1092 break;
1093#ifdef CONFIG_HOTPLUG_CPU
Christoph Lameter5830c592007-05-09 02:34:22 -07001094 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001095 case CPU_DOWN_PREPARE_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001096 /*
Christoph Lameter18004c52012-07-06 15:25:12 -05001097 * Shutdown cache reaper. Note that the slab_mutex is
Christoph Lameter5830c592007-05-09 02:34:22 -07001098 * held so that if cache_reap() is invoked it cannot do
1099 * anything expensive but will only modify reap_work
1100 * and reschedule the timer.
1101 */
Tejun Heoafe2c512010-12-14 16:21:17 +01001102 cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu));
Christoph Lameter5830c592007-05-09 02:34:22 -07001103 /* Now the cache_reaper is guaranteed to be not running. */
Tejun Heo1871e522009-10-29 22:34:13 +09001104 per_cpu(slab_reap_work, cpu).work.func = NULL;
Christoph Lameter5830c592007-05-09 02:34:22 -07001105 break;
1106 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001107 case CPU_DOWN_FAILED_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001108 start_cpu_timer(cpu);
1109 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001111 case CPU_DEAD_FROZEN:
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001112 /*
1113 * Even if all the cpus of a node are down, we don't free the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001114 * kmem_cache_node of any cache. This to avoid a race between
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001115 * cpu_down, and a kmalloc allocation from another cpu for
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001116 * memory from the node of the cpu going down. The node
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001117 * structure is usually allocated from kmem_cache_create() and
1118 * gets destroyed at kmem_cache_destroy().
1119 */
Simon Arlott183ff222007-10-20 01:27:18 +02001120 /* fall through */
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08001121#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001123 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001124 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001125 cpuup_canceled(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001126 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 }
Akinobu Mitaeac40682010-05-26 14:43:32 -07001129 return notifier_from_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
Paul Gortmaker0db06282013-06-19 14:53:51 -04001132static struct notifier_block cpucache_notifier = {
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001133 &cpuup_callback, NULL, 0
1134};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135
David Rientjes8f9f8d92010-03-27 19:40:47 -07001136#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
1137/*
1138 * Drains freelist for a node on each slab cache, used for memory hot-remove.
1139 * Returns -EBUSY if all objects cannot be drained so that the node is not
1140 * removed.
1141 *
Christoph Lameter18004c52012-07-06 15:25:12 -05001142 * Must hold slab_mutex.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001143 */
Christoph Lameter6a673682013-01-10 19:14:19 +00001144static int __meminit drain_cache_node_node(int node)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001145{
1146 struct kmem_cache *cachep;
1147 int ret = 0;
1148
Christoph Lameter18004c52012-07-06 15:25:12 -05001149 list_for_each_entry(cachep, &slab_caches, list) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001150 struct kmem_cache_node *n;
David Rientjes8f9f8d92010-03-27 19:40:47 -07001151
Christoph Lameter18bf8542014-08-06 16:04:11 -07001152 n = get_node(cachep, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001153 if (!n)
David Rientjes8f9f8d92010-03-27 19:40:47 -07001154 continue;
1155
Joonsoo Kima5aa63a2016-05-19 17:10:08 -07001156 drain_freelist(cachep, n, INT_MAX);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001157
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001158 if (!list_empty(&n->slabs_full) ||
1159 !list_empty(&n->slabs_partial)) {
David Rientjes8f9f8d92010-03-27 19:40:47 -07001160 ret = -EBUSY;
1161 break;
1162 }
1163 }
1164 return ret;
1165}
1166
1167static int __meminit slab_memory_callback(struct notifier_block *self,
1168 unsigned long action, void *arg)
1169{
1170 struct memory_notify *mnb = arg;
1171 int ret = 0;
1172 int nid;
1173
1174 nid = mnb->status_change_nid;
1175 if (nid < 0)
1176 goto out;
1177
1178 switch (action) {
1179 case MEM_GOING_ONLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001180 mutex_lock(&slab_mutex);
Christoph Lameter6a673682013-01-10 19:14:19 +00001181 ret = init_cache_node_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001182 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001183 break;
1184 case MEM_GOING_OFFLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001185 mutex_lock(&slab_mutex);
Christoph Lameter6a673682013-01-10 19:14:19 +00001186 ret = drain_cache_node_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001187 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001188 break;
1189 case MEM_ONLINE:
1190 case MEM_OFFLINE:
1191 case MEM_CANCEL_ONLINE:
1192 case MEM_CANCEL_OFFLINE:
1193 break;
1194 }
1195out:
Prarit Bhargava5fda1bd2011-03-22 16:30:49 -07001196 return notifier_from_errno(ret);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001197}
1198#endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
1199
Christoph Lametere498be72005-09-09 13:03:32 -07001200/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001201 * swap the static kmem_cache_node with kmalloced memory
Christoph Lametere498be72005-09-09 13:03:32 -07001202 */
Christoph Lameter6744f082013-01-10 19:12:17 +00001203static void __init init_list(struct kmem_cache *cachep, struct kmem_cache_node *list,
David Rientjes8f9f8d92010-03-27 19:40:47 -07001204 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07001205{
Christoph Lameter6744f082013-01-10 19:12:17 +00001206 struct kmem_cache_node *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001207
Christoph Lameter6744f082013-01-10 19:12:17 +00001208 ptr = kmalloc_node(sizeof(struct kmem_cache_node), GFP_NOWAIT, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07001209 BUG_ON(!ptr);
1210
Christoph Lameter6744f082013-01-10 19:12:17 +00001211 memcpy(ptr, list, sizeof(struct kmem_cache_node));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001212 /*
1213 * Do not assume that spinlocks can be initialized via memcpy:
1214 */
1215 spin_lock_init(&ptr->list_lock);
1216
Christoph Lametere498be72005-09-09 13:03:32 -07001217 MAKE_ALL_LISTS(cachep, ptr, nodeid);
Christoph Lameter6a673682013-01-10 19:14:19 +00001218 cachep->node[nodeid] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001219}
1220
Andrew Mortona737b3e2006-03-22 00:08:11 -08001221/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001222 * For setting up all the kmem_cache_node for cache whose buffer_size is same as
1223 * size of kmem_cache_node.
Pekka Enberg556a1692008-01-25 08:20:51 +02001224 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001225static void __init set_up_node(struct kmem_cache *cachep, int index)
Pekka Enberg556a1692008-01-25 08:20:51 +02001226{
1227 int node;
1228
1229 for_each_online_node(node) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001230 cachep->node[node] = &init_kmem_cache_node[index + node];
Christoph Lameter6a673682013-01-10 19:14:19 +00001231 cachep->node[node]->next_reap = jiffies +
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08001232 REAPTIMEOUT_NODE +
1233 ((unsigned long)cachep) % REAPTIMEOUT_NODE;
Pekka Enberg556a1692008-01-25 08:20:51 +02001234 }
1235}
1236
1237/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08001238 * Initialisation. Called after the page allocator have been initialised and
1239 * before smp_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 */
1241void __init kmem_cache_init(void)
1242{
Christoph Lametere498be72005-09-09 13:03:32 -07001243 int i;
1244
Joonsoo Kim68126702013-10-24 10:07:42 +09001245 BUILD_BUG_ON(sizeof(((struct page *)NULL)->lru) <
1246 sizeof(struct rcu_head));
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001247 kmem_cache = &kmem_cache_boot;
1248
Joonsoo Kim88881772016-05-19 17:10:05 -07001249 if (!IS_ENABLED(CONFIG_NUMA) || num_possible_nodes() == 1)
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001250 use_alien_caches = 0;
1251
Christoph Lameter3c583462012-11-28 16:23:01 +00001252 for (i = 0; i < NUM_INIT_LISTS; i++)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001253 kmem_cache_node_init(&init_kmem_cache_node[i]);
Christoph Lameter3c583462012-11-28 16:23:01 +00001254
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 /*
1256 * Fragmentation resistance on low memory - only use bigger
David Rientjes3df1ccc2011-10-18 22:09:28 -07001257 * page orders on machines with more than 32MB of memory if
1258 * not overridden on the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 */
David Rientjes3df1ccc2011-10-18 22:09:28 -07001260 if (!slab_max_order_set && totalram_pages > (32 << 20) >> PAGE_SHIFT)
David Rientjes543585c2011-10-18 22:09:24 -07001261 slab_max_order = SLAB_MAX_ORDER_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 /* Bootstrap is tricky, because several objects are allocated
1264 * from caches that do not exist yet:
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001265 * 1) initialize the kmem_cache cache: it contains the struct
1266 * kmem_cache structures of all caches, except kmem_cache itself:
1267 * kmem_cache is statically allocated.
Christoph Lametere498be72005-09-09 13:03:32 -07001268 * Initially an __init data area is used for the head array and the
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001269 * kmem_cache_node structures, it's replaced with a kmalloc allocated
Christoph Lametere498be72005-09-09 13:03:32 -07001270 * array at the end of the bootstrap.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 * 2) Create the first kmalloc cache.
Pekka Enberg343e0d72006-02-01 03:05:50 -08001272 * The struct kmem_cache for the new cache is allocated normally.
Christoph Lametere498be72005-09-09 13:03:32 -07001273 * An __init data area is used for the head array.
1274 * 3) Create the remaining kmalloc caches, with minimally sized
1275 * head arrays.
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001276 * 4) Replace the __init data head arrays for kmem_cache and the first
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 * kmalloc cache with kmalloc allocated arrays.
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001278 * 5) Replace the __init data for kmem_cache_node for kmem_cache and
Christoph Lametere498be72005-09-09 13:03:32 -07001279 * the other cache's with kmalloc allocated memory.
1280 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 */
1282
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001283 /* 1) create the kmem_cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Eric Dumazet8da34302007-05-06 14:49:29 -07001285 /*
Eric Dumazetb56efcf2011-07-20 19:04:23 +02001286 * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
Eric Dumazet8da34302007-05-06 14:49:29 -07001287 */
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001288 create_boot_cache(kmem_cache, "kmem_cache",
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001289 offsetof(struct kmem_cache, node) +
Christoph Lameter6744f082013-01-10 19:12:17 +00001290 nr_node_ids * sizeof(struct kmem_cache_node *),
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001291 SLAB_HWCACHE_ALIGN);
1292 list_add(&kmem_cache->list, &slab_caches);
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001293 slab_state = PARTIAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Andrew Mortona737b3e2006-03-22 00:08:11 -08001295 /*
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001296 * Initialize the caches that provide memory for the kmem_cache_node
1297 * structures first. Without this, further allocations will bug.
Christoph Lametere498be72005-09-09 13:03:32 -07001298 */
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001299 kmalloc_caches[INDEX_NODE] = create_kmalloc_cache("kmalloc-node",
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001300 kmalloc_size(INDEX_NODE), ARCH_KMALLOC_FLAGS);
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001301 slab_state = PARTIAL_NODE;
Daniel Sanders34cc6992015-06-24 16:55:57 -07001302 setup_kmalloc_cache_index_table();
Christoph Lametere498be72005-09-09 13:03:32 -07001303
Ingo Molnare0a42722006-06-23 02:03:46 -07001304 slab_early_init = 0;
1305
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001306 /* 5) Replace the bootstrap kmem_cache_node */
Christoph Lametere498be72005-09-09 13:03:32 -07001307 {
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001308 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Mel Gorman9c09a952008-01-24 05:49:54 -08001310 for_each_online_node(nid) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001311 init_list(kmem_cache, &init_kmem_cache_node[CACHE_CACHE + nid], nid);
Pekka Enberg556a1692008-01-25 08:20:51 +02001312
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001313 init_list(kmalloc_caches[INDEX_NODE],
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001314 &init_kmem_cache_node[SIZE_NODE + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001315 }
1316 }
1317
Christoph Lameterf97d5f62013-01-10 19:12:17 +00001318 create_kmalloc_caches(ARCH_KMALLOC_FLAGS);
Pekka Enberg8429db52009-06-12 15:58:59 +03001319}
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001320
Pekka Enberg8429db52009-06-12 15:58:59 +03001321void __init kmem_cache_init_late(void)
1322{
1323 struct kmem_cache *cachep;
1324
Christoph Lameter97d06602012-07-06 15:25:11 -05001325 slab_state = UP;
Peter Zijlstra52cef182011-11-28 21:12:40 +01001326
Pekka Enberg8429db52009-06-12 15:58:59 +03001327 /* 6) resize the head arrays to their final sizes */
Christoph Lameter18004c52012-07-06 15:25:12 -05001328 mutex_lock(&slab_mutex);
1329 list_for_each_entry(cachep, &slab_caches, list)
Pekka Enberg8429db52009-06-12 15:58:59 +03001330 if (enable_cpucache(cachep, GFP_NOWAIT))
1331 BUG();
Christoph Lameter18004c52012-07-06 15:25:12 -05001332 mutex_unlock(&slab_mutex);
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001333
Christoph Lameter97d06602012-07-06 15:25:11 -05001334 /* Done! */
1335 slab_state = FULL;
1336
Andrew Mortona737b3e2006-03-22 00:08:11 -08001337 /*
1338 * Register a cpu startup notifier callback that initializes
1339 * cpu_cache_get for all new cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 */
1341 register_cpu_notifier(&cpucache_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
David Rientjes8f9f8d92010-03-27 19:40:47 -07001343#ifdef CONFIG_NUMA
1344 /*
1345 * Register a memory hotplug callback that initializes and frees
Christoph Lameter6a673682013-01-10 19:14:19 +00001346 * node.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001347 */
1348 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
1349#endif
1350
Andrew Mortona737b3e2006-03-22 00:08:11 -08001351 /*
1352 * The reap timers are started later, with a module init call: That part
1353 * of the kernel is not yet operational.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 */
1355}
1356
1357static int __init cpucache_init(void)
1358{
1359 int cpu;
1360
Andrew Mortona737b3e2006-03-22 00:08:11 -08001361 /*
1362 * Register the timers that return unneeded pages to the page allocator
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 */
Christoph Lametere498be72005-09-09 13:03:32 -07001364 for_each_online_cpu(cpu)
Andrew Mortona737b3e2006-03-22 00:08:11 -08001365 start_cpu_timer(cpu);
Glauber Costaa164f8962012-06-21 00:59:18 +04001366
1367 /* Done! */
Christoph Lameter97d06602012-07-06 15:25:11 -05001368 slab_state = FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return 0;
1370}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371__initcall(cpucache_init);
1372
Rafael Aquini8bdec192012-03-09 17:27:27 -03001373static noinline void
1374slab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid)
1375{
David Rientjes9a02d692014-06-04 16:06:36 -07001376#if DEBUG
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001377 struct kmem_cache_node *n;
Joonsoo Kim8456a642013-10-24 10:07:49 +09001378 struct page *page;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001379 unsigned long flags;
1380 int node;
David Rientjes9a02d692014-06-04 16:06:36 -07001381 static DEFINE_RATELIMIT_STATE(slab_oom_rs, DEFAULT_RATELIMIT_INTERVAL,
1382 DEFAULT_RATELIMIT_BURST);
1383
1384 if ((gfpflags & __GFP_NOWARN) || !__ratelimit(&slab_oom_rs))
1385 return;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001386
Vlastimil Babka5b3810e2016-03-15 14:56:33 -07001387 pr_warn("SLAB: Unable to allocate memory on node %d, gfp=%#x(%pGg)\n",
1388 nodeid, gfpflags, &gfpflags);
1389 pr_warn(" cache: %s, object size: %d, order: %d\n",
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05001390 cachep->name, cachep->size, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001391
Christoph Lameter18bf8542014-08-06 16:04:11 -07001392 for_each_kmem_cache_node(cachep, node, n) {
Rafael Aquini8bdec192012-03-09 17:27:27 -03001393 unsigned long active_objs = 0, num_objs = 0, free_objects = 0;
1394 unsigned long active_slabs = 0, num_slabs = 0;
1395
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001396 spin_lock_irqsave(&n->list_lock, flags);
Joonsoo Kim8456a642013-10-24 10:07:49 +09001397 list_for_each_entry(page, &n->slabs_full, lru) {
Rafael Aquini8bdec192012-03-09 17:27:27 -03001398 active_objs += cachep->num;
1399 active_slabs++;
1400 }
Joonsoo Kim8456a642013-10-24 10:07:49 +09001401 list_for_each_entry(page, &n->slabs_partial, lru) {
1402 active_objs += page->active;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001403 active_slabs++;
1404 }
Joonsoo Kim8456a642013-10-24 10:07:49 +09001405 list_for_each_entry(page, &n->slabs_free, lru)
Rafael Aquini8bdec192012-03-09 17:27:27 -03001406 num_slabs++;
1407
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00001408 free_objects += n->free_objects;
1409 spin_unlock_irqrestore(&n->list_lock, flags);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001410
1411 num_slabs += active_slabs;
1412 num_objs = num_slabs * cachep->num;
Vlastimil Babka5b3810e2016-03-15 14:56:33 -07001413 pr_warn(" node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
Rafael Aquini8bdec192012-03-09 17:27:27 -03001414 node, active_slabs, num_slabs, active_objs, num_objs,
1415 free_objects);
1416 }
David Rientjes9a02d692014-06-04 16:06:36 -07001417#endif
Rafael Aquini8bdec192012-03-09 17:27:27 -03001418}
1419
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420/*
Wang Sheng-Hui8a7d9b42014-08-06 16:04:46 -07001421 * Interface to system's page allocator. No need to hold the
1422 * kmem_cache_node ->list_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 *
1424 * If we requested dmaable memory, we will get it. Even if we
1425 * did not request dmaable memory, we might get it, but that
1426 * would be relatively rare and ignorable.
1427 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001428static struct page *kmem_getpages(struct kmem_cache *cachep, gfp_t flags,
1429 int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430{
1431 struct page *page;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001432 int nr_pages;
Christoph Lameter765c4502006-09-27 01:50:08 -07001433
Glauber Costaa618e892012-06-14 16:17:21 +04001434 flags |= cachep->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001435 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1436 flags |= __GFP_RECLAIMABLE;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001437
Vlastimil Babka96db8002015-09-08 15:03:50 -07001438 page = __alloc_pages_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001439 if (!page) {
David Rientjes9a02d692014-06-04 16:06:36 -07001440 slab_out_of_memory(cachep, flags, nodeid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 return NULL;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Vladimir Davydovf3ccb2c42015-11-05 18:49:01 -08001444 if (memcg_charge_slab(page, flags, cachep->gfporder, cachep)) {
1445 __free_pages(page, cachep->gfporder);
1446 return NULL;
1447 }
1448
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001449 nr_pages = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
Christoph Lameter972d1a72006-09-25 23:31:51 -07001451 add_zone_page_state(page_zone(page),
1452 NR_SLAB_RECLAIMABLE, nr_pages);
1453 else
1454 add_zone_page_state(page_zone(page),
1455 NR_SLAB_UNRECLAIMABLE, nr_pages);
Joonsoo Kimf68f8dd2016-03-15 14:54:56 -07001456
Joonsoo Kima57a4982013-10-24 10:07:44 +09001457 __SetPageSlab(page);
Joonsoo Kimf68f8dd2016-03-15 14:54:56 -07001458 /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
1459 if (sk_memalloc_socks() && page_is_pfmemalloc(page))
Joonsoo Kima57a4982013-10-24 10:07:44 +09001460 SetPageSlabPfmemalloc(page);
Mel Gorman072bb0a2012-07-31 16:43:58 -07001461
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001462 if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
1463 kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
1464
1465 if (cachep->ctor)
1466 kmemcheck_mark_uninitialized_pages(page, nr_pages);
1467 else
1468 kmemcheck_mark_unallocated_pages(page, nr_pages);
1469 }
Pekka Enbergc175eea2008-05-09 20:35:53 +02001470
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001471 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472}
1473
1474/*
1475 * Interface to system's page release.
1476 */
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001477static void kmem_freepages(struct kmem_cache *cachep, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478{
Vladimir Davydov27ee57c2016-03-17 14:17:35 -07001479 int order = cachep->gfporder;
1480 unsigned long nr_freed = (1 << order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481
Vladimir Davydov27ee57c2016-03-17 14:17:35 -07001482 kmemcheck_free_shadow(page, order);
Pekka Enbergc175eea2008-05-09 20:35:53 +02001483
Christoph Lameter972d1a72006-09-25 23:31:51 -07001484 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1485 sub_zone_page_state(page_zone(page),
1486 NR_SLAB_RECLAIMABLE, nr_freed);
1487 else
1488 sub_zone_page_state(page_zone(page),
1489 NR_SLAB_UNRECLAIMABLE, nr_freed);
Joonsoo Kim73293c22013-10-24 10:07:37 +09001490
Joonsoo Kima57a4982013-10-24 10:07:44 +09001491 BUG_ON(!PageSlab(page));
Joonsoo Kim73293c22013-10-24 10:07:37 +09001492 __ClearPageSlabPfmemalloc(page);
Joonsoo Kima57a4982013-10-24 10:07:44 +09001493 __ClearPageSlab(page);
Joonsoo Kim8456a642013-10-24 10:07:49 +09001494 page_mapcount_reset(page);
1495 page->mapping = NULL;
Glauber Costa1f458cb2012-12-18 14:22:50 -08001496
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 if (current->reclaim_state)
1498 current->reclaim_state->reclaimed_slab += nr_freed;
Vladimir Davydov27ee57c2016-03-17 14:17:35 -07001499 memcg_uncharge_slab(page, order, cachep);
1500 __free_pages(page, order);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501}
1502
1503static void kmem_rcu_free(struct rcu_head *head)
1504{
Joonsoo Kim68126702013-10-24 10:07:42 +09001505 struct kmem_cache *cachep;
1506 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507
Joonsoo Kim68126702013-10-24 10:07:42 +09001508 page = container_of(head, struct page, rcu_head);
1509 cachep = page->slab_cache;
1510
1511 kmem_freepages(cachep, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
1514#if DEBUG
Joonsoo Kim40b44132016-03-15 14:54:21 -07001515static bool is_debug_pagealloc_cache(struct kmem_cache *cachep)
1516{
1517 if (debug_pagealloc_enabled() && OFF_SLAB(cachep) &&
1518 (cachep->size % PAGE_SIZE) == 0)
1519 return true;
1520
1521 return false;
1522}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
1524#ifdef CONFIG_DEBUG_PAGEALLOC
Pekka Enberg343e0d72006-02-01 03:05:50 -08001525static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001526 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001528 int size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001530 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001532 if (size < 5 * sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 return;
1534
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001535 *addr++ = 0x12345678;
1536 *addr++ = caller;
1537 *addr++ = smp_processor_id();
1538 size -= 3 * sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 {
1540 unsigned long *sptr = &caller;
1541 unsigned long svalue;
1542
1543 while (!kstack_end(sptr)) {
1544 svalue = *sptr++;
1545 if (kernel_text_address(svalue)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001546 *addr++ = svalue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 size -= sizeof(unsigned long);
1548 if (size <= sizeof(unsigned long))
1549 break;
1550 }
1551 }
1552
1553 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001554 *addr++ = 0x87654321;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555}
Joonsoo Kim40b44132016-03-15 14:54:21 -07001556
1557static void slab_kernel_map(struct kmem_cache *cachep, void *objp,
1558 int map, unsigned long caller)
1559{
1560 if (!is_debug_pagealloc_cache(cachep))
1561 return;
1562
1563 if (caller)
1564 store_stackinfo(cachep, objp, caller);
1565
1566 kernel_map_pages(virt_to_page(objp), cachep->size / PAGE_SIZE, map);
1567}
1568
1569#else
1570static inline void slab_kernel_map(struct kmem_cache *cachep, void *objp,
1571 int map, unsigned long caller) {}
1572
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573#endif
1574
Pekka Enberg343e0d72006-02-01 03:05:50 -08001575static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001577 int size = cachep->object_size;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001578 addr = &((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
1580 memset(addr, val, size);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001581 *(unsigned char *)(addr + size - 1) = POISON_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582}
1583
1584static void dump_line(char *data, int offset, int limit)
1585{
1586 int i;
Dave Jonesaa83aa42006-09-29 01:59:51 -07001587 unsigned char error = 0;
1588 int bad_count = 0;
1589
Joe Perches11705322016-03-17 14:19:50 -07001590 pr_err("%03x: ", offset);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001591 for (i = 0; i < limit; i++) {
1592 if (data[offset + i] != POISON_FREE) {
1593 error = data[offset + i];
1594 bad_count++;
1595 }
Dave Jonesaa83aa42006-09-29 01:59:51 -07001596 }
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02001597 print_hex_dump(KERN_CONT, "", 0, 16, 1,
1598 &data[offset], limit, 1);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001599
1600 if (bad_count == 1) {
1601 error ^= POISON_FREE;
1602 if (!(error & (error - 1))) {
Joe Perches11705322016-03-17 14:19:50 -07001603 pr_err("Single bit error detected. Probably bad RAM.\n");
Dave Jonesaa83aa42006-09-29 01:59:51 -07001604#ifdef CONFIG_X86
Joe Perches11705322016-03-17 14:19:50 -07001605 pr_err("Run memtest86+ or a similar memory test tool.\n");
Dave Jonesaa83aa42006-09-29 01:59:51 -07001606#else
Joe Perches11705322016-03-17 14:19:50 -07001607 pr_err("Run a memory test tool.\n");
Dave Jonesaa83aa42006-09-29 01:59:51 -07001608#endif
1609 }
1610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611}
1612#endif
1613
1614#if DEBUG
1615
Pekka Enberg343e0d72006-02-01 03:05:50 -08001616static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
1618 int i, size;
1619 char *realobj;
1620
1621 if (cachep->flags & SLAB_RED_ZONE) {
Joe Perches11705322016-03-17 14:19:50 -07001622 pr_err("Redzone: 0x%llx/0x%llx\n",
1623 *dbg_redzone1(cachep, objp),
1624 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 }
1626
1627 if (cachep->flags & SLAB_STORE_USER) {
Joe Perches11705322016-03-17 14:19:50 -07001628 pr_err("Last user: [<%p>](%pSR)\n",
Joe Perches071361d2012-12-12 10:19:12 -08001629 *dbg_userword(cachep, objp),
1630 *dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 }
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001632 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001633 size = cachep->object_size;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001634 for (i = 0; i < size && lines; i += 16, lines--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 int limit;
1636 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001637 if (i + limit > size)
1638 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 dump_line(realobj, i, limit);
1640 }
1641}
1642
Pekka Enberg343e0d72006-02-01 03:05:50 -08001643static void check_poison_obj(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644{
1645 char *realobj;
1646 int size, i;
1647 int lines = 0;
1648
Joonsoo Kim40b44132016-03-15 14:54:21 -07001649 if (is_debug_pagealloc_cache(cachep))
1650 return;
1651
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001652 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001653 size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001655 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 char exp = POISON_FREE;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001657 if (i == size - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 exp = POISON_END;
1659 if (realobj[i] != exp) {
1660 int limit;
1661 /* Mismatch ! */
1662 /* Print header */
1663 if (lines == 0) {
Joe Perches11705322016-03-17 14:19:50 -07001664 pr_err("Slab corruption (%s): %s start=%p, len=%d\n",
1665 print_tainted(), cachep->name,
1666 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 print_objinfo(cachep, objp, 0);
1668 }
1669 /* Hexdump the affected line */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001670 i = (i / 16) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001672 if (i + limit > size)
1673 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 dump_line(realobj, i, limit);
1675 i += 16;
1676 lines++;
1677 /* Limit to 5 lines */
1678 if (lines > 5)
1679 break;
1680 }
1681 }
1682 if (lines != 0) {
1683 /* Print some data about the neighboring objects, if they
1684 * exist:
1685 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09001686 struct page *page = virt_to_head_page(objp);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001687 unsigned int objnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Joonsoo Kim8456a642013-10-24 10:07:49 +09001689 objnr = obj_to_index(cachep, page, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 if (objnr) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09001691 objp = index_to_obj(cachep, page, objnr - 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001692 realobj = (char *)objp + obj_offset(cachep);
Joe Perches11705322016-03-17 14:19:50 -07001693 pr_err("Prev obj: start=%p, len=%d\n", realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 print_objinfo(cachep, objp, 2);
1695 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001696 if (objnr + 1 < cachep->num) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09001697 objp = index_to_obj(cachep, page, objnr + 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001698 realobj = (char *)objp + obj_offset(cachep);
Joe Perches11705322016-03-17 14:19:50 -07001699 pr_err("Next obj: start=%p, len=%d\n", realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 print_objinfo(cachep, objp, 2);
1701 }
1702 }
1703}
1704#endif
1705
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706#if DEBUG
Joonsoo Kim8456a642013-10-24 10:07:49 +09001707static void slab_destroy_debugcheck(struct kmem_cache *cachep,
1708 struct page *page)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001709{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 int i;
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07001711
1712 if (OBJFREELIST_SLAB(cachep) && cachep->flags & SLAB_POISON) {
1713 poison_obj(cachep, page->freelist - obj_offset(cachep),
1714 POISON_FREE);
1715 }
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 for (i = 0; i < cachep->num; i++) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09001718 void *objp = index_to_obj(cachep, page, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719
1720 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 check_poison_obj(cachep, objp);
Joonsoo Kim40b44132016-03-15 14:54:21 -07001722 slab_kernel_map(cachep, objp, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 }
1724 if (cachep->flags & SLAB_RED_ZONE) {
1725 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
Joe Perches756a025f02016-03-17 14:19:47 -07001726 slab_error(cachep, "start of a freed object was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
Joe Perches756a025f02016-03-17 14:19:47 -07001728 slab_error(cachep, "end of a freed object was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001731}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732#else
Joonsoo Kim8456a642013-10-24 10:07:49 +09001733static void slab_destroy_debugcheck(struct kmem_cache *cachep,
1734 struct page *page)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001735{
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001736}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737#endif
1738
Randy Dunlap911851e2006-03-22 00:08:14 -08001739/**
1740 * slab_destroy - destroy and release all objects in a slab
1741 * @cachep: cache pointer being destroyed
Masanari Iidacb8ee1a2014-01-28 02:57:08 +09001742 * @page: page pointer being destroyed
Randy Dunlap911851e2006-03-22 00:08:14 -08001743 *
Wang Sheng-Hui8a7d9b42014-08-06 16:04:46 -07001744 * Destroy all the objs in a slab page, and release the mem back to the system.
1745 * Before calling the slab page must have been unlinked from the cache. The
1746 * kmem_cache_node ->list_lock is not held/needed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001747 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09001748static void slab_destroy(struct kmem_cache *cachep, struct page *page)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001749{
Joonsoo Kim7e007352013-10-30 19:04:01 +09001750 void *freelist;
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001751
Joonsoo Kim8456a642013-10-24 10:07:49 +09001752 freelist = page->freelist;
1753 slab_destroy_debugcheck(cachep, page);
Kirill A. Shutemovbc4f6102015-11-06 16:29:44 -08001754 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU))
1755 call_rcu(&page->rcu_head, kmem_rcu_free);
1756 else
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09001757 kmem_freepages(cachep, page);
Joonsoo Kim68126702013-10-24 10:07:42 +09001758
1759 /*
Joonsoo Kim8456a642013-10-24 10:07:49 +09001760 * From now on, we don't use freelist
Joonsoo Kim68126702013-10-24 10:07:42 +09001761 * although actual page can be freed in rcu context
1762 */
1763 if (OFF_SLAB(cachep))
Joonsoo Kim8456a642013-10-24 10:07:49 +09001764 kmem_cache_free(cachep->freelist_cache, freelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
1766
Joonsoo Kim97654df2014-08-06 16:04:25 -07001767static void slabs_destroy(struct kmem_cache *cachep, struct list_head *list)
1768{
1769 struct page *page, *n;
1770
1771 list_for_each_entry_safe(page, n, list, lru) {
1772 list_del(&page->lru);
1773 slab_destroy(cachep, page);
1774 }
1775}
1776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777/**
Randy.Dunlapa70773d2006-02-01 03:05:52 -08001778 * calculate_slab_order - calculate size (page order) of slabs
1779 * @cachep: pointer to the cache that is being created
1780 * @size: size of objects to be created in this cache.
Randy.Dunlapa70773d2006-02-01 03:05:52 -08001781 * @flags: slab allocation flags
1782 *
1783 * Also calculates the number of objects per slab.
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001784 *
1785 * This could be made much more intelligent. For now, try to avoid using
1786 * high order pages for slabs. When the gfp() functions are more friendly
1787 * towards high-order requests, this should be changed.
1788 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001789static size_t calculate_slab_order(struct kmem_cache *cachep,
Joonsoo Kim2e6b3602016-03-15 14:54:30 -07001790 size_t size, unsigned long flags)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001791{
1792 size_t left_over = 0;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001793 int gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001794
Christoph Lameter0aa817f2007-05-16 22:11:01 -07001795 for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001796 unsigned int num;
1797 size_t remainder;
1798
Joonsoo Kim70f75062016-03-15 14:54:53 -07001799 num = cache_estimate(gfporder, size, flags, &remainder);
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001800 if (!num)
1801 continue;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001802
Joonsoo Kimf315e3f2013-12-02 17:49:41 +09001803 /* Can't handle number of objects more than SLAB_OBJ_MAX_NUM */
1804 if (num > SLAB_OBJ_MAX_NUM)
1805 break;
1806
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02001807 if (flags & CFLGS_OFF_SLAB) {
Joonsoo Kim3217fd92016-03-15 14:54:41 -07001808 struct kmem_cache *freelist_cache;
1809 size_t freelist_size;
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02001810
Joonsoo Kim3217fd92016-03-15 14:54:41 -07001811 freelist_size = num * sizeof(freelist_idx_t);
1812 freelist_cache = kmalloc_slab(freelist_size, 0u);
1813 if (!freelist_cache)
1814 continue;
1815
1816 /*
1817 * Needed to avoid possible looping condition
Joonsoo Kim76b342b2016-05-19 17:10:26 -07001818 * in cache_grow_begin()
Joonsoo Kim3217fd92016-03-15 14:54:41 -07001819 */
1820 if (OFF_SLAB(freelist_cache))
1821 continue;
1822
1823 /* check if off slab has enough benefit */
1824 if (freelist_cache->size > cachep->size / 2)
1825 continue;
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02001826 }
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001827
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001828 /* Found something acceptable - save it away */
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001829 cachep->num = num;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001830 cachep->gfporder = gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001831 left_over = remainder;
1832
1833 /*
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08001834 * A VFS-reclaimable slab tends to have most allocations
1835 * as GFP_NOFS and we really don't want to have to be allocating
1836 * higher-order pages when we are unable to shrink dcache.
1837 */
1838 if (flags & SLAB_RECLAIM_ACCOUNT)
1839 break;
1840
1841 /*
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001842 * Large number of objects is good, but very large slabs are
1843 * currently bad for the gfp()s.
1844 */
David Rientjes543585c2011-10-18 22:09:24 -07001845 if (gfporder >= slab_max_order)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001846 break;
1847
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001848 /*
1849 * Acceptable internal fragmentation?
1850 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001851 if (left_over * 8 <= (PAGE_SIZE << gfporder))
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001852 break;
1853 }
1854 return left_over;
1855}
1856
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001857static struct array_cache __percpu *alloc_kmem_cache_cpus(
1858 struct kmem_cache *cachep, int entries, int batchcount)
1859{
1860 int cpu;
1861 size_t size;
1862 struct array_cache __percpu *cpu_cache;
1863
1864 size = sizeof(void *) * entries + sizeof(struct array_cache);
Joonsoo Kim85c9f4b2014-10-13 15:51:01 -07001865 cpu_cache = __alloc_percpu(size, sizeof(void *));
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001866
1867 if (!cpu_cache)
1868 return NULL;
1869
1870 for_each_possible_cpu(cpu) {
1871 init_arraycache(per_cpu_ptr(cpu_cache, cpu),
1872 entries, batchcount);
1873 }
1874
1875 return cpu_cache;
1876}
1877
Pekka Enberg83b519e2009-06-10 19:40:04 +03001878static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08001879{
Christoph Lameter97d06602012-07-06 15:25:11 -05001880 if (slab_state >= FULL)
Pekka Enberg83b519e2009-06-10 19:40:04 +03001881 return enable_cpucache(cachep, gfp);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07001882
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001883 cachep->cpu_cache = alloc_kmem_cache_cpus(cachep, 1, 1);
1884 if (!cachep->cpu_cache)
1885 return 1;
1886
Christoph Lameter97d06602012-07-06 15:25:11 -05001887 if (slab_state == DOWN) {
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001888 /* Creation of first cache (kmem_cache). */
1889 set_up_node(kmem_cache, CACHE_CACHE);
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001890 } else if (slab_state == PARTIAL) {
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001891 /* For kmem_cache_node */
1892 set_up_node(cachep, SIZE_NODE);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08001893 } else {
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001894 int node;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08001895
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001896 for_each_online_node(node) {
1897 cachep->node[node] = kmalloc_node(
1898 sizeof(struct kmem_cache_node), gfp, node);
1899 BUG_ON(!cachep->node[node]);
1900 kmem_cache_node_init(cachep->node[node]);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08001901 }
1902 }
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07001903
Christoph Lameter6a673682013-01-10 19:14:19 +00001904 cachep->node[numa_mem_id()]->next_reap =
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08001905 jiffies + REAPTIMEOUT_NODE +
1906 ((unsigned long)cachep) % REAPTIMEOUT_NODE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08001907
1908 cpu_cache_get(cachep)->avail = 0;
1909 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
1910 cpu_cache_get(cachep)->batchcount = 1;
1911 cpu_cache_get(cachep)->touched = 0;
1912 cachep->batchcount = 1;
1913 cachep->limit = BOOT_CPUCACHE_ENTRIES;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07001914 return 0;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08001915}
1916
Joonsoo Kim12220de2014-10-09 15:26:24 -07001917unsigned long kmem_cache_flags(unsigned long object_size,
1918 unsigned long flags, const char *name,
1919 void (*ctor)(void *))
1920{
1921 return flags;
1922}
1923
1924struct kmem_cache *
1925__kmem_cache_alias(const char *name, size_t size, size_t align,
1926 unsigned long flags, void (*ctor)(void *))
1927{
1928 struct kmem_cache *cachep;
1929
1930 cachep = find_mergeable(size, align, flags, name, ctor);
1931 if (cachep) {
1932 cachep->refcount++;
1933
1934 /*
1935 * Adjust the object sizes so that we clear
1936 * the complete object on kzalloc.
1937 */
1938 cachep->object_size = max_t(int, cachep->object_size, size);
1939 }
1940 return cachep;
1941}
1942
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07001943static bool set_objfreelist_slab_cache(struct kmem_cache *cachep,
1944 size_t size, unsigned long flags)
1945{
1946 size_t left;
1947
1948 cachep->num = 0;
1949
1950 if (cachep->ctor || flags & SLAB_DESTROY_BY_RCU)
1951 return false;
1952
1953 left = calculate_slab_order(cachep, size,
1954 flags | CFLGS_OBJFREELIST_SLAB);
1955 if (!cachep->num)
1956 return false;
1957
1958 if (cachep->num * sizeof(freelist_idx_t) > cachep->object_size)
1959 return false;
1960
1961 cachep->colour = left / cachep->colour_off;
1962
1963 return true;
1964}
1965
Joonsoo Kim158e3192016-03-15 14:54:35 -07001966static bool set_off_slab_cache(struct kmem_cache *cachep,
1967 size_t size, unsigned long flags)
1968{
1969 size_t left;
1970
1971 cachep->num = 0;
1972
1973 /*
Joonsoo Kim3217fd92016-03-15 14:54:41 -07001974 * Always use on-slab management when SLAB_NOLEAKTRACE
1975 * to avoid recursive calls into kmemleak.
Joonsoo Kim158e3192016-03-15 14:54:35 -07001976 */
Joonsoo Kim158e3192016-03-15 14:54:35 -07001977 if (flags & SLAB_NOLEAKTRACE)
1978 return false;
1979
1980 /*
1981 * Size is large, assume best to place the slab management obj
1982 * off-slab (should allow better packing of objs).
1983 */
1984 left = calculate_slab_order(cachep, size, flags | CFLGS_OFF_SLAB);
1985 if (!cachep->num)
1986 return false;
1987
1988 /*
1989 * If the slab has been placed off-slab, and we have enough space then
1990 * move it on-slab. This is at the expense of any extra colouring.
1991 */
1992 if (left >= cachep->num * sizeof(freelist_idx_t))
1993 return false;
1994
1995 cachep->colour = left / cachep->colour_off;
1996
1997 return true;
1998}
1999
2000static bool set_on_slab_cache(struct kmem_cache *cachep,
2001 size_t size, unsigned long flags)
2002{
2003 size_t left;
2004
2005 cachep->num = 0;
2006
2007 left = calculate_slab_order(cachep, size, flags);
2008 if (!cachep->num)
2009 return false;
2010
2011 cachep->colour = left / cachep->colour_off;
2012
2013 return true;
2014}
2015
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002016/**
Christoph Lameter039363f2012-07-06 15:25:10 -05002017 * __kmem_cache_create - Create a cache.
Randy Dunlapa755b762012-11-06 17:10:10 -08002018 * @cachep: cache management descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 * @flags: SLAB flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 *
2021 * Returns a ptr to the cache on success, NULL on failure.
2022 * Cannot be called within a int, but can be interrupted.
Paul Mundt20c2df82007-07-20 10:11:58 +09002023 * The @ctor is run when new pages are allocated by the cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 * The flags are
2026 *
2027 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2028 * to catch references to uninitialised memory.
2029 *
2030 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2031 * for buffer overruns.
2032 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2034 * cacheline. This can be beneficial if you're counting cycles as closely
2035 * as davem.
2036 */
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002037int
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002038__kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039{
David Rientjesd4a5fca2014-09-25 16:05:20 -07002040 size_t ralign = BYTES_PER_WORD;
Pekka Enberg83b519e2009-06-10 19:40:04 +03002041 gfp_t gfp;
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002042 int err;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002043 size_t size = cachep->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046#if FORCED_DEBUG
2047 /*
2048 * Enable redzoning and last user accounting, except for caches with
2049 * large objects, if the increased size would increase the object size
2050 * above the next power of two: caches with object sizes just above a
2051 * power of two have a significant amount of internal fragmentation.
2052 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002053 if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
2054 2 * sizeof(unsigned long long)))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002055 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 if (!(flags & SLAB_DESTROY_BY_RCU))
2057 flags |= SLAB_POISON;
2058#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Andrew Mortona737b3e2006-03-22 00:08:11 -08002061 /*
2062 * Check that size is in terms of words. This is needed to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 * unaligned accesses for some archs when redzoning is used, and makes
2064 * sure any on-slab bufctl's are also correctly aligned.
2065 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002066 if (size & (BYTES_PER_WORD - 1)) {
2067 size += (BYTES_PER_WORD - 1);
2068 size &= ~(BYTES_PER_WORD - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
2070
David Woodhouse87a927c2007-07-04 21:26:44 -04002071 if (flags & SLAB_RED_ZONE) {
2072 ralign = REDZONE_ALIGN;
2073 /* If redzoning, ensure that the second redzone is suitably
2074 * aligned, by adjusting the object size accordingly. */
2075 size += REDZONE_ALIGN - 1;
2076 size &= ~(REDZONE_ALIGN - 1);
2077 }
Pekka Enbergca5f9702006-09-25 23:31:25 -07002078
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002079 /* 3) caller mandated alignment */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002080 if (ralign < cachep->align) {
2081 ralign = cachep->align;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 }
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002083 /* disable debug if necessary */
2084 if (ralign > __alignof__(unsigned long long))
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002085 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002086 /*
Pekka Enbergca5f9702006-09-25 23:31:25 -07002087 * 4) Store it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002089 cachep->align = ralign;
Joonsoo Kim158e3192016-03-15 14:54:35 -07002090 cachep->colour_off = cache_line_size();
2091 /* Offset must be a multiple of the alignment. */
2092 if (cachep->colour_off < cachep->align)
2093 cachep->colour_off = cachep->align;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Pekka Enberg83b519e2009-06-10 19:40:04 +03002095 if (slab_is_available())
2096 gfp = GFP_KERNEL;
2097 else
2098 gfp = GFP_NOWAIT;
2099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
Pekka Enbergca5f9702006-09-25 23:31:25 -07002102 /*
2103 * Both debugging options require word-alignment which is calculated
2104 * into align above.
2105 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 if (flags & SLAB_RED_ZONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 /* add space for red zone words */
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002108 cachep->obj_offset += sizeof(unsigned long long);
2109 size += 2 * sizeof(unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 }
2111 if (flags & SLAB_STORE_USER) {
Pekka Enbergca5f9702006-09-25 23:31:25 -07002112 /* user store requires one word storage behind the end of
David Woodhouse87a927c2007-07-04 21:26:44 -04002113 * the real object. But if the second red zone needs to be
2114 * aligned to 64 bits, we must allow that much space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002116 if (flags & SLAB_RED_ZONE)
2117 size += REDZONE_ALIGN;
2118 else
2119 size += BYTES_PER_WORD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 }
Joonsoo Kim832a15d2016-03-15 14:54:33 -07002121#endif
2122
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -07002123 kasan_cache_create(cachep, &size, &flags);
2124
Joonsoo Kim832a15d2016-03-15 14:54:33 -07002125 size = ALIGN(size, cachep->align);
2126 /*
2127 * We should restrict the number of objects in a slab to implement
2128 * byte sized index. Refer comment on SLAB_OBJ_MIN_SIZE definition.
2129 */
2130 if (FREELIST_BYTE_INDEX && size < SLAB_OBJ_MIN_SIZE)
2131 size = ALIGN(SLAB_OBJ_MIN_SIZE, cachep->align);
2132
2133#if DEBUG
Joonsoo Kim03a2d2a2015-10-01 15:36:54 -07002134 /*
2135 * To activate debug pagealloc, off-slab management is necessary
2136 * requirement. In early phase of initialization, small sized slab
2137 * doesn't get initialized so it would not be possible. So, we need
2138 * to check size >= 256. It guarantees that all necessary small
2139 * sized slab is initialized in current slab initialization sequence.
2140 */
Joonsoo Kim40323272016-03-15 14:54:18 -07002141 if (debug_pagealloc_enabled() && (flags & SLAB_POISON) &&
Joonsoo Kimf3a3c322016-03-15 14:54:38 -07002142 size >= 256 && cachep->object_size > cache_line_size()) {
2143 if (size < PAGE_SIZE || size % PAGE_SIZE == 0) {
2144 size_t tmp_size = ALIGN(size, PAGE_SIZE);
2145
2146 if (set_off_slab_cache(cachep, tmp_size, flags)) {
2147 flags |= CFLGS_OFF_SLAB;
2148 cachep->obj_offset += tmp_size - size;
2149 size = tmp_size;
2150 goto done;
2151 }
2152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 }
2154#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002156 if (set_objfreelist_slab_cache(cachep, size, flags)) {
2157 flags |= CFLGS_OBJFREELIST_SLAB;
2158 goto done;
2159 }
2160
Joonsoo Kim158e3192016-03-15 14:54:35 -07002161 if (set_off_slab_cache(cachep, size, flags)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 flags |= CFLGS_OFF_SLAB;
Joonsoo Kim158e3192016-03-15 14:54:35 -07002163 goto done;
Joonsoo Kim832a15d2016-03-15 14:54:33 -07002164 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165
Joonsoo Kim158e3192016-03-15 14:54:35 -07002166 if (set_on_slab_cache(cachep, size, flags))
2167 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
Joonsoo Kim158e3192016-03-15 14:54:35 -07002169 return -E2BIG;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002170
Joonsoo Kim158e3192016-03-15 14:54:35 -07002171done:
2172 cachep->freelist_size = cachep->num * sizeof(freelist_idx_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 cachep->flags = flags;
Joonsoo Kima57a4982013-10-24 10:07:44 +09002174 cachep->allocflags = __GFP_COMP;
Christoph Lameter4b51d662007-02-10 01:43:10 -08002175 if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
Glauber Costaa618e892012-06-14 16:17:21 +04002176 cachep->allocflags |= GFP_DMA;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002177 cachep->size = size;
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08002178 cachep->reciprocal_buffer_size = reciprocal_value(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179
Joonsoo Kim40b44132016-03-15 14:54:21 -07002180#if DEBUG
2181 /*
2182 * If we're going to use the generic kernel_map_pages()
2183 * poisoning, then it's going to smash the contents of
2184 * the redzone and userword anyhow, so switch them off.
2185 */
2186 if (IS_ENABLED(CONFIG_PAGE_POISONING) &&
2187 (cachep->flags & SLAB_POISON) &&
2188 is_debug_pagealloc_cache(cachep))
2189 cachep->flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
2190#endif
2191
2192 if (OFF_SLAB(cachep)) {
Joonsoo Kim158e3192016-03-15 14:54:35 -07002193 cachep->freelist_cache =
2194 kmalloc_slab(cachep->freelist_size, 0u);
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002197 err = setup_cpu_cache(cachep, gfp);
2198 if (err) {
Dmitry Safonov52b4b952016-02-17 13:11:37 -08002199 __kmem_cache_release(cachep);
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002200 return err;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002203 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205
2206#if DEBUG
2207static void check_irq_off(void)
2208{
2209 BUG_ON(!irqs_disabled());
2210}
2211
2212static void check_irq_on(void)
2213{
2214 BUG_ON(irqs_disabled());
2215}
2216
Joonsoo Kim18726ca2016-05-19 17:10:02 -07002217static void check_mutex_acquired(void)
2218{
2219 BUG_ON(!mutex_is_locked(&slab_mutex));
2220}
2221
Pekka Enberg343e0d72006-02-01 03:05:50 -08002222static void check_spinlock_acquired(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
2224#ifdef CONFIG_SMP
2225 check_irq_off();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002226 assert_spin_locked(&get_node(cachep, numa_mem_id())->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227#endif
2228}
Christoph Lametere498be72005-09-09 13:03:32 -07002229
Pekka Enberg343e0d72006-02-01 03:05:50 -08002230static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07002231{
2232#ifdef CONFIG_SMP
2233 check_irq_off();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002234 assert_spin_locked(&get_node(cachep, node)->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002235#endif
2236}
2237
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238#else
2239#define check_irq_off() do { } while(0)
2240#define check_irq_on() do { } while(0)
Joonsoo Kim18726ca2016-05-19 17:10:02 -07002241#define check_mutex_acquired() do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242#define check_spinlock_acquired(x) do { } while(0)
Christoph Lametere498be72005-09-09 13:03:32 -07002243#define check_spinlock_acquired_node(x, y) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244#endif
2245
Joonsoo Kim18726ca2016-05-19 17:10:02 -07002246static void drain_array_locked(struct kmem_cache *cachep, struct array_cache *ac,
2247 int node, bool free_all, struct list_head *list)
2248{
2249 int tofree;
2250
2251 if (!ac || !ac->avail)
2252 return;
2253
2254 tofree = free_all ? ac->avail : (ac->limit + 4) / 5;
2255 if (tofree > ac->avail)
2256 tofree = (ac->avail + 1) / 2;
2257
2258 free_block(cachep, ac->entry, tofree, node, list);
2259 ac->avail -= tofree;
2260 memmove(ac->entry, &(ac->entry[tofree]), sizeof(void *) * ac->avail);
2261}
Christoph Lameteraab22072006-03-22 00:09:06 -08002262
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263static void do_drain(void *arg)
2264{
Andrew Mortona737b3e2006-03-22 00:08:11 -08002265 struct kmem_cache *cachep = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 struct array_cache *ac;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07002267 int node = numa_mem_id();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002268 struct kmem_cache_node *n;
Joonsoo Kim97654df2014-08-06 16:04:25 -07002269 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
2271 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002272 ac = cpu_cache_get(cachep);
Christoph Lameter18bf8542014-08-06 16:04:11 -07002273 n = get_node(cachep, node);
2274 spin_lock(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07002275 free_block(cachep, ac->entry, ac->avail, node, &list);
Christoph Lameter18bf8542014-08-06 16:04:11 -07002276 spin_unlock(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07002277 slabs_destroy(cachep, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 ac->avail = 0;
2279}
2280
Pekka Enberg343e0d72006-02-01 03:05:50 -08002281static void drain_cpu_caches(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002283 struct kmem_cache_node *n;
Christoph Lametere498be72005-09-09 13:03:32 -07002284 int node;
Joonsoo Kim18726ca2016-05-19 17:10:02 -07002285 LIST_HEAD(list);
Christoph Lametere498be72005-09-09 13:03:32 -07002286
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002287 on_each_cpu(do_drain, cachep, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 check_irq_on();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002289 for_each_kmem_cache_node(cachep, node, n)
2290 if (n->alien)
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002291 drain_alien_cache(cachep, n->alien);
Roland Dreiera4523a82006-05-15 11:41:00 -07002292
Joonsoo Kim18726ca2016-05-19 17:10:02 -07002293 for_each_kmem_cache_node(cachep, node, n) {
2294 spin_lock_irq(&n->list_lock);
2295 drain_array_locked(cachep, n->shared, node, true, &list);
2296 spin_unlock_irq(&n->list_lock);
2297
2298 slabs_destroy(cachep, &list);
2299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300}
2301
Christoph Lametered11d9e2006-06-30 01:55:45 -07002302/*
2303 * Remove slabs from the list of free slabs.
2304 * Specify the number of slabs to drain in tofree.
2305 *
2306 * Returns the actual number of slabs released.
2307 */
2308static int drain_freelist(struct kmem_cache *cache,
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002309 struct kmem_cache_node *n, int tofree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
Christoph Lametered11d9e2006-06-30 01:55:45 -07002311 struct list_head *p;
2312 int nr_freed;
Joonsoo Kim8456a642013-10-24 10:07:49 +09002313 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Christoph Lametered11d9e2006-06-30 01:55:45 -07002315 nr_freed = 0;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002316 while (nr_freed < tofree && !list_empty(&n->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002318 spin_lock_irq(&n->list_lock);
2319 p = n->slabs_free.prev;
2320 if (p == &n->slabs_free) {
2321 spin_unlock_irq(&n->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002322 goto out;
2323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Joonsoo Kim8456a642013-10-24 10:07:49 +09002325 page = list_entry(p, struct page, lru);
Joonsoo Kim8456a642013-10-24 10:07:49 +09002326 list_del(&page->lru);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002327 /*
2328 * Safe to drop the lock. The slab is no longer linked
2329 * to the cache.
2330 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002331 n->free_objects -= cache->num;
2332 spin_unlock_irq(&n->list_lock);
Joonsoo Kim8456a642013-10-24 10:07:49 +09002333 slab_destroy(cache, page);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002334 nr_freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
Christoph Lametered11d9e2006-06-30 01:55:45 -07002336out:
2337 return nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338}
2339
Vladimir Davydovd6e0b7f2015-02-12 14:59:47 -08002340int __kmem_cache_shrink(struct kmem_cache *cachep, bool deactivate)
Christoph Lametere498be72005-09-09 13:03:32 -07002341{
Christoph Lameter18bf8542014-08-06 16:04:11 -07002342 int ret = 0;
2343 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002344 struct kmem_cache_node *n;
Christoph Lametere498be72005-09-09 13:03:32 -07002345
2346 drain_cpu_caches(cachep);
2347
2348 check_irq_on();
Christoph Lameter18bf8542014-08-06 16:04:11 -07002349 for_each_kmem_cache_node(cachep, node, n) {
Joonsoo Kima5aa63a2016-05-19 17:10:08 -07002350 drain_freelist(cachep, n, INT_MAX);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002351
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002352 ret += !list_empty(&n->slabs_full) ||
2353 !list_empty(&n->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07002354 }
2355 return (ret ? 1 : 0);
2356}
2357
Christoph Lameter945cf2b2012-09-04 23:18:33 +00002358int __kmem_cache_shutdown(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
Dmitry Safonov52b4b952016-02-17 13:11:37 -08002360 return __kmem_cache_shrink(cachep, false);
2361}
2362
2363void __kmem_cache_release(struct kmem_cache *cachep)
2364{
Christoph Lameter12c36672012-09-04 23:38:33 +00002365 int i;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002366 struct kmem_cache_node *n;
Christoph Lameter12c36672012-09-04 23:38:33 +00002367
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07002368 free_percpu(cachep->cpu_cache);
Christoph Lameter12c36672012-09-04 23:38:33 +00002369
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002370 /* NUMA: free the node structures */
Christoph Lameter18bf8542014-08-06 16:04:11 -07002371 for_each_kmem_cache_node(cachep, i, n) {
2372 kfree(n->shared);
2373 free_alien_cache(n->alien);
2374 kfree(n);
2375 cachep->node[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002379/*
2380 * Get the memory for a slab management obj.
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08002381 *
2382 * For a slab cache when the slab descriptor is off-slab, the
2383 * slab descriptor can't come from the same cache which is being created,
2384 * Because if it is the case, that means we defer the creation of
2385 * the kmalloc_{dma,}_cache of size sizeof(slab descriptor) to this point.
2386 * And we eventually call down to __kmem_cache_create(), which
2387 * in turn looks up in the kmalloc_{dma,}_caches for the disired-size one.
2388 * This is a "chicken-and-egg" problem.
2389 *
2390 * So the off-slab slab descriptor shall come from the kmalloc_{dma,}_caches,
2391 * which are all initialized during kmem_cache_init().
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002392 */
Joonsoo Kim7e007352013-10-30 19:04:01 +09002393static void *alloc_slabmgmt(struct kmem_cache *cachep,
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002394 struct page *page, int colour_off,
2395 gfp_t local_flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396{
Joonsoo Kim7e007352013-10-30 19:04:01 +09002397 void *freelist;
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002398 void *addr = page_address(page);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002399
Joonsoo Kim2e6b3602016-03-15 14:54:30 -07002400 page->s_mem = addr + colour_off;
2401 page->active = 0;
2402
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002403 if (OBJFREELIST_SLAB(cachep))
2404 freelist = NULL;
2405 else if (OFF_SLAB(cachep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 /* Slab management obj is off-slab. */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002407 freelist = kmem_cache_alloc_node(cachep->freelist_cache,
Pekka Enberg8759ec52008-11-26 10:01:31 +02002408 local_flags, nodeid);
Joonsoo Kim8456a642013-10-24 10:07:49 +09002409 if (!freelist)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 return NULL;
2411 } else {
Joonsoo Kim2e6b3602016-03-15 14:54:30 -07002412 /* We will use last bytes at the slab for freelist */
2413 freelist = addr + (PAGE_SIZE << cachep->gfporder) -
2414 cachep->freelist_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 }
Joonsoo Kim2e6b3602016-03-15 14:54:30 -07002416
Joonsoo Kim8456a642013-10-24 10:07:49 +09002417 return freelist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
2419
Joonsoo Kim7cc689732014-04-18 16:24:09 +09002420static inline freelist_idx_t get_free_obj(struct page *page, unsigned int idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421{
Joonsoo Kima41adfa2013-12-02 17:49:42 +09002422 return ((freelist_idx_t *)page->freelist)[idx];
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002423}
2424
2425static inline void set_free_obj(struct page *page,
Joonsoo Kim7cc689732014-04-18 16:24:09 +09002426 unsigned int idx, freelist_idx_t val)
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002427{
Joonsoo Kima41adfa2013-12-02 17:49:42 +09002428 ((freelist_idx_t *)(page->freelist))[idx] = val;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429}
2430
Joonsoo Kim10b2e9e2016-03-15 14:54:47 -07002431static void cache_init_objs_debug(struct kmem_cache *cachep, struct page *page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
Joonsoo Kim10b2e9e2016-03-15 14:54:47 -07002433#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 int i;
2435
2436 for (i = 0; i < cachep->num; i++) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09002437 void *objp = index_to_obj(cachep, page, i);
Joonsoo Kim10b2e9e2016-03-15 14:54:47 -07002438
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439 if (cachep->flags & SLAB_STORE_USER)
2440 *dbg_userword(cachep, objp) = NULL;
2441
2442 if (cachep->flags & SLAB_RED_ZONE) {
2443 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2444 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2445 }
2446 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002447 * Constructors are not allowed to allocate memory from the same
2448 * cache which they are a constructor for. Otherwise, deadlock.
2449 * They must also be threaded.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 */
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -07002451 if (cachep->ctor && !(cachep->flags & SLAB_POISON)) {
2452 kasan_unpoison_object_data(cachep,
2453 objp + obj_offset(cachep));
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002454 cachep->ctor(objp + obj_offset(cachep));
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -07002455 kasan_poison_object_data(
2456 cachep, objp + obj_offset(cachep));
2457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458
2459 if (cachep->flags & SLAB_RED_ZONE) {
2460 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
Joe Perches756a025f02016-03-17 14:19:47 -07002461 slab_error(cachep, "constructor overwrote the end of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
Joe Perches756a025f02016-03-17 14:19:47 -07002463 slab_error(cachep, "constructor overwrote the start of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 }
Joonsoo Kim40b44132016-03-15 14:54:21 -07002465 /* need to poison the objs? */
2466 if (cachep->flags & SLAB_POISON) {
2467 poison_obj(cachep, objp, POISON_FREE);
2468 slab_kernel_map(cachep, objp, 0, 0);
2469 }
Joonsoo Kim10b2e9e2016-03-15 14:54:47 -07002470 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471#endif
Joonsoo Kim10b2e9e2016-03-15 14:54:47 -07002472}
2473
2474static void cache_init_objs(struct kmem_cache *cachep,
2475 struct page *page)
2476{
2477 int i;
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -07002478 void *objp;
Joonsoo Kim10b2e9e2016-03-15 14:54:47 -07002479
2480 cache_init_objs_debug(cachep, page);
2481
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002482 if (OBJFREELIST_SLAB(cachep)) {
2483 page->freelist = index_to_obj(cachep, page, cachep->num - 1) +
2484 obj_offset(cachep);
2485 }
2486
Joonsoo Kim10b2e9e2016-03-15 14:54:47 -07002487 for (i = 0; i < cachep->num; i++) {
2488 /* constructor could break poison info */
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -07002489 if (DEBUG == 0 && cachep->ctor) {
2490 objp = index_to_obj(cachep, page, i);
2491 kasan_unpoison_object_data(cachep, objp);
2492 cachep->ctor(objp);
2493 kasan_poison_object_data(cachep, objp);
2494 }
Joonsoo Kim10b2e9e2016-03-15 14:54:47 -07002495
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002496 set_free_obj(page, i, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498}
2499
Pekka Enberg343e0d72006-02-01 03:05:50 -08002500static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501{
Christoph Lameter4b51d662007-02-10 01:43:10 -08002502 if (CONFIG_ZONE_DMA_FLAG) {
2503 if (flags & GFP_DMA)
Glauber Costaa618e892012-06-14 16:17:21 +04002504 BUG_ON(!(cachep->allocflags & GFP_DMA));
Christoph Lameter4b51d662007-02-10 01:43:10 -08002505 else
Glauber Costaa618e892012-06-14 16:17:21 +04002506 BUG_ON(cachep->allocflags & GFP_DMA);
Christoph Lameter4b51d662007-02-10 01:43:10 -08002507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508}
2509
Joonsoo Kim260b61d2016-03-15 14:54:12 -07002510static void *slab_get_obj(struct kmem_cache *cachep, struct page *page)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002511{
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002512 void *objp;
Matthew Dobson78d382d2006-02-01 03:05:47 -08002513
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002514 objp = index_to_obj(cachep, page, get_free_obj(page, page->active));
Joonsoo Kim8456a642013-10-24 10:07:49 +09002515 page->active++;
Matthew Dobson78d382d2006-02-01 03:05:47 -08002516
Joonsoo Kimd31676d2016-03-15 14:54:24 -07002517#if DEBUG
2518 if (cachep->flags & SLAB_STORE_USER)
2519 set_store_user_dirty(cachep);
2520#endif
2521
Matthew Dobson78d382d2006-02-01 03:05:47 -08002522 return objp;
2523}
2524
Joonsoo Kim260b61d2016-03-15 14:54:12 -07002525static void slab_put_obj(struct kmem_cache *cachep,
2526 struct page *page, void *objp)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002527{
Joonsoo Kim8456a642013-10-24 10:07:49 +09002528 unsigned int objnr = obj_to_index(cachep, page, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002529#if DEBUG
Joonsoo Kim16025172013-10-24 10:07:46 +09002530 unsigned int i;
Matthew Dobson78d382d2006-02-01 03:05:47 -08002531
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002532 /* Verify double free bug */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002533 for (i = page->active; i < cachep->num; i++) {
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002534 if (get_free_obj(page, i) == objnr) {
Joe Perches11705322016-03-17 14:19:50 -07002535 pr_err("slab: double free detected in cache '%s', objp %p\n",
Joe Perches756a025f02016-03-17 14:19:47 -07002536 cachep->name, objp);
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09002537 BUG();
2538 }
Matthew Dobson78d382d2006-02-01 03:05:47 -08002539 }
2540#endif
Joonsoo Kim8456a642013-10-24 10:07:49 +09002541 page->active--;
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002542 if (!page->freelist)
2543 page->freelist = objp + obj_offset(cachep);
2544
Joonsoo Kime5c58df2013-12-02 17:49:40 +09002545 set_free_obj(page, page->active, objnr);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002546}
2547
Pekka Enberg47768742006-06-23 02:03:07 -07002548/*
2549 * Map pages beginning at addr to the given cache and slab. This is required
2550 * for the slab allocator to be able to lookup the cache and slab of a
Nick Pigginccd35fb2011-01-07 17:49:17 +11002551 * virtual address for kfree, ksize, and slab debugging.
Pekka Enberg47768742006-06-23 02:03:07 -07002552 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002553static void slab_map_pages(struct kmem_cache *cache, struct page *page,
Joonsoo Kim7e007352013-10-30 19:04:01 +09002554 void *freelist)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
Joonsoo Kima57a4982013-10-24 10:07:44 +09002556 page->slab_cache = cache;
Joonsoo Kim8456a642013-10-24 10:07:49 +09002557 page->freelist = freelist;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558}
2559
2560/*
2561 * Grow (by 1) the number of slabs within a cache. This is called by
2562 * kmem_cache_alloc() when there are no active objs left in a cache.
2563 */
Joonsoo Kim76b342b2016-05-19 17:10:26 -07002564static struct page *cache_grow_begin(struct kmem_cache *cachep,
2565 gfp_t flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566{
Joonsoo Kim7e007352013-10-30 19:04:01 +09002567 void *freelist;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002568 size_t offset;
2569 gfp_t local_flags;
Joonsoo Kim511e3a02016-05-19 17:10:23 -07002570 int page_node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002571 struct kmem_cache_node *n;
Joonsoo Kim511e3a02016-05-19 17:10:23 -07002572 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573
Andrew Mortona737b3e2006-03-22 00:08:11 -08002574 /*
2575 * Be lazy and only check for valid flags here, keeping it out of the
2576 * critical path in kmem_cache_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 */
Andrew Mortonc871ac42014-12-10 15:42:25 -08002578 if (unlikely(flags & GFP_SLAB_BUG_MASK)) {
2579 pr_emerg("gfp: %u\n", flags & GFP_SLAB_BUG_MASK);
2580 BUG();
2581 }
Christoph Lameter6cb06222007-10-16 01:25:41 -07002582 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 check_irq_off();
Mel Gormand0164ad2015-11-06 16:28:21 -08002585 if (gfpflags_allow_blocking(local_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 local_irq_enable();
2587
2588 /*
2589 * The test for missing atomic flag is performed here, rather than
2590 * the more obvious place, simply to reduce the critical path length
2591 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2592 * will eventually be caught here (where it matters).
2593 */
2594 kmem_flagcheck(cachep, flags);
2595
Andrew Mortona737b3e2006-03-22 00:08:11 -08002596 /*
2597 * Get mem for the objs. Attempt to allocate a physical page from
2598 * 'nodeid'.
Christoph Lametere498be72005-09-09 13:03:32 -07002599 */
Joonsoo Kim511e3a02016-05-19 17:10:23 -07002600 page = kmem_getpages(cachep, local_flags, nodeid);
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002601 if (!page)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 goto failed;
2603
Joonsoo Kim511e3a02016-05-19 17:10:23 -07002604 page_node = page_to_nid(page);
2605 n = get_node(cachep, page_node);
Joonsoo Kim03d1d432016-05-19 17:10:20 -07002606
2607 /* Get colour for the slab, and cal the next value. */
2608 n->colour_next++;
2609 if (n->colour_next >= cachep->colour)
2610 n->colour_next = 0;
2611
2612 offset = n->colour_next;
2613 if (offset >= cachep->colour)
2614 offset = 0;
2615
2616 offset *= cachep->colour_off;
2617
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 /* Get slab management. */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002619 freelist = alloc_slabmgmt(cachep, page, offset,
Joonsoo Kim511e3a02016-05-19 17:10:23 -07002620 local_flags & ~GFP_CONSTRAINT_MASK, page_node);
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002621 if (OFF_SLAB(cachep) && !freelist)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622 goto opps1;
2623
Joonsoo Kim8456a642013-10-24 10:07:49 +09002624 slab_map_pages(cachep, page, freelist);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -07002626 kasan_poison_slab(page);
Joonsoo Kim8456a642013-10-24 10:07:49 +09002627 cache_init_objs(cachep, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628
Mel Gormand0164ad2015-11-06 16:28:21 -08002629 if (gfpflags_allow_blocking(local_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 local_irq_disable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631
Joonsoo Kim76b342b2016-05-19 17:10:26 -07002632 return page;
2633
Andrew Mortona737b3e2006-03-22 00:08:11 -08002634opps1:
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09002635 kmem_freepages(cachep, page);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002636failed:
Mel Gormand0164ad2015-11-06 16:28:21 -08002637 if (gfpflags_allow_blocking(local_flags))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 local_irq_disable();
Joonsoo Kim76b342b2016-05-19 17:10:26 -07002639 return NULL;
2640}
2641
2642static void cache_grow_end(struct kmem_cache *cachep, struct page *page)
2643{
2644 struct kmem_cache_node *n;
2645 void *list = NULL;
2646
2647 check_irq_off();
2648
2649 if (!page)
2650 return;
2651
2652 INIT_LIST_HEAD(&page->lru);
2653 n = get_node(cachep, page_to_nid(page));
2654
2655 spin_lock(&n->list_lock);
2656 if (!page->active)
2657 list_add_tail(&page->lru, &(n->slabs_free));
2658 else
2659 fixup_slab_list(cachep, n, page, &list);
2660 STATS_INC_GROWN(cachep);
2661 n->free_objects += cachep->num - page->active;
2662 spin_unlock(&n->list_lock);
2663
2664 fixup_objfreelist_debug(cachep, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665}
2666
2667#if DEBUG
2668
2669/*
2670 * Perform extra freeing checks:
2671 * - detect bad pointers.
2672 * - POISON/RED_ZONE checking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 */
2674static void kfree_debugcheck(const void *objp)
2675{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 if (!virt_addr_valid(objp)) {
Joe Perches11705322016-03-17 14:19:50 -07002677 pr_err("kfree_debugcheck: out of range ptr %lxh\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002678 (unsigned long)objp);
2679 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681}
2682
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002683static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2684{
David Woodhouseb46b8f12007-05-08 00:22:59 -07002685 unsigned long long redzone1, redzone2;
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002686
2687 redzone1 = *dbg_redzone1(cache, obj);
2688 redzone2 = *dbg_redzone2(cache, obj);
2689
2690 /*
2691 * Redzone is ok.
2692 */
2693 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2694 return;
2695
2696 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2697 slab_error(cache, "double free detected");
2698 else
2699 slab_error(cache, "memory outside object was overwritten");
2700
Joe Perches11705322016-03-17 14:19:50 -07002701 pr_err("%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
2702 obj, redzone1, redzone2);
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002703}
2704
Pekka Enberg343e0d72006-02-01 03:05:50 -08002705static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002706 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 unsigned int objnr;
Joonsoo Kim8456a642013-10-24 10:07:49 +09002709 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710
Matthew Wilcox80cbd912007-11-29 12:05:13 -07002711 BUG_ON(virt_to_cache(objp) != cachep);
2712
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002713 objp -= obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 kfree_debugcheck(objp);
Christoph Lameterb49af682007-05-06 14:49:41 -07002715 page = virt_to_head_page(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 if (cachep->flags & SLAB_RED_ZONE) {
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002718 verify_redzone_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2720 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2721 }
Joonsoo Kimd31676d2016-03-15 14:54:24 -07002722 if (cachep->flags & SLAB_STORE_USER) {
2723 set_store_user_dirty(cachep);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002724 *dbg_userword(cachep, objp) = (void *)caller;
Joonsoo Kimd31676d2016-03-15 14:54:24 -07002725 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726
Joonsoo Kim8456a642013-10-24 10:07:49 +09002727 objnr = obj_to_index(cachep, page, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728
2729 BUG_ON(objnr >= cachep->num);
Joonsoo Kim8456a642013-10-24 10:07:49 +09002730 BUG_ON(objp != index_to_obj(cachep, page, objnr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 poison_obj(cachep, objp, POISON_FREE);
Joonsoo Kim40b44132016-03-15 14:54:21 -07002734 slab_kernel_map(cachep, objp, 0, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 }
2736 return objp;
2737}
2738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739#else
2740#define kfree_debugcheck(x) do { } while(0)
2741#define cache_free_debugcheck(x,objp,z) (objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742#endif
2743
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002744static inline void fixup_objfreelist_debug(struct kmem_cache *cachep,
2745 void **list)
2746{
2747#if DEBUG
2748 void *next = *list;
2749 void *objp;
2750
2751 while (next) {
2752 objp = next - obj_offset(cachep);
2753 next = *(void **)next;
2754 poison_obj(cachep, objp, POISON_FREE);
2755 }
2756#endif
2757}
2758
Joonsoo Kimd8410232016-03-15 14:54:44 -07002759static inline void fixup_slab_list(struct kmem_cache *cachep,
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002760 struct kmem_cache_node *n, struct page *page,
2761 void **list)
Joonsoo Kimd8410232016-03-15 14:54:44 -07002762{
2763 /* move slabp to correct slabp list: */
2764 list_del(&page->lru);
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002765 if (page->active == cachep->num) {
Joonsoo Kimd8410232016-03-15 14:54:44 -07002766 list_add(&page->lru, &n->slabs_full);
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002767 if (OBJFREELIST_SLAB(cachep)) {
2768#if DEBUG
2769 /* Poisoning will be done without holding the lock */
2770 if (cachep->flags & SLAB_POISON) {
2771 void **objp = page->freelist;
2772
2773 *objp = *list;
2774 *list = objp;
2775 }
2776#endif
2777 page->freelist = NULL;
2778 }
2779 } else
Joonsoo Kimd8410232016-03-15 14:54:44 -07002780 list_add(&page->lru, &n->slabs_partial);
2781}
2782
Joonsoo Kimf68f8dd2016-03-15 14:54:56 -07002783/* Try to find non-pfmemalloc slab if needed */
2784static noinline struct page *get_valid_first_slab(struct kmem_cache_node *n,
2785 struct page *page, bool pfmemalloc)
2786{
2787 if (!page)
2788 return NULL;
2789
2790 if (pfmemalloc)
2791 return page;
2792
2793 if (!PageSlabPfmemalloc(page))
2794 return page;
2795
2796 /* No need to keep pfmemalloc slab if we have enough free objects */
2797 if (n->free_objects > n->free_limit) {
2798 ClearPageSlabPfmemalloc(page);
2799 return page;
2800 }
2801
2802 /* Move pfmemalloc slab to the end of list to speed up next search */
2803 list_del(&page->lru);
2804 if (!page->active)
2805 list_add_tail(&page->lru, &n->slabs_free);
2806 else
2807 list_add_tail(&page->lru, &n->slabs_partial);
2808
2809 list_for_each_entry(page, &n->slabs_partial, lru) {
2810 if (!PageSlabPfmemalloc(page))
2811 return page;
2812 }
2813
2814 list_for_each_entry(page, &n->slabs_free, lru) {
2815 if (!PageSlabPfmemalloc(page))
2816 return page;
2817 }
2818
2819 return NULL;
2820}
2821
2822static struct page *get_first_slab(struct kmem_cache_node *n, bool pfmemalloc)
Geliang Tang7aa0d222016-01-14 15:18:02 -08002823{
2824 struct page *page;
2825
2826 page = list_first_entry_or_null(&n->slabs_partial,
2827 struct page, lru);
2828 if (!page) {
2829 n->free_touched = 1;
2830 page = list_first_entry_or_null(&n->slabs_free,
2831 struct page, lru);
2832 }
2833
Joonsoo Kimf68f8dd2016-03-15 14:54:56 -07002834 if (sk_memalloc_socks())
2835 return get_valid_first_slab(n, page, pfmemalloc);
2836
Geliang Tang7aa0d222016-01-14 15:18:02 -08002837 return page;
2838}
2839
Joonsoo Kimf68f8dd2016-03-15 14:54:56 -07002840static noinline void *cache_alloc_pfmemalloc(struct kmem_cache *cachep,
2841 struct kmem_cache_node *n, gfp_t flags)
2842{
2843 struct page *page;
2844 void *obj;
2845 void *list = NULL;
2846
2847 if (!gfp_pfmemalloc_allowed(flags))
2848 return NULL;
2849
2850 spin_lock(&n->list_lock);
2851 page = get_first_slab(n, true);
2852 if (!page) {
2853 spin_unlock(&n->list_lock);
2854 return NULL;
2855 }
2856
2857 obj = slab_get_obj(cachep, page);
2858 n->free_objects--;
2859
2860 fixup_slab_list(cachep, n, page, &list);
2861
2862 spin_unlock(&n->list_lock);
2863 fixup_objfreelist_debug(cachep, &list);
2864
2865 return obj;
2866}
2867
2868static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869{
2870 int batchcount;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002871 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 struct array_cache *ac;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002873 int node;
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002874 void *list = NULL;
Joonsoo Kim76b342b2016-05-19 17:10:26 -07002875 struct page *page;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002876
Joe Korty6d2144d2008-03-05 15:04:59 -08002877 check_irq_off();
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07002878 node = numa_mem_id();
Joonsoo Kimf68f8dd2016-03-15 14:54:56 -07002879
Mel Gorman072bb0a2012-07-31 16:43:58 -07002880retry:
Joe Korty6d2144d2008-03-05 15:04:59 -08002881 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 batchcount = ac->batchcount;
2883 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002884 /*
2885 * If there was little recent activity on this cache, then
2886 * perform only a partial refill. Otherwise we could generate
2887 * refill bouncing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 */
2889 batchcount = BATCHREFILL_LIMIT;
2890 }
Christoph Lameter18bf8542014-08-06 16:04:11 -07002891 n = get_node(cachep, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002893 BUG_ON(ac->avail > 0 || !n);
2894 spin_lock(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002895
Christoph Lameter3ded1752006-03-25 03:06:44 -08002896 /* See if we can refill from the shared array */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002897 if (n->shared && transfer_objects(ac, n->shared, batchcount)) {
2898 n->shared->touched = 1;
Christoph Lameter3ded1752006-03-25 03:06:44 -08002899 goto alloc_done;
Nick Piggin44b57f12010-01-27 22:27:40 +11002900 }
Christoph Lameter3ded1752006-03-25 03:06:44 -08002901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 while (batchcount > 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 /* Get slab alloc is to come from. */
Joonsoo Kimf68f8dd2016-03-15 14:54:56 -07002904 page = get_first_slab(n, false);
Geliang Tang7aa0d222016-01-14 15:18:02 -08002905 if (!page)
2906 goto must_grow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
Linus Torvalds1da177e2005-04-16 15:20:36 -07002908 check_spinlock_acquired(cachep);
Pekka Enberg714b81712007-05-06 14:49:03 -07002909
2910 /*
2911 * The slab was either on partial or free list so
2912 * there must be at least one object available for
2913 * allocation.
2914 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09002915 BUG_ON(page->active >= cachep->num);
Pekka Enberg714b81712007-05-06 14:49:03 -07002916
Joonsoo Kim8456a642013-10-24 10:07:49 +09002917 while (page->active < cachep->num && batchcount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 STATS_INC_ALLOCED(cachep);
2919 STATS_INC_ACTIVE(cachep);
2920 STATS_SET_HIGH(cachep);
2921
Joonsoo Kimf68f8dd2016-03-15 14:54:56 -07002922 ac->entry[ac->avail++] = slab_get_obj(cachep, page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002925 fixup_slab_list(cachep, n, page, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 }
2927
Andrew Mortona737b3e2006-03-22 00:08:11 -08002928must_grow:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002929 n->free_objects -= ac->avail;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002930alloc_done:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00002931 spin_unlock(&n->list_lock);
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07002932 fixup_objfreelist_debug(cachep, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
2934 if (unlikely(!ac->avail)) {
Joonsoo Kimf68f8dd2016-03-15 14:54:56 -07002935 /* Check if we can use obj in pfmemalloc slab */
2936 if (sk_memalloc_socks()) {
2937 void *obj = cache_alloc_pfmemalloc(cachep, n, flags);
2938
2939 if (obj)
2940 return obj;
2941 }
2942
Joonsoo Kim76b342b2016-05-19 17:10:26 -07002943 page = cache_grow_begin(cachep, gfp_exact_node(flags), node);
2944 cache_grow_end(cachep, page);
Christoph Lametere498be72005-09-09 13:03:32 -07002945
Joonsoo Kim76b342b2016-05-19 17:10:26 -07002946 /*
2947 * cache_grow_begin() can reenable interrupts,
2948 * then ac could change.
2949 */
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002950 ac = cpu_cache_get(cachep);
David Rientjes51cd8e62012-08-28 19:57:21 -07002951 node = numa_mem_id();
Mel Gorman072bb0a2012-07-31 16:43:58 -07002952
2953 /* no objects in sight? abort */
Joonsoo Kim76b342b2016-05-19 17:10:26 -07002954 if (!page && ac->avail == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 return NULL;
2956
Andrew Mortona737b3e2006-03-22 00:08:11 -08002957 if (!ac->avail) /* objects refilled by interrupt? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 goto retry;
2959 }
2960 ac->touched = 1;
Mel Gorman072bb0a2012-07-31 16:43:58 -07002961
Joonsoo Kimf68f8dd2016-03-15 14:54:56 -07002962 return ac->entry[--ac->avail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963}
2964
Andrew Mortona737b3e2006-03-22 00:08:11 -08002965static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
2966 gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967{
Mel Gormand0164ad2015-11-06 16:28:21 -08002968 might_sleep_if(gfpflags_allow_blocking(flags));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969#if DEBUG
2970 kmem_flagcheck(cachep, flags);
2971#endif
2972}
2973
2974#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08002975static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002976 gfp_t flags, void *objp, unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002978 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 return objp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002980 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 check_poison_obj(cachep, objp);
Joonsoo Kim40b44132016-03-15 14:54:21 -07002982 slab_kernel_map(cachep, objp, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 poison_obj(cachep, objp, POISON_INUSE);
2984 }
2985 if (cachep->flags & SLAB_STORE_USER)
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002986 *dbg_userword(cachep, objp) = (void *)caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987
2988 if (cachep->flags & SLAB_RED_ZONE) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002989 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
2990 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
Joe Perches756a025f02016-03-17 14:19:47 -07002991 slab_error(cachep, "double free, or memory outside object was overwritten");
Joe Perches11705322016-03-17 14:19:50 -07002992 pr_err("%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
2993 objp, *dbg_redzone1(cachep, objp),
2994 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 }
2996 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
2997 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
2998 }
Joonsoo Kim03787302014-06-23 13:22:06 -07002999
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003000 objp += obj_offset(cachep);
Christoph Lameter4f104932007-05-06 14:50:17 -07003001 if (cachep->ctor && cachep->flags & SLAB_POISON)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003002 cachep->ctor(objp);
Tetsuo Handa7ea466f2011-07-21 09:42:45 +09003003 if (ARCH_SLAB_MINALIGN &&
3004 ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
Joe Perches11705322016-03-17 14:19:50 -07003005 pr_err("0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
Hugh Dickinsc2251502011-07-11 13:35:08 -07003006 objp, (int)ARCH_SLAB_MINALIGN);
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 return objp;
3009}
3010#else
3011#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
3012#endif
3013
Pekka Enberg343e0d72006-02-01 03:05:50 -08003014static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003016 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 struct array_cache *ac;
3018
Alok N Kataria5c382302005-09-27 21:45:46 -07003019 check_irq_off();
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003020
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003021 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 if (likely(ac->avail)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 ac->touched = 1;
Joonsoo Kimf68f8dd2016-03-15 14:54:56 -07003024 objp = ac->entry[--ac->avail];
Mel Gorman072bb0a2012-07-31 16:43:58 -07003025
Joonsoo Kimf68f8dd2016-03-15 14:54:56 -07003026 STATS_INC_ALLOCHIT(cachep);
3027 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 }
Mel Gorman072bb0a2012-07-31 16:43:58 -07003029
3030 STATS_INC_ALLOCMISS(cachep);
Joonsoo Kimf68f8dd2016-03-15 14:54:56 -07003031 objp = cache_alloc_refill(cachep, flags);
Mel Gorman072bb0a2012-07-31 16:43:58 -07003032 /*
3033 * the 'ac' may be updated by cache_alloc_refill(),
3034 * and kmemleak_erase() requires its correct value.
3035 */
3036 ac = cpu_cache_get(cachep);
3037
3038out:
Catalin Marinasd5cff632009-06-11 13:22:40 +01003039 /*
3040 * To avoid a false negative, if an object that is in one of the
3041 * per-CPU caches is leaked, we need to make sure kmemleak doesn't
3042 * treat the array pointers as a reference to the object.
3043 */
J. R. Okajimaf3d8b532009-12-02 16:55:49 +09003044 if (objp)
3045 kmemleak_erase(&ac->entry[ac->avail]);
Alok N Kataria5c382302005-09-27 21:45:46 -07003046 return objp;
3047}
3048
Christoph Lametere498be72005-09-09 13:03:32 -07003049#ifdef CONFIG_NUMA
3050/*
Zefan Li2ad654b2014-09-25 09:41:02 +08003051 * Try allocating on another node if PFA_SPREAD_SLAB is a mempolicy is set.
Paul Jacksonc61afb12006-03-24 03:16:08 -08003052 *
3053 * If we are in_interrupt, then process context, including cpusets and
3054 * mempolicy, may not apply and should not be used for allocation policy.
3055 */
3056static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
3057{
3058 int nid_alloc, nid_here;
3059
Christoph Lameter765c4502006-09-27 01:50:08 -07003060 if (in_interrupt() || (flags & __GFP_THISNODE))
Paul Jacksonc61afb12006-03-24 03:16:08 -08003061 return NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003062 nid_alloc = nid_here = numa_mem_id();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003063 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
Jack Steiner6adef3e2010-05-26 14:42:49 -07003064 nid_alloc = cpuset_slab_spread_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003065 else if (current->mempolicy)
David Rientjes2a389612014-04-07 15:37:29 -07003066 nid_alloc = mempolicy_slab_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003067 if (nid_alloc != nid_here)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003068 return ____cache_alloc_node(cachep, flags, nid_alloc);
Paul Jacksonc61afb12006-03-24 03:16:08 -08003069 return NULL;
3070}
3071
3072/*
Christoph Lameter765c4502006-09-27 01:50:08 -07003073 * Fallback function if there was no memory available and no objects on a
Christoph Lameter3c517a62006-12-06 20:33:29 -08003074 * certain node and fall back is permitted. First we scan all the
Christoph Lameter6a673682013-01-10 19:14:19 +00003075 * available node for available objects. If that fails then we
Christoph Lameter3c517a62006-12-06 20:33:29 -08003076 * perform an allocation without specifying a node. This allows the page
3077 * allocator to do its reclaim / fallback magic. We then insert the
3078 * slab into the proper nodelist and then allocate from it.
Christoph Lameter765c4502006-09-27 01:50:08 -07003079 */
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003080static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
Christoph Lameter765c4502006-09-27 01:50:08 -07003081{
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003082 struct zonelist *zonelist;
Mel Gormandd1a2392008-04-28 02:12:17 -07003083 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07003084 struct zone *zone;
3085 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003086 void *obj = NULL;
Joonsoo Kim76b342b2016-05-19 17:10:26 -07003087 struct page *page;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003088 int nid;
Mel Gormancc9a6c82012-03-21 16:34:11 -07003089 unsigned int cpuset_mems_cookie;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003090
3091 if (flags & __GFP_THISNODE)
3092 return NULL;
3093
Mel Gormancc9a6c82012-03-21 16:34:11 -07003094retry_cpuset:
Mel Gormand26914d2014-04-03 14:47:24 -07003095 cpuset_mems_cookie = read_mems_allowed_begin();
David Rientjes2a389612014-04-07 15:37:29 -07003096 zonelist = node_zonelist(mempolicy_slab_node(), flags);
Mel Gormancc9a6c82012-03-21 16:34:11 -07003097
Christoph Lameter3c517a62006-12-06 20:33:29 -08003098retry:
3099 /*
3100 * Look through allowed nodes for objects available
3101 * from existing per node queues.
3102 */
Mel Gorman54a6eb52008-04-28 02:12:16 -07003103 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
3104 nid = zone_to_nid(zone);
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003105
Vladimir Davydov061d7072014-12-12 16:58:25 -08003106 if (cpuset_zone_allowed(zone, flags) &&
Christoph Lameter18bf8542014-08-06 16:04:11 -07003107 get_node(cache, nid) &&
3108 get_node(cache, nid)->free_objects) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003109 obj = ____cache_alloc_node(cache,
David Rientjes4167e9b2015-04-14 15:46:55 -07003110 gfp_exact_node(flags), nid);
Christoph Lameter481c5342008-06-21 16:46:35 -07003111 if (obj)
3112 break;
3113 }
Christoph Lameter3c517a62006-12-06 20:33:29 -08003114 }
3115
Christoph Lametercfce6602007-05-06 14:50:17 -07003116 if (!obj) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003117 /*
3118 * This allocation will be performed within the constraints
3119 * of the current cpuset / memory policy requirements.
3120 * We may trigger various forms of reclaim on the allowed
3121 * set and go into memory reserves if necessary.
3122 */
Joonsoo Kim76b342b2016-05-19 17:10:26 -07003123 page = cache_grow_begin(cache, flags, numa_mem_id());
3124 cache_grow_end(cache, page);
3125 if (page) {
3126 nid = page_to_nid(page);
Joonsoo Kim511e3a02016-05-19 17:10:23 -07003127 obj = ____cache_alloc_node(cache,
3128 gfp_exact_node(flags), nid);
Joonsoo Kim0c3aa832013-10-24 10:07:38 +09003129
Christoph Lameter3c517a62006-12-06 20:33:29 -08003130 /*
Joonsoo Kim511e3a02016-05-19 17:10:23 -07003131 * Another processor may allocate the objects in
3132 * the slab since we are not holding any locks.
Christoph Lameter3c517a62006-12-06 20:33:29 -08003133 */
Joonsoo Kim511e3a02016-05-19 17:10:23 -07003134 if (!obj)
3135 goto retry;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003136 }
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003137 }
Mel Gormancc9a6c82012-03-21 16:34:11 -07003138
Mel Gormand26914d2014-04-03 14:47:24 -07003139 if (unlikely(!obj && read_mems_allowed_retry(cpuset_mems_cookie)))
Mel Gormancc9a6c82012-03-21 16:34:11 -07003140 goto retry_cpuset;
Christoph Lameter765c4502006-09-27 01:50:08 -07003141 return obj;
3142}
3143
3144/*
Christoph Lametere498be72005-09-09 13:03:32 -07003145 * A interface to enable slab creation on nodeid
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003147static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003148 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07003149{
Joonsoo Kim8456a642013-10-24 10:07:49 +09003150 struct page *page;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003151 struct kmem_cache_node *n;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003152 void *obj;
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07003153 void *list = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154
Paul Mackerras7c3fbbd2014-12-02 15:59:48 -08003155 VM_BUG_ON(nodeid < 0 || nodeid >= MAX_NUMNODES);
Christoph Lameter18bf8542014-08-06 16:04:11 -07003156 n = get_node(cachep, nodeid);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003157 BUG_ON(!n);
Christoph Lametere498be72005-09-09 13:03:32 -07003158
Andrew Mortona737b3e2006-03-22 00:08:11 -08003159retry:
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003160 check_irq_off();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003161 spin_lock(&n->list_lock);
Joonsoo Kimf68f8dd2016-03-15 14:54:56 -07003162 page = get_first_slab(n, false);
Geliang Tang7aa0d222016-01-14 15:18:02 -08003163 if (!page)
3164 goto must_grow;
Christoph Lametere498be72005-09-09 13:03:32 -07003165
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003166 check_spinlock_acquired_node(cachep, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07003167
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003168 STATS_INC_NODEALLOCS(cachep);
3169 STATS_INC_ACTIVE(cachep);
3170 STATS_SET_HIGH(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003171
Joonsoo Kim8456a642013-10-24 10:07:49 +09003172 BUG_ON(page->active == cachep->num);
Christoph Lametere498be72005-09-09 13:03:32 -07003173
Joonsoo Kim260b61d2016-03-15 14:54:12 -07003174 obj = slab_get_obj(cachep, page);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003175 n->free_objects--;
Christoph Lametere498be72005-09-09 13:03:32 -07003176
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07003177 fixup_slab_list(cachep, n, page, &list);
Christoph Lametere498be72005-09-09 13:03:32 -07003178
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003179 spin_unlock(&n->list_lock);
Joonsoo Kimb03a017b2016-03-15 14:54:50 -07003180 fixup_objfreelist_debug(cachep, &list);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003181 goto done;
Christoph Lametere498be72005-09-09 13:03:32 -07003182
Andrew Mortona737b3e2006-03-22 00:08:11 -08003183must_grow:
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003184 spin_unlock(&n->list_lock);
Joonsoo Kim76b342b2016-05-19 17:10:26 -07003185 page = cache_grow_begin(cachep, gfp_exact_node(flags), nodeid);
3186 cache_grow_end(cachep, page);
3187 if (page)
Christoph Lameter765c4502006-09-27 01:50:08 -07003188 goto retry;
Christoph Lametere498be72005-09-09 13:03:32 -07003189
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003190 return fallback_alloc(cachep, flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003191
Andrew Mortona737b3e2006-03-22 00:08:11 -08003192done:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003193 return obj;
Christoph Lametere498be72005-09-09 13:03:32 -07003194}
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003195
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003196static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003197slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003198 unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003199{
3200 unsigned long save_flags;
3201 void *ptr;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003202 int slab_node = numa_mem_id();
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003203
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003204 flags &= gfp_allowed_mask;
Jesper Dangaard Brouer011ecea2016-03-15 14:53:41 -07003205 cachep = slab_pre_alloc_hook(cachep, flags);
3206 if (unlikely(!cachep))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003207 return NULL;
3208
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003209 cache_alloc_debugcheck_before(cachep, flags);
3210 local_irq_save(save_flags);
3211
Andrew Mortoneacbbae2011-07-28 13:59:49 -07003212 if (nodeid == NUMA_NO_NODE)
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003213 nodeid = slab_node;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003214
Christoph Lameter18bf8542014-08-06 16:04:11 -07003215 if (unlikely(!get_node(cachep, nodeid))) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003216 /* Node not bootstrapped yet */
3217 ptr = fallback_alloc(cachep, flags);
3218 goto out;
3219 }
3220
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003221 if (nodeid == slab_node) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003222 /*
3223 * Use the locally cached objects if possible.
3224 * However ____cache_alloc does not allow fallback
3225 * to other nodes. It may fail while we still have
3226 * objects on other nodes available.
3227 */
3228 ptr = ____cache_alloc(cachep, flags);
3229 if (ptr)
3230 goto out;
3231 }
3232 /* ___cache_alloc_node can fall back to other nodes */
3233 ptr = ____cache_alloc_node(cachep, flags, nodeid);
3234 out:
3235 local_irq_restore(save_flags);
3236 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
3237
Jesper Dangaard Brouerd5e3ed62016-03-15 14:53:47 -07003238 if (unlikely(flags & __GFP_ZERO) && ptr)
3239 memset(ptr, 0, cachep->object_size);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003240
Jesper Dangaard Brouerd5e3ed62016-03-15 14:53:47 -07003241 slab_post_alloc_hook(cachep, flags, 1, &ptr);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003242 return ptr;
3243}
3244
3245static __always_inline void *
3246__do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
3247{
3248 void *objp;
3249
Zefan Li2ad654b2014-09-25 09:41:02 +08003250 if (current->mempolicy || cpuset_do_slab_mem_spread()) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003251 objp = alternate_node_alloc(cache, flags);
3252 if (objp)
3253 goto out;
3254 }
3255 objp = ____cache_alloc(cache, flags);
3256
3257 /*
3258 * We may just have run out of memory on the local node.
3259 * ____cache_alloc_node() knows how to locate memory on other nodes
3260 */
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003261 if (!objp)
3262 objp = ____cache_alloc_node(cache, flags, numa_mem_id());
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003263
3264 out:
3265 return objp;
3266}
3267#else
3268
3269static __always_inline void *
3270__do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
3271{
3272 return ____cache_alloc(cachep, flags);
3273}
3274
3275#endif /* CONFIG_NUMA */
3276
3277static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003278slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003279{
3280 unsigned long save_flags;
3281 void *objp;
3282
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003283 flags &= gfp_allowed_mask;
Jesper Dangaard Brouer011ecea2016-03-15 14:53:41 -07003284 cachep = slab_pre_alloc_hook(cachep, flags);
3285 if (unlikely(!cachep))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003286 return NULL;
3287
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003288 cache_alloc_debugcheck_before(cachep, flags);
3289 local_irq_save(save_flags);
3290 objp = __do_cache_alloc(cachep, flags);
3291 local_irq_restore(save_flags);
3292 objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
3293 prefetchw(objp);
3294
Jesper Dangaard Brouerd5e3ed62016-03-15 14:53:47 -07003295 if (unlikely(flags & __GFP_ZERO) && objp)
3296 memset(objp, 0, cachep->object_size);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003297
Jesper Dangaard Brouerd5e3ed62016-03-15 14:53:47 -07003298 slab_post_alloc_hook(cachep, flags, 1, &objp);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003299 return objp;
3300}
Christoph Lametere498be72005-09-09 13:03:32 -07003301
3302/*
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08003303 * Caller needs to acquire correct kmem_cache_node's list_lock
Joonsoo Kim97654df2014-08-06 16:04:25 -07003304 * @list: List of detached free slabs should be freed by caller
Christoph Lametere498be72005-09-09 13:03:32 -07003305 */
Joonsoo Kim97654df2014-08-06 16:04:25 -07003306static void free_block(struct kmem_cache *cachep, void **objpp,
3307 int nr_objects, int node, struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308{
3309 int i;
Joonsoo Kim25c063f2014-08-06 16:04:22 -07003310 struct kmem_cache_node *n = get_node(cachep, node);
Joonsoo Kim6052b782016-05-19 17:10:17 -07003311 struct page *page;
3312
3313 n->free_objects += nr_objects;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
3315 for (i = 0; i < nr_objects; i++) {
Mel Gorman072bb0a2012-07-31 16:43:58 -07003316 void *objp;
Joonsoo Kim8456a642013-10-24 10:07:49 +09003317 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Mel Gorman072bb0a2012-07-31 16:43:58 -07003319 objp = objpp[i];
3320
Joonsoo Kim8456a642013-10-24 10:07:49 +09003321 page = virt_to_head_page(objp);
Joonsoo Kim8456a642013-10-24 10:07:49 +09003322 list_del(&page->lru);
Christoph Lameterff694162005-09-22 21:44:02 -07003323 check_spinlock_acquired_node(cachep, node);
Joonsoo Kim260b61d2016-03-15 14:54:12 -07003324 slab_put_obj(cachep, page, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325 STATS_DEC_ACTIVE(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
3327 /* fixup slab chains */
Joonsoo Kim6052b782016-05-19 17:10:17 -07003328 if (page->active == 0)
3329 list_add(&page->lru, &n->slabs_free);
3330 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003331 /* Unconditionally move a slab to the end of the
3332 * partial list on free - maximum time for the
3333 * other objects to be freed, too.
3334 */
Joonsoo Kim8456a642013-10-24 10:07:49 +09003335 list_add_tail(&page->lru, &n->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 }
3337 }
Joonsoo Kim6052b782016-05-19 17:10:17 -07003338
3339 while (n->free_objects > n->free_limit && !list_empty(&n->slabs_free)) {
3340 n->free_objects -= cachep->num;
3341
3342 page = list_last_entry(&n->slabs_free, struct page, lru);
3343 list_del(&page->lru);
3344 list_add(&page->lru, list);
3345 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346}
3347
Pekka Enberg343e0d72006-02-01 03:05:50 -08003348static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349{
3350 int batchcount;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003351 struct kmem_cache_node *n;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003352 int node = numa_mem_id();
Joonsoo Kim97654df2014-08-06 16:04:25 -07003353 LIST_HEAD(list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354
3355 batchcount = ac->batchcount;
Joonsoo Kim260b61d2016-03-15 14:54:12 -07003356
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 check_irq_off();
Christoph Lameter18bf8542014-08-06 16:04:11 -07003358 n = get_node(cachep, node);
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003359 spin_lock(&n->list_lock);
3360 if (n->shared) {
3361 struct array_cache *shared_array = n->shared;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003362 int max = shared_array->limit - shared_array->avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 if (max) {
3364 if (batchcount > max)
3365 batchcount = max;
Christoph Lametere498be72005-09-09 13:03:32 -07003366 memcpy(&(shared_array->entry[shared_array->avail]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003367 ac->entry, sizeof(void *) * batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368 shared_array->avail += batchcount;
3369 goto free_done;
3370 }
3371 }
3372
Joonsoo Kim97654df2014-08-06 16:04:25 -07003373 free_block(cachep, ac->entry, batchcount, node, &list);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003374free_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375#if STATS
3376 {
3377 int i = 0;
Geliang Tang73c02192016-01-14 15:17:59 -08003378 struct page *page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379
Geliang Tang73c02192016-01-14 15:17:59 -08003380 list_for_each_entry(page, &n->slabs_free, lru) {
Joonsoo Kim8456a642013-10-24 10:07:49 +09003381 BUG_ON(page->active);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
3383 i++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 }
3385 STATS_SET_FREEABLE(cachep, i);
3386 }
3387#endif
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003388 spin_unlock(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07003389 slabs_destroy(cachep, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 ac->avail -= batchcount;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003391 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392}
3393
3394/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08003395 * Release an obj back to its cache. If the obj has a constructed state, it must
3396 * be in this state _before_ it is released. Called with disabled ints.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 */
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003398static inline void __cache_free(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003399 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400{
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003401 struct array_cache *ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -07003403 kasan_slab_free(cachep, objp);
3404
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 check_irq_off();
Catalin Marinasd5cff632009-06-11 13:22:40 +01003406 kmemleak_free_recursive(objp, cachep->flags);
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003407 objp = cache_free_debugcheck(cachep, objp, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003409 kmemcheck_slab_free(cachep, objp, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003410
Siddha, Suresh B1807a1a2007-08-22 14:01:49 -07003411 /*
3412 * Skip calling cache_free_alien() when the platform is not numa.
3413 * This will avoid cache misses that happen while accessing slabp (which
3414 * is per page memory reference) to get nodeid. Instead use a global
3415 * variable to skip the call, which is mostly likely to be present in
3416 * the cache.
3417 */
Mel Gormanb6e68bc2009-06-16 15:32:16 -07003418 if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07003419 return;
Christoph Lametere498be72005-09-09 13:03:32 -07003420
Joonsoo Kim3d880192014-10-09 15:26:04 -07003421 if (ac->avail < ac->limit) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 STATS_INC_FREEHIT(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 } else {
3424 STATS_INC_FREEMISS(cachep);
3425 cache_flusharray(cachep, ac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 }
Zhao Jin42c8c992011-08-27 00:26:17 +08003427
Joonsoo Kimf68f8dd2016-03-15 14:54:56 -07003428 if (sk_memalloc_socks()) {
3429 struct page *page = virt_to_head_page(objp);
3430
3431 if (unlikely(PageSlabPfmemalloc(page))) {
3432 cache_free_pfmemalloc(cachep, page, objp);
3433 return;
3434 }
3435 }
3436
3437 ac->entry[ac->avail++] = objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438}
3439
3440/**
3441 * kmem_cache_alloc - Allocate an object
3442 * @cachep: The cache to allocate from.
3443 * @flags: See kmalloc().
3444 *
3445 * Allocate an object from this cache. The flags are only relevant
3446 * if the cache has no available objects.
3447 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003448void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003450 void *ret = slab_alloc(cachep, flags, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003451
Alexander Potapenko505f5dc2016-03-25 14:22:02 -07003452 kasan_slab_alloc(cachep, ret, flags);
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003453 trace_kmem_cache_alloc(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003454 cachep->object_size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003455
3456 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457}
3458EXPORT_SYMBOL(kmem_cache_alloc);
3459
Jesper Dangaard Brouer7b0501d2016-03-15 14:53:53 -07003460static __always_inline void
3461cache_alloc_debugcheck_after_bulk(struct kmem_cache *s, gfp_t flags,
3462 size_t size, void **p, unsigned long caller)
3463{
3464 size_t i;
3465
3466 for (i = 0; i < size; i++)
3467 p[i] = cache_alloc_debugcheck_after(s, flags, p[i], caller);
3468}
3469
Jesper Dangaard Brouer865762a2015-11-20 15:57:58 -08003470int kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t size,
Jesper Dangaard Brouer2a777ea2016-03-15 14:53:50 -07003471 void **p)
Christoph Lameter484748f2015-09-04 15:45:34 -07003472{
Jesper Dangaard Brouer2a777ea2016-03-15 14:53:50 -07003473 size_t i;
3474
3475 s = slab_pre_alloc_hook(s, flags);
3476 if (!s)
3477 return 0;
3478
3479 cache_alloc_debugcheck_before(s, flags);
3480
3481 local_irq_disable();
3482 for (i = 0; i < size; i++) {
3483 void *objp = __do_cache_alloc(s, flags);
3484
Jesper Dangaard Brouer2a777ea2016-03-15 14:53:50 -07003485 if (unlikely(!objp))
3486 goto error;
3487 p[i] = objp;
3488 }
3489 local_irq_enable();
3490
Jesper Dangaard Brouer7b0501d2016-03-15 14:53:53 -07003491 cache_alloc_debugcheck_after_bulk(s, flags, size, p, _RET_IP_);
3492
Jesper Dangaard Brouer2a777ea2016-03-15 14:53:50 -07003493 /* Clear memory outside IRQ disabled section */
3494 if (unlikely(flags & __GFP_ZERO))
3495 for (i = 0; i < size; i++)
3496 memset(p[i], 0, s->object_size);
3497
3498 slab_post_alloc_hook(s, flags, size, p);
3499 /* FIXME: Trace call missing. Christoph would like a bulk variant */
3500 return size;
3501error:
3502 local_irq_enable();
Jesper Dangaard Brouer7b0501d2016-03-15 14:53:53 -07003503 cache_alloc_debugcheck_after_bulk(s, flags, i, p, _RET_IP_);
Jesper Dangaard Brouer2a777ea2016-03-15 14:53:50 -07003504 slab_post_alloc_hook(s, flags, i, p);
3505 __kmem_cache_free_bulk(s, i, p);
3506 return 0;
Christoph Lameter484748f2015-09-04 15:45:34 -07003507}
3508EXPORT_SYMBOL(kmem_cache_alloc_bulk);
3509
Li Zefan0f24f122009-12-11 15:45:30 +08003510#ifdef CONFIG_TRACING
Steven Rostedt85beb582010-11-24 16:23:34 -05003511void *
Ezequiel Garcia40521472012-09-08 17:47:56 -03003512kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003513{
Steven Rostedt85beb582010-11-24 16:23:34 -05003514 void *ret;
3515
Ezequiel Garcia48356302012-09-08 17:47:57 -03003516 ret = slab_alloc(cachep, flags, _RET_IP_);
Steven Rostedt85beb582010-11-24 16:23:34 -05003517
Alexander Potapenko505f5dc2016-03-25 14:22:02 -07003518 kasan_kmalloc(cachep, ret, size, flags);
Steven Rostedt85beb582010-11-24 16:23:34 -05003519 trace_kmalloc(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003520 size, cachep->size, flags);
Steven Rostedt85beb582010-11-24 16:23:34 -05003521 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003522}
Steven Rostedt85beb582010-11-24 16:23:34 -05003523EXPORT_SYMBOL(kmem_cache_alloc_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003524#endif
3525
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526#ifdef CONFIG_NUMA
Zhouping Liud0d04b72013-05-16 11:36:23 +08003527/**
3528 * kmem_cache_alloc_node - Allocate an object on the specified node
3529 * @cachep: The cache to allocate from.
3530 * @flags: See kmalloc().
3531 * @nodeid: node number of the target node.
3532 *
3533 * Identical to kmem_cache_alloc but it will allocate memory on the given
3534 * node, which can improve the performance for cpu bound structures.
3535 *
3536 * Fallback to other node is possible if __GFP_THISNODE is not set.
3537 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003538void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
3539{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003540 void *ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003541
Alexander Potapenko505f5dc2016-03-25 14:22:02 -07003542 kasan_slab_alloc(cachep, ret, flags);
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003543 trace_kmem_cache_alloc_node(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003544 cachep->object_size, cachep->size,
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003545 flags, nodeid);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003546
3547 return ret;
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003548}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549EXPORT_SYMBOL(kmem_cache_alloc_node);
3550
Li Zefan0f24f122009-12-11 15:45:30 +08003551#ifdef CONFIG_TRACING
Ezequiel Garcia40521472012-09-08 17:47:56 -03003552void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
Steven Rostedt85beb582010-11-24 16:23:34 -05003553 gfp_t flags,
Ezequiel Garcia40521472012-09-08 17:47:56 -03003554 int nodeid,
3555 size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003556{
Steven Rostedt85beb582010-11-24 16:23:34 -05003557 void *ret;
3558
Ezequiel Garcia592f4142012-09-25 08:07:08 -03003559 ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Alexander Potapenko505f5dc2016-03-25 14:22:02 -07003560
3561 kasan_kmalloc(cachep, ret, size, flags);
Steven Rostedt85beb582010-11-24 16:23:34 -05003562 trace_kmalloc_node(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003563 size, cachep->size,
Steven Rostedt85beb582010-11-24 16:23:34 -05003564 flags, nodeid);
3565 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003566}
Steven Rostedt85beb582010-11-24 16:23:34 -05003567EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003568#endif
3569
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003570static __always_inline void *
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003571__do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller)
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003572{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003573 struct kmem_cache *cachep;
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -07003574 void *ret;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003575
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003576 cachep = kmalloc_slab(size, flags);
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003577 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3578 return cachep;
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -07003579 ret = kmem_cache_alloc_node_trace(cachep, flags, node, size);
Alexander Potapenko505f5dc2016-03-25 14:22:02 -07003580 kasan_kmalloc(cachep, ret, size, flags);
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -07003581
3582 return ret;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003583}
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003584
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003585void *__kmalloc_node(size_t size, gfp_t flags, int node)
3586{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003587 return __do_kmalloc_node(size, flags, node, _RET_IP_);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003588}
Christoph Hellwigdbe5e692006-09-25 23:31:36 -07003589EXPORT_SYMBOL(__kmalloc_node);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003590
3591void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003592 int node, unsigned long caller)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003593{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003594 return __do_kmalloc_node(size, flags, node, caller);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003595}
3596EXPORT_SYMBOL(__kmalloc_node_track_caller);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003597#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
3599/**
Paul Drynoff800590f2006-06-23 02:03:48 -07003600 * __do_kmalloc - allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 * @size: how many bytes of memory are required.
Paul Drynoff800590f2006-06-23 02:03:48 -07003602 * @flags: the type of memory to allocate (see kmalloc).
Randy Dunlap911851e2006-03-22 00:08:14 -08003603 * @caller: function caller for debug tracking of the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604 */
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003605static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003606 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003608 struct kmem_cache *cachep;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003609 void *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610
Christoph Lameter2c59dd62013-01-10 19:14:19 +00003611 cachep = kmalloc_slab(size, flags);
Linus Torvaldsa5c96d82007-07-19 13:17:15 -07003612 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3613 return cachep;
Ezequiel Garcia48356302012-09-08 17:47:57 -03003614 ret = slab_alloc(cachep, flags, caller);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003615
Alexander Potapenko505f5dc2016-03-25 14:22:02 -07003616 kasan_kmalloc(cachep, ret, size, flags);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003617 trace_kmalloc(caller, ret,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003618 size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003619
3620 return ret;
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003621}
3622
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003623void *__kmalloc(size_t size, gfp_t flags)
3624{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003625 return __do_kmalloc(size, flags, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626}
3627EXPORT_SYMBOL(__kmalloc);
3628
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003629void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003630{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003631 return __do_kmalloc(size, flags, caller);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003632}
3633EXPORT_SYMBOL(__kmalloc_track_caller);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003634
Linus Torvalds1da177e2005-04-16 15:20:36 -07003635/**
3636 * kmem_cache_free - Deallocate an object
3637 * @cachep: The cache the allocation was from.
3638 * @objp: The previously allocated object.
3639 *
3640 * Free an object which was previously allocated from this
3641 * cache.
3642 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003643void kmem_cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644{
3645 unsigned long flags;
Glauber Costab9ce5ef2012-12-18 14:22:46 -08003646 cachep = cache_from_obj(cachep, objp);
3647 if (!cachep)
3648 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649
3650 local_irq_save(flags);
Feng Tangd97d4762012-07-02 14:29:10 +08003651 debug_check_no_locks_freed(objp, cachep->object_size);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07003652 if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003653 debug_check_no_obj_freed(objp, cachep->object_size);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003654 __cache_free(cachep, objp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 local_irq_restore(flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003656
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003657 trace_kmem_cache_free(_RET_IP_, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658}
3659EXPORT_SYMBOL(kmem_cache_free);
3660
Jesper Dangaard Brouere6cdb582016-03-15 14:53:56 -07003661void kmem_cache_free_bulk(struct kmem_cache *orig_s, size_t size, void **p)
3662{
3663 struct kmem_cache *s;
3664 size_t i;
3665
3666 local_irq_disable();
3667 for (i = 0; i < size; i++) {
3668 void *objp = p[i];
3669
Jesper Dangaard Brouerca257192016-03-15 14:54:00 -07003670 if (!orig_s) /* called via kfree_bulk */
3671 s = virt_to_cache(objp);
3672 else
3673 s = cache_from_obj(orig_s, objp);
Jesper Dangaard Brouere6cdb582016-03-15 14:53:56 -07003674
3675 debug_check_no_locks_freed(objp, s->object_size);
3676 if (!(s->flags & SLAB_DEBUG_OBJECTS))
3677 debug_check_no_obj_freed(objp, s->object_size);
3678
3679 __cache_free(s, objp, _RET_IP_);
3680 }
3681 local_irq_enable();
3682
3683 /* FIXME: add tracing */
3684}
3685EXPORT_SYMBOL(kmem_cache_free_bulk);
3686
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688 * kfree - free previously allocated memory
3689 * @objp: pointer returned by kmalloc.
3690 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07003691 * If @objp is NULL, no operation is performed.
3692 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 * Don't free memory not originally allocated by kmalloc()
3694 * or you will run into trouble.
3695 */
3696void kfree(const void *objp)
3697{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003698 struct kmem_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 unsigned long flags;
3700
Pekka Enberg2121db72009-03-25 11:05:57 +02003701 trace_kfree(_RET_IP_, objp);
3702
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003703 if (unlikely(ZERO_OR_NULL_PTR(objp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 return;
3705 local_irq_save(flags);
3706 kfree_debugcheck(objp);
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003707 c = virt_to_cache(objp);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003708 debug_check_no_locks_freed(objp, c->object_size);
3709
3710 debug_check_no_obj_freed(objp, c->object_size);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003711 __cache_free(c, (void *)objp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 local_irq_restore(flags);
3713}
3714EXPORT_SYMBOL(kfree);
3715
Christoph Lametere498be72005-09-09 13:03:32 -07003716/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003717 * This initializes kmem_cache_node or resizes various caches for all nodes.
Christoph Lametere498be72005-09-09 13:03:32 -07003718 */
Joonsoo Kimc3d332b2016-05-19 17:10:14 -07003719static int setup_kmem_cache_nodes(struct kmem_cache *cachep, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -07003720{
Joonsoo Kimc3d332b2016-05-19 17:10:14 -07003721 int ret;
Christoph Lametere498be72005-09-09 13:03:32 -07003722 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003723 struct kmem_cache_node *n;
Christoph Lametere498be72005-09-09 13:03:32 -07003724
Mel Gorman9c09a952008-01-24 05:49:54 -08003725 for_each_online_node(node) {
Joonsoo Kimc3d332b2016-05-19 17:10:14 -07003726 ret = setup_kmem_cache_node(cachep, node, gfp, true);
3727 if (ret)
Christoph Lametere498be72005-09-09 13:03:32 -07003728 goto fail;
3729
Christoph Lametere498be72005-09-09 13:03:32 -07003730 }
Joonsoo Kimc3d332b2016-05-19 17:10:14 -07003731
Christoph Lametercafeb022006-03-25 03:06:46 -08003732 return 0;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003733
Andrew Mortona737b3e2006-03-22 00:08:11 -08003734fail:
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003735 if (!cachep->list.next) {
Christoph Lameter0718dc22006-03-25 03:06:47 -08003736 /* Cache is not active yet. Roll back what we did */
3737 node--;
3738 while (node >= 0) {
Christoph Lameter18bf8542014-08-06 16:04:11 -07003739 n = get_node(cachep, node);
3740 if (n) {
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003741 kfree(n->shared);
3742 free_alien_cache(n->alien);
3743 kfree(n);
Christoph Lameter6a673682013-01-10 19:14:19 +00003744 cachep->node[node] = NULL;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003745 }
3746 node--;
3747 }
3748 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003749 return -ENOMEM;
Christoph Lametere498be72005-09-09 13:03:32 -07003750}
3751
Christoph Lameter18004c52012-07-06 15:25:12 -05003752/* Always called with the slab_mutex held */
Glauber Costa943a4512012-12-18 14:23:03 -08003753static int __do_tune_cpucache(struct kmem_cache *cachep, int limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003754 int batchcount, int shared, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755{
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07003756 struct array_cache __percpu *cpu_cache, *prev;
3757 int cpu;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07003759 cpu_cache = alloc_kmem_cache_cpus(cachep, limit, batchcount);
3760 if (!cpu_cache)
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003761 return -ENOMEM;
3762
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07003763 prev = cachep->cpu_cache;
3764 cachep->cpu_cache = cpu_cache;
3765 kick_all_cpus_sync();
Christoph Lametere498be72005-09-09 13:03:32 -07003766
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 check_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 cachep->batchcount = batchcount;
3769 cachep->limit = limit;
Christoph Lametere498be72005-09-09 13:03:32 -07003770 cachep->shared = shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003771
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07003772 if (!prev)
Joonsoo Kimc3d332b2016-05-19 17:10:14 -07003773 goto setup_node;
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07003774
3775 for_each_online_cpu(cpu) {
Joonsoo Kim97654df2014-08-06 16:04:25 -07003776 LIST_HEAD(list);
Christoph Lameter18bf8542014-08-06 16:04:11 -07003777 int node;
3778 struct kmem_cache_node *n;
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07003779 struct array_cache *ac = per_cpu_ptr(prev, cpu);
Christoph Lameter18bf8542014-08-06 16:04:11 -07003780
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07003781 node = cpu_to_mem(cpu);
Christoph Lameter18bf8542014-08-06 16:04:11 -07003782 n = get_node(cachep, node);
3783 spin_lock_irq(&n->list_lock);
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07003784 free_block(cachep, ac->entry, ac->avail, node, &list);
Christoph Lameter18bf8542014-08-06 16:04:11 -07003785 spin_unlock_irq(&n->list_lock);
Joonsoo Kim97654df2014-08-06 16:04:25 -07003786 slabs_destroy(cachep, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 }
Joonsoo Kimbf0dea22014-10-09 15:26:27 -07003788 free_percpu(prev);
3789
Joonsoo Kimc3d332b2016-05-19 17:10:14 -07003790setup_node:
3791 return setup_kmem_cache_nodes(cachep, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792}
3793
Glauber Costa943a4512012-12-18 14:23:03 -08003794static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
3795 int batchcount, int shared, gfp_t gfp)
3796{
3797 int ret;
Vladimir Davydov426589f2015-02-12 14:59:23 -08003798 struct kmem_cache *c;
Glauber Costa943a4512012-12-18 14:23:03 -08003799
3800 ret = __do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
3801
3802 if (slab_state < FULL)
3803 return ret;
3804
3805 if ((ret < 0) || !is_root_cache(cachep))
3806 return ret;
3807
Vladimir Davydov426589f2015-02-12 14:59:23 -08003808 lockdep_assert_held(&slab_mutex);
3809 for_each_memcg_cache(c, cachep) {
3810 /* return value determined by the root cache only */
3811 __do_tune_cpucache(c, limit, batchcount, shared, gfp);
Glauber Costa943a4512012-12-18 14:23:03 -08003812 }
3813
3814 return ret;
3815}
3816
Christoph Lameter18004c52012-07-06 15:25:12 -05003817/* Called with slab_mutex held always */
Pekka Enberg83b519e2009-06-10 19:40:04 +03003818static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819{
3820 int err;
Glauber Costa943a4512012-12-18 14:23:03 -08003821 int limit = 0;
3822 int shared = 0;
3823 int batchcount = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824
Glauber Costa943a4512012-12-18 14:23:03 -08003825 if (!is_root_cache(cachep)) {
3826 struct kmem_cache *root = memcg_root_cache(cachep);
3827 limit = root->limit;
3828 shared = root->shared;
3829 batchcount = root->batchcount;
3830 }
3831
3832 if (limit && shared && batchcount)
3833 goto skip_setup;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003834 /*
3835 * The head array serves three purposes:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 * - create a LIFO ordering, i.e. return objects that are cache-warm
3837 * - reduce the number of spinlock operations.
Andrew Mortona737b3e2006-03-22 00:08:11 -08003838 * - reduce the number of linked list operations on the slab and
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 * bufctl chains: array operations are cheaper.
3840 * The numbers are guessed, we should auto-tune as described by
3841 * Bonwick.
3842 */
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003843 if (cachep->size > 131072)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 limit = 1;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003845 else if (cachep->size > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 limit = 8;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003847 else if (cachep->size > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848 limit = 24;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003849 else if (cachep->size > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850 limit = 54;
3851 else
3852 limit = 120;
3853
Andrew Mortona737b3e2006-03-22 00:08:11 -08003854 /*
3855 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 * allocation behaviour: Most allocs on one cpu, most free operations
3857 * on another cpu. For these cases, an efficient object passing between
3858 * cpus is necessary. This is provided by a shared array. The array
3859 * replaces Bonwick's magazine layer.
3860 * On uniprocessor, it's functionally equivalent (but less efficient)
3861 * to a larger limit. Thus disabled by default.
3862 */
3863 shared = 0;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003864 if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 shared = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866
3867#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003868 /*
3869 * With debugging enabled, large batchcount lead to excessively long
3870 * periods with disabled local interrupts. Limit the batchcount
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 */
3872 if (limit > 32)
3873 limit = 32;
3874#endif
Glauber Costa943a4512012-12-18 14:23:03 -08003875 batchcount = (limit + 1) / 2;
3876skip_setup:
3877 err = do_tune_cpucache(cachep, limit, batchcount, shared, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878 if (err)
Joe Perches11705322016-03-17 14:19:50 -07003879 pr_err("enable_cpucache failed for %s, error %d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003880 cachep->name, -err);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003881 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003882}
3883
Christoph Lameter1b552532006-03-22 00:09:07 -08003884/*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003885 * Drain an array if it contains any elements taking the node lock only if
3886 * necessary. Note that the node listlock also protects the array_cache
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003887 * if drain_array() is used on the shared array.
Christoph Lameter1b552532006-03-22 00:09:07 -08003888 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003889static void drain_array(struct kmem_cache *cachep, struct kmem_cache_node *n,
Joonsoo Kim18726ca2016-05-19 17:10:02 -07003890 struct array_cache *ac, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891{
Joonsoo Kim97654df2014-08-06 16:04:25 -07003892 LIST_HEAD(list);
Joonsoo Kim18726ca2016-05-19 17:10:02 -07003893
3894 /* ac from n->shared can be freed if we don't hold the slab_mutex. */
3895 check_mutex_acquired();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
Christoph Lameter1b552532006-03-22 00:09:07 -08003897 if (!ac || !ac->avail)
3898 return;
Joonsoo Kim18726ca2016-05-19 17:10:02 -07003899
3900 if (ac->touched) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 ac->touched = 0;
Joonsoo Kim18726ca2016-05-19 17:10:02 -07003902 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 }
Joonsoo Kim18726ca2016-05-19 17:10:02 -07003904
3905 spin_lock_irq(&n->list_lock);
3906 drain_array_locked(cachep, ac, node, false, &list);
3907 spin_unlock_irq(&n->list_lock);
3908
3909 slabs_destroy(cachep, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910}
3911
3912/**
3913 * cache_reap - Reclaim memory from caches.
Randy Dunlap05fb6bf2007-02-28 20:12:13 -08003914 * @w: work descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915 *
3916 * Called from workqueue/eventd every few seconds.
3917 * Purpose:
3918 * - clear the per-cpu caches for this CPU.
3919 * - return freeable pages to the main free memory pool.
3920 *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003921 * If we cannot acquire the cache chain mutex then just give up - we'll try
3922 * again on the next iteration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08003924static void cache_reap(struct work_struct *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925{
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07003926 struct kmem_cache *searchp;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003927 struct kmem_cache_node *n;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003928 int node = numa_mem_id();
Jean Delvarebf6aede2009-04-02 16:56:54 -07003929 struct delayed_work *work = to_delayed_work(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
Christoph Lameter18004c52012-07-06 15:25:12 -05003931 if (!mutex_trylock(&slab_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 /* Give up. Setup the next iteration. */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08003933 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934
Christoph Lameter18004c52012-07-06 15:25:12 -05003935 list_for_each_entry(searchp, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 check_irq_on();
3937
Christoph Lameter35386e32006-03-22 00:09:05 -08003938 /*
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003939 * We only take the node lock if absolutely necessary and we
Christoph Lameter35386e32006-03-22 00:09:05 -08003940 * have established with reasonable certainty that
3941 * we can do some work if the lock was obtained.
3942 */
Christoph Lameter18bf8542014-08-06 16:04:11 -07003943 n = get_node(searchp, node);
Christoph Lameter35386e32006-03-22 00:09:05 -08003944
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003945 reap_alien(searchp, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
Joonsoo Kim18726ca2016-05-19 17:10:02 -07003947 drain_array(searchp, n, cpu_cache_get(searchp), node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948
Christoph Lameter35386e32006-03-22 00:09:05 -08003949 /*
3950 * These are racy checks but it does not matter
3951 * if we skip one check or scan twice.
3952 */
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003953 if (time_after(n->next_reap, jiffies))
Christoph Lameter35386e32006-03-22 00:09:05 -08003954 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08003956 n->next_reap = jiffies + REAPTIMEOUT_NODE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
Joonsoo Kim18726ca2016-05-19 17:10:02 -07003958 drain_array(searchp, n, n->shared, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003960 if (n->free_touched)
3961 n->free_touched = 0;
Christoph Lametered11d9e2006-06-30 01:55:45 -07003962 else {
3963 int freed;
3964
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003965 freed = drain_freelist(searchp, n, (n->free_limit +
Christoph Lametered11d9e2006-06-30 01:55:45 -07003966 5 * searchp->num - 1) / (5 * searchp->num));
3967 STATS_ADD_REAPED(searchp, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 }
Christoph Lameter35386e32006-03-22 00:09:05 -08003969next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 cond_resched();
3971 }
3972 check_irq_on();
Christoph Lameter18004c52012-07-06 15:25:12 -05003973 mutex_unlock(&slab_mutex);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08003974 next_reap_node();
Christoph Lameter7c5cae32007-02-10 01:42:55 -08003975out:
Andrew Mortona737b3e2006-03-22 00:08:11 -08003976 /* Set up the next iteration */
Jianyu Zhan5f0985b2014-03-30 17:02:20 +08003977 schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_AC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978}
3979
Linus Torvalds158a9622008-01-02 13:04:48 -08003980#ifdef CONFIG_SLABINFO
Glauber Costa0d7561c2012-10-19 18:20:27 +04003981void get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982{
Joonsoo Kim8456a642013-10-24 10:07:49 +09003983 struct page *page;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003984 unsigned long active_objs;
3985 unsigned long num_objs;
3986 unsigned long active_slabs = 0;
3987 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07003988 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 char *error = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003990 int node;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003991 struct kmem_cache_node *n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 active_objs = 0;
3994 num_slabs = 0;
Christoph Lameter18bf8542014-08-06 16:04:11 -07003995 for_each_kmem_cache_node(cachep, node, n) {
Christoph Lametere498be72005-09-09 13:03:32 -07003996
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003997 check_irq_on();
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00003998 spin_lock_irq(&n->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07003999
Joonsoo Kim8456a642013-10-24 10:07:49 +09004000 list_for_each_entry(page, &n->slabs_full, lru) {
4001 if (page->active != cachep->num && !error)
Christoph Lametere498be72005-09-09 13:03:32 -07004002 error = "slabs_full accounting error";
4003 active_objs += cachep->num;
4004 active_slabs++;
4005 }
Joonsoo Kim8456a642013-10-24 10:07:49 +09004006 list_for_each_entry(page, &n->slabs_partial, lru) {
4007 if (page->active == cachep->num && !error)
Joonsoo Kim106a74e2013-10-24 10:07:48 +09004008 error = "slabs_partial accounting error";
Joonsoo Kim8456a642013-10-24 10:07:49 +09004009 if (!page->active && !error)
Joonsoo Kim106a74e2013-10-24 10:07:48 +09004010 error = "slabs_partial accounting error";
Joonsoo Kim8456a642013-10-24 10:07:49 +09004011 active_objs += page->active;
Christoph Lametere498be72005-09-09 13:03:32 -07004012 active_slabs++;
4013 }
Joonsoo Kim8456a642013-10-24 10:07:49 +09004014 list_for_each_entry(page, &n->slabs_free, lru) {
4015 if (page->active && !error)
Joonsoo Kim106a74e2013-10-24 10:07:48 +09004016 error = "slabs_free accounting error";
Christoph Lametere498be72005-09-09 13:03:32 -07004017 num_slabs++;
4018 }
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004019 free_objects += n->free_objects;
4020 if (n->shared)
4021 shared_avail += n->shared->avail;
Christoph Lametere498be72005-09-09 13:03:32 -07004022
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004023 spin_unlock_irq(&n->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004025 num_slabs += active_slabs;
4026 num_objs = num_slabs * cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07004027 if (num_objs - active_objs != free_objects && !error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 error = "free_objects accounting error";
4029
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004030 name = cachep->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 if (error)
Joe Perches11705322016-03-17 14:19:50 -07004032 pr_err("slab: cache %s error: %s\n", name, error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033
Glauber Costa0d7561c2012-10-19 18:20:27 +04004034 sinfo->active_objs = active_objs;
4035 sinfo->num_objs = num_objs;
4036 sinfo->active_slabs = active_slabs;
4037 sinfo->num_slabs = num_slabs;
4038 sinfo->shared_avail = shared_avail;
4039 sinfo->limit = cachep->limit;
4040 sinfo->batchcount = cachep->batchcount;
4041 sinfo->shared = cachep->shared;
4042 sinfo->objects_per_slab = cachep->num;
4043 sinfo->cache_order = cachep->gfporder;
4044}
4045
4046void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *cachep)
4047{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048#if STATS
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004049 { /* node stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 unsigned long high = cachep->high_mark;
4051 unsigned long allocs = cachep->num_allocations;
4052 unsigned long grown = cachep->grown;
4053 unsigned long reaped = cachep->reaped;
4054 unsigned long errors = cachep->errors;
4055 unsigned long max_freeable = cachep->max_freeable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 unsigned long node_allocs = cachep->node_allocs;
Christoph Lametere498be72005-09-09 13:03:32 -07004057 unsigned long node_frees = cachep->node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004058 unsigned long overflows = cachep->node_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004059
Joe Perches756a025f02016-03-17 14:19:47 -07004060 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu %4lu %4lu %4lu %4lu %4lu",
Joe Perchese92dd4f2010-03-26 19:27:58 -07004061 allocs, high, grown,
4062 reaped, errors, max_freeable, node_allocs,
4063 node_frees, overflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 }
4065 /* cpu stats */
4066 {
4067 unsigned long allochit = atomic_read(&cachep->allochit);
4068 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
4069 unsigned long freehit = atomic_read(&cachep->freehit);
4070 unsigned long freemiss = atomic_read(&cachep->freemiss);
4071
4072 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004073 allochit, allocmiss, freehit, freemiss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 }
4075#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076}
4077
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078#define MAX_SLABINFO_WRITE 128
4079/**
4080 * slabinfo_write - Tuning for the slab allocator
4081 * @file: unused
4082 * @buffer: user buffer
4083 * @count: data length
4084 * @ppos: unused
4085 */
Glauber Costab7454ad2012-10-19 18:20:25 +04004086ssize_t slabinfo_write(struct file *file, const char __user *buffer,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004087 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004089 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090 int limit, batchcount, shared, res;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004091 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004092
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 if (count > MAX_SLABINFO_WRITE)
4094 return -EINVAL;
4095 if (copy_from_user(&kbuf, buffer, count))
4096 return -EFAULT;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004097 kbuf[MAX_SLABINFO_WRITE] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098
4099 tmp = strchr(kbuf, ' ');
4100 if (!tmp)
4101 return -EINVAL;
4102 *tmp = '\0';
4103 tmp++;
4104 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
4105 return -EINVAL;
4106
4107 /* Find the cache in the chain of caches. */
Christoph Lameter18004c52012-07-06 15:25:12 -05004108 mutex_lock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109 res = -EINVAL;
Christoph Lameter18004c52012-07-06 15:25:12 -05004110 list_for_each_entry(cachep, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 if (!strcmp(cachep->name, kbuf)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08004112 if (limit < 1 || batchcount < 1 ||
4113 batchcount > limit || shared < 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07004114 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004115 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07004116 res = do_tune_cpucache(cachep, limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03004117 batchcount, shared,
4118 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 }
4120 break;
4121 }
4122 }
Christoph Lameter18004c52012-07-06 15:25:12 -05004123 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 if (res >= 0)
4125 res = count;
4126 return res;
4127}
Al Viro871751e2006-03-25 03:06:39 -08004128
4129#ifdef CONFIG_DEBUG_SLAB_LEAK
4130
Al Viro871751e2006-03-25 03:06:39 -08004131static inline int add_caller(unsigned long *n, unsigned long v)
4132{
4133 unsigned long *p;
4134 int l;
4135 if (!v)
4136 return 1;
4137 l = n[1];
4138 p = n + 2;
4139 while (l) {
4140 int i = l/2;
4141 unsigned long *q = p + 2 * i;
4142 if (*q == v) {
4143 q[1]++;
4144 return 1;
4145 }
4146 if (*q > v) {
4147 l = i;
4148 } else {
4149 p = q + 2;
4150 l -= i + 1;
4151 }
4152 }
4153 if (++n[1] == n[0])
4154 return 0;
4155 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4156 p[0] = v;
4157 p[1] = 1;
4158 return 1;
4159}
4160
Joonsoo Kim8456a642013-10-24 10:07:49 +09004161static void handle_slab(unsigned long *n, struct kmem_cache *c,
4162 struct page *page)
Al Viro871751e2006-03-25 03:06:39 -08004163{
4164 void *p;
Joonsoo Kimd31676d2016-03-15 14:54:24 -07004165 int i, j;
4166 unsigned long v;
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09004167
Al Viro871751e2006-03-25 03:06:39 -08004168 if (n[0] == n[1])
4169 return;
Joonsoo Kim8456a642013-10-24 10:07:49 +09004170 for (i = 0, p = page->s_mem; i < c->num; i++, p += c->size) {
Joonsoo Kimd31676d2016-03-15 14:54:24 -07004171 bool active = true;
4172
4173 for (j = page->active; j < c->num; j++) {
4174 if (get_free_obj(page, j) == i) {
4175 active = false;
4176 break;
4177 }
4178 }
4179
4180 if (!active)
Al Viro871751e2006-03-25 03:06:39 -08004181 continue;
Joonsoo Kimb1cb0982013-10-24 10:07:45 +09004182
Joonsoo Kimd31676d2016-03-15 14:54:24 -07004183 /*
4184 * probe_kernel_read() is used for DEBUG_PAGEALLOC. page table
4185 * mapping is established when actual object allocation and
4186 * we could mistakenly access the unmapped object in the cpu
4187 * cache.
4188 */
4189 if (probe_kernel_read(&v, dbg_userword(c, p), sizeof(v)))
4190 continue;
4191
4192 if (!add_caller(n, v))
Al Viro871751e2006-03-25 03:06:39 -08004193 return;
4194 }
4195}
4196
4197static void show_symbol(struct seq_file *m, unsigned long address)
4198{
4199#ifdef CONFIG_KALLSYMS
Al Viro871751e2006-03-25 03:06:39 -08004200 unsigned long offset, size;
Tejun Heo9281ace2007-07-17 04:03:51 -07004201 char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
Al Viro871751e2006-03-25 03:06:39 -08004202
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004203 if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
Al Viro871751e2006-03-25 03:06:39 -08004204 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004205 if (modname[0])
Al Viro871751e2006-03-25 03:06:39 -08004206 seq_printf(m, " [%s]", modname);
4207 return;
4208 }
4209#endif
4210 seq_printf(m, "%p", (void *)address);
4211}
4212
4213static int leaks_show(struct seq_file *m, void *p)
4214{
Thierry Reding0672aa72012-06-22 19:42:49 +02004215 struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);
Joonsoo Kim8456a642013-10-24 10:07:49 +09004216 struct page *page;
Christoph Lameterce8eb6c2013-01-10 19:14:19 +00004217 struct kmem_cache_node *n;
Al Viro871751e2006-03-25 03:06:39 -08004218 const char *name;
Christoph Lameterdb845062013-02-05 18:45:23 +00004219 unsigned long *x = m->private;
Al Viro871751e2006-03-25 03:06:39 -08004220 int node;
4221 int i;
4222
4223 if (!(cachep->flags & SLAB_STORE_USER))
4224 return 0;
4225 if (!(cachep->flags & SLAB_RED_ZONE))
4226 return 0;
4227
Joonsoo Kimd31676d2016-03-15 14:54:24 -07004228 /*
4229 * Set store_user_clean and start to grab stored user information
4230 * for all objects on this cache. If some alloc/free requests comes
4231 * during the processing, information would be wrong so restart
4232 * whole processing.
4233 */
4234 do {
4235 set_store_user_clean(cachep);
4236 drain_cpu_caches(cachep);
Al Viro871751e2006-03-25 03:06:39 -08004237
Joonsoo Kimd31676d2016-03-15 14:54:24 -07004238 x[1] = 0;
Al Viro871751e2006-03-25 03:06:39 -08004239
Joonsoo Kimd31676d2016-03-15 14:54:24 -07004240 for_each_kmem_cache_node(cachep, node, n) {
Al Viro871751e2006-03-25 03:06:39 -08004241
Joonsoo Kimd31676d2016-03-15 14:54:24 -07004242 check_irq_on();
4243 spin_lock_irq(&n->list_lock);
Al Viro871751e2006-03-25 03:06:39 -08004244
Joonsoo Kimd31676d2016-03-15 14:54:24 -07004245 list_for_each_entry(page, &n->slabs_full, lru)
4246 handle_slab(x, cachep, page);
4247 list_for_each_entry(page, &n->slabs_partial, lru)
4248 handle_slab(x, cachep, page);
4249 spin_unlock_irq(&n->list_lock);
4250 }
4251 } while (!is_store_user_clean(cachep));
4252
Al Viro871751e2006-03-25 03:06:39 -08004253 name = cachep->name;
Christoph Lameterdb845062013-02-05 18:45:23 +00004254 if (x[0] == x[1]) {
Al Viro871751e2006-03-25 03:06:39 -08004255 /* Increase the buffer size */
Christoph Lameter18004c52012-07-06 15:25:12 -05004256 mutex_unlock(&slab_mutex);
Christoph Lameterdb845062013-02-05 18:45:23 +00004257 m->private = kzalloc(x[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
Al Viro871751e2006-03-25 03:06:39 -08004258 if (!m->private) {
4259 /* Too bad, we are really out */
Christoph Lameterdb845062013-02-05 18:45:23 +00004260 m->private = x;
Christoph Lameter18004c52012-07-06 15:25:12 -05004261 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004262 return -ENOMEM;
4263 }
Christoph Lameterdb845062013-02-05 18:45:23 +00004264 *(unsigned long *)m->private = x[0] * 2;
4265 kfree(x);
Christoph Lameter18004c52012-07-06 15:25:12 -05004266 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004267 /* Now make sure this entry will be retried */
4268 m->count = m->size;
4269 return 0;
4270 }
Christoph Lameterdb845062013-02-05 18:45:23 +00004271 for (i = 0; i < x[1]; i++) {
4272 seq_printf(m, "%s: %lu ", name, x[2*i+3]);
4273 show_symbol(m, x[2*i+2]);
Al Viro871751e2006-03-25 03:06:39 -08004274 seq_putc(m, '\n');
4275 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004276
Al Viro871751e2006-03-25 03:06:39 -08004277 return 0;
4278}
4279
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004280static const struct seq_operations slabstats_op = {
Vladimir Davydov1df3b262014-12-10 15:42:16 -08004281 .start = slab_start,
Wanpeng Li276a2432013-07-08 08:08:28 +08004282 .next = slab_next,
4283 .stop = slab_stop,
Al Viro871751e2006-03-25 03:06:39 -08004284 .show = leaks_show,
4285};
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004286
4287static int slabstats_open(struct inode *inode, struct file *file)
4288{
Rob Jonesb208ce32014-10-09 15:28:03 -07004289 unsigned long *n;
4290
4291 n = __seq_open_private(file, &slabstats_op, PAGE_SIZE);
4292 if (!n)
4293 return -ENOMEM;
4294
4295 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
4296
4297 return 0;
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004298}
4299
4300static const struct file_operations proc_slabstats_operations = {
4301 .open = slabstats_open,
4302 .read = seq_read,
4303 .llseek = seq_lseek,
4304 .release = seq_release_private,
4305};
Al Viro871751e2006-03-25 03:06:39 -08004306#endif
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004307
4308static int __init slab_proc_init(void)
4309{
4310#ifdef CONFIG_DEBUG_SLAB_LEAK
4311 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
4312#endif
4313 return 0;
4314}
4315module_init(slab_proc_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316#endif
4317
Manfred Spraul00e145b2005-09-03 15:55:07 -07004318/**
4319 * ksize - get the actual amount of memory allocated for a given object
4320 * @objp: Pointer to the object
4321 *
4322 * kmalloc may internally round up allocations and return more memory
4323 * than requested. ksize() can be used to determine the actual amount of
4324 * memory allocated. The caller may use this additional memory, even though
4325 * a smaller amount of memory was initially specified with the kmalloc call.
4326 * The caller must guarantee that objp points to a valid object previously
4327 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4328 * must not be freed during the duration of the call.
4329 */
Pekka Enbergfd76bab2007-05-06 14:48:40 -07004330size_t ksize(const void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331{
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -07004332 size_t size;
4333
Christoph Lameteref8b4522007-10-16 01:24:46 -07004334 BUG_ON(!objp);
4335 if (unlikely(objp == ZERO_SIZE_PTR))
Manfred Spraul00e145b2005-09-03 15:55:07 -07004336 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -07004338 size = virt_to_cache(objp)->object_size;
4339 /* We assume that ksize callers could use the whole allocated area,
4340 * so we need to unpoison this area.
4341 */
Alexander Potapenko505f5dc2016-03-25 14:22:02 -07004342 kasan_krealloc(objp, size, GFP_NOWAIT);
Alexander Potapenko7ed2f9e2016-03-25 14:21:59 -07004343
4344 return size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345}
Kirill A. Shutemovb1aabec2009-02-10 15:21:44 +02004346EXPORT_SYMBOL(ksize);