blob: e1790e56fd86b500333017b741123d15640da540 [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>
Christoph Lameter97d06602012-07-06 15:25:11 -050090#include "slab.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#include <linux/mm.h>
Randy Dunlapc9cf5522006-06-27 02:53:52 -070092#include <linux/poison.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#include <linux/swap.h>
94#include <linux/cache.h>
95#include <linux/interrupt.h>
96#include <linux/init.h>
97#include <linux/compiler.h>
Paul Jackson101a5002006-03-24 03:16:07 -080098#include <linux/cpuset.h>
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +040099#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100#include <linux/seq_file.h>
101#include <linux/notifier.h>
102#include <linux/kallsyms.h>
103#include <linux/cpu.h>
104#include <linux/sysctl.h>
105#include <linux/module.h>
106#include <linux/rcupdate.h>
Paulo Marques543537b2005-06-23 00:09:02 -0700107#include <linux/string.h>
Andrew Morton138ae662006-12-06 20:36:41 -0800108#include <linux/uaccess.h>
Christoph Lametere498be72005-09-09 13:03:32 -0700109#include <linux/nodemask.h>
Catalin Marinasd5cff632009-06-11 13:22:40 +0100110#include <linux/kmemleak.h>
Christoph Lameterdc85da12006-01-18 17:42:36 -0800111#include <linux/mempolicy.h>
Ingo Molnarfc0abb12006-01-18 17:42:33 -0800112#include <linux/mutex.h>
Akinobu Mita8a8b6502006-12-08 02:39:44 -0800113#include <linux/fault-inject.h>
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700114#include <linux/rtmutex.h>
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800115#include <linux/reciprocal_div.h>
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700116#include <linux/debugobjects.h>
Pekka Enbergc175eea2008-05-09 20:35:53 +0200117#include <linux/kmemcheck.h>
David Rientjes8f9f8d92010-03-27 19:40:47 -0700118#include <linux/memory.h>
Linus Torvalds268bb0c2011-05-20 12:50:29 -0700119#include <linux/prefetch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Mel Gorman381760e2012-07-31 16:44:30 -0700121#include <net/sock.h>
122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#include <asm/cacheflush.h>
124#include <asm/tlbflush.h>
125#include <asm/page.h>
126
Steven Rostedt4dee6b62012-01-09 17:15:42 -0500127#include <trace/events/kmem.h>
128
Mel Gorman072bb0a2012-07-31 16:43:58 -0700129#include "internal.h"
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131/*
Christoph Lameter50953fe2007-05-06 14:50:16 -0700132 * DEBUG - 1 for kmem_cache_create() to honour; SLAB_RED_ZONE & SLAB_POISON.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 * 0 for faster, smaller code (especially in the critical paths).
134 *
135 * STATS - 1 to collect stats for /proc/slabinfo.
136 * 0 for faster, smaller code (especially in the critical paths).
137 *
138 * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
139 */
140
141#ifdef CONFIG_DEBUG_SLAB
142#define DEBUG 1
143#define STATS 1
144#define FORCED_DEBUG 1
145#else
146#define DEBUG 0
147#define STATS 0
148#define FORCED_DEBUG 0
149#endif
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151/* Shouldn't this be in a header file somewhere? */
152#define BYTES_PER_WORD sizeof(void *)
David Woodhouse87a927c2007-07-04 21:26:44 -0400153#define REDZONE_ALIGN max(BYTES_PER_WORD, __alignof__(unsigned long long))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155#ifndef ARCH_KMALLOC_FLAGS
156#define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
157#endif
158
Mel Gorman072bb0a2012-07-31 16:43:58 -0700159/*
160 * true if a page was allocated from pfmemalloc reserves for network-based
161 * swap
162 */
163static bool pfmemalloc_active __read_mostly;
164
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165/*
166 * kmem_bufctl_t:
167 *
168 * Bufctl's are used for linking objs within a slab
169 * linked offsets.
170 *
171 * This implementation relies on "struct page" for locating the cache &
172 * slab an object belongs to.
173 * This allows the bufctl structure to be small (one int), but limits
174 * the number of objects a slab (not a cache) can contain when off-slab
175 * bufctls are used. The limit is the size of the largest general cache
176 * that does not use off-slab slabs.
177 * For 32bit archs with 4 kB pages, is this 56.
178 * This is not serious, as it is only for large objects, when it is unwise
179 * to have too many per slab.
180 * Note: This limit can be raised by introducing a general cache whose size
181 * is less than 512 (PAGE_SIZE<<3), but greater than 256.
182 */
183
Kyle Moffettfa5b08d2005-09-03 15:55:03 -0700184typedef unsigned int kmem_bufctl_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185#define BUFCTL_END (((kmem_bufctl_t)(~0U))-0)
186#define BUFCTL_FREE (((kmem_bufctl_t)(~0U))-1)
Al Viro871751e2006-03-25 03:06:39 -0800187#define BUFCTL_ACTIVE (((kmem_bufctl_t)(~0U))-2)
188#define SLAB_LIMIT (((kmem_bufctl_t)(~0U))-3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 * struct slab_rcu
192 *
193 * slab_destroy on a SLAB_DESTROY_BY_RCU cache uses this structure to
194 * arrange for kmem_freepages to be called via RCU. This is useful if
195 * we need to approach a kernel structure obliquely, from its address
196 * obtained without the usual locking. We can lock the structure to
197 * stabilize it and check it's still at the given address, only if we
198 * can be sure that the memory has not been meanwhile reused for some
199 * other kind of object (which our subsystem's lock might corrupt).
200 *
201 * rcu_read_lock before reading the address, then rcu_read_unlock after
202 * taking the spinlock within the structure expected at that address.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 */
204struct slab_rcu {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800205 struct rcu_head head;
Pekka Enberg343e0d72006-02-01 03:05:50 -0800206 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800207 void *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
210/*
Lai Jiangshan5bfe53a2011-03-10 15:22:24 +0800211 * struct slab
212 *
213 * Manages the objs in a slab. Placed either at the beginning of mem allocated
214 * for a slab, or allocated from an general cache.
215 * Slabs are chained into three list: fully used, partial, fully free slabs.
216 */
217struct slab {
218 union {
219 struct {
220 struct list_head list;
221 unsigned long colouroff;
222 void *s_mem; /* including colour offset */
223 unsigned int inuse; /* num of objs active in slab */
224 kmem_bufctl_t free;
225 unsigned short nodeid;
226 };
227 struct slab_rcu __slab_cover_slab_rcu;
228 };
229};
230
231/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 * struct array_cache
233 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 * Purpose:
235 * - LIFO ordering, to hand out cache-warm objects from _alloc
236 * - reduce the number of linked list operations
237 * - reduce spinlock operations
238 *
239 * The limit is stored in the per-cpu structure to reduce the data cache
240 * footprint.
241 *
242 */
243struct array_cache {
244 unsigned int avail;
245 unsigned int limit;
246 unsigned int batchcount;
247 unsigned int touched;
Christoph Lametere498be72005-09-09 13:03:32 -0700248 spinlock_t lock;
Robert P. J. Daybda5b652007-10-16 23:30:05 -0700249 void *entry[]; /*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800250 * Must have this definition in here for the proper
251 * alignment of array_cache. Also simplifies accessing
252 * the entries.
Mel Gorman072bb0a2012-07-31 16:43:58 -0700253 *
254 * Entries should not be directly dereferenced as
255 * entries belonging to slabs marked pfmemalloc will
256 * have the lower bits set SLAB_OBJ_PFMEMALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -0800257 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258};
259
Mel Gorman072bb0a2012-07-31 16:43:58 -0700260#define SLAB_OBJ_PFMEMALLOC 1
261static inline bool is_obj_pfmemalloc(void *objp)
262{
263 return (unsigned long)objp & SLAB_OBJ_PFMEMALLOC;
264}
265
266static inline void set_obj_pfmemalloc(void **objp)
267{
268 *objp = (void *)((unsigned long)*objp | SLAB_OBJ_PFMEMALLOC);
269 return;
270}
271
272static inline void clear_obj_pfmemalloc(void **objp)
273{
274 *objp = (void *)((unsigned long)*objp & ~SLAB_OBJ_PFMEMALLOC);
275}
276
Andrew Mortona737b3e2006-03-22 00:08:11 -0800277/*
278 * bootstrap: The caches do not work without cpuarrays anymore, but the
279 * cpuarrays are allocated from the generic caches...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
281#define BOOT_CPUCACHE_ENTRIES 1
282struct arraycache_init {
283 struct array_cache cache;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800284 void *entries[BOOT_CPUCACHE_ENTRIES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285};
286
287/*
Christoph Lametere498be72005-09-09 13:03:32 -0700288 * The slab lists for all objects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 */
290struct kmem_list3 {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800291 struct list_head slabs_partial; /* partial list first, better asm code */
292 struct list_head slabs_full;
293 struct list_head slabs_free;
294 unsigned long free_objects;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800295 unsigned int free_limit;
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -0800296 unsigned int colour_next; /* Per-node cache coloring */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800297 spinlock_t list_lock;
298 struct array_cache *shared; /* shared per node */
299 struct array_cache **alien; /* on other nodes */
Christoph Lameter35386e32006-03-22 00:09:05 -0800300 unsigned long next_reap; /* updated without locking */
301 int free_touched; /* updated without locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302};
303
Christoph Lametere498be72005-09-09 13:03:32 -0700304/*
305 * Need this for bootstrapping a per node allocator.
306 */
Pekka Enberg556a1692008-01-25 08:20:51 +0200307#define NUM_INIT_LISTS (3 * MAX_NUMNODES)
H Hartley Sweeten68a1b192011-01-11 17:49:32 -0600308static struct kmem_list3 __initdata initkmem_list3[NUM_INIT_LISTS];
Christoph Lametere498be72005-09-09 13:03:32 -0700309#define CACHE_CACHE 0
Pekka Enberg556a1692008-01-25 08:20:51 +0200310#define SIZE_AC MAX_NUMNODES
311#define SIZE_L3 (2 * MAX_NUMNODES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Christoph Lametered11d9e2006-06-30 01:55:45 -0700313static int drain_freelist(struct kmem_cache *cache,
314 struct kmem_list3 *l3, int tofree);
315static void free_block(struct kmem_cache *cachep, void **objpp, int len,
316 int node);
Pekka Enberg83b519e2009-06-10 19:40:04 +0300317static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp);
David Howells65f27f32006-11-22 14:55:48 +0000318static void cache_reap(struct work_struct *unused);
Christoph Lametered11d9e2006-06-30 01:55:45 -0700319
Christoph Lametere498be72005-09-09 13:03:32 -0700320/*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800321 * This function must be completely optimized away if a constant is passed to
322 * it. Mostly the same as what is in linux/slab.h except it returns an index.
Christoph Lametere498be72005-09-09 13:03:32 -0700323 */
Ivan Kokshaysky7243cc02005-09-22 21:43:58 -0700324static __always_inline int index_of(const size_t size)
Christoph Lametere498be72005-09-09 13:03:32 -0700325{
Steven Rostedt5ec8a842006-02-01 03:05:44 -0800326 extern void __bad_size(void);
327
Christoph Lametere498be72005-09-09 13:03:32 -0700328 if (__builtin_constant_p(size)) {
329 int i = 0;
330
331#define CACHE(x) \
332 if (size <=x) \
333 return i; \
334 else \
335 i++;
Joe Perches1c61fc42008-03-05 13:58:17 -0800336#include <linux/kmalloc_sizes.h>
Christoph Lametere498be72005-09-09 13:03:32 -0700337#undef CACHE
Steven Rostedt5ec8a842006-02-01 03:05:44 -0800338 __bad_size();
Ivan Kokshaysky7243cc02005-09-22 21:43:58 -0700339 } else
Steven Rostedt5ec8a842006-02-01 03:05:44 -0800340 __bad_size();
Christoph Lametere498be72005-09-09 13:03:32 -0700341 return 0;
342}
343
Ingo Molnare0a42722006-06-23 02:03:46 -0700344static int slab_early_init = 1;
345
Christoph Lametere498be72005-09-09 13:03:32 -0700346#define INDEX_AC index_of(sizeof(struct arraycache_init))
347#define INDEX_L3 index_of(sizeof(struct kmem_list3))
348
Pekka Enberg5295a742006-02-01 03:05:48 -0800349static void kmem_list3_init(struct kmem_list3 *parent)
Christoph Lametere498be72005-09-09 13:03:32 -0700350{
351 INIT_LIST_HEAD(&parent->slabs_full);
352 INIT_LIST_HEAD(&parent->slabs_partial);
353 INIT_LIST_HEAD(&parent->slabs_free);
354 parent->shared = NULL;
355 parent->alien = NULL;
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -0800356 parent->colour_next = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700357 spin_lock_init(&parent->list_lock);
358 parent->free_objects = 0;
359 parent->free_touched = 0;
360}
361
Andrew Mortona737b3e2006-03-22 00:08:11 -0800362#define MAKE_LIST(cachep, listp, slab, nodeid) \
363 do { \
364 INIT_LIST_HEAD(listp); \
365 list_splice(&(cachep->nodelists[nodeid]->slab), listp); \
Christoph Lametere498be72005-09-09 13:03:32 -0700366 } while (0)
367
Andrew Mortona737b3e2006-03-22 00:08:11 -0800368#define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
369 do { \
Christoph Lametere498be72005-09-09 13:03:32 -0700370 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
371 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
372 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
373 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375#define CFLGS_OFF_SLAB (0x80000000UL)
376#define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
377
378#define BATCHREFILL_LIMIT 16
Andrew Mortona737b3e2006-03-22 00:08:11 -0800379/*
380 * Optimization question: fewer reaps means less probability for unnessary
381 * cpucache drain/refill cycles.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 *
Adrian Bunkdc6f3f22005-11-08 16:44:08 +0100383 * OTOH the cpuarrays can contain lots of objects,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 * which could lock up otherwise freeable slabs.
385 */
386#define REAPTIMEOUT_CPUC (2*HZ)
387#define REAPTIMEOUT_LIST3 (4*HZ)
388
389#if STATS
390#define STATS_INC_ACTIVE(x) ((x)->num_active++)
391#define STATS_DEC_ACTIVE(x) ((x)->num_active--)
392#define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
393#define STATS_INC_GROWN(x) ((x)->grown++)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700394#define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
Andrew Mortona737b3e2006-03-22 00:08:11 -0800395#define STATS_SET_HIGH(x) \
396 do { \
397 if ((x)->num_active > (x)->high_mark) \
398 (x)->high_mark = (x)->num_active; \
399 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400#define STATS_INC_ERR(x) ((x)->errors++)
401#define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
Christoph Lametere498be72005-09-09 13:03:32 -0700402#define STATS_INC_NODEFREES(x) ((x)->node_frees++)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700403#define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800404#define STATS_SET_FREEABLE(x, i) \
405 do { \
406 if ((x)->max_freeable < i) \
407 (x)->max_freeable = i; \
408 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409#define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
410#define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
411#define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
412#define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
413#else
414#define STATS_INC_ACTIVE(x) do { } while (0)
415#define STATS_DEC_ACTIVE(x) do { } while (0)
416#define STATS_INC_ALLOCED(x) do { } while (0)
417#define STATS_INC_GROWN(x) do { } while (0)
Andi Kleen4e60c862010-08-09 17:19:03 -0700418#define STATS_ADD_REAPED(x,y) do { (void)(y); } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419#define STATS_SET_HIGH(x) do { } while (0)
420#define STATS_INC_ERR(x) do { } while (0)
421#define STATS_INC_NODEALLOCS(x) do { } while (0)
Christoph Lametere498be72005-09-09 13:03:32 -0700422#define STATS_INC_NODEFREES(x) do { } while (0)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700423#define STATS_INC_ACOVERFLOW(x) do { } while (0)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800424#define STATS_SET_FREEABLE(x, i) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425#define STATS_INC_ALLOCHIT(x) do { } while (0)
426#define STATS_INC_ALLOCMISS(x) do { } while (0)
427#define STATS_INC_FREEHIT(x) do { } while (0)
428#define STATS_INC_FREEMISS(x) do { } while (0)
429#endif
430
431#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Andrew Mortona737b3e2006-03-22 00:08:11 -0800433/*
434 * memory layout of objects:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 * 0 : objp
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800436 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * the end of an object is aligned with the end of the real
438 * allocation. Catches writes behind the end of the allocation.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800439 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 * redzone word.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800441 * cachep->obj_offset: The real object.
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500442 * cachep->size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
443 * cachep->size - 1* BYTES_PER_WORD: last caller address
Andrew Mortona737b3e2006-03-22 00:08:11 -0800444 * [BYTES_PER_WORD long]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800446static int obj_offset(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800448 return cachep->obj_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449}
450
David Woodhouseb46b8f12007-05-08 00:22:59 -0700451static unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
David Woodhouseb46b8f12007-05-08 00:22:59 -0700454 return (unsigned long long*) (objp + obj_offset(cachep) -
455 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
David Woodhouseb46b8f12007-05-08 00:22:59 -0700458static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
461 if (cachep->flags & SLAB_STORE_USER)
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500462 return (unsigned long long *)(objp + cachep->size -
David Woodhouseb46b8f12007-05-08 00:22:59 -0700463 sizeof(unsigned long long) -
David Woodhouse87a927c2007-07-04 21:26:44 -0400464 REDZONE_ALIGN);
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500465 return (unsigned long long *) (objp + cachep->size -
David Woodhouseb46b8f12007-05-08 00:22:59 -0700466 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Pekka Enberg343e0d72006-02-01 03:05:50 -0800469static void **dbg_userword(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470{
471 BUG_ON(!(cachep->flags & SLAB_STORE_USER));
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500472 return (void **)(objp + cachep->size - BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
475#else
476
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800477#define obj_offset(x) 0
David Woodhouseb46b8f12007-05-08 00:22:59 -0700478#define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
479#define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480#define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
481
482#endif
483
484/*
David Rientjes3df1ccc2011-10-18 22:09:28 -0700485 * Do not go above this order unless 0 objects fit into the slab or
486 * overridden on the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 */
David Rientjes543585c2011-10-18 22:09:24 -0700488#define SLAB_MAX_ORDER_HI 1
489#define SLAB_MAX_ORDER_LO 0
490static int slab_max_order = SLAB_MAX_ORDER_LO;
David Rientjes3df1ccc2011-10-18 22:09:28 -0700491static bool slab_max_order_set __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800493static inline struct kmem_cache *virt_to_cache(const void *obj)
494{
Christoph Lameterb49af682007-05-06 14:49:41 -0700495 struct page *page = virt_to_head_page(obj);
Christoph Lameter35026082012-06-13 10:24:56 -0500496 return page->slab_cache;
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800497}
498
499static inline struct slab *virt_to_slab(const void *obj)
500{
Christoph Lameterb49af682007-05-06 14:49:41 -0700501 struct page *page = virt_to_head_page(obj);
Christoph Lameter35026082012-06-13 10:24:56 -0500502
503 VM_BUG_ON(!PageSlab(page));
504 return page->slab_page;
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800505}
506
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800507static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
508 unsigned int idx)
509{
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500510 return slab->s_mem + cache->size * idx;
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800511}
512
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800513/*
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500514 * We want to avoid an expensive divide : (offset / cache->size)
515 * Using the fact that size is a constant for a particular cache,
516 * we can replace (offset / cache->size) by
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800517 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
518 */
519static inline unsigned int obj_to_index(const struct kmem_cache *cache,
520 const struct slab *slab, void *obj)
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800521{
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800522 u32 offset = (obj - slab->s_mem);
523 return reciprocal_divide(offset, cache->reciprocal_buffer_size);
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800524}
525
Andrew Mortona737b3e2006-03-22 00:08:11 -0800526/*
527 * These are the default caches for kmalloc. Custom caches can have other sizes.
528 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529struct cache_sizes malloc_sizes[] = {
530#define CACHE(x) { .cs_size = (x) },
531#include <linux/kmalloc_sizes.h>
532 CACHE(ULONG_MAX)
533#undef CACHE
534};
535EXPORT_SYMBOL(malloc_sizes);
536
537/* Must match cache_sizes above. Out of line to keep cache footprint low. */
538struct cache_names {
539 char *name;
540 char *name_dma;
541};
542
543static struct cache_names __initdata cache_names[] = {
544#define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },
545#include <linux/kmalloc_sizes.h>
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800546 {NULL,}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547#undef CACHE
548};
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550static struct arraycache_init initarray_generic =
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800551 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553/* internal cache of cache description objs */
Christoph Lameter9b030cb2012-09-05 00:20:33 +0000554static struct kmem_cache kmem_cache_boot = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800555 .batchcount = 1,
556 .limit = BOOT_CPUCACHE_ENTRIES,
557 .shared = 1,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -0500558 .size = sizeof(struct kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800559 .name = "kmem_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560};
561
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700562#define BAD_ALIEN_MAGIC 0x01020304ul
563
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200564#ifdef CONFIG_LOCKDEP
565
566/*
567 * Slab sometimes uses the kmalloc slabs to store the slab headers
568 * for other slabs "off slab".
569 * The locking for this is tricky in that it nests within the locks
570 * of all other slabs in a few places; to deal with this special
571 * locking we put on-slab caches into a separate lock-class.
572 *
573 * We set lock class for alien array caches which are up during init.
574 * The lock annotation will be lost if all cpus of a node goes down and
575 * then comes back up during hotplug
576 */
577static struct lock_class_key on_slab_l3_key;
578static struct lock_class_key on_slab_alc_key;
579
Peter Zijlstra83835b32011-07-22 15:26:05 +0200580static struct lock_class_key debugobj_l3_key;
581static struct lock_class_key debugobj_alc_key;
582
583static void slab_set_lock_classes(struct kmem_cache *cachep,
584 struct lock_class_key *l3_key, struct lock_class_key *alc_key,
585 int q)
586{
587 struct array_cache **alc;
588 struct kmem_list3 *l3;
589 int r;
590
591 l3 = cachep->nodelists[q];
592 if (!l3)
593 return;
594
595 lockdep_set_class(&l3->list_lock, l3_key);
596 alc = l3->alien;
597 /*
598 * FIXME: This check for BAD_ALIEN_MAGIC
599 * should go away when common slab code is taught to
600 * work even without alien caches.
601 * Currently, non NUMA code returns BAD_ALIEN_MAGIC
602 * for alloc_alien_cache,
603 */
604 if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC)
605 return;
606 for_each_node(r) {
607 if (alc[r])
608 lockdep_set_class(&alc[r]->lock, alc_key);
609 }
610}
611
612static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
613{
614 slab_set_lock_classes(cachep, &debugobj_l3_key, &debugobj_alc_key, node);
615}
616
617static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
618{
619 int node;
620
621 for_each_online_node(node)
622 slab_set_debugobj_lock_classes_node(cachep, node);
623}
624
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200625static void init_node_lock_keys(int q)
626{
627 struct cache_sizes *s = malloc_sizes;
628
Christoph Lameter97d06602012-07-06 15:25:11 -0500629 if (slab_state < UP)
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200630 return;
631
632 for (s = malloc_sizes; s->cs_size != ULONG_MAX; s++) {
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200633 struct kmem_list3 *l3;
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200634
635 l3 = s->cs_cachep->nodelists[q];
636 if (!l3 || OFF_SLAB(s->cs_cachep))
Pekka Enberg00afa752009-12-27 14:33:14 +0200637 continue;
Peter Zijlstra83835b32011-07-22 15:26:05 +0200638
639 slab_set_lock_classes(s->cs_cachep, &on_slab_l3_key,
640 &on_slab_alc_key, q);
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200641 }
642}
643
644static inline void init_lock_keys(void)
645{
646 int node;
647
648 for_each_node(node)
649 init_node_lock_keys(node);
650}
651#else
652static void init_node_lock_keys(int q)
653{
654}
655
656static inline void init_lock_keys(void)
657{
658}
Peter Zijlstra83835b32011-07-22 15:26:05 +0200659
660static void slab_set_debugobj_lock_classes_node(struct kmem_cache *cachep, int node)
661{
662}
663
664static void slab_set_debugobj_lock_classes(struct kmem_cache *cachep)
665{
666}
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200667#endif
668
Tejun Heo1871e522009-10-29 22:34:13 +0900669static DEFINE_PER_CPU(struct delayed_work, slab_reap_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Pekka Enberg343e0d72006-02-01 03:05:50 -0800671static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672{
673 return cachep->array[smp_processor_id()];
674}
675
Andrew Mortona737b3e2006-03-22 00:08:11 -0800676static inline struct kmem_cache *__find_general_cachep(size_t size,
677 gfp_t gfpflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 struct cache_sizes *csizep = malloc_sizes;
680
681#if DEBUG
682 /* This happens if someone tries to call
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800683 * kmem_cache_create(), or __kmalloc(), before
684 * the generic caches are initialized.
685 */
Alok Katariac7e43c72005-09-14 12:17:53 -0700686 BUG_ON(malloc_sizes[INDEX_AC].cs_cachep == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687#endif
Christoph Lameter6cb8f912007-07-17 04:03:22 -0700688 if (!size)
689 return ZERO_SIZE_PTR;
690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 while (size > csizep->cs_size)
692 csizep++;
693
694 /*
Martin Hicks0abf40c2005-09-03 15:54:54 -0700695 * Really subtle: The last entry with cs->cs_size==ULONG_MAX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 * has cs_{dma,}cachep==NULL. Thus no special case
697 * for large kmalloc calls required.
698 */
Christoph Lameter4b51d662007-02-10 01:43:10 -0800699#ifdef CONFIG_ZONE_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (unlikely(gfpflags & GFP_DMA))
701 return csizep->cs_dmacachep;
Christoph Lameter4b51d662007-02-10 01:43:10 -0800702#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return csizep->cs_cachep;
704}
705
Adrian Bunkb2213852006-09-25 23:31:02 -0700706static struct kmem_cache *kmem_find_general_cachep(size_t size, gfp_t gfpflags)
Manfred Spraul97e2bde2005-05-01 08:58:38 -0700707{
708 return __find_general_cachep(size, gfpflags);
709}
Manfred Spraul97e2bde2005-05-01 08:58:38 -0700710
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800711static size_t slab_mgmt_size(size_t nr_objs, size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800713 return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align);
714}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Andrew Mortona737b3e2006-03-22 00:08:11 -0800716/*
717 * Calculate the number of objects and left-over bytes for a given buffer size.
718 */
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800719static void cache_estimate(unsigned long gfporder, size_t buffer_size,
720 size_t align, int flags, size_t *left_over,
721 unsigned int *num)
722{
723 int nr_objs;
724 size_t mgmt_size;
725 size_t slab_size = PAGE_SIZE << gfporder;
726
727 /*
728 * The slab management structure can be either off the slab or
729 * on it. For the latter case, the memory allocated for a
730 * slab is used for:
731 *
732 * - The struct slab
733 * - One kmem_bufctl_t for each object
734 * - Padding to respect alignment of @align
735 * - @buffer_size bytes for each object
736 *
737 * If the slab management structure is off the slab, then the
738 * alignment will already be calculated into the size. Because
739 * the slabs are all pages aligned, the objects will be at the
740 * correct alignment when allocated.
741 */
742 if (flags & CFLGS_OFF_SLAB) {
743 mgmt_size = 0;
744 nr_objs = slab_size / buffer_size;
745
746 if (nr_objs > SLAB_LIMIT)
747 nr_objs = SLAB_LIMIT;
748 } else {
749 /*
750 * Ignore padding for the initial guess. The padding
751 * is at most @align-1 bytes, and @buffer_size is at
752 * least @align. In the worst case, this result will
753 * be one greater than the number of objects that fit
754 * into the memory allocation when taking the padding
755 * into account.
756 */
757 nr_objs = (slab_size - sizeof(struct slab)) /
758 (buffer_size + sizeof(kmem_bufctl_t));
759
760 /*
761 * This calculated number will be either the right
762 * amount, or one greater than what we want.
763 */
764 if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
765 > slab_size)
766 nr_objs--;
767
768 if (nr_objs > SLAB_LIMIT)
769 nr_objs = SLAB_LIMIT;
770
771 mgmt_size = slab_mgmt_size(nr_objs, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800773 *num = nr_objs;
774 *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775}
776
Christoph Lameterf28510d2012-09-11 19:49:38 +0000777#if DEBUG
Harvey Harrisond40cee22008-04-30 00:55:07 -0700778#define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
Andrew Mortona737b3e2006-03-22 00:08:11 -0800780static void __slab_error(const char *function, struct kmem_cache *cachep,
781 char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800784 function, cachep->name, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 dump_stack();
Dave Jones645df232012-09-18 15:54:12 -0400786 add_taint(TAINT_BAD_PAGE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787}
Christoph Lameterf28510d2012-09-11 19:49:38 +0000788#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
Paul Menage3395ee02006-12-06 20:32:16 -0800790/*
791 * By default on NUMA we use alien caches to stage the freeing of
792 * objects allocated from other nodes. This causes massive memory
793 * inefficiencies when using fake NUMA setup to split memory into a
794 * large number of small nodes, so it can be disabled on the command
795 * line
796 */
797
798static int use_alien_caches __read_mostly = 1;
799static int __init noaliencache_setup(char *s)
800{
801 use_alien_caches = 0;
802 return 1;
803}
804__setup("noaliencache", noaliencache_setup);
805
David Rientjes3df1ccc2011-10-18 22:09:28 -0700806static int __init slab_max_order_setup(char *str)
807{
808 get_option(&str, &slab_max_order);
809 slab_max_order = slab_max_order < 0 ? 0 :
810 min(slab_max_order, MAX_ORDER - 1);
811 slab_max_order_set = true;
812
813 return 1;
814}
815__setup("slab_max_order=", slab_max_order_setup);
816
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800817#ifdef CONFIG_NUMA
818/*
819 * Special reaping functions for NUMA systems called from cache_reap().
820 * These take care of doing round robin flushing of alien caches (containing
821 * objects freed on different nodes from which they were allocated) and the
822 * flushing of remote pcps by calling drain_node_pages.
823 */
Tejun Heo1871e522009-10-29 22:34:13 +0900824static DEFINE_PER_CPU(unsigned long, slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800825
826static void init_reap_node(int cpu)
827{
828 int node;
829
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -0700830 node = next_node(cpu_to_mem(cpu), node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800831 if (node == MAX_NUMNODES)
Paul Jackson442295c2006-03-22 00:09:11 -0800832 node = first_node(node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800833
Tejun Heo1871e522009-10-29 22:34:13 +0900834 per_cpu(slab_reap_node, cpu) = node;
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800835}
836
837static void next_reap_node(void)
838{
Christoph Lameter909ea962010-12-08 16:22:55 +0100839 int node = __this_cpu_read(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800840
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800841 node = next_node(node, node_online_map);
842 if (unlikely(node >= MAX_NUMNODES))
843 node = first_node(node_online_map);
Christoph Lameter909ea962010-12-08 16:22:55 +0100844 __this_cpu_write(slab_reap_node, node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800845}
846
847#else
848#define init_reap_node(cpu) do { } while (0)
849#define next_reap_node(void) do { } while (0)
850#endif
851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852/*
853 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
854 * via the workqueue/eventd.
855 * Add the CPU number into the expiration time to minimize the possibility of
856 * the CPUs getting into lockstep and contending for the global cache chain
857 * lock.
858 */
Adrian Bunk897e6792007-07-15 23:38:20 -0700859static void __cpuinit start_cpu_timer(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Tejun Heo1871e522009-10-29 22:34:13 +0900861 struct delayed_work *reap_work = &per_cpu(slab_reap_work, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
863 /*
864 * When this gets called from do_initcalls via cpucache_init(),
865 * init_workqueues() has already run, so keventd will be setup
866 * at that time.
867 */
David Howells52bad642006-11-22 14:54:01 +0000868 if (keventd_up() && reap_work->work.func == NULL) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800869 init_reap_node(cpu);
Tejun Heo203b42f2012-08-21 13:18:23 -0700870 INIT_DEFERRABLE_WORK(reap_work, cache_reap);
Arjan van de Ven2b284212006-12-10 02:21:28 -0800871 schedule_delayed_work_on(cpu, reap_work,
872 __round_jiffies_relative(HZ, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
874}
875
Christoph Lametere498be72005-09-09 13:03:32 -0700876static struct array_cache *alloc_arraycache(int node, int entries,
Pekka Enberg83b519e2009-06-10 19:40:04 +0300877 int batchcount, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800879 int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 struct array_cache *nc = NULL;
881
Pekka Enberg83b519e2009-06-10 19:40:04 +0300882 nc = kmalloc_node(memsize, gfp, node);
Catalin Marinasd5cff632009-06-11 13:22:40 +0100883 /*
884 * The array_cache structures contain pointers to free object.
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300885 * However, when such objects are allocated or transferred to another
Catalin Marinasd5cff632009-06-11 13:22:40 +0100886 * cache the pointers are not cleared and they could be counted as
887 * valid references during a kmemleak scan. Therefore, kmemleak must
888 * not scan such objects.
889 */
890 kmemleak_no_scan(nc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (nc) {
892 nc->avail = 0;
893 nc->limit = entries;
894 nc->batchcount = batchcount;
895 nc->touched = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700896 spin_lock_init(&nc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898 return nc;
899}
900
Mel Gorman072bb0a2012-07-31 16:43:58 -0700901static inline bool is_slab_pfmemalloc(struct slab *slabp)
902{
903 struct page *page = virt_to_page(slabp->s_mem);
904
905 return PageSlabPfmemalloc(page);
906}
907
908/* Clears pfmemalloc_active if no slabs have pfmalloc set */
909static void recheck_pfmemalloc_active(struct kmem_cache *cachep,
910 struct array_cache *ac)
911{
912 struct kmem_list3 *l3 = cachep->nodelists[numa_mem_id()];
913 struct slab *slabp;
914 unsigned long flags;
915
916 if (!pfmemalloc_active)
917 return;
918
919 spin_lock_irqsave(&l3->list_lock, flags);
920 list_for_each_entry(slabp, &l3->slabs_full, list)
921 if (is_slab_pfmemalloc(slabp))
922 goto out;
923
924 list_for_each_entry(slabp, &l3->slabs_partial, list)
925 if (is_slab_pfmemalloc(slabp))
926 goto out;
927
928 list_for_each_entry(slabp, &l3->slabs_free, list)
929 if (is_slab_pfmemalloc(slabp))
930 goto out;
931
932 pfmemalloc_active = false;
933out:
934 spin_unlock_irqrestore(&l3->list_lock, flags);
935}
936
Mel Gorman381760e2012-07-31 16:44:30 -0700937static void *__ac_get_obj(struct kmem_cache *cachep, struct array_cache *ac,
Mel Gorman072bb0a2012-07-31 16:43:58 -0700938 gfp_t flags, bool force_refill)
939{
940 int i;
941 void *objp = ac->entry[--ac->avail];
942
943 /* Ensure the caller is allowed to use objects from PFMEMALLOC slab */
944 if (unlikely(is_obj_pfmemalloc(objp))) {
945 struct kmem_list3 *l3;
946
947 if (gfp_pfmemalloc_allowed(flags)) {
948 clear_obj_pfmemalloc(&objp);
949 return objp;
950 }
951
952 /* The caller cannot use PFMEMALLOC objects, find another one */
Joonsoo Kimd014dc22012-09-17 14:09:06 -0700953 for (i = 0; i < ac->avail; i++) {
Mel Gorman072bb0a2012-07-31 16:43:58 -0700954 /* If a !PFMEMALLOC object is found, swap them */
955 if (!is_obj_pfmemalloc(ac->entry[i])) {
956 objp = ac->entry[i];
957 ac->entry[i] = ac->entry[ac->avail];
958 ac->entry[ac->avail] = objp;
959 return objp;
960 }
961 }
962
963 /*
964 * If there are empty slabs on the slabs_free list and we are
965 * being forced to refill the cache, mark this one !pfmemalloc.
966 */
967 l3 = cachep->nodelists[numa_mem_id()];
968 if (!list_empty(&l3->slabs_free) && force_refill) {
969 struct slab *slabp = virt_to_slab(objp);
Mel Gorman30c29be2012-09-17 14:09:03 -0700970 ClearPageSlabPfmemalloc(virt_to_head_page(slabp->s_mem));
Mel Gorman072bb0a2012-07-31 16:43:58 -0700971 clear_obj_pfmemalloc(&objp);
972 recheck_pfmemalloc_active(cachep, ac);
973 return objp;
974 }
975
976 /* No !PFMEMALLOC objects available */
977 ac->avail++;
978 objp = NULL;
979 }
980
981 return objp;
982}
983
Mel Gorman381760e2012-07-31 16:44:30 -0700984static inline void *ac_get_obj(struct kmem_cache *cachep,
985 struct array_cache *ac, gfp_t flags, bool force_refill)
986{
987 void *objp;
988
989 if (unlikely(sk_memalloc_socks()))
990 objp = __ac_get_obj(cachep, ac, flags, force_refill);
991 else
992 objp = ac->entry[--ac->avail];
993
994 return objp;
995}
996
997static void *__ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
Mel Gorman072bb0a2012-07-31 16:43:58 -0700998 void *objp)
999{
1000 if (unlikely(pfmemalloc_active)) {
1001 /* Some pfmemalloc slabs exist, check if this is one */
Mel Gorman30c29be2012-09-17 14:09:03 -07001002 struct page *page = virt_to_head_page(objp);
Mel Gorman072bb0a2012-07-31 16:43:58 -07001003 if (PageSlabPfmemalloc(page))
1004 set_obj_pfmemalloc(&objp);
1005 }
1006
Mel Gorman381760e2012-07-31 16:44:30 -07001007 return objp;
1008}
1009
1010static inline void ac_put_obj(struct kmem_cache *cachep, struct array_cache *ac,
1011 void *objp)
1012{
1013 if (unlikely(sk_memalloc_socks()))
1014 objp = __ac_put_obj(cachep, ac, objp);
1015
Mel Gorman072bb0a2012-07-31 16:43:58 -07001016 ac->entry[ac->avail++] = objp;
1017}
1018
Christoph Lameter3ded1752006-03-25 03:06:44 -08001019/*
1020 * Transfer objects in one arraycache to another.
1021 * Locking must be handled by the caller.
1022 *
1023 * Return the number of entries transferred.
1024 */
1025static int transfer_objects(struct array_cache *to,
1026 struct array_cache *from, unsigned int max)
1027{
1028 /* Figure out how many entries to transfer */
Hagen Paul Pfeifer732eacc2010-10-26 14:22:23 -07001029 int nr = min3(from->avail, max, to->limit - to->avail);
Christoph Lameter3ded1752006-03-25 03:06:44 -08001030
1031 if (!nr)
1032 return 0;
1033
1034 memcpy(to->entry + to->avail, from->entry + from->avail -nr,
1035 sizeof(void *) *nr);
1036
1037 from->avail -= nr;
1038 to->avail += nr;
Christoph Lameter3ded1752006-03-25 03:06:44 -08001039 return nr;
1040}
1041
Christoph Lameter765c4502006-09-27 01:50:08 -07001042#ifndef CONFIG_NUMA
1043
1044#define drain_alien_cache(cachep, alien) do { } while (0)
1045#define reap_alien(cachep, l3) do { } while (0)
1046
Pekka Enberg83b519e2009-06-10 19:40:04 +03001047static inline struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
Christoph Lameter765c4502006-09-27 01:50:08 -07001048{
1049 return (struct array_cache **)BAD_ALIEN_MAGIC;
1050}
1051
1052static inline void free_alien_cache(struct array_cache **ac_ptr)
1053{
1054}
1055
1056static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
1057{
1058 return 0;
1059}
1060
1061static inline void *alternate_node_alloc(struct kmem_cache *cachep,
1062 gfp_t flags)
1063{
1064 return NULL;
1065}
1066
Christoph Hellwig8b98c162006-12-06 20:32:30 -08001067static inline void *____cache_alloc_node(struct kmem_cache *cachep,
Christoph Lameter765c4502006-09-27 01:50:08 -07001068 gfp_t flags, int nodeid)
1069{
1070 return NULL;
1071}
1072
1073#else /* CONFIG_NUMA */
1074
Christoph Hellwig8b98c162006-12-06 20:32:30 -08001075static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
Paul Jacksonc61afb12006-03-24 03:16:08 -08001076static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
Christoph Lameterdc85da12006-01-18 17:42:36 -08001077
Pekka Enberg83b519e2009-06-10 19:40:04 +03001078static struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -07001079{
1080 struct array_cache **ac_ptr;
Christoph Lameter8ef82862007-02-20 13:57:52 -08001081 int memsize = sizeof(void *) * nr_node_ids;
Christoph Lametere498be72005-09-09 13:03:32 -07001082 int i;
1083
1084 if (limit > 1)
1085 limit = 12;
Haicheng Lif3186a92010-01-06 15:25:23 +08001086 ac_ptr = kzalloc_node(memsize, gfp, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001087 if (ac_ptr) {
1088 for_each_node(i) {
Haicheng Lif3186a92010-01-06 15:25:23 +08001089 if (i == node || !node_online(i))
Christoph Lametere498be72005-09-09 13:03:32 -07001090 continue;
Pekka Enberg83b519e2009-06-10 19:40:04 +03001091 ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d, gfp);
Christoph Lametere498be72005-09-09 13:03:32 -07001092 if (!ac_ptr[i]) {
Akinobu Mitacc550de2007-11-14 16:58:35 -08001093 for (i--; i >= 0; i--)
Christoph Lametere498be72005-09-09 13:03:32 -07001094 kfree(ac_ptr[i]);
1095 kfree(ac_ptr);
1096 return NULL;
1097 }
1098 }
1099 }
1100 return ac_ptr;
1101}
1102
Pekka Enberg5295a742006-02-01 03:05:48 -08001103static void free_alien_cache(struct array_cache **ac_ptr)
Christoph Lametere498be72005-09-09 13:03:32 -07001104{
1105 int i;
1106
1107 if (!ac_ptr)
1108 return;
Christoph Lametere498be72005-09-09 13:03:32 -07001109 for_each_node(i)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001110 kfree(ac_ptr[i]);
Christoph Lametere498be72005-09-09 13:03:32 -07001111 kfree(ac_ptr);
1112}
1113
Pekka Enberg343e0d72006-02-01 03:05:50 -08001114static void __drain_alien_cache(struct kmem_cache *cachep,
Pekka Enberg5295a742006-02-01 03:05:48 -08001115 struct array_cache *ac, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07001116{
1117 struct kmem_list3 *rl3 = cachep->nodelists[node];
1118
1119 if (ac->avail) {
1120 spin_lock(&rl3->list_lock);
Christoph Lametere00946f2006-03-25 03:06:45 -08001121 /*
1122 * Stuff objects into the remote nodes shared array first.
1123 * That way we could avoid the overhead of putting the objects
1124 * into the free lists and getting them back later.
1125 */
shin, jacob693f7d32006-04-28 10:54:37 -05001126 if (rl3->shared)
1127 transfer_objects(rl3->shared, ac, ac->limit);
Christoph Lametere00946f2006-03-25 03:06:45 -08001128
Christoph Lameterff694162005-09-22 21:44:02 -07001129 free_block(cachep, ac->entry, ac->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001130 ac->avail = 0;
1131 spin_unlock(&rl3->list_lock);
1132 }
1133}
1134
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001135/*
1136 * Called from cache_reap() to regularly drain alien caches round robin.
1137 */
1138static void reap_alien(struct kmem_cache *cachep, struct kmem_list3 *l3)
1139{
Christoph Lameter909ea962010-12-08 16:22:55 +01001140 int node = __this_cpu_read(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001141
1142 if (l3->alien) {
1143 struct array_cache *ac = l3->alien[node];
Christoph Lametere00946f2006-03-25 03:06:45 -08001144
1145 if (ac && ac->avail && spin_trylock_irq(&ac->lock)) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001146 __drain_alien_cache(cachep, ac, node);
1147 spin_unlock_irq(&ac->lock);
1148 }
1149 }
1150}
1151
Andrew Mortona737b3e2006-03-22 00:08:11 -08001152static void drain_alien_cache(struct kmem_cache *cachep,
1153 struct array_cache **alien)
Christoph Lametere498be72005-09-09 13:03:32 -07001154{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001155 int i = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07001156 struct array_cache *ac;
1157 unsigned long flags;
1158
1159 for_each_online_node(i) {
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001160 ac = alien[i];
Christoph Lametere498be72005-09-09 13:03:32 -07001161 if (ac) {
1162 spin_lock_irqsave(&ac->lock, flags);
1163 __drain_alien_cache(cachep, ac, i);
1164 spin_unlock_irqrestore(&ac->lock, flags);
1165 }
1166 }
1167}
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001168
Ingo Molnar873623d2006-07-13 14:44:38 +02001169static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001170{
1171 struct slab *slabp = virt_to_slab(objp);
1172 int nodeid = slabp->nodeid;
1173 struct kmem_list3 *l3;
1174 struct array_cache *alien = NULL;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001175 int node;
1176
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001177 node = numa_mem_id();
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001178
1179 /*
1180 * Make sure we are not freeing a object from another node to the array
1181 * cache on this cpu.
1182 */
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001183 if (likely(slabp->nodeid == node))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001184 return 0;
1185
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001186 l3 = cachep->nodelists[node];
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001187 STATS_INC_NODEFREES(cachep);
1188 if (l3->alien && l3->alien[nodeid]) {
1189 alien = l3->alien[nodeid];
Ingo Molnar873623d2006-07-13 14:44:38 +02001190 spin_lock(&alien->lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001191 if (unlikely(alien->avail == alien->limit)) {
1192 STATS_INC_ACOVERFLOW(cachep);
1193 __drain_alien_cache(cachep, alien, nodeid);
1194 }
Mel Gorman072bb0a2012-07-31 16:43:58 -07001195 ac_put_obj(cachep, alien, objp);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001196 spin_unlock(&alien->lock);
1197 } else {
1198 spin_lock(&(cachep->nodelists[nodeid])->list_lock);
1199 free_block(cachep, &objp, 1, nodeid);
1200 spin_unlock(&(cachep->nodelists[nodeid])->list_lock);
1201 }
1202 return 1;
1203}
Christoph Lametere498be72005-09-09 13:03:32 -07001204#endif
1205
David Rientjes8f9f8d92010-03-27 19:40:47 -07001206/*
1207 * Allocates and initializes nodelists for a node on each slab cache, used for
1208 * either memory or cpu hotplug. If memory is being hot-added, the kmem_list3
1209 * will be allocated off-node since memory is not yet online for the new node.
1210 * When hotplugging memory or a cpu, existing nodelists are not replaced if
1211 * already in use.
1212 *
Christoph Lameter18004c52012-07-06 15:25:12 -05001213 * Must hold slab_mutex.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001214 */
1215static int init_cache_nodelists_node(int node)
1216{
1217 struct kmem_cache *cachep;
1218 struct kmem_list3 *l3;
1219 const int memsize = sizeof(struct kmem_list3);
1220
Christoph Lameter18004c52012-07-06 15:25:12 -05001221 list_for_each_entry(cachep, &slab_caches, list) {
David Rientjes8f9f8d92010-03-27 19:40:47 -07001222 /*
1223 * Set up the size64 kmemlist for cpu before we can
1224 * begin anything. Make sure some other cpu on this
1225 * node has not already allocated this
1226 */
1227 if (!cachep->nodelists[node]) {
1228 l3 = kmalloc_node(memsize, GFP_KERNEL, node);
1229 if (!l3)
1230 return -ENOMEM;
1231 kmem_list3_init(l3);
1232 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
1233 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1234
1235 /*
1236 * The l3s don't come and go as CPUs come and
Christoph Lameter18004c52012-07-06 15:25:12 -05001237 * go. slab_mutex is sufficient
David Rientjes8f9f8d92010-03-27 19:40:47 -07001238 * protection here.
1239 */
1240 cachep->nodelists[node] = l3;
1241 }
1242
1243 spin_lock_irq(&cachep->nodelists[node]->list_lock);
1244 cachep->nodelists[node]->free_limit =
1245 (1 + nr_cpus_node(node)) *
1246 cachep->batchcount + cachep->num;
1247 spin_unlock_irq(&cachep->nodelists[node]->list_lock);
1248 }
1249 return 0;
1250}
1251
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001252static void __cpuinit cpuup_canceled(long cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253{
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001254 struct kmem_cache *cachep;
1255 struct kmem_list3 *l3 = NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001256 int node = cpu_to_mem(cpu);
Rusty Russella70f7302009-03-13 14:49:46 +10301257 const struct cpumask *mask = cpumask_of_node(node);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001258
Christoph Lameter18004c52012-07-06 15:25:12 -05001259 list_for_each_entry(cachep, &slab_caches, list) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001260 struct array_cache *nc;
1261 struct array_cache *shared;
1262 struct array_cache **alien;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001263
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001264 /* cpu is dead; no one can alloc from it. */
1265 nc = cachep->array[cpu];
1266 cachep->array[cpu] = NULL;
1267 l3 = cachep->nodelists[node];
1268
1269 if (!l3)
1270 goto free_array_cache;
1271
1272 spin_lock_irq(&l3->list_lock);
1273
1274 /* Free limit for this kmem_list3 */
1275 l3->free_limit -= cachep->batchcount;
1276 if (nc)
1277 free_block(cachep, nc->entry, nc->avail, node);
1278
Rusty Russell58463c12009-12-17 11:43:12 -06001279 if (!cpumask_empty(mask)) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001280 spin_unlock_irq(&l3->list_lock);
1281 goto free_array_cache;
1282 }
1283
1284 shared = l3->shared;
1285 if (shared) {
1286 free_block(cachep, shared->entry,
1287 shared->avail, node);
1288 l3->shared = NULL;
1289 }
1290
1291 alien = l3->alien;
1292 l3->alien = NULL;
1293
1294 spin_unlock_irq(&l3->list_lock);
1295
1296 kfree(shared);
1297 if (alien) {
1298 drain_alien_cache(cachep, alien);
1299 free_alien_cache(alien);
1300 }
1301free_array_cache:
1302 kfree(nc);
1303 }
1304 /*
1305 * In the previous loop, all the objects were freed to
1306 * the respective cache's slabs, now we can go ahead and
1307 * shrink each nodelist to its limit.
1308 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001309 list_for_each_entry(cachep, &slab_caches, list) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001310 l3 = cachep->nodelists[node];
1311 if (!l3)
1312 continue;
1313 drain_freelist(cachep, l3, l3->free_objects);
1314 }
1315}
1316
1317static int __cpuinit cpuup_prepare(long cpu)
1318{
Pekka Enberg343e0d72006-02-01 03:05:50 -08001319 struct kmem_cache *cachep;
Christoph Lametere498be72005-09-09 13:03:32 -07001320 struct kmem_list3 *l3 = NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07001321 int node = cpu_to_mem(cpu);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001322 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001324 /*
1325 * We need to do this right in the beginning since
1326 * alloc_arraycache's are going to use this list.
1327 * kmalloc_node allows us to add the slab to the right
1328 * kmem_list3 and not this cpu's kmem_list3
1329 */
David Rientjes8f9f8d92010-03-27 19:40:47 -07001330 err = init_cache_nodelists_node(node);
1331 if (err < 0)
1332 goto bad;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001333
1334 /*
1335 * Now we can go ahead with allocating the shared arrays and
1336 * array caches
1337 */
Christoph Lameter18004c52012-07-06 15:25:12 -05001338 list_for_each_entry(cachep, &slab_caches, list) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001339 struct array_cache *nc;
1340 struct array_cache *shared = NULL;
1341 struct array_cache **alien = NULL;
1342
1343 nc = alloc_arraycache(node, cachep->limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001344 cachep->batchcount, GFP_KERNEL);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001345 if (!nc)
1346 goto bad;
1347 if (cachep->shared) {
1348 shared = alloc_arraycache(node,
1349 cachep->shared * cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001350 0xbaadf00d, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001351 if (!shared) {
1352 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001353 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001354 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001355 }
1356 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03001357 alien = alloc_alien_cache(node, cachep->limit, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001358 if (!alien) {
1359 kfree(shared);
1360 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001361 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001362 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001363 }
1364 cachep->array[cpu] = nc;
1365 l3 = cachep->nodelists[node];
1366 BUG_ON(!l3);
1367
1368 spin_lock_irq(&l3->list_lock);
1369 if (!l3->shared) {
1370 /*
1371 * We are serialised from CPU_DEAD or
1372 * CPU_UP_CANCELLED by the cpucontrol lock
1373 */
1374 l3->shared = shared;
1375 shared = NULL;
1376 }
1377#ifdef CONFIG_NUMA
1378 if (!l3->alien) {
1379 l3->alien = alien;
1380 alien = NULL;
1381 }
1382#endif
1383 spin_unlock_irq(&l3->list_lock);
1384 kfree(shared);
1385 free_alien_cache(alien);
Peter Zijlstra83835b32011-07-22 15:26:05 +02001386 if (cachep->flags & SLAB_DEBUG_OBJECTS)
1387 slab_set_debugobj_lock_classes_node(cachep, node);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001388 }
Pekka Enbergce79ddc2009-11-23 22:01:15 +02001389 init_node_lock_keys(node);
1390
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001391 return 0;
1392bad:
Akinobu Mita12d00f62007-10-18 03:05:11 -07001393 cpuup_canceled(cpu);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001394 return -ENOMEM;
1395}
1396
1397static int __cpuinit cpuup_callback(struct notifier_block *nfb,
1398 unsigned long action, void *hcpu)
1399{
1400 long cpu = (long)hcpu;
1401 int err = 0;
1402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 switch (action) {
Heiko Carstens38c3bd92007-05-09 02:34:05 -07001404 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001405 case CPU_UP_PREPARE_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001406 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001407 err = cpuup_prepare(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001408 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 break;
1410 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001411 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 start_cpu_timer(cpu);
1413 break;
1414#ifdef CONFIG_HOTPLUG_CPU
Christoph Lameter5830c592007-05-09 02:34:22 -07001415 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001416 case CPU_DOWN_PREPARE_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001417 /*
Christoph Lameter18004c52012-07-06 15:25:12 -05001418 * Shutdown cache reaper. Note that the slab_mutex is
Christoph Lameter5830c592007-05-09 02:34:22 -07001419 * held so that if cache_reap() is invoked it cannot do
1420 * anything expensive but will only modify reap_work
1421 * and reschedule the timer.
1422 */
Tejun Heoafe2c512010-12-14 16:21:17 +01001423 cancel_delayed_work_sync(&per_cpu(slab_reap_work, cpu));
Christoph Lameter5830c592007-05-09 02:34:22 -07001424 /* Now the cache_reaper is guaranteed to be not running. */
Tejun Heo1871e522009-10-29 22:34:13 +09001425 per_cpu(slab_reap_work, cpu).work.func = NULL;
Christoph Lameter5830c592007-05-09 02:34:22 -07001426 break;
1427 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001428 case CPU_DOWN_FAILED_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001429 start_cpu_timer(cpu);
1430 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001432 case CPU_DEAD_FROZEN:
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001433 /*
1434 * Even if all the cpus of a node are down, we don't free the
1435 * kmem_list3 of any cache. This to avoid a race between
1436 * cpu_down, and a kmalloc allocation from another cpu for
1437 * memory from the node of the cpu going down. The list3
1438 * structure is usually allocated from kmem_cache_create() and
1439 * gets destroyed at kmem_cache_destroy().
1440 */
Simon Arlott183ff222007-10-20 01:27:18 +02001441 /* fall through */
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08001442#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001444 case CPU_UP_CANCELED_FROZEN:
Christoph Lameter18004c52012-07-06 15:25:12 -05001445 mutex_lock(&slab_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001446 cpuup_canceled(cpu);
Christoph Lameter18004c52012-07-06 15:25:12 -05001447 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
Akinobu Mitaeac40682010-05-26 14:43:32 -07001450 return notifier_from_errno(err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451}
1452
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001453static struct notifier_block __cpuinitdata cpucache_notifier = {
1454 &cpuup_callback, NULL, 0
1455};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
David Rientjes8f9f8d92010-03-27 19:40:47 -07001457#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
1458/*
1459 * Drains freelist for a node on each slab cache, used for memory hot-remove.
1460 * Returns -EBUSY if all objects cannot be drained so that the node is not
1461 * removed.
1462 *
Christoph Lameter18004c52012-07-06 15:25:12 -05001463 * Must hold slab_mutex.
David Rientjes8f9f8d92010-03-27 19:40:47 -07001464 */
1465static int __meminit drain_cache_nodelists_node(int node)
1466{
1467 struct kmem_cache *cachep;
1468 int ret = 0;
1469
Christoph Lameter18004c52012-07-06 15:25:12 -05001470 list_for_each_entry(cachep, &slab_caches, list) {
David Rientjes8f9f8d92010-03-27 19:40:47 -07001471 struct kmem_list3 *l3;
1472
1473 l3 = cachep->nodelists[node];
1474 if (!l3)
1475 continue;
1476
1477 drain_freelist(cachep, l3, l3->free_objects);
1478
1479 if (!list_empty(&l3->slabs_full) ||
1480 !list_empty(&l3->slabs_partial)) {
1481 ret = -EBUSY;
1482 break;
1483 }
1484 }
1485 return ret;
1486}
1487
1488static int __meminit slab_memory_callback(struct notifier_block *self,
1489 unsigned long action, void *arg)
1490{
1491 struct memory_notify *mnb = arg;
1492 int ret = 0;
1493 int nid;
1494
1495 nid = mnb->status_change_nid;
1496 if (nid < 0)
1497 goto out;
1498
1499 switch (action) {
1500 case MEM_GOING_ONLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001501 mutex_lock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001502 ret = init_cache_nodelists_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001503 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001504 break;
1505 case MEM_GOING_OFFLINE:
Christoph Lameter18004c52012-07-06 15:25:12 -05001506 mutex_lock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001507 ret = drain_cache_nodelists_node(nid);
Christoph Lameter18004c52012-07-06 15:25:12 -05001508 mutex_unlock(&slab_mutex);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001509 break;
1510 case MEM_ONLINE:
1511 case MEM_OFFLINE:
1512 case MEM_CANCEL_ONLINE:
1513 case MEM_CANCEL_OFFLINE:
1514 break;
1515 }
1516out:
Prarit Bhargava5fda1bd2011-03-22 16:30:49 -07001517 return notifier_from_errno(ret);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001518}
1519#endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
1520
Christoph Lametere498be72005-09-09 13:03:32 -07001521/*
1522 * swap the static kmem_list3 with kmalloced memory
1523 */
David Rientjes8f9f8d92010-03-27 19:40:47 -07001524static void __init init_list(struct kmem_cache *cachep, struct kmem_list3 *list,
1525 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07001526{
1527 struct kmem_list3 *ptr;
1528
Pekka Enberg83b519e2009-06-10 19:40:04 +03001529 ptr = kmalloc_node(sizeof(struct kmem_list3), GFP_NOWAIT, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07001530 BUG_ON(!ptr);
1531
Christoph Lametere498be72005-09-09 13:03:32 -07001532 memcpy(ptr, list, sizeof(struct kmem_list3));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001533 /*
1534 * Do not assume that spinlocks can be initialized via memcpy:
1535 */
1536 spin_lock_init(&ptr->list_lock);
1537
Christoph Lametere498be72005-09-09 13:03:32 -07001538 MAKE_ALL_LISTS(cachep, ptr, nodeid);
1539 cachep->nodelists[nodeid] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001540}
1541
Andrew Mortona737b3e2006-03-22 00:08:11 -08001542/*
Pekka Enberg556a1692008-01-25 08:20:51 +02001543 * For setting up all the kmem_list3s for cache whose buffer_size is same as
1544 * size of kmem_list3.
1545 */
1546static void __init set_up_list3s(struct kmem_cache *cachep, int index)
1547{
1548 int node;
1549
1550 for_each_online_node(node) {
1551 cachep->nodelists[node] = &initkmem_list3[index + node];
1552 cachep->nodelists[node]->next_reap = jiffies +
1553 REAPTIMEOUT_LIST3 +
1554 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1555 }
1556}
1557
1558/*
Christoph Lameter3c583462012-11-28 16:23:01 +00001559 * The memory after the last cpu cache pointer is used for the
1560 * the nodelists pointer.
1561 */
1562static void setup_nodelists_pointer(struct kmem_cache *cachep)
1563{
1564 cachep->nodelists = (struct kmem_list3 **)&cachep->array[nr_cpu_ids];
1565}
1566
1567/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08001568 * Initialisation. Called after the page allocator have been initialised and
1569 * before smp_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 */
1571void __init kmem_cache_init(void)
1572{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 struct cache_sizes *sizes;
1574 struct cache_names *names;
Christoph Lametere498be72005-09-09 13:03:32 -07001575 int i;
1576
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001577 kmem_cache = &kmem_cache_boot;
Christoph Lameter3c583462012-11-28 16:23:01 +00001578 setup_nodelists_pointer(kmem_cache);
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001579
Mel Gormanb6e68bc2009-06-16 15:32:16 -07001580 if (num_possible_nodes() == 1)
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001581 use_alien_caches = 0;
1582
Christoph Lameter3c583462012-11-28 16:23:01 +00001583 for (i = 0; i < NUM_INIT_LISTS; i++)
Christoph Lametere498be72005-09-09 13:03:32 -07001584 kmem_list3_init(&initkmem_list3[i]);
Christoph Lameter3c583462012-11-28 16:23:01 +00001585
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001586 set_up_list3s(kmem_cache, CACHE_CACHE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587
1588 /*
1589 * Fragmentation resistance on low memory - only use bigger
David Rientjes3df1ccc2011-10-18 22:09:28 -07001590 * page orders on machines with more than 32MB of memory if
1591 * not overridden on the command line.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 */
David Rientjes3df1ccc2011-10-18 22:09:28 -07001593 if (!slab_max_order_set && totalram_pages > (32 << 20) >> PAGE_SHIFT)
David Rientjes543585c2011-10-18 22:09:24 -07001594 slab_max_order = SLAB_MAX_ORDER_HI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 /* Bootstrap is tricky, because several objects are allocated
1597 * from caches that do not exist yet:
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001598 * 1) initialize the kmem_cache cache: it contains the struct
1599 * kmem_cache structures of all caches, except kmem_cache itself:
1600 * kmem_cache is statically allocated.
Christoph Lametere498be72005-09-09 13:03:32 -07001601 * Initially an __init data area is used for the head array and the
1602 * kmem_list3 structures, it's replaced with a kmalloc allocated
1603 * array at the end of the bootstrap.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 * 2) Create the first kmalloc cache.
Pekka Enberg343e0d72006-02-01 03:05:50 -08001605 * The struct kmem_cache for the new cache is allocated normally.
Christoph Lametere498be72005-09-09 13:03:32 -07001606 * An __init data area is used for the head array.
1607 * 3) Create the remaining kmalloc caches, with minimally sized
1608 * head arrays.
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001609 * 4) Replace the __init data head arrays for kmem_cache and the first
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 * kmalloc cache with kmalloc allocated arrays.
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001611 * 5) Replace the __init data for kmem_list3 for kmem_cache and
Christoph Lametere498be72005-09-09 13:03:32 -07001612 * the other cache's with kmalloc allocated memory.
1613 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 */
1615
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001616 /* 1) create the kmem_cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Eric Dumazet8da34302007-05-06 14:49:29 -07001618 /*
Eric Dumazetb56efcf2011-07-20 19:04:23 +02001619 * struct kmem_cache size depends on nr_node_ids & nr_cpu_ids
Eric Dumazet8da34302007-05-06 14:49:29 -07001620 */
Christoph Lameter2f9baa92012-11-28 16:23:09 +00001621 create_boot_cache(kmem_cache, "kmem_cache",
1622 offsetof(struct kmem_cache, array[nr_cpu_ids]) +
1623 nr_node_ids * sizeof(struct kmem_list3 *),
1624 SLAB_HWCACHE_ALIGN);
1625 list_add(&kmem_cache->list, &slab_caches);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
1627 /* 2+3) create the kmalloc caches */
1628 sizes = malloc_sizes;
1629 names = cache_names;
1630
Andrew Mortona737b3e2006-03-22 00:08:11 -08001631 /*
1632 * Initialize the caches that provide memory for the array cache and the
1633 * kmem_list3 structures first. Without this, further allocations will
1634 * bug.
Christoph Lametere498be72005-09-09 13:03:32 -07001635 */
1636
Christoph Lameter45530c42012-11-28 16:23:07 +00001637 sizes[INDEX_AC].cs_cachep = create_kmalloc_cache(names[INDEX_AC].name,
1638 sizes[INDEX_AC].cs_size, ARCH_KMALLOC_FLAGS);
Christoph Lametere498be72005-09-09 13:03:32 -07001639
Christoph Lameter45530c42012-11-28 16:23:07 +00001640 if (INDEX_AC != INDEX_L3)
1641 sizes[INDEX_L3].cs_cachep =
1642 create_kmalloc_cache(names[INDEX_L3].name,
1643 sizes[INDEX_L3].cs_size, ARCH_KMALLOC_FLAGS);
Christoph Lametere498be72005-09-09 13:03:32 -07001644
Ingo Molnare0a42722006-06-23 02:03:46 -07001645 slab_early_init = 0;
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 while (sizes->cs_size != ULONG_MAX) {
Christoph Lametere498be72005-09-09 13:03:32 -07001648 /*
1649 * For performance, all the general caches are L1 aligned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 * This should be particularly beneficial on SMP boxes, as it
1651 * eliminates "false sharing".
1652 * Note for systems short on memory removing the alignment will
Christoph Lametere498be72005-09-09 13:03:32 -07001653 * allow tighter packing of the smaller caches.
1654 */
Christoph Lameter45530c42012-11-28 16:23:07 +00001655 if (!sizes->cs_cachep)
1656 sizes->cs_cachep = create_kmalloc_cache(names->name,
1657 sizes->cs_size, ARCH_KMALLOC_FLAGS);
1658
Christoph Lameter4b51d662007-02-10 01:43:10 -08001659#ifdef CONFIG_ZONE_DMA
Christoph Lameter45530c42012-11-28 16:23:07 +00001660 sizes->cs_dmacachep = create_kmalloc_cache(
1661 names->name_dma, sizes->cs_size,
1662 SLAB_CACHE_DMA|ARCH_KMALLOC_FLAGS);
Christoph Lameter4b51d662007-02-10 01:43:10 -08001663#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 sizes++;
1665 names++;
1666 }
1667 /* 4) Replace the bootstrap head arrays */
1668 {
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001669 struct array_cache *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001670
Pekka Enberg83b519e2009-06-10 19:40:04 +03001671 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001672
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001673 memcpy(ptr, cpu_cache_get(kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001674 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001675 /*
1676 * Do not assume that spinlocks can be initialized via memcpy:
1677 */
1678 spin_lock_init(&ptr->lock);
1679
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001680 kmem_cache->array[smp_processor_id()] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001681
Pekka Enberg83b519e2009-06-10 19:40:04 +03001682 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001683
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001684 BUG_ON(cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001685 != &initarray_generic.cache);
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001686 memcpy(ptr, cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001687 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001688 /*
1689 * Do not assume that spinlocks can be initialized via memcpy:
1690 */
1691 spin_lock_init(&ptr->lock);
1692
Christoph Lametere498be72005-09-09 13:03:32 -07001693 malloc_sizes[INDEX_AC].cs_cachep->array[smp_processor_id()] =
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001694 ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 }
Christoph Lametere498be72005-09-09 13:03:32 -07001696 /* 5) Replace the bootstrap kmem_list3's */
1697 {
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001698 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
Mel Gorman9c09a952008-01-24 05:49:54 -08001700 for_each_online_node(nid) {
Christoph Lameter9b030cb2012-09-05 00:20:33 +00001701 init_list(kmem_cache, &initkmem_list3[CACHE_CACHE + nid], nid);
Pekka Enberg556a1692008-01-25 08:20:51 +02001702
Christoph Lametere498be72005-09-09 13:03:32 -07001703 init_list(malloc_sizes[INDEX_AC].cs_cachep,
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001704 &initkmem_list3[SIZE_AC + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001705
1706 if (INDEX_AC != INDEX_L3) {
1707 init_list(malloc_sizes[INDEX_L3].cs_cachep,
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001708 &initkmem_list3[SIZE_L3 + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001709 }
1710 }
1711 }
1712
Christoph Lameter97d06602012-07-06 15:25:11 -05001713 slab_state = UP;
Pekka Enberg8429db52009-06-12 15:58:59 +03001714}
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001715
Pekka Enberg8429db52009-06-12 15:58:59 +03001716void __init kmem_cache_init_late(void)
1717{
1718 struct kmem_cache *cachep;
1719
Christoph Lameter97d06602012-07-06 15:25:11 -05001720 slab_state = UP;
Peter Zijlstra52cef182011-11-28 21:12:40 +01001721
Pekka Enberg8429db52009-06-12 15:58:59 +03001722 /* 6) resize the head arrays to their final sizes */
Christoph Lameter18004c52012-07-06 15:25:12 -05001723 mutex_lock(&slab_mutex);
1724 list_for_each_entry(cachep, &slab_caches, list)
Pekka Enberg8429db52009-06-12 15:58:59 +03001725 if (enable_cpucache(cachep, GFP_NOWAIT))
1726 BUG();
Christoph Lameter18004c52012-07-06 15:25:12 -05001727 mutex_unlock(&slab_mutex);
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001728
Michael Wang947ca182012-09-05 10:33:18 +08001729 /* Annotate slab for lockdep -- annotate the malloc caches */
1730 init_lock_keys();
1731
Christoph Lameter97d06602012-07-06 15:25:11 -05001732 /* Done! */
1733 slab_state = FULL;
1734
Andrew Mortona737b3e2006-03-22 00:08:11 -08001735 /*
1736 * Register a cpu startup notifier callback that initializes
1737 * cpu_cache_get for all new cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 */
1739 register_cpu_notifier(&cpucache_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
David Rientjes8f9f8d92010-03-27 19:40:47 -07001741#ifdef CONFIG_NUMA
1742 /*
1743 * Register a memory hotplug callback that initializes and frees
1744 * nodelists.
1745 */
1746 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
1747#endif
1748
Andrew Mortona737b3e2006-03-22 00:08:11 -08001749 /*
1750 * The reap timers are started later, with a module init call: That part
1751 * of the kernel is not yet operational.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 */
1753}
1754
1755static int __init cpucache_init(void)
1756{
1757 int cpu;
1758
Andrew Mortona737b3e2006-03-22 00:08:11 -08001759 /*
1760 * Register the timers that return unneeded pages to the page allocator
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 */
Christoph Lametere498be72005-09-09 13:03:32 -07001762 for_each_online_cpu(cpu)
Andrew Mortona737b3e2006-03-22 00:08:11 -08001763 start_cpu_timer(cpu);
Glauber Costaa164f8962012-06-21 00:59:18 +04001764
1765 /* Done! */
Christoph Lameter97d06602012-07-06 15:25:11 -05001766 slab_state = FULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return 0;
1768}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769__initcall(cpucache_init);
1770
Rafael Aquini8bdec192012-03-09 17:27:27 -03001771static noinline void
1772slab_out_of_memory(struct kmem_cache *cachep, gfp_t gfpflags, int nodeid)
1773{
1774 struct kmem_list3 *l3;
1775 struct slab *slabp;
1776 unsigned long flags;
1777 int node;
1778
1779 printk(KERN_WARNING
1780 "SLAB: Unable to allocate memory on node %d (gfp=0x%x)\n",
1781 nodeid, gfpflags);
1782 printk(KERN_WARNING " cache: %s, object size: %d, order: %d\n",
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05001783 cachep->name, cachep->size, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001784
1785 for_each_online_node(node) {
1786 unsigned long active_objs = 0, num_objs = 0, free_objects = 0;
1787 unsigned long active_slabs = 0, num_slabs = 0;
1788
1789 l3 = cachep->nodelists[node];
1790 if (!l3)
1791 continue;
1792
1793 spin_lock_irqsave(&l3->list_lock, flags);
1794 list_for_each_entry(slabp, &l3->slabs_full, list) {
1795 active_objs += cachep->num;
1796 active_slabs++;
1797 }
1798 list_for_each_entry(slabp, &l3->slabs_partial, list) {
1799 active_objs += slabp->inuse;
1800 active_slabs++;
1801 }
1802 list_for_each_entry(slabp, &l3->slabs_free, list)
1803 num_slabs++;
1804
1805 free_objects += l3->free_objects;
1806 spin_unlock_irqrestore(&l3->list_lock, flags);
1807
1808 num_slabs += active_slabs;
1809 num_objs = num_slabs * cachep->num;
1810 printk(KERN_WARNING
1811 " node %d: slabs: %ld/%ld, objs: %ld/%ld, free: %ld\n",
1812 node, active_slabs, num_slabs, active_objs, num_objs,
1813 free_objects);
1814 }
1815}
1816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817/*
1818 * Interface to system's page allocator. No need to hold the cache-lock.
1819 *
1820 * If we requested dmaable memory, we will get it. Even if we
1821 * did not request dmaable memory, we might get it, but that
1822 * would be relatively rare and ignorable.
1823 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001824static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
1826 struct page *page;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001827 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 int i;
1829
Luke Yangd6fef9d2006-04-10 22:52:56 -07001830#ifndef CONFIG_MMU
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001831 /*
1832 * Nommu uses slab's for process anonymous memory allocations, and thus
1833 * requires __GFP_COMP to properly refcount higher order allocations
Luke Yangd6fef9d2006-04-10 22:52:56 -07001834 */
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001835 flags |= __GFP_COMP;
Luke Yangd6fef9d2006-04-10 22:52:56 -07001836#endif
Christoph Lameter765c4502006-09-27 01:50:08 -07001837
Glauber Costaa618e892012-06-14 16:17:21 +04001838 flags |= cachep->allocflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001839 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1840 flags |= __GFP_RECLAIMABLE;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001841
Linus Torvalds517d0862009-06-16 19:50:13 -07001842 page = alloc_pages_exact_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
Rafael Aquini8bdec192012-03-09 17:27:27 -03001843 if (!page) {
1844 if (!(flags & __GFP_NOWARN) && printk_ratelimit())
1845 slab_out_of_memory(cachep, flags, nodeid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 return NULL;
Rafael Aquini8bdec192012-03-09 17:27:27 -03001847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
Mel Gormanb37f1dd2012-07-31 16:44:03 -07001849 /* Record if ALLOC_NO_WATERMARKS was set when allocating the slab */
Mel Gorman072bb0a2012-07-31 16:43:58 -07001850 if (unlikely(page->pfmemalloc))
1851 pfmemalloc_active = true;
1852
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001853 nr_pages = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
Christoph Lameter972d1a72006-09-25 23:31:51 -07001855 add_zone_page_state(page_zone(page),
1856 NR_SLAB_RECLAIMABLE, nr_pages);
1857 else
1858 add_zone_page_state(page_zone(page),
1859 NR_SLAB_UNRECLAIMABLE, nr_pages);
Mel Gorman072bb0a2012-07-31 16:43:58 -07001860 for (i = 0; i < nr_pages; i++) {
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001861 __SetPageSlab(page + i);
Pekka Enbergc175eea2008-05-09 20:35:53 +02001862
Mel Gorman072bb0a2012-07-31 16:43:58 -07001863 if (page->pfmemalloc)
1864 SetPageSlabPfmemalloc(page + i);
1865 }
1866
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001867 if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
1868 kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
1869
1870 if (cachep->ctor)
1871 kmemcheck_mark_uninitialized_pages(page, nr_pages);
1872 else
1873 kmemcheck_mark_unallocated_pages(page, nr_pages);
1874 }
Pekka Enbergc175eea2008-05-09 20:35:53 +02001875
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001876 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877}
1878
1879/*
1880 * Interface to system's page release.
1881 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001882static void kmem_freepages(struct kmem_cache *cachep, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001884 unsigned long i = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 struct page *page = virt_to_page(addr);
1886 const unsigned long nr_freed = i;
1887
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001888 kmemcheck_free_shadow(page, cachep->gfporder);
Pekka Enbergc175eea2008-05-09 20:35:53 +02001889
Christoph Lameter972d1a72006-09-25 23:31:51 -07001890 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1891 sub_zone_page_state(page_zone(page),
1892 NR_SLAB_RECLAIMABLE, nr_freed);
1893 else
1894 sub_zone_page_state(page_zone(page),
1895 NR_SLAB_UNRECLAIMABLE, nr_freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 while (i--) {
Nick Pigginf205b2f2006-03-22 00:08:02 -08001897 BUG_ON(!PageSlab(page));
Mel Gorman072bb0a2012-07-31 16:43:58 -07001898 __ClearPageSlabPfmemalloc(page);
Nick Pigginf205b2f2006-03-22 00:08:02 -08001899 __ClearPageSlab(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 page++;
1901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 if (current->reclaim_state)
1903 current->reclaim_state->reclaimed_slab += nr_freed;
1904 free_pages((unsigned long)addr, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}
1906
1907static void kmem_rcu_free(struct rcu_head *head)
1908{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001909 struct slab_rcu *slab_rcu = (struct slab_rcu *)head;
Pekka Enberg343e0d72006-02-01 03:05:50 -08001910 struct kmem_cache *cachep = slab_rcu->cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 kmem_freepages(cachep, slab_rcu->addr);
1913 if (OFF_SLAB(cachep))
1914 kmem_cache_free(cachep->slabp_cache, slab_rcu);
1915}
1916
1917#if DEBUG
1918
1919#ifdef CONFIG_DEBUG_PAGEALLOC
Pekka Enberg343e0d72006-02-01 03:05:50 -08001920static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001921 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001923 int size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001925 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001927 if (size < 5 * sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 return;
1929
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001930 *addr++ = 0x12345678;
1931 *addr++ = caller;
1932 *addr++ = smp_processor_id();
1933 size -= 3 * sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 {
1935 unsigned long *sptr = &caller;
1936 unsigned long svalue;
1937
1938 while (!kstack_end(sptr)) {
1939 svalue = *sptr++;
1940 if (kernel_text_address(svalue)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001941 *addr++ = svalue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 size -= sizeof(unsigned long);
1943 if (size <= sizeof(unsigned long))
1944 break;
1945 }
1946 }
1947
1948 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001949 *addr++ = 0x87654321;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950}
1951#endif
1952
Pekka Enberg343e0d72006-02-01 03:05:50 -08001953static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
Christoph Lameter8c138bc2012-06-13 10:24:58 -05001955 int size = cachep->object_size;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001956 addr = &((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958 memset(addr, val, size);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001959 *(unsigned char *)(addr + size - 1) = POISON_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960}
1961
1962static void dump_line(char *data, int offset, int limit)
1963{
1964 int i;
Dave Jonesaa83aa42006-09-29 01:59:51 -07001965 unsigned char error = 0;
1966 int bad_count = 0;
1967
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02001968 printk(KERN_ERR "%03x: ", offset);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001969 for (i = 0; i < limit; i++) {
1970 if (data[offset + i] != POISON_FREE) {
1971 error = data[offset + i];
1972 bad_count++;
1973 }
Dave Jonesaa83aa42006-09-29 01:59:51 -07001974 }
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02001975 print_hex_dump(KERN_CONT, "", 0, 16, 1,
1976 &data[offset], limit, 1);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001977
1978 if (bad_count == 1) {
1979 error ^= POISON_FREE;
1980 if (!(error & (error - 1))) {
1981 printk(KERN_ERR "Single bit error detected. Probably "
1982 "bad RAM.\n");
1983#ifdef CONFIG_X86
1984 printk(KERN_ERR "Run memtest86+ or a similar memory "
1985 "test tool.\n");
1986#else
1987 printk(KERN_ERR "Run a memory test tool.\n");
1988#endif
1989 }
1990 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991}
1992#endif
1993
1994#if DEBUG
1995
Pekka Enberg343e0d72006-02-01 03:05:50 -08001996static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 int i, size;
1999 char *realobj;
2000
2001 if (cachep->flags & SLAB_RED_ZONE) {
David Woodhouseb46b8f12007-05-08 00:22:59 -07002002 printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08002003 *dbg_redzone1(cachep, objp),
2004 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 }
2006
2007 if (cachep->flags & SLAB_STORE_USER) {
2008 printk(KERN_ERR "Last user: [<%p>]",
Andrew Mortona737b3e2006-03-22 00:08:11 -08002009 *dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 print_symbol("(%s)",
Andrew Mortona737b3e2006-03-22 00:08:11 -08002011 (unsigned long)*dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 printk("\n");
2013 }
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002014 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05002015 size = cachep->object_size;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002016 for (i = 0; i < size && lines; i += 16, lines--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 int limit;
2018 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002019 if (i + limit > size)
2020 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 dump_line(realobj, i, limit);
2022 }
2023}
2024
Pekka Enberg343e0d72006-02-01 03:05:50 -08002025static void check_poison_obj(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026{
2027 char *realobj;
2028 int size, i;
2029 int lines = 0;
2030
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002031 realobj = (char *)objp + obj_offset(cachep);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05002032 size = cachep->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002034 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 char exp = POISON_FREE;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002036 if (i == size - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 exp = POISON_END;
2038 if (realobj[i] != exp) {
2039 int limit;
2040 /* Mismatch ! */
2041 /* Print header */
2042 if (lines == 0) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002043 printk(KERN_ERR
Dave Jonesface37f2011-11-15 15:03:52 -08002044 "Slab corruption (%s): %s start=%p, len=%d\n",
2045 print_tainted(), cachep->name, realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 print_objinfo(cachep, objp, 0);
2047 }
2048 /* Hexdump the affected line */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002049 i = (i / 16) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002051 if (i + limit > size)
2052 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 dump_line(realobj, i, limit);
2054 i += 16;
2055 lines++;
2056 /* Limit to 5 lines */
2057 if (lines > 5)
2058 break;
2059 }
2060 }
2061 if (lines != 0) {
2062 /* Print some data about the neighboring objects, if they
2063 * exist:
2064 */
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08002065 struct slab *slabp = virt_to_slab(objp);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002066 unsigned int objnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002068 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 if (objnr) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002070 objp = index_to_obj(cachep, slabp, objnr - 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002071 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002073 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 print_objinfo(cachep, objp, 2);
2075 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002076 if (objnr + 1 < cachep->num) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002077 objp = index_to_obj(cachep, slabp, objnr + 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002078 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002080 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 print_objinfo(cachep, objp, 2);
2082 }
2083 }
2084}
2085#endif
2086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087#if DEBUG
Rabin Vincente79aec22008-07-04 00:40:32 +05302088static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002089{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 int i;
2091 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002092 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094 if (cachep->flags & SLAB_POISON) {
2095#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002096 if (cachep->size % PAGE_SIZE == 0 &&
Andrew Mortona737b3e2006-03-22 00:08:11 -08002097 OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002098 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002099 cachep->size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 else
2101 check_poison_obj(cachep, objp);
2102#else
2103 check_poison_obj(cachep, objp);
2104#endif
2105 }
2106 if (cachep->flags & SLAB_RED_ZONE) {
2107 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2108 slab_error(cachep, "start of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002109 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2111 slab_error(cachep, "end of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002112 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002115}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116#else
Rabin Vincente79aec22008-07-04 00:40:32 +05302117static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002118{
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002119}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120#endif
2121
Randy Dunlap911851e2006-03-22 00:08:14 -08002122/**
2123 * slab_destroy - destroy and release all objects in a slab
2124 * @cachep: cache pointer being destroyed
2125 * @slabp: slab pointer being destroyed
2126 *
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002127 * Destroy all the objs in a slab, and release the mem back to the system.
Andrew Mortona737b3e2006-03-22 00:08:11 -08002128 * Before calling the slab must have been unlinked from the cache. The
2129 * cache-lock is not held/needed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002130 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002131static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08002132{
2133 void *addr = slabp->s_mem - slabp->colouroff;
2134
Rabin Vincente79aec22008-07-04 00:40:32 +05302135 slab_destroy_debugcheck(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
2137 struct slab_rcu *slab_rcu;
2138
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002139 slab_rcu = (struct slab_rcu *)slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 slab_rcu->cachep = cachep;
2141 slab_rcu->addr = addr;
2142 call_rcu(&slab_rcu->head, kmem_rcu_free);
2143 } else {
2144 kmem_freepages(cachep, addr);
Ingo Molnar873623d2006-07-13 14:44:38 +02002145 if (OFF_SLAB(cachep))
2146 kmem_cache_free(cachep->slabp_cache, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
2148}
2149
2150/**
Randy.Dunlapa70773d2006-02-01 03:05:52 -08002151 * calculate_slab_order - calculate size (page order) of slabs
2152 * @cachep: pointer to the cache that is being created
2153 * @size: size of objects to be created in this cache.
2154 * @align: required alignment for the objects.
2155 * @flags: slab allocation flags
2156 *
2157 * Also calculates the number of objects per slab.
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002158 *
2159 * This could be made much more intelligent. For now, try to avoid using
2160 * high order pages for slabs. When the gfp() functions are more friendly
2161 * towards high-order requests, this should be changed.
2162 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002163static size_t calculate_slab_order(struct kmem_cache *cachep,
Randy Dunlapee13d782006-02-01 03:05:53 -08002164 size_t size, size_t align, unsigned long flags)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002165{
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002166 unsigned long offslab_limit;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002167 size_t left_over = 0;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002168 int gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002169
Christoph Lameter0aa817f2007-05-16 22:11:01 -07002170 for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002171 unsigned int num;
2172 size_t remainder;
2173
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002174 cache_estimate(gfporder, size, align, flags, &remainder, &num);
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002175 if (!num)
2176 continue;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002177
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002178 if (flags & CFLGS_OFF_SLAB) {
2179 /*
2180 * Max number of objs-per-slab for caches which
2181 * use off-slab slabs. Needed to avoid a possible
2182 * looping condition in cache_grow().
2183 */
2184 offslab_limit = size - sizeof(struct slab);
2185 offslab_limit /= sizeof(kmem_bufctl_t);
2186
2187 if (num > offslab_limit)
2188 break;
2189 }
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002190
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002191 /* Found something acceptable - save it away */
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002192 cachep->num = num;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002193 cachep->gfporder = gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002194 left_over = remainder;
2195
2196 /*
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08002197 * A VFS-reclaimable slab tends to have most allocations
2198 * as GFP_NOFS and we really don't want to have to be allocating
2199 * higher-order pages when we are unable to shrink dcache.
2200 */
2201 if (flags & SLAB_RECLAIM_ACCOUNT)
2202 break;
2203
2204 /*
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002205 * Large number of objects is good, but very large slabs are
2206 * currently bad for the gfp()s.
2207 */
David Rientjes543585c2011-10-18 22:09:24 -07002208 if (gfporder >= slab_max_order)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002209 break;
2210
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002211 /*
2212 * Acceptable internal fragmentation?
2213 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002214 if (left_over * 8 <= (PAGE_SIZE << gfporder))
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002215 break;
2216 }
2217 return left_over;
2218}
2219
Pekka Enberg83b519e2009-06-10 19:40:04 +03002220static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002221{
Christoph Lameter97d06602012-07-06 15:25:11 -05002222 if (slab_state >= FULL)
Pekka Enberg83b519e2009-06-10 19:40:04 +03002223 return enable_cpucache(cachep, gfp);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002224
Christoph Lameter97d06602012-07-06 15:25:11 -05002225 if (slab_state == DOWN) {
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002226 /*
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002227 * Note: Creation of first cache (kmem_cache).
2228 * The setup_list3s is taken care
2229 * of by the caller of __kmem_cache_create
2230 */
2231 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2232 slab_state = PARTIAL;
2233 } else if (slab_state == PARTIAL) {
2234 /*
2235 * Note: the second kmem_cache_create must create the cache
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002236 * that's used by kmalloc(24), otherwise the creation of
2237 * further caches will BUG().
2238 */
2239 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2240
2241 /*
2242 * If the cache that's used by kmalloc(sizeof(kmem_list3)) is
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002243 * the second cache, then we need to set up all its list3s,
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002244 * otherwise the creation of further caches will BUG().
2245 */
2246 set_up_list3s(cachep, SIZE_AC);
2247 if (INDEX_AC == INDEX_L3)
Christoph Lameter97d06602012-07-06 15:25:11 -05002248 slab_state = PARTIAL_L3;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002249 else
Christoph Lameter97d06602012-07-06 15:25:11 -05002250 slab_state = PARTIAL_ARRAYCACHE;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002251 } else {
Christoph Lameter2f9baa92012-11-28 16:23:09 +00002252 /* Remaining boot caches */
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002253 cachep->array[smp_processor_id()] =
Pekka Enberg83b519e2009-06-10 19:40:04 +03002254 kmalloc(sizeof(struct arraycache_init), gfp);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002255
Christoph Lameter97d06602012-07-06 15:25:11 -05002256 if (slab_state == PARTIAL_ARRAYCACHE) {
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002257 set_up_list3s(cachep, SIZE_L3);
Christoph Lameter97d06602012-07-06 15:25:11 -05002258 slab_state = PARTIAL_L3;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002259 } else {
2260 int node;
Pekka Enberg556a1692008-01-25 08:20:51 +02002261 for_each_online_node(node) {
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002262 cachep->nodelists[node] =
2263 kmalloc_node(sizeof(struct kmem_list3),
Pekka Enbergeb91f1d2009-06-12 14:56:09 +03002264 gfp, node);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002265 BUG_ON(!cachep->nodelists[node]);
2266 kmem_list3_init(cachep->nodelists[node]);
2267 }
2268 }
2269 }
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07002270 cachep->nodelists[numa_mem_id()]->next_reap =
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002271 jiffies + REAPTIMEOUT_LIST3 +
2272 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
2273
2274 cpu_cache_get(cachep)->avail = 0;
2275 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
2276 cpu_cache_get(cachep)->batchcount = 1;
2277 cpu_cache_get(cachep)->touched = 0;
2278 cachep->batchcount = 1;
2279 cachep->limit = BOOT_CPUCACHE_ENTRIES;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002280 return 0;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002281}
2282
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002283/**
Christoph Lameter039363f2012-07-06 15:25:10 -05002284 * __kmem_cache_create - Create a cache.
Randy Dunlapa755b762012-11-06 17:10:10 -08002285 * @cachep: cache management descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 * @flags: SLAB flags
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 *
2288 * Returns a ptr to the cache on success, NULL on failure.
2289 * Cannot be called within a int, but can be interrupted.
Paul Mundt20c2df82007-07-20 10:11:58 +09002290 * The @ctor is run when new pages are allocated by the cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 * The flags are
2293 *
2294 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2295 * to catch references to uninitialised memory.
2296 *
2297 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2298 * for buffer overruns.
2299 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2301 * cacheline. This can be beneficial if you're counting cycles as closely
2302 * as davem.
2303 */
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002304int
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002305__kmem_cache_create (struct kmem_cache *cachep, unsigned long flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306{
2307 size_t left_over, slab_size, ralign;
Pekka Enberg83b519e2009-06-10 19:40:04 +03002308 gfp_t gfp;
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002309 int err;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002310 size_t size = cachep->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313#if FORCED_DEBUG
2314 /*
2315 * Enable redzoning and last user accounting, except for caches with
2316 * large objects, if the increased size would increase the object size
2317 * above the next power of two: caches with object sizes just above a
2318 * power of two have a significant amount of internal fragmentation.
2319 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002320 if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
2321 2 * sizeof(unsigned long long)))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002322 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 if (!(flags & SLAB_DESTROY_BY_RCU))
2324 flags |= SLAB_POISON;
2325#endif
2326 if (flags & SLAB_DESTROY_BY_RCU)
2327 BUG_ON(flags & SLAB_POISON);
2328#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329
Andrew Mortona737b3e2006-03-22 00:08:11 -08002330 /*
2331 * Check that size is in terms of words. This is needed to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332 * unaligned accesses for some archs when redzoning is used, and makes
2333 * sure any on-slab bufctl's are also correctly aligned.
2334 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002335 if (size & (BYTES_PER_WORD - 1)) {
2336 size += (BYTES_PER_WORD - 1);
2337 size &= ~(BYTES_PER_WORD - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 }
2339
Andrew Mortona737b3e2006-03-22 00:08:11 -08002340 /* calculate the final buffer alignment: */
2341
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 /* 1) arch recommendation: can be overridden for debug */
2343 if (flags & SLAB_HWCACHE_ALIGN) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002344 /*
2345 * Default alignment: as specified by the arch code. Except if
2346 * an object is really small, then squeeze multiple objects into
2347 * one cacheline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 */
2349 ralign = cache_line_size();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002350 while (size <= ralign / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 ralign /= 2;
2352 } else {
2353 ralign = BYTES_PER_WORD;
2354 }
Pekka Enbergca5f9702006-09-25 23:31:25 -07002355
2356 /*
David Woodhouse87a927c2007-07-04 21:26:44 -04002357 * Redzoning and user store require word alignment or possibly larger.
2358 * Note this will be overridden by architecture or caller mandated
2359 * alignment if either is greater than BYTES_PER_WORD.
Pekka Enbergca5f9702006-09-25 23:31:25 -07002360 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002361 if (flags & SLAB_STORE_USER)
2362 ralign = BYTES_PER_WORD;
2363
2364 if (flags & SLAB_RED_ZONE) {
2365 ralign = REDZONE_ALIGN;
2366 /* If redzoning, ensure that the second redzone is suitably
2367 * aligned, by adjusting the object size accordingly. */
2368 size += REDZONE_ALIGN - 1;
2369 size &= ~(REDZONE_ALIGN - 1);
2370 }
Pekka Enbergca5f9702006-09-25 23:31:25 -07002371
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002372 /* 2) arch mandated alignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if (ralign < ARCH_SLAB_MINALIGN) {
2374 ralign = ARCH_SLAB_MINALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 }
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002376 /* 3) caller mandated alignment */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002377 if (ralign < cachep->align) {
2378 ralign = cachep->align;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 }
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002380 /* disable debug if necessary */
2381 if (ralign > __alignof__(unsigned long long))
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002382 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002383 /*
Pekka Enbergca5f9702006-09-25 23:31:25 -07002384 * 4) Store it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002386 cachep->align = ralign;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Pekka Enberg83b519e2009-06-10 19:40:04 +03002388 if (slab_is_available())
2389 gfp = GFP_KERNEL;
2390 else
2391 gfp = GFP_NOWAIT;
2392
Christoph Lameter3c583462012-11-28 16:23:01 +00002393 setup_nodelists_pointer(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Pekka Enbergca5f9702006-09-25 23:31:25 -07002396 /*
2397 * Both debugging options require word-alignment which is calculated
2398 * into align above.
2399 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 if (flags & SLAB_RED_ZONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 /* add space for red zone words */
Pekka Enberg3ff84a72011-02-14 17:46:21 +02002402 cachep->obj_offset += sizeof(unsigned long long);
2403 size += 2 * sizeof(unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 }
2405 if (flags & SLAB_STORE_USER) {
Pekka Enbergca5f9702006-09-25 23:31:25 -07002406 /* user store requires one word storage behind the end of
David Woodhouse87a927c2007-07-04 21:26:44 -04002407 * the real object. But if the second red zone needs to be
2408 * aligned to 64 bits, we must allow that much space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002410 if (flags & SLAB_RED_ZONE)
2411 size += REDZONE_ALIGN;
2412 else
2413 size += BYTES_PER_WORD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 }
2415#if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002416 if (size >= malloc_sizes[INDEX_L3 + 1].cs_size
Tetsuo Handa608da7e2012-09-30 17:28:25 +09002417 && cachep->object_size > cache_line_size()
2418 && ALIGN(size, cachep->align) < PAGE_SIZE) {
2419 cachep->obj_offset += PAGE_SIZE - ALIGN(size, cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 size = PAGE_SIZE;
2421 }
2422#endif
2423#endif
2424
Ingo Molnare0a42722006-06-23 02:03:46 -07002425 /*
2426 * Determine if the slab management is 'on' or 'off' slab.
2427 * (bootstrapping cannot cope with offslab caches so don't do
Catalin Marinase7cb55b2009-10-28 13:33:08 +00002428 * it too early on. Always use on-slab management when
2429 * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
Ingo Molnare0a42722006-06-23 02:03:46 -07002430 */
Catalin Marinase7cb55b2009-10-28 13:33:08 +00002431 if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init &&
2432 !(flags & SLAB_NOLEAKTRACE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433 /*
2434 * Size is large, assume best to place the slab management obj
2435 * off-slab (should allow better packing of objs).
2436 */
2437 flags |= CFLGS_OFF_SLAB;
2438
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002439 size = ALIGN(size, cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002441 left_over = calculate_slab_order(cachep, size, cachep->align, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002443 if (!cachep->num)
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002444 return -E2BIG;
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002445
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002446 slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002447 + sizeof(struct slab), cachep->align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
2449 /*
2450 * If the slab has been placed off-slab, and we have enough space then
2451 * move it on-slab. This is at the expense of any extra colouring.
2452 */
2453 if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
2454 flags &= ~CFLGS_OFF_SLAB;
2455 left_over -= slab_size;
2456 }
2457
2458 if (flags & CFLGS_OFF_SLAB) {
2459 /* really off slab. No need for manual alignment */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002460 slab_size =
2461 cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
Ron Lee67461362009-05-22 04:58:22 +09302462
2463#ifdef CONFIG_PAGE_POISONING
2464 /* If we're going to use the generic kernel_map_pages()
2465 * poisoning, then it's going to smash the contents of
2466 * the redzone and userword anyhow, so switch them off.
2467 */
2468 if (size % PAGE_SIZE == 0 && flags & SLAB_POISON)
2469 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
2470#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 }
2472
2473 cachep->colour_off = cache_line_size();
2474 /* Offset must be a multiple of the alignment. */
Christoph Lameter8a13a4c2012-09-04 23:18:33 +00002475 if (cachep->colour_off < cachep->align)
2476 cachep->colour_off = cachep->align;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002477 cachep->colour = left_over / cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 cachep->slab_size = slab_size;
2479 cachep->flags = flags;
Glauber Costaa618e892012-06-14 16:17:21 +04002480 cachep->allocflags = 0;
Christoph Lameter4b51d662007-02-10 01:43:10 -08002481 if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
Glauber Costaa618e892012-06-14 16:17:21 +04002482 cachep->allocflags |= GFP_DMA;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002483 cachep->size = size;
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08002484 cachep->reciprocal_buffer_size = reciprocal_value(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002486 if (flags & CFLGS_OFF_SLAB) {
Victor Fuscob2d55072005-09-10 00:26:36 -07002487 cachep->slabp_cache = kmem_find_general_cachep(slab_size, 0u);
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002488 /*
2489 * This is a possibility for one of the malloc_sizes caches.
2490 * But since we go off slab only for object size greater than
2491 * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
2492 * this should not happen at all.
2493 * But leave a BUG_ON for some lucky dude.
2494 */
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002495 BUG_ON(ZERO_OR_NULL_PTR(cachep->slabp_cache));
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002498 err = setup_cpu_cache(cachep, gfp);
2499 if (err) {
Christoph Lameter12c36672012-09-04 23:38:33 +00002500 __kmem_cache_shutdown(cachep);
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002501 return err;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503
Peter Zijlstra83835b32011-07-22 15:26:05 +02002504 if (flags & SLAB_DEBUG_OBJECTS) {
2505 /*
2506 * Would deadlock through slab_destroy()->call_rcu()->
2507 * debug_object_activate()->kmem_cache_alloc().
2508 */
2509 WARN_ON_ONCE(flags & SLAB_DESTROY_BY_RCU);
2510
2511 slab_set_debugobj_lock_classes(cachep);
2512 }
2513
Christoph Lameter278b1bb2012-09-05 00:20:34 +00002514 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516
2517#if DEBUG
2518static void check_irq_off(void)
2519{
2520 BUG_ON(!irqs_disabled());
2521}
2522
2523static void check_irq_on(void)
2524{
2525 BUG_ON(irqs_disabled());
2526}
2527
Pekka Enberg343e0d72006-02-01 03:05:50 -08002528static void check_spinlock_acquired(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529{
2530#ifdef CONFIG_SMP
2531 check_irq_off();
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07002532 assert_spin_locked(&cachep->nodelists[numa_mem_id()]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533#endif
2534}
Christoph Lametere498be72005-09-09 13:03:32 -07002535
Pekka Enberg343e0d72006-02-01 03:05:50 -08002536static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07002537{
2538#ifdef CONFIG_SMP
2539 check_irq_off();
2540 assert_spin_locked(&cachep->nodelists[node]->list_lock);
2541#endif
2542}
2543
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544#else
2545#define check_irq_off() do { } while(0)
2546#define check_irq_on() do { } while(0)
2547#define check_spinlock_acquired(x) do { } while(0)
Christoph Lametere498be72005-09-09 13:03:32 -07002548#define check_spinlock_acquired_node(x, y) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549#endif
2550
Christoph Lameteraab22072006-03-22 00:09:06 -08002551static void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
2552 struct array_cache *ac,
2553 int force, int node);
2554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555static void do_drain(void *arg)
2556{
Andrew Mortona737b3e2006-03-22 00:08:11 -08002557 struct kmem_cache *cachep = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 struct array_cache *ac;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07002559 int node = numa_mem_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560
2561 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002562 ac = cpu_cache_get(cachep);
Christoph Lameterff694162005-09-22 21:44:02 -07002563 spin_lock(&cachep->nodelists[node]->list_lock);
2564 free_block(cachep, ac->entry, ac->avail, node);
2565 spin_unlock(&cachep->nodelists[node]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 ac->avail = 0;
2567}
2568
Pekka Enberg343e0d72006-02-01 03:05:50 -08002569static void drain_cpu_caches(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570{
Christoph Lametere498be72005-09-09 13:03:32 -07002571 struct kmem_list3 *l3;
2572 int node;
2573
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002574 on_each_cpu(do_drain, cachep, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 check_irq_on();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002576 for_each_online_node(node) {
Christoph Lametere498be72005-09-09 13:03:32 -07002577 l3 = cachep->nodelists[node];
Roland Dreiera4523a82006-05-15 11:41:00 -07002578 if (l3 && l3->alien)
2579 drain_alien_cache(cachep, l3->alien);
2580 }
2581
2582 for_each_online_node(node) {
2583 l3 = cachep->nodelists[node];
2584 if (l3)
Christoph Lameteraab22072006-03-22 00:09:06 -08002585 drain_array(cachep, l3, l3->shared, 1, node);
Christoph Lametere498be72005-09-09 13:03:32 -07002586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587}
2588
Christoph Lametered11d9e2006-06-30 01:55:45 -07002589/*
2590 * Remove slabs from the list of free slabs.
2591 * Specify the number of slabs to drain in tofree.
2592 *
2593 * Returns the actual number of slabs released.
2594 */
2595static int drain_freelist(struct kmem_cache *cache,
2596 struct kmem_list3 *l3, int tofree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597{
Christoph Lametered11d9e2006-06-30 01:55:45 -07002598 struct list_head *p;
2599 int nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601
Christoph Lametered11d9e2006-06-30 01:55:45 -07002602 nr_freed = 0;
2603 while (nr_freed < tofree && !list_empty(&l3->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Christoph Lametered11d9e2006-06-30 01:55:45 -07002605 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002606 p = l3->slabs_free.prev;
Christoph Lametered11d9e2006-06-30 01:55:45 -07002607 if (p == &l3->slabs_free) {
2608 spin_unlock_irq(&l3->list_lock);
2609 goto out;
2610 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611
Christoph Lametered11d9e2006-06-30 01:55:45 -07002612 slabp = list_entry(p, struct slab, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613#if DEBUG
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002614 BUG_ON(slabp->inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615#endif
2616 list_del(&slabp->list);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002617 /*
2618 * Safe to drop the lock. The slab is no longer linked
2619 * to the cache.
2620 */
2621 l3->free_objects -= cache->num;
Christoph Lametere498be72005-09-09 13:03:32 -07002622 spin_unlock_irq(&l3->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002623 slab_destroy(cache, slabp);
2624 nr_freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 }
Christoph Lametered11d9e2006-06-30 01:55:45 -07002626out:
2627 return nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628}
2629
Christoph Lameter18004c52012-07-06 15:25:12 -05002630/* Called with slab_mutex held to protect against cpu hotplug */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002631static int __cache_shrink(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07002632{
2633 int ret = 0, i = 0;
2634 struct kmem_list3 *l3;
2635
2636 drain_cpu_caches(cachep);
2637
2638 check_irq_on();
2639 for_each_online_node(i) {
2640 l3 = cachep->nodelists[i];
Christoph Lametered11d9e2006-06-30 01:55:45 -07002641 if (!l3)
2642 continue;
2643
2644 drain_freelist(cachep, l3, l3->free_objects);
2645
2646 ret += !list_empty(&l3->slabs_full) ||
2647 !list_empty(&l3->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07002648 }
2649 return (ret ? 1 : 0);
2650}
2651
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652/**
2653 * kmem_cache_shrink - Shrink a cache.
2654 * @cachep: The cache to shrink.
2655 *
2656 * Releases as many slabs as possible for a cache.
2657 * To help debugging, a zero exit status indicates all slabs were released.
2658 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002659int kmem_cache_shrink(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660{
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002661 int ret;
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002662 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002664 get_online_cpus();
Christoph Lameter18004c52012-07-06 15:25:12 -05002665 mutex_lock(&slab_mutex);
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002666 ret = __cache_shrink(cachep);
Christoph Lameter18004c52012-07-06 15:25:12 -05002667 mutex_unlock(&slab_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002668 put_online_cpus();
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002669 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670}
2671EXPORT_SYMBOL(kmem_cache_shrink);
2672
Christoph Lameter945cf2b2012-09-04 23:18:33 +00002673int __kmem_cache_shutdown(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674{
Christoph Lameter12c36672012-09-04 23:38:33 +00002675 int i;
2676 struct kmem_list3 *l3;
2677 int rc = __cache_shrink(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678
Christoph Lameter12c36672012-09-04 23:38:33 +00002679 if (rc)
2680 return rc;
2681
2682 for_each_online_cpu(i)
2683 kfree(cachep->array[i]);
2684
2685 /* NUMA: free the list3 structures */
2686 for_each_online_node(i) {
2687 l3 = cachep->nodelists[i];
2688 if (l3) {
2689 kfree(l3->shared);
2690 free_alien_cache(l3->alien);
2691 kfree(l3);
2692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 }
Christoph Lameter12c36672012-09-04 23:38:33 +00002694 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002697/*
2698 * Get the memory for a slab management obj.
2699 * For a slab cache when the slab descriptor is off-slab, slab descriptors
2700 * always come from malloc_sizes caches. The slab descriptor cannot
2701 * come from the same cache which is getting created because,
2702 * when we are searching for an appropriate cache for these
2703 * descriptors in kmem_cache_create, we search through the malloc_sizes array.
2704 * If we are creating a malloc_sizes cache here it would not be visible to
2705 * kmem_find_general_cachep till the initialization is complete.
2706 * Hence we cannot have slabp_cache same as the original cache.
2707 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002708static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002709 int colour_off, gfp_t local_flags,
2710 int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
2712 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002713
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 if (OFF_SLAB(cachep)) {
2715 /* Slab management obj is off-slab. */
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002716 slabp = kmem_cache_alloc_node(cachep->slabp_cache,
Pekka Enberg8759ec52008-11-26 10:01:31 +02002717 local_flags, nodeid);
Catalin Marinasd5cff632009-06-11 13:22:40 +01002718 /*
2719 * If the first object in the slab is leaked (it's allocated
2720 * but no one has a reference to it), we want to make sure
2721 * kmemleak does not treat the ->s_mem pointer as a reference
2722 * to the object. Otherwise we will not report the leak.
2723 */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002724 kmemleak_scan_area(&slabp->list, sizeof(struct list_head),
2725 local_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 if (!slabp)
2727 return NULL;
2728 } else {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002729 slabp = objp + colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 colour_off += cachep->slab_size;
2731 }
2732 slabp->inuse = 0;
2733 slabp->colouroff = colour_off;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002734 slabp->s_mem = objp + colour_off;
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002735 slabp->nodeid = nodeid;
Marcin Slusarze51bfd02008-02-10 11:21:54 +01002736 slabp->free = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 return slabp;
2738}
2739
2740static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
2741{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002742 return (kmem_bufctl_t *) (slabp + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743}
2744
Pekka Enberg343e0d72006-02-01 03:05:50 -08002745static void cache_init_objs(struct kmem_cache *cachep,
Christoph Lametera35afb82007-05-16 22:10:57 -07002746 struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747{
2748 int i;
2749
2750 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002751 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752#if DEBUG
2753 /* need to poison the objs? */
2754 if (cachep->flags & SLAB_POISON)
2755 poison_obj(cachep, objp, POISON_FREE);
2756 if (cachep->flags & SLAB_STORE_USER)
2757 *dbg_userword(cachep, objp) = NULL;
2758
2759 if (cachep->flags & SLAB_RED_ZONE) {
2760 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2761 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2762 }
2763 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002764 * Constructors are not allowed to allocate memory from the same
2765 * cache which they are a constructor for. Otherwise, deadlock.
2766 * They must also be threaded.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 */
2768 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002769 cachep->ctor(objp + obj_offset(cachep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
2771 if (cachep->flags & SLAB_RED_ZONE) {
2772 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2773 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002774 " end of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2776 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002777 " start of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 }
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002779 if ((cachep->size % PAGE_SIZE) == 0 &&
Andrew Mortona737b3e2006-03-22 00:08:11 -08002780 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002781 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05002782 cachep->size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783#else
2784 if (cachep->ctor)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002785 cachep->ctor(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786#endif
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002787 slab_bufctl(slabp)[i] = i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002789 slab_bufctl(slabp)[i - 1] = BUFCTL_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790}
2791
Pekka Enberg343e0d72006-02-01 03:05:50 -08002792static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793{
Christoph Lameter4b51d662007-02-10 01:43:10 -08002794 if (CONFIG_ZONE_DMA_FLAG) {
2795 if (flags & GFP_DMA)
Glauber Costaa618e892012-06-14 16:17:21 +04002796 BUG_ON(!(cachep->allocflags & GFP_DMA));
Christoph Lameter4b51d662007-02-10 01:43:10 -08002797 else
Glauber Costaa618e892012-06-14 16:17:21 +04002798 BUG_ON(cachep->allocflags & GFP_DMA);
Christoph Lameter4b51d662007-02-10 01:43:10 -08002799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800}
2801
Andrew Mortona737b3e2006-03-22 00:08:11 -08002802static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
2803 int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002804{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002805 void *objp = index_to_obj(cachep, slabp, slabp->free);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002806 kmem_bufctl_t next;
2807
2808 slabp->inuse++;
2809 next = slab_bufctl(slabp)[slabp->free];
2810#if DEBUG
2811 slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
2812 WARN_ON(slabp->nodeid != nodeid);
2813#endif
2814 slabp->free = next;
2815
2816 return objp;
2817}
2818
Andrew Mortona737b3e2006-03-22 00:08:11 -08002819static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
2820 void *objp, int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002821{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002822 unsigned int objnr = obj_to_index(cachep, slabp, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002823
2824#if DEBUG
2825 /* Verify that the slab belongs to the intended node */
2826 WARN_ON(slabp->nodeid != nodeid);
2827
Al Viro871751e2006-03-25 03:06:39 -08002828 if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) {
Matthew Dobson78d382d2006-02-01 03:05:47 -08002829 printk(KERN_ERR "slab: double free detected in cache "
Andrew Mortona737b3e2006-03-22 00:08:11 -08002830 "'%s', objp %p\n", cachep->name, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002831 BUG();
2832 }
2833#endif
2834 slab_bufctl(slabp)[objnr] = slabp->free;
2835 slabp->free = objnr;
2836 slabp->inuse--;
2837}
2838
Pekka Enberg47768742006-06-23 02:03:07 -07002839/*
2840 * Map pages beginning at addr to the given cache and slab. This is required
2841 * for the slab allocator to be able to lookup the cache and slab of a
Nick Pigginccd35fb2011-01-07 17:49:17 +11002842 * virtual address for kfree, ksize, and slab debugging.
Pekka Enberg47768742006-06-23 02:03:07 -07002843 */
2844static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
2845 void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846{
Pekka Enberg47768742006-06-23 02:03:07 -07002847 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 struct page *page;
2849
Pekka Enberg47768742006-06-23 02:03:07 -07002850 page = virt_to_page(addr);
Nick Piggin84097512006-03-22 00:08:34 -08002851
Pekka Enberg47768742006-06-23 02:03:07 -07002852 nr_pages = 1;
Nick Piggin84097512006-03-22 00:08:34 -08002853 if (likely(!PageCompound(page)))
Pekka Enberg47768742006-06-23 02:03:07 -07002854 nr_pages <<= cache->gfporder;
2855
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 do {
Christoph Lameter35026082012-06-13 10:24:56 -05002857 page->slab_cache = cache;
2858 page->slab_page = slab;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 page++;
Pekka Enberg47768742006-06-23 02:03:07 -07002860 } while (--nr_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861}
2862
2863/*
2864 * Grow (by 1) the number of slabs within a cache. This is called by
2865 * kmem_cache_alloc() when there are no active objs left in a cache.
2866 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002867static int cache_grow(struct kmem_cache *cachep,
2868 gfp_t flags, int nodeid, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002870 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002871 size_t offset;
2872 gfp_t local_flags;
Christoph Lametere498be72005-09-09 13:03:32 -07002873 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Andrew Mortona737b3e2006-03-22 00:08:11 -08002875 /*
2876 * Be lazy and only check for valid flags here, keeping it out of the
2877 * critical path in kmem_cache_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 */
Christoph Lameter6cb06222007-10-16 01:25:41 -07002879 BUG_ON(flags & GFP_SLAB_BUG_MASK);
2880 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002882 /* Take the l3 list lock to change the colour_next on this node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 check_irq_off();
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002884 l3 = cachep->nodelists[nodeid];
2885 spin_lock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
2887 /* Get colour for the slab, and cal the next value. */
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002888 offset = l3->colour_next;
2889 l3->colour_next++;
2890 if (l3->colour_next >= cachep->colour)
2891 l3->colour_next = 0;
2892 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002894 offset *= cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895
2896 if (local_flags & __GFP_WAIT)
2897 local_irq_enable();
2898
2899 /*
2900 * The test for missing atomic flag is performed here, rather than
2901 * the more obvious place, simply to reduce the critical path length
2902 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2903 * will eventually be caught here (where it matters).
2904 */
2905 kmem_flagcheck(cachep, flags);
2906
Andrew Mortona737b3e2006-03-22 00:08:11 -08002907 /*
2908 * Get mem for the objs. Attempt to allocate a physical page from
2909 * 'nodeid'.
Christoph Lametere498be72005-09-09 13:03:32 -07002910 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002911 if (!objp)
Andrew Mortonb8c1c5d2007-07-24 12:02:40 -07002912 objp = kmem_getpages(cachep, local_flags, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002913 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 goto failed;
2915
2916 /* Get slab management. */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002917 slabp = alloc_slabmgmt(cachep, objp, offset,
Christoph Lameter6cb06222007-10-16 01:25:41 -07002918 local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002919 if (!slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 goto opps1;
2921
Pekka Enberg47768742006-06-23 02:03:07 -07002922 slab_map_pages(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
Christoph Lametera35afb82007-05-16 22:10:57 -07002924 cache_init_objs(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925
2926 if (local_flags & __GFP_WAIT)
2927 local_irq_disable();
2928 check_irq_off();
Christoph Lametere498be72005-09-09 13:03:32 -07002929 spin_lock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
2931 /* Make slab active. */
Christoph Lametere498be72005-09-09 13:03:32 -07002932 list_add_tail(&slabp->list, &(l3->slabs_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 STATS_INC_GROWN(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07002934 l3->free_objects += cachep->num;
2935 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002937opps1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 kmem_freepages(cachep, objp);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002939failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 if (local_flags & __GFP_WAIT)
2941 local_irq_disable();
2942 return 0;
2943}
2944
2945#if DEBUG
2946
2947/*
2948 * Perform extra freeing checks:
2949 * - detect bad pointers.
2950 * - POISON/RED_ZONE checking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 */
2952static void kfree_debugcheck(const void *objp)
2953{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 if (!virt_addr_valid(objp)) {
2955 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002956 (unsigned long)objp);
2957 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959}
2960
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002961static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2962{
David Woodhouseb46b8f12007-05-08 00:22:59 -07002963 unsigned long long redzone1, redzone2;
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002964
2965 redzone1 = *dbg_redzone1(cache, obj);
2966 redzone2 = *dbg_redzone2(cache, obj);
2967
2968 /*
2969 * Redzone is ok.
2970 */
2971 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2972 return;
2973
2974 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2975 slab_error(cache, "double free detected");
2976 else
2977 slab_error(cache, "memory outside object was overwritten");
2978
David Woodhouseb46b8f12007-05-08 00:22:59 -07002979 printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002980 obj, redzone1, redzone2);
2981}
2982
Pekka Enberg343e0d72006-02-01 03:05:50 -08002983static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03002984 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
2986 struct page *page;
2987 unsigned int objnr;
2988 struct slab *slabp;
2989
Matthew Wilcox80cbd912007-11-29 12:05:13 -07002990 BUG_ON(virt_to_cache(objp) != cachep);
2991
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002992 objp -= obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 kfree_debugcheck(objp);
Christoph Lameterb49af682007-05-06 14:49:41 -07002994 page = virt_to_head_page(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995
Christoph Lameter35026082012-06-13 10:24:56 -05002996 slabp = page->slab_page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997
2998 if (cachep->flags & SLAB_RED_ZONE) {
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002999 verify_redzone_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
3001 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
3002 }
3003 if (cachep->flags & SLAB_STORE_USER)
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003004 *dbg_userword(cachep, objp) = (void *)caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005
Pekka Enberg8fea4e92006-03-22 00:08:10 -08003006 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
3008 BUG_ON(objnr >= cachep->num);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08003009 BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010
Al Viro871751e2006-03-25 03:06:39 -08003011#ifdef CONFIG_DEBUG_SLAB_LEAK
3012 slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
3013#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 if (cachep->flags & SLAB_POISON) {
3015#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003016 if ((cachep->size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003017 store_stackinfo(cachep, objp, caller);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003018 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003019 cachep->size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 } else {
3021 poison_obj(cachep, objp, POISON_FREE);
3022 }
3023#else
3024 poison_obj(cachep, objp, POISON_FREE);
3025#endif
3026 }
3027 return objp;
3028}
3029
Pekka Enberg343e0d72006-02-01 03:05:50 -08003030static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
3032 kmem_bufctl_t i;
3033 int entries = 0;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003034
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 /* Check slab's freelist to see if this obj is there. */
3036 for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
3037 entries++;
3038 if (entries > cachep->num || i >= cachep->num)
3039 goto bad;
3040 }
3041 if (entries != cachep->num - slabp->inuse) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003042bad:
3043 printk(KERN_ERR "slab: Internal list corruption detected in "
Dave Jonesface37f2011-11-15 15:03:52 -08003044 "cache '%s'(%d), slabp %p(%d). Tainted(%s). Hexdump:\n",
3045 cachep->name, cachep->num, slabp, slabp->inuse,
3046 print_tainted());
Sebastian Andrzej Siewiorfdde6ab2011-07-29 18:22:13 +02003047 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 16, 1, slabp,
3048 sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t),
3049 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 BUG();
3051 }
3052}
3053#else
3054#define kfree_debugcheck(x) do { } while(0)
3055#define cache_free_debugcheck(x,objp,z) (objp)
3056#define check_slabp(x,y) do { } while(0)
3057#endif
3058
Mel Gorman072bb0a2012-07-31 16:43:58 -07003059static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags,
3060 bool force_refill)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
3062 int batchcount;
3063 struct kmem_list3 *l3;
3064 struct array_cache *ac;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07003065 int node;
3066
Joe Korty6d2144d2008-03-05 15:04:59 -08003067 check_irq_off();
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003068 node = numa_mem_id();
Mel Gorman072bb0a2012-07-31 16:43:58 -07003069 if (unlikely(force_refill))
3070 goto force_grow;
3071retry:
Joe Korty6d2144d2008-03-05 15:04:59 -08003072 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 batchcount = ac->batchcount;
3074 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003075 /*
3076 * If there was little recent activity on this cache, then
3077 * perform only a partial refill. Otherwise we could generate
3078 * refill bouncing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 */
3080 batchcount = BATCHREFILL_LIMIT;
3081 }
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07003082 l3 = cachep->nodelists[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083
Christoph Lametere498be72005-09-09 13:03:32 -07003084 BUG_ON(ac->avail > 0 || !l3);
3085 spin_lock(&l3->list_lock);
3086
Christoph Lameter3ded1752006-03-25 03:06:44 -08003087 /* See if we can refill from the shared array */
Nick Piggin44b57f12010-01-27 22:27:40 +11003088 if (l3->shared && transfer_objects(ac, l3->shared, batchcount)) {
3089 l3->shared->touched = 1;
Christoph Lameter3ded1752006-03-25 03:06:44 -08003090 goto alloc_done;
Nick Piggin44b57f12010-01-27 22:27:40 +11003091 }
Christoph Lameter3ded1752006-03-25 03:06:44 -08003092
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 while (batchcount > 0) {
3094 struct list_head *entry;
3095 struct slab *slabp;
3096 /* Get slab alloc is to come from. */
3097 entry = l3->slabs_partial.next;
3098 if (entry == &l3->slabs_partial) {
3099 l3->free_touched = 1;
3100 entry = l3->slabs_free.next;
3101 if (entry == &l3->slabs_free)
3102 goto must_grow;
3103 }
3104
3105 slabp = list_entry(entry, struct slab, list);
3106 check_slabp(cachep, slabp);
3107 check_spinlock_acquired(cachep);
Pekka Enberg714b81712007-05-06 14:49:03 -07003108
3109 /*
3110 * The slab was either on partial or free list so
3111 * there must be at least one object available for
3112 * allocation.
3113 */
roel kluin249b9f32008-10-29 17:18:07 -04003114 BUG_ON(slabp->inuse >= cachep->num);
Pekka Enberg714b81712007-05-06 14:49:03 -07003115
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 while (slabp->inuse < cachep->num && batchcount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 STATS_INC_ALLOCED(cachep);
3118 STATS_INC_ACTIVE(cachep);
3119 STATS_SET_HIGH(cachep);
3120
Mel Gorman072bb0a2012-07-31 16:43:58 -07003121 ac_put_obj(cachep, ac, slab_get_obj(cachep, slabp,
3122 node));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 }
3124 check_slabp(cachep, slabp);
3125
3126 /* move slabp to correct slabp list: */
3127 list_del(&slabp->list);
3128 if (slabp->free == BUFCTL_END)
3129 list_add(&slabp->list, &l3->slabs_full);
3130 else
3131 list_add(&slabp->list, &l3->slabs_partial);
3132 }
3133
Andrew Mortona737b3e2006-03-22 00:08:11 -08003134must_grow:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135 l3->free_objects -= ac->avail;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003136alloc_done:
Christoph Lametere498be72005-09-09 13:03:32 -07003137 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
3139 if (unlikely(!ac->avail)) {
3140 int x;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003141force_grow:
Christoph Lameter3c517a62006-12-06 20:33:29 -08003142 x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07003143
Andrew Mortona737b3e2006-03-22 00:08:11 -08003144 /* cache_grow can reenable interrupts, then ac could change. */
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003145 ac = cpu_cache_get(cachep);
David Rientjes51cd8e62012-08-28 19:57:21 -07003146 node = numa_mem_id();
Mel Gorman072bb0a2012-07-31 16:43:58 -07003147
3148 /* no objects in sight? abort */
3149 if (!x && (ac->avail == 0 || force_refill))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 return NULL;
3151
Andrew Mortona737b3e2006-03-22 00:08:11 -08003152 if (!ac->avail) /* objects refilled by interrupt? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 goto retry;
3154 }
3155 ac->touched = 1;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003156
3157 return ac_get_obj(cachep, ac, flags, force_refill);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158}
3159
Andrew Mortona737b3e2006-03-22 00:08:11 -08003160static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
3161 gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162{
3163 might_sleep_if(flags & __GFP_WAIT);
3164#if DEBUG
3165 kmem_flagcheck(cachep, flags);
3166#endif
3167}
3168
3169#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003170static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003171 gfp_t flags, void *objp, unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003173 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 return objp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003175 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176#ifdef CONFIG_DEBUG_PAGEALLOC
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003177 if ((cachep->size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003178 kernel_map_pages(virt_to_page(objp),
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003179 cachep->size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 else
3181 check_poison_obj(cachep, objp);
3182#else
3183 check_poison_obj(cachep, objp);
3184#endif
3185 poison_obj(cachep, objp, POISON_INUSE);
3186 }
3187 if (cachep->flags & SLAB_STORE_USER)
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003188 *dbg_userword(cachep, objp) = (void *)caller;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189
3190 if (cachep->flags & SLAB_RED_ZONE) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003191 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
3192 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
3193 slab_error(cachep, "double free, or memory outside"
3194 " object was overwritten");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003195 printk(KERN_ERR
David Woodhouseb46b8f12007-05-08 00:22:59 -07003196 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08003197 objp, *dbg_redzone1(cachep, objp),
3198 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 }
3200 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
3201 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
3202 }
Al Viro871751e2006-03-25 03:06:39 -08003203#ifdef CONFIG_DEBUG_SLAB_LEAK
3204 {
3205 struct slab *slabp;
3206 unsigned objnr;
3207
Christoph Lameter35026082012-06-13 10:24:56 -05003208 slabp = virt_to_head_page(objp)->slab_page;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003209 objnr = (unsigned)(objp - slabp->s_mem) / cachep->size;
Al Viro871751e2006-03-25 03:06:39 -08003210 slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE;
3211 }
3212#endif
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003213 objp += obj_offset(cachep);
Christoph Lameter4f104932007-05-06 14:50:17 -07003214 if (cachep->ctor && cachep->flags & SLAB_POISON)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003215 cachep->ctor(objp);
Tetsuo Handa7ea466f2011-07-21 09:42:45 +09003216 if (ARCH_SLAB_MINALIGN &&
3217 ((unsigned long)objp & (ARCH_SLAB_MINALIGN-1))) {
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003218 printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
Hugh Dickinsc2251502011-07-11 13:35:08 -07003219 objp, (int)ARCH_SLAB_MINALIGN);
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 return objp;
3222}
3223#else
3224#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
3225#endif
3226
Akinobu Mita773ff602008-12-23 19:37:01 +09003227static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags)
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003228{
Christoph Lameter9b030cb2012-09-05 00:20:33 +00003229 if (cachep == kmem_cache)
Akinobu Mita773ff602008-12-23 19:37:01 +09003230 return false;
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003231
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003232 return should_failslab(cachep->object_size, flags, cachep->flags);
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003233}
3234
Pekka Enberg343e0d72006-02-01 03:05:50 -08003235static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003236{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003237 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003238 struct array_cache *ac;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003239 bool force_refill = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240
Alok N Kataria5c382302005-09-27 21:45:46 -07003241 check_irq_off();
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003242
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003243 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 if (likely(ac->avail)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 ac->touched = 1;
Mel Gorman072bb0a2012-07-31 16:43:58 -07003246 objp = ac_get_obj(cachep, ac, flags, false);
3247
J. R. Okajimaddbf2e82009-12-02 16:55:50 +09003248 /*
Mel Gorman072bb0a2012-07-31 16:43:58 -07003249 * Allow for the possibility all avail objects are not allowed
3250 * by the current flags
J. R. Okajimaddbf2e82009-12-02 16:55:50 +09003251 */
Mel Gorman072bb0a2012-07-31 16:43:58 -07003252 if (objp) {
3253 STATS_INC_ALLOCHIT(cachep);
3254 goto out;
3255 }
3256 force_refill = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 }
Mel Gorman072bb0a2012-07-31 16:43:58 -07003258
3259 STATS_INC_ALLOCMISS(cachep);
3260 objp = cache_alloc_refill(cachep, flags, force_refill);
3261 /*
3262 * the 'ac' may be updated by cache_alloc_refill(),
3263 * and kmemleak_erase() requires its correct value.
3264 */
3265 ac = cpu_cache_get(cachep);
3266
3267out:
Catalin Marinasd5cff632009-06-11 13:22:40 +01003268 /*
3269 * To avoid a false negative, if an object that is in one of the
3270 * per-CPU caches is leaked, we need to make sure kmemleak doesn't
3271 * treat the array pointers as a reference to the object.
3272 */
J. R. Okajimaf3d8b532009-12-02 16:55:49 +09003273 if (objp)
3274 kmemleak_erase(&ac->entry[ac->avail]);
Alok N Kataria5c382302005-09-27 21:45:46 -07003275 return objp;
3276}
3277
Christoph Lametere498be72005-09-09 13:03:32 -07003278#ifdef CONFIG_NUMA
3279/*
Paul Jacksonb2455392006-03-24 03:16:12 -08003280 * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
Paul Jacksonc61afb12006-03-24 03:16:08 -08003281 *
3282 * If we are in_interrupt, then process context, including cpusets and
3283 * mempolicy, may not apply and should not be used for allocation policy.
3284 */
3285static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
3286{
3287 int nid_alloc, nid_here;
3288
Christoph Lameter765c4502006-09-27 01:50:08 -07003289 if (in_interrupt() || (flags & __GFP_THISNODE))
Paul Jacksonc61afb12006-03-24 03:16:08 -08003290 return NULL;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003291 nid_alloc = nid_here = numa_mem_id();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003292 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
Jack Steiner6adef3e2010-05-26 14:42:49 -07003293 nid_alloc = cpuset_slab_spread_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003294 else if (current->mempolicy)
Andi Kleene7b691b2012-06-09 02:40:03 -07003295 nid_alloc = slab_node();
Paul Jacksonc61afb12006-03-24 03:16:08 -08003296 if (nid_alloc != nid_here)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003297 return ____cache_alloc_node(cachep, flags, nid_alloc);
Paul Jacksonc61afb12006-03-24 03:16:08 -08003298 return NULL;
3299}
3300
3301/*
Christoph Lameter765c4502006-09-27 01:50:08 -07003302 * Fallback function if there was no memory available and no objects on a
Christoph Lameter3c517a62006-12-06 20:33:29 -08003303 * certain node and fall back is permitted. First we scan all the
3304 * available nodelists for available objects. If that fails then we
3305 * perform an allocation without specifying a node. This allows the page
3306 * allocator to do its reclaim / fallback magic. We then insert the
3307 * slab into the proper nodelist and then allocate from it.
Christoph Lameter765c4502006-09-27 01:50:08 -07003308 */
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003309static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
Christoph Lameter765c4502006-09-27 01:50:08 -07003310{
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003311 struct zonelist *zonelist;
3312 gfp_t local_flags;
Mel Gormandd1a2392008-04-28 02:12:17 -07003313 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07003314 struct zone *zone;
3315 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003316 void *obj = NULL;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003317 int nid;
Mel Gormancc9a6c82012-03-21 16:34:11 -07003318 unsigned int cpuset_mems_cookie;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003319
3320 if (flags & __GFP_THISNODE)
3321 return NULL;
3322
Christoph Lameter6cb06222007-10-16 01:25:41 -07003323 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Christoph Lameter765c4502006-09-27 01:50:08 -07003324
Mel Gormancc9a6c82012-03-21 16:34:11 -07003325retry_cpuset:
3326 cpuset_mems_cookie = get_mems_allowed();
Andi Kleene7b691b2012-06-09 02:40:03 -07003327 zonelist = node_zonelist(slab_node(), flags);
Mel Gormancc9a6c82012-03-21 16:34:11 -07003328
Christoph Lameter3c517a62006-12-06 20:33:29 -08003329retry:
3330 /*
3331 * Look through allowed nodes for objects available
3332 * from existing per node queues.
3333 */
Mel Gorman54a6eb52008-04-28 02:12:16 -07003334 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
3335 nid = zone_to_nid(zone);
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003336
Mel Gorman54a6eb52008-04-28 02:12:16 -07003337 if (cpuset_zone_allowed_hardwall(zone, flags) &&
Christoph Lameter3c517a62006-12-06 20:33:29 -08003338 cache->nodelists[nid] &&
Christoph Lameter481c5342008-06-21 16:46:35 -07003339 cache->nodelists[nid]->free_objects) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003340 obj = ____cache_alloc_node(cache,
3341 flags | GFP_THISNODE, nid);
Christoph Lameter481c5342008-06-21 16:46:35 -07003342 if (obj)
3343 break;
3344 }
Christoph Lameter3c517a62006-12-06 20:33:29 -08003345 }
3346
Christoph Lametercfce6602007-05-06 14:50:17 -07003347 if (!obj) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003348 /*
3349 * This allocation will be performed within the constraints
3350 * of the current cpuset / memory policy requirements.
3351 * We may trigger various forms of reclaim on the allowed
3352 * set and go into memory reserves if necessary.
3353 */
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003354 if (local_flags & __GFP_WAIT)
3355 local_irq_enable();
3356 kmem_flagcheck(cache, flags);
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003357 obj = kmem_getpages(cache, local_flags, numa_mem_id());
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003358 if (local_flags & __GFP_WAIT)
3359 local_irq_disable();
Christoph Lameter3c517a62006-12-06 20:33:29 -08003360 if (obj) {
3361 /*
3362 * Insert into the appropriate per node queues
3363 */
3364 nid = page_to_nid(virt_to_page(obj));
3365 if (cache_grow(cache, flags, nid, obj)) {
3366 obj = ____cache_alloc_node(cache,
3367 flags | GFP_THISNODE, nid);
3368 if (!obj)
3369 /*
3370 * Another processor may allocate the
3371 * objects in the slab since we are
3372 * not holding any locks.
3373 */
3374 goto retry;
3375 } else {
Hugh Dickinsb6a60452007-01-05 16:36:36 -08003376 /* cache_grow already freed obj */
Christoph Lameter3c517a62006-12-06 20:33:29 -08003377 obj = NULL;
3378 }
3379 }
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003380 }
Mel Gormancc9a6c82012-03-21 16:34:11 -07003381
3382 if (unlikely(!put_mems_allowed(cpuset_mems_cookie) && !obj))
3383 goto retry_cpuset;
Christoph Lameter765c4502006-09-27 01:50:08 -07003384 return obj;
3385}
3386
3387/*
Christoph Lametere498be72005-09-09 13:03:32 -07003388 * A interface to enable slab creation on nodeid
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003390static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003391 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07003392{
3393 struct list_head *entry;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003394 struct slab *slabp;
3395 struct kmem_list3 *l3;
3396 void *obj;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003397 int x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003399 l3 = cachep->nodelists[nodeid];
3400 BUG_ON(!l3);
Christoph Lametere498be72005-09-09 13:03:32 -07003401
Andrew Mortona737b3e2006-03-22 00:08:11 -08003402retry:
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003403 check_irq_off();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003404 spin_lock(&l3->list_lock);
3405 entry = l3->slabs_partial.next;
3406 if (entry == &l3->slabs_partial) {
3407 l3->free_touched = 1;
3408 entry = l3->slabs_free.next;
3409 if (entry == &l3->slabs_free)
3410 goto must_grow;
3411 }
Christoph Lametere498be72005-09-09 13:03:32 -07003412
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003413 slabp = list_entry(entry, struct slab, list);
3414 check_spinlock_acquired_node(cachep, nodeid);
3415 check_slabp(cachep, slabp);
Christoph Lametere498be72005-09-09 13:03:32 -07003416
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003417 STATS_INC_NODEALLOCS(cachep);
3418 STATS_INC_ACTIVE(cachep);
3419 STATS_SET_HIGH(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003420
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003421 BUG_ON(slabp->inuse == cachep->num);
Christoph Lametere498be72005-09-09 13:03:32 -07003422
Matthew Dobson78d382d2006-02-01 03:05:47 -08003423 obj = slab_get_obj(cachep, slabp, nodeid);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003424 check_slabp(cachep, slabp);
3425 l3->free_objects--;
3426 /* move slabp to correct slabp list: */
3427 list_del(&slabp->list);
Christoph Lametere498be72005-09-09 13:03:32 -07003428
Andrew Mortona737b3e2006-03-22 00:08:11 -08003429 if (slabp->free == BUFCTL_END)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003430 list_add(&slabp->list, &l3->slabs_full);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003431 else
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003432 list_add(&slabp->list, &l3->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07003433
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003434 spin_unlock(&l3->list_lock);
3435 goto done;
Christoph Lametere498be72005-09-09 13:03:32 -07003436
Andrew Mortona737b3e2006-03-22 00:08:11 -08003437must_grow:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003438 spin_unlock(&l3->list_lock);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003439 x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
Christoph Lameter765c4502006-09-27 01:50:08 -07003440 if (x)
3441 goto retry;
Christoph Lametere498be72005-09-09 13:03:32 -07003442
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003443 return fallback_alloc(cachep, flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003444
Andrew Mortona737b3e2006-03-22 00:08:11 -08003445done:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003446 return obj;
Christoph Lametere498be72005-09-09 13:03:32 -07003447}
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003448
3449/**
3450 * kmem_cache_alloc_node - Allocate an object on the specified node
3451 * @cachep: The cache to allocate from.
3452 * @flags: See kmalloc().
3453 * @nodeid: node number of the target node.
3454 * @caller: return address of caller, used for debug information
3455 *
3456 * Identical to kmem_cache_alloc but it will allocate memory on the given
3457 * node, which can improve the performance for cpu bound structures.
3458 *
3459 * Fallback to other node is possible if __GFP_THISNODE is not set.
3460 */
3461static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003462slab_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003463 unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003464{
3465 unsigned long save_flags;
3466 void *ptr;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003467 int slab_node = numa_mem_id();
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003468
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003469 flags &= gfp_allowed_mask;
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003470
Nick Piggincf40bd12009-01-21 08:12:39 +01003471 lockdep_trace_alloc(flags);
3472
Akinobu Mita773ff602008-12-23 19:37:01 +09003473 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003474 return NULL;
3475
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003476 cache_alloc_debugcheck_before(cachep, flags);
3477 local_irq_save(save_flags);
3478
Andrew Mortoneacbbae2011-07-28 13:59:49 -07003479 if (nodeid == NUMA_NO_NODE)
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003480 nodeid = slab_node;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003481
3482 if (unlikely(!cachep->nodelists[nodeid])) {
3483 /* Node not bootstrapped yet */
3484 ptr = fallback_alloc(cachep, flags);
3485 goto out;
3486 }
3487
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003488 if (nodeid == slab_node) {
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003489 /*
3490 * Use the locally cached objects if possible.
3491 * However ____cache_alloc does not allow fallback
3492 * to other nodes. It may fail while we still have
3493 * objects on other nodes available.
3494 */
3495 ptr = ____cache_alloc(cachep, flags);
3496 if (ptr)
3497 goto out;
3498 }
3499 /* ___cache_alloc_node can fall back to other nodes */
3500 ptr = ____cache_alloc_node(cachep, flags, nodeid);
3501 out:
3502 local_irq_restore(save_flags);
3503 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003504 kmemleak_alloc_recursive(ptr, cachep->object_size, 1, cachep->flags,
Catalin Marinasd5cff632009-06-11 13:22:40 +01003505 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003506
Pekka Enbergc175eea2008-05-09 20:35:53 +02003507 if (likely(ptr))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003508 kmemcheck_slab_alloc(cachep, flags, ptr, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003509
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003510 if (unlikely((flags & __GFP_ZERO) && ptr))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003511 memset(ptr, 0, cachep->object_size);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003512
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003513 return ptr;
3514}
3515
3516static __always_inline void *
3517__do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
3518{
3519 void *objp;
3520
3521 if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
3522 objp = alternate_node_alloc(cache, flags);
3523 if (objp)
3524 goto out;
3525 }
3526 objp = ____cache_alloc(cache, flags);
3527
3528 /*
3529 * We may just have run out of memory on the local node.
3530 * ____cache_alloc_node() knows how to locate memory on other nodes
3531 */
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003532 if (!objp)
3533 objp = ____cache_alloc_node(cache, flags, numa_mem_id());
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003534
3535 out:
3536 return objp;
3537}
3538#else
3539
3540static __always_inline void *
3541__do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
3542{
3543 return ____cache_alloc(cachep, flags);
3544}
3545
3546#endif /* CONFIG_NUMA */
3547
3548static __always_inline void *
Ezequiel Garcia48356302012-09-08 17:47:57 -03003549slab_alloc(struct kmem_cache *cachep, gfp_t flags, unsigned long caller)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003550{
3551 unsigned long save_flags;
3552 void *objp;
3553
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003554 flags &= gfp_allowed_mask;
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003555
Nick Piggincf40bd12009-01-21 08:12:39 +01003556 lockdep_trace_alloc(flags);
3557
Akinobu Mita773ff602008-12-23 19:37:01 +09003558 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003559 return NULL;
3560
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003561 cache_alloc_debugcheck_before(cachep, flags);
3562 local_irq_save(save_flags);
3563 objp = __do_cache_alloc(cachep, flags);
3564 local_irq_restore(save_flags);
3565 objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003566 kmemleak_alloc_recursive(objp, cachep->object_size, 1, cachep->flags,
Catalin Marinasd5cff632009-06-11 13:22:40 +01003567 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003568 prefetchw(objp);
3569
Pekka Enbergc175eea2008-05-09 20:35:53 +02003570 if (likely(objp))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003571 kmemcheck_slab_alloc(cachep, flags, objp, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003572
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003573 if (unlikely((flags & __GFP_ZERO) && objp))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003574 memset(objp, 0, cachep->object_size);
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003575
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003576 return objp;
3577}
Christoph Lametere498be72005-09-09 13:03:32 -07003578
3579/*
3580 * Caller needs to acquire correct kmem_list's list_lock
3581 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003582static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003583 int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584{
3585 int i;
Christoph Lametere498be72005-09-09 13:03:32 -07003586 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587
3588 for (i = 0; i < nr_objects; i++) {
Mel Gorman072bb0a2012-07-31 16:43:58 -07003589 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
Mel Gorman072bb0a2012-07-31 16:43:58 -07003592 clear_obj_pfmemalloc(&objpp[i]);
3593 objp = objpp[i];
3594
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003595 slabp = virt_to_slab(objp);
Christoph Lameterff694162005-09-22 21:44:02 -07003596 l3 = cachep->nodelists[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 list_del(&slabp->list);
Christoph Lameterff694162005-09-22 21:44:02 -07003598 check_spinlock_acquired_node(cachep, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003599 check_slabp(cachep, slabp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08003600 slab_put_obj(cachep, slabp, objp, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 STATS_DEC_ACTIVE(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003602 l3->free_objects++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 check_slabp(cachep, slabp);
3604
3605 /* fixup slab chains */
3606 if (slabp->inuse == 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07003607 if (l3->free_objects > l3->free_limit) {
3608 l3->free_objects -= cachep->num;
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07003609 /* No need to drop any previously held
3610 * lock here, even if we have a off-slab slab
3611 * descriptor it is guaranteed to come from
3612 * a different cache, refer to comments before
3613 * alloc_slabmgmt.
3614 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 slab_destroy(cachep, slabp);
3616 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07003617 list_add(&slabp->list, &l3->slabs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 }
3619 } else {
3620 /* Unconditionally move a slab to the end of the
3621 * partial list on free - maximum time for the
3622 * other objects to be freed, too.
3623 */
Christoph Lametere498be72005-09-09 13:03:32 -07003624 list_add_tail(&slabp->list, &l3->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 }
3626 }
3627}
3628
Pekka Enberg343e0d72006-02-01 03:05:50 -08003629static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630{
3631 int batchcount;
Christoph Lametere498be72005-09-09 13:03:32 -07003632 struct kmem_list3 *l3;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07003633 int node = numa_mem_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634
3635 batchcount = ac->batchcount;
3636#if DEBUG
3637 BUG_ON(!batchcount || batchcount > ac->avail);
3638#endif
3639 check_irq_off();
Christoph Lameterff694162005-09-22 21:44:02 -07003640 l3 = cachep->nodelists[node];
Ingo Molnar873623d2006-07-13 14:44:38 +02003641 spin_lock(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07003642 if (l3->shared) {
3643 struct array_cache *shared_array = l3->shared;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003644 int max = shared_array->limit - shared_array->avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 if (max) {
3646 if (batchcount > max)
3647 batchcount = max;
Christoph Lametere498be72005-09-09 13:03:32 -07003648 memcpy(&(shared_array->entry[shared_array->avail]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003649 ac->entry, sizeof(void *) * batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 shared_array->avail += batchcount;
3651 goto free_done;
3652 }
3653 }
3654
Christoph Lameterff694162005-09-22 21:44:02 -07003655 free_block(cachep, ac->entry, batchcount, node);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003656free_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657#if STATS
3658 {
3659 int i = 0;
3660 struct list_head *p;
3661
Christoph Lametere498be72005-09-09 13:03:32 -07003662 p = l3->slabs_free.next;
3663 while (p != &(l3->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664 struct slab *slabp;
3665
3666 slabp = list_entry(p, struct slab, list);
3667 BUG_ON(slabp->inuse);
3668
3669 i++;
3670 p = p->next;
3671 }
3672 STATS_SET_FREEABLE(cachep, i);
3673 }
3674#endif
Christoph Lametere498be72005-09-09 13:03:32 -07003675 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 ac->avail -= batchcount;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003677 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678}
3679
3680/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08003681 * Release an obj back to its cache. If the obj has a constructed state, it must
3682 * be in this state _before_ it is released. Called with disabled ints.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683 */
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003684static inline void __cache_free(struct kmem_cache *cachep, void *objp,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003685 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686{
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003687 struct array_cache *ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003688
3689 check_irq_off();
Catalin Marinasd5cff632009-06-11 13:22:40 +01003690 kmemleak_free_recursive(objp, cachep->flags);
Suleiman Souhlala947eb92011-06-02 00:16:42 -07003691 objp = cache_free_debugcheck(cachep, objp, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003693 kmemcheck_slab_free(cachep, objp, cachep->object_size);
Pekka Enbergc175eea2008-05-09 20:35:53 +02003694
Siddha, Suresh B1807a1a2007-08-22 14:01:49 -07003695 /*
3696 * Skip calling cache_free_alien() when the platform is not numa.
3697 * This will avoid cache misses that happen while accessing slabp (which
3698 * is per page memory reference) to get nodeid. Instead use a global
3699 * variable to skip the call, which is mostly likely to be present in
3700 * the cache.
3701 */
Mel Gormanb6e68bc2009-06-16 15:32:16 -07003702 if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07003703 return;
Christoph Lametere498be72005-09-09 13:03:32 -07003704
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 if (likely(ac->avail < ac->limit)) {
3706 STATS_INC_FREEHIT(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 } else {
3708 STATS_INC_FREEMISS(cachep);
3709 cache_flusharray(cachep, ac);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 }
Zhao Jin42c8c992011-08-27 00:26:17 +08003711
Mel Gorman072bb0a2012-07-31 16:43:58 -07003712 ac_put_obj(cachep, ac, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713}
3714
3715/**
3716 * kmem_cache_alloc - Allocate an object
3717 * @cachep: The cache to allocate from.
3718 * @flags: See kmalloc().
3719 *
3720 * Allocate an object from this cache. The flags are only relevant
3721 * if the cache has no available objects.
3722 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003723void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003725 void *ret = slab_alloc(cachep, flags, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003726
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003727 trace_kmem_cache_alloc(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003728 cachep->object_size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003729
3730 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731}
3732EXPORT_SYMBOL(kmem_cache_alloc);
3733
Li Zefan0f24f122009-12-11 15:45:30 +08003734#ifdef CONFIG_TRACING
Steven Rostedt85beb582010-11-24 16:23:34 -05003735void *
Ezequiel Garcia40521472012-09-08 17:47:56 -03003736kmem_cache_alloc_trace(struct kmem_cache *cachep, gfp_t flags, size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003737{
Steven Rostedt85beb582010-11-24 16:23:34 -05003738 void *ret;
3739
Ezequiel Garcia48356302012-09-08 17:47:57 -03003740 ret = slab_alloc(cachep, flags, _RET_IP_);
Steven Rostedt85beb582010-11-24 16:23:34 -05003741
3742 trace_kmalloc(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003743 size, cachep->size, flags);
Steven Rostedt85beb582010-11-24 16:23:34 -05003744 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003745}
Steven Rostedt85beb582010-11-24 16:23:34 -05003746EXPORT_SYMBOL(kmem_cache_alloc_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003747#endif
3748
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749#ifdef CONFIG_NUMA
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003750void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
3751{
Ezequiel Garcia48356302012-09-08 17:47:57 -03003752 void *ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003753
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003754 trace_kmem_cache_alloc_node(_RET_IP_, ret,
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003755 cachep->object_size, cachep->size,
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003756 flags, nodeid);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003757
3758 return ret;
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003759}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760EXPORT_SYMBOL(kmem_cache_alloc_node);
3761
Li Zefan0f24f122009-12-11 15:45:30 +08003762#ifdef CONFIG_TRACING
Ezequiel Garcia40521472012-09-08 17:47:56 -03003763void *kmem_cache_alloc_node_trace(struct kmem_cache *cachep,
Steven Rostedt85beb582010-11-24 16:23:34 -05003764 gfp_t flags,
Ezequiel Garcia40521472012-09-08 17:47:56 -03003765 int nodeid,
3766 size_t size)
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003767{
Steven Rostedt85beb582010-11-24 16:23:34 -05003768 void *ret;
3769
Ezequiel Garcia592f4142012-09-25 08:07:08 -03003770 ret = slab_alloc_node(cachep, flags, nodeid, _RET_IP_);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003771
Steven Rostedt85beb582010-11-24 16:23:34 -05003772 trace_kmalloc_node(_RET_IP_, ret,
Ezequiel Garciaff4fcd02012-09-08 17:47:52 -03003773 size, cachep->size,
Steven Rostedt85beb582010-11-24 16:23:34 -05003774 flags, nodeid);
3775 return ret;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003776}
Steven Rostedt85beb582010-11-24 16:23:34 -05003777EXPORT_SYMBOL(kmem_cache_alloc_node_trace);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003778#endif
3779
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003780static __always_inline void *
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003781__do_kmalloc_node(size_t size, gfp_t flags, int node, unsigned long caller)
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003782{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003783 struct kmem_cache *cachep;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003784
3785 cachep = kmem_find_general_cachep(size, flags);
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003786 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3787 return cachep;
Ezequiel Garcia40521472012-09-08 17:47:56 -03003788 return kmem_cache_alloc_node_trace(cachep, flags, node, size);
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003789}
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003790
Li Zefan0bb38a52009-12-11 15:45:50 +08003791#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003792void *__kmalloc_node(size_t size, gfp_t flags, int node)
3793{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003794 return __do_kmalloc_node(size, flags, node, _RET_IP_);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003795}
Christoph Hellwigdbe5e692006-09-25 23:31:36 -07003796EXPORT_SYMBOL(__kmalloc_node);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003797
3798void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003799 int node, unsigned long caller)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003800{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003801 return __do_kmalloc_node(size, flags, node, caller);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003802}
3803EXPORT_SYMBOL(__kmalloc_node_track_caller);
3804#else
3805void *__kmalloc_node(size_t size, gfp_t flags, int node)
3806{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003807 return __do_kmalloc_node(size, flags, node, 0);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003808}
3809EXPORT_SYMBOL(__kmalloc_node);
Li Zefan0bb38a52009-12-11 15:45:50 +08003810#endif /* CONFIG_DEBUG_SLAB || CONFIG_TRACING */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003811#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812
3813/**
Paul Drynoff800590f2006-06-23 02:03:48 -07003814 * __do_kmalloc - allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 * @size: how many bytes of memory are required.
Paul Drynoff800590f2006-06-23 02:03:48 -07003816 * @flags: the type of memory to allocate (see kmalloc).
Randy Dunlap911851e2006-03-22 00:08:14 -08003817 * @caller: function caller for debug tracking of the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 */
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003819static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003820 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003822 struct kmem_cache *cachep;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003823 void *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003825 /* If you want to save a few bytes .text space: replace
3826 * __ with kmem_.
3827 * Then kmalloc uses the uninlined functions instead of the inline
3828 * functions.
3829 */
3830 cachep = __find_general_cachep(size, flags);
Linus Torvaldsa5c96d82007-07-19 13:17:15 -07003831 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3832 return cachep;
Ezequiel Garcia48356302012-09-08 17:47:57 -03003833 ret = slab_alloc(cachep, flags, caller);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003834
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003835 trace_kmalloc(caller, ret,
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003836 size, cachep->size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003837
3838 return ret;
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003839}
3840
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003841
Li Zefan0bb38a52009-12-11 15:45:50 +08003842#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003843void *__kmalloc(size_t size, gfp_t flags)
3844{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003845 return __do_kmalloc(size, flags, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846}
3847EXPORT_SYMBOL(__kmalloc);
3848
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003849void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003850{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003851 return __do_kmalloc(size, flags, caller);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003852}
3853EXPORT_SYMBOL(__kmalloc_track_caller);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003854
3855#else
3856void *__kmalloc(size_t size, gfp_t flags)
3857{
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003858 return __do_kmalloc(size, flags, 0);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003859}
3860EXPORT_SYMBOL(__kmalloc);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003861#endif
3862
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863/**
3864 * kmem_cache_free - Deallocate an object
3865 * @cachep: The cache the allocation was from.
3866 * @objp: The previously allocated object.
3867 *
3868 * Free an object which was previously allocated from this
3869 * cache.
3870 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003871void kmem_cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872{
3873 unsigned long flags;
3874
3875 local_irq_save(flags);
Feng Tangd97d4762012-07-02 14:29:10 +08003876 debug_check_no_locks_freed(objp, cachep->object_size);
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07003877 if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003878 debug_check_no_obj_freed(objp, cachep->object_size);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003879 __cache_free(cachep, objp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880 local_irq_restore(flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003881
Eduard - Gabriel Munteanuca2b84cb2009-03-23 15:12:24 +02003882 trace_kmem_cache_free(_RET_IP_, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883}
3884EXPORT_SYMBOL(kmem_cache_free);
3885
3886/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887 * kfree - free previously allocated memory
3888 * @objp: pointer returned by kmalloc.
3889 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07003890 * If @objp is NULL, no operation is performed.
3891 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 * Don't free memory not originally allocated by kmalloc()
3893 * or you will run into trouble.
3894 */
3895void kfree(const void *objp)
3896{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003897 struct kmem_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003898 unsigned long flags;
3899
Pekka Enberg2121db72009-03-25 11:05:57 +02003900 trace_kfree(_RET_IP_, objp);
3901
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003902 if (unlikely(ZERO_OR_NULL_PTR(objp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903 return;
3904 local_irq_save(flags);
3905 kfree_debugcheck(objp);
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003906 c = virt_to_cache(objp);
Christoph Lameter8c138bc2012-06-13 10:24:58 -05003907 debug_check_no_locks_freed(objp, c->object_size);
3908
3909 debug_check_no_obj_freed(objp, c->object_size);
Ezequiel Garcia7c0cb9c2012-09-08 17:47:55 -03003910 __cache_free(c, (void *)objp, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 local_irq_restore(flags);
3912}
3913EXPORT_SYMBOL(kfree);
3914
Christoph Lametere498be72005-09-09 13:03:32 -07003915/*
Simon Arlott183ff222007-10-20 01:27:18 +02003916 * This initializes kmem_list3 or resizes various caches for all nodes.
Christoph Lametere498be72005-09-09 13:03:32 -07003917 */
Pekka Enberg83b519e2009-06-10 19:40:04 +03003918static int alloc_kmemlist(struct kmem_cache *cachep, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -07003919{
3920 int node;
3921 struct kmem_list3 *l3;
Christoph Lametercafeb022006-03-25 03:06:46 -08003922 struct array_cache *new_shared;
Paul Menage3395ee02006-12-06 20:32:16 -08003923 struct array_cache **new_alien = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003924
Mel Gorman9c09a952008-01-24 05:49:54 -08003925 for_each_online_node(node) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003926
Paul Menage3395ee02006-12-06 20:32:16 -08003927 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03003928 new_alien = alloc_alien_cache(node, cachep->limit, gfp);
Paul Menage3395ee02006-12-06 20:32:16 -08003929 if (!new_alien)
3930 goto fail;
3931 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003932
Eric Dumazet63109842007-05-06 14:49:28 -07003933 new_shared = NULL;
3934 if (cachep->shared) {
3935 new_shared = alloc_arraycache(node,
Christoph Lameter0718dc22006-03-25 03:06:47 -08003936 cachep->shared*cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003937 0xbaadf00d, gfp);
Eric Dumazet63109842007-05-06 14:49:28 -07003938 if (!new_shared) {
3939 free_alien_cache(new_alien);
3940 goto fail;
3941 }
Christoph Lameter0718dc22006-03-25 03:06:47 -08003942 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003943
Andrew Mortona737b3e2006-03-22 00:08:11 -08003944 l3 = cachep->nodelists[node];
3945 if (l3) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003946 struct array_cache *shared = l3->shared;
3947
Christoph Lametere498be72005-09-09 13:03:32 -07003948 spin_lock_irq(&l3->list_lock);
3949
Christoph Lametercafeb022006-03-25 03:06:46 -08003950 if (shared)
Christoph Lameter0718dc22006-03-25 03:06:47 -08003951 free_block(cachep, shared->entry,
3952 shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07003953
Christoph Lametercafeb022006-03-25 03:06:46 -08003954 l3->shared = new_shared;
3955 if (!l3->alien) {
Christoph Lametere498be72005-09-09 13:03:32 -07003956 l3->alien = new_alien;
3957 new_alien = NULL;
3958 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003959 l3->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003960 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07003961 spin_unlock_irq(&l3->list_lock);
Christoph Lametercafeb022006-03-25 03:06:46 -08003962 kfree(shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003963 free_alien_cache(new_alien);
3964 continue;
3965 }
Pekka Enberg83b519e2009-06-10 19:40:04 +03003966 l3 = kmalloc_node(sizeof(struct kmem_list3), gfp, node);
Christoph Lameter0718dc22006-03-25 03:06:47 -08003967 if (!l3) {
3968 free_alien_cache(new_alien);
3969 kfree(new_shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003970 goto fail;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003971 }
Christoph Lametere498be72005-09-09 13:03:32 -07003972
3973 kmem_list3_init(l3);
3974 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
Andrew Mortona737b3e2006-03-22 00:08:11 -08003975 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lametercafeb022006-03-25 03:06:46 -08003976 l3->shared = new_shared;
Christoph Lametere498be72005-09-09 13:03:32 -07003977 l3->alien = new_alien;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003978 l3->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003979 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07003980 cachep->nodelists[node] = l3;
3981 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003982 return 0;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003983
Andrew Mortona737b3e2006-03-22 00:08:11 -08003984fail:
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05003985 if (!cachep->list.next) {
Christoph Lameter0718dc22006-03-25 03:06:47 -08003986 /* Cache is not active yet. Roll back what we did */
3987 node--;
3988 while (node >= 0) {
3989 if (cachep->nodelists[node]) {
3990 l3 = cachep->nodelists[node];
3991
3992 kfree(l3->shared);
3993 free_alien_cache(l3->alien);
3994 kfree(l3);
3995 cachep->nodelists[node] = NULL;
3996 }
3997 node--;
3998 }
3999 }
Christoph Lametercafeb022006-03-25 03:06:46 -08004000 return -ENOMEM;
Christoph Lametere498be72005-09-09 13:03:32 -07004001}
4002
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003struct ccupdate_struct {
Pekka Enberg343e0d72006-02-01 03:05:50 -08004004 struct kmem_cache *cachep;
Eric Dumazetacfe7d72011-07-25 08:55:42 +02004005 struct array_cache *new[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006};
4007
4008static void do_ccupdate_local(void *info)
4009{
Andrew Mortona737b3e2006-03-22 00:08:11 -08004010 struct ccupdate_struct *new = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011 struct array_cache *old;
4012
4013 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08004014 old = cpu_cache_get(new->cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07004015
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
4017 new->new[smp_processor_id()] = old;
4018}
4019
Christoph Lameter18004c52012-07-06 15:25:12 -05004020/* Always called with the slab_mutex held */
Andrew Mortona737b3e2006-03-22 00:08:11 -08004021static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03004022 int batchcount, int shared, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023{
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004024 struct ccupdate_struct *new;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07004025 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026
Eric Dumazetacfe7d72011-07-25 08:55:42 +02004027 new = kzalloc(sizeof(*new) + nr_cpu_ids * sizeof(struct array_cache *),
4028 gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004029 if (!new)
4030 return -ENOMEM;
4031
Christoph Lametere498be72005-09-09 13:03:32 -07004032 for_each_online_cpu(i) {
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07004033 new->new[i] = alloc_arraycache(cpu_to_mem(i), limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03004034 batchcount, gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004035 if (!new->new[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004036 for (i--; i >= 0; i--)
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004037 kfree(new->new[i]);
4038 kfree(new);
Christoph Lametere498be72005-09-09 13:03:32 -07004039 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 }
4041 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004042 new->cachep = cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043
Jens Axboe15c8b6c2008-05-09 09:39:44 +02004044 on_each_cpu(do_ccupdate_local, (void *)new, 1);
Christoph Lametere498be72005-09-09 13:03:32 -07004045
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 check_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047 cachep->batchcount = batchcount;
4048 cachep->limit = limit;
Christoph Lametere498be72005-09-09 13:03:32 -07004049 cachep->shared = shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050
Christoph Lametere498be72005-09-09 13:03:32 -07004051 for_each_online_cpu(i) {
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004052 struct array_cache *ccold = new->new[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 if (!ccold)
4054 continue;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07004055 spin_lock_irq(&cachep->nodelists[cpu_to_mem(i)]->list_lock);
4056 free_block(cachep, ccold->entry, ccold->avail, cpu_to_mem(i));
4057 spin_unlock_irq(&cachep->nodelists[cpu_to_mem(i)]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 kfree(ccold);
4059 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004060 kfree(new);
Pekka Enberg83b519e2009-06-10 19:40:04 +03004061 return alloc_kmemlist(cachep, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062}
4063
Christoph Lameter18004c52012-07-06 15:25:12 -05004064/* Called with slab_mutex held always */
Pekka Enberg83b519e2009-06-10 19:40:04 +03004065static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066{
4067 int err;
4068 int limit, shared;
4069
Andrew Mortona737b3e2006-03-22 00:08:11 -08004070 /*
4071 * The head array serves three purposes:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 * - create a LIFO ordering, i.e. return objects that are cache-warm
4073 * - reduce the number of spinlock operations.
Andrew Mortona737b3e2006-03-22 00:08:11 -08004074 * - reduce the number of linked list operations on the slab and
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 * bufctl chains: array operations are cheaper.
4076 * The numbers are guessed, we should auto-tune as described by
4077 * Bonwick.
4078 */
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004079 if (cachep->size > 131072)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 limit = 1;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004081 else if (cachep->size > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 limit = 8;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004083 else if (cachep->size > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 limit = 24;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004085 else if (cachep->size > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 limit = 54;
4087 else
4088 limit = 120;
4089
Andrew Mortona737b3e2006-03-22 00:08:11 -08004090 /*
4091 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 * allocation behaviour: Most allocs on one cpu, most free operations
4093 * on another cpu. For these cases, an efficient object passing between
4094 * cpus is necessary. This is provided by a shared array. The array
4095 * replaces Bonwick's magazine layer.
4096 * On uniprocessor, it's functionally equivalent (but less efficient)
4097 * to a larger limit. Thus disabled by default.
4098 */
4099 shared = 0;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004100 if (cachep->size <= PAGE_SIZE && num_possible_cpus() > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 shared = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102
4103#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08004104 /*
4105 * With debugging enabled, large batchcount lead to excessively long
4106 * periods with disabled local interrupts. Limit the batchcount
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 */
4108 if (limit > 32)
4109 limit = 32;
4110#endif
Pekka Enberg83b519e2009-06-10 19:40:04 +03004111 err = do_tune_cpucache(cachep, limit, (limit + 1) / 2, shared, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 if (err)
4113 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004114 cachep->name, -err);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07004115 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116}
4117
Christoph Lameter1b552532006-03-22 00:09:07 -08004118/*
4119 * Drain an array if it contains any elements taking the l3 lock only if
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004120 * necessary. Note that the l3 listlock also protects the array_cache
4121 * if drain_array() is used on the shared array.
Christoph Lameter1b552532006-03-22 00:09:07 -08004122 */
H Hartley Sweeten68a1b192011-01-11 17:49:32 -06004123static void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
Christoph Lameter1b552532006-03-22 00:09:07 -08004124 struct array_cache *ac, int force, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125{
4126 int tofree;
4127
Christoph Lameter1b552532006-03-22 00:09:07 -08004128 if (!ac || !ac->avail)
4129 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 if (ac->touched && !force) {
4131 ac->touched = 0;
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004132 } else {
Christoph Lameter1b552532006-03-22 00:09:07 -08004133 spin_lock_irq(&l3->list_lock);
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004134 if (ac->avail) {
4135 tofree = force ? ac->avail : (ac->limit + 4) / 5;
4136 if (tofree > ac->avail)
4137 tofree = (ac->avail + 1) / 2;
4138 free_block(cachep, ac->entry, tofree, node);
4139 ac->avail -= tofree;
4140 memmove(ac->entry, &(ac->entry[tofree]),
4141 sizeof(void *) * ac->avail);
4142 }
Christoph Lameter1b552532006-03-22 00:09:07 -08004143 spin_unlock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 }
4145}
4146
4147/**
4148 * cache_reap - Reclaim memory from caches.
Randy Dunlap05fb6bf2007-02-28 20:12:13 -08004149 * @w: work descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 *
4151 * Called from workqueue/eventd every few seconds.
4152 * Purpose:
4153 * - clear the per-cpu caches for this CPU.
4154 * - return freeable pages to the main free memory pool.
4155 *
Andrew Mortona737b3e2006-03-22 00:08:11 -08004156 * If we cannot acquire the cache chain mutex then just give up - we'll try
4157 * again on the next iteration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004159static void cache_reap(struct work_struct *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160{
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004161 struct kmem_cache *searchp;
Christoph Lametere498be72005-09-09 13:03:32 -07004162 struct kmem_list3 *l3;
Lee Schermerhorn7d6e6d02010-05-26 14:45:03 -07004163 int node = numa_mem_id();
Jean Delvarebf6aede2009-04-02 16:56:54 -07004164 struct delayed_work *work = to_delayed_work(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165
Christoph Lameter18004c52012-07-06 15:25:12 -05004166 if (!mutex_trylock(&slab_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 /* Give up. Setup the next iteration. */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004168 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
Christoph Lameter18004c52012-07-06 15:25:12 -05004170 list_for_each_entry(searchp, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 check_irq_on();
4172
Christoph Lameter35386e32006-03-22 00:09:05 -08004173 /*
4174 * We only take the l3 lock if absolutely necessary and we
4175 * have established with reasonable certainty that
4176 * we can do some work if the lock was obtained.
4177 */
Christoph Lameteraab22072006-03-22 00:09:06 -08004178 l3 = searchp->nodelists[node];
Christoph Lameter35386e32006-03-22 00:09:05 -08004179
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004180 reap_alien(searchp, l3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181
Christoph Lameteraab22072006-03-22 00:09:06 -08004182 drain_array(searchp, l3, cpu_cache_get(searchp), 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
Christoph Lameter35386e32006-03-22 00:09:05 -08004184 /*
4185 * These are racy checks but it does not matter
4186 * if we skip one check or scan twice.
4187 */
Christoph Lametere498be72005-09-09 13:03:32 -07004188 if (time_after(l3->next_reap, jiffies))
Christoph Lameter35386e32006-03-22 00:09:05 -08004189 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190
Christoph Lametere498be72005-09-09 13:03:32 -07004191 l3->next_reap = jiffies + REAPTIMEOUT_LIST3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192
Christoph Lameteraab22072006-03-22 00:09:06 -08004193 drain_array(searchp, l3, l3->shared, 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194
Christoph Lametered11d9e2006-06-30 01:55:45 -07004195 if (l3->free_touched)
Christoph Lametere498be72005-09-09 13:03:32 -07004196 l3->free_touched = 0;
Christoph Lametered11d9e2006-06-30 01:55:45 -07004197 else {
4198 int freed;
4199
4200 freed = drain_freelist(searchp, l3, (l3->free_limit +
4201 5 * searchp->num - 1) / (5 * searchp->num));
4202 STATS_ADD_REAPED(searchp, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 }
Christoph Lameter35386e32006-03-22 00:09:05 -08004204next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205 cond_resched();
4206 }
4207 check_irq_on();
Christoph Lameter18004c52012-07-06 15:25:12 -05004208 mutex_unlock(&slab_mutex);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004209 next_reap_node();
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004210out:
Andrew Mortona737b3e2006-03-22 00:08:11 -08004211 /* Set up the next iteration */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004212 schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_CPUC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213}
4214
Linus Torvalds158a9622008-01-02 13:04:48 -08004215#ifdef CONFIG_SLABINFO
Glauber Costa0d7561c2012-10-19 18:20:27 +04004216void get_slabinfo(struct kmem_cache *cachep, struct slabinfo *sinfo)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004218 struct slab *slabp;
4219 unsigned long active_objs;
4220 unsigned long num_objs;
4221 unsigned long active_slabs = 0;
4222 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004223 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 char *error = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07004225 int node;
4226 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 active_objs = 0;
4229 num_slabs = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004230 for_each_online_node(node) {
4231 l3 = cachep->nodelists[node];
4232 if (!l3)
4233 continue;
4234
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004235 check_irq_on();
4236 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07004237
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004238 list_for_each_entry(slabp, &l3->slabs_full, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004239 if (slabp->inuse != cachep->num && !error)
4240 error = "slabs_full accounting error";
4241 active_objs += cachep->num;
4242 active_slabs++;
4243 }
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004244 list_for_each_entry(slabp, &l3->slabs_partial, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004245 if (slabp->inuse == cachep->num && !error)
4246 error = "slabs_partial inuse accounting error";
4247 if (!slabp->inuse && !error)
4248 error = "slabs_partial/inuse accounting error";
4249 active_objs += slabp->inuse;
4250 active_slabs++;
4251 }
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004252 list_for_each_entry(slabp, &l3->slabs_free, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004253 if (slabp->inuse && !error)
4254 error = "slabs_free/inuse accounting error";
4255 num_slabs++;
4256 }
4257 free_objects += l3->free_objects;
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08004258 if (l3->shared)
4259 shared_avail += l3->shared->avail;
Christoph Lametere498be72005-09-09 13:03:32 -07004260
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004261 spin_unlock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004263 num_slabs += active_slabs;
4264 num_objs = num_slabs * cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07004265 if (num_objs - active_objs != free_objects && !error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 error = "free_objects accounting error";
4267
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004268 name = cachep->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 if (error)
4270 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
4271
Glauber Costa0d7561c2012-10-19 18:20:27 +04004272 sinfo->active_objs = active_objs;
4273 sinfo->num_objs = num_objs;
4274 sinfo->active_slabs = active_slabs;
4275 sinfo->num_slabs = num_slabs;
4276 sinfo->shared_avail = shared_avail;
4277 sinfo->limit = cachep->limit;
4278 sinfo->batchcount = cachep->batchcount;
4279 sinfo->shared = cachep->shared;
4280 sinfo->objects_per_slab = cachep->num;
4281 sinfo->cache_order = cachep->gfporder;
4282}
4283
4284void slabinfo_show_stats(struct seq_file *m, struct kmem_cache *cachep)
4285{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286#if STATS
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004287 { /* list3 stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288 unsigned long high = cachep->high_mark;
4289 unsigned long allocs = cachep->num_allocations;
4290 unsigned long grown = cachep->grown;
4291 unsigned long reaped = cachep->reaped;
4292 unsigned long errors = cachep->errors;
4293 unsigned long max_freeable = cachep->max_freeable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 unsigned long node_allocs = cachep->node_allocs;
Christoph Lametere498be72005-09-09 13:03:32 -07004295 unsigned long node_frees = cachep->node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004296 unsigned long overflows = cachep->node_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297
Joe Perchese92dd4f2010-03-26 19:27:58 -07004298 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu "
4299 "%4lu %4lu %4lu %4lu %4lu",
4300 allocs, high, grown,
4301 reaped, errors, max_freeable, node_allocs,
4302 node_frees, overflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 }
4304 /* cpu stats */
4305 {
4306 unsigned long allochit = atomic_read(&cachep->allochit);
4307 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
4308 unsigned long freehit = atomic_read(&cachep->freehit);
4309 unsigned long freemiss = atomic_read(&cachep->freemiss);
4310
4311 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004312 allochit, allocmiss, freehit, freemiss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 }
4314#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315}
4316
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317#define MAX_SLABINFO_WRITE 128
4318/**
4319 * slabinfo_write - Tuning for the slab allocator
4320 * @file: unused
4321 * @buffer: user buffer
4322 * @count: data length
4323 * @ppos: unused
4324 */
Glauber Costab7454ad2012-10-19 18:20:25 +04004325ssize_t slabinfo_write(struct file *file, const char __user *buffer,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004326 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004328 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 int limit, batchcount, shared, res;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004330 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004331
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 if (count > MAX_SLABINFO_WRITE)
4333 return -EINVAL;
4334 if (copy_from_user(&kbuf, buffer, count))
4335 return -EFAULT;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004336 kbuf[MAX_SLABINFO_WRITE] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337
4338 tmp = strchr(kbuf, ' ');
4339 if (!tmp)
4340 return -EINVAL;
4341 *tmp = '\0';
4342 tmp++;
4343 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
4344 return -EINVAL;
4345
4346 /* Find the cache in the chain of caches. */
Christoph Lameter18004c52012-07-06 15:25:12 -05004347 mutex_lock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 res = -EINVAL;
Christoph Lameter18004c52012-07-06 15:25:12 -05004349 list_for_each_entry(cachep, &slab_caches, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 if (!strcmp(cachep->name, kbuf)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08004351 if (limit < 1 || batchcount < 1 ||
4352 batchcount > limit || shared < 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07004353 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07004355 res = do_tune_cpucache(cachep, limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03004356 batchcount, shared,
4357 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 }
4359 break;
4360 }
4361 }
Christoph Lameter18004c52012-07-06 15:25:12 -05004362 mutex_unlock(&slab_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 if (res >= 0)
4364 res = count;
4365 return res;
4366}
Al Viro871751e2006-03-25 03:06:39 -08004367
4368#ifdef CONFIG_DEBUG_SLAB_LEAK
4369
4370static void *leaks_start(struct seq_file *m, loff_t *pos)
4371{
Christoph Lameter18004c52012-07-06 15:25:12 -05004372 mutex_lock(&slab_mutex);
4373 return seq_list_start(&slab_caches, *pos);
Al Viro871751e2006-03-25 03:06:39 -08004374}
4375
4376static inline int add_caller(unsigned long *n, unsigned long v)
4377{
4378 unsigned long *p;
4379 int l;
4380 if (!v)
4381 return 1;
4382 l = n[1];
4383 p = n + 2;
4384 while (l) {
4385 int i = l/2;
4386 unsigned long *q = p + 2 * i;
4387 if (*q == v) {
4388 q[1]++;
4389 return 1;
4390 }
4391 if (*q > v) {
4392 l = i;
4393 } else {
4394 p = q + 2;
4395 l -= i + 1;
4396 }
4397 }
4398 if (++n[1] == n[0])
4399 return 0;
4400 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4401 p[0] = v;
4402 p[1] = 1;
4403 return 1;
4404}
4405
4406static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
4407{
4408 void *p;
4409 int i;
4410 if (n[0] == n[1])
4411 return;
Christoph Lameter3b0efdf2012-06-13 10:24:57 -05004412 for (i = 0, p = s->s_mem; i < c->num; i++, p += c->size) {
Al Viro871751e2006-03-25 03:06:39 -08004413 if (slab_bufctl(s)[i] != BUFCTL_ACTIVE)
4414 continue;
4415 if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
4416 return;
4417 }
4418}
4419
4420static void show_symbol(struct seq_file *m, unsigned long address)
4421{
4422#ifdef CONFIG_KALLSYMS
Al Viro871751e2006-03-25 03:06:39 -08004423 unsigned long offset, size;
Tejun Heo9281ace2007-07-17 04:03:51 -07004424 char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
Al Viro871751e2006-03-25 03:06:39 -08004425
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004426 if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
Al Viro871751e2006-03-25 03:06:39 -08004427 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004428 if (modname[0])
Al Viro871751e2006-03-25 03:06:39 -08004429 seq_printf(m, " [%s]", modname);
4430 return;
4431 }
4432#endif
4433 seq_printf(m, "%p", (void *)address);
4434}
4435
4436static int leaks_show(struct seq_file *m, void *p)
4437{
Thierry Reding0672aa72012-06-22 19:42:49 +02004438 struct kmem_cache *cachep = list_entry(p, struct kmem_cache, list);
Al Viro871751e2006-03-25 03:06:39 -08004439 struct slab *slabp;
4440 struct kmem_list3 *l3;
4441 const char *name;
4442 unsigned long *n = m->private;
4443 int node;
4444 int i;
4445
4446 if (!(cachep->flags & SLAB_STORE_USER))
4447 return 0;
4448 if (!(cachep->flags & SLAB_RED_ZONE))
4449 return 0;
4450
4451 /* OK, we can do it */
4452
4453 n[1] = 0;
4454
4455 for_each_online_node(node) {
4456 l3 = cachep->nodelists[node];
4457 if (!l3)
4458 continue;
4459
4460 check_irq_on();
4461 spin_lock_irq(&l3->list_lock);
4462
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004463 list_for_each_entry(slabp, &l3->slabs_full, list)
Al Viro871751e2006-03-25 03:06:39 -08004464 handle_slab(n, cachep, slabp);
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004465 list_for_each_entry(slabp, &l3->slabs_partial, list)
Al Viro871751e2006-03-25 03:06:39 -08004466 handle_slab(n, cachep, slabp);
Al Viro871751e2006-03-25 03:06:39 -08004467 spin_unlock_irq(&l3->list_lock);
4468 }
4469 name = cachep->name;
4470 if (n[0] == n[1]) {
4471 /* Increase the buffer size */
Christoph Lameter18004c52012-07-06 15:25:12 -05004472 mutex_unlock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004473 m->private = kzalloc(n[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
4474 if (!m->private) {
4475 /* Too bad, we are really out */
4476 m->private = n;
Christoph Lameter18004c52012-07-06 15:25:12 -05004477 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004478 return -ENOMEM;
4479 }
4480 *(unsigned long *)m->private = n[0] * 2;
4481 kfree(n);
Christoph Lameter18004c52012-07-06 15:25:12 -05004482 mutex_lock(&slab_mutex);
Al Viro871751e2006-03-25 03:06:39 -08004483 /* Now make sure this entry will be retried */
4484 m->count = m->size;
4485 return 0;
4486 }
4487 for (i = 0; i < n[1]; i++) {
4488 seq_printf(m, "%s: %lu ", name, n[2*i+3]);
4489 show_symbol(m, n[2*i+2]);
4490 seq_putc(m, '\n');
4491 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004492
Al Viro871751e2006-03-25 03:06:39 -08004493 return 0;
4494}
4495
Glauber Costab7454ad2012-10-19 18:20:25 +04004496static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4497{
4498 return seq_list_next(p, &slab_caches, pos);
4499}
4500
4501static void s_stop(struct seq_file *m, void *p)
4502{
4503 mutex_unlock(&slab_mutex);
4504}
4505
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004506static const struct seq_operations slabstats_op = {
Al Viro871751e2006-03-25 03:06:39 -08004507 .start = leaks_start,
4508 .next = s_next,
4509 .stop = s_stop,
4510 .show = leaks_show,
4511};
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004512
4513static int slabstats_open(struct inode *inode, struct file *file)
4514{
4515 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
4516 int ret = -ENOMEM;
4517 if (n) {
4518 ret = seq_open(file, &slabstats_op);
4519 if (!ret) {
4520 struct seq_file *m = file->private_data;
4521 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
4522 m->private = n;
4523 n = NULL;
4524 }
4525 kfree(n);
4526 }
4527 return ret;
4528}
4529
4530static const struct file_operations proc_slabstats_operations = {
4531 .open = slabstats_open,
4532 .read = seq_read,
4533 .llseek = seq_lseek,
4534 .release = seq_release_private,
4535};
Al Viro871751e2006-03-25 03:06:39 -08004536#endif
Alexey Dobriyana0ec95a2008-10-06 00:59:10 +04004537
4538static int __init slab_proc_init(void)
4539{
4540#ifdef CONFIG_DEBUG_SLAB_LEAK
4541 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
4542#endif
4543 return 0;
4544}
4545module_init(slab_proc_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546#endif
4547
Manfred Spraul00e145b2005-09-03 15:55:07 -07004548/**
4549 * ksize - get the actual amount of memory allocated for a given object
4550 * @objp: Pointer to the object
4551 *
4552 * kmalloc may internally round up allocations and return more memory
4553 * than requested. ksize() can be used to determine the actual amount of
4554 * memory allocated. The caller may use this additional memory, even though
4555 * a smaller amount of memory was initially specified with the kmalloc call.
4556 * The caller must guarantee that objp points to a valid object previously
4557 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4558 * must not be freed during the duration of the call.
4559 */
Pekka Enbergfd76bab2007-05-06 14:48:40 -07004560size_t ksize(const void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561{
Christoph Lameteref8b4522007-10-16 01:24:46 -07004562 BUG_ON(!objp);
4563 if (unlikely(objp == ZERO_SIZE_PTR))
Manfred Spraul00e145b2005-09-03 15:55:07 -07004564 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565
Christoph Lameter8c138bc2012-06-13 10:24:58 -05004566 return virt_to_cache(objp)->object_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567}
Kirill A. Shutemovb1aabec2009-02-10 15:21:44 +02004568EXPORT_SYMBOL(ksize);