blob: a9c4472e92041e0fbeba9d9cc0a271f9e18c4a95 [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
David Woodhouseb46b8f12007-05-08 00:22:59 -0700151 * alignment larger than the alignment of a 64-bit integer.
152 * ARCH_KMALLOC_MINALIGN allows that.
153 * Note that increasing this value may disable some debug features.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 */
David Woodhouseb46b8f12007-05-08 00:22:59 -0700155#define ARCH_KMALLOC_MINALIGN __alignof__(unsigned long long)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156#endif
157
158#ifndef ARCH_SLAB_MINALIGN
159/*
160 * Enforce a minimum alignment for all caches.
161 * Intended for archs that get misalignment faults even for BYTES_PER_WORD
162 * aligned buffers. Includes ARCH_KMALLOC_MINALIGN.
163 * If possible: Do not enable this flag for CONFIG_DEBUG_SLAB, it disables
164 * some debug features.
165 */
166#define ARCH_SLAB_MINALIGN 0
167#endif
168
169#ifndef ARCH_KMALLOC_FLAGS
170#define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
171#endif
172
173/* Legal flag mask for kmem_cache_create(). */
174#if DEBUG
Christoph Lameter50953fe2007-05-06 14:50:16 -0700175# define CREATE_MASK (SLAB_RED_ZONE | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 SLAB_POISON | SLAB_HWCACHE_ALIGN | \
Christoph Lameterac2b8982006-03-22 00:08:15 -0800177 SLAB_CACHE_DMA | \
Christoph Lameter5af60832007-05-06 14:49:56 -0700178 SLAB_STORE_USER | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
Paul Jackson101a5002006-03-24 03:16:07 -0800180 SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181#else
Christoph Lameterac2b8982006-03-22 00:08:15 -0800182# define CREATE_MASK (SLAB_HWCACHE_ALIGN | \
Christoph Lameter5af60832007-05-06 14:49:56 -0700183 SLAB_CACHE_DMA | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
Paul Jackson101a5002006-03-24 03:16:07 -0800185 SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#endif
187
188/*
189 * kmem_bufctl_t:
190 *
191 * Bufctl's are used for linking objs within a slab
192 * linked offsets.
193 *
194 * This implementation relies on "struct page" for locating the cache &
195 * slab an object belongs to.
196 * This allows the bufctl structure to be small (one int), but limits
197 * the number of objects a slab (not a cache) can contain when off-slab
198 * bufctls are used. The limit is the size of the largest general cache
199 * that does not use off-slab slabs.
200 * For 32bit archs with 4 kB pages, is this 56.
201 * This is not serious, as it is only for large objects, when it is unwise
202 * to have too many per slab.
203 * Note: This limit can be raised by introducing a general cache whose size
204 * is less than 512 (PAGE_SIZE<<3), but greater than 256.
205 */
206
Kyle Moffettfa5b08d2005-09-03 15:55:03 -0700207typedef unsigned int kmem_bufctl_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208#define BUFCTL_END (((kmem_bufctl_t)(~0U))-0)
209#define BUFCTL_FREE (((kmem_bufctl_t)(~0U))-1)
Al Viro871751e2006-03-25 03:06:39 -0800210#define BUFCTL_ACTIVE (((kmem_bufctl_t)(~0U))-2)
211#define SLAB_LIMIT (((kmem_bufctl_t)(~0U))-3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213/*
214 * struct slab
215 *
216 * Manages the objs in a slab. Placed either at the beginning of mem allocated
217 * for a slab, or allocated from an general cache.
218 * Slabs are chained into three list: fully used, partial, fully free slabs.
219 */
220struct slab {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800221 struct list_head list;
222 unsigned long colouroff;
223 void *s_mem; /* including colour offset */
224 unsigned int inuse; /* num of objs active in slab */
225 kmem_bufctl_t free;
226 unsigned short nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227};
228
229/*
230 * struct slab_rcu
231 *
232 * slab_destroy on a SLAB_DESTROY_BY_RCU cache uses this structure to
233 * arrange for kmem_freepages to be called via RCU. This is useful if
234 * we need to approach a kernel structure obliquely, from its address
235 * obtained without the usual locking. We can lock the structure to
236 * stabilize it and check it's still at the given address, only if we
237 * can be sure that the memory has not been meanwhile reused for some
238 * other kind of object (which our subsystem's lock might corrupt).
239 *
240 * rcu_read_lock before reading the address, then rcu_read_unlock after
241 * taking the spinlock within the structure expected at that address.
242 *
243 * We assume struct slab_rcu can overlay struct slab when destroying.
244 */
245struct slab_rcu {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800246 struct rcu_head head;
Pekka Enberg343e0d72006-02-01 03:05:50 -0800247 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800248 void *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249};
250
251/*
252 * struct array_cache
253 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 * Purpose:
255 * - LIFO ordering, to hand out cache-warm objects from _alloc
256 * - reduce the number of linked list operations
257 * - reduce spinlock operations
258 *
259 * The limit is stored in the per-cpu structure to reduce the data cache
260 * footprint.
261 *
262 */
263struct array_cache {
264 unsigned int avail;
265 unsigned int limit;
266 unsigned int batchcount;
267 unsigned int touched;
Christoph Lametere498be72005-09-09 13:03:32 -0700268 spinlock_t lock;
Andrew Mortona737b3e2006-03-22 00:08:11 -0800269 void *entry[0]; /*
270 * Must have this definition in here for the proper
271 * alignment of array_cache. Also simplifies accessing
272 * the entries.
273 * [0] is for gcc 2.95. It should really be [].
274 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275};
276
Andrew Mortona737b3e2006-03-22 00:08:11 -0800277/*
278 * bootstrap: The caches do not work without cpuarrays anymore, but the
279 * cpuarrays are allocated from the generic caches...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 */
281#define BOOT_CPUCACHE_ENTRIES 1
282struct arraycache_init {
283 struct array_cache cache;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800284 void *entries[BOOT_CPUCACHE_ENTRIES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285};
286
287/*
Christoph Lametere498be72005-09-09 13:03:32 -0700288 * The slab lists for all objects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 */
290struct kmem_list3 {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800291 struct list_head slabs_partial; /* partial list first, better asm code */
292 struct list_head slabs_full;
293 struct list_head slabs_free;
294 unsigned long free_objects;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800295 unsigned int free_limit;
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -0800296 unsigned int colour_next; /* Per-node cache coloring */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800297 spinlock_t list_lock;
298 struct array_cache *shared; /* shared per node */
299 struct array_cache **alien; /* on other nodes */
Christoph Lameter35386e32006-03-22 00:09:05 -0800300 unsigned long next_reap; /* updated without locking */
301 int free_touched; /* updated without locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302};
303
Christoph Lametere498be72005-09-09 13:03:32 -0700304/*
305 * Need this for bootstrapping a per node allocator.
306 */
307#define NUM_INIT_LISTS (2 * MAX_NUMNODES + 1)
308struct kmem_list3 __initdata initkmem_list3[NUM_INIT_LISTS];
309#define CACHE_CACHE 0
310#define SIZE_AC 1
311#define SIZE_L3 (1 + MAX_NUMNODES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Christoph Lametered11d9e2006-06-30 01:55:45 -0700313static int drain_freelist(struct kmem_cache *cache,
314 struct kmem_list3 *l3, int tofree);
315static void free_block(struct kmem_cache *cachep, void **objpp, int len,
316 int node);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -0700317static int enable_cpucache(struct kmem_cache *cachep);
David Howells65f27f32006-11-22 14:55:48 +0000318static void cache_reap(struct work_struct *unused);
Christoph Lametered11d9e2006-06-30 01:55:45 -0700319
Christoph Lametere498be72005-09-09 13:03:32 -0700320/*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800321 * This function must be completely optimized away if a constant is passed to
322 * it. Mostly the same as what is in linux/slab.h except it returns an index.
Christoph Lametere498be72005-09-09 13:03:32 -0700323 */
Ivan Kokshaysky7243cc02005-09-22 21:43:58 -0700324static __always_inline int index_of(const size_t size)
Christoph Lametere498be72005-09-09 13:03:32 -0700325{
Steven Rostedt5ec8a842006-02-01 03:05:44 -0800326 extern void __bad_size(void);
327
Christoph Lametere498be72005-09-09 13:03:32 -0700328 if (__builtin_constant_p(size)) {
329 int i = 0;
330
331#define CACHE(x) \
332 if (size <=x) \
333 return i; \
334 else \
335 i++;
336#include "linux/kmalloc_sizes.h"
337#undef CACHE
Steven Rostedt5ec8a842006-02-01 03:05:44 -0800338 __bad_size();
Ivan Kokshaysky7243cc02005-09-22 21:43:58 -0700339 } else
Steven Rostedt5ec8a842006-02-01 03:05:44 -0800340 __bad_size();
Christoph Lametere498be72005-09-09 13:03:32 -0700341 return 0;
342}
343
Ingo Molnare0a42722006-06-23 02:03:46 -0700344static int slab_early_init = 1;
345
Christoph Lametere498be72005-09-09 13:03:32 -0700346#define INDEX_AC index_of(sizeof(struct arraycache_init))
347#define INDEX_L3 index_of(sizeof(struct kmem_list3))
348
Pekka Enberg5295a742006-02-01 03:05:48 -0800349static void kmem_list3_init(struct kmem_list3 *parent)
Christoph Lametere498be72005-09-09 13:03:32 -0700350{
351 INIT_LIST_HEAD(&parent->slabs_full);
352 INIT_LIST_HEAD(&parent->slabs_partial);
353 INIT_LIST_HEAD(&parent->slabs_free);
354 parent->shared = NULL;
355 parent->alien = NULL;
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -0800356 parent->colour_next = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700357 spin_lock_init(&parent->list_lock);
358 parent->free_objects = 0;
359 parent->free_touched = 0;
360}
361
Andrew Mortona737b3e2006-03-22 00:08:11 -0800362#define MAKE_LIST(cachep, listp, slab, nodeid) \
363 do { \
364 INIT_LIST_HEAD(listp); \
365 list_splice(&(cachep->nodelists[nodeid]->slab), listp); \
Christoph Lametere498be72005-09-09 13:03:32 -0700366 } while (0)
367
Andrew Mortona737b3e2006-03-22 00:08:11 -0800368#define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
369 do { \
Christoph Lametere498be72005-09-09 13:03:32 -0700370 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
371 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
372 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
373 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375/*
Pekka Enberg343e0d72006-02-01 03:05:50 -0800376 * struct kmem_cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 *
378 * manages a cache.
379 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800380
Pekka J Enberg2109a2d2005-11-07 00:58:01 -0800381struct kmem_cache {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382/* 1) per-cpu data, touched during every alloc/free */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800383 struct array_cache *array[NR_CPUS];
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800384/* 2) Cache tunables. Protected by cache_chain_mutex */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800385 unsigned int batchcount;
386 unsigned int limit;
387 unsigned int shared;
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800388
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800389 unsigned int buffer_size;
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800390 u32 reciprocal_buffer_size;
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800391/* 3) touched by every alloc & free from the backend */
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800392
Andrew Mortona737b3e2006-03-22 00:08:11 -0800393 unsigned int flags; /* constant flags */
394 unsigned int num; /* # of objs per slab */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800396/* 4) cache_grow/shrink */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /* order of pgs per slab (2^n) */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800398 unsigned int gfporder;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /* force GFP flags, e.g. GFP_DMA */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800401 gfp_t gfpflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Andrew Mortona737b3e2006-03-22 00:08:11 -0800403 size_t colour; /* cache colouring range */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800404 unsigned int colour_off; /* colour offset */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800405 struct kmem_cache *slabp_cache;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800406 unsigned int slab_size;
Andrew Mortona737b3e2006-03-22 00:08:11 -0800407 unsigned int dflags; /* dynamic flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /* constructor func */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800410 void (*ctor) (void *, struct kmem_cache *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800412/* 5) cache creation/removal */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800413 const char *name;
414 struct list_head next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800416/* 6) statistics */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417#if STATS
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800418 unsigned long num_active;
419 unsigned long num_allocations;
420 unsigned long high_mark;
421 unsigned long grown;
422 unsigned long reaped;
423 unsigned long errors;
424 unsigned long max_freeable;
425 unsigned long node_allocs;
426 unsigned long node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700427 unsigned long node_overflow;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800428 atomic_t allochit;
429 atomic_t allocmiss;
430 atomic_t freehit;
431 atomic_t freemiss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432#endif
433#if DEBUG
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800434 /*
435 * If debugging is enabled, then the allocator can add additional
436 * fields and/or padding to every object. buffer_size contains the total
437 * object size including these internal fields, the following two
438 * variables contain the offset to the user object and its size.
439 */
440 int obj_offset;
441 int obj_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442#endif
Eric Dumazet8da34302007-05-06 14:49:29 -0700443 /*
444 * We put nodelists[] at the end of kmem_cache, because we want to size
445 * this array to nr_node_ids slots instead of MAX_NUMNODES
446 * (see kmem_cache_init())
447 * We still use [MAX_NUMNODES] and not [1] or [0] because cache_cache
448 * is statically defined, so we reserve the max number of nodes.
449 */
450 struct kmem_list3 *nodelists[MAX_NUMNODES];
451 /*
452 * Do not add fields after nodelists[]
453 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454};
455
456#define CFLGS_OFF_SLAB (0x80000000UL)
457#define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
458
459#define BATCHREFILL_LIMIT 16
Andrew Mortona737b3e2006-03-22 00:08:11 -0800460/*
461 * Optimization question: fewer reaps means less probability for unnessary
462 * cpucache drain/refill cycles.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 *
Adrian Bunkdc6f3f22005-11-08 16:44:08 +0100464 * OTOH the cpuarrays can contain lots of objects,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 * which could lock up otherwise freeable slabs.
466 */
467#define REAPTIMEOUT_CPUC (2*HZ)
468#define REAPTIMEOUT_LIST3 (4*HZ)
469
470#if STATS
471#define STATS_INC_ACTIVE(x) ((x)->num_active++)
472#define STATS_DEC_ACTIVE(x) ((x)->num_active--)
473#define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
474#define STATS_INC_GROWN(x) ((x)->grown++)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700475#define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
Andrew Mortona737b3e2006-03-22 00:08:11 -0800476#define STATS_SET_HIGH(x) \
477 do { \
478 if ((x)->num_active > (x)->high_mark) \
479 (x)->high_mark = (x)->num_active; \
480 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481#define STATS_INC_ERR(x) ((x)->errors++)
482#define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
Christoph Lametere498be72005-09-09 13:03:32 -0700483#define STATS_INC_NODEFREES(x) ((x)->node_frees++)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700484#define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800485#define STATS_SET_FREEABLE(x, i) \
486 do { \
487 if ((x)->max_freeable < i) \
488 (x)->max_freeable = i; \
489 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490#define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
491#define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
492#define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
493#define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
494#else
495#define STATS_INC_ACTIVE(x) do { } while (0)
496#define STATS_DEC_ACTIVE(x) do { } while (0)
497#define STATS_INC_ALLOCED(x) do { } while (0)
498#define STATS_INC_GROWN(x) do { } while (0)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700499#define STATS_ADD_REAPED(x,y) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500#define STATS_SET_HIGH(x) do { } while (0)
501#define STATS_INC_ERR(x) do { } while (0)
502#define STATS_INC_NODEALLOCS(x) do { } while (0)
Christoph Lametere498be72005-09-09 13:03:32 -0700503#define STATS_INC_NODEFREES(x) do { } while (0)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700504#define STATS_INC_ACOVERFLOW(x) do { } while (0)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800505#define STATS_SET_FREEABLE(x, i) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506#define STATS_INC_ALLOCHIT(x) do { } while (0)
507#define STATS_INC_ALLOCMISS(x) do { } while (0)
508#define STATS_INC_FREEHIT(x) do { } while (0)
509#define STATS_INC_FREEMISS(x) do { } while (0)
510#endif
511
512#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Andrew Mortona737b3e2006-03-22 00:08:11 -0800514/*
515 * memory layout of objects:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 * 0 : objp
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800517 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 * the end of an object is aligned with the end of the real
519 * allocation. Catches writes behind the end of the allocation.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800520 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 * redzone word.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800522 * cachep->obj_offset: The real object.
523 * cachep->buffer_size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
Andrew Mortona737b3e2006-03-22 00:08:11 -0800524 * cachep->buffer_size - 1* BYTES_PER_WORD: last caller address
525 * [BYTES_PER_WORD long]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800527static int obj_offset(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800529 return cachep->obj_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530}
531
Pekka Enberg343e0d72006-02-01 03:05:50 -0800532static int obj_size(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800534 return cachep->obj_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
David Woodhouseb46b8f12007-05-08 00:22:59 -0700537static unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
David Woodhouseb46b8f12007-05-08 00:22:59 -0700540 return (unsigned long long*) (objp + obj_offset(cachep) -
541 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
David Woodhouseb46b8f12007-05-08 00:22:59 -0700544static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
547 if (cachep->flags & SLAB_STORE_USER)
David Woodhouseb46b8f12007-05-08 00:22:59 -0700548 return (unsigned long long *)(objp + cachep->buffer_size -
549 sizeof(unsigned long long) -
550 BYTES_PER_WORD);
551 return (unsigned long long *) (objp + cachep->buffer_size -
552 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Pekka Enberg343e0d72006-02-01 03:05:50 -0800555static void **dbg_userword(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 BUG_ON(!(cachep->flags & SLAB_STORE_USER));
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800558 return (void **)(objp + cachep->buffer_size - BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561#else
562
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800563#define obj_offset(x) 0
564#define obj_size(cachep) (cachep->buffer_size)
David Woodhouseb46b8f12007-05-08 00:22:59 -0700565#define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
566#define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567#define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
568
569#endif
570
571/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 * Do not go above this order unless 0 objects fit into the slab.
573 */
574#define BREAK_GFP_ORDER_HI 1
575#define BREAK_GFP_ORDER_LO 0
576static int slab_break_gfp_order = BREAK_GFP_ORDER_LO;
577
Andrew Mortona737b3e2006-03-22 00:08:11 -0800578/*
579 * Functions for storing/retrieving the cachep and or slab from the page
580 * allocator. These are used to find the slab an obj belongs to. With kfree(),
581 * these are used to find the cache which an obj belongs to.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
Pekka Enberg065d41c2005-11-13 16:06:46 -0800583static inline void page_set_cache(struct page *page, struct kmem_cache *cache)
584{
585 page->lru.next = (struct list_head *)cache;
586}
587
588static inline struct kmem_cache *page_get_cache(struct page *page)
589{
Christoph Lameterd85f3382007-05-06 14:49:39 -0700590 page = compound_head(page);
Pekka Enbergddc2e812006-06-23 02:03:40 -0700591 BUG_ON(!PageSlab(page));
Pekka Enberg065d41c2005-11-13 16:06:46 -0800592 return (struct kmem_cache *)page->lru.next;
593}
594
595static inline void page_set_slab(struct page *page, struct slab *slab)
596{
597 page->lru.prev = (struct list_head *)slab;
598}
599
600static inline struct slab *page_get_slab(struct page *page)
601{
Pekka Enbergddc2e812006-06-23 02:03:40 -0700602 BUG_ON(!PageSlab(page));
Pekka Enberg065d41c2005-11-13 16:06:46 -0800603 return (struct slab *)page->lru.prev;
604}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Pekka Enberg6ed5eb22006-02-01 03:05:49 -0800606static inline struct kmem_cache *virt_to_cache(const void *obj)
607{
Christoph Lameterb49af682007-05-06 14:49:41 -0700608 struct page *page = virt_to_head_page(obj);
Pekka Enberg6ed5eb22006-02-01 03:05:49 -0800609 return page_get_cache(page);
610}
611
612static inline struct slab *virt_to_slab(const void *obj)
613{
Christoph Lameterb49af682007-05-06 14:49:41 -0700614 struct page *page = virt_to_head_page(obj);
Pekka Enberg6ed5eb22006-02-01 03:05:49 -0800615 return page_get_slab(page);
616}
617
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800618static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
619 unsigned int idx)
620{
621 return slab->s_mem + cache->buffer_size * idx;
622}
623
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800624/*
625 * We want to avoid an expensive divide : (offset / cache->buffer_size)
626 * Using the fact that buffer_size is a constant for a particular cache,
627 * we can replace (offset / cache->buffer_size) by
628 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
629 */
630static inline unsigned int obj_to_index(const struct kmem_cache *cache,
631 const struct slab *slab, void *obj)
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800632{
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800633 u32 offset = (obj - slab->s_mem);
634 return reciprocal_divide(offset, cache->reciprocal_buffer_size);
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800635}
636
Andrew Mortona737b3e2006-03-22 00:08:11 -0800637/*
638 * These are the default caches for kmalloc. Custom caches can have other sizes.
639 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640struct cache_sizes malloc_sizes[] = {
641#define CACHE(x) { .cs_size = (x) },
642#include <linux/kmalloc_sizes.h>
643 CACHE(ULONG_MAX)
644#undef CACHE
645};
646EXPORT_SYMBOL(malloc_sizes);
647
648/* Must match cache_sizes above. Out of line to keep cache footprint low. */
649struct cache_names {
650 char *name;
651 char *name_dma;
652};
653
654static struct cache_names __initdata cache_names[] = {
655#define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },
656#include <linux/kmalloc_sizes.h>
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800657 {NULL,}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658#undef CACHE
659};
660
661static struct arraycache_init initarray_cache __initdata =
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800662 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663static struct arraycache_init initarray_generic =
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800664 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666/* internal cache of cache description objs */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800667static struct kmem_cache cache_cache = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800668 .batchcount = 1,
669 .limit = BOOT_CPUCACHE_ENTRIES,
670 .shared = 1,
Pekka Enberg343e0d72006-02-01 03:05:50 -0800671 .buffer_size = sizeof(struct kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800672 .name = "kmem_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673};
674
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700675#define BAD_ALIEN_MAGIC 0x01020304ul
676
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200677#ifdef CONFIG_LOCKDEP
678
679/*
680 * Slab sometimes uses the kmalloc slabs to store the slab headers
681 * for other slabs "off slab".
682 * The locking for this is tricky in that it nests within the locks
683 * of all other slabs in a few places; to deal with this special
684 * locking we put on-slab caches into a separate lock-class.
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700685 *
686 * We set lock class for alien array caches which are up during init.
687 * The lock annotation will be lost if all cpus of a node goes down and
688 * then comes back up during hotplug
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200689 */
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700690static struct lock_class_key on_slab_l3_key;
691static struct lock_class_key on_slab_alc_key;
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200692
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700693static inline void init_lock_keys(void)
694
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200695{
696 int q;
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700697 struct cache_sizes *s = malloc_sizes;
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200698
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700699 while (s->cs_size != ULONG_MAX) {
700 for_each_node(q) {
701 struct array_cache **alc;
702 int r;
703 struct kmem_list3 *l3 = s->cs_cachep->nodelists[q];
704 if (!l3 || OFF_SLAB(s->cs_cachep))
705 continue;
706 lockdep_set_class(&l3->list_lock, &on_slab_l3_key);
707 alc = l3->alien;
708 /*
709 * FIXME: This check for BAD_ALIEN_MAGIC
710 * should go away when common slab code is taught to
711 * work even without alien caches.
712 * Currently, non NUMA code returns BAD_ALIEN_MAGIC
713 * for alloc_alien_cache,
714 */
715 if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC)
716 continue;
717 for_each_node(r) {
718 if (alc[r])
719 lockdep_set_class(&alc[r]->lock,
720 &on_slab_alc_key);
721 }
722 }
723 s++;
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200724 }
725}
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200726#else
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700727static inline void init_lock_keys(void)
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200728{
729}
730#endif
731
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -0800732/*
733 * 1. Guard access to the cache-chain.
734 * 2. Protect sanity of cpu_online_map against cpu hotplug events
735 */
Ingo Molnarfc0abb12006-01-18 17:42:33 -0800736static DEFINE_MUTEX(cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737static struct list_head cache_chain;
738
739/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 * chicken and egg problem: delay the per-cpu array allocation
741 * until the general caches are up.
742 */
743static enum {
744 NONE,
Christoph Lametere498be72005-09-09 13:03:32 -0700745 PARTIAL_AC,
746 PARTIAL_L3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 FULL
748} g_cpucache_up;
749
Mike Kravetz39d24e62006-05-15 09:44:13 -0700750/*
751 * used by boot code to determine if it can use slab based allocator
752 */
753int slab_is_available(void)
754{
755 return g_cpucache_up == FULL;
756}
757
David Howells52bad642006-11-22 14:54:01 +0000758static DEFINE_PER_CPU(struct delayed_work, reap_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Pekka Enberg343e0d72006-02-01 03:05:50 -0800760static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 return cachep->array[smp_processor_id()];
763}
764
Andrew Mortona737b3e2006-03-22 00:08:11 -0800765static inline struct kmem_cache *__find_general_cachep(size_t size,
766 gfp_t gfpflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
768 struct cache_sizes *csizep = malloc_sizes;
769
770#if DEBUG
771 /* This happens if someone tries to call
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800772 * kmem_cache_create(), or __kmalloc(), before
773 * the generic caches are initialized.
774 */
Alok Katariac7e43c72005-09-14 12:17:53 -0700775 BUG_ON(malloc_sizes[INDEX_AC].cs_cachep == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776#endif
777 while (size > csizep->cs_size)
778 csizep++;
779
780 /*
Martin Hicks0abf40c2005-09-03 15:54:54 -0700781 * Really subtle: The last entry with cs->cs_size==ULONG_MAX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 * has cs_{dma,}cachep==NULL. Thus no special case
783 * for large kmalloc calls required.
784 */
Christoph Lameter4b51d662007-02-10 01:43:10 -0800785#ifdef CONFIG_ZONE_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (unlikely(gfpflags & GFP_DMA))
787 return csizep->cs_dmacachep;
Christoph Lameter4b51d662007-02-10 01:43:10 -0800788#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 return csizep->cs_cachep;
790}
791
Adrian Bunkb2213852006-09-25 23:31:02 -0700792static struct kmem_cache *kmem_find_general_cachep(size_t size, gfp_t gfpflags)
Manfred Spraul97e2bde2005-05-01 08:58:38 -0700793{
794 return __find_general_cachep(size, gfpflags);
795}
Manfred Spraul97e2bde2005-05-01 08:58:38 -0700796
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800797static size_t slab_mgmt_size(size_t nr_objs, size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800799 return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align);
800}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Andrew Mortona737b3e2006-03-22 00:08:11 -0800802/*
803 * Calculate the number of objects and left-over bytes for a given buffer size.
804 */
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800805static void cache_estimate(unsigned long gfporder, size_t buffer_size,
806 size_t align, int flags, size_t *left_over,
807 unsigned int *num)
808{
809 int nr_objs;
810 size_t mgmt_size;
811 size_t slab_size = PAGE_SIZE << gfporder;
812
813 /*
814 * The slab management structure can be either off the slab or
815 * on it. For the latter case, the memory allocated for a
816 * slab is used for:
817 *
818 * - The struct slab
819 * - One kmem_bufctl_t for each object
820 * - Padding to respect alignment of @align
821 * - @buffer_size bytes for each object
822 *
823 * If the slab management structure is off the slab, then the
824 * alignment will already be calculated into the size. Because
825 * the slabs are all pages aligned, the objects will be at the
826 * correct alignment when allocated.
827 */
828 if (flags & CFLGS_OFF_SLAB) {
829 mgmt_size = 0;
830 nr_objs = slab_size / buffer_size;
831
832 if (nr_objs > SLAB_LIMIT)
833 nr_objs = SLAB_LIMIT;
834 } else {
835 /*
836 * Ignore padding for the initial guess. The padding
837 * is at most @align-1 bytes, and @buffer_size is at
838 * least @align. In the worst case, this result will
839 * be one greater than the number of objects that fit
840 * into the memory allocation when taking the padding
841 * into account.
842 */
843 nr_objs = (slab_size - sizeof(struct slab)) /
844 (buffer_size + sizeof(kmem_bufctl_t));
845
846 /*
847 * This calculated number will be either the right
848 * amount, or one greater than what we want.
849 */
850 if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
851 > slab_size)
852 nr_objs--;
853
854 if (nr_objs > SLAB_LIMIT)
855 nr_objs = SLAB_LIMIT;
856
857 mgmt_size = slab_mgmt_size(nr_objs, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 }
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800859 *num = nr_objs;
860 *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861}
862
863#define slab_error(cachep, msg) __slab_error(__FUNCTION__, cachep, msg)
864
Andrew Mortona737b3e2006-03-22 00:08:11 -0800865static void __slab_error(const char *function, struct kmem_cache *cachep,
866 char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867{
868 printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800869 function, cachep->name, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 dump_stack();
871}
872
Paul Menage3395ee02006-12-06 20:32:16 -0800873/*
874 * By default on NUMA we use alien caches to stage the freeing of
875 * objects allocated from other nodes. This causes massive memory
876 * inefficiencies when using fake NUMA setup to split memory into a
877 * large number of small nodes, so it can be disabled on the command
878 * line
879 */
880
881static int use_alien_caches __read_mostly = 1;
882static int __init noaliencache_setup(char *s)
883{
884 use_alien_caches = 0;
885 return 1;
886}
887__setup("noaliencache", noaliencache_setup);
888
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800889#ifdef CONFIG_NUMA
890/*
891 * Special reaping functions for NUMA systems called from cache_reap().
892 * These take care of doing round robin flushing of alien caches (containing
893 * objects freed on different nodes from which they were allocated) and the
894 * flushing of remote pcps by calling drain_node_pages.
895 */
896static DEFINE_PER_CPU(unsigned long, reap_node);
897
898static void init_reap_node(int cpu)
899{
900 int node;
901
902 node = next_node(cpu_to_node(cpu), node_online_map);
903 if (node == MAX_NUMNODES)
Paul Jackson442295c2006-03-22 00:09:11 -0800904 node = first_node(node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800905
Daniel Yeisley7f6b8872006-11-02 22:07:14 -0800906 per_cpu(reap_node, cpu) = node;
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800907}
908
909static void next_reap_node(void)
910{
911 int node = __get_cpu_var(reap_node);
912
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800913 node = next_node(node, node_online_map);
914 if (unlikely(node >= MAX_NUMNODES))
915 node = first_node(node_online_map);
916 __get_cpu_var(reap_node) = node;
917}
918
919#else
920#define init_reap_node(cpu) do { } while (0)
921#define next_reap_node(void) do { } while (0)
922#endif
923
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924/*
925 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
926 * via the workqueue/eventd.
927 * Add the CPU number into the expiration time to minimize the possibility of
928 * the CPUs getting into lockstep and contending for the global cache chain
929 * lock.
930 */
931static void __devinit start_cpu_timer(int cpu)
932{
David Howells52bad642006-11-22 14:54:01 +0000933 struct delayed_work *reap_work = &per_cpu(reap_work, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 /*
936 * When this gets called from do_initcalls via cpucache_init(),
937 * init_workqueues() has already run, so keventd will be setup
938 * at that time.
939 */
David Howells52bad642006-11-22 14:54:01 +0000940 if (keventd_up() && reap_work->work.func == NULL) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800941 init_reap_node(cpu);
David Howells65f27f32006-11-22 14:55:48 +0000942 INIT_DELAYED_WORK(reap_work, cache_reap);
Arjan van de Ven2b284212006-12-10 02:21:28 -0800943 schedule_delayed_work_on(cpu, reap_work,
944 __round_jiffies_relative(HZ, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 }
946}
947
Christoph Lametere498be72005-09-09 13:03:32 -0700948static struct array_cache *alloc_arraycache(int node, int entries,
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800949 int batchcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800951 int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 struct array_cache *nc = NULL;
953
Christoph Lametere498be72005-09-09 13:03:32 -0700954 nc = kmalloc_node(memsize, GFP_KERNEL, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 if (nc) {
956 nc->avail = 0;
957 nc->limit = entries;
958 nc->batchcount = batchcount;
959 nc->touched = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700960 spin_lock_init(&nc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
962 return nc;
963}
964
Christoph Lameter3ded1752006-03-25 03:06:44 -0800965/*
966 * Transfer objects in one arraycache to another.
967 * Locking must be handled by the caller.
968 *
969 * Return the number of entries transferred.
970 */
971static int transfer_objects(struct array_cache *to,
972 struct array_cache *from, unsigned int max)
973{
974 /* Figure out how many entries to transfer */
975 int nr = min(min(from->avail, max), to->limit - to->avail);
976
977 if (!nr)
978 return 0;
979
980 memcpy(to->entry + to->avail, from->entry + from->avail -nr,
981 sizeof(void *) *nr);
982
983 from->avail -= nr;
984 to->avail += nr;
985 to->touched = 1;
986 return nr;
987}
988
Christoph Lameter765c4502006-09-27 01:50:08 -0700989#ifndef CONFIG_NUMA
990
991#define drain_alien_cache(cachep, alien) do { } while (0)
992#define reap_alien(cachep, l3) do { } while (0)
993
994static inline struct array_cache **alloc_alien_cache(int node, int limit)
995{
996 return (struct array_cache **)BAD_ALIEN_MAGIC;
997}
998
999static inline void free_alien_cache(struct array_cache **ac_ptr)
1000{
1001}
1002
1003static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
1004{
1005 return 0;
1006}
1007
1008static inline void *alternate_node_alloc(struct kmem_cache *cachep,
1009 gfp_t flags)
1010{
1011 return NULL;
1012}
1013
Christoph Hellwig8b98c162006-12-06 20:32:30 -08001014static inline void *____cache_alloc_node(struct kmem_cache *cachep,
Christoph Lameter765c4502006-09-27 01:50:08 -07001015 gfp_t flags, int nodeid)
1016{
1017 return NULL;
1018}
1019
1020#else /* CONFIG_NUMA */
1021
Christoph Hellwig8b98c162006-12-06 20:32:30 -08001022static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
Paul Jacksonc61afb12006-03-24 03:16:08 -08001023static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
Christoph Lameterdc85da12006-01-18 17:42:36 -08001024
Pekka Enberg5295a742006-02-01 03:05:48 -08001025static struct array_cache **alloc_alien_cache(int node, int limit)
Christoph Lametere498be72005-09-09 13:03:32 -07001026{
1027 struct array_cache **ac_ptr;
Christoph Lameter8ef82862007-02-20 13:57:52 -08001028 int memsize = sizeof(void *) * nr_node_ids;
Christoph Lametere498be72005-09-09 13:03:32 -07001029 int i;
1030
1031 if (limit > 1)
1032 limit = 12;
1033 ac_ptr = kmalloc_node(memsize, GFP_KERNEL, node);
1034 if (ac_ptr) {
1035 for_each_node(i) {
1036 if (i == node || !node_online(i)) {
1037 ac_ptr[i] = NULL;
1038 continue;
1039 }
1040 ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d);
1041 if (!ac_ptr[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001042 for (i--; i <= 0; i--)
Christoph Lametere498be72005-09-09 13:03:32 -07001043 kfree(ac_ptr[i]);
1044 kfree(ac_ptr);
1045 return NULL;
1046 }
1047 }
1048 }
1049 return ac_ptr;
1050}
1051
Pekka Enberg5295a742006-02-01 03:05:48 -08001052static void free_alien_cache(struct array_cache **ac_ptr)
Christoph Lametere498be72005-09-09 13:03:32 -07001053{
1054 int i;
1055
1056 if (!ac_ptr)
1057 return;
Christoph Lametere498be72005-09-09 13:03:32 -07001058 for_each_node(i)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001059 kfree(ac_ptr[i]);
Christoph Lametere498be72005-09-09 13:03:32 -07001060 kfree(ac_ptr);
1061}
1062
Pekka Enberg343e0d72006-02-01 03:05:50 -08001063static void __drain_alien_cache(struct kmem_cache *cachep,
Pekka Enberg5295a742006-02-01 03:05:48 -08001064 struct array_cache *ac, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07001065{
1066 struct kmem_list3 *rl3 = cachep->nodelists[node];
1067
1068 if (ac->avail) {
1069 spin_lock(&rl3->list_lock);
Christoph Lametere00946f2006-03-25 03:06:45 -08001070 /*
1071 * Stuff objects into the remote nodes shared array first.
1072 * That way we could avoid the overhead of putting the objects
1073 * into the free lists and getting them back later.
1074 */
shin, jacob693f7d32006-04-28 10:54:37 -05001075 if (rl3->shared)
1076 transfer_objects(rl3->shared, ac, ac->limit);
Christoph Lametere00946f2006-03-25 03:06:45 -08001077
Christoph Lameterff694162005-09-22 21:44:02 -07001078 free_block(cachep, ac->entry, ac->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001079 ac->avail = 0;
1080 spin_unlock(&rl3->list_lock);
1081 }
1082}
1083
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001084/*
1085 * Called from cache_reap() to regularly drain alien caches round robin.
1086 */
1087static void reap_alien(struct kmem_cache *cachep, struct kmem_list3 *l3)
1088{
1089 int node = __get_cpu_var(reap_node);
1090
1091 if (l3->alien) {
1092 struct array_cache *ac = l3->alien[node];
Christoph Lametere00946f2006-03-25 03:06:45 -08001093
1094 if (ac && ac->avail && spin_trylock_irq(&ac->lock)) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001095 __drain_alien_cache(cachep, ac, node);
1096 spin_unlock_irq(&ac->lock);
1097 }
1098 }
1099}
1100
Andrew Mortona737b3e2006-03-22 00:08:11 -08001101static void drain_alien_cache(struct kmem_cache *cachep,
1102 struct array_cache **alien)
Christoph Lametere498be72005-09-09 13:03:32 -07001103{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001104 int i = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07001105 struct array_cache *ac;
1106 unsigned long flags;
1107
1108 for_each_online_node(i) {
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001109 ac = alien[i];
Christoph Lametere498be72005-09-09 13:03:32 -07001110 if (ac) {
1111 spin_lock_irqsave(&ac->lock, flags);
1112 __drain_alien_cache(cachep, ac, i);
1113 spin_unlock_irqrestore(&ac->lock, flags);
1114 }
1115 }
1116}
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001117
Ingo Molnar873623d2006-07-13 14:44:38 +02001118static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001119{
1120 struct slab *slabp = virt_to_slab(objp);
1121 int nodeid = slabp->nodeid;
1122 struct kmem_list3 *l3;
1123 struct array_cache *alien = NULL;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001124 int node;
1125
1126 node = numa_node_id();
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001127
1128 /*
1129 * Make sure we are not freeing a object from another node to the array
1130 * cache on this cpu.
1131 */
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001132 if (likely(slabp->nodeid == node))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001133 return 0;
1134
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001135 l3 = cachep->nodelists[node];
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001136 STATS_INC_NODEFREES(cachep);
1137 if (l3->alien && l3->alien[nodeid]) {
1138 alien = l3->alien[nodeid];
Ingo Molnar873623d2006-07-13 14:44:38 +02001139 spin_lock(&alien->lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001140 if (unlikely(alien->avail == alien->limit)) {
1141 STATS_INC_ACOVERFLOW(cachep);
1142 __drain_alien_cache(cachep, alien, nodeid);
1143 }
1144 alien->entry[alien->avail++] = objp;
1145 spin_unlock(&alien->lock);
1146 } else {
1147 spin_lock(&(cachep->nodelists[nodeid])->list_lock);
1148 free_block(cachep, &objp, 1, nodeid);
1149 spin_unlock(&(cachep->nodelists[nodeid])->list_lock);
1150 }
1151 return 1;
1152}
Christoph Lametere498be72005-09-09 13:03:32 -07001153#endif
1154
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001155static int __cpuinit cpuup_callback(struct notifier_block *nfb,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001156 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
1158 long cpu = (long)hcpu;
Pekka Enberg343e0d72006-02-01 03:05:50 -08001159 struct kmem_cache *cachep;
Christoph Lametere498be72005-09-09 13:03:32 -07001160 struct kmem_list3 *l3 = NULL;
1161 int node = cpu_to_node(cpu);
1162 int memsize = sizeof(struct kmem_list3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163
1164 switch (action) {
Heiko Carstens38c3bd92007-05-09 02:34:05 -07001165 case CPU_LOCK_ACQUIRE:
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001166 mutex_lock(&cache_chain_mutex);
Heiko Carstens38c3bd92007-05-09 02:34:05 -07001167 break;
1168 case CPU_UP_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001169 case CPU_UP_PREPARE_FROZEN:
Andrew Mortona737b3e2006-03-22 00:08:11 -08001170 /*
1171 * We need to do this right in the beginning since
Christoph Lametere498be72005-09-09 13:03:32 -07001172 * alloc_arraycache's are going to use this list.
1173 * kmalloc_node allows us to add the slab to the right
1174 * kmem_list3 and not this cpu's kmem_list3
1175 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Christoph Lametere498be72005-09-09 13:03:32 -07001177 list_for_each_entry(cachep, &cache_chain, next) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08001178 /*
1179 * Set up the size64 kmemlist for cpu before we can
Christoph Lametere498be72005-09-09 13:03:32 -07001180 * begin anything. Make sure some other cpu on this
1181 * node has not already allocated this
1182 */
1183 if (!cachep->nodelists[node]) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08001184 l3 = kmalloc_node(memsize, GFP_KERNEL, node);
1185 if (!l3)
Christoph Lametere498be72005-09-09 13:03:32 -07001186 goto bad;
1187 kmem_list3_init(l3);
1188 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001189 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lametere498be72005-09-09 13:03:32 -07001190
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001191 /*
1192 * The l3s don't come and go as CPUs come and
1193 * go. cache_chain_mutex is sufficient
1194 * protection here.
1195 */
Christoph Lametere498be72005-09-09 13:03:32 -07001196 cachep->nodelists[node] = l3;
1197 }
1198
1199 spin_lock_irq(&cachep->nodelists[node]->list_lock);
1200 cachep->nodelists[node]->free_limit =
Andrew Mortona737b3e2006-03-22 00:08:11 -08001201 (1 + nr_cpus_node(node)) *
1202 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07001203 spin_unlock_irq(&cachep->nodelists[node]->list_lock);
1204 }
1205
Andrew Mortona737b3e2006-03-22 00:08:11 -08001206 /*
1207 * Now we can go ahead with allocating the shared arrays and
1208 * array caches
1209 */
Christoph Lametere498be72005-09-09 13:03:32 -07001210 list_for_each_entry(cachep, &cache_chain, next) {
Tobias Klausercd105df2006-01-08 01:00:59 -08001211 struct array_cache *nc;
Eric Dumazet63109842007-05-06 14:49:28 -07001212 struct array_cache *shared = NULL;
Paul Menage3395ee02006-12-06 20:32:16 -08001213 struct array_cache **alien = NULL;
Tobias Klausercd105df2006-01-08 01:00:59 -08001214
Christoph Lametere498be72005-09-09 13:03:32 -07001215 nc = alloc_arraycache(node, cachep->limit,
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001216 cachep->batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (!nc)
1218 goto bad;
Eric Dumazet63109842007-05-06 14:49:28 -07001219 if (cachep->shared) {
1220 shared = alloc_arraycache(node,
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001221 cachep->shared * cachep->batchcount,
1222 0xbaadf00d);
Eric Dumazet63109842007-05-06 14:49:28 -07001223 if (!shared)
1224 goto bad;
1225 }
Paul Menage3395ee02006-12-06 20:32:16 -08001226 if (use_alien_caches) {
1227 alien = alloc_alien_cache(node, cachep->limit);
1228 if (!alien)
1229 goto bad;
1230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 cachep->array[cpu] = nc;
Christoph Lametere498be72005-09-09 13:03:32 -07001232 l3 = cachep->nodelists[node];
1233 BUG_ON(!l3);
Christoph Lametere498be72005-09-09 13:03:32 -07001234
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001235 spin_lock_irq(&l3->list_lock);
1236 if (!l3->shared) {
1237 /*
1238 * We are serialised from CPU_DEAD or
1239 * CPU_UP_CANCELLED by the cpucontrol lock
1240 */
1241 l3->shared = shared;
1242 shared = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07001243 }
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001244#ifdef CONFIG_NUMA
1245 if (!l3->alien) {
1246 l3->alien = alien;
1247 alien = NULL;
1248 }
1249#endif
1250 spin_unlock_irq(&l3->list_lock);
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001251 kfree(shared);
1252 free_alien_cache(alien);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 break;
1255 case CPU_ONLINE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001256 case CPU_ONLINE_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 start_cpu_timer(cpu);
1258 break;
1259#ifdef CONFIG_HOTPLUG_CPU
Christoph Lameter5830c592007-05-09 02:34:22 -07001260 case CPU_DOWN_PREPARE:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001261 case CPU_DOWN_PREPARE_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001262 /*
1263 * Shutdown cache reaper. Note that the cache_chain_mutex is
1264 * held so that if cache_reap() is invoked it cannot do
1265 * anything expensive but will only modify reap_work
1266 * and reschedule the timer.
1267 */
1268 cancel_rearming_delayed_work(&per_cpu(reap_work, cpu));
1269 /* Now the cache_reaper is guaranteed to be not running. */
1270 per_cpu(reap_work, cpu).work.func = NULL;
1271 break;
1272 case CPU_DOWN_FAILED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001273 case CPU_DOWN_FAILED_FROZEN:
Christoph Lameter5830c592007-05-09 02:34:22 -07001274 start_cpu_timer(cpu);
1275 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 case CPU_DEAD:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001277 case CPU_DEAD_FROZEN:
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001278 /*
1279 * Even if all the cpus of a node are down, we don't free the
1280 * kmem_list3 of any cache. This to avoid a race between
1281 * cpu_down, and a kmalloc allocation from another cpu for
1282 * memory from the node of the cpu going down. The list3
1283 * structure is usually allocated from kmem_cache_create() and
1284 * gets destroyed at kmem_cache_destroy().
1285 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 /* fall thru */
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08001287#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 case CPU_UP_CANCELED:
Rafael J. Wysocki8bb78442007-05-09 02:35:10 -07001289 case CPU_UP_CANCELED_FROZEN:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 list_for_each_entry(cachep, &cache_chain, next) {
1291 struct array_cache *nc;
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001292 struct array_cache *shared;
1293 struct array_cache **alien;
Christoph Lametere498be72005-09-09 13:03:32 -07001294 cpumask_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Christoph Lametere498be72005-09-09 13:03:32 -07001296 mask = node_to_cpumask(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 /* cpu is dead; no one can alloc from it. */
1298 nc = cachep->array[cpu];
1299 cachep->array[cpu] = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07001300 l3 = cachep->nodelists[node];
1301
1302 if (!l3)
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001303 goto free_array_cache;
Christoph Lametere498be72005-09-09 13:03:32 -07001304
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08001305 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07001306
1307 /* Free limit for this kmem_list3 */
1308 l3->free_limit -= cachep->batchcount;
1309 if (nc)
Christoph Lameterff694162005-09-22 21:44:02 -07001310 free_block(cachep, nc->entry, nc->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001311
1312 if (!cpus_empty(mask)) {
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08001313 spin_unlock_irq(&l3->list_lock);
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001314 goto free_array_cache;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001315 }
Christoph Lametere498be72005-09-09 13:03:32 -07001316
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001317 shared = l3->shared;
1318 if (shared) {
Eric Dumazet63109842007-05-06 14:49:28 -07001319 free_block(cachep, shared->entry,
1320 shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001321 l3->shared = NULL;
1322 }
Christoph Lametere498be72005-09-09 13:03:32 -07001323
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001324 alien = l3->alien;
1325 l3->alien = NULL;
1326
1327 spin_unlock_irq(&l3->list_lock);
1328
1329 kfree(shared);
1330 if (alien) {
1331 drain_alien_cache(cachep, alien);
1332 free_alien_cache(alien);
Christoph Lametere498be72005-09-09 13:03:32 -07001333 }
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001334free_array_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001335 kfree(nc);
1336 }
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001337 /*
1338 * In the previous loop, all the objects were freed to
1339 * the respective cache's slabs, now we can go ahead and
1340 * shrink each nodelist to its limit.
1341 */
1342 list_for_each_entry(cachep, &cache_chain, next) {
1343 l3 = cachep->nodelists[node];
1344 if (!l3)
1345 continue;
Christoph Lametered11d9e2006-06-30 01:55:45 -07001346 drain_freelist(cachep, l3, l3->free_objects);
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001347 }
Heiko Carstens38c3bd92007-05-09 02:34:05 -07001348 break;
1349 case CPU_LOCK_RELEASE:
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001350 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
1353 return NOTIFY_OK;
Andrew Mortona737b3e2006-03-22 00:08:11 -08001354bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 return NOTIFY_BAD;
1356}
1357
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001358static struct notifier_block __cpuinitdata cpucache_notifier = {
1359 &cpuup_callback, NULL, 0
1360};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
Christoph Lametere498be72005-09-09 13:03:32 -07001362/*
1363 * swap the static kmem_list3 with kmalloced memory
1364 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001365static void init_list(struct kmem_cache *cachep, struct kmem_list3 *list,
1366 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07001367{
1368 struct kmem_list3 *ptr;
1369
Christoph Lametere498be72005-09-09 13:03:32 -07001370 ptr = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, nodeid);
1371 BUG_ON(!ptr);
1372
1373 local_irq_disable();
1374 memcpy(ptr, list, sizeof(struct kmem_list3));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001375 /*
1376 * Do not assume that spinlocks can be initialized via memcpy:
1377 */
1378 spin_lock_init(&ptr->list_lock);
1379
Christoph Lametere498be72005-09-09 13:03:32 -07001380 MAKE_ALL_LISTS(cachep, ptr, nodeid);
1381 cachep->nodelists[nodeid] = ptr;
1382 local_irq_enable();
1383}
1384
Andrew Mortona737b3e2006-03-22 00:08:11 -08001385/*
1386 * Initialisation. Called after the page allocator have been initialised and
1387 * before smp_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 */
1389void __init kmem_cache_init(void)
1390{
1391 size_t left_over;
1392 struct cache_sizes *sizes;
1393 struct cache_names *names;
Christoph Lametere498be72005-09-09 13:03:32 -07001394 int i;
Jack Steiner07ed76b2006-03-07 21:55:46 -08001395 int order;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001396 int node;
Christoph Lametere498be72005-09-09 13:03:32 -07001397
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001398 if (num_possible_nodes() == 1)
1399 use_alien_caches = 0;
1400
Christoph Lametere498be72005-09-09 13:03:32 -07001401 for (i = 0; i < NUM_INIT_LISTS; i++) {
1402 kmem_list3_init(&initkmem_list3[i]);
1403 if (i < MAX_NUMNODES)
1404 cache_cache.nodelists[i] = NULL;
1405 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
1407 /*
1408 * Fragmentation resistance on low memory - only use bigger
1409 * page orders on machines with more than 32MB of memory.
1410 */
1411 if (num_physpages > (32 << 20) >> PAGE_SHIFT)
1412 slab_break_gfp_order = BREAK_GFP_ORDER_HI;
1413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 /* Bootstrap is tricky, because several objects are allocated
1415 * from caches that do not exist yet:
Andrew Mortona737b3e2006-03-22 00:08:11 -08001416 * 1) initialize the cache_cache cache: it contains the struct
1417 * kmem_cache structures of all caches, except cache_cache itself:
1418 * cache_cache is statically allocated.
Christoph Lametere498be72005-09-09 13:03:32 -07001419 * Initially an __init data area is used for the head array and the
1420 * kmem_list3 structures, it's replaced with a kmalloc allocated
1421 * array at the end of the bootstrap.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 * 2) Create the first kmalloc cache.
Pekka Enberg343e0d72006-02-01 03:05:50 -08001423 * The struct kmem_cache for the new cache is allocated normally.
Christoph Lametere498be72005-09-09 13:03:32 -07001424 * An __init data area is used for the head array.
1425 * 3) Create the remaining kmalloc caches, with minimally sized
1426 * head arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 * 4) Replace the __init data head arrays for cache_cache and the first
1428 * kmalloc cache with kmalloc allocated arrays.
Christoph Lametere498be72005-09-09 13:03:32 -07001429 * 5) Replace the __init data for kmem_list3 for cache_cache and
1430 * the other cache's with kmalloc allocated memory.
1431 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 */
1433
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001434 node = numa_node_id();
1435
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 /* 1) create the cache_cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 INIT_LIST_HEAD(&cache_chain);
1438 list_add(&cache_cache.next, &cache_chain);
1439 cache_cache.colour_off = cache_line_size();
1440 cache_cache.array[smp_processor_id()] = &initarray_cache.cache;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001441 cache_cache.nodelists[node] = &initkmem_list3[CACHE_CACHE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442
Eric Dumazet8da34302007-05-06 14:49:29 -07001443 /*
1444 * struct kmem_cache size depends on nr_node_ids, which
1445 * can be less than MAX_NUMNODES.
1446 */
1447 cache_cache.buffer_size = offsetof(struct kmem_cache, nodelists) +
1448 nr_node_ids * sizeof(struct kmem_list3 *);
1449#if DEBUG
1450 cache_cache.obj_size = cache_cache.buffer_size;
1451#endif
Andrew Mortona737b3e2006-03-22 00:08:11 -08001452 cache_cache.buffer_size = ALIGN(cache_cache.buffer_size,
1453 cache_line_size());
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08001454 cache_cache.reciprocal_buffer_size =
1455 reciprocal_value(cache_cache.buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001456
Jack Steiner07ed76b2006-03-07 21:55:46 -08001457 for (order = 0; order < MAX_ORDER; order++) {
1458 cache_estimate(order, cache_cache.buffer_size,
1459 cache_line_size(), 0, &left_over, &cache_cache.num);
1460 if (cache_cache.num)
1461 break;
1462 }
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02001463 BUG_ON(!cache_cache.num);
Jack Steiner07ed76b2006-03-07 21:55:46 -08001464 cache_cache.gfporder = order;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001465 cache_cache.colour = left_over / cache_cache.colour_off;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001466 cache_cache.slab_size = ALIGN(cache_cache.num * sizeof(kmem_bufctl_t) +
1467 sizeof(struct slab), cache_line_size());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
1469 /* 2+3) create the kmalloc caches */
1470 sizes = malloc_sizes;
1471 names = cache_names;
1472
Andrew Mortona737b3e2006-03-22 00:08:11 -08001473 /*
1474 * Initialize the caches that provide memory for the array cache and the
1475 * kmem_list3 structures first. Without this, further allocations will
1476 * bug.
Christoph Lametere498be72005-09-09 13:03:32 -07001477 */
1478
1479 sizes[INDEX_AC].cs_cachep = kmem_cache_create(names[INDEX_AC].name,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001480 sizes[INDEX_AC].cs_size,
1481 ARCH_KMALLOC_MINALIGN,
1482 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
1483 NULL, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07001484
Andrew Mortona737b3e2006-03-22 00:08:11 -08001485 if (INDEX_AC != INDEX_L3) {
Christoph Lametere498be72005-09-09 13:03:32 -07001486 sizes[INDEX_L3].cs_cachep =
Andrew Mortona737b3e2006-03-22 00:08:11 -08001487 kmem_cache_create(names[INDEX_L3].name,
1488 sizes[INDEX_L3].cs_size,
1489 ARCH_KMALLOC_MINALIGN,
1490 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
1491 NULL, NULL);
1492 }
Christoph Lametere498be72005-09-09 13:03:32 -07001493
Ingo Molnare0a42722006-06-23 02:03:46 -07001494 slab_early_init = 0;
1495
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 while (sizes->cs_size != ULONG_MAX) {
Christoph Lametere498be72005-09-09 13:03:32 -07001497 /*
1498 * For performance, all the general caches are L1 aligned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 * This should be particularly beneficial on SMP boxes, as it
1500 * eliminates "false sharing".
1501 * Note for systems short on memory removing the alignment will
Christoph Lametere498be72005-09-09 13:03:32 -07001502 * allow tighter packing of the smaller caches.
1503 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001504 if (!sizes->cs_cachep) {
Christoph Lametere498be72005-09-09 13:03:32 -07001505 sizes->cs_cachep = kmem_cache_create(names->name,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001506 sizes->cs_size,
1507 ARCH_KMALLOC_MINALIGN,
1508 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
1509 NULL, NULL);
1510 }
Christoph Lameter4b51d662007-02-10 01:43:10 -08001511#ifdef CONFIG_ZONE_DMA
1512 sizes->cs_dmacachep = kmem_cache_create(
1513 names->name_dma,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001514 sizes->cs_size,
1515 ARCH_KMALLOC_MINALIGN,
1516 ARCH_KMALLOC_FLAGS|SLAB_CACHE_DMA|
1517 SLAB_PANIC,
1518 NULL, NULL);
Christoph Lameter4b51d662007-02-10 01:43:10 -08001519#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 sizes++;
1521 names++;
1522 }
1523 /* 4) Replace the bootstrap head arrays */
1524 {
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001525 struct array_cache *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001526
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
Christoph Lametere498be72005-09-09 13:03:32 -07001528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 local_irq_disable();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001530 BUG_ON(cpu_cache_get(&cache_cache) != &initarray_cache.cache);
1531 memcpy(ptr, cpu_cache_get(&cache_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001532 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001533 /*
1534 * Do not assume that spinlocks can be initialized via memcpy:
1535 */
1536 spin_lock_init(&ptr->lock);
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 cache_cache.array[smp_processor_id()] = ptr;
1539 local_irq_enable();
Christoph Lametere498be72005-09-09 13:03:32 -07001540
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
Christoph Lametere498be72005-09-09 13:03:32 -07001542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 local_irq_disable();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001544 BUG_ON(cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001545 != &initarray_generic.cache);
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001546 memcpy(ptr, cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001547 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001548 /*
1549 * Do not assume that spinlocks can be initialized via memcpy:
1550 */
1551 spin_lock_init(&ptr->lock);
1552
Christoph Lametere498be72005-09-09 13:03:32 -07001553 malloc_sizes[INDEX_AC].cs_cachep->array[smp_processor_id()] =
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001554 ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 local_irq_enable();
1556 }
Christoph Lametere498be72005-09-09 13:03:32 -07001557 /* 5) Replace the bootstrap kmem_list3's */
1558 {
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001559 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001561 /* Replace the static kmem_list3 structures for the boot cpu */
1562 init_list(&cache_cache, &initkmem_list3[CACHE_CACHE], node);
1563
1564 for_each_online_node(nid) {
Christoph Lametere498be72005-09-09 13:03:32 -07001565 init_list(malloc_sizes[INDEX_AC].cs_cachep,
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001566 &initkmem_list3[SIZE_AC + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001567
1568 if (INDEX_AC != INDEX_L3) {
1569 init_list(malloc_sizes[INDEX_L3].cs_cachep,
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001570 &initkmem_list3[SIZE_L3 + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001571 }
1572 }
1573 }
1574
1575 /* 6) resize the head arrays to their final sizes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 {
Pekka Enberg343e0d72006-02-01 03:05:50 -08001577 struct kmem_cache *cachep;
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001578 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 list_for_each_entry(cachep, &cache_chain, next)
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07001580 if (enable_cpucache(cachep))
1581 BUG();
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001582 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 }
1584
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001585 /* Annotate slab for lockdep -- annotate the malloc caches */
1586 init_lock_keys();
1587
1588
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 /* Done! */
1590 g_cpucache_up = FULL;
1591
Andrew Mortona737b3e2006-03-22 00:08:11 -08001592 /*
1593 * Register a cpu startup notifier callback that initializes
1594 * cpu_cache_get for all new cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 */
1596 register_cpu_notifier(&cpucache_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
Andrew Mortona737b3e2006-03-22 00:08:11 -08001598 /*
1599 * The reap timers are started later, with a module init call: That part
1600 * of the kernel is not yet operational.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 */
1602}
1603
1604static int __init cpucache_init(void)
1605{
1606 int cpu;
1607
Andrew Mortona737b3e2006-03-22 00:08:11 -08001608 /*
1609 * Register the timers that return unneeded pages to the page allocator
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 */
Christoph Lametere498be72005-09-09 13:03:32 -07001611 for_each_online_cpu(cpu)
Andrew Mortona737b3e2006-03-22 00:08:11 -08001612 start_cpu_timer(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return 0;
1614}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615__initcall(cpucache_init);
1616
1617/*
1618 * Interface to system's page allocator. No need to hold the cache-lock.
1619 *
1620 * If we requested dmaable memory, we will get it. Even if we
1621 * did not request dmaable memory, we might get it, but that
1622 * would be relatively rare and ignorable.
1623 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001624static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625{
1626 struct page *page;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001627 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 int i;
1629
Luke Yangd6fef9d2006-04-10 22:52:56 -07001630#ifndef CONFIG_MMU
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001631 /*
1632 * Nommu uses slab's for process anonymous memory allocations, and thus
1633 * requires __GFP_COMP to properly refcount higher order allocations
Luke Yangd6fef9d2006-04-10 22:52:56 -07001634 */
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001635 flags |= __GFP_COMP;
Luke Yangd6fef9d2006-04-10 22:52:56 -07001636#endif
Christoph Lameter765c4502006-09-27 01:50:08 -07001637
Christoph Lameter3c517a62006-12-06 20:33:29 -08001638 flags |= cachep->gfpflags;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001639
1640 page = alloc_pages_node(nodeid, flags, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 if (!page)
1642 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001644 nr_pages = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
Christoph Lameter972d1a72006-09-25 23:31:51 -07001646 add_zone_page_state(page_zone(page),
1647 NR_SLAB_RECLAIMABLE, nr_pages);
1648 else
1649 add_zone_page_state(page_zone(page),
1650 NR_SLAB_UNRECLAIMABLE, nr_pages);
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001651 for (i = 0; i < nr_pages; i++)
1652 __SetPageSlab(page + i);
1653 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654}
1655
1656/*
1657 * Interface to system's page release.
1658 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001659static void kmem_freepages(struct kmem_cache *cachep, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001661 unsigned long i = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 struct page *page = virt_to_page(addr);
1663 const unsigned long nr_freed = i;
1664
Christoph Lameter972d1a72006-09-25 23:31:51 -07001665 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1666 sub_zone_page_state(page_zone(page),
1667 NR_SLAB_RECLAIMABLE, nr_freed);
1668 else
1669 sub_zone_page_state(page_zone(page),
1670 NR_SLAB_UNRECLAIMABLE, nr_freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 while (i--) {
Nick Pigginf205b2f2006-03-22 00:08:02 -08001672 BUG_ON(!PageSlab(page));
1673 __ClearPageSlab(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 page++;
1675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 if (current->reclaim_state)
1677 current->reclaim_state->reclaimed_slab += nr_freed;
1678 free_pages((unsigned long)addr, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679}
1680
1681static void kmem_rcu_free(struct rcu_head *head)
1682{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001683 struct slab_rcu *slab_rcu = (struct slab_rcu *)head;
Pekka Enberg343e0d72006-02-01 03:05:50 -08001684 struct kmem_cache *cachep = slab_rcu->cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
1686 kmem_freepages(cachep, slab_rcu->addr);
1687 if (OFF_SLAB(cachep))
1688 kmem_cache_free(cachep->slabp_cache, slab_rcu);
1689}
1690
1691#if DEBUG
1692
1693#ifdef CONFIG_DEBUG_PAGEALLOC
Pekka Enberg343e0d72006-02-01 03:05:50 -08001694static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001695 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001697 int size = obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001699 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001701 if (size < 5 * sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return;
1703
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001704 *addr++ = 0x12345678;
1705 *addr++ = caller;
1706 *addr++ = smp_processor_id();
1707 size -= 3 * sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 {
1709 unsigned long *sptr = &caller;
1710 unsigned long svalue;
1711
1712 while (!kstack_end(sptr)) {
1713 svalue = *sptr++;
1714 if (kernel_text_address(svalue)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001715 *addr++ = svalue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 size -= sizeof(unsigned long);
1717 if (size <= sizeof(unsigned long))
1718 break;
1719 }
1720 }
1721
1722 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001723 *addr++ = 0x87654321;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724}
1725#endif
1726
Pekka Enberg343e0d72006-02-01 03:05:50 -08001727static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001729 int size = obj_size(cachep);
1730 addr = &((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732 memset(addr, val, size);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001733 *(unsigned char *)(addr + size - 1) = POISON_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734}
1735
1736static void dump_line(char *data, int offset, int limit)
1737{
1738 int i;
Dave Jonesaa83aa42006-09-29 01:59:51 -07001739 unsigned char error = 0;
1740 int bad_count = 0;
1741
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 printk(KERN_ERR "%03x:", offset);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001743 for (i = 0; i < limit; i++) {
1744 if (data[offset + i] != POISON_FREE) {
1745 error = data[offset + i];
1746 bad_count++;
1747 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001748 printk(" %02x", (unsigned char)data[offset + i]);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001749 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 printk("\n");
Dave Jonesaa83aa42006-09-29 01:59:51 -07001751
1752 if (bad_count == 1) {
1753 error ^= POISON_FREE;
1754 if (!(error & (error - 1))) {
1755 printk(KERN_ERR "Single bit error detected. Probably "
1756 "bad RAM.\n");
1757#ifdef CONFIG_X86
1758 printk(KERN_ERR "Run memtest86+ or a similar memory "
1759 "test tool.\n");
1760#else
1761 printk(KERN_ERR "Run a memory test tool.\n");
1762#endif
1763 }
1764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765}
1766#endif
1767
1768#if DEBUG
1769
Pekka Enberg343e0d72006-02-01 03:05:50 -08001770static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
1772 int i, size;
1773 char *realobj;
1774
1775 if (cachep->flags & SLAB_RED_ZONE) {
David Woodhouseb46b8f12007-05-08 00:22:59 -07001776 printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001777 *dbg_redzone1(cachep, objp),
1778 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 }
1780
1781 if (cachep->flags & SLAB_STORE_USER) {
1782 printk(KERN_ERR "Last user: [<%p>]",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001783 *dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 print_symbol("(%s)",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001785 (unsigned long)*dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 printk("\n");
1787 }
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001788 realobj = (char *)objp + obj_offset(cachep);
1789 size = obj_size(cachep);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001790 for (i = 0; i < size && lines; i += 16, lines--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 int limit;
1792 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001793 if (i + limit > size)
1794 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 dump_line(realobj, i, limit);
1796 }
1797}
1798
Pekka Enberg343e0d72006-02-01 03:05:50 -08001799static void check_poison_obj(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
1801 char *realobj;
1802 int size, i;
1803 int lines = 0;
1804
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001805 realobj = (char *)objp + obj_offset(cachep);
1806 size = obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001808 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 char exp = POISON_FREE;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001810 if (i == size - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 exp = POISON_END;
1812 if (realobj[i] != exp) {
1813 int limit;
1814 /* Mismatch ! */
1815 /* Print header */
1816 if (lines == 0) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001817 printk(KERN_ERR
David Howellse94a40c2007-04-02 23:46:28 +01001818 "Slab corruption: %s start=%p, len=%d\n",
1819 cachep->name, realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 print_objinfo(cachep, objp, 0);
1821 }
1822 /* Hexdump the affected line */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001823 i = (i / 16) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001825 if (i + limit > size)
1826 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 dump_line(realobj, i, limit);
1828 i += 16;
1829 lines++;
1830 /* Limit to 5 lines */
1831 if (lines > 5)
1832 break;
1833 }
1834 }
1835 if (lines != 0) {
1836 /* Print some data about the neighboring objects, if they
1837 * exist:
1838 */
Pekka Enberg6ed5eb22006-02-01 03:05:49 -08001839 struct slab *slabp = virt_to_slab(objp);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001840 unsigned int objnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001842 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 if (objnr) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001844 objp = index_to_obj(cachep, slabp, objnr - 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001845 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001847 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 print_objinfo(cachep, objp, 2);
1849 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001850 if (objnr + 1 < cachep->num) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001851 objp = index_to_obj(cachep, slabp, objnr + 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001852 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001854 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 print_objinfo(cachep, objp, 2);
1856 }
1857 }
1858}
1859#endif
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861#if DEBUG
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001862/**
Randy Dunlap911851e2006-03-22 00:08:14 -08001863 * slab_destroy_objs - destroy a slab and its objects
1864 * @cachep: cache pointer being destroyed
1865 * @slabp: slab pointer being destroyed
1866 *
1867 * Call the registered destructor for each object in a slab that is being
1868 * destroyed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001869 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001870static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001871{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 int i;
1873 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001874 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 if (cachep->flags & SLAB_POISON) {
1877#ifdef CONFIG_DEBUG_PAGEALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -08001878 if (cachep->buffer_size % PAGE_SIZE == 0 &&
1879 OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001880 kernel_map_pages(virt_to_page(objp),
Andrew Mortona737b3e2006-03-22 00:08:11 -08001881 cachep->buffer_size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 else
1883 check_poison_obj(cachep, objp);
1884#else
1885 check_poison_obj(cachep, objp);
1886#endif
1887 }
1888 if (cachep->flags & SLAB_RED_ZONE) {
1889 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
1890 slab_error(cachep, "start of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001891 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
1893 slab_error(cachep, "end of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001894 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001897}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898#else
Pekka Enberg343e0d72006-02-01 03:05:50 -08001899static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001900{
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001901}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902#endif
1903
Randy Dunlap911851e2006-03-22 00:08:14 -08001904/**
1905 * slab_destroy - destroy and release all objects in a slab
1906 * @cachep: cache pointer being destroyed
1907 * @slabp: slab pointer being destroyed
1908 *
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001909 * Destroy all the objs in a slab, and release the mem back to the system.
Andrew Mortona737b3e2006-03-22 00:08:11 -08001910 * Before calling the slab must have been unlinked from the cache. The
1911 * cache-lock is not held/needed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001912 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001913static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001914{
1915 void *addr = slabp->s_mem - slabp->colouroff;
1916
1917 slab_destroy_objs(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
1919 struct slab_rcu *slab_rcu;
1920
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001921 slab_rcu = (struct slab_rcu *)slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 slab_rcu->cachep = cachep;
1923 slab_rcu->addr = addr;
1924 call_rcu(&slab_rcu->head, kmem_rcu_free);
1925 } else {
1926 kmem_freepages(cachep, addr);
Ingo Molnar873623d2006-07-13 14:44:38 +02001927 if (OFF_SLAB(cachep))
1928 kmem_cache_free(cachep->slabp_cache, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 }
1930}
1931
Andrew Mortona737b3e2006-03-22 00:08:11 -08001932/*
1933 * For setting up all the kmem_list3s for cache whose buffer_size is same as
1934 * size of kmem_list3.
1935 */
Andrew Mortona3a02be2007-05-06 14:49:31 -07001936static void __init set_up_list3s(struct kmem_cache *cachep, int index)
Christoph Lametere498be72005-09-09 13:03:32 -07001937{
1938 int node;
1939
1940 for_each_online_node(node) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001941 cachep->nodelists[node] = &initkmem_list3[index + node];
Christoph Lametere498be72005-09-09 13:03:32 -07001942 cachep->nodelists[node]->next_reap = jiffies +
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001943 REAPTIMEOUT_LIST3 +
1944 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lametere498be72005-09-09 13:03:32 -07001945 }
1946}
1947
Christoph Lameter117f6eb2006-09-25 23:31:37 -07001948static void __kmem_cache_destroy(struct kmem_cache *cachep)
1949{
1950 int i;
1951 struct kmem_list3 *l3;
1952
1953 for_each_online_cpu(i)
1954 kfree(cachep->array[i]);
1955
1956 /* NUMA: free the list3 structures */
1957 for_each_online_node(i) {
1958 l3 = cachep->nodelists[i];
1959 if (l3) {
1960 kfree(l3->shared);
1961 free_alien_cache(l3->alien);
1962 kfree(l3);
1963 }
1964 }
1965 kmem_cache_free(&cache_cache, cachep);
1966}
1967
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969/**
Randy.Dunlapa70773d2006-02-01 03:05:52 -08001970 * calculate_slab_order - calculate size (page order) of slabs
1971 * @cachep: pointer to the cache that is being created
1972 * @size: size of objects to be created in this cache.
1973 * @align: required alignment for the objects.
1974 * @flags: slab allocation flags
1975 *
1976 * Also calculates the number of objects per slab.
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001977 *
1978 * This could be made much more intelligent. For now, try to avoid using
1979 * high order pages for slabs. When the gfp() functions are more friendly
1980 * towards high-order requests, this should be changed.
1981 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001982static size_t calculate_slab_order(struct kmem_cache *cachep,
Randy Dunlapee13d782006-02-01 03:05:53 -08001983 size_t size, size_t align, unsigned long flags)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001984{
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02001985 unsigned long offslab_limit;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001986 size_t left_over = 0;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001987 int gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001988
Christoph Lameter0aa817f2007-05-16 22:11:01 -07001989 for (gfporder = 0; gfporder <= KMALLOC_MAX_ORDER; gfporder++) {
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001990 unsigned int num;
1991 size_t remainder;
1992
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001993 cache_estimate(gfporder, size, align, flags, &remainder, &num);
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001994 if (!num)
1995 continue;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001996
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02001997 if (flags & CFLGS_OFF_SLAB) {
1998 /*
1999 * Max number of objs-per-slab for caches which
2000 * use off-slab slabs. Needed to avoid a possible
2001 * looping condition in cache_grow().
2002 */
2003 offslab_limit = size - sizeof(struct slab);
2004 offslab_limit /= sizeof(kmem_bufctl_t);
2005
2006 if (num > offslab_limit)
2007 break;
2008 }
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002009
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002010 /* Found something acceptable - save it away */
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002011 cachep->num = num;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002012 cachep->gfporder = gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002013 left_over = remainder;
2014
2015 /*
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08002016 * A VFS-reclaimable slab tends to have most allocations
2017 * as GFP_NOFS and we really don't want to have to be allocating
2018 * higher-order pages when we are unable to shrink dcache.
2019 */
2020 if (flags & SLAB_RECLAIM_ACCOUNT)
2021 break;
2022
2023 /*
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002024 * Large number of objects is good, but very large slabs are
2025 * currently bad for the gfp()s.
2026 */
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002027 if (gfporder >= slab_break_gfp_order)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002028 break;
2029
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002030 /*
2031 * Acceptable internal fragmentation?
2032 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002033 if (left_over * 8 <= (PAGE_SIZE << gfporder))
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002034 break;
2035 }
2036 return left_over;
2037}
2038
Sam Ravnborg38bdc322007-05-17 23:48:19 +02002039static int __init_refok setup_cpu_cache(struct kmem_cache *cachep)
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002040{
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002041 if (g_cpucache_up == FULL)
2042 return enable_cpucache(cachep);
2043
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002044 if (g_cpucache_up == NONE) {
2045 /*
2046 * Note: the first kmem_cache_create must create the cache
2047 * that's used by kmalloc(24), otherwise the creation of
2048 * further caches will BUG().
2049 */
2050 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2051
2052 /*
2053 * If the cache that's used by kmalloc(sizeof(kmem_list3)) is
2054 * the first cache, then we need to set up all its list3s,
2055 * otherwise the creation of further caches will BUG().
2056 */
2057 set_up_list3s(cachep, SIZE_AC);
2058 if (INDEX_AC == INDEX_L3)
2059 g_cpucache_up = PARTIAL_L3;
2060 else
2061 g_cpucache_up = PARTIAL_AC;
2062 } else {
2063 cachep->array[smp_processor_id()] =
2064 kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
2065
2066 if (g_cpucache_up == PARTIAL_AC) {
2067 set_up_list3s(cachep, SIZE_L3);
2068 g_cpucache_up = PARTIAL_L3;
2069 } else {
2070 int node;
2071 for_each_online_node(node) {
2072 cachep->nodelists[node] =
2073 kmalloc_node(sizeof(struct kmem_list3),
2074 GFP_KERNEL, node);
2075 BUG_ON(!cachep->nodelists[node]);
2076 kmem_list3_init(cachep->nodelists[node]);
2077 }
2078 }
2079 }
2080 cachep->nodelists[numa_node_id()]->next_reap =
2081 jiffies + REAPTIMEOUT_LIST3 +
2082 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
2083
2084 cpu_cache_get(cachep)->avail = 0;
2085 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
2086 cpu_cache_get(cachep)->batchcount = 1;
2087 cpu_cache_get(cachep)->touched = 0;
2088 cachep->batchcount = 1;
2089 cachep->limit = BOOT_CPUCACHE_ENTRIES;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002090 return 0;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002091}
2092
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002093/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 * kmem_cache_create - Create a cache.
2095 * @name: A string which is used in /proc/slabinfo to identify this cache.
2096 * @size: The size of objects to be created in this cache.
2097 * @align: The required alignment for the objects.
2098 * @flags: SLAB flags
2099 * @ctor: A constructor for the objects.
Christoph Lameterc59def9f2007-05-16 22:10:50 -07002100 * @dtor: A destructor for the objects (not implemented anymore).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 *
2102 * Returns a ptr to the cache on success, NULL on failure.
2103 * Cannot be called within a int, but can be interrupted.
2104 * The @ctor is run when new pages are allocated by the cache
2105 * and the @dtor is run before the pages are handed back.
2106 *
2107 * @name must be valid until the cache is destroyed. This implies that
Andrew Mortona737b3e2006-03-22 00:08:11 -08002108 * the module calling this has to destroy the cache before getting unloaded.
2109 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 * The flags are
2111 *
2112 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2113 * to catch references to uninitialised memory.
2114 *
2115 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2116 * for buffer overruns.
2117 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2119 * cacheline. This can be beneficial if you're counting cycles as closely
2120 * as davem.
2121 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002122struct kmem_cache *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123kmem_cache_create (const char *name, size_t size, size_t align,
Andrew Mortona737b3e2006-03-22 00:08:11 -08002124 unsigned long flags,
2125 void (*ctor)(void*, struct kmem_cache *, unsigned long),
Pekka Enberg343e0d72006-02-01 03:05:50 -08002126 void (*dtor)(void*, struct kmem_cache *, unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127{
2128 size_t left_over, slab_size, ralign;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07002129 struct kmem_cache *cachep = NULL, *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131 /*
2132 * Sanity checks... these are all serious usage bugs.
2133 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002134 if (!name || in_interrupt() || (size < BYTES_PER_WORD) ||
Christoph Lameter0aa817f2007-05-16 22:11:01 -07002135 size > KMALLOC_MAX_SIZE || dtor) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002136 printk(KERN_ERR "%s: Early error in slab %s\n", __FUNCTION__,
2137 name);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002138 BUG();
2139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
Ravikiran G Thirumalaif0188f42006-02-10 01:51:13 -08002141 /*
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002142 * We use cache_chain_mutex to ensure a consistent view of
2143 * cpu_online_map as well. Please see cpuup_callback
Ravikiran G Thirumalaif0188f42006-02-10 01:51:13 -08002144 */
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002145 mutex_lock(&cache_chain_mutex);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002146
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07002147 list_for_each_entry(pc, &cache_chain, next) {
Andrew Morton4f12bb42005-11-07 00:58:00 -08002148 char tmp;
2149 int res;
2150
2151 /*
2152 * This happens when the module gets unloaded and doesn't
2153 * destroy its slab cache and no-one else reuses the vmalloc
2154 * area of the module. Print a warning.
2155 */
Andrew Morton138ae662006-12-06 20:36:41 -08002156 res = probe_kernel_address(pc->name, tmp);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002157 if (res) {
matzeb4169522007-05-06 14:49:52 -07002158 printk(KERN_ERR
2159 "SLAB: cache with size %d has lost its name\n",
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002160 pc->buffer_size);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002161 continue;
2162 }
2163
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002164 if (!strcmp(pc->name, name)) {
matzeb4169522007-05-06 14:49:52 -07002165 printk(KERN_ERR
2166 "kmem_cache_create: duplicate cache %s\n", name);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002167 dump_stack();
2168 goto oops;
2169 }
2170 }
2171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172#if DEBUG
2173 WARN_ON(strchr(name, ' ')); /* It confuses parsers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174#if FORCED_DEBUG
2175 /*
2176 * Enable redzoning and last user accounting, except for caches with
2177 * large objects, if the increased size would increase the object size
2178 * above the next power of two: caches with object sizes just above a
2179 * power of two have a significant amount of internal fragmentation.
2180 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002181 if (size < 4096 || fls(size - 1) == fls(size-1 + 3 * BYTES_PER_WORD))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002182 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 if (!(flags & SLAB_DESTROY_BY_RCU))
2184 flags |= SLAB_POISON;
2185#endif
2186 if (flags & SLAB_DESTROY_BY_RCU)
2187 BUG_ON(flags & SLAB_POISON);
2188#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002190 * Always checks flags, a caller might be expecting debug support which
2191 * isn't available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 */
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002193 BUG_ON(flags & ~CREATE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Andrew Mortona737b3e2006-03-22 00:08:11 -08002195 /*
2196 * Check that size is in terms of words. This is needed to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 * unaligned accesses for some archs when redzoning is used, and makes
2198 * sure any on-slab bufctl's are also correctly aligned.
2199 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002200 if (size & (BYTES_PER_WORD - 1)) {
2201 size += (BYTES_PER_WORD - 1);
2202 size &= ~(BYTES_PER_WORD - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 }
2204
Andrew Mortona737b3e2006-03-22 00:08:11 -08002205 /* calculate the final buffer alignment: */
2206
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 /* 1) arch recommendation: can be overridden for debug */
2208 if (flags & SLAB_HWCACHE_ALIGN) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002209 /*
2210 * Default alignment: as specified by the arch code. Except if
2211 * an object is really small, then squeeze multiple objects into
2212 * one cacheline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 */
2214 ralign = cache_line_size();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002215 while (size <= ralign / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 ralign /= 2;
2217 } else {
2218 ralign = BYTES_PER_WORD;
2219 }
Pekka Enbergca5f9702006-09-25 23:31:25 -07002220
2221 /*
2222 * Redzoning and user store require word alignment. Note this will be
2223 * overridden by architecture or caller mandated alignment if either
2224 * is greater than BYTES_PER_WORD.
2225 */
2226 if (flags & SLAB_RED_ZONE || flags & SLAB_STORE_USER)
David Woodhouseb46b8f12007-05-08 00:22:59 -07002227 ralign = __alignof__(unsigned long long);
Pekka Enbergca5f9702006-09-25 23:31:25 -07002228
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002229 /* 2) arch mandated alignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 if (ralign < ARCH_SLAB_MINALIGN) {
2231 ralign = ARCH_SLAB_MINALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 }
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002233 /* 3) caller mandated alignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 if (ralign < align) {
2235 ralign = align;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 }
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002237 /* disable debug if necessary */
David Woodhouseb46b8f12007-05-08 00:22:59 -07002238 if (ralign > __alignof__(unsigned long long))
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002239 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002240 /*
Pekka Enbergca5f9702006-09-25 23:31:25 -07002241 * 4) Store it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 */
2243 align = ralign;
2244
2245 /* Get cache's description obj. */
Christoph Lametere94b1762006-12-06 20:33:17 -08002246 cachep = kmem_cache_zalloc(&cache_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 if (!cachep)
Andrew Morton4f12bb42005-11-07 00:58:00 -08002248 goto oops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
2250#if DEBUG
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002251 cachep->obj_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Pekka Enbergca5f9702006-09-25 23:31:25 -07002253 /*
2254 * Both debugging options require word-alignment which is calculated
2255 * into align above.
2256 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 if (flags & SLAB_RED_ZONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 /* add space for red zone words */
David Woodhouseb46b8f12007-05-08 00:22:59 -07002259 cachep->obj_offset += sizeof(unsigned long long);
2260 size += 2 * sizeof(unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 }
2262 if (flags & SLAB_STORE_USER) {
Pekka Enbergca5f9702006-09-25 23:31:25 -07002263 /* user store requires one word storage behind the end of
2264 * the real object.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 size += BYTES_PER_WORD;
2267 }
2268#if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002269 if (size >= malloc_sizes[INDEX_L3 + 1].cs_size
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002270 && cachep->obj_size > cache_line_size() && size < PAGE_SIZE) {
2271 cachep->obj_offset += PAGE_SIZE - size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 size = PAGE_SIZE;
2273 }
2274#endif
2275#endif
2276
Ingo Molnare0a42722006-06-23 02:03:46 -07002277 /*
2278 * Determine if the slab management is 'on' or 'off' slab.
2279 * (bootstrapping cannot cope with offslab caches so don't do
2280 * it too early on.)
2281 */
2282 if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 /*
2284 * Size is large, assume best to place the slab management obj
2285 * off-slab (should allow better packing of objs).
2286 */
2287 flags |= CFLGS_OFF_SLAB;
2288
2289 size = ALIGN(size, align);
2290
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08002291 left_over = calculate_slab_order(cachep, size, align, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293 if (!cachep->num) {
matzeb4169522007-05-06 14:49:52 -07002294 printk(KERN_ERR
2295 "kmem_cache_create: couldn't create cache %s.\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 kmem_cache_free(&cache_cache, cachep);
2297 cachep = NULL;
Andrew Morton4f12bb42005-11-07 00:58:00 -08002298 goto oops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002300 slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
2301 + sizeof(struct slab), align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
2303 /*
2304 * If the slab has been placed off-slab, and we have enough space then
2305 * move it on-slab. This is at the expense of any extra colouring.
2306 */
2307 if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
2308 flags &= ~CFLGS_OFF_SLAB;
2309 left_over -= slab_size;
2310 }
2311
2312 if (flags & CFLGS_OFF_SLAB) {
2313 /* really off slab. No need for manual alignment */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002314 slab_size =
2315 cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 }
2317
2318 cachep->colour_off = cache_line_size();
2319 /* Offset must be a multiple of the alignment. */
2320 if (cachep->colour_off < align)
2321 cachep->colour_off = align;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002322 cachep->colour = left_over / cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 cachep->slab_size = slab_size;
2324 cachep->flags = flags;
2325 cachep->gfpflags = 0;
Christoph Lameter4b51d662007-02-10 01:43:10 -08002326 if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 cachep->gfpflags |= GFP_DMA;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002328 cachep->buffer_size = size;
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08002329 cachep->reciprocal_buffer_size = reciprocal_value(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002331 if (flags & CFLGS_OFF_SLAB) {
Victor Fuscob2d55072005-09-10 00:26:36 -07002332 cachep->slabp_cache = kmem_find_general_cachep(slab_size, 0u);
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002333 /*
2334 * This is a possibility for one of the malloc_sizes caches.
2335 * But since we go off slab only for object size greater than
2336 * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
2337 * this should not happen at all.
2338 * But leave a BUG_ON for some lucky dude.
2339 */
2340 BUG_ON(!cachep->slabp_cache);
2341 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 cachep->ctor = ctor;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343 cachep->name = name;
2344
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002345 if (setup_cpu_cache(cachep)) {
2346 __kmem_cache_destroy(cachep);
2347 cachep = NULL;
2348 goto oops;
2349 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 /* cache setup completed, link it into the list */
2352 list_add(&cachep->next, &cache_chain);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002353oops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 if (!cachep && (flags & SLAB_PANIC))
2355 panic("kmem_cache_create(): failed to create slab `%s'\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002356 name);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002357 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 return cachep;
2359}
2360EXPORT_SYMBOL(kmem_cache_create);
2361
2362#if DEBUG
2363static void check_irq_off(void)
2364{
2365 BUG_ON(!irqs_disabled());
2366}
2367
2368static void check_irq_on(void)
2369{
2370 BUG_ON(irqs_disabled());
2371}
2372
Pekka Enberg343e0d72006-02-01 03:05:50 -08002373static void check_spinlock_acquired(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
2375#ifdef CONFIG_SMP
2376 check_irq_off();
Christoph Lametere498be72005-09-09 13:03:32 -07002377 assert_spin_locked(&cachep->nodelists[numa_node_id()]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378#endif
2379}
Christoph Lametere498be72005-09-09 13:03:32 -07002380
Pekka Enberg343e0d72006-02-01 03:05:50 -08002381static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07002382{
2383#ifdef CONFIG_SMP
2384 check_irq_off();
2385 assert_spin_locked(&cachep->nodelists[node]->list_lock);
2386#endif
2387}
2388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389#else
2390#define check_irq_off() do { } while(0)
2391#define check_irq_on() do { } while(0)
2392#define check_spinlock_acquired(x) do { } while(0)
Christoph Lametere498be72005-09-09 13:03:32 -07002393#define check_spinlock_acquired_node(x, y) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394#endif
2395
Christoph Lameteraab22072006-03-22 00:09:06 -08002396static void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
2397 struct array_cache *ac,
2398 int force, int node);
2399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400static void do_drain(void *arg)
2401{
Andrew Mortona737b3e2006-03-22 00:08:11 -08002402 struct kmem_cache *cachep = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 struct array_cache *ac;
Christoph Lameterff694162005-09-22 21:44:02 -07002404 int node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
2406 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002407 ac = cpu_cache_get(cachep);
Christoph Lameterff694162005-09-22 21:44:02 -07002408 spin_lock(&cachep->nodelists[node]->list_lock);
2409 free_block(cachep, ac->entry, ac->avail, node);
2410 spin_unlock(&cachep->nodelists[node]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 ac->avail = 0;
2412}
2413
Pekka Enberg343e0d72006-02-01 03:05:50 -08002414static void drain_cpu_caches(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415{
Christoph Lametere498be72005-09-09 13:03:32 -07002416 struct kmem_list3 *l3;
2417 int node;
2418
Andrew Mortona07fa392006-03-22 00:08:17 -08002419 on_each_cpu(do_drain, cachep, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 check_irq_on();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002421 for_each_online_node(node) {
Christoph Lametere498be72005-09-09 13:03:32 -07002422 l3 = cachep->nodelists[node];
Roland Dreiera4523a82006-05-15 11:41:00 -07002423 if (l3 && l3->alien)
2424 drain_alien_cache(cachep, l3->alien);
2425 }
2426
2427 for_each_online_node(node) {
2428 l3 = cachep->nodelists[node];
2429 if (l3)
Christoph Lameteraab22072006-03-22 00:09:06 -08002430 drain_array(cachep, l3, l3->shared, 1, node);
Christoph Lametere498be72005-09-09 13:03:32 -07002431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432}
2433
Christoph Lametered11d9e2006-06-30 01:55:45 -07002434/*
2435 * Remove slabs from the list of free slabs.
2436 * Specify the number of slabs to drain in tofree.
2437 *
2438 * Returns the actual number of slabs released.
2439 */
2440static int drain_freelist(struct kmem_cache *cache,
2441 struct kmem_list3 *l3, int tofree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442{
Christoph Lametered11d9e2006-06-30 01:55:45 -07002443 struct list_head *p;
2444 int nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Christoph Lametered11d9e2006-06-30 01:55:45 -07002447 nr_freed = 0;
2448 while (nr_freed < tofree && !list_empty(&l3->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449
Christoph Lametered11d9e2006-06-30 01:55:45 -07002450 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002451 p = l3->slabs_free.prev;
Christoph Lametered11d9e2006-06-30 01:55:45 -07002452 if (p == &l3->slabs_free) {
2453 spin_unlock_irq(&l3->list_lock);
2454 goto out;
2455 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Christoph Lametered11d9e2006-06-30 01:55:45 -07002457 slabp = list_entry(p, struct slab, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458#if DEBUG
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002459 BUG_ON(slabp->inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460#endif
2461 list_del(&slabp->list);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002462 /*
2463 * Safe to drop the lock. The slab is no longer linked
2464 * to the cache.
2465 */
2466 l3->free_objects -= cache->num;
Christoph Lametere498be72005-09-09 13:03:32 -07002467 spin_unlock_irq(&l3->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002468 slab_destroy(cache, slabp);
2469 nr_freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 }
Christoph Lametered11d9e2006-06-30 01:55:45 -07002471out:
2472 return nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473}
2474
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002475/* Called with cache_chain_mutex held to protect against cpu hotplug */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002476static int __cache_shrink(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07002477{
2478 int ret = 0, i = 0;
2479 struct kmem_list3 *l3;
2480
2481 drain_cpu_caches(cachep);
2482
2483 check_irq_on();
2484 for_each_online_node(i) {
2485 l3 = cachep->nodelists[i];
Christoph Lametered11d9e2006-06-30 01:55:45 -07002486 if (!l3)
2487 continue;
2488
2489 drain_freelist(cachep, l3, l3->free_objects);
2490
2491 ret += !list_empty(&l3->slabs_full) ||
2492 !list_empty(&l3->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07002493 }
2494 return (ret ? 1 : 0);
2495}
2496
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497/**
2498 * kmem_cache_shrink - Shrink a cache.
2499 * @cachep: The cache to shrink.
2500 *
2501 * Releases as many slabs as possible for a cache.
2502 * To help debugging, a zero exit status indicates all slabs were released.
2503 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002504int kmem_cache_shrink(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002506 int ret;
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002507 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002508
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002509 mutex_lock(&cache_chain_mutex);
2510 ret = __cache_shrink(cachep);
2511 mutex_unlock(&cache_chain_mutex);
2512 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513}
2514EXPORT_SYMBOL(kmem_cache_shrink);
2515
2516/**
2517 * kmem_cache_destroy - delete a cache
2518 * @cachep: the cache to destroy
2519 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002520 * Remove a &struct kmem_cache object from the slab cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 *
2522 * It is expected this function will be called by a module when it is
2523 * unloaded. This will remove the cache completely, and avoid a duplicate
2524 * cache being allocated each time a module is loaded and unloaded, if the
2525 * module doesn't have persistent in-kernel storage across loads and unloads.
2526 *
2527 * The cache must be empty before calling this function.
2528 *
2529 * The caller must guarantee that noone will allocate memory from the cache
2530 * during the kmem_cache_destroy().
2531 */
Alexey Dobriyan133d2052006-09-27 01:49:41 -07002532void kmem_cache_destroy(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533{
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002534 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 /* Find the cache in the chain of caches. */
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002537 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 /*
2539 * the chain is never empty, cache_cache is never destroyed
2540 */
2541 list_del(&cachep->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002542 if (__cache_shrink(cachep)) {
2543 slab_error(cachep, "Can't free all objects");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002544 list_add(&cachep->next, &cache_chain);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002545 mutex_unlock(&cache_chain_mutex);
Alexey Dobriyan133d2052006-09-27 01:49:41 -07002546 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002547 }
2548
2549 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU))
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002550 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Christoph Lameter117f6eb2006-09-25 23:31:37 -07002552 __kmem_cache_destroy(cachep);
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002553 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554}
2555EXPORT_SYMBOL(kmem_cache_destroy);
2556
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002557/*
2558 * Get the memory for a slab management obj.
2559 * For a slab cache when the slab descriptor is off-slab, slab descriptors
2560 * always come from malloc_sizes caches. The slab descriptor cannot
2561 * come from the same cache which is getting created because,
2562 * when we are searching for an appropriate cache for these
2563 * descriptors in kmem_cache_create, we search through the malloc_sizes array.
2564 * If we are creating a malloc_sizes cache here it would not be visible to
2565 * kmem_find_general_cachep till the initialization is complete.
2566 * Hence we cannot have slabp_cache same as the original cache.
2567 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002568static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002569 int colour_off, gfp_t local_flags,
2570 int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
2572 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002573
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 if (OFF_SLAB(cachep)) {
2575 /* Slab management obj is off-slab. */
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002576 slabp = kmem_cache_alloc_node(cachep->slabp_cache,
Christoph Lameter3c517a62006-12-06 20:33:29 -08002577 local_flags & ~GFP_THISNODE, nodeid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 if (!slabp)
2579 return NULL;
2580 } else {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002581 slabp = objp + colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 colour_off += cachep->slab_size;
2583 }
2584 slabp->inuse = 0;
2585 slabp->colouroff = colour_off;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002586 slabp->s_mem = objp + colour_off;
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002587 slabp->nodeid = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 return slabp;
2589}
2590
2591static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
2592{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002593 return (kmem_bufctl_t *) (slabp + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594}
2595
Pekka Enberg343e0d72006-02-01 03:05:50 -08002596static void cache_init_objs(struct kmem_cache *cachep,
Christoph Lametera35afb82007-05-16 22:10:57 -07002597 struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598{
2599 int i;
2600
2601 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002602 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603#if DEBUG
2604 /* need to poison the objs? */
2605 if (cachep->flags & SLAB_POISON)
2606 poison_obj(cachep, objp, POISON_FREE);
2607 if (cachep->flags & SLAB_STORE_USER)
2608 *dbg_userword(cachep, objp) = NULL;
2609
2610 if (cachep->flags & SLAB_RED_ZONE) {
2611 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2612 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2613 }
2614 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002615 * Constructors are not allowed to allocate memory from the same
2616 * cache which they are a constructor for. Otherwise, deadlock.
2617 * They must also be threaded.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 */
2619 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002620 cachep->ctor(objp + obj_offset(cachep), cachep,
Christoph Lametera35afb82007-05-16 22:10:57 -07002621 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
2623 if (cachep->flags & SLAB_RED_ZONE) {
2624 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2625 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002626 " end of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2628 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002629 " start of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 }
Andrew Mortona737b3e2006-03-22 00:08:11 -08002631 if ((cachep->buffer_size % PAGE_SIZE) == 0 &&
2632 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002633 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002634 cachep->buffer_size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635#else
2636 if (cachep->ctor)
Christoph Lametera35afb82007-05-16 22:10:57 -07002637 cachep->ctor(objp, cachep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638#endif
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002639 slab_bufctl(slabp)[i] = i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002641 slab_bufctl(slabp)[i - 1] = BUFCTL_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 slabp->free = 0;
2643}
2644
Pekka Enberg343e0d72006-02-01 03:05:50 -08002645static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646{
Christoph Lameter4b51d662007-02-10 01:43:10 -08002647 if (CONFIG_ZONE_DMA_FLAG) {
2648 if (flags & GFP_DMA)
2649 BUG_ON(!(cachep->gfpflags & GFP_DMA));
2650 else
2651 BUG_ON(cachep->gfpflags & GFP_DMA);
2652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653}
2654
Andrew Mortona737b3e2006-03-22 00:08:11 -08002655static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
2656 int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002657{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002658 void *objp = index_to_obj(cachep, slabp, slabp->free);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002659 kmem_bufctl_t next;
2660
2661 slabp->inuse++;
2662 next = slab_bufctl(slabp)[slabp->free];
2663#if DEBUG
2664 slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
2665 WARN_ON(slabp->nodeid != nodeid);
2666#endif
2667 slabp->free = next;
2668
2669 return objp;
2670}
2671
Andrew Mortona737b3e2006-03-22 00:08:11 -08002672static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
2673 void *objp, int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002674{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002675 unsigned int objnr = obj_to_index(cachep, slabp, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002676
2677#if DEBUG
2678 /* Verify that the slab belongs to the intended node */
2679 WARN_ON(slabp->nodeid != nodeid);
2680
Al Viro871751e2006-03-25 03:06:39 -08002681 if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) {
Matthew Dobson78d382d2006-02-01 03:05:47 -08002682 printk(KERN_ERR "slab: double free detected in cache "
Andrew Mortona737b3e2006-03-22 00:08:11 -08002683 "'%s', objp %p\n", cachep->name, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002684 BUG();
2685 }
2686#endif
2687 slab_bufctl(slabp)[objnr] = slabp->free;
2688 slabp->free = objnr;
2689 slabp->inuse--;
2690}
2691
Pekka Enberg47768742006-06-23 02:03:07 -07002692/*
2693 * Map pages beginning at addr to the given cache and slab. This is required
2694 * for the slab allocator to be able to lookup the cache and slab of a
2695 * virtual address for kfree, ksize, kmem_ptr_validate, and slab debugging.
2696 */
2697static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
2698 void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699{
Pekka Enberg47768742006-06-23 02:03:07 -07002700 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 struct page *page;
2702
Pekka Enberg47768742006-06-23 02:03:07 -07002703 page = virt_to_page(addr);
Nick Piggin84097512006-03-22 00:08:34 -08002704
Pekka Enberg47768742006-06-23 02:03:07 -07002705 nr_pages = 1;
Nick Piggin84097512006-03-22 00:08:34 -08002706 if (likely(!PageCompound(page)))
Pekka Enberg47768742006-06-23 02:03:07 -07002707 nr_pages <<= cache->gfporder;
2708
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 do {
Pekka Enberg47768742006-06-23 02:03:07 -07002710 page_set_cache(page, cache);
2711 page_set_slab(page, slab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 page++;
Pekka Enberg47768742006-06-23 02:03:07 -07002713 } while (--nr_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
2715
2716/*
2717 * Grow (by 1) the number of slabs within a cache. This is called by
2718 * kmem_cache_alloc() when there are no active objs left in a cache.
2719 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002720static int cache_grow(struct kmem_cache *cachep,
2721 gfp_t flags, int nodeid, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002723 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002724 size_t offset;
2725 gfp_t local_flags;
Christoph Lametere498be72005-09-09 13:03:32 -07002726 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
Andrew Mortona737b3e2006-03-22 00:08:11 -08002728 /*
2729 * Be lazy and only check for valid flags here, keeping it out of the
2730 * critical path in kmem_cache_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 */
Christoph Lametercfce6602007-05-06 14:50:17 -07002732 BUG_ON(flags & ~(GFP_DMA | GFP_LEVEL_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733
Christoph Lametera06d72c2006-12-06 20:33:12 -08002734 local_flags = (flags & GFP_LEVEL_MASK);
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002735 /* Take the l3 list lock to change the colour_next on this node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 check_irq_off();
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002737 l3 = cachep->nodelists[nodeid];
2738 spin_lock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
2740 /* Get colour for the slab, and cal the next value. */
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002741 offset = l3->colour_next;
2742 l3->colour_next++;
2743 if (l3->colour_next >= cachep->colour)
2744 l3->colour_next = 0;
2745 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002747 offset *= cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748
2749 if (local_flags & __GFP_WAIT)
2750 local_irq_enable();
2751
2752 /*
2753 * The test for missing atomic flag is performed here, rather than
2754 * the more obvious place, simply to reduce the critical path length
2755 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2756 * will eventually be caught here (where it matters).
2757 */
2758 kmem_flagcheck(cachep, flags);
2759
Andrew Mortona737b3e2006-03-22 00:08:11 -08002760 /*
2761 * Get mem for the objs. Attempt to allocate a physical page from
2762 * 'nodeid'.
Christoph Lametere498be72005-09-09 13:03:32 -07002763 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002764 if (!objp)
2765 objp = kmem_getpages(cachep, flags, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002766 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 goto failed;
2768
2769 /* Get slab management. */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002770 slabp = alloc_slabmgmt(cachep, objp, offset,
2771 local_flags & ~GFP_THISNODE, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002772 if (!slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 goto opps1;
2774
Christoph Lametere498be72005-09-09 13:03:32 -07002775 slabp->nodeid = nodeid;
Pekka Enberg47768742006-06-23 02:03:07 -07002776 slab_map_pages(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777
Christoph Lametera35afb82007-05-16 22:10:57 -07002778 cache_init_objs(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
2780 if (local_flags & __GFP_WAIT)
2781 local_irq_disable();
2782 check_irq_off();
Christoph Lametere498be72005-09-09 13:03:32 -07002783 spin_lock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784
2785 /* Make slab active. */
Christoph Lametere498be72005-09-09 13:03:32 -07002786 list_add_tail(&slabp->list, &(l3->slabs_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 STATS_INC_GROWN(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07002788 l3->free_objects += cachep->num;
2789 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002791opps1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 kmem_freepages(cachep, objp);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002793failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 if (local_flags & __GFP_WAIT)
2795 local_irq_disable();
2796 return 0;
2797}
2798
2799#if DEBUG
2800
2801/*
2802 * Perform extra freeing checks:
2803 * - detect bad pointers.
2804 * - POISON/RED_ZONE checking
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 */
2806static void kfree_debugcheck(const void *objp)
2807{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 if (!virt_addr_valid(objp)) {
2809 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002810 (unsigned long)objp);
2811 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813}
2814
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002815static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2816{
David Woodhouseb46b8f12007-05-08 00:22:59 -07002817 unsigned long long redzone1, redzone2;
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002818
2819 redzone1 = *dbg_redzone1(cache, obj);
2820 redzone2 = *dbg_redzone2(cache, obj);
2821
2822 /*
2823 * Redzone is ok.
2824 */
2825 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2826 return;
2827
2828 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2829 slab_error(cache, "double free detected");
2830 else
2831 slab_error(cache, "memory outside object was overwritten");
2832
David Woodhouseb46b8f12007-05-08 00:22:59 -07002833 printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002834 obj, redzone1, redzone2);
2835}
2836
Pekka Enberg343e0d72006-02-01 03:05:50 -08002837static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002838 void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839{
2840 struct page *page;
2841 unsigned int objnr;
2842 struct slab *slabp;
2843
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002844 objp -= obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 kfree_debugcheck(objp);
Christoph Lameterb49af682007-05-06 14:49:41 -07002846 page = virt_to_head_page(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Pekka Enberg065d41c2005-11-13 16:06:46 -08002848 slabp = page_get_slab(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 if (cachep->flags & SLAB_RED_ZONE) {
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002851 verify_redzone_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2853 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2854 }
2855 if (cachep->flags & SLAB_STORE_USER)
2856 *dbg_userword(cachep, objp) = caller;
2857
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002858 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
2860 BUG_ON(objnr >= cachep->num);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002861 BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
Al Viro871751e2006-03-25 03:06:39 -08002863#ifdef CONFIG_DEBUG_SLAB_LEAK
2864 slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
2865#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 if (cachep->flags & SLAB_POISON) {
2867#ifdef CONFIG_DEBUG_PAGEALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -08002868 if ((cachep->buffer_size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 store_stackinfo(cachep, objp, (unsigned long)caller);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002870 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002871 cachep->buffer_size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 } else {
2873 poison_obj(cachep, objp, POISON_FREE);
2874 }
2875#else
2876 poison_obj(cachep, objp, POISON_FREE);
2877#endif
2878 }
2879 return objp;
2880}
2881
Pekka Enberg343e0d72006-02-01 03:05:50 -08002882static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883{
2884 kmem_bufctl_t i;
2885 int entries = 0;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 /* Check slab's freelist to see if this obj is there. */
2888 for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
2889 entries++;
2890 if (entries > cachep->num || i >= cachep->num)
2891 goto bad;
2892 }
2893 if (entries != cachep->num - slabp->inuse) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002894bad:
2895 printk(KERN_ERR "slab: Internal list corruption detected in "
2896 "cache '%s'(%d), slabp %p(%d). Hexdump:\n",
2897 cachep->name, cachep->num, slabp, slabp->inuse);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002898 for (i = 0;
Linus Torvalds264132b2006-03-06 12:10:07 -08002899 i < sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002900 i++) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002901 if (i % 16 == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 printk("\n%03x:", i);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002903 printk(" %02x", ((unsigned char *)slabp)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 }
2905 printk("\n");
2906 BUG();
2907 }
2908}
2909#else
2910#define kfree_debugcheck(x) do { } while(0)
2911#define cache_free_debugcheck(x,objp,z) (objp)
2912#define check_slabp(x,y) do { } while(0)
2913#endif
2914
Pekka Enberg343e0d72006-02-01 03:05:50 -08002915static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916{
2917 int batchcount;
2918 struct kmem_list3 *l3;
2919 struct array_cache *ac;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002920 int node;
2921
2922 node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923
2924 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002925 ac = cpu_cache_get(cachep);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002926retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 batchcount = ac->batchcount;
2928 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002929 /*
2930 * If there was little recent activity on this cache, then
2931 * perform only a partial refill. Otherwise we could generate
2932 * refill bouncing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 */
2934 batchcount = BATCHREFILL_LIMIT;
2935 }
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002936 l3 = cachep->nodelists[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Christoph Lametere498be72005-09-09 13:03:32 -07002938 BUG_ON(ac->avail > 0 || !l3);
2939 spin_lock(&l3->list_lock);
2940
Christoph Lameter3ded1752006-03-25 03:06:44 -08002941 /* See if we can refill from the shared array */
2942 if (l3->shared && transfer_objects(ac, l3->shared, batchcount))
2943 goto alloc_done;
2944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 while (batchcount > 0) {
2946 struct list_head *entry;
2947 struct slab *slabp;
2948 /* Get slab alloc is to come from. */
2949 entry = l3->slabs_partial.next;
2950 if (entry == &l3->slabs_partial) {
2951 l3->free_touched = 1;
2952 entry = l3->slabs_free.next;
2953 if (entry == &l3->slabs_free)
2954 goto must_grow;
2955 }
2956
2957 slabp = list_entry(entry, struct slab, list);
2958 check_slabp(cachep, slabp);
2959 check_spinlock_acquired(cachep);
Pekka Enberg714b8172007-05-06 14:49:03 -07002960
2961 /*
2962 * The slab was either on partial or free list so
2963 * there must be at least one object available for
2964 * allocation.
2965 */
2966 BUG_ON(slabp->inuse < 0 || slabp->inuse >= cachep->num);
2967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 while (slabp->inuse < cachep->num && batchcount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 STATS_INC_ALLOCED(cachep);
2970 STATS_INC_ACTIVE(cachep);
2971 STATS_SET_HIGH(cachep);
2972
Matthew Dobson78d382d2006-02-01 03:05:47 -08002973 ac->entry[ac->avail++] = slab_get_obj(cachep, slabp,
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002974 node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 }
2976 check_slabp(cachep, slabp);
2977
2978 /* move slabp to correct slabp list: */
2979 list_del(&slabp->list);
2980 if (slabp->free == BUFCTL_END)
2981 list_add(&slabp->list, &l3->slabs_full);
2982 else
2983 list_add(&slabp->list, &l3->slabs_partial);
2984 }
2985
Andrew Mortona737b3e2006-03-22 00:08:11 -08002986must_grow:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 l3->free_objects -= ac->avail;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002988alloc_done:
Christoph Lametere498be72005-09-09 13:03:32 -07002989 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990
2991 if (unlikely(!ac->avail)) {
2992 int x;
Christoph Lameter3c517a62006-12-06 20:33:29 -08002993 x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07002994
Andrew Mortona737b3e2006-03-22 00:08:11 -08002995 /* cache_grow can reenable interrupts, then ac could change. */
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002996 ac = cpu_cache_get(cachep);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002997 if (!x && ac->avail == 0) /* no objects in sight? abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 return NULL;
2999
Andrew Mortona737b3e2006-03-22 00:08:11 -08003000 if (!ac->avail) /* objects refilled by interrupt? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 goto retry;
3002 }
3003 ac->touched = 1;
Christoph Lametere498be72005-09-09 13:03:32 -07003004 return ac->entry[--ac->avail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005}
3006
Andrew Mortona737b3e2006-03-22 00:08:11 -08003007static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
3008 gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009{
3010 might_sleep_if(flags & __GFP_WAIT);
3011#if DEBUG
3012 kmem_flagcheck(cachep, flags);
3013#endif
3014}
3015
3016#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003017static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
3018 gfp_t flags, void *objp, void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003020 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 return objp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003022 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023#ifdef CONFIG_DEBUG_PAGEALLOC
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003024 if ((cachep->buffer_size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003025 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003026 cachep->buffer_size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 else
3028 check_poison_obj(cachep, objp);
3029#else
3030 check_poison_obj(cachep, objp);
3031#endif
3032 poison_obj(cachep, objp, POISON_INUSE);
3033 }
3034 if (cachep->flags & SLAB_STORE_USER)
3035 *dbg_userword(cachep, objp) = caller;
3036
3037 if (cachep->flags & SLAB_RED_ZONE) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003038 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
3039 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
3040 slab_error(cachep, "double free, or memory outside"
3041 " object was overwritten");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003042 printk(KERN_ERR
David Woodhouseb46b8f12007-05-08 00:22:59 -07003043 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08003044 objp, *dbg_redzone1(cachep, objp),
3045 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 }
3047 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
3048 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
3049 }
Al Viro871751e2006-03-25 03:06:39 -08003050#ifdef CONFIG_DEBUG_SLAB_LEAK
3051 {
3052 struct slab *slabp;
3053 unsigned objnr;
3054
Christoph Lameterb49af682007-05-06 14:49:41 -07003055 slabp = page_get_slab(virt_to_head_page(objp));
Al Viro871751e2006-03-25 03:06:39 -08003056 objnr = (unsigned)(objp - slabp->s_mem) / cachep->buffer_size;
3057 slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE;
3058 }
3059#endif
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003060 objp += obj_offset(cachep);
Christoph Lameter4f104932007-05-06 14:50:17 -07003061 if (cachep->ctor && cachep->flags & SLAB_POISON)
Christoph Lametera35afb82007-05-16 22:10:57 -07003062 cachep->ctor(objp, cachep, 0);
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003063#if ARCH_SLAB_MINALIGN
3064 if ((u32)objp & (ARCH_SLAB_MINALIGN-1)) {
3065 printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
3066 objp, ARCH_SLAB_MINALIGN);
3067 }
3068#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 return objp;
3070}
3071#else
3072#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
3073#endif
3074
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003075#ifdef CONFIG_FAILSLAB
3076
3077static struct failslab_attr {
3078
3079 struct fault_attr attr;
3080
3081 u32 ignore_gfp_wait;
3082#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
3083 struct dentry *ignore_gfp_wait_file;
3084#endif
3085
3086} failslab = {
3087 .attr = FAULT_ATTR_INITIALIZER,
Don Mullis6b1b60f2006-12-08 02:39:53 -08003088 .ignore_gfp_wait = 1,
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003089};
3090
3091static int __init setup_failslab(char *str)
3092{
3093 return setup_fault_attr(&failslab.attr, str);
3094}
3095__setup("failslab=", setup_failslab);
3096
3097static int should_failslab(struct kmem_cache *cachep, gfp_t flags)
3098{
3099 if (cachep == &cache_cache)
3100 return 0;
3101 if (flags & __GFP_NOFAIL)
3102 return 0;
3103 if (failslab.ignore_gfp_wait && (flags & __GFP_WAIT))
3104 return 0;
3105
3106 return should_fail(&failslab.attr, obj_size(cachep));
3107}
3108
3109#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
3110
3111static int __init failslab_debugfs(void)
3112{
3113 mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
3114 struct dentry *dir;
3115 int err;
3116
Akinobu Mita824ebef2007-05-06 14:49:58 -07003117 err = init_fault_attr_dentries(&failslab.attr, "failslab");
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003118 if (err)
3119 return err;
3120 dir = failslab.attr.dentries.dir;
3121
3122 failslab.ignore_gfp_wait_file =
3123 debugfs_create_bool("ignore-gfp-wait", mode, dir,
3124 &failslab.ignore_gfp_wait);
3125
3126 if (!failslab.ignore_gfp_wait_file) {
3127 err = -ENOMEM;
3128 debugfs_remove(failslab.ignore_gfp_wait_file);
3129 cleanup_fault_attr_dentries(&failslab.attr);
3130 }
3131
3132 return err;
3133}
3134
3135late_initcall(failslab_debugfs);
3136
3137#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
3138
3139#else /* CONFIG_FAILSLAB */
3140
3141static inline int should_failslab(struct kmem_cache *cachep, gfp_t flags)
3142{
3143 return 0;
3144}
3145
3146#endif /* CONFIG_FAILSLAB */
3147
Pekka Enberg343e0d72006-02-01 03:05:50 -08003148static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003150 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 struct array_cache *ac;
3152
Alok N Kataria5c382302005-09-27 21:45:46 -07003153 check_irq_off();
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003154
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003155 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 if (likely(ac->avail)) {
3157 STATS_INC_ALLOCHIT(cachep);
3158 ac->touched = 1;
Christoph Lametere498be72005-09-09 13:03:32 -07003159 objp = ac->entry[--ac->avail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 } else {
3161 STATS_INC_ALLOCMISS(cachep);
3162 objp = cache_alloc_refill(cachep, flags);
3163 }
Alok N Kataria5c382302005-09-27 21:45:46 -07003164 return objp;
3165}
3166
Christoph Lametere498be72005-09-09 13:03:32 -07003167#ifdef CONFIG_NUMA
3168/*
Paul Jacksonb2455392006-03-24 03:16:12 -08003169 * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
Paul Jacksonc61afb12006-03-24 03:16:08 -08003170 *
3171 * If we are in_interrupt, then process context, including cpusets and
3172 * mempolicy, may not apply and should not be used for allocation policy.
3173 */
3174static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
3175{
3176 int nid_alloc, nid_here;
3177
Christoph Lameter765c4502006-09-27 01:50:08 -07003178 if (in_interrupt() || (flags & __GFP_THISNODE))
Paul Jacksonc61afb12006-03-24 03:16:08 -08003179 return NULL;
3180 nid_alloc = nid_here = numa_node_id();
3181 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
3182 nid_alloc = cpuset_mem_spread_node();
3183 else if (current->mempolicy)
3184 nid_alloc = slab_node(current->mempolicy);
3185 if (nid_alloc != nid_here)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003186 return ____cache_alloc_node(cachep, flags, nid_alloc);
Paul Jacksonc61afb12006-03-24 03:16:08 -08003187 return NULL;
3188}
3189
3190/*
Christoph Lameter765c4502006-09-27 01:50:08 -07003191 * Fallback function if there was no memory available and no objects on a
Christoph Lameter3c517a62006-12-06 20:33:29 -08003192 * certain node and fall back is permitted. First we scan all the
3193 * available nodelists for available objects. If that fails then we
3194 * perform an allocation without specifying a node. This allows the page
3195 * allocator to do its reclaim / fallback magic. We then insert the
3196 * slab into the proper nodelist and then allocate from it.
Christoph Lameter765c4502006-09-27 01:50:08 -07003197 */
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003198static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
Christoph Lameter765c4502006-09-27 01:50:08 -07003199{
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003200 struct zonelist *zonelist;
3201 gfp_t local_flags;
Christoph Lameter765c4502006-09-27 01:50:08 -07003202 struct zone **z;
3203 void *obj = NULL;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003204 int nid;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003205
3206 if (flags & __GFP_THISNODE)
3207 return NULL;
3208
3209 zonelist = &NODE_DATA(slab_node(current->mempolicy))
3210 ->node_zonelists[gfp_zone(flags)];
3211 local_flags = (flags & GFP_LEVEL_MASK);
Christoph Lameter765c4502006-09-27 01:50:08 -07003212
Christoph Lameter3c517a62006-12-06 20:33:29 -08003213retry:
3214 /*
3215 * Look through allowed nodes for objects available
3216 * from existing per node queues.
3217 */
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003218 for (z = zonelist->zones; *z && !obj; z++) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003219 nid = zone_to_nid(*z);
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003220
Paul Jackson02a0e532006-12-13 00:34:25 -08003221 if (cpuset_zone_allowed_hardwall(*z, flags) &&
Christoph Lameter3c517a62006-12-06 20:33:29 -08003222 cache->nodelists[nid] &&
3223 cache->nodelists[nid]->free_objects)
3224 obj = ____cache_alloc_node(cache,
3225 flags | GFP_THISNODE, nid);
3226 }
3227
Christoph Lametercfce6602007-05-06 14:50:17 -07003228 if (!obj) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003229 /*
3230 * This allocation will be performed within the constraints
3231 * of the current cpuset / memory policy requirements.
3232 * We may trigger various forms of reclaim on the allowed
3233 * set and go into memory reserves if necessary.
3234 */
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003235 if (local_flags & __GFP_WAIT)
3236 local_irq_enable();
3237 kmem_flagcheck(cache, flags);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003238 obj = kmem_getpages(cache, flags, -1);
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003239 if (local_flags & __GFP_WAIT)
3240 local_irq_disable();
Christoph Lameter3c517a62006-12-06 20:33:29 -08003241 if (obj) {
3242 /*
3243 * Insert into the appropriate per node queues
3244 */
3245 nid = page_to_nid(virt_to_page(obj));
3246 if (cache_grow(cache, flags, nid, obj)) {
3247 obj = ____cache_alloc_node(cache,
3248 flags | GFP_THISNODE, nid);
3249 if (!obj)
3250 /*
3251 * Another processor may allocate the
3252 * objects in the slab since we are
3253 * not holding any locks.
3254 */
3255 goto retry;
3256 } else {
Hugh Dickinsb6a60452007-01-05 16:36:36 -08003257 /* cache_grow already freed obj */
Christoph Lameter3c517a62006-12-06 20:33:29 -08003258 obj = NULL;
3259 }
3260 }
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003261 }
Christoph Lameter765c4502006-09-27 01:50:08 -07003262 return obj;
3263}
3264
3265/*
Christoph Lametere498be72005-09-09 13:03:32 -07003266 * A interface to enable slab creation on nodeid
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003268static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003269 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07003270{
3271 struct list_head *entry;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003272 struct slab *slabp;
3273 struct kmem_list3 *l3;
3274 void *obj;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003275 int x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003277 l3 = cachep->nodelists[nodeid];
3278 BUG_ON(!l3);
Christoph Lametere498be72005-09-09 13:03:32 -07003279
Andrew Mortona737b3e2006-03-22 00:08:11 -08003280retry:
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003281 check_irq_off();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003282 spin_lock(&l3->list_lock);
3283 entry = l3->slabs_partial.next;
3284 if (entry == &l3->slabs_partial) {
3285 l3->free_touched = 1;
3286 entry = l3->slabs_free.next;
3287 if (entry == &l3->slabs_free)
3288 goto must_grow;
3289 }
Christoph Lametere498be72005-09-09 13:03:32 -07003290
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003291 slabp = list_entry(entry, struct slab, list);
3292 check_spinlock_acquired_node(cachep, nodeid);
3293 check_slabp(cachep, slabp);
Christoph Lametere498be72005-09-09 13:03:32 -07003294
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003295 STATS_INC_NODEALLOCS(cachep);
3296 STATS_INC_ACTIVE(cachep);
3297 STATS_SET_HIGH(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003298
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003299 BUG_ON(slabp->inuse == cachep->num);
Christoph Lametere498be72005-09-09 13:03:32 -07003300
Matthew Dobson78d382d2006-02-01 03:05:47 -08003301 obj = slab_get_obj(cachep, slabp, nodeid);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003302 check_slabp(cachep, slabp);
3303 l3->free_objects--;
3304 /* move slabp to correct slabp list: */
3305 list_del(&slabp->list);
Christoph Lametere498be72005-09-09 13:03:32 -07003306
Andrew Mortona737b3e2006-03-22 00:08:11 -08003307 if (slabp->free == BUFCTL_END)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003308 list_add(&slabp->list, &l3->slabs_full);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003309 else
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003310 list_add(&slabp->list, &l3->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07003311
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003312 spin_unlock(&l3->list_lock);
3313 goto done;
Christoph Lametere498be72005-09-09 13:03:32 -07003314
Andrew Mortona737b3e2006-03-22 00:08:11 -08003315must_grow:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003316 spin_unlock(&l3->list_lock);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003317 x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
Christoph Lameter765c4502006-09-27 01:50:08 -07003318 if (x)
3319 goto retry;
Christoph Lametere498be72005-09-09 13:03:32 -07003320
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003321 return fallback_alloc(cachep, flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003322
Andrew Mortona737b3e2006-03-22 00:08:11 -08003323done:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003324 return obj;
Christoph Lametere498be72005-09-09 13:03:32 -07003325}
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003326
3327/**
3328 * kmem_cache_alloc_node - Allocate an object on the specified node
3329 * @cachep: The cache to allocate from.
3330 * @flags: See kmalloc().
3331 * @nodeid: node number of the target node.
3332 * @caller: return address of caller, used for debug information
3333 *
3334 * Identical to kmem_cache_alloc but it will allocate memory on the given
3335 * node, which can improve the performance for cpu bound structures.
3336 *
3337 * Fallback to other node is possible if __GFP_THISNODE is not set.
3338 */
3339static __always_inline void *
3340__cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
3341 void *caller)
3342{
3343 unsigned long save_flags;
3344 void *ptr;
3345
Akinobu Mita824ebef2007-05-06 14:49:58 -07003346 if (should_failslab(cachep, flags))
3347 return NULL;
3348
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003349 cache_alloc_debugcheck_before(cachep, flags);
3350 local_irq_save(save_flags);
3351
3352 if (unlikely(nodeid == -1))
3353 nodeid = numa_node_id();
3354
3355 if (unlikely(!cachep->nodelists[nodeid])) {
3356 /* Node not bootstrapped yet */
3357 ptr = fallback_alloc(cachep, flags);
3358 goto out;
3359 }
3360
3361 if (nodeid == numa_node_id()) {
3362 /*
3363 * Use the locally cached objects if possible.
3364 * However ____cache_alloc does not allow fallback
3365 * to other nodes. It may fail while we still have
3366 * objects on other nodes available.
3367 */
3368 ptr = ____cache_alloc(cachep, flags);
3369 if (ptr)
3370 goto out;
3371 }
3372 /* ___cache_alloc_node can fall back to other nodes */
3373 ptr = ____cache_alloc_node(cachep, flags, nodeid);
3374 out:
3375 local_irq_restore(save_flags);
3376 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
3377
3378 return ptr;
3379}
3380
3381static __always_inline void *
3382__do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
3383{
3384 void *objp;
3385
3386 if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
3387 objp = alternate_node_alloc(cache, flags);
3388 if (objp)
3389 goto out;
3390 }
3391 objp = ____cache_alloc(cache, flags);
3392
3393 /*
3394 * We may just have run out of memory on the local node.
3395 * ____cache_alloc_node() knows how to locate memory on other nodes
3396 */
3397 if (!objp)
3398 objp = ____cache_alloc_node(cache, flags, numa_node_id());
3399
3400 out:
3401 return objp;
3402}
3403#else
3404
3405static __always_inline void *
3406__do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
3407{
3408 return ____cache_alloc(cachep, flags);
3409}
3410
3411#endif /* CONFIG_NUMA */
3412
3413static __always_inline void *
3414__cache_alloc(struct kmem_cache *cachep, gfp_t flags, void *caller)
3415{
3416 unsigned long save_flags;
3417 void *objp;
3418
Akinobu Mita824ebef2007-05-06 14:49:58 -07003419 if (should_failslab(cachep, flags))
3420 return NULL;
3421
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003422 cache_alloc_debugcheck_before(cachep, flags);
3423 local_irq_save(save_flags);
3424 objp = __do_cache_alloc(cachep, flags);
3425 local_irq_restore(save_flags);
3426 objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
3427 prefetchw(objp);
3428
3429 return objp;
3430}
Christoph Lametere498be72005-09-09 13:03:32 -07003431
3432/*
3433 * Caller needs to acquire correct kmem_list's list_lock
3434 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003435static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003436 int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437{
3438 int i;
Christoph Lametere498be72005-09-09 13:03:32 -07003439 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
3441 for (i = 0; i < nr_objects; i++) {
3442 void *objp = objpp[i];
3443 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Pekka Enberg6ed5eb22006-02-01 03:05:49 -08003445 slabp = virt_to_slab(objp);
Christoph Lameterff694162005-09-22 21:44:02 -07003446 l3 = cachep->nodelists[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447 list_del(&slabp->list);
Christoph Lameterff694162005-09-22 21:44:02 -07003448 check_spinlock_acquired_node(cachep, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449 check_slabp(cachep, slabp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08003450 slab_put_obj(cachep, slabp, objp, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 STATS_DEC_ACTIVE(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003452 l3->free_objects++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 check_slabp(cachep, slabp);
3454
3455 /* fixup slab chains */
3456 if (slabp->inuse == 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07003457 if (l3->free_objects > l3->free_limit) {
3458 l3->free_objects -= cachep->num;
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07003459 /* No need to drop any previously held
3460 * lock here, even if we have a off-slab slab
3461 * descriptor it is guaranteed to come from
3462 * a different cache, refer to comments before
3463 * alloc_slabmgmt.
3464 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 slab_destroy(cachep, slabp);
3466 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07003467 list_add(&slabp->list, &l3->slabs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 }
3469 } else {
3470 /* Unconditionally move a slab to the end of the
3471 * partial list on free - maximum time for the
3472 * other objects to be freed, too.
3473 */
Christoph Lametere498be72005-09-09 13:03:32 -07003474 list_add_tail(&slabp->list, &l3->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 }
3476 }
3477}
3478
Pekka Enberg343e0d72006-02-01 03:05:50 -08003479static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480{
3481 int batchcount;
Christoph Lametere498be72005-09-09 13:03:32 -07003482 struct kmem_list3 *l3;
Christoph Lameterff694162005-09-22 21:44:02 -07003483 int node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
3485 batchcount = ac->batchcount;
3486#if DEBUG
3487 BUG_ON(!batchcount || batchcount > ac->avail);
3488#endif
3489 check_irq_off();
Christoph Lameterff694162005-09-22 21:44:02 -07003490 l3 = cachep->nodelists[node];
Ingo Molnar873623d2006-07-13 14:44:38 +02003491 spin_lock(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07003492 if (l3->shared) {
3493 struct array_cache *shared_array = l3->shared;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003494 int max = shared_array->limit - shared_array->avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 if (max) {
3496 if (batchcount > max)
3497 batchcount = max;
Christoph Lametere498be72005-09-09 13:03:32 -07003498 memcpy(&(shared_array->entry[shared_array->avail]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003499 ac->entry, sizeof(void *) * batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 shared_array->avail += batchcount;
3501 goto free_done;
3502 }
3503 }
3504
Christoph Lameterff694162005-09-22 21:44:02 -07003505 free_block(cachep, ac->entry, batchcount, node);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003506free_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507#if STATS
3508 {
3509 int i = 0;
3510 struct list_head *p;
3511
Christoph Lametere498be72005-09-09 13:03:32 -07003512 p = l3->slabs_free.next;
3513 while (p != &(l3->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 struct slab *slabp;
3515
3516 slabp = list_entry(p, struct slab, list);
3517 BUG_ON(slabp->inuse);
3518
3519 i++;
3520 p = p->next;
3521 }
3522 STATS_SET_FREEABLE(cachep, i);
3523 }
3524#endif
Christoph Lametere498be72005-09-09 13:03:32 -07003525 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526 ac->avail -= batchcount;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003527 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528}
3529
3530/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08003531 * Release an obj back to its cache. If the obj has a constructed state, it must
3532 * be in this state _before_ it is released. Called with disabled ints.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 */
Ingo Molnar873623d2006-07-13 14:44:38 +02003534static inline void __cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535{
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003536 struct array_cache *ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537
3538 check_irq_off();
3539 objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));
3540
Christoph Lameter3cdc0ed2007-06-08 13:46:46 -07003541 if (cache_free_alien(cachep, objp))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07003542 return;
Christoph Lametere498be72005-09-09 13:03:32 -07003543
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 if (likely(ac->avail < ac->limit)) {
3545 STATS_INC_FREEHIT(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003546 ac->entry[ac->avail++] = objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 return;
3548 } else {
3549 STATS_INC_FREEMISS(cachep);
3550 cache_flusharray(cachep, ac);
Christoph Lametere498be72005-09-09 13:03:32 -07003551 ac->entry[ac->avail++] = objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 }
3553}
3554
3555/**
3556 * kmem_cache_alloc - Allocate an object
3557 * @cachep: The cache to allocate from.
3558 * @flags: See kmalloc().
3559 *
3560 * Allocate an object from this cache. The flags are only relevant
3561 * if the cache has no available objects.
3562 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003563void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564{
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003565 return __cache_alloc(cachep, flags, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566}
3567EXPORT_SYMBOL(kmem_cache_alloc);
3568
3569/**
Rolf Eike Beerb8008b22006-07-30 03:04:04 -07003570 * kmem_cache_zalloc - Allocate an object. The memory is set to zero.
Pekka Enberga8c0f9a2006-03-25 03:06:42 -08003571 * @cache: The cache to allocate from.
3572 * @flags: See kmalloc().
3573 *
3574 * Allocate an object from this cache and set the allocated memory to zero.
3575 * The flags are only relevant if the cache has no available objects.
3576 */
3577void *kmem_cache_zalloc(struct kmem_cache *cache, gfp_t flags)
3578{
3579 void *ret = __cache_alloc(cache, flags, __builtin_return_address(0));
3580 if (ret)
3581 memset(ret, 0, obj_size(cache));
3582 return ret;
3583}
3584EXPORT_SYMBOL(kmem_cache_zalloc);
3585
3586/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 * kmem_ptr_validate - check if an untrusted pointer might
3588 * be a slab entry.
3589 * @cachep: the cache we're checking against
3590 * @ptr: pointer to validate
3591 *
3592 * This verifies that the untrusted pointer looks sane:
3593 * it is _not_ a guarantee that the pointer is actually
3594 * part of the slab cache in question, but it at least
3595 * validates that the pointer can be dereferenced and
3596 * looks half-way sane.
3597 *
3598 * Currently only used for dentry validation.
3599 */
Christoph Lameterb7f869a2006-12-22 01:06:44 -08003600int kmem_ptr_validate(struct kmem_cache *cachep, const void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003602 unsigned long addr = (unsigned long)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 unsigned long min_addr = PAGE_OFFSET;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003604 unsigned long align_mask = BYTES_PER_WORD - 1;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003605 unsigned long size = cachep->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 struct page *page;
3607
3608 if (unlikely(addr < min_addr))
3609 goto out;
3610 if (unlikely(addr > (unsigned long)high_memory - size))
3611 goto out;
3612 if (unlikely(addr & align_mask))
3613 goto out;
3614 if (unlikely(!kern_addr_valid(addr)))
3615 goto out;
3616 if (unlikely(!kern_addr_valid(addr + size - 1)))
3617 goto out;
3618 page = virt_to_page(ptr);
3619 if (unlikely(!PageSlab(page)))
3620 goto out;
Pekka Enberg065d41c2005-11-13 16:06:46 -08003621 if (unlikely(page_get_cache(page) != cachep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 goto out;
3623 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003624out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 return 0;
3626}
3627
3628#ifdef CONFIG_NUMA
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003629void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
3630{
3631 return __cache_alloc_node(cachep, flags, nodeid,
3632 __builtin_return_address(0));
3633}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634EXPORT_SYMBOL(kmem_cache_alloc_node);
3635
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003636static __always_inline void *
3637__do_kmalloc_node(size_t size, gfp_t flags, int node, void *caller)
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003638{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003639 struct kmem_cache *cachep;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003640
3641 cachep = kmem_find_general_cachep(size, flags);
3642 if (unlikely(cachep == NULL))
3643 return NULL;
3644 return kmem_cache_alloc_node(cachep, flags, node);
3645}
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003646
3647#ifdef CONFIG_DEBUG_SLAB
3648void *__kmalloc_node(size_t size, gfp_t flags, int node)
3649{
3650 return __do_kmalloc_node(size, flags, node,
3651 __builtin_return_address(0));
3652}
Christoph Hellwigdbe5e692006-09-25 23:31:36 -07003653EXPORT_SYMBOL(__kmalloc_node);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003654
3655void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
3656 int node, void *caller)
3657{
3658 return __do_kmalloc_node(size, flags, node, caller);
3659}
3660EXPORT_SYMBOL(__kmalloc_node_track_caller);
3661#else
3662void *__kmalloc_node(size_t size, gfp_t flags, int node)
3663{
3664 return __do_kmalloc_node(size, flags, node, NULL);
3665}
3666EXPORT_SYMBOL(__kmalloc_node);
3667#endif /* CONFIG_DEBUG_SLAB */
3668#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003669
3670/**
Paul Drynoff800590f2006-06-23 02:03:48 -07003671 * __do_kmalloc - allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672 * @size: how many bytes of memory are required.
Paul Drynoff800590f2006-06-23 02:03:48 -07003673 * @flags: the type of memory to allocate (see kmalloc).
Randy Dunlap911851e2006-03-22 00:08:14 -08003674 * @caller: function caller for debug tracking of the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 */
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003676static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
3677 void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003679 struct kmem_cache *cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003681 /* If you want to save a few bytes .text space: replace
3682 * __ with kmem_.
3683 * Then kmalloc uses the uninlined functions instead of the inline
3684 * functions.
3685 */
3686 cachep = __find_general_cachep(size, flags);
Andrew Mortondbdb9042005-09-23 13:24:10 -07003687 if (unlikely(cachep == NULL))
3688 return NULL;
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003689 return __cache_alloc(cachep, flags, caller);
3690}
3691
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003692
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003693#ifdef CONFIG_DEBUG_SLAB
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003694void *__kmalloc(size_t size, gfp_t flags)
3695{
Al Viro871751e2006-03-25 03:06:39 -08003696 return __do_kmalloc(size, flags, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697}
3698EXPORT_SYMBOL(__kmalloc);
3699
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003700void *__kmalloc_track_caller(size_t size, gfp_t flags, void *caller)
3701{
3702 return __do_kmalloc(size, flags, caller);
3703}
3704EXPORT_SYMBOL(__kmalloc_track_caller);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003705
3706#else
3707void *__kmalloc(size_t size, gfp_t flags)
3708{
3709 return __do_kmalloc(size, flags, NULL);
3710}
3711EXPORT_SYMBOL(__kmalloc);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003712#endif
3713
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714/**
Pekka Enbergfd76bab2007-05-06 14:48:40 -07003715 * krealloc - reallocate memory. The contents will remain unchanged.
Pekka Enbergfd76bab2007-05-06 14:48:40 -07003716 * @p: object to reallocate memory for.
3717 * @new_size: how many bytes of memory are required.
3718 * @flags: the type of memory to allocate.
3719 *
3720 * The contents of the object pointed to are preserved up to the
3721 * lesser of the new and old sizes. If @p is %NULL, krealloc()
3722 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
3723 * %NULL pointer, the object pointed to is freed.
3724 */
3725void *krealloc(const void *p, size_t new_size, gfp_t flags)
3726{
3727 struct kmem_cache *cache, *new_cache;
3728 void *ret;
3729
3730 if (unlikely(!p))
3731 return kmalloc_track_caller(new_size, flags);
3732
3733 if (unlikely(!new_size)) {
3734 kfree(p);
3735 return NULL;
3736 }
3737
3738 cache = virt_to_cache(p);
3739 new_cache = __find_general_cachep(new_size, flags);
3740
3741 /*
3742 * If new size fits in the current cache, bail out.
3743 */
3744 if (likely(cache == new_cache))
3745 return (void *)p;
3746
3747 /*
3748 * We are on the slow-path here so do not use __cache_alloc
3749 * because it bloats kernel text.
3750 */
3751 ret = kmalloc_track_caller(new_size, flags);
3752 if (ret) {
3753 memcpy(ret, p, min(new_size, ksize(p)));
3754 kfree(p);
3755 }
3756 return ret;
3757}
3758EXPORT_SYMBOL(krealloc);
3759
3760/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 * kmem_cache_free - Deallocate an object
3762 * @cachep: The cache the allocation was from.
3763 * @objp: The previously allocated object.
3764 *
3765 * Free an object which was previously allocated from this
3766 * cache.
3767 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003768void kmem_cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769{
3770 unsigned long flags;
3771
Pekka Enbergddc2e812006-06-23 02:03:40 -07003772 BUG_ON(virt_to_cache(objp) != cachep);
3773
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 local_irq_save(flags);
Ingo Molnar898552c2007-02-10 01:44:57 -08003775 debug_check_no_locks_freed(objp, obj_size(cachep));
Ingo Molnar873623d2006-07-13 14:44:38 +02003776 __cache_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777 local_irq_restore(flags);
3778}
3779EXPORT_SYMBOL(kmem_cache_free);
3780
3781/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 * kfree - free previously allocated memory
3783 * @objp: pointer returned by kmalloc.
3784 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07003785 * If @objp is NULL, no operation is performed.
3786 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 * Don't free memory not originally allocated by kmalloc()
3788 * or you will run into trouble.
3789 */
3790void kfree(const void *objp)
3791{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003792 struct kmem_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 unsigned long flags;
3794
3795 if (unlikely(!objp))
3796 return;
3797 local_irq_save(flags);
3798 kfree_debugcheck(objp);
Pekka Enberg6ed5eb22006-02-01 03:05:49 -08003799 c = virt_to_cache(objp);
Ingo Molnarf9b84042006-06-27 02:54:49 -07003800 debug_check_no_locks_freed(objp, obj_size(c));
Ingo Molnar873623d2006-07-13 14:44:38 +02003801 __cache_free(c, (void *)objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 local_irq_restore(flags);
3803}
3804EXPORT_SYMBOL(kfree);
3805
Pekka Enberg343e0d72006-02-01 03:05:50 -08003806unsigned int kmem_cache_size(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003808 return obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809}
3810EXPORT_SYMBOL(kmem_cache_size);
3811
Pekka Enberg343e0d72006-02-01 03:05:50 -08003812const char *kmem_cache_name(struct kmem_cache *cachep)
Arnaldo Carvalho de Melo19449722005-06-18 22:46:19 -07003813{
3814 return cachep->name;
3815}
3816EXPORT_SYMBOL_GPL(kmem_cache_name);
3817
Christoph Lametere498be72005-09-09 13:03:32 -07003818/*
Christoph Lameter0718dc22006-03-25 03:06:47 -08003819 * This initializes kmem_list3 or resizes varioius caches for all nodes.
Christoph Lametere498be72005-09-09 13:03:32 -07003820 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003821static int alloc_kmemlist(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07003822{
3823 int node;
3824 struct kmem_list3 *l3;
Christoph Lametercafeb022006-03-25 03:06:46 -08003825 struct array_cache *new_shared;
Paul Menage3395ee02006-12-06 20:32:16 -08003826 struct array_cache **new_alien = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003827
3828 for_each_online_node(node) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003829
Paul Menage3395ee02006-12-06 20:32:16 -08003830 if (use_alien_caches) {
3831 new_alien = alloc_alien_cache(node, cachep->limit);
3832 if (!new_alien)
3833 goto fail;
3834 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003835
Eric Dumazet63109842007-05-06 14:49:28 -07003836 new_shared = NULL;
3837 if (cachep->shared) {
3838 new_shared = alloc_arraycache(node,
Christoph Lameter0718dc22006-03-25 03:06:47 -08003839 cachep->shared*cachep->batchcount,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003840 0xbaadf00d);
Eric Dumazet63109842007-05-06 14:49:28 -07003841 if (!new_shared) {
3842 free_alien_cache(new_alien);
3843 goto fail;
3844 }
Christoph Lameter0718dc22006-03-25 03:06:47 -08003845 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003846
Andrew Mortona737b3e2006-03-22 00:08:11 -08003847 l3 = cachep->nodelists[node];
3848 if (l3) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003849 struct array_cache *shared = l3->shared;
3850
Christoph Lametere498be72005-09-09 13:03:32 -07003851 spin_lock_irq(&l3->list_lock);
3852
Christoph Lametercafeb022006-03-25 03:06:46 -08003853 if (shared)
Christoph Lameter0718dc22006-03-25 03:06:47 -08003854 free_block(cachep, shared->entry,
3855 shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07003856
Christoph Lametercafeb022006-03-25 03:06:46 -08003857 l3->shared = new_shared;
3858 if (!l3->alien) {
Christoph Lametere498be72005-09-09 13:03:32 -07003859 l3->alien = new_alien;
3860 new_alien = NULL;
3861 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003862 l3->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003863 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07003864 spin_unlock_irq(&l3->list_lock);
Christoph Lametercafeb022006-03-25 03:06:46 -08003865 kfree(shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003866 free_alien_cache(new_alien);
3867 continue;
3868 }
Andrew Mortona737b3e2006-03-22 00:08:11 -08003869 l3 = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, node);
Christoph Lameter0718dc22006-03-25 03:06:47 -08003870 if (!l3) {
3871 free_alien_cache(new_alien);
3872 kfree(new_shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003873 goto fail;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003874 }
Christoph Lametere498be72005-09-09 13:03:32 -07003875
3876 kmem_list3_init(l3);
3877 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
Andrew Mortona737b3e2006-03-22 00:08:11 -08003878 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lametercafeb022006-03-25 03:06:46 -08003879 l3->shared = new_shared;
Christoph Lametere498be72005-09-09 13:03:32 -07003880 l3->alien = new_alien;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003881 l3->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003882 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07003883 cachep->nodelists[node] = l3;
3884 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003885 return 0;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003886
Andrew Mortona737b3e2006-03-22 00:08:11 -08003887fail:
Christoph Lameter0718dc22006-03-25 03:06:47 -08003888 if (!cachep->next.next) {
3889 /* Cache is not active yet. Roll back what we did */
3890 node--;
3891 while (node >= 0) {
3892 if (cachep->nodelists[node]) {
3893 l3 = cachep->nodelists[node];
3894
3895 kfree(l3->shared);
3896 free_alien_cache(l3->alien);
3897 kfree(l3);
3898 cachep->nodelists[node] = NULL;
3899 }
3900 node--;
3901 }
3902 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003903 return -ENOMEM;
Christoph Lametere498be72005-09-09 13:03:32 -07003904}
3905
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906struct ccupdate_struct {
Pekka Enberg343e0d72006-02-01 03:05:50 -08003907 struct kmem_cache *cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003908 struct array_cache *new[NR_CPUS];
3909};
3910
3911static void do_ccupdate_local(void *info)
3912{
Andrew Mortona737b3e2006-03-22 00:08:11 -08003913 struct ccupdate_struct *new = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 struct array_cache *old;
3915
3916 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003917 old = cpu_cache_get(new->cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003918
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
3920 new->new[smp_processor_id()] = old;
3921}
3922
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -08003923/* Always called with the cache_chain_mutex held */
Andrew Mortona737b3e2006-03-22 00:08:11 -08003924static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
3925 int batchcount, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926{
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003927 struct ccupdate_struct *new;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003928 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003930 new = kzalloc(sizeof(*new), GFP_KERNEL);
3931 if (!new)
3932 return -ENOMEM;
3933
Christoph Lametere498be72005-09-09 13:03:32 -07003934 for_each_online_cpu(i) {
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003935 new->new[i] = alloc_arraycache(cpu_to_node(i), limit,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003936 batchcount);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003937 if (!new->new[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003938 for (i--; i >= 0; i--)
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003939 kfree(new->new[i]);
3940 kfree(new);
Christoph Lametere498be72005-09-09 13:03:32 -07003941 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942 }
3943 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003944 new->cachep = cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003945
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003946 on_each_cpu(do_ccupdate_local, (void *)new, 1, 1);
Christoph Lametere498be72005-09-09 13:03:32 -07003947
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 check_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 cachep->batchcount = batchcount;
3950 cachep->limit = limit;
Christoph Lametere498be72005-09-09 13:03:32 -07003951 cachep->shared = shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952
Christoph Lametere498be72005-09-09 13:03:32 -07003953 for_each_online_cpu(i) {
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003954 struct array_cache *ccold = new->new[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003955 if (!ccold)
3956 continue;
Christoph Lametere498be72005-09-09 13:03:32 -07003957 spin_lock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
Christoph Lameterff694162005-09-22 21:44:02 -07003958 free_block(cachep, ccold->entry, ccold->avail, cpu_to_node(i));
Christoph Lametere498be72005-09-09 13:03:32 -07003959 spin_unlock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 kfree(ccold);
3961 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003962 kfree(new);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003963 return alloc_kmemlist(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003964}
3965
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -08003966/* Called with cache_chain_mutex held always */
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003967static int enable_cpucache(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968{
3969 int err;
3970 int limit, shared;
3971
Andrew Mortona737b3e2006-03-22 00:08:11 -08003972 /*
3973 * The head array serves three purposes:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974 * - create a LIFO ordering, i.e. return objects that are cache-warm
3975 * - reduce the number of spinlock operations.
Andrew Mortona737b3e2006-03-22 00:08:11 -08003976 * - reduce the number of linked list operations on the slab and
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 * bufctl chains: array operations are cheaper.
3978 * The numbers are guessed, we should auto-tune as described by
3979 * Bonwick.
3980 */
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003981 if (cachep->buffer_size > 131072)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 limit = 1;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003983 else if (cachep->buffer_size > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984 limit = 8;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003985 else if (cachep->buffer_size > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 limit = 24;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003987 else if (cachep->buffer_size > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 limit = 54;
3989 else
3990 limit = 120;
3991
Andrew Mortona737b3e2006-03-22 00:08:11 -08003992 /*
3993 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 * allocation behaviour: Most allocs on one cpu, most free operations
3995 * on another cpu. For these cases, an efficient object passing between
3996 * cpus is necessary. This is provided by a shared array. The array
3997 * replaces Bonwick's magazine layer.
3998 * On uniprocessor, it's functionally equivalent (but less efficient)
3999 * to a larger limit. Thus disabled by default.
4000 */
4001 shared = 0;
Eric Dumazet364fbb22007-05-06 14:49:27 -07004002 if (cachep->buffer_size <= PAGE_SIZE && num_possible_cpus() > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 shared = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004
4005#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08004006 /*
4007 * With debugging enabled, large batchcount lead to excessively long
4008 * periods with disabled local interrupts. Limit the batchcount
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 */
4010 if (limit > 32)
4011 limit = 32;
4012#endif
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004013 err = do_tune_cpucache(cachep, limit, (limit + 1) / 2, shared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 if (err)
4015 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004016 cachep->name, -err);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07004017 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018}
4019
Christoph Lameter1b552532006-03-22 00:09:07 -08004020/*
4021 * Drain an array if it contains any elements taking the l3 lock only if
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004022 * necessary. Note that the l3 listlock also protects the array_cache
4023 * if drain_array() is used on the shared array.
Christoph Lameter1b552532006-03-22 00:09:07 -08004024 */
4025void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
4026 struct array_cache *ac, int force, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027{
4028 int tofree;
4029
Christoph Lameter1b552532006-03-22 00:09:07 -08004030 if (!ac || !ac->avail)
4031 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 if (ac->touched && !force) {
4033 ac->touched = 0;
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004034 } else {
Christoph Lameter1b552532006-03-22 00:09:07 -08004035 spin_lock_irq(&l3->list_lock);
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004036 if (ac->avail) {
4037 tofree = force ? ac->avail : (ac->limit + 4) / 5;
4038 if (tofree > ac->avail)
4039 tofree = (ac->avail + 1) / 2;
4040 free_block(cachep, ac->entry, tofree, node);
4041 ac->avail -= tofree;
4042 memmove(ac->entry, &(ac->entry[tofree]),
4043 sizeof(void *) * ac->avail);
4044 }
Christoph Lameter1b552532006-03-22 00:09:07 -08004045 spin_unlock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 }
4047}
4048
4049/**
4050 * cache_reap - Reclaim memory from caches.
Randy Dunlap05fb6bf2007-02-28 20:12:13 -08004051 * @w: work descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 *
4053 * Called from workqueue/eventd every few seconds.
4054 * Purpose:
4055 * - clear the per-cpu caches for this CPU.
4056 * - return freeable pages to the main free memory pool.
4057 *
Andrew Mortona737b3e2006-03-22 00:08:11 -08004058 * If we cannot acquire the cache chain mutex then just give up - we'll try
4059 * again on the next iteration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004061static void cache_reap(struct work_struct *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062{
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004063 struct kmem_cache *searchp;
Christoph Lametere498be72005-09-09 13:03:32 -07004064 struct kmem_list3 *l3;
Christoph Lameteraab22072006-03-22 00:09:06 -08004065 int node = numa_node_id();
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004066 struct delayed_work *work =
4067 container_of(w, struct delayed_work, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004069 if (!mutex_trylock(&cache_chain_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 /* Give up. Setup the next iteration. */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004071 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004073 list_for_each_entry(searchp, &cache_chain, next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 check_irq_on();
4075
Christoph Lameter35386e32006-03-22 00:09:05 -08004076 /*
4077 * We only take the l3 lock if absolutely necessary and we
4078 * have established with reasonable certainty that
4079 * we can do some work if the lock was obtained.
4080 */
Christoph Lameteraab22072006-03-22 00:09:06 -08004081 l3 = searchp->nodelists[node];
Christoph Lameter35386e32006-03-22 00:09:05 -08004082
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004083 reap_alien(searchp, l3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
Christoph Lameteraab22072006-03-22 00:09:06 -08004085 drain_array(searchp, l3, cpu_cache_get(searchp), 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
Christoph Lameter35386e32006-03-22 00:09:05 -08004087 /*
4088 * These are racy checks but it does not matter
4089 * if we skip one check or scan twice.
4090 */
Christoph Lametere498be72005-09-09 13:03:32 -07004091 if (time_after(l3->next_reap, jiffies))
Christoph Lameter35386e32006-03-22 00:09:05 -08004092 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
Christoph Lametere498be72005-09-09 13:03:32 -07004094 l3->next_reap = jiffies + REAPTIMEOUT_LIST3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095
Christoph Lameteraab22072006-03-22 00:09:06 -08004096 drain_array(searchp, l3, l3->shared, 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
Christoph Lametered11d9e2006-06-30 01:55:45 -07004098 if (l3->free_touched)
Christoph Lametere498be72005-09-09 13:03:32 -07004099 l3->free_touched = 0;
Christoph Lametered11d9e2006-06-30 01:55:45 -07004100 else {
4101 int freed;
4102
4103 freed = drain_freelist(searchp, l3, (l3->free_limit +
4104 5 * searchp->num - 1) / (5 * searchp->num));
4105 STATS_ADD_REAPED(searchp, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 }
Christoph Lameter35386e32006-03-22 00:09:05 -08004107next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 cond_resched();
4109 }
4110 check_irq_on();
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004111 mutex_unlock(&cache_chain_mutex);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004112 next_reap_node();
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004113out:
Andrew Mortona737b3e2006-03-22 00:08:11 -08004114 /* Set up the next iteration */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004115 schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_CPUC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116}
4117
4118#ifdef CONFIG_PROC_FS
4119
Pekka Enberg85289f92006-01-08 01:00:36 -08004120static void print_slabinfo_header(struct seq_file *m)
4121{
4122 /*
4123 * Output format version, so at least we can change it
4124 * without _too_ many complaints.
4125 */
4126#if STATS
4127 seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
4128#else
4129 seq_puts(m, "slabinfo - version: 2.1\n");
4130#endif
4131 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
4132 "<objperslab> <pagesperslab>");
4133 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
4134 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
4135#if STATS
4136 seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004137 "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
Pekka Enberg85289f92006-01-08 01:00:36 -08004138 seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
4139#endif
4140 seq_putc(m, '\n');
4141}
4142
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143static void *s_start(struct seq_file *m, loff_t *pos)
4144{
4145 loff_t n = *pos;
4146 struct list_head *p;
4147
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004148 mutex_lock(&cache_chain_mutex);
Pekka Enberg85289f92006-01-08 01:00:36 -08004149 if (!n)
4150 print_slabinfo_header(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 p = cache_chain.next;
4152 while (n--) {
4153 p = p->next;
4154 if (p == &cache_chain)
4155 return NULL;
4156 }
Pekka Enberg343e0d72006-02-01 03:05:50 -08004157 return list_entry(p, struct kmem_cache, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158}
4159
4160static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4161{
Pekka Enberg343e0d72006-02-01 03:05:50 -08004162 struct kmem_cache *cachep = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 ++*pos;
Andrew Mortona737b3e2006-03-22 00:08:11 -08004164 return cachep->next.next == &cache_chain ?
4165 NULL : list_entry(cachep->next.next, struct kmem_cache, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166}
4167
4168static void s_stop(struct seq_file *m, void *p)
4169{
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004170 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171}
4172
4173static int s_show(struct seq_file *m, void *p)
4174{
Pekka Enberg343e0d72006-02-01 03:05:50 -08004175 struct kmem_cache *cachep = p;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004176 struct slab *slabp;
4177 unsigned long active_objs;
4178 unsigned long num_objs;
4179 unsigned long active_slabs = 0;
4180 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004181 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 char *error = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07004183 int node;
4184 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 active_objs = 0;
4187 num_slabs = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004188 for_each_online_node(node) {
4189 l3 = cachep->nodelists[node];
4190 if (!l3)
4191 continue;
4192
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004193 check_irq_on();
4194 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07004195
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004196 list_for_each_entry(slabp, &l3->slabs_full, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004197 if (slabp->inuse != cachep->num && !error)
4198 error = "slabs_full accounting error";
4199 active_objs += cachep->num;
4200 active_slabs++;
4201 }
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004202 list_for_each_entry(slabp, &l3->slabs_partial, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004203 if (slabp->inuse == cachep->num && !error)
4204 error = "slabs_partial inuse accounting error";
4205 if (!slabp->inuse && !error)
4206 error = "slabs_partial/inuse accounting error";
4207 active_objs += slabp->inuse;
4208 active_slabs++;
4209 }
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004210 list_for_each_entry(slabp, &l3->slabs_free, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004211 if (slabp->inuse && !error)
4212 error = "slabs_free/inuse accounting error";
4213 num_slabs++;
4214 }
4215 free_objects += l3->free_objects;
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08004216 if (l3->shared)
4217 shared_avail += l3->shared->avail;
Christoph Lametere498be72005-09-09 13:03:32 -07004218
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004219 spin_unlock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004221 num_slabs += active_slabs;
4222 num_objs = num_slabs * cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07004223 if (num_objs - active_objs != free_objects && !error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224 error = "free_objects accounting error";
4225
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004226 name = cachep->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 if (error)
4228 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
4229
4230 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
Manfred Spraul3dafccf2006-02-01 03:05:42 -08004231 name, active_objs, num_objs, cachep->buffer_size,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004232 cachep->num, (1 << cachep->gfporder));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 seq_printf(m, " : tunables %4u %4u %4u",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004234 cachep->limit, cachep->batchcount, cachep->shared);
Christoph Lametere498be72005-09-09 13:03:32 -07004235 seq_printf(m, " : slabdata %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004236 active_slabs, num_slabs, shared_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237#if STATS
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004238 { /* list3 stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 unsigned long high = cachep->high_mark;
4240 unsigned long allocs = cachep->num_allocations;
4241 unsigned long grown = cachep->grown;
4242 unsigned long reaped = cachep->reaped;
4243 unsigned long errors = cachep->errors;
4244 unsigned long max_freeable = cachep->max_freeable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 unsigned long node_allocs = cachep->node_allocs;
Christoph Lametere498be72005-09-09 13:03:32 -07004246 unsigned long node_frees = cachep->node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004247 unsigned long overflows = cachep->node_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Christoph Lametere498be72005-09-09 13:03:32 -07004249 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu \
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004250 %4lu %4lu %4lu %4lu %4lu", allocs, high, grown,
Andrew Mortona737b3e2006-03-22 00:08:11 -08004251 reaped, errors, max_freeable, node_allocs,
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004252 node_frees, overflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004253 }
4254 /* cpu stats */
4255 {
4256 unsigned long allochit = atomic_read(&cachep->allochit);
4257 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
4258 unsigned long freehit = atomic_read(&cachep->freehit);
4259 unsigned long freemiss = atomic_read(&cachep->freemiss);
4260
4261 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004262 allochit, allocmiss, freehit, freemiss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 }
4264#endif
4265 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266 return 0;
4267}
4268
4269/*
4270 * slabinfo_op - iterator that generates /proc/slabinfo
4271 *
4272 * Output layout:
4273 * cache-name
4274 * num-active-objs
4275 * total-objs
4276 * object size
4277 * num-active-slabs
4278 * total-slabs
4279 * num-pages-per-slab
4280 * + further values on SMP and with statistics enabled
4281 */
4282
Helge Deller15ad7cd2006-12-06 20:40:36 -08004283const struct seq_operations slabinfo_op = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004284 .start = s_start,
4285 .next = s_next,
4286 .stop = s_stop,
4287 .show = s_show,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288};
4289
4290#define MAX_SLABINFO_WRITE 128
4291/**
4292 * slabinfo_write - Tuning for the slab allocator
4293 * @file: unused
4294 * @buffer: user buffer
4295 * @count: data length
4296 * @ppos: unused
4297 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004298ssize_t slabinfo_write(struct file *file, const char __user * buffer,
4299 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004301 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004302 int limit, batchcount, shared, res;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004303 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004304
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305 if (count > MAX_SLABINFO_WRITE)
4306 return -EINVAL;
4307 if (copy_from_user(&kbuf, buffer, count))
4308 return -EFAULT;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004309 kbuf[MAX_SLABINFO_WRITE] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07004310
4311 tmp = strchr(kbuf, ' ');
4312 if (!tmp)
4313 return -EINVAL;
4314 *tmp = '\0';
4315 tmp++;
4316 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
4317 return -EINVAL;
4318
4319 /* Find the cache in the chain of caches. */
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004320 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 res = -EINVAL;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004322 list_for_each_entry(cachep, &cache_chain, next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323 if (!strcmp(cachep->name, kbuf)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08004324 if (limit < 1 || batchcount < 1 ||
4325 batchcount > limit || shared < 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07004326 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07004328 res = do_tune_cpucache(cachep, limit,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004329 batchcount, shared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 }
4331 break;
4332 }
4333 }
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004334 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 if (res >= 0)
4336 res = count;
4337 return res;
4338}
Al Viro871751e2006-03-25 03:06:39 -08004339
4340#ifdef CONFIG_DEBUG_SLAB_LEAK
4341
4342static void *leaks_start(struct seq_file *m, loff_t *pos)
4343{
4344 loff_t n = *pos;
4345 struct list_head *p;
4346
4347 mutex_lock(&cache_chain_mutex);
4348 p = cache_chain.next;
4349 while (n--) {
4350 p = p->next;
4351 if (p == &cache_chain)
4352 return NULL;
4353 }
4354 return list_entry(p, struct kmem_cache, next);
4355}
4356
4357static inline int add_caller(unsigned long *n, unsigned long v)
4358{
4359 unsigned long *p;
4360 int l;
4361 if (!v)
4362 return 1;
4363 l = n[1];
4364 p = n + 2;
4365 while (l) {
4366 int i = l/2;
4367 unsigned long *q = p + 2 * i;
4368 if (*q == v) {
4369 q[1]++;
4370 return 1;
4371 }
4372 if (*q > v) {
4373 l = i;
4374 } else {
4375 p = q + 2;
4376 l -= i + 1;
4377 }
4378 }
4379 if (++n[1] == n[0])
4380 return 0;
4381 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4382 p[0] = v;
4383 p[1] = 1;
4384 return 1;
4385}
4386
4387static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
4388{
4389 void *p;
4390 int i;
4391 if (n[0] == n[1])
4392 return;
4393 for (i = 0, p = s->s_mem; i < c->num; i++, p += c->buffer_size) {
4394 if (slab_bufctl(s)[i] != BUFCTL_ACTIVE)
4395 continue;
4396 if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
4397 return;
4398 }
4399}
4400
4401static void show_symbol(struct seq_file *m, unsigned long address)
4402{
4403#ifdef CONFIG_KALLSYMS
Al Viro871751e2006-03-25 03:06:39 -08004404 unsigned long offset, size;
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004405 char modname[MODULE_NAME_LEN + 1], name[KSYM_NAME_LEN + 1];
Al Viro871751e2006-03-25 03:06:39 -08004406
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004407 if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
Al Viro871751e2006-03-25 03:06:39 -08004408 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004409 if (modname[0])
Al Viro871751e2006-03-25 03:06:39 -08004410 seq_printf(m, " [%s]", modname);
4411 return;
4412 }
4413#endif
4414 seq_printf(m, "%p", (void *)address);
4415}
4416
4417static int leaks_show(struct seq_file *m, void *p)
4418{
4419 struct kmem_cache *cachep = p;
Al Viro871751e2006-03-25 03:06:39 -08004420 struct slab *slabp;
4421 struct kmem_list3 *l3;
4422 const char *name;
4423 unsigned long *n = m->private;
4424 int node;
4425 int i;
4426
4427 if (!(cachep->flags & SLAB_STORE_USER))
4428 return 0;
4429 if (!(cachep->flags & SLAB_RED_ZONE))
4430 return 0;
4431
4432 /* OK, we can do it */
4433
4434 n[1] = 0;
4435
4436 for_each_online_node(node) {
4437 l3 = cachep->nodelists[node];
4438 if (!l3)
4439 continue;
4440
4441 check_irq_on();
4442 spin_lock_irq(&l3->list_lock);
4443
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004444 list_for_each_entry(slabp, &l3->slabs_full, list)
Al Viro871751e2006-03-25 03:06:39 -08004445 handle_slab(n, cachep, slabp);
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004446 list_for_each_entry(slabp, &l3->slabs_partial, list)
Al Viro871751e2006-03-25 03:06:39 -08004447 handle_slab(n, cachep, slabp);
Al Viro871751e2006-03-25 03:06:39 -08004448 spin_unlock_irq(&l3->list_lock);
4449 }
4450 name = cachep->name;
4451 if (n[0] == n[1]) {
4452 /* Increase the buffer size */
4453 mutex_unlock(&cache_chain_mutex);
4454 m->private = kzalloc(n[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
4455 if (!m->private) {
4456 /* Too bad, we are really out */
4457 m->private = n;
4458 mutex_lock(&cache_chain_mutex);
4459 return -ENOMEM;
4460 }
4461 *(unsigned long *)m->private = n[0] * 2;
4462 kfree(n);
4463 mutex_lock(&cache_chain_mutex);
4464 /* Now make sure this entry will be retried */
4465 m->count = m->size;
4466 return 0;
4467 }
4468 for (i = 0; i < n[1]; i++) {
4469 seq_printf(m, "%s: %lu ", name, n[2*i+3]);
4470 show_symbol(m, n[2*i+2]);
4471 seq_putc(m, '\n');
4472 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004473
Al Viro871751e2006-03-25 03:06:39 -08004474 return 0;
4475}
4476
Helge Deller15ad7cd2006-12-06 20:40:36 -08004477const struct seq_operations slabstats_op = {
Al Viro871751e2006-03-25 03:06:39 -08004478 .start = leaks_start,
4479 .next = s_next,
4480 .stop = s_stop,
4481 .show = leaks_show,
4482};
4483#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484#endif
4485
Manfred Spraul00e145b2005-09-03 15:55:07 -07004486/**
4487 * ksize - get the actual amount of memory allocated for a given object
4488 * @objp: Pointer to the object
4489 *
4490 * kmalloc may internally round up allocations and return more memory
4491 * than requested. ksize() can be used to determine the actual amount of
4492 * memory allocated. The caller may use this additional memory, even though
4493 * a smaller amount of memory was initially specified with the kmalloc call.
4494 * The caller must guarantee that objp points to a valid object previously
4495 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4496 * must not be freed during the duration of the call.
4497 */
Pekka Enbergfd76bab2007-05-06 14:48:40 -07004498size_t ksize(const void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499{
Manfred Spraul00e145b2005-09-03 15:55:07 -07004500 if (unlikely(objp == NULL))
4501 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
Pekka Enberg6ed5eb22006-02-01 03:05:49 -08004503 return obj_size(virt_to_cache(objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504}