blob: bf0c3af143fb1a6b92e40a3ee10cd14a21af171b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118#include <asm/cacheflush.h>
119#include <asm/tlbflush.h>
120#include <asm/page.h>
121
122/*
Christoph Lameter50953fe2007-05-06 14:50:16 -0700123 * DEBUG - 1 for kmem_cache_create() to honour; SLAB_RED_ZONE & SLAB_POISON.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 * 0 for faster, smaller code (especially in the critical paths).
125 *
126 * STATS - 1 to collect stats for /proc/slabinfo.
127 * 0 for faster, smaller code (especially in the critical paths).
128 *
129 * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
130 */
131
132#ifdef CONFIG_DEBUG_SLAB
133#define DEBUG 1
134#define STATS 1
135#define FORCED_DEBUG 1
136#else
137#define DEBUG 0
138#define STATS 0
139#define FORCED_DEBUG 0
140#endif
141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142/* Shouldn't this be in a header file somewhere? */
143#define BYTES_PER_WORD sizeof(void *)
David Woodhouse87a927c2007-07-04 21:26:44 -0400144#define REDZONE_ALIGN max(BYTES_PER_WORD, __alignof__(unsigned long long))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146#ifndef ARCH_KMALLOC_MINALIGN
147/*
148 * Enforce a minimum alignment for the kmalloc caches.
149 * Usually, the kmalloc caches are cache_line_size() aligned, except when
150 * DEBUG and FORCED_DEBUG are enabled, then they are BYTES_PER_WORD aligned.
151 * Some archs want to perform DMA into kmalloc caches and need a guaranteed
David Woodhouseb46b8f12007-05-08 00:22:59 -0700152 * alignment larger than the alignment of a 64-bit integer.
153 * ARCH_KMALLOC_MINALIGN allows that.
154 * Note that increasing this value may disable some debug features.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 */
David Woodhouseb46b8f12007-05-08 00:22:59 -0700156#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157#endif
158
159#ifndef ARCH_SLAB_MINALIGN
160/*
161 * Enforce a minimum alignment for all caches.
162 * Intended for archs that get misalignment faults even for BYTES_PER_WORD
163 * aligned buffers. Includes ARCH_KMALLOC_MINALIGN.
164 * If possible: Do not enable this flag for CONFIG_DEBUG_SLAB, it disables
165 * some debug features.
166 */
167#define ARCH_SLAB_MINALIGN 0
168#endif
169
170#ifndef ARCH_KMALLOC_FLAGS
171#define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
172#endif
173
174/* Legal flag mask for kmem_cache_create(). */
175#if DEBUG
Christoph Lameter50953fe2007-05-06 14:50:16 -0700176# define CREATE_MASK (SLAB_RED_ZONE | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 SLAB_POISON | SLAB_HWCACHE_ALIGN | \
Christoph Lameterac2b8982006-03-22 00:08:15 -0800178 SLAB_CACHE_DMA | \
Christoph Lameter5af60832007-05-06 14:49:56 -0700179 SLAB_STORE_USER | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700181 SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD | \
Catalin Marinasd5cff632009-06-11 13:22:40 +0100182 SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183#else
Christoph Lameterac2b8982006-03-22 00:08:15 -0800184# define CREATE_MASK (SLAB_HWCACHE_ALIGN | \
Christoph Lameter5af60832007-05-06 14:49:56 -0700185 SLAB_CACHE_DMA | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -0700187 SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD | \
Catalin Marinasd5cff632009-06-11 13:22:40 +0100188 SLAB_DEBUG_OBJECTS | SLAB_NOLEAKTRACE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#endif
190
191/*
192 * kmem_bufctl_t:
193 *
194 * Bufctl's are used for linking objs within a slab
195 * linked offsets.
196 *
197 * This implementation relies on "struct page" for locating the cache &
198 * slab an object belongs to.
199 * This allows the bufctl structure to be small (one int), but limits
200 * the number of objects a slab (not a cache) can contain when off-slab
201 * bufctls are used. The limit is the size of the largest general cache
202 * that does not use off-slab slabs.
203 * For 32bit archs with 4 kB pages, is this 56.
204 * This is not serious, as it is only for large objects, when it is unwise
205 * to have too many per slab.
206 * Note: This limit can be raised by introducing a general cache whose size
207 * is less than 512 (PAGE_SIZE<<3), but greater than 256.
208 */
209
Kyle Moffettfa5b08d2005-09-03 15:55:03 -0700210typedef unsigned int kmem_bufctl_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211#define BUFCTL_END (((kmem_bufctl_t)(~0U))-0)
212#define BUFCTL_FREE (((kmem_bufctl_t)(~0U))-1)
Al Viro871751e2006-03-25 03:06:39 -0800213#define BUFCTL_ACTIVE (((kmem_bufctl_t)(~0U))-2)
214#define SLAB_LIMIT (((kmem_bufctl_t)(~0U))-3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/*
217 * struct slab
218 *
219 * Manages the objs in a slab. Placed either at the beginning of mem allocated
220 * for a slab, or allocated from an general cache.
221 * Slabs are chained into three list: fully used, partial, fully free slabs.
222 */
223struct slab {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800224 struct list_head list;
225 unsigned long colouroff;
226 void *s_mem; /* including colour offset */
227 unsigned int inuse; /* num of objs active in slab */
228 kmem_bufctl_t free;
229 unsigned short nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230};
231
232/*
233 * struct slab_rcu
234 *
235 * slab_destroy on a SLAB_DESTROY_BY_RCU cache uses this structure to
236 * arrange for kmem_freepages to be called via RCU. This is useful if
237 * we need to approach a kernel structure obliquely, from its address
238 * obtained without the usual locking. We can lock the structure to
239 * stabilize it and check it's still at the given address, only if we
240 * can be sure that the memory has not been meanwhile reused for some
241 * other kind of object (which our subsystem's lock might corrupt).
242 *
243 * rcu_read_lock before reading the address, then rcu_read_unlock after
244 * taking the spinlock within the structure expected at that address.
245 *
246 * We assume struct slab_rcu can overlay struct slab when destroying.
247 */
248struct slab_rcu {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800249 struct rcu_head head;
Pekka Enberg343e0d72006-02-01 03:05:50 -0800250 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800251 void *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252};
253
254/*
255 * struct array_cache
256 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 * Purpose:
258 * - LIFO ordering, to hand out cache-warm objects from _alloc
259 * - reduce the number of linked list operations
260 * - reduce spinlock operations
261 *
262 * The limit is stored in the per-cpu structure to reduce the data cache
263 * footprint.
264 *
265 */
266struct array_cache {
267 unsigned int avail;
268 unsigned int limit;
269 unsigned int batchcount;
270 unsigned int touched;
Christoph Lametere498be72005-09-09 13:03:32 -0700271 spinlock_t lock;
Robert P. J. Daybda5b652007-10-16 23:30:05 -0700272 void *entry[]; /*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800273 * Must have this definition in here for the proper
274 * alignment of array_cache. Also simplifies accessing
275 * the entries.
Andrew Mortona737b3e2006-03-22 00:08:11 -0800276 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277};
278
Andrew Mortona737b3e2006-03-22 00:08:11 -0800279/*
280 * bootstrap: The caches do not work without cpuarrays anymore, but the
281 * cpuarrays are allocated from the generic caches...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 */
283#define BOOT_CPUCACHE_ENTRIES 1
284struct arraycache_init {
285 struct array_cache cache;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800286 void *entries[BOOT_CPUCACHE_ENTRIES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287};
288
289/*
Christoph Lametere498be72005-09-09 13:03:32 -0700290 * The slab lists for all objects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 */
292struct kmem_list3 {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800293 struct list_head slabs_partial; /* partial list first, better asm code */
294 struct list_head slabs_full;
295 struct list_head slabs_free;
296 unsigned long free_objects;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800297 unsigned int free_limit;
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -0800298 unsigned int colour_next; /* Per-node cache coloring */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800299 spinlock_t list_lock;
300 struct array_cache *shared; /* shared per node */
301 struct array_cache **alien; /* on other nodes */
Christoph Lameter35386e32006-03-22 00:09:05 -0800302 unsigned long next_reap; /* updated without locking */
303 int free_touched; /* updated without locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304};
305
Christoph Lametere498be72005-09-09 13:03:32 -0700306/*
307 * Need this for bootstrapping a per node allocator.
308 */
Pekka Enberg556a1692008-01-25 08:20:51 +0200309#define NUM_INIT_LISTS (3 * MAX_NUMNODES)
Christoph Lametere498be72005-09-09 13:03:32 -0700310struct kmem_list3 __initdata initkmem_list3[NUM_INIT_LISTS];
311#define CACHE_CACHE 0
Pekka Enberg556a1692008-01-25 08:20:51 +0200312#define SIZE_AC MAX_NUMNODES
313#define SIZE_L3 (2 * MAX_NUMNODES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Christoph Lametered11d9e2006-06-30 01:55:45 -0700315static int drain_freelist(struct kmem_cache *cache,
316 struct kmem_list3 *l3, int tofree);
317static void free_block(struct kmem_cache *cachep, void **objpp, int len,
318 int node);
Pekka Enberg83b519e2009-06-10 19:40:04 +0300319static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp);
David Howells65f27f32006-11-22 14:55:48 +0000320static void cache_reap(struct work_struct *unused);
Christoph Lametered11d9e2006-06-30 01:55:45 -0700321
Christoph Lametere498be72005-09-09 13:03:32 -0700322/*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800323 * This function must be completely optimized away if a constant is passed to
324 * it. Mostly the same as what is in linux/slab.h except it returns an index.
Christoph Lametere498be72005-09-09 13:03:32 -0700325 */
Ivan Kokshaysky7243cc02005-09-22 21:43:58 -0700326static __always_inline int index_of(const size_t size)
Christoph Lametere498be72005-09-09 13:03:32 -0700327{
Steven Rostedt5ec8a842006-02-01 03:05:44 -0800328 extern void __bad_size(void);
329
Christoph Lametere498be72005-09-09 13:03:32 -0700330 if (__builtin_constant_p(size)) {
331 int i = 0;
332
333#define CACHE(x) \
334 if (size <=x) \
335 return i; \
336 else \
337 i++;
Joe Perches1c61fc42008-03-05 13:58:17 -0800338#include <linux/kmalloc_sizes.h>
Christoph Lametere498be72005-09-09 13:03:32 -0700339#undef CACHE
Steven Rostedt5ec8a842006-02-01 03:05:44 -0800340 __bad_size();
Ivan Kokshaysky7243cc02005-09-22 21:43:58 -0700341 } else
Steven Rostedt5ec8a842006-02-01 03:05:44 -0800342 __bad_size();
Christoph Lametere498be72005-09-09 13:03:32 -0700343 return 0;
344}
345
Ingo Molnare0a42722006-06-23 02:03:46 -0700346static int slab_early_init = 1;
347
Christoph Lametere498be72005-09-09 13:03:32 -0700348#define INDEX_AC index_of(sizeof(struct arraycache_init))
349#define INDEX_L3 index_of(sizeof(struct kmem_list3))
350
Pekka Enberg5295a742006-02-01 03:05:48 -0800351static void kmem_list3_init(struct kmem_list3 *parent)
Christoph Lametere498be72005-09-09 13:03:32 -0700352{
353 INIT_LIST_HEAD(&parent->slabs_full);
354 INIT_LIST_HEAD(&parent->slabs_partial);
355 INIT_LIST_HEAD(&parent->slabs_free);
356 parent->shared = NULL;
357 parent->alien = NULL;
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -0800358 parent->colour_next = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700359 spin_lock_init(&parent->list_lock);
360 parent->free_objects = 0;
361 parent->free_touched = 0;
362}
363
Andrew Mortona737b3e2006-03-22 00:08:11 -0800364#define MAKE_LIST(cachep, listp, slab, nodeid) \
365 do { \
366 INIT_LIST_HEAD(listp); \
367 list_splice(&(cachep->nodelists[nodeid]->slab), listp); \
Christoph Lametere498be72005-09-09 13:03:32 -0700368 } while (0)
369
Andrew Mortona737b3e2006-03-22 00:08:11 -0800370#define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
371 do { \
Christoph Lametere498be72005-09-09 13:03:32 -0700372 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
373 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
374 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
375 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377#define CFLGS_OFF_SLAB (0x80000000UL)
378#define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
379
380#define BATCHREFILL_LIMIT 16
Andrew Mortona737b3e2006-03-22 00:08:11 -0800381/*
382 * Optimization question: fewer reaps means less probability for unnessary
383 * cpucache drain/refill cycles.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 *
Adrian Bunkdc6f3f22005-11-08 16:44:08 +0100385 * OTOH the cpuarrays can contain lots of objects,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 * which could lock up otherwise freeable slabs.
387 */
388#define REAPTIMEOUT_CPUC (2*HZ)
389#define REAPTIMEOUT_LIST3 (4*HZ)
390
391#if STATS
392#define STATS_INC_ACTIVE(x) ((x)->num_active++)
393#define STATS_DEC_ACTIVE(x) ((x)->num_active--)
394#define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
395#define STATS_INC_GROWN(x) ((x)->grown++)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700396#define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
Andrew Mortona737b3e2006-03-22 00:08:11 -0800397#define STATS_SET_HIGH(x) \
398 do { \
399 if ((x)->num_active > (x)->high_mark) \
400 (x)->high_mark = (x)->num_active; \
401 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402#define STATS_INC_ERR(x) ((x)->errors++)
403#define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
Christoph Lametere498be72005-09-09 13:03:32 -0700404#define STATS_INC_NODEFREES(x) ((x)->node_frees++)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700405#define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800406#define STATS_SET_FREEABLE(x, i) \
407 do { \
408 if ((x)->max_freeable < i) \
409 (x)->max_freeable = i; \
410 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411#define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
412#define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
413#define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
414#define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
415#else
416#define STATS_INC_ACTIVE(x) do { } while (0)
417#define STATS_DEC_ACTIVE(x) do { } while (0)
418#define STATS_INC_ALLOCED(x) do { } while (0)
419#define STATS_INC_GROWN(x) do { } while (0)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700420#define STATS_ADD_REAPED(x,y) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421#define STATS_SET_HIGH(x) do { } while (0)
422#define STATS_INC_ERR(x) do { } while (0)
423#define STATS_INC_NODEALLOCS(x) do { } while (0)
Christoph Lametere498be72005-09-09 13:03:32 -0700424#define STATS_INC_NODEFREES(x) do { } while (0)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700425#define STATS_INC_ACOVERFLOW(x) do { } while (0)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800426#define STATS_SET_FREEABLE(x, i) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427#define STATS_INC_ALLOCHIT(x) do { } while (0)
428#define STATS_INC_ALLOCMISS(x) do { } while (0)
429#define STATS_INC_FREEHIT(x) do { } while (0)
430#define STATS_INC_FREEMISS(x) do { } while (0)
431#endif
432
433#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Andrew Mortona737b3e2006-03-22 00:08:11 -0800435/*
436 * memory layout of objects:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 * 0 : objp
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800438 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 * the end of an object is aligned with the end of the real
440 * allocation. Catches writes behind the end of the allocation.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800441 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 * redzone word.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800443 * cachep->obj_offset: The real object.
444 * cachep->buffer_size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
Andrew Mortona737b3e2006-03-22 00:08:11 -0800445 * cachep->buffer_size - 1* BYTES_PER_WORD: last caller address
446 * [BYTES_PER_WORD long]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800448static int obj_offset(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800450 return cachep->obj_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Pekka Enberg343e0d72006-02-01 03:05:50 -0800453static int obj_size(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800455 return cachep->obj_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456}
457
David Woodhouseb46b8f12007-05-08 00:22:59 -0700458static unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459{
460 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
David Woodhouseb46b8f12007-05-08 00:22:59 -0700461 return (unsigned long long*) (objp + obj_offset(cachep) -
462 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
David Woodhouseb46b8f12007-05-08 00:22:59 -0700465static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
468 if (cachep->flags & SLAB_STORE_USER)
David Woodhouseb46b8f12007-05-08 00:22:59 -0700469 return (unsigned long long *)(objp + cachep->buffer_size -
470 sizeof(unsigned long long) -
David Woodhouse87a927c2007-07-04 21:26:44 -0400471 REDZONE_ALIGN);
David Woodhouseb46b8f12007-05-08 00:22:59 -0700472 return (unsigned long long *) (objp + cachep->buffer_size -
473 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Pekka Enberg343e0d72006-02-01 03:05:50 -0800476static void **dbg_userword(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 BUG_ON(!(cachep->flags & SLAB_STORE_USER));
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800479 return (void **)(objp + cachep->buffer_size - BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482#else
483
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800484#define obj_offset(x) 0
485#define obj_size(cachep) (cachep->buffer_size)
David Woodhouseb46b8f12007-05-08 00:22:59 -0700486#define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
487#define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488#define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
489
490#endif
491
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +0300492#ifdef CONFIG_KMEMTRACE
493size_t slab_buffer_size(struct kmem_cache *cachep)
494{
495 return cachep->buffer_size;
496}
497EXPORT_SYMBOL(slab_buffer_size);
498#endif
499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * Do not go above this order unless 0 objects fit into the slab.
502 */
503#define BREAK_GFP_ORDER_HI 1
504#define BREAK_GFP_ORDER_LO 0
505static int slab_break_gfp_order = BREAK_GFP_ORDER_LO;
506
Andrew Mortona737b3e2006-03-22 00:08:11 -0800507/*
508 * Functions for storing/retrieving the cachep and or slab from the page
509 * allocator. These are used to find the slab an obj belongs to. With kfree(),
510 * these are used to find the cache which an obj belongs to.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 */
Pekka Enberg065d41c2005-11-13 16:06:46 -0800512static inline void page_set_cache(struct page *page, struct kmem_cache *cache)
513{
514 page->lru.next = (struct list_head *)cache;
515}
516
517static inline struct kmem_cache *page_get_cache(struct page *page)
518{
Christoph Lameterd85f3382007-05-06 14:49:39 -0700519 page = compound_head(page);
Pekka Enbergddc2e812006-06-23 02:03:40 -0700520 BUG_ON(!PageSlab(page));
Pekka Enberg065d41c2005-11-13 16:06:46 -0800521 return (struct kmem_cache *)page->lru.next;
522}
523
524static inline void page_set_slab(struct page *page, struct slab *slab)
525{
526 page->lru.prev = (struct list_head *)slab;
527}
528
529static inline struct slab *page_get_slab(struct page *page)
530{
Pekka Enbergddc2e812006-06-23 02:03:40 -0700531 BUG_ON(!PageSlab(page));
Pekka Enberg065d41c2005-11-13 16:06:46 -0800532 return (struct slab *)page->lru.prev;
533}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
Pekka Enberg6ed5eb22006-02-01 03:05:49 -0800535static inline struct kmem_cache *virt_to_cache(const void *obj)
536{
Christoph Lameterb49af682007-05-06 14:49:41 -0700537 struct page *page = virt_to_head_page(obj);
Pekka Enberg6ed5eb22006-02-01 03:05:49 -0800538 return page_get_cache(page);
539}
540
541static inline struct slab *virt_to_slab(const void *obj)
542{
Christoph Lameterb49af682007-05-06 14:49:41 -0700543 struct page *page = virt_to_head_page(obj);
Pekka Enberg6ed5eb22006-02-01 03:05:49 -0800544 return page_get_slab(page);
545}
546
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800547static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
548 unsigned int idx)
549{
550 return slab->s_mem + cache->buffer_size * idx;
551}
552
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800553/*
554 * We want to avoid an expensive divide : (offset / cache->buffer_size)
555 * Using the fact that buffer_size is a constant for a particular cache,
556 * we can replace (offset / cache->buffer_size) by
557 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
558 */
559static inline unsigned int obj_to_index(const struct kmem_cache *cache,
560 const struct slab *slab, void *obj)
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800561{
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800562 u32 offset = (obj - slab->s_mem);
563 return reciprocal_divide(offset, cache->reciprocal_buffer_size);
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800564}
565
Andrew Mortona737b3e2006-03-22 00:08:11 -0800566/*
567 * These are the default caches for kmalloc. Custom caches can have other sizes.
568 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569struct cache_sizes malloc_sizes[] = {
570#define CACHE(x) { .cs_size = (x) },
571#include <linux/kmalloc_sizes.h>
572 CACHE(ULONG_MAX)
573#undef CACHE
574};
575EXPORT_SYMBOL(malloc_sizes);
576
577/* Must match cache_sizes above. Out of line to keep cache footprint low. */
578struct cache_names {
579 char *name;
580 char *name_dma;
581};
582
583static struct cache_names __initdata cache_names[] = {
584#define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },
585#include <linux/kmalloc_sizes.h>
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800586 {NULL,}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587#undef CACHE
588};
589
590static struct arraycache_init initarray_cache __initdata =
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800591 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592static struct arraycache_init initarray_generic =
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800593 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595/* internal cache of cache description objs */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800596static struct kmem_cache cache_cache = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800597 .batchcount = 1,
598 .limit = BOOT_CPUCACHE_ENTRIES,
599 .shared = 1,
Pekka Enberg343e0d72006-02-01 03:05:50 -0800600 .buffer_size = sizeof(struct kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800601 .name = "kmem_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602};
603
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700604#define BAD_ALIEN_MAGIC 0x01020304ul
605
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200606#ifdef CONFIG_LOCKDEP
607
608/*
609 * Slab sometimes uses the kmalloc slabs to store the slab headers
610 * for other slabs "off slab".
611 * The locking for this is tricky in that it nests within the locks
612 * of all other slabs in a few places; to deal with this special
613 * locking we put on-slab caches into a separate lock-class.
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700614 *
615 * We set lock class for alien array caches which are up during init.
616 * The lock annotation will be lost if all cpus of a node goes down and
617 * then comes back up during hotplug
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200618 */
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700619static struct lock_class_key on_slab_l3_key;
620static struct lock_class_key on_slab_alc_key;
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200621
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700622static inline void init_lock_keys(void)
623
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200624{
625 int q;
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700626 struct cache_sizes *s = malloc_sizes;
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200627
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700628 while (s->cs_size != ULONG_MAX) {
629 for_each_node(q) {
630 struct array_cache **alc;
631 int r;
632 struct kmem_list3 *l3 = s->cs_cachep->nodelists[q];
633 if (!l3 || OFF_SLAB(s->cs_cachep))
634 continue;
635 lockdep_set_class(&l3->list_lock, &on_slab_l3_key);
636 alc = l3->alien;
637 /*
638 * FIXME: This check for BAD_ALIEN_MAGIC
639 * should go away when common slab code is taught to
640 * work even without alien caches.
641 * Currently, non NUMA code returns BAD_ALIEN_MAGIC
642 * for alloc_alien_cache,
643 */
644 if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC)
645 continue;
646 for_each_node(r) {
647 if (alc[r])
648 lockdep_set_class(&alc[r]->lock,
649 &on_slab_alc_key);
650 }
651 }
652 s++;
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200653 }
654}
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200655#else
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700656static inline void init_lock_keys(void)
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200657{
658}
659#endif
660
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -0800661/*
Gautham R Shenoy95402b32008-01-25 21:08:02 +0100662 * Guard access to the cache-chain.
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -0800663 */
Ingo Molnarfc0abb12006-01-18 17:42:33 -0800664static DEFINE_MUTEX(cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665static struct list_head cache_chain;
666
667/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 * chicken and egg problem: delay the per-cpu array allocation
669 * until the general caches are up.
670 */
671static enum {
672 NONE,
Christoph Lametere498be72005-09-09 13:03:32 -0700673 PARTIAL_AC,
674 PARTIAL_L3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 FULL
676} g_cpucache_up;
677
Mike Kravetz39d24e62006-05-15 09:44:13 -0700678/*
679 * used by boot code to determine if it can use slab based allocator
680 */
681int slab_is_available(void)
682{
683 return g_cpucache_up == FULL;
684}
685
David Howells52bad642006-11-22 14:54:01 +0000686static DEFINE_PER_CPU(struct delayed_work, reap_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Pekka Enberg343e0d72006-02-01 03:05:50 -0800688static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 return cachep->array[smp_processor_id()];
691}
692
Andrew Mortona737b3e2006-03-22 00:08:11 -0800693static inline struct kmem_cache *__find_general_cachep(size_t size,
694 gfp_t gfpflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695{
696 struct cache_sizes *csizep = malloc_sizes;
697
698#if DEBUG
699 /* This happens if someone tries to call
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800700 * kmem_cache_create(), or __kmalloc(), before
701 * the generic caches are initialized.
702 */
Alok Katariac7e43c72005-09-14 12:17:53 -0700703 BUG_ON(malloc_sizes[INDEX_AC].cs_cachep == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704#endif
Christoph Lameter6cb8f912007-07-17 04:03:22 -0700705 if (!size)
706 return ZERO_SIZE_PTR;
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 while (size > csizep->cs_size)
709 csizep++;
710
711 /*
Martin Hicks0abf40c2005-09-03 15:54:54 -0700712 * Really subtle: The last entry with cs->cs_size==ULONG_MAX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 * has cs_{dma,}cachep==NULL. Thus no special case
714 * for large kmalloc calls required.
715 */
Christoph Lameter4b51d662007-02-10 01:43:10 -0800716#ifdef CONFIG_ZONE_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (unlikely(gfpflags & GFP_DMA))
718 return csizep->cs_dmacachep;
Christoph Lameter4b51d662007-02-10 01:43:10 -0800719#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 return csizep->cs_cachep;
721}
722
Adrian Bunkb2213852006-09-25 23:31:02 -0700723static struct kmem_cache *kmem_find_general_cachep(size_t size, gfp_t gfpflags)
Manfred Spraul97e2bde2005-05-01 08:58:38 -0700724{
725 return __find_general_cachep(size, gfpflags);
726}
Manfred Spraul97e2bde2005-05-01 08:58:38 -0700727
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800728static size_t slab_mgmt_size(size_t nr_objs, size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800730 return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align);
731}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Andrew Mortona737b3e2006-03-22 00:08:11 -0800733/*
734 * Calculate the number of objects and left-over bytes for a given buffer size.
735 */
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800736static void cache_estimate(unsigned long gfporder, size_t buffer_size,
737 size_t align, int flags, size_t *left_over,
738 unsigned int *num)
739{
740 int nr_objs;
741 size_t mgmt_size;
742 size_t slab_size = PAGE_SIZE << gfporder;
743
744 /*
745 * The slab management structure can be either off the slab or
746 * on it. For the latter case, the memory allocated for a
747 * slab is used for:
748 *
749 * - The struct slab
750 * - One kmem_bufctl_t for each object
751 * - Padding to respect alignment of @align
752 * - @buffer_size bytes for each object
753 *
754 * If the slab management structure is off the slab, then the
755 * alignment will already be calculated into the size. Because
756 * the slabs are all pages aligned, the objects will be at the
757 * correct alignment when allocated.
758 */
759 if (flags & CFLGS_OFF_SLAB) {
760 mgmt_size = 0;
761 nr_objs = slab_size / buffer_size;
762
763 if (nr_objs > SLAB_LIMIT)
764 nr_objs = SLAB_LIMIT;
765 } else {
766 /*
767 * Ignore padding for the initial guess. The padding
768 * is at most @align-1 bytes, and @buffer_size is at
769 * least @align. In the worst case, this result will
770 * be one greater than the number of objects that fit
771 * into the memory allocation when taking the padding
772 * into account.
773 */
774 nr_objs = (slab_size - sizeof(struct slab)) /
775 (buffer_size + sizeof(kmem_bufctl_t));
776
777 /*
778 * This calculated number will be either the right
779 * amount, or one greater than what we want.
780 */
781 if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
782 > slab_size)
783 nr_objs--;
784
785 if (nr_objs > SLAB_LIMIT)
786 nr_objs = SLAB_LIMIT;
787
788 mgmt_size = slab_mgmt_size(nr_objs, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800790 *num = nr_objs;
791 *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792}
793
Harvey Harrisond40cee22008-04-30 00:55:07 -0700794#define slab_error(cachep, msg) __slab_error(__func__, cachep, msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
Andrew Mortona737b3e2006-03-22 00:08:11 -0800796static void __slab_error(const char *function, struct kmem_cache *cachep,
797 char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
799 printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800800 function, cachep->name, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 dump_stack();
802}
803
Paul Menage3395ee02006-12-06 20:32:16 -0800804/*
805 * By default on NUMA we use alien caches to stage the freeing of
806 * objects allocated from other nodes. This causes massive memory
807 * inefficiencies when using fake NUMA setup to split memory into a
808 * large number of small nodes, so it can be disabled on the command
809 * line
810 */
811
812static int use_alien_caches __read_mostly = 1;
Siddha, Suresh B1807a1a2007-08-22 14:01:49 -0700813static int numa_platform __read_mostly = 1;
Paul Menage3395ee02006-12-06 20:32:16 -0800814static int __init noaliencache_setup(char *s)
815{
816 use_alien_caches = 0;
817 return 1;
818}
819__setup("noaliencache", noaliencache_setup);
820
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800821#ifdef CONFIG_NUMA
822/*
823 * Special reaping functions for NUMA systems called from cache_reap().
824 * These take care of doing round robin flushing of alien caches (containing
825 * objects freed on different nodes from which they were allocated) and the
826 * flushing of remote pcps by calling drain_node_pages.
827 */
828static DEFINE_PER_CPU(unsigned long, reap_node);
829
830static void init_reap_node(int cpu)
831{
832 int node;
833
834 node = next_node(cpu_to_node(cpu), node_online_map);
835 if (node == MAX_NUMNODES)
Paul Jackson442295c2006-03-22 00:09:11 -0800836 node = first_node(node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800837
Daniel Yeisley7f6b8872006-11-02 22:07:14 -0800838 per_cpu(reap_node, cpu) = node;
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800839}
840
841static void next_reap_node(void)
842{
843 int node = __get_cpu_var(reap_node);
844
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800845 node = next_node(node, node_online_map);
846 if (unlikely(node >= MAX_NUMNODES))
847 node = first_node(node_online_map);
848 __get_cpu_var(reap_node) = node;
849}
850
851#else
852#define init_reap_node(cpu) do { } while (0)
853#define next_reap_node(void) do { } while (0)
854#endif
855
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856/*
857 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
858 * via the workqueue/eventd.
859 * Add the CPU number into the expiration time to minimize the possibility of
860 * the CPUs getting into lockstep and contending for the global cache chain
861 * lock.
862 */
Adrian Bunk897e6792007-07-15 23:38:20 -0700863static void __cpuinit start_cpu_timer(int cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
David Howells52bad642006-11-22 14:54:01 +0000865 struct delayed_work *reap_work = &per_cpu(reap_work, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 /*
868 * When this gets called from do_initcalls via cpucache_init(),
869 * init_workqueues() has already run, so keventd will be setup
870 * at that time.
871 */
David Howells52bad642006-11-22 14:54:01 +0000872 if (keventd_up() && reap_work->work.func == NULL) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800873 init_reap_node(cpu);
David Howells65f27f32006-11-22 14:55:48 +0000874 INIT_DELAYED_WORK(reap_work, cache_reap);
Arjan van de Ven2b284212006-12-10 02:21:28 -0800875 schedule_delayed_work_on(cpu, reap_work,
876 __round_jiffies_relative(HZ, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 }
878}
879
Christoph Lametere498be72005-09-09 13:03:32 -0700880static struct array_cache *alloc_arraycache(int node, int entries,
Pekka Enberg83b519e2009-06-10 19:40:04 +0300881 int batchcount, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800883 int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 struct array_cache *nc = NULL;
885
Pekka Enberg83b519e2009-06-10 19:40:04 +0300886 nc = kmalloc_node(memsize, gfp, node);
Catalin Marinasd5cff632009-06-11 13:22:40 +0100887 /*
888 * The array_cache structures contain pointers to free object.
889 * However, when such objects are allocated or transfered to another
890 * cache the pointers are not cleared and they could be counted as
891 * valid references during a kmemleak scan. Therefore, kmemleak must
892 * not scan such objects.
893 */
894 kmemleak_no_scan(nc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (nc) {
896 nc->avail = 0;
897 nc->limit = entries;
898 nc->batchcount = batchcount;
899 nc->touched = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700900 spin_lock_init(&nc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 }
902 return nc;
903}
904
Christoph Lameter3ded1752006-03-25 03:06:44 -0800905/*
906 * Transfer objects in one arraycache to another.
907 * Locking must be handled by the caller.
908 *
909 * Return the number of entries transferred.
910 */
911static int transfer_objects(struct array_cache *to,
912 struct array_cache *from, unsigned int max)
913{
914 /* Figure out how many entries to transfer */
915 int nr = min(min(from->avail, max), to->limit - to->avail);
916
917 if (!nr)
918 return 0;
919
920 memcpy(to->entry + to->avail, from->entry + from->avail -nr,
921 sizeof(void *) *nr);
922
923 from->avail -= nr;
924 to->avail += nr;
925 to->touched = 1;
926 return nr;
927}
928
Christoph Lameter765c4502006-09-27 01:50:08 -0700929#ifndef CONFIG_NUMA
930
931#define drain_alien_cache(cachep, alien) do { } while (0)
932#define reap_alien(cachep, l3) do { } while (0)
933
Pekka Enberg83b519e2009-06-10 19:40:04 +0300934static inline struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
Christoph Lameter765c4502006-09-27 01:50:08 -0700935{
936 return (struct array_cache **)BAD_ALIEN_MAGIC;
937}
938
939static inline void free_alien_cache(struct array_cache **ac_ptr)
940{
941}
942
943static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
944{
945 return 0;
946}
947
948static inline void *alternate_node_alloc(struct kmem_cache *cachep,
949 gfp_t flags)
950{
951 return NULL;
952}
953
Christoph Hellwig8b98c162006-12-06 20:32:30 -0800954static inline void *____cache_alloc_node(struct kmem_cache *cachep,
Christoph Lameter765c4502006-09-27 01:50:08 -0700955 gfp_t flags, int nodeid)
956{
957 return NULL;
958}
959
960#else /* CONFIG_NUMA */
961
Christoph Hellwig8b98c162006-12-06 20:32:30 -0800962static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
Paul Jacksonc61afb12006-03-24 03:16:08 -0800963static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
Christoph Lameterdc85da12006-01-18 17:42:36 -0800964
Pekka Enberg83b519e2009-06-10 19:40:04 +0300965static struct array_cache **alloc_alien_cache(int node, int limit, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -0700966{
967 struct array_cache **ac_ptr;
Christoph Lameter8ef82862007-02-20 13:57:52 -0800968 int memsize = sizeof(void *) * nr_node_ids;
Christoph Lametere498be72005-09-09 13:03:32 -0700969 int i;
970
971 if (limit > 1)
972 limit = 12;
Pekka Enberg83b519e2009-06-10 19:40:04 +0300973 ac_ptr = kmalloc_node(memsize, gfp, node);
Christoph Lametere498be72005-09-09 13:03:32 -0700974 if (ac_ptr) {
975 for_each_node(i) {
976 if (i == node || !node_online(i)) {
977 ac_ptr[i] = NULL;
978 continue;
979 }
Pekka Enberg83b519e2009-06-10 19:40:04 +0300980 ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d, gfp);
Christoph Lametere498be72005-09-09 13:03:32 -0700981 if (!ac_ptr[i]) {
Akinobu Mitacc550de2007-11-14 16:58:35 -0800982 for (i--; i >= 0; i--)
Christoph Lametere498be72005-09-09 13:03:32 -0700983 kfree(ac_ptr[i]);
984 kfree(ac_ptr);
985 return NULL;
986 }
987 }
988 }
989 return ac_ptr;
990}
991
Pekka Enberg5295a742006-02-01 03:05:48 -0800992static void free_alien_cache(struct array_cache **ac_ptr)
Christoph Lametere498be72005-09-09 13:03:32 -0700993{
994 int i;
995
996 if (!ac_ptr)
997 return;
Christoph Lametere498be72005-09-09 13:03:32 -0700998 for_each_node(i)
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800999 kfree(ac_ptr[i]);
Christoph Lametere498be72005-09-09 13:03:32 -07001000 kfree(ac_ptr);
1001}
1002
Pekka Enberg343e0d72006-02-01 03:05:50 -08001003static void __drain_alien_cache(struct kmem_cache *cachep,
Pekka Enberg5295a742006-02-01 03:05:48 -08001004 struct array_cache *ac, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07001005{
1006 struct kmem_list3 *rl3 = cachep->nodelists[node];
1007
1008 if (ac->avail) {
1009 spin_lock(&rl3->list_lock);
Christoph Lametere00946f2006-03-25 03:06:45 -08001010 /*
1011 * Stuff objects into the remote nodes shared array first.
1012 * That way we could avoid the overhead of putting the objects
1013 * into the free lists and getting them back later.
1014 */
shin, jacob693f7d32006-04-28 10:54:37 -05001015 if (rl3->shared)
1016 transfer_objects(rl3->shared, ac, ac->limit);
Christoph Lametere00946f2006-03-25 03:06:45 -08001017
Christoph Lameterff694162005-09-22 21:44:02 -07001018 free_block(cachep, ac->entry, ac->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001019 ac->avail = 0;
1020 spin_unlock(&rl3->list_lock);
1021 }
1022}
1023
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001024/*
1025 * Called from cache_reap() to regularly drain alien caches round robin.
1026 */
1027static void reap_alien(struct kmem_cache *cachep, struct kmem_list3 *l3)
1028{
1029 int node = __get_cpu_var(reap_node);
1030
1031 if (l3->alien) {
1032 struct array_cache *ac = l3->alien[node];
Christoph Lametere00946f2006-03-25 03:06:45 -08001033
1034 if (ac && ac->avail && spin_trylock_irq(&ac->lock)) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001035 __drain_alien_cache(cachep, ac, node);
1036 spin_unlock_irq(&ac->lock);
1037 }
1038 }
1039}
1040
Andrew Mortona737b3e2006-03-22 00:08:11 -08001041static void drain_alien_cache(struct kmem_cache *cachep,
1042 struct array_cache **alien)
Christoph Lametere498be72005-09-09 13:03:32 -07001043{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001044 int i = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07001045 struct array_cache *ac;
1046 unsigned long flags;
1047
1048 for_each_online_node(i) {
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001049 ac = alien[i];
Christoph Lametere498be72005-09-09 13:03:32 -07001050 if (ac) {
1051 spin_lock_irqsave(&ac->lock, flags);
1052 __drain_alien_cache(cachep, ac, i);
1053 spin_unlock_irqrestore(&ac->lock, flags);
1054 }
1055 }
1056}
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001057
Ingo Molnar873623d2006-07-13 14:44:38 +02001058static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001059{
1060 struct slab *slabp = virt_to_slab(objp);
1061 int nodeid = slabp->nodeid;
1062 struct kmem_list3 *l3;
1063 struct array_cache *alien = NULL;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001064 int node;
1065
1066 node = numa_node_id();
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001067
1068 /*
1069 * Make sure we are not freeing a object from another node to the array
1070 * cache on this cpu.
1071 */
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001072 if (likely(slabp->nodeid == node))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001073 return 0;
1074
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001075 l3 = cachep->nodelists[node];
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001076 STATS_INC_NODEFREES(cachep);
1077 if (l3->alien && l3->alien[nodeid]) {
1078 alien = l3->alien[nodeid];
Ingo Molnar873623d2006-07-13 14:44:38 +02001079 spin_lock(&alien->lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001080 if (unlikely(alien->avail == alien->limit)) {
1081 STATS_INC_ACOVERFLOW(cachep);
1082 __drain_alien_cache(cachep, alien, nodeid);
1083 }
1084 alien->entry[alien->avail++] = objp;
1085 spin_unlock(&alien->lock);
1086 } else {
1087 spin_lock(&(cachep->nodelists[nodeid])->list_lock);
1088 free_block(cachep, &objp, 1, nodeid);
1089 spin_unlock(&(cachep->nodelists[nodeid])->list_lock);
1090 }
1091 return 1;
1092}
Christoph Lametere498be72005-09-09 13:03:32 -07001093#endif
1094
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001095static void __cpuinit cpuup_canceled(long cpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001097 struct kmem_cache *cachep;
1098 struct kmem_list3 *l3 = NULL;
1099 int node = cpu_to_node(cpu);
Rusty Russella70f7302009-03-13 14:49:46 +10301100 const struct cpumask *mask = cpumask_of_node(node);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001101
1102 list_for_each_entry(cachep, &cache_chain, next) {
1103 struct array_cache *nc;
1104 struct array_cache *shared;
1105 struct array_cache **alien;
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001106
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001107 /* cpu is dead; no one can alloc from it. */
1108 nc = cachep->array[cpu];
1109 cachep->array[cpu] = NULL;
1110 l3 = cachep->nodelists[node];
1111
1112 if (!l3)
1113 goto free_array_cache;
1114
1115 spin_lock_irq(&l3->list_lock);
1116
1117 /* Free limit for this kmem_list3 */
1118 l3->free_limit -= cachep->batchcount;
1119 if (nc)
1120 free_block(cachep, nc->entry, nc->avail, node);
1121
Mike Travisc5f59f02008-04-04 18:11:10 -07001122 if (!cpus_empty(*mask)) {
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001123 spin_unlock_irq(&l3->list_lock);
1124 goto free_array_cache;
1125 }
1126
1127 shared = l3->shared;
1128 if (shared) {
1129 free_block(cachep, shared->entry,
1130 shared->avail, node);
1131 l3->shared = NULL;
1132 }
1133
1134 alien = l3->alien;
1135 l3->alien = NULL;
1136
1137 spin_unlock_irq(&l3->list_lock);
1138
1139 kfree(shared);
1140 if (alien) {
1141 drain_alien_cache(cachep, alien);
1142 free_alien_cache(alien);
1143 }
1144free_array_cache:
1145 kfree(nc);
1146 }
1147 /*
1148 * In the previous loop, all the objects were freed to
1149 * the respective cache's slabs, now we can go ahead and
1150 * shrink each nodelist to its limit.
1151 */
1152 list_for_each_entry(cachep, &cache_chain, next) {
1153 l3 = cachep->nodelists[node];
1154 if (!l3)
1155 continue;
1156 drain_freelist(cachep, l3, l3->free_objects);
1157 }
1158}
1159
1160static int __cpuinit cpuup_prepare(long cpu)
1161{
Pekka Enberg343e0d72006-02-01 03:05:50 -08001162 struct kmem_cache *cachep;
Christoph Lametere498be72005-09-09 13:03:32 -07001163 struct kmem_list3 *l3 = NULL;
1164 int node = cpu_to_node(cpu);
David Howellsea02e3d2007-07-19 01:49:09 -07001165 const int memsize = sizeof(struct kmem_list3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001167 /*
1168 * We need to do this right in the beginning since
1169 * alloc_arraycache's are going to use this list.
1170 * kmalloc_node allows us to add the slab to the right
1171 * kmem_list3 and not this cpu's kmem_list3
1172 */
1173
1174 list_for_each_entry(cachep, &cache_chain, next) {
1175 /*
1176 * Set up the size64 kmemlist for cpu before we can
1177 * begin anything. Make sure some other cpu on this
1178 * node has not already allocated this
1179 */
1180 if (!cachep->nodelists[node]) {
1181 l3 = kmalloc_node(memsize, GFP_KERNEL, node);
1182 if (!l3)
1183 goto bad;
1184 kmem_list3_init(l3);
1185 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
1186 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1187
1188 /*
1189 * The l3s don't come and go as CPUs come and
1190 * go. cache_chain_mutex is sufficient
1191 * protection here.
1192 */
1193 cachep->nodelists[node] = l3;
1194 }
1195
1196 spin_lock_irq(&cachep->nodelists[node]->list_lock);
1197 cachep->nodelists[node]->free_limit =
1198 (1 + nr_cpus_node(node)) *
1199 cachep->batchcount + cachep->num;
1200 spin_unlock_irq(&cachep->nodelists[node]->list_lock);
1201 }
1202
1203 /*
1204 * Now we can go ahead with allocating the shared arrays and
1205 * array caches
1206 */
1207 list_for_each_entry(cachep, &cache_chain, next) {
1208 struct array_cache *nc;
1209 struct array_cache *shared = NULL;
1210 struct array_cache **alien = NULL;
1211
1212 nc = alloc_arraycache(node, cachep->limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001213 cachep->batchcount, GFP_KERNEL);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001214 if (!nc)
1215 goto bad;
1216 if (cachep->shared) {
1217 shared = alloc_arraycache(node,
1218 cachep->shared * cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03001219 0xbaadf00d, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001220 if (!shared) {
1221 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001222 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001223 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001224 }
1225 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03001226 alien = alloc_alien_cache(node, cachep->limit, GFP_KERNEL);
Akinobu Mita12d00f62007-10-18 03:05:11 -07001227 if (!alien) {
1228 kfree(shared);
1229 kfree(nc);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001230 goto bad;
Akinobu Mita12d00f62007-10-18 03:05:11 -07001231 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001232 }
1233 cachep->array[cpu] = nc;
1234 l3 = cachep->nodelists[node];
1235 BUG_ON(!l3);
1236
1237 spin_lock_irq(&l3->list_lock);
1238 if (!l3->shared) {
1239 /*
1240 * We are serialised from CPU_DEAD or
1241 * CPU_UP_CANCELLED by the cpucontrol lock
1242 */
1243 l3->shared = shared;
1244 shared = NULL;
1245 }
1246#ifdef CONFIG_NUMA
1247 if (!l3->alien) {
1248 l3->alien = alien;
1249 alien = NULL;
1250 }
1251#endif
1252 spin_unlock_irq(&l3->list_lock);
1253 kfree(shared);
1254 free_alien_cache(alien);
1255 }
1256 return 0;
1257bad:
Akinobu Mita12d00f62007-10-18 03:05:11 -07001258 cpuup_canceled(cpu);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001259 return -ENOMEM;
1260}
1261
1262static int __cpuinit cpuup_callback(struct notifier_block *nfb,
1263 unsigned long action, void *hcpu)
1264{
1265 long cpu = (long)hcpu;
1266 int err = 0;
1267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 switch (action) {
Heiko Carstens38c3bd92007-05-09 02:34:05 -07001269 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001270 case CPU_UP_PREPARE_FROZEN:
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001271 mutex_lock(&cache_chain_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001272 err = cpuup_prepare(cpu);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001273 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 break;
1275 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001276 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 start_cpu_timer(cpu);
1278 break;
1279#ifdef CONFIG_HOTPLUG_CPU
Christoph Lameter5830c592007-05-09 02:34:22 -07001280 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001281 case CPU_DOWN_PREPARE_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001282 /*
1283 * Shutdown cache reaper. Note that the cache_chain_mutex is
1284 * held so that if cache_reap() is invoked it cannot do
1285 * anything expensive but will only modify reap_work
1286 * and reschedule the timer.
1287 */
1288 cancel_rearming_delayed_work(&per_cpu(reap_work, cpu));
1289 /* Now the cache_reaper is guaranteed to be not running. */
1290 per_cpu(reap_work, cpu).work.func = NULL;
1291 break;
1292 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001293 case CPU_DOWN_FAILED_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001294 start_cpu_timer(cpu);
1295 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001297 case CPU_DEAD_FROZEN:
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001298 /*
1299 * Even if all the cpus of a node are down, we don't free the
1300 * kmem_list3 of any cache. This to avoid a race between
1301 * cpu_down, and a kmalloc allocation from another cpu for
1302 * memory from the node of the cpu going down. The list3
1303 * structure is usually allocated from kmem_cache_create() and
1304 * gets destroyed at kmem_cache_destroy().
1305 */
Simon Arlott183ff222007-10-20 01:27:18 +02001306 /* fall through */
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08001307#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001309 case CPU_UP_CANCELED_FROZEN:
Gautham R Shenoy95402b32008-01-25 21:08:02 +01001310 mutex_lock(&cache_chain_mutex);
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001311 cpuup_canceled(cpu);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001312 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
Akinobu Mitafbf1e472007-10-18 03:05:09 -07001315 return err ? NOTIFY_BAD : NOTIFY_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001318static struct notifier_block __cpuinitdata cpucache_notifier = {
1319 &cpuup_callback, NULL, 0
1320};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321
Christoph Lametere498be72005-09-09 13:03:32 -07001322/*
1323 * swap the static kmem_list3 with kmalloced memory
1324 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001325static void init_list(struct kmem_cache *cachep, struct kmem_list3 *list,
1326 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07001327{
1328 struct kmem_list3 *ptr;
1329
Pekka Enberg83b519e2009-06-10 19:40:04 +03001330 ptr = kmalloc_node(sizeof(struct kmem_list3), GFP_NOWAIT, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07001331 BUG_ON(!ptr);
1332
Christoph Lametere498be72005-09-09 13:03:32 -07001333 memcpy(ptr, list, sizeof(struct kmem_list3));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001334 /*
1335 * Do not assume that spinlocks can be initialized via memcpy:
1336 */
1337 spin_lock_init(&ptr->list_lock);
1338
Christoph Lametere498be72005-09-09 13:03:32 -07001339 MAKE_ALL_LISTS(cachep, ptr, nodeid);
1340 cachep->nodelists[nodeid] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001341}
1342
Andrew Mortona737b3e2006-03-22 00:08:11 -08001343/*
Pekka Enberg556a1692008-01-25 08:20:51 +02001344 * For setting up all the kmem_list3s for cache whose buffer_size is same as
1345 * size of kmem_list3.
1346 */
1347static void __init set_up_list3s(struct kmem_cache *cachep, int index)
1348{
1349 int node;
1350
1351 for_each_online_node(node) {
1352 cachep->nodelists[node] = &initkmem_list3[index + node];
1353 cachep->nodelists[node]->next_reap = jiffies +
1354 REAPTIMEOUT_LIST3 +
1355 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1356 }
1357}
1358
1359/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08001360 * Initialisation. Called after the page allocator have been initialised and
1361 * before smp_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 */
1363void __init kmem_cache_init(void)
1364{
1365 size_t left_over;
1366 struct cache_sizes *sizes;
1367 struct cache_names *names;
Christoph Lametere498be72005-09-09 13:03:32 -07001368 int i;
Jack Steiner07ed76b2006-03-07 21:55:46 -08001369 int order;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001370 int node;
Christoph Lametere498be72005-09-09 13:03:32 -07001371
Siddha, Suresh B1807a1a2007-08-22 14:01:49 -07001372 if (num_possible_nodes() == 1) {
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001373 use_alien_caches = 0;
Siddha, Suresh B1807a1a2007-08-22 14:01:49 -07001374 numa_platform = 0;
1375 }
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001376
Christoph Lametere498be72005-09-09 13:03:32 -07001377 for (i = 0; i < NUM_INIT_LISTS; i++) {
1378 kmem_list3_init(&initkmem_list3[i]);
1379 if (i < MAX_NUMNODES)
1380 cache_cache.nodelists[i] = NULL;
1381 }
Pekka Enberg556a1692008-01-25 08:20:51 +02001382 set_up_list3s(&cache_cache, CACHE_CACHE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
1384 /*
1385 * Fragmentation resistance on low memory - only use bigger
1386 * page orders on machines with more than 32MB of memory.
1387 */
1388 if (num_physpages > (32 << 20) >> PAGE_SHIFT)
1389 slab_break_gfp_order = BREAK_GFP_ORDER_HI;
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 /* Bootstrap is tricky, because several objects are allocated
1392 * from caches that do not exist yet:
Andrew Mortona737b3e2006-03-22 00:08:11 -08001393 * 1) initialize the cache_cache cache: it contains the struct
1394 * kmem_cache structures of all caches, except cache_cache itself:
1395 * cache_cache is statically allocated.
Christoph Lametere498be72005-09-09 13:03:32 -07001396 * Initially an __init data area is used for the head array and the
1397 * kmem_list3 structures, it's replaced with a kmalloc allocated
1398 * array at the end of the bootstrap.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 * 2) Create the first kmalloc cache.
Pekka Enberg343e0d72006-02-01 03:05:50 -08001400 * The struct kmem_cache for the new cache is allocated normally.
Christoph Lametere498be72005-09-09 13:03:32 -07001401 * An __init data area is used for the head array.
1402 * 3) Create the remaining kmalloc caches, with minimally sized
1403 * head arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 * 4) Replace the __init data head arrays for cache_cache and the first
1405 * kmalloc cache with kmalloc allocated arrays.
Christoph Lametere498be72005-09-09 13:03:32 -07001406 * 5) Replace the __init data for kmem_list3 for cache_cache and
1407 * the other cache's with kmalloc allocated memory.
1408 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 */
1410
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001411 node = numa_node_id();
1412
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 /* 1) create the cache_cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 INIT_LIST_HEAD(&cache_chain);
1415 list_add(&cache_cache.next, &cache_chain);
1416 cache_cache.colour_off = cache_line_size();
1417 cache_cache.array[smp_processor_id()] = &initarray_cache.cache;
Daniel Yeisleyec1f5ee2008-03-25 23:59:08 +02001418 cache_cache.nodelists[node] = &initkmem_list3[CACHE_CACHE + node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Eric Dumazet8da34302007-05-06 14:49:29 -07001420 /*
1421 * struct kmem_cache size depends on nr_node_ids, which
1422 * can be less than MAX_NUMNODES.
1423 */
1424 cache_cache.buffer_size = offsetof(struct kmem_cache, nodelists) +
1425 nr_node_ids * sizeof(struct kmem_list3 *);
1426#if DEBUG
1427 cache_cache.obj_size = cache_cache.buffer_size;
1428#endif
Andrew Mortona737b3e2006-03-22 00:08:11 -08001429 cache_cache.buffer_size = ALIGN(cache_cache.buffer_size,
1430 cache_line_size());
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08001431 cache_cache.reciprocal_buffer_size =
1432 reciprocal_value(cache_cache.buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
Jack Steiner07ed76b2006-03-07 21:55:46 -08001434 for (order = 0; order < MAX_ORDER; order++) {
1435 cache_estimate(order, cache_cache.buffer_size,
1436 cache_line_size(), 0, &left_over, &cache_cache.num);
1437 if (cache_cache.num)
1438 break;
1439 }
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02001440 BUG_ON(!cache_cache.num);
Jack Steiner07ed76b2006-03-07 21:55:46 -08001441 cache_cache.gfporder = order;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001442 cache_cache.colour = left_over / cache_cache.colour_off;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001443 cache_cache.slab_size = ALIGN(cache_cache.num * sizeof(kmem_bufctl_t) +
1444 sizeof(struct slab), cache_line_size());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 /* 2+3) create the kmalloc caches */
1447 sizes = malloc_sizes;
1448 names = cache_names;
1449
Andrew Mortona737b3e2006-03-22 00:08:11 -08001450 /*
1451 * Initialize the caches that provide memory for the array cache and the
1452 * kmem_list3 structures first. Without this, further allocations will
1453 * bug.
Christoph Lametere498be72005-09-09 13:03:32 -07001454 */
1455
1456 sizes[INDEX_AC].cs_cachep = kmem_cache_create(names[INDEX_AC].name,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001457 sizes[INDEX_AC].cs_size,
1458 ARCH_KMALLOC_MINALIGN,
1459 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09001460 NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07001461
Andrew Mortona737b3e2006-03-22 00:08:11 -08001462 if (INDEX_AC != INDEX_L3) {
Christoph Lametere498be72005-09-09 13:03:32 -07001463 sizes[INDEX_L3].cs_cachep =
Andrew Mortona737b3e2006-03-22 00:08:11 -08001464 kmem_cache_create(names[INDEX_L3].name,
1465 sizes[INDEX_L3].cs_size,
1466 ARCH_KMALLOC_MINALIGN,
1467 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09001468 NULL);
Andrew Mortona737b3e2006-03-22 00:08:11 -08001469 }
Christoph Lametere498be72005-09-09 13:03:32 -07001470
Ingo Molnare0a42722006-06-23 02:03:46 -07001471 slab_early_init = 0;
1472
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 while (sizes->cs_size != ULONG_MAX) {
Christoph Lametere498be72005-09-09 13:03:32 -07001474 /*
1475 * For performance, all the general caches are L1 aligned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 * This should be particularly beneficial on SMP boxes, as it
1477 * eliminates "false sharing".
1478 * Note for systems short on memory removing the alignment will
Christoph Lametere498be72005-09-09 13:03:32 -07001479 * allow tighter packing of the smaller caches.
1480 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001481 if (!sizes->cs_cachep) {
Christoph Lametere498be72005-09-09 13:03:32 -07001482 sizes->cs_cachep = kmem_cache_create(names->name,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001483 sizes->cs_size,
1484 ARCH_KMALLOC_MINALIGN,
1485 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09001486 NULL);
Andrew Mortona737b3e2006-03-22 00:08:11 -08001487 }
Christoph Lameter4b51d662007-02-10 01:43:10 -08001488#ifdef CONFIG_ZONE_DMA
1489 sizes->cs_dmacachep = kmem_cache_create(
1490 names->name_dma,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001491 sizes->cs_size,
1492 ARCH_KMALLOC_MINALIGN,
1493 ARCH_KMALLOC_FLAGS|SLAB_CACHE_DMA|
1494 SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09001495 NULL);
Christoph Lameter4b51d662007-02-10 01:43:10 -08001496#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 sizes++;
1498 names++;
1499 }
1500 /* 4) Replace the bootstrap head arrays */
1501 {
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001502 struct array_cache *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001503
Pekka Enberg83b519e2009-06-10 19:40:04 +03001504 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001505
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001506 BUG_ON(cpu_cache_get(&cache_cache) != &initarray_cache.cache);
1507 memcpy(ptr, cpu_cache_get(&cache_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001508 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001509 /*
1510 * Do not assume that spinlocks can be initialized via memcpy:
1511 */
1512 spin_lock_init(&ptr->lock);
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 cache_cache.array[smp_processor_id()] = ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001515
Pekka Enberg83b519e2009-06-10 19:40:04 +03001516 ptr = kmalloc(sizeof(struct arraycache_init), GFP_NOWAIT);
Christoph Lametere498be72005-09-09 13:03:32 -07001517
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001518 BUG_ON(cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001519 != &initarray_generic.cache);
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001520 memcpy(ptr, cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001521 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001522 /*
1523 * Do not assume that spinlocks can be initialized via memcpy:
1524 */
1525 spin_lock_init(&ptr->lock);
1526
Christoph Lametere498be72005-09-09 13:03:32 -07001527 malloc_sizes[INDEX_AC].cs_cachep->array[smp_processor_id()] =
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001528 ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
Christoph Lametere498be72005-09-09 13:03:32 -07001530 /* 5) Replace the bootstrap kmem_list3's */
1531 {
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001532 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533
Mel Gorman9c09a952008-01-24 05:49:54 -08001534 for_each_online_node(nid) {
Daniel Yeisleyec1f5ee2008-03-25 23:59:08 +02001535 init_list(&cache_cache, &initkmem_list3[CACHE_CACHE + nid], nid);
Pekka Enberg556a1692008-01-25 08:20:51 +02001536
Christoph Lametere498be72005-09-09 13:03:32 -07001537 init_list(malloc_sizes[INDEX_AC].cs_cachep,
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001538 &initkmem_list3[SIZE_AC + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001539
1540 if (INDEX_AC != INDEX_L3) {
1541 init_list(malloc_sizes[INDEX_L3].cs_cachep,
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001542 &initkmem_list3[SIZE_L3 + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001543 }
1544 }
1545 }
1546
1547 /* 6) resize the head arrays to their final sizes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 {
Pekka Enberg343e0d72006-02-01 03:05:50 -08001549 struct kmem_cache *cachep;
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001550 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 list_for_each_entry(cachep, &cache_chain, next)
Pekka Enberg83b519e2009-06-10 19:40:04 +03001552 if (enable_cpucache(cachep, GFP_NOWAIT))
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07001553 BUG();
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001554 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001557 /* Annotate slab for lockdep -- annotate the malloc caches */
1558 init_lock_keys();
1559
1560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 /* Done! */
1562 g_cpucache_up = FULL;
1563
Andrew Mortona737b3e2006-03-22 00:08:11 -08001564 /*
1565 * Register a cpu startup notifier callback that initializes
1566 * cpu_cache_get for all new cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567 */
1568 register_cpu_notifier(&cpucache_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Andrew Mortona737b3e2006-03-22 00:08:11 -08001570 /*
1571 * The reap timers are started later, with a module init call: That part
1572 * of the kernel is not yet operational.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 */
1574}
1575
1576static int __init cpucache_init(void)
1577{
1578 int cpu;
1579
Andrew Mortona737b3e2006-03-22 00:08:11 -08001580 /*
1581 * Register the timers that return unneeded pages to the page allocator
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 */
Christoph Lametere498be72005-09-09 13:03:32 -07001583 for_each_online_cpu(cpu)
Andrew Mortona737b3e2006-03-22 00:08:11 -08001584 start_cpu_timer(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 return 0;
1586}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587__initcall(cpucache_init);
1588
1589/*
1590 * Interface to system's page allocator. No need to hold the cache-lock.
1591 *
1592 * If we requested dmaable memory, we will get it. Even if we
1593 * did not request dmaable memory, we might get it, but that
1594 * would be relatively rare and ignorable.
1595 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001596static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597{
1598 struct page *page;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001599 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 int i;
1601
Luke Yangd6fef9d2006-04-10 22:52:56 -07001602#ifndef CONFIG_MMU
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001603 /*
1604 * Nommu uses slab's for process anonymous memory allocations, and thus
1605 * requires __GFP_COMP to properly refcount higher order allocations
Luke Yangd6fef9d2006-04-10 22:52:56 -07001606 */
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001607 flags |= __GFP_COMP;
Luke Yangd6fef9d2006-04-10 22:52:56 -07001608#endif
Christoph Lameter765c4502006-09-27 01:50:08 -07001609
Christoph Lameter3c517a62006-12-06 20:33:29 -08001610 flags |= cachep->gfpflags;
Mel Gormane12ba742007-10-16 01:25:52 -07001611 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1612 flags |= __GFP_RECLAIMABLE;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001613
1614 page = alloc_pages_node(nodeid, flags, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (!page)
1616 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001618 nr_pages = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
Christoph Lameter972d1a72006-09-25 23:31:51 -07001620 add_zone_page_state(page_zone(page),
1621 NR_SLAB_RECLAIMABLE, nr_pages);
1622 else
1623 add_zone_page_state(page_zone(page),
1624 NR_SLAB_UNRECLAIMABLE, nr_pages);
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001625 for (i = 0; i < nr_pages; i++)
1626 __SetPageSlab(page + i);
1627 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628}
1629
1630/*
1631 * Interface to system's page release.
1632 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001633static void kmem_freepages(struct kmem_cache *cachep, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001635 unsigned long i = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 struct page *page = virt_to_page(addr);
1637 const unsigned long nr_freed = i;
1638
Christoph Lameter972d1a72006-09-25 23:31:51 -07001639 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1640 sub_zone_page_state(page_zone(page),
1641 NR_SLAB_RECLAIMABLE, nr_freed);
1642 else
1643 sub_zone_page_state(page_zone(page),
1644 NR_SLAB_UNRECLAIMABLE, nr_freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 while (i--) {
Nick Pigginf205b2f2006-03-22 00:08:02 -08001646 BUG_ON(!PageSlab(page));
1647 __ClearPageSlab(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 page++;
1649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 if (current->reclaim_state)
1651 current->reclaim_state->reclaimed_slab += nr_freed;
1652 free_pages((unsigned long)addr, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653}
1654
1655static void kmem_rcu_free(struct rcu_head *head)
1656{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001657 struct slab_rcu *slab_rcu = (struct slab_rcu *)head;
Pekka Enberg343e0d72006-02-01 03:05:50 -08001658 struct kmem_cache *cachep = slab_rcu->cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659
1660 kmem_freepages(cachep, slab_rcu->addr);
1661 if (OFF_SLAB(cachep))
1662 kmem_cache_free(cachep->slabp_cache, slab_rcu);
1663}
1664
1665#if DEBUG
1666
1667#ifdef CONFIG_DEBUG_PAGEALLOC
Pekka Enberg343e0d72006-02-01 03:05:50 -08001668static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001669 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001671 int size = obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001673 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001675 if (size < 5 * sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 return;
1677
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001678 *addr++ = 0x12345678;
1679 *addr++ = caller;
1680 *addr++ = smp_processor_id();
1681 size -= 3 * sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 {
1683 unsigned long *sptr = &caller;
1684 unsigned long svalue;
1685
1686 while (!kstack_end(sptr)) {
1687 svalue = *sptr++;
1688 if (kernel_text_address(svalue)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001689 *addr++ = svalue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 size -= sizeof(unsigned long);
1691 if (size <= sizeof(unsigned long))
1692 break;
1693 }
1694 }
1695
1696 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001697 *addr++ = 0x87654321;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698}
1699#endif
1700
Pekka Enberg343e0d72006-02-01 03:05:50 -08001701static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001703 int size = obj_size(cachep);
1704 addr = &((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705
1706 memset(addr, val, size);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001707 *(unsigned char *)(addr + size - 1) = POISON_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708}
1709
1710static void dump_line(char *data, int offset, int limit)
1711{
1712 int i;
Dave Jonesaa83aa42006-09-29 01:59:51 -07001713 unsigned char error = 0;
1714 int bad_count = 0;
1715
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 printk(KERN_ERR "%03x:", offset);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001717 for (i = 0; i < limit; i++) {
1718 if (data[offset + i] != POISON_FREE) {
1719 error = data[offset + i];
1720 bad_count++;
1721 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001722 printk(" %02x", (unsigned char)data[offset + i]);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001723 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 printk("\n");
Dave Jonesaa83aa42006-09-29 01:59:51 -07001725
1726 if (bad_count == 1) {
1727 error ^= POISON_FREE;
1728 if (!(error & (error - 1))) {
1729 printk(KERN_ERR "Single bit error detected. Probably "
1730 "bad RAM.\n");
1731#ifdef CONFIG_X86
1732 printk(KERN_ERR "Run memtest86+ or a similar memory "
1733 "test tool.\n");
1734#else
1735 printk(KERN_ERR "Run a memory test tool.\n");
1736#endif
1737 }
1738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739}
1740#endif
1741
1742#if DEBUG
1743
Pekka Enberg343e0d72006-02-01 03:05:50 -08001744static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
1746 int i, size;
1747 char *realobj;
1748
1749 if (cachep->flags & SLAB_RED_ZONE) {
David Woodhouseb46b8f12007-05-08 00:22:59 -07001750 printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001751 *dbg_redzone1(cachep, objp),
1752 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
1754
1755 if (cachep->flags & SLAB_STORE_USER) {
1756 printk(KERN_ERR "Last user: [<%p>]",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001757 *dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 print_symbol("(%s)",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001759 (unsigned long)*dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 printk("\n");
1761 }
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001762 realobj = (char *)objp + obj_offset(cachep);
1763 size = obj_size(cachep);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001764 for (i = 0; i < size && lines; i += 16, lines--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 int limit;
1766 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001767 if (i + limit > size)
1768 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 dump_line(realobj, i, limit);
1770 }
1771}
1772
Pekka Enberg343e0d72006-02-01 03:05:50 -08001773static void check_poison_obj(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
1775 char *realobj;
1776 int size, i;
1777 int lines = 0;
1778
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001779 realobj = (char *)objp + obj_offset(cachep);
1780 size = obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001782 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 char exp = POISON_FREE;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001784 if (i == size - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 exp = POISON_END;
1786 if (realobj[i] != exp) {
1787 int limit;
1788 /* Mismatch ! */
1789 /* Print header */
1790 if (lines == 0) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001791 printk(KERN_ERR
David Howellse94a40c2007-04-02 23:46:28 +01001792 "Slab corruption: %s start=%p, len=%d\n",
1793 cachep->name, realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 print_objinfo(cachep, objp, 0);
1795 }
1796 /* Hexdump the affected line */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001797 i = (i / 16) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001799 if (i + limit > size)
1800 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 dump_line(realobj, i, limit);
1802 i += 16;
1803 lines++;
1804 /* Limit to 5 lines */
1805 if (lines > 5)
1806 break;
1807 }
1808 }
1809 if (lines != 0) {
1810 /* Print some data about the neighboring objects, if they
1811 * exist:
1812 */
Pekka Enberg6ed5eb22006-02-01 03:05:49 -08001813 struct slab *slabp = virt_to_slab(objp);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001814 unsigned int objnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001816 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 if (objnr) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001818 objp = index_to_obj(cachep, slabp, objnr - 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001819 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001821 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 print_objinfo(cachep, objp, 2);
1823 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001824 if (objnr + 1 < cachep->num) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001825 objp = index_to_obj(cachep, slabp, objnr + 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001826 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001828 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 print_objinfo(cachep, objp, 2);
1830 }
1831 }
1832}
1833#endif
1834
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835#if DEBUG
Rabin Vincente79aec22008-07-04 00:40:32 +05301836static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001837{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 int i;
1839 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001840 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 if (cachep->flags & SLAB_POISON) {
1843#ifdef CONFIG_DEBUG_PAGEALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -08001844 if (cachep->buffer_size % PAGE_SIZE == 0 &&
1845 OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001846 kernel_map_pages(virt_to_page(objp),
Andrew Mortona737b3e2006-03-22 00:08:11 -08001847 cachep->buffer_size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 else
1849 check_poison_obj(cachep, objp);
1850#else
1851 check_poison_obj(cachep, objp);
1852#endif
1853 }
1854 if (cachep->flags & SLAB_RED_ZONE) {
1855 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
1856 slab_error(cachep, "start of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001857 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
1859 slab_error(cachep, "end of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001860 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001863}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864#else
Rabin Vincente79aec22008-07-04 00:40:32 +05301865static void slab_destroy_debugcheck(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001866{
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001867}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868#endif
1869
Randy Dunlap911851e2006-03-22 00:08:14 -08001870/**
1871 * slab_destroy - destroy and release all objects in a slab
1872 * @cachep: cache pointer being destroyed
1873 * @slabp: slab pointer being destroyed
1874 *
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001875 * Destroy all the objs in a slab, and release the mem back to the system.
Andrew Mortona737b3e2006-03-22 00:08:11 -08001876 * Before calling the slab must have been unlinked from the cache. The
1877 * cache-lock is not held/needed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001878 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001879static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001880{
1881 void *addr = slabp->s_mem - slabp->colouroff;
1882
Rabin Vincente79aec22008-07-04 00:40:32 +05301883 slab_destroy_debugcheck(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
1885 struct slab_rcu *slab_rcu;
1886
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001887 slab_rcu = (struct slab_rcu *)slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 slab_rcu->cachep = cachep;
1889 slab_rcu->addr = addr;
1890 call_rcu(&slab_rcu->head, kmem_rcu_free);
1891 } else {
1892 kmem_freepages(cachep, addr);
Ingo Molnar873623d2006-07-13 14:44:38 +02001893 if (OFF_SLAB(cachep))
1894 kmem_cache_free(cachep->slabp_cache, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
1896}
1897
Christoph Lameter117f6eb2006-09-25 23:31:37 -07001898static void __kmem_cache_destroy(struct kmem_cache *cachep)
1899{
1900 int i;
1901 struct kmem_list3 *l3;
1902
1903 for_each_online_cpu(i)
1904 kfree(cachep->array[i]);
1905
1906 /* NUMA: free the list3 structures */
1907 for_each_online_node(i) {
1908 l3 = cachep->nodelists[i];
1909 if (l3) {
1910 kfree(l3->shared);
1911 free_alien_cache(l3->alien);
1912 kfree(l3);
1913 }
1914 }
1915 kmem_cache_free(&cache_cache, cachep);
1916}
1917
1918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919/**
Randy.Dunlapa70773d2006-02-01 03:05:52 -08001920 * calculate_slab_order - calculate size (page order) of slabs
1921 * @cachep: pointer to the cache that is being created
1922 * @size: size of objects to be created in this cache.
1923 * @align: required alignment for the objects.
1924 * @flags: slab allocation flags
1925 *
1926 * Also calculates the number of objects per slab.
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001927 *
1928 * This could be made much more intelligent. For now, try to avoid using
1929 * high order pages for slabs. When the gfp() functions are more friendly
1930 * towards high-order requests, this should be changed.
1931 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001932static size_t calculate_slab_order(struct kmem_cache *cachep,
Randy Dunlapee13d782006-02-01 03:05:53 -08001933 size_t size, size_t align, unsigned long flags)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001934{
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02001935 unsigned long offslab_limit;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001936 size_t left_over = 0;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001937 int gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001938
Christoph Lameter0aa817f2007-05-16 22:11:01 -07001939 for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001940 unsigned int num;
1941 size_t remainder;
1942
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001943 cache_estimate(gfporder, size, align, flags, &remainder, &num);
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001944 if (!num)
1945 continue;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001946
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02001947 if (flags & CFLGS_OFF_SLAB) {
1948 /*
1949 * Max number of objs-per-slab for caches which
1950 * use off-slab slabs. Needed to avoid a possible
1951 * looping condition in cache_grow().
1952 */
1953 offslab_limit = size - sizeof(struct slab);
1954 offslab_limit /= sizeof(kmem_bufctl_t);
1955
1956 if (num > offslab_limit)
1957 break;
1958 }
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001959
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001960 /* Found something acceptable - save it away */
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001961 cachep->num = num;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001962 cachep->gfporder = gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001963 left_over = remainder;
1964
1965 /*
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08001966 * A VFS-reclaimable slab tends to have most allocations
1967 * as GFP_NOFS and we really don't want to have to be allocating
1968 * higher-order pages when we are unable to shrink dcache.
1969 */
1970 if (flags & SLAB_RECLAIM_ACCOUNT)
1971 break;
1972
1973 /*
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001974 * Large number of objects is good, but very large slabs are
1975 * currently bad for the gfp()s.
1976 */
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001977 if (gfporder >= slab_break_gfp_order)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001978 break;
1979
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001980 /*
1981 * Acceptable internal fragmentation?
1982 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001983 if (left_over * 8 <= (PAGE_SIZE << gfporder))
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001984 break;
1985 }
1986 return left_over;
1987}
1988
Pekka Enberg83b519e2009-06-10 19:40:04 +03001989static int __init_refok setup_cpu_cache(struct kmem_cache *cachep, gfp_t gfp)
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08001990{
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07001991 if (g_cpucache_up == FULL)
Pekka Enberg83b519e2009-06-10 19:40:04 +03001992 return enable_cpucache(cachep, gfp);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07001993
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08001994 if (g_cpucache_up == NONE) {
1995 /*
1996 * Note: the first kmem_cache_create must create the cache
1997 * that's used by kmalloc(24), otherwise the creation of
1998 * further caches will BUG().
1999 */
2000 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2001
2002 /*
2003 * If the cache that's used by kmalloc(sizeof(kmem_list3)) is
2004 * the first cache, then we need to set up all its list3s,
2005 * otherwise the creation of further caches will BUG().
2006 */
2007 set_up_list3s(cachep, SIZE_AC);
2008 if (INDEX_AC == INDEX_L3)
2009 g_cpucache_up = PARTIAL_L3;
2010 else
2011 g_cpucache_up = PARTIAL_AC;
2012 } else {
2013 cachep->array[smp_processor_id()] =
Pekka Enberg83b519e2009-06-10 19:40:04 +03002014 kmalloc(sizeof(struct arraycache_init), gfp);
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002015
2016 if (g_cpucache_up == PARTIAL_AC) {
2017 set_up_list3s(cachep, SIZE_L3);
2018 g_cpucache_up = PARTIAL_L3;
2019 } else {
2020 int node;
Pekka Enberg556a1692008-01-25 08:20:51 +02002021 for_each_online_node(node) {
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002022 cachep->nodelists[node] =
2023 kmalloc_node(sizeof(struct kmem_list3),
2024 GFP_KERNEL, node);
2025 BUG_ON(!cachep->nodelists[node]);
2026 kmem_list3_init(cachep->nodelists[node]);
2027 }
2028 }
2029 }
2030 cachep->nodelists[numa_node_id()]->next_reap =
2031 jiffies + REAPTIMEOUT_LIST3 +
2032 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
2033
2034 cpu_cache_get(cachep)->avail = 0;
2035 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
2036 cpu_cache_get(cachep)->batchcount = 1;
2037 cpu_cache_get(cachep)->touched = 0;
2038 cachep->batchcount = 1;
2039 cachep->limit = BOOT_CPUCACHE_ENTRIES;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002040 return 0;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002041}
2042
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002043/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 * kmem_cache_create - Create a cache.
2045 * @name: A string which is used in /proc/slabinfo to identify this cache.
2046 * @size: The size of objects to be created in this cache.
2047 * @align: The required alignment for the objects.
2048 * @flags: SLAB flags
2049 * @ctor: A constructor for the objects.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 *
2051 * Returns a ptr to the cache on success, NULL on failure.
2052 * Cannot be called within a int, but can be interrupted.
Paul Mundt20c2df82007-07-20 10:11:58 +09002053 * The @ctor is run when new pages are allocated by the cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 *
2055 * @name must be valid until the cache is destroyed. This implies that
Andrew Mortona737b3e2006-03-22 00:08:11 -08002056 * the module calling this has to destroy the cache before getting unloaded.
Catalin Marinas249da162008-11-21 12:56:22 +00002057 * Note that kmem_cache_name() is not guaranteed to return the same pointer,
2058 * therefore applications must manage it themselves.
Andrew Mortona737b3e2006-03-22 00:08:11 -08002059 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 * The flags are
2061 *
2062 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2063 * to catch references to uninitialised memory.
2064 *
2065 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2066 * for buffer overruns.
2067 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2069 * cacheline. This can be beneficial if you're counting cycles as closely
2070 * as davem.
2071 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002072struct kmem_cache *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073kmem_cache_create (const char *name, size_t size, size_t align,
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002074 unsigned long flags, void (*ctor)(void *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
2076 size_t left_over, slab_size, ralign;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07002077 struct kmem_cache *cachep = NULL, *pc;
Pekka Enberg83b519e2009-06-10 19:40:04 +03002078 gfp_t gfp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 /*
2081 * Sanity checks... these are all serious usage bugs.
2082 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002083 if (!name || in_interrupt() || (size < BYTES_PER_WORD) ||
Paul Mundt20c2df82007-07-20 10:11:58 +09002084 size > KMALLOC_MAX_SIZE) {
Harvey Harrisond40cee22008-04-30 00:55:07 -07002085 printk(KERN_ERR "%s: Early error in slab %s\n", __func__,
Andrew Mortona737b3e2006-03-22 00:08:11 -08002086 name);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002087 BUG();
2088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
Ravikiran G Thirumalaif0188f42006-02-10 01:51:13 -08002090 /*
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002091 * We use cache_chain_mutex to ensure a consistent view of
Rusty Russell174596a2009-01-01 10:12:29 +10302092 * cpu_online_mask as well. Please see cpuup_callback
Ravikiran G Thirumalaif0188f42006-02-10 01:51:13 -08002093 */
Pekka Enberg83b519e2009-06-10 19:40:04 +03002094 if (slab_is_available()) {
2095 get_online_cpus();
2096 mutex_lock(&cache_chain_mutex);
2097 }
Andrew Morton4f12bb42005-11-07 00:58:00 -08002098
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07002099 list_for_each_entry(pc, &cache_chain, next) {
Andrew Morton4f12bb42005-11-07 00:58:00 -08002100 char tmp;
2101 int res;
2102
2103 /*
2104 * This happens when the module gets unloaded and doesn't
2105 * destroy its slab cache and no-one else reuses the vmalloc
2106 * area of the module. Print a warning.
2107 */
Andrew Morton138ae662006-12-06 20:36:41 -08002108 res = probe_kernel_address(pc->name, tmp);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002109 if (res) {
matzeb4169522007-05-06 14:49:52 -07002110 printk(KERN_ERR
2111 "SLAB: cache with size %d has lost its name\n",
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002112 pc->buffer_size);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002113 continue;
2114 }
2115
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002116 if (!strcmp(pc->name, name)) {
matzeb4169522007-05-06 14:49:52 -07002117 printk(KERN_ERR
2118 "kmem_cache_create: duplicate cache %s\n", name);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002119 dump_stack();
2120 goto oops;
2121 }
2122 }
2123
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124#if DEBUG
2125 WARN_ON(strchr(name, ' ')); /* It confuses parsers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126#if FORCED_DEBUG
2127 /*
2128 * Enable redzoning and last user accounting, except for caches with
2129 * large objects, if the increased size would increase the object size
2130 * above the next power of two: caches with object sizes just above a
2131 * power of two have a significant amount of internal fragmentation.
2132 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002133 if (size < 4096 || fls(size - 1) == fls(size-1 + REDZONE_ALIGN +
2134 2 * sizeof(unsigned long long)))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002135 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 if (!(flags & SLAB_DESTROY_BY_RCU))
2137 flags |= SLAB_POISON;
2138#endif
2139 if (flags & SLAB_DESTROY_BY_RCU)
2140 BUG_ON(flags & SLAB_POISON);
2141#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002143 * Always checks flags, a caller might be expecting debug support which
2144 * isn't available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 */
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002146 BUG_ON(flags & ~CREATE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Andrew Mortona737b3e2006-03-22 00:08:11 -08002148 /*
2149 * Check that size is in terms of words. This is needed to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 * unaligned accesses for some archs when redzoning is used, and makes
2151 * sure any on-slab bufctl's are also correctly aligned.
2152 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002153 if (size & (BYTES_PER_WORD - 1)) {
2154 size += (BYTES_PER_WORD - 1);
2155 size &= ~(BYTES_PER_WORD - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 }
2157
Andrew Mortona737b3e2006-03-22 00:08:11 -08002158 /* calculate the final buffer alignment: */
2159
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 /* 1) arch recommendation: can be overridden for debug */
2161 if (flags & SLAB_HWCACHE_ALIGN) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002162 /*
2163 * Default alignment: as specified by the arch code. Except if
2164 * an object is really small, then squeeze multiple objects into
2165 * one cacheline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 */
2167 ralign = cache_line_size();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002168 while (size <= ralign / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 ralign /= 2;
2170 } else {
2171 ralign = BYTES_PER_WORD;
2172 }
Pekka Enbergca5f9702006-09-25 23:31:25 -07002173
2174 /*
David Woodhouse87a927c2007-07-04 21:26:44 -04002175 * Redzoning and user store require word alignment or possibly larger.
2176 * Note this will be overridden by architecture or caller mandated
2177 * alignment if either is greater than BYTES_PER_WORD.
Pekka Enbergca5f9702006-09-25 23:31:25 -07002178 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002179 if (flags & SLAB_STORE_USER)
2180 ralign = BYTES_PER_WORD;
2181
2182 if (flags & SLAB_RED_ZONE) {
2183 ralign = REDZONE_ALIGN;
2184 /* If redzoning, ensure that the second redzone is suitably
2185 * aligned, by adjusting the object size accordingly. */
2186 size += REDZONE_ALIGN - 1;
2187 size &= ~(REDZONE_ALIGN - 1);
2188 }
Pekka Enbergca5f9702006-09-25 23:31:25 -07002189
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002190 /* 2) arch mandated alignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 if (ralign < ARCH_SLAB_MINALIGN) {
2192 ralign = ARCH_SLAB_MINALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 }
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002194 /* 3) caller mandated alignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (ralign < align) {
2196 ralign = align;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 }
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002198 /* disable debug if necessary */
David Woodhouseb46b8f12007-05-08 00:22:59 -07002199 if (ralign > __alignof__(unsigned long long))
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002200 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002201 /*
Pekka Enbergca5f9702006-09-25 23:31:25 -07002202 * 4) Store it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 */
2204 align = ralign;
2205
Pekka Enberg83b519e2009-06-10 19:40:04 +03002206 if (slab_is_available())
2207 gfp = GFP_KERNEL;
2208 else
2209 gfp = GFP_NOWAIT;
2210
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 /* Get cache's description obj. */
Pekka Enberg83b519e2009-06-10 19:40:04 +03002212 cachep = kmem_cache_zalloc(&cache_cache, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if (!cachep)
Andrew Morton4f12bb42005-11-07 00:58:00 -08002214 goto oops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215
2216#if DEBUG
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002217 cachep->obj_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218
Pekka Enbergca5f9702006-09-25 23:31:25 -07002219 /*
2220 * Both debugging options require word-alignment which is calculated
2221 * into align above.
2222 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223 if (flags & SLAB_RED_ZONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 /* add space for red zone words */
David Woodhouseb46b8f12007-05-08 00:22:59 -07002225 cachep->obj_offset += sizeof(unsigned long long);
2226 size += 2 * sizeof(unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 }
2228 if (flags & SLAB_STORE_USER) {
Pekka Enbergca5f9702006-09-25 23:31:25 -07002229 /* user store requires one word storage behind the end of
David Woodhouse87a927c2007-07-04 21:26:44 -04002230 * the real object. But if the second red zone needs to be
2231 * aligned to 64 bits, we must allow that much space.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 */
David Woodhouse87a927c2007-07-04 21:26:44 -04002233 if (flags & SLAB_RED_ZONE)
2234 size += REDZONE_ALIGN;
2235 else
2236 size += BYTES_PER_WORD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 }
2238#if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002239 if (size >= malloc_sizes[INDEX_L3 + 1].cs_size
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002240 && cachep->obj_size > cache_line_size() && size < PAGE_SIZE) {
2241 cachep->obj_offset += PAGE_SIZE - size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 size = PAGE_SIZE;
2243 }
2244#endif
2245#endif
2246
Ingo Molnare0a42722006-06-23 02:03:46 -07002247 /*
2248 * Determine if the slab management is 'on' or 'off' slab.
2249 * (bootstrapping cannot cope with offslab caches so don't do
2250 * it too early on.)
2251 */
2252 if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 /*
2254 * Size is large, assume best to place the slab management obj
2255 * off-slab (should allow better packing of objs).
2256 */
2257 flags |= CFLGS_OFF_SLAB;
2258
2259 size = ALIGN(size, align);
2260
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08002261 left_over = calculate_slab_order(cachep, size, align, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
2263 if (!cachep->num) {
matzeb4169522007-05-06 14:49:52 -07002264 printk(KERN_ERR
2265 "kmem_cache_create: couldn't create cache %s.\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 kmem_cache_free(&cache_cache, cachep);
2267 cachep = NULL;
Andrew Morton4f12bb42005-11-07 00:58:00 -08002268 goto oops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002270 slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
2271 + sizeof(struct slab), align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272
2273 /*
2274 * If the slab has been placed off-slab, and we have enough space then
2275 * move it on-slab. This is at the expense of any extra colouring.
2276 */
2277 if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
2278 flags &= ~CFLGS_OFF_SLAB;
2279 left_over -= slab_size;
2280 }
2281
2282 if (flags & CFLGS_OFF_SLAB) {
2283 /* really off slab. No need for manual alignment */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002284 slab_size =
2285 cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 }
2287
2288 cachep->colour_off = cache_line_size();
2289 /* Offset must be a multiple of the alignment. */
2290 if (cachep->colour_off < align)
2291 cachep->colour_off = align;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002292 cachep->colour = left_over / cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 cachep->slab_size = slab_size;
2294 cachep->flags = flags;
2295 cachep->gfpflags = 0;
Christoph Lameter4b51d662007-02-10 01:43:10 -08002296 if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 cachep->gfpflags |= GFP_DMA;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002298 cachep->buffer_size = size;
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08002299 cachep->reciprocal_buffer_size = reciprocal_value(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002301 if (flags & CFLGS_OFF_SLAB) {
Victor Fuscob2d55072005-09-10 00:26:36 -07002302 cachep->slabp_cache = kmem_find_general_cachep(slab_size, 0u);
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002303 /*
2304 * This is a possibility for one of the malloc_sizes caches.
2305 * But since we go off slab only for object size greater than
2306 * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
2307 * this should not happen at all.
2308 * But leave a BUG_ON for some lucky dude.
2309 */
Christoph Lameter6cb8f912007-07-17 04:03:22 -07002310 BUG_ON(ZERO_OR_NULL_PTR(cachep->slabp_cache));
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 cachep->ctor = ctor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 cachep->name = name;
2314
Pekka Enberg83b519e2009-06-10 19:40:04 +03002315 if (setup_cpu_cache(cachep, gfp)) {
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002316 __kmem_cache_destroy(cachep);
2317 cachep = NULL;
2318 goto oops;
2319 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 /* cache setup completed, link it into the list */
2322 list_add(&cachep->next, &cache_chain);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002323oops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 if (!cachep && (flags & SLAB_PANIC))
2325 panic("kmem_cache_create(): failed to create slab `%s'\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002326 name);
Pekka Enberg83b519e2009-06-10 19:40:04 +03002327 if (slab_is_available()) {
2328 mutex_unlock(&cache_chain_mutex);
2329 put_online_cpus();
2330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 return cachep;
2332}
2333EXPORT_SYMBOL(kmem_cache_create);
2334
2335#if DEBUG
2336static void check_irq_off(void)
2337{
2338 BUG_ON(!irqs_disabled());
2339}
2340
2341static void check_irq_on(void)
2342{
2343 BUG_ON(irqs_disabled());
2344}
2345
Pekka Enberg343e0d72006-02-01 03:05:50 -08002346static void check_spinlock_acquired(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
2348#ifdef CONFIG_SMP
2349 check_irq_off();
Christoph Lametere498be72005-09-09 13:03:32 -07002350 assert_spin_locked(&cachep->nodelists[numa_node_id()]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351#endif
2352}
Christoph Lametere498be72005-09-09 13:03:32 -07002353
Pekka Enberg343e0d72006-02-01 03:05:50 -08002354static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07002355{
2356#ifdef CONFIG_SMP
2357 check_irq_off();
2358 assert_spin_locked(&cachep->nodelists[node]->list_lock);
2359#endif
2360}
2361
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362#else
2363#define check_irq_off() do { } while(0)
2364#define check_irq_on() do { } while(0)
2365#define check_spinlock_acquired(x) do { } while(0)
Christoph Lametere498be72005-09-09 13:03:32 -07002366#define check_spinlock_acquired_node(x, y) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367#endif
2368
Christoph Lameteraab22072006-03-22 00:09:06 -08002369static void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
2370 struct array_cache *ac,
2371 int force, int node);
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373static void do_drain(void *arg)
2374{
Andrew Mortona737b3e2006-03-22 00:08:11 -08002375 struct kmem_cache *cachep = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 struct array_cache *ac;
Christoph Lameterff694162005-09-22 21:44:02 -07002377 int node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002380 ac = cpu_cache_get(cachep);
Christoph Lameterff694162005-09-22 21:44:02 -07002381 spin_lock(&cachep->nodelists[node]->list_lock);
2382 free_block(cachep, ac->entry, ac->avail, node);
2383 spin_unlock(&cachep->nodelists[node]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 ac->avail = 0;
2385}
2386
Pekka Enberg343e0d72006-02-01 03:05:50 -08002387static void drain_cpu_caches(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388{
Christoph Lametere498be72005-09-09 13:03:32 -07002389 struct kmem_list3 *l3;
2390 int node;
2391
Jens Axboe15c8b6c2008-05-09 09:39:44 +02002392 on_each_cpu(do_drain, cachep, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 check_irq_on();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002394 for_each_online_node(node) {
Christoph Lametere498be72005-09-09 13:03:32 -07002395 l3 = cachep->nodelists[node];
Roland Dreiera4523a82006-05-15 11:41:00 -07002396 if (l3 && l3->alien)
2397 drain_alien_cache(cachep, l3->alien);
2398 }
2399
2400 for_each_online_node(node) {
2401 l3 = cachep->nodelists[node];
2402 if (l3)
Christoph Lameteraab22072006-03-22 00:09:06 -08002403 drain_array(cachep, l3, l3->shared, 1, node);
Christoph Lametere498be72005-09-09 13:03:32 -07002404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405}
2406
Christoph Lametered11d9e2006-06-30 01:55:45 -07002407/*
2408 * Remove slabs from the list of free slabs.
2409 * Specify the number of slabs to drain in tofree.
2410 *
2411 * Returns the actual number of slabs released.
2412 */
2413static int drain_freelist(struct kmem_cache *cache,
2414 struct kmem_list3 *l3, int tofree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
Christoph Lametered11d9e2006-06-30 01:55:45 -07002416 struct list_head *p;
2417 int nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419
Christoph Lametered11d9e2006-06-30 01:55:45 -07002420 nr_freed = 0;
2421 while (nr_freed < tofree && !list_empty(&l3->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Christoph Lametered11d9e2006-06-30 01:55:45 -07002423 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002424 p = l3->slabs_free.prev;
Christoph Lametered11d9e2006-06-30 01:55:45 -07002425 if (p == &l3->slabs_free) {
2426 spin_unlock_irq(&l3->list_lock);
2427 goto out;
2428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429
Christoph Lametered11d9e2006-06-30 01:55:45 -07002430 slabp = list_entry(p, struct slab, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431#if DEBUG
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002432 BUG_ON(slabp->inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433#endif
2434 list_del(&slabp->list);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002435 /*
2436 * Safe to drop the lock. The slab is no longer linked
2437 * to the cache.
2438 */
2439 l3->free_objects -= cache->num;
Christoph Lametere498be72005-09-09 13:03:32 -07002440 spin_unlock_irq(&l3->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002441 slab_destroy(cache, slabp);
2442 nr_freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 }
Christoph Lametered11d9e2006-06-30 01:55:45 -07002444out:
2445 return nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446}
2447
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002448/* Called with cache_chain_mutex held to protect against cpu hotplug */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002449static int __cache_shrink(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07002450{
2451 int ret = 0, i = 0;
2452 struct kmem_list3 *l3;
2453
2454 drain_cpu_caches(cachep);
2455
2456 check_irq_on();
2457 for_each_online_node(i) {
2458 l3 = cachep->nodelists[i];
Christoph Lametered11d9e2006-06-30 01:55:45 -07002459 if (!l3)
2460 continue;
2461
2462 drain_freelist(cachep, l3, l3->free_objects);
2463
2464 ret += !list_empty(&l3->slabs_full) ||
2465 !list_empty(&l3->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07002466 }
2467 return (ret ? 1 : 0);
2468}
2469
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470/**
2471 * kmem_cache_shrink - Shrink a cache.
2472 * @cachep: The cache to shrink.
2473 *
2474 * Releases as many slabs as possible for a cache.
2475 * To help debugging, a zero exit status indicates all slabs were released.
2476 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002477int kmem_cache_shrink(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478{
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002479 int ret;
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002480 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002482 get_online_cpus();
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002483 mutex_lock(&cache_chain_mutex);
2484 ret = __cache_shrink(cachep);
2485 mutex_unlock(&cache_chain_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002486 put_online_cpus();
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002487 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488}
2489EXPORT_SYMBOL(kmem_cache_shrink);
2490
2491/**
2492 * kmem_cache_destroy - delete a cache
2493 * @cachep: the cache to destroy
2494 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002495 * Remove a &struct kmem_cache object from the slab cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 *
2497 * It is expected this function will be called by a module when it is
2498 * unloaded. This will remove the cache completely, and avoid a duplicate
2499 * cache being allocated each time a module is loaded and unloaded, if the
2500 * module doesn't have persistent in-kernel storage across loads and unloads.
2501 *
2502 * The cache must be empty before calling this function.
2503 *
2504 * The caller must guarantee that noone will allocate memory from the cache
2505 * during the kmem_cache_destroy().
2506 */
Alexey Dobriyan133d2052006-09-27 01:49:41 -07002507void kmem_cache_destroy(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508{
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002509 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511 /* Find the cache in the chain of caches. */
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002512 get_online_cpus();
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002513 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514 /*
2515 * the chain is never empty, cache_cache is never destroyed
2516 */
2517 list_del(&cachep->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 if (__cache_shrink(cachep)) {
2519 slab_error(cachep, "Can't free all objects");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002520 list_add(&cachep->next, &cache_chain);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002521 mutex_unlock(&cache_chain_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002522 put_online_cpus();
Alexey Dobriyan133d2052006-09-27 01:49:41 -07002523 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 }
2525
2526 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU))
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002527 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528
Christoph Lameter117f6eb2006-09-25 23:31:37 -07002529 __kmem_cache_destroy(cachep);
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002530 mutex_unlock(&cache_chain_mutex);
Gautham R Shenoy95402b32008-01-25 21:08:02 +01002531 put_online_cpus();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532}
2533EXPORT_SYMBOL(kmem_cache_destroy);
2534
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002535/*
2536 * Get the memory for a slab management obj.
2537 * For a slab cache when the slab descriptor is off-slab, slab descriptors
2538 * always come from malloc_sizes caches. The slab descriptor cannot
2539 * come from the same cache which is getting created because,
2540 * when we are searching for an appropriate cache for these
2541 * descriptors in kmem_cache_create, we search through the malloc_sizes array.
2542 * If we are creating a malloc_sizes cache here it would not be visible to
2543 * kmem_find_general_cachep till the initialization is complete.
2544 * Hence we cannot have slabp_cache same as the original cache.
2545 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002546static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002547 int colour_off, gfp_t local_flags,
2548 int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
2550 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 if (OFF_SLAB(cachep)) {
2553 /* Slab management obj is off-slab. */
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002554 slabp = kmem_cache_alloc_node(cachep->slabp_cache,
Pekka Enberg8759ec52008-11-26 10:01:31 +02002555 local_flags, nodeid);
Catalin Marinasd5cff632009-06-11 13:22:40 +01002556 /*
2557 * If the first object in the slab is leaked (it's allocated
2558 * but no one has a reference to it), we want to make sure
2559 * kmemleak does not treat the ->s_mem pointer as a reference
2560 * to the object. Otherwise we will not report the leak.
2561 */
2562 kmemleak_scan_area(slabp, offsetof(struct slab, list),
2563 sizeof(struct list_head), local_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 if (!slabp)
2565 return NULL;
2566 } else {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002567 slabp = objp + colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 colour_off += cachep->slab_size;
2569 }
2570 slabp->inuse = 0;
2571 slabp->colouroff = colour_off;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002572 slabp->s_mem = objp + colour_off;
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002573 slabp->nodeid = nodeid;
Marcin Slusarze51bfd02008-02-10 11:21:54 +01002574 slabp->free = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 return slabp;
2576}
2577
2578static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
2579{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002580 return (kmem_bufctl_t *) (slabp + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581}
2582
Pekka Enberg343e0d72006-02-01 03:05:50 -08002583static void cache_init_objs(struct kmem_cache *cachep,
Christoph Lametera35afb82007-05-16 22:10:57 -07002584 struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585{
2586 int i;
2587
2588 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002589 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590#if DEBUG
2591 /* need to poison the objs? */
2592 if (cachep->flags & SLAB_POISON)
2593 poison_obj(cachep, objp, POISON_FREE);
2594 if (cachep->flags & SLAB_STORE_USER)
2595 *dbg_userword(cachep, objp) = NULL;
2596
2597 if (cachep->flags & SLAB_RED_ZONE) {
2598 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2599 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2600 }
2601 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002602 * Constructors are not allowed to allocate memory from the same
2603 * cache which they are a constructor for. Otherwise, deadlock.
2604 * They must also be threaded.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 */
2606 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002607 cachep->ctor(objp + obj_offset(cachep));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 if (cachep->flags & SLAB_RED_ZONE) {
2610 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2611 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002612 " end of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2614 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002615 " start of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616 }
Andrew Mortona737b3e2006-03-22 00:08:11 -08002617 if ((cachep->buffer_size % PAGE_SIZE) == 0 &&
2618 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002619 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002620 cachep->buffer_size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621#else
2622 if (cachep->ctor)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07002623 cachep->ctor(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624#endif
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002625 slab_bufctl(slabp)[i] = i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002627 slab_bufctl(slabp)[i - 1] = BUFCTL_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628}
2629
Pekka Enberg343e0d72006-02-01 03:05:50 -08002630static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
Christoph Lameter4b51d662007-02-10 01:43:10 -08002632 if (CONFIG_ZONE_DMA_FLAG) {
2633 if (flags & GFP_DMA)
2634 BUG_ON(!(cachep->gfpflags & GFP_DMA));
2635 else
2636 BUG_ON(cachep->gfpflags & GFP_DMA);
2637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638}
2639
Andrew Mortona737b3e2006-03-22 00:08:11 -08002640static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
2641 int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002642{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002643 void *objp = index_to_obj(cachep, slabp, slabp->free);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002644 kmem_bufctl_t next;
2645
2646 slabp->inuse++;
2647 next = slab_bufctl(slabp)[slabp->free];
2648#if DEBUG
2649 slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
2650 WARN_ON(slabp->nodeid != nodeid);
2651#endif
2652 slabp->free = next;
2653
2654 return objp;
2655}
2656
Andrew Mortona737b3e2006-03-22 00:08:11 -08002657static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
2658 void *objp, int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002659{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002660 unsigned int objnr = obj_to_index(cachep, slabp, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002661
2662#if DEBUG
2663 /* Verify that the slab belongs to the intended node */
2664 WARN_ON(slabp->nodeid != nodeid);
2665
Al Viro871751e2006-03-25 03:06:39 -08002666 if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) {
Matthew Dobson78d382d2006-02-01 03:05:47 -08002667 printk(KERN_ERR "slab: double free detected in cache "
Andrew Mortona737b3e2006-03-22 00:08:11 -08002668 "'%s', objp %p\n", cachep->name, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002669 BUG();
2670 }
2671#endif
2672 slab_bufctl(slabp)[objnr] = slabp->free;
2673 slabp->free = objnr;
2674 slabp->inuse--;
2675}
2676
Pekka Enberg47768742006-06-23 02:03:07 -07002677/*
2678 * Map pages beginning at addr to the given cache and slab. This is required
2679 * for the slab allocator to be able to lookup the cache and slab of a
2680 * virtual address for kfree, ksize, kmem_ptr_validate, and slab debugging.
2681 */
2682static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
2683 void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
Pekka Enberg47768742006-06-23 02:03:07 -07002685 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 struct page *page;
2687
Pekka Enberg47768742006-06-23 02:03:07 -07002688 page = virt_to_page(addr);
Nick Piggin84097512006-03-22 00:08:34 -08002689
Pekka Enberg47768742006-06-23 02:03:07 -07002690 nr_pages = 1;
Nick Piggin84097512006-03-22 00:08:34 -08002691 if (likely(!PageCompound(page)))
Pekka Enberg47768742006-06-23 02:03:07 -07002692 nr_pages <<= cache->gfporder;
2693
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694 do {
Pekka Enberg47768742006-06-23 02:03:07 -07002695 page_set_cache(page, cache);
2696 page_set_slab(page, slab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 page++;
Pekka Enberg47768742006-06-23 02:03:07 -07002698 } while (--nr_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699}
2700
2701/*
2702 * Grow (by 1) the number of slabs within a cache. This is called by
2703 * kmem_cache_alloc() when there are no active objs left in a cache.
2704 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002705static int cache_grow(struct kmem_cache *cachep,
2706 gfp_t flags, int nodeid, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002708 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002709 size_t offset;
2710 gfp_t local_flags;
Christoph Lametere498be72005-09-09 13:03:32 -07002711 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Andrew Mortona737b3e2006-03-22 00:08:11 -08002713 /*
2714 * Be lazy and only check for valid flags here, keeping it out of the
2715 * critical path in kmem_cache_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 */
Christoph Lameter6cb06222007-10-16 01:25:41 -07002717 BUG_ON(flags & GFP_SLAB_BUG_MASK);
2718 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002720 /* Take the l3 list lock to change the colour_next on this node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 check_irq_off();
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002722 l3 = cachep->nodelists[nodeid];
2723 spin_lock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724
2725 /* Get colour for the slab, and cal the next value. */
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002726 offset = l3->colour_next;
2727 l3->colour_next++;
2728 if (l3->colour_next >= cachep->colour)
2729 l3->colour_next = 0;
2730 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002732 offset *= cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
2734 if (local_flags & __GFP_WAIT)
2735 local_irq_enable();
2736
2737 /*
2738 * The test for missing atomic flag is performed here, rather than
2739 * the more obvious place, simply to reduce the critical path length
2740 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2741 * will eventually be caught here (where it matters).
2742 */
2743 kmem_flagcheck(cachep, flags);
2744
Andrew Mortona737b3e2006-03-22 00:08:11 -08002745 /*
2746 * Get mem for the objs. Attempt to allocate a physical page from
2747 * 'nodeid'.
Christoph Lametere498be72005-09-09 13:03:32 -07002748 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002749 if (!objp)
Andrew Mortonb8c1c5d2007-07-24 12:02:40 -07002750 objp = kmem_getpages(cachep, local_flags, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002751 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 goto failed;
2753
2754 /* Get slab management. */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002755 slabp = alloc_slabmgmt(cachep, objp, offset,
Christoph Lameter6cb06222007-10-16 01:25:41 -07002756 local_flags & ~GFP_CONSTRAINT_MASK, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002757 if (!slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 goto opps1;
2759
Pekka Enberg47768742006-06-23 02:03:07 -07002760 slab_map_pages(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
Christoph Lametera35afb82007-05-16 22:10:57 -07002762 cache_init_objs(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
2764 if (local_flags & __GFP_WAIT)
2765 local_irq_disable();
2766 check_irq_off();
Christoph Lametere498be72005-09-09 13:03:32 -07002767 spin_lock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
2769 /* Make slab active. */
Christoph Lametere498be72005-09-09 13:03:32 -07002770 list_add_tail(&slabp->list, &(l3->slabs_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 STATS_INC_GROWN(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07002772 l3->free_objects += cachep->num;
2773 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002775opps1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 kmem_freepages(cachep, objp);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002777failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 if (local_flags & __GFP_WAIT)
2779 local_irq_disable();
2780 return 0;
2781}
2782
2783#if DEBUG
2784
2785/*
2786 * Perform extra freeing checks:
2787 * - detect bad pointers.
2788 * - POISON/RED_ZONE checking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 */
2790static void kfree_debugcheck(const void *objp)
2791{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 if (!virt_addr_valid(objp)) {
2793 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002794 (unsigned long)objp);
2795 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797}
2798
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002799static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2800{
David Woodhouseb46b8f12007-05-08 00:22:59 -07002801 unsigned long long redzone1, redzone2;
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002802
2803 redzone1 = *dbg_redzone1(cache, obj);
2804 redzone2 = *dbg_redzone2(cache, obj);
2805
2806 /*
2807 * Redzone is ok.
2808 */
2809 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2810 return;
2811
2812 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2813 slab_error(cache, "double free detected");
2814 else
2815 slab_error(cache, "memory outside object was overwritten");
2816
David Woodhouseb46b8f12007-05-08 00:22:59 -07002817 printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002818 obj, redzone1, redzone2);
2819}
2820
Pekka Enberg343e0d72006-02-01 03:05:50 -08002821static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002822 void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823{
2824 struct page *page;
2825 unsigned int objnr;
2826 struct slab *slabp;
2827
Matthew Wilcox80cbd912007-11-29 12:05:13 -07002828 BUG_ON(virt_to_cache(objp) != cachep);
2829
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002830 objp -= obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 kfree_debugcheck(objp);
Christoph Lameterb49af682007-05-06 14:49:41 -07002832 page = virt_to_head_page(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Pekka Enberg065d41c2005-11-13 16:06:46 -08002834 slabp = page_get_slab(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
2836 if (cachep->flags & SLAB_RED_ZONE) {
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002837 verify_redzone_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2839 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2840 }
2841 if (cachep->flags & SLAB_STORE_USER)
2842 *dbg_userword(cachep, objp) = caller;
2843
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002844 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845
2846 BUG_ON(objnr >= cachep->num);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002847 BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
Al Viro871751e2006-03-25 03:06:39 -08002849#ifdef CONFIG_DEBUG_SLAB_LEAK
2850 slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
2851#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 if (cachep->flags & SLAB_POISON) {
2853#ifdef CONFIG_DEBUG_PAGEALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -08002854 if ((cachep->buffer_size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 store_stackinfo(cachep, objp, (unsigned long)caller);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002856 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002857 cachep->buffer_size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002858 } else {
2859 poison_obj(cachep, objp, POISON_FREE);
2860 }
2861#else
2862 poison_obj(cachep, objp, POISON_FREE);
2863#endif
2864 }
2865 return objp;
2866}
2867
Pekka Enberg343e0d72006-02-01 03:05:50 -08002868static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869{
2870 kmem_bufctl_t i;
2871 int entries = 0;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002872
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 /* Check slab's freelist to see if this obj is there. */
2874 for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
2875 entries++;
2876 if (entries > cachep->num || i >= cachep->num)
2877 goto bad;
2878 }
2879 if (entries != cachep->num - slabp->inuse) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002880bad:
2881 printk(KERN_ERR "slab: Internal list corruption detected in "
2882 "cache '%s'(%d), slabp %p(%d). Hexdump:\n",
2883 cachep->name, cachep->num, slabp, slabp->inuse);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002884 for (i = 0;
Linus Torvalds264132b2006-03-06 12:10:07 -08002885 i < sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002886 i++) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002887 if (i % 16 == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888 printk("\n%03x:", i);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002889 printk(" %02x", ((unsigned char *)slabp)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 }
2891 printk("\n");
2892 BUG();
2893 }
2894}
2895#else
2896#define kfree_debugcheck(x) do { } while(0)
2897#define cache_free_debugcheck(x,objp,z) (objp)
2898#define check_slabp(x,y) do { } while(0)
2899#endif
2900
Pekka Enberg343e0d72006-02-01 03:05:50 -08002901static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902{
2903 int batchcount;
2904 struct kmem_list3 *l3;
2905 struct array_cache *ac;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002906 int node;
2907
Andrew Mortona737b3e2006-03-22 00:08:11 -08002908retry:
Joe Korty6d2144d2008-03-05 15:04:59 -08002909 check_irq_off();
2910 node = numa_node_id();
2911 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 batchcount = ac->batchcount;
2913 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002914 /*
2915 * If there was little recent activity on this cache, then
2916 * perform only a partial refill. Otherwise we could generate
2917 * refill bouncing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 */
2919 batchcount = BATCHREFILL_LIMIT;
2920 }
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002921 l3 = cachep->nodelists[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Christoph Lametere498be72005-09-09 13:03:32 -07002923 BUG_ON(ac->avail > 0 || !l3);
2924 spin_lock(&l3->list_lock);
2925
Christoph Lameter3ded1752006-03-25 03:06:44 -08002926 /* See if we can refill from the shared array */
2927 if (l3->shared && transfer_objects(ac, l3->shared, batchcount))
2928 goto alloc_done;
2929
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 while (batchcount > 0) {
2931 struct list_head *entry;
2932 struct slab *slabp;
2933 /* Get slab alloc is to come from. */
2934 entry = l3->slabs_partial.next;
2935 if (entry == &l3->slabs_partial) {
2936 l3->free_touched = 1;
2937 entry = l3->slabs_free.next;
2938 if (entry == &l3->slabs_free)
2939 goto must_grow;
2940 }
2941
2942 slabp = list_entry(entry, struct slab, list);
2943 check_slabp(cachep, slabp);
2944 check_spinlock_acquired(cachep);
Pekka Enberg714b8172007-05-06 14:49:03 -07002945
2946 /*
2947 * The slab was either on partial or free list so
2948 * there must be at least one object available for
2949 * allocation.
2950 */
roel kluin249b9f32008-10-29 17:18:07 -04002951 BUG_ON(slabp->inuse >= cachep->num);
Pekka Enberg714b8172007-05-06 14:49:03 -07002952
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 while (slabp->inuse < cachep->num && batchcount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 STATS_INC_ALLOCED(cachep);
2955 STATS_INC_ACTIVE(cachep);
2956 STATS_SET_HIGH(cachep);
2957
Matthew Dobson78d382d2006-02-01 03:05:47 -08002958 ac->entry[ac->avail++] = slab_get_obj(cachep, slabp,
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002959 node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 }
2961 check_slabp(cachep, slabp);
2962
2963 /* move slabp to correct slabp list: */
2964 list_del(&slabp->list);
2965 if (slabp->free == BUFCTL_END)
2966 list_add(&slabp->list, &l3->slabs_full);
2967 else
2968 list_add(&slabp->list, &l3->slabs_partial);
2969 }
2970
Andrew Mortona737b3e2006-03-22 00:08:11 -08002971must_grow:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 l3->free_objects -= ac->avail;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002973alloc_done:
Christoph Lametere498be72005-09-09 13:03:32 -07002974 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
2976 if (unlikely(!ac->avail)) {
2977 int x;
Christoph Lameter3c517a62006-12-06 20:33:29 -08002978 x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07002979
Andrew Mortona737b3e2006-03-22 00:08:11 -08002980 /* cache_grow can reenable interrupts, then ac could change. */
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002981 ac = cpu_cache_get(cachep);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002982 if (!x && ac->avail == 0) /* no objects in sight? abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 return NULL;
2984
Andrew Mortona737b3e2006-03-22 00:08:11 -08002985 if (!ac->avail) /* objects refilled by interrupt? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 goto retry;
2987 }
2988 ac->touched = 1;
Christoph Lametere498be72005-09-09 13:03:32 -07002989 return ac->entry[--ac->avail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990}
2991
Andrew Mortona737b3e2006-03-22 00:08:11 -08002992static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
2993 gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994{
2995 might_sleep_if(flags & __GFP_WAIT);
2996#if DEBUG
2997 kmem_flagcheck(cachep, flags);
2998#endif
2999}
3000
3001#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003002static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
3003 gfp_t flags, void *objp, void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003005 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 return objp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003007 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008#ifdef CONFIG_DEBUG_PAGEALLOC
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003009 if ((cachep->buffer_size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003010 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003011 cachep->buffer_size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 else
3013 check_poison_obj(cachep, objp);
3014#else
3015 check_poison_obj(cachep, objp);
3016#endif
3017 poison_obj(cachep, objp, POISON_INUSE);
3018 }
3019 if (cachep->flags & SLAB_STORE_USER)
3020 *dbg_userword(cachep, objp) = caller;
3021
3022 if (cachep->flags & SLAB_RED_ZONE) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003023 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
3024 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
3025 slab_error(cachep, "double free, or memory outside"
3026 " object was overwritten");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003027 printk(KERN_ERR
David Woodhouseb46b8f12007-05-08 00:22:59 -07003028 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08003029 objp, *dbg_redzone1(cachep, objp),
3030 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 }
3032 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
3033 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
3034 }
Al Viro871751e2006-03-25 03:06:39 -08003035#ifdef CONFIG_DEBUG_SLAB_LEAK
3036 {
3037 struct slab *slabp;
3038 unsigned objnr;
3039
Christoph Lameterb49af682007-05-06 14:49:41 -07003040 slabp = page_get_slab(virt_to_head_page(objp));
Al Viro871751e2006-03-25 03:06:39 -08003041 objnr = (unsigned)(objp - slabp->s_mem) / cachep->buffer_size;
3042 slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE;
3043 }
3044#endif
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003045 objp += obj_offset(cachep);
Christoph Lameter4f104932007-05-06 14:50:17 -07003046 if (cachep->ctor && cachep->flags & SLAB_POISON)
Alexey Dobriyan51cc5062008-07-25 19:45:34 -07003047 cachep->ctor(objp);
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003048#if ARCH_SLAB_MINALIGN
3049 if ((u32)objp & (ARCH_SLAB_MINALIGN-1)) {
3050 printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
3051 objp, ARCH_SLAB_MINALIGN);
3052 }
3053#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 return objp;
3055}
3056#else
3057#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
3058#endif
3059
Akinobu Mita773ff602008-12-23 19:37:01 +09003060static bool slab_should_failslab(struct kmem_cache *cachep, gfp_t flags)
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003061{
3062 if (cachep == &cache_cache)
Akinobu Mita773ff602008-12-23 19:37:01 +09003063 return false;
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003064
Akinobu Mita773ff602008-12-23 19:37:01 +09003065 return should_failslab(obj_size(cachep), flags);
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003066}
3067
Pekka Enberg343e0d72006-02-01 03:05:50 -08003068static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003070 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071 struct array_cache *ac;
3072
Alok N Kataria5c382302005-09-27 21:45:46 -07003073 check_irq_off();
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003074
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003075 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 if (likely(ac->avail)) {
3077 STATS_INC_ALLOCHIT(cachep);
3078 ac->touched = 1;
Christoph Lametere498be72005-09-09 13:03:32 -07003079 objp = ac->entry[--ac->avail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080 } else {
3081 STATS_INC_ALLOCMISS(cachep);
3082 objp = cache_alloc_refill(cachep, flags);
3083 }
Catalin Marinasd5cff632009-06-11 13:22:40 +01003084 /*
3085 * To avoid a false negative, if an object that is in one of the
3086 * per-CPU caches is leaked, we need to make sure kmemleak doesn't
3087 * treat the array pointers as a reference to the object.
3088 */
3089 kmemleak_erase(&ac->entry[ac->avail]);
Alok N Kataria5c382302005-09-27 21:45:46 -07003090 return objp;
3091}
3092
Christoph Lametere498be72005-09-09 13:03:32 -07003093#ifdef CONFIG_NUMA
3094/*
Paul Jacksonb2455392006-03-24 03:16:12 -08003095 * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
Paul Jacksonc61afb12006-03-24 03:16:08 -08003096 *
3097 * If we are in_interrupt, then process context, including cpusets and
3098 * mempolicy, may not apply and should not be used for allocation policy.
3099 */
3100static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
3101{
3102 int nid_alloc, nid_here;
3103
Christoph Lameter765c4502006-09-27 01:50:08 -07003104 if (in_interrupt() || (flags & __GFP_THISNODE))
Paul Jacksonc61afb12006-03-24 03:16:08 -08003105 return NULL;
3106 nid_alloc = nid_here = numa_node_id();
3107 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
3108 nid_alloc = cpuset_mem_spread_node();
3109 else if (current->mempolicy)
3110 nid_alloc = slab_node(current->mempolicy);
3111 if (nid_alloc != nid_here)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003112 return ____cache_alloc_node(cachep, flags, nid_alloc);
Paul Jacksonc61afb12006-03-24 03:16:08 -08003113 return NULL;
3114}
3115
3116/*
Christoph Lameter765c4502006-09-27 01:50:08 -07003117 * Fallback function if there was no memory available and no objects on a
Christoph Lameter3c517a62006-12-06 20:33:29 -08003118 * certain node and fall back is permitted. First we scan all the
3119 * available nodelists for available objects. If that fails then we
3120 * perform an allocation without specifying a node. This allows the page
3121 * allocator to do its reclaim / fallback magic. We then insert the
3122 * slab into the proper nodelist and then allocate from it.
Christoph Lameter765c4502006-09-27 01:50:08 -07003123 */
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003124static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
Christoph Lameter765c4502006-09-27 01:50:08 -07003125{
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003126 struct zonelist *zonelist;
3127 gfp_t local_flags;
Mel Gormandd1a2392008-04-28 02:12:17 -07003128 struct zoneref *z;
Mel Gorman54a6eb52008-04-28 02:12:16 -07003129 struct zone *zone;
3130 enum zone_type high_zoneidx = gfp_zone(flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003131 void *obj = NULL;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003132 int nid;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003133
3134 if (flags & __GFP_THISNODE)
3135 return NULL;
3136
Mel Gorman0e884602008-04-28 02:12:14 -07003137 zonelist = node_zonelist(slab_node(current->mempolicy), flags);
Christoph Lameter6cb06222007-10-16 01:25:41 -07003138 local_flags = flags & (GFP_CONSTRAINT_MASK|GFP_RECLAIM_MASK);
Christoph Lameter765c4502006-09-27 01:50:08 -07003139
Christoph Lameter3c517a62006-12-06 20:33:29 -08003140retry:
3141 /*
3142 * Look through allowed nodes for objects available
3143 * from existing per node queues.
3144 */
Mel Gorman54a6eb52008-04-28 02:12:16 -07003145 for_each_zone_zonelist(zone, z, zonelist, high_zoneidx) {
3146 nid = zone_to_nid(zone);
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003147
Mel Gorman54a6eb52008-04-28 02:12:16 -07003148 if (cpuset_zone_allowed_hardwall(zone, flags) &&
Christoph Lameter3c517a62006-12-06 20:33:29 -08003149 cache->nodelists[nid] &&
Christoph Lameter481c5342008-06-21 16:46:35 -07003150 cache->nodelists[nid]->free_objects) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003151 obj = ____cache_alloc_node(cache,
3152 flags | GFP_THISNODE, nid);
Christoph Lameter481c5342008-06-21 16:46:35 -07003153 if (obj)
3154 break;
3155 }
Christoph Lameter3c517a62006-12-06 20:33:29 -08003156 }
3157
Christoph Lametercfce6602007-05-06 14:50:17 -07003158 if (!obj) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003159 /*
3160 * This allocation will be performed within the constraints
3161 * of the current cpuset / memory policy requirements.
3162 * We may trigger various forms of reclaim on the allowed
3163 * set and go into memory reserves if necessary.
3164 */
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003165 if (local_flags & __GFP_WAIT)
3166 local_irq_enable();
3167 kmem_flagcheck(cache, flags);
Christoph Lameter9ac33b22008-03-04 12:24:22 -08003168 obj = kmem_getpages(cache, local_flags, -1);
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003169 if (local_flags & __GFP_WAIT)
3170 local_irq_disable();
Christoph Lameter3c517a62006-12-06 20:33:29 -08003171 if (obj) {
3172 /*
3173 * Insert into the appropriate per node queues
3174 */
3175 nid = page_to_nid(virt_to_page(obj));
3176 if (cache_grow(cache, flags, nid, obj)) {
3177 obj = ____cache_alloc_node(cache,
3178 flags | GFP_THISNODE, nid);
3179 if (!obj)
3180 /*
3181 * Another processor may allocate the
3182 * objects in the slab since we are
3183 * not holding any locks.
3184 */
3185 goto retry;
3186 } else {
Hugh Dickinsb6a60452007-01-05 16:36:36 -08003187 /* cache_grow already freed obj */
Christoph Lameter3c517a62006-12-06 20:33:29 -08003188 obj = NULL;
3189 }
3190 }
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003191 }
Christoph Lameter765c4502006-09-27 01:50:08 -07003192 return obj;
3193}
3194
3195/*
Christoph Lametere498be72005-09-09 13:03:32 -07003196 * A interface to enable slab creation on nodeid
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003198static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003199 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07003200{
3201 struct list_head *entry;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003202 struct slab *slabp;
3203 struct kmem_list3 *l3;
3204 void *obj;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003205 int x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003207 l3 = cachep->nodelists[nodeid];
3208 BUG_ON(!l3);
Christoph Lametere498be72005-09-09 13:03:32 -07003209
Andrew Mortona737b3e2006-03-22 00:08:11 -08003210retry:
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003211 check_irq_off();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003212 spin_lock(&l3->list_lock);
3213 entry = l3->slabs_partial.next;
3214 if (entry == &l3->slabs_partial) {
3215 l3->free_touched = 1;
3216 entry = l3->slabs_free.next;
3217 if (entry == &l3->slabs_free)
3218 goto must_grow;
3219 }
Christoph Lametere498be72005-09-09 13:03:32 -07003220
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003221 slabp = list_entry(entry, struct slab, list);
3222 check_spinlock_acquired_node(cachep, nodeid);
3223 check_slabp(cachep, slabp);
Christoph Lametere498be72005-09-09 13:03:32 -07003224
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003225 STATS_INC_NODEALLOCS(cachep);
3226 STATS_INC_ACTIVE(cachep);
3227 STATS_SET_HIGH(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003228
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003229 BUG_ON(slabp->inuse == cachep->num);
Christoph Lametere498be72005-09-09 13:03:32 -07003230
Matthew Dobson78d382d2006-02-01 03:05:47 -08003231 obj = slab_get_obj(cachep, slabp, nodeid);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003232 check_slabp(cachep, slabp);
3233 l3->free_objects--;
3234 /* move slabp to correct slabp list: */
3235 list_del(&slabp->list);
Christoph Lametere498be72005-09-09 13:03:32 -07003236
Andrew Mortona737b3e2006-03-22 00:08:11 -08003237 if (slabp->free == BUFCTL_END)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003238 list_add(&slabp->list, &l3->slabs_full);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003239 else
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003240 list_add(&slabp->list, &l3->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07003241
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003242 spin_unlock(&l3->list_lock);
3243 goto done;
Christoph Lametere498be72005-09-09 13:03:32 -07003244
Andrew Mortona737b3e2006-03-22 00:08:11 -08003245must_grow:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003246 spin_unlock(&l3->list_lock);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003247 x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
Christoph Lameter765c4502006-09-27 01:50:08 -07003248 if (x)
3249 goto retry;
Christoph Lametere498be72005-09-09 13:03:32 -07003250
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003251 return fallback_alloc(cachep, flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003252
Andrew Mortona737b3e2006-03-22 00:08:11 -08003253done:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003254 return obj;
Christoph Lametere498be72005-09-09 13:03:32 -07003255}
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003256
3257/**
3258 * kmem_cache_alloc_node - Allocate an object on the specified node
3259 * @cachep: The cache to allocate from.
3260 * @flags: See kmalloc().
3261 * @nodeid: node number of the target node.
3262 * @caller: return address of caller, used for debug information
3263 *
3264 * Identical to kmem_cache_alloc but it will allocate memory on the given
3265 * node, which can improve the performance for cpu bound structures.
3266 *
3267 * Fallback to other node is possible if __GFP_THISNODE is not set.
3268 */
3269static __always_inline void *
3270__cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
3271 void *caller)
3272{
3273 unsigned long save_flags;
3274 void *ptr;
3275
Nick Piggincf40bd12009-01-21 08:12:39 +01003276 lockdep_trace_alloc(flags);
3277
Akinobu Mita773ff602008-12-23 19:37:01 +09003278 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003279 return NULL;
3280
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003281 cache_alloc_debugcheck_before(cachep, flags);
3282 local_irq_save(save_flags);
3283
3284 if (unlikely(nodeid == -1))
3285 nodeid = numa_node_id();
3286
3287 if (unlikely(!cachep->nodelists[nodeid])) {
3288 /* Node not bootstrapped yet */
3289 ptr = fallback_alloc(cachep, flags);
3290 goto out;
3291 }
3292
3293 if (nodeid == numa_node_id()) {
3294 /*
3295 * Use the locally cached objects if possible.
3296 * However ____cache_alloc does not allow fallback
3297 * to other nodes. It may fail while we still have
3298 * objects on other nodes available.
3299 */
3300 ptr = ____cache_alloc(cachep, flags);
3301 if (ptr)
3302 goto out;
3303 }
3304 /* ___cache_alloc_node can fall back to other nodes */
3305 ptr = ____cache_alloc_node(cachep, flags, nodeid);
3306 out:
3307 local_irq_restore(save_flags);
3308 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
Catalin Marinasd5cff632009-06-11 13:22:40 +01003309 kmemleak_alloc_recursive(ptr, obj_size(cachep), 1, cachep->flags,
3310 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003311
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003312 if (unlikely((flags & __GFP_ZERO) && ptr))
3313 memset(ptr, 0, obj_size(cachep));
3314
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003315 return ptr;
3316}
3317
3318static __always_inline void *
3319__do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
3320{
3321 void *objp;
3322
3323 if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
3324 objp = alternate_node_alloc(cache, flags);
3325 if (objp)
3326 goto out;
3327 }
3328 objp = ____cache_alloc(cache, flags);
3329
3330 /*
3331 * We may just have run out of memory on the local node.
3332 * ____cache_alloc_node() knows how to locate memory on other nodes
3333 */
3334 if (!objp)
3335 objp = ____cache_alloc_node(cache, flags, numa_node_id());
3336
3337 out:
3338 return objp;
3339}
3340#else
3341
3342static __always_inline void *
3343__do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
3344{
3345 return ____cache_alloc(cachep, flags);
3346}
3347
3348#endif /* CONFIG_NUMA */
3349
3350static __always_inline void *
3351__cache_alloc(struct kmem_cache *cachep, gfp_t flags, void *caller)
3352{
3353 unsigned long save_flags;
3354 void *objp;
3355
Nick Piggincf40bd12009-01-21 08:12:39 +01003356 lockdep_trace_alloc(flags);
3357
Akinobu Mita773ff602008-12-23 19:37:01 +09003358 if (slab_should_failslab(cachep, flags))
Akinobu Mita824ebef2007-05-06 14:49:58 -07003359 return NULL;
3360
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003361 cache_alloc_debugcheck_before(cachep, flags);
3362 local_irq_save(save_flags);
3363 objp = __do_cache_alloc(cachep, flags);
3364 local_irq_restore(save_flags);
3365 objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
Catalin Marinasd5cff632009-06-11 13:22:40 +01003366 kmemleak_alloc_recursive(objp, obj_size(cachep), 1, cachep->flags,
3367 flags);
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003368 prefetchw(objp);
3369
Christoph Lameterd07dbea2007-07-17 04:03:23 -07003370 if (unlikely((flags & __GFP_ZERO) && objp))
3371 memset(objp, 0, obj_size(cachep));
3372
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003373 return objp;
3374}
Christoph Lametere498be72005-09-09 13:03:32 -07003375
3376/*
3377 * Caller needs to acquire correct kmem_list's list_lock
3378 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003379static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003380 int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381{
3382 int i;
Christoph Lametere498be72005-09-09 13:03:32 -07003383 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
3385 for (i = 0; i < nr_objects; i++) {
3386 void *objp = objpp[i];
3387 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
Pekka Enberg6ed5eb22006-02-01 03:05:49 -08003389 slabp = virt_to_slab(objp);
Christoph Lameterff694162005-09-22 21:44:02 -07003390 l3 = cachep->nodelists[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 list_del(&slabp->list);
Christoph Lameterff694162005-09-22 21:44:02 -07003392 check_spinlock_acquired_node(cachep, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 check_slabp(cachep, slabp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08003394 slab_put_obj(cachep, slabp, objp, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 STATS_DEC_ACTIVE(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003396 l3->free_objects++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 check_slabp(cachep, slabp);
3398
3399 /* fixup slab chains */
3400 if (slabp->inuse == 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07003401 if (l3->free_objects > l3->free_limit) {
3402 l3->free_objects -= cachep->num;
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07003403 /* No need to drop any previously held
3404 * lock here, even if we have a off-slab slab
3405 * descriptor it is guaranteed to come from
3406 * a different cache, refer to comments before
3407 * alloc_slabmgmt.
3408 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003409 slab_destroy(cachep, slabp);
3410 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07003411 list_add(&slabp->list, &l3->slabs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 }
3413 } else {
3414 /* Unconditionally move a slab to the end of the
3415 * partial list on free - maximum time for the
3416 * other objects to be freed, too.
3417 */
Christoph Lametere498be72005-09-09 13:03:32 -07003418 list_add_tail(&slabp->list, &l3->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 }
3420 }
3421}
3422
Pekka Enberg343e0d72006-02-01 03:05:50 -08003423static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424{
3425 int batchcount;
Christoph Lametere498be72005-09-09 13:03:32 -07003426 struct kmem_list3 *l3;
Christoph Lameterff694162005-09-22 21:44:02 -07003427 int node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
3429 batchcount = ac->batchcount;
3430#if DEBUG
3431 BUG_ON(!batchcount || batchcount > ac->avail);
3432#endif
3433 check_irq_off();
Christoph Lameterff694162005-09-22 21:44:02 -07003434 l3 = cachep->nodelists[node];
Ingo Molnar873623d2006-07-13 14:44:38 +02003435 spin_lock(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07003436 if (l3->shared) {
3437 struct array_cache *shared_array = l3->shared;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003438 int max = shared_array->limit - shared_array->avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439 if (max) {
3440 if (batchcount > max)
3441 batchcount = max;
Christoph Lametere498be72005-09-09 13:03:32 -07003442 memcpy(&(shared_array->entry[shared_array->avail]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003443 ac->entry, sizeof(void *) * batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 shared_array->avail += batchcount;
3445 goto free_done;
3446 }
3447 }
3448
Christoph Lameterff694162005-09-22 21:44:02 -07003449 free_block(cachep, ac->entry, batchcount, node);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003450free_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451#if STATS
3452 {
3453 int i = 0;
3454 struct list_head *p;
3455
Christoph Lametere498be72005-09-09 13:03:32 -07003456 p = l3->slabs_free.next;
3457 while (p != &(l3->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 struct slab *slabp;
3459
3460 slabp = list_entry(p, struct slab, list);
3461 BUG_ON(slabp->inuse);
3462
3463 i++;
3464 p = p->next;
3465 }
3466 STATS_SET_FREEABLE(cachep, i);
3467 }
3468#endif
Christoph Lametere498be72005-09-09 13:03:32 -07003469 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470 ac->avail -= batchcount;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003471 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472}
3473
3474/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08003475 * Release an obj back to its cache. If the obj has a constructed state, it must
3476 * be in this state _before_ it is released. Called with disabled ints.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 */
Ingo Molnar873623d2006-07-13 14:44:38 +02003478static inline void __cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479{
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003480 struct array_cache *ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481
3482 check_irq_off();
Catalin Marinasd5cff632009-06-11 13:22:40 +01003483 kmemleak_free_recursive(objp, cachep->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));
3485
Siddha, Suresh B1807a1a2007-08-22 14:01:49 -07003486 /*
3487 * Skip calling cache_free_alien() when the platform is not numa.
3488 * This will avoid cache misses that happen while accessing slabp (which
3489 * is per page memory reference) to get nodeid. Instead use a global
3490 * variable to skip the call, which is mostly likely to be present in
3491 * the cache.
3492 */
3493 if (numa_platform && cache_free_alien(cachep, objp))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07003494 return;
Christoph Lametere498be72005-09-09 13:03:32 -07003495
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 if (likely(ac->avail < ac->limit)) {
3497 STATS_INC_FREEHIT(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003498 ac->entry[ac->avail++] = objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 return;
3500 } else {
3501 STATS_INC_FREEMISS(cachep);
3502 cache_flusharray(cachep, ac);
Christoph Lametere498be72005-09-09 13:03:32 -07003503 ac->entry[ac->avail++] = objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504 }
3505}
3506
3507/**
3508 * kmem_cache_alloc - Allocate an object
3509 * @cachep: The cache to allocate from.
3510 * @flags: See kmalloc().
3511 *
3512 * Allocate an object from this cache. The flags are only relevant
3513 * if the cache has no available objects.
3514 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003515void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516{
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003517 void *ret = __cache_alloc(cachep, flags, __builtin_return_address(0));
3518
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003519 trace_kmem_cache_alloc(_RET_IP_, ret,
3520 obj_size(cachep), cachep->buffer_size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003521
3522 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523}
3524EXPORT_SYMBOL(kmem_cache_alloc);
3525
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003526#ifdef CONFIG_KMEMTRACE
3527void *kmem_cache_alloc_notrace(struct kmem_cache *cachep, gfp_t flags)
3528{
3529 return __cache_alloc(cachep, flags, __builtin_return_address(0));
3530}
3531EXPORT_SYMBOL(kmem_cache_alloc_notrace);
3532#endif
3533
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534/**
Randy Dunlap76824862008-03-19 17:00:40 -07003535 * kmem_ptr_validate - check if an untrusted pointer might be a slab entry.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003536 * @cachep: the cache we're checking against
3537 * @ptr: pointer to validate
3538 *
Randy Dunlap76824862008-03-19 17:00:40 -07003539 * This verifies that the untrusted pointer looks sane;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 * it is _not_ a guarantee that the pointer is actually
3541 * part of the slab cache in question, but it at least
3542 * validates that the pointer can be dereferenced and
3543 * looks half-way sane.
3544 *
3545 * Currently only used for dentry validation.
3546 */
Christoph Lameterb7f869a2006-12-22 01:06:44 -08003547int kmem_ptr_validate(struct kmem_cache *cachep, const void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003549 unsigned long addr = (unsigned long)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 unsigned long min_addr = PAGE_OFFSET;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003551 unsigned long align_mask = BYTES_PER_WORD - 1;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003552 unsigned long size = cachep->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 struct page *page;
3554
3555 if (unlikely(addr < min_addr))
3556 goto out;
3557 if (unlikely(addr > (unsigned long)high_memory - size))
3558 goto out;
3559 if (unlikely(addr & align_mask))
3560 goto out;
3561 if (unlikely(!kern_addr_valid(addr)))
3562 goto out;
3563 if (unlikely(!kern_addr_valid(addr + size - 1)))
3564 goto out;
3565 page = virt_to_page(ptr);
3566 if (unlikely(!PageSlab(page)))
3567 goto out;
Pekka Enberg065d41c2005-11-13 16:06:46 -08003568 if (unlikely(page_get_cache(page) != cachep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 goto out;
3570 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003571out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003572 return 0;
3573}
3574
3575#ifdef CONFIG_NUMA
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003576void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
3577{
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003578 void *ret = __cache_alloc_node(cachep, flags, nodeid,
3579 __builtin_return_address(0));
3580
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003581 trace_kmem_cache_alloc_node(_RET_IP_, ret,
3582 obj_size(cachep), cachep->buffer_size,
3583 flags, nodeid);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003584
3585 return ret;
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003586}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587EXPORT_SYMBOL(kmem_cache_alloc_node);
3588
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003589#ifdef CONFIG_KMEMTRACE
3590void *kmem_cache_alloc_node_notrace(struct kmem_cache *cachep,
3591 gfp_t flags,
3592 int nodeid)
3593{
3594 return __cache_alloc_node(cachep, flags, nodeid,
3595 __builtin_return_address(0));
3596}
3597EXPORT_SYMBOL(kmem_cache_alloc_node_notrace);
3598#endif
3599
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003600static __always_inline void *
3601__do_kmalloc_node(size_t size, gfp_t flags, int node, void *caller)
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003602{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003603 struct kmem_cache *cachep;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003604 void *ret;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003605
3606 cachep = kmem_find_general_cachep(size, flags);
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003607 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3608 return cachep;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003609 ret = kmem_cache_alloc_node_notrace(cachep, flags, node);
3610
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003611 trace_kmalloc_node((unsigned long) caller, ret,
3612 size, cachep->buffer_size, flags, node);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003613
3614 return ret;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003615}
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003616
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003617#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_KMEMTRACE)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003618void *__kmalloc_node(size_t size, gfp_t flags, int node)
3619{
3620 return __do_kmalloc_node(size, flags, node,
3621 __builtin_return_address(0));
3622}
Christoph Hellwigdbe5e692006-09-25 23:31:36 -07003623EXPORT_SYMBOL(__kmalloc_node);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003624
3625void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003626 int node, unsigned long caller)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003627{
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003628 return __do_kmalloc_node(size, flags, node, (void *)caller);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003629}
3630EXPORT_SYMBOL(__kmalloc_node_track_caller);
3631#else
3632void *__kmalloc_node(size_t size, gfp_t flags, int node)
3633{
3634 return __do_kmalloc_node(size, flags, node, NULL);
3635}
3636EXPORT_SYMBOL(__kmalloc_node);
3637#endif /* CONFIG_DEBUG_SLAB */
3638#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639
3640/**
Paul Drynoff800590f2006-06-23 02:03:48 -07003641 * __do_kmalloc - allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642 * @size: how many bytes of memory are required.
Paul Drynoff800590f2006-06-23 02:03:48 -07003643 * @flags: the type of memory to allocate (see kmalloc).
Randy Dunlap911851e2006-03-22 00:08:14 -08003644 * @caller: function caller for debug tracking of the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 */
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003646static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
3647 void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003649 struct kmem_cache *cachep;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003650 void *ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003652 /* If you want to save a few bytes .text space: replace
3653 * __ with kmem_.
3654 * Then kmalloc uses the uninlined functions instead of the inline
3655 * functions.
3656 */
3657 cachep = __find_general_cachep(size, flags);
Linus Torvaldsa5c96d82007-07-19 13:17:15 -07003658 if (unlikely(ZERO_OR_NULL_PTR(cachep)))
3659 return cachep;
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003660 ret = __cache_alloc(cachep, flags, caller);
3661
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003662 trace_kmalloc((unsigned long) caller, ret,
3663 size, cachep->buffer_size, flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003664
3665 return ret;
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003666}
3667
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003668
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003669#if defined(CONFIG_DEBUG_SLAB) || defined(CONFIG_KMEMTRACE)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003670void *__kmalloc(size_t size, gfp_t flags)
3671{
Al Viro871751e2006-03-25 03:06:39 -08003672 return __do_kmalloc(size, flags, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673}
3674EXPORT_SYMBOL(__kmalloc);
3675
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003676void *__kmalloc_track_caller(size_t size, gfp_t flags, unsigned long caller)
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003677{
Eduard - Gabriel Munteanuce71e272008-08-19 20:43:25 +03003678 return __do_kmalloc(size, flags, (void *)caller);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003679}
3680EXPORT_SYMBOL(__kmalloc_track_caller);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003681
3682#else
3683void *__kmalloc(size_t size, gfp_t flags)
3684{
3685 return __do_kmalloc(size, flags, NULL);
3686}
3687EXPORT_SYMBOL(__kmalloc);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003688#endif
3689
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690/**
3691 * kmem_cache_free - Deallocate an object
3692 * @cachep: The cache the allocation was from.
3693 * @objp: The previously allocated object.
3694 *
3695 * Free an object which was previously allocated from this
3696 * cache.
3697 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003698void kmem_cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699{
3700 unsigned long flags;
3701
3702 local_irq_save(flags);
Ingo Molnar898552c2007-02-10 01:44:57 -08003703 debug_check_no_locks_freed(objp, obj_size(cachep));
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07003704 if (!(cachep->flags & SLAB_DEBUG_OBJECTS))
3705 debug_check_no_obj_freed(objp, obj_size(cachep));
Ingo Molnar873623d2006-07-13 14:44:38 +02003706 __cache_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 local_irq_restore(flags);
Eduard - Gabriel Munteanu36555752008-08-10 20:14:05 +03003708
Eduard - Gabriel Munteanuca2b84c2009-03-23 15:12:24 +02003709 trace_kmem_cache_free(_RET_IP_, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710}
3711EXPORT_SYMBOL(kmem_cache_free);
3712
3713/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 * kfree - free previously allocated memory
3715 * @objp: pointer returned by kmalloc.
3716 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07003717 * If @objp is NULL, no operation is performed.
3718 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 * Don't free memory not originally allocated by kmalloc()
3720 * or you will run into trouble.
3721 */
3722void kfree(const void *objp)
3723{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003724 struct kmem_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 unsigned long flags;
3726
Pekka Enberg2121db72009-03-25 11:05:57 +02003727 trace_kfree(_RET_IP_, objp);
3728
Christoph Lameter6cb8f912007-07-17 04:03:22 -07003729 if (unlikely(ZERO_OR_NULL_PTR(objp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 return;
3731 local_irq_save(flags);
3732 kfree_debugcheck(objp);
Pekka Enberg6ed5eb22006-02-01 03:05:49 -08003733 c = virt_to_cache(objp);
Ingo Molnarf9b84042006-06-27 02:54:49 -07003734 debug_check_no_locks_freed(objp, obj_size(c));
Thomas Gleixner3ac7fe52008-04-30 00:55:01 -07003735 debug_check_no_obj_freed(objp, obj_size(c));
Ingo Molnar873623d2006-07-13 14:44:38 +02003736 __cache_free(c, (void *)objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737 local_irq_restore(flags);
3738}
3739EXPORT_SYMBOL(kfree);
3740
Pekka Enberg343e0d72006-02-01 03:05:50 -08003741unsigned int kmem_cache_size(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003743 return obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744}
3745EXPORT_SYMBOL(kmem_cache_size);
3746
Pekka Enberg343e0d72006-02-01 03:05:50 -08003747const char *kmem_cache_name(struct kmem_cache *cachep)
Arnaldo Carvalho de Melo19449722005-06-18 22:46:19 -07003748{
3749 return cachep->name;
3750}
3751EXPORT_SYMBOL_GPL(kmem_cache_name);
3752
Christoph Lametere498be72005-09-09 13:03:32 -07003753/*
Simon Arlott183ff222007-10-20 01:27:18 +02003754 * This initializes kmem_list3 or resizes various caches for all nodes.
Christoph Lametere498be72005-09-09 13:03:32 -07003755 */
Pekka Enberg83b519e2009-06-10 19:40:04 +03003756static int alloc_kmemlist(struct kmem_cache *cachep, gfp_t gfp)
Christoph Lametere498be72005-09-09 13:03:32 -07003757{
3758 int node;
3759 struct kmem_list3 *l3;
Christoph Lametercafeb022006-03-25 03:06:46 -08003760 struct array_cache *new_shared;
Paul Menage3395ee02006-12-06 20:32:16 -08003761 struct array_cache **new_alien = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003762
Mel Gorman9c09a952008-01-24 05:49:54 -08003763 for_each_online_node(node) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003764
Paul Menage3395ee02006-12-06 20:32:16 -08003765 if (use_alien_caches) {
Pekka Enberg83b519e2009-06-10 19:40:04 +03003766 new_alien = alloc_alien_cache(node, cachep->limit, gfp);
Paul Menage3395ee02006-12-06 20:32:16 -08003767 if (!new_alien)
3768 goto fail;
3769 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003770
Eric Dumazet63109842007-05-06 14:49:28 -07003771 new_shared = NULL;
3772 if (cachep->shared) {
3773 new_shared = alloc_arraycache(node,
Christoph Lameter0718dc22006-03-25 03:06:47 -08003774 cachep->shared*cachep->batchcount,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003775 0xbaadf00d, gfp);
Eric Dumazet63109842007-05-06 14:49:28 -07003776 if (!new_shared) {
3777 free_alien_cache(new_alien);
3778 goto fail;
3779 }
Christoph Lameter0718dc22006-03-25 03:06:47 -08003780 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003781
Andrew Mortona737b3e2006-03-22 00:08:11 -08003782 l3 = cachep->nodelists[node];
3783 if (l3) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003784 struct array_cache *shared = l3->shared;
3785
Christoph Lametere498be72005-09-09 13:03:32 -07003786 spin_lock_irq(&l3->list_lock);
3787
Christoph Lametercafeb022006-03-25 03:06:46 -08003788 if (shared)
Christoph Lameter0718dc22006-03-25 03:06:47 -08003789 free_block(cachep, shared->entry,
3790 shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07003791
Christoph Lametercafeb022006-03-25 03:06:46 -08003792 l3->shared = new_shared;
3793 if (!l3->alien) {
Christoph Lametere498be72005-09-09 13:03:32 -07003794 l3->alien = new_alien;
3795 new_alien = NULL;
3796 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003797 l3->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003798 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07003799 spin_unlock_irq(&l3->list_lock);
Christoph Lametercafeb022006-03-25 03:06:46 -08003800 kfree(shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003801 free_alien_cache(new_alien);
3802 continue;
3803 }
Pekka Enberg83b519e2009-06-10 19:40:04 +03003804 l3 = kmalloc_node(sizeof(struct kmem_list3), gfp, node);
Christoph Lameter0718dc22006-03-25 03:06:47 -08003805 if (!l3) {
3806 free_alien_cache(new_alien);
3807 kfree(new_shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003808 goto fail;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003809 }
Christoph Lametere498be72005-09-09 13:03:32 -07003810
3811 kmem_list3_init(l3);
3812 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
Andrew Mortona737b3e2006-03-22 00:08:11 -08003813 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lametercafeb022006-03-25 03:06:46 -08003814 l3->shared = new_shared;
Christoph Lametere498be72005-09-09 13:03:32 -07003815 l3->alien = new_alien;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003816 l3->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003817 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07003818 cachep->nodelists[node] = l3;
3819 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003820 return 0;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003821
Andrew Mortona737b3e2006-03-22 00:08:11 -08003822fail:
Christoph Lameter0718dc22006-03-25 03:06:47 -08003823 if (!cachep->next.next) {
3824 /* Cache is not active yet. Roll back what we did */
3825 node--;
3826 while (node >= 0) {
3827 if (cachep->nodelists[node]) {
3828 l3 = cachep->nodelists[node];
3829
3830 kfree(l3->shared);
3831 free_alien_cache(l3->alien);
3832 kfree(l3);
3833 cachep->nodelists[node] = NULL;
3834 }
3835 node--;
3836 }
3837 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003838 return -ENOMEM;
Christoph Lametere498be72005-09-09 13:03:32 -07003839}
3840
Linus Torvalds1da177e2005-04-16 15:20:36 -07003841struct ccupdate_struct {
Pekka Enberg343e0d72006-02-01 03:05:50 -08003842 struct kmem_cache *cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843 struct array_cache *new[NR_CPUS];
3844};
3845
3846static void do_ccupdate_local(void *info)
3847{
Andrew Mortona737b3e2006-03-22 00:08:11 -08003848 struct ccupdate_struct *new = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 struct array_cache *old;
3850
3851 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003852 old = cpu_cache_get(new->cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003853
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854 new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
3855 new->new[smp_processor_id()] = old;
3856}
3857
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -08003858/* Always called with the cache_chain_mutex held */
Andrew Mortona737b3e2006-03-22 00:08:11 -08003859static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003860 int batchcount, int shared, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003861{
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003862 struct ccupdate_struct *new;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003863 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864
Pekka Enberg83b519e2009-06-10 19:40:04 +03003865 new = kzalloc(sizeof(*new), gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003866 if (!new)
3867 return -ENOMEM;
3868
Christoph Lametere498be72005-09-09 13:03:32 -07003869 for_each_online_cpu(i) {
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003870 new->new[i] = alloc_arraycache(cpu_to_node(i), limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03003871 batchcount, gfp);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003872 if (!new->new[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003873 for (i--; i >= 0; i--)
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003874 kfree(new->new[i]);
3875 kfree(new);
Christoph Lametere498be72005-09-09 13:03:32 -07003876 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877 }
3878 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003879 new->cachep = cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880
Jens Axboe15c8b6c2008-05-09 09:39:44 +02003881 on_each_cpu(do_ccupdate_local, (void *)new, 1);
Christoph Lametere498be72005-09-09 13:03:32 -07003882
Linus Torvalds1da177e2005-04-16 15:20:36 -07003883 check_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 cachep->batchcount = batchcount;
3885 cachep->limit = limit;
Christoph Lametere498be72005-09-09 13:03:32 -07003886 cachep->shared = shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003887
Christoph Lametere498be72005-09-09 13:03:32 -07003888 for_each_online_cpu(i) {
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003889 struct array_cache *ccold = new->new[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 if (!ccold)
3891 continue;
Christoph Lametere498be72005-09-09 13:03:32 -07003892 spin_lock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
Christoph Lameterff694162005-09-22 21:44:02 -07003893 free_block(cachep, ccold->entry, ccold->avail, cpu_to_node(i));
Christoph Lametere498be72005-09-09 13:03:32 -07003894 spin_unlock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895 kfree(ccold);
3896 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003897 kfree(new);
Pekka Enberg83b519e2009-06-10 19:40:04 +03003898 return alloc_kmemlist(cachep, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899}
3900
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -08003901/* Called with cache_chain_mutex held always */
Pekka Enberg83b519e2009-06-10 19:40:04 +03003902static int enable_cpucache(struct kmem_cache *cachep, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003903{
3904 int err;
3905 int limit, shared;
3906
Andrew Mortona737b3e2006-03-22 00:08:11 -08003907 /*
3908 * The head array serves three purposes:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 * - create a LIFO ordering, i.e. return objects that are cache-warm
3910 * - reduce the number of spinlock operations.
Andrew Mortona737b3e2006-03-22 00:08:11 -08003911 * - reduce the number of linked list operations on the slab and
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 * bufctl chains: array operations are cheaper.
3913 * The numbers are guessed, we should auto-tune as described by
3914 * Bonwick.
3915 */
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003916 if (cachep->buffer_size > 131072)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917 limit = 1;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003918 else if (cachep->buffer_size > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 limit = 8;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003920 else if (cachep->buffer_size > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 limit = 24;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003922 else if (cachep->buffer_size > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923 limit = 54;
3924 else
3925 limit = 120;
3926
Andrew Mortona737b3e2006-03-22 00:08:11 -08003927 /*
3928 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 * allocation behaviour: Most allocs on one cpu, most free operations
3930 * on another cpu. For these cases, an efficient object passing between
3931 * cpus is necessary. This is provided by a shared array. The array
3932 * replaces Bonwick's magazine layer.
3933 * On uniprocessor, it's functionally equivalent (but less efficient)
3934 * to a larger limit. Thus disabled by default.
3935 */
3936 shared = 0;
Eric Dumazet364fbb22007-05-06 14:49:27 -07003937 if (cachep->buffer_size <= PAGE_SIZE && num_possible_cpus() > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 shared = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939
3940#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003941 /*
3942 * With debugging enabled, large batchcount lead to excessively long
3943 * periods with disabled local interrupts. Limit the batchcount
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944 */
3945 if (limit > 32)
3946 limit = 32;
3947#endif
Pekka Enberg83b519e2009-06-10 19:40:04 +03003948 err = do_tune_cpucache(cachep, limit, (limit + 1) / 2, shared, gfp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 if (err)
3950 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003951 cachep->name, -err);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003952 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953}
3954
Christoph Lameter1b552532006-03-22 00:09:07 -08003955/*
3956 * Drain an array if it contains any elements taking the l3 lock only if
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003957 * necessary. Note that the l3 listlock also protects the array_cache
3958 * if drain_array() is used on the shared array.
Christoph Lameter1b552532006-03-22 00:09:07 -08003959 */
3960void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
3961 struct array_cache *ac, int force, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962{
3963 int tofree;
3964
Christoph Lameter1b552532006-03-22 00:09:07 -08003965 if (!ac || !ac->avail)
3966 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003967 if (ac->touched && !force) {
3968 ac->touched = 0;
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003969 } else {
Christoph Lameter1b552532006-03-22 00:09:07 -08003970 spin_lock_irq(&l3->list_lock);
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003971 if (ac->avail) {
3972 tofree = force ? ac->avail : (ac->limit + 4) / 5;
3973 if (tofree > ac->avail)
3974 tofree = (ac->avail + 1) / 2;
3975 free_block(cachep, ac->entry, tofree, node);
3976 ac->avail -= tofree;
3977 memmove(ac->entry, &(ac->entry[tofree]),
3978 sizeof(void *) * ac->avail);
3979 }
Christoph Lameter1b552532006-03-22 00:09:07 -08003980 spin_unlock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981 }
3982}
3983
3984/**
3985 * cache_reap - Reclaim memory from caches.
Randy Dunlap05fb6bf2007-02-28 20:12:13 -08003986 * @w: work descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07003987 *
3988 * Called from workqueue/eventd every few seconds.
3989 * Purpose:
3990 * - clear the per-cpu caches for this CPU.
3991 * - return freeable pages to the main free memory pool.
3992 *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003993 * If we cannot acquire the cache chain mutex then just give up - we'll try
3994 * again on the next iteration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08003996static void cache_reap(struct work_struct *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997{
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07003998 struct kmem_cache *searchp;
Christoph Lametere498be72005-09-09 13:03:32 -07003999 struct kmem_list3 *l3;
Christoph Lameteraab22072006-03-22 00:09:06 -08004000 int node = numa_node_id();
Jean Delvarebf6aede2009-04-02 16:56:54 -07004001 struct delayed_work *work = to_delayed_work(w);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004003 if (!mutex_trylock(&cache_chain_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 /* Give up. Setup the next iteration. */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004005 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004007 list_for_each_entry(searchp, &cache_chain, next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 check_irq_on();
4009
Christoph Lameter35386e32006-03-22 00:09:05 -08004010 /*
4011 * We only take the l3 lock if absolutely necessary and we
4012 * have established with reasonable certainty that
4013 * we can do some work if the lock was obtained.
4014 */
Christoph Lameteraab22072006-03-22 00:09:06 -08004015 l3 = searchp->nodelists[node];
Christoph Lameter35386e32006-03-22 00:09:05 -08004016
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004017 reap_alien(searchp, l3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018
Christoph Lameteraab22072006-03-22 00:09:06 -08004019 drain_array(searchp, l3, cpu_cache_get(searchp), 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
Christoph Lameter35386e32006-03-22 00:09:05 -08004021 /*
4022 * These are racy checks but it does not matter
4023 * if we skip one check or scan twice.
4024 */
Christoph Lametere498be72005-09-09 13:03:32 -07004025 if (time_after(l3->next_reap, jiffies))
Christoph Lameter35386e32006-03-22 00:09:05 -08004026 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
Christoph Lametere498be72005-09-09 13:03:32 -07004028 l3->next_reap = jiffies + REAPTIMEOUT_LIST3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029
Christoph Lameteraab22072006-03-22 00:09:06 -08004030 drain_array(searchp, l3, l3->shared, 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
Christoph Lametered11d9e2006-06-30 01:55:45 -07004032 if (l3->free_touched)
Christoph Lametere498be72005-09-09 13:03:32 -07004033 l3->free_touched = 0;
Christoph Lametered11d9e2006-06-30 01:55:45 -07004034 else {
4035 int freed;
4036
4037 freed = drain_freelist(searchp, l3, (l3->free_limit +
4038 5 * searchp->num - 1) / (5 * searchp->num));
4039 STATS_ADD_REAPED(searchp, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040 }
Christoph Lameter35386e32006-03-22 00:09:05 -08004041next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 cond_resched();
4043 }
4044 check_irq_on();
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004045 mutex_unlock(&cache_chain_mutex);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004046 next_reap_node();
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004047out:
Andrew Mortona737b3e2006-03-22 00:08:11 -08004048 /* Set up the next iteration */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004049 schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_CPUC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050}
4051
Linus Torvalds158a9622008-01-02 13:04:48 -08004052#ifdef CONFIG_SLABINFO
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053
Pekka Enberg85289f92006-01-08 01:00:36 -08004054static void print_slabinfo_header(struct seq_file *m)
4055{
4056 /*
4057 * Output format version, so at least we can change it
4058 * without _too_ many complaints.
4059 */
4060#if STATS
4061 seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
4062#else
4063 seq_puts(m, "slabinfo - version: 2.1\n");
4064#endif
4065 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
4066 "<objperslab> <pagesperslab>");
4067 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
4068 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
4069#if STATS
4070 seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004071 "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
Pekka Enberg85289f92006-01-08 01:00:36 -08004072 seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
4073#endif
4074 seq_putc(m, '\n');
4075}
4076
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077static void *s_start(struct seq_file *m, loff_t *pos)
4078{
4079 loff_t n = *pos;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004081 mutex_lock(&cache_chain_mutex);
Pekka Enberg85289f92006-01-08 01:00:36 -08004082 if (!n)
4083 print_slabinfo_header(m);
Pavel Emelianovb92151b2007-07-15 23:38:04 -07004084
4085 return seq_list_start(&cache_chain, *pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086}
4087
4088static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4089{
Pavel Emelianovb92151b2007-07-15 23:38:04 -07004090 return seq_list_next(p, &cache_chain, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091}
4092
4093static void s_stop(struct seq_file *m, void *p)
4094{
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004095 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096}
4097
4098static int s_show(struct seq_file *m, void *p)
4099{
Pavel Emelianovb92151b2007-07-15 23:38:04 -07004100 struct kmem_cache *cachep = list_entry(p, struct kmem_cache, next);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004101 struct slab *slabp;
4102 unsigned long active_objs;
4103 unsigned long num_objs;
4104 unsigned long active_slabs = 0;
4105 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004106 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004107 char *error = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07004108 int node;
4109 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 active_objs = 0;
4112 num_slabs = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004113 for_each_online_node(node) {
4114 l3 = cachep->nodelists[node];
4115 if (!l3)
4116 continue;
4117
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004118 check_irq_on();
4119 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07004120
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004121 list_for_each_entry(slabp, &l3->slabs_full, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004122 if (slabp->inuse != cachep->num && !error)
4123 error = "slabs_full accounting error";
4124 active_objs += cachep->num;
4125 active_slabs++;
4126 }
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004127 list_for_each_entry(slabp, &l3->slabs_partial, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004128 if (slabp->inuse == cachep->num && !error)
4129 error = "slabs_partial inuse accounting error";
4130 if (!slabp->inuse && !error)
4131 error = "slabs_partial/inuse accounting error";
4132 active_objs += slabp->inuse;
4133 active_slabs++;
4134 }
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004135 list_for_each_entry(slabp, &l3->slabs_free, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004136 if (slabp->inuse && !error)
4137 error = "slabs_free/inuse accounting error";
4138 num_slabs++;
4139 }
4140 free_objects += l3->free_objects;
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08004141 if (l3->shared)
4142 shared_avail += l3->shared->avail;
Christoph Lametere498be72005-09-09 13:03:32 -07004143
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004144 spin_unlock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004146 num_slabs += active_slabs;
4147 num_objs = num_slabs * cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07004148 if (num_objs - active_objs != free_objects && !error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 error = "free_objects accounting error";
4150
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004151 name = cachep->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 if (error)
4153 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
4154
4155 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
Manfred Spraul3dafccf2006-02-01 03:05:42 -08004156 name, active_objs, num_objs, cachep->buffer_size,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004157 cachep->num, (1 << cachep->gfporder));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 seq_printf(m, " : tunables %4u %4u %4u",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004159 cachep->limit, cachep->batchcount, cachep->shared);
Christoph Lametere498be72005-09-09 13:03:32 -07004160 seq_printf(m, " : slabdata %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004161 active_slabs, num_slabs, shared_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162#if STATS
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004163 { /* list3 stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 unsigned long high = cachep->high_mark;
4165 unsigned long allocs = cachep->num_allocations;
4166 unsigned long grown = cachep->grown;
4167 unsigned long reaped = cachep->reaped;
4168 unsigned long errors = cachep->errors;
4169 unsigned long max_freeable = cachep->max_freeable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 unsigned long node_allocs = cachep->node_allocs;
Christoph Lametere498be72005-09-09 13:03:32 -07004171 unsigned long node_frees = cachep->node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004172 unsigned long overflows = cachep->node_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173
Christoph Lametere498be72005-09-09 13:03:32 -07004174 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu \
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004175 %4lu %4lu %4lu %4lu %4lu", allocs, high, grown,
Andrew Mortona737b3e2006-03-22 00:08:11 -08004176 reaped, errors, max_freeable, node_allocs,
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004177 node_frees, overflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 }
4179 /* cpu stats */
4180 {
4181 unsigned long allochit = atomic_read(&cachep->allochit);
4182 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
4183 unsigned long freehit = atomic_read(&cachep->freehit);
4184 unsigned long freemiss = atomic_read(&cachep->freemiss);
4185
4186 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004187 allochit, allocmiss, freehit, freemiss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 }
4189#endif
4190 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 return 0;
4192}
4193
4194/*
4195 * slabinfo_op - iterator that generates /proc/slabinfo
4196 *
4197 * Output layout:
4198 * cache-name
4199 * num-active-objs
4200 * total-objs
4201 * object size
4202 * num-active-slabs
4203 * total-slabs
4204 * num-pages-per-slab
4205 * + further values on SMP and with statistics enabled
4206 */
4207
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04004208static const struct seq_operations slabinfo_op = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004209 .start = s_start,
4210 .next = s_next,
4211 .stop = s_stop,
4212 .show = s_show,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213};
4214
4215#define MAX_SLABINFO_WRITE 128
4216/**
4217 * slabinfo_write - Tuning for the slab allocator
4218 * @file: unused
4219 * @buffer: user buffer
4220 * @count: data length
4221 * @ppos: unused
4222 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004223ssize_t slabinfo_write(struct file *file, const char __user * buffer,
4224 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004226 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 int limit, batchcount, shared, res;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004228 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004229
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 if (count > MAX_SLABINFO_WRITE)
4231 return -EINVAL;
4232 if (copy_from_user(&kbuf, buffer, count))
4233 return -EFAULT;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004234 kbuf[MAX_SLABINFO_WRITE] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235
4236 tmp = strchr(kbuf, ' ');
4237 if (!tmp)
4238 return -EINVAL;
4239 *tmp = '\0';
4240 tmp++;
4241 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
4242 return -EINVAL;
4243
4244 /* Find the cache in the chain of caches. */
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004245 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246 res = -EINVAL;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004247 list_for_each_entry(cachep, &cache_chain, next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 if (!strcmp(cachep->name, kbuf)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08004249 if (limit < 1 || batchcount < 1 ||
4250 batchcount > limit || shared < 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07004251 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07004253 res = do_tune_cpucache(cachep, limit,
Pekka Enberg83b519e2009-06-10 19:40:04 +03004254 batchcount, shared,
4255 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 }
4257 break;
4258 }
4259 }
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004260 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 if (res >= 0)
4262 res = count;
4263 return res;
4264}
Al Viro871751e2006-03-25 03:06:39 -08004265
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04004266static int slabinfo_open(struct inode *inode, struct file *file)
4267{
4268 return seq_open(file, &slabinfo_op);
4269}
4270
4271static const struct file_operations proc_slabinfo_operations = {
4272 .open = slabinfo_open,
4273 .read = seq_read,
4274 .write = slabinfo_write,
4275 .llseek = seq_lseek,
4276 .release = seq_release,
4277};
4278
Al Viro871751e2006-03-25 03:06:39 -08004279#ifdef CONFIG_DEBUG_SLAB_LEAK
4280
4281static void *leaks_start(struct seq_file *m, loff_t *pos)
4282{
Al Viro871751e2006-03-25 03:06:39 -08004283 mutex_lock(&cache_chain_mutex);
Pavel Emelianovb92151b2007-07-15 23:38:04 -07004284 return seq_list_start(&cache_chain, *pos);
Al Viro871751e2006-03-25 03:06:39 -08004285}
4286
4287static inline int add_caller(unsigned long *n, unsigned long v)
4288{
4289 unsigned long *p;
4290 int l;
4291 if (!v)
4292 return 1;
4293 l = n[1];
4294 p = n + 2;
4295 while (l) {
4296 int i = l/2;
4297 unsigned long *q = p + 2 * i;
4298 if (*q == v) {
4299 q[1]++;
4300 return 1;
4301 }
4302 if (*q > v) {
4303 l = i;
4304 } else {
4305 p = q + 2;
4306 l -= i + 1;
4307 }
4308 }
4309 if (++n[1] == n[0])
4310 return 0;
4311 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4312 p[0] = v;
4313 p[1] = 1;
4314 return 1;
4315}
4316
4317static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
4318{
4319 void *p;
4320 int i;
4321 if (n[0] == n[1])
4322 return;
4323 for (i = 0, p = s->s_mem; i < c->num; i++, p += c->buffer_size) {
4324 if (slab_bufctl(s)[i] != BUFCTL_ACTIVE)
4325 continue;
4326 if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
4327 return;
4328 }
4329}
4330
4331static void show_symbol(struct seq_file *m, unsigned long address)
4332{
4333#ifdef CONFIG_KALLSYMS
Al Viro871751e2006-03-25 03:06:39 -08004334 unsigned long offset, size;
Tejun Heo9281ace2007-07-17 04:03:51 -07004335 char modname[MODULE_NAME_LEN], name[KSYM_NAME_LEN];
Al Viro871751e2006-03-25 03:06:39 -08004336
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004337 if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
Al Viro871751e2006-03-25 03:06:39 -08004338 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004339 if (modname[0])
Al Viro871751e2006-03-25 03:06:39 -08004340 seq_printf(m, " [%s]", modname);
4341 return;
4342 }
4343#endif
4344 seq_printf(m, "%p", (void *)address);
4345}
4346
4347static int leaks_show(struct seq_file *m, void *p)
4348{
Pavel Emelianovb92151b2007-07-15 23:38:04 -07004349 struct kmem_cache *cachep = list_entry(p, struct kmem_cache, next);
Al Viro871751e2006-03-25 03:06:39 -08004350 struct slab *slabp;
4351 struct kmem_list3 *l3;
4352 const char *name;
4353 unsigned long *n = m->private;
4354 int node;
4355 int i;
4356
4357 if (!(cachep->flags & SLAB_STORE_USER))
4358 return 0;
4359 if (!(cachep->flags & SLAB_RED_ZONE))
4360 return 0;
4361
4362 /* OK, we can do it */
4363
4364 n[1] = 0;
4365
4366 for_each_online_node(node) {
4367 l3 = cachep->nodelists[node];
4368 if (!l3)
4369 continue;
4370
4371 check_irq_on();
4372 spin_lock_irq(&l3->list_lock);
4373
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004374 list_for_each_entry(slabp, &l3->slabs_full, list)
Al Viro871751e2006-03-25 03:06:39 -08004375 handle_slab(n, cachep, slabp);
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004376 list_for_each_entry(slabp, &l3->slabs_partial, list)
Al Viro871751e2006-03-25 03:06:39 -08004377 handle_slab(n, cachep, slabp);
Al Viro871751e2006-03-25 03:06:39 -08004378 spin_unlock_irq(&l3->list_lock);
4379 }
4380 name = cachep->name;
4381 if (n[0] == n[1]) {
4382 /* Increase the buffer size */
4383 mutex_unlock(&cache_chain_mutex);
4384 m->private = kzalloc(n[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
4385 if (!m->private) {
4386 /* Too bad, we are really out */
4387 m->private = n;
4388 mutex_lock(&cache_chain_mutex);
4389 return -ENOMEM;
4390 }
4391 *(unsigned long *)m->private = n[0] * 2;
4392 kfree(n);
4393 mutex_lock(&cache_chain_mutex);
4394 /* Now make sure this entry will be retried */
4395 m->count = m->size;
4396 return 0;
4397 }
4398 for (i = 0; i < n[1]; i++) {
4399 seq_printf(m, "%s: %lu ", name, n[2*i+3]);
4400 show_symbol(m, n[2*i+2]);
4401 seq_putc(m, '\n');
4402 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004403
Al Viro871751e2006-03-25 03:06:39 -08004404 return 0;
4405}
4406
Alexey Dobriyana0ec95a82008-10-06 00:59:10 +04004407static const struct seq_operations slabstats_op = {
Al Viro871751e2006-03-25 03:06:39 -08004408 .start = leaks_start,
4409 .next = s_next,
4410 .stop = s_stop,
4411 .show = leaks_show,
4412};
Alexey Dobriyana0ec95a82008-10-06 00:59:10 +04004413
4414static int slabstats_open(struct inode *inode, struct file *file)
4415{
4416 unsigned long *n = kzalloc(PAGE_SIZE, GFP_KERNEL);
4417 int ret = -ENOMEM;
4418 if (n) {
4419 ret = seq_open(file, &slabstats_op);
4420 if (!ret) {
4421 struct seq_file *m = file->private_data;
4422 *n = PAGE_SIZE / (2 * sizeof(unsigned long));
4423 m->private = n;
4424 n = NULL;
4425 }
4426 kfree(n);
4427 }
4428 return ret;
4429}
4430
4431static const struct file_operations proc_slabstats_operations = {
4432 .open = slabstats_open,
4433 .read = seq_read,
4434 .llseek = seq_lseek,
4435 .release = seq_release_private,
4436};
Al Viro871751e2006-03-25 03:06:39 -08004437#endif
Alexey Dobriyana0ec95a82008-10-06 00:59:10 +04004438
4439static int __init slab_proc_init(void)
4440{
Alexey Dobriyan7b3c3a52008-10-06 02:42:17 +04004441 proc_create("slabinfo",S_IWUSR|S_IRUGO,NULL,&proc_slabinfo_operations);
Alexey Dobriyana0ec95a82008-10-06 00:59:10 +04004442#ifdef CONFIG_DEBUG_SLAB_LEAK
4443 proc_create("slab_allocators", 0, NULL, &proc_slabstats_operations);
4444#endif
4445 return 0;
4446}
4447module_init(slab_proc_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448#endif
4449
Manfred Spraul00e145b2005-09-03 15:55:07 -07004450/**
4451 * ksize - get the actual amount of memory allocated for a given object
4452 * @objp: Pointer to the object
4453 *
4454 * kmalloc may internally round up allocations and return more memory
4455 * than requested. ksize() can be used to determine the actual amount of
4456 * memory allocated. The caller may use this additional memory, even though
4457 * a smaller amount of memory was initially specified with the kmalloc call.
4458 * The caller must guarantee that objp points to a valid object previously
4459 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4460 * must not be freed during the duration of the call.
4461 */
Pekka Enbergfd76bab2007-05-06 14:48:40 -07004462size_t ksize(const void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463{
Christoph Lameteref8b4522007-10-16 01:24:46 -07004464 BUG_ON(!objp);
4465 if (unlikely(objp == ZERO_SIZE_PTR))
Manfred Spraul00e145b2005-09-03 15:55:07 -07004466 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467
Pekka Enberg6ed5eb22006-02-01 03:05:49 -08004468 return obj_size(virt_to_cache(objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469}
Kirill A. Shutemovb1aabec2009-02-10 15:21:44 +02004470EXPORT_SYMBOL(ksize);