blob: 52ecf7599a7b7a8d22e12cb8ad1eef98122cb935 [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>
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800112#include <linux/reciprocal_div.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114#include <asm/cacheflush.h>
115#include <asm/tlbflush.h>
116#include <asm/page.h>
117
118/*
Christoph Lameter50953fe2007-05-06 14:50:16 -0700119 * DEBUG - 1 for kmem_cache_create() to honour; SLAB_RED_ZONE & SLAB_POISON.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 * 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
Christoph Lameter50953fe2007-05-06 14:50:16 -0700174# define CREATE_MASK (SLAB_RED_ZONE | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 SLAB_POISON | SLAB_HWCACHE_ALIGN | \
Christoph Lameterac2b8982006-03-22 00:08:15 -0800176 SLAB_CACHE_DMA | \
Christoph Lameter5af60832007-05-06 14:49:56 -0700177 SLAB_STORE_USER | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 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 | \
Christoph Lameter5af60832007-05-06 14:49:56 -0700182 SLAB_CACHE_DMA | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 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;
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800389 u32 reciprocal_buffer_size;
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800390/* 3) touched by every alloc & free from the backend */
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
Eric Dumazet8da34302007-05-06 14:49:29 -0700445 /*
446 * We put nodelists[] at the end of kmem_cache, because we want to size
447 * this array to nr_node_ids slots instead of MAX_NUMNODES
448 * (see kmem_cache_init())
449 * We still use [MAX_NUMNODES] and not [1] or [0] because cache_cache
450 * is statically defined, so we reserve the max number of nodes.
451 */
452 struct kmem_list3 *nodelists[MAX_NUMNODES];
453 /*
454 * Do not add fields after nodelists[]
455 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456};
457
458#define CFLGS_OFF_SLAB (0x80000000UL)
459#define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
460
461#define BATCHREFILL_LIMIT 16
Andrew Mortona737b3e2006-03-22 00:08:11 -0800462/*
463 * Optimization question: fewer reaps means less probability for unnessary
464 * cpucache drain/refill cycles.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 *
Adrian Bunkdc6f3f22005-11-08 16:44:08 +0100466 * OTOH the cpuarrays can contain lots of objects,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 * which could lock up otherwise freeable slabs.
468 */
469#define REAPTIMEOUT_CPUC (2*HZ)
470#define REAPTIMEOUT_LIST3 (4*HZ)
471
472#if STATS
473#define STATS_INC_ACTIVE(x) ((x)->num_active++)
474#define STATS_DEC_ACTIVE(x) ((x)->num_active--)
475#define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
476#define STATS_INC_GROWN(x) ((x)->grown++)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700477#define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
Andrew Mortona737b3e2006-03-22 00:08:11 -0800478#define STATS_SET_HIGH(x) \
479 do { \
480 if ((x)->num_active > (x)->high_mark) \
481 (x)->high_mark = (x)->num_active; \
482 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483#define STATS_INC_ERR(x) ((x)->errors++)
484#define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
Christoph Lametere498be72005-09-09 13:03:32 -0700485#define STATS_INC_NODEFREES(x) ((x)->node_frees++)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700486#define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800487#define STATS_SET_FREEABLE(x, i) \
488 do { \
489 if ((x)->max_freeable < i) \
490 (x)->max_freeable = i; \
491 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492#define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
493#define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
494#define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
495#define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
496#else
497#define STATS_INC_ACTIVE(x) do { } while (0)
498#define STATS_DEC_ACTIVE(x) do { } while (0)
499#define STATS_INC_ALLOCED(x) do { } while (0)
500#define STATS_INC_GROWN(x) do { } while (0)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700501#define STATS_ADD_REAPED(x,y) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502#define STATS_SET_HIGH(x) do { } while (0)
503#define STATS_INC_ERR(x) do { } while (0)
504#define STATS_INC_NODEALLOCS(x) do { } while (0)
Christoph Lametere498be72005-09-09 13:03:32 -0700505#define STATS_INC_NODEFREES(x) do { } while (0)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700506#define STATS_INC_ACOVERFLOW(x) do { } while (0)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800507#define STATS_SET_FREEABLE(x, i) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508#define STATS_INC_ALLOCHIT(x) do { } while (0)
509#define STATS_INC_ALLOCMISS(x) do { } while (0)
510#define STATS_INC_FREEHIT(x) do { } while (0)
511#define STATS_INC_FREEMISS(x) do { } while (0)
512#endif
513
514#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Andrew Mortona737b3e2006-03-22 00:08:11 -0800516/*
517 * memory layout of objects:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 * 0 : objp
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800519 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 * the end of an object is aligned with the end of the real
521 * allocation. Catches writes behind the end of the allocation.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800522 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 * redzone word.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800524 * cachep->obj_offset: The real object.
525 * cachep->buffer_size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
Andrew Mortona737b3e2006-03-22 00:08:11 -0800526 * cachep->buffer_size - 1* BYTES_PER_WORD: last caller address
527 * [BYTES_PER_WORD long]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800529static int obj_offset(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800531 return cachep->obj_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Pekka Enberg343e0d72006-02-01 03:05:50 -0800534static int obj_size(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800536 return cachep->obj_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Pekka Enberg343e0d72006-02-01 03:05:50 -0800539static unsigned long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
541 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800542 return (unsigned long*) (objp+obj_offset(cachep)-BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543}
544
Pekka Enberg343e0d72006-02-01 03:05:50 -0800545static unsigned long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
547 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
548 if (cachep->flags & SLAB_STORE_USER)
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800549 return (unsigned long *)(objp + cachep->buffer_size -
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800550 2 * BYTES_PER_WORD);
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800551 return (unsigned long *)(objp + cachep->buffer_size - BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
553
Pekka Enberg343e0d72006-02-01 03:05:50 -0800554static void **dbg_userword(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 BUG_ON(!(cachep->flags & SLAB_STORE_USER));
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800557 return (void **)(objp + cachep->buffer_size - BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
559
560#else
561
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800562#define obj_offset(x) 0
563#define obj_size(cachep) (cachep->buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564#define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long *)NULL;})
565#define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long *)NULL;})
566#define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
567
568#endif
569
570/*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800571 * Maximum size of an obj (in 2^order pages) and absolute limit for the gfp
572 * order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 */
574#if defined(CONFIG_LARGE_ALLOCS)
575#define MAX_OBJ_ORDER 13 /* up to 32Mb */
576#define MAX_GFP_ORDER 13 /* up to 32Mb */
577#elif defined(CONFIG_MMU)
578#define MAX_OBJ_ORDER 5 /* 32 pages */
579#define MAX_GFP_ORDER 5 /* 32 pages */
580#else
581#define MAX_OBJ_ORDER 8 /* up to 1Mb */
582#define MAX_GFP_ORDER 8 /* up to 1Mb */
583#endif
584
585/*
586 * Do not go above this order unless 0 objects fit into the slab.
587 */
588#define BREAK_GFP_ORDER_HI 1
589#define BREAK_GFP_ORDER_LO 0
590static int slab_break_gfp_order = BREAK_GFP_ORDER_LO;
591
Andrew Mortona737b3e2006-03-22 00:08:11 -0800592/*
593 * Functions for storing/retrieving the cachep and or slab from the page
594 * allocator. These are used to find the slab an obj belongs to. With kfree(),
595 * these are used to find the cache which an obj belongs to.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 */
Pekka Enberg065d41c2005-11-13 16:06:46 -0800597static inline void page_set_cache(struct page *page, struct kmem_cache *cache)
598{
599 page->lru.next = (struct list_head *)cache;
600}
601
602static inline struct kmem_cache *page_get_cache(struct page *page)
603{
Christoph Lameterd85f3382007-05-06 14:49:39 -0700604 page = compound_head(page);
Pekka Enbergddc2e812006-06-23 02:03:40 -0700605 BUG_ON(!PageSlab(page));
Pekka Enberg065d41c2005-11-13 16:06:46 -0800606 return (struct kmem_cache *)page->lru.next;
607}
608
609static inline void page_set_slab(struct page *page, struct slab *slab)
610{
611 page->lru.prev = (struct list_head *)slab;
612}
613
614static inline struct slab *page_get_slab(struct page *page)
615{
Pekka Enbergddc2e812006-06-23 02:03:40 -0700616 BUG_ON(!PageSlab(page));
Pekka Enberg065d41c2005-11-13 16:06:46 -0800617 return (struct slab *)page->lru.prev;
618}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800620static inline struct kmem_cache *virt_to_cache(const void *obj)
621{
Christoph Lameterb49af682007-05-06 14:49:41 -0700622 struct page *page = virt_to_head_page(obj);
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800623 return page_get_cache(page);
624}
625
626static inline struct slab *virt_to_slab(const void *obj)
627{
Christoph Lameterb49af682007-05-06 14:49:41 -0700628 struct page *page = virt_to_head_page(obj);
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800629 return page_get_slab(page);
630}
631
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800632static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
633 unsigned int idx)
634{
635 return slab->s_mem + cache->buffer_size * idx;
636}
637
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800638/*
639 * We want to avoid an expensive divide : (offset / cache->buffer_size)
640 * Using the fact that buffer_size is a constant for a particular cache,
641 * we can replace (offset / cache->buffer_size) by
642 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
643 */
644static inline unsigned int obj_to_index(const struct kmem_cache *cache,
645 const struct slab *slab, void *obj)
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800646{
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800647 u32 offset = (obj - slab->s_mem);
648 return reciprocal_divide(offset, cache->reciprocal_buffer_size);
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800649}
650
Andrew Mortona737b3e2006-03-22 00:08:11 -0800651/*
652 * These are the default caches for kmalloc. Custom caches can have other sizes.
653 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654struct cache_sizes malloc_sizes[] = {
655#define CACHE(x) { .cs_size = (x) },
656#include <linux/kmalloc_sizes.h>
657 CACHE(ULONG_MAX)
658#undef CACHE
659};
660EXPORT_SYMBOL(malloc_sizes);
661
662/* Must match cache_sizes above. Out of line to keep cache footprint low. */
663struct cache_names {
664 char *name;
665 char *name_dma;
666};
667
668static struct cache_names __initdata cache_names[] = {
669#define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },
670#include <linux/kmalloc_sizes.h>
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800671 {NULL,}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672#undef CACHE
673};
674
675static struct arraycache_init initarray_cache __initdata =
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800676 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677static struct arraycache_init initarray_generic =
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800678 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680/* internal cache of cache description objs */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800681static struct kmem_cache cache_cache = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800682 .batchcount = 1,
683 .limit = BOOT_CPUCACHE_ENTRIES,
684 .shared = 1,
Pekka Enberg343e0d72006-02-01 03:05:50 -0800685 .buffer_size = sizeof(struct kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800686 .name = "kmem_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687};
688
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700689#define BAD_ALIEN_MAGIC 0x01020304ul
690
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200691#ifdef CONFIG_LOCKDEP
692
693/*
694 * Slab sometimes uses the kmalloc slabs to store the slab headers
695 * for other slabs "off slab".
696 * The locking for this is tricky in that it nests within the locks
697 * of all other slabs in a few places; to deal with this special
698 * locking we put on-slab caches into a separate lock-class.
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700699 *
700 * We set lock class for alien array caches which are up during init.
701 * The lock annotation will be lost if all cpus of a node goes down and
702 * then comes back up during hotplug
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200703 */
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700704static struct lock_class_key on_slab_l3_key;
705static struct lock_class_key on_slab_alc_key;
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200706
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700707static inline void init_lock_keys(void)
708
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200709{
710 int q;
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700711 struct cache_sizes *s = malloc_sizes;
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200712
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700713 while (s->cs_size != ULONG_MAX) {
714 for_each_node(q) {
715 struct array_cache **alc;
716 int r;
717 struct kmem_list3 *l3 = s->cs_cachep->nodelists[q];
718 if (!l3 || OFF_SLAB(s->cs_cachep))
719 continue;
720 lockdep_set_class(&l3->list_lock, &on_slab_l3_key);
721 alc = l3->alien;
722 /*
723 * FIXME: This check for BAD_ALIEN_MAGIC
724 * should go away when common slab code is taught to
725 * work even without alien caches.
726 * Currently, non NUMA code returns BAD_ALIEN_MAGIC
727 * for alloc_alien_cache,
728 */
729 if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC)
730 continue;
731 for_each_node(r) {
732 if (alc[r])
733 lockdep_set_class(&alc[r]->lock,
734 &on_slab_alc_key);
735 }
736 }
737 s++;
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200738 }
739}
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200740#else
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700741static inline void init_lock_keys(void)
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200742{
743}
744#endif
745
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -0800746/*
747 * 1. Guard access to the cache-chain.
748 * 2. Protect sanity of cpu_online_map against cpu hotplug events
749 */
Ingo Molnarfc0abb12006-01-18 17:42:33 -0800750static DEFINE_MUTEX(cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751static struct list_head cache_chain;
752
753/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 * chicken and egg problem: delay the per-cpu array allocation
755 * until the general caches are up.
756 */
757static enum {
758 NONE,
Christoph Lametere498be72005-09-09 13:03:32 -0700759 PARTIAL_AC,
760 PARTIAL_L3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 FULL
762} g_cpucache_up;
763
Mike Kravetz39d24e62006-05-15 09:44:13 -0700764/*
765 * used by boot code to determine if it can use slab based allocator
766 */
767int slab_is_available(void)
768{
769 return g_cpucache_up == FULL;
770}
771
David Howells52bad642006-11-22 14:54:01 +0000772static DEFINE_PER_CPU(struct delayed_work, reap_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Pekka Enberg343e0d72006-02-01 03:05:50 -0800774static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 return cachep->array[smp_processor_id()];
777}
778
Andrew Mortona737b3e2006-03-22 00:08:11 -0800779static inline struct kmem_cache *__find_general_cachep(size_t size,
780 gfp_t gfpflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781{
782 struct cache_sizes *csizep = malloc_sizes;
783
784#if DEBUG
785 /* This happens if someone tries to call
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800786 * kmem_cache_create(), or __kmalloc(), before
787 * the generic caches are initialized.
788 */
Alok Katariac7e43c72005-09-14 12:17:53 -0700789 BUG_ON(malloc_sizes[INDEX_AC].cs_cachep == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790#endif
791 while (size > csizep->cs_size)
792 csizep++;
793
794 /*
Martin Hicks0abf40c2005-09-03 15:54:54 -0700795 * Really subtle: The last entry with cs->cs_size==ULONG_MAX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 * has cs_{dma,}cachep==NULL. Thus no special case
797 * for large kmalloc calls required.
798 */
Christoph Lameter4b51d662007-02-10 01:43:10 -0800799#ifdef CONFIG_ZONE_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 if (unlikely(gfpflags & GFP_DMA))
801 return csizep->cs_dmacachep;
Christoph Lameter4b51d662007-02-10 01:43:10 -0800802#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return csizep->cs_cachep;
804}
805
Adrian Bunkb2213852006-09-25 23:31:02 -0700806static struct kmem_cache *kmem_find_general_cachep(size_t size, gfp_t gfpflags)
Manfred Spraul97e2bde2005-05-01 08:58:38 -0700807{
808 return __find_general_cachep(size, gfpflags);
809}
Manfred Spraul97e2bde2005-05-01 08:58:38 -0700810
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800811static size_t slab_mgmt_size(size_t nr_objs, size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800813 return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align);
814}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Andrew Mortona737b3e2006-03-22 00:08:11 -0800816/*
817 * Calculate the number of objects and left-over bytes for a given buffer size.
818 */
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800819static void cache_estimate(unsigned long gfporder, size_t buffer_size,
820 size_t align, int flags, size_t *left_over,
821 unsigned int *num)
822{
823 int nr_objs;
824 size_t mgmt_size;
825 size_t slab_size = PAGE_SIZE << gfporder;
826
827 /*
828 * The slab management structure can be either off the slab or
829 * on it. For the latter case, the memory allocated for a
830 * slab is used for:
831 *
832 * - The struct slab
833 * - One kmem_bufctl_t for each object
834 * - Padding to respect alignment of @align
835 * - @buffer_size bytes for each object
836 *
837 * If the slab management structure is off the slab, then the
838 * alignment will already be calculated into the size. Because
839 * the slabs are all pages aligned, the objects will be at the
840 * correct alignment when allocated.
841 */
842 if (flags & CFLGS_OFF_SLAB) {
843 mgmt_size = 0;
844 nr_objs = slab_size / buffer_size;
845
846 if (nr_objs > SLAB_LIMIT)
847 nr_objs = SLAB_LIMIT;
848 } else {
849 /*
850 * Ignore padding for the initial guess. The padding
851 * is at most @align-1 bytes, and @buffer_size is at
852 * least @align. In the worst case, this result will
853 * be one greater than the number of objects that fit
854 * into the memory allocation when taking the padding
855 * into account.
856 */
857 nr_objs = (slab_size - sizeof(struct slab)) /
858 (buffer_size + sizeof(kmem_bufctl_t));
859
860 /*
861 * This calculated number will be either the right
862 * amount, or one greater than what we want.
863 */
864 if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
865 > slab_size)
866 nr_objs--;
867
868 if (nr_objs > SLAB_LIMIT)
869 nr_objs = SLAB_LIMIT;
870
871 mgmt_size = slab_mgmt_size(nr_objs, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800873 *num = nr_objs;
874 *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
876
877#define slab_error(cachep, msg) __slab_error(__FUNCTION__, cachep, msg)
878
Andrew Mortona737b3e2006-03-22 00:08:11 -0800879static void __slab_error(const char *function, struct kmem_cache *cachep,
880 char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800883 function, cachep->name, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 dump_stack();
885}
886
Paul Menage3395ee02006-12-06 20:32:16 -0800887/*
888 * By default on NUMA we use alien caches to stage the freeing of
889 * objects allocated from other nodes. This causes massive memory
890 * inefficiencies when using fake NUMA setup to split memory into a
891 * large number of small nodes, so it can be disabled on the command
892 * line
893 */
894
895static int use_alien_caches __read_mostly = 1;
896static int __init noaliencache_setup(char *s)
897{
898 use_alien_caches = 0;
899 return 1;
900}
901__setup("noaliencache", noaliencache_setup);
902
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800903#ifdef CONFIG_NUMA
904/*
905 * Special reaping functions for NUMA systems called from cache_reap().
906 * These take care of doing round robin flushing of alien caches (containing
907 * objects freed on different nodes from which they were allocated) and the
908 * flushing of remote pcps by calling drain_node_pages.
909 */
910static DEFINE_PER_CPU(unsigned long, reap_node);
911
912static void init_reap_node(int cpu)
913{
914 int node;
915
916 node = next_node(cpu_to_node(cpu), node_online_map);
917 if (node == MAX_NUMNODES)
Paul Jackson442295c2006-03-22 00:09:11 -0800918 node = first_node(node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800919
Daniel Yeisley7f6b8872006-11-02 22:07:14 -0800920 per_cpu(reap_node, cpu) = node;
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800921}
922
923static void next_reap_node(void)
924{
925 int node = __get_cpu_var(reap_node);
926
927 /*
928 * Also drain per cpu pages on remote zones
929 */
930 if (node != numa_node_id())
931 drain_node_pages(node);
932
933 node = next_node(node, node_online_map);
934 if (unlikely(node >= MAX_NUMNODES))
935 node = first_node(node_online_map);
936 __get_cpu_var(reap_node) = node;
937}
938
939#else
940#define init_reap_node(cpu) do { } while (0)
941#define next_reap_node(void) do { } while (0)
942#endif
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944/*
945 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
946 * via the workqueue/eventd.
947 * Add the CPU number into the expiration time to minimize the possibility of
948 * the CPUs getting into lockstep and contending for the global cache chain
949 * lock.
950 */
951static void __devinit start_cpu_timer(int cpu)
952{
David Howells52bad642006-11-22 14:54:01 +0000953 struct delayed_work *reap_work = &per_cpu(reap_work, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 /*
956 * When this gets called from do_initcalls via cpucache_init(),
957 * init_workqueues() has already run, so keventd will be setup
958 * at that time.
959 */
David Howells52bad642006-11-22 14:54:01 +0000960 if (keventd_up() && reap_work->work.func == NULL) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800961 init_reap_node(cpu);
David Howells65f27f32006-11-22 14:55:48 +0000962 INIT_DELAYED_WORK(reap_work, cache_reap);
Arjan van de Ven2b284212006-12-10 02:21:28 -0800963 schedule_delayed_work_on(cpu, reap_work,
964 __round_jiffies_relative(HZ, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 }
966}
967
Christoph Lametere498be72005-09-09 13:03:32 -0700968static struct array_cache *alloc_arraycache(int node, int entries,
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800969 int batchcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800971 int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 struct array_cache *nc = NULL;
973
Christoph Lametere498be72005-09-09 13:03:32 -0700974 nc = kmalloc_node(memsize, GFP_KERNEL, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 if (nc) {
976 nc->avail = 0;
977 nc->limit = entries;
978 nc->batchcount = batchcount;
979 nc->touched = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700980 spin_lock_init(&nc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
982 return nc;
983}
984
Christoph Lameter3ded1752006-03-25 03:06:44 -0800985/*
986 * Transfer objects in one arraycache to another.
987 * Locking must be handled by the caller.
988 *
989 * Return the number of entries transferred.
990 */
991static int transfer_objects(struct array_cache *to,
992 struct array_cache *from, unsigned int max)
993{
994 /* Figure out how many entries to transfer */
995 int nr = min(min(from->avail, max), to->limit - to->avail);
996
997 if (!nr)
998 return 0;
999
1000 memcpy(to->entry + to->avail, from->entry + from->avail -nr,
1001 sizeof(void *) *nr);
1002
1003 from->avail -= nr;
1004 to->avail += nr;
1005 to->touched = 1;
1006 return nr;
1007}
1008
Christoph Lameter765c4502006-09-27 01:50:08 -07001009#ifndef CONFIG_NUMA
1010
1011#define drain_alien_cache(cachep, alien) do { } while (0)
1012#define reap_alien(cachep, l3) do { } while (0)
1013
1014static inline struct array_cache **alloc_alien_cache(int node, int limit)
1015{
1016 return (struct array_cache **)BAD_ALIEN_MAGIC;
1017}
1018
1019static inline void free_alien_cache(struct array_cache **ac_ptr)
1020{
1021}
1022
1023static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
1024{
1025 return 0;
1026}
1027
1028static inline void *alternate_node_alloc(struct kmem_cache *cachep,
1029 gfp_t flags)
1030{
1031 return NULL;
1032}
1033
Christoph Hellwig8b98c162006-12-06 20:32:30 -08001034static inline void *____cache_alloc_node(struct kmem_cache *cachep,
Christoph Lameter765c4502006-09-27 01:50:08 -07001035 gfp_t flags, int nodeid)
1036{
1037 return NULL;
1038}
1039
1040#else /* CONFIG_NUMA */
1041
Christoph Hellwig8b98c162006-12-06 20:32:30 -08001042static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
Paul Jacksonc61afb12006-03-24 03:16:08 -08001043static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
Christoph Lameterdc85da12006-01-18 17:42:36 -08001044
Pekka Enberg5295a742006-02-01 03:05:48 -08001045static struct array_cache **alloc_alien_cache(int node, int limit)
Christoph Lametere498be72005-09-09 13:03:32 -07001046{
1047 struct array_cache **ac_ptr;
Christoph Lameter8ef82862007-02-20 13:57:52 -08001048 int memsize = sizeof(void *) * nr_node_ids;
Christoph Lametere498be72005-09-09 13:03:32 -07001049 int i;
1050
1051 if (limit > 1)
1052 limit = 12;
1053 ac_ptr = kmalloc_node(memsize, GFP_KERNEL, node);
1054 if (ac_ptr) {
1055 for_each_node(i) {
1056 if (i == node || !node_online(i)) {
1057 ac_ptr[i] = NULL;
1058 continue;
1059 }
1060 ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d);
1061 if (!ac_ptr[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001062 for (i--; i <= 0; i--)
Christoph Lametere498be72005-09-09 13:03:32 -07001063 kfree(ac_ptr[i]);
1064 kfree(ac_ptr);
1065 return NULL;
1066 }
1067 }
1068 }
1069 return ac_ptr;
1070}
1071
Pekka Enberg5295a742006-02-01 03:05:48 -08001072static void free_alien_cache(struct array_cache **ac_ptr)
Christoph Lametere498be72005-09-09 13:03:32 -07001073{
1074 int i;
1075
1076 if (!ac_ptr)
1077 return;
Christoph Lametere498be72005-09-09 13:03:32 -07001078 for_each_node(i)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001079 kfree(ac_ptr[i]);
Christoph Lametere498be72005-09-09 13:03:32 -07001080 kfree(ac_ptr);
1081}
1082
Pekka Enberg343e0d72006-02-01 03:05:50 -08001083static void __drain_alien_cache(struct kmem_cache *cachep,
Pekka Enberg5295a742006-02-01 03:05:48 -08001084 struct array_cache *ac, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07001085{
1086 struct kmem_list3 *rl3 = cachep->nodelists[node];
1087
1088 if (ac->avail) {
1089 spin_lock(&rl3->list_lock);
Christoph Lametere00946f2006-03-25 03:06:45 -08001090 /*
1091 * Stuff objects into the remote nodes shared array first.
1092 * That way we could avoid the overhead of putting the objects
1093 * into the free lists and getting them back later.
1094 */
shin, jacob693f7d32006-04-28 10:54:37 -05001095 if (rl3->shared)
1096 transfer_objects(rl3->shared, ac, ac->limit);
Christoph Lametere00946f2006-03-25 03:06:45 -08001097
Christoph Lameterff694162005-09-22 21:44:02 -07001098 free_block(cachep, ac->entry, ac->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001099 ac->avail = 0;
1100 spin_unlock(&rl3->list_lock);
1101 }
1102}
1103
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001104/*
1105 * Called from cache_reap() to regularly drain alien caches round robin.
1106 */
1107static void reap_alien(struct kmem_cache *cachep, struct kmem_list3 *l3)
1108{
1109 int node = __get_cpu_var(reap_node);
1110
1111 if (l3->alien) {
1112 struct array_cache *ac = l3->alien[node];
Christoph Lametere00946f2006-03-25 03:06:45 -08001113
1114 if (ac && ac->avail && spin_trylock_irq(&ac->lock)) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001115 __drain_alien_cache(cachep, ac, node);
1116 spin_unlock_irq(&ac->lock);
1117 }
1118 }
1119}
1120
Andrew Mortona737b3e2006-03-22 00:08:11 -08001121static void drain_alien_cache(struct kmem_cache *cachep,
1122 struct array_cache **alien)
Christoph Lametere498be72005-09-09 13:03:32 -07001123{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001124 int i = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07001125 struct array_cache *ac;
1126 unsigned long flags;
1127
1128 for_each_online_node(i) {
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001129 ac = alien[i];
Christoph Lametere498be72005-09-09 13:03:32 -07001130 if (ac) {
1131 spin_lock_irqsave(&ac->lock, flags);
1132 __drain_alien_cache(cachep, ac, i);
1133 spin_unlock_irqrestore(&ac->lock, flags);
1134 }
1135 }
1136}
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001137
Ingo Molnar873623d2006-07-13 14:44:38 +02001138static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001139{
1140 struct slab *slabp = virt_to_slab(objp);
1141 int nodeid = slabp->nodeid;
1142 struct kmem_list3 *l3;
1143 struct array_cache *alien = NULL;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001144 int node;
1145
1146 node = numa_node_id();
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001147
1148 /*
1149 * Make sure we are not freeing a object from another node to the array
1150 * cache on this cpu.
1151 */
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001152 if (likely(slabp->nodeid == node))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001153 return 0;
1154
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001155 l3 = cachep->nodelists[node];
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001156 STATS_INC_NODEFREES(cachep);
1157 if (l3->alien && l3->alien[nodeid]) {
1158 alien = l3->alien[nodeid];
Ingo Molnar873623d2006-07-13 14:44:38 +02001159 spin_lock(&alien->lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001160 if (unlikely(alien->avail == alien->limit)) {
1161 STATS_INC_ACOVERFLOW(cachep);
1162 __drain_alien_cache(cachep, alien, nodeid);
1163 }
1164 alien->entry[alien->avail++] = objp;
1165 spin_unlock(&alien->lock);
1166 } else {
1167 spin_lock(&(cachep->nodelists[nodeid])->list_lock);
1168 free_block(cachep, &objp, 1, nodeid);
1169 spin_unlock(&(cachep->nodelists[nodeid])->list_lock);
1170 }
1171 return 1;
1172}
Christoph Lametere498be72005-09-09 13:03:32 -07001173#endif
1174
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001175static int __cpuinit cpuup_callback(struct notifier_block *nfb,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001176 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
1178 long cpu = (long)hcpu;
Pekka Enberg343e0d72006-02-01 03:05:50 -08001179 struct kmem_cache *cachep;
Christoph Lametere498be72005-09-09 13:03:32 -07001180 struct kmem_list3 *l3 = NULL;
1181 int node = cpu_to_node(cpu);
1182 int memsize = sizeof(struct kmem_list3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 switch (action) {
1185 case CPU_UP_PREPARE:
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001186 mutex_lock(&cache_chain_mutex);
Andrew Mortona737b3e2006-03-22 00:08:11 -08001187 /*
1188 * We need to do this right in the beginning since
Christoph Lametere498be72005-09-09 13:03:32 -07001189 * alloc_arraycache's are going to use this list.
1190 * kmalloc_node allows us to add the slab to the right
1191 * kmem_list3 and not this cpu's kmem_list3
1192 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Christoph Lametere498be72005-09-09 13:03:32 -07001194 list_for_each_entry(cachep, &cache_chain, next) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08001195 /*
1196 * Set up the size64 kmemlist for cpu before we can
Christoph Lametere498be72005-09-09 13:03:32 -07001197 * begin anything. Make sure some other cpu on this
1198 * node has not already allocated this
1199 */
1200 if (!cachep->nodelists[node]) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08001201 l3 = kmalloc_node(memsize, GFP_KERNEL, node);
1202 if (!l3)
Christoph Lametere498be72005-09-09 13:03:32 -07001203 goto bad;
1204 kmem_list3_init(l3);
1205 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001206 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lametere498be72005-09-09 13:03:32 -07001207
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001208 /*
1209 * The l3s don't come and go as CPUs come and
1210 * go. cache_chain_mutex is sufficient
1211 * protection here.
1212 */
Christoph Lametere498be72005-09-09 13:03:32 -07001213 cachep->nodelists[node] = l3;
1214 }
1215
1216 spin_lock_irq(&cachep->nodelists[node]->list_lock);
1217 cachep->nodelists[node]->free_limit =
Andrew Mortona737b3e2006-03-22 00:08:11 -08001218 (1 + nr_cpus_node(node)) *
1219 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07001220 spin_unlock_irq(&cachep->nodelists[node]->list_lock);
1221 }
1222
Andrew Mortona737b3e2006-03-22 00:08:11 -08001223 /*
1224 * Now we can go ahead with allocating the shared arrays and
1225 * array caches
1226 */
Christoph Lametere498be72005-09-09 13:03:32 -07001227 list_for_each_entry(cachep, &cache_chain, next) {
Tobias Klausercd105df2006-01-08 01:00:59 -08001228 struct array_cache *nc;
Eric Dumazet63109842007-05-06 14:49:28 -07001229 struct array_cache *shared = NULL;
Paul Menage3395ee02006-12-06 20:32:16 -08001230 struct array_cache **alien = NULL;
Tobias Klausercd105df2006-01-08 01:00:59 -08001231
Christoph Lametere498be72005-09-09 13:03:32 -07001232 nc = alloc_arraycache(node, cachep->limit,
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001233 cachep->batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 if (!nc)
1235 goto bad;
Eric Dumazet63109842007-05-06 14:49:28 -07001236 if (cachep->shared) {
1237 shared = alloc_arraycache(node,
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001238 cachep->shared * cachep->batchcount,
1239 0xbaadf00d);
Eric Dumazet63109842007-05-06 14:49:28 -07001240 if (!shared)
1241 goto bad;
1242 }
Paul Menage3395ee02006-12-06 20:32:16 -08001243 if (use_alien_caches) {
1244 alien = alloc_alien_cache(node, cachep->limit);
1245 if (!alien)
1246 goto bad;
1247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 cachep->array[cpu] = nc;
Christoph Lametere498be72005-09-09 13:03:32 -07001249 l3 = cachep->nodelists[node];
1250 BUG_ON(!l3);
Christoph Lametere498be72005-09-09 13:03:32 -07001251
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001252 spin_lock_irq(&l3->list_lock);
1253 if (!l3->shared) {
1254 /*
1255 * We are serialised from CPU_DEAD or
1256 * CPU_UP_CANCELLED by the cpucontrol lock
1257 */
1258 l3->shared = shared;
1259 shared = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07001260 }
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001261#ifdef CONFIG_NUMA
1262 if (!l3->alien) {
1263 l3->alien = alien;
1264 alien = NULL;
1265 }
1266#endif
1267 spin_unlock_irq(&l3->list_lock);
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001268 kfree(shared);
1269 free_alien_cache(alien);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 break;
1272 case CPU_ONLINE:
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08001273 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 start_cpu_timer(cpu);
1275 break;
1276#ifdef CONFIG_HOTPLUG_CPU
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08001277 case CPU_DOWN_PREPARE:
1278 mutex_lock(&cache_chain_mutex);
1279 break;
1280 case CPU_DOWN_FAILED:
1281 mutex_unlock(&cache_chain_mutex);
1282 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 case CPU_DEAD:
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001284 /*
1285 * Even if all the cpus of a node are down, we don't free the
1286 * kmem_list3 of any cache. This to avoid a race between
1287 * cpu_down, and a kmalloc allocation from another cpu for
1288 * memory from the node of the cpu going down. The list3
1289 * structure is usually allocated from kmem_cache_create() and
1290 * gets destroyed at kmem_cache_destroy().
1291 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 /* fall thru */
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08001293#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 case CPU_UP_CANCELED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 list_for_each_entry(cachep, &cache_chain, next) {
1296 struct array_cache *nc;
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001297 struct array_cache *shared;
1298 struct array_cache **alien;
Christoph Lametere498be72005-09-09 13:03:32 -07001299 cpumask_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Christoph Lametere498be72005-09-09 13:03:32 -07001301 mask = node_to_cpumask(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 /* cpu is dead; no one can alloc from it. */
1303 nc = cachep->array[cpu];
1304 cachep->array[cpu] = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07001305 l3 = cachep->nodelists[node];
1306
1307 if (!l3)
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001308 goto free_array_cache;
Christoph Lametere498be72005-09-09 13:03:32 -07001309
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08001310 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07001311
1312 /* Free limit for this kmem_list3 */
1313 l3->free_limit -= cachep->batchcount;
1314 if (nc)
Christoph Lameterff694162005-09-22 21:44:02 -07001315 free_block(cachep, nc->entry, nc->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001316
1317 if (!cpus_empty(mask)) {
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08001318 spin_unlock_irq(&l3->list_lock);
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001319 goto free_array_cache;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001320 }
Christoph Lametere498be72005-09-09 13:03:32 -07001321
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001322 shared = l3->shared;
1323 if (shared) {
Eric Dumazet63109842007-05-06 14:49:28 -07001324 free_block(cachep, shared->entry,
1325 shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001326 l3->shared = NULL;
1327 }
Christoph Lametere498be72005-09-09 13:03:32 -07001328
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001329 alien = l3->alien;
1330 l3->alien = NULL;
1331
1332 spin_unlock_irq(&l3->list_lock);
1333
1334 kfree(shared);
1335 if (alien) {
1336 drain_alien_cache(cachep, alien);
1337 free_alien_cache(alien);
Christoph Lametere498be72005-09-09 13:03:32 -07001338 }
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001339free_array_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 kfree(nc);
1341 }
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001342 /*
1343 * In the previous loop, all the objects were freed to
1344 * the respective cache's slabs, now we can go ahead and
1345 * shrink each nodelist to its limit.
1346 */
1347 list_for_each_entry(cachep, &cache_chain, next) {
1348 l3 = cachep->nodelists[node];
1349 if (!l3)
1350 continue;
Christoph Lametered11d9e2006-06-30 01:55:45 -07001351 drain_freelist(cachep, l3, l3->free_objects);
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001352 }
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001353 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 }
1356 return NOTIFY_OK;
Andrew Mortona737b3e2006-03-22 00:08:11 -08001357bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 return NOTIFY_BAD;
1359}
1360
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001361static struct notifier_block __cpuinitdata cpucache_notifier = {
1362 &cpuup_callback, NULL, 0
1363};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Christoph Lametere498be72005-09-09 13:03:32 -07001365/*
1366 * swap the static kmem_list3 with kmalloced memory
1367 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001368static void init_list(struct kmem_cache *cachep, struct kmem_list3 *list,
1369 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07001370{
1371 struct kmem_list3 *ptr;
1372
Christoph Lametere498be72005-09-09 13:03:32 -07001373 ptr = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, nodeid);
1374 BUG_ON(!ptr);
1375
1376 local_irq_disable();
1377 memcpy(ptr, list, sizeof(struct kmem_list3));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001378 /*
1379 * Do not assume that spinlocks can be initialized via memcpy:
1380 */
1381 spin_lock_init(&ptr->list_lock);
1382
Christoph Lametere498be72005-09-09 13:03:32 -07001383 MAKE_ALL_LISTS(cachep, ptr, nodeid);
1384 cachep->nodelists[nodeid] = ptr;
1385 local_irq_enable();
1386}
1387
Andrew Mortona737b3e2006-03-22 00:08:11 -08001388/*
1389 * Initialisation. Called after the page allocator have been initialised and
1390 * before smp_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 */
1392void __init kmem_cache_init(void)
1393{
1394 size_t left_over;
1395 struct cache_sizes *sizes;
1396 struct cache_names *names;
Christoph Lametere498be72005-09-09 13:03:32 -07001397 int i;
Jack Steiner07ed76b2006-03-07 21:55:46 -08001398 int order;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001399 int node;
Christoph Lametere498be72005-09-09 13:03:32 -07001400
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001401 if (num_possible_nodes() == 1)
1402 use_alien_caches = 0;
1403
Christoph Lametere498be72005-09-09 13:03:32 -07001404 for (i = 0; i < NUM_INIT_LISTS; i++) {
1405 kmem_list3_init(&initkmem_list3[i]);
1406 if (i < MAX_NUMNODES)
1407 cache_cache.nodelists[i] = NULL;
1408 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 /*
1411 * Fragmentation resistance on low memory - only use bigger
1412 * page orders on machines with more than 32MB of memory.
1413 */
1414 if (num_physpages > (32 << 20) >> PAGE_SHIFT)
1415 slab_break_gfp_order = BREAK_GFP_ORDER_HI;
1416
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 /* Bootstrap is tricky, because several objects are allocated
1418 * from caches that do not exist yet:
Andrew Mortona737b3e2006-03-22 00:08:11 -08001419 * 1) initialize the cache_cache cache: it contains the struct
1420 * kmem_cache structures of all caches, except cache_cache itself:
1421 * cache_cache is statically allocated.
Christoph Lametere498be72005-09-09 13:03:32 -07001422 * Initially an __init data area is used for the head array and the
1423 * kmem_list3 structures, it's replaced with a kmalloc allocated
1424 * array at the end of the bootstrap.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 * 2) Create the first kmalloc cache.
Pekka Enberg343e0d72006-02-01 03:05:50 -08001426 * The struct kmem_cache for the new cache is allocated normally.
Christoph Lametere498be72005-09-09 13:03:32 -07001427 * An __init data area is used for the head array.
1428 * 3) Create the remaining kmalloc caches, with minimally sized
1429 * head arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 * 4) Replace the __init data head arrays for cache_cache and the first
1431 * kmalloc cache with kmalloc allocated arrays.
Christoph Lametere498be72005-09-09 13:03:32 -07001432 * 5) Replace the __init data for kmem_list3 for cache_cache and
1433 * the other cache's with kmalloc allocated memory.
1434 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 */
1436
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001437 node = numa_node_id();
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439 /* 1) create the cache_cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 INIT_LIST_HEAD(&cache_chain);
1441 list_add(&cache_cache.next, &cache_chain);
1442 cache_cache.colour_off = cache_line_size();
1443 cache_cache.array[smp_processor_id()] = &initarray_cache.cache;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001444 cache_cache.nodelists[node] = &initkmem_list3[CACHE_CACHE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
Eric Dumazet8da34302007-05-06 14:49:29 -07001446 /*
1447 * struct kmem_cache size depends on nr_node_ids, which
1448 * can be less than MAX_NUMNODES.
1449 */
1450 cache_cache.buffer_size = offsetof(struct kmem_cache, nodelists) +
1451 nr_node_ids * sizeof(struct kmem_list3 *);
1452#if DEBUG
1453 cache_cache.obj_size = cache_cache.buffer_size;
1454#endif
Andrew Mortona737b3e2006-03-22 00:08:11 -08001455 cache_cache.buffer_size = ALIGN(cache_cache.buffer_size,
1456 cache_line_size());
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08001457 cache_cache.reciprocal_buffer_size =
1458 reciprocal_value(cache_cache.buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Jack Steiner07ed76b2006-03-07 21:55:46 -08001460 for (order = 0; order < MAX_ORDER; order++) {
1461 cache_estimate(order, cache_cache.buffer_size,
1462 cache_line_size(), 0, &left_over, &cache_cache.num);
1463 if (cache_cache.num)
1464 break;
1465 }
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02001466 BUG_ON(!cache_cache.num);
Jack Steiner07ed76b2006-03-07 21:55:46 -08001467 cache_cache.gfporder = order;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001468 cache_cache.colour = left_over / cache_cache.colour_off;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001469 cache_cache.slab_size = ALIGN(cache_cache.num * sizeof(kmem_bufctl_t) +
1470 sizeof(struct slab), cache_line_size());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 /* 2+3) create the kmalloc caches */
1473 sizes = malloc_sizes;
1474 names = cache_names;
1475
Andrew Mortona737b3e2006-03-22 00:08:11 -08001476 /*
1477 * Initialize the caches that provide memory for the array cache and the
1478 * kmem_list3 structures first. Without this, further allocations will
1479 * bug.
Christoph Lametere498be72005-09-09 13:03:32 -07001480 */
1481
1482 sizes[INDEX_AC].cs_cachep = kmem_cache_create(names[INDEX_AC].name,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001483 sizes[INDEX_AC].cs_size,
1484 ARCH_KMALLOC_MINALIGN,
1485 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
1486 NULL, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07001487
Andrew Mortona737b3e2006-03-22 00:08:11 -08001488 if (INDEX_AC != INDEX_L3) {
Christoph Lametere498be72005-09-09 13:03:32 -07001489 sizes[INDEX_L3].cs_cachep =
Andrew Mortona737b3e2006-03-22 00:08:11 -08001490 kmem_cache_create(names[INDEX_L3].name,
1491 sizes[INDEX_L3].cs_size,
1492 ARCH_KMALLOC_MINALIGN,
1493 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
1494 NULL, NULL);
1495 }
Christoph Lametere498be72005-09-09 13:03:32 -07001496
Ingo Molnare0a42722006-06-23 02:03:46 -07001497 slab_early_init = 0;
1498
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 while (sizes->cs_size != ULONG_MAX) {
Christoph Lametere498be72005-09-09 13:03:32 -07001500 /*
1501 * For performance, all the general caches are L1 aligned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 * This should be particularly beneficial on SMP boxes, as it
1503 * eliminates "false sharing".
1504 * Note for systems short on memory removing the alignment will
Christoph Lametere498be72005-09-09 13:03:32 -07001505 * allow tighter packing of the smaller caches.
1506 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001507 if (!sizes->cs_cachep) {
Christoph Lametere498be72005-09-09 13:03:32 -07001508 sizes->cs_cachep = kmem_cache_create(names->name,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001509 sizes->cs_size,
1510 ARCH_KMALLOC_MINALIGN,
1511 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
1512 NULL, NULL);
1513 }
Christoph Lameter4b51d662007-02-10 01:43:10 -08001514#ifdef CONFIG_ZONE_DMA
1515 sizes->cs_dmacachep = kmem_cache_create(
1516 names->name_dma,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001517 sizes->cs_size,
1518 ARCH_KMALLOC_MINALIGN,
1519 ARCH_KMALLOC_FLAGS|SLAB_CACHE_DMA|
1520 SLAB_PANIC,
1521 NULL, NULL);
Christoph Lameter4b51d662007-02-10 01:43:10 -08001522#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 sizes++;
1524 names++;
1525 }
1526 /* 4) Replace the bootstrap head arrays */
1527 {
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001528 struct array_cache *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
Christoph Lametere498be72005-09-09 13:03:32 -07001531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 local_irq_disable();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001533 BUG_ON(cpu_cache_get(&cache_cache) != &initarray_cache.cache);
1534 memcpy(ptr, cpu_cache_get(&cache_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001535 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001536 /*
1537 * Do not assume that spinlocks can be initialized via memcpy:
1538 */
1539 spin_lock_init(&ptr->lock);
1540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 cache_cache.array[smp_processor_id()] = ptr;
1542 local_irq_enable();
Christoph Lametere498be72005-09-09 13:03:32 -07001543
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
Christoph Lametere498be72005-09-09 13:03:32 -07001545
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 local_irq_disable();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001547 BUG_ON(cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001548 != &initarray_generic.cache);
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001549 memcpy(ptr, cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001550 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001551 /*
1552 * Do not assume that spinlocks can be initialized via memcpy:
1553 */
1554 spin_lock_init(&ptr->lock);
1555
Christoph Lametere498be72005-09-09 13:03:32 -07001556 malloc_sizes[INDEX_AC].cs_cachep->array[smp_processor_id()] =
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001557 ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 local_irq_enable();
1559 }
Christoph Lametere498be72005-09-09 13:03:32 -07001560 /* 5) Replace the bootstrap kmem_list3's */
1561 {
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001562 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001564 /* Replace the static kmem_list3 structures for the boot cpu */
1565 init_list(&cache_cache, &initkmem_list3[CACHE_CACHE], node);
1566
1567 for_each_online_node(nid) {
Christoph Lametere498be72005-09-09 13:03:32 -07001568 init_list(malloc_sizes[INDEX_AC].cs_cachep,
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001569 &initkmem_list3[SIZE_AC + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001570
1571 if (INDEX_AC != INDEX_L3) {
1572 init_list(malloc_sizes[INDEX_L3].cs_cachep,
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001573 &initkmem_list3[SIZE_L3 + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001574 }
1575 }
1576 }
1577
1578 /* 6) resize the head arrays to their final sizes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 {
Pekka Enberg343e0d72006-02-01 03:05:50 -08001580 struct kmem_cache *cachep;
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001581 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 list_for_each_entry(cachep, &cache_chain, next)
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07001583 if (enable_cpucache(cachep))
1584 BUG();
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001585 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
1587
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001588 /* Annotate slab for lockdep -- annotate the malloc caches */
1589 init_lock_keys();
1590
1591
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 /* Done! */
1593 g_cpucache_up = FULL;
1594
Andrew Mortona737b3e2006-03-22 00:08:11 -08001595 /*
1596 * Register a cpu startup notifier callback that initializes
1597 * cpu_cache_get for all new cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 */
1599 register_cpu_notifier(&cpucache_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600
Andrew Mortona737b3e2006-03-22 00:08:11 -08001601 /*
1602 * The reap timers are started later, with a module init call: That part
1603 * of the kernel is not yet operational.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 */
1605}
1606
1607static int __init cpucache_init(void)
1608{
1609 int cpu;
1610
Andrew Mortona737b3e2006-03-22 00:08:11 -08001611 /*
1612 * Register the timers that return unneeded pages to the page allocator
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 */
Christoph Lametere498be72005-09-09 13:03:32 -07001614 for_each_online_cpu(cpu)
Andrew Mortona737b3e2006-03-22 00:08:11 -08001615 start_cpu_timer(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return 0;
1617}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618__initcall(cpucache_init);
1619
1620/*
1621 * Interface to system's page allocator. No need to hold the cache-lock.
1622 *
1623 * If we requested dmaable memory, we will get it. Even if we
1624 * did not request dmaable memory, we might get it, but that
1625 * would be relatively rare and ignorable.
1626 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001627static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628{
1629 struct page *page;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001630 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 int i;
1632
Luke Yangd6fef9d2006-04-10 22:52:56 -07001633#ifndef CONFIG_MMU
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001634 /*
1635 * Nommu uses slab's for process anonymous memory allocations, and thus
1636 * requires __GFP_COMP to properly refcount higher order allocations
Luke Yangd6fef9d2006-04-10 22:52:56 -07001637 */
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001638 flags |= __GFP_COMP;
Luke Yangd6fef9d2006-04-10 22:52:56 -07001639#endif
Christoph Lameter765c4502006-09-27 01:50:08 -07001640
Christoph Lameter3c517a62006-12-06 20:33:29 -08001641 flags |= cachep->gfpflags;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001642
1643 page = alloc_pages_node(nodeid, flags, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 if (!page)
1645 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001647 nr_pages = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
Christoph Lameter972d1a72006-09-25 23:31:51 -07001649 add_zone_page_state(page_zone(page),
1650 NR_SLAB_RECLAIMABLE, nr_pages);
1651 else
1652 add_zone_page_state(page_zone(page),
1653 NR_SLAB_UNRECLAIMABLE, nr_pages);
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001654 for (i = 0; i < nr_pages; i++)
1655 __SetPageSlab(page + i);
1656 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
1659/*
1660 * Interface to system's page release.
1661 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001662static void kmem_freepages(struct kmem_cache *cachep, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001664 unsigned long i = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 struct page *page = virt_to_page(addr);
1666 const unsigned long nr_freed = i;
1667
Christoph Lameter972d1a72006-09-25 23:31:51 -07001668 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1669 sub_zone_page_state(page_zone(page),
1670 NR_SLAB_RECLAIMABLE, nr_freed);
1671 else
1672 sub_zone_page_state(page_zone(page),
1673 NR_SLAB_UNRECLAIMABLE, nr_freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 while (i--) {
Nick Pigginf205b2f2006-03-22 00:08:02 -08001675 BUG_ON(!PageSlab(page));
1676 __ClearPageSlab(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 page++;
1678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 if (current->reclaim_state)
1680 current->reclaim_state->reclaimed_slab += nr_freed;
1681 free_pages((unsigned long)addr, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682}
1683
1684static void kmem_rcu_free(struct rcu_head *head)
1685{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001686 struct slab_rcu *slab_rcu = (struct slab_rcu *)head;
Pekka Enberg343e0d72006-02-01 03:05:50 -08001687 struct kmem_cache *cachep = slab_rcu->cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 kmem_freepages(cachep, slab_rcu->addr);
1690 if (OFF_SLAB(cachep))
1691 kmem_cache_free(cachep->slabp_cache, slab_rcu);
1692}
1693
1694#if DEBUG
1695
1696#ifdef CONFIG_DEBUG_PAGEALLOC
Pekka Enberg343e0d72006-02-01 03:05:50 -08001697static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001698 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001700 int size = obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001702 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001704 if (size < 5 * sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return;
1706
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001707 *addr++ = 0x12345678;
1708 *addr++ = caller;
1709 *addr++ = smp_processor_id();
1710 size -= 3 * sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 {
1712 unsigned long *sptr = &caller;
1713 unsigned long svalue;
1714
1715 while (!kstack_end(sptr)) {
1716 svalue = *sptr++;
1717 if (kernel_text_address(svalue)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001718 *addr++ = svalue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719 size -= sizeof(unsigned long);
1720 if (size <= sizeof(unsigned long))
1721 break;
1722 }
1723 }
1724
1725 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001726 *addr++ = 0x87654321;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728#endif
1729
Pekka Enberg343e0d72006-02-01 03:05:50 -08001730static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001732 int size = obj_size(cachep);
1733 addr = &((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 memset(addr, val, size);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001736 *(unsigned char *)(addr + size - 1) = POISON_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737}
1738
1739static void dump_line(char *data, int offset, int limit)
1740{
1741 int i;
Dave Jonesaa83aa42006-09-29 01:59:51 -07001742 unsigned char error = 0;
1743 int bad_count = 0;
1744
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 printk(KERN_ERR "%03x:", offset);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001746 for (i = 0; i < limit; i++) {
1747 if (data[offset + i] != POISON_FREE) {
1748 error = data[offset + i];
1749 bad_count++;
1750 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001751 printk(" %02x", (unsigned char)data[offset + i]);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 printk("\n");
Dave Jonesaa83aa42006-09-29 01:59:51 -07001754
1755 if (bad_count == 1) {
1756 error ^= POISON_FREE;
1757 if (!(error & (error - 1))) {
1758 printk(KERN_ERR "Single bit error detected. Probably "
1759 "bad RAM.\n");
1760#ifdef CONFIG_X86
1761 printk(KERN_ERR "Run memtest86+ or a similar memory "
1762 "test tool.\n");
1763#else
1764 printk(KERN_ERR "Run a memory test tool.\n");
1765#endif
1766 }
1767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768}
1769#endif
1770
1771#if DEBUG
1772
Pekka Enberg343e0d72006-02-01 03:05:50 -08001773static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
1775 int i, size;
1776 char *realobj;
1777
1778 if (cachep->flags & SLAB_RED_ZONE) {
1779 printk(KERN_ERR "Redzone: 0x%lx/0x%lx.\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001780 *dbg_redzone1(cachep, objp),
1781 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 }
1783
1784 if (cachep->flags & SLAB_STORE_USER) {
1785 printk(KERN_ERR "Last user: [<%p>]",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001786 *dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 print_symbol("(%s)",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001788 (unsigned long)*dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 printk("\n");
1790 }
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001791 realobj = (char *)objp + obj_offset(cachep);
1792 size = obj_size(cachep);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001793 for (i = 0; i < size && lines; i += 16, lines--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 int limit;
1795 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001796 if (i + limit > size)
1797 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 dump_line(realobj, i, limit);
1799 }
1800}
1801
Pekka Enberg343e0d72006-02-01 03:05:50 -08001802static void check_poison_obj(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
1804 char *realobj;
1805 int size, i;
1806 int lines = 0;
1807
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001808 realobj = (char *)objp + obj_offset(cachep);
1809 size = obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001811 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 char exp = POISON_FREE;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001813 if (i == size - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 exp = POISON_END;
1815 if (realobj[i] != exp) {
1816 int limit;
1817 /* Mismatch ! */
1818 /* Print header */
1819 if (lines == 0) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001820 printk(KERN_ERR
David Howellse94a40c2007-04-02 23:46:28 +01001821 "Slab corruption: %s start=%p, len=%d\n",
1822 cachep->name, realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 print_objinfo(cachep, objp, 0);
1824 }
1825 /* Hexdump the affected line */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001826 i = (i / 16) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001828 if (i + limit > size)
1829 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 dump_line(realobj, i, limit);
1831 i += 16;
1832 lines++;
1833 /* Limit to 5 lines */
1834 if (lines > 5)
1835 break;
1836 }
1837 }
1838 if (lines != 0) {
1839 /* Print some data about the neighboring objects, if they
1840 * exist:
1841 */
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08001842 struct slab *slabp = virt_to_slab(objp);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001843 unsigned int objnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001845 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 if (objnr) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001847 objp = index_to_obj(cachep, slabp, objnr - 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001848 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001850 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 print_objinfo(cachep, objp, 2);
1852 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001853 if (objnr + 1 < cachep->num) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001854 objp = index_to_obj(cachep, slabp, objnr + 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001855 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001857 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 print_objinfo(cachep, objp, 2);
1859 }
1860 }
1861}
1862#endif
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864#if DEBUG
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001865/**
Randy Dunlap911851e2006-03-22 00:08:14 -08001866 * slab_destroy_objs - destroy a slab and its objects
1867 * @cachep: cache pointer being destroyed
1868 * @slabp: slab pointer being destroyed
1869 *
1870 * Call the registered destructor for each object in a slab that is being
1871 * destroyed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001872 */
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 int i;
1876 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001877 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
1879 if (cachep->flags & SLAB_POISON) {
1880#ifdef CONFIG_DEBUG_PAGEALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -08001881 if (cachep->buffer_size % PAGE_SIZE == 0 &&
1882 OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001883 kernel_map_pages(virt_to_page(objp),
Andrew Mortona737b3e2006-03-22 00:08:11 -08001884 cachep->buffer_size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 else
1886 check_poison_obj(cachep, objp);
1887#else
1888 check_poison_obj(cachep, objp);
1889#endif
1890 }
1891 if (cachep->flags & SLAB_RED_ZONE) {
1892 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
1893 slab_error(cachep, "start of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001894 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
1896 slab_error(cachep, "end of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001897 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 }
1899 if (cachep->dtor && !(cachep->flags & SLAB_POISON))
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001900 (cachep->dtor) (objp + obj_offset(cachep), cachep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001902}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903#else
Pekka Enberg343e0d72006-02-01 03:05:50 -08001904static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001905{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (cachep->dtor) {
1907 int i;
1908 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001909 void *objp = index_to_obj(cachep, slabp, i);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001910 (cachep->dtor) (objp, cachep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001913}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914#endif
1915
Randy Dunlap911851e2006-03-22 00:08:14 -08001916/**
1917 * slab_destroy - destroy and release all objects in a slab
1918 * @cachep: cache pointer being destroyed
1919 * @slabp: slab pointer being destroyed
1920 *
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001921 * Destroy all the objs in a slab, and release the mem back to the system.
Andrew Mortona737b3e2006-03-22 00:08:11 -08001922 * Before calling the slab must have been unlinked from the cache. The
1923 * cache-lock is not held/needed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001924 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001925static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001926{
1927 void *addr = slabp->s_mem - slabp->colouroff;
1928
1929 slab_destroy_objs(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
1931 struct slab_rcu *slab_rcu;
1932
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001933 slab_rcu = (struct slab_rcu *)slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 slab_rcu->cachep = cachep;
1935 slab_rcu->addr = addr;
1936 call_rcu(&slab_rcu->head, kmem_rcu_free);
1937 } else {
1938 kmem_freepages(cachep, addr);
Ingo Molnar873623d2006-07-13 14:44:38 +02001939 if (OFF_SLAB(cachep))
1940 kmem_cache_free(cachep->slabp_cache, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 }
1942}
1943
Andrew Mortona737b3e2006-03-22 00:08:11 -08001944/*
1945 * For setting up all the kmem_list3s for cache whose buffer_size is same as
1946 * size of kmem_list3.
1947 */
Andrew Mortona3a02be2007-05-06 14:49:31 -07001948static void __init set_up_list3s(struct kmem_cache *cachep, int index)
Christoph Lametere498be72005-09-09 13:03:32 -07001949{
1950 int node;
1951
1952 for_each_online_node(node) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001953 cachep->nodelists[node] = &initkmem_list3[index + node];
Christoph Lametere498be72005-09-09 13:03:32 -07001954 cachep->nodelists[node]->next_reap = jiffies +
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001955 REAPTIMEOUT_LIST3 +
1956 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lametere498be72005-09-09 13:03:32 -07001957 }
1958}
1959
Christoph Lameter117f6eb2006-09-25 23:31:37 -07001960static void __kmem_cache_destroy(struct kmem_cache *cachep)
1961{
1962 int i;
1963 struct kmem_list3 *l3;
1964
1965 for_each_online_cpu(i)
1966 kfree(cachep->array[i]);
1967
1968 /* NUMA: free the list3 structures */
1969 for_each_online_node(i) {
1970 l3 = cachep->nodelists[i];
1971 if (l3) {
1972 kfree(l3->shared);
1973 free_alien_cache(l3->alien);
1974 kfree(l3);
1975 }
1976 }
1977 kmem_cache_free(&cache_cache, cachep);
1978}
1979
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981/**
Randy.Dunlapa70773d2006-02-01 03:05:52 -08001982 * calculate_slab_order - calculate size (page order) of slabs
1983 * @cachep: pointer to the cache that is being created
1984 * @size: size of objects to be created in this cache.
1985 * @align: required alignment for the objects.
1986 * @flags: slab allocation flags
1987 *
1988 * Also calculates the number of objects per slab.
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001989 *
1990 * This could be made much more intelligent. For now, try to avoid using
1991 * high order pages for slabs. When the gfp() functions are more friendly
1992 * towards high-order requests, this should be changed.
1993 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001994static size_t calculate_slab_order(struct kmem_cache *cachep,
Randy Dunlapee13d782006-02-01 03:05:53 -08001995 size_t size, size_t align, unsigned long flags)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001996{
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02001997 unsigned long offslab_limit;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001998 size_t left_over = 0;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001999 int gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002000
Andrew Mortona737b3e2006-03-22 00:08:11 -08002001 for (gfporder = 0; gfporder <= MAX_GFP_ORDER; gfporder++) {
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002002 unsigned int num;
2003 size_t remainder;
2004
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002005 cache_estimate(gfporder, size, align, flags, &remainder, &num);
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002006 if (!num)
2007 continue;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002008
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002009 if (flags & CFLGS_OFF_SLAB) {
2010 /*
2011 * Max number of objs-per-slab for caches which
2012 * use off-slab slabs. Needed to avoid a possible
2013 * looping condition in cache_grow().
2014 */
2015 offslab_limit = size - sizeof(struct slab);
2016 offslab_limit /= sizeof(kmem_bufctl_t);
2017
2018 if (num > offslab_limit)
2019 break;
2020 }
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002021
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002022 /* Found something acceptable - save it away */
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002023 cachep->num = num;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002024 cachep->gfporder = gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002025 left_over = remainder;
2026
2027 /*
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08002028 * A VFS-reclaimable slab tends to have most allocations
2029 * as GFP_NOFS and we really don't want to have to be allocating
2030 * higher-order pages when we are unable to shrink dcache.
2031 */
2032 if (flags & SLAB_RECLAIM_ACCOUNT)
2033 break;
2034
2035 /*
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002036 * Large number of objects is good, but very large slabs are
2037 * currently bad for the gfp()s.
2038 */
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002039 if (gfporder >= slab_break_gfp_order)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002040 break;
2041
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002042 /*
2043 * Acceptable internal fragmentation?
2044 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002045 if (left_over * 8 <= (PAGE_SIZE << gfporder))
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002046 break;
2047 }
2048 return left_over;
2049}
2050
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002051static int setup_cpu_cache(struct kmem_cache *cachep)
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002052{
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002053 if (g_cpucache_up == FULL)
2054 return enable_cpucache(cachep);
2055
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002056 if (g_cpucache_up == NONE) {
2057 /*
2058 * Note: the first kmem_cache_create must create the cache
2059 * that's used by kmalloc(24), otherwise the creation of
2060 * further caches will BUG().
2061 */
2062 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2063
2064 /*
2065 * If the cache that's used by kmalloc(sizeof(kmem_list3)) is
2066 * the first cache, then we need to set up all its list3s,
2067 * otherwise the creation of further caches will BUG().
2068 */
2069 set_up_list3s(cachep, SIZE_AC);
2070 if (INDEX_AC == INDEX_L3)
2071 g_cpucache_up = PARTIAL_L3;
2072 else
2073 g_cpucache_up = PARTIAL_AC;
2074 } else {
2075 cachep->array[smp_processor_id()] =
2076 kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
2077
2078 if (g_cpucache_up == PARTIAL_AC) {
2079 set_up_list3s(cachep, SIZE_L3);
2080 g_cpucache_up = PARTIAL_L3;
2081 } else {
2082 int node;
2083 for_each_online_node(node) {
2084 cachep->nodelists[node] =
2085 kmalloc_node(sizeof(struct kmem_list3),
2086 GFP_KERNEL, node);
2087 BUG_ON(!cachep->nodelists[node]);
2088 kmem_list3_init(cachep->nodelists[node]);
2089 }
2090 }
2091 }
2092 cachep->nodelists[numa_node_id()]->next_reap =
2093 jiffies + REAPTIMEOUT_LIST3 +
2094 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
2095
2096 cpu_cache_get(cachep)->avail = 0;
2097 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
2098 cpu_cache_get(cachep)->batchcount = 1;
2099 cpu_cache_get(cachep)->touched = 0;
2100 cachep->batchcount = 1;
2101 cachep->limit = BOOT_CPUCACHE_ENTRIES;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002102 return 0;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002103}
2104
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002105/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 * kmem_cache_create - Create a cache.
2107 * @name: A string which is used in /proc/slabinfo to identify this cache.
2108 * @size: The size of objects to be created in this cache.
2109 * @align: The required alignment for the objects.
2110 * @flags: SLAB flags
2111 * @ctor: A constructor for the objects.
2112 * @dtor: A destructor for the objects.
2113 *
2114 * Returns a ptr to the cache on success, NULL on failure.
2115 * Cannot be called within a int, but can be interrupted.
2116 * The @ctor is run when new pages are allocated by the cache
2117 * and the @dtor is run before the pages are handed back.
2118 *
2119 * @name must be valid until the cache is destroyed. This implies that
Andrew Mortona737b3e2006-03-22 00:08:11 -08002120 * the module calling this has to destroy the cache before getting unloaded.
2121 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 * The flags are
2123 *
2124 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2125 * to catch references to uninitialised memory.
2126 *
2127 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2128 * for buffer overruns.
2129 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2131 * cacheline. This can be beneficial if you're counting cycles as closely
2132 * as davem.
2133 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002134struct kmem_cache *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135kmem_cache_create (const char *name, size_t size, size_t align,
Andrew Mortona737b3e2006-03-22 00:08:11 -08002136 unsigned long flags,
2137 void (*ctor)(void*, struct kmem_cache *, unsigned long),
Pekka Enberg343e0d72006-02-01 03:05:50 -08002138 void (*dtor)(void*, struct kmem_cache *, unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139{
2140 size_t left_over, slab_size, ralign;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07002141 struct kmem_cache *cachep = NULL, *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
2143 /*
2144 * Sanity checks... these are all serious usage bugs.
2145 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002146 if (!name || in_interrupt() || (size < BYTES_PER_WORD) ||
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002147 (size > (1 << MAX_OBJ_ORDER) * PAGE_SIZE) || (dtor && !ctor)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002148 printk(KERN_ERR "%s: Early error in slab %s\n", __FUNCTION__,
2149 name);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002150 BUG();
2151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Ravikiran G Thirumalaif0188f42006-02-10 01:51:13 -08002153 /*
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002154 * We use cache_chain_mutex to ensure a consistent view of
2155 * cpu_online_map as well. Please see cpuup_callback
Ravikiran G Thirumalaif0188f42006-02-10 01:51:13 -08002156 */
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002157 mutex_lock(&cache_chain_mutex);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002158
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07002159 list_for_each_entry(pc, &cache_chain, next) {
Andrew Morton4f12bb42005-11-07 00:58:00 -08002160 char tmp;
2161 int res;
2162
2163 /*
2164 * This happens when the module gets unloaded and doesn't
2165 * destroy its slab cache and no-one else reuses the vmalloc
2166 * area of the module. Print a warning.
2167 */
Andrew Morton138ae662006-12-06 20:36:41 -08002168 res = probe_kernel_address(pc->name, tmp);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002169 if (res) {
matzeb4169522007-05-06 14:49:52 -07002170 printk(KERN_ERR
2171 "SLAB: cache with size %d has lost its name\n",
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002172 pc->buffer_size);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002173 continue;
2174 }
2175
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002176 if (!strcmp(pc->name, name)) {
matzeb4169522007-05-06 14:49:52 -07002177 printk(KERN_ERR
2178 "kmem_cache_create: duplicate cache %s\n", name);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002179 dump_stack();
2180 goto oops;
2181 }
2182 }
2183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184#if DEBUG
2185 WARN_ON(strchr(name, ' ')); /* It confuses parsers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186#if FORCED_DEBUG
2187 /*
2188 * Enable redzoning and last user accounting, except for caches with
2189 * large objects, if the increased size would increase the object size
2190 * above the next power of two: caches with object sizes just above a
2191 * power of two have a significant amount of internal fragmentation.
2192 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002193 if (size < 4096 || fls(size - 1) == fls(size-1 + 3 * BYTES_PER_WORD))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002194 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 if (!(flags & SLAB_DESTROY_BY_RCU))
2196 flags |= SLAB_POISON;
2197#endif
2198 if (flags & SLAB_DESTROY_BY_RCU)
2199 BUG_ON(flags & SLAB_POISON);
2200#endif
2201 if (flags & SLAB_DESTROY_BY_RCU)
2202 BUG_ON(dtor);
2203
2204 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002205 * Always checks flags, a caller might be expecting debug support which
2206 * isn't available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 */
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002208 BUG_ON(flags & ~CREATE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Andrew Mortona737b3e2006-03-22 00:08:11 -08002210 /*
2211 * Check that size is in terms of words. This is needed to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 * unaligned accesses for some archs when redzoning is used, and makes
2213 * sure any on-slab bufctl's are also correctly aligned.
2214 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002215 if (size & (BYTES_PER_WORD - 1)) {
2216 size += (BYTES_PER_WORD - 1);
2217 size &= ~(BYTES_PER_WORD - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 }
2219
Andrew Mortona737b3e2006-03-22 00:08:11 -08002220 /* calculate the final buffer alignment: */
2221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 /* 1) arch recommendation: can be overridden for debug */
2223 if (flags & SLAB_HWCACHE_ALIGN) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002224 /*
2225 * Default alignment: as specified by the arch code. Except if
2226 * an object is really small, then squeeze multiple objects into
2227 * one cacheline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 */
2229 ralign = cache_line_size();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002230 while (size <= ralign / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 ralign /= 2;
2232 } else {
2233 ralign = BYTES_PER_WORD;
2234 }
Pekka Enbergca5f9702006-09-25 23:31:25 -07002235
2236 /*
2237 * Redzoning and user store require word alignment. Note this will be
2238 * overridden by architecture or caller mandated alignment if either
2239 * is greater than BYTES_PER_WORD.
2240 */
2241 if (flags & SLAB_RED_ZONE || flags & SLAB_STORE_USER)
2242 ralign = BYTES_PER_WORD;
2243
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002244 /* 2) arch mandated alignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 if (ralign < ARCH_SLAB_MINALIGN) {
2246 ralign = ARCH_SLAB_MINALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002248 /* 3) caller mandated alignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 if (ralign < align) {
2250 ralign = align;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 }
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002252 /* disable debug if necessary */
2253 if (ralign > BYTES_PER_WORD)
2254 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002255 /*
Pekka Enbergca5f9702006-09-25 23:31:25 -07002256 * 4) Store it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 */
2258 align = ralign;
2259
2260 /* Get cache's description obj. */
Christoph Lametere94b1762006-12-06 20:33:17 -08002261 cachep = kmem_cache_zalloc(&cache_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 if (!cachep)
Andrew Morton4f12bb42005-11-07 00:58:00 -08002263 goto oops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
2265#if DEBUG
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002266 cachep->obj_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Pekka Enbergca5f9702006-09-25 23:31:25 -07002268 /*
2269 * Both debugging options require word-alignment which is calculated
2270 * into align above.
2271 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 if (flags & SLAB_RED_ZONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 /* add space for red zone words */
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002274 cachep->obj_offset += BYTES_PER_WORD;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002275 size += 2 * BYTES_PER_WORD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 }
2277 if (flags & SLAB_STORE_USER) {
Pekka Enbergca5f9702006-09-25 23:31:25 -07002278 /* user store requires one word storage behind the end of
2279 * the real object.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 size += BYTES_PER_WORD;
2282 }
2283#if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002284 if (size >= malloc_sizes[INDEX_L3 + 1].cs_size
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002285 && cachep->obj_size > cache_line_size() && size < PAGE_SIZE) {
2286 cachep->obj_offset += PAGE_SIZE - size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 size = PAGE_SIZE;
2288 }
2289#endif
2290#endif
2291
Ingo Molnare0a42722006-06-23 02:03:46 -07002292 /*
2293 * Determine if the slab management is 'on' or 'off' slab.
2294 * (bootstrapping cannot cope with offslab caches so don't do
2295 * it too early on.)
2296 */
2297 if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 /*
2299 * Size is large, assume best to place the slab management obj
2300 * off-slab (should allow better packing of objs).
2301 */
2302 flags |= CFLGS_OFF_SLAB;
2303
2304 size = ALIGN(size, align);
2305
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08002306 left_over = calculate_slab_order(cachep, size, align, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
2308 if (!cachep->num) {
matzeb4169522007-05-06 14:49:52 -07002309 printk(KERN_ERR
2310 "kmem_cache_create: couldn't create cache %s.\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 kmem_cache_free(&cache_cache, cachep);
2312 cachep = NULL;
Andrew Morton4f12bb42005-11-07 00:58:00 -08002313 goto oops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002315 slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
2316 + sizeof(struct slab), align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318 /*
2319 * If the slab has been placed off-slab, and we have enough space then
2320 * move it on-slab. This is at the expense of any extra colouring.
2321 */
2322 if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
2323 flags &= ~CFLGS_OFF_SLAB;
2324 left_over -= slab_size;
2325 }
2326
2327 if (flags & CFLGS_OFF_SLAB) {
2328 /* really off slab. No need for manual alignment */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002329 slab_size =
2330 cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 }
2332
2333 cachep->colour_off = cache_line_size();
2334 /* Offset must be a multiple of the alignment. */
2335 if (cachep->colour_off < align)
2336 cachep->colour_off = align;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002337 cachep->colour = left_over / cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 cachep->slab_size = slab_size;
2339 cachep->flags = flags;
2340 cachep->gfpflags = 0;
Christoph Lameter4b51d662007-02-10 01:43:10 -08002341 if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 cachep->gfpflags |= GFP_DMA;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002343 cachep->buffer_size = size;
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08002344 cachep->reciprocal_buffer_size = reciprocal_value(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002346 if (flags & CFLGS_OFF_SLAB) {
Victor Fuscob2d55072005-09-10 00:26:36 -07002347 cachep->slabp_cache = kmem_find_general_cachep(slab_size, 0u);
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002348 /*
2349 * This is a possibility for one of the malloc_sizes caches.
2350 * But since we go off slab only for object size greater than
2351 * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
2352 * this should not happen at all.
2353 * But leave a BUG_ON for some lucky dude.
2354 */
2355 BUG_ON(!cachep->slabp_cache);
2356 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 cachep->ctor = ctor;
2358 cachep->dtor = dtor;
2359 cachep->name = name;
2360
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002361 if (setup_cpu_cache(cachep)) {
2362 __kmem_cache_destroy(cachep);
2363 cachep = NULL;
2364 goto oops;
2365 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 /* cache setup completed, link it into the list */
2368 list_add(&cachep->next, &cache_chain);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002369oops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 if (!cachep && (flags & SLAB_PANIC))
2371 panic("kmem_cache_create(): failed to create slab `%s'\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002372 name);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002373 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 return cachep;
2375}
2376EXPORT_SYMBOL(kmem_cache_create);
2377
2378#if DEBUG
2379static void check_irq_off(void)
2380{
2381 BUG_ON(!irqs_disabled());
2382}
2383
2384static void check_irq_on(void)
2385{
2386 BUG_ON(irqs_disabled());
2387}
2388
Pekka Enberg343e0d72006-02-01 03:05:50 -08002389static void check_spinlock_acquired(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390{
2391#ifdef CONFIG_SMP
2392 check_irq_off();
Christoph Lametere498be72005-09-09 13:03:32 -07002393 assert_spin_locked(&cachep->nodelists[numa_node_id()]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394#endif
2395}
Christoph Lametere498be72005-09-09 13:03:32 -07002396
Pekka Enberg343e0d72006-02-01 03:05:50 -08002397static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07002398{
2399#ifdef CONFIG_SMP
2400 check_irq_off();
2401 assert_spin_locked(&cachep->nodelists[node]->list_lock);
2402#endif
2403}
2404
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405#else
2406#define check_irq_off() do { } while(0)
2407#define check_irq_on() do { } while(0)
2408#define check_spinlock_acquired(x) do { } while(0)
Christoph Lametere498be72005-09-09 13:03:32 -07002409#define check_spinlock_acquired_node(x, y) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410#endif
2411
Christoph Lameteraab22072006-03-22 00:09:06 -08002412static void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
2413 struct array_cache *ac,
2414 int force, int node);
2415
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416static void do_drain(void *arg)
2417{
Andrew Mortona737b3e2006-03-22 00:08:11 -08002418 struct kmem_cache *cachep = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 struct array_cache *ac;
Christoph Lameterff694162005-09-22 21:44:02 -07002420 int node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
2422 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002423 ac = cpu_cache_get(cachep);
Christoph Lameterff694162005-09-22 21:44:02 -07002424 spin_lock(&cachep->nodelists[node]->list_lock);
2425 free_block(cachep, ac->entry, ac->avail, node);
2426 spin_unlock(&cachep->nodelists[node]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 ac->avail = 0;
2428}
2429
Pekka Enberg343e0d72006-02-01 03:05:50 -08002430static void drain_cpu_caches(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431{
Christoph Lametere498be72005-09-09 13:03:32 -07002432 struct kmem_list3 *l3;
2433 int node;
2434
Andrew Mortona07fa392006-03-22 00:08:17 -08002435 on_each_cpu(do_drain, cachep, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 check_irq_on();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002437 for_each_online_node(node) {
Christoph Lametere498be72005-09-09 13:03:32 -07002438 l3 = cachep->nodelists[node];
Roland Dreiera4523a82006-05-15 11:41:00 -07002439 if (l3 && l3->alien)
2440 drain_alien_cache(cachep, l3->alien);
2441 }
2442
2443 for_each_online_node(node) {
2444 l3 = cachep->nodelists[node];
2445 if (l3)
Christoph Lameteraab22072006-03-22 00:09:06 -08002446 drain_array(cachep, l3, l3->shared, 1, node);
Christoph Lametere498be72005-09-09 13:03:32 -07002447 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448}
2449
Christoph Lametered11d9e2006-06-30 01:55:45 -07002450/*
2451 * Remove slabs from the list of free slabs.
2452 * Specify the number of slabs to drain in tofree.
2453 *
2454 * Returns the actual number of slabs released.
2455 */
2456static int drain_freelist(struct kmem_cache *cache,
2457 struct kmem_list3 *l3, int tofree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458{
Christoph Lametered11d9e2006-06-30 01:55:45 -07002459 struct list_head *p;
2460 int nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Christoph Lametered11d9e2006-06-30 01:55:45 -07002463 nr_freed = 0;
2464 while (nr_freed < tofree && !list_empty(&l3->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465
Christoph Lametered11d9e2006-06-30 01:55:45 -07002466 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002467 p = l3->slabs_free.prev;
Christoph Lametered11d9e2006-06-30 01:55:45 -07002468 if (p == &l3->slabs_free) {
2469 spin_unlock_irq(&l3->list_lock);
2470 goto out;
2471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Christoph Lametered11d9e2006-06-30 01:55:45 -07002473 slabp = list_entry(p, struct slab, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474#if DEBUG
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002475 BUG_ON(slabp->inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476#endif
2477 list_del(&slabp->list);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002478 /*
2479 * Safe to drop the lock. The slab is no longer linked
2480 * to the cache.
2481 */
2482 l3->free_objects -= cache->num;
Christoph Lametere498be72005-09-09 13:03:32 -07002483 spin_unlock_irq(&l3->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002484 slab_destroy(cache, slabp);
2485 nr_freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 }
Christoph Lametered11d9e2006-06-30 01:55:45 -07002487out:
2488 return nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489}
2490
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002491/* Called with cache_chain_mutex held to protect against cpu hotplug */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002492static int __cache_shrink(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07002493{
2494 int ret = 0, i = 0;
2495 struct kmem_list3 *l3;
2496
2497 drain_cpu_caches(cachep);
2498
2499 check_irq_on();
2500 for_each_online_node(i) {
2501 l3 = cachep->nodelists[i];
Christoph Lametered11d9e2006-06-30 01:55:45 -07002502 if (!l3)
2503 continue;
2504
2505 drain_freelist(cachep, l3, l3->free_objects);
2506
2507 ret += !list_empty(&l3->slabs_full) ||
2508 !list_empty(&l3->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07002509 }
2510 return (ret ? 1 : 0);
2511}
2512
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513/**
2514 * kmem_cache_shrink - Shrink a cache.
2515 * @cachep: The cache to shrink.
2516 *
2517 * Releases as many slabs as possible for a cache.
2518 * To help debugging, a zero exit status indicates all slabs were released.
2519 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002520int kmem_cache_shrink(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002522 int ret;
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002523 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002525 mutex_lock(&cache_chain_mutex);
2526 ret = __cache_shrink(cachep);
2527 mutex_unlock(&cache_chain_mutex);
2528 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529}
2530EXPORT_SYMBOL(kmem_cache_shrink);
2531
2532/**
2533 * kmem_cache_destroy - delete a cache
2534 * @cachep: the cache to destroy
2535 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002536 * Remove a &struct kmem_cache object from the slab cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 *
2538 * It is expected this function will be called by a module when it is
2539 * unloaded. This will remove the cache completely, and avoid a duplicate
2540 * cache being allocated each time a module is loaded and unloaded, if the
2541 * module doesn't have persistent in-kernel storage across loads and unloads.
2542 *
2543 * The cache must be empty before calling this function.
2544 *
2545 * The caller must guarantee that noone will allocate memory from the cache
2546 * during the kmem_cache_destroy().
2547 */
Alexey Dobriyan133d2052006-09-27 01:49:41 -07002548void kmem_cache_destroy(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002550 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 /* Find the cache in the chain of caches. */
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002553 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 /*
2555 * the chain is never empty, cache_cache is never destroyed
2556 */
2557 list_del(&cachep->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 if (__cache_shrink(cachep)) {
2559 slab_error(cachep, "Can't free all objects");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002560 list_add(&cachep->next, &cache_chain);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002561 mutex_unlock(&cache_chain_mutex);
Alexey Dobriyan133d2052006-09-27 01:49:41 -07002562 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563 }
2564
2565 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU))
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002566 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567
Christoph Lameter117f6eb2006-09-25 23:31:37 -07002568 __kmem_cache_destroy(cachep);
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002569 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570}
2571EXPORT_SYMBOL(kmem_cache_destroy);
2572
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002573/*
2574 * Get the memory for a slab management obj.
2575 * For a slab cache when the slab descriptor is off-slab, slab descriptors
2576 * always come from malloc_sizes caches. The slab descriptor cannot
2577 * come from the same cache which is getting created because,
2578 * when we are searching for an appropriate cache for these
2579 * descriptors in kmem_cache_create, we search through the malloc_sizes array.
2580 * If we are creating a malloc_sizes cache here it would not be visible to
2581 * kmem_find_general_cachep till the initialization is complete.
2582 * Hence we cannot have slabp_cache same as the original cache.
2583 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002584static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002585 int colour_off, gfp_t local_flags,
2586 int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002587{
2588 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002589
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 if (OFF_SLAB(cachep)) {
2591 /* Slab management obj is off-slab. */
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002592 slabp = kmem_cache_alloc_node(cachep->slabp_cache,
Christoph Lameter3c517a62006-12-06 20:33:29 -08002593 local_flags & ~GFP_THISNODE, nodeid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 if (!slabp)
2595 return NULL;
2596 } else {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002597 slabp = objp + colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 colour_off += cachep->slab_size;
2599 }
2600 slabp->inuse = 0;
2601 slabp->colouroff = colour_off;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002602 slabp->s_mem = objp + colour_off;
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002603 slabp->nodeid = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 return slabp;
2605}
2606
2607static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
2608{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002609 return (kmem_bufctl_t *) (slabp + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610}
2611
Pekka Enberg343e0d72006-02-01 03:05:50 -08002612static void cache_init_objs(struct kmem_cache *cachep,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002613 struct slab *slabp, unsigned long ctor_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614{
2615 int i;
2616
2617 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002618 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619#if DEBUG
2620 /* need to poison the objs? */
2621 if (cachep->flags & SLAB_POISON)
2622 poison_obj(cachep, objp, POISON_FREE);
2623 if (cachep->flags & SLAB_STORE_USER)
2624 *dbg_userword(cachep, objp) = NULL;
2625
2626 if (cachep->flags & SLAB_RED_ZONE) {
2627 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2628 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2629 }
2630 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002631 * Constructors are not allowed to allocate memory from the same
2632 * cache which they are a constructor for. Otherwise, deadlock.
2633 * They must also be threaded.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 */
2635 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002636 cachep->ctor(objp + obj_offset(cachep), cachep,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002637 ctor_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
2639 if (cachep->flags & SLAB_RED_ZONE) {
2640 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2641 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002642 " end of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2644 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002645 " start of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 }
Andrew Mortona737b3e2006-03-22 00:08:11 -08002647 if ((cachep->buffer_size % PAGE_SIZE) == 0 &&
2648 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002649 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002650 cachep->buffer_size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651#else
2652 if (cachep->ctor)
2653 cachep->ctor(objp, cachep, ctor_flags);
2654#endif
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002655 slab_bufctl(slabp)[i] = i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002657 slab_bufctl(slabp)[i - 1] = BUFCTL_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 slabp->free = 0;
2659}
2660
Pekka Enberg343e0d72006-02-01 03:05:50 -08002661static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662{
Christoph Lameter4b51d662007-02-10 01:43:10 -08002663 if (CONFIG_ZONE_DMA_FLAG) {
2664 if (flags & GFP_DMA)
2665 BUG_ON(!(cachep->gfpflags & GFP_DMA));
2666 else
2667 BUG_ON(cachep->gfpflags & GFP_DMA);
2668 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669}
2670
Andrew Mortona737b3e2006-03-22 00:08:11 -08002671static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
2672 int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002673{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002674 void *objp = index_to_obj(cachep, slabp, slabp->free);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002675 kmem_bufctl_t next;
2676
2677 slabp->inuse++;
2678 next = slab_bufctl(slabp)[slabp->free];
2679#if DEBUG
2680 slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
2681 WARN_ON(slabp->nodeid != nodeid);
2682#endif
2683 slabp->free = next;
2684
2685 return objp;
2686}
2687
Andrew Mortona737b3e2006-03-22 00:08:11 -08002688static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
2689 void *objp, int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002690{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002691 unsigned int objnr = obj_to_index(cachep, slabp, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002692
2693#if DEBUG
2694 /* Verify that the slab belongs to the intended node */
2695 WARN_ON(slabp->nodeid != nodeid);
2696
Al Viro871751e2006-03-25 03:06:39 -08002697 if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) {
Matthew Dobson78d382d2006-02-01 03:05:47 -08002698 printk(KERN_ERR "slab: double free detected in cache "
Andrew Mortona737b3e2006-03-22 00:08:11 -08002699 "'%s', objp %p\n", cachep->name, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002700 BUG();
2701 }
2702#endif
2703 slab_bufctl(slabp)[objnr] = slabp->free;
2704 slabp->free = objnr;
2705 slabp->inuse--;
2706}
2707
Pekka Enberg47768742006-06-23 02:03:07 -07002708/*
2709 * Map pages beginning at addr to the given cache and slab. This is required
2710 * for the slab allocator to be able to lookup the cache and slab of a
2711 * virtual address for kfree, ksize, kmem_ptr_validate, and slab debugging.
2712 */
2713static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
2714 void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002715{
Pekka Enberg47768742006-06-23 02:03:07 -07002716 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 struct page *page;
2718
Pekka Enberg47768742006-06-23 02:03:07 -07002719 page = virt_to_page(addr);
Nick Piggin84097512006-03-22 00:08:34 -08002720
Pekka Enberg47768742006-06-23 02:03:07 -07002721 nr_pages = 1;
Nick Piggin84097512006-03-22 00:08:34 -08002722 if (likely(!PageCompound(page)))
Pekka Enberg47768742006-06-23 02:03:07 -07002723 nr_pages <<= cache->gfporder;
2724
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 do {
Pekka Enberg47768742006-06-23 02:03:07 -07002726 page_set_cache(page, cache);
2727 page_set_slab(page, slab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 page++;
Pekka Enberg47768742006-06-23 02:03:07 -07002729 } while (--nr_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730}
2731
2732/*
2733 * Grow (by 1) the number of slabs within a cache. This is called by
2734 * kmem_cache_alloc() when there are no active objs left in a cache.
2735 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002736static int cache_grow(struct kmem_cache *cachep,
2737 gfp_t flags, int nodeid, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002739 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002740 size_t offset;
2741 gfp_t local_flags;
2742 unsigned long ctor_flags;
Christoph Lametere498be72005-09-09 13:03:32 -07002743 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Andrew Mortona737b3e2006-03-22 00:08:11 -08002745 /*
2746 * Be lazy and only check for valid flags here, keeping it out of the
2747 * critical path in kmem_cache_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 */
Christoph Lameter441e1432006-12-06 20:33:19 -08002749 BUG_ON(flags & ~(GFP_DMA | GFP_LEVEL_MASK | __GFP_NO_GROW));
Christoph Lameter6e0eaa42006-12-06 20:33:10 -08002750 if (flags & __GFP_NO_GROW)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 return 0;
2752
2753 ctor_flags = SLAB_CTOR_CONSTRUCTOR;
Christoph Lametera06d72c2006-12-06 20:33:12 -08002754 local_flags = (flags & GFP_LEVEL_MASK);
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002755 /* Take the l3 list lock to change the colour_next on this node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 check_irq_off();
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002757 l3 = cachep->nodelists[nodeid];
2758 spin_lock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
2760 /* Get colour for the slab, and cal the next value. */
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002761 offset = l3->colour_next;
2762 l3->colour_next++;
2763 if (l3->colour_next >= cachep->colour)
2764 l3->colour_next = 0;
2765 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002767 offset *= cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
2769 if (local_flags & __GFP_WAIT)
2770 local_irq_enable();
2771
2772 /*
2773 * The test for missing atomic flag is performed here, rather than
2774 * the more obvious place, simply to reduce the critical path length
2775 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2776 * will eventually be caught here (where it matters).
2777 */
2778 kmem_flagcheck(cachep, flags);
2779
Andrew Mortona737b3e2006-03-22 00:08:11 -08002780 /*
2781 * Get mem for the objs. Attempt to allocate a physical page from
2782 * 'nodeid'.
Christoph Lametere498be72005-09-09 13:03:32 -07002783 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002784 if (!objp)
2785 objp = kmem_getpages(cachep, flags, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002786 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 goto failed;
2788
2789 /* Get slab management. */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002790 slabp = alloc_slabmgmt(cachep, objp, offset,
2791 local_flags & ~GFP_THISNODE, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002792 if (!slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 goto opps1;
2794
Christoph Lametere498be72005-09-09 13:03:32 -07002795 slabp->nodeid = nodeid;
Pekka Enberg47768742006-06-23 02:03:07 -07002796 slab_map_pages(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
2798 cache_init_objs(cachep, slabp, ctor_flags);
2799
2800 if (local_flags & __GFP_WAIT)
2801 local_irq_disable();
2802 check_irq_off();
Christoph Lametere498be72005-09-09 13:03:32 -07002803 spin_lock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
2805 /* Make slab active. */
Christoph Lametere498be72005-09-09 13:03:32 -07002806 list_add_tail(&slabp->list, &(l3->slabs_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 STATS_INC_GROWN(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07002808 l3->free_objects += cachep->num;
2809 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002811opps1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 kmem_freepages(cachep, objp);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002813failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 if (local_flags & __GFP_WAIT)
2815 local_irq_disable();
2816 return 0;
2817}
2818
2819#if DEBUG
2820
2821/*
2822 * Perform extra freeing checks:
2823 * - detect bad pointers.
2824 * - POISON/RED_ZONE checking
2825 * - destructor calls, for caches with POISON+dtor
2826 */
2827static void kfree_debugcheck(const void *objp)
2828{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 if (!virt_addr_valid(objp)) {
2830 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002831 (unsigned long)objp);
2832 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834}
2835
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002836static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2837{
2838 unsigned long redzone1, redzone2;
2839
2840 redzone1 = *dbg_redzone1(cache, obj);
2841 redzone2 = *dbg_redzone2(cache, obj);
2842
2843 /*
2844 * Redzone is ok.
2845 */
2846 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2847 return;
2848
2849 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2850 slab_error(cache, "double free detected");
2851 else
2852 slab_error(cache, "memory outside object was overwritten");
2853
2854 printk(KERN_ERR "%p: redzone 1:0x%lx, redzone 2:0x%lx.\n",
2855 obj, redzone1, redzone2);
2856}
2857
Pekka Enberg343e0d72006-02-01 03:05:50 -08002858static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002859 void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860{
2861 struct page *page;
2862 unsigned int objnr;
2863 struct slab *slabp;
2864
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002865 objp -= obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 kfree_debugcheck(objp);
Christoph Lameterb49af682007-05-06 14:49:41 -07002867 page = virt_to_head_page(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
Pekka Enberg065d41c2005-11-13 16:06:46 -08002869 slabp = page_get_slab(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870
2871 if (cachep->flags & SLAB_RED_ZONE) {
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002872 verify_redzone_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2874 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2875 }
2876 if (cachep->flags & SLAB_STORE_USER)
2877 *dbg_userword(cachep, objp) = caller;
2878
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002879 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880
2881 BUG_ON(objnr >= cachep->num);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002882 BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 if (cachep->flags & SLAB_POISON && cachep->dtor) {
2885 /* we want to cache poison the object,
2886 * call the destruction callback
2887 */
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002888 cachep->dtor(objp + obj_offset(cachep), cachep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 }
Al Viro871751e2006-03-25 03:06:39 -08002890#ifdef CONFIG_DEBUG_SLAB_LEAK
2891 slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
2892#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 if (cachep->flags & SLAB_POISON) {
2894#ifdef CONFIG_DEBUG_PAGEALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -08002895 if ((cachep->buffer_size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 store_stackinfo(cachep, objp, (unsigned long)caller);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002897 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002898 cachep->buffer_size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 } else {
2900 poison_obj(cachep, objp, POISON_FREE);
2901 }
2902#else
2903 poison_obj(cachep, objp, POISON_FREE);
2904#endif
2905 }
2906 return objp;
2907}
2908
Pekka Enberg343e0d72006-02-01 03:05:50 -08002909static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910{
2911 kmem_bufctl_t i;
2912 int entries = 0;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002913
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 /* Check slab's freelist to see if this obj is there. */
2915 for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
2916 entries++;
2917 if (entries > cachep->num || i >= cachep->num)
2918 goto bad;
2919 }
2920 if (entries != cachep->num - slabp->inuse) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002921bad:
2922 printk(KERN_ERR "slab: Internal list corruption detected in "
2923 "cache '%s'(%d), slabp %p(%d). Hexdump:\n",
2924 cachep->name, cachep->num, slabp, slabp->inuse);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002925 for (i = 0;
Linus Torvalds264132b2006-03-06 12:10:07 -08002926 i < sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002927 i++) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002928 if (i % 16 == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 printk("\n%03x:", i);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002930 printk(" %02x", ((unsigned char *)slabp)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 }
2932 printk("\n");
2933 BUG();
2934 }
2935}
2936#else
2937#define kfree_debugcheck(x) do { } while(0)
2938#define cache_free_debugcheck(x,objp,z) (objp)
2939#define check_slabp(x,y) do { } while(0)
2940#endif
2941
Pekka Enberg343e0d72006-02-01 03:05:50 -08002942static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
2944 int batchcount;
2945 struct kmem_list3 *l3;
2946 struct array_cache *ac;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002947 int node;
2948
2949 node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
2951 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002952 ac = cpu_cache_get(cachep);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002953retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 batchcount = ac->batchcount;
2955 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002956 /*
2957 * If there was little recent activity on this cache, then
2958 * perform only a partial refill. Otherwise we could generate
2959 * refill bouncing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 */
2961 batchcount = BATCHREFILL_LIMIT;
2962 }
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002963 l3 = cachep->nodelists[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Christoph Lametere498be72005-09-09 13:03:32 -07002965 BUG_ON(ac->avail > 0 || !l3);
2966 spin_lock(&l3->list_lock);
2967
Christoph Lameter3ded1752006-03-25 03:06:44 -08002968 /* See if we can refill from the shared array */
2969 if (l3->shared && transfer_objects(ac, l3->shared, batchcount))
2970 goto alloc_done;
2971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 while (batchcount > 0) {
2973 struct list_head *entry;
2974 struct slab *slabp;
2975 /* Get slab alloc is to come from. */
2976 entry = l3->slabs_partial.next;
2977 if (entry == &l3->slabs_partial) {
2978 l3->free_touched = 1;
2979 entry = l3->slabs_free.next;
2980 if (entry == &l3->slabs_free)
2981 goto must_grow;
2982 }
2983
2984 slabp = list_entry(entry, struct slab, list);
2985 check_slabp(cachep, slabp);
2986 check_spinlock_acquired(cachep);
Pekka Enberg714b81712007-05-06 14:49:03 -07002987
2988 /*
2989 * The slab was either on partial or free list so
2990 * there must be at least one object available for
2991 * allocation.
2992 */
2993 BUG_ON(slabp->inuse < 0 || slabp->inuse >= cachep->num);
2994
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995 while (slabp->inuse < cachep->num && batchcount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 STATS_INC_ALLOCED(cachep);
2997 STATS_INC_ACTIVE(cachep);
2998 STATS_SET_HIGH(cachep);
2999
Matthew Dobson78d382d2006-02-01 03:05:47 -08003000 ac->entry[ac->avail++] = slab_get_obj(cachep, slabp,
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07003001 node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 }
3003 check_slabp(cachep, slabp);
3004
3005 /* move slabp to correct slabp list: */
3006 list_del(&slabp->list);
3007 if (slabp->free == BUFCTL_END)
3008 list_add(&slabp->list, &l3->slabs_full);
3009 else
3010 list_add(&slabp->list, &l3->slabs_partial);
3011 }
3012
Andrew Mortona737b3e2006-03-22 00:08:11 -08003013must_grow:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 l3->free_objects -= ac->avail;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003015alloc_done:
Christoph Lametere498be72005-09-09 13:03:32 -07003016 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017
3018 if (unlikely(!ac->avail)) {
3019 int x;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003020 x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07003021
Andrew Mortona737b3e2006-03-22 00:08:11 -08003022 /* cache_grow can reenable interrupts, then ac could change. */
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003023 ac = cpu_cache_get(cachep);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003024 if (!x && ac->avail == 0) /* no objects in sight? abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 return NULL;
3026
Andrew Mortona737b3e2006-03-22 00:08:11 -08003027 if (!ac->avail) /* objects refilled by interrupt? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 goto retry;
3029 }
3030 ac->touched = 1;
Christoph Lametere498be72005-09-09 13:03:32 -07003031 return ac->entry[--ac->avail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032}
3033
Andrew Mortona737b3e2006-03-22 00:08:11 -08003034static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
3035 gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036{
3037 might_sleep_if(flags & __GFP_WAIT);
3038#if DEBUG
3039 kmem_flagcheck(cachep, flags);
3040#endif
3041}
3042
3043#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003044static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
3045 gfp_t flags, void *objp, void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003047 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 return objp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003049 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050#ifdef CONFIG_DEBUG_PAGEALLOC
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003051 if ((cachep->buffer_size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003052 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003053 cachep->buffer_size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 else
3055 check_poison_obj(cachep, objp);
3056#else
3057 check_poison_obj(cachep, objp);
3058#endif
3059 poison_obj(cachep, objp, POISON_INUSE);
3060 }
3061 if (cachep->flags & SLAB_STORE_USER)
3062 *dbg_userword(cachep, objp) = caller;
3063
3064 if (cachep->flags & SLAB_RED_ZONE) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003065 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
3066 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
3067 slab_error(cachep, "double free, or memory outside"
3068 " object was overwritten");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003069 printk(KERN_ERR
Andrew Mortona737b3e2006-03-22 00:08:11 -08003070 "%p: redzone 1:0x%lx, redzone 2:0x%lx\n",
3071 objp, *dbg_redzone1(cachep, objp),
3072 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 }
3074 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
3075 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
3076 }
Al Viro871751e2006-03-25 03:06:39 -08003077#ifdef CONFIG_DEBUG_SLAB_LEAK
3078 {
3079 struct slab *slabp;
3080 unsigned objnr;
3081
Christoph Lameterb49af682007-05-06 14:49:41 -07003082 slabp = page_get_slab(virt_to_head_page(objp));
Al Viro871751e2006-03-25 03:06:39 -08003083 objnr = (unsigned)(objp - slabp->s_mem) / cachep->buffer_size;
3084 slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE;
3085 }
3086#endif
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003087 objp += obj_offset(cachep);
Christoph Lameter4f104932007-05-06 14:50:17 -07003088 if (cachep->ctor && cachep->flags & SLAB_POISON)
3089 cachep->ctor(objp, cachep, SLAB_CTOR_CONSTRUCTOR);
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003090#if ARCH_SLAB_MINALIGN
3091 if ((u32)objp & (ARCH_SLAB_MINALIGN-1)) {
3092 printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
3093 objp, ARCH_SLAB_MINALIGN);
3094 }
3095#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 return objp;
3097}
3098#else
3099#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
3100#endif
3101
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003102#ifdef CONFIG_FAILSLAB
3103
3104static struct failslab_attr {
3105
3106 struct fault_attr attr;
3107
3108 u32 ignore_gfp_wait;
3109#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
3110 struct dentry *ignore_gfp_wait_file;
3111#endif
3112
3113} failslab = {
3114 .attr = FAULT_ATTR_INITIALIZER,
Don Mullis6b1b60f2006-12-08 02:39:53 -08003115 .ignore_gfp_wait = 1,
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003116};
3117
3118static int __init setup_failslab(char *str)
3119{
3120 return setup_fault_attr(&failslab.attr, str);
3121}
3122__setup("failslab=", setup_failslab);
3123
3124static int should_failslab(struct kmem_cache *cachep, gfp_t flags)
3125{
3126 if (cachep == &cache_cache)
3127 return 0;
3128 if (flags & __GFP_NOFAIL)
3129 return 0;
3130 if (failslab.ignore_gfp_wait && (flags & __GFP_WAIT))
3131 return 0;
3132
3133 return should_fail(&failslab.attr, obj_size(cachep));
3134}
3135
3136#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
3137
3138static int __init failslab_debugfs(void)
3139{
3140 mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
3141 struct dentry *dir;
3142 int err;
3143
Akinobu Mita824ebef2007-05-06 14:49:58 -07003144 err = init_fault_attr_dentries(&failslab.attr, "failslab");
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003145 if (err)
3146 return err;
3147 dir = failslab.attr.dentries.dir;
3148
3149 failslab.ignore_gfp_wait_file =
3150 debugfs_create_bool("ignore-gfp-wait", mode, dir,
3151 &failslab.ignore_gfp_wait);
3152
3153 if (!failslab.ignore_gfp_wait_file) {
3154 err = -ENOMEM;
3155 debugfs_remove(failslab.ignore_gfp_wait_file);
3156 cleanup_fault_attr_dentries(&failslab.attr);
3157 }
3158
3159 return err;
3160}
3161
3162late_initcall(failslab_debugfs);
3163
3164#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
3165
3166#else /* CONFIG_FAILSLAB */
3167
3168static inline int should_failslab(struct kmem_cache *cachep, gfp_t flags)
3169{
3170 return 0;
3171}
3172
3173#endif /* CONFIG_FAILSLAB */
3174
Pekka Enberg343e0d72006-02-01 03:05:50 -08003175static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003177 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 struct array_cache *ac;
3179
Alok N Kataria5c382302005-09-27 21:45:46 -07003180 check_irq_off();
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003181
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003182 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 if (likely(ac->avail)) {
3184 STATS_INC_ALLOCHIT(cachep);
3185 ac->touched = 1;
Christoph Lametere498be72005-09-09 13:03:32 -07003186 objp = ac->entry[--ac->avail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 } else {
3188 STATS_INC_ALLOCMISS(cachep);
3189 objp = cache_alloc_refill(cachep, flags);
3190 }
Alok N Kataria5c382302005-09-27 21:45:46 -07003191 return objp;
3192}
3193
Christoph Lametere498be72005-09-09 13:03:32 -07003194#ifdef CONFIG_NUMA
3195/*
Paul Jacksonb2455392006-03-24 03:16:12 -08003196 * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
Paul Jacksonc61afb12006-03-24 03:16:08 -08003197 *
3198 * If we are in_interrupt, then process context, including cpusets and
3199 * mempolicy, may not apply and should not be used for allocation policy.
3200 */
3201static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
3202{
3203 int nid_alloc, nid_here;
3204
Christoph Lameter765c4502006-09-27 01:50:08 -07003205 if (in_interrupt() || (flags & __GFP_THISNODE))
Paul Jacksonc61afb12006-03-24 03:16:08 -08003206 return NULL;
3207 nid_alloc = nid_here = numa_node_id();
3208 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
3209 nid_alloc = cpuset_mem_spread_node();
3210 else if (current->mempolicy)
3211 nid_alloc = slab_node(current->mempolicy);
3212 if (nid_alloc != nid_here)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003213 return ____cache_alloc_node(cachep, flags, nid_alloc);
Paul Jacksonc61afb12006-03-24 03:16:08 -08003214 return NULL;
3215}
3216
3217/*
Christoph Lameter765c4502006-09-27 01:50:08 -07003218 * Fallback function if there was no memory available and no objects on a
Christoph Lameter3c517a62006-12-06 20:33:29 -08003219 * certain node and fall back is permitted. First we scan all the
3220 * available nodelists for available objects. If that fails then we
3221 * perform an allocation without specifying a node. This allows the page
3222 * allocator to do its reclaim / fallback magic. We then insert the
3223 * slab into the proper nodelist and then allocate from it.
Christoph Lameter765c4502006-09-27 01:50:08 -07003224 */
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003225static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
Christoph Lameter765c4502006-09-27 01:50:08 -07003226{
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003227 struct zonelist *zonelist;
3228 gfp_t local_flags;
Christoph Lameter765c4502006-09-27 01:50:08 -07003229 struct zone **z;
3230 void *obj = NULL;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003231 int nid;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003232
3233 if (flags & __GFP_THISNODE)
3234 return NULL;
3235
3236 zonelist = &NODE_DATA(slab_node(current->mempolicy))
3237 ->node_zonelists[gfp_zone(flags)];
3238 local_flags = (flags & GFP_LEVEL_MASK);
Christoph Lameter765c4502006-09-27 01:50:08 -07003239
Christoph Lameter3c517a62006-12-06 20:33:29 -08003240retry:
3241 /*
3242 * Look through allowed nodes for objects available
3243 * from existing per node queues.
3244 */
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003245 for (z = zonelist->zones; *z && !obj; z++) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003246 nid = zone_to_nid(*z);
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003247
Paul Jackson02a0e532006-12-13 00:34:25 -08003248 if (cpuset_zone_allowed_hardwall(*z, flags) &&
Christoph Lameter3c517a62006-12-06 20:33:29 -08003249 cache->nodelists[nid] &&
3250 cache->nodelists[nid]->free_objects)
3251 obj = ____cache_alloc_node(cache,
3252 flags | GFP_THISNODE, nid);
3253 }
3254
Hugh Dickinsb6a60452007-01-05 16:36:36 -08003255 if (!obj && !(flags & __GFP_NO_GROW)) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003256 /*
3257 * This allocation will be performed within the constraints
3258 * of the current cpuset / memory policy requirements.
3259 * We may trigger various forms of reclaim on the allowed
3260 * set and go into memory reserves if necessary.
3261 */
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003262 if (local_flags & __GFP_WAIT)
3263 local_irq_enable();
3264 kmem_flagcheck(cache, flags);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003265 obj = kmem_getpages(cache, flags, -1);
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003266 if (local_flags & __GFP_WAIT)
3267 local_irq_disable();
Christoph Lameter3c517a62006-12-06 20:33:29 -08003268 if (obj) {
3269 /*
3270 * Insert into the appropriate per node queues
3271 */
3272 nid = page_to_nid(virt_to_page(obj));
3273 if (cache_grow(cache, flags, nid, obj)) {
3274 obj = ____cache_alloc_node(cache,
3275 flags | GFP_THISNODE, nid);
3276 if (!obj)
3277 /*
3278 * Another processor may allocate the
3279 * objects in the slab since we are
3280 * not holding any locks.
3281 */
3282 goto retry;
3283 } else {
Hugh Dickinsb6a60452007-01-05 16:36:36 -08003284 /* cache_grow already freed obj */
Christoph Lameter3c517a62006-12-06 20:33:29 -08003285 obj = NULL;
3286 }
3287 }
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003288 }
Christoph Lameter765c4502006-09-27 01:50:08 -07003289 return obj;
3290}
3291
3292/*
Christoph Lametere498be72005-09-09 13:03:32 -07003293 * A interface to enable slab creation on nodeid
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003295static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003296 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07003297{
3298 struct list_head *entry;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003299 struct slab *slabp;
3300 struct kmem_list3 *l3;
3301 void *obj;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003302 int x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003304 l3 = cachep->nodelists[nodeid];
3305 BUG_ON(!l3);
Christoph Lametere498be72005-09-09 13:03:32 -07003306
Andrew Mortona737b3e2006-03-22 00:08:11 -08003307retry:
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003308 check_irq_off();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003309 spin_lock(&l3->list_lock);
3310 entry = l3->slabs_partial.next;
3311 if (entry == &l3->slabs_partial) {
3312 l3->free_touched = 1;
3313 entry = l3->slabs_free.next;
3314 if (entry == &l3->slabs_free)
3315 goto must_grow;
3316 }
Christoph Lametere498be72005-09-09 13:03:32 -07003317
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003318 slabp = list_entry(entry, struct slab, list);
3319 check_spinlock_acquired_node(cachep, nodeid);
3320 check_slabp(cachep, slabp);
Christoph Lametere498be72005-09-09 13:03:32 -07003321
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003322 STATS_INC_NODEALLOCS(cachep);
3323 STATS_INC_ACTIVE(cachep);
3324 STATS_SET_HIGH(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003325
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003326 BUG_ON(slabp->inuse == cachep->num);
Christoph Lametere498be72005-09-09 13:03:32 -07003327
Matthew Dobson78d382d2006-02-01 03:05:47 -08003328 obj = slab_get_obj(cachep, slabp, nodeid);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003329 check_slabp(cachep, slabp);
3330 l3->free_objects--;
3331 /* move slabp to correct slabp list: */
3332 list_del(&slabp->list);
Christoph Lametere498be72005-09-09 13:03:32 -07003333
Andrew Mortona737b3e2006-03-22 00:08:11 -08003334 if (slabp->free == BUFCTL_END)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003335 list_add(&slabp->list, &l3->slabs_full);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003336 else
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003337 list_add(&slabp->list, &l3->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07003338
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003339 spin_unlock(&l3->list_lock);
3340 goto done;
Christoph Lametere498be72005-09-09 13:03:32 -07003341
Andrew Mortona737b3e2006-03-22 00:08:11 -08003342must_grow:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003343 spin_unlock(&l3->list_lock);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003344 x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
Christoph Lameter765c4502006-09-27 01:50:08 -07003345 if (x)
3346 goto retry;
Christoph Lametere498be72005-09-09 13:03:32 -07003347
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003348 return fallback_alloc(cachep, flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003349
Andrew Mortona737b3e2006-03-22 00:08:11 -08003350done:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003351 return obj;
Christoph Lametere498be72005-09-09 13:03:32 -07003352}
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003353
3354/**
3355 * kmem_cache_alloc_node - Allocate an object on the specified node
3356 * @cachep: The cache to allocate from.
3357 * @flags: See kmalloc().
3358 * @nodeid: node number of the target node.
3359 * @caller: return address of caller, used for debug information
3360 *
3361 * Identical to kmem_cache_alloc but it will allocate memory on the given
3362 * node, which can improve the performance for cpu bound structures.
3363 *
3364 * Fallback to other node is possible if __GFP_THISNODE is not set.
3365 */
3366static __always_inline void *
3367__cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
3368 void *caller)
3369{
3370 unsigned long save_flags;
3371 void *ptr;
3372
Akinobu Mita824ebef2007-05-06 14:49:58 -07003373 if (should_failslab(cachep, flags))
3374 return NULL;
3375
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003376 cache_alloc_debugcheck_before(cachep, flags);
3377 local_irq_save(save_flags);
3378
3379 if (unlikely(nodeid == -1))
3380 nodeid = numa_node_id();
3381
3382 if (unlikely(!cachep->nodelists[nodeid])) {
3383 /* Node not bootstrapped yet */
3384 ptr = fallback_alloc(cachep, flags);
3385 goto out;
3386 }
3387
3388 if (nodeid == numa_node_id()) {
3389 /*
3390 * Use the locally cached objects if possible.
3391 * However ____cache_alloc does not allow fallback
3392 * to other nodes. It may fail while we still have
3393 * objects on other nodes available.
3394 */
3395 ptr = ____cache_alloc(cachep, flags);
3396 if (ptr)
3397 goto out;
3398 }
3399 /* ___cache_alloc_node can fall back to other nodes */
3400 ptr = ____cache_alloc_node(cachep, flags, nodeid);
3401 out:
3402 local_irq_restore(save_flags);
3403 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
3404
3405 return ptr;
3406}
3407
3408static __always_inline void *
3409__do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
3410{
3411 void *objp;
3412
3413 if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
3414 objp = alternate_node_alloc(cache, flags);
3415 if (objp)
3416 goto out;
3417 }
3418 objp = ____cache_alloc(cache, flags);
3419
3420 /*
3421 * We may just have run out of memory on the local node.
3422 * ____cache_alloc_node() knows how to locate memory on other nodes
3423 */
3424 if (!objp)
3425 objp = ____cache_alloc_node(cache, flags, numa_node_id());
3426
3427 out:
3428 return objp;
3429}
3430#else
3431
3432static __always_inline void *
3433__do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
3434{
3435 return ____cache_alloc(cachep, flags);
3436}
3437
3438#endif /* CONFIG_NUMA */
3439
3440static __always_inline void *
3441__cache_alloc(struct kmem_cache *cachep, gfp_t flags, void *caller)
3442{
3443 unsigned long save_flags;
3444 void *objp;
3445
Akinobu Mita824ebef2007-05-06 14:49:58 -07003446 if (should_failslab(cachep, flags))
3447 return NULL;
3448
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003449 cache_alloc_debugcheck_before(cachep, flags);
3450 local_irq_save(save_flags);
3451 objp = __do_cache_alloc(cachep, flags);
3452 local_irq_restore(save_flags);
3453 objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
3454 prefetchw(objp);
3455
3456 return objp;
3457}
Christoph Lametere498be72005-09-09 13:03:32 -07003458
3459/*
3460 * Caller needs to acquire correct kmem_list's list_lock
3461 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003462static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003463 int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464{
3465 int i;
Christoph Lametere498be72005-09-09 13:03:32 -07003466 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467
3468 for (i = 0; i < nr_objects; i++) {
3469 void *objp = objpp[i];
3470 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003472 slabp = virt_to_slab(objp);
Christoph Lameterff694162005-09-22 21:44:02 -07003473 l3 = cachep->nodelists[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 list_del(&slabp->list);
Christoph Lameterff694162005-09-22 21:44:02 -07003475 check_spinlock_acquired_node(cachep, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 check_slabp(cachep, slabp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08003477 slab_put_obj(cachep, slabp, objp, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 STATS_DEC_ACTIVE(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003479 l3->free_objects++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 check_slabp(cachep, slabp);
3481
3482 /* fixup slab chains */
3483 if (slabp->inuse == 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07003484 if (l3->free_objects > l3->free_limit) {
3485 l3->free_objects -= cachep->num;
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07003486 /* No need to drop any previously held
3487 * lock here, even if we have a off-slab slab
3488 * descriptor it is guaranteed to come from
3489 * a different cache, refer to comments before
3490 * alloc_slabmgmt.
3491 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 slab_destroy(cachep, slabp);
3493 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07003494 list_add(&slabp->list, &l3->slabs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 }
3496 } else {
3497 /* Unconditionally move a slab to the end of the
3498 * partial list on free - maximum time for the
3499 * other objects to be freed, too.
3500 */
Christoph Lametere498be72005-09-09 13:03:32 -07003501 list_add_tail(&slabp->list, &l3->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 }
3503 }
3504}
3505
Pekka Enberg343e0d72006-02-01 03:05:50 -08003506static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507{
3508 int batchcount;
Christoph Lametere498be72005-09-09 13:03:32 -07003509 struct kmem_list3 *l3;
Christoph Lameterff694162005-09-22 21:44:02 -07003510 int node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511
3512 batchcount = ac->batchcount;
3513#if DEBUG
3514 BUG_ON(!batchcount || batchcount > ac->avail);
3515#endif
3516 check_irq_off();
Christoph Lameterff694162005-09-22 21:44:02 -07003517 l3 = cachep->nodelists[node];
Ingo Molnar873623d2006-07-13 14:44:38 +02003518 spin_lock(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07003519 if (l3->shared) {
3520 struct array_cache *shared_array = l3->shared;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003521 int max = shared_array->limit - shared_array->avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 if (max) {
3523 if (batchcount > max)
3524 batchcount = max;
Christoph Lametere498be72005-09-09 13:03:32 -07003525 memcpy(&(shared_array->entry[shared_array->avail]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003526 ac->entry, sizeof(void *) * batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 shared_array->avail += batchcount;
3528 goto free_done;
3529 }
3530 }
3531
Christoph Lameterff694162005-09-22 21:44:02 -07003532 free_block(cachep, ac->entry, batchcount, node);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003533free_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534#if STATS
3535 {
3536 int i = 0;
3537 struct list_head *p;
3538
Christoph Lametere498be72005-09-09 13:03:32 -07003539 p = l3->slabs_free.next;
3540 while (p != &(l3->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 struct slab *slabp;
3542
3543 slabp = list_entry(p, struct slab, list);
3544 BUG_ON(slabp->inuse);
3545
3546 i++;
3547 p = p->next;
3548 }
3549 STATS_SET_FREEABLE(cachep, i);
3550 }
3551#endif
Christoph Lametere498be72005-09-09 13:03:32 -07003552 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 ac->avail -= batchcount;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003554 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555}
3556
3557/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08003558 * Release an obj back to its cache. If the obj has a constructed state, it must
3559 * be in this state _before_ it is released. Called with disabled ints.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 */
Ingo Molnar873623d2006-07-13 14:44:38 +02003561static inline void __cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562{
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003563 struct array_cache *ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
3565 check_irq_off();
3566 objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));
3567
Siddha, Suresh B62918a02007-05-02 19:27:18 +02003568 if (use_alien_caches && cache_free_alien(cachep, objp))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07003569 return;
Christoph Lametere498be72005-09-09 13:03:32 -07003570
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571 if (likely(ac->avail < ac->limit)) {
3572 STATS_INC_FREEHIT(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003573 ac->entry[ac->avail++] = objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 return;
3575 } else {
3576 STATS_INC_FREEMISS(cachep);
3577 cache_flusharray(cachep, ac);
Christoph Lametere498be72005-09-09 13:03:32 -07003578 ac->entry[ac->avail++] = objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 }
3580}
3581
3582/**
3583 * kmem_cache_alloc - Allocate an object
3584 * @cachep: The cache to allocate from.
3585 * @flags: See kmalloc().
3586 *
3587 * Allocate an object from this cache. The flags are only relevant
3588 * if the cache has no available objects.
3589 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003590void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591{
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003592 return __cache_alloc(cachep, flags, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593}
3594EXPORT_SYMBOL(kmem_cache_alloc);
3595
3596/**
Rolf Eike Beerb8008b22006-07-30 03:04:04 -07003597 * kmem_cache_zalloc - Allocate an object. The memory is set to zero.
Pekka Enberga8c0f9a2006-03-25 03:06:42 -08003598 * @cache: The cache to allocate from.
3599 * @flags: See kmalloc().
3600 *
3601 * Allocate an object from this cache and set the allocated memory to zero.
3602 * The flags are only relevant if the cache has no available objects.
3603 */
3604void *kmem_cache_zalloc(struct kmem_cache *cache, gfp_t flags)
3605{
3606 void *ret = __cache_alloc(cache, flags, __builtin_return_address(0));
3607 if (ret)
3608 memset(ret, 0, obj_size(cache));
3609 return ret;
3610}
3611EXPORT_SYMBOL(kmem_cache_zalloc);
3612
3613/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 * kmem_ptr_validate - check if an untrusted pointer might
3615 * be a slab entry.
3616 * @cachep: the cache we're checking against
3617 * @ptr: pointer to validate
3618 *
3619 * This verifies that the untrusted pointer looks sane:
3620 * it is _not_ a guarantee that the pointer is actually
3621 * part of the slab cache in question, but it at least
3622 * validates that the pointer can be dereferenced and
3623 * looks half-way sane.
3624 *
3625 * Currently only used for dentry validation.
3626 */
Christoph Lameterb7f869a22006-12-22 01:06:44 -08003627int kmem_ptr_validate(struct kmem_cache *cachep, const void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003629 unsigned long addr = (unsigned long)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630 unsigned long min_addr = PAGE_OFFSET;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003631 unsigned long align_mask = BYTES_PER_WORD - 1;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003632 unsigned long size = cachep->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 struct page *page;
3634
3635 if (unlikely(addr < min_addr))
3636 goto out;
3637 if (unlikely(addr > (unsigned long)high_memory - size))
3638 goto out;
3639 if (unlikely(addr & align_mask))
3640 goto out;
3641 if (unlikely(!kern_addr_valid(addr)))
3642 goto out;
3643 if (unlikely(!kern_addr_valid(addr + size - 1)))
3644 goto out;
3645 page = virt_to_page(ptr);
3646 if (unlikely(!PageSlab(page)))
3647 goto out;
Pekka Enberg065d41c2005-11-13 16:06:46 -08003648 if (unlikely(page_get_cache(page) != cachep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649 goto out;
3650 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003651out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 return 0;
3653}
3654
3655#ifdef CONFIG_NUMA
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003656void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
3657{
3658 return __cache_alloc_node(cachep, flags, nodeid,
3659 __builtin_return_address(0));
3660}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661EXPORT_SYMBOL(kmem_cache_alloc_node);
3662
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003663static __always_inline void *
3664__do_kmalloc_node(size_t size, gfp_t flags, int node, void *caller)
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003665{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003666 struct kmem_cache *cachep;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003667
3668 cachep = kmem_find_general_cachep(size, flags);
3669 if (unlikely(cachep == NULL))
3670 return NULL;
3671 return kmem_cache_alloc_node(cachep, flags, node);
3672}
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003673
3674#ifdef CONFIG_DEBUG_SLAB
3675void *__kmalloc_node(size_t size, gfp_t flags, int node)
3676{
3677 return __do_kmalloc_node(size, flags, node,
3678 __builtin_return_address(0));
3679}
Christoph Hellwigdbe5e692006-09-25 23:31:36 -07003680EXPORT_SYMBOL(__kmalloc_node);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003681
3682void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
3683 int node, void *caller)
3684{
3685 return __do_kmalloc_node(size, flags, node, caller);
3686}
3687EXPORT_SYMBOL(__kmalloc_node_track_caller);
3688#else
3689void *__kmalloc_node(size_t size, gfp_t flags, int node)
3690{
3691 return __do_kmalloc_node(size, flags, node, NULL);
3692}
3693EXPORT_SYMBOL(__kmalloc_node);
3694#endif /* CONFIG_DEBUG_SLAB */
3695#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003696
3697/**
Paul Drynoff800590f2006-06-23 02:03:48 -07003698 * __do_kmalloc - allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 * @size: how many bytes of memory are required.
Paul Drynoff800590f2006-06-23 02:03:48 -07003700 * @flags: the type of memory to allocate (see kmalloc).
Randy Dunlap911851e2006-03-22 00:08:14 -08003701 * @caller: function caller for debug tracking of the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 */
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003703static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
3704 void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003706 struct kmem_cache *cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003708 /* If you want to save a few bytes .text space: replace
3709 * __ with kmem_.
3710 * Then kmalloc uses the uninlined functions instead of the inline
3711 * functions.
3712 */
3713 cachep = __find_general_cachep(size, flags);
Andrew Mortondbdb9042005-09-23 13:24:10 -07003714 if (unlikely(cachep == NULL))
3715 return NULL;
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003716 return __cache_alloc(cachep, flags, caller);
3717}
3718
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003719
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003720#ifdef CONFIG_DEBUG_SLAB
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003721void *__kmalloc(size_t size, gfp_t flags)
3722{
Al Viro871751e2006-03-25 03:06:39 -08003723 return __do_kmalloc(size, flags, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724}
3725EXPORT_SYMBOL(__kmalloc);
3726
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003727void *__kmalloc_track_caller(size_t size, gfp_t flags, void *caller)
3728{
3729 return __do_kmalloc(size, flags, caller);
3730}
3731EXPORT_SYMBOL(__kmalloc_track_caller);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003732
3733#else
3734void *__kmalloc(size_t size, gfp_t flags)
3735{
3736 return __do_kmalloc(size, flags, NULL);
3737}
3738EXPORT_SYMBOL(__kmalloc);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003739#endif
3740
Linus Torvalds1da177e2005-04-16 15:20:36 -07003741/**
Pekka Enbergfd76bab2007-05-06 14:48:40 -07003742 * krealloc - reallocate memory. The contents will remain unchanged.
3743 *
3744 * @p: object to reallocate memory for.
3745 * @new_size: how many bytes of memory are required.
3746 * @flags: the type of memory to allocate.
3747 *
3748 * The contents of the object pointed to are preserved up to the
3749 * lesser of the new and old sizes. If @p is %NULL, krealloc()
3750 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
3751 * %NULL pointer, the object pointed to is freed.
3752 */
3753void *krealloc(const void *p, size_t new_size, gfp_t flags)
3754{
3755 struct kmem_cache *cache, *new_cache;
3756 void *ret;
3757
3758 if (unlikely(!p))
3759 return kmalloc_track_caller(new_size, flags);
3760
3761 if (unlikely(!new_size)) {
3762 kfree(p);
3763 return NULL;
3764 }
3765
3766 cache = virt_to_cache(p);
3767 new_cache = __find_general_cachep(new_size, flags);
3768
3769 /*
3770 * If new size fits in the current cache, bail out.
3771 */
3772 if (likely(cache == new_cache))
3773 return (void *)p;
3774
3775 /*
3776 * We are on the slow-path here so do not use __cache_alloc
3777 * because it bloats kernel text.
3778 */
3779 ret = kmalloc_track_caller(new_size, flags);
3780 if (ret) {
3781 memcpy(ret, p, min(new_size, ksize(p)));
3782 kfree(p);
3783 }
3784 return ret;
3785}
3786EXPORT_SYMBOL(krealloc);
3787
3788/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 * kmem_cache_free - Deallocate an object
3790 * @cachep: The cache the allocation was from.
3791 * @objp: The previously allocated object.
3792 *
3793 * Free an object which was previously allocated from this
3794 * cache.
3795 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003796void kmem_cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797{
3798 unsigned long flags;
3799
Pekka Enbergddc2e812006-06-23 02:03:40 -07003800 BUG_ON(virt_to_cache(objp) != cachep);
3801
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 local_irq_save(flags);
Ingo Molnar898552c2007-02-10 01:44:57 -08003803 debug_check_no_locks_freed(objp, obj_size(cachep));
Ingo Molnar873623d2006-07-13 14:44:38 +02003804 __cache_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003805 local_irq_restore(flags);
3806}
3807EXPORT_SYMBOL(kmem_cache_free);
3808
3809/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 * kfree - free previously allocated memory
3811 * @objp: pointer returned by kmalloc.
3812 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07003813 * If @objp is NULL, no operation is performed.
3814 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 * Don't free memory not originally allocated by kmalloc()
3816 * or you will run into trouble.
3817 */
3818void kfree(const void *objp)
3819{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003820 struct kmem_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 unsigned long flags;
3822
3823 if (unlikely(!objp))
3824 return;
3825 local_irq_save(flags);
3826 kfree_debugcheck(objp);
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003827 c = virt_to_cache(objp);
Ingo Molnarf9b84042006-06-27 02:54:49 -07003828 debug_check_no_locks_freed(objp, obj_size(c));
Ingo Molnar873623d2006-07-13 14:44:38 +02003829 __cache_free(c, (void *)objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 local_irq_restore(flags);
3831}
3832EXPORT_SYMBOL(kfree);
3833
Pekka Enberg343e0d72006-02-01 03:05:50 -08003834unsigned int kmem_cache_size(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003836 return obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837}
3838EXPORT_SYMBOL(kmem_cache_size);
3839
Pekka Enberg343e0d72006-02-01 03:05:50 -08003840const char *kmem_cache_name(struct kmem_cache *cachep)
Arnaldo Carvalho de Melo19449722005-06-18 22:46:19 -07003841{
3842 return cachep->name;
3843}
3844EXPORT_SYMBOL_GPL(kmem_cache_name);
3845
Christoph Lametere498be72005-09-09 13:03:32 -07003846/*
Christoph Lameter0718dc22006-03-25 03:06:47 -08003847 * This initializes kmem_list3 or resizes varioius caches for all nodes.
Christoph Lametere498be72005-09-09 13:03:32 -07003848 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003849static int alloc_kmemlist(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07003850{
3851 int node;
3852 struct kmem_list3 *l3;
Christoph Lametercafeb022006-03-25 03:06:46 -08003853 struct array_cache *new_shared;
Paul Menage3395ee02006-12-06 20:32:16 -08003854 struct array_cache **new_alien = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003855
3856 for_each_online_node(node) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003857
Paul Menage3395ee02006-12-06 20:32:16 -08003858 if (use_alien_caches) {
3859 new_alien = alloc_alien_cache(node, cachep->limit);
3860 if (!new_alien)
3861 goto fail;
3862 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003863
Eric Dumazet63109842007-05-06 14:49:28 -07003864 new_shared = NULL;
3865 if (cachep->shared) {
3866 new_shared = alloc_arraycache(node,
Christoph Lameter0718dc22006-03-25 03:06:47 -08003867 cachep->shared*cachep->batchcount,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003868 0xbaadf00d);
Eric Dumazet63109842007-05-06 14:49:28 -07003869 if (!new_shared) {
3870 free_alien_cache(new_alien);
3871 goto fail;
3872 }
Christoph Lameter0718dc22006-03-25 03:06:47 -08003873 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003874
Andrew Mortona737b3e2006-03-22 00:08:11 -08003875 l3 = cachep->nodelists[node];
3876 if (l3) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003877 struct array_cache *shared = l3->shared;
3878
Christoph Lametere498be72005-09-09 13:03:32 -07003879 spin_lock_irq(&l3->list_lock);
3880
Christoph Lametercafeb022006-03-25 03:06:46 -08003881 if (shared)
Christoph Lameter0718dc22006-03-25 03:06:47 -08003882 free_block(cachep, shared->entry,
3883 shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07003884
Christoph Lametercafeb022006-03-25 03:06:46 -08003885 l3->shared = new_shared;
3886 if (!l3->alien) {
Christoph Lametere498be72005-09-09 13:03:32 -07003887 l3->alien = new_alien;
3888 new_alien = NULL;
3889 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003890 l3->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003891 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07003892 spin_unlock_irq(&l3->list_lock);
Christoph Lametercafeb022006-03-25 03:06:46 -08003893 kfree(shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003894 free_alien_cache(new_alien);
3895 continue;
3896 }
Andrew Mortona737b3e2006-03-22 00:08:11 -08003897 l3 = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, node);
Christoph Lameter0718dc22006-03-25 03:06:47 -08003898 if (!l3) {
3899 free_alien_cache(new_alien);
3900 kfree(new_shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003901 goto fail;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003902 }
Christoph Lametere498be72005-09-09 13:03:32 -07003903
3904 kmem_list3_init(l3);
3905 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
Andrew Mortona737b3e2006-03-22 00:08:11 -08003906 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lametercafeb022006-03-25 03:06:46 -08003907 l3->shared = new_shared;
Christoph Lametere498be72005-09-09 13:03:32 -07003908 l3->alien = new_alien;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003909 l3->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003910 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07003911 cachep->nodelists[node] = l3;
3912 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003913 return 0;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003914
Andrew Mortona737b3e2006-03-22 00:08:11 -08003915fail:
Christoph Lameter0718dc22006-03-25 03:06:47 -08003916 if (!cachep->next.next) {
3917 /* Cache is not active yet. Roll back what we did */
3918 node--;
3919 while (node >= 0) {
3920 if (cachep->nodelists[node]) {
3921 l3 = cachep->nodelists[node];
3922
3923 kfree(l3->shared);
3924 free_alien_cache(l3->alien);
3925 kfree(l3);
3926 cachep->nodelists[node] = NULL;
3927 }
3928 node--;
3929 }
3930 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003931 return -ENOMEM;
Christoph Lametere498be72005-09-09 13:03:32 -07003932}
3933
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934struct ccupdate_struct {
Pekka Enberg343e0d72006-02-01 03:05:50 -08003935 struct kmem_cache *cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 struct array_cache *new[NR_CPUS];
3937};
3938
3939static void do_ccupdate_local(void *info)
3940{
Andrew Mortona737b3e2006-03-22 00:08:11 -08003941 struct ccupdate_struct *new = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 struct array_cache *old;
3943
3944 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003945 old = cpu_cache_get(new->cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003946
Linus Torvalds1da177e2005-04-16 15:20:36 -07003947 new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
3948 new->new[smp_processor_id()] = old;
3949}
3950
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -08003951/* Always called with the cache_chain_mutex held */
Andrew Mortona737b3e2006-03-22 00:08:11 -08003952static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
3953 int batchcount, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954{
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003955 struct ccupdate_struct *new;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003956 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003958 new = kzalloc(sizeof(*new), GFP_KERNEL);
3959 if (!new)
3960 return -ENOMEM;
3961
Christoph Lametere498be72005-09-09 13:03:32 -07003962 for_each_online_cpu(i) {
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003963 new->new[i] = alloc_arraycache(cpu_to_node(i), limit,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003964 batchcount);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003965 if (!new->new[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003966 for (i--; i >= 0; i--)
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003967 kfree(new->new[i]);
3968 kfree(new);
Christoph Lametere498be72005-09-09 13:03:32 -07003969 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 }
3971 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003972 new->cachep = cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003974 on_each_cpu(do_ccupdate_local, (void *)new, 1, 1);
Christoph Lametere498be72005-09-09 13:03:32 -07003975
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976 check_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 cachep->batchcount = batchcount;
3978 cachep->limit = limit;
Christoph Lametere498be72005-09-09 13:03:32 -07003979 cachep->shared = shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980
Christoph Lametere498be72005-09-09 13:03:32 -07003981 for_each_online_cpu(i) {
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003982 struct array_cache *ccold = new->new[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983 if (!ccold)
3984 continue;
Christoph Lametere498be72005-09-09 13:03:32 -07003985 spin_lock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
Christoph Lameterff694162005-09-22 21:44:02 -07003986 free_block(cachep, ccold->entry, ccold->avail, cpu_to_node(i));
Christoph Lametere498be72005-09-09 13:03:32 -07003987 spin_unlock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 kfree(ccold);
3989 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003990 kfree(new);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003991 return alloc_kmemlist(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992}
3993
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -08003994/* Called with cache_chain_mutex held always */
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003995static int enable_cpucache(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996{
3997 int err;
3998 int limit, shared;
3999
Andrew Mortona737b3e2006-03-22 00:08:11 -08004000 /*
4001 * The head array serves three purposes:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 * - create a LIFO ordering, i.e. return objects that are cache-warm
4003 * - reduce the number of spinlock operations.
Andrew Mortona737b3e2006-03-22 00:08:11 -08004004 * - reduce the number of linked list operations on the slab and
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 * bufctl chains: array operations are cheaper.
4006 * The numbers are guessed, we should auto-tune as described by
4007 * Bonwick.
4008 */
Manfred Spraul3dafccf2006-02-01 03:05:42 -08004009 if (cachep->buffer_size > 131072)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 limit = 1;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08004011 else if (cachep->buffer_size > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012 limit = 8;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08004013 else if (cachep->buffer_size > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 limit = 24;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08004015 else if (cachep->buffer_size > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 limit = 54;
4017 else
4018 limit = 120;
4019
Andrew Mortona737b3e2006-03-22 00:08:11 -08004020 /*
4021 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 * allocation behaviour: Most allocs on one cpu, most free operations
4023 * on another cpu. For these cases, an efficient object passing between
4024 * cpus is necessary. This is provided by a shared array. The array
4025 * replaces Bonwick's magazine layer.
4026 * On uniprocessor, it's functionally equivalent (but less efficient)
4027 * to a larger limit. Thus disabled by default.
4028 */
4029 shared = 0;
Eric Dumazet364fbb22007-05-06 14:49:27 -07004030 if (cachep->buffer_size <= PAGE_SIZE && num_possible_cpus() > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 shared = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032
4033#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08004034 /*
4035 * With debugging enabled, large batchcount lead to excessively long
4036 * periods with disabled local interrupts. Limit the batchcount
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 */
4038 if (limit > 32)
4039 limit = 32;
4040#endif
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004041 err = do_tune_cpucache(cachep, limit, (limit + 1) / 2, shared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004042 if (err)
4043 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004044 cachep->name, -err);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07004045 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046}
4047
Christoph Lameter1b552532006-03-22 00:09:07 -08004048/*
4049 * Drain an array if it contains any elements taking the l3 lock only if
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004050 * necessary. Note that the l3 listlock also protects the array_cache
4051 * if drain_array() is used on the shared array.
Christoph Lameter1b552532006-03-22 00:09:07 -08004052 */
4053void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
4054 struct array_cache *ac, int force, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055{
4056 int tofree;
4057
Christoph Lameter1b552532006-03-22 00:09:07 -08004058 if (!ac || !ac->avail)
4059 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 if (ac->touched && !force) {
4061 ac->touched = 0;
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004062 } else {
Christoph Lameter1b552532006-03-22 00:09:07 -08004063 spin_lock_irq(&l3->list_lock);
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004064 if (ac->avail) {
4065 tofree = force ? ac->avail : (ac->limit + 4) / 5;
4066 if (tofree > ac->avail)
4067 tofree = (ac->avail + 1) / 2;
4068 free_block(cachep, ac->entry, tofree, node);
4069 ac->avail -= tofree;
4070 memmove(ac->entry, &(ac->entry[tofree]),
4071 sizeof(void *) * ac->avail);
4072 }
Christoph Lameter1b552532006-03-22 00:09:07 -08004073 spin_unlock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 }
4075}
4076
4077/**
4078 * cache_reap - Reclaim memory from caches.
Randy Dunlap05fb6bf2007-02-28 20:12:13 -08004079 * @w: work descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 *
4081 * Called from workqueue/eventd every few seconds.
4082 * Purpose:
4083 * - clear the per-cpu caches for this CPU.
4084 * - return freeable pages to the main free memory pool.
4085 *
Andrew Mortona737b3e2006-03-22 00:08:11 -08004086 * If we cannot acquire the cache chain mutex then just give up - we'll try
4087 * again on the next iteration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004089static void cache_reap(struct work_struct *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090{
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004091 struct kmem_cache *searchp;
Christoph Lametere498be72005-09-09 13:03:32 -07004092 struct kmem_list3 *l3;
Christoph Lameteraab22072006-03-22 00:09:06 -08004093 int node = numa_node_id();
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004094 struct delayed_work *work =
4095 container_of(w, struct delayed_work, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004097 if (!mutex_trylock(&cache_chain_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 /* Give up. Setup the next iteration. */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004099 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004101 list_for_each_entry(searchp, &cache_chain, next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 check_irq_on();
4103
Christoph Lameter35386e32006-03-22 00:09:05 -08004104 /*
4105 * We only take the l3 lock if absolutely necessary and we
4106 * have established with reasonable certainty that
4107 * we can do some work if the lock was obtained.
4108 */
Christoph Lameteraab22072006-03-22 00:09:06 -08004109 l3 = searchp->nodelists[node];
Christoph Lameter35386e32006-03-22 00:09:05 -08004110
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004111 reap_alien(searchp, l3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
Christoph Lameteraab22072006-03-22 00:09:06 -08004113 drain_array(searchp, l3, cpu_cache_get(searchp), 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114
Christoph Lameter35386e32006-03-22 00:09:05 -08004115 /*
4116 * These are racy checks but it does not matter
4117 * if we skip one check or scan twice.
4118 */
Christoph Lametere498be72005-09-09 13:03:32 -07004119 if (time_after(l3->next_reap, jiffies))
Christoph Lameter35386e32006-03-22 00:09:05 -08004120 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004121
Christoph Lametere498be72005-09-09 13:03:32 -07004122 l3->next_reap = jiffies + REAPTIMEOUT_LIST3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123
Christoph Lameteraab22072006-03-22 00:09:06 -08004124 drain_array(searchp, l3, l3->shared, 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125
Christoph Lametered11d9e2006-06-30 01:55:45 -07004126 if (l3->free_touched)
Christoph Lametere498be72005-09-09 13:03:32 -07004127 l3->free_touched = 0;
Christoph Lametered11d9e2006-06-30 01:55:45 -07004128 else {
4129 int freed;
4130
4131 freed = drain_freelist(searchp, l3, (l3->free_limit +
4132 5 * searchp->num - 1) / (5 * searchp->num));
4133 STATS_ADD_REAPED(searchp, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 }
Christoph Lameter35386e32006-03-22 00:09:05 -08004135next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136 cond_resched();
4137 }
4138 check_irq_on();
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004139 mutex_unlock(&cache_chain_mutex);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004140 next_reap_node();
Christoph Lameter2244b952006-06-30 01:55:33 -07004141 refresh_cpu_vm_stats(smp_processor_id());
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004142out:
Andrew Mortona737b3e2006-03-22 00:08:11 -08004143 /* Set up the next iteration */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004144 schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_CPUC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145}
4146
4147#ifdef CONFIG_PROC_FS
4148
Pekka Enberg85289f92006-01-08 01:00:36 -08004149static void print_slabinfo_header(struct seq_file *m)
4150{
4151 /*
4152 * Output format version, so at least we can change it
4153 * without _too_ many complaints.
4154 */
4155#if STATS
4156 seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
4157#else
4158 seq_puts(m, "slabinfo - version: 2.1\n");
4159#endif
4160 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
4161 "<objperslab> <pagesperslab>");
4162 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
4163 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
4164#if STATS
4165 seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004166 "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
Pekka Enberg85289f92006-01-08 01:00:36 -08004167 seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
4168#endif
4169 seq_putc(m, '\n');
4170}
4171
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172static void *s_start(struct seq_file *m, loff_t *pos)
4173{
4174 loff_t n = *pos;
4175 struct list_head *p;
4176
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004177 mutex_lock(&cache_chain_mutex);
Pekka Enberg85289f92006-01-08 01:00:36 -08004178 if (!n)
4179 print_slabinfo_header(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 p = cache_chain.next;
4181 while (n--) {
4182 p = p->next;
4183 if (p == &cache_chain)
4184 return NULL;
4185 }
Pekka Enberg343e0d72006-02-01 03:05:50 -08004186 return list_entry(p, struct kmem_cache, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187}
4188
4189static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4190{
Pekka Enberg343e0d72006-02-01 03:05:50 -08004191 struct kmem_cache *cachep = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 ++*pos;
Andrew Mortona737b3e2006-03-22 00:08:11 -08004193 return cachep->next.next == &cache_chain ?
4194 NULL : list_entry(cachep->next.next, struct kmem_cache, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195}
4196
4197static void s_stop(struct seq_file *m, void *p)
4198{
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004199 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200}
4201
4202static int s_show(struct seq_file *m, void *p)
4203{
Pekka Enberg343e0d72006-02-01 03:05:50 -08004204 struct kmem_cache *cachep = p;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004205 struct slab *slabp;
4206 unsigned long active_objs;
4207 unsigned long num_objs;
4208 unsigned long active_slabs = 0;
4209 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004210 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 char *error = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07004212 int node;
4213 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 active_objs = 0;
4216 num_slabs = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004217 for_each_online_node(node) {
4218 l3 = cachep->nodelists[node];
4219 if (!l3)
4220 continue;
4221
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004222 check_irq_on();
4223 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07004224
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004225 list_for_each_entry(slabp, &l3->slabs_full, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004226 if (slabp->inuse != cachep->num && !error)
4227 error = "slabs_full accounting error";
4228 active_objs += cachep->num;
4229 active_slabs++;
4230 }
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004231 list_for_each_entry(slabp, &l3->slabs_partial, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004232 if (slabp->inuse == cachep->num && !error)
4233 error = "slabs_partial inuse accounting error";
4234 if (!slabp->inuse && !error)
4235 error = "slabs_partial/inuse accounting error";
4236 active_objs += slabp->inuse;
4237 active_slabs++;
4238 }
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004239 list_for_each_entry(slabp, &l3->slabs_free, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004240 if (slabp->inuse && !error)
4241 error = "slabs_free/inuse accounting error";
4242 num_slabs++;
4243 }
4244 free_objects += l3->free_objects;
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08004245 if (l3->shared)
4246 shared_avail += l3->shared->avail;
Christoph Lametere498be72005-09-09 13:03:32 -07004247
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004248 spin_unlock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004250 num_slabs += active_slabs;
4251 num_objs = num_slabs * cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07004252 if (num_objs - active_objs != free_objects && !error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 error = "free_objects accounting error";
4254
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004255 name = cachep->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 if (error)
4257 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
4258
4259 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
Manfred Spraul3dafccf2006-02-01 03:05:42 -08004260 name, active_objs, num_objs, cachep->buffer_size,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004261 cachep->num, (1 << cachep->gfporder));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 seq_printf(m, " : tunables %4u %4u %4u",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004263 cachep->limit, cachep->batchcount, cachep->shared);
Christoph Lametere498be72005-09-09 13:03:32 -07004264 seq_printf(m, " : slabdata %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004265 active_slabs, num_slabs, shared_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266#if STATS
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004267 { /* list3 stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 unsigned long high = cachep->high_mark;
4269 unsigned long allocs = cachep->num_allocations;
4270 unsigned long grown = cachep->grown;
4271 unsigned long reaped = cachep->reaped;
4272 unsigned long errors = cachep->errors;
4273 unsigned long max_freeable = cachep->max_freeable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 unsigned long node_allocs = cachep->node_allocs;
Christoph Lametere498be72005-09-09 13:03:32 -07004275 unsigned long node_frees = cachep->node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004276 unsigned long overflows = cachep->node_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277
Christoph Lametere498be72005-09-09 13:03:32 -07004278 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu \
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004279 %4lu %4lu %4lu %4lu %4lu", allocs, high, grown,
Andrew Mortona737b3e2006-03-22 00:08:11 -08004280 reaped, errors, max_freeable, node_allocs,
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004281 node_frees, overflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 }
4283 /* cpu stats */
4284 {
4285 unsigned long allochit = atomic_read(&cachep->allochit);
4286 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
4287 unsigned long freehit = atomic_read(&cachep->freehit);
4288 unsigned long freemiss = atomic_read(&cachep->freemiss);
4289
4290 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004291 allochit, allocmiss, freehit, freemiss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 }
4293#endif
4294 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07004295 return 0;
4296}
4297
4298/*
4299 * slabinfo_op - iterator that generates /proc/slabinfo
4300 *
4301 * Output layout:
4302 * cache-name
4303 * num-active-objs
4304 * total-objs
4305 * object size
4306 * num-active-slabs
4307 * total-slabs
4308 * num-pages-per-slab
4309 * + further values on SMP and with statistics enabled
4310 */
4311
Helge Deller15ad7cd2006-12-06 20:40:36 -08004312const struct seq_operations slabinfo_op = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004313 .start = s_start,
4314 .next = s_next,
4315 .stop = s_stop,
4316 .show = s_show,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004317};
4318
4319#define MAX_SLABINFO_WRITE 128
4320/**
4321 * slabinfo_write - Tuning for the slab allocator
4322 * @file: unused
4323 * @buffer: user buffer
4324 * @count: data length
4325 * @ppos: unused
4326 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004327ssize_t slabinfo_write(struct file *file, const char __user * buffer,
4328 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004330 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 int limit, batchcount, shared, res;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004332 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004333
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 if (count > MAX_SLABINFO_WRITE)
4335 return -EINVAL;
4336 if (copy_from_user(&kbuf, buffer, count))
4337 return -EFAULT;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004338 kbuf[MAX_SLABINFO_WRITE] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339
4340 tmp = strchr(kbuf, ' ');
4341 if (!tmp)
4342 return -EINVAL;
4343 *tmp = '\0';
4344 tmp++;
4345 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
4346 return -EINVAL;
4347
4348 /* Find the cache in the chain of caches. */
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004349 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 res = -EINVAL;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004351 list_for_each_entry(cachep, &cache_chain, next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 if (!strcmp(cachep->name, kbuf)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08004353 if (limit < 1 || batchcount < 1 ||
4354 batchcount > limit || shared < 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07004355 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07004357 res = do_tune_cpucache(cachep, limit,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004358 batchcount, shared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 }
4360 break;
4361 }
4362 }
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004363 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 if (res >= 0)
4365 res = count;
4366 return res;
4367}
Al Viro871751e2006-03-25 03:06:39 -08004368
4369#ifdef CONFIG_DEBUG_SLAB_LEAK
4370
4371static void *leaks_start(struct seq_file *m, loff_t *pos)
4372{
4373 loff_t n = *pos;
4374 struct list_head *p;
4375
4376 mutex_lock(&cache_chain_mutex);
4377 p = cache_chain.next;
4378 while (n--) {
4379 p = p->next;
4380 if (p == &cache_chain)
4381 return NULL;
4382 }
4383 return list_entry(p, struct kmem_cache, next);
4384}
4385
4386static inline int add_caller(unsigned long *n, unsigned long v)
4387{
4388 unsigned long *p;
4389 int l;
4390 if (!v)
4391 return 1;
4392 l = n[1];
4393 p = n + 2;
4394 while (l) {
4395 int i = l/2;
4396 unsigned long *q = p + 2 * i;
4397 if (*q == v) {
4398 q[1]++;
4399 return 1;
4400 }
4401 if (*q > v) {
4402 l = i;
4403 } else {
4404 p = q + 2;
4405 l -= i + 1;
4406 }
4407 }
4408 if (++n[1] == n[0])
4409 return 0;
4410 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4411 p[0] = v;
4412 p[1] = 1;
4413 return 1;
4414}
4415
4416static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
4417{
4418 void *p;
4419 int i;
4420 if (n[0] == n[1])
4421 return;
4422 for (i = 0, p = s->s_mem; i < c->num; i++, p += c->buffer_size) {
4423 if (slab_bufctl(s)[i] != BUFCTL_ACTIVE)
4424 continue;
4425 if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
4426 return;
4427 }
4428}
4429
4430static void show_symbol(struct seq_file *m, unsigned long address)
4431{
4432#ifdef CONFIG_KALLSYMS
4433 char *modname;
4434 const char *name;
4435 unsigned long offset, size;
4436 char namebuf[KSYM_NAME_LEN+1];
4437
4438 name = kallsyms_lookup(address, &size, &offset, &modname, namebuf);
4439
4440 if (name) {
4441 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
4442 if (modname)
4443 seq_printf(m, " [%s]", modname);
4444 return;
4445 }
4446#endif
4447 seq_printf(m, "%p", (void *)address);
4448}
4449
4450static int leaks_show(struct seq_file *m, void *p)
4451{
4452 struct kmem_cache *cachep = p;
Al Viro871751e2006-03-25 03:06:39 -08004453 struct slab *slabp;
4454 struct kmem_list3 *l3;
4455 const char *name;
4456 unsigned long *n = m->private;
4457 int node;
4458 int i;
4459
4460 if (!(cachep->flags & SLAB_STORE_USER))
4461 return 0;
4462 if (!(cachep->flags & SLAB_RED_ZONE))
4463 return 0;
4464
4465 /* OK, we can do it */
4466
4467 n[1] = 0;
4468
4469 for_each_online_node(node) {
4470 l3 = cachep->nodelists[node];
4471 if (!l3)
4472 continue;
4473
4474 check_irq_on();
4475 spin_lock_irq(&l3->list_lock);
4476
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004477 list_for_each_entry(slabp, &l3->slabs_full, list)
Al Viro871751e2006-03-25 03:06:39 -08004478 handle_slab(n, cachep, slabp);
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004479 list_for_each_entry(slabp, &l3->slabs_partial, list)
Al Viro871751e2006-03-25 03:06:39 -08004480 handle_slab(n, cachep, slabp);
Al Viro871751e2006-03-25 03:06:39 -08004481 spin_unlock_irq(&l3->list_lock);
4482 }
4483 name = cachep->name;
4484 if (n[0] == n[1]) {
4485 /* Increase the buffer size */
4486 mutex_unlock(&cache_chain_mutex);
4487 m->private = kzalloc(n[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
4488 if (!m->private) {
4489 /* Too bad, we are really out */
4490 m->private = n;
4491 mutex_lock(&cache_chain_mutex);
4492 return -ENOMEM;
4493 }
4494 *(unsigned long *)m->private = n[0] * 2;
4495 kfree(n);
4496 mutex_lock(&cache_chain_mutex);
4497 /* Now make sure this entry will be retried */
4498 m->count = m->size;
4499 return 0;
4500 }
4501 for (i = 0; i < n[1]; i++) {
4502 seq_printf(m, "%s: %lu ", name, n[2*i+3]);
4503 show_symbol(m, n[2*i+2]);
4504 seq_putc(m, '\n');
4505 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004506
Al Viro871751e2006-03-25 03:06:39 -08004507 return 0;
4508}
4509
Helge Deller15ad7cd2006-12-06 20:40:36 -08004510const struct seq_operations slabstats_op = {
Al Viro871751e2006-03-25 03:06:39 -08004511 .start = leaks_start,
4512 .next = s_next,
4513 .stop = s_stop,
4514 .show = leaks_show,
4515};
4516#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517#endif
4518
Manfred Spraul00e145b2005-09-03 15:55:07 -07004519/**
4520 * ksize - get the actual amount of memory allocated for a given object
4521 * @objp: Pointer to the object
4522 *
4523 * kmalloc may internally round up allocations and return more memory
4524 * than requested. ksize() can be used to determine the actual amount of
4525 * memory allocated. The caller may use this additional memory, even though
4526 * a smaller amount of memory was initially specified with the kmalloc call.
4527 * The caller must guarantee that objp points to a valid object previously
4528 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4529 * must not be freed during the duration of the call.
4530 */
Pekka Enbergfd76bab2007-05-06 14:48:40 -07004531size_t ksize(const void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532{
Manfred Spraul00e145b2005-09-03 15:55:07 -07004533 if (unlikely(objp == NULL))
4534 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08004536 return obj_size(virt_to_cache(objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537}