blob: c7576b9027a6fbe53c754053890036f94c0e69fe [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
29 * slabs and you must pass objects with the same intializations to
30 * 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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070098#include <linux/seq_file.h>
99#include <linux/notifier.h>
100#include <linux/kallsyms.h>
101#include <linux/cpu.h>
102#include <linux/sysctl.h>
103#include <linux/module.h>
104#include <linux/rcupdate.h>
Paulo Marques543537b2005-06-23 00:09:02 -0700105#include <linux/string.h>
Andrew Morton138ae662006-12-06 20:36:41 -0800106#include <linux/uaccess.h>
Christoph Lametere498be72005-09-09 13:03:32 -0700107#include <linux/nodemask.h>
Christoph Lameterdc85da12006-01-18 17:42:36 -0800108#include <linux/mempolicy.h>
Ingo Molnarfc0abb12006-01-18 17:42:33 -0800109#include <linux/mutex.h>
Akinobu Mita8a8b6502006-12-08 02:39:44 -0800110#include <linux/fault-inject.h>
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700111#include <linux/rtmutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113#include <asm/cacheflush.h>
114#include <asm/tlbflush.h>
115#include <asm/page.h>
116
117/*
118 * DEBUG - 1 for kmem_cache_create() to honour; SLAB_DEBUG_INITIAL,
119 * SLAB_RED_ZONE & SLAB_POISON.
120 * 0 for faster, smaller code (especially in the critical paths).
121 *
122 * STATS - 1 to collect stats for /proc/slabinfo.
123 * 0 for faster, smaller code (especially in the critical paths).
124 *
125 * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
126 */
127
128#ifdef CONFIG_DEBUG_SLAB
129#define DEBUG 1
130#define STATS 1
131#define FORCED_DEBUG 1
132#else
133#define DEBUG 0
134#define STATS 0
135#define FORCED_DEBUG 0
136#endif
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/* Shouldn't this be in a header file somewhere? */
139#define BYTES_PER_WORD sizeof(void *)
140
141#ifndef cache_line_size
142#define cache_line_size() L1_CACHE_BYTES
143#endif
144
145#ifndef ARCH_KMALLOC_MINALIGN
146/*
147 * Enforce a minimum alignment for the kmalloc caches.
148 * Usually, the kmalloc caches are cache_line_size() aligned, except when
149 * DEBUG and FORCED_DEBUG are enabled, then they are BYTES_PER_WORD aligned.
150 * Some archs want to perform DMA into kmalloc caches and need a guaranteed
151 * alignment larger than BYTES_PER_WORD. ARCH_KMALLOC_MINALIGN allows that.
152 * Note that this flag disables some debug features.
153 */
154#define ARCH_KMALLOC_MINALIGN 0
155#endif
156
157#ifndef ARCH_SLAB_MINALIGN
158/*
159 * Enforce a minimum alignment for all caches.
160 * Intended for archs that get misalignment faults even for BYTES_PER_WORD
161 * aligned buffers. Includes ARCH_KMALLOC_MINALIGN.
162 * If possible: Do not enable this flag for CONFIG_DEBUG_SLAB, it disables
163 * some debug features.
164 */
165#define ARCH_SLAB_MINALIGN 0
166#endif
167
168#ifndef ARCH_KMALLOC_FLAGS
169#define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
170#endif
171
172/* Legal flag mask for kmem_cache_create(). */
173#if DEBUG
174# define CREATE_MASK (SLAB_DEBUG_INITIAL | SLAB_RED_ZONE | \
175 SLAB_POISON | SLAB_HWCACHE_ALIGN | \
Christoph Lameterac2b8982006-03-22 00:08:15 -0800176 SLAB_CACHE_DMA | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 SLAB_MUST_HWCACHE_ALIGN | SLAB_STORE_USER | \
178 SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
Paul Jackson101a5002006-03-24 03:16:07 -0800179 SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#else
Christoph Lameterac2b8982006-03-22 00:08:15 -0800181# define CREATE_MASK (SLAB_HWCACHE_ALIGN | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 SLAB_CACHE_DMA | SLAB_MUST_HWCACHE_ALIGN | \
183 SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
Paul Jackson101a5002006-03-24 03:16:07 -0800184 SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185#endif
186
187/*
188 * kmem_bufctl_t:
189 *
190 * Bufctl's are used for linking objs within a slab
191 * linked offsets.
192 *
193 * This implementation relies on "struct page" for locating the cache &
194 * slab an object belongs to.
195 * This allows the bufctl structure to be small (one int), but limits
196 * the number of objects a slab (not a cache) can contain when off-slab
197 * bufctls are used. The limit is the size of the largest general cache
198 * that does not use off-slab slabs.
199 * For 32bit archs with 4 kB pages, is this 56.
200 * This is not serious, as it is only for large objects, when it is unwise
201 * to have too many per slab.
202 * Note: This limit can be raised by introducing a general cache whose size
203 * is less than 512 (PAGE_SIZE<<3), but greater than 256.
204 */
205
Kyle Moffettfa5b08d2005-09-03 15:55:03 -0700206typedef unsigned int kmem_bufctl_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207#define BUFCTL_END (((kmem_bufctl_t)(~0U))-0)
208#define BUFCTL_FREE (((kmem_bufctl_t)(~0U))-1)
Al Viro871751e2006-03-25 03:06:39 -0800209#define BUFCTL_ACTIVE (((kmem_bufctl_t)(~0U))-2)
210#define SLAB_LIMIT (((kmem_bufctl_t)(~0U))-3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/*
213 * struct slab
214 *
215 * Manages the objs in a slab. Placed either at the beginning of mem allocated
216 * for a slab, or allocated from an general cache.
217 * Slabs are chained into three list: fully used, partial, fully free slabs.
218 */
219struct slab {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800220 struct list_head list;
221 unsigned long colouroff;
222 void *s_mem; /* including colour offset */
223 unsigned int inuse; /* num of objs active in slab */
224 kmem_bufctl_t free;
225 unsigned short nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226};
227
228/*
229 * struct slab_rcu
230 *
231 * slab_destroy on a SLAB_DESTROY_BY_RCU cache uses this structure to
232 * arrange for kmem_freepages to be called via RCU. This is useful if
233 * we need to approach a kernel structure obliquely, from its address
234 * obtained without the usual locking. We can lock the structure to
235 * stabilize it and check it's still at the given address, only if we
236 * can be sure that the memory has not been meanwhile reused for some
237 * other kind of object (which our subsystem's lock might corrupt).
238 *
239 * rcu_read_lock before reading the address, then rcu_read_unlock after
240 * taking the spinlock within the structure expected at that address.
241 *
242 * We assume struct slab_rcu can overlay struct slab when destroying.
243 */
244struct slab_rcu {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800245 struct rcu_head head;
Pekka Enberg343e0d72006-02-01 03:05:50 -0800246 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800247 void *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248};
249
250/*
251 * struct array_cache
252 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * Purpose:
254 * - LIFO ordering, to hand out cache-warm objects from _alloc
255 * - reduce the number of linked list operations
256 * - reduce spinlock operations
257 *
258 * The limit is stored in the per-cpu structure to reduce the data cache
259 * footprint.
260 *
261 */
262struct array_cache {
263 unsigned int avail;
264 unsigned int limit;
265 unsigned int batchcount;
266 unsigned int touched;
Christoph Lametere498be72005-09-09 13:03:32 -0700267 spinlock_t lock;
Andrew Mortona737b3e2006-03-22 00:08:11 -0800268 void *entry[0]; /*
269 * Must have this definition in here for the proper
270 * alignment of array_cache. Also simplifies accessing
271 * the entries.
272 * [0] is for gcc 2.95. It should really be [].
273 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274};
275
Andrew Mortona737b3e2006-03-22 00:08:11 -0800276/*
277 * bootstrap: The caches do not work without cpuarrays anymore, but the
278 * cpuarrays are allocated from the generic caches...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 */
280#define BOOT_CPUCACHE_ENTRIES 1
281struct arraycache_init {
282 struct array_cache cache;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800283 void *entries[BOOT_CPUCACHE_ENTRIES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284};
285
286/*
Christoph Lametere498be72005-09-09 13:03:32 -0700287 * The slab lists for all objects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
289struct kmem_list3 {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800290 struct list_head slabs_partial; /* partial list first, better asm code */
291 struct list_head slabs_full;
292 struct list_head slabs_free;
293 unsigned long free_objects;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800294 unsigned int free_limit;
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -0800295 unsigned int colour_next; /* Per-node cache coloring */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800296 spinlock_t list_lock;
297 struct array_cache *shared; /* shared per node */
298 struct array_cache **alien; /* on other nodes */
Christoph Lameter35386e32006-03-22 00:09:05 -0800299 unsigned long next_reap; /* updated without locking */
300 int free_touched; /* updated without locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301};
302
Christoph Lametere498be72005-09-09 13:03:32 -0700303/*
304 * Need this for bootstrapping a per node allocator.
305 */
306#define NUM_INIT_LISTS (2 * MAX_NUMNODES + 1)
307struct kmem_list3 __initdata initkmem_list3[NUM_INIT_LISTS];
308#define CACHE_CACHE 0
309#define SIZE_AC 1
310#define SIZE_L3 (1 + MAX_NUMNODES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Christoph Lametered11d9e2006-06-30 01:55:45 -0700312static int drain_freelist(struct kmem_cache *cache,
313 struct kmem_list3 *l3, int tofree);
314static void free_block(struct kmem_cache *cachep, void **objpp, int len,
315 int node);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -0700316static int enable_cpucache(struct kmem_cache *cachep);
David Howells65f27f32006-11-22 14:55:48 +0000317static void cache_reap(struct work_struct *unused);
Christoph Lametered11d9e2006-06-30 01:55:45 -0700318
Christoph Lametere498be72005-09-09 13:03:32 -0700319/*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800320 * This function must be completely optimized away if a constant is passed to
321 * it. Mostly the same as what is in linux/slab.h except it returns an index.
Christoph Lametere498be72005-09-09 13:03:32 -0700322 */
Ivan Kokshaysky7243cc02005-09-22 21:43:58 -0700323static __always_inline int index_of(const size_t size)
Christoph Lametere498be72005-09-09 13:03:32 -0700324{
Steven Rostedt5ec8a842006-02-01 03:05:44 -0800325 extern void __bad_size(void);
326
Christoph Lametere498be72005-09-09 13:03:32 -0700327 if (__builtin_constant_p(size)) {
328 int i = 0;
329
330#define CACHE(x) \
331 if (size <=x) \
332 return i; \
333 else \
334 i++;
335#include "linux/kmalloc_sizes.h"
336#undef CACHE
Steven Rostedt5ec8a842006-02-01 03:05:44 -0800337 __bad_size();
Ivan Kokshaysky7243cc02005-09-22 21:43:58 -0700338 } else
Steven Rostedt5ec8a842006-02-01 03:05:44 -0800339 __bad_size();
Christoph Lametere498be72005-09-09 13:03:32 -0700340 return 0;
341}
342
Ingo Molnare0a42722006-06-23 02:03:46 -0700343static int slab_early_init = 1;
344
Christoph Lametere498be72005-09-09 13:03:32 -0700345#define INDEX_AC index_of(sizeof(struct arraycache_init))
346#define INDEX_L3 index_of(sizeof(struct kmem_list3))
347
Pekka Enberg5295a742006-02-01 03:05:48 -0800348static void kmem_list3_init(struct kmem_list3 *parent)
Christoph Lametere498be72005-09-09 13:03:32 -0700349{
350 INIT_LIST_HEAD(&parent->slabs_full);
351 INIT_LIST_HEAD(&parent->slabs_partial);
352 INIT_LIST_HEAD(&parent->slabs_free);
353 parent->shared = NULL;
354 parent->alien = NULL;
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -0800355 parent->colour_next = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700356 spin_lock_init(&parent->list_lock);
357 parent->free_objects = 0;
358 parent->free_touched = 0;
359}
360
Andrew Mortona737b3e2006-03-22 00:08:11 -0800361#define MAKE_LIST(cachep, listp, slab, nodeid) \
362 do { \
363 INIT_LIST_HEAD(listp); \
364 list_splice(&(cachep->nodelists[nodeid]->slab), listp); \
Christoph Lametere498be72005-09-09 13:03:32 -0700365 } while (0)
366
Andrew Mortona737b3e2006-03-22 00:08:11 -0800367#define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
368 do { \
Christoph Lametere498be72005-09-09 13:03:32 -0700369 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
370 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
371 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
372 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374/*
Pekka Enberg343e0d72006-02-01 03:05:50 -0800375 * struct kmem_cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 *
377 * manages a cache.
378 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800379
Pekka J Enberg2109a2d2005-11-07 00:58:01 -0800380struct kmem_cache {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/* 1) per-cpu data, touched during every alloc/free */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800382 struct array_cache *array[NR_CPUS];
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800383/* 2) Cache tunables. Protected by cache_chain_mutex */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800384 unsigned int batchcount;
385 unsigned int limit;
386 unsigned int shared;
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800387
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800388 unsigned int buffer_size;
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800389/* 3) touched by every alloc & free from the backend */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800390 struct kmem_list3 *nodelists[MAX_NUMNODES];
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800391
Andrew Mortona737b3e2006-03-22 00:08:11 -0800392 unsigned int flags; /* constant flags */
393 unsigned int num; /* # of objs per slab */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800395/* 4) cache_grow/shrink */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /* order of pgs per slab (2^n) */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800397 unsigned int gfporder;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /* force GFP flags, e.g. GFP_DMA */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800400 gfp_t gfpflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Andrew Mortona737b3e2006-03-22 00:08:11 -0800402 size_t colour; /* cache colouring range */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800403 unsigned int colour_off; /* colour offset */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800404 struct kmem_cache *slabp_cache;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800405 unsigned int slab_size;
Andrew Mortona737b3e2006-03-22 00:08:11 -0800406 unsigned int dflags; /* dynamic flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /* constructor func */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800409 void (*ctor) (void *, struct kmem_cache *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /* de-constructor func */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800412 void (*dtor) (void *, struct kmem_cache *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800414/* 5) cache creation/removal */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800415 const char *name;
416 struct list_head next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800418/* 6) statistics */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419#if STATS
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800420 unsigned long num_active;
421 unsigned long num_allocations;
422 unsigned long high_mark;
423 unsigned long grown;
424 unsigned long reaped;
425 unsigned long errors;
426 unsigned long max_freeable;
427 unsigned long node_allocs;
428 unsigned long node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700429 unsigned long node_overflow;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800430 atomic_t allochit;
431 atomic_t allocmiss;
432 atomic_t freehit;
433 atomic_t freemiss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434#endif
435#if DEBUG
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800436 /*
437 * If debugging is enabled, then the allocator can add additional
438 * fields and/or padding to every object. buffer_size contains the total
439 * object size including these internal fields, the following two
440 * variables contain the offset to the user object and its size.
441 */
442 int obj_offset;
443 int obj_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444#endif
445};
446
447#define CFLGS_OFF_SLAB (0x80000000UL)
448#define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
449
450#define BATCHREFILL_LIMIT 16
Andrew Mortona737b3e2006-03-22 00:08:11 -0800451/*
452 * Optimization question: fewer reaps means less probability for unnessary
453 * cpucache drain/refill cycles.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 *
Adrian Bunkdc6f3f22005-11-08 16:44:08 +0100455 * OTOH the cpuarrays can contain lots of objects,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * which could lock up otherwise freeable slabs.
457 */
458#define REAPTIMEOUT_CPUC (2*HZ)
459#define REAPTIMEOUT_LIST3 (4*HZ)
460
461#if STATS
462#define STATS_INC_ACTIVE(x) ((x)->num_active++)
463#define STATS_DEC_ACTIVE(x) ((x)->num_active--)
464#define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
465#define STATS_INC_GROWN(x) ((x)->grown++)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700466#define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
Andrew Mortona737b3e2006-03-22 00:08:11 -0800467#define STATS_SET_HIGH(x) \
468 do { \
469 if ((x)->num_active > (x)->high_mark) \
470 (x)->high_mark = (x)->num_active; \
471 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472#define STATS_INC_ERR(x) ((x)->errors++)
473#define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
Christoph Lametere498be72005-09-09 13:03:32 -0700474#define STATS_INC_NODEFREES(x) ((x)->node_frees++)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700475#define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800476#define STATS_SET_FREEABLE(x, i) \
477 do { \
478 if ((x)->max_freeable < i) \
479 (x)->max_freeable = i; \
480 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481#define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
482#define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
483#define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
484#define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
485#else
486#define STATS_INC_ACTIVE(x) do { } while (0)
487#define STATS_DEC_ACTIVE(x) do { } while (0)
488#define STATS_INC_ALLOCED(x) do { } while (0)
489#define STATS_INC_GROWN(x) do { } while (0)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700490#define STATS_ADD_REAPED(x,y) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491#define STATS_SET_HIGH(x) do { } while (0)
492#define STATS_INC_ERR(x) do { } while (0)
493#define STATS_INC_NODEALLOCS(x) do { } while (0)
Christoph Lametere498be72005-09-09 13:03:32 -0700494#define STATS_INC_NODEFREES(x) do { } while (0)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700495#define STATS_INC_ACOVERFLOW(x) do { } while (0)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800496#define STATS_SET_FREEABLE(x, i) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497#define STATS_INC_ALLOCHIT(x) do { } while (0)
498#define STATS_INC_ALLOCMISS(x) do { } while (0)
499#define STATS_INC_FREEHIT(x) do { } while (0)
500#define STATS_INC_FREEMISS(x) do { } while (0)
501#endif
502
503#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Andrew Mortona737b3e2006-03-22 00:08:11 -0800505/*
506 * memory layout of objects:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 * 0 : objp
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800508 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 * the end of an object is aligned with the end of the real
510 * allocation. Catches writes behind the end of the allocation.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800511 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 * redzone word.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800513 * cachep->obj_offset: The real object.
514 * cachep->buffer_size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
Andrew Mortona737b3e2006-03-22 00:08:11 -0800515 * cachep->buffer_size - 1* BYTES_PER_WORD: last caller address
516 * [BYTES_PER_WORD long]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800518static int obj_offset(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800520 return cachep->obj_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Pekka Enberg343e0d72006-02-01 03:05:50 -0800523static int obj_size(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800525 return cachep->obj_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
Pekka Enberg343e0d72006-02-01 03:05:50 -0800528static unsigned long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800531 return (unsigned long*) (objp+obj_offset(cachep)-BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Pekka Enberg343e0d72006-02-01 03:05:50 -0800534static unsigned long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
537 if (cachep->flags & SLAB_STORE_USER)
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800538 return (unsigned long *)(objp + cachep->buffer_size -
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800539 2 * BYTES_PER_WORD);
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800540 return (unsigned long *)(objp + cachep->buffer_size - BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Pekka Enberg343e0d72006-02-01 03:05:50 -0800543static void **dbg_userword(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 BUG_ON(!(cachep->flags & SLAB_STORE_USER));
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800546 return (void **)(objp + cachep->buffer_size - BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
549#else
550
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800551#define obj_offset(x) 0
552#define obj_size(cachep) (cachep->buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553#define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long *)NULL;})
554#define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long *)NULL;})
555#define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
556
557#endif
558
559/*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800560 * Maximum size of an obj (in 2^order pages) and absolute limit for the gfp
561 * order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 */
563#if defined(CONFIG_LARGE_ALLOCS)
564#define MAX_OBJ_ORDER 13 /* up to 32Mb */
565#define MAX_GFP_ORDER 13 /* up to 32Mb */
566#elif defined(CONFIG_MMU)
567#define MAX_OBJ_ORDER 5 /* 32 pages */
568#define MAX_GFP_ORDER 5 /* 32 pages */
569#else
570#define MAX_OBJ_ORDER 8 /* up to 1Mb */
571#define MAX_GFP_ORDER 8 /* up to 1Mb */
572#endif
573
574/*
575 * Do not go above this order unless 0 objects fit into the slab.
576 */
577#define BREAK_GFP_ORDER_HI 1
578#define BREAK_GFP_ORDER_LO 0
579static int slab_break_gfp_order = BREAK_GFP_ORDER_LO;
580
Andrew Mortona737b3e2006-03-22 00:08:11 -0800581/*
582 * Functions for storing/retrieving the cachep and or slab from the page
583 * allocator. These are used to find the slab an obj belongs to. With kfree(),
584 * these are used to find the cache which an obj belongs to.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 */
Pekka Enberg065d41c2005-11-13 16:06:46 -0800586static inline void page_set_cache(struct page *page, struct kmem_cache *cache)
587{
588 page->lru.next = (struct list_head *)cache;
589}
590
591static inline struct kmem_cache *page_get_cache(struct page *page)
592{
Nick Piggin84097512006-03-22 00:08:34 -0800593 if (unlikely(PageCompound(page)))
594 page = (struct page *)page_private(page);
Pekka Enbergddc2e812006-06-23 02:03:40 -0700595 BUG_ON(!PageSlab(page));
Pekka Enberg065d41c2005-11-13 16:06:46 -0800596 return (struct kmem_cache *)page->lru.next;
597}
598
599static inline void page_set_slab(struct page *page, struct slab *slab)
600{
601 page->lru.prev = (struct list_head *)slab;
602}
603
604static inline struct slab *page_get_slab(struct page *page)
605{
Nick Piggin84097512006-03-22 00:08:34 -0800606 if (unlikely(PageCompound(page)))
607 page = (struct page *)page_private(page);
Pekka Enbergddc2e812006-06-23 02:03:40 -0700608 BUG_ON(!PageSlab(page));
Pekka Enberg065d41c2005-11-13 16:06:46 -0800609 return (struct slab *)page->lru.prev;
610}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800612static inline struct kmem_cache *virt_to_cache(const void *obj)
613{
614 struct page *page = virt_to_page(obj);
615 return page_get_cache(page);
616}
617
618static inline struct slab *virt_to_slab(const void *obj)
619{
620 struct page *page = virt_to_page(obj);
621 return page_get_slab(page);
622}
623
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800624static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
625 unsigned int idx)
626{
627 return slab->s_mem + cache->buffer_size * idx;
628}
629
630static inline unsigned int obj_to_index(struct kmem_cache *cache,
631 struct slab *slab, void *obj)
632{
633 return (unsigned)(obj - slab->s_mem) / cache->buffer_size;
634}
635
Andrew Mortona737b3e2006-03-22 00:08:11 -0800636/*
637 * These are the default caches for kmalloc. Custom caches can have other sizes.
638 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639struct cache_sizes malloc_sizes[] = {
640#define CACHE(x) { .cs_size = (x) },
641#include <linux/kmalloc_sizes.h>
642 CACHE(ULONG_MAX)
643#undef CACHE
644};
645EXPORT_SYMBOL(malloc_sizes);
646
647/* Must match cache_sizes above. Out of line to keep cache footprint low. */
648struct cache_names {
649 char *name;
650 char *name_dma;
651};
652
653static struct cache_names __initdata cache_names[] = {
654#define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },
655#include <linux/kmalloc_sizes.h>
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800656 {NULL,}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657#undef CACHE
658};
659
660static struct arraycache_init initarray_cache __initdata =
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800661 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662static struct arraycache_init initarray_generic =
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800663 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665/* internal cache of cache description objs */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800666static struct kmem_cache cache_cache = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800667 .batchcount = 1,
668 .limit = BOOT_CPUCACHE_ENTRIES,
669 .shared = 1,
Pekka Enberg343e0d72006-02-01 03:05:50 -0800670 .buffer_size = sizeof(struct kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800671 .name = "kmem_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672#if DEBUG
Pekka Enberg343e0d72006-02-01 03:05:50 -0800673 .obj_size = sizeof(struct kmem_cache),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674#endif
675};
676
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700677#define BAD_ALIEN_MAGIC 0x01020304ul
678
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200679#ifdef CONFIG_LOCKDEP
680
681/*
682 * Slab sometimes uses the kmalloc slabs to store the slab headers
683 * for other slabs "off slab".
684 * The locking for this is tricky in that it nests within the locks
685 * of all other slabs in a few places; to deal with this special
686 * locking we put on-slab caches into a separate lock-class.
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700687 *
688 * We set lock class for alien array caches which are up during init.
689 * The lock annotation will be lost if all cpus of a node goes down and
690 * then comes back up during hotplug
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200691 */
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700692static struct lock_class_key on_slab_l3_key;
693static struct lock_class_key on_slab_alc_key;
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200694
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700695static inline void init_lock_keys(void)
696
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200697{
698 int q;
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700699 struct cache_sizes *s = malloc_sizes;
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200700
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700701 while (s->cs_size != ULONG_MAX) {
702 for_each_node(q) {
703 struct array_cache **alc;
704 int r;
705 struct kmem_list3 *l3 = s->cs_cachep->nodelists[q];
706 if (!l3 || OFF_SLAB(s->cs_cachep))
707 continue;
708 lockdep_set_class(&l3->list_lock, &on_slab_l3_key);
709 alc = l3->alien;
710 /*
711 * FIXME: This check for BAD_ALIEN_MAGIC
712 * should go away when common slab code is taught to
713 * work even without alien caches.
714 * Currently, non NUMA code returns BAD_ALIEN_MAGIC
715 * for alloc_alien_cache,
716 */
717 if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC)
718 continue;
719 for_each_node(r) {
720 if (alc[r])
721 lockdep_set_class(&alc[r]->lock,
722 &on_slab_alc_key);
723 }
724 }
725 s++;
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200726 }
727}
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200728#else
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700729static inline void init_lock_keys(void)
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200730{
731}
732#endif
733
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -0800734/*
735 * 1. Guard access to the cache-chain.
736 * 2. Protect sanity of cpu_online_map against cpu hotplug events
737 */
Ingo Molnarfc0abb12006-01-18 17:42:33 -0800738static DEFINE_MUTEX(cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739static struct list_head cache_chain;
740
741/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 * chicken and egg problem: delay the per-cpu array allocation
743 * until the general caches are up.
744 */
745static enum {
746 NONE,
Christoph Lametere498be72005-09-09 13:03:32 -0700747 PARTIAL_AC,
748 PARTIAL_L3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 FULL
750} g_cpucache_up;
751
Mike Kravetz39d24e62006-05-15 09:44:13 -0700752/*
753 * used by boot code to determine if it can use slab based allocator
754 */
755int slab_is_available(void)
756{
757 return g_cpucache_up == FULL;
758}
759
David Howells52bad642006-11-22 14:54:01 +0000760static DEFINE_PER_CPU(struct delayed_work, reap_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Pekka Enberg343e0d72006-02-01 03:05:50 -0800762static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763{
764 return cachep->array[smp_processor_id()];
765}
766
Andrew Mortona737b3e2006-03-22 00:08:11 -0800767static inline struct kmem_cache *__find_general_cachep(size_t size,
768 gfp_t gfpflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 struct cache_sizes *csizep = malloc_sizes;
771
772#if DEBUG
773 /* This happens if someone tries to call
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800774 * kmem_cache_create(), or __kmalloc(), before
775 * the generic caches are initialized.
776 */
Alok Katariac7e43c72005-09-14 12:17:53 -0700777 BUG_ON(malloc_sizes[INDEX_AC].cs_cachep == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778#endif
779 while (size > csizep->cs_size)
780 csizep++;
781
782 /*
Martin Hicks0abf40c2005-09-03 15:54:54 -0700783 * Really subtle: The last entry with cs->cs_size==ULONG_MAX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 * has cs_{dma,}cachep==NULL. Thus no special case
785 * for large kmalloc calls required.
786 */
787 if (unlikely(gfpflags & GFP_DMA))
788 return csizep->cs_dmacachep;
789 return csizep->cs_cachep;
790}
791
Adrian Bunkb2213852006-09-25 23:31:02 -0700792static struct kmem_cache *kmem_find_general_cachep(size_t size, gfp_t gfpflags)
Manfred Spraul97e2bde2005-05-01 08:58:38 -0700793{
794 return __find_general_cachep(size, gfpflags);
795}
Manfred Spraul97e2bde2005-05-01 08:58:38 -0700796
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800797static size_t slab_mgmt_size(size_t nr_objs, size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800799 return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align);
800}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Andrew Mortona737b3e2006-03-22 00:08:11 -0800802/*
803 * Calculate the number of objects and left-over bytes for a given buffer size.
804 */
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800805static void cache_estimate(unsigned long gfporder, size_t buffer_size,
806 size_t align, int flags, size_t *left_over,
807 unsigned int *num)
808{
809 int nr_objs;
810 size_t mgmt_size;
811 size_t slab_size = PAGE_SIZE << gfporder;
812
813 /*
814 * The slab management structure can be either off the slab or
815 * on it. For the latter case, the memory allocated for a
816 * slab is used for:
817 *
818 * - The struct slab
819 * - One kmem_bufctl_t for each object
820 * - Padding to respect alignment of @align
821 * - @buffer_size bytes for each object
822 *
823 * If the slab management structure is off the slab, then the
824 * alignment will already be calculated into the size. Because
825 * the slabs are all pages aligned, the objects will be at the
826 * correct alignment when allocated.
827 */
828 if (flags & CFLGS_OFF_SLAB) {
829 mgmt_size = 0;
830 nr_objs = slab_size / buffer_size;
831
832 if (nr_objs > SLAB_LIMIT)
833 nr_objs = SLAB_LIMIT;
834 } else {
835 /*
836 * Ignore padding for the initial guess. The padding
837 * is at most @align-1 bytes, and @buffer_size is at
838 * least @align. In the worst case, this result will
839 * be one greater than the number of objects that fit
840 * into the memory allocation when taking the padding
841 * into account.
842 */
843 nr_objs = (slab_size - sizeof(struct slab)) /
844 (buffer_size + sizeof(kmem_bufctl_t));
845
846 /*
847 * This calculated number will be either the right
848 * amount, or one greater than what we want.
849 */
850 if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
851 > slab_size)
852 nr_objs--;
853
854 if (nr_objs > SLAB_LIMIT)
855 nr_objs = SLAB_LIMIT;
856
857 mgmt_size = slab_mgmt_size(nr_objs, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800859 *num = nr_objs;
860 *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
863#define slab_error(cachep, msg) __slab_error(__FUNCTION__, cachep, msg)
864
Andrew Mortona737b3e2006-03-22 00:08:11 -0800865static void __slab_error(const char *function, struct kmem_cache *cachep,
866 char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800869 function, cachep->name, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 dump_stack();
871}
872
Paul Menage3395ee02006-12-06 20:32:16 -0800873/*
874 * By default on NUMA we use alien caches to stage the freeing of
875 * objects allocated from other nodes. This causes massive memory
876 * inefficiencies when using fake NUMA setup to split memory into a
877 * large number of small nodes, so it can be disabled on the command
878 * line
879 */
880
881static int use_alien_caches __read_mostly = 1;
882static int __init noaliencache_setup(char *s)
883{
884 use_alien_caches = 0;
885 return 1;
886}
887__setup("noaliencache", noaliencache_setup);
888
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800889#ifdef CONFIG_NUMA
890/*
891 * Special reaping functions for NUMA systems called from cache_reap().
892 * These take care of doing round robin flushing of alien caches (containing
893 * objects freed on different nodes from which they were allocated) and the
894 * flushing of remote pcps by calling drain_node_pages.
895 */
896static DEFINE_PER_CPU(unsigned long, reap_node);
897
898static void init_reap_node(int cpu)
899{
900 int node;
901
902 node = next_node(cpu_to_node(cpu), node_online_map);
903 if (node == MAX_NUMNODES)
Paul Jackson442295c2006-03-22 00:09:11 -0800904 node = first_node(node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800905
Daniel Yeisley7f6b8872006-11-02 22:07:14 -0800906 per_cpu(reap_node, cpu) = node;
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800907}
908
909static void next_reap_node(void)
910{
911 int node = __get_cpu_var(reap_node);
912
913 /*
914 * Also drain per cpu pages on remote zones
915 */
916 if (node != numa_node_id())
917 drain_node_pages(node);
918
919 node = next_node(node, node_online_map);
920 if (unlikely(node >= MAX_NUMNODES))
921 node = first_node(node_online_map);
922 __get_cpu_var(reap_node) = node;
923}
924
925#else
926#define init_reap_node(cpu) do { } while (0)
927#define next_reap_node(void) do { } while (0)
928#endif
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930/*
931 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
932 * via the workqueue/eventd.
933 * Add the CPU number into the expiration time to minimize the possibility of
934 * the CPUs getting into lockstep and contending for the global cache chain
935 * lock.
936 */
937static void __devinit start_cpu_timer(int cpu)
938{
David Howells52bad642006-11-22 14:54:01 +0000939 struct delayed_work *reap_work = &per_cpu(reap_work, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940
941 /*
942 * When this gets called from do_initcalls via cpucache_init(),
943 * init_workqueues() has already run, so keventd will be setup
944 * at that time.
945 */
David Howells52bad642006-11-22 14:54:01 +0000946 if (keventd_up() && reap_work->work.func == NULL) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800947 init_reap_node(cpu);
David Howells65f27f32006-11-22 14:55:48 +0000948 INIT_DELAYED_WORK(reap_work, cache_reap);
Arjan van de Ven2b284212006-12-10 02:21:28 -0800949 schedule_delayed_work_on(cpu, reap_work,
950 __round_jiffies_relative(HZ, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
952}
953
Christoph Lametere498be72005-09-09 13:03:32 -0700954static struct array_cache *alloc_arraycache(int node, int entries,
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800955 int batchcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800957 int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 struct array_cache *nc = NULL;
959
Christoph Lametere498be72005-09-09 13:03:32 -0700960 nc = kmalloc_node(memsize, GFP_KERNEL, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (nc) {
962 nc->avail = 0;
963 nc->limit = entries;
964 nc->batchcount = batchcount;
965 nc->touched = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700966 spin_lock_init(&nc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 }
968 return nc;
969}
970
Christoph Lameter3ded1752006-03-25 03:06:44 -0800971/*
972 * Transfer objects in one arraycache to another.
973 * Locking must be handled by the caller.
974 *
975 * Return the number of entries transferred.
976 */
977static int transfer_objects(struct array_cache *to,
978 struct array_cache *from, unsigned int max)
979{
980 /* Figure out how many entries to transfer */
981 int nr = min(min(from->avail, max), to->limit - to->avail);
982
983 if (!nr)
984 return 0;
985
986 memcpy(to->entry + to->avail, from->entry + from->avail -nr,
987 sizeof(void *) *nr);
988
989 from->avail -= nr;
990 to->avail += nr;
991 to->touched = 1;
992 return nr;
993}
994
Christoph Lameter765c4502006-09-27 01:50:08 -0700995#ifndef CONFIG_NUMA
996
997#define drain_alien_cache(cachep, alien) do { } while (0)
998#define reap_alien(cachep, l3) do { } while (0)
999
1000static inline struct array_cache **alloc_alien_cache(int node, int limit)
1001{
1002 return (struct array_cache **)BAD_ALIEN_MAGIC;
1003}
1004
1005static inline void free_alien_cache(struct array_cache **ac_ptr)
1006{
1007}
1008
1009static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
1010{
1011 return 0;
1012}
1013
1014static inline void *alternate_node_alloc(struct kmem_cache *cachep,
1015 gfp_t flags)
1016{
1017 return NULL;
1018}
1019
Christoph Hellwig8b98c162006-12-06 20:32:30 -08001020static inline void *____cache_alloc_node(struct kmem_cache *cachep,
Christoph Lameter765c4502006-09-27 01:50:08 -07001021 gfp_t flags, int nodeid)
1022{
1023 return NULL;
1024}
1025
1026#else /* CONFIG_NUMA */
1027
Christoph Hellwig8b98c162006-12-06 20:32:30 -08001028static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
Paul Jacksonc61afb12006-03-24 03:16:08 -08001029static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
Christoph Lameterdc85da12006-01-18 17:42:36 -08001030
Pekka Enberg5295a742006-02-01 03:05:48 -08001031static struct array_cache **alloc_alien_cache(int node, int limit)
Christoph Lametere498be72005-09-09 13:03:32 -07001032{
1033 struct array_cache **ac_ptr;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001034 int memsize = sizeof(void *) * MAX_NUMNODES;
Christoph Lametere498be72005-09-09 13:03:32 -07001035 int i;
1036
1037 if (limit > 1)
1038 limit = 12;
1039 ac_ptr = kmalloc_node(memsize, GFP_KERNEL, node);
1040 if (ac_ptr) {
1041 for_each_node(i) {
1042 if (i == node || !node_online(i)) {
1043 ac_ptr[i] = NULL;
1044 continue;
1045 }
1046 ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d);
1047 if (!ac_ptr[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001048 for (i--; i <= 0; i--)
Christoph Lametere498be72005-09-09 13:03:32 -07001049 kfree(ac_ptr[i]);
1050 kfree(ac_ptr);
1051 return NULL;
1052 }
1053 }
1054 }
1055 return ac_ptr;
1056}
1057
Pekka Enberg5295a742006-02-01 03:05:48 -08001058static void free_alien_cache(struct array_cache **ac_ptr)
Christoph Lametere498be72005-09-09 13:03:32 -07001059{
1060 int i;
1061
1062 if (!ac_ptr)
1063 return;
Christoph Lametere498be72005-09-09 13:03:32 -07001064 for_each_node(i)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001065 kfree(ac_ptr[i]);
Christoph Lametere498be72005-09-09 13:03:32 -07001066 kfree(ac_ptr);
1067}
1068
Pekka Enberg343e0d72006-02-01 03:05:50 -08001069static void __drain_alien_cache(struct kmem_cache *cachep,
Pekka Enberg5295a742006-02-01 03:05:48 -08001070 struct array_cache *ac, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07001071{
1072 struct kmem_list3 *rl3 = cachep->nodelists[node];
1073
1074 if (ac->avail) {
1075 spin_lock(&rl3->list_lock);
Christoph Lametere00946f2006-03-25 03:06:45 -08001076 /*
1077 * Stuff objects into the remote nodes shared array first.
1078 * That way we could avoid the overhead of putting the objects
1079 * into the free lists and getting them back later.
1080 */
shin, jacob693f7d32006-04-28 10:54:37 -05001081 if (rl3->shared)
1082 transfer_objects(rl3->shared, ac, ac->limit);
Christoph Lametere00946f2006-03-25 03:06:45 -08001083
Christoph Lameterff694162005-09-22 21:44:02 -07001084 free_block(cachep, ac->entry, ac->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001085 ac->avail = 0;
1086 spin_unlock(&rl3->list_lock);
1087 }
1088}
1089
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001090/*
1091 * Called from cache_reap() to regularly drain alien caches round robin.
1092 */
1093static void reap_alien(struct kmem_cache *cachep, struct kmem_list3 *l3)
1094{
1095 int node = __get_cpu_var(reap_node);
1096
1097 if (l3->alien) {
1098 struct array_cache *ac = l3->alien[node];
Christoph Lametere00946f2006-03-25 03:06:45 -08001099
1100 if (ac && ac->avail && spin_trylock_irq(&ac->lock)) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001101 __drain_alien_cache(cachep, ac, node);
1102 spin_unlock_irq(&ac->lock);
1103 }
1104 }
1105}
1106
Andrew Mortona737b3e2006-03-22 00:08:11 -08001107static void drain_alien_cache(struct kmem_cache *cachep,
1108 struct array_cache **alien)
Christoph Lametere498be72005-09-09 13:03:32 -07001109{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001110 int i = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07001111 struct array_cache *ac;
1112 unsigned long flags;
1113
1114 for_each_online_node(i) {
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001115 ac = alien[i];
Christoph Lametere498be72005-09-09 13:03:32 -07001116 if (ac) {
1117 spin_lock_irqsave(&ac->lock, flags);
1118 __drain_alien_cache(cachep, ac, i);
1119 spin_unlock_irqrestore(&ac->lock, flags);
1120 }
1121 }
1122}
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001123
Ingo Molnar873623d2006-07-13 14:44:38 +02001124static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001125{
1126 struct slab *slabp = virt_to_slab(objp);
1127 int nodeid = slabp->nodeid;
1128 struct kmem_list3 *l3;
1129 struct array_cache *alien = NULL;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001130 int node;
1131
1132 node = numa_node_id();
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001133
1134 /*
1135 * Make sure we are not freeing a object from another node to the array
1136 * cache on this cpu.
1137 */
Paul Menage3395ee02006-12-06 20:32:16 -08001138 if (likely(slabp->nodeid == node) || unlikely(!use_alien_caches))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001139 return 0;
1140
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001141 l3 = cachep->nodelists[node];
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001142 STATS_INC_NODEFREES(cachep);
1143 if (l3->alien && l3->alien[nodeid]) {
1144 alien = l3->alien[nodeid];
Ingo Molnar873623d2006-07-13 14:44:38 +02001145 spin_lock(&alien->lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001146 if (unlikely(alien->avail == alien->limit)) {
1147 STATS_INC_ACOVERFLOW(cachep);
1148 __drain_alien_cache(cachep, alien, nodeid);
1149 }
1150 alien->entry[alien->avail++] = objp;
1151 spin_unlock(&alien->lock);
1152 } else {
1153 spin_lock(&(cachep->nodelists[nodeid])->list_lock);
1154 free_block(cachep, &objp, 1, nodeid);
1155 spin_unlock(&(cachep->nodelists[nodeid])->list_lock);
1156 }
1157 return 1;
1158}
Christoph Lametere498be72005-09-09 13:03:32 -07001159#endif
1160
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001161static int __cpuinit cpuup_callback(struct notifier_block *nfb,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001162 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163{
1164 long cpu = (long)hcpu;
Pekka Enberg343e0d72006-02-01 03:05:50 -08001165 struct kmem_cache *cachep;
Christoph Lametere498be72005-09-09 13:03:32 -07001166 struct kmem_list3 *l3 = NULL;
1167 int node = cpu_to_node(cpu);
1168 int memsize = sizeof(struct kmem_list3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
1170 switch (action) {
1171 case CPU_UP_PREPARE:
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001172 mutex_lock(&cache_chain_mutex);
Andrew Mortona737b3e2006-03-22 00:08:11 -08001173 /*
1174 * We need to do this right in the beginning since
Christoph Lametere498be72005-09-09 13:03:32 -07001175 * alloc_arraycache's are going to use this list.
1176 * kmalloc_node allows us to add the slab to the right
1177 * kmem_list3 and not this cpu's kmem_list3
1178 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179
Christoph Lametere498be72005-09-09 13:03:32 -07001180 list_for_each_entry(cachep, &cache_chain, next) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08001181 /*
1182 * Set up the size64 kmemlist for cpu before we can
Christoph Lametere498be72005-09-09 13:03:32 -07001183 * begin anything. Make sure some other cpu on this
1184 * node has not already allocated this
1185 */
1186 if (!cachep->nodelists[node]) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08001187 l3 = kmalloc_node(memsize, GFP_KERNEL, node);
1188 if (!l3)
Christoph Lametere498be72005-09-09 13:03:32 -07001189 goto bad;
1190 kmem_list3_init(l3);
1191 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001192 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lametere498be72005-09-09 13:03:32 -07001193
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001194 /*
1195 * The l3s don't come and go as CPUs come and
1196 * go. cache_chain_mutex is sufficient
1197 * protection here.
1198 */
Christoph Lametere498be72005-09-09 13:03:32 -07001199 cachep->nodelists[node] = l3;
1200 }
1201
1202 spin_lock_irq(&cachep->nodelists[node]->list_lock);
1203 cachep->nodelists[node]->free_limit =
Andrew Mortona737b3e2006-03-22 00:08:11 -08001204 (1 + nr_cpus_node(node)) *
1205 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07001206 spin_unlock_irq(&cachep->nodelists[node]->list_lock);
1207 }
1208
Andrew Mortona737b3e2006-03-22 00:08:11 -08001209 /*
1210 * Now we can go ahead with allocating the shared arrays and
1211 * array caches
1212 */
Christoph Lametere498be72005-09-09 13:03:32 -07001213 list_for_each_entry(cachep, &cache_chain, next) {
Tobias Klausercd105df2006-01-08 01:00:59 -08001214 struct array_cache *nc;
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001215 struct array_cache *shared;
Paul Menage3395ee02006-12-06 20:32:16 -08001216 struct array_cache **alien = NULL;
Tobias Klausercd105df2006-01-08 01:00:59 -08001217
Christoph Lametere498be72005-09-09 13:03:32 -07001218 nc = alloc_arraycache(node, cachep->limit,
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001219 cachep->batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (!nc)
1221 goto bad;
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001222 shared = alloc_arraycache(node,
1223 cachep->shared * cachep->batchcount,
1224 0xbaadf00d);
1225 if (!shared)
1226 goto bad;
Linus Torvalds7a21ef62006-02-05 11:26:38 -08001227
Paul Menage3395ee02006-12-06 20:32:16 -08001228 if (use_alien_caches) {
1229 alien = alloc_alien_cache(node, cachep->limit);
1230 if (!alien)
1231 goto bad;
1232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 cachep->array[cpu] = nc;
Christoph Lametere498be72005-09-09 13:03:32 -07001234 l3 = cachep->nodelists[node];
1235 BUG_ON(!l3);
Christoph Lametere498be72005-09-09 13:03:32 -07001236
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001237 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;
Christoph Lametere498be72005-09-09 13:03:32 -07001245 }
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001246#ifdef CONFIG_NUMA
1247 if (!l3->alien) {
1248 l3->alien = alien;
1249 alien = NULL;
1250 }
1251#endif
1252 spin_unlock_irq(&l3->list_lock);
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001253 kfree(shared);
1254 free_alien_cache(alien);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 break;
1257 case CPU_ONLINE:
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08001258 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 start_cpu_timer(cpu);
1260 break;
1261#ifdef CONFIG_HOTPLUG_CPU
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08001262 case CPU_DOWN_PREPARE:
1263 mutex_lock(&cache_chain_mutex);
1264 break;
1265 case CPU_DOWN_FAILED:
1266 mutex_unlock(&cache_chain_mutex);
1267 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 case CPU_DEAD:
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001269 /*
1270 * Even if all the cpus of a node are down, we don't free the
1271 * kmem_list3 of any cache. This to avoid a race between
1272 * cpu_down, and a kmalloc allocation from another cpu for
1273 * memory from the node of the cpu going down. The list3
1274 * structure is usually allocated from kmem_cache_create() and
1275 * gets destroyed at kmem_cache_destroy().
1276 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 /* fall thru */
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08001278#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 case CPU_UP_CANCELED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 list_for_each_entry(cachep, &cache_chain, next) {
1281 struct array_cache *nc;
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001282 struct array_cache *shared;
1283 struct array_cache **alien;
Christoph Lametere498be72005-09-09 13:03:32 -07001284 cpumask_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
Christoph Lametere498be72005-09-09 13:03:32 -07001286 mask = node_to_cpumask(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 /* cpu is dead; no one can alloc from it. */
1288 nc = cachep->array[cpu];
1289 cachep->array[cpu] = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07001290 l3 = cachep->nodelists[node];
1291
1292 if (!l3)
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001293 goto free_array_cache;
Christoph Lametere498be72005-09-09 13:03:32 -07001294
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08001295 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07001296
1297 /* Free limit for this kmem_list3 */
1298 l3->free_limit -= cachep->batchcount;
1299 if (nc)
Christoph Lameterff694162005-09-22 21:44:02 -07001300 free_block(cachep, nc->entry, nc->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001301
1302 if (!cpus_empty(mask)) {
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08001303 spin_unlock_irq(&l3->list_lock);
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001304 goto free_array_cache;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001305 }
Christoph Lametere498be72005-09-09 13:03:32 -07001306
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001307 shared = l3->shared;
1308 if (shared) {
Christoph Lametere498be72005-09-09 13:03:32 -07001309 free_block(cachep, l3->shared->entry,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001310 l3->shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001311 l3->shared = NULL;
1312 }
Christoph Lametere498be72005-09-09 13:03:32 -07001313
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001314 alien = l3->alien;
1315 l3->alien = NULL;
1316
1317 spin_unlock_irq(&l3->list_lock);
1318
1319 kfree(shared);
1320 if (alien) {
1321 drain_alien_cache(cachep, alien);
1322 free_alien_cache(alien);
Christoph Lametere498be72005-09-09 13:03:32 -07001323 }
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001324free_array_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 kfree(nc);
1326 }
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001327 /*
1328 * In the previous loop, all the objects were freed to
1329 * the respective cache's slabs, now we can go ahead and
1330 * shrink each nodelist to its limit.
1331 */
1332 list_for_each_entry(cachep, &cache_chain, next) {
1333 l3 = cachep->nodelists[node];
1334 if (!l3)
1335 continue;
Christoph Lametered11d9e2006-06-30 01:55:45 -07001336 drain_freelist(cachep, l3, l3->free_objects);
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001337 }
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001338 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
1341 return NOTIFY_OK;
Andrew Mortona737b3e2006-03-22 00:08:11 -08001342bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 return NOTIFY_BAD;
1344}
1345
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001346static struct notifier_block __cpuinitdata cpucache_notifier = {
1347 &cpuup_callback, NULL, 0
1348};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Christoph Lametere498be72005-09-09 13:03:32 -07001350/*
1351 * swap the static kmem_list3 with kmalloced memory
1352 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001353static void init_list(struct kmem_cache *cachep, struct kmem_list3 *list,
1354 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07001355{
1356 struct kmem_list3 *ptr;
1357
Christoph Lametere498be72005-09-09 13:03:32 -07001358 ptr = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, nodeid);
1359 BUG_ON(!ptr);
1360
1361 local_irq_disable();
1362 memcpy(ptr, list, sizeof(struct kmem_list3));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001363 /*
1364 * Do not assume that spinlocks can be initialized via memcpy:
1365 */
1366 spin_lock_init(&ptr->list_lock);
1367
Christoph Lametere498be72005-09-09 13:03:32 -07001368 MAKE_ALL_LISTS(cachep, ptr, nodeid);
1369 cachep->nodelists[nodeid] = ptr;
1370 local_irq_enable();
1371}
1372
Andrew Mortona737b3e2006-03-22 00:08:11 -08001373/*
1374 * Initialisation. Called after the page allocator have been initialised and
1375 * before smp_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 */
1377void __init kmem_cache_init(void)
1378{
1379 size_t left_over;
1380 struct cache_sizes *sizes;
1381 struct cache_names *names;
Christoph Lametere498be72005-09-09 13:03:32 -07001382 int i;
Jack Steiner07ed76b2006-03-07 21:55:46 -08001383 int order;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001384 int node;
Christoph Lametere498be72005-09-09 13:03:32 -07001385
1386 for (i = 0; i < NUM_INIT_LISTS; i++) {
1387 kmem_list3_init(&initkmem_list3[i]);
1388 if (i < MAX_NUMNODES)
1389 cache_cache.nodelists[i] = NULL;
1390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
1392 /*
1393 * Fragmentation resistance on low memory - only use bigger
1394 * page orders on machines with more than 32MB of memory.
1395 */
1396 if (num_physpages > (32 << 20) >> PAGE_SHIFT)
1397 slab_break_gfp_order = BREAK_GFP_ORDER_HI;
1398
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 /* Bootstrap is tricky, because several objects are allocated
1400 * from caches that do not exist yet:
Andrew Mortona737b3e2006-03-22 00:08:11 -08001401 * 1) initialize the cache_cache cache: it contains the struct
1402 * kmem_cache structures of all caches, except cache_cache itself:
1403 * cache_cache is statically allocated.
Christoph Lametere498be72005-09-09 13:03:32 -07001404 * Initially an __init data area is used for the head array and the
1405 * kmem_list3 structures, it's replaced with a kmalloc allocated
1406 * array at the end of the bootstrap.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 * 2) Create the first kmalloc cache.
Pekka Enberg343e0d72006-02-01 03:05:50 -08001408 * The struct kmem_cache for the new cache is allocated normally.
Christoph Lametere498be72005-09-09 13:03:32 -07001409 * An __init data area is used for the head array.
1410 * 3) Create the remaining kmalloc caches, with minimally sized
1411 * head arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 * 4) Replace the __init data head arrays for cache_cache and the first
1413 * kmalloc cache with kmalloc allocated arrays.
Christoph Lametere498be72005-09-09 13:03:32 -07001414 * 5) Replace the __init data for kmem_list3 for cache_cache and
1415 * the other cache's with kmalloc allocated memory.
1416 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 */
1418
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001419 node = numa_node_id();
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 /* 1) create the cache_cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 INIT_LIST_HEAD(&cache_chain);
1423 list_add(&cache_cache.next, &cache_chain);
1424 cache_cache.colour_off = cache_line_size();
1425 cache_cache.array[smp_processor_id()] = &initarray_cache.cache;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001426 cache_cache.nodelists[node] = &initkmem_list3[CACHE_CACHE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427
Andrew Mortona737b3e2006-03-22 00:08:11 -08001428 cache_cache.buffer_size = ALIGN(cache_cache.buffer_size,
1429 cache_line_size());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
Jack Steiner07ed76b2006-03-07 21:55:46 -08001431 for (order = 0; order < MAX_ORDER; order++) {
1432 cache_estimate(order, cache_cache.buffer_size,
1433 cache_line_size(), 0, &left_over, &cache_cache.num);
1434 if (cache_cache.num)
1435 break;
1436 }
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02001437 BUG_ON(!cache_cache.num);
Jack Steiner07ed76b2006-03-07 21:55:46 -08001438 cache_cache.gfporder = order;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001439 cache_cache.colour = left_over / cache_cache.colour_off;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001440 cache_cache.slab_size = ALIGN(cache_cache.num * sizeof(kmem_bufctl_t) +
1441 sizeof(struct slab), cache_line_size());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
1443 /* 2+3) create the kmalloc caches */
1444 sizes = malloc_sizes;
1445 names = cache_names;
1446
Andrew Mortona737b3e2006-03-22 00:08:11 -08001447 /*
1448 * Initialize the caches that provide memory for the array cache and the
1449 * kmem_list3 structures first. Without this, further allocations will
1450 * bug.
Christoph Lametere498be72005-09-09 13:03:32 -07001451 */
1452
1453 sizes[INDEX_AC].cs_cachep = kmem_cache_create(names[INDEX_AC].name,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001454 sizes[INDEX_AC].cs_size,
1455 ARCH_KMALLOC_MINALIGN,
1456 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
1457 NULL, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07001458
Andrew Mortona737b3e2006-03-22 00:08:11 -08001459 if (INDEX_AC != INDEX_L3) {
Christoph Lametere498be72005-09-09 13:03:32 -07001460 sizes[INDEX_L3].cs_cachep =
Andrew Mortona737b3e2006-03-22 00:08:11 -08001461 kmem_cache_create(names[INDEX_L3].name,
1462 sizes[INDEX_L3].cs_size,
1463 ARCH_KMALLOC_MINALIGN,
1464 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
1465 NULL, NULL);
1466 }
Christoph Lametere498be72005-09-09 13:03:32 -07001467
Ingo Molnare0a42722006-06-23 02:03:46 -07001468 slab_early_init = 0;
1469
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 while (sizes->cs_size != ULONG_MAX) {
Christoph Lametere498be72005-09-09 13:03:32 -07001471 /*
1472 * For performance, all the general caches are L1 aligned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 * This should be particularly beneficial on SMP boxes, as it
1474 * eliminates "false sharing".
1475 * Note for systems short on memory removing the alignment will
Christoph Lametere498be72005-09-09 13:03:32 -07001476 * allow tighter packing of the smaller caches.
1477 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001478 if (!sizes->cs_cachep) {
Christoph Lametere498be72005-09-09 13:03:32 -07001479 sizes->cs_cachep = kmem_cache_create(names->name,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001480 sizes->cs_size,
1481 ARCH_KMALLOC_MINALIGN,
1482 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
1483 NULL, NULL);
1484 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 sizes->cs_dmacachep = kmem_cache_create(names->name_dma,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001487 sizes->cs_size,
1488 ARCH_KMALLOC_MINALIGN,
1489 ARCH_KMALLOC_FLAGS|SLAB_CACHE_DMA|
1490 SLAB_PANIC,
1491 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 sizes++;
1493 names++;
1494 }
1495 /* 4) Replace the bootstrap head arrays */
1496 {
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001497 struct array_cache *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
Christoph Lametere498be72005-09-09 13:03:32 -07001500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 local_irq_disable();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001502 BUG_ON(cpu_cache_get(&cache_cache) != &initarray_cache.cache);
1503 memcpy(ptr, cpu_cache_get(&cache_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001504 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001505 /*
1506 * Do not assume that spinlocks can be initialized via memcpy:
1507 */
1508 spin_lock_init(&ptr->lock);
1509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 cache_cache.array[smp_processor_id()] = ptr;
1511 local_irq_enable();
Christoph Lametere498be72005-09-09 13:03:32 -07001512
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
Christoph Lametere498be72005-09-09 13:03:32 -07001514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 local_irq_disable();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001516 BUG_ON(cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001517 != &initarray_generic.cache);
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001518 memcpy(ptr, cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001519 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001520 /*
1521 * Do not assume that spinlocks can be initialized via memcpy:
1522 */
1523 spin_lock_init(&ptr->lock);
1524
Christoph Lametere498be72005-09-09 13:03:32 -07001525 malloc_sizes[INDEX_AC].cs_cachep->array[smp_processor_id()] =
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001526 ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 local_irq_enable();
1528 }
Christoph Lametere498be72005-09-09 13:03:32 -07001529 /* 5) Replace the bootstrap kmem_list3's */
1530 {
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001531 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001533 /* Replace the static kmem_list3 structures for the boot cpu */
1534 init_list(&cache_cache, &initkmem_list3[CACHE_CACHE], node);
1535
1536 for_each_online_node(nid) {
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)
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07001552 if (enable_cpucache(cachep))
1553 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;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001611
1612 page = alloc_pages_node(nodeid, flags, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (!page)
1614 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001616 nr_pages = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
Christoph Lameter972d1a72006-09-25 23:31:51 -07001618 add_zone_page_state(page_zone(page),
1619 NR_SLAB_RECLAIMABLE, nr_pages);
1620 else
1621 add_zone_page_state(page_zone(page),
1622 NR_SLAB_UNRECLAIMABLE, nr_pages);
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001623 for (i = 0; i < nr_pages; i++)
1624 __SetPageSlab(page + i);
1625 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626}
1627
1628/*
1629 * Interface to system's page release.
1630 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001631static void kmem_freepages(struct kmem_cache *cachep, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001633 unsigned long i = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 struct page *page = virt_to_page(addr);
1635 const unsigned long nr_freed = i;
1636
Christoph Lameter972d1a72006-09-25 23:31:51 -07001637 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1638 sub_zone_page_state(page_zone(page),
1639 NR_SLAB_RECLAIMABLE, nr_freed);
1640 else
1641 sub_zone_page_state(page_zone(page),
1642 NR_SLAB_UNRECLAIMABLE, nr_freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 while (i--) {
Nick Pigginf205b2f2006-03-22 00:08:02 -08001644 BUG_ON(!PageSlab(page));
1645 __ClearPageSlab(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 page++;
1647 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (current->reclaim_state)
1649 current->reclaim_state->reclaimed_slab += nr_freed;
1650 free_pages((unsigned long)addr, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651}
1652
1653static void kmem_rcu_free(struct rcu_head *head)
1654{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001655 struct slab_rcu *slab_rcu = (struct slab_rcu *)head;
Pekka Enberg343e0d72006-02-01 03:05:50 -08001656 struct kmem_cache *cachep = slab_rcu->cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
1658 kmem_freepages(cachep, slab_rcu->addr);
1659 if (OFF_SLAB(cachep))
1660 kmem_cache_free(cachep->slabp_cache, slab_rcu);
1661}
1662
1663#if DEBUG
1664
1665#ifdef CONFIG_DEBUG_PAGEALLOC
Pekka Enberg343e0d72006-02-01 03:05:50 -08001666static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001667 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001669 int size = obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001671 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001673 if (size < 5 * sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return;
1675
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001676 *addr++ = 0x12345678;
1677 *addr++ = caller;
1678 *addr++ = smp_processor_id();
1679 size -= 3 * sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 {
1681 unsigned long *sptr = &caller;
1682 unsigned long svalue;
1683
1684 while (!kstack_end(sptr)) {
1685 svalue = *sptr++;
1686 if (kernel_text_address(svalue)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001687 *addr++ = svalue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 size -= sizeof(unsigned long);
1689 if (size <= sizeof(unsigned long))
1690 break;
1691 }
1692 }
1693
1694 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001695 *addr++ = 0x87654321;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696}
1697#endif
1698
Pekka Enberg343e0d72006-02-01 03:05:50 -08001699static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001701 int size = obj_size(cachep);
1702 addr = &((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 memset(addr, val, size);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001705 *(unsigned char *)(addr + size - 1) = POISON_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706}
1707
1708static void dump_line(char *data, int offset, int limit)
1709{
1710 int i;
Dave Jonesaa83aa42006-09-29 01:59:51 -07001711 unsigned char error = 0;
1712 int bad_count = 0;
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 printk(KERN_ERR "%03x:", offset);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001715 for (i = 0; i < limit; i++) {
1716 if (data[offset + i] != POISON_FREE) {
1717 error = data[offset + i];
1718 bad_count++;
1719 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001720 printk(" %02x", (unsigned char)data[offset + i]);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001721 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 printk("\n");
Dave Jonesaa83aa42006-09-29 01:59:51 -07001723
1724 if (bad_count == 1) {
1725 error ^= POISON_FREE;
1726 if (!(error & (error - 1))) {
1727 printk(KERN_ERR "Single bit error detected. Probably "
1728 "bad RAM.\n");
1729#ifdef CONFIG_X86
1730 printk(KERN_ERR "Run memtest86+ or a similar memory "
1731 "test tool.\n");
1732#else
1733 printk(KERN_ERR "Run a memory test tool.\n");
1734#endif
1735 }
1736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
1738#endif
1739
1740#if DEBUG
1741
Pekka Enberg343e0d72006-02-01 03:05:50 -08001742static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743{
1744 int i, size;
1745 char *realobj;
1746
1747 if (cachep->flags & SLAB_RED_ZONE) {
1748 printk(KERN_ERR "Redzone: 0x%lx/0x%lx.\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001749 *dbg_redzone1(cachep, objp),
1750 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 }
1752
1753 if (cachep->flags & SLAB_STORE_USER) {
1754 printk(KERN_ERR "Last user: [<%p>]",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001755 *dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 print_symbol("(%s)",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001757 (unsigned long)*dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 printk("\n");
1759 }
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001760 realobj = (char *)objp + obj_offset(cachep);
1761 size = obj_size(cachep);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001762 for (i = 0; i < size && lines; i += 16, lines--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 int limit;
1764 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001765 if (i + limit > size)
1766 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 dump_line(realobj, i, limit);
1768 }
1769}
1770
Pekka Enberg343e0d72006-02-01 03:05:50 -08001771static void check_poison_obj(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772{
1773 char *realobj;
1774 int size, i;
1775 int lines = 0;
1776
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001777 realobj = (char *)objp + obj_offset(cachep);
1778 size = obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001780 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 char exp = POISON_FREE;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001782 if (i == size - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 exp = POISON_END;
1784 if (realobj[i] != exp) {
1785 int limit;
1786 /* Mismatch ! */
1787 /* Print header */
1788 if (lines == 0) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001789 printk(KERN_ERR
Andrew Mortona737b3e2006-03-22 00:08:11 -08001790 "Slab corruption: start=%p, len=%d\n",
1791 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 print_objinfo(cachep, objp, 0);
1793 }
1794 /* Hexdump the affected line */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001795 i = (i / 16) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001797 if (i + limit > size)
1798 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 dump_line(realobj, i, limit);
1800 i += 16;
1801 lines++;
1802 /* Limit to 5 lines */
1803 if (lines > 5)
1804 break;
1805 }
1806 }
1807 if (lines != 0) {
1808 /* Print some data about the neighboring objects, if they
1809 * exist:
1810 */
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08001811 struct slab *slabp = virt_to_slab(objp);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001812 unsigned int objnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001814 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 if (objnr) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001816 objp = index_to_obj(cachep, slabp, objnr - 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001817 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001819 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 print_objinfo(cachep, objp, 2);
1821 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001822 if (objnr + 1 < cachep->num) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001823 objp = index_to_obj(cachep, slabp, objnr + 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001824 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001826 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 print_objinfo(cachep, objp, 2);
1828 }
1829 }
1830}
1831#endif
1832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833#if DEBUG
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001834/**
Randy Dunlap911851e2006-03-22 00:08:14 -08001835 * slab_destroy_objs - destroy a slab and its objects
1836 * @cachep: cache pointer being destroyed
1837 * @slabp: slab pointer being destroyed
1838 *
1839 * Call the registered destructor for each object in a slab that is being
1840 * destroyed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001841 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001842static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001843{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 int i;
1845 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001846 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
1848 if (cachep->flags & SLAB_POISON) {
1849#ifdef CONFIG_DEBUG_PAGEALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -08001850 if (cachep->buffer_size % PAGE_SIZE == 0 &&
1851 OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001852 kernel_map_pages(virt_to_page(objp),
Andrew Mortona737b3e2006-03-22 00:08:11 -08001853 cachep->buffer_size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 else
1855 check_poison_obj(cachep, objp);
1856#else
1857 check_poison_obj(cachep, objp);
1858#endif
1859 }
1860 if (cachep->flags & SLAB_RED_ZONE) {
1861 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
1862 slab_error(cachep, "start of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001863 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
1865 slab_error(cachep, "end of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001866 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 }
1868 if (cachep->dtor && !(cachep->flags & SLAB_POISON))
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001869 (cachep->dtor) (objp + obj_offset(cachep), cachep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001871}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872#else
Pekka Enberg343e0d72006-02-01 03:05:50 -08001873static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001874{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 if (cachep->dtor) {
1876 int i;
1877 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001878 void *objp = index_to_obj(cachep, slabp, i);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001879 (cachep->dtor) (objp, cachep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
1881 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001882}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883#endif
1884
Randy Dunlap911851e2006-03-22 00:08:14 -08001885/**
1886 * slab_destroy - destroy and release all objects in a slab
1887 * @cachep: cache pointer being destroyed
1888 * @slabp: slab pointer being destroyed
1889 *
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001890 * Destroy all the objs in a slab, and release the mem back to the system.
Andrew Mortona737b3e2006-03-22 00:08:11 -08001891 * Before calling the slab must have been unlinked from the cache. The
1892 * cache-lock is not held/needed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001893 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001894static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001895{
1896 void *addr = slabp->s_mem - slabp->colouroff;
1897
1898 slab_destroy_objs(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
1900 struct slab_rcu *slab_rcu;
1901
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001902 slab_rcu = (struct slab_rcu *)slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 slab_rcu->cachep = cachep;
1904 slab_rcu->addr = addr;
1905 call_rcu(&slab_rcu->head, kmem_rcu_free);
1906 } else {
1907 kmem_freepages(cachep, addr);
Ingo Molnar873623d2006-07-13 14:44:38 +02001908 if (OFF_SLAB(cachep))
1909 kmem_cache_free(cachep->slabp_cache, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 }
1911}
1912
Andrew Mortona737b3e2006-03-22 00:08:11 -08001913/*
1914 * For setting up all the kmem_list3s for cache whose buffer_size is same as
1915 * size of kmem_list3.
1916 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001917static void set_up_list3s(struct kmem_cache *cachep, int index)
Christoph Lametere498be72005-09-09 13:03:32 -07001918{
1919 int node;
1920
1921 for_each_online_node(node) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001922 cachep->nodelists[node] = &initkmem_list3[index + node];
Christoph Lametere498be72005-09-09 13:03:32 -07001923 cachep->nodelists[node]->next_reap = jiffies +
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001924 REAPTIMEOUT_LIST3 +
1925 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lametere498be72005-09-09 13:03:32 -07001926 }
1927}
1928
Christoph Lameter117f6eb2006-09-25 23:31:37 -07001929static void __kmem_cache_destroy(struct kmem_cache *cachep)
1930{
1931 int i;
1932 struct kmem_list3 *l3;
1933
1934 for_each_online_cpu(i)
1935 kfree(cachep->array[i]);
1936
1937 /* NUMA: free the list3 structures */
1938 for_each_online_node(i) {
1939 l3 = cachep->nodelists[i];
1940 if (l3) {
1941 kfree(l3->shared);
1942 free_alien_cache(l3->alien);
1943 kfree(l3);
1944 }
1945 }
1946 kmem_cache_free(&cache_cache, cachep);
1947}
1948
1949
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950/**
Randy.Dunlapa70773d2006-02-01 03:05:52 -08001951 * calculate_slab_order - calculate size (page order) of slabs
1952 * @cachep: pointer to the cache that is being created
1953 * @size: size of objects to be created in this cache.
1954 * @align: required alignment for the objects.
1955 * @flags: slab allocation flags
1956 *
1957 * Also calculates the number of objects per slab.
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001958 *
1959 * This could be made much more intelligent. For now, try to avoid using
1960 * high order pages for slabs. When the gfp() functions are more friendly
1961 * towards high-order requests, this should be changed.
1962 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001963static size_t calculate_slab_order(struct kmem_cache *cachep,
Randy Dunlapee13d782006-02-01 03:05:53 -08001964 size_t size, size_t align, unsigned long flags)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001965{
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02001966 unsigned long offslab_limit;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001967 size_t left_over = 0;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001968 int gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001969
Andrew Mortona737b3e2006-03-22 00:08:11 -08001970 for (gfporder = 0; gfporder <= MAX_GFP_ORDER; gfporder++) {
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001971 unsigned int num;
1972 size_t remainder;
1973
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001974 cache_estimate(gfporder, size, align, flags, &remainder, &num);
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001975 if (!num)
1976 continue;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001977
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02001978 if (flags & CFLGS_OFF_SLAB) {
1979 /*
1980 * Max number of objs-per-slab for caches which
1981 * use off-slab slabs. Needed to avoid a possible
1982 * looping condition in cache_grow().
1983 */
1984 offslab_limit = size - sizeof(struct slab);
1985 offslab_limit /= sizeof(kmem_bufctl_t);
1986
1987 if (num > offslab_limit)
1988 break;
1989 }
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001990
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001991 /* Found something acceptable - save it away */
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001992 cachep->num = num;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001993 cachep->gfporder = gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001994 left_over = remainder;
1995
1996 /*
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08001997 * A VFS-reclaimable slab tends to have most allocations
1998 * as GFP_NOFS and we really don't want to have to be allocating
1999 * higher-order pages when we are unable to shrink dcache.
2000 */
2001 if (flags & SLAB_RECLAIM_ACCOUNT)
2002 break;
2003
2004 /*
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002005 * Large number of objects is good, but very large slabs are
2006 * currently bad for the gfp()s.
2007 */
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002008 if (gfporder >= slab_break_gfp_order)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002009 break;
2010
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002011 /*
2012 * Acceptable internal fragmentation?
2013 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002014 if (left_over * 8 <= (PAGE_SIZE << gfporder))
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002015 break;
2016 }
2017 return left_over;
2018}
2019
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002020static int setup_cpu_cache(struct kmem_cache *cachep)
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002021{
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002022 if (g_cpucache_up == FULL)
2023 return enable_cpucache(cachep);
2024
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002025 if (g_cpucache_up == NONE) {
2026 /*
2027 * Note: the first kmem_cache_create must create the cache
2028 * that's used by kmalloc(24), otherwise the creation of
2029 * further caches will BUG().
2030 */
2031 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2032
2033 /*
2034 * If the cache that's used by kmalloc(sizeof(kmem_list3)) is
2035 * the first cache, then we need to set up all its list3s,
2036 * otherwise the creation of further caches will BUG().
2037 */
2038 set_up_list3s(cachep, SIZE_AC);
2039 if (INDEX_AC == INDEX_L3)
2040 g_cpucache_up = PARTIAL_L3;
2041 else
2042 g_cpucache_up = PARTIAL_AC;
2043 } else {
2044 cachep->array[smp_processor_id()] =
2045 kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
2046
2047 if (g_cpucache_up == PARTIAL_AC) {
2048 set_up_list3s(cachep, SIZE_L3);
2049 g_cpucache_up = PARTIAL_L3;
2050 } else {
2051 int node;
2052 for_each_online_node(node) {
2053 cachep->nodelists[node] =
2054 kmalloc_node(sizeof(struct kmem_list3),
2055 GFP_KERNEL, node);
2056 BUG_ON(!cachep->nodelists[node]);
2057 kmem_list3_init(cachep->nodelists[node]);
2058 }
2059 }
2060 }
2061 cachep->nodelists[numa_node_id()]->next_reap =
2062 jiffies + REAPTIMEOUT_LIST3 +
2063 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
2064
2065 cpu_cache_get(cachep)->avail = 0;
2066 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
2067 cpu_cache_get(cachep)->batchcount = 1;
2068 cpu_cache_get(cachep)->touched = 0;
2069 cachep->batchcount = 1;
2070 cachep->limit = BOOT_CPUCACHE_ENTRIES;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002071 return 0;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002072}
2073
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002074/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 * kmem_cache_create - Create a cache.
2076 * @name: A string which is used in /proc/slabinfo to identify this cache.
2077 * @size: The size of objects to be created in this cache.
2078 * @align: The required alignment for the objects.
2079 * @flags: SLAB flags
2080 * @ctor: A constructor for the objects.
2081 * @dtor: A destructor for the objects.
2082 *
2083 * Returns a ptr to the cache on success, NULL on failure.
2084 * Cannot be called within a int, but can be interrupted.
2085 * The @ctor is run when new pages are allocated by the cache
2086 * and the @dtor is run before the pages are handed back.
2087 *
2088 * @name must be valid until the cache is destroyed. This implies that
Andrew Mortona737b3e2006-03-22 00:08:11 -08002089 * the module calling this has to destroy the cache before getting unloaded.
2090 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 * The flags are
2092 *
2093 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2094 * to catch references to uninitialised memory.
2095 *
2096 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2097 * for buffer overruns.
2098 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2100 * cacheline. This can be beneficial if you're counting cycles as closely
2101 * as davem.
2102 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002103struct kmem_cache *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104kmem_cache_create (const char *name, size_t size, size_t align,
Andrew Mortona737b3e2006-03-22 00:08:11 -08002105 unsigned long flags,
2106 void (*ctor)(void*, struct kmem_cache *, unsigned long),
Pekka Enberg343e0d72006-02-01 03:05:50 -08002107 void (*dtor)(void*, struct kmem_cache *, unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108{
2109 size_t left_over, slab_size, ralign;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07002110 struct kmem_cache *cachep = NULL, *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 /*
2113 * Sanity checks... these are all serious usage bugs.
2114 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002115 if (!name || in_interrupt() || (size < BYTES_PER_WORD) ||
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002116 (size > (1 << MAX_OBJ_ORDER) * PAGE_SIZE) || (dtor && !ctor)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002117 printk(KERN_ERR "%s: Early error in slab %s\n", __FUNCTION__,
2118 name);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002119 BUG();
2120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
Ravikiran G Thirumalaif0188f42006-02-10 01:51:13 -08002122 /*
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002123 * We use cache_chain_mutex to ensure a consistent view of
2124 * cpu_online_map as well. Please see cpuup_callback
Ravikiran G Thirumalaif0188f42006-02-10 01:51:13 -08002125 */
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002126 mutex_lock(&cache_chain_mutex);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002127
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07002128 list_for_each_entry(pc, &cache_chain, next) {
Andrew Morton4f12bb42005-11-07 00:58:00 -08002129 char tmp;
2130 int res;
2131
2132 /*
2133 * This happens when the module gets unloaded and doesn't
2134 * destroy its slab cache and no-one else reuses the vmalloc
2135 * area of the module. Print a warning.
2136 */
Andrew Morton138ae662006-12-06 20:36:41 -08002137 res = probe_kernel_address(pc->name, tmp);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002138 if (res) {
2139 printk("SLAB: cache with size %d has lost its name\n",
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002140 pc->buffer_size);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002141 continue;
2142 }
2143
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002144 if (!strcmp(pc->name, name)) {
Andrew Morton4f12bb42005-11-07 00:58:00 -08002145 printk("kmem_cache_create: duplicate cache %s\n", name);
2146 dump_stack();
2147 goto oops;
2148 }
2149 }
2150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151#if DEBUG
2152 WARN_ON(strchr(name, ' ')); /* It confuses parsers */
2153 if ((flags & SLAB_DEBUG_INITIAL) && !ctor) {
2154 /* No constructor, but inital state check requested */
2155 printk(KERN_ERR "%s: No con, but init state check "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002156 "requested - %s\n", __FUNCTION__, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 flags &= ~SLAB_DEBUG_INITIAL;
2158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159#if FORCED_DEBUG
2160 /*
2161 * Enable redzoning and last user accounting, except for caches with
2162 * large objects, if the increased size would increase the object size
2163 * above the next power of two: caches with object sizes just above a
2164 * power of two have a significant amount of internal fragmentation.
2165 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002166 if (size < 4096 || fls(size - 1) == fls(size-1 + 3 * BYTES_PER_WORD))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002167 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 if (!(flags & SLAB_DESTROY_BY_RCU))
2169 flags |= SLAB_POISON;
2170#endif
2171 if (flags & SLAB_DESTROY_BY_RCU)
2172 BUG_ON(flags & SLAB_POISON);
2173#endif
2174 if (flags & SLAB_DESTROY_BY_RCU)
2175 BUG_ON(dtor);
2176
2177 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002178 * Always checks flags, a caller might be expecting debug support which
2179 * isn't available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 */
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002181 BUG_ON(flags & ~CREATE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
Andrew Mortona737b3e2006-03-22 00:08:11 -08002183 /*
2184 * Check that size is in terms of words. This is needed to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 * unaligned accesses for some archs when redzoning is used, and makes
2186 * sure any on-slab bufctl's are also correctly aligned.
2187 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002188 if (size & (BYTES_PER_WORD - 1)) {
2189 size += (BYTES_PER_WORD - 1);
2190 size &= ~(BYTES_PER_WORD - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 }
2192
Andrew Mortona737b3e2006-03-22 00:08:11 -08002193 /* calculate the final buffer alignment: */
2194
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 /* 1) arch recommendation: can be overridden for debug */
2196 if (flags & SLAB_HWCACHE_ALIGN) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002197 /*
2198 * Default alignment: as specified by the arch code. Except if
2199 * an object is really small, then squeeze multiple objects into
2200 * one cacheline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 */
2202 ralign = cache_line_size();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002203 while (size <= ralign / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 ralign /= 2;
2205 } else {
2206 ralign = BYTES_PER_WORD;
2207 }
Pekka Enbergca5f9702006-09-25 23:31:25 -07002208
2209 /*
2210 * Redzoning and user store require word alignment. Note this will be
2211 * overridden by architecture or caller mandated alignment if either
2212 * is greater than BYTES_PER_WORD.
2213 */
2214 if (flags & SLAB_RED_ZONE || flags & SLAB_STORE_USER)
2215 ralign = BYTES_PER_WORD;
2216
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002217 /* 2) arch mandated alignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 if (ralign < ARCH_SLAB_MINALIGN) {
2219 ralign = ARCH_SLAB_MINALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002221 /* 3) caller mandated alignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 if (ralign < align) {
2223 ralign = align;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002225 /* disable debug if necessary */
2226 if (ralign > BYTES_PER_WORD)
2227 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002228 /*
Pekka Enbergca5f9702006-09-25 23:31:25 -07002229 * 4) Store it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 */
2231 align = ralign;
2232
2233 /* Get cache's description obj. */
Christoph Lametere94b1762006-12-06 20:33:17 -08002234 cachep = kmem_cache_zalloc(&cache_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 if (!cachep)
Andrew Morton4f12bb42005-11-07 00:58:00 -08002236 goto oops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238#if DEBUG
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002239 cachep->obj_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Pekka Enbergca5f9702006-09-25 23:31:25 -07002241 /*
2242 * Both debugging options require word-alignment which is calculated
2243 * into align above.
2244 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 if (flags & SLAB_RED_ZONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 /* add space for red zone words */
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002247 cachep->obj_offset += BYTES_PER_WORD;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002248 size += 2 * BYTES_PER_WORD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 }
2250 if (flags & SLAB_STORE_USER) {
Pekka Enbergca5f9702006-09-25 23:31:25 -07002251 /* user store requires one word storage behind the end of
2252 * the real object.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 size += BYTES_PER_WORD;
2255 }
2256#if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002257 if (size >= malloc_sizes[INDEX_L3 + 1].cs_size
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002258 && cachep->obj_size > cache_line_size() && size < PAGE_SIZE) {
2259 cachep->obj_offset += PAGE_SIZE - size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 size = PAGE_SIZE;
2261 }
2262#endif
2263#endif
2264
Ingo Molnare0a42722006-06-23 02:03:46 -07002265 /*
2266 * Determine if the slab management is 'on' or 'off' slab.
2267 * (bootstrapping cannot cope with offslab caches so don't do
2268 * it too early on.)
2269 */
2270 if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 /*
2272 * Size is large, assume best to place the slab management obj
2273 * off-slab (should allow better packing of objs).
2274 */
2275 flags |= CFLGS_OFF_SLAB;
2276
2277 size = ALIGN(size, align);
2278
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08002279 left_over = calculate_slab_order(cachep, size, align, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
2281 if (!cachep->num) {
2282 printk("kmem_cache_create: couldn't create cache %s.\n", name);
2283 kmem_cache_free(&cache_cache, cachep);
2284 cachep = NULL;
Andrew Morton4f12bb42005-11-07 00:58:00 -08002285 goto oops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002287 slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
2288 + sizeof(struct slab), align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
2290 /*
2291 * If the slab has been placed off-slab, and we have enough space then
2292 * move it on-slab. This is at the expense of any extra colouring.
2293 */
2294 if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
2295 flags &= ~CFLGS_OFF_SLAB;
2296 left_over -= slab_size;
2297 }
2298
2299 if (flags & CFLGS_OFF_SLAB) {
2300 /* really off slab. No need for manual alignment */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002301 slab_size =
2302 cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 }
2304
2305 cachep->colour_off = cache_line_size();
2306 /* Offset must be a multiple of the alignment. */
2307 if (cachep->colour_off < align)
2308 cachep->colour_off = align;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002309 cachep->colour = left_over / cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 cachep->slab_size = slab_size;
2311 cachep->flags = flags;
2312 cachep->gfpflags = 0;
2313 if (flags & SLAB_CACHE_DMA)
2314 cachep->gfpflags |= GFP_DMA;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002315 cachep->buffer_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002317 if (flags & CFLGS_OFF_SLAB) {
Victor Fuscob2d55072005-09-10 00:26:36 -07002318 cachep->slabp_cache = kmem_find_general_cachep(slab_size, 0u);
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002319 /*
2320 * This is a possibility for one of the malloc_sizes caches.
2321 * But since we go off slab only for object size greater than
2322 * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
2323 * this should not happen at all.
2324 * But leave a BUG_ON for some lucky dude.
2325 */
2326 BUG_ON(!cachep->slabp_cache);
2327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 cachep->ctor = ctor;
2329 cachep->dtor = dtor;
2330 cachep->name = name;
2331
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002332 if (setup_cpu_cache(cachep)) {
2333 __kmem_cache_destroy(cachep);
2334 cachep = NULL;
2335 goto oops;
2336 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 /* cache setup completed, link it into the list */
2339 list_add(&cachep->next, &cache_chain);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002340oops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if (!cachep && (flags & SLAB_PANIC))
2342 panic("kmem_cache_create(): failed to create slab `%s'\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002343 name);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002344 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 return cachep;
2346}
2347EXPORT_SYMBOL(kmem_cache_create);
2348
2349#if DEBUG
2350static void check_irq_off(void)
2351{
2352 BUG_ON(!irqs_disabled());
2353}
2354
2355static void check_irq_on(void)
2356{
2357 BUG_ON(irqs_disabled());
2358}
2359
Pekka Enberg343e0d72006-02-01 03:05:50 -08002360static void check_spinlock_acquired(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
2362#ifdef CONFIG_SMP
2363 check_irq_off();
Christoph Lametere498be72005-09-09 13:03:32 -07002364 assert_spin_locked(&cachep->nodelists[numa_node_id()]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365#endif
2366}
Christoph Lametere498be72005-09-09 13:03:32 -07002367
Pekka Enberg343e0d72006-02-01 03:05:50 -08002368static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07002369{
2370#ifdef CONFIG_SMP
2371 check_irq_off();
2372 assert_spin_locked(&cachep->nodelists[node]->list_lock);
2373#endif
2374}
2375
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376#else
2377#define check_irq_off() do { } while(0)
2378#define check_irq_on() do { } while(0)
2379#define check_spinlock_acquired(x) do { } while(0)
Christoph Lametere498be72005-09-09 13:03:32 -07002380#define check_spinlock_acquired_node(x, y) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381#endif
2382
Christoph Lameteraab22072006-03-22 00:09:06 -08002383static void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
2384 struct array_cache *ac,
2385 int force, int node);
2386
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387static void do_drain(void *arg)
2388{
Andrew Mortona737b3e2006-03-22 00:08:11 -08002389 struct kmem_cache *cachep = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 struct array_cache *ac;
Christoph Lameterff694162005-09-22 21:44:02 -07002391 int node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
2393 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002394 ac = cpu_cache_get(cachep);
Christoph Lameterff694162005-09-22 21:44:02 -07002395 spin_lock(&cachep->nodelists[node]->list_lock);
2396 free_block(cachep, ac->entry, ac->avail, node);
2397 spin_unlock(&cachep->nodelists[node]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 ac->avail = 0;
2399}
2400
Pekka Enberg343e0d72006-02-01 03:05:50 -08002401static void drain_cpu_caches(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402{
Christoph Lametere498be72005-09-09 13:03:32 -07002403 struct kmem_list3 *l3;
2404 int node;
2405
Andrew Mortona07fa392006-03-22 00:08:17 -08002406 on_each_cpu(do_drain, cachep, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 check_irq_on();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002408 for_each_online_node(node) {
Christoph Lametere498be72005-09-09 13:03:32 -07002409 l3 = cachep->nodelists[node];
Roland Dreiera4523a82006-05-15 11:41:00 -07002410 if (l3 && l3->alien)
2411 drain_alien_cache(cachep, l3->alien);
2412 }
2413
2414 for_each_online_node(node) {
2415 l3 = cachep->nodelists[node];
2416 if (l3)
Christoph Lameteraab22072006-03-22 00:09:06 -08002417 drain_array(cachep, l3, l3->shared, 1, node);
Christoph Lametere498be72005-09-09 13:03:32 -07002418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419}
2420
Christoph Lametered11d9e2006-06-30 01:55:45 -07002421/*
2422 * Remove slabs from the list of free slabs.
2423 * Specify the number of slabs to drain in tofree.
2424 *
2425 * Returns the actual number of slabs released.
2426 */
2427static int drain_freelist(struct kmem_cache *cache,
2428 struct kmem_list3 *l3, int tofree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429{
Christoph Lametered11d9e2006-06-30 01:55:45 -07002430 struct list_head *p;
2431 int nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433
Christoph Lametered11d9e2006-06-30 01:55:45 -07002434 nr_freed = 0;
2435 while (nr_freed < tofree && !list_empty(&l3->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
Christoph Lametered11d9e2006-06-30 01:55:45 -07002437 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002438 p = l3->slabs_free.prev;
Christoph Lametered11d9e2006-06-30 01:55:45 -07002439 if (p == &l3->slabs_free) {
2440 spin_unlock_irq(&l3->list_lock);
2441 goto out;
2442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
Christoph Lametered11d9e2006-06-30 01:55:45 -07002444 slabp = list_entry(p, struct slab, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445#if DEBUG
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002446 BUG_ON(slabp->inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002447#endif
2448 list_del(&slabp->list);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002449 /*
2450 * Safe to drop the lock. The slab is no longer linked
2451 * to the cache.
2452 */
2453 l3->free_objects -= cache->num;
Christoph Lametere498be72005-09-09 13:03:32 -07002454 spin_unlock_irq(&l3->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002455 slab_destroy(cache, slabp);
2456 nr_freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 }
Christoph Lametered11d9e2006-06-30 01:55:45 -07002458out:
2459 return nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460}
2461
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002462/* Called with cache_chain_mutex held to protect against cpu hotplug */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002463static int __cache_shrink(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07002464{
2465 int ret = 0, i = 0;
2466 struct kmem_list3 *l3;
2467
2468 drain_cpu_caches(cachep);
2469
2470 check_irq_on();
2471 for_each_online_node(i) {
2472 l3 = cachep->nodelists[i];
Christoph Lametered11d9e2006-06-30 01:55:45 -07002473 if (!l3)
2474 continue;
2475
2476 drain_freelist(cachep, l3, l3->free_objects);
2477
2478 ret += !list_empty(&l3->slabs_full) ||
2479 !list_empty(&l3->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07002480 }
2481 return (ret ? 1 : 0);
2482}
2483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484/**
2485 * kmem_cache_shrink - Shrink a cache.
2486 * @cachep: The cache to shrink.
2487 *
2488 * Releases as many slabs as possible for a cache.
2489 * To help debugging, a zero exit status indicates all slabs were released.
2490 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002491int kmem_cache_shrink(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492{
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002493 int ret;
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002494 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002496 mutex_lock(&cache_chain_mutex);
2497 ret = __cache_shrink(cachep);
2498 mutex_unlock(&cache_chain_mutex);
2499 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500}
2501EXPORT_SYMBOL(kmem_cache_shrink);
2502
2503/**
2504 * kmem_cache_destroy - delete a cache
2505 * @cachep: the cache to destroy
2506 *
Pekka Enberg343e0d72006-02-01 03:05:50 -08002507 * Remove a struct kmem_cache object from the slab cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508 *
2509 * It is expected this function will be called by a module when it is
2510 * unloaded. This will remove the cache completely, and avoid a duplicate
2511 * cache being allocated each time a module is loaded and unloaded, if the
2512 * module doesn't have persistent in-kernel storage across loads and unloads.
2513 *
2514 * The cache must be empty before calling this function.
2515 *
2516 * The caller must guarantee that noone will allocate memory from the cache
2517 * during the kmem_cache_destroy().
2518 */
Alexey Dobriyan133d2052006-09-27 01:49:41 -07002519void kmem_cache_destroy(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520{
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002521 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 /* Find the cache in the chain of caches. */
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002524 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002525 /*
2526 * the chain is never empty, cache_cache is never destroyed
2527 */
2528 list_del(&cachep->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 if (__cache_shrink(cachep)) {
2530 slab_error(cachep, "Can't free all objects");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002531 list_add(&cachep->next, &cache_chain);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002532 mutex_unlock(&cache_chain_mutex);
Alexey Dobriyan133d2052006-09-27 01:49:41 -07002533 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 }
2535
2536 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU))
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002537 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
Christoph Lameter117f6eb2006-09-25 23:31:37 -07002539 __kmem_cache_destroy(cachep);
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002540 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541}
2542EXPORT_SYMBOL(kmem_cache_destroy);
2543
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002544/*
2545 * Get the memory for a slab management obj.
2546 * For a slab cache when the slab descriptor is off-slab, slab descriptors
2547 * always come from malloc_sizes caches. The slab descriptor cannot
2548 * come from the same cache which is getting created because,
2549 * when we are searching for an appropriate cache for these
2550 * descriptors in kmem_cache_create, we search through the malloc_sizes array.
2551 * If we are creating a malloc_sizes cache here it would not be visible to
2552 * kmem_find_general_cachep till the initialization is complete.
2553 * Hence we cannot have slabp_cache same as the original cache.
2554 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002555static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002556 int colour_off, gfp_t local_flags,
2557 int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558{
2559 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002560
Linus Torvalds1da177e2005-04-16 15:20:36 -07002561 if (OFF_SLAB(cachep)) {
2562 /* Slab management obj is off-slab. */
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002563 slabp = kmem_cache_alloc_node(cachep->slabp_cache,
Christoph Lameter3c517a62006-12-06 20:33:29 -08002564 local_flags & ~GFP_THISNODE, nodeid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 if (!slabp)
2566 return NULL;
2567 } else {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002568 slabp = objp + colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 colour_off += cachep->slab_size;
2570 }
2571 slabp->inuse = 0;
2572 slabp->colouroff = colour_off;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002573 slabp->s_mem = objp + colour_off;
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002574 slabp->nodeid = nodeid;
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,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002584 struct slab *slabp, unsigned long ctor_flags)
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))
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002607 cachep->ctor(objp + obj_offset(cachep), cachep,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002608 ctor_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610 if (cachep->flags & SLAB_RED_ZONE) {
2611 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2612 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002613 " end of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2615 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002616 " start of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 }
Andrew Mortona737b3e2006-03-22 00:08:11 -08002618 if ((cachep->buffer_size % PAGE_SIZE) == 0 &&
2619 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002620 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002621 cachep->buffer_size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622#else
2623 if (cachep->ctor)
2624 cachep->ctor(objp, cachep, ctor_flags);
2625#endif
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002626 slab_bufctl(slabp)[i] = i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002628 slab_bufctl(slabp)[i - 1] = BUFCTL_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 slabp->free = 0;
2630}
2631
Pekka Enberg343e0d72006-02-01 03:05:50 -08002632static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633{
Christoph Lameter441e1432006-12-06 20:33:19 -08002634 if (flags & GFP_DMA)
Andrew Mortona737b3e2006-03-22 00:08:11 -08002635 BUG_ON(!(cachep->gfpflags & GFP_DMA));
2636 else
2637 BUG_ON(cachep->gfpflags & GFP_DMA);
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;
2711 unsigned long ctor_flags;
Christoph Lametere498be72005-09-09 13:03:32 -07002712 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Andrew Mortona737b3e2006-03-22 00:08:11 -08002714 /*
2715 * Be lazy and only check for valid flags here, keeping it out of the
2716 * critical path in kmem_cache_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 */
Christoph Lameter441e1432006-12-06 20:33:19 -08002718 BUG_ON(flags & ~(GFP_DMA | GFP_LEVEL_MASK | __GFP_NO_GROW));
Christoph Lameter6e0eaa42006-12-06 20:33:10 -08002719 if (flags & __GFP_NO_GROW)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 return 0;
2721
2722 ctor_flags = SLAB_CTOR_CONSTRUCTOR;
Christoph Lametera06d72c2006-12-06 20:33:12 -08002723 local_flags = (flags & GFP_LEVEL_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 if (!(local_flags & __GFP_WAIT))
2725 /*
2726 * Not allowed to sleep. Need to tell a constructor about
2727 * this - it might need to know...
2728 */
2729 ctor_flags |= SLAB_CTOR_ATOMIC;
2730
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002731 /* Take the l3 list lock to change the colour_next on this node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 check_irq_off();
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002733 l3 = cachep->nodelists[nodeid];
2734 spin_lock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735
2736 /* Get colour for the slab, and cal the next value. */
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002737 offset = l3->colour_next;
2738 l3->colour_next++;
2739 if (l3->colour_next >= cachep->colour)
2740 l3->colour_next = 0;
2741 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002743 offset *= cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
2745 if (local_flags & __GFP_WAIT)
2746 local_irq_enable();
2747
2748 /*
2749 * The test for missing atomic flag is performed here, rather than
2750 * the more obvious place, simply to reduce the critical path length
2751 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2752 * will eventually be caught here (where it matters).
2753 */
2754 kmem_flagcheck(cachep, flags);
2755
Andrew Mortona737b3e2006-03-22 00:08:11 -08002756 /*
2757 * Get mem for the objs. Attempt to allocate a physical page from
2758 * 'nodeid'.
Christoph Lametere498be72005-09-09 13:03:32 -07002759 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002760 if (!objp)
2761 objp = kmem_getpages(cachep, flags, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002762 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 goto failed;
2764
2765 /* Get slab management. */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002766 slabp = alloc_slabmgmt(cachep, objp, offset,
2767 local_flags & ~GFP_THISNODE, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002768 if (!slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 goto opps1;
2770
Christoph Lametere498be72005-09-09 13:03:32 -07002771 slabp->nodeid = nodeid;
Pekka Enberg47768742006-06-23 02:03:07 -07002772 slab_map_pages(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
2774 cache_init_objs(cachep, slabp, ctor_flags);
2775
2776 if (local_flags & __GFP_WAIT)
2777 local_irq_disable();
2778 check_irq_off();
Christoph Lametere498be72005-09-09 13:03:32 -07002779 spin_lock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
2781 /* Make slab active. */
Christoph Lametere498be72005-09-09 13:03:32 -07002782 list_add_tail(&slabp->list, &(l3->slabs_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 STATS_INC_GROWN(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07002784 l3->free_objects += cachep->num;
2785 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002787opps1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 kmem_freepages(cachep, objp);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002789failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 if (local_flags & __GFP_WAIT)
2791 local_irq_disable();
2792 return 0;
2793}
2794
2795#if DEBUG
2796
2797/*
2798 * Perform extra freeing checks:
2799 * - detect bad pointers.
2800 * - POISON/RED_ZONE checking
2801 * - destructor calls, for caches with POISON+dtor
2802 */
2803static void kfree_debugcheck(const void *objp)
2804{
2805 struct page *page;
2806
2807 if (!virt_addr_valid(objp)) {
2808 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002809 (unsigned long)objp);
2810 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 }
2812 page = virt_to_page(objp);
2813 if (!PageSlab(page)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002814 printk(KERN_ERR "kfree_debugcheck: bad ptr %lxh.\n",
2815 (unsigned long)objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 BUG();
2817 }
2818}
2819
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002820static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2821{
2822 unsigned long redzone1, redzone2;
2823
2824 redzone1 = *dbg_redzone1(cache, obj);
2825 redzone2 = *dbg_redzone2(cache, obj);
2826
2827 /*
2828 * Redzone is ok.
2829 */
2830 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2831 return;
2832
2833 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2834 slab_error(cache, "double free detected");
2835 else
2836 slab_error(cache, "memory outside object was overwritten");
2837
2838 printk(KERN_ERR "%p: redzone 1:0x%lx, redzone 2:0x%lx.\n",
2839 obj, redzone1, redzone2);
2840}
2841
Pekka Enberg343e0d72006-02-01 03:05:50 -08002842static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002843 void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844{
2845 struct page *page;
2846 unsigned int objnr;
2847 struct slab *slabp;
2848
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002849 objp -= obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 kfree_debugcheck(objp);
2851 page = virt_to_page(objp);
2852
Pekka Enberg065d41c2005-11-13 16:06:46 -08002853 slabp = page_get_slab(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854
2855 if (cachep->flags & SLAB_RED_ZONE) {
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002856 verify_redzone_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2858 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2859 }
2860 if (cachep->flags & SLAB_STORE_USER)
2861 *dbg_userword(cachep, objp) = caller;
2862
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002863 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
2865 BUG_ON(objnr >= cachep->num);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002866 BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
2868 if (cachep->flags & SLAB_DEBUG_INITIAL) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002869 /*
2870 * Need to call the slab's constructor so the caller can
2871 * perform a verify of its state (debugging). Called without
2872 * the cache-lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 */
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002874 cachep->ctor(objp + obj_offset(cachep),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002875 cachep, SLAB_CTOR_CONSTRUCTOR | SLAB_CTOR_VERIFY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 }
2877 if (cachep->flags & SLAB_POISON && cachep->dtor) {
2878 /* we want to cache poison the object,
2879 * call the destruction callback
2880 */
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002881 cachep->dtor(objp + obj_offset(cachep), cachep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 }
Al Viro871751e2006-03-25 03:06:39 -08002883#ifdef CONFIG_DEBUG_SLAB_LEAK
2884 slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
2885#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 if (cachep->flags & SLAB_POISON) {
2887#ifdef CONFIG_DEBUG_PAGEALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -08002888 if ((cachep->buffer_size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 store_stackinfo(cachep, objp, (unsigned long)caller);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002890 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002891 cachep->buffer_size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 } else {
2893 poison_obj(cachep, objp, POISON_FREE);
2894 }
2895#else
2896 poison_obj(cachep, objp, POISON_FREE);
2897#endif
2898 }
2899 return objp;
2900}
2901
Pekka Enberg343e0d72006-02-01 03:05:50 -08002902static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903{
2904 kmem_bufctl_t i;
2905 int entries = 0;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 /* Check slab's freelist to see if this obj is there. */
2908 for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
2909 entries++;
2910 if (entries > cachep->num || i >= cachep->num)
2911 goto bad;
2912 }
2913 if (entries != cachep->num - slabp->inuse) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002914bad:
2915 printk(KERN_ERR "slab: Internal list corruption detected in "
2916 "cache '%s'(%d), slabp %p(%d). Hexdump:\n",
2917 cachep->name, cachep->num, slabp, slabp->inuse);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002918 for (i = 0;
Linus Torvalds264132b2006-03-06 12:10:07 -08002919 i < sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002920 i++) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002921 if (i % 16 == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 printk("\n%03x:", i);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002923 printk(" %02x", ((unsigned char *)slabp)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 }
2925 printk("\n");
2926 BUG();
2927 }
2928}
2929#else
2930#define kfree_debugcheck(x) do { } while(0)
2931#define cache_free_debugcheck(x,objp,z) (objp)
2932#define check_slabp(x,y) do { } while(0)
2933#endif
2934
Pekka Enberg343e0d72006-02-01 03:05:50 -08002935static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936{
2937 int batchcount;
2938 struct kmem_list3 *l3;
2939 struct array_cache *ac;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002940 int node;
2941
2942 node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
2944 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002945 ac = cpu_cache_get(cachep);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002946retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 batchcount = ac->batchcount;
2948 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002949 /*
2950 * If there was little recent activity on this cache, then
2951 * perform only a partial refill. Otherwise we could generate
2952 * refill bouncing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 */
2954 batchcount = BATCHREFILL_LIMIT;
2955 }
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002956 l3 = cachep->nodelists[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957
Christoph Lametere498be72005-09-09 13:03:32 -07002958 BUG_ON(ac->avail > 0 || !l3);
2959 spin_lock(&l3->list_lock);
2960
Christoph Lameter3ded1752006-03-25 03:06:44 -08002961 /* See if we can refill from the shared array */
2962 if (l3->shared && transfer_objects(ac, l3->shared, batchcount))
2963 goto alloc_done;
2964
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 while (batchcount > 0) {
2966 struct list_head *entry;
2967 struct slab *slabp;
2968 /* Get slab alloc is to come from. */
2969 entry = l3->slabs_partial.next;
2970 if (entry == &l3->slabs_partial) {
2971 l3->free_touched = 1;
2972 entry = l3->slabs_free.next;
2973 if (entry == &l3->slabs_free)
2974 goto must_grow;
2975 }
2976
2977 slabp = list_entry(entry, struct slab, list);
2978 check_slabp(cachep, slabp);
2979 check_spinlock_acquired(cachep);
2980 while (slabp->inuse < cachep->num && batchcount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 STATS_INC_ALLOCED(cachep);
2982 STATS_INC_ACTIVE(cachep);
2983 STATS_SET_HIGH(cachep);
2984
Matthew Dobson78d382d2006-02-01 03:05:47 -08002985 ac->entry[ac->avail++] = slab_get_obj(cachep, slabp,
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002986 node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 }
2988 check_slabp(cachep, slabp);
2989
2990 /* move slabp to correct slabp list: */
2991 list_del(&slabp->list);
2992 if (slabp->free == BUFCTL_END)
2993 list_add(&slabp->list, &l3->slabs_full);
2994 else
2995 list_add(&slabp->list, &l3->slabs_partial);
2996 }
2997
Andrew Mortona737b3e2006-03-22 00:08:11 -08002998must_grow:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 l3->free_objects -= ac->avail;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003000alloc_done:
Christoph Lametere498be72005-09-09 13:03:32 -07003001 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002
3003 if (unlikely(!ac->avail)) {
3004 int x;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003005 x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07003006
Andrew Mortona737b3e2006-03-22 00:08:11 -08003007 /* cache_grow can reenable interrupts, then ac could change. */
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003008 ac = cpu_cache_get(cachep);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003009 if (!x && ac->avail == 0) /* no objects in sight? abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 return NULL;
3011
Andrew Mortona737b3e2006-03-22 00:08:11 -08003012 if (!ac->avail) /* objects refilled by interrupt? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 goto retry;
3014 }
3015 ac->touched = 1;
Christoph Lametere498be72005-09-09 13:03:32 -07003016 return ac->entry[--ac->avail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017}
3018
Andrew Mortona737b3e2006-03-22 00:08:11 -08003019static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
3020 gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021{
3022 might_sleep_if(flags & __GFP_WAIT);
3023#if DEBUG
3024 kmem_flagcheck(cachep, flags);
3025#endif
3026}
3027
3028#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003029static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
3030 gfp_t flags, void *objp, void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003032 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 return objp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003034 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035#ifdef CONFIG_DEBUG_PAGEALLOC
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003036 if ((cachep->buffer_size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003037 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003038 cachep->buffer_size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 else
3040 check_poison_obj(cachep, objp);
3041#else
3042 check_poison_obj(cachep, objp);
3043#endif
3044 poison_obj(cachep, objp, POISON_INUSE);
3045 }
3046 if (cachep->flags & SLAB_STORE_USER)
3047 *dbg_userword(cachep, objp) = caller;
3048
3049 if (cachep->flags & SLAB_RED_ZONE) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003050 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
3051 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
3052 slab_error(cachep, "double free, or memory outside"
3053 " object was overwritten");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003054 printk(KERN_ERR
Andrew Mortona737b3e2006-03-22 00:08:11 -08003055 "%p: redzone 1:0x%lx, redzone 2:0x%lx\n",
3056 objp, *dbg_redzone1(cachep, objp),
3057 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 }
3059 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
3060 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
3061 }
Al Viro871751e2006-03-25 03:06:39 -08003062#ifdef CONFIG_DEBUG_SLAB_LEAK
3063 {
3064 struct slab *slabp;
3065 unsigned objnr;
3066
3067 slabp = page_get_slab(virt_to_page(objp));
3068 objnr = (unsigned)(objp - slabp->s_mem) / cachep->buffer_size;
3069 slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE;
3070 }
3071#endif
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003072 objp += obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 if (cachep->ctor && cachep->flags & SLAB_POISON) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003074 unsigned long ctor_flags = SLAB_CTOR_CONSTRUCTOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075
3076 if (!(flags & __GFP_WAIT))
3077 ctor_flags |= SLAB_CTOR_ATOMIC;
3078
3079 cachep->ctor(objp, cachep, ctor_flags);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003080 }
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003081#if ARCH_SLAB_MINALIGN
3082 if ((u32)objp & (ARCH_SLAB_MINALIGN-1)) {
3083 printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
3084 objp, ARCH_SLAB_MINALIGN);
3085 }
3086#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 return objp;
3088}
3089#else
3090#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
3091#endif
3092
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003093#ifdef CONFIG_FAILSLAB
3094
3095static struct failslab_attr {
3096
3097 struct fault_attr attr;
3098
3099 u32 ignore_gfp_wait;
3100#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
3101 struct dentry *ignore_gfp_wait_file;
3102#endif
3103
3104} failslab = {
3105 .attr = FAULT_ATTR_INITIALIZER,
Don Mullis6b1b60f2006-12-08 02:39:53 -08003106 .ignore_gfp_wait = 1,
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003107};
3108
3109static int __init setup_failslab(char *str)
3110{
3111 return setup_fault_attr(&failslab.attr, str);
3112}
3113__setup("failslab=", setup_failslab);
3114
3115static int should_failslab(struct kmem_cache *cachep, gfp_t flags)
3116{
3117 if (cachep == &cache_cache)
3118 return 0;
3119 if (flags & __GFP_NOFAIL)
3120 return 0;
3121 if (failslab.ignore_gfp_wait && (flags & __GFP_WAIT))
3122 return 0;
3123
3124 return should_fail(&failslab.attr, obj_size(cachep));
3125}
3126
3127#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
3128
3129static int __init failslab_debugfs(void)
3130{
3131 mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
3132 struct dentry *dir;
3133 int err;
3134
3135 err = init_fault_attr_dentries(&failslab.attr, "failslab");
3136 if (err)
3137 return err;
3138 dir = failslab.attr.dentries.dir;
3139
3140 failslab.ignore_gfp_wait_file =
3141 debugfs_create_bool("ignore-gfp-wait", mode, dir,
3142 &failslab.ignore_gfp_wait);
3143
3144 if (!failslab.ignore_gfp_wait_file) {
3145 err = -ENOMEM;
3146 debugfs_remove(failslab.ignore_gfp_wait_file);
3147 cleanup_fault_attr_dentries(&failslab.attr);
3148 }
3149
3150 return err;
3151}
3152
3153late_initcall(failslab_debugfs);
3154
3155#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
3156
3157#else /* CONFIG_FAILSLAB */
3158
3159static inline int should_failslab(struct kmem_cache *cachep, gfp_t flags)
3160{
3161 return 0;
3162}
3163
3164#endif /* CONFIG_FAILSLAB */
3165
Pekka Enberg343e0d72006-02-01 03:05:50 -08003166static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003168 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 struct array_cache *ac;
3170
Alok N Kataria5c382302005-09-27 21:45:46 -07003171 check_irq_off();
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003172
3173 if (should_failslab(cachep, flags))
3174 return NULL;
3175
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003176 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 if (likely(ac->avail)) {
3178 STATS_INC_ALLOCHIT(cachep);
3179 ac->touched = 1;
Christoph Lametere498be72005-09-09 13:03:32 -07003180 objp = ac->entry[--ac->avail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181 } else {
3182 STATS_INC_ALLOCMISS(cachep);
3183 objp = cache_alloc_refill(cachep, flags);
3184 }
Alok N Kataria5c382302005-09-27 21:45:46 -07003185 return objp;
3186}
3187
Andrew Mortona737b3e2006-03-22 00:08:11 -08003188static __always_inline void *__cache_alloc(struct kmem_cache *cachep,
3189 gfp_t flags, void *caller)
Alok N Kataria5c382302005-09-27 21:45:46 -07003190{
3191 unsigned long save_flags;
Christoph Lameterde3083e2006-09-27 01:50:03 -07003192 void *objp = NULL;
Alok N Kataria5c382302005-09-27 21:45:46 -07003193
3194 cache_alloc_debugcheck_before(cachep, flags);
3195
3196 local_irq_save(save_flags);
Christoph Lameterde3083e2006-09-27 01:50:03 -07003197
Christoph Lameter765c4502006-09-27 01:50:08 -07003198 if (unlikely(NUMA_BUILD &&
3199 current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY)))
Christoph Lameterde3083e2006-09-27 01:50:03 -07003200 objp = alternate_node_alloc(cachep, flags);
Christoph Lameterde3083e2006-09-27 01:50:03 -07003201
3202 if (!objp)
3203 objp = ____cache_alloc(cachep, flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003204 /*
3205 * We may just have run out of memory on the local node.
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003206 * ____cache_alloc_node() knows how to locate memory on other nodes
Christoph Lameter765c4502006-09-27 01:50:08 -07003207 */
3208 if (NUMA_BUILD && !objp)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003209 objp = ____cache_alloc_node(cachep, flags, numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 local_irq_restore(save_flags);
Eric Dumazet34342e82005-09-03 15:55:06 -07003211 objp = cache_alloc_debugcheck_after(cachep, flags, objp,
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003212 caller);
Eric Dumazet34342e82005-09-03 15:55:06 -07003213 prefetchw(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214 return objp;
3215}
3216
Christoph Lametere498be72005-09-09 13:03:32 -07003217#ifdef CONFIG_NUMA
3218/*
Paul Jacksonb2455392006-03-24 03:16:12 -08003219 * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
Paul Jacksonc61afb12006-03-24 03:16:08 -08003220 *
3221 * If we are in_interrupt, then process context, including cpusets and
3222 * mempolicy, may not apply and should not be used for allocation policy.
3223 */
3224static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
3225{
3226 int nid_alloc, nid_here;
3227
Christoph Lameter765c4502006-09-27 01:50:08 -07003228 if (in_interrupt() || (flags & __GFP_THISNODE))
Paul Jacksonc61afb12006-03-24 03:16:08 -08003229 return NULL;
3230 nid_alloc = nid_here = numa_node_id();
3231 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
3232 nid_alloc = cpuset_mem_spread_node();
3233 else if (current->mempolicy)
3234 nid_alloc = slab_node(current->mempolicy);
3235 if (nid_alloc != nid_here)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003236 return ____cache_alloc_node(cachep, flags, nid_alloc);
Paul Jacksonc61afb12006-03-24 03:16:08 -08003237 return NULL;
3238}
3239
3240/*
Christoph Lameter765c4502006-09-27 01:50:08 -07003241 * Fallback function if there was no memory available and no objects on a
Christoph Lameter3c517a62006-12-06 20:33:29 -08003242 * certain node and fall back is permitted. First we scan all the
3243 * available nodelists for available objects. If that fails then we
3244 * perform an allocation without specifying a node. This allows the page
3245 * allocator to do its reclaim / fallback magic. We then insert the
3246 * slab into the proper nodelist and then allocate from it.
Christoph Lameter765c4502006-09-27 01:50:08 -07003247 */
3248void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
3249{
3250 struct zonelist *zonelist = &NODE_DATA(slab_node(current->mempolicy))
3251 ->node_zonelists[gfp_zone(flags)];
3252 struct zone **z;
3253 void *obj = NULL;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003254 int nid;
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003255 gfp_t local_flags = (flags & GFP_LEVEL_MASK);
Christoph Lameter765c4502006-09-27 01:50:08 -07003256
Christoph Lameter3c517a62006-12-06 20:33:29 -08003257retry:
3258 /*
3259 * Look through allowed nodes for objects available
3260 * from existing per node queues.
3261 */
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003262 for (z = zonelist->zones; *z && !obj; z++) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003263 nid = zone_to_nid(*z);
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003264
Paul Jacksonb8b50b62006-12-08 02:35:53 -08003265 if (cpuset_zone_allowed(*z, flags | __GFP_HARDWALL) &&
Christoph Lameter3c517a62006-12-06 20:33:29 -08003266 cache->nodelists[nid] &&
3267 cache->nodelists[nid]->free_objects)
3268 obj = ____cache_alloc_node(cache,
3269 flags | GFP_THISNODE, nid);
3270 }
3271
3272 if (!obj) {
3273 /*
3274 * This allocation will be performed within the constraints
3275 * of the current cpuset / memory policy requirements.
3276 * We may trigger various forms of reclaim on the allowed
3277 * set and go into memory reserves if necessary.
3278 */
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003279 if (local_flags & __GFP_WAIT)
3280 local_irq_enable();
3281 kmem_flagcheck(cache, flags);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003282 obj = kmem_getpages(cache, flags, -1);
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003283 if (local_flags & __GFP_WAIT)
3284 local_irq_disable();
Christoph Lameter3c517a62006-12-06 20:33:29 -08003285 if (obj) {
3286 /*
3287 * Insert into the appropriate per node queues
3288 */
3289 nid = page_to_nid(virt_to_page(obj));
3290 if (cache_grow(cache, flags, nid, obj)) {
3291 obj = ____cache_alloc_node(cache,
3292 flags | GFP_THISNODE, nid);
3293 if (!obj)
3294 /*
3295 * Another processor may allocate the
3296 * objects in the slab since we are
3297 * not holding any locks.
3298 */
3299 goto retry;
3300 } else {
3301 kmem_freepages(cache, obj);
3302 obj = NULL;
3303 }
3304 }
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003305 }
Christoph Lameter765c4502006-09-27 01:50:08 -07003306 return obj;
3307}
3308
3309/*
Christoph Lametere498be72005-09-09 13:03:32 -07003310 * A interface to enable slab creation on nodeid
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003312static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003313 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07003314{
3315 struct list_head *entry;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003316 struct slab *slabp;
3317 struct kmem_list3 *l3;
3318 void *obj;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003319 int x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003321 l3 = cachep->nodelists[nodeid];
3322 BUG_ON(!l3);
Christoph Lametere498be72005-09-09 13:03:32 -07003323
Andrew Mortona737b3e2006-03-22 00:08:11 -08003324retry:
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003325 check_irq_off();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003326 spin_lock(&l3->list_lock);
3327 entry = l3->slabs_partial.next;
3328 if (entry == &l3->slabs_partial) {
3329 l3->free_touched = 1;
3330 entry = l3->slabs_free.next;
3331 if (entry == &l3->slabs_free)
3332 goto must_grow;
3333 }
Christoph Lametere498be72005-09-09 13:03:32 -07003334
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003335 slabp = list_entry(entry, struct slab, list);
3336 check_spinlock_acquired_node(cachep, nodeid);
3337 check_slabp(cachep, slabp);
Christoph Lametere498be72005-09-09 13:03:32 -07003338
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003339 STATS_INC_NODEALLOCS(cachep);
3340 STATS_INC_ACTIVE(cachep);
3341 STATS_SET_HIGH(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003342
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003343 BUG_ON(slabp->inuse == cachep->num);
Christoph Lametere498be72005-09-09 13:03:32 -07003344
Matthew Dobson78d382d2006-02-01 03:05:47 -08003345 obj = slab_get_obj(cachep, slabp, nodeid);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003346 check_slabp(cachep, slabp);
3347 l3->free_objects--;
3348 /* move slabp to correct slabp list: */
3349 list_del(&slabp->list);
Christoph Lametere498be72005-09-09 13:03:32 -07003350
Andrew Mortona737b3e2006-03-22 00:08:11 -08003351 if (slabp->free == BUFCTL_END)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003352 list_add(&slabp->list, &l3->slabs_full);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003353 else
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003354 list_add(&slabp->list, &l3->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07003355
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003356 spin_unlock(&l3->list_lock);
3357 goto done;
Christoph Lametere498be72005-09-09 13:03:32 -07003358
Andrew Mortona737b3e2006-03-22 00:08:11 -08003359must_grow:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003360 spin_unlock(&l3->list_lock);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003361 x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
Christoph Lameter765c4502006-09-27 01:50:08 -07003362 if (x)
3363 goto retry;
Christoph Lametere498be72005-09-09 13:03:32 -07003364
Christoph Lameter765c4502006-09-27 01:50:08 -07003365 if (!(flags & __GFP_THISNODE))
3366 /* Unable to grow the cache. Fall back to other nodes. */
3367 return fallback_alloc(cachep, flags);
Christoph Lametere498be72005-09-09 13:03:32 -07003368
Christoph Lameter765c4502006-09-27 01:50:08 -07003369 return NULL;
3370
Andrew Mortona737b3e2006-03-22 00:08:11 -08003371done:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003372 return obj;
Christoph Lametere498be72005-09-09 13:03:32 -07003373}
3374#endif
3375
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 Enberg6ed5eb2212006-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();
3483 objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));
3484
Ingo Molnar873623d2006-07-13 14:44:38 +02003485 if (cache_free_alien(cachep, objp))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07003486 return;
Christoph Lametere498be72005-09-09 13:03:32 -07003487
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 if (likely(ac->avail < ac->limit)) {
3489 STATS_INC_FREEHIT(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003490 ac->entry[ac->avail++] = objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 return;
3492 } else {
3493 STATS_INC_FREEMISS(cachep);
3494 cache_flusharray(cachep, ac);
Christoph Lametere498be72005-09-09 13:03:32 -07003495 ac->entry[ac->avail++] = objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 }
3497}
3498
3499/**
3500 * kmem_cache_alloc - Allocate an object
3501 * @cachep: The cache to allocate from.
3502 * @flags: See kmalloc().
3503 *
3504 * Allocate an object from this cache. The flags are only relevant
3505 * if the cache has no available objects.
3506 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003507void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508{
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003509 return __cache_alloc(cachep, flags, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510}
3511EXPORT_SYMBOL(kmem_cache_alloc);
3512
3513/**
Rolf Eike Beerb8008b22006-07-30 03:04:04 -07003514 * kmem_cache_zalloc - Allocate an object. The memory is set to zero.
Pekka Enberga8c0f9a2006-03-25 03:06:42 -08003515 * @cache: The cache to allocate from.
3516 * @flags: See kmalloc().
3517 *
3518 * Allocate an object from this cache and set the allocated memory to zero.
3519 * The flags are only relevant if the cache has no available objects.
3520 */
3521void *kmem_cache_zalloc(struct kmem_cache *cache, gfp_t flags)
3522{
3523 void *ret = __cache_alloc(cache, flags, __builtin_return_address(0));
3524 if (ret)
3525 memset(ret, 0, obj_size(cache));
3526 return ret;
3527}
3528EXPORT_SYMBOL(kmem_cache_zalloc);
3529
3530/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 * kmem_ptr_validate - check if an untrusted pointer might
3532 * be a slab entry.
3533 * @cachep: the cache we're checking against
3534 * @ptr: pointer to validate
3535 *
3536 * This verifies that the untrusted pointer looks sane:
3537 * it is _not_ a guarantee that the pointer is actually
3538 * part of the slab cache in question, but it at least
3539 * validates that the pointer can be dereferenced and
3540 * looks half-way sane.
3541 *
3542 * Currently only used for dentry validation.
3543 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003544int fastcall kmem_ptr_validate(struct kmem_cache *cachep, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003546 unsigned long addr = (unsigned long)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 unsigned long min_addr = PAGE_OFFSET;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003548 unsigned long align_mask = BYTES_PER_WORD - 1;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003549 unsigned long size = cachep->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550 struct page *page;
3551
3552 if (unlikely(addr < min_addr))
3553 goto out;
3554 if (unlikely(addr > (unsigned long)high_memory - size))
3555 goto out;
3556 if (unlikely(addr & align_mask))
3557 goto out;
3558 if (unlikely(!kern_addr_valid(addr)))
3559 goto out;
3560 if (unlikely(!kern_addr_valid(addr + size - 1)))
3561 goto out;
3562 page = virt_to_page(ptr);
3563 if (unlikely(!PageSlab(page)))
3564 goto out;
Pekka Enberg065d41c2005-11-13 16:06:46 -08003565 if (unlikely(page_get_cache(page) != cachep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 goto out;
3567 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003568out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 return 0;
3570}
3571
3572#ifdef CONFIG_NUMA
3573/**
3574 * kmem_cache_alloc_node - Allocate an object on the specified node
3575 * @cachep: The cache to allocate from.
3576 * @flags: See kmalloc().
3577 * @nodeid: node number of the target node.
3578 *
Christoph Lameter5bcd2342006-12-06 20:33:24 -08003579 * Identical to kmem_cache_alloc but it will allocate memory on the given
3580 * node, which can improve the performance for cpu bound structures.
3581 *
3582 * Fallback to other node is possible if __GFP_THISNODE is not set.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003584static __always_inline void *
3585__cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
3586 int nodeid, void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
Christoph Lametere498be72005-09-09 13:03:32 -07003588 unsigned long save_flags;
Christoph Lameter5bcd2342006-12-06 20:33:24 -08003589 void *ptr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
Christoph Lametere498be72005-09-09 13:03:32 -07003591 cache_alloc_debugcheck_before(cachep, flags);
3592 local_irq_save(save_flags);
Christoph Lameter18f820f2006-02-01 03:05:43 -08003593
Christoph Lameter5bcd2342006-12-06 20:33:24 -08003594 if (unlikely(nodeid == -1))
3595 nodeid = numa_node_id();
Christoph Lameter18f820f2006-02-01 03:05:43 -08003596
Christoph Lameter5bcd2342006-12-06 20:33:24 -08003597 if (likely(cachep->nodelists[nodeid])) {
3598 if (nodeid == numa_node_id()) {
3599 /*
3600 * Use the locally cached objects if possible.
3601 * However ____cache_alloc does not allow fallback
3602 * to other nodes. It may fail while we still have
3603 * objects on other nodes available.
3604 */
3605 ptr = ____cache_alloc(cachep, flags);
3606 }
3607 if (!ptr) {
3608 /* ___cache_alloc_node can fall back to other nodes */
3609 ptr = ____cache_alloc_node(cachep, flags, nodeid);
3610 }
3611 } else {
3612 /* Node not bootstrapped yet */
3613 if (!(flags & __GFP_THISNODE))
3614 ptr = fallback_alloc(cachep, flags);
3615 }
3616
3617 local_irq_restore(save_flags);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003618 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619
Christoph Lametere498be72005-09-09 13:03:32 -07003620 return ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621}
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003622
3623void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
3624{
3625 return __cache_alloc_node(cachep, flags, nodeid,
3626 __builtin_return_address(0));
3627}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628EXPORT_SYMBOL(kmem_cache_alloc_node);
3629
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003630static __always_inline void *
3631__do_kmalloc_node(size_t size, gfp_t flags, int node, void *caller)
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003632{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003633 struct kmem_cache *cachep;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003634
3635 cachep = kmem_find_general_cachep(size, flags);
3636 if (unlikely(cachep == NULL))
3637 return NULL;
3638 return kmem_cache_alloc_node(cachep, flags, node);
3639}
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003640
3641#ifdef CONFIG_DEBUG_SLAB
3642void *__kmalloc_node(size_t size, gfp_t flags, int node)
3643{
3644 return __do_kmalloc_node(size, flags, node,
3645 __builtin_return_address(0));
3646}
Christoph Hellwigdbe5e692006-09-25 23:31:36 -07003647EXPORT_SYMBOL(__kmalloc_node);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003648
3649void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
3650 int node, void *caller)
3651{
3652 return __do_kmalloc_node(size, flags, node, caller);
3653}
3654EXPORT_SYMBOL(__kmalloc_node_track_caller);
3655#else
3656void *__kmalloc_node(size_t size, gfp_t flags, int node)
3657{
3658 return __do_kmalloc_node(size, flags, node, NULL);
3659}
3660EXPORT_SYMBOL(__kmalloc_node);
3661#endif /* CONFIG_DEBUG_SLAB */
3662#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
3664/**
Paul Drynoff800590f2006-06-23 02:03:48 -07003665 * __do_kmalloc - allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 * @size: how many bytes of memory are required.
Paul Drynoff800590f2006-06-23 02:03:48 -07003667 * @flags: the type of memory to allocate (see kmalloc).
Randy Dunlap911851e2006-03-22 00:08:14 -08003668 * @caller: function caller for debug tracking of the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669 */
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003670static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
3671 void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003673 struct kmem_cache *cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003675 /* If you want to save a few bytes .text space: replace
3676 * __ with kmem_.
3677 * Then kmalloc uses the uninlined functions instead of the inline
3678 * functions.
3679 */
3680 cachep = __find_general_cachep(size, flags);
Andrew Mortondbdb9042005-09-23 13:24:10 -07003681 if (unlikely(cachep == NULL))
3682 return NULL;
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003683 return __cache_alloc(cachep, flags, caller);
3684}
3685
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003686
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003687#ifdef CONFIG_DEBUG_SLAB
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003688void *__kmalloc(size_t size, gfp_t flags)
3689{
Al Viro871751e2006-03-25 03:06:39 -08003690 return __do_kmalloc(size, flags, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691}
3692EXPORT_SYMBOL(__kmalloc);
3693
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003694void *__kmalloc_track_caller(size_t size, gfp_t flags, void *caller)
3695{
3696 return __do_kmalloc(size, flags, caller);
3697}
3698EXPORT_SYMBOL(__kmalloc_track_caller);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003699
3700#else
3701void *__kmalloc(size_t size, gfp_t flags)
3702{
3703 return __do_kmalloc(size, flags, NULL);
3704}
3705EXPORT_SYMBOL(__kmalloc);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003706#endif
3707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708/**
3709 * kmem_cache_free - Deallocate an object
3710 * @cachep: The cache the allocation was from.
3711 * @objp: The previously allocated object.
3712 *
3713 * Free an object which was previously allocated from this
3714 * cache.
3715 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003716void kmem_cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717{
3718 unsigned long flags;
3719
Pekka Enbergddc2e812006-06-23 02:03:40 -07003720 BUG_ON(virt_to_cache(objp) != cachep);
3721
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 local_irq_save(flags);
Ingo Molnar873623d2006-07-13 14:44:38 +02003723 __cache_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 local_irq_restore(flags);
3725}
3726EXPORT_SYMBOL(kmem_cache_free);
3727
3728/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 * kfree - free previously allocated memory
3730 * @objp: pointer returned by kmalloc.
3731 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07003732 * If @objp is NULL, no operation is performed.
3733 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003734 * Don't free memory not originally allocated by kmalloc()
3735 * or you will run into trouble.
3736 */
3737void kfree(const void *objp)
3738{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003739 struct kmem_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 unsigned long flags;
3741
3742 if (unlikely(!objp))
3743 return;
3744 local_irq_save(flags);
3745 kfree_debugcheck(objp);
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003746 c = virt_to_cache(objp);
Ingo Molnarf9b84042006-06-27 02:54:49 -07003747 debug_check_no_locks_freed(objp, obj_size(c));
Ingo Molnar873623d2006-07-13 14:44:38 +02003748 __cache_free(c, (void *)objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 local_irq_restore(flags);
3750}
3751EXPORT_SYMBOL(kfree);
3752
Pekka Enberg343e0d72006-02-01 03:05:50 -08003753unsigned int kmem_cache_size(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003755 return obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003756}
3757EXPORT_SYMBOL(kmem_cache_size);
3758
Pekka Enberg343e0d72006-02-01 03:05:50 -08003759const char *kmem_cache_name(struct kmem_cache *cachep)
Arnaldo Carvalho de Melo19449722005-06-18 22:46:19 -07003760{
3761 return cachep->name;
3762}
3763EXPORT_SYMBOL_GPL(kmem_cache_name);
3764
Christoph Lametere498be72005-09-09 13:03:32 -07003765/*
Christoph Lameter0718dc22006-03-25 03:06:47 -08003766 * This initializes kmem_list3 or resizes varioius caches for all nodes.
Christoph Lametere498be72005-09-09 13:03:32 -07003767 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003768static int alloc_kmemlist(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07003769{
3770 int node;
3771 struct kmem_list3 *l3;
Christoph Lametercafeb022006-03-25 03:06:46 -08003772 struct array_cache *new_shared;
Paul Menage3395ee02006-12-06 20:32:16 -08003773 struct array_cache **new_alien = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003774
3775 for_each_online_node(node) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003776
Paul Menage3395ee02006-12-06 20:32:16 -08003777 if (use_alien_caches) {
3778 new_alien = alloc_alien_cache(node, cachep->limit);
3779 if (!new_alien)
3780 goto fail;
3781 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003782
Christoph Lameter0718dc22006-03-25 03:06:47 -08003783 new_shared = alloc_arraycache(node,
3784 cachep->shared*cachep->batchcount,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003785 0xbaadf00d);
Christoph Lameter0718dc22006-03-25 03:06:47 -08003786 if (!new_shared) {
3787 free_alien_cache(new_alien);
Christoph Lametere498be72005-09-09 13:03:32 -07003788 goto fail;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003789 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003790
Andrew Mortona737b3e2006-03-22 00:08:11 -08003791 l3 = cachep->nodelists[node];
3792 if (l3) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003793 struct array_cache *shared = l3->shared;
3794
Christoph Lametere498be72005-09-09 13:03:32 -07003795 spin_lock_irq(&l3->list_lock);
3796
Christoph Lametercafeb022006-03-25 03:06:46 -08003797 if (shared)
Christoph Lameter0718dc22006-03-25 03:06:47 -08003798 free_block(cachep, shared->entry,
3799 shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07003800
Christoph Lametercafeb022006-03-25 03:06:46 -08003801 l3->shared = new_shared;
3802 if (!l3->alien) {
Christoph Lametere498be72005-09-09 13:03:32 -07003803 l3->alien = new_alien;
3804 new_alien = NULL;
3805 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003806 l3->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003807 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07003808 spin_unlock_irq(&l3->list_lock);
Christoph Lametercafeb022006-03-25 03:06:46 -08003809 kfree(shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003810 free_alien_cache(new_alien);
3811 continue;
3812 }
Andrew Mortona737b3e2006-03-22 00:08:11 -08003813 l3 = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, node);
Christoph Lameter0718dc22006-03-25 03:06:47 -08003814 if (!l3) {
3815 free_alien_cache(new_alien);
3816 kfree(new_shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003817 goto fail;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003818 }
Christoph Lametere498be72005-09-09 13:03:32 -07003819
3820 kmem_list3_init(l3);
3821 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
Andrew Mortona737b3e2006-03-22 00:08:11 -08003822 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lametercafeb022006-03-25 03:06:46 -08003823 l3->shared = new_shared;
Christoph Lametere498be72005-09-09 13:03:32 -07003824 l3->alien = new_alien;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003825 l3->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003826 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07003827 cachep->nodelists[node] = l3;
3828 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003829 return 0;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003830
Andrew Mortona737b3e2006-03-22 00:08:11 -08003831fail:
Christoph Lameter0718dc22006-03-25 03:06:47 -08003832 if (!cachep->next.next) {
3833 /* Cache is not active yet. Roll back what we did */
3834 node--;
3835 while (node >= 0) {
3836 if (cachep->nodelists[node]) {
3837 l3 = cachep->nodelists[node];
3838
3839 kfree(l3->shared);
3840 free_alien_cache(l3->alien);
3841 kfree(l3);
3842 cachep->nodelists[node] = NULL;
3843 }
3844 node--;
3845 }
3846 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003847 return -ENOMEM;
Christoph Lametere498be72005-09-09 13:03:32 -07003848}
3849
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850struct ccupdate_struct {
Pekka Enberg343e0d72006-02-01 03:05:50 -08003851 struct kmem_cache *cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 struct array_cache *new[NR_CPUS];
3853};
3854
3855static void do_ccupdate_local(void *info)
3856{
Andrew Mortona737b3e2006-03-22 00:08:11 -08003857 struct ccupdate_struct *new = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858 struct array_cache *old;
3859
3860 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003861 old = cpu_cache_get(new->cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003862
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
3864 new->new[smp_processor_id()] = old;
3865}
3866
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -08003867/* Always called with the cache_chain_mutex held */
Andrew Mortona737b3e2006-03-22 00:08:11 -08003868static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
3869 int batchcount, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003870{
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003871 struct ccupdate_struct *new;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003872 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003874 new = kzalloc(sizeof(*new), GFP_KERNEL);
3875 if (!new)
3876 return -ENOMEM;
3877
Christoph Lametere498be72005-09-09 13:03:32 -07003878 for_each_online_cpu(i) {
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003879 new->new[i] = alloc_arraycache(cpu_to_node(i), limit,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003880 batchcount);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003881 if (!new->new[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003882 for (i--; i >= 0; i--)
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003883 kfree(new->new[i]);
3884 kfree(new);
Christoph Lametere498be72005-09-09 13:03:32 -07003885 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003886 }
3887 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003888 new->cachep = cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003890 on_each_cpu(do_ccupdate_local, (void *)new, 1, 1);
Christoph Lametere498be72005-09-09 13:03:32 -07003891
Linus Torvalds1da177e2005-04-16 15:20:36 -07003892 check_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 cachep->batchcount = batchcount;
3894 cachep->limit = limit;
Christoph Lametere498be72005-09-09 13:03:32 -07003895 cachep->shared = shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896
Christoph Lametere498be72005-09-09 13:03:32 -07003897 for_each_online_cpu(i) {
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003898 struct array_cache *ccold = new->new[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 if (!ccold)
3900 continue;
Christoph Lametere498be72005-09-09 13:03:32 -07003901 spin_lock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
Christoph Lameterff694162005-09-22 21:44:02 -07003902 free_block(cachep, ccold->entry, ccold->avail, cpu_to_node(i));
Christoph Lametere498be72005-09-09 13:03:32 -07003903 spin_unlock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 kfree(ccold);
3905 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003906 kfree(new);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003907 return alloc_kmemlist(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908}
3909
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -08003910/* Called with cache_chain_mutex held always */
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003911static int enable_cpucache(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912{
3913 int err;
3914 int limit, shared;
3915
Andrew Mortona737b3e2006-03-22 00:08:11 -08003916 /*
3917 * The head array serves three purposes:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 * - create a LIFO ordering, i.e. return objects that are cache-warm
3919 * - reduce the number of spinlock operations.
Andrew Mortona737b3e2006-03-22 00:08:11 -08003920 * - reduce the number of linked list operations on the slab and
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921 * bufctl chains: array operations are cheaper.
3922 * The numbers are guessed, we should auto-tune as described by
3923 * Bonwick.
3924 */
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003925 if (cachep->buffer_size > 131072)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 limit = 1;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003927 else if (cachep->buffer_size > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 limit = 8;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003929 else if (cachep->buffer_size > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930 limit = 24;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003931 else if (cachep->buffer_size > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 limit = 54;
3933 else
3934 limit = 120;
3935
Andrew Mortona737b3e2006-03-22 00:08:11 -08003936 /*
3937 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 * allocation behaviour: Most allocs on one cpu, most free operations
3939 * on another cpu. For these cases, an efficient object passing between
3940 * cpus is necessary. This is provided by a shared array. The array
3941 * replaces Bonwick's magazine layer.
3942 * On uniprocessor, it's functionally equivalent (but less efficient)
3943 * to a larger limit. Thus disabled by default.
3944 */
3945 shared = 0;
3946#ifdef CONFIG_SMP
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003947 if (cachep->buffer_size <= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 shared = 8;
3949#endif
3950
3951#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003952 /*
3953 * With debugging enabled, large batchcount lead to excessively long
3954 * periods with disabled local interrupts. Limit the batchcount
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 */
3956 if (limit > 32)
3957 limit = 32;
3958#endif
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003959 err = do_tune_cpucache(cachep, limit, (limit + 1) / 2, shared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 if (err)
3961 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003962 cachep->name, -err);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003963 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964}
3965
Christoph Lameter1b552532006-03-22 00:09:07 -08003966/*
3967 * Drain an array if it contains any elements taking the l3 lock only if
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003968 * necessary. Note that the l3 listlock also protects the array_cache
3969 * if drain_array() is used on the shared array.
Christoph Lameter1b552532006-03-22 00:09:07 -08003970 */
3971void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
3972 struct array_cache *ac, int force, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973{
3974 int tofree;
3975
Christoph Lameter1b552532006-03-22 00:09:07 -08003976 if (!ac || !ac->avail)
3977 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 if (ac->touched && !force) {
3979 ac->touched = 0;
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003980 } else {
Christoph Lameter1b552532006-03-22 00:09:07 -08003981 spin_lock_irq(&l3->list_lock);
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003982 if (ac->avail) {
3983 tofree = force ? ac->avail : (ac->limit + 4) / 5;
3984 if (tofree > ac->avail)
3985 tofree = (ac->avail + 1) / 2;
3986 free_block(cachep, ac->entry, tofree, node);
3987 ac->avail -= tofree;
3988 memmove(ac->entry, &(ac->entry[tofree]),
3989 sizeof(void *) * ac->avail);
3990 }
Christoph Lameter1b552532006-03-22 00:09:07 -08003991 spin_unlock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 }
3993}
3994
3995/**
3996 * cache_reap - Reclaim memory from caches.
Randy Dunlap1e5d5332005-11-07 01:01:06 -08003997 * @unused: unused parameter
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 *
3999 * Called from workqueue/eventd every few seconds.
4000 * Purpose:
4001 * - clear the per-cpu caches for this CPU.
4002 * - return freeable pages to the main free memory pool.
4003 *
Andrew Mortona737b3e2006-03-22 00:08:11 -08004004 * If we cannot acquire the cache chain mutex then just give up - we'll try
4005 * again on the next iteration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 */
David Howells65f27f32006-11-22 14:55:48 +00004007static void cache_reap(struct work_struct *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008{
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004009 struct kmem_cache *searchp;
Christoph Lametere498be72005-09-09 13:03:32 -07004010 struct kmem_list3 *l3;
Christoph Lameteraab22072006-03-22 00:09:06 -08004011 int node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004013 if (!mutex_trylock(&cache_chain_mutex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 /* Give up. Setup the next iteration. */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004015 schedule_delayed_work(&__get_cpu_var(reap_work),
Arjan van de Ven2b284212006-12-10 02:21:28 -08004016 round_jiffies_relative(REAPTIMEOUT_CPUC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 return;
4018 }
4019
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004020 list_for_each_entry(searchp, &cache_chain, next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021 check_irq_on();
4022
Christoph Lameter35386e32006-03-22 00:09:05 -08004023 /*
4024 * We only take the l3 lock if absolutely necessary and we
4025 * have established with reasonable certainty that
4026 * we can do some work if the lock was obtained.
4027 */
Christoph Lameteraab22072006-03-22 00:09:06 -08004028 l3 = searchp->nodelists[node];
Christoph Lameter35386e32006-03-22 00:09:05 -08004029
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004030 reap_alien(searchp, l3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031
Christoph Lameteraab22072006-03-22 00:09:06 -08004032 drain_array(searchp, l3, cpu_cache_get(searchp), 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033
Christoph Lameter35386e32006-03-22 00:09:05 -08004034 /*
4035 * These are racy checks but it does not matter
4036 * if we skip one check or scan twice.
4037 */
Christoph Lametere498be72005-09-09 13:03:32 -07004038 if (time_after(l3->next_reap, jiffies))
Christoph Lameter35386e32006-03-22 00:09:05 -08004039 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004040
Christoph Lametere498be72005-09-09 13:03:32 -07004041 l3->next_reap = jiffies + REAPTIMEOUT_LIST3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042
Christoph Lameteraab22072006-03-22 00:09:06 -08004043 drain_array(searchp, l3, l3->shared, 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
Christoph Lametered11d9e2006-06-30 01:55:45 -07004045 if (l3->free_touched)
Christoph Lametere498be72005-09-09 13:03:32 -07004046 l3->free_touched = 0;
Christoph Lametered11d9e2006-06-30 01:55:45 -07004047 else {
4048 int freed;
4049
4050 freed = drain_freelist(searchp, l3, (l3->free_limit +
4051 5 * searchp->num - 1) / (5 * searchp->num));
4052 STATS_ADD_REAPED(searchp, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 }
Christoph Lameter35386e32006-03-22 00:09:05 -08004054next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 cond_resched();
4056 }
4057 check_irq_on();
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004058 mutex_unlock(&cache_chain_mutex);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004059 next_reap_node();
Christoph Lameter2244b952006-06-30 01:55:33 -07004060 refresh_cpu_vm_stats(smp_processor_id());
Andrew Mortona737b3e2006-03-22 00:08:11 -08004061 /* Set up the next iteration */
Arjan van de Ven2b284212006-12-10 02:21:28 -08004062 schedule_delayed_work(&__get_cpu_var(reap_work),
4063 round_jiffies_relative(REAPTIMEOUT_CPUC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064}
4065
4066#ifdef CONFIG_PROC_FS
4067
Pekka Enberg85289f92006-01-08 01:00:36 -08004068static void print_slabinfo_header(struct seq_file *m)
4069{
4070 /*
4071 * Output format version, so at least we can change it
4072 * without _too_ many complaints.
4073 */
4074#if STATS
4075 seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
4076#else
4077 seq_puts(m, "slabinfo - version: 2.1\n");
4078#endif
4079 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
4080 "<objperslab> <pagesperslab>");
4081 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
4082 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
4083#if STATS
4084 seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004085 "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
Pekka Enberg85289f92006-01-08 01:00:36 -08004086 seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
4087#endif
4088 seq_putc(m, '\n');
4089}
4090
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091static void *s_start(struct seq_file *m, loff_t *pos)
4092{
4093 loff_t n = *pos;
4094 struct list_head *p;
4095
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004096 mutex_lock(&cache_chain_mutex);
Pekka Enberg85289f92006-01-08 01:00:36 -08004097 if (!n)
4098 print_slabinfo_header(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 p = cache_chain.next;
4100 while (n--) {
4101 p = p->next;
4102 if (p == &cache_chain)
4103 return NULL;
4104 }
Pekka Enberg343e0d72006-02-01 03:05:50 -08004105 return list_entry(p, struct kmem_cache, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106}
4107
4108static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4109{
Pekka Enberg343e0d72006-02-01 03:05:50 -08004110 struct kmem_cache *cachep = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 ++*pos;
Andrew Mortona737b3e2006-03-22 00:08:11 -08004112 return cachep->next.next == &cache_chain ?
4113 NULL : list_entry(cachep->next.next, struct kmem_cache, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114}
4115
4116static void s_stop(struct seq_file *m, void *p)
4117{
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004118 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119}
4120
4121static int s_show(struct seq_file *m, void *p)
4122{
Pekka Enberg343e0d72006-02-01 03:05:50 -08004123 struct kmem_cache *cachep = p;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004124 struct slab *slabp;
4125 unsigned long active_objs;
4126 unsigned long num_objs;
4127 unsigned long active_slabs = 0;
4128 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004129 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 char *error = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07004131 int node;
4132 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 active_objs = 0;
4135 num_slabs = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004136 for_each_online_node(node) {
4137 l3 = cachep->nodelists[node];
4138 if (!l3)
4139 continue;
4140
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004141 check_irq_on();
4142 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07004143
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004144 list_for_each_entry(slabp, &l3->slabs_full, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004145 if (slabp->inuse != cachep->num && !error)
4146 error = "slabs_full accounting error";
4147 active_objs += cachep->num;
4148 active_slabs++;
4149 }
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004150 list_for_each_entry(slabp, &l3->slabs_partial, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004151 if (slabp->inuse == cachep->num && !error)
4152 error = "slabs_partial inuse accounting error";
4153 if (!slabp->inuse && !error)
4154 error = "slabs_partial/inuse accounting error";
4155 active_objs += slabp->inuse;
4156 active_slabs++;
4157 }
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004158 list_for_each_entry(slabp, &l3->slabs_free, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004159 if (slabp->inuse && !error)
4160 error = "slabs_free/inuse accounting error";
4161 num_slabs++;
4162 }
4163 free_objects += l3->free_objects;
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08004164 if (l3->shared)
4165 shared_avail += l3->shared->avail;
Christoph Lametere498be72005-09-09 13:03:32 -07004166
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004167 spin_unlock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004169 num_slabs += active_slabs;
4170 num_objs = num_slabs * cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07004171 if (num_objs - active_objs != free_objects && !error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 error = "free_objects accounting error";
4173
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004174 name = cachep->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 if (error)
4176 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
4177
4178 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
Manfred Spraul3dafccf2006-02-01 03:05:42 -08004179 name, active_objs, num_objs, cachep->buffer_size,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004180 cachep->num, (1 << cachep->gfporder));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 seq_printf(m, " : tunables %4u %4u %4u",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004182 cachep->limit, cachep->batchcount, cachep->shared);
Christoph Lametere498be72005-09-09 13:03:32 -07004183 seq_printf(m, " : slabdata %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004184 active_slabs, num_slabs, shared_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185#if STATS
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004186 { /* list3 stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 unsigned long high = cachep->high_mark;
4188 unsigned long allocs = cachep->num_allocations;
4189 unsigned long grown = cachep->grown;
4190 unsigned long reaped = cachep->reaped;
4191 unsigned long errors = cachep->errors;
4192 unsigned long max_freeable = cachep->max_freeable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 unsigned long node_allocs = cachep->node_allocs;
Christoph Lametere498be72005-09-09 13:03:32 -07004194 unsigned long node_frees = cachep->node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004195 unsigned long overflows = cachep->node_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
Christoph Lametere498be72005-09-09 13:03:32 -07004197 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu \
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004198 %4lu %4lu %4lu %4lu %4lu", allocs, high, grown,
Andrew Mortona737b3e2006-03-22 00:08:11 -08004199 reaped, errors, max_freeable, node_allocs,
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004200 node_frees, overflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 }
4202 /* cpu stats */
4203 {
4204 unsigned long allochit = atomic_read(&cachep->allochit);
4205 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
4206 unsigned long freehit = atomic_read(&cachep->freehit);
4207 unsigned long freemiss = atomic_read(&cachep->freemiss);
4208
4209 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004210 allochit, allocmiss, freehit, freemiss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 }
4212#endif
4213 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 return 0;
4215}
4216
4217/*
4218 * slabinfo_op - iterator that generates /proc/slabinfo
4219 *
4220 * Output layout:
4221 * cache-name
4222 * num-active-objs
4223 * total-objs
4224 * object size
4225 * num-active-slabs
4226 * total-slabs
4227 * num-pages-per-slab
4228 * + further values on SMP and with statistics enabled
4229 */
4230
Helge Deller15ad7cd2006-12-06 20:40:36 -08004231const struct seq_operations slabinfo_op = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004232 .start = s_start,
4233 .next = s_next,
4234 .stop = s_stop,
4235 .show = s_show,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236};
4237
4238#define MAX_SLABINFO_WRITE 128
4239/**
4240 * slabinfo_write - Tuning for the slab allocator
4241 * @file: unused
4242 * @buffer: user buffer
4243 * @count: data length
4244 * @ppos: unused
4245 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004246ssize_t slabinfo_write(struct file *file, const char __user * buffer,
4247 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004249 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 int limit, batchcount, shared, res;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004251 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004252
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 if (count > MAX_SLABINFO_WRITE)
4254 return -EINVAL;
4255 if (copy_from_user(&kbuf, buffer, count))
4256 return -EFAULT;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004257 kbuf[MAX_SLABINFO_WRITE] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258
4259 tmp = strchr(kbuf, ' ');
4260 if (!tmp)
4261 return -EINVAL;
4262 *tmp = '\0';
4263 tmp++;
4264 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
4265 return -EINVAL;
4266
4267 /* Find the cache in the chain of caches. */
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004268 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269 res = -EINVAL;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004270 list_for_each_entry(cachep, &cache_chain, next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 if (!strcmp(cachep->name, kbuf)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08004272 if (limit < 1 || batchcount < 1 ||
4273 batchcount > limit || shared < 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07004274 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07004276 res = do_tune_cpucache(cachep, limit,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004277 batchcount, shared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 }
4279 break;
4280 }
4281 }
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004282 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 if (res >= 0)
4284 res = count;
4285 return res;
4286}
Al Viro871751e2006-03-25 03:06:39 -08004287
4288#ifdef CONFIG_DEBUG_SLAB_LEAK
4289
4290static void *leaks_start(struct seq_file *m, loff_t *pos)
4291{
4292 loff_t n = *pos;
4293 struct list_head *p;
4294
4295 mutex_lock(&cache_chain_mutex);
4296 p = cache_chain.next;
4297 while (n--) {
4298 p = p->next;
4299 if (p == &cache_chain)
4300 return NULL;
4301 }
4302 return list_entry(p, struct kmem_cache, next);
4303}
4304
4305static inline int add_caller(unsigned long *n, unsigned long v)
4306{
4307 unsigned long *p;
4308 int l;
4309 if (!v)
4310 return 1;
4311 l = n[1];
4312 p = n + 2;
4313 while (l) {
4314 int i = l/2;
4315 unsigned long *q = p + 2 * i;
4316 if (*q == v) {
4317 q[1]++;
4318 return 1;
4319 }
4320 if (*q > v) {
4321 l = i;
4322 } else {
4323 p = q + 2;
4324 l -= i + 1;
4325 }
4326 }
4327 if (++n[1] == n[0])
4328 return 0;
4329 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4330 p[0] = v;
4331 p[1] = 1;
4332 return 1;
4333}
4334
4335static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
4336{
4337 void *p;
4338 int i;
4339 if (n[0] == n[1])
4340 return;
4341 for (i = 0, p = s->s_mem; i < c->num; i++, p += c->buffer_size) {
4342 if (slab_bufctl(s)[i] != BUFCTL_ACTIVE)
4343 continue;
4344 if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
4345 return;
4346 }
4347}
4348
4349static void show_symbol(struct seq_file *m, unsigned long address)
4350{
4351#ifdef CONFIG_KALLSYMS
4352 char *modname;
4353 const char *name;
4354 unsigned long offset, size;
4355 char namebuf[KSYM_NAME_LEN+1];
4356
4357 name = kallsyms_lookup(address, &size, &offset, &modname, namebuf);
4358
4359 if (name) {
4360 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
4361 if (modname)
4362 seq_printf(m, " [%s]", modname);
4363 return;
4364 }
4365#endif
4366 seq_printf(m, "%p", (void *)address);
4367}
4368
4369static int leaks_show(struct seq_file *m, void *p)
4370{
4371 struct kmem_cache *cachep = p;
Al Viro871751e2006-03-25 03:06:39 -08004372 struct slab *slabp;
4373 struct kmem_list3 *l3;
4374 const char *name;
4375 unsigned long *n = m->private;
4376 int node;
4377 int i;
4378
4379 if (!(cachep->flags & SLAB_STORE_USER))
4380 return 0;
4381 if (!(cachep->flags & SLAB_RED_ZONE))
4382 return 0;
4383
4384 /* OK, we can do it */
4385
4386 n[1] = 0;
4387
4388 for_each_online_node(node) {
4389 l3 = cachep->nodelists[node];
4390 if (!l3)
4391 continue;
4392
4393 check_irq_on();
4394 spin_lock_irq(&l3->list_lock);
4395
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004396 list_for_each_entry(slabp, &l3->slabs_full, list)
Al Viro871751e2006-03-25 03:06:39 -08004397 handle_slab(n, cachep, slabp);
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004398 list_for_each_entry(slabp, &l3->slabs_partial, list)
Al Viro871751e2006-03-25 03:06:39 -08004399 handle_slab(n, cachep, slabp);
Al Viro871751e2006-03-25 03:06:39 -08004400 spin_unlock_irq(&l3->list_lock);
4401 }
4402 name = cachep->name;
4403 if (n[0] == n[1]) {
4404 /* Increase the buffer size */
4405 mutex_unlock(&cache_chain_mutex);
4406 m->private = kzalloc(n[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
4407 if (!m->private) {
4408 /* Too bad, we are really out */
4409 m->private = n;
4410 mutex_lock(&cache_chain_mutex);
4411 return -ENOMEM;
4412 }
4413 *(unsigned long *)m->private = n[0] * 2;
4414 kfree(n);
4415 mutex_lock(&cache_chain_mutex);
4416 /* Now make sure this entry will be retried */
4417 m->count = m->size;
4418 return 0;
4419 }
4420 for (i = 0; i < n[1]; i++) {
4421 seq_printf(m, "%s: %lu ", name, n[2*i+3]);
4422 show_symbol(m, n[2*i+2]);
4423 seq_putc(m, '\n');
4424 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004425
Al Viro871751e2006-03-25 03:06:39 -08004426 return 0;
4427}
4428
Helge Deller15ad7cd2006-12-06 20:40:36 -08004429const struct seq_operations slabstats_op = {
Al Viro871751e2006-03-25 03:06:39 -08004430 .start = leaks_start,
4431 .next = s_next,
4432 .stop = s_stop,
4433 .show = leaks_show,
4434};
4435#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436#endif
4437
Manfred Spraul00e145b2005-09-03 15:55:07 -07004438/**
4439 * ksize - get the actual amount of memory allocated for a given object
4440 * @objp: Pointer to the object
4441 *
4442 * kmalloc may internally round up allocations and return more memory
4443 * than requested. ksize() can be used to determine the actual amount of
4444 * memory allocated. The caller may use this additional memory, even though
4445 * a smaller amount of memory was initially specified with the kmalloc call.
4446 * The caller must guarantee that objp points to a valid object previously
4447 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4448 * must not be freed during the duration of the call.
4449 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004450unsigned int ksize(const void *objp)
4451{
Manfred Spraul00e145b2005-09-03 15:55:07 -07004452 if (unlikely(objp == NULL))
4453 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08004455 return obj_size(virt_to_cache(objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456}