blob: 3230cd2c6b3b2051a1dd35b902dc6db2b5e92259 [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
Ingo Molnarfc0abb12006-01-18 17:42:33 -080071 * The global cache-chain is protected by the mutex 'cache_chain_mutex'.
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * The sem is only needed when accessing/extending the cache-chain, which
73 * can never happen inside an interrupt (kmem_cache_create(),
74 * kmem_cache_shrink() and kmem_cache_reap()).
75 *
76 * At present, each engine can be growing a cache. This should be blocked.
77 *
Christoph Lametere498be72005-09-09 13:03:32 -070078 * 15 March 2005. NUMA slab allocator.
79 * Shai Fultheim <shai@scalex86.org>.
80 * Shobhit Dayal <shobhit@calsoftinc.com>
81 * Alok N Kataria <alokk@calsoftinc.com>
82 * Christoph Lameter <christoph@lameter.com>
83 *
84 * Modified the slab allocator to be node aware on NUMA systems.
85 * Each node has its own list of partial, free and full slabs.
86 * All object allocations for a node occur from node specific slab lists.
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 */
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089#include <linux/slab.h>
90#include <linux/mm.h>
Randy Dunlapc9cf5522006-06-27 02:53:52 -070091#include <linux/poison.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070092#include <linux/swap.h>
93#include <linux/cache.h>
94#include <linux/interrupt.h>
95#include <linux/init.h>
96#include <linux/compiler.h>
Paul Jackson101a5002006-03-24 03:16:07 -080097#include <linux/cpuset.h>
Alexey Dobriyana0ec95a82008-10-06 00:59:10 +040098#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#include <linux/seq_file.h>
100#include <linux/notifier.h>
101#include <linux/kallsyms.h>
102#include <linux/cpu.h>
103#include <linux/sysctl.h>
104#include <linux/module.h>
Zhaolei02af61b2009-04-10 14:26:18 +0800105#include <linux/kmemtrace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106#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 Torvalds1da177e2005-04-16 15:20:36 -0700119
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#include <asm/cacheflush.h>
121#include <asm/tlbflush.h>
122#include <asm/page.h>
123
124/*
Christoph Lameter50953fe2007-05-06 14:50:16 -0700125 * DEBUG - 1 for kmem_cache_create() to honour; SLAB_RED_ZONE & SLAB_POISON.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 * 0 for faster, smaller code (especially in the critical paths).
127 *
128 * STATS - 1 to collect stats for /proc/slabinfo.
129 * 0 for faster, smaller code (especially in the critical paths).
130 *
131 * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
132 */
133
134#ifdef CONFIG_DEBUG_SLAB
135#define DEBUG 1
136#define STATS 1
137#define FORCED_DEBUG 1
138#else
139#define DEBUG 0
140#define STATS 0
141#define FORCED_DEBUG 0
142#endif
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144/* Shouldn't this be in a header file somewhere? */
145#define BYTES_PER_WORD sizeof(void *)
David Woodhouse87a927c2007-07-04 21:26:44 -0400146#define REDZONE_ALIGN max(BYTES_PER_WORD, __alignof__(unsigned long long))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148#ifndef ARCH_KMALLOC_MINALIGN
149/*
150 * Enforce a minimum alignment for the kmalloc caches.
151 * Usually, the kmalloc caches are cache_line_size() aligned, except when
152 * DEBUG and FORCED_DEBUG are enabled, then they are BYTES_PER_WORD aligned.
153 * Some archs want to perform DMA into kmalloc caches and need a guaranteed
David Woodhouseb46b8f12007-05-08 00:22:59 -0700154 * alignment larger than the alignment of a 64-bit integer.
155 * ARCH_KMALLOC_MINALIGN allows that.
156 * Note that increasing this value may disable some debug features.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 */
David Woodhouseb46b8f12007-05-08 00:22:59 -0700158#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#endif
160
161#ifndef ARCH_SLAB_MINALIGN
162/*
163 * Enforce a minimum alignment for all caches.
164 * Intended for archs that get misalignment faults even for BYTES_PER_WORD
165 * aligned buffers. Includes ARCH_KMALLOC_MINALIGN.
166 * If possible: Do not enable this flag for CONFIG_DEBUG_SLAB, it disables
167 * some debug features.
168 */
169#define ARCH_SLAB_MINALIGN 0
170#endif
171
172#ifndef ARCH_KMALLOC_FLAGS
173#define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
174#endif
175
176/* Legal flag mask for kmem_cache_create(). */
177#if DEBUG
Christoph Lameter50953fe2007-05-06 14:50:16 -0700178# define CREATE_MASK (SLAB_RED_ZONE | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 SLAB_POISON | SLAB_HWCACHE_ALIGN | \
Christoph Lameterac2b8982006-03-22 00:08:15 -0800180 SLAB_CACHE_DMA | \
Christoph Lameter5af60832007-05-06 14:49:56 -0700181 SLAB_STORE_USER | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700183 SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD | \
Pekka Enbergc175eea2008-05-09 20:35:53 +0200184 SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE | SLAB_NOTRACK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185#else
Christoph Lameterac2b8982006-03-22 00:08:15 -0800186# define CREATE_MASK (SLAB_HWCACHE_ALIGN | \
Christoph Lameter5af60832007-05-06 14:49:56 -0700187 SLAB_CACHE_DMA | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700189 SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD | \
Pekka Enbergc175eea2008-05-09 20:35:53 +0200190 SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE | SLAB_NOTRACK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#endif
192
193/*
194 * kmem_bufctl_t:
195 *
196 * Bufctl's are used for linking objs within a slab
197 * linked offsets.
198 *
199 * This implementation relies on "struct page" for locating the cache &
200 * slab an object belongs to.
201 * This allows the bufctl structure to be small (one int), but limits
202 * the number of objects a slab (not a cache) can contain when off-slab
203 * bufctls are used. The limit is the size of the largest general cache
204 * that does not use off-slab slabs.
205 * For 32bit archs with 4 kB pages, is this 56.
206 * This is not serious, as it is only for large objects, when it is unwise
207 * to have too many per slab.
208 * Note: This limit can be raised by introducing a general cache whose size
209 * is less than 512 (PAGE_SIZE<<3), but greater than 256.
210 */
211
Kyle Moffettfa5b08d2005-09-03 15:55:03 -0700212typedef unsigned int kmem_bufctl_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213#define BUFCTL_END (((kmem_bufctl_t)(~0U))-0)
214#define BUFCTL_FREE (((kmem_bufctl_t)(~0U))-1)
Al Viro871751e2006-03-25 03:06:39 -0800215#define BUFCTL_ACTIVE (((kmem_bufctl_t)(~0U))-2)
216#define SLAB_LIMIT (((kmem_bufctl_t)(~0U))-3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/*
219 * struct slab
220 *
221 * Manages the objs in a slab. Placed either at the beginning of mem allocated
222 * for a slab, or allocated from an general cache.
223 * Slabs are chained into three list: fully used, partial, fully free slabs.
224 */
225struct slab {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800226 struct list_head list;
227 unsigned long colouroff;
228 void *s_mem; /* including colour offset */
229 unsigned int inuse; /* num of objs active in slab */
230 kmem_bufctl_t free;
231 unsigned short nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232};
233
234/*
235 * struct slab_rcu
236 *
237 * slab_destroy on a SLAB_DESTROY_BY_RCU cache uses this structure to
238 * arrange for kmem_freepages to be called via RCU. This is useful if
239 * we need to approach a kernel structure obliquely, from its address
240 * obtained without the usual locking. We can lock the structure to
241 * stabilize it and check it's still at the given address, only if we
242 * can be sure that the memory has not been meanwhile reused for some
243 * other kind of object (which our subsystem's lock might corrupt).
244 *
245 * rcu_read_lock before reading the address, then rcu_read_unlock after
246 * taking the spinlock within the structure expected at that address.
247 *
248 * We assume struct slab_rcu can overlay struct slab when destroying.
249 */
250struct slab_rcu {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800251 struct rcu_head head;
Pekka Enberg343e0d72006-02-01 03:05:50 -0800252 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800253 void *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254};
255
256/*
257 * struct array_cache
258 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 * Purpose:
260 * - LIFO ordering, to hand out cache-warm objects from _alloc
261 * - reduce the number of linked list operations
262 * - reduce spinlock operations
263 *
264 * The limit is stored in the per-cpu structure to reduce the data cache
265 * footprint.
266 *
267 */
268struct array_cache {
269 unsigned int avail;
270 unsigned int limit;
271 unsigned int batchcount;
272 unsigned int touched;
Christoph Lametere498be72005-09-09 13:03:32 -0700273 spinlock_t lock;
Robert P. J. Daybda5b652007-10-16 23:30:05 -0700274 void *entry[]; /*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800275 * Must have this definition in here for the proper
276 * alignment of array_cache. Also simplifies accessing
277 * the entries.
Andrew Mortona737b3e2006-03-22 00:08:11 -0800278 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279};
280
Andrew Mortona737b3e2006-03-22 00:08:11 -0800281/*
282 * bootstrap: The caches do not work without cpuarrays anymore, but the
283 * cpuarrays are allocated from the generic caches...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 */
285#define BOOT_CPUCACHE_ENTRIES 1
286struct arraycache_init {
287 struct array_cache cache;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800288 void *entries[BOOT_CPUCACHE_ENTRIES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289};
290
291/*
Christoph Lametere498be72005-09-09 13:03:32 -0700292 * The slab lists for all objects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 */
294struct kmem_list3 {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800295 struct list_head slabs_partial; /* partial list first, better asm code */
296 struct list_head slabs_full;
297 struct list_head slabs_free;
298 unsigned long free_objects;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800299 unsigned int free_limit;
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -0800300 unsigned int colour_next; /* Per-node cache coloring */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800301 spinlock_t list_lock;
302 struct array_cache *shared; /* shared per node */
303 struct array_cache **alien; /* on other nodes */
Christoph Lameter35386e32006-03-22 00:09:05 -0800304 unsigned long next_reap; /* updated without locking */
305 int free_touched; /* updated without locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306};
307
Christoph Lametere498be72005-09-09 13:03:32 -0700308/*
309 * Need this for bootstrapping a per node allocator.
310 */
Pekka Enberg556a1692008-01-25 08:20:51 +0200311#define NUM_INIT_LISTS (3 * MAX_NUMNODES)
Christoph Lametere498be72005-09-09 13:03:32 -0700312struct kmem_list3 __initdata initkmem_list3[NUM_INIT_LISTS];
313#define CACHE_CACHE 0
Pekka Enberg556a1692008-01-25 08:20:51 +0200314#define SIZE_AC MAX_NUMNODES
315#define SIZE_L3 (2 * MAX_NUMNODES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Christoph Lametered11d9e2006-06-30 01:55:45 -0700317static int drain_freelist(struct kmem_cache *cache,
318 struct kmem_list3 *l3, int tofree);
319static void free_block(struct kmem_cache *cachep, void **objpp, int len,
320 int node);
Pekka Enberg83b519e2009-06-10 19:40:04 +0300321static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp);
David Howells65f27f32006-11-22 14:55:48 +0000322static void cache_reap(struct work_struct *unused);
Christoph Lametered11d9e2006-06-30 01:55:45 -0700323
Christoph Lametere498be72005-09-09 13:03:32 -0700324/*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800325 * This function must be completely optimized away if a constant is passed to
326 * it. Mostly the same as what is in linux/slab.h except it returns an index.
Christoph Lametere498be72005-09-09 13:03:32 -0700327 */
Ivan Kokshaysky7243cc02005-09-22 21:43:58 -0700328static __always_inline int index_of(const size_t size)
Christoph Lametere498be72005-09-09 13:03:32 -0700329{
Steven Rostedt5ec8a842006-02-01 03:05:44 -0800330 extern void __bad_size(void);
331
Christoph Lametere498be72005-09-09 13:03:32 -0700332 if (__builtin_constant_p(size)) {
333 int i = 0;
334
335#define CACHE(x) \
336 if (size <=x) \
337 return i; \
338 else \
339 i++;
Joe Perches1c61fc42008-03-05 13:58:17 -0800340#include <linux/kmalloc_sizes.h>
Christoph Lametere498be72005-09-09 13:03:32 -0700341#undef CACHE
Steven Rostedt5ec8a842006-02-01 03:05:44 -0800342 __bad_size();
Ivan Kokshaysky7243cc02005-09-22 21:43:58 -0700343 } else
Steven Rostedt5ec8a842006-02-01 03:05:44 -0800344 __bad_size();
Christoph Lametere498be72005-09-09 13:03:32 -0700345 return 0;
346}
347
Ingo Molnare0a42722006-06-23 02:03:46 -0700348static int slab_early_init = 1;
349
Christoph Lametere498be72005-09-09 13:03:32 -0700350#define INDEX_AC index_of(sizeof(struct arraycache_init))
351#define INDEX_L3 index_of(sizeof(struct kmem_list3))
352
Pekka Enberg5295a742006-02-01 03:05:48 -0800353static void kmem_list3_init(struct kmem_list3 *parent)
Christoph Lametere498be72005-09-09 13:03:32 -0700354{
355 INIT_LIST_HEAD(&parent->slabs_full);
356 INIT_LIST_HEAD(&parent->slabs_partial);
357 INIT_LIST_HEAD(&parent->slabs_free);
358 parent->shared = NULL;
359 parent->alien = NULL;
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -0800360 parent->colour_next = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700361 spin_lock_init(&parent->list_lock);
362 parent->free_objects = 0;
363 parent->free_touched = 0;
364}
365
Andrew Mortona737b3e2006-03-22 00:08:11 -0800366#define MAKE_LIST(cachep, listp, slab, nodeid) \
367 do { \
368 INIT_LIST_HEAD(listp); \
369 list_splice(&(cachep->nodelists[nodeid]->slab), listp); \
Christoph Lametere498be72005-09-09 13:03:32 -0700370 } while (0)
371
Andrew Mortona737b3e2006-03-22 00:08:11 -0800372#define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
373 do { \
Christoph Lametere498be72005-09-09 13:03:32 -0700374 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
375 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
376 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
377 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379#define CFLGS_OFF_SLAB (0x80000000UL)
380#define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
381
382#define BATCHREFILL_LIMIT 16
Andrew Mortona737b3e2006-03-22 00:08:11 -0800383/*
384 * Optimization question: fewer reaps means less probability for unnessary
385 * cpucache drain/refill cycles.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 *
Adrian Bunkdc6f3f22005-11-08 16:44:08 +0100387 * OTOH the cpuarrays can contain lots of objects,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 * which could lock up otherwise freeable slabs.
389 */
390#define REAPTIMEOUT_CPUC (2*HZ)
391#define REAPTIMEOUT_LIST3 (4*HZ)
392
393#if STATS
394#define STATS_INC_ACTIVE(x) ((x)->num_active++)
395#define STATS_DEC_ACTIVE(x) ((x)->num_active--)
396#define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
397#define STATS_INC_GROWN(x) ((x)->grown++)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700398#define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
Andrew Mortona737b3e2006-03-22 00:08:11 -0800399#define STATS_SET_HIGH(x) \
400 do { \
401 if ((x)->num_active > (x)->high_mark) \
402 (x)->high_mark = (x)->num_active; \
403 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404#define STATS_INC_ERR(x) ((x)->errors++)
405#define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
Christoph Lametere498be72005-09-09 13:03:32 -0700406#define STATS_INC_NODEFREES(x) ((x)->node_frees++)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700407#define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800408#define STATS_SET_FREEABLE(x, i) \
409 do { \
410 if ((x)->max_freeable < i) \
411 (x)->max_freeable = i; \
412 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413#define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
414#define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
415#define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
416#define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
417#else
418#define STATS_INC_ACTIVE(x) do { } while (0)
419#define STATS_DEC_ACTIVE(x) do { } while (0)
420#define STATS_INC_ALLOCED(x) do { } while (0)
421#define STATS_INC_GROWN(x) do { } while (0)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700422#define STATS_ADD_REAPED(x,y) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423#define STATS_SET_HIGH(x) do { } while (0)
424#define STATS_INC_ERR(x) do { } while (0)
425#define STATS_INC_NODEALLOCS(x) do { } while (0)
Christoph Lametere498be72005-09-09 13:03:32 -0700426#define STATS_INC_NODEFREES(x) do { } while (0)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700427#define STATS_INC_ACOVERFLOW(x) do { } while (0)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800428#define STATS_SET_FREEABLE(x, i) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429#define STATS_INC_ALLOCHIT(x) do { } while (0)
430#define STATS_INC_ALLOCMISS(x) do { } while (0)
431#define STATS_INC_FREEHIT(x) do { } while (0)
432#define STATS_INC_FREEMISS(x) do { } while (0)
433#endif
434
435#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436
Andrew Mortona737b3e2006-03-22 00:08:11 -0800437/*
438 * memory layout of objects:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 * 0 : objp
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800440 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 * the end of an object is aligned with the end of the real
442 * allocation. Catches writes behind the end of the allocation.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800443 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 * redzone word.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800445 * cachep->obj_offset: The real object.
446 * cachep->buffer_size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
Andrew Mortona737b3e2006-03-22 00:08:11 -0800447 * cachep->buffer_size - 1* BYTES_PER_WORD: last caller address
448 * [BYTES_PER_WORD long]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800450static int obj_offset(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800452 return cachep->obj_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Pekka Enberg343e0d72006-02-01 03:05:50 -0800455static int obj_size(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800457 return cachep->obj_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
David Woodhouseb46b8f12007-05-08 00:22:59 -0700460static unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
462 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
David Woodhouseb46b8f12007-05-08 00:22:59 -0700463 return (unsigned long long*) (objp + obj_offset(cachep) -
464 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
David Woodhouseb46b8f12007-05-08 00:22:59 -0700467static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
470 if (cachep->flags & SLAB_STORE_USER)
David Woodhouseb46b8f12007-05-08 00:22:59 -0700471 return (unsigned long long *)(objp + cachep->buffer_size -
472 sizeof(unsigned long long) -
David Woodhouse87a927c2007-07-04 21:26:44 -0400473 REDZONE_ALIGN);
David Woodhouseb46b8f12007-05-08 00:22:59 -0700474 return (unsigned long long *) (objp + cachep->buffer_size -
475 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
Pekka Enberg343e0d72006-02-01 03:05:50 -0800478static void **dbg_userword(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 BUG_ON(!(cachep->flags & SLAB_STORE_USER));
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800481 return (void **)(objp + cachep->buffer_size - BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482}
483
484#else
485
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800486#define obj_offset(x) 0
487#define obj_size(cachep) (cachep->buffer_size)
David Woodhouseb46b8f12007-05-08 00:22:59 -0700488#define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
489#define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490#define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
491
492#endif
493
Li Zefan0f24f122009-12-11 15:45:30 +0800494#ifdef CONFIG_TRACING
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +0300495size_t slab_buffer_size(struct kmem_cache *cachep)
496{
497 return cachep->buffer_size;
498}
499EXPORT_SYMBOL(slab_buffer_size);
500#endif
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 * Do not go above this order unless 0 objects fit into the slab.
504 */
505#define BREAK_GFP_ORDER_HI 1
506#define BREAK_GFP_ORDER_LO 0
507static int slab_break_gfp_order = BREAK_GFP_ORDER_LO;
508
Andrew Mortona737b3e2006-03-22 00:08:11 -0800509/*
510 * Functions for storing/retrieving the cachep and or slab from the page
511 * allocator. These are used to find the slab an obj belongs to. With kfree(),
512 * these are used to find the cache which an obj belongs to.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 */
Pekka Enberg065d41c2005-11-13 16:06:46 -0800514static inline void page_set_cache(struct page *page, struct kmem_cache *cache)
515{
516 page->lru.next = (struct list_head *)cache;
517}
518
519static inline struct kmem_cache *page_get_cache(struct page *page)
520{
Christoph Lameterd85f3382007-05-06 14:49:39 -0700521 page = compound_head(page);
Pekka Enbergddc2e812006-06-23 02:03:40 -0700522 BUG_ON(!PageSlab(page));
Pekka Enberg065d41c2005-11-13 16:06:46 -0800523 return (struct kmem_cache *)page->lru.next;
524}
525
526static inline void page_set_slab(struct page *page, struct slab *slab)
527{
528 page->lru.prev = (struct list_head *)slab;
529}
530
531static inline struct slab *page_get_slab(struct page *page)
532{
Pekka Enbergddc2e812006-06-23 02:03:40 -0700533 BUG_ON(!PageSlab(page));
Pekka Enberg065d41c2005-11-13 16:06:46 -0800534 return (struct slab *)page->lru.prev;
535}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Pekka Enberg6ed5eb22006-02-01 03:05:49 -0800537static inline struct kmem_cache *virt_to_cache(const void *obj)
538{
Christoph Lameterb49af682007-05-06 14:49:41 -0700539 struct page *page = virt_to_head_page(obj);
Pekka Enberg6ed5eb22006-02-01 03:05:49 -0800540 return page_get_cache(page);
541}
542
543static inline struct slab *virt_to_slab(const void *obj)
544{
Christoph Lameterb49af682007-05-06 14:49:41 -0700545 struct page *page = virt_to_head_page(obj);
Pekka Enberg6ed5eb22006-02-01 03:05:49 -0800546 return page_get_slab(page);
547}
548
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800549static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
550 unsigned int idx)
551{
552 return slab->s_mem + cache->buffer_size * idx;
553}
554
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800555/*
556 * We want to avoid an expensive divide : (offset / cache->buffer_size)
557 * Using the fact that buffer_size is a constant for a particular cache,
558 * we can replace (offset / cache->buffer_size) by
559 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
560 */
561static inline unsigned int obj_to_index(const struct kmem_cache *cache,
562 const struct slab *slab, void *obj)
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800563{
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800564 u32 offset = (obj - slab->s_mem);
565 return reciprocal_divide(offset, cache->reciprocal_buffer_size);
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800566}
567
Andrew Mortona737b3e2006-03-22 00:08:11 -0800568/*
569 * These are the default caches for kmalloc. Custom caches can have other sizes.
570 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571struct cache_sizes malloc_sizes[] = {
572#define CACHE(x) { .cs_size = (x) },
573#include <linux/kmalloc_sizes.h>
574 CACHE(ULONG_MAX)
575#undef CACHE
576};
577EXPORT_SYMBOL(malloc_sizes);
578
579/* Must match cache_sizes above. Out of line to keep cache footprint low. */
580struct cache_names {
581 char *name;
582 char *name_dma;
583};
584
585static struct cache_names __initdata cache_names[] = {
586#define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },
587#include <linux/kmalloc_sizes.h>
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800588 {NULL,}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589#undef CACHE
590};
591
592static struct arraycache_init initarray_cache __initdata =
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800593 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594static struct arraycache_init initarray_generic =
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800595 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597/* internal cache of cache description objs */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800598static struct kmem_cache cache_cache = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800599 .batchcount = 1,
600 .limit = BOOT_CPUCACHE_ENTRIES,
601 .shared = 1,
Pekka Enberg343e0d72006-02-01 03:05:50 -0800602 .buffer_size = sizeof(struct kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800603 .name = "kmem_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604};
605
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700606#define BAD_ALIEN_MAGIC 0x01020304ul
607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 * chicken and egg problem: delay the per-cpu array allocation
610 * until the general caches are up.
611 */
612static enum {
613 NONE,
Christoph Lametere498be72005-09-09 13:03:32 -0700614 PARTIAL_AC,
615 PARTIAL_L3,
Pekka Enberg8429db52009-06-12 15:58:59 +0300616 EARLY,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 FULL
618} g_cpucache_up;
619
Mike Kravetz39d24e62006-05-15 09:44:13 -0700620/*
621 * used by boot code to determine if it can use slab based allocator
622 */
623int slab_is_available(void)
624{
Pekka Enberg8429db52009-06-12 15:58:59 +0300625 return g_cpucache_up >= EARLY;
Mike Kravetz39d24e62006-05-15 09:44:13 -0700626}
627
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200628#ifdef CONFIG_LOCKDEP
629
630/*
631 * Slab sometimes uses the kmalloc slabs to store the slab headers
632 * for other slabs "off slab".
633 * The locking for this is tricky in that it nests within the locks
634 * of all other slabs in a few places; to deal with this special
635 * locking we put on-slab caches into a separate lock-class.
636 *
637 * We set lock class for alien array caches which are up during init.
638 * The lock annotation will be lost if all cpus of a node goes down and
639 * then comes back up during hotplug
640 */
641static struct lock_class_key on_slab_l3_key;
642static struct lock_class_key on_slab_alc_key;
643
644static void init_node_lock_keys(int q)
645{
646 struct cache_sizes *s = malloc_sizes;
647
648 if (g_cpucache_up != FULL)
649 return;
650
651 for (s = malloc_sizes; s->cs_size != ULONG_MAX; s++) {
652 struct array_cache **alc;
653 struct kmem_list3 *l3;
654 int r;
655
656 l3 = s->cs_cachep->nodelists[q];
657 if (!l3 || OFF_SLAB(s->cs_cachep))
Pekka Enberg00afa752009-12-27 14:33:14 +0200658 continue;
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200659 lockdep_set_class(&l3->list_lock, &on_slab_l3_key);
660 alc = l3->alien;
661 /*
662 * FIXME: This check for BAD_ALIEN_MAGIC
663 * should go away when common slab code is taught to
664 * work even without alien caches.
665 * Currently, non NUMA code returns BAD_ALIEN_MAGIC
666 * for alloc_alien_cache,
667 */
668 if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC)
Pekka Enberg00afa752009-12-27 14:33:14 +0200669 continue;
Pekka Enbergce79ddc2009-11-23 22:01:15 +0200670 for_each_node(r) {
671 if (alc[r])
672 lockdep_set_class(&alc[r]->lock,
673 &on_slab_alc_key);
674 }
675 }
676}
677
678static inline void init_lock_keys(void)
679{
680 int node;
681
682 for_each_node(node)
683 init_node_lock_keys(node);
684}
685#else
686static void init_node_lock_keys(int q)
687{
688}
689
690static inline void init_lock_keys(void)
691{
692}
693#endif
694
695/*
696 * Guard access to the cache-chain.
697 */
698static DEFINE_MUTEX(cache_chain_mutex);
699static struct list_head cache_chain;
700
Tejun Heo1871e522009-10-29 22:34:13 +0900701static DEFINE_PER_CPU(struct delayed_work, slab_reap_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Pekka Enberg343e0d72006-02-01 03:05:50 -0800703static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
705 return cachep->array[smp_processor_id()];
706}
707
Andrew Mortona737b3e2006-03-22 00:08:11 -0800708static inline struct kmem_cache *__find_general_cachep(size_t size,
709 gfp_t gfpflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 struct cache_sizes *csizep = malloc_sizes;
712
713#if DEBUG
714 /* This happens if someone tries to call
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800715 * kmem_cache_create(), or __kmalloc(), before
716 * the generic caches are initialized.
717 */
Alok Katariac7e43c72005-09-14 12:17:53 -0700718 BUG_ON(malloc_sizes[INDEX_AC].cs_cachep == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719#endif
Christoph Lameter6cb8f912007-07-17 04:03:22 -0700720 if (!size)
721 return ZERO_SIZE_PTR;
722
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 while (size > csizep->cs_size)
724 csizep++;
725
726 /*
Martin Hicks0abf40c2005-09-03 15:54:54 -0700727 * Really subtle: The last entry with cs->cs_size==ULONG_MAX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 * has cs_{dma,}cachep==NULL. Thus no special case
729 * for large kmalloc calls required.
730 */
Christoph Lameter4b51d662007-02-10 01:43:10 -0800731#ifdef CONFIG_ZONE_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (unlikely(gfpflags & GFP_DMA))
733 return csizep->cs_dmacachep;
Christoph Lameter4b51d662007-02-10 01:43:10 -0800734#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return csizep->cs_cachep;
736}
737
Adrian Bunkb2213852006-09-25 23:31:02 -0700738static struct kmem_cache *kmem_find_general_cachep(size_t size, gfp_t gfpflags)
Manfred Spraul97e2bde2005-05-01 08:58:38 -0700739{
740 return __find_general_cachep(size, gfpflags);
741}
Manfred Spraul97e2bde2005-05-01 08:58:38 -0700742
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800743static size_t slab_mgmt_size(size_t nr_objs, size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744{
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800745 return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align);
746}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Andrew Mortona737b3e2006-03-22 00:08:11 -0800748/*
749 * Calculate the number of objects and left-over bytes for a given buffer size.
750 */
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800751static void cache_estimate(unsigned long gfporder, size_t buffer_size,
752 size_t align, int flags, size_t *left_over,
753 unsigned int *num)
754{
755 int nr_objs;
756 size_t mgmt_size;
757 size_t slab_size = PAGE_SIZE << gfporder;
758
759 /*
760 * The slab management structure can be either off the slab or
761 * on it. For the latter case, the memory allocated for a
762 * slab is used for:
763 *
764 * - The struct slab
765 * - One kmem_bufctl_t for each object
766 * - Padding to respect alignment of @align
767 * - @buffer_size bytes for each object
768 *
769 * If the slab management structure is off the slab, then the
770 * alignment will already be calculated into the size. Because
771 * the slabs are all pages aligned, the objects will be at the
772 * correct alignment when allocated.
773 */
774 if (flags & CFLGS_OFF_SLAB) {
775 mgmt_size = 0;
776 nr_objs = slab_size / buffer_size;
777
778 if (nr_objs > SLAB_LIMIT)
779 nr_objs = SLAB_LIMIT;
780 } else {
781 /*
782 * Ignore padding for the initial guess. The padding
783 * is at most @align-1 bytes, and @buffer_size is at
784 * least @align. In the worst case, this result will
785 * be one greater than the number of objects that fit
786 * into the memory allocation when taking the padding
787 * into account.
788 */
789 nr_objs = (slab_size - sizeof(struct slab)) /
790 (buffer_size + sizeof(kmem_bufctl_t));
791
792 /*
793 * This calculated number will be either the right
794 * amount, or one greater than what we want.
795 */
796 if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
797 > slab_size)
798 nr_objs--;
799
800 if (nr_objs > SLAB_LIMIT)
801 nr_objs = SLAB_LIMIT;
802
803 mgmt_size = slab_mgmt_size(nr_objs, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 }
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800805 *num = nr_objs;
806 *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
Harvey Harrisond40cee22008-04-30 00:55:07 -0700809#define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Andrew Mortona737b3e2006-03-22 00:08:11 -0800811static void __slab_error(const char *function, struct kmem_cache *cachep,
812 char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
814 printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800815 function, cachep->name, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 dump_stack();
817}
818
Paul Menage3395ee02006-12-06 20:32:16 -0800819/*
820 * By default on NUMA we use alien caches to stage the freeing of
821 * objects allocated from other nodes. This causes massive memory
822 * inefficiencies when using fake NUMA setup to split memory into a
823 * large number of small nodes, so it can be disabled on the command
824 * line
825 */
826
827static int use_alien_caches __read_mostly = 1;
828static int __init noaliencache_setup(char *s)
829{
830 use_alien_caches = 0;
831 return 1;
832}
833__setup("noaliencache", noaliencache_setup);
834
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800835#ifdef CONFIG_NUMA
836/*
837 * Special reaping functions for NUMA systems called from cache_reap().
838 * These take care of doing round robin flushing of alien caches (containing
839 * objects freed on different nodes from which they were allocated) and the
840 * flushing of remote pcps by calling drain_node_pages.
841 */
Tejun Heo1871e522009-10-29 22:34:13 +0900842static DEFINE_PER_CPU(unsigned long, slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800843
844static void init_reap_node(int cpu)
845{
846 int node;
847
848 node = next_node(cpu_to_node(cpu), node_online_map);
849 if (node == MAX_NUMNODES)
Paul Jackson442295c2006-03-22 00:09:11 -0800850 node = first_node(node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800851
Tejun Heo1871e522009-10-29 22:34:13 +0900852 per_cpu(slab_reap_node, cpu) = node;
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800853}
854
855static void next_reap_node(void)
856{
Tejun Heo1871e522009-10-29 22:34:13 +0900857 int node = __get_cpu_var(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800858
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800859 node = next_node(node, node_online_map);
860 if (unlikely(node >= MAX_NUMNODES))
861 node = first_node(node_online_map);
Tejun Heo1871e522009-10-29 22:34:13 +0900862 __get_cpu_var(slab_reap_node) = node;
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800863}
864
865#else
866#define init_reap_node(cpu) do { } while (0)
867#define next_reap_node(void) do { } while (0)
868#endif
869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870/*
871 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
872 * via the workqueue/eventd.
873 * Add the CPU number into the expiration time to minimize the possibility of
874 * the CPUs getting into lockstep and contending for the global cache chain
875 * lock.
876 */
Adrian Bunk897e6792007-07-15 23:38:20 -0700877static void __cpuinit start_cpu_timer(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
Tejun Heo1871e522009-10-29 22:34:13 +0900879 struct delayed_work *reap_work = &per_cpu(slab_reap_work, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 /*
882 * When this gets called from do_initcalls via cpucache_init(),
883 * init_workqueues() has already run, so keventd will be setup
884 * at that time.
885 */
David Howells52bad642006-11-22 14:54:01 +0000886 if (keventd_up() && reap_work->work.func == NULL) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800887 init_reap_node(cpu);
David Howells65f27f32006-11-22 14:55:48 +0000888 INIT_DELAYED_WORK(reap_work, cache_reap);
Arjan van de Ven2b284212006-12-10 02:21:28 -0800889 schedule_delayed_work_on(cpu, reap_work,
890 __round_jiffies_relative(HZ, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 }
892}
893
Christoph Lametere498be72005-09-09 13:03:32 -0700894static struct array_cache *alloc_arraycache(int node, int entries,
Pekka Enberg83b519e2009-06-10 19:40:04 +0300895 int batchcount, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800897 int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 struct array_cache *nc = NULL;
899
Pekka Enberg83b519e2009-06-10 19:40:04 +0300900 nc = kmalloc_node(memsize, gfp, node);
Catalin Marinasd5cff632009-06-11 13:22:40 +0100901 /*
902 * The array_cache structures contain pointers to free object.
903 * However, when such objects are allocated or transfered to another
904 * cache the pointers are not cleared and they could be counted as
905 * valid references during a kmemleak scan. Therefore, kmemleak must
906 * not scan such objects.
907 */
908 kmemleak_no_scan(nc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (nc) {
910 nc->avail = 0;
911 nc->limit = entries;
912 nc->batchcount = batchcount;
913 nc->touched = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700914 spin_lock_init(&nc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 }
916 return nc;
917}
918
Christoph Lameter3ded1752006-03-25 03:06:44 -0800919/*
920 * Transfer objects in one arraycache to another.
921 * Locking must be handled by the caller.
922 *
923 * Return the number of entries transferred.
924 */
925static int transfer_objects(struct array_cache *to,
926 struct array_cache *from, unsigned int max)
927{
928 /* Figure out how many entries to transfer */
929 int nr = min(min(from->avail, max), to->limit - to->avail);
930
931 if (!nr)
932 return 0;
933
934 memcpy(to->entry + to->avail, from->entry + from->avail -nr,
935 sizeof(void *) *nr);
936
937 from->avail -= nr;
938 to->avail += nr;
Christoph Lameter3ded1752006-03-25 03:06:44 -0800939 return nr;
940}
941
Christoph Lameter765c4502006-09-27 01:50:08 -0700942#ifndef CONFIG_NUMA
943
944#define drain_alien_cache(cachep, alien) do { } while (0)
945#define reap_alien(cachep, l3) do { } while (0)
946
Pekka Enberg83b519e2009-06-10 19:40:04 +0300947static inline struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
Christoph Lameter765c4502006-09-27 01:50:08 -0700948{
949 return (struct array_cache **)BAD_ALIEN_MAGIC;
950}
951
952static inline void free_alien_cache(struct array_cache **ac_ptr)
953{
954}
955
956static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
957{
958 return 0;
959}
960
961static inline void *alternate_node_alloc(struct kmem_cache *cachep,
962 gfp_t flags)
963{
964 return NULL;
965}
966
Christoph Hellwig8b98c162006-12-06 20:32:30 -0800967static inline void *____cache_alloc_node(struct kmem_cache *cachep,
Christoph Lameter765c4502006-09-27 01:50:08 -0700968 gfp_t flags, int nodeid)
969{
970 return NULL;
971}
972
973#else /* CONFIG_NUMA */
974
Christoph Hellwig8b98c162006-12-06 20:32:30 -0800975static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
Paul Jacksonc61afb12006-03-24 03:16:08 -0800976static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
Christoph Lameterdc85da12006-01-18 17:42:36 -0800977
Pekka Enberg83b519e2009-06-10 19:40:04 +0300978static struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -0700979{
980 struct array_cache **ac_ptr;
Christoph Lameter8ef82862007-02-20 13:57:52 -0800981 int memsize = sizeof(void *) * nr_node_ids;
Christoph Lametere498be72005-09-09 13:03:32 -0700982 int i;
983
984 if (limit > 1)
985 limit = 12;
Haicheng Lif3186a92010-01-06 15:25:23 +0800986 ac_ptr = kzalloc_node(memsize, gfp, node);
Christoph Lametere498be72005-09-09 13:03:32 -0700987 if (ac_ptr) {
988 for_each_node(i) {
Haicheng Lif3186a92010-01-06 15:25:23 +0800989 if (i == node || !node_online(i))
Christoph Lametere498be72005-09-09 13:03:32 -0700990 continue;
Pekka Enberg83b519e2009-06-10 19:40:04 +0300991 ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d, gfp);
Christoph Lametere498be72005-09-09 13:03:32 -0700992 if (!ac_ptr[i]) {
Akinobu Mitacc550de2007-11-14 16:58:35 -0800993 for (i--; i >= 0; i--)
Christoph Lametere498be72005-09-09 13:03:32 -0700994 kfree(ac_ptr[i]);
995 kfree(ac_ptr);
996 return NULL;
997 }
998 }
999 }
1000 return ac_ptr;
1001}
1002
Pekka Enberg5295a742006-02-01 03:05:48 -08001003static void free_alien_cache(struct array_cache **ac_ptr)
Christoph Lametere498be72005-09-09 13:03:32 -07001004{
1005 int i;
1006
1007 if (!ac_ptr)
1008 return;
Christoph Lametere498be72005-09-09 13:03:32 -07001009 for_each_node(i)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001010 kfree(ac_ptr[i]);
Christoph Lametere498be72005-09-09 13:03:32 -07001011 kfree(ac_ptr);
1012}
1013
Pekka Enberg343e0d72006-02-01 03:05:50 -08001014static void __drain_alien_cache(struct kmem_cache *cachep,
Pekka Enberg5295a742006-02-01 03:05:48 -08001015 struct array_cache *ac, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07001016{
1017 struct kmem_list3 *rl3 = cachep->nodelists[node];
1018
1019 if (ac->avail) {
1020 spin_lock(&rl3->list_lock);
Christoph Lametere00946f2006-03-25 03:06:45 -08001021 /*
1022 * Stuff objects into the remote nodes shared array first.
1023 * That way we could avoid the overhead of putting the objects
1024 * into the free lists and getting them back later.
1025 */
shin, jacob693f7d32006-04-28 10:54:37 -05001026 if (rl3->shared)
1027 transfer_objects(rl3->shared, ac, ac->limit);
Christoph Lametere00946f2006-03-25 03:06:45 -08001028
Christoph Lameterff694162005-09-22 21:44:02 -07001029 free_block(cachep, ac->entry, ac->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001030 ac->avail = 0;
1031 spin_unlock(&rl3->list_lock);
1032 }
1033}
1034
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001035/*
1036 * Called from cache_reap() to regularly drain alien caches round robin.
1037 */
1038static void reap_alien(struct kmem_cache *cachep, struct kmem_list3 *l3)
1039{
Tejun Heo1871e522009-10-29 22:34:13 +09001040 int node = __get_cpu_var(slab_reap_node);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001041
1042 if (l3->alien) {
1043 struct array_cache *ac = l3->alien[node];
Christoph Lametere00946f2006-03-25 03:06:45 -08001044
1045 if (ac && ac->avail && spin_trylock_irq(&ac->lock)) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001046 __drain_alien_cache(cachep, ac, node);
1047 spin_unlock_irq(&ac->lock);
1048 }
1049 }
1050}
1051
Andrew Mortona737b3e2006-03-22 00:08:11 -08001052static void drain_alien_cache(struct kmem_cache *cachep,
1053 struct array_cache **alien)
Christoph Lametere498be72005-09-09 13:03:32 -07001054{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001055 int i = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07001056 struct array_cache *ac;
1057 unsigned long flags;
1058
1059 for_each_online_node(i) {
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001060 ac = alien[i];
Christoph Lametere498be72005-09-09 13:03:32 -07001061 if (ac) {
1062 spin_lock_irqsave(&ac->lock, flags);
1063 __drain_alien_cache(cachep, ac, i);
1064 spin_unlock_irqrestore(&ac->lock, flags);
1065 }
1066 }
1067}
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001068
Ingo Molnar873623d2006-07-13 14:44:38 +02001069static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001070{
1071 struct slab *slabp = virt_to_slab(objp);
1072 int nodeid = slabp->nodeid;
1073 struct kmem_list3 *l3;
1074 struct array_cache *alien = NULL;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001075 int node;
1076
1077 node = numa_node_id();
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001078
1079 /*
1080 * Make sure we are not freeing a object from another node to the array
1081 * cache on this cpu.
1082 */
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001083 if (likely(slabp->nodeid == node))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001084 return 0;
1085
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001086 l3 = cachep->nodelists[node];
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001087 STATS_INC_NODEFREES(cachep);
1088 if (l3->alien && l3->alien[nodeid]) {
1089 alien = l3->alien[nodeid];
Ingo Molnar873623d2006-07-13 14:44:38 +02001090 spin_lock(&alien->lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001091 if (unlikely(alien->avail == alien->limit)) {
1092 STATS_INC_ACOVERFLOW(cachep);
1093 __drain_alien_cache(cachep, alien, nodeid);
1094 }
1095 alien->entry[alien->avail++] = objp;
1096 spin_unlock(&alien->lock);
1097 } else {
1098 spin_lock(&(cachep->nodelists[nodeid])->list_lock);
1099 free_block(cachep, &objp, 1, nodeid);
1100 spin_unlock(&(cachep->nodelists[nodeid])->list_lock);
1101 }
1102 return 1;
1103}
Christoph Lametere498be72005-09-09 13:03:32 -07001104#endif
1105
David Rientjes8f9f8d92010-03-27 19:40:47 -07001106/*
1107 * Allocates and initializes nodelists for a node on each slab cache, used for
1108 * either memory or cpu hotplug. If memory is being hot-added, the kmem_list3
1109 * will be allocated off-node since memory is not yet online for the new node.
1110 * When hotplugging memory or a cpu, existing nodelists are not replaced if
1111 * already in use.
1112 *
1113 * Must hold cache_chain_mutex.
1114 */
1115static int init_cache_nodelists_node(int node)
1116{
1117 struct kmem_cache *cachep;
1118 struct kmem_list3 *l3;
1119 const int memsize = sizeof(struct kmem_list3);
1120
1121 list_for_each_entry(cachep, &cache_chain, next) {
1122 /*
1123 * Set up the size64 kmemlist for cpu before we can
1124 * begin anything. Make sure some other cpu on this
1125 * node has not already allocated this
1126 */
1127 if (!cachep->nodelists[node]) {
1128 l3 = kmalloc_node(memsize, GFP_KERNEL, node);
1129 if (!l3)
1130 return -ENOMEM;
1131 kmem_list3_init(l3);
1132 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
1133 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1134
1135 /*
1136 * The l3s don't come and go as CPUs come and
1137 * go. cache_chain_mutex is sufficient
1138 * protection here.
1139 */
1140 cachep->nodelists[node] = l3;
1141 }
1142
1143 spin_lock_irq(&cachep->nodelists[node]->list_lock);
1144 cachep->nodelists[node]->free_limit =
1145 (1 + nr_cpus_node(node)) *
1146 cachep->batchcount + cachep->num;
1147 spin_unlock_irq(&cachep->nodelists[node]->list_lock);
1148 }
1149 return 0;
1150}
1151
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001152static void __cpuinit cpuup_canceled(long cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001154 struct kmem_cache *cachep;
1155 struct kmem_list3 *l3 = NULL;
1156 int node = cpu_to_node(cpu);
Rusty Russella70f7302009-03-13 14:49:46 +10301157 const struct cpumask *mask = cpumask_of_node(node);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001158
1159 list_for_each_entry(cachep, &cache_chain, next) {
1160 struct array_cache *nc;
1161 struct array_cache *shared;
1162 struct array_cache **alien;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001163
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001164 /* cpu is dead; no one can alloc from it. */
1165 nc = cachep->array[cpu];
1166 cachep->array[cpu] = NULL;
1167 l3 = cachep->nodelists[node];
1168
1169 if (!l3)
1170 goto free_array_cache;
1171
1172 spin_lock_irq(&l3->list_lock);
1173
1174 /* Free limit for this kmem_list3 */
1175 l3->free_limit -= cachep->batchcount;
1176 if (nc)
1177 free_block(cachep, nc->entry, nc->avail, node);
1178
Rusty Russell58463c12009-12-17 11:43:12 -06001179 if (!cpumask_empty(mask)) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001180 spin_unlock_irq(&l3->list_lock);
1181 goto free_array_cache;
1182 }
1183
1184 shared = l3->shared;
1185 if (shared) {
1186 free_block(cachep, shared->entry,
1187 shared->avail, node);
1188 l3->shared = NULL;
1189 }
1190
1191 alien = l3->alien;
1192 l3->alien = NULL;
1193
1194 spin_unlock_irq(&l3->list_lock);
1195
1196 kfree(shared);
1197 if (alien) {
1198 drain_alien_cache(cachep, alien);
1199 free_alien_cache(alien);
1200 }
1201free_array_cache:
1202 kfree(nc);
1203 }
1204 /*
1205 * In the previous loop, all the objects were freed to
1206 * the respective cache's slabs, now we can go ahead and
1207 * shrink each nodelist to its limit.
1208 */
1209 list_for_each_entry(cachep, &cache_chain, next) {
1210 l3 = cachep->nodelists[node];
1211 if (!l3)
1212 continue;
1213 drain_freelist(cachep, l3, l3->free_objects);
1214 }
1215}
1216
1217static int __cpuinit cpuup_prepare(long cpu)
1218{
Pekka Enberg343e0d72006-02-01 03:05:50 -08001219 struct kmem_cache *cachep;
Christoph Lametere498be72005-09-09 13:03:32 -07001220 struct kmem_list3 *l3 = NULL;
1221 int node = cpu_to_node(cpu);
David Rientjes8f9f8d92010-03-27 19:40:47 -07001222 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001224 /*
1225 * We need to do this right in the beginning since
1226 * alloc_arraycache's are going to use this list.
1227 * kmalloc_node allows us to add the slab to the right
1228 * kmem_list3 and not this cpu's kmem_list3
1229 */
David Rientjes8f9f8d92010-03-27 19:40:47 -07001230 err = init_cache_nodelists_node(node);
1231 if (err < 0)
1232 goto bad;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001233
1234 /*
1235 * Now we can go ahead with allocating the shared arrays and
1236 * array caches
1237 */
1238 list_for_each_entry(cachep, &cache_chain, next) {
1239 struct array_cache *nc;
1240 struct array_cache *shared = NULL;
1241 struct array_cache **alien = NULL;
1242
1243 nc = alloc_arraycache(node, cachep->limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001244 cachep->batchcount, GFP_KERNEL);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001245 if (!nc)
1246 goto bad;
1247 if (cachep->shared) {
1248 shared = alloc_arraycache(node,
1249 cachep->shared * cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001250 0xbaadf00d, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001251 if (!shared) {
1252 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001253 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001254 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001255 }
1256 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03001257 alien = alloc_alien_cache(node, cachep->limit, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001258 if (!alien) {
1259 kfree(shared);
1260 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001261 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001262 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001263 }
1264 cachep->array[cpu] = nc;
1265 l3 = cachep->nodelists[node];
1266 BUG_ON(!l3);
1267
1268 spin_lock_irq(&l3->list_lock);
1269 if (!l3->shared) {
1270 /*
1271 * We are serialised from CPU_DEAD or
1272 * CPU_UP_CANCELLED by the cpucontrol lock
1273 */
1274 l3->shared = shared;
1275 shared = NULL;
1276 }
1277#ifdef CONFIG_NUMA
1278 if (!l3->alien) {
1279 l3->alien = alien;
1280 alien = NULL;
1281 }
1282#endif
1283 spin_unlock_irq(&l3->list_lock);
1284 kfree(shared);
1285 free_alien_cache(alien);
1286 }
Pekka Enbergce79ddc2009-11-23 22:01:15 +02001287 init_node_lock_keys(node);
1288
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001289 return 0;
1290bad:
Akinobu Mita12d00f62007-10-18 03:05:11 -07001291 cpuup_canceled(cpu);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001292 return -ENOMEM;
1293}
1294
1295static int __cpuinit cpuup_callback(struct notifier_block *nfb,
1296 unsigned long action, void *hcpu)
1297{
1298 long cpu = (long)hcpu;
1299 int err = 0;
1300
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 switch (action) {
Heiko Carstens38c3bd92007-05-09 02:34:05 -07001302 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001303 case CPU_UP_PREPARE_FROZEN:
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001304 mutex_lock(&cache_chain_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001305 err = cpuup_prepare(cpu);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001306 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 break;
1308 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001309 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 start_cpu_timer(cpu);
1311 break;
1312#ifdef CONFIG_HOTPLUG_CPU
Christoph Lameter5830c592007-05-09 02:34:22 -07001313 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001314 case CPU_DOWN_PREPARE_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001315 /*
1316 * Shutdown cache reaper. Note that the cache_chain_mutex is
1317 * held so that if cache_reap() is invoked it cannot do
1318 * anything expensive but will only modify reap_work
1319 * and reschedule the timer.
1320 */
Tejun Heo1871e522009-10-29 22:34:13 +09001321 cancel_rearming_delayed_work(&per_cpu(slab_reap_work, cpu));
Christoph Lameter5830c592007-05-09 02:34:22 -07001322 /* Now the cache_reaper is guaranteed to be not running. */
Tejun Heo1871e522009-10-29 22:34:13 +09001323 per_cpu(slab_reap_work, cpu).work.func = NULL;
Christoph Lameter5830c592007-05-09 02:34:22 -07001324 break;
1325 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001326 case CPU_DOWN_FAILED_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001327 start_cpu_timer(cpu);
1328 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001330 case CPU_DEAD_FROZEN:
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001331 /*
1332 * Even if all the cpus of a node are down, we don't free the
1333 * kmem_list3 of any cache. This to avoid a race between
1334 * cpu_down, and a kmalloc allocation from another cpu for
1335 * memory from the node of the cpu going down. The list3
1336 * structure is usually allocated from kmem_cache_create() and
1337 * gets destroyed at kmem_cache_destroy().
1338 */
Simon Arlott183ff222007-10-20 01:27:18 +02001339 /* fall through */
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08001340#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001342 case CPU_UP_CANCELED_FROZEN:
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001343 mutex_lock(&cache_chain_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001344 cpuup_canceled(cpu);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001345 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001348 return err ? NOTIFY_BAD : NOTIFY_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349}
1350
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001351static struct notifier_block __cpuinitdata cpucache_notifier = {
1352 &cpuup_callback, NULL, 0
1353};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
David Rientjes8f9f8d92010-03-27 19:40:47 -07001355#if defined(CONFIG_NUMA) && defined(CONFIG_MEMORY_HOTPLUG)
1356/*
1357 * Drains freelist for a node on each slab cache, used for memory hot-remove.
1358 * Returns -EBUSY if all objects cannot be drained so that the node is not
1359 * removed.
1360 *
1361 * Must hold cache_chain_mutex.
1362 */
1363static int __meminit drain_cache_nodelists_node(int node)
1364{
1365 struct kmem_cache *cachep;
1366 int ret = 0;
1367
1368 list_for_each_entry(cachep, &cache_chain, next) {
1369 struct kmem_list3 *l3;
1370
1371 l3 = cachep->nodelists[node];
1372 if (!l3)
1373 continue;
1374
1375 drain_freelist(cachep, l3, l3->free_objects);
1376
1377 if (!list_empty(&l3->slabs_full) ||
1378 !list_empty(&l3->slabs_partial)) {
1379 ret = -EBUSY;
1380 break;
1381 }
1382 }
1383 return ret;
1384}
1385
1386static int __meminit slab_memory_callback(struct notifier_block *self,
1387 unsigned long action, void *arg)
1388{
1389 struct memory_notify *mnb = arg;
1390 int ret = 0;
1391 int nid;
1392
1393 nid = mnb->status_change_nid;
1394 if (nid < 0)
1395 goto out;
1396
1397 switch (action) {
1398 case MEM_GOING_ONLINE:
1399 mutex_lock(&cache_chain_mutex);
1400 ret = init_cache_nodelists_node(nid);
1401 mutex_unlock(&cache_chain_mutex);
1402 break;
1403 case MEM_GOING_OFFLINE:
1404 mutex_lock(&cache_chain_mutex);
1405 ret = drain_cache_nodelists_node(nid);
1406 mutex_unlock(&cache_chain_mutex);
1407 break;
1408 case MEM_ONLINE:
1409 case MEM_OFFLINE:
1410 case MEM_CANCEL_ONLINE:
1411 case MEM_CANCEL_OFFLINE:
1412 break;
1413 }
1414out:
1415 return ret ? notifier_from_errno(ret) : NOTIFY_OK;
1416}
1417#endif /* CONFIG_NUMA && CONFIG_MEMORY_HOTPLUG */
1418
Christoph Lametere498be72005-09-09 13:03:32 -07001419/*
1420 * swap the static kmem_list3 with kmalloced memory
1421 */
David Rientjes8f9f8d92010-03-27 19:40:47 -07001422static void __init init_list(struct kmem_cache *cachep, struct kmem_list3 *list,
1423 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07001424{
1425 struct kmem_list3 *ptr;
1426
Pekka Enberg83b519e2009-06-10 19:40:04 +03001427 ptr = kmalloc_node(sizeof(struct kmem_list3), GFP_NOWAIT, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07001428 BUG_ON(!ptr);
1429
Christoph Lametere498be72005-09-09 13:03:32 -07001430 memcpy(ptr, list, sizeof(struct kmem_list3));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001431 /*
1432 * Do not assume that spinlocks can be initialized via memcpy:
1433 */
1434 spin_lock_init(&ptr->list_lock);
1435
Christoph Lametere498be72005-09-09 13:03:32 -07001436 MAKE_ALL_LISTS(cachep, ptr, nodeid);
1437 cachep->nodelists[nodeid] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001438}
1439
Andrew Mortona737b3e2006-03-22 00:08:11 -08001440/*
Pekka Enberg556a1692008-01-25 08:20:51 +02001441 * For setting up all the kmem_list3s for cache whose buffer_size is same as
1442 * size of kmem_list3.
1443 */
1444static void __init set_up_list3s(struct kmem_cache *cachep, int index)
1445{
1446 int node;
1447
1448 for_each_online_node(node) {
1449 cachep->nodelists[node] = &initkmem_list3[index + node];
1450 cachep->nodelists[node]->next_reap = jiffies +
1451 REAPTIMEOUT_LIST3 +
1452 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1453 }
1454}
1455
1456/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08001457 * Initialisation. Called after the page allocator have been initialised and
1458 * before smp_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459 */
1460void __init kmem_cache_init(void)
1461{
1462 size_t left_over;
1463 struct cache_sizes *sizes;
1464 struct cache_names *names;
Christoph Lametere498be72005-09-09 13:03:32 -07001465 int i;
Jack Steiner07ed76b2006-03-07 21:55:46 -08001466 int order;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001467 int node;
Christoph Lametere498be72005-09-09 13:03:32 -07001468
Mel Gormanb6e68bc2009-06-16 15:32:16 -07001469 if (num_possible_nodes() == 1)
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001470 use_alien_caches = 0;
1471
Christoph Lametere498be72005-09-09 13:03:32 -07001472 for (i = 0; i < NUM_INIT_LISTS; i++) {
1473 kmem_list3_init(&initkmem_list3[i]);
1474 if (i < MAX_NUMNODES)
1475 cache_cache.nodelists[i] = NULL;
1476 }
Pekka Enberg556a1692008-01-25 08:20:51 +02001477 set_up_list3s(&cache_cache, CACHE_CACHE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
1479 /*
1480 * Fragmentation resistance on low memory - only use bigger
1481 * page orders on machines with more than 32MB of memory.
1482 */
Jan Beulich44813742009-09-21 17:03:05 -07001483 if (totalram_pages > (32 << 20) >> PAGE_SHIFT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 slab_break_gfp_order = BREAK_GFP_ORDER_HI;
1485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 /* Bootstrap is tricky, because several objects are allocated
1487 * from caches that do not exist yet:
Andrew Mortona737b3e2006-03-22 00:08:11 -08001488 * 1) initialize the cache_cache cache: it contains the struct
1489 * kmem_cache structures of all caches, except cache_cache itself:
1490 * cache_cache is statically allocated.
Christoph Lametere498be72005-09-09 13:03:32 -07001491 * Initially an __init data area is used for the head array and the
1492 * kmem_list3 structures, it's replaced with a kmalloc allocated
1493 * array at the end of the bootstrap.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 * 2) Create the first kmalloc cache.
Pekka Enberg343e0d72006-02-01 03:05:50 -08001495 * The struct kmem_cache for the new cache is allocated normally.
Christoph Lametere498be72005-09-09 13:03:32 -07001496 * An __init data area is used for the head array.
1497 * 3) Create the remaining kmalloc caches, with minimally sized
1498 * head arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 * 4) Replace the __init data head arrays for cache_cache and the first
1500 * kmalloc cache with kmalloc allocated arrays.
Christoph Lametere498be72005-09-09 13:03:32 -07001501 * 5) Replace the __init data for kmem_list3 for cache_cache and
1502 * the other cache's with kmalloc allocated memory.
1503 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 */
1505
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001506 node = numa_node_id();
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 /* 1) create the cache_cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 INIT_LIST_HEAD(&cache_chain);
1510 list_add(&cache_cache.next, &cache_chain);
1511 cache_cache.colour_off = cache_line_size();
1512 cache_cache.array[smp_processor_id()] = &initarray_cache.cache;
Daniel Yeisleyec1f5ee2008-03-25 23:59:08 +02001513 cache_cache.nodelists[node] = &initkmem_list3[CACHE_CACHE + node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Eric Dumazet8da34302007-05-06 14:49:29 -07001515 /*
1516 * struct kmem_cache size depends on nr_node_ids, which
1517 * can be less than MAX_NUMNODES.
1518 */
1519 cache_cache.buffer_size = offsetof(struct kmem_cache, nodelists) +
1520 nr_node_ids * sizeof(struct kmem_list3 *);
1521#if DEBUG
1522 cache_cache.obj_size = cache_cache.buffer_size;
1523#endif
Andrew Mortona737b3e2006-03-22 00:08:11 -08001524 cache_cache.buffer_size = ALIGN(cache_cache.buffer_size,
1525 cache_line_size());
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08001526 cache_cache.reciprocal_buffer_size =
1527 reciprocal_value(cache_cache.buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528
Jack Steiner07ed76b2006-03-07 21:55:46 -08001529 for (order = 0; order < MAX_ORDER; order++) {
1530 cache_estimate(order, cache_cache.buffer_size,
1531 cache_line_size(), 0, &left_over, &cache_cache.num);
1532 if (cache_cache.num)
1533 break;
1534 }
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02001535 BUG_ON(!cache_cache.num);
Jack Steiner07ed76b2006-03-07 21:55:46 -08001536 cache_cache.gfporder = order;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001537 cache_cache.colour = left_over / cache_cache.colour_off;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001538 cache_cache.slab_size = ALIGN(cache_cache.num * sizeof(kmem_bufctl_t) +
1539 sizeof(struct slab), cache_line_size());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 /* 2+3) create the kmalloc caches */
1542 sizes = malloc_sizes;
1543 names = cache_names;
1544
Andrew Mortona737b3e2006-03-22 00:08:11 -08001545 /*
1546 * Initialize the caches that provide memory for the array cache and the
1547 * kmem_list3 structures first. Without this, further allocations will
1548 * bug.
Christoph Lametere498be72005-09-09 13:03:32 -07001549 */
1550
1551 sizes[INDEX_AC].cs_cachep = kmem_cache_create(names[INDEX_AC].name,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001552 sizes[INDEX_AC].cs_size,
1553 ARCH_KMALLOC_MINALIGN,
1554 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09001555 NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07001556
Andrew Mortona737b3e2006-03-22 00:08:11 -08001557 if (INDEX_AC != INDEX_L3) {
Christoph Lametere498be72005-09-09 13:03:32 -07001558 sizes[INDEX_L3].cs_cachep =
Andrew Mortona737b3e2006-03-22 00:08:11 -08001559 kmem_cache_create(names[INDEX_L3].name,
1560 sizes[INDEX_L3].cs_size,
1561 ARCH_KMALLOC_MINALIGN,
1562 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09001563 NULL);
Andrew Mortona737b3e2006-03-22 00:08:11 -08001564 }
Christoph Lametere498be72005-09-09 13:03:32 -07001565
Ingo Molnare0a42722006-06-23 02:03:46 -07001566 slab_early_init = 0;
1567
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 while (sizes->cs_size != ULONG_MAX) {
Christoph Lametere498be72005-09-09 13:03:32 -07001569 /*
1570 * For performance, all the general caches are L1 aligned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 * This should be particularly beneficial on SMP boxes, as it
1572 * eliminates "false sharing".
1573 * Note for systems short on memory removing the alignment will
Christoph Lametere498be72005-09-09 13:03:32 -07001574 * allow tighter packing of the smaller caches.
1575 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001576 if (!sizes->cs_cachep) {
Christoph Lametere498be72005-09-09 13:03:32 -07001577 sizes->cs_cachep = kmem_cache_create(names->name,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001578 sizes->cs_size,
1579 ARCH_KMALLOC_MINALIGN,
1580 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09001581 NULL);
Andrew Mortona737b3e2006-03-22 00:08:11 -08001582 }
Christoph Lameter4b51d662007-02-10 01:43:10 -08001583#ifdef CONFIG_ZONE_DMA
1584 sizes->cs_dmacachep = kmem_cache_create(
1585 names->name_dma,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001586 sizes->cs_size,
1587 ARCH_KMALLOC_MINALIGN,
1588 ARCH_KMALLOC_FLAGS|SLAB_CACHE_DMA|
1589 SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09001590 NULL);
Christoph Lameter4b51d662007-02-10 01:43:10 -08001591#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 sizes++;
1593 names++;
1594 }
1595 /* 4) Replace the bootstrap head arrays */
1596 {
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001597 struct array_cache *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001598
Pekka Enberg83b519e2009-06-10 19:40:04 +03001599 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001600
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001601 BUG_ON(cpu_cache_get(&cache_cache) != &initarray_cache.cache);
1602 memcpy(ptr, cpu_cache_get(&cache_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001603 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001604 /*
1605 * Do not assume that spinlocks can be initialized via memcpy:
1606 */
1607 spin_lock_init(&ptr->lock);
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 cache_cache.array[smp_processor_id()] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001610
Pekka Enberg83b519e2009-06-10 19:40:04 +03001611 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001612
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001613 BUG_ON(cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001614 != &initarray_generic.cache);
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001615 memcpy(ptr, cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001616 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001617 /*
1618 * Do not assume that spinlocks can be initialized via memcpy:
1619 */
1620 spin_lock_init(&ptr->lock);
1621
Christoph Lametere498be72005-09-09 13:03:32 -07001622 malloc_sizes[INDEX_AC].cs_cachep->array[smp_processor_id()] =
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001623 ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
Christoph Lametere498be72005-09-09 13:03:32 -07001625 /* 5) Replace the bootstrap kmem_list3's */
1626 {
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001627 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Mel Gorman9c09a952008-01-24 05:49:54 -08001629 for_each_online_node(nid) {
Daniel Yeisleyec1f5ee2008-03-25 23:59:08 +02001630 init_list(&cache_cache, &initkmem_list3[CACHE_CACHE + nid], nid);
Pekka Enberg556a1692008-01-25 08:20:51 +02001631
Christoph Lametere498be72005-09-09 13:03:32 -07001632 init_list(malloc_sizes[INDEX_AC].cs_cachep,
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001633 &initkmem_list3[SIZE_AC + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001634
1635 if (INDEX_AC != INDEX_L3) {
1636 init_list(malloc_sizes[INDEX_L3].cs_cachep,
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001637 &initkmem_list3[SIZE_L3 + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001638 }
1639 }
1640 }
1641
Pekka Enberg8429db52009-06-12 15:58:59 +03001642 g_cpucache_up = EARLY;
Pekka Enberg8429db52009-06-12 15:58:59 +03001643}
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001644
Pekka Enberg8429db52009-06-12 15:58:59 +03001645void __init kmem_cache_init_late(void)
1646{
1647 struct kmem_cache *cachep;
1648
Pekka Enberg8429db52009-06-12 15:58:59 +03001649 /* 6) resize the head arrays to their final sizes */
1650 mutex_lock(&cache_chain_mutex);
1651 list_for_each_entry(cachep, &cache_chain, next)
1652 if (enable_cpucache(cachep, GFP_NOWAIT))
1653 BUG();
1654 mutex_unlock(&cache_chain_mutex);
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 /* Done! */
1657 g_cpucache_up = FULL;
1658
Pekka Enbergec5a36f2009-06-29 09:57:10 +03001659 /* Annotate slab for lockdep -- annotate the malloc caches */
1660 init_lock_keys();
1661
Andrew Mortona737b3e2006-03-22 00:08:11 -08001662 /*
1663 * Register a cpu startup notifier callback that initializes
1664 * cpu_cache_get for all new cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 */
1666 register_cpu_notifier(&cpucache_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
David Rientjes8f9f8d92010-03-27 19:40:47 -07001668#ifdef CONFIG_NUMA
1669 /*
1670 * Register a memory hotplug callback that initializes and frees
1671 * nodelists.
1672 */
1673 hotplug_memory_notifier(slab_memory_callback, SLAB_CALLBACK_PRI);
1674#endif
1675
Andrew Mortona737b3e2006-03-22 00:08:11 -08001676 /*
1677 * The reap timers are started later, with a module init call: That part
1678 * of the kernel is not yet operational.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 */
1680}
1681
1682static int __init cpucache_init(void)
1683{
1684 int cpu;
1685
Andrew Mortona737b3e2006-03-22 00:08:11 -08001686 /*
1687 * Register the timers that return unneeded pages to the page allocator
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 */
Christoph Lametere498be72005-09-09 13:03:32 -07001689 for_each_online_cpu(cpu)
Andrew Mortona737b3e2006-03-22 00:08:11 -08001690 start_cpu_timer(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 return 0;
1692}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693__initcall(cpucache_init);
1694
1695/*
1696 * Interface to system's page allocator. No need to hold the cache-lock.
1697 *
1698 * If we requested dmaable memory, we will get it. Even if we
1699 * did not request dmaable memory, we might get it, but that
1700 * would be relatively rare and ignorable.
1701 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001702static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703{
1704 struct page *page;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001705 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 int i;
1707
Luke Yangd6fef9d2006-04-10 22:52:56 -07001708#ifndef CONFIG_MMU
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001709 /*
1710 * Nommu uses slab's for process anonymous memory allocations, and thus
1711 * requires __GFP_COMP to properly refcount higher order allocations
Luke Yangd6fef9d2006-04-10 22:52:56 -07001712 */
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001713 flags |= __GFP_COMP;
Luke Yangd6fef9d2006-04-10 22:52:56 -07001714#endif
Christoph Lameter765c4502006-09-27 01:50:08 -07001715
Christoph Lameter3c517a62006-12-06 20:33:29 -08001716 flags |= cachep->gfpflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001717 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1718 flags |= __GFP_RECLAIMABLE;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001719
Linus Torvalds517d0862009-06-16 19:50:13 -07001720 page = alloc_pages_exact_node(nodeid, flags | __GFP_NOTRACK, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 if (!page)
1722 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001724 nr_pages = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
Christoph Lameter972d1a72006-09-25 23:31:51 -07001726 add_zone_page_state(page_zone(page),
1727 NR_SLAB_RECLAIMABLE, nr_pages);
1728 else
1729 add_zone_page_state(page_zone(page),
1730 NR_SLAB_UNRECLAIMABLE, nr_pages);
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001731 for (i = 0; i < nr_pages; i++)
1732 __SetPageSlab(page + i);
Pekka Enbergc175eea2008-05-09 20:35:53 +02001733
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001734 if (kmemcheck_enabled && !(cachep->flags & SLAB_NOTRACK)) {
1735 kmemcheck_alloc_shadow(page, cachep->gfporder, flags, nodeid);
1736
1737 if (cachep->ctor)
1738 kmemcheck_mark_uninitialized_pages(page, nr_pages);
1739 else
1740 kmemcheck_mark_unallocated_pages(page, nr_pages);
1741 }
Pekka Enbergc175eea2008-05-09 20:35:53 +02001742
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001743 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
1745
1746/*
1747 * Interface to system's page release.
1748 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001749static void kmem_freepages(struct kmem_cache *cachep, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001751 unsigned long i = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 struct page *page = virt_to_page(addr);
1753 const unsigned long nr_freed = i;
1754
Vegard Nossumb1eeab62008-11-25 16:55:53 +01001755 kmemcheck_free_shadow(page, cachep->gfporder);
Pekka Enbergc175eea2008-05-09 20:35:53 +02001756
Christoph Lameter972d1a72006-09-25 23:31:51 -07001757 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1758 sub_zone_page_state(page_zone(page),
1759 NR_SLAB_RECLAIMABLE, nr_freed);
1760 else
1761 sub_zone_page_state(page_zone(page),
1762 NR_SLAB_UNRECLAIMABLE, nr_freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 while (i--) {
Nick Pigginf205b2f2006-03-22 00:08:02 -08001764 BUG_ON(!PageSlab(page));
1765 __ClearPageSlab(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 page++;
1767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 if (current->reclaim_state)
1769 current->reclaim_state->reclaimed_slab += nr_freed;
1770 free_pages((unsigned long)addr, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
1773static void kmem_rcu_free(struct rcu_head *head)
1774{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001775 struct slab_rcu *slab_rcu = (struct slab_rcu *)head;
Pekka Enberg343e0d72006-02-01 03:05:50 -08001776 struct kmem_cache *cachep = slab_rcu->cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
1778 kmem_freepages(cachep, slab_rcu->addr);
1779 if (OFF_SLAB(cachep))
1780 kmem_cache_free(cachep->slabp_cache, slab_rcu);
1781}
1782
1783#if DEBUG
1784
1785#ifdef CONFIG_DEBUG_PAGEALLOC
Pekka Enberg343e0d72006-02-01 03:05:50 -08001786static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001787 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001789 int size = obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001791 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001793 if (size < 5 * sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 return;
1795
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001796 *addr++ = 0x12345678;
1797 *addr++ = caller;
1798 *addr++ = smp_processor_id();
1799 size -= 3 * sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 {
1801 unsigned long *sptr = &caller;
1802 unsigned long svalue;
1803
1804 while (!kstack_end(sptr)) {
1805 svalue = *sptr++;
1806 if (kernel_text_address(svalue)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001807 *addr++ = svalue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 size -= sizeof(unsigned long);
1809 if (size <= sizeof(unsigned long))
1810 break;
1811 }
1812 }
1813
1814 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001815 *addr++ = 0x87654321;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817#endif
1818
Pekka Enberg343e0d72006-02-01 03:05:50 -08001819static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001821 int size = obj_size(cachep);
1822 addr = &((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 memset(addr, val, size);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001825 *(unsigned char *)(addr + size - 1) = POISON_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826}
1827
1828static void dump_line(char *data, int offset, int limit)
1829{
1830 int i;
Dave Jonesaa83aa42006-09-29 01:59:51 -07001831 unsigned char error = 0;
1832 int bad_count = 0;
1833
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 printk(KERN_ERR "%03x:", offset);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001835 for (i = 0; i < limit; i++) {
1836 if (data[offset + i] != POISON_FREE) {
1837 error = data[offset + i];
1838 bad_count++;
1839 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001840 printk(" %02x", (unsigned char)data[offset + i]);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001841 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 printk("\n");
Dave Jonesaa83aa42006-09-29 01:59:51 -07001843
1844 if (bad_count == 1) {
1845 error ^= POISON_FREE;
1846 if (!(error & (error - 1))) {
1847 printk(KERN_ERR "Single bit error detected. Probably "
1848 "bad RAM.\n");
1849#ifdef CONFIG_X86
1850 printk(KERN_ERR "Run memtest86+ or a similar memory "
1851 "test tool.\n");
1852#else
1853 printk(KERN_ERR "Run a memory test tool.\n");
1854#endif
1855 }
1856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857}
1858#endif
1859
1860#if DEBUG
1861
Pekka Enberg343e0d72006-02-01 03:05:50 -08001862static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
1864 int i, size;
1865 char *realobj;
1866
1867 if (cachep->flags & SLAB_RED_ZONE) {
David Woodhouseb46b8f12007-05-08 00:22:59 -07001868 printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001869 *dbg_redzone1(cachep, objp),
1870 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 }
1872
1873 if (cachep->flags & SLAB_STORE_USER) {
1874 printk(KERN_ERR "Last user: [<%p>]",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001875 *dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 print_symbol("(%s)",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001877 (unsigned long)*dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 printk("\n");
1879 }
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001880 realobj = (char *)objp + obj_offset(cachep);
1881 size = obj_size(cachep);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001882 for (i = 0; i < size && lines; i += 16, lines--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 int limit;
1884 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001885 if (i + limit > size)
1886 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 dump_line(realobj, i, limit);
1888 }
1889}
1890
Pekka Enberg343e0d72006-02-01 03:05:50 -08001891static void check_poison_obj(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892{
1893 char *realobj;
1894 int size, i;
1895 int lines = 0;
1896
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001897 realobj = (char *)objp + obj_offset(cachep);
1898 size = obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001900 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 char exp = POISON_FREE;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001902 if (i == size - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 exp = POISON_END;
1904 if (realobj[i] != exp) {
1905 int limit;
1906 /* Mismatch ! */
1907 /* Print header */
1908 if (lines == 0) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001909 printk(KERN_ERR
David Howellse94a40c2007-04-02 23:46:28 +01001910 "Slab corruption: %s start=%p, len=%d\n",
1911 cachep->name, realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 print_objinfo(cachep, objp, 0);
1913 }
1914 /* Hexdump the affected line */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001915 i = (i / 16) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001917 if (i + limit > size)
1918 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 dump_line(realobj, i, limit);
1920 i += 16;
1921 lines++;
1922 /* Limit to 5 lines */
1923 if (lines > 5)
1924 break;
1925 }
1926 }
1927 if (lines != 0) {
1928 /* Print some data about the neighboring objects, if they
1929 * exist:
1930 */
Pekka Enberg6ed5eb22006-02-01 03:05:49 -08001931 struct slab *slabp = virt_to_slab(objp);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001932 unsigned int objnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001934 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 if (objnr) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001936 objp = index_to_obj(cachep, slabp, objnr - 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001937 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001939 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 print_objinfo(cachep, objp, 2);
1941 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001942 if (objnr + 1 < cachep->num) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001943 objp = index_to_obj(cachep, slabp, objnr + 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001944 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001946 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 print_objinfo(cachep, objp, 2);
1948 }
1949 }
1950}
1951#endif
1952
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953#if DEBUG
Rabin Vincente79aec22008-07-04 00:40:32 +05301954static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001955{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 int i;
1957 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001958 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959
1960 if (cachep->flags & SLAB_POISON) {
1961#ifdef CONFIG_DEBUG_PAGEALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -08001962 if (cachep->buffer_size % PAGE_SIZE == 0 &&
1963 OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001964 kernel_map_pages(virt_to_page(objp),
Andrew Mortona737b3e2006-03-22 00:08:11 -08001965 cachep->buffer_size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 else
1967 check_poison_obj(cachep, objp);
1968#else
1969 check_poison_obj(cachep, objp);
1970#endif
1971 }
1972 if (cachep->flags & SLAB_RED_ZONE) {
1973 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
1974 slab_error(cachep, "start of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001975 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
1977 slab_error(cachep, "end of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001978 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001981}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982#else
Rabin Vincente79aec22008-07-04 00:40:32 +05301983static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001984{
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001985}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986#endif
1987
Randy Dunlap911851e2006-03-22 00:08:14 -08001988/**
1989 * slab_destroy - destroy and release all objects in a slab
1990 * @cachep: cache pointer being destroyed
1991 * @slabp: slab pointer being destroyed
1992 *
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001993 * Destroy all the objs in a slab, and release the mem back to the system.
Andrew Mortona737b3e2006-03-22 00:08:11 -08001994 * Before calling the slab must have been unlinked from the cache. The
1995 * cache-lock is not held/needed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001996 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001997static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001998{
1999 void *addr = slabp->s_mem - slabp->colouroff;
2000
Rabin Vincente79aec22008-07-04 00:40:32 +05302001 slab_destroy_debugcheck(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
2003 struct slab_rcu *slab_rcu;
2004
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002005 slab_rcu = (struct slab_rcu *)slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 slab_rcu->cachep = cachep;
2007 slab_rcu->addr = addr;
2008 call_rcu(&slab_rcu->head, kmem_rcu_free);
2009 } else {
2010 kmem_freepages(cachep, addr);
Ingo Molnar873623d2006-07-13 14:44:38 +02002011 if (OFF_SLAB(cachep))
2012 kmem_cache_free(cachep->slabp_cache, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
2014}
2015
Christoph Lameter117f6eb2006-09-25 23:31:37 -07002016static void __kmem_cache_destroy(struct kmem_cache *cachep)
2017{
2018 int i;
2019 struct kmem_list3 *l3;
2020
2021 for_each_online_cpu(i)
2022 kfree(cachep->array[i]);
2023
2024 /* NUMA: free the list3 structures */
2025 for_each_online_node(i) {
2026 l3 = cachep->nodelists[i];
2027 if (l3) {
2028 kfree(l3->shared);
2029 free_alien_cache(l3->alien);
2030 kfree(l3);
2031 }
2032 }
2033 kmem_cache_free(&cache_cache, cachep);
2034}
2035
2036
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037/**
Randy.Dunlapa70773d2006-02-01 03:05:52 -08002038 * calculate_slab_order - calculate size (page order) of slabs
2039 * @cachep: pointer to the cache that is being created
2040 * @size: size of objects to be created in this cache.
2041 * @align: required alignment for the objects.
2042 * @flags: slab allocation flags
2043 *
2044 * Also calculates the number of objects per slab.
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002045 *
2046 * This could be made much more intelligent. For now, try to avoid using
2047 * high order pages for slabs. When the gfp() functions are more friendly
2048 * towards high-order requests, this should be changed.
2049 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002050static size_t calculate_slab_order(struct kmem_cache *cachep,
Randy Dunlapee13d782006-02-01 03:05:53 -08002051 size_t size, size_t align, unsigned long flags)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002052{
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002053 unsigned long offslab_limit;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002054 size_t left_over = 0;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002055 int gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002056
Christoph Lameter0aa817f2007-05-16 22:11:01 -07002057 for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002058 unsigned int num;
2059 size_t remainder;
2060
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002061 cache_estimate(gfporder, size, align, flags, &remainder, &num);
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002062 if (!num)
2063 continue;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002064
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002065 if (flags & CFLGS_OFF_SLAB) {
2066 /*
2067 * Max number of objs-per-slab for caches which
2068 * use off-slab slabs. Needed to avoid a possible
2069 * looping condition in cache_grow().
2070 */
2071 offslab_limit = size - sizeof(struct slab);
2072 offslab_limit /= sizeof(kmem_bufctl_t);
2073
2074 if (num > offslab_limit)
2075 break;
2076 }
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002077
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002078 /* Found something acceptable - save it away */
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002079 cachep->num = num;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002080 cachep->gfporder = gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002081 left_over = remainder;
2082
2083 /*
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08002084 * A VFS-reclaimable slab tends to have most allocations
2085 * as GFP_NOFS and we really don't want to have to be allocating
2086 * higher-order pages when we are unable to shrink dcache.
2087 */
2088 if (flags & SLAB_RECLAIM_ACCOUNT)
2089 break;
2090
2091 /*
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002092 * Large number of objects is good, but very large slabs are
2093 * currently bad for the gfp()s.
2094 */
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002095 if (gfporder >= slab_break_gfp_order)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002096 break;
2097
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002098 /*
2099 * Acceptable internal fragmentation?
2100 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002101 if (left_over * 8 <= (PAGE_SIZE << gfporder))
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002102 break;
2103 }
2104 return left_over;
2105}
2106
Pekka Enberg83b519e2009-06-10 19:40:04 +03002107static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002108{
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002109 if (g_cpucache_up == FULL)
Pekka Enberg83b519e2009-06-10 19:40:04 +03002110 return enable_cpucache(cachep, gfp);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002111
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002112 if (g_cpucache_up == NONE) {
2113 /*
2114 * Note: the first kmem_cache_create must create the cache
2115 * that's used by kmalloc(24), otherwise the creation of
2116 * further caches will BUG().
2117 */
2118 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2119
2120 /*
2121 * If the cache that's used by kmalloc(sizeof(kmem_list3)) is
2122 * the first cache, then we need to set up all its list3s,
2123 * otherwise the creation of further caches will BUG().
2124 */
2125 set_up_list3s(cachep, SIZE_AC);
2126 if (INDEX_AC == INDEX_L3)
2127 g_cpucache_up = PARTIAL_L3;
2128 else
2129 g_cpucache_up = PARTIAL_AC;
2130 } else {
2131 cachep->array[smp_processor_id()] =
Pekka Enberg83b519e2009-06-10 19:40:04 +03002132 kmalloc(sizeof(struct arraycache_init), gfp);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002133
2134 if (g_cpucache_up == PARTIAL_AC) {
2135 set_up_list3s(cachep, SIZE_L3);
2136 g_cpucache_up = PARTIAL_L3;
2137 } else {
2138 int node;
Pekka Enberg556a1692008-01-25 08:20:51 +02002139 for_each_online_node(node) {
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002140 cachep->nodelists[node] =
2141 kmalloc_node(sizeof(struct kmem_list3),
Pekka Enbergeb91f1d2009-06-12 14:56:09 +03002142 gfp, node);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002143 BUG_ON(!cachep->nodelists[node]);
2144 kmem_list3_init(cachep->nodelists[node]);
2145 }
2146 }
2147 }
2148 cachep->nodelists[numa_node_id()]->next_reap =
2149 jiffies + REAPTIMEOUT_LIST3 +
2150 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
2151
2152 cpu_cache_get(cachep)->avail = 0;
2153 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
2154 cpu_cache_get(cachep)->batchcount = 1;
2155 cpu_cache_get(cachep)->touched = 0;
2156 cachep->batchcount = 1;
2157 cachep->limit = BOOT_CPUCACHE_ENTRIES;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002158 return 0;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002159}
2160
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002161/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 * kmem_cache_create - Create a cache.
2163 * @name: A string which is used in /proc/slabinfo to identify this cache.
2164 * @size: The size of objects to be created in this cache.
2165 * @align: The required alignment for the objects.
2166 * @flags: SLAB flags
2167 * @ctor: A constructor for the objects.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 *
2169 * Returns a ptr to the cache on success, NULL on failure.
2170 * Cannot be called within a int, but can be interrupted.
Paul Mundt20c2df82007-07-20 10:11:58 +09002171 * The @ctor is run when new pages are allocated by the cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 *
2173 * @name must be valid until the cache is destroyed. This implies that
Andrew Mortona737b3e2006-03-22 00:08:11 -08002174 * the module calling this has to destroy the cache before getting unloaded.
Catalin Marinas249da162008-11-21 12:56:22 +00002175 * Note that kmem_cache_name() is not guaranteed to return the same pointer,
2176 * therefore applications must manage it themselves.
Andrew Mortona737b3e2006-03-22 00:08:11 -08002177 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 * The flags are
2179 *
2180 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2181 * to catch references to uninitialised memory.
2182 *
2183 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2184 * for buffer overruns.
2185 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2187 * cacheline. This can be beneficial if you're counting cycles as closely
2188 * as davem.
2189 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002190struct kmem_cache *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191kmem_cache_create (const char *name, size_t size, size_t align,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002192 unsigned long flags, void (*ctor)(void *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
2194 size_t left_over, slab_size, ralign;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07002195 struct kmem_cache *cachep = NULL, *pc;
Pekka Enberg83b519e2009-06-10 19:40:04 +03002196 gfp_t gfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197
2198 /*
2199 * Sanity checks... these are all serious usage bugs.
2200 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002201 if (!name || in_interrupt() || (size < BYTES_PER_WORD) ||
Paul Mundt20c2df82007-07-20 10:11:58 +09002202 size > KMALLOC_MAX_SIZE) {
Harvey Harrisond40cee22008-04-30 00:55:07 -07002203 printk(KERN_ERR "%s: Early error in slab %s\n", __func__,
Andrew Mortona737b3e2006-03-22 00:08:11 -08002204 name);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002205 BUG();
2206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Ravikiran G Thirumalaif0188f42006-02-10 01:51:13 -08002208 /*
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002209 * We use cache_chain_mutex to ensure a consistent view of
Rusty Russell174596a2009-01-01 10:12:29 +10302210 * cpu_online_mask as well. Please see cpuup_callback
Ravikiran G Thirumalaif0188f42006-02-10 01:51:13 -08002211 */
Pekka Enberg83b519e2009-06-10 19:40:04 +03002212 if (slab_is_available()) {
2213 get_online_cpus();
2214 mutex_lock(&cache_chain_mutex);
2215 }
Andrew Morton4f12bb42005-11-07 00:58:00 -08002216
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07002217 list_for_each_entry(pc, &cache_chain, next) {
Andrew Morton4f12bb42005-11-07 00:58:00 -08002218 char tmp;
2219 int res;
2220
2221 /*
2222 * This happens when the module gets unloaded and doesn't
2223 * destroy its slab cache and no-one else reuses the vmalloc
2224 * area of the module. Print a warning.
2225 */
Andrew Morton138ae662006-12-06 20:36:41 -08002226 res = probe_kernel_address(pc->name, tmp);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002227 if (res) {
matzeb4169522007-05-06 14:49:52 -07002228 printk(KERN_ERR
2229 "SLAB: cache with size %d has lost its name\n",
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002230 pc->buffer_size);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002231 continue;
2232 }
2233
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002234 if (!strcmp(pc->name, name)) {
matzeb4169522007-05-06 14:49:52 -07002235 printk(KERN_ERR
2236 "kmem_cache_create: duplicate cache %s\n", name);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002237 dump_stack();
2238 goto oops;
2239 }
2240 }
2241
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242#if DEBUG
2243 WARN_ON(strchr(name, ' ')); /* It confuses parsers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244#if FORCED_DEBUG
2245 /*
2246 * Enable redzoning and last user accounting, except for caches with
2247 * large objects, if the increased size would increase the object size
2248 * above the next power of two: caches with object sizes just above a
2249 * power of two have a significant amount of internal fragmentation.
2250 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002251 if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
2252 2 * sizeof(unsigned long long)))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002253 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 if (!(flags & SLAB_DESTROY_BY_RCU))
2255 flags |= SLAB_POISON;
2256#endif
2257 if (flags & SLAB_DESTROY_BY_RCU)
2258 BUG_ON(flags & SLAB_POISON);
2259#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002261 * Always checks flags, a caller might be expecting debug support which
2262 * isn't available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 */
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002264 BUG_ON(flags & ~CREATE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Andrew Mortona737b3e2006-03-22 00:08:11 -08002266 /*
2267 * Check that size is in terms of words. This is needed to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 * unaligned accesses for some archs when redzoning is used, and makes
2269 * sure any on-slab bufctl's are also correctly aligned.
2270 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002271 if (size & (BYTES_PER_WORD - 1)) {
2272 size += (BYTES_PER_WORD - 1);
2273 size &= ~(BYTES_PER_WORD - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 }
2275
Andrew Mortona737b3e2006-03-22 00:08:11 -08002276 /* calculate the final buffer alignment: */
2277
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 /* 1) arch recommendation: can be overridden for debug */
2279 if (flags & SLAB_HWCACHE_ALIGN) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002280 /*
2281 * Default alignment: as specified by the arch code. Except if
2282 * an object is really small, then squeeze multiple objects into
2283 * one cacheline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 */
2285 ralign = cache_line_size();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002286 while (size <= ralign / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 ralign /= 2;
2288 } else {
2289 ralign = BYTES_PER_WORD;
2290 }
Pekka Enbergca5f9702006-09-25 23:31:25 -07002291
2292 /*
David Woodhouse87a927c2007-07-04 21:26:44 -04002293 * Redzoning and user store require word alignment or possibly larger.
2294 * Note this will be overridden by architecture or caller mandated
2295 * alignment if either is greater than BYTES_PER_WORD.
Pekka Enbergca5f9702006-09-25 23:31:25 -07002296 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002297 if (flags & SLAB_STORE_USER)
2298 ralign = BYTES_PER_WORD;
2299
2300 if (flags & SLAB_RED_ZONE) {
2301 ralign = REDZONE_ALIGN;
2302 /* If redzoning, ensure that the second redzone is suitably
2303 * aligned, by adjusting the object size accordingly. */
2304 size += REDZONE_ALIGN - 1;
2305 size &= ~(REDZONE_ALIGN - 1);
2306 }
Pekka Enbergca5f9702006-09-25 23:31:25 -07002307
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002308 /* 2) arch mandated alignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 if (ralign < ARCH_SLAB_MINALIGN) {
2310 ralign = ARCH_SLAB_MINALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002312 /* 3) caller mandated alignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 if (ralign < align) {
2314 ralign = align;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 }
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002316 /* disable debug if necessary */
David Woodhouseb46b8f12007-05-08 00:22:59 -07002317 if (ralign > __alignof__(unsigned long long))
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002318 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002319 /*
Pekka Enbergca5f9702006-09-25 23:31:25 -07002320 * 4) Store it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 */
2322 align = ralign;
2323
Pekka Enberg83b519e2009-06-10 19:40:04 +03002324 if (slab_is_available())
2325 gfp = GFP_KERNEL;
2326 else
2327 gfp = GFP_NOWAIT;
2328
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 /* Get cache's description obj. */
Pekka Enberg83b519e2009-06-10 19:40:04 +03002330 cachep = kmem_cache_zalloc(&cache_cache, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 if (!cachep)
Andrew Morton4f12bb42005-11-07 00:58:00 -08002332 goto oops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334#if DEBUG
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002335 cachep->obj_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Pekka Enbergca5f9702006-09-25 23:31:25 -07002337 /*
2338 * Both debugging options require word-alignment which is calculated
2339 * into align above.
2340 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if (flags & SLAB_RED_ZONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 /* add space for red zone words */
David Woodhouseb46b8f12007-05-08 00:22:59 -07002343 cachep->obj_offset += sizeof(unsigned long long);
2344 size += 2 * sizeof(unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 }
2346 if (flags & SLAB_STORE_USER) {
Pekka Enbergca5f9702006-09-25 23:31:25 -07002347 /* user store requires one word storage behind the end of
David Woodhouse87a927c2007-07-04 21:26:44 -04002348 * the real object. But if the second red zone needs to be
2349 * aligned to 64 bits, we must allow that much space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002351 if (flags & SLAB_RED_ZONE)
2352 size += REDZONE_ALIGN;
2353 else
2354 size += BYTES_PER_WORD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 }
2356#if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002357 if (size >= malloc_sizes[INDEX_L3 + 1].cs_size
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002358 && cachep->obj_size > cache_line_size() && size < PAGE_SIZE) {
2359 cachep->obj_offset += PAGE_SIZE - size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 size = PAGE_SIZE;
2361 }
2362#endif
2363#endif
2364
Ingo Molnare0a42722006-06-23 02:03:46 -07002365 /*
2366 * Determine if the slab management is 'on' or 'off' slab.
2367 * (bootstrapping cannot cope with offslab caches so don't do
Catalin Marinase7cb55b2009-10-28 13:33:08 +00002368 * it too early on. Always use on-slab management when
2369 * SLAB_NOLEAKTRACE to avoid recursive calls into kmemleak)
Ingo Molnare0a42722006-06-23 02:03:46 -07002370 */
Catalin Marinase7cb55b2009-10-28 13:33:08 +00002371 if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init &&
2372 !(flags & SLAB_NOLEAKTRACE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 /*
2374 * Size is large, assume best to place the slab management obj
2375 * off-slab (should allow better packing of objs).
2376 */
2377 flags |= CFLGS_OFF_SLAB;
2378
2379 size = ALIGN(size, align);
2380
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08002381 left_over = calculate_slab_order(cachep, size, align, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383 if (!cachep->num) {
matzeb4169522007-05-06 14:49:52 -07002384 printk(KERN_ERR
2385 "kmem_cache_create: couldn't create cache %s.\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 kmem_cache_free(&cache_cache, cachep);
2387 cachep = NULL;
Andrew Morton4f12bb42005-11-07 00:58:00 -08002388 goto oops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002390 slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
2391 + sizeof(struct slab), align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393 /*
2394 * If the slab has been placed off-slab, and we have enough space then
2395 * move it on-slab. This is at the expense of any extra colouring.
2396 */
2397 if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
2398 flags &= ~CFLGS_OFF_SLAB;
2399 left_over -= slab_size;
2400 }
2401
2402 if (flags & CFLGS_OFF_SLAB) {
2403 /* really off slab. No need for manual alignment */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002404 slab_size =
2405 cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
Ron Lee67461362009-05-22 04:58:22 +09302406
2407#ifdef CONFIG_PAGE_POISONING
2408 /* If we're going to use the generic kernel_map_pages()
2409 * poisoning, then it's going to smash the contents of
2410 * the redzone and userword anyhow, so switch them off.
2411 */
2412 if (size % PAGE_SIZE == 0 && flags & SLAB_POISON)
2413 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
2414#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 }
2416
2417 cachep->colour_off = cache_line_size();
2418 /* Offset must be a multiple of the alignment. */
2419 if (cachep->colour_off < align)
2420 cachep->colour_off = align;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002421 cachep->colour = left_over / cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 cachep->slab_size = slab_size;
2423 cachep->flags = flags;
2424 cachep->gfpflags = 0;
Christoph Lameter4b51d662007-02-10 01:43:10 -08002425 if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 cachep->gfpflags |= GFP_DMA;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002427 cachep->buffer_size = size;
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08002428 cachep->reciprocal_buffer_size = reciprocal_value(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002430 if (flags & CFLGS_OFF_SLAB) {
Victor Fuscob2d55072005-09-10 00:26:36 -07002431 cachep->slabp_cache = kmem_find_general_cachep(slab_size, 0u);
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002432 /*
2433 * This is a possibility for one of the malloc_sizes caches.
2434 * But since we go off slab only for object size greater than
2435 * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
2436 * this should not happen at all.
2437 * But leave a BUG_ON for some lucky dude.
2438 */
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002439 BUG_ON(ZERO_OR_NULL_PTR(cachep->slabp_cache));
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002440 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 cachep->ctor = ctor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 cachep->name = name;
2443
Pekka Enberg83b519e2009-06-10 19:40:04 +03002444 if (setup_cpu_cache(cachep, gfp)) {
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002445 __kmem_cache_destroy(cachep);
2446 cachep = NULL;
2447 goto oops;
2448 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 /* cache setup completed, link it into the list */
2451 list_add(&cachep->next, &cache_chain);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002452oops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453 if (!cachep && (flags & SLAB_PANIC))
2454 panic("kmem_cache_create(): failed to create slab `%s'\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002455 name);
Pekka Enberg83b519e2009-06-10 19:40:04 +03002456 if (slab_is_available()) {
2457 mutex_unlock(&cache_chain_mutex);
2458 put_online_cpus();
2459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 return cachep;
2461}
2462EXPORT_SYMBOL(kmem_cache_create);
2463
2464#if DEBUG
2465static void check_irq_off(void)
2466{
2467 BUG_ON(!irqs_disabled());
2468}
2469
2470static void check_irq_on(void)
2471{
2472 BUG_ON(irqs_disabled());
2473}
2474
Pekka Enberg343e0d72006-02-01 03:05:50 -08002475static void check_spinlock_acquired(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
2477#ifdef CONFIG_SMP
2478 check_irq_off();
Christoph Lametere498be72005-09-09 13:03:32 -07002479 assert_spin_locked(&cachep->nodelists[numa_node_id()]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480#endif
2481}
Christoph Lametere498be72005-09-09 13:03:32 -07002482
Pekka Enberg343e0d72006-02-01 03:05:50 -08002483static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07002484{
2485#ifdef CONFIG_SMP
2486 check_irq_off();
2487 assert_spin_locked(&cachep->nodelists[node]->list_lock);
2488#endif
2489}
2490
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491#else
2492#define check_irq_off() do { } while(0)
2493#define check_irq_on() do { } while(0)
2494#define check_spinlock_acquired(x) do { } while(0)
Christoph Lametere498be72005-09-09 13:03:32 -07002495#define check_spinlock_acquired_node(x, y) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496#endif
2497
Christoph Lameteraab22072006-03-22 00:09:06 -08002498static void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
2499 struct array_cache *ac,
2500 int force, int node);
2501
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502static void do_drain(void *arg)
2503{
Andrew Mortona737b3e2006-03-22 00:08:11 -08002504 struct kmem_cache *cachep = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505 struct array_cache *ac;
Christoph Lameterff694162005-09-22 21:44:02 -07002506 int node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507
2508 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002509 ac = cpu_cache_get(cachep);
Christoph Lameterff694162005-09-22 21:44:02 -07002510 spin_lock(&cachep->nodelists[node]->list_lock);
2511 free_block(cachep, ac->entry, ac->avail, node);
2512 spin_unlock(&cachep->nodelists[node]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 ac->avail = 0;
2514}
2515
Pekka Enberg343e0d72006-02-01 03:05:50 -08002516static void drain_cpu_caches(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517{
Christoph Lametere498be72005-09-09 13:03:32 -07002518 struct kmem_list3 *l3;
2519 int node;
2520
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002521 on_each_cpu(do_drain, cachep, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 check_irq_on();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002523 for_each_online_node(node) {
Christoph Lametere498be72005-09-09 13:03:32 -07002524 l3 = cachep->nodelists[node];
Roland Dreiera4523a82006-05-15 11:41:00 -07002525 if (l3 && l3->alien)
2526 drain_alien_cache(cachep, l3->alien);
2527 }
2528
2529 for_each_online_node(node) {
2530 l3 = cachep->nodelists[node];
2531 if (l3)
Christoph Lameteraab22072006-03-22 00:09:06 -08002532 drain_array(cachep, l3, l3->shared, 1, node);
Christoph Lametere498be72005-09-09 13:03:32 -07002533 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534}
2535
Christoph Lametered11d9e2006-06-30 01:55:45 -07002536/*
2537 * Remove slabs from the list of free slabs.
2538 * Specify the number of slabs to drain in tofree.
2539 *
2540 * Returns the actual number of slabs released.
2541 */
2542static int drain_freelist(struct kmem_cache *cache,
2543 struct kmem_list3 *l3, int tofree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544{
Christoph Lametered11d9e2006-06-30 01:55:45 -07002545 struct list_head *p;
2546 int nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Christoph Lametered11d9e2006-06-30 01:55:45 -07002549 nr_freed = 0;
2550 while (nr_freed < tofree && !list_empty(&l3->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Christoph Lametered11d9e2006-06-30 01:55:45 -07002552 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002553 p = l3->slabs_free.prev;
Christoph Lametered11d9e2006-06-30 01:55:45 -07002554 if (p == &l3->slabs_free) {
2555 spin_unlock_irq(&l3->list_lock);
2556 goto out;
2557 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558
Christoph Lametered11d9e2006-06-30 01:55:45 -07002559 slabp = list_entry(p, struct slab, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560#if DEBUG
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002561 BUG_ON(slabp->inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562#endif
2563 list_del(&slabp->list);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002564 /*
2565 * Safe to drop the lock. The slab is no longer linked
2566 * to the cache.
2567 */
2568 l3->free_objects -= cache->num;
Christoph Lametere498be72005-09-09 13:03:32 -07002569 spin_unlock_irq(&l3->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002570 slab_destroy(cache, slabp);
2571 nr_freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 }
Christoph Lametered11d9e2006-06-30 01:55:45 -07002573out:
2574 return nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575}
2576
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002577/* Called with cache_chain_mutex held to protect against cpu hotplug */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002578static int __cache_shrink(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07002579{
2580 int ret = 0, i = 0;
2581 struct kmem_list3 *l3;
2582
2583 drain_cpu_caches(cachep);
2584
2585 check_irq_on();
2586 for_each_online_node(i) {
2587 l3 = cachep->nodelists[i];
Christoph Lametered11d9e2006-06-30 01:55:45 -07002588 if (!l3)
2589 continue;
2590
2591 drain_freelist(cachep, l3, l3->free_objects);
2592
2593 ret += !list_empty(&l3->slabs_full) ||
2594 !list_empty(&l3->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07002595 }
2596 return (ret ? 1 : 0);
2597}
2598
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599/**
2600 * kmem_cache_shrink - Shrink a cache.
2601 * @cachep: The cache to shrink.
2602 *
2603 * Releases as many slabs as possible for a cache.
2604 * To help debugging, a zero exit status indicates all slabs were released.
2605 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002606int kmem_cache_shrink(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607{
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002608 int ret;
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002609 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002611 get_online_cpus();
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002612 mutex_lock(&cache_chain_mutex);
2613 ret = __cache_shrink(cachep);
2614 mutex_unlock(&cache_chain_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002615 put_online_cpus();
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002616 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618EXPORT_SYMBOL(kmem_cache_shrink);
2619
2620/**
2621 * kmem_cache_destroy - delete a cache
2622 * @cachep: the cache to destroy
2623 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002624 * Remove a &struct kmem_cache object from the slab cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 *
2626 * It is expected this function will be called by a module when it is
2627 * unloaded. This will remove the cache completely, and avoid a duplicate
2628 * cache being allocated each time a module is loaded and unloaded, if the
2629 * module doesn't have persistent in-kernel storage across loads and unloads.
2630 *
2631 * The cache must be empty before calling this function.
2632 *
2633 * The caller must guarantee that noone will allocate memory from the cache
2634 * during the kmem_cache_destroy().
2635 */
Alexey Dobriyan133d2052006-09-27 01:49:41 -07002636void kmem_cache_destroy(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637{
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002638 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 /* Find the cache in the chain of caches. */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002641 get_online_cpus();
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002642 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 /*
2644 * the chain is never empty, cache_cache is never destroyed
2645 */
2646 list_del(&cachep->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 if (__cache_shrink(cachep)) {
2648 slab_error(cachep, "Can't free all objects");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002649 list_add(&cachep->next, &cache_chain);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002650 mutex_unlock(&cache_chain_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002651 put_online_cpus();
Alexey Dobriyan133d2052006-09-27 01:49:41 -07002652 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 }
2654
2655 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU))
Paul E. McKenney7ed9f7e2009-06-25 12:31:37 -07002656 rcu_barrier();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Christoph Lameter117f6eb2006-09-25 23:31:37 -07002658 __kmem_cache_destroy(cachep);
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002659 mutex_unlock(&cache_chain_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002660 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661}
2662EXPORT_SYMBOL(kmem_cache_destroy);
2663
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002664/*
2665 * Get the memory for a slab management obj.
2666 * For a slab cache when the slab descriptor is off-slab, slab descriptors
2667 * always come from malloc_sizes caches. The slab descriptor cannot
2668 * come from the same cache which is getting created because,
2669 * when we are searching for an appropriate cache for these
2670 * descriptors in kmem_cache_create, we search through the malloc_sizes array.
2671 * If we are creating a malloc_sizes cache here it would not be visible to
2672 * kmem_find_general_cachep till the initialization is complete.
2673 * Hence we cannot have slabp_cache same as the original cache.
2674 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002675static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002676 int colour_off, gfp_t local_flags,
2677 int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678{
2679 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002680
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 if (OFF_SLAB(cachep)) {
2682 /* Slab management obj is off-slab. */
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002683 slabp = kmem_cache_alloc_node(cachep->slabp_cache,
Pekka Enberg8759ec52008-11-26 10:01:31 +02002684 local_flags, nodeid);
Catalin Marinasd5cff632009-06-11 13:22:40 +01002685 /*
2686 * If the first object in the slab is leaked (it's allocated
2687 * but no one has a reference to it), we want to make sure
2688 * kmemleak does not treat the ->s_mem pointer as a reference
2689 * to the object. Otherwise we will not report the leak.
2690 */
Catalin Marinasc017b4b2009-10-28 13:33:09 +00002691 kmemleak_scan_area(&slabp->list, sizeof(struct list_head),
2692 local_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 if (!slabp)
2694 return NULL;
2695 } else {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002696 slabp = objp + colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 colour_off += cachep->slab_size;
2698 }
2699 slabp->inuse = 0;
2700 slabp->colouroff = colour_off;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002701 slabp->s_mem = objp + colour_off;
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002702 slabp->nodeid = nodeid;
Marcin Slusarze51bfd02008-02-10 11:21:54 +01002703 slabp->free = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 return slabp;
2705}
2706
2707static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
2708{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002709 return (kmem_bufctl_t *) (slabp + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}
2711
Pekka Enberg343e0d72006-02-01 03:05:50 -08002712static void cache_init_objs(struct kmem_cache *cachep,
Christoph Lametera35afb82007-05-16 22:10:57 -07002713 struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714{
2715 int i;
2716
2717 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002718 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719#if DEBUG
2720 /* need to poison the objs? */
2721 if (cachep->flags & SLAB_POISON)
2722 poison_obj(cachep, objp, POISON_FREE);
2723 if (cachep->flags & SLAB_STORE_USER)
2724 *dbg_userword(cachep, objp) = NULL;
2725
2726 if (cachep->flags & SLAB_RED_ZONE) {
2727 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2728 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2729 }
2730 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002731 * Constructors are not allowed to allocate memory from the same
2732 * cache which they are a constructor for. Otherwise, deadlock.
2733 * They must also be threaded.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 */
2735 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002736 cachep->ctor(objp + obj_offset(cachep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737
2738 if (cachep->flags & SLAB_RED_ZONE) {
2739 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2740 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002741 " end of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2743 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002744 " start of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 }
Andrew Mortona737b3e2006-03-22 00:08:11 -08002746 if ((cachep->buffer_size % PAGE_SIZE) == 0 &&
2747 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002748 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002749 cachep->buffer_size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750#else
2751 if (cachep->ctor)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002752 cachep->ctor(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753#endif
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002754 slab_bufctl(slabp)[i] = i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002756 slab_bufctl(slabp)[i - 1] = BUFCTL_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757}
2758
Pekka Enberg343e0d72006-02-01 03:05:50 -08002759static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760{
Christoph Lameter4b51d662007-02-10 01:43:10 -08002761 if (CONFIG_ZONE_DMA_FLAG) {
2762 if (flags & GFP_DMA)
2763 BUG_ON(!(cachep->gfpflags & GFP_DMA));
2764 else
2765 BUG_ON(cachep->gfpflags & GFP_DMA);
2766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767}
2768
Andrew Mortona737b3e2006-03-22 00:08:11 -08002769static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
2770 int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002771{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002772 void *objp = index_to_obj(cachep, slabp, slabp->free);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002773 kmem_bufctl_t next;
2774
2775 slabp->inuse++;
2776 next = slab_bufctl(slabp)[slabp->free];
2777#if DEBUG
2778 slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
2779 WARN_ON(slabp->nodeid != nodeid);
2780#endif
2781 slabp->free = next;
2782
2783 return objp;
2784}
2785
Andrew Mortona737b3e2006-03-22 00:08:11 -08002786static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
2787 void *objp, int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002788{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002789 unsigned int objnr = obj_to_index(cachep, slabp, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002790
2791#if DEBUG
2792 /* Verify that the slab belongs to the intended node */
2793 WARN_ON(slabp->nodeid != nodeid);
2794
Al Viro871751e2006-03-25 03:06:39 -08002795 if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) {
Matthew Dobson78d382d2006-02-01 03:05:47 -08002796 printk(KERN_ERR "slab: double free detected in cache "
Andrew Mortona737b3e2006-03-22 00:08:11 -08002797 "'%s', objp %p\n", cachep->name, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002798 BUG();
2799 }
2800#endif
2801 slab_bufctl(slabp)[objnr] = slabp->free;
2802 slabp->free = objnr;
2803 slabp->inuse--;
2804}
2805
Pekka Enberg47768742006-06-23 02:03:07 -07002806/*
2807 * Map pages beginning at addr to the given cache and slab. This is required
2808 * for the slab allocator to be able to lookup the cache and slab of a
2809 * virtual address for kfree, ksize, kmem_ptr_validate, and slab debugging.
2810 */
2811static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
2812 void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813{
Pekka Enberg47768742006-06-23 02:03:07 -07002814 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 struct page *page;
2816
Pekka Enberg47768742006-06-23 02:03:07 -07002817 page = virt_to_page(addr);
Nick Piggin84097512006-03-22 00:08:34 -08002818
Pekka Enberg47768742006-06-23 02:03:07 -07002819 nr_pages = 1;
Nick Piggin84097512006-03-22 00:08:34 -08002820 if (likely(!PageCompound(page)))
Pekka Enberg47768742006-06-23 02:03:07 -07002821 nr_pages <<= cache->gfporder;
2822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 do {
Pekka Enberg47768742006-06-23 02:03:07 -07002824 page_set_cache(page, cache);
2825 page_set_slab(page, slab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 page++;
Pekka Enberg47768742006-06-23 02:03:07 -07002827 } while (--nr_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828}
2829
2830/*
2831 * Grow (by 1) the number of slabs within a cache. This is called by
2832 * kmem_cache_alloc() when there are no active objs left in a cache.
2833 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002834static int cache_grow(struct kmem_cache *cachep,
2835 gfp_t flags, int nodeid, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002837 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002838 size_t offset;
2839 gfp_t local_flags;
Christoph Lametere498be72005-09-09 13:03:32 -07002840 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Andrew Mortona737b3e2006-03-22 00:08:11 -08002842 /*
2843 * Be lazy and only check for valid flags here, keeping it out of the
2844 * critical path in kmem_cache_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 */
Christoph Lameter6cb06222007-10-16 01:25:41 -07002846 BUG_ON(flags & GFP_SLAB_BUG_MASK);
2847 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002849 /* Take the l3 list lock to change the colour_next on this node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 check_irq_off();
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002851 l3 = cachep->nodelists[nodeid];
2852 spin_lock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
2854 /* Get colour for the slab, and cal the next value. */
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002855 offset = l3->colour_next;
2856 l3->colour_next++;
2857 if (l3->colour_next >= cachep->colour)
2858 l3->colour_next = 0;
2859 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002861 offset *= cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
2863 if (local_flags & __GFP_WAIT)
2864 local_irq_enable();
2865
2866 /*
2867 * The test for missing atomic flag is performed here, rather than
2868 * the more obvious place, simply to reduce the critical path length
2869 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2870 * will eventually be caught here (where it matters).
2871 */
2872 kmem_flagcheck(cachep, flags);
2873
Andrew Mortona737b3e2006-03-22 00:08:11 -08002874 /*
2875 * Get mem for the objs. Attempt to allocate a physical page from
2876 * 'nodeid'.
Christoph Lametere498be72005-09-09 13:03:32 -07002877 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002878 if (!objp)
Andrew Mortonb8c1c5d2007-07-24 12:02:40 -07002879 objp = kmem_getpages(cachep, local_flags, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002880 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 goto failed;
2882
2883 /* Get slab management. */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002884 slabp = alloc_slabmgmt(cachep, objp, offset,
Christoph Lameter6cb06222007-10-16 01:25:41 -07002885 local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002886 if (!slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 goto opps1;
2888
Pekka Enberg47768742006-06-23 02:03:07 -07002889 slab_map_pages(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Christoph Lametera35afb82007-05-16 22:10:57 -07002891 cache_init_objs(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892
2893 if (local_flags & __GFP_WAIT)
2894 local_irq_disable();
2895 check_irq_off();
Christoph Lametere498be72005-09-09 13:03:32 -07002896 spin_lock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897
2898 /* Make slab active. */
Christoph Lametere498be72005-09-09 13:03:32 -07002899 list_add_tail(&slabp->list, &(l3->slabs_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900 STATS_INC_GROWN(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07002901 l3->free_objects += cachep->num;
2902 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002904opps1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 kmem_freepages(cachep, objp);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002906failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 if (local_flags & __GFP_WAIT)
2908 local_irq_disable();
2909 return 0;
2910}
2911
2912#if DEBUG
2913
2914/*
2915 * Perform extra freeing checks:
2916 * - detect bad pointers.
2917 * - POISON/RED_ZONE checking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 */
2919static void kfree_debugcheck(const void *objp)
2920{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 if (!virt_addr_valid(objp)) {
2922 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002923 (unsigned long)objp);
2924 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926}
2927
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002928static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2929{
David Woodhouseb46b8f12007-05-08 00:22:59 -07002930 unsigned long long redzone1, redzone2;
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002931
2932 redzone1 = *dbg_redzone1(cache, obj);
2933 redzone2 = *dbg_redzone2(cache, obj);
2934
2935 /*
2936 * Redzone is ok.
2937 */
2938 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2939 return;
2940
2941 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2942 slab_error(cache, "double free detected");
2943 else
2944 slab_error(cache, "memory outside object was overwritten");
2945
David Woodhouseb46b8f12007-05-08 00:22:59 -07002946 printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002947 obj, redzone1, redzone2);
2948}
2949
Pekka Enberg343e0d72006-02-01 03:05:50 -08002950static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002951 void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952{
2953 struct page *page;
2954 unsigned int objnr;
2955 struct slab *slabp;
2956
Matthew Wilcox80cbd912007-11-29 12:05:13 -07002957 BUG_ON(virt_to_cache(objp) != cachep);
2958
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002959 objp -= obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 kfree_debugcheck(objp);
Christoph Lameterb49af682007-05-06 14:49:41 -07002961 page = virt_to_head_page(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
Pekka Enberg065d41c2005-11-13 16:06:46 -08002963 slabp = page_get_slab(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
2965 if (cachep->flags & SLAB_RED_ZONE) {
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002966 verify_redzone_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2968 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2969 }
2970 if (cachep->flags & SLAB_STORE_USER)
2971 *dbg_userword(cachep, objp) = caller;
2972
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002973 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
2975 BUG_ON(objnr >= cachep->num);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002976 BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
Al Viro871751e2006-03-25 03:06:39 -08002978#ifdef CONFIG_DEBUG_SLAB_LEAK
2979 slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
2980#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if (cachep->flags & SLAB_POISON) {
2982#ifdef CONFIG_DEBUG_PAGEALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -08002983 if ((cachep->buffer_size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 store_stackinfo(cachep, objp, (unsigned long)caller);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002985 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002986 cachep->buffer_size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 } else {
2988 poison_obj(cachep, objp, POISON_FREE);
2989 }
2990#else
2991 poison_obj(cachep, objp, POISON_FREE);
2992#endif
2993 }
2994 return objp;
2995}
2996
Pekka Enberg343e0d72006-02-01 03:05:50 -08002997static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998{
2999 kmem_bufctl_t i;
3000 int entries = 0;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003001
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 /* Check slab's freelist to see if this obj is there. */
3003 for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
3004 entries++;
3005 if (entries > cachep->num || i >= cachep->num)
3006 goto bad;
3007 }
3008 if (entries != cachep->num - slabp->inuse) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003009bad:
3010 printk(KERN_ERR "slab: Internal list corruption detected in "
3011 "cache '%s'(%d), slabp %p(%d). Hexdump:\n",
3012 cachep->name, cachep->num, slabp, slabp->inuse);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003013 for (i = 0;
Linus Torvalds264132b2006-03-06 12:10:07 -08003014 i < sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003015 i++) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003016 if (i % 16 == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 printk("\n%03x:", i);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003018 printk(" %02x", ((unsigned char *)slabp)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 }
3020 printk("\n");
3021 BUG();
3022 }
3023}
3024#else
3025#define kfree_debugcheck(x) do { } while(0)
3026#define cache_free_debugcheck(x,objp,z) (objp)
3027#define check_slabp(x,y) do { } while(0)
3028#endif
3029
Pekka Enberg343e0d72006-02-01 03:05:50 -08003030static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
3032 int batchcount;
3033 struct kmem_list3 *l3;
3034 struct array_cache *ac;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07003035 int node;
3036
Andrew Mortona737b3e2006-03-22 00:08:11 -08003037retry:
Joe Korty6d2144d2008-03-05 15:04:59 -08003038 check_irq_off();
3039 node = numa_node_id();
3040 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 batchcount = ac->batchcount;
3042 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003043 /*
3044 * If there was little recent activity on this cache, then
3045 * perform only a partial refill. Otherwise we could generate
3046 * refill bouncing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 */
3048 batchcount = BATCHREFILL_LIMIT;
3049 }
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07003050 l3 = cachep->nodelists[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051
Christoph Lametere498be72005-09-09 13:03:32 -07003052 BUG_ON(ac->avail > 0 || !l3);
3053 spin_lock(&l3->list_lock);
3054
Christoph Lameter3ded1752006-03-25 03:06:44 -08003055 /* See if we can refill from the shared array */
Nick Piggin44b57f12010-01-27 22:27:40 +11003056 if (l3->shared && transfer_objects(ac, l3->shared, batchcount)) {
3057 l3->shared->touched = 1;
Christoph Lameter3ded1752006-03-25 03:06:44 -08003058 goto alloc_done;
Nick Piggin44b57f12010-01-27 22:27:40 +11003059 }
Christoph Lameter3ded1752006-03-25 03:06:44 -08003060
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 while (batchcount > 0) {
3062 struct list_head *entry;
3063 struct slab *slabp;
3064 /* Get slab alloc is to come from. */
3065 entry = l3->slabs_partial.next;
3066 if (entry == &l3->slabs_partial) {
3067 l3->free_touched = 1;
3068 entry = l3->slabs_free.next;
3069 if (entry == &l3->slabs_free)
3070 goto must_grow;
3071 }
3072
3073 slabp = list_entry(entry, struct slab, list);
3074 check_slabp(cachep, slabp);
3075 check_spinlock_acquired(cachep);
Pekka Enberg714b8172007-05-06 14:49:03 -07003076
3077 /*
3078 * The slab was either on partial or free list so
3079 * there must be at least one object available for
3080 * allocation.
3081 */
roel kluin249b9f32008-10-29 17:18:07 -04003082 BUG_ON(slabp->inuse >= cachep->num);
Pekka Enberg714b8172007-05-06 14:49:03 -07003083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 while (slabp->inuse < cachep->num && batchcount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 STATS_INC_ALLOCED(cachep);
3086 STATS_INC_ACTIVE(cachep);
3087 STATS_SET_HIGH(cachep);
3088
Matthew Dobson78d382d2006-02-01 03:05:47 -08003089 ac->entry[ac->avail++] = slab_get_obj(cachep, slabp,
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07003090 node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 }
3092 check_slabp(cachep, slabp);
3093
3094 /* move slabp to correct slabp list: */
3095 list_del(&slabp->list);
3096 if (slabp->free == BUFCTL_END)
3097 list_add(&slabp->list, &l3->slabs_full);
3098 else
3099 list_add(&slabp->list, &l3->slabs_partial);
3100 }
3101
Andrew Mortona737b3e2006-03-22 00:08:11 -08003102must_grow:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 l3->free_objects -= ac->avail;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003104alloc_done:
Christoph Lametere498be72005-09-09 13:03:32 -07003105 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
3107 if (unlikely(!ac->avail)) {
3108 int x;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003109 x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07003110
Andrew Mortona737b3e2006-03-22 00:08:11 -08003111 /* cache_grow can reenable interrupts, then ac could change. */
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003112 ac = cpu_cache_get(cachep);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003113 if (!x && ac->avail == 0) /* no objects in sight? abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114 return NULL;
3115
Andrew Mortona737b3e2006-03-22 00:08:11 -08003116 if (!ac->avail) /* objects refilled by interrupt? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 goto retry;
3118 }
3119 ac->touched = 1;
Christoph Lametere498be72005-09-09 13:03:32 -07003120 return ac->entry[--ac->avail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121}
3122
Andrew Mortona737b3e2006-03-22 00:08:11 -08003123static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
3124 gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125{
3126 might_sleep_if(flags & __GFP_WAIT);
3127#if DEBUG
3128 kmem_flagcheck(cachep, flags);
3129#endif
3130}
3131
3132#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003133static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
3134 gfp_t flags, void *objp, void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003136 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 return objp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003138 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139#ifdef CONFIG_DEBUG_PAGEALLOC
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003140 if ((cachep->buffer_size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003141 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003142 cachep->buffer_size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 else
3144 check_poison_obj(cachep, objp);
3145#else
3146 check_poison_obj(cachep, objp);
3147#endif
3148 poison_obj(cachep, objp, POISON_INUSE);
3149 }
3150 if (cachep->flags & SLAB_STORE_USER)
3151 *dbg_userword(cachep, objp) = caller;
3152
3153 if (cachep->flags & SLAB_RED_ZONE) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003154 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
3155 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
3156 slab_error(cachep, "double free, or memory outside"
3157 " object was overwritten");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003158 printk(KERN_ERR
David Woodhouseb46b8f12007-05-08 00:22:59 -07003159 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08003160 objp, *dbg_redzone1(cachep, objp),
3161 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 }
3163 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
3164 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
3165 }
Al Viro871751e2006-03-25 03:06:39 -08003166#ifdef CONFIG_DEBUG_SLAB_LEAK
3167 {
3168 struct slab *slabp;
3169 unsigned objnr;
3170
Christoph Lameterb49af682007-05-06 14:49:41 -07003171 slabp = page_get_slab(virt_to_head_page(objp));
Al Viro871751e2006-03-25 03:06:39 -08003172 objnr = (unsigned)(objp - slabp->s_mem) / cachep->buffer_size;
3173 slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE;
3174 }
3175#endif
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003176 objp += obj_offset(cachep);
Christoph Lameter4f104932007-05-06 14:50:17 -07003177 if (cachep->ctor && cachep->flags & SLAB_POISON)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003178 cachep->ctor(objp);
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003179#if ARCH_SLAB_MINALIGN
3180 if ((u32)objp & (ARCH_SLAB_MINALIGN-1)) {
3181 printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
3182 objp, ARCH_SLAB_MINALIGN);
3183 }
3184#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003185 return objp;
3186}
3187#else
3188#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
3189#endif
3190
Akinobu Mita773ff602008-12-23 19:37:01 +09003191static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags)
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003192{
3193 if (cachep == &cache_cache)
Akinobu Mita773ff602008-12-23 19:37:01 +09003194 return false;
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003195
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +03003196 return should_failslab(obj_size(cachep), flags, cachep->flags);
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003197}
3198
Pekka Enberg343e0d72006-02-01 03:05:50 -08003199static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003201 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202 struct array_cache *ac;
3203
Alok N Kataria5c382302005-09-27 21:45:46 -07003204 check_irq_off();
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003205
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003206 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 if (likely(ac->avail)) {
3208 STATS_INC_ALLOCHIT(cachep);
3209 ac->touched = 1;
Christoph Lametere498be72005-09-09 13:03:32 -07003210 objp = ac->entry[--ac->avail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 } else {
3212 STATS_INC_ALLOCMISS(cachep);
3213 objp = cache_alloc_refill(cachep, flags);
J. R. Okajimaddbf2e82009-12-02 16:55:50 +09003214 /*
3215 * the 'ac' may be updated by cache_alloc_refill(),
3216 * and kmemleak_erase() requires its correct value.
3217 */
3218 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 }
Catalin Marinasd5cff632009-06-11 13:22:40 +01003220 /*
3221 * To avoid a false negative, if an object that is in one of the
3222 * per-CPU caches is leaked, we need to make sure kmemleak doesn't
3223 * treat the array pointers as a reference to the object.
3224 */
J. R. Okajimaf3d8b532009-12-02 16:55:49 +09003225 if (objp)
3226 kmemleak_erase(&ac->entry[ac->avail]);
Alok N Kataria5c382302005-09-27 21:45:46 -07003227 return objp;
3228}
3229
Christoph Lametere498be72005-09-09 13:03:32 -07003230#ifdef CONFIG_NUMA
3231/*
Paul Jacksonb2455392006-03-24 03:16:12 -08003232 * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
Paul Jacksonc61afb12006-03-24 03:16:08 -08003233 *
3234 * If we are in_interrupt, then process context, including cpusets and
3235 * mempolicy, may not apply and should not be used for allocation policy.
3236 */
3237static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
3238{
3239 int nid_alloc, nid_here;
3240
Christoph Lameter765c4502006-09-27 01:50:08 -07003241 if (in_interrupt() || (flags & __GFP_THISNODE))
Paul Jacksonc61afb12006-03-24 03:16:08 -08003242 return NULL;
3243 nid_alloc = nid_here = numa_node_id();
3244 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
3245 nid_alloc = cpuset_mem_spread_node();
3246 else if (current->mempolicy)
3247 nid_alloc = slab_node(current->mempolicy);
3248 if (nid_alloc != nid_here)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003249 return ____cache_alloc_node(cachep, flags, nid_alloc);
Paul Jacksonc61afb12006-03-24 03:16:08 -08003250 return NULL;
3251}
3252
3253/*
Christoph Lameter765c4502006-09-27 01:50:08 -07003254 * Fallback function if there was no memory available and no objects on a
Christoph Lameter3c517a62006-12-06 20:33:29 -08003255 * certain node and fall back is permitted. First we scan all the
3256 * available nodelists for available objects. If that fails then we
3257 * perform an allocation without specifying a node. This allows the page
3258 * allocator to do its reclaim / fallback magic. We then insert the
3259 * slab into the proper nodelist and then allocate from it.
Christoph Lameter765c4502006-09-27 01:50:08 -07003260 */
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003261static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
Christoph Lameter765c4502006-09-27 01:50:08 -07003262{
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003263 struct zonelist *zonelist;
3264 gfp_t local_flags;
Mel Gormandd1a2392008-04-28 02:12:17 -07003265 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07003266 struct zone *zone;
3267 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003268 void *obj = NULL;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003269 int nid;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003270
3271 if (flags & __GFP_THISNODE)
3272 return NULL;
3273
Mel Gorman0e884602008-04-28 02:12:14 -07003274 zonelist = node_zonelist(slab_node(current->mempolicy), flags);
Christoph Lameter6cb06222007-10-16 01:25:41 -07003275 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Christoph Lameter765c4502006-09-27 01:50:08 -07003276
Christoph Lameter3c517a62006-12-06 20:33:29 -08003277retry:
3278 /*
3279 * Look through allowed nodes for objects available
3280 * from existing per node queues.
3281 */
Mel Gorman54a6eb52008-04-28 02:12:16 -07003282 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
3283 nid = zone_to_nid(zone);
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003284
Mel Gorman54a6eb52008-04-28 02:12:16 -07003285 if (cpuset_zone_allowed_hardwall(zone, flags) &&
Christoph Lameter3c517a62006-12-06 20:33:29 -08003286 cache->nodelists[nid] &&
Christoph Lameter481c5342008-06-21 16:46:35 -07003287 cache->nodelists[nid]->free_objects) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003288 obj = ____cache_alloc_node(cache,
3289 flags | GFP_THISNODE, nid);
Christoph Lameter481c5342008-06-21 16:46:35 -07003290 if (obj)
3291 break;
3292 }
Christoph Lameter3c517a62006-12-06 20:33:29 -08003293 }
3294
Christoph Lametercfce6602007-05-06 14:50:17 -07003295 if (!obj) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003296 /*
3297 * This allocation will be performed within the constraints
3298 * of the current cpuset / memory policy requirements.
3299 * We may trigger various forms of reclaim on the allowed
3300 * set and go into memory reserves if necessary.
3301 */
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003302 if (local_flags & __GFP_WAIT)
3303 local_irq_enable();
3304 kmem_flagcheck(cache, flags);
Mel Gorman6484eb32009-06-16 15:31:54 -07003305 obj = kmem_getpages(cache, local_flags, numa_node_id());
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003306 if (local_flags & __GFP_WAIT)
3307 local_irq_disable();
Christoph Lameter3c517a62006-12-06 20:33:29 -08003308 if (obj) {
3309 /*
3310 * Insert into the appropriate per node queues
3311 */
3312 nid = page_to_nid(virt_to_page(obj));
3313 if (cache_grow(cache, flags, nid, obj)) {
3314 obj = ____cache_alloc_node(cache,
3315 flags | GFP_THISNODE, nid);
3316 if (!obj)
3317 /*
3318 * Another processor may allocate the
3319 * objects in the slab since we are
3320 * not holding any locks.
3321 */
3322 goto retry;
3323 } else {
Hugh Dickinsb6a60452007-01-05 16:36:36 -08003324 /* cache_grow already freed obj */
Christoph Lameter3c517a62006-12-06 20:33:29 -08003325 obj = NULL;
3326 }
3327 }
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003328 }
Christoph Lameter765c4502006-09-27 01:50:08 -07003329 return obj;
3330}
3331
3332/*
Christoph Lametere498be72005-09-09 13:03:32 -07003333 * A interface to enable slab creation on nodeid
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003335static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003336 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07003337{
3338 struct list_head *entry;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003339 struct slab *slabp;
3340 struct kmem_list3 *l3;
3341 void *obj;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003342 int x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003344 l3 = cachep->nodelists[nodeid];
3345 BUG_ON(!l3);
Christoph Lametere498be72005-09-09 13:03:32 -07003346
Andrew Mortona737b3e2006-03-22 00:08:11 -08003347retry:
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003348 check_irq_off();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003349 spin_lock(&l3->list_lock);
3350 entry = l3->slabs_partial.next;
3351 if (entry == &l3->slabs_partial) {
3352 l3->free_touched = 1;
3353 entry = l3->slabs_free.next;
3354 if (entry == &l3->slabs_free)
3355 goto must_grow;
3356 }
Christoph Lametere498be72005-09-09 13:03:32 -07003357
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003358 slabp = list_entry(entry, struct slab, list);
3359 check_spinlock_acquired_node(cachep, nodeid);
3360 check_slabp(cachep, slabp);
Christoph Lametere498be72005-09-09 13:03:32 -07003361
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003362 STATS_INC_NODEALLOCS(cachep);
3363 STATS_INC_ACTIVE(cachep);
3364 STATS_SET_HIGH(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003365
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003366 BUG_ON(slabp->inuse == cachep->num);
Christoph Lametere498be72005-09-09 13:03:32 -07003367
Matthew Dobson78d382d2006-02-01 03:05:47 -08003368 obj = slab_get_obj(cachep, slabp, nodeid);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003369 check_slabp(cachep, slabp);
3370 l3->free_objects--;
3371 /* move slabp to correct slabp list: */
3372 list_del(&slabp->list);
Christoph Lametere498be72005-09-09 13:03:32 -07003373
Andrew Mortona737b3e2006-03-22 00:08:11 -08003374 if (slabp->free == BUFCTL_END)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003375 list_add(&slabp->list, &l3->slabs_full);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003376 else
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003377 list_add(&slabp->list, &l3->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07003378
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003379 spin_unlock(&l3->list_lock);
3380 goto done;
Christoph Lametere498be72005-09-09 13:03:32 -07003381
Andrew Mortona737b3e2006-03-22 00:08:11 -08003382must_grow:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003383 spin_unlock(&l3->list_lock);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003384 x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
Christoph Lameter765c4502006-09-27 01:50:08 -07003385 if (x)
3386 goto retry;
Christoph Lametere498be72005-09-09 13:03:32 -07003387
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003388 return fallback_alloc(cachep, flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003389
Andrew Mortona737b3e2006-03-22 00:08:11 -08003390done:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003391 return obj;
Christoph Lametere498be72005-09-09 13:03:32 -07003392}
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003393
3394/**
3395 * kmem_cache_alloc_node - Allocate an object on the specified node
3396 * @cachep: The cache to allocate from.
3397 * @flags: See kmalloc().
3398 * @nodeid: node number of the target node.
3399 * @caller: return address of caller, used for debug information
3400 *
3401 * Identical to kmem_cache_alloc but it will allocate memory on the given
3402 * node, which can improve the performance for cpu bound structures.
3403 *
3404 * Fallback to other node is possible if __GFP_THISNODE is not set.
3405 */
3406static __always_inline void *
3407__cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
3408 void *caller)
3409{
3410 unsigned long save_flags;
3411 void *ptr;
3412
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003413 flags &= gfp_allowed_mask;
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003414
Nick Piggincf40bd12009-01-21 08:12:39 +01003415 lockdep_trace_alloc(flags);
3416
Akinobu Mita773ff602008-12-23 19:37:01 +09003417 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003418 return NULL;
3419
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003420 cache_alloc_debugcheck_before(cachep, flags);
3421 local_irq_save(save_flags);
3422
Tim Blechmann8e15b792009-11-30 18:59:34 +01003423 if (nodeid == -1)
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003424 nodeid = numa_node_id();
3425
3426 if (unlikely(!cachep->nodelists[nodeid])) {
3427 /* Node not bootstrapped yet */
3428 ptr = fallback_alloc(cachep, flags);
3429 goto out;
3430 }
3431
3432 if (nodeid == numa_node_id()) {
3433 /*
3434 * Use the locally cached objects if possible.
3435 * However ____cache_alloc does not allow fallback
3436 * to other nodes. It may fail while we still have
3437 * objects on other nodes available.
3438 */
3439 ptr = ____cache_alloc(cachep, flags);
3440 if (ptr)
3441 goto out;
3442 }
3443 /* ___cache_alloc_node can fall back to other nodes */
3444 ptr = ____cache_alloc_node(cachep, flags, nodeid);
3445 out:
3446 local_irq_restore(save_flags);
3447 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
Catalin Marinasd5cff632009-06-11 13:22:40 +01003448 kmemleak_alloc_recursive(ptr, obj_size(cachep), 1, cachep->flags,
3449 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003450
Pekka Enbergc175eea2008-05-09 20:35:53 +02003451 if (likely(ptr))
3452 kmemcheck_slab_alloc(cachep, flags, ptr, obj_size(cachep));
3453
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003454 if (unlikely((flags & __GFP_ZERO) && ptr))
3455 memset(ptr, 0, obj_size(cachep));
3456
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003457 return ptr;
3458}
3459
3460static __always_inline void *
3461__do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
3462{
3463 void *objp;
3464
3465 if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
3466 objp = alternate_node_alloc(cache, flags);
3467 if (objp)
3468 goto out;
3469 }
3470 objp = ____cache_alloc(cache, flags);
3471
3472 /*
3473 * We may just have run out of memory on the local node.
3474 * ____cache_alloc_node() knows how to locate memory on other nodes
3475 */
3476 if (!objp)
3477 objp = ____cache_alloc_node(cache, flags, numa_node_id());
3478
3479 out:
3480 return objp;
3481}
3482#else
3483
3484static __always_inline void *
3485__do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
3486{
3487 return ____cache_alloc(cachep, flags);
3488}
3489
3490#endif /* CONFIG_NUMA */
3491
3492static __always_inline void *
3493__cache_alloc(struct kmem_cache *cachep, gfp_t flags, void *caller)
3494{
3495 unsigned long save_flags;
3496 void *objp;
3497
Benjamin Herrenschmidtdcce2842009-06-18 13:24:12 +10003498 flags &= gfp_allowed_mask;
Pekka Enberg7e85ee02009-06-12 14:03:06 +03003499
Nick Piggincf40bd12009-01-21 08:12:39 +01003500 lockdep_trace_alloc(flags);
3501
Akinobu Mita773ff602008-12-23 19:37:01 +09003502 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003503 return NULL;
3504
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003505 cache_alloc_debugcheck_before(cachep, flags);
3506 local_irq_save(save_flags);
3507 objp = __do_cache_alloc(cachep, flags);
3508 local_irq_restore(save_flags);
3509 objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
Catalin Marinasd5cff632009-06-11 13:22:40 +01003510 kmemleak_alloc_recursive(objp, obj_size(cachep), 1, cachep->flags,
3511 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003512 prefetchw(objp);
3513
Pekka Enbergc175eea2008-05-09 20:35:53 +02003514 if (likely(objp))
3515 kmemcheck_slab_alloc(cachep, flags, objp, obj_size(cachep));
3516
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003517 if (unlikely((flags & __GFP_ZERO) && objp))
3518 memset(objp, 0, obj_size(cachep));
3519
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003520 return objp;
3521}
Christoph Lametere498be72005-09-09 13:03:32 -07003522
3523/*
3524 * Caller needs to acquire correct kmem_list's list_lock
3525 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003526static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003527 int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528{
3529 int i;
Christoph Lametere498be72005-09-09 13:03:32 -07003530 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531
3532 for (i = 0; i < nr_objects; i++) {
3533 void *objp = objpp[i];
3534 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535
Pekka Enberg6ed5eb22006-02-01 03:05:49 -08003536 slabp = virt_to_slab(objp);
Christoph Lameterff694162005-09-22 21:44:02 -07003537 l3 = cachep->nodelists[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 list_del(&slabp->list);
Christoph Lameterff694162005-09-22 21:44:02 -07003539 check_spinlock_acquired_node(cachep, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 check_slabp(cachep, slabp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08003541 slab_put_obj(cachep, slabp, objp, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 STATS_DEC_ACTIVE(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003543 l3->free_objects++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 check_slabp(cachep, slabp);
3545
3546 /* fixup slab chains */
3547 if (slabp->inuse == 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07003548 if (l3->free_objects > l3->free_limit) {
3549 l3->free_objects -= cachep->num;
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07003550 /* No need to drop any previously held
3551 * lock here, even if we have a off-slab slab
3552 * descriptor it is guaranteed to come from
3553 * a different cache, refer to comments before
3554 * alloc_slabmgmt.
3555 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 slab_destroy(cachep, slabp);
3557 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07003558 list_add(&slabp->list, &l3->slabs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003559 }
3560 } else {
3561 /* Unconditionally move a slab to the end of the
3562 * partial list on free - maximum time for the
3563 * other objects to be freed, too.
3564 */
Christoph Lametere498be72005-09-09 13:03:32 -07003565 list_add_tail(&slabp->list, &l3->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 }
3567 }
3568}
3569
Pekka Enberg343e0d72006-02-01 03:05:50 -08003570static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571{
3572 int batchcount;
Christoph Lametere498be72005-09-09 13:03:32 -07003573 struct kmem_list3 *l3;
Christoph Lameterff694162005-09-22 21:44:02 -07003574 int node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575
3576 batchcount = ac->batchcount;
3577#if DEBUG
3578 BUG_ON(!batchcount || batchcount > ac->avail);
3579#endif
3580 check_irq_off();
Christoph Lameterff694162005-09-22 21:44:02 -07003581 l3 = cachep->nodelists[node];
Ingo Molnar873623d2006-07-13 14:44:38 +02003582 spin_lock(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07003583 if (l3->shared) {
3584 struct array_cache *shared_array = l3->shared;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003585 int max = shared_array->limit - shared_array->avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 if (max) {
3587 if (batchcount > max)
3588 batchcount = max;
Christoph Lametere498be72005-09-09 13:03:32 -07003589 memcpy(&(shared_array->entry[shared_array->avail]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003590 ac->entry, sizeof(void *) * batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 shared_array->avail += batchcount;
3592 goto free_done;
3593 }
3594 }
3595
Christoph Lameterff694162005-09-22 21:44:02 -07003596 free_block(cachep, ac->entry, batchcount, node);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003597free_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598#if STATS
3599 {
3600 int i = 0;
3601 struct list_head *p;
3602
Christoph Lametere498be72005-09-09 13:03:32 -07003603 p = l3->slabs_free.next;
3604 while (p != &(l3->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 struct slab *slabp;
3606
3607 slabp = list_entry(p, struct slab, list);
3608 BUG_ON(slabp->inuse);
3609
3610 i++;
3611 p = p->next;
3612 }
3613 STATS_SET_FREEABLE(cachep, i);
3614 }
3615#endif
Christoph Lametere498be72005-09-09 13:03:32 -07003616 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 ac->avail -= batchcount;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003618 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619}
3620
3621/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08003622 * Release an obj back to its cache. If the obj has a constructed state, it must
3623 * be in this state _before_ it is released. Called with disabled ints.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 */
Ingo Molnar873623d2006-07-13 14:44:38 +02003625static inline void __cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626{
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003627 struct array_cache *ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628
3629 check_irq_off();
Catalin Marinasd5cff632009-06-11 13:22:40 +01003630 kmemleak_free_recursive(objp, cachep->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));
3632
Pekka Enbergc175eea2008-05-09 20:35:53 +02003633 kmemcheck_slab_free(cachep, objp, obj_size(cachep));
3634
Siddha, Suresh B1807a1a2007-08-22 14:01:49 -07003635 /*
3636 * Skip calling cache_free_alien() when the platform is not numa.
3637 * This will avoid cache misses that happen while accessing slabp (which
3638 * is per page memory reference) to get nodeid. Instead use a global
3639 * variable to skip the call, which is mostly likely to be present in
3640 * the cache.
3641 */
Mel Gormanb6e68bc2009-06-16 15:32:16 -07003642 if (nr_online_nodes > 1 && cache_free_alien(cachep, objp))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07003643 return;
Christoph Lametere498be72005-09-09 13:03:32 -07003644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 if (likely(ac->avail < ac->limit)) {
3646 STATS_INC_FREEHIT(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003647 ac->entry[ac->avail++] = objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 return;
3649 } else {
3650 STATS_INC_FREEMISS(cachep);
3651 cache_flusharray(cachep, ac);
Christoph Lametere498be72005-09-09 13:03:32 -07003652 ac->entry[ac->avail++] = objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 }
3654}
3655
3656/**
3657 * kmem_cache_alloc - Allocate an object
3658 * @cachep: The cache to allocate from.
3659 * @flags: See kmalloc().
3660 *
3661 * Allocate an object from this cache. The flags are only relevant
3662 * if the cache has no available objects.
3663 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003664void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665{
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003666 void *ret = __cache_alloc(cachep, flags, __builtin_return_address(0));
3667
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003668 trace_kmem_cache_alloc(_RET_IP_, ret,
3669 obj_size(cachep), cachep->buffer_size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003670
3671 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672}
3673EXPORT_SYMBOL(kmem_cache_alloc);
3674
Li Zefan0f24f122009-12-11 15:45:30 +08003675#ifdef CONFIG_TRACING
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003676void *kmem_cache_alloc_notrace(struct kmem_cache *cachep, gfp_t flags)
3677{
3678 return __cache_alloc(cachep, flags, __builtin_return_address(0));
3679}
3680EXPORT_SYMBOL(kmem_cache_alloc_notrace);
3681#endif
3682
Linus Torvalds1da177e2005-04-16 15:20:36 -07003683/**
Randy Dunlap76824862008-03-19 17:00:40 -07003684 * kmem_ptr_validate - check if an untrusted pointer might be a slab entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 * @cachep: the cache we're checking against
3686 * @ptr: pointer to validate
3687 *
Randy Dunlap76824862008-03-19 17:00:40 -07003688 * This verifies that the untrusted pointer looks sane;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003689 * it is _not_ a guarantee that the pointer is actually
3690 * part of the slab cache in question, but it at least
3691 * validates that the pointer can be dereferenced and
3692 * looks half-way sane.
3693 *
3694 * Currently only used for dentry validation.
3695 */
Christoph Lameterb7f869a2006-12-22 01:06:44 -08003696int kmem_ptr_validate(struct kmem_cache *cachep, const void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003698 unsigned long addr = (unsigned long)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 unsigned long min_addr = PAGE_OFFSET;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003700 unsigned long align_mask = BYTES_PER_WORD - 1;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003701 unsigned long size = cachep->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 struct page *page;
3703
3704 if (unlikely(addr < min_addr))
3705 goto out;
3706 if (unlikely(addr > (unsigned long)high_memory - size))
3707 goto out;
3708 if (unlikely(addr & align_mask))
3709 goto out;
3710 if (unlikely(!kern_addr_valid(addr)))
3711 goto out;
3712 if (unlikely(!kern_addr_valid(addr + size - 1)))
3713 goto out;
3714 page = virt_to_page(ptr);
3715 if (unlikely(!PageSlab(page)))
3716 goto out;
Pekka Enberg065d41c2005-11-13 16:06:46 -08003717 if (unlikely(page_get_cache(page) != cachep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 goto out;
3719 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003720out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 return 0;
3722}
3723
3724#ifdef CONFIG_NUMA
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003725void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
3726{
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003727 void *ret = __cache_alloc_node(cachep, flags, nodeid,
3728 __builtin_return_address(0));
3729
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003730 trace_kmem_cache_alloc_node(_RET_IP_, ret,
3731 obj_size(cachep), cachep->buffer_size,
3732 flags, nodeid);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003733
3734 return ret;
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003735}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736EXPORT_SYMBOL(kmem_cache_alloc_node);
3737
Li Zefan0f24f122009-12-11 15:45:30 +08003738#ifdef CONFIG_TRACING
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003739void *kmem_cache_alloc_node_notrace(struct kmem_cache *cachep,
3740 gfp_t flags,
3741 int nodeid)
3742{
3743 return __cache_alloc_node(cachep, flags, nodeid,
3744 __builtin_return_address(0));
3745}
3746EXPORT_SYMBOL(kmem_cache_alloc_node_notrace);
3747#endif
3748
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003749static __always_inline void *
3750__do_kmalloc_node(size_t size, gfp_t flags, int node, void *caller)
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003751{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003752 struct kmem_cache *cachep;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003753 void *ret;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003754
3755 cachep = kmem_find_general_cachep(size, flags);
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003756 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3757 return cachep;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003758 ret = kmem_cache_alloc_node_notrace(cachep, flags, node);
3759
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003760 trace_kmalloc_node((unsigned long) caller, ret,
3761 size, cachep->buffer_size, flags, node);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003762
3763 return ret;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003764}
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003765
Li Zefan0bb38a52009-12-11 15:45:50 +08003766#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003767void *__kmalloc_node(size_t size, gfp_t flags, int node)
3768{
3769 return __do_kmalloc_node(size, flags, node,
3770 __builtin_return_address(0));
3771}
Christoph Hellwigdbe5e692006-09-25 23:31:36 -07003772EXPORT_SYMBOL(__kmalloc_node);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003773
3774void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003775 int node, unsigned long caller)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003776{
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003777 return __do_kmalloc_node(size, flags, node, (void *)caller);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003778}
3779EXPORT_SYMBOL(__kmalloc_node_track_caller);
3780#else
3781void *__kmalloc_node(size_t size, gfp_t flags, int node)
3782{
3783 return __do_kmalloc_node(size, flags, node, NULL);
3784}
3785EXPORT_SYMBOL(__kmalloc_node);
Li Zefan0bb38a52009-12-11 15:45:50 +08003786#endif /* CONFIG_DEBUG_SLAB || CONFIG_TRACING */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003787#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788
3789/**
Paul Drynoff800590f2006-06-23 02:03:48 -07003790 * __do_kmalloc - allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 * @size: how many bytes of memory are required.
Paul Drynoff800590f2006-06-23 02:03:48 -07003792 * @flags: the type of memory to allocate (see kmalloc).
Randy Dunlap911851e2006-03-22 00:08:14 -08003793 * @caller: function caller for debug tracking of the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 */
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003795static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
3796 void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003798 struct kmem_cache *cachep;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003799 void *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003801 /* If you want to save a few bytes .text space: replace
3802 * __ with kmem_.
3803 * Then kmalloc uses the uninlined functions instead of the inline
3804 * functions.
3805 */
3806 cachep = __find_general_cachep(size, flags);
Linus Torvaldsa5c96d82007-07-19 13:17:15 -07003807 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3808 return cachep;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003809 ret = __cache_alloc(cachep, flags, caller);
3810
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003811 trace_kmalloc((unsigned long) caller, ret,
3812 size, cachep->buffer_size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003813
3814 return ret;
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003815}
3816
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003817
Li Zefan0bb38a52009-12-11 15:45:50 +08003818#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_TRACING)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003819void *__kmalloc(size_t size, gfp_t flags)
3820{
Al Viro871751e2006-03-25 03:06:39 -08003821 return __do_kmalloc(size, flags, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822}
3823EXPORT_SYMBOL(__kmalloc);
3824
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003825void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003826{
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003827 return __do_kmalloc(size, flags, (void *)caller);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003828}
3829EXPORT_SYMBOL(__kmalloc_track_caller);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003830
3831#else
3832void *__kmalloc(size_t size, gfp_t flags)
3833{
3834 return __do_kmalloc(size, flags, NULL);
3835}
3836EXPORT_SYMBOL(__kmalloc);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003837#endif
3838
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839/**
3840 * kmem_cache_free - Deallocate an object
3841 * @cachep: The cache the allocation was from.
3842 * @objp: The previously allocated object.
3843 *
3844 * Free an object which was previously allocated from this
3845 * cache.
3846 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003847void kmem_cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848{
3849 unsigned long flags;
3850
3851 local_irq_save(flags);
Ingo Molnar898552c2007-02-10 01:44:57 -08003852 debug_check_no_locks_freed(objp, obj_size(cachep));
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07003853 if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
3854 debug_check_no_obj_freed(objp, obj_size(cachep));
Ingo Molnar873623d2006-07-13 14:44:38 +02003855 __cache_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 local_irq_restore(flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003857
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003858 trace_kmem_cache_free(_RET_IP_, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859}
3860EXPORT_SYMBOL(kmem_cache_free);
3861
3862/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 * kfree - free previously allocated memory
3864 * @objp: pointer returned by kmalloc.
3865 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07003866 * If @objp is NULL, no operation is performed.
3867 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 * Don't free memory not originally allocated by kmalloc()
3869 * or you will run into trouble.
3870 */
3871void kfree(const void *objp)
3872{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003873 struct kmem_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874 unsigned long flags;
3875
Pekka Enberg2121db72009-03-25 11:05:57 +02003876 trace_kfree(_RET_IP_, objp);
3877
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003878 if (unlikely(ZERO_OR_NULL_PTR(objp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003879 return;
3880 local_irq_save(flags);
3881 kfree_debugcheck(objp);
Pekka Enberg6ed5eb22006-02-01 03:05:49 -08003882 c = virt_to_cache(objp);
Ingo Molnarf9b84042006-06-27 02:54:49 -07003883 debug_check_no_locks_freed(objp, obj_size(c));
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07003884 debug_check_no_obj_freed(objp, obj_size(c));
Ingo Molnar873623d2006-07-13 14:44:38 +02003885 __cache_free(c, (void *)objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 local_irq_restore(flags);
3887}
3888EXPORT_SYMBOL(kfree);
3889
Pekka Enberg343e0d72006-02-01 03:05:50 -08003890unsigned int kmem_cache_size(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003892 return obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893}
3894EXPORT_SYMBOL(kmem_cache_size);
3895
Pekka Enberg343e0d72006-02-01 03:05:50 -08003896const char *kmem_cache_name(struct kmem_cache *cachep)
Arnaldo Carvalho de Melo19449722005-06-18 22:46:19 -07003897{
3898 return cachep->name;
3899}
3900EXPORT_SYMBOL_GPL(kmem_cache_name);
3901
Christoph Lametere498be72005-09-09 13:03:32 -07003902/*
Simon Arlott183ff222007-10-20 01:27:18 +02003903 * This initializes kmem_list3 or resizes various caches for all nodes.
Christoph Lametere498be72005-09-09 13:03:32 -07003904 */
Pekka Enberg83b519e2009-06-10 19:40:04 +03003905static int alloc_kmemlist(struct kmem_cache *cachep, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -07003906{
3907 int node;
3908 struct kmem_list3 *l3;
Christoph Lametercafeb022006-03-25 03:06:46 -08003909 struct array_cache *new_shared;
Paul Menage3395ee02006-12-06 20:32:16 -08003910 struct array_cache **new_alien = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003911
Mel Gorman9c09a952008-01-24 05:49:54 -08003912 for_each_online_node(node) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003913
Paul Menage3395ee02006-12-06 20:32:16 -08003914 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03003915 new_alien = alloc_alien_cache(node, cachep->limit, gfp);
Paul Menage3395ee02006-12-06 20:32:16 -08003916 if (!new_alien)
3917 goto fail;
3918 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003919
Eric Dumazet63109842007-05-06 14:49:28 -07003920 new_shared = NULL;
3921 if (cachep->shared) {
3922 new_shared = alloc_arraycache(node,
Christoph Lameter0718dc22006-03-25 03:06:47 -08003923 cachep->shared*cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003924 0xbaadf00d, gfp);
Eric Dumazet63109842007-05-06 14:49:28 -07003925 if (!new_shared) {
3926 free_alien_cache(new_alien);
3927 goto fail;
3928 }
Christoph Lameter0718dc22006-03-25 03:06:47 -08003929 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003930
Andrew Mortona737b3e2006-03-22 00:08:11 -08003931 l3 = cachep->nodelists[node];
3932 if (l3) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003933 struct array_cache *shared = l3->shared;
3934
Christoph Lametere498be72005-09-09 13:03:32 -07003935 spin_lock_irq(&l3->list_lock);
3936
Christoph Lametercafeb022006-03-25 03:06:46 -08003937 if (shared)
Christoph Lameter0718dc22006-03-25 03:06:47 -08003938 free_block(cachep, shared->entry,
3939 shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07003940
Christoph Lametercafeb022006-03-25 03:06:46 -08003941 l3->shared = new_shared;
3942 if (!l3->alien) {
Christoph Lametere498be72005-09-09 13:03:32 -07003943 l3->alien = new_alien;
3944 new_alien = NULL;
3945 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003946 l3->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003947 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07003948 spin_unlock_irq(&l3->list_lock);
Christoph Lametercafeb022006-03-25 03:06:46 -08003949 kfree(shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003950 free_alien_cache(new_alien);
3951 continue;
3952 }
Pekka Enberg83b519e2009-06-10 19:40:04 +03003953 l3 = kmalloc_node(sizeof(struct kmem_list3), gfp, node);
Christoph Lameter0718dc22006-03-25 03:06:47 -08003954 if (!l3) {
3955 free_alien_cache(new_alien);
3956 kfree(new_shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003957 goto fail;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003958 }
Christoph Lametere498be72005-09-09 13:03:32 -07003959
3960 kmem_list3_init(l3);
3961 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
Andrew Mortona737b3e2006-03-22 00:08:11 -08003962 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lametercafeb022006-03-25 03:06:46 -08003963 l3->shared = new_shared;
Christoph Lametere498be72005-09-09 13:03:32 -07003964 l3->alien = new_alien;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003965 l3->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003966 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07003967 cachep->nodelists[node] = l3;
3968 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003969 return 0;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003970
Andrew Mortona737b3e2006-03-22 00:08:11 -08003971fail:
Christoph Lameter0718dc22006-03-25 03:06:47 -08003972 if (!cachep->next.next) {
3973 /* Cache is not active yet. Roll back what we did */
3974 node--;
3975 while (node >= 0) {
3976 if (cachep->nodelists[node]) {
3977 l3 = cachep->nodelists[node];
3978
3979 kfree(l3->shared);
3980 free_alien_cache(l3->alien);
3981 kfree(l3);
3982 cachep->nodelists[node] = NULL;
3983 }
3984 node--;
3985 }
3986 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003987 return -ENOMEM;
Christoph Lametere498be72005-09-09 13:03:32 -07003988}
3989
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990struct ccupdate_struct {
Pekka Enberg343e0d72006-02-01 03:05:50 -08003991 struct kmem_cache *cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 struct array_cache *new[NR_CPUS];
3993};
3994
3995static void do_ccupdate_local(void *info)
3996{
Andrew Mortona737b3e2006-03-22 00:08:11 -08003997 struct ccupdate_struct *new = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 struct array_cache *old;
3999
4000 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08004001 old = cpu_cache_get(new->cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07004002
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
4004 new->new[smp_processor_id()] = old;
4005}
4006
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -08004007/* Always called with the cache_chain_mutex held */
Andrew Mortona737b3e2006-03-22 00:08:11 -08004008static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03004009 int batchcount, int shared, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010{
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004011 struct ccupdate_struct *new;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07004012 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004013
Pekka Enberg83b519e2009-06-10 19:40:04 +03004014 new = kzalloc(sizeof(*new), gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004015 if (!new)
4016 return -ENOMEM;
4017
Christoph Lametere498be72005-09-09 13:03:32 -07004018 for_each_online_cpu(i) {
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004019 new->new[i] = alloc_arraycache(cpu_to_node(i), limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03004020 batchcount, gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004021 if (!new->new[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004022 for (i--; i >= 0; i--)
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004023 kfree(new->new[i]);
4024 kfree(new);
Christoph Lametere498be72005-09-09 13:03:32 -07004025 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 }
4027 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004028 new->cachep = cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Jens Axboe15c8b6c2008-05-09 09:39:44 +02004030 on_each_cpu(do_ccupdate_local, (void *)new, 1);
Christoph Lametere498be72005-09-09 13:03:32 -07004031
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 check_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 cachep->batchcount = batchcount;
4034 cachep->limit = limit;
Christoph Lametere498be72005-09-09 13:03:32 -07004035 cachep->shared = shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036
Christoph Lametere498be72005-09-09 13:03:32 -07004037 for_each_online_cpu(i) {
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004038 struct array_cache *ccold = new->new[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 if (!ccold)
4040 continue;
Christoph Lametere498be72005-09-09 13:03:32 -07004041 spin_lock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
Christoph Lameterff694162005-09-22 21:44:02 -07004042 free_block(cachep, ccold->entry, ccold->avail, cpu_to_node(i));
Christoph Lametere498be72005-09-09 13:03:32 -07004043 spin_unlock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 kfree(ccold);
4045 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004046 kfree(new);
Pekka Enberg83b519e2009-06-10 19:40:04 +03004047 return alloc_kmemlist(cachep, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048}
4049
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -08004050/* Called with cache_chain_mutex held always */
Pekka Enberg83b519e2009-06-10 19:40:04 +03004051static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052{
4053 int err;
4054 int limit, shared;
4055
Andrew Mortona737b3e2006-03-22 00:08:11 -08004056 /*
4057 * The head array serves three purposes:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 * - create a LIFO ordering, i.e. return objects that are cache-warm
4059 * - reduce the number of spinlock operations.
Andrew Mortona737b3e2006-03-22 00:08:11 -08004060 * - reduce the number of linked list operations on the slab and
Linus Torvalds1da177e2005-04-16 15:20:36 -07004061 * bufctl chains: array operations are cheaper.
4062 * The numbers are guessed, we should auto-tune as described by
4063 * Bonwick.
4064 */
Manfred Spraul3dafccf2006-02-01 03:05:42 -08004065 if (cachep->buffer_size > 131072)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066 limit = 1;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08004067 else if (cachep->buffer_size > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 limit = 8;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08004069 else if (cachep->buffer_size > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 limit = 24;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08004071 else if (cachep->buffer_size > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 limit = 54;
4073 else
4074 limit = 120;
4075
Andrew Mortona737b3e2006-03-22 00:08:11 -08004076 /*
4077 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 * allocation behaviour: Most allocs on one cpu, most free operations
4079 * on another cpu. For these cases, an efficient object passing between
4080 * cpus is necessary. This is provided by a shared array. The array
4081 * replaces Bonwick's magazine layer.
4082 * On uniprocessor, it's functionally equivalent (but less efficient)
4083 * to a larger limit. Thus disabled by default.
4084 */
4085 shared = 0;
Eric Dumazet364fbb22007-05-06 14:49:27 -07004086 if (cachep->buffer_size <= PAGE_SIZE && num_possible_cpus() > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 shared = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088
4089#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08004090 /*
4091 * With debugging enabled, large batchcount lead to excessively long
4092 * periods with disabled local interrupts. Limit the batchcount
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 */
4094 if (limit > 32)
4095 limit = 32;
4096#endif
Pekka Enberg83b519e2009-06-10 19:40:04 +03004097 err = do_tune_cpucache(cachep, limit, (limit + 1) / 2, shared, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 if (err)
4099 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004100 cachep->name, -err);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07004101 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102}
4103
Christoph Lameter1b552532006-03-22 00:09:07 -08004104/*
4105 * Drain an array if it contains any elements taking the l3 lock only if
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004106 * necessary. Note that the l3 listlock also protects the array_cache
4107 * if drain_array() is used on the shared array.
Christoph Lameter1b552532006-03-22 00:09:07 -08004108 */
4109void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
4110 struct array_cache *ac, int force, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111{
4112 int tofree;
4113
Christoph Lameter1b552532006-03-22 00:09:07 -08004114 if (!ac || !ac->avail)
4115 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 if (ac->touched && !force) {
4117 ac->touched = 0;
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004118 } else {
Christoph Lameter1b552532006-03-22 00:09:07 -08004119 spin_lock_irq(&l3->list_lock);
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004120 if (ac->avail) {
4121 tofree = force ? ac->avail : (ac->limit + 4) / 5;
4122 if (tofree > ac->avail)
4123 tofree = (ac->avail + 1) / 2;
4124 free_block(cachep, ac->entry, tofree, node);
4125 ac->avail -= tofree;
4126 memmove(ac->entry, &(ac->entry[tofree]),
4127 sizeof(void *) * ac->avail);
4128 }
Christoph Lameter1b552532006-03-22 00:09:07 -08004129 spin_unlock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 }
4131}
4132
4133/**
4134 * cache_reap - Reclaim memory from caches.
Randy Dunlap05fb6bf2007-02-28 20:12:13 -08004135 * @w: work descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 *
4137 * Called from workqueue/eventd every few seconds.
4138 * Purpose:
4139 * - clear the per-cpu caches for this CPU.
4140 * - return freeable pages to the main free memory pool.
4141 *
Andrew Mortona737b3e2006-03-22 00:08:11 -08004142 * If we cannot acquire the cache chain mutex then just give up - we'll try
4143 * again on the next iteration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004144 */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004145static void cache_reap(struct work_struct *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146{
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004147 struct kmem_cache *searchp;
Christoph Lametere498be72005-09-09 13:03:32 -07004148 struct kmem_list3 *l3;
Christoph Lameteraab22072006-03-22 00:09:06 -08004149 int node = numa_node_id();
Jean Delvarebf6aede2009-04-02 16:56:54 -07004150 struct delayed_work *work = to_delayed_work(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004152 if (!mutex_trylock(&cache_chain_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 /* Give up. Setup the next iteration. */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004154 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004156 list_for_each_entry(searchp, &cache_chain, next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 check_irq_on();
4158
Christoph Lameter35386e32006-03-22 00:09:05 -08004159 /*
4160 * We only take the l3 lock if absolutely necessary and we
4161 * have established with reasonable certainty that
4162 * we can do some work if the lock was obtained.
4163 */
Christoph Lameteraab22072006-03-22 00:09:06 -08004164 l3 = searchp->nodelists[node];
Christoph Lameter35386e32006-03-22 00:09:05 -08004165
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004166 reap_alien(searchp, l3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
Christoph Lameteraab22072006-03-22 00:09:06 -08004168 drain_array(searchp, l3, cpu_cache_get(searchp), 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
Christoph Lameter35386e32006-03-22 00:09:05 -08004170 /*
4171 * These are racy checks but it does not matter
4172 * if we skip one check or scan twice.
4173 */
Christoph Lametere498be72005-09-09 13:03:32 -07004174 if (time_after(l3->next_reap, jiffies))
Christoph Lameter35386e32006-03-22 00:09:05 -08004175 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176
Christoph Lametere498be72005-09-09 13:03:32 -07004177 l3->next_reap = jiffies + REAPTIMEOUT_LIST3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178
Christoph Lameteraab22072006-03-22 00:09:06 -08004179 drain_array(searchp, l3, l3->shared, 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
Christoph Lametered11d9e2006-06-30 01:55:45 -07004181 if (l3->free_touched)
Christoph Lametere498be72005-09-09 13:03:32 -07004182 l3->free_touched = 0;
Christoph Lametered11d9e2006-06-30 01:55:45 -07004183 else {
4184 int freed;
4185
4186 freed = drain_freelist(searchp, l3, (l3->free_limit +
4187 5 * searchp->num - 1) / (5 * searchp->num));
4188 STATS_ADD_REAPED(searchp, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 }
Christoph Lameter35386e32006-03-22 00:09:05 -08004190next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 cond_resched();
4192 }
4193 check_irq_on();
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004194 mutex_unlock(&cache_chain_mutex);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004195 next_reap_node();
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004196out:
Andrew Mortona737b3e2006-03-22 00:08:11 -08004197 /* Set up the next iteration */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004198 schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_CPUC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199}
4200
Linus Torvalds158a9622008-01-02 13:04:48 -08004201#ifdef CONFIG_SLABINFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
Pekka Enberg85289f92006-01-08 01:00:36 -08004203static void print_slabinfo_header(struct seq_file *m)
4204{
4205 /*
4206 * Output format version, so at least we can change it
4207 * without _too_ many complaints.
4208 */
4209#if STATS
4210 seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
4211#else
4212 seq_puts(m, "slabinfo - version: 2.1\n");
4213#endif
4214 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
4215 "<objperslab> <pagesperslab>");
4216 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
4217 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
4218#if STATS
4219 seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004220 "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
Pekka Enberg85289f92006-01-08 01:00:36 -08004221 seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
4222#endif
4223 seq_putc(m, '\n');
4224}
4225
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226static void *s_start(struct seq_file *m, loff_t *pos)
4227{
4228 loff_t n = *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004230 mutex_lock(&cache_chain_mutex);
Pekka Enberg85289f92006-01-08 01:00:36 -08004231 if (!n)
4232 print_slabinfo_header(m);
Pavel Emelianovb92151b2007-07-15 23:38:04 -07004233
4234 return seq_list_start(&cache_chain, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235}
4236
4237static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4238{
Pavel Emelianovb92151b2007-07-15 23:38:04 -07004239 return seq_list_next(p, &cache_chain, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004240}
4241
4242static void s_stop(struct seq_file *m, void *p)
4243{
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004244 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245}
4246
4247static int s_show(struct seq_file *m, void *p)
4248{
Pavel Emelianovb92151b2007-07-15 23:38:04 -07004249 struct kmem_cache *cachep = list_entry(p, struct kmem_cache, next);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004250 struct slab *slabp;
4251 unsigned long active_objs;
4252 unsigned long num_objs;
4253 unsigned long active_slabs = 0;
4254 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004255 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 char *error = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07004257 int node;
4258 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 active_objs = 0;
4261 num_slabs = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004262 for_each_online_node(node) {
4263 l3 = cachep->nodelists[node];
4264 if (!l3)
4265 continue;
4266
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004267 check_irq_on();
4268 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07004269
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004270 list_for_each_entry(slabp, &l3->slabs_full, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004271 if (slabp->inuse != cachep->num && !error)
4272 error = "slabs_full accounting error";
4273 active_objs += cachep->num;
4274 active_slabs++;
4275 }
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004276 list_for_each_entry(slabp, &l3->slabs_partial, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004277 if (slabp->inuse == cachep->num && !error)
4278 error = "slabs_partial inuse accounting error";
4279 if (!slabp->inuse && !error)
4280 error = "slabs_partial/inuse accounting error";
4281 active_objs += slabp->inuse;
4282 active_slabs++;
4283 }
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004284 list_for_each_entry(slabp, &l3->slabs_free, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004285 if (slabp->inuse && !error)
4286 error = "slabs_free/inuse accounting error";
4287 num_slabs++;
4288 }
4289 free_objects += l3->free_objects;
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08004290 if (l3->shared)
4291 shared_avail += l3->shared->avail;
Christoph Lametere498be72005-09-09 13:03:32 -07004292
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004293 spin_unlock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004295 num_slabs += active_slabs;
4296 num_objs = num_slabs * cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07004297 if (num_objs - active_objs != free_objects && !error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 error = "free_objects accounting error";
4299
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004300 name = cachep->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301 if (error)
4302 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
4303
4304 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
Manfred Spraul3dafccf2006-02-01 03:05:42 -08004305 name, active_objs, num_objs, cachep->buffer_size,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004306 cachep->num, (1 << cachep->gfporder));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 seq_printf(m, " : tunables %4u %4u %4u",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004308 cachep->limit, cachep->batchcount, cachep->shared);
Christoph Lametere498be72005-09-09 13:03:32 -07004309 seq_printf(m, " : slabdata %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004310 active_slabs, num_slabs, shared_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311#if STATS
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004312 { /* list3 stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 unsigned long high = cachep->high_mark;
4314 unsigned long allocs = cachep->num_allocations;
4315 unsigned long grown = cachep->grown;
4316 unsigned long reaped = cachep->reaped;
4317 unsigned long errors = cachep->errors;
4318 unsigned long max_freeable = cachep->max_freeable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 unsigned long node_allocs = cachep->node_allocs;
Christoph Lametere498be72005-09-09 13:03:32 -07004320 unsigned long node_frees = cachep->node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004321 unsigned long overflows = cachep->node_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322
Christoph Lametere498be72005-09-09 13:03:32 -07004323 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu \
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004324 %4lu %4lu %4lu %4lu %4lu", allocs, high, grown,
Andrew Mortona737b3e2006-03-22 00:08:11 -08004325 reaped, errors, max_freeable, node_allocs,
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004326 node_frees, overflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 }
4328 /* cpu stats */
4329 {
4330 unsigned long allochit = atomic_read(&cachep->allochit);
4331 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
4332 unsigned long freehit = atomic_read(&cachep->freehit);
4333 unsigned long freemiss = atomic_read(&cachep->freemiss);
4334
4335 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004336 allochit, allocmiss, freehit, freemiss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 }
4338#endif
4339 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340 return 0;
4341}
4342
4343/*
4344 * slabinfo_op - iterator that generates /proc/slabinfo
4345 *
4346 * Output layout:
4347 * cache-name
4348 * num-active-objs
4349 * total-objs
4350 * object size
4351 * num-active-slabs
4352 * total-slabs
4353 * num-pages-per-slab
4354 * + further values on SMP and with statistics enabled
4355 */
4356
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04004357static const struct seq_operations slabinfo_op = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004358 .start = s_start,
4359 .next = s_next,
4360 .stop = s_stop,
4361 .show = s_show,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362};
4363
4364#define MAX_SLABINFO_WRITE 128
4365/**
4366 * slabinfo_write - Tuning for the slab allocator
4367 * @file: unused
4368 * @buffer: user buffer
4369 * @count: data length
4370 * @ppos: unused
4371 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004372ssize_t slabinfo_write(struct file *file, const char __user * buffer,
4373 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004375 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 int limit, batchcount, shared, res;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004377 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004378
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 if (count > MAX_SLABINFO_WRITE)
4380 return -EINVAL;
4381 if (copy_from_user(&kbuf, buffer, count))
4382 return -EFAULT;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004383 kbuf[MAX_SLABINFO_WRITE] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384
4385 tmp = strchr(kbuf, ' ');
4386 if (!tmp)
4387 return -EINVAL;
4388 *tmp = '\0';
4389 tmp++;
4390 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
4391 return -EINVAL;
4392
4393 /* Find the cache in the chain of caches. */
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004394 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395 res = -EINVAL;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004396 list_for_each_entry(cachep, &cache_chain, next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 if (!strcmp(cachep->name, kbuf)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08004398 if (limit < 1 || batchcount < 1 ||
4399 batchcount > limit || shared < 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07004400 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004401 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07004402 res = do_tune_cpucache(cachep, limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03004403 batchcount, shared,
4404 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 }
4406 break;
4407 }
4408 }
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004409 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 if (res >= 0)
4411 res = count;
4412 return res;
4413}
Al Viro871751e2006-03-25 03:06:39 -08004414
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04004415static int slabinfo_open(struct inode *inode, struct file *file)
4416{
4417 return seq_open(file, &slabinfo_op);
4418}
4419
4420static const struct file_operations proc_slabinfo_operations = {
4421 .open = slabinfo_open,
4422 .read = seq_read,
4423 .write = slabinfo_write,
4424 .llseek = seq_lseek,
4425 .release = seq_release,
4426};
4427
Al Viro871751e2006-03-25 03:06:39 -08004428#ifdef CONFIG_DEBUG_SLAB_LEAK
4429
4430static void *leaks_start(struct seq_file *m, loff_t *pos)
4431{
Al Viro871751e2006-03-25 03:06:39 -08004432 mutex_lock(&cache_chain_mutex);
Pavel Emelianovb92151b2007-07-15 23:38:04 -07004433 return seq_list_start(&cache_chain, *pos);
Al Viro871751e2006-03-25 03:06:39 -08004434}
4435
4436static inline int add_caller(unsigned long *n, unsigned long v)
4437{
4438 unsigned long *p;
4439 int l;
4440 if (!v)
4441 return 1;
4442 l = n[1];
4443 p = n + 2;
4444 while (l) {
4445 int i = l/2;
4446 unsigned long *q = p + 2 * i;
4447 if (*q == v) {
4448 q[1]++;
4449 return 1;
4450 }
4451 if (*q > v) {
4452 l = i;
4453 } else {
4454 p = q + 2;
4455 l -= i + 1;
4456 }
4457 }
4458 if (++n[1] == n[0])
4459 return 0;
4460 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4461 p[0] = v;
4462 p[1] = 1;
4463 return 1;
4464}
4465
4466static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
4467{
4468 void *p;
4469 int i;
4470 if (n[0] == n[1])
4471 return;
4472 for (i = 0, p = s->s_mem; i < c->num; i++, p += c->buffer_size) {
4473 if (slab_bufctl(s)[i] != BUFCTL_ACTIVE)
4474 continue;
4475 if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
4476 return;
4477 }
4478}
4479
4480static void show_symbol(struct seq_file *m, unsigned long address)
4481{
4482#ifdef CONFIG_KALLSYMS
Al Viro871751e2006-03-25 03:06:39 -08004483 unsigned long offset, size;
Tejun Heo9281ace2007-07-17 04:03:51 -07004484 char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
Al Viro871751e2006-03-25 03:06:39 -08004485
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004486 if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
Al Viro871751e2006-03-25 03:06:39 -08004487 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004488 if (modname[0])
Al Viro871751e2006-03-25 03:06:39 -08004489 seq_printf(m, " [%s]", modname);
4490 return;
4491 }
4492#endif
4493 seq_printf(m, "%p", (void *)address);
4494}
4495
4496static int leaks_show(struct seq_file *m, void *p)
4497{
Pavel Emelianovb92151b2007-07-15 23:38:04 -07004498 struct kmem_cache *cachep = list_entry(p, struct kmem_cache, next);
Al Viro871751e2006-03-25 03:06:39 -08004499 struct slab *slabp;
4500 struct kmem_list3 *l3;
4501 const char *name;
4502 unsigned long *n = m->private;
4503 int node;
4504 int i;
4505
4506 if (!(cachep->flags & SLAB_STORE_USER))
4507 return 0;
4508 if (!(cachep->flags & SLAB_RED_ZONE))
4509 return 0;
4510
4511 /* OK, we can do it */
4512
4513 n[1] = 0;
4514
4515 for_each_online_node(node) {
4516 l3 = cachep->nodelists[node];
4517 if (!l3)
4518 continue;
4519
4520 check_irq_on();
4521 spin_lock_irq(&l3->list_lock);
4522
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004523 list_for_each_entry(slabp, &l3->slabs_full, list)
Al Viro871751e2006-03-25 03:06:39 -08004524 handle_slab(n, cachep, slabp);
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004525 list_for_each_entry(slabp, &l3->slabs_partial, list)
Al Viro871751e2006-03-25 03:06:39 -08004526 handle_slab(n, cachep, slabp);
Al Viro871751e2006-03-25 03:06:39 -08004527 spin_unlock_irq(&l3->list_lock);
4528 }
4529 name = cachep->name;
4530 if (n[0] == n[1]) {
4531 /* Increase the buffer size */
4532 mutex_unlock(&cache_chain_mutex);
4533 m->private = kzalloc(n[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
4534 if (!m->private) {
4535 /* Too bad, we are really out */
4536 m->private = n;
4537 mutex_lock(&cache_chain_mutex);
4538 return -ENOMEM;
4539 }
4540 *(unsigned long *)m->private = n[0] * 2;
4541 kfree(n);
4542 mutex_lock(&cache_chain_mutex);
4543 /* Now make sure this entry will be retried */
4544 m->count = m->size;
4545 return 0;
4546 }
4547 for (i = 0; i < n[1]; i++) {
4548 seq_printf(m, "%s: %lu ", name, n[2*i+3]);
4549 show_symbol(m, n[2*i+2]);
4550 seq_putc(m, '\n');
4551 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004552
Al Viro871751e2006-03-25 03:06:39 -08004553 return 0;
4554}
4555
Alexey Dobriyana0ec95a82008-10-06 00:59:10 +04004556static const struct seq_operations slabstats_op = {
Al Viro871751e2006-03-25 03:06:39 -08004557 .start = leaks_start,
4558 .next = s_next,
4559 .stop = s_stop,
4560 .show = leaks_show,
4561};
Alexey Dobriyana0ec95a82008-10-06 00:59:10 +04004562
4563static int slabstats_open(struct inode *inode, struct file *file)
4564{
4565 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
4566 int ret = -ENOMEM;
4567 if (n) {
4568 ret = seq_open(file, &slabstats_op);
4569 if (!ret) {
4570 struct seq_file *m = file->private_data;
4571 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
4572 m->private = n;
4573 n = NULL;
4574 }
4575 kfree(n);
4576 }
4577 return ret;
4578}
4579
4580static const struct file_operations proc_slabstats_operations = {
4581 .open = slabstats_open,
4582 .read = seq_read,
4583 .llseek = seq_lseek,
4584 .release = seq_release_private,
4585};
Al Viro871751e2006-03-25 03:06:39 -08004586#endif
Alexey Dobriyana0ec95a82008-10-06 00:59:10 +04004587
4588static int __init slab_proc_init(void)
4589{
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04004590 proc_create("slabinfo",S_IWUSR|S_IRUGO,NULL,&proc_slabinfo_operations);
Alexey Dobriyana0ec95a82008-10-06 00:59:10 +04004591#ifdef CONFIG_DEBUG_SLAB_LEAK
4592 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
4593#endif
4594 return 0;
4595}
4596module_init(slab_proc_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597#endif
4598
Manfred Spraul00e145b2005-09-03 15:55:07 -07004599/**
4600 * ksize - get the actual amount of memory allocated for a given object
4601 * @objp: Pointer to the object
4602 *
4603 * kmalloc may internally round up allocations and return more memory
4604 * than requested. ksize() can be used to determine the actual amount of
4605 * memory allocated. The caller may use this additional memory, even though
4606 * a smaller amount of memory was initially specified with the kmalloc call.
4607 * The caller must guarantee that objp points to a valid object previously
4608 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4609 * must not be freed during the duration of the call.
4610 */
Pekka Enbergfd76bab2007-05-06 14:48:40 -07004611size_t ksize(const void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612{
Christoph Lameteref8b4522007-10-16 01:24:46 -07004613 BUG_ON(!objp);
4614 if (unlikely(objp == ZERO_SIZE_PTR))
Manfred Spraul00e145b2005-09-03 15:55:07 -07004615 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
Pekka Enberg6ed5eb22006-02-01 03:05:49 -08004617 return obj_size(virt_to_cache(objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618}
Kirill A. Shutemovb1aabec2009-02-10 15:21:44 +02004619EXPORT_SYMBOL(ksize);