blob: 1a7a10de2a4d21737a4e89281a7d2a7b56ec214f [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
412 /* de-constructor func */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800413 void (*dtor) (void *, struct kmem_cache *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800415/* 5) cache creation/removal */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800416 const char *name;
417 struct list_head next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800419/* 6) statistics */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420#if STATS
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800421 unsigned long num_active;
422 unsigned long num_allocations;
423 unsigned long high_mark;
424 unsigned long grown;
425 unsigned long reaped;
426 unsigned long errors;
427 unsigned long max_freeable;
428 unsigned long node_allocs;
429 unsigned long node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700430 unsigned long node_overflow;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800431 atomic_t allochit;
432 atomic_t allocmiss;
433 atomic_t freehit;
434 atomic_t freemiss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435#endif
436#if DEBUG
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800437 /*
438 * If debugging is enabled, then the allocator can add additional
439 * fields and/or padding to every object. buffer_size contains the total
440 * object size including these internal fields, the following two
441 * variables contain the offset to the user object and its size.
442 */
443 int obj_offset;
444 int obj_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445#endif
Eric Dumazet8da34302007-05-06 14:49:29 -0700446 /*
447 * We put nodelists[] at the end of kmem_cache, because we want to size
448 * this array to nr_node_ids slots instead of MAX_NUMNODES
449 * (see kmem_cache_init())
450 * We still use [MAX_NUMNODES] and not [1] or [0] because cache_cache
451 * is statically defined, so we reserve the max number of nodes.
452 */
453 struct kmem_list3 *nodelists[MAX_NUMNODES];
454 /*
455 * Do not add fields after nodelists[]
456 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457};
458
459#define CFLGS_OFF_SLAB (0x80000000UL)
460#define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
461
462#define BATCHREFILL_LIMIT 16
Andrew Mortona737b3e2006-03-22 00:08:11 -0800463/*
464 * Optimization question: fewer reaps means less probability for unnessary
465 * cpucache drain/refill cycles.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 *
Adrian Bunkdc6f3f22005-11-08 16:44:08 +0100467 * OTOH the cpuarrays can contain lots of objects,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 * which could lock up otherwise freeable slabs.
469 */
470#define REAPTIMEOUT_CPUC (2*HZ)
471#define REAPTIMEOUT_LIST3 (4*HZ)
472
473#if STATS
474#define STATS_INC_ACTIVE(x) ((x)->num_active++)
475#define STATS_DEC_ACTIVE(x) ((x)->num_active--)
476#define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
477#define STATS_INC_GROWN(x) ((x)->grown++)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700478#define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
Andrew Mortona737b3e2006-03-22 00:08:11 -0800479#define STATS_SET_HIGH(x) \
480 do { \
481 if ((x)->num_active > (x)->high_mark) \
482 (x)->high_mark = (x)->num_active; \
483 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484#define STATS_INC_ERR(x) ((x)->errors++)
485#define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
Christoph Lametere498be72005-09-09 13:03:32 -0700486#define STATS_INC_NODEFREES(x) ((x)->node_frees++)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700487#define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800488#define STATS_SET_FREEABLE(x, i) \
489 do { \
490 if ((x)->max_freeable < i) \
491 (x)->max_freeable = i; \
492 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493#define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
494#define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
495#define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
496#define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
497#else
498#define STATS_INC_ACTIVE(x) do { } while (0)
499#define STATS_DEC_ACTIVE(x) do { } while (0)
500#define STATS_INC_ALLOCED(x) do { } while (0)
501#define STATS_INC_GROWN(x) do { } while (0)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700502#define STATS_ADD_REAPED(x,y) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503#define STATS_SET_HIGH(x) do { } while (0)
504#define STATS_INC_ERR(x) do { } while (0)
505#define STATS_INC_NODEALLOCS(x) do { } while (0)
Christoph Lametere498be72005-09-09 13:03:32 -0700506#define STATS_INC_NODEFREES(x) do { } while (0)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700507#define STATS_INC_ACOVERFLOW(x) do { } while (0)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800508#define STATS_SET_FREEABLE(x, i) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509#define STATS_INC_ALLOCHIT(x) do { } while (0)
510#define STATS_INC_ALLOCMISS(x) do { } while (0)
511#define STATS_INC_FREEHIT(x) do { } while (0)
512#define STATS_INC_FREEMISS(x) do { } while (0)
513#endif
514
515#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Andrew Mortona737b3e2006-03-22 00:08:11 -0800517/*
518 * memory layout of objects:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 * 0 : objp
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800520 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 * the end of an object is aligned with the end of the real
522 * allocation. Catches writes behind the end of the allocation.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800523 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 * redzone word.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800525 * cachep->obj_offset: The real object.
526 * cachep->buffer_size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
Andrew Mortona737b3e2006-03-22 00:08:11 -0800527 * cachep->buffer_size - 1* BYTES_PER_WORD: last caller address
528 * [BYTES_PER_WORD long]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800530static int obj_offset(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800532 return cachep->obj_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
Pekka Enberg343e0d72006-02-01 03:05:50 -0800535static int obj_size(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800537 return cachep->obj_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
539
David Woodhouseb46b8f12007-05-08 00:22:59 -0700540static unsigned long long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
542 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
David Woodhouseb46b8f12007-05-08 00:22:59 -0700543 return (unsigned long long*) (objp + obj_offset(cachep) -
544 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
David Woodhouseb46b8f12007-05-08 00:22:59 -0700547static unsigned long long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
550 if (cachep->flags & SLAB_STORE_USER)
David Woodhouseb46b8f12007-05-08 00:22:59 -0700551 return (unsigned long long *)(objp + cachep->buffer_size -
552 sizeof(unsigned long long) -
553 BYTES_PER_WORD);
554 return (unsigned long long *) (objp + cachep->buffer_size -
555 sizeof(unsigned long long));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Pekka Enberg343e0d72006-02-01 03:05:50 -0800558static void **dbg_userword(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 BUG_ON(!(cachep->flags & SLAB_STORE_USER));
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800561 return (void **)(objp + cachep->buffer_size - BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
564#else
565
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800566#define obj_offset(x) 0
567#define obj_size(cachep) (cachep->buffer_size)
David Woodhouseb46b8f12007-05-08 00:22:59 -0700568#define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
569#define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long long *)NULL;})
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570#define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
571
572#endif
573
574/*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800575 * Maximum size of an obj (in 2^order pages) and absolute limit for the gfp
576 * order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
578#if defined(CONFIG_LARGE_ALLOCS)
579#define MAX_OBJ_ORDER 13 /* up to 32Mb */
580#define MAX_GFP_ORDER 13 /* up to 32Mb */
581#elif defined(CONFIG_MMU)
582#define MAX_OBJ_ORDER 5 /* 32 pages */
583#define MAX_GFP_ORDER 5 /* 32 pages */
584#else
585#define MAX_OBJ_ORDER 8 /* up to 1Mb */
586#define MAX_GFP_ORDER 8 /* up to 1Mb */
587#endif
588
589/*
590 * Do not go above this order unless 0 objects fit into the slab.
591 */
592#define BREAK_GFP_ORDER_HI 1
593#define BREAK_GFP_ORDER_LO 0
594static int slab_break_gfp_order = BREAK_GFP_ORDER_LO;
595
Andrew Mortona737b3e2006-03-22 00:08:11 -0800596/*
597 * Functions for storing/retrieving the cachep and or slab from the page
598 * allocator. These are used to find the slab an obj belongs to. With kfree(),
599 * these are used to find the cache which an obj belongs to.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
Pekka Enberg065d41c2005-11-13 16:06:46 -0800601static inline void page_set_cache(struct page *page, struct kmem_cache *cache)
602{
603 page->lru.next = (struct list_head *)cache;
604}
605
606static inline struct kmem_cache *page_get_cache(struct page *page)
607{
Christoph Lameterd85f3382007-05-06 14:49:39 -0700608 page = compound_head(page);
Pekka Enbergddc2e812006-06-23 02:03:40 -0700609 BUG_ON(!PageSlab(page));
Pekka Enberg065d41c2005-11-13 16:06:46 -0800610 return (struct kmem_cache *)page->lru.next;
611}
612
613static inline void page_set_slab(struct page *page, struct slab *slab)
614{
615 page->lru.prev = (struct list_head *)slab;
616}
617
618static inline struct slab *page_get_slab(struct page *page)
619{
Pekka Enbergddc2e812006-06-23 02:03:40 -0700620 BUG_ON(!PageSlab(page));
Pekka Enberg065d41c2005-11-13 16:06:46 -0800621 return (struct slab *)page->lru.prev;
622}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800624static inline struct kmem_cache *virt_to_cache(const void *obj)
625{
Christoph Lameterb49af682007-05-06 14:49:41 -0700626 struct page *page = virt_to_head_page(obj);
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800627 return page_get_cache(page);
628}
629
630static inline struct slab *virt_to_slab(const void *obj)
631{
Christoph Lameterb49af682007-05-06 14:49:41 -0700632 struct page *page = virt_to_head_page(obj);
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -0800633 return page_get_slab(page);
634}
635
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800636static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
637 unsigned int idx)
638{
639 return slab->s_mem + cache->buffer_size * idx;
640}
641
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800642/*
643 * We want to avoid an expensive divide : (offset / cache->buffer_size)
644 * Using the fact that buffer_size is a constant for a particular cache,
645 * we can replace (offset / cache->buffer_size) by
646 * reciprocal_divide(offset, cache->reciprocal_buffer_size)
647 */
648static inline unsigned int obj_to_index(const struct kmem_cache *cache,
649 const struct slab *slab, void *obj)
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800650{
Eric Dumazet6a2d7a92006-12-13 00:34:27 -0800651 u32 offset = (obj - slab->s_mem);
652 return reciprocal_divide(offset, cache->reciprocal_buffer_size);
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800653}
654
Andrew Mortona737b3e2006-03-22 00:08:11 -0800655/*
656 * These are the default caches for kmalloc. Custom caches can have other sizes.
657 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658struct cache_sizes malloc_sizes[] = {
659#define CACHE(x) { .cs_size = (x) },
660#include <linux/kmalloc_sizes.h>
661 CACHE(ULONG_MAX)
662#undef CACHE
663};
664EXPORT_SYMBOL(malloc_sizes);
665
666/* Must match cache_sizes above. Out of line to keep cache footprint low. */
667struct cache_names {
668 char *name;
669 char *name_dma;
670};
671
672static struct cache_names __initdata cache_names[] = {
673#define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },
674#include <linux/kmalloc_sizes.h>
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800675 {NULL,}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676#undef CACHE
677};
678
679static struct arraycache_init initarray_cache __initdata =
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800680 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681static struct arraycache_init initarray_generic =
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800682 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
684/* internal cache of cache description objs */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800685static struct kmem_cache cache_cache = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800686 .batchcount = 1,
687 .limit = BOOT_CPUCACHE_ENTRIES,
688 .shared = 1,
Pekka Enberg343e0d72006-02-01 03:05:50 -0800689 .buffer_size = sizeof(struct kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800690 .name = "kmem_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691};
692
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700693#define BAD_ALIEN_MAGIC 0x01020304ul
694
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200695#ifdef CONFIG_LOCKDEP
696
697/*
698 * Slab sometimes uses the kmalloc slabs to store the slab headers
699 * for other slabs "off slab".
700 * The locking for this is tricky in that it nests within the locks
701 * of all other slabs in a few places; to deal with this special
702 * locking we put on-slab caches into a separate lock-class.
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700703 *
704 * We set lock class for alien array caches which are up during init.
705 * The lock annotation will be lost if all cpus of a node goes down and
706 * then comes back up during hotplug
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200707 */
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700708static struct lock_class_key on_slab_l3_key;
709static struct lock_class_key on_slab_alc_key;
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200710
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700711static inline void init_lock_keys(void)
712
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200713{
714 int q;
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700715 struct cache_sizes *s = malloc_sizes;
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200716
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700717 while (s->cs_size != ULONG_MAX) {
718 for_each_node(q) {
719 struct array_cache **alc;
720 int r;
721 struct kmem_list3 *l3 = s->cs_cachep->nodelists[q];
722 if (!l3 || OFF_SLAB(s->cs_cachep))
723 continue;
724 lockdep_set_class(&l3->list_lock, &on_slab_l3_key);
725 alc = l3->alien;
726 /*
727 * FIXME: This check for BAD_ALIEN_MAGIC
728 * should go away when common slab code is taught to
729 * work even without alien caches.
730 * Currently, non NUMA code returns BAD_ALIEN_MAGIC
731 * for alloc_alien_cache,
732 */
733 if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC)
734 continue;
735 for_each_node(r) {
736 if (alc[r])
737 lockdep_set_class(&alc[r]->lock,
738 &on_slab_alc_key);
739 }
740 }
741 s++;
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200742 }
743}
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200744#else
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -0700745static inline void init_lock_keys(void)
Arjan van de Venf1aaee52006-07-13 14:46:03 +0200746{
747}
748#endif
749
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -0800750/*
751 * 1. Guard access to the cache-chain.
752 * 2. Protect sanity of cpu_online_map against cpu hotplug events
753 */
Ingo Molnarfc0abb12006-01-18 17:42:33 -0800754static DEFINE_MUTEX(cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755static struct list_head cache_chain;
756
757/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 * chicken and egg problem: delay the per-cpu array allocation
759 * until the general caches are up.
760 */
761static enum {
762 NONE,
Christoph Lametere498be72005-09-09 13:03:32 -0700763 PARTIAL_AC,
764 PARTIAL_L3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 FULL
766} g_cpucache_up;
767
Mike Kravetz39d24e62006-05-15 09:44:13 -0700768/*
769 * used by boot code to determine if it can use slab based allocator
770 */
771int slab_is_available(void)
772{
773 return g_cpucache_up == FULL;
774}
775
David Howells52bad642006-11-22 14:54:01 +0000776static DEFINE_PER_CPU(struct delayed_work, reap_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
Pekka Enberg343e0d72006-02-01 03:05:50 -0800778static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
780 return cachep->array[smp_processor_id()];
781}
782
Andrew Mortona737b3e2006-03-22 00:08:11 -0800783static inline struct kmem_cache *__find_general_cachep(size_t size,
784 gfp_t gfpflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 struct cache_sizes *csizep = malloc_sizes;
787
788#if DEBUG
789 /* This happens if someone tries to call
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800790 * kmem_cache_create(), or __kmalloc(), before
791 * the generic caches are initialized.
792 */
Alok Katariac7e43c72005-09-14 12:17:53 -0700793 BUG_ON(malloc_sizes[INDEX_AC].cs_cachep == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794#endif
795 while (size > csizep->cs_size)
796 csizep++;
797
798 /*
Martin Hicks0abf40c2005-09-03 15:54:54 -0700799 * Really subtle: The last entry with cs->cs_size==ULONG_MAX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 * has cs_{dma,}cachep==NULL. Thus no special case
801 * for large kmalloc calls required.
802 */
Christoph Lameter4b51d662007-02-10 01:43:10 -0800803#ifdef CONFIG_ZONE_DMA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 if (unlikely(gfpflags & GFP_DMA))
805 return csizep->cs_dmacachep;
Christoph Lameter4b51d662007-02-10 01:43:10 -0800806#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return csizep->cs_cachep;
808}
809
Adrian Bunkb2213852006-09-25 23:31:02 -0700810static struct kmem_cache *kmem_find_general_cachep(size_t size, gfp_t gfpflags)
Manfred Spraul97e2bde2005-05-01 08:58:38 -0700811{
812 return __find_general_cachep(size, gfpflags);
813}
Manfred Spraul97e2bde2005-05-01 08:58:38 -0700814
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800815static size_t slab_mgmt_size(size_t nr_objs, size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800817 return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align);
818}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Andrew Mortona737b3e2006-03-22 00:08:11 -0800820/*
821 * Calculate the number of objects and left-over bytes for a given buffer size.
822 */
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800823static void cache_estimate(unsigned long gfporder, size_t buffer_size,
824 size_t align, int flags, size_t *left_over,
825 unsigned int *num)
826{
827 int nr_objs;
828 size_t mgmt_size;
829 size_t slab_size = PAGE_SIZE << gfporder;
830
831 /*
832 * The slab management structure can be either off the slab or
833 * on it. For the latter case, the memory allocated for a
834 * slab is used for:
835 *
836 * - The struct slab
837 * - One kmem_bufctl_t for each object
838 * - Padding to respect alignment of @align
839 * - @buffer_size bytes for each object
840 *
841 * If the slab management structure is off the slab, then the
842 * alignment will already be calculated into the size. Because
843 * the slabs are all pages aligned, the objects will be at the
844 * correct alignment when allocated.
845 */
846 if (flags & CFLGS_OFF_SLAB) {
847 mgmt_size = 0;
848 nr_objs = slab_size / buffer_size;
849
850 if (nr_objs > SLAB_LIMIT)
851 nr_objs = SLAB_LIMIT;
852 } else {
853 /*
854 * Ignore padding for the initial guess. The padding
855 * is at most @align-1 bytes, and @buffer_size is at
856 * least @align. In the worst case, this result will
857 * be one greater than the number of objects that fit
858 * into the memory allocation when taking the padding
859 * into account.
860 */
861 nr_objs = (slab_size - sizeof(struct slab)) /
862 (buffer_size + sizeof(kmem_bufctl_t));
863
864 /*
865 * This calculated number will be either the right
866 * amount, or one greater than what we want.
867 */
868 if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
869 > slab_size)
870 nr_objs--;
871
872 if (nr_objs > SLAB_LIMIT)
873 nr_objs = SLAB_LIMIT;
874
875 mgmt_size = slab_mgmt_size(nr_objs, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 }
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800877 *num = nr_objs;
878 *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
881#define slab_error(cachep, msg) __slab_error(__FUNCTION__, cachep, msg)
882
Andrew Mortona737b3e2006-03-22 00:08:11 -0800883static void __slab_error(const char *function, struct kmem_cache *cachep,
884 char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885{
886 printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800887 function, cachep->name, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 dump_stack();
889}
890
Paul Menage3395ee02006-12-06 20:32:16 -0800891/*
892 * By default on NUMA we use alien caches to stage the freeing of
893 * objects allocated from other nodes. This causes massive memory
894 * inefficiencies when using fake NUMA setup to split memory into a
895 * large number of small nodes, so it can be disabled on the command
896 * line
897 */
898
899static int use_alien_caches __read_mostly = 1;
900static int __init noaliencache_setup(char *s)
901{
902 use_alien_caches = 0;
903 return 1;
904}
905__setup("noaliencache", noaliencache_setup);
906
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800907#ifdef CONFIG_NUMA
908/*
909 * Special reaping functions for NUMA systems called from cache_reap().
910 * These take care of doing round robin flushing of alien caches (containing
911 * objects freed on different nodes from which they were allocated) and the
912 * flushing of remote pcps by calling drain_node_pages.
913 */
914static DEFINE_PER_CPU(unsigned long, reap_node);
915
916static void init_reap_node(int cpu)
917{
918 int node;
919
920 node = next_node(cpu_to_node(cpu), node_online_map);
921 if (node == MAX_NUMNODES)
Paul Jackson442295c2006-03-22 00:09:11 -0800922 node = first_node(node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800923
Daniel Yeisley7f6b8872006-11-02 22:07:14 -0800924 per_cpu(reap_node, cpu) = node;
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800925}
926
927static void next_reap_node(void)
928{
929 int node = __get_cpu_var(reap_node);
930
931 /*
932 * Also drain per cpu pages on remote zones
933 */
934 if (node != numa_node_id())
935 drain_node_pages(node);
936
937 node = next_node(node, node_online_map);
938 if (unlikely(node >= MAX_NUMNODES))
939 node = first_node(node_online_map);
940 __get_cpu_var(reap_node) = node;
941}
942
943#else
944#define init_reap_node(cpu) do { } while (0)
945#define next_reap_node(void) do { } while (0)
946#endif
947
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948/*
949 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
950 * via the workqueue/eventd.
951 * Add the CPU number into the expiration time to minimize the possibility of
952 * the CPUs getting into lockstep and contending for the global cache chain
953 * lock.
954 */
955static void __devinit start_cpu_timer(int cpu)
956{
David Howells52bad642006-11-22 14:54:01 +0000957 struct delayed_work *reap_work = &per_cpu(reap_work, cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958
959 /*
960 * When this gets called from do_initcalls via cpucache_init(),
961 * init_workqueues() has already run, so keventd will be setup
962 * at that time.
963 */
David Howells52bad642006-11-22 14:54:01 +0000964 if (keventd_up() && reap_work->work.func == NULL) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800965 init_reap_node(cpu);
David Howells65f27f32006-11-22 14:55:48 +0000966 INIT_DELAYED_WORK(reap_work, cache_reap);
Arjan van de Ven2b284212006-12-10 02:21:28 -0800967 schedule_delayed_work_on(cpu, reap_work,
968 __round_jiffies_relative(HZ, cpu));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970}
971
Christoph Lametere498be72005-09-09 13:03:32 -0700972static struct array_cache *alloc_arraycache(int node, int entries,
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800973 int batchcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800975 int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 struct array_cache *nc = NULL;
977
Christoph Lametere498be72005-09-09 13:03:32 -0700978 nc = kmalloc_node(memsize, GFP_KERNEL, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 if (nc) {
980 nc->avail = 0;
981 nc->limit = entries;
982 nc->batchcount = batchcount;
983 nc->touched = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700984 spin_lock_init(&nc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 }
986 return nc;
987}
988
Christoph Lameter3ded1752006-03-25 03:06:44 -0800989/*
990 * Transfer objects in one arraycache to another.
991 * Locking must be handled by the caller.
992 *
993 * Return the number of entries transferred.
994 */
995static int transfer_objects(struct array_cache *to,
996 struct array_cache *from, unsigned int max)
997{
998 /* Figure out how many entries to transfer */
999 int nr = min(min(from->avail, max), to->limit - to->avail);
1000
1001 if (!nr)
1002 return 0;
1003
1004 memcpy(to->entry + to->avail, from->entry + from->avail -nr,
1005 sizeof(void *) *nr);
1006
1007 from->avail -= nr;
1008 to->avail += nr;
1009 to->touched = 1;
1010 return nr;
1011}
1012
Christoph Lameter765c4502006-09-27 01:50:08 -07001013#ifndef CONFIG_NUMA
1014
1015#define drain_alien_cache(cachep, alien) do { } while (0)
1016#define reap_alien(cachep, l3) do { } while (0)
1017
1018static inline struct array_cache **alloc_alien_cache(int node, int limit)
1019{
1020 return (struct array_cache **)BAD_ALIEN_MAGIC;
1021}
1022
1023static inline void free_alien_cache(struct array_cache **ac_ptr)
1024{
1025}
1026
1027static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
1028{
1029 return 0;
1030}
1031
1032static inline void *alternate_node_alloc(struct kmem_cache *cachep,
1033 gfp_t flags)
1034{
1035 return NULL;
1036}
1037
Christoph Hellwig8b98c162006-12-06 20:32:30 -08001038static inline void *____cache_alloc_node(struct kmem_cache *cachep,
Christoph Lameter765c4502006-09-27 01:50:08 -07001039 gfp_t flags, int nodeid)
1040{
1041 return NULL;
1042}
1043
1044#else /* CONFIG_NUMA */
1045
Christoph Hellwig8b98c162006-12-06 20:32:30 -08001046static void *____cache_alloc_node(struct kmem_cache *, gfp_t, int);
Paul Jacksonc61afb12006-03-24 03:16:08 -08001047static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
Christoph Lameterdc85da12006-01-18 17:42:36 -08001048
Pekka Enberg5295a742006-02-01 03:05:48 -08001049static struct array_cache **alloc_alien_cache(int node, int limit)
Christoph Lametere498be72005-09-09 13:03:32 -07001050{
1051 struct array_cache **ac_ptr;
Christoph Lameter8ef82862007-02-20 13:57:52 -08001052 int memsize = sizeof(void *) * nr_node_ids;
Christoph Lametere498be72005-09-09 13:03:32 -07001053 int i;
1054
1055 if (limit > 1)
1056 limit = 12;
1057 ac_ptr = kmalloc_node(memsize, GFP_KERNEL, node);
1058 if (ac_ptr) {
1059 for_each_node(i) {
1060 if (i == node || !node_online(i)) {
1061 ac_ptr[i] = NULL;
1062 continue;
1063 }
1064 ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d);
1065 if (!ac_ptr[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001066 for (i--; i <= 0; i--)
Christoph Lametere498be72005-09-09 13:03:32 -07001067 kfree(ac_ptr[i]);
1068 kfree(ac_ptr);
1069 return NULL;
1070 }
1071 }
1072 }
1073 return ac_ptr;
1074}
1075
Pekka Enberg5295a742006-02-01 03:05:48 -08001076static void free_alien_cache(struct array_cache **ac_ptr)
Christoph Lametere498be72005-09-09 13:03:32 -07001077{
1078 int i;
1079
1080 if (!ac_ptr)
1081 return;
Christoph Lametere498be72005-09-09 13:03:32 -07001082 for_each_node(i)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001083 kfree(ac_ptr[i]);
Christoph Lametere498be72005-09-09 13:03:32 -07001084 kfree(ac_ptr);
1085}
1086
Pekka Enberg343e0d72006-02-01 03:05:50 -08001087static void __drain_alien_cache(struct kmem_cache *cachep,
Pekka Enberg5295a742006-02-01 03:05:48 -08001088 struct array_cache *ac, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07001089{
1090 struct kmem_list3 *rl3 = cachep->nodelists[node];
1091
1092 if (ac->avail) {
1093 spin_lock(&rl3->list_lock);
Christoph Lametere00946f2006-03-25 03:06:45 -08001094 /*
1095 * Stuff objects into the remote nodes shared array first.
1096 * That way we could avoid the overhead of putting the objects
1097 * into the free lists and getting them back later.
1098 */
shin, jacob693f7d32006-04-28 10:54:37 -05001099 if (rl3->shared)
1100 transfer_objects(rl3->shared, ac, ac->limit);
Christoph Lametere00946f2006-03-25 03:06:45 -08001101
Christoph Lameterff694162005-09-22 21:44:02 -07001102 free_block(cachep, ac->entry, ac->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001103 ac->avail = 0;
1104 spin_unlock(&rl3->list_lock);
1105 }
1106}
1107
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001108/*
1109 * Called from cache_reap() to regularly drain alien caches round robin.
1110 */
1111static void reap_alien(struct kmem_cache *cachep, struct kmem_list3 *l3)
1112{
1113 int node = __get_cpu_var(reap_node);
1114
1115 if (l3->alien) {
1116 struct array_cache *ac = l3->alien[node];
Christoph Lametere00946f2006-03-25 03:06:45 -08001117
1118 if (ac && ac->avail && spin_trylock_irq(&ac->lock)) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001119 __drain_alien_cache(cachep, ac, node);
1120 spin_unlock_irq(&ac->lock);
1121 }
1122 }
1123}
1124
Andrew Mortona737b3e2006-03-22 00:08:11 -08001125static void drain_alien_cache(struct kmem_cache *cachep,
1126 struct array_cache **alien)
Christoph Lametere498be72005-09-09 13:03:32 -07001127{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001128 int i = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07001129 struct array_cache *ac;
1130 unsigned long flags;
1131
1132 for_each_online_node(i) {
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001133 ac = alien[i];
Christoph Lametere498be72005-09-09 13:03:32 -07001134 if (ac) {
1135 spin_lock_irqsave(&ac->lock, flags);
1136 __drain_alien_cache(cachep, ac, i);
1137 spin_unlock_irqrestore(&ac->lock, flags);
1138 }
1139 }
1140}
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001141
Ingo Molnar873623d2006-07-13 14:44:38 +02001142static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001143{
1144 struct slab *slabp = virt_to_slab(objp);
1145 int nodeid = slabp->nodeid;
1146 struct kmem_list3 *l3;
1147 struct array_cache *alien = NULL;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001148 int node;
1149
1150 node = numa_node_id();
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001151
1152 /*
1153 * Make sure we are not freeing a object from another node to the array
1154 * cache on this cpu.
1155 */
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001156 if (likely(slabp->nodeid == node))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001157 return 0;
1158
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001159 l3 = cachep->nodelists[node];
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001160 STATS_INC_NODEFREES(cachep);
1161 if (l3->alien && l3->alien[nodeid]) {
1162 alien = l3->alien[nodeid];
Ingo Molnar873623d2006-07-13 14:44:38 +02001163 spin_lock(&alien->lock);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001164 if (unlikely(alien->avail == alien->limit)) {
1165 STATS_INC_ACOVERFLOW(cachep);
1166 __drain_alien_cache(cachep, alien, nodeid);
1167 }
1168 alien->entry[alien->avail++] = objp;
1169 spin_unlock(&alien->lock);
1170 } else {
1171 spin_lock(&(cachep->nodelists[nodeid])->list_lock);
1172 free_block(cachep, &objp, 1, nodeid);
1173 spin_unlock(&(cachep->nodelists[nodeid])->list_lock);
1174 }
1175 return 1;
1176}
Christoph Lametere498be72005-09-09 13:03:32 -07001177#endif
1178
Chandra Seetharaman8c78f302006-07-30 03:03:35 -07001179static int __cpuinit cpuup_callback(struct notifier_block *nfb,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001180 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
1182 long cpu = (long)hcpu;
Pekka Enberg343e0d72006-02-01 03:05:50 -08001183 struct kmem_cache *cachep;
Christoph Lametere498be72005-09-09 13:03:32 -07001184 struct kmem_list3 *l3 = NULL;
1185 int node = cpu_to_node(cpu);
1186 int memsize = sizeof(struct kmem_list3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 switch (action) {
Heiko Carstens38c3bd92007-05-09 02:34:05 -07001189 case CPU_LOCK_ACQUIRE:
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001190 mutex_lock(&cache_chain_mutex);
Heiko Carstens38c3bd92007-05-09 02:34:05 -07001191 break;
1192 case CPU_UP_PREPARE:
Andrew Mortona737b3e2006-03-22 00:08:11 -08001193 /*
1194 * We need to do this right in the beginning since
Christoph Lametere498be72005-09-09 13:03:32 -07001195 * alloc_arraycache's are going to use this list.
1196 * kmalloc_node allows us to add the slab to the right
1197 * kmem_list3 and not this cpu's kmem_list3
1198 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Christoph Lametere498be72005-09-09 13:03:32 -07001200 list_for_each_entry(cachep, &cache_chain, next) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08001201 /*
1202 * Set up the size64 kmemlist for cpu before we can
Christoph Lametere498be72005-09-09 13:03:32 -07001203 * begin anything. Make sure some other cpu on this
1204 * node has not already allocated this
1205 */
1206 if (!cachep->nodelists[node]) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08001207 l3 = kmalloc_node(memsize, GFP_KERNEL, node);
1208 if (!l3)
Christoph Lametere498be72005-09-09 13:03:32 -07001209 goto bad;
1210 kmem_list3_init(l3);
1211 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001212 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lametere498be72005-09-09 13:03:32 -07001213
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001214 /*
1215 * The l3s don't come and go as CPUs come and
1216 * go. cache_chain_mutex is sufficient
1217 * protection here.
1218 */
Christoph Lametere498be72005-09-09 13:03:32 -07001219 cachep->nodelists[node] = l3;
1220 }
1221
1222 spin_lock_irq(&cachep->nodelists[node]->list_lock);
1223 cachep->nodelists[node]->free_limit =
Andrew Mortona737b3e2006-03-22 00:08:11 -08001224 (1 + nr_cpus_node(node)) *
1225 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07001226 spin_unlock_irq(&cachep->nodelists[node]->list_lock);
1227 }
1228
Andrew Mortona737b3e2006-03-22 00:08:11 -08001229 /*
1230 * Now we can go ahead with allocating the shared arrays and
1231 * array caches
1232 */
Christoph Lametere498be72005-09-09 13:03:32 -07001233 list_for_each_entry(cachep, &cache_chain, next) {
Tobias Klausercd105df2006-01-08 01:00:59 -08001234 struct array_cache *nc;
Eric Dumazet63109842007-05-06 14:49:28 -07001235 struct array_cache *shared = NULL;
Paul Menage3395ee02006-12-06 20:32:16 -08001236 struct array_cache **alien = NULL;
Tobias Klausercd105df2006-01-08 01:00:59 -08001237
Christoph Lametere498be72005-09-09 13:03:32 -07001238 nc = alloc_arraycache(node, cachep->limit,
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001239 cachep->batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 if (!nc)
1241 goto bad;
Eric Dumazet63109842007-05-06 14:49:28 -07001242 if (cachep->shared) {
1243 shared = alloc_arraycache(node,
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001244 cachep->shared * cachep->batchcount,
1245 0xbaadf00d);
Eric Dumazet63109842007-05-06 14:49:28 -07001246 if (!shared)
1247 goto bad;
1248 }
Paul Menage3395ee02006-12-06 20:32:16 -08001249 if (use_alien_caches) {
1250 alien = alloc_alien_cache(node, cachep->limit);
1251 if (!alien)
1252 goto bad;
1253 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 cachep->array[cpu] = nc;
Christoph Lametere498be72005-09-09 13:03:32 -07001255 l3 = cachep->nodelists[node];
1256 BUG_ON(!l3);
Christoph Lametere498be72005-09-09 13:03:32 -07001257
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001258 spin_lock_irq(&l3->list_lock);
1259 if (!l3->shared) {
1260 /*
1261 * We are serialised from CPU_DEAD or
1262 * CPU_UP_CANCELLED by the cpucontrol lock
1263 */
1264 l3->shared = shared;
1265 shared = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07001266 }
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001267#ifdef CONFIG_NUMA
1268 if (!l3->alien) {
1269 l3->alien = alien;
1270 alien = NULL;
1271 }
1272#endif
1273 spin_unlock_irq(&l3->list_lock);
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001274 kfree(shared);
1275 free_alien_cache(alien);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 break;
1278 case CPU_ONLINE:
1279 start_cpu_timer(cpu);
1280 break;
1281#ifdef CONFIG_HOTPLUG_CPU
Christoph Lameter5830c592007-05-09 02:34:22 -07001282 case CPU_DOWN_PREPARE:
1283 /*
1284 * Shutdown cache reaper. Note that the cache_chain_mutex is
1285 * held so that if cache_reap() is invoked it cannot do
1286 * anything expensive but will only modify reap_work
1287 * and reschedule the timer.
1288 */
1289 cancel_rearming_delayed_work(&per_cpu(reap_work, cpu));
1290 /* Now the cache_reaper is guaranteed to be not running. */
1291 per_cpu(reap_work, cpu).work.func = NULL;
1292 break;
1293 case CPU_DOWN_FAILED:
1294 start_cpu_timer(cpu);
1295 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 case CPU_DEAD:
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001297 /*
1298 * Even if all the cpus of a node are down, we don't free the
1299 * kmem_list3 of any cache. This to avoid a race between
1300 * cpu_down, and a kmalloc allocation from another cpu for
1301 * memory from the node of the cpu going down. The list3
1302 * structure is usually allocated from kmem_cache_create() and
1303 * gets destroyed at kmem_cache_destroy().
1304 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 /* fall thru */
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08001306#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 case CPU_UP_CANCELED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 list_for_each_entry(cachep, &cache_chain, next) {
1309 struct array_cache *nc;
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001310 struct array_cache *shared;
1311 struct array_cache **alien;
Christoph Lametere498be72005-09-09 13:03:32 -07001312 cpumask_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313
Christoph Lametere498be72005-09-09 13:03:32 -07001314 mask = node_to_cpumask(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 /* cpu is dead; no one can alloc from it. */
1316 nc = cachep->array[cpu];
1317 cachep->array[cpu] = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07001318 l3 = cachep->nodelists[node];
1319
1320 if (!l3)
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001321 goto free_array_cache;
Christoph Lametere498be72005-09-09 13:03:32 -07001322
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08001323 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07001324
1325 /* Free limit for this kmem_list3 */
1326 l3->free_limit -= cachep->batchcount;
1327 if (nc)
Christoph Lameterff694162005-09-22 21:44:02 -07001328 free_block(cachep, nc->entry, nc->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001329
1330 if (!cpus_empty(mask)) {
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08001331 spin_unlock_irq(&l3->list_lock);
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001332 goto free_array_cache;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001333 }
Christoph Lametere498be72005-09-09 13:03:32 -07001334
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001335 shared = l3->shared;
1336 if (shared) {
Eric Dumazet63109842007-05-06 14:49:28 -07001337 free_block(cachep, shared->entry,
1338 shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001339 l3->shared = NULL;
1340 }
Christoph Lametere498be72005-09-09 13:03:32 -07001341
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001342 alien = l3->alien;
1343 l3->alien = NULL;
1344
1345 spin_unlock_irq(&l3->list_lock);
1346
1347 kfree(shared);
1348 if (alien) {
1349 drain_alien_cache(cachep, alien);
1350 free_alien_cache(alien);
Christoph Lametere498be72005-09-09 13:03:32 -07001351 }
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001352free_array_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 kfree(nc);
1354 }
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001355 /*
1356 * In the previous loop, all the objects were freed to
1357 * the respective cache's slabs, now we can go ahead and
1358 * shrink each nodelist to its limit.
1359 */
1360 list_for_each_entry(cachep, &cache_chain, next) {
1361 l3 = cachep->nodelists[node];
1362 if (!l3)
1363 continue;
Christoph Lametered11d9e2006-06-30 01:55:45 -07001364 drain_freelist(cachep, l3, l3->free_objects);
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001365 }
Heiko Carstens38c3bd92007-05-09 02:34:05 -07001366 break;
1367 case CPU_LOCK_RELEASE:
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001368 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
1371 return NOTIFY_OK;
Andrew Mortona737b3e2006-03-22 00:08:11 -08001372bad:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return NOTIFY_BAD;
1374}
1375
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001376static struct notifier_block __cpuinitdata cpucache_notifier = {
1377 &cpuup_callback, NULL, 0
1378};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
Christoph Lametere498be72005-09-09 13:03:32 -07001380/*
1381 * swap the static kmem_list3 with kmalloced memory
1382 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001383static void init_list(struct kmem_cache *cachep, struct kmem_list3 *list,
1384 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07001385{
1386 struct kmem_list3 *ptr;
1387
Christoph Lametere498be72005-09-09 13:03:32 -07001388 ptr = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, nodeid);
1389 BUG_ON(!ptr);
1390
1391 local_irq_disable();
1392 memcpy(ptr, list, sizeof(struct kmem_list3));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001393 /*
1394 * Do not assume that spinlocks can be initialized via memcpy:
1395 */
1396 spin_lock_init(&ptr->list_lock);
1397
Christoph Lametere498be72005-09-09 13:03:32 -07001398 MAKE_ALL_LISTS(cachep, ptr, nodeid);
1399 cachep->nodelists[nodeid] = ptr;
1400 local_irq_enable();
1401}
1402
Andrew Mortona737b3e2006-03-22 00:08:11 -08001403/*
1404 * Initialisation. Called after the page allocator have been initialised and
1405 * before smp_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 */
1407void __init kmem_cache_init(void)
1408{
1409 size_t left_over;
1410 struct cache_sizes *sizes;
1411 struct cache_names *names;
Christoph Lametere498be72005-09-09 13:03:32 -07001412 int i;
Jack Steiner07ed76b2006-03-07 21:55:46 -08001413 int order;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001414 int node;
Christoph Lametere498be72005-09-09 13:03:32 -07001415
Siddha, Suresh B62918a02007-05-02 19:27:18 +02001416 if (num_possible_nodes() == 1)
1417 use_alien_caches = 0;
1418
Christoph Lametere498be72005-09-09 13:03:32 -07001419 for (i = 0; i < NUM_INIT_LISTS; i++) {
1420 kmem_list3_init(&initkmem_list3[i]);
1421 if (i < MAX_NUMNODES)
1422 cache_cache.nodelists[i] = NULL;
1423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425 /*
1426 * Fragmentation resistance on low memory - only use bigger
1427 * page orders on machines with more than 32MB of memory.
1428 */
1429 if (num_physpages > (32 << 20) >> PAGE_SHIFT)
1430 slab_break_gfp_order = BREAK_GFP_ORDER_HI;
1431
Linus Torvalds1da177e2005-04-16 15:20:36 -07001432 /* Bootstrap is tricky, because several objects are allocated
1433 * from caches that do not exist yet:
Andrew Mortona737b3e2006-03-22 00:08:11 -08001434 * 1) initialize the cache_cache cache: it contains the struct
1435 * kmem_cache structures of all caches, except cache_cache itself:
1436 * cache_cache is statically allocated.
Christoph Lametere498be72005-09-09 13:03:32 -07001437 * Initially an __init data area is used for the head array and the
1438 * kmem_list3 structures, it's replaced with a kmalloc allocated
1439 * array at the end of the bootstrap.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 * 2) Create the first kmalloc cache.
Pekka Enberg343e0d72006-02-01 03:05:50 -08001441 * The struct kmem_cache for the new cache is allocated normally.
Christoph Lametere498be72005-09-09 13:03:32 -07001442 * An __init data area is used for the head array.
1443 * 3) Create the remaining kmalloc caches, with minimally sized
1444 * head arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 * 4) Replace the __init data head arrays for cache_cache and the first
1446 * kmalloc cache with kmalloc allocated arrays.
Christoph Lametere498be72005-09-09 13:03:32 -07001447 * 5) Replace the __init data for kmem_list3 for cache_cache and
1448 * the other cache's with kmalloc allocated memory.
1449 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 */
1451
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001452 node = numa_node_id();
1453
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 /* 1) create the cache_cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 INIT_LIST_HEAD(&cache_chain);
1456 list_add(&cache_cache.next, &cache_chain);
1457 cache_cache.colour_off = cache_line_size();
1458 cache_cache.array[smp_processor_id()] = &initarray_cache.cache;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001459 cache_cache.nodelists[node] = &initkmem_list3[CACHE_CACHE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460
Eric Dumazet8da34302007-05-06 14:49:29 -07001461 /*
1462 * struct kmem_cache size depends on nr_node_ids, which
1463 * can be less than MAX_NUMNODES.
1464 */
1465 cache_cache.buffer_size = offsetof(struct kmem_cache, nodelists) +
1466 nr_node_ids * sizeof(struct kmem_list3 *);
1467#if DEBUG
1468 cache_cache.obj_size = cache_cache.buffer_size;
1469#endif
Andrew Mortona737b3e2006-03-22 00:08:11 -08001470 cache_cache.buffer_size = ALIGN(cache_cache.buffer_size,
1471 cache_line_size());
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08001472 cache_cache.reciprocal_buffer_size =
1473 reciprocal_value(cache_cache.buffer_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474
Jack Steiner07ed76b2006-03-07 21:55:46 -08001475 for (order = 0; order < MAX_ORDER; order++) {
1476 cache_estimate(order, cache_cache.buffer_size,
1477 cache_line_size(), 0, &left_over, &cache_cache.num);
1478 if (cache_cache.num)
1479 break;
1480 }
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02001481 BUG_ON(!cache_cache.num);
Jack Steiner07ed76b2006-03-07 21:55:46 -08001482 cache_cache.gfporder = order;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001483 cache_cache.colour = left_over / cache_cache.colour_off;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001484 cache_cache.slab_size = ALIGN(cache_cache.num * sizeof(kmem_bufctl_t) +
1485 sizeof(struct slab), cache_line_size());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486
1487 /* 2+3) create the kmalloc caches */
1488 sizes = malloc_sizes;
1489 names = cache_names;
1490
Andrew Mortona737b3e2006-03-22 00:08:11 -08001491 /*
1492 * Initialize the caches that provide memory for the array cache and the
1493 * kmem_list3 structures first. Without this, further allocations will
1494 * bug.
Christoph Lametere498be72005-09-09 13:03:32 -07001495 */
1496
1497 sizes[INDEX_AC].cs_cachep = kmem_cache_create(names[INDEX_AC].name,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001498 sizes[INDEX_AC].cs_size,
1499 ARCH_KMALLOC_MINALIGN,
1500 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
1501 NULL, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07001502
Andrew Mortona737b3e2006-03-22 00:08:11 -08001503 if (INDEX_AC != INDEX_L3) {
Christoph Lametere498be72005-09-09 13:03:32 -07001504 sizes[INDEX_L3].cs_cachep =
Andrew Mortona737b3e2006-03-22 00:08:11 -08001505 kmem_cache_create(names[INDEX_L3].name,
1506 sizes[INDEX_L3].cs_size,
1507 ARCH_KMALLOC_MINALIGN,
1508 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
1509 NULL, NULL);
1510 }
Christoph Lametere498be72005-09-09 13:03:32 -07001511
Ingo Molnare0a42722006-06-23 02:03:46 -07001512 slab_early_init = 0;
1513
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 while (sizes->cs_size != ULONG_MAX) {
Christoph Lametere498be72005-09-09 13:03:32 -07001515 /*
1516 * For performance, all the general caches are L1 aligned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 * This should be particularly beneficial on SMP boxes, as it
1518 * eliminates "false sharing".
1519 * Note for systems short on memory removing the alignment will
Christoph Lametere498be72005-09-09 13:03:32 -07001520 * allow tighter packing of the smaller caches.
1521 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001522 if (!sizes->cs_cachep) {
Christoph Lametere498be72005-09-09 13:03:32 -07001523 sizes->cs_cachep = kmem_cache_create(names->name,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001524 sizes->cs_size,
1525 ARCH_KMALLOC_MINALIGN,
1526 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
1527 NULL, NULL);
1528 }
Christoph Lameter4b51d662007-02-10 01:43:10 -08001529#ifdef CONFIG_ZONE_DMA
1530 sizes->cs_dmacachep = kmem_cache_create(
1531 names->name_dma,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001532 sizes->cs_size,
1533 ARCH_KMALLOC_MINALIGN,
1534 ARCH_KMALLOC_FLAGS|SLAB_CACHE_DMA|
1535 SLAB_PANIC,
1536 NULL, NULL);
Christoph Lameter4b51d662007-02-10 01:43:10 -08001537#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 sizes++;
1539 names++;
1540 }
1541 /* 4) Replace the bootstrap head arrays */
1542 {
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001543 struct array_cache *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
Christoph Lametere498be72005-09-09 13:03:32 -07001546
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 local_irq_disable();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001548 BUG_ON(cpu_cache_get(&cache_cache) != &initarray_cache.cache);
1549 memcpy(ptr, cpu_cache_get(&cache_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001550 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001551 /*
1552 * Do not assume that spinlocks can be initialized via memcpy:
1553 */
1554 spin_lock_init(&ptr->lock);
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 cache_cache.array[smp_processor_id()] = ptr;
1557 local_irq_enable();
Christoph Lametere498be72005-09-09 13:03:32 -07001558
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
Christoph Lametere498be72005-09-09 13:03:32 -07001560
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561 local_irq_disable();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001562 BUG_ON(cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001563 != &initarray_generic.cache);
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001564 memcpy(ptr, cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001565 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001566 /*
1567 * Do not assume that spinlocks can be initialized via memcpy:
1568 */
1569 spin_lock_init(&ptr->lock);
1570
Christoph Lametere498be72005-09-09 13:03:32 -07001571 malloc_sizes[INDEX_AC].cs_cachep->array[smp_processor_id()] =
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001572 ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 local_irq_enable();
1574 }
Christoph Lametere498be72005-09-09 13:03:32 -07001575 /* 5) Replace the bootstrap kmem_list3's */
1576 {
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001577 int nid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001579 /* Replace the static kmem_list3 structures for the boot cpu */
1580 init_list(&cache_cache, &initkmem_list3[CACHE_CACHE], node);
1581
1582 for_each_online_node(nid) {
Christoph Lametere498be72005-09-09 13:03:32 -07001583 init_list(malloc_sizes[INDEX_AC].cs_cachep,
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001584 &initkmem_list3[SIZE_AC + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001585
1586 if (INDEX_AC != INDEX_L3) {
1587 init_list(malloc_sizes[INDEX_L3].cs_cachep,
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07001588 &initkmem_list3[SIZE_L3 + nid], nid);
Christoph Lametere498be72005-09-09 13:03:32 -07001589 }
1590 }
1591 }
1592
1593 /* 6) resize the head arrays to their final sizes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 {
Pekka Enberg343e0d72006-02-01 03:05:50 -08001595 struct kmem_cache *cachep;
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001596 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 list_for_each_entry(cachep, &cache_chain, next)
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07001598 if (enable_cpucache(cachep))
1599 BUG();
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001600 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
1602
Ravikiran G Thirumalai056c6242006-09-25 23:31:38 -07001603 /* Annotate slab for lockdep -- annotate the malloc caches */
1604 init_lock_keys();
1605
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607 /* Done! */
1608 g_cpucache_up = FULL;
1609
Andrew Mortona737b3e2006-03-22 00:08:11 -08001610 /*
1611 * Register a cpu startup notifier callback that initializes
1612 * cpu_cache_get for all new cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 */
1614 register_cpu_notifier(&cpucache_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Andrew Mortona737b3e2006-03-22 00:08:11 -08001616 /*
1617 * The reap timers are started later, with a module init call: That part
1618 * of the kernel is not yet operational.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 */
1620}
1621
1622static int __init cpucache_init(void)
1623{
1624 int cpu;
1625
Andrew Mortona737b3e2006-03-22 00:08:11 -08001626 /*
1627 * Register the timers that return unneeded pages to the page allocator
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 */
Christoph Lametere498be72005-09-09 13:03:32 -07001629 for_each_online_cpu(cpu)
Andrew Mortona737b3e2006-03-22 00:08:11 -08001630 start_cpu_timer(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 return 0;
1632}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633__initcall(cpucache_init);
1634
1635/*
1636 * Interface to system's page allocator. No need to hold the cache-lock.
1637 *
1638 * If we requested dmaable memory, we will get it. Even if we
1639 * did not request dmaable memory, we might get it, but that
1640 * would be relatively rare and ignorable.
1641 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001642static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643{
1644 struct page *page;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001645 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 int i;
1647
Luke Yangd6fef9d2006-04-10 22:52:56 -07001648#ifndef CONFIG_MMU
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001649 /*
1650 * Nommu uses slab's for process anonymous memory allocations, and thus
1651 * requires __GFP_COMP to properly refcount higher order allocations
Luke Yangd6fef9d2006-04-10 22:52:56 -07001652 */
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001653 flags |= __GFP_COMP;
Luke Yangd6fef9d2006-04-10 22:52:56 -07001654#endif
Christoph Lameter765c4502006-09-27 01:50:08 -07001655
Christoph Lameter3c517a62006-12-06 20:33:29 -08001656 flags |= cachep->gfpflags;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001657
1658 page = alloc_pages_node(nodeid, flags, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 if (!page)
1660 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001662 nr_pages = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
Christoph Lameter972d1a72006-09-25 23:31:51 -07001664 add_zone_page_state(page_zone(page),
1665 NR_SLAB_RECLAIMABLE, nr_pages);
1666 else
1667 add_zone_page_state(page_zone(page),
1668 NR_SLAB_UNRECLAIMABLE, nr_pages);
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001669 for (i = 0; i < nr_pages; i++)
1670 __SetPageSlab(page + i);
1671 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672}
1673
1674/*
1675 * Interface to system's page release.
1676 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001677static void kmem_freepages(struct kmem_cache *cachep, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001679 unsigned long i = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 struct page *page = virt_to_page(addr);
1681 const unsigned long nr_freed = i;
1682
Christoph Lameter972d1a72006-09-25 23:31:51 -07001683 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1684 sub_zone_page_state(page_zone(page),
1685 NR_SLAB_RECLAIMABLE, nr_freed);
1686 else
1687 sub_zone_page_state(page_zone(page),
1688 NR_SLAB_UNRECLAIMABLE, nr_freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 while (i--) {
Nick Pigginf205b2f2006-03-22 00:08:02 -08001690 BUG_ON(!PageSlab(page));
1691 __ClearPageSlab(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 page++;
1693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (current->reclaim_state)
1695 current->reclaim_state->reclaimed_slab += nr_freed;
1696 free_pages((unsigned long)addr, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697}
1698
1699static void kmem_rcu_free(struct rcu_head *head)
1700{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001701 struct slab_rcu *slab_rcu = (struct slab_rcu *)head;
Pekka Enberg343e0d72006-02-01 03:05:50 -08001702 struct kmem_cache *cachep = slab_rcu->cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 kmem_freepages(cachep, slab_rcu->addr);
1705 if (OFF_SLAB(cachep))
1706 kmem_cache_free(cachep->slabp_cache, slab_rcu);
1707}
1708
1709#if DEBUG
1710
1711#ifdef CONFIG_DEBUG_PAGEALLOC
Pekka Enberg343e0d72006-02-01 03:05:50 -08001712static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001713 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001715 int size = obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001717 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001719 if (size < 5 * sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 return;
1721
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001722 *addr++ = 0x12345678;
1723 *addr++ = caller;
1724 *addr++ = smp_processor_id();
1725 size -= 3 * sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 {
1727 unsigned long *sptr = &caller;
1728 unsigned long svalue;
1729
1730 while (!kstack_end(sptr)) {
1731 svalue = *sptr++;
1732 if (kernel_text_address(svalue)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001733 *addr++ = svalue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 size -= sizeof(unsigned long);
1735 if (size <= sizeof(unsigned long))
1736 break;
1737 }
1738 }
1739
1740 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001741 *addr++ = 0x87654321;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742}
1743#endif
1744
Pekka Enberg343e0d72006-02-01 03:05:50 -08001745static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001747 int size = obj_size(cachep);
1748 addr = &((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
1750 memset(addr, val, size);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001751 *(unsigned char *)(addr + size - 1) = POISON_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
1754static void dump_line(char *data, int offset, int limit)
1755{
1756 int i;
Dave Jonesaa83aa42006-09-29 01:59:51 -07001757 unsigned char error = 0;
1758 int bad_count = 0;
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 printk(KERN_ERR "%03x:", offset);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001761 for (i = 0; i < limit; i++) {
1762 if (data[offset + i] != POISON_FREE) {
1763 error = data[offset + i];
1764 bad_count++;
1765 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001766 printk(" %02x", (unsigned char)data[offset + i]);
Dave Jonesaa83aa42006-09-29 01:59:51 -07001767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 printk("\n");
Dave Jonesaa83aa42006-09-29 01:59:51 -07001769
1770 if (bad_count == 1) {
1771 error ^= POISON_FREE;
1772 if (!(error & (error - 1))) {
1773 printk(KERN_ERR "Single bit error detected. Probably "
1774 "bad RAM.\n");
1775#ifdef CONFIG_X86
1776 printk(KERN_ERR "Run memtest86+ or a similar memory "
1777 "test tool.\n");
1778#else
1779 printk(KERN_ERR "Run a memory test tool.\n");
1780#endif
1781 }
1782 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783}
1784#endif
1785
1786#if DEBUG
1787
Pekka Enberg343e0d72006-02-01 03:05:50 -08001788static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
1790 int i, size;
1791 char *realobj;
1792
1793 if (cachep->flags & SLAB_RED_ZONE) {
David Woodhouseb46b8f12007-05-08 00:22:59 -07001794 printk(KERN_ERR "Redzone: 0x%llx/0x%llx.\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001795 *dbg_redzone1(cachep, objp),
1796 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 }
1798
1799 if (cachep->flags & SLAB_STORE_USER) {
1800 printk(KERN_ERR "Last user: [<%p>]",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001801 *dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 print_symbol("(%s)",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001803 (unsigned long)*dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 printk("\n");
1805 }
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001806 realobj = (char *)objp + obj_offset(cachep);
1807 size = obj_size(cachep);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001808 for (i = 0; i < size && lines; i += 16, lines--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 int limit;
1810 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001811 if (i + limit > size)
1812 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 dump_line(realobj, i, limit);
1814 }
1815}
1816
Pekka Enberg343e0d72006-02-01 03:05:50 -08001817static void check_poison_obj(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
1819 char *realobj;
1820 int size, i;
1821 int lines = 0;
1822
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001823 realobj = (char *)objp + obj_offset(cachep);
1824 size = obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001826 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 char exp = POISON_FREE;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001828 if (i == size - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 exp = POISON_END;
1830 if (realobj[i] != exp) {
1831 int limit;
1832 /* Mismatch ! */
1833 /* Print header */
1834 if (lines == 0) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001835 printk(KERN_ERR
David Howellse94a40c2007-04-02 23:46:28 +01001836 "Slab corruption: %s start=%p, len=%d\n",
1837 cachep->name, realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 print_objinfo(cachep, objp, 0);
1839 }
1840 /* Hexdump the affected line */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001841 i = (i / 16) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001843 if (i + limit > size)
1844 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 dump_line(realobj, i, limit);
1846 i += 16;
1847 lines++;
1848 /* Limit to 5 lines */
1849 if (lines > 5)
1850 break;
1851 }
1852 }
1853 if (lines != 0) {
1854 /* Print some data about the neighboring objects, if they
1855 * exist:
1856 */
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08001857 struct slab *slabp = virt_to_slab(objp);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001858 unsigned int objnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001860 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 if (objnr) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001862 objp = index_to_obj(cachep, slabp, objnr - 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001863 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001865 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 print_objinfo(cachep, objp, 2);
1867 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001868 if (objnr + 1 < cachep->num) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001869 objp = index_to_obj(cachep, slabp, objnr + 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001870 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001872 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 print_objinfo(cachep, objp, 2);
1874 }
1875 }
1876}
1877#endif
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879#if DEBUG
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001880/**
Randy Dunlap911851e2006-03-22 00:08:14 -08001881 * slab_destroy_objs - destroy a slab and its objects
1882 * @cachep: cache pointer being destroyed
1883 * @slabp: slab pointer being destroyed
1884 *
1885 * Call the registered destructor for each object in a slab that is being
1886 * destroyed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001887 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001888static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001889{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 int i;
1891 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001892 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893
1894 if (cachep->flags & SLAB_POISON) {
1895#ifdef CONFIG_DEBUG_PAGEALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -08001896 if (cachep->buffer_size % PAGE_SIZE == 0 &&
1897 OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001898 kernel_map_pages(virt_to_page(objp),
Andrew Mortona737b3e2006-03-22 00:08:11 -08001899 cachep->buffer_size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 else
1901 check_poison_obj(cachep, objp);
1902#else
1903 check_poison_obj(cachep, objp);
1904#endif
1905 }
1906 if (cachep->flags & SLAB_RED_ZONE) {
1907 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
1908 slab_error(cachep, "start of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001909 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
1911 slab_error(cachep, "end of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001912 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
1914 if (cachep->dtor && !(cachep->flags & SLAB_POISON))
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001915 (cachep->dtor) (objp + obj_offset(cachep), cachep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001917}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918#else
Pekka Enberg343e0d72006-02-01 03:05:50 -08001919static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001920{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 if (cachep->dtor) {
1922 int i;
1923 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001924 void *objp = index_to_obj(cachep, slabp, i);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001925 (cachep->dtor) (objp, cachep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
1927 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001928}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929#endif
1930
Randy Dunlap911851e2006-03-22 00:08:14 -08001931/**
1932 * slab_destroy - destroy and release all objects in a slab
1933 * @cachep: cache pointer being destroyed
1934 * @slabp: slab pointer being destroyed
1935 *
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001936 * Destroy all the objs in a slab, and release the mem back to the system.
Andrew Mortona737b3e2006-03-22 00:08:11 -08001937 * Before calling the slab must have been unlinked from the cache. The
1938 * cache-lock is not held/needed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001939 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001940static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001941{
1942 void *addr = slabp->s_mem - slabp->colouroff;
1943
1944 slab_destroy_objs(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
1946 struct slab_rcu *slab_rcu;
1947
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001948 slab_rcu = (struct slab_rcu *)slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 slab_rcu->cachep = cachep;
1950 slab_rcu->addr = addr;
1951 call_rcu(&slab_rcu->head, kmem_rcu_free);
1952 } else {
1953 kmem_freepages(cachep, addr);
Ingo Molnar873623d2006-07-13 14:44:38 +02001954 if (OFF_SLAB(cachep))
1955 kmem_cache_free(cachep->slabp_cache, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 }
1957}
1958
Andrew Mortona737b3e2006-03-22 00:08:11 -08001959/*
1960 * For setting up all the kmem_list3s for cache whose buffer_size is same as
1961 * size of kmem_list3.
1962 */
Andrew Mortona3a02be2007-05-06 14:49:31 -07001963static void __init set_up_list3s(struct kmem_cache *cachep, int index)
Christoph Lametere498be72005-09-09 13:03:32 -07001964{
1965 int node;
1966
1967 for_each_online_node(node) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001968 cachep->nodelists[node] = &initkmem_list3[index + node];
Christoph Lametere498be72005-09-09 13:03:32 -07001969 cachep->nodelists[node]->next_reap = jiffies +
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001970 REAPTIMEOUT_LIST3 +
1971 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lametere498be72005-09-09 13:03:32 -07001972 }
1973}
1974
Christoph Lameter117f6eb2006-09-25 23:31:37 -07001975static void __kmem_cache_destroy(struct kmem_cache *cachep)
1976{
1977 int i;
1978 struct kmem_list3 *l3;
1979
1980 for_each_online_cpu(i)
1981 kfree(cachep->array[i]);
1982
1983 /* NUMA: free the list3 structures */
1984 for_each_online_node(i) {
1985 l3 = cachep->nodelists[i];
1986 if (l3) {
1987 kfree(l3->shared);
1988 free_alien_cache(l3->alien);
1989 kfree(l3);
1990 }
1991 }
1992 kmem_cache_free(&cache_cache, cachep);
1993}
1994
1995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996/**
Randy.Dunlapa70773d2006-02-01 03:05:52 -08001997 * calculate_slab_order - calculate size (page order) of slabs
1998 * @cachep: pointer to the cache that is being created
1999 * @size: size of objects to be created in this cache.
2000 * @align: required alignment for the objects.
2001 * @flags: slab allocation flags
2002 *
2003 * Also calculates the number of objects per slab.
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002004 *
2005 * This could be made much more intelligent. For now, try to avoid using
2006 * high order pages for slabs. When the gfp() functions are more friendly
2007 * towards high-order requests, this should be changed.
2008 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002009static size_t calculate_slab_order(struct kmem_cache *cachep,
Randy Dunlapee13d782006-02-01 03:05:53 -08002010 size_t size, size_t align, unsigned long flags)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002011{
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002012 unsigned long offslab_limit;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002013 size_t left_over = 0;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002014 int gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002015
Andrew Mortona737b3e2006-03-22 00:08:11 -08002016 for (gfporder = 0; gfporder <= MAX_GFP_ORDER; gfporder++) {
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002017 unsigned int num;
2018 size_t remainder;
2019
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002020 cache_estimate(gfporder, size, align, flags, &remainder, &num);
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002021 if (!num)
2022 continue;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002023
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02002024 if (flags & CFLGS_OFF_SLAB) {
2025 /*
2026 * Max number of objs-per-slab for caches which
2027 * use off-slab slabs. Needed to avoid a possible
2028 * looping condition in cache_grow().
2029 */
2030 offslab_limit = size - sizeof(struct slab);
2031 offslab_limit /= sizeof(kmem_bufctl_t);
2032
2033 if (num > offslab_limit)
2034 break;
2035 }
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002036
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002037 /* Found something acceptable - save it away */
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002038 cachep->num = num;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002039 cachep->gfporder = gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002040 left_over = remainder;
2041
2042 /*
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08002043 * A VFS-reclaimable slab tends to have most allocations
2044 * as GFP_NOFS and we really don't want to have to be allocating
2045 * higher-order pages when we are unable to shrink dcache.
2046 */
2047 if (flags & SLAB_RECLAIM_ACCOUNT)
2048 break;
2049
2050 /*
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002051 * Large number of objects is good, but very large slabs are
2052 * currently bad for the gfp()s.
2053 */
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002054 if (gfporder >= slab_break_gfp_order)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002055 break;
2056
Linus Torvalds9888e6f2006-03-06 17:44:43 -08002057 /*
2058 * Acceptable internal fragmentation?
2059 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002060 if (left_over * 8 <= (PAGE_SIZE << gfporder))
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002061 break;
2062 }
2063 return left_over;
2064}
2065
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002066static int setup_cpu_cache(struct kmem_cache *cachep)
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002067{
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002068 if (g_cpucache_up == FULL)
2069 return enable_cpucache(cachep);
2070
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002071 if (g_cpucache_up == NONE) {
2072 /*
2073 * Note: the first kmem_cache_create must create the cache
2074 * that's used by kmalloc(24), otherwise the creation of
2075 * further caches will BUG().
2076 */
2077 cachep->array[smp_processor_id()] = &initarray_generic.cache;
2078
2079 /*
2080 * If the cache that's used by kmalloc(sizeof(kmem_list3)) is
2081 * the first cache, then we need to set up all its list3s,
2082 * otherwise the creation of further caches will BUG().
2083 */
2084 set_up_list3s(cachep, SIZE_AC);
2085 if (INDEX_AC == INDEX_L3)
2086 g_cpucache_up = PARTIAL_L3;
2087 else
2088 g_cpucache_up = PARTIAL_AC;
2089 } else {
2090 cachep->array[smp_processor_id()] =
2091 kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
2092
2093 if (g_cpucache_up == PARTIAL_AC) {
2094 set_up_list3s(cachep, SIZE_L3);
2095 g_cpucache_up = PARTIAL_L3;
2096 } else {
2097 int node;
2098 for_each_online_node(node) {
2099 cachep->nodelists[node] =
2100 kmalloc_node(sizeof(struct kmem_list3),
2101 GFP_KERNEL, node);
2102 BUG_ON(!cachep->nodelists[node]);
2103 kmem_list3_init(cachep->nodelists[node]);
2104 }
2105 }
2106 }
2107 cachep->nodelists[numa_node_id()]->next_reap =
2108 jiffies + REAPTIMEOUT_LIST3 +
2109 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
2110
2111 cpu_cache_get(cachep)->avail = 0;
2112 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
2113 cpu_cache_get(cachep)->batchcount = 1;
2114 cpu_cache_get(cachep)->touched = 0;
2115 cachep->batchcount = 1;
2116 cachep->limit = BOOT_CPUCACHE_ENTRIES;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002117 return 0;
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002118}
2119
Pekka Enberg4d268eb2006-01-08 01:00:36 -08002120/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 * kmem_cache_create - Create a cache.
2122 * @name: A string which is used in /proc/slabinfo to identify this cache.
2123 * @size: The size of objects to be created in this cache.
2124 * @align: The required alignment for the objects.
2125 * @flags: SLAB flags
2126 * @ctor: A constructor for the objects.
2127 * @dtor: A destructor for the objects.
2128 *
2129 * Returns a ptr to the cache on success, NULL on failure.
2130 * Cannot be called within a int, but can be interrupted.
2131 * The @ctor is run when new pages are allocated by the cache
2132 * and the @dtor is run before the pages are handed back.
2133 *
2134 * @name must be valid until the cache is destroyed. This implies that
Andrew Mortona737b3e2006-03-22 00:08:11 -08002135 * the module calling this has to destroy the cache before getting unloaded.
2136 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 * The flags are
2138 *
2139 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
2140 * to catch references to uninitialised memory.
2141 *
2142 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
2143 * for buffer overruns.
2144 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
2146 * cacheline. This can be beneficial if you're counting cycles as closely
2147 * as davem.
2148 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002149struct kmem_cache *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150kmem_cache_create (const char *name, size_t size, size_t align,
Andrew Mortona737b3e2006-03-22 00:08:11 -08002151 unsigned long flags,
2152 void (*ctor)(void*, struct kmem_cache *, unsigned long),
Pekka Enberg343e0d72006-02-01 03:05:50 -08002153 void (*dtor)(void*, struct kmem_cache *, unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154{
2155 size_t left_over, slab_size, ralign;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07002156 struct kmem_cache *cachep = NULL, *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 /*
2159 * Sanity checks... these are all serious usage bugs.
2160 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002161 if (!name || in_interrupt() || (size < BYTES_PER_WORD) ||
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002162 (size > (1 << MAX_OBJ_ORDER) * PAGE_SIZE) || (dtor && !ctor)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002163 printk(KERN_ERR "%s: Early error in slab %s\n", __FUNCTION__,
2164 name);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002165 BUG();
2166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
Ravikiran G Thirumalaif0188f42006-02-10 01:51:13 -08002168 /*
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002169 * We use cache_chain_mutex to ensure a consistent view of
2170 * cpu_online_map as well. Please see cpuup_callback
Ravikiran G Thirumalaif0188f42006-02-10 01:51:13 -08002171 */
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002172 mutex_lock(&cache_chain_mutex);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002173
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07002174 list_for_each_entry(pc, &cache_chain, next) {
Andrew Morton4f12bb42005-11-07 00:58:00 -08002175 char tmp;
2176 int res;
2177
2178 /*
2179 * This happens when the module gets unloaded and doesn't
2180 * destroy its slab cache and no-one else reuses the vmalloc
2181 * area of the module. Print a warning.
2182 */
Andrew Morton138ae662006-12-06 20:36:41 -08002183 res = probe_kernel_address(pc->name, tmp);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002184 if (res) {
matzeb4169522007-05-06 14:49:52 -07002185 printk(KERN_ERR
2186 "SLAB: cache with size %d has lost its name\n",
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002187 pc->buffer_size);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002188 continue;
2189 }
2190
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002191 if (!strcmp(pc->name, name)) {
matzeb4169522007-05-06 14:49:52 -07002192 printk(KERN_ERR
2193 "kmem_cache_create: duplicate cache %s\n", name);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002194 dump_stack();
2195 goto oops;
2196 }
2197 }
2198
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199#if DEBUG
2200 WARN_ON(strchr(name, ' ')); /* It confuses parsers */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201#if FORCED_DEBUG
2202 /*
2203 * Enable redzoning and last user accounting, except for caches with
2204 * large objects, if the increased size would increase the object size
2205 * above the next power of two: caches with object sizes just above a
2206 * power of two have a significant amount of internal fragmentation.
2207 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002208 if (size < 4096 || fls(size - 1) == fls(size-1 + 3 * BYTES_PER_WORD))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002209 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 if (!(flags & SLAB_DESTROY_BY_RCU))
2211 flags |= SLAB_POISON;
2212#endif
2213 if (flags & SLAB_DESTROY_BY_RCU)
2214 BUG_ON(flags & SLAB_POISON);
2215#endif
2216 if (flags & SLAB_DESTROY_BY_RCU)
2217 BUG_ON(dtor);
2218
2219 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002220 * Always checks flags, a caller might be expecting debug support which
2221 * isn't available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 */
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002223 BUG_ON(flags & ~CREATE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Andrew Mortona737b3e2006-03-22 00:08:11 -08002225 /*
2226 * Check that size is in terms of words. This is needed to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 * unaligned accesses for some archs when redzoning is used, and makes
2228 * sure any on-slab bufctl's are also correctly aligned.
2229 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002230 if (size & (BYTES_PER_WORD - 1)) {
2231 size += (BYTES_PER_WORD - 1);
2232 size &= ~(BYTES_PER_WORD - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 }
2234
Andrew Mortona737b3e2006-03-22 00:08:11 -08002235 /* calculate the final buffer alignment: */
2236
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 /* 1) arch recommendation: can be overridden for debug */
2238 if (flags & SLAB_HWCACHE_ALIGN) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002239 /*
2240 * Default alignment: as specified by the arch code. Except if
2241 * an object is really small, then squeeze multiple objects into
2242 * one cacheline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 */
2244 ralign = cache_line_size();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002245 while (size <= ralign / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 ralign /= 2;
2247 } else {
2248 ralign = BYTES_PER_WORD;
2249 }
Pekka Enbergca5f9702006-09-25 23:31:25 -07002250
2251 /*
2252 * Redzoning and user store require word alignment. Note this will be
2253 * overridden by architecture or caller mandated alignment if either
2254 * is greater than BYTES_PER_WORD.
2255 */
2256 if (flags & SLAB_RED_ZONE || flags & SLAB_STORE_USER)
David Woodhouseb46b8f12007-05-08 00:22:59 -07002257 ralign = __alignof__(unsigned long long);
Pekka Enbergca5f9702006-09-25 23:31:25 -07002258
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002259 /* 2) arch mandated alignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 if (ralign < ARCH_SLAB_MINALIGN) {
2261 ralign = ARCH_SLAB_MINALIGN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 }
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002263 /* 3) caller mandated alignment */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 if (ralign < align) {
2265 ralign = align;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 }
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002267 /* disable debug if necessary */
David Woodhouseb46b8f12007-05-08 00:22:59 -07002268 if (ralign > __alignof__(unsigned long long))
Kevin Hilmana44b56d2006-12-06 20:32:11 -08002269 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002270 /*
Pekka Enbergca5f9702006-09-25 23:31:25 -07002271 * 4) Store it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 */
2273 align = ralign;
2274
2275 /* Get cache's description obj. */
Christoph Lametere94b1762006-12-06 20:33:17 -08002276 cachep = kmem_cache_zalloc(&cache_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 if (!cachep)
Andrew Morton4f12bb42005-11-07 00:58:00 -08002278 goto oops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279
2280#if DEBUG
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002281 cachep->obj_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Pekka Enbergca5f9702006-09-25 23:31:25 -07002283 /*
2284 * Both debugging options require word-alignment which is calculated
2285 * into align above.
2286 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 if (flags & SLAB_RED_ZONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 /* add space for red zone words */
David Woodhouseb46b8f12007-05-08 00:22:59 -07002289 cachep->obj_offset += sizeof(unsigned long long);
2290 size += 2 * sizeof(unsigned long long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 }
2292 if (flags & SLAB_STORE_USER) {
Pekka Enbergca5f9702006-09-25 23:31:25 -07002293 /* user store requires one word storage behind the end of
2294 * the real object.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 size += BYTES_PER_WORD;
2297 }
2298#if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002299 if (size >= malloc_sizes[INDEX_L3 + 1].cs_size
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002300 && cachep->obj_size > cache_line_size() && size < PAGE_SIZE) {
2301 cachep->obj_offset += PAGE_SIZE - size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 size = PAGE_SIZE;
2303 }
2304#endif
2305#endif
2306
Ingo Molnare0a42722006-06-23 02:03:46 -07002307 /*
2308 * Determine if the slab management is 'on' or 'off' slab.
2309 * (bootstrapping cannot cope with offslab caches so don't do
2310 * it too early on.)
2311 */
2312 if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 /*
2314 * Size is large, assume best to place the slab management obj
2315 * off-slab (should allow better packing of objs).
2316 */
2317 flags |= CFLGS_OFF_SLAB;
2318
2319 size = ALIGN(size, align);
2320
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08002321 left_over = calculate_slab_order(cachep, size, align, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322
2323 if (!cachep->num) {
matzeb4169522007-05-06 14:49:52 -07002324 printk(KERN_ERR
2325 "kmem_cache_create: couldn't create cache %s.\n", name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 kmem_cache_free(&cache_cache, cachep);
2327 cachep = NULL;
Andrew Morton4f12bb42005-11-07 00:58:00 -08002328 goto oops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002330 slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
2331 + sizeof(struct slab), align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
2333 /*
2334 * If the slab has been placed off-slab, and we have enough space then
2335 * move it on-slab. This is at the expense of any extra colouring.
2336 */
2337 if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
2338 flags &= ~CFLGS_OFF_SLAB;
2339 left_over -= slab_size;
2340 }
2341
2342 if (flags & CFLGS_OFF_SLAB) {
2343 /* really off slab. No need for manual alignment */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002344 slab_size =
2345 cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 }
2347
2348 cachep->colour_off = cache_line_size();
2349 /* Offset must be a multiple of the alignment. */
2350 if (cachep->colour_off < align)
2351 cachep->colour_off = align;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002352 cachep->colour = left_over / cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 cachep->slab_size = slab_size;
2354 cachep->flags = flags;
2355 cachep->gfpflags = 0;
Christoph Lameter4b51d662007-02-10 01:43:10 -08002356 if (CONFIG_ZONE_DMA_FLAG && (flags & SLAB_CACHE_DMA))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 cachep->gfpflags |= GFP_DMA;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002358 cachep->buffer_size = size;
Eric Dumazet6a2d7a92006-12-13 00:34:27 -08002359 cachep->reciprocal_buffer_size = reciprocal_value(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002361 if (flags & CFLGS_OFF_SLAB) {
Victor Fuscob2d55072005-09-10 00:26:36 -07002362 cachep->slabp_cache = kmem_find_general_cachep(slab_size, 0u);
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002363 /*
2364 * This is a possibility for one of the malloc_sizes caches.
2365 * But since we go off slab only for object size greater than
2366 * PAGE_SIZE/8, and malloc_sizes gets created in ascending order,
2367 * this should not happen at all.
2368 * But leave a BUG_ON for some lucky dude.
2369 */
2370 BUG_ON(!cachep->slabp_cache);
2371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 cachep->ctor = ctor;
2373 cachep->dtor = dtor;
2374 cachep->name = name;
2375
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07002376 if (setup_cpu_cache(cachep)) {
2377 __kmem_cache_destroy(cachep);
2378 cachep = NULL;
2379 goto oops;
2380 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 /* cache setup completed, link it into the list */
2383 list_add(&cachep->next, &cache_chain);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002384oops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 if (!cachep && (flags & SLAB_PANIC))
2386 panic("kmem_cache_create(): failed to create slab `%s'\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002387 name);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002388 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 return cachep;
2390}
2391EXPORT_SYMBOL(kmem_cache_create);
2392
2393#if DEBUG
2394static void check_irq_off(void)
2395{
2396 BUG_ON(!irqs_disabled());
2397}
2398
2399static void check_irq_on(void)
2400{
2401 BUG_ON(irqs_disabled());
2402}
2403
Pekka Enberg343e0d72006-02-01 03:05:50 -08002404static void check_spinlock_acquired(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405{
2406#ifdef CONFIG_SMP
2407 check_irq_off();
Christoph Lametere498be72005-09-09 13:03:32 -07002408 assert_spin_locked(&cachep->nodelists[numa_node_id()]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409#endif
2410}
Christoph Lametere498be72005-09-09 13:03:32 -07002411
Pekka Enberg343e0d72006-02-01 03:05:50 -08002412static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07002413{
2414#ifdef CONFIG_SMP
2415 check_irq_off();
2416 assert_spin_locked(&cachep->nodelists[node]->list_lock);
2417#endif
2418}
2419
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420#else
2421#define check_irq_off() do { } while(0)
2422#define check_irq_on() do { } while(0)
2423#define check_spinlock_acquired(x) do { } while(0)
Christoph Lametere498be72005-09-09 13:03:32 -07002424#define check_spinlock_acquired_node(x, y) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425#endif
2426
Christoph Lameteraab22072006-03-22 00:09:06 -08002427static void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
2428 struct array_cache *ac,
2429 int force, int node);
2430
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431static void do_drain(void *arg)
2432{
Andrew Mortona737b3e2006-03-22 00:08:11 -08002433 struct kmem_cache *cachep = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 struct array_cache *ac;
Christoph Lameterff694162005-09-22 21:44:02 -07002435 int node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436
2437 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002438 ac = cpu_cache_get(cachep);
Christoph Lameterff694162005-09-22 21:44:02 -07002439 spin_lock(&cachep->nodelists[node]->list_lock);
2440 free_block(cachep, ac->entry, ac->avail, node);
2441 spin_unlock(&cachep->nodelists[node]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 ac->avail = 0;
2443}
2444
Pekka Enberg343e0d72006-02-01 03:05:50 -08002445static void drain_cpu_caches(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446{
Christoph Lametere498be72005-09-09 13:03:32 -07002447 struct kmem_list3 *l3;
2448 int node;
2449
Andrew Mortona07fa392006-03-22 00:08:17 -08002450 on_each_cpu(do_drain, cachep, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 check_irq_on();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002452 for_each_online_node(node) {
Christoph Lametere498be72005-09-09 13:03:32 -07002453 l3 = cachep->nodelists[node];
Roland Dreiera4523a82006-05-15 11:41:00 -07002454 if (l3 && l3->alien)
2455 drain_alien_cache(cachep, l3->alien);
2456 }
2457
2458 for_each_online_node(node) {
2459 l3 = cachep->nodelists[node];
2460 if (l3)
Christoph Lameteraab22072006-03-22 00:09:06 -08002461 drain_array(cachep, l3, l3->shared, 1, node);
Christoph Lametere498be72005-09-09 13:03:32 -07002462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463}
2464
Christoph Lametered11d9e2006-06-30 01:55:45 -07002465/*
2466 * Remove slabs from the list of free slabs.
2467 * Specify the number of slabs to drain in tofree.
2468 *
2469 * Returns the actual number of slabs released.
2470 */
2471static int drain_freelist(struct kmem_cache *cache,
2472 struct kmem_list3 *l3, int tofree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
Christoph Lametered11d9e2006-06-30 01:55:45 -07002474 struct list_head *p;
2475 int nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477
Christoph Lametered11d9e2006-06-30 01:55:45 -07002478 nr_freed = 0;
2479 while (nr_freed < tofree && !list_empty(&l3->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
Christoph Lametered11d9e2006-06-30 01:55:45 -07002481 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002482 p = l3->slabs_free.prev;
Christoph Lametered11d9e2006-06-30 01:55:45 -07002483 if (p == &l3->slabs_free) {
2484 spin_unlock_irq(&l3->list_lock);
2485 goto out;
2486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
Christoph Lametered11d9e2006-06-30 01:55:45 -07002488 slabp = list_entry(p, struct slab, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489#if DEBUG
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002490 BUG_ON(slabp->inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491#endif
2492 list_del(&slabp->list);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002493 /*
2494 * Safe to drop the lock. The slab is no longer linked
2495 * to the cache.
2496 */
2497 l3->free_objects -= cache->num;
Christoph Lametere498be72005-09-09 13:03:32 -07002498 spin_unlock_irq(&l3->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002499 slab_destroy(cache, slabp);
2500 nr_freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501 }
Christoph Lametered11d9e2006-06-30 01:55:45 -07002502out:
2503 return nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002504}
2505
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002506/* Called with cache_chain_mutex held to protect against cpu hotplug */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002507static int __cache_shrink(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07002508{
2509 int ret = 0, i = 0;
2510 struct kmem_list3 *l3;
2511
2512 drain_cpu_caches(cachep);
2513
2514 check_irq_on();
2515 for_each_online_node(i) {
2516 l3 = cachep->nodelists[i];
Christoph Lametered11d9e2006-06-30 01:55:45 -07002517 if (!l3)
2518 continue;
2519
2520 drain_freelist(cachep, l3, l3->free_objects);
2521
2522 ret += !list_empty(&l3->slabs_full) ||
2523 !list_empty(&l3->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07002524 }
2525 return (ret ? 1 : 0);
2526}
2527
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528/**
2529 * kmem_cache_shrink - Shrink a cache.
2530 * @cachep: The cache to shrink.
2531 *
2532 * Releases as many slabs as possible for a cache.
2533 * To help debugging, a zero exit status indicates all slabs were released.
2534 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002535int kmem_cache_shrink(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536{
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002537 int ret;
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002538 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002540 mutex_lock(&cache_chain_mutex);
2541 ret = __cache_shrink(cachep);
2542 mutex_unlock(&cache_chain_mutex);
2543 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544}
2545EXPORT_SYMBOL(kmem_cache_shrink);
2546
2547/**
2548 * kmem_cache_destroy - delete a cache
2549 * @cachep: the cache to destroy
2550 *
Robert P. J. Day72fd4a32007-02-10 01:45:59 -08002551 * Remove a &struct kmem_cache object from the slab cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 *
2553 * It is expected this function will be called by a module when it is
2554 * unloaded. This will remove the cache completely, and avoid a duplicate
2555 * cache being allocated each time a module is loaded and unloaded, if the
2556 * module doesn't have persistent in-kernel storage across loads and unloads.
2557 *
2558 * The cache must be empty before calling this function.
2559 *
2560 * The caller must guarantee that noone will allocate memory from the cache
2561 * during the kmem_cache_destroy().
2562 */
Alexey Dobriyan133d2052006-09-27 01:49:41 -07002563void kmem_cache_destroy(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564{
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002565 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 /* Find the cache in the chain of caches. */
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002568 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 /*
2570 * the chain is never empty, cache_cache is never destroyed
2571 */
2572 list_del(&cachep->next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 if (__cache_shrink(cachep)) {
2574 slab_error(cachep, "Can't free all objects");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002575 list_add(&cachep->next, &cache_chain);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002576 mutex_unlock(&cache_chain_mutex);
Alexey Dobriyan133d2052006-09-27 01:49:41 -07002577 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 }
2579
2580 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU))
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002581 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Christoph Lameter117f6eb2006-09-25 23:31:37 -07002583 __kmem_cache_destroy(cachep);
Ravikiran G Thirumalai8f5be202006-12-06 20:32:14 -08002584 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585}
2586EXPORT_SYMBOL(kmem_cache_destroy);
2587
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07002588/*
2589 * Get the memory for a slab management obj.
2590 * For a slab cache when the slab descriptor is off-slab, slab descriptors
2591 * always come from malloc_sizes caches. The slab descriptor cannot
2592 * come from the same cache which is getting created because,
2593 * when we are searching for an appropriate cache for these
2594 * descriptors in kmem_cache_create, we search through the malloc_sizes array.
2595 * If we are creating a malloc_sizes cache here it would not be visible to
2596 * kmem_find_general_cachep till the initialization is complete.
2597 * Hence we cannot have slabp_cache same as the original cache.
2598 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002599static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002600 int colour_off, gfp_t local_flags,
2601 int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
2603 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002604
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 if (OFF_SLAB(cachep)) {
2606 /* Slab management obj is off-slab. */
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002607 slabp = kmem_cache_alloc_node(cachep->slabp_cache,
Christoph Lameter3c517a62006-12-06 20:33:29 -08002608 local_flags & ~GFP_THISNODE, nodeid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 if (!slabp)
2610 return NULL;
2611 } else {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002612 slabp = objp + colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 colour_off += cachep->slab_size;
2614 }
2615 slabp->inuse = 0;
2616 slabp->colouroff = colour_off;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002617 slabp->s_mem = objp + colour_off;
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002618 slabp->nodeid = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 return slabp;
2620}
2621
2622static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
2623{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002624 return (kmem_bufctl_t *) (slabp + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625}
2626
Pekka Enberg343e0d72006-02-01 03:05:50 -08002627static void cache_init_objs(struct kmem_cache *cachep,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002628 struct slab *slabp, unsigned long ctor_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629{
2630 int i;
2631
2632 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002633 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634#if DEBUG
2635 /* need to poison the objs? */
2636 if (cachep->flags & SLAB_POISON)
2637 poison_obj(cachep, objp, POISON_FREE);
2638 if (cachep->flags & SLAB_STORE_USER)
2639 *dbg_userword(cachep, objp) = NULL;
2640
2641 if (cachep->flags & SLAB_RED_ZONE) {
2642 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2643 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2644 }
2645 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002646 * Constructors are not allowed to allocate memory from the same
2647 * cache which they are a constructor for. Otherwise, deadlock.
2648 * They must also be threaded.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 */
2650 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002651 cachep->ctor(objp + obj_offset(cachep), cachep,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002652 ctor_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
2654 if (cachep->flags & SLAB_RED_ZONE) {
2655 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2656 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002657 " end of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2659 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002660 " start of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 }
Andrew Mortona737b3e2006-03-22 00:08:11 -08002662 if ((cachep->buffer_size % PAGE_SIZE) == 0 &&
2663 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002664 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002665 cachep->buffer_size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666#else
2667 if (cachep->ctor)
2668 cachep->ctor(objp, cachep, ctor_flags);
2669#endif
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002670 slab_bufctl(slabp)[i] = i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002672 slab_bufctl(slabp)[i - 1] = BUFCTL_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 slabp->free = 0;
2674}
2675
Pekka Enberg343e0d72006-02-01 03:05:50 -08002676static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677{
Christoph Lameter4b51d662007-02-10 01:43:10 -08002678 if (CONFIG_ZONE_DMA_FLAG) {
2679 if (flags & GFP_DMA)
2680 BUG_ON(!(cachep->gfpflags & GFP_DMA));
2681 else
2682 BUG_ON(cachep->gfpflags & GFP_DMA);
2683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684}
2685
Andrew Mortona737b3e2006-03-22 00:08:11 -08002686static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
2687 int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002688{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002689 void *objp = index_to_obj(cachep, slabp, slabp->free);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002690 kmem_bufctl_t next;
2691
2692 slabp->inuse++;
2693 next = slab_bufctl(slabp)[slabp->free];
2694#if DEBUG
2695 slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
2696 WARN_ON(slabp->nodeid != nodeid);
2697#endif
2698 slabp->free = next;
2699
2700 return objp;
2701}
2702
Andrew Mortona737b3e2006-03-22 00:08:11 -08002703static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
2704 void *objp, int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002705{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002706 unsigned int objnr = obj_to_index(cachep, slabp, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002707
2708#if DEBUG
2709 /* Verify that the slab belongs to the intended node */
2710 WARN_ON(slabp->nodeid != nodeid);
2711
Al Viro871751e2006-03-25 03:06:39 -08002712 if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) {
Matthew Dobson78d382d2006-02-01 03:05:47 -08002713 printk(KERN_ERR "slab: double free detected in cache "
Andrew Mortona737b3e2006-03-22 00:08:11 -08002714 "'%s', objp %p\n", cachep->name, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002715 BUG();
2716 }
2717#endif
2718 slab_bufctl(slabp)[objnr] = slabp->free;
2719 slabp->free = objnr;
2720 slabp->inuse--;
2721}
2722
Pekka Enberg47768742006-06-23 02:03:07 -07002723/*
2724 * Map pages beginning at addr to the given cache and slab. This is required
2725 * for the slab allocator to be able to lookup the cache and slab of a
2726 * virtual address for kfree, ksize, kmem_ptr_validate, and slab debugging.
2727 */
2728static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
2729 void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730{
Pekka Enberg47768742006-06-23 02:03:07 -07002731 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 struct page *page;
2733
Pekka Enberg47768742006-06-23 02:03:07 -07002734 page = virt_to_page(addr);
Nick Piggin84097512006-03-22 00:08:34 -08002735
Pekka Enberg47768742006-06-23 02:03:07 -07002736 nr_pages = 1;
Nick Piggin84097512006-03-22 00:08:34 -08002737 if (likely(!PageCompound(page)))
Pekka Enberg47768742006-06-23 02:03:07 -07002738 nr_pages <<= cache->gfporder;
2739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 do {
Pekka Enberg47768742006-06-23 02:03:07 -07002741 page_set_cache(page, cache);
2742 page_set_slab(page, slab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002743 page++;
Pekka Enberg47768742006-06-23 02:03:07 -07002744 } while (--nr_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745}
2746
2747/*
2748 * Grow (by 1) the number of slabs within a cache. This is called by
2749 * kmem_cache_alloc() when there are no active objs left in a cache.
2750 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002751static int cache_grow(struct kmem_cache *cachep,
2752 gfp_t flags, int nodeid, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002754 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002755 size_t offset;
2756 gfp_t local_flags;
2757 unsigned long ctor_flags;
Christoph Lametere498be72005-09-09 13:03:32 -07002758 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759
Andrew Mortona737b3e2006-03-22 00:08:11 -08002760 /*
2761 * Be lazy and only check for valid flags here, keeping it out of the
2762 * critical path in kmem_cache_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 */
Christoph Lametercfce6602007-05-06 14:50:17 -07002764 BUG_ON(flags & ~(GFP_DMA | GFP_LEVEL_MASK));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765
2766 ctor_flags = SLAB_CTOR_CONSTRUCTOR;
Christoph Lametera06d72c2006-12-06 20:33:12 -08002767 local_flags = (flags & GFP_LEVEL_MASK);
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002768 /* Take the l3 list lock to change the colour_next on this node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 check_irq_off();
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002770 l3 = cachep->nodelists[nodeid];
2771 spin_lock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772
2773 /* Get colour for the slab, and cal the next value. */
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002774 offset = l3->colour_next;
2775 l3->colour_next++;
2776 if (l3->colour_next >= cachep->colour)
2777 l3->colour_next = 0;
2778 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002780 offset *= cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781
2782 if (local_flags & __GFP_WAIT)
2783 local_irq_enable();
2784
2785 /*
2786 * The test for missing atomic flag is performed here, rather than
2787 * the more obvious place, simply to reduce the critical path length
2788 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2789 * will eventually be caught here (where it matters).
2790 */
2791 kmem_flagcheck(cachep, flags);
2792
Andrew Mortona737b3e2006-03-22 00:08:11 -08002793 /*
2794 * Get mem for the objs. Attempt to allocate a physical page from
2795 * 'nodeid'.
Christoph Lametere498be72005-09-09 13:03:32 -07002796 */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002797 if (!objp)
2798 objp = kmem_getpages(cachep, flags, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002799 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 goto failed;
2801
2802 /* Get slab management. */
Christoph Lameter3c517a62006-12-06 20:33:29 -08002803 slabp = alloc_slabmgmt(cachep, objp, offset,
2804 local_flags & ~GFP_THISNODE, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002805 if (!slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 goto opps1;
2807
Christoph Lametere498be72005-09-09 13:03:32 -07002808 slabp->nodeid = nodeid;
Pekka Enberg47768742006-06-23 02:03:07 -07002809 slab_map_pages(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
2811 cache_init_objs(cachep, slabp, ctor_flags);
2812
2813 if (local_flags & __GFP_WAIT)
2814 local_irq_disable();
2815 check_irq_off();
Christoph Lametere498be72005-09-09 13:03:32 -07002816 spin_lock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
2818 /* Make slab active. */
Christoph Lametere498be72005-09-09 13:03:32 -07002819 list_add_tail(&slabp->list, &(l3->slabs_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 STATS_INC_GROWN(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07002821 l3->free_objects += cachep->num;
2822 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002824opps1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 kmem_freepages(cachep, objp);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002826failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 if (local_flags & __GFP_WAIT)
2828 local_irq_disable();
2829 return 0;
2830}
2831
2832#if DEBUG
2833
2834/*
2835 * Perform extra freeing checks:
2836 * - detect bad pointers.
2837 * - POISON/RED_ZONE checking
2838 * - destructor calls, for caches with POISON+dtor
2839 */
2840static void kfree_debugcheck(const void *objp)
2841{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 if (!virt_addr_valid(objp)) {
2843 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002844 (unsigned long)objp);
2845 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847}
2848
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002849static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2850{
David Woodhouseb46b8f12007-05-08 00:22:59 -07002851 unsigned long long redzone1, redzone2;
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002852
2853 redzone1 = *dbg_redzone1(cache, obj);
2854 redzone2 = *dbg_redzone2(cache, obj);
2855
2856 /*
2857 * Redzone is ok.
2858 */
2859 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2860 return;
2861
2862 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2863 slab_error(cache, "double free detected");
2864 else
2865 slab_error(cache, "memory outside object was overwritten");
2866
David Woodhouseb46b8f12007-05-08 00:22:59 -07002867 printk(KERN_ERR "%p: redzone 1:0x%llx, redzone 2:0x%llx.\n",
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002868 obj, redzone1, redzone2);
2869}
2870
Pekka Enberg343e0d72006-02-01 03:05:50 -08002871static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002872 void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873{
2874 struct page *page;
2875 unsigned int objnr;
2876 struct slab *slabp;
2877
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002878 objp -= obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 kfree_debugcheck(objp);
Christoph Lameterb49af682007-05-06 14:49:41 -07002880 page = virt_to_head_page(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
Pekka Enberg065d41c2005-11-13 16:06:46 -08002882 slabp = page_get_slab(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883
2884 if (cachep->flags & SLAB_RED_ZONE) {
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002885 verify_redzone_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2887 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2888 }
2889 if (cachep->flags & SLAB_STORE_USER)
2890 *dbg_userword(cachep, objp) = caller;
2891
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002892 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
2894 BUG_ON(objnr >= cachep->num);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002895 BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 if (cachep->flags & SLAB_POISON && cachep->dtor) {
2898 /* we want to cache poison the object,
2899 * call the destruction callback
2900 */
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002901 cachep->dtor(objp + obj_offset(cachep), cachep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 }
Al Viro871751e2006-03-25 03:06:39 -08002903#ifdef CONFIG_DEBUG_SLAB_LEAK
2904 slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
2905#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906 if (cachep->flags & SLAB_POISON) {
2907#ifdef CONFIG_DEBUG_PAGEALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -08002908 if ((cachep->buffer_size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 store_stackinfo(cachep, objp, (unsigned long)caller);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002910 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002911 cachep->buffer_size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 } else {
2913 poison_obj(cachep, objp, POISON_FREE);
2914 }
2915#else
2916 poison_obj(cachep, objp, POISON_FREE);
2917#endif
2918 }
2919 return objp;
2920}
2921
Pekka Enberg343e0d72006-02-01 03:05:50 -08002922static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002923{
2924 kmem_bufctl_t i;
2925 int entries = 0;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002926
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 /* Check slab's freelist to see if this obj is there. */
2928 for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
2929 entries++;
2930 if (entries > cachep->num || i >= cachep->num)
2931 goto bad;
2932 }
2933 if (entries != cachep->num - slabp->inuse) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002934bad:
2935 printk(KERN_ERR "slab: Internal list corruption detected in "
2936 "cache '%s'(%d), slabp %p(%d). Hexdump:\n",
2937 cachep->name, cachep->num, slabp, slabp->inuse);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002938 for (i = 0;
Linus Torvalds264132b2006-03-06 12:10:07 -08002939 i < sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002940 i++) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002941 if (i % 16 == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 printk("\n%03x:", i);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002943 printk(" %02x", ((unsigned char *)slabp)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 }
2945 printk("\n");
2946 BUG();
2947 }
2948}
2949#else
2950#define kfree_debugcheck(x) do { } while(0)
2951#define cache_free_debugcheck(x,objp,z) (objp)
2952#define check_slabp(x,y) do { } while(0)
2953#endif
2954
Pekka Enberg343e0d72006-02-01 03:05:50 -08002955static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
2957 int batchcount;
2958 struct kmem_list3 *l3;
2959 struct array_cache *ac;
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002960 int node;
2961
2962 node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
2964 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002965 ac = cpu_cache_get(cachep);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002966retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 batchcount = ac->batchcount;
2968 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002969 /*
2970 * If there was little recent activity on this cache, then
2971 * perform only a partial refill. Otherwise we could generate
2972 * refill bouncing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 */
2974 batchcount = BATCHREFILL_LIMIT;
2975 }
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07002976 l3 = cachep->nodelists[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977
Christoph Lametere498be72005-09-09 13:03:32 -07002978 BUG_ON(ac->avail > 0 || !l3);
2979 spin_lock(&l3->list_lock);
2980
Christoph Lameter3ded1752006-03-25 03:06:44 -08002981 /* See if we can refill from the shared array */
2982 if (l3->shared && transfer_objects(ac, l3->shared, batchcount))
2983 goto alloc_done;
2984
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 while (batchcount > 0) {
2986 struct list_head *entry;
2987 struct slab *slabp;
2988 /* Get slab alloc is to come from. */
2989 entry = l3->slabs_partial.next;
2990 if (entry == &l3->slabs_partial) {
2991 l3->free_touched = 1;
2992 entry = l3->slabs_free.next;
2993 if (entry == &l3->slabs_free)
2994 goto must_grow;
2995 }
2996
2997 slabp = list_entry(entry, struct slab, list);
2998 check_slabp(cachep, slabp);
2999 check_spinlock_acquired(cachep);
Pekka Enberg714b81712007-05-06 14:49:03 -07003000
3001 /*
3002 * The slab was either on partial or free list so
3003 * there must be at least one object available for
3004 * allocation.
3005 */
3006 BUG_ON(slabp->inuse < 0 || slabp->inuse >= cachep->num);
3007
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 while (slabp->inuse < cachep->num && batchcount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 STATS_INC_ALLOCED(cachep);
3010 STATS_INC_ACTIVE(cachep);
3011 STATS_SET_HIGH(cachep);
3012
Matthew Dobson78d382d2006-02-01 03:05:47 -08003013 ac->entry[ac->avail++] = slab_get_obj(cachep, slabp,
Pekka Enberg1ca4cb22006-10-06 00:43:52 -07003014 node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 }
3016 check_slabp(cachep, slabp);
3017
3018 /* move slabp to correct slabp list: */
3019 list_del(&slabp->list);
3020 if (slabp->free == BUFCTL_END)
3021 list_add(&slabp->list, &l3->slabs_full);
3022 else
3023 list_add(&slabp->list, &l3->slabs_partial);
3024 }
3025
Andrew Mortona737b3e2006-03-22 00:08:11 -08003026must_grow:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 l3->free_objects -= ac->avail;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003028alloc_done:
Christoph Lametere498be72005-09-09 13:03:32 -07003029 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030
3031 if (unlikely(!ac->avail)) {
3032 int x;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003033 x = cache_grow(cachep, flags | GFP_THISNODE, node, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07003034
Andrew Mortona737b3e2006-03-22 00:08:11 -08003035 /* cache_grow can reenable interrupts, then ac could change. */
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003036 ac = cpu_cache_get(cachep);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003037 if (!x && ac->avail == 0) /* no objects in sight? abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 return NULL;
3039
Andrew Mortona737b3e2006-03-22 00:08:11 -08003040 if (!ac->avail) /* objects refilled by interrupt? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 goto retry;
3042 }
3043 ac->touched = 1;
Christoph Lametere498be72005-09-09 13:03:32 -07003044 return ac->entry[--ac->avail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045}
3046
Andrew Mortona737b3e2006-03-22 00:08:11 -08003047static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
3048 gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049{
3050 might_sleep_if(flags & __GFP_WAIT);
3051#if DEBUG
3052 kmem_flagcheck(cachep, flags);
3053#endif
3054}
3055
3056#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003057static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
3058 gfp_t flags, void *objp, void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003060 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 return objp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003062 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063#ifdef CONFIG_DEBUG_PAGEALLOC
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003064 if ((cachep->buffer_size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003065 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003066 cachep->buffer_size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 else
3068 check_poison_obj(cachep, objp);
3069#else
3070 check_poison_obj(cachep, objp);
3071#endif
3072 poison_obj(cachep, objp, POISON_INUSE);
3073 }
3074 if (cachep->flags & SLAB_STORE_USER)
3075 *dbg_userword(cachep, objp) = caller;
3076
3077 if (cachep->flags & SLAB_RED_ZONE) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003078 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
3079 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
3080 slab_error(cachep, "double free, or memory outside"
3081 " object was overwritten");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003082 printk(KERN_ERR
David Woodhouseb46b8f12007-05-08 00:22:59 -07003083 "%p: redzone 1:0x%llx, redzone 2:0x%llx\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08003084 objp, *dbg_redzone1(cachep, objp),
3085 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 }
3087 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
3088 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
3089 }
Al Viro871751e2006-03-25 03:06:39 -08003090#ifdef CONFIG_DEBUG_SLAB_LEAK
3091 {
3092 struct slab *slabp;
3093 unsigned objnr;
3094
Christoph Lameterb49af682007-05-06 14:49:41 -07003095 slabp = page_get_slab(virt_to_head_page(objp));
Al Viro871751e2006-03-25 03:06:39 -08003096 objnr = (unsigned)(objp - slabp->s_mem) / cachep->buffer_size;
3097 slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE;
3098 }
3099#endif
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003100 objp += obj_offset(cachep);
Christoph Lameter4f104932007-05-06 14:50:17 -07003101 if (cachep->ctor && cachep->flags & SLAB_POISON)
3102 cachep->ctor(objp, cachep, SLAB_CTOR_CONSTRUCTOR);
Kevin Hilmana44b56d2006-12-06 20:32:11 -08003103#if ARCH_SLAB_MINALIGN
3104 if ((u32)objp & (ARCH_SLAB_MINALIGN-1)) {
3105 printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n",
3106 objp, ARCH_SLAB_MINALIGN);
3107 }
3108#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 return objp;
3110}
3111#else
3112#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
3113#endif
3114
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003115#ifdef CONFIG_FAILSLAB
3116
3117static struct failslab_attr {
3118
3119 struct fault_attr attr;
3120
3121 u32 ignore_gfp_wait;
3122#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
3123 struct dentry *ignore_gfp_wait_file;
3124#endif
3125
3126} failslab = {
3127 .attr = FAULT_ATTR_INITIALIZER,
Don Mullis6b1b60f2006-12-08 02:39:53 -08003128 .ignore_gfp_wait = 1,
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003129};
3130
3131static int __init setup_failslab(char *str)
3132{
3133 return setup_fault_attr(&failslab.attr, str);
3134}
3135__setup("failslab=", setup_failslab);
3136
3137static int should_failslab(struct kmem_cache *cachep, gfp_t flags)
3138{
3139 if (cachep == &cache_cache)
3140 return 0;
3141 if (flags & __GFP_NOFAIL)
3142 return 0;
3143 if (failslab.ignore_gfp_wait && (flags & __GFP_WAIT))
3144 return 0;
3145
3146 return should_fail(&failslab.attr, obj_size(cachep));
3147}
3148
3149#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
3150
3151static int __init failslab_debugfs(void)
3152{
3153 mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
3154 struct dentry *dir;
3155 int err;
3156
Akinobu Mita824ebef2007-05-06 14:49:58 -07003157 err = init_fault_attr_dentries(&failslab.attr, "failslab");
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003158 if (err)
3159 return err;
3160 dir = failslab.attr.dentries.dir;
3161
3162 failslab.ignore_gfp_wait_file =
3163 debugfs_create_bool("ignore-gfp-wait", mode, dir,
3164 &failslab.ignore_gfp_wait);
3165
3166 if (!failslab.ignore_gfp_wait_file) {
3167 err = -ENOMEM;
3168 debugfs_remove(failslab.ignore_gfp_wait_file);
3169 cleanup_fault_attr_dentries(&failslab.attr);
3170 }
3171
3172 return err;
3173}
3174
3175late_initcall(failslab_debugfs);
3176
3177#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
3178
3179#else /* CONFIG_FAILSLAB */
3180
3181static inline int should_failslab(struct kmem_cache *cachep, gfp_t flags)
3182{
3183 return 0;
3184}
3185
3186#endif /* CONFIG_FAILSLAB */
3187
Pekka Enberg343e0d72006-02-01 03:05:50 -08003188static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003190 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 struct array_cache *ac;
3192
Alok N Kataria5c382302005-09-27 21:45:46 -07003193 check_irq_off();
Akinobu Mita8a8b6502006-12-08 02:39:44 -08003194
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003195 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 if (likely(ac->avail)) {
3197 STATS_INC_ALLOCHIT(cachep);
3198 ac->touched = 1;
Christoph Lametere498be72005-09-09 13:03:32 -07003199 objp = ac->entry[--ac->avail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 } else {
3201 STATS_INC_ALLOCMISS(cachep);
3202 objp = cache_alloc_refill(cachep, flags);
3203 }
Alok N Kataria5c382302005-09-27 21:45:46 -07003204 return objp;
3205}
3206
Christoph Lametere498be72005-09-09 13:03:32 -07003207#ifdef CONFIG_NUMA
3208/*
Paul Jacksonb2455392006-03-24 03:16:12 -08003209 * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
Paul Jacksonc61afb12006-03-24 03:16:08 -08003210 *
3211 * If we are in_interrupt, then process context, including cpusets and
3212 * mempolicy, may not apply and should not be used for allocation policy.
3213 */
3214static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
3215{
3216 int nid_alloc, nid_here;
3217
Christoph Lameter765c4502006-09-27 01:50:08 -07003218 if (in_interrupt() || (flags & __GFP_THISNODE))
Paul Jacksonc61afb12006-03-24 03:16:08 -08003219 return NULL;
3220 nid_alloc = nid_here = numa_node_id();
3221 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
3222 nid_alloc = cpuset_mem_spread_node();
3223 else if (current->mempolicy)
3224 nid_alloc = slab_node(current->mempolicy);
3225 if (nid_alloc != nid_here)
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003226 return ____cache_alloc_node(cachep, flags, nid_alloc);
Paul Jacksonc61afb12006-03-24 03:16:08 -08003227 return NULL;
3228}
3229
3230/*
Christoph Lameter765c4502006-09-27 01:50:08 -07003231 * Fallback function if there was no memory available and no objects on a
Christoph Lameter3c517a62006-12-06 20:33:29 -08003232 * certain node and fall back is permitted. First we scan all the
3233 * available nodelists for available objects. If that fails then we
3234 * perform an allocation without specifying a node. This allows the page
3235 * allocator to do its reclaim / fallback magic. We then insert the
3236 * slab into the proper nodelist and then allocate from it.
Christoph Lameter765c4502006-09-27 01:50:08 -07003237 */
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003238static void *fallback_alloc(struct kmem_cache *cache, gfp_t flags)
Christoph Lameter765c4502006-09-27 01:50:08 -07003239{
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003240 struct zonelist *zonelist;
3241 gfp_t local_flags;
Christoph Lameter765c4502006-09-27 01:50:08 -07003242 struct zone **z;
3243 void *obj = NULL;
Christoph Lameter3c517a62006-12-06 20:33:29 -08003244 int nid;
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003245
3246 if (flags & __GFP_THISNODE)
3247 return NULL;
3248
3249 zonelist = &NODE_DATA(slab_node(current->mempolicy))
3250 ->node_zonelists[gfp_zone(flags)];
3251 local_flags = (flags & GFP_LEVEL_MASK);
Christoph Lameter765c4502006-09-27 01:50:08 -07003252
Christoph Lameter3c517a62006-12-06 20:33:29 -08003253retry:
3254 /*
3255 * Look through allowed nodes for objects available
3256 * from existing per node queues.
3257 */
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003258 for (z = zonelist->zones; *z && !obj; z++) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003259 nid = zone_to_nid(*z);
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003260
Paul Jackson02a0e532006-12-13 00:34:25 -08003261 if (cpuset_zone_allowed_hardwall(*z, flags) &&
Christoph Lameter3c517a62006-12-06 20:33:29 -08003262 cache->nodelists[nid] &&
3263 cache->nodelists[nid]->free_objects)
3264 obj = ____cache_alloc_node(cache,
3265 flags | GFP_THISNODE, nid);
3266 }
3267
Christoph Lametercfce6602007-05-06 14:50:17 -07003268 if (!obj) {
Christoph Lameter3c517a62006-12-06 20:33:29 -08003269 /*
3270 * This allocation will be performed within the constraints
3271 * of the current cpuset / memory policy requirements.
3272 * We may trigger various forms of reclaim on the allowed
3273 * set and go into memory reserves if necessary.
3274 */
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003275 if (local_flags & __GFP_WAIT)
3276 local_irq_enable();
3277 kmem_flagcheck(cache, flags);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003278 obj = kmem_getpages(cache, flags, -1);
Christoph Lameterdd47ea72006-12-13 00:34:11 -08003279 if (local_flags & __GFP_WAIT)
3280 local_irq_disable();
Christoph Lameter3c517a62006-12-06 20:33:29 -08003281 if (obj) {
3282 /*
3283 * Insert into the appropriate per node queues
3284 */
3285 nid = page_to_nid(virt_to_page(obj));
3286 if (cache_grow(cache, flags, nid, obj)) {
3287 obj = ____cache_alloc_node(cache,
3288 flags | GFP_THISNODE, nid);
3289 if (!obj)
3290 /*
3291 * Another processor may allocate the
3292 * objects in the slab since we are
3293 * not holding any locks.
3294 */
3295 goto retry;
3296 } else {
Hugh Dickinsb6a60452007-01-05 16:36:36 -08003297 /* cache_grow already freed obj */
Christoph Lameter3c517a62006-12-06 20:33:29 -08003298 obj = NULL;
3299 }
3300 }
Christoph Lameteraedb0eb2006-10-21 10:24:16 -07003301 }
Christoph Lameter765c4502006-09-27 01:50:08 -07003302 return obj;
3303}
3304
3305/*
Christoph Lametere498be72005-09-09 13:03:32 -07003306 * A interface to enable slab creation on nodeid
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 */
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003308static void *____cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003309 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07003310{
3311 struct list_head *entry;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003312 struct slab *slabp;
3313 struct kmem_list3 *l3;
3314 void *obj;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003315 int x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003317 l3 = cachep->nodelists[nodeid];
3318 BUG_ON(!l3);
Christoph Lametere498be72005-09-09 13:03:32 -07003319
Andrew Mortona737b3e2006-03-22 00:08:11 -08003320retry:
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003321 check_irq_off();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003322 spin_lock(&l3->list_lock);
3323 entry = l3->slabs_partial.next;
3324 if (entry == &l3->slabs_partial) {
3325 l3->free_touched = 1;
3326 entry = l3->slabs_free.next;
3327 if (entry == &l3->slabs_free)
3328 goto must_grow;
3329 }
Christoph Lametere498be72005-09-09 13:03:32 -07003330
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003331 slabp = list_entry(entry, struct slab, list);
3332 check_spinlock_acquired_node(cachep, nodeid);
3333 check_slabp(cachep, slabp);
Christoph Lametere498be72005-09-09 13:03:32 -07003334
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003335 STATS_INC_NODEALLOCS(cachep);
3336 STATS_INC_ACTIVE(cachep);
3337 STATS_SET_HIGH(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003338
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003339 BUG_ON(slabp->inuse == cachep->num);
Christoph Lametere498be72005-09-09 13:03:32 -07003340
Matthew Dobson78d382d2006-02-01 03:05:47 -08003341 obj = slab_get_obj(cachep, slabp, nodeid);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003342 check_slabp(cachep, slabp);
3343 l3->free_objects--;
3344 /* move slabp to correct slabp list: */
3345 list_del(&slabp->list);
Christoph Lametere498be72005-09-09 13:03:32 -07003346
Andrew Mortona737b3e2006-03-22 00:08:11 -08003347 if (slabp->free == BUFCTL_END)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003348 list_add(&slabp->list, &l3->slabs_full);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003349 else
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003350 list_add(&slabp->list, &l3->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07003351
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003352 spin_unlock(&l3->list_lock);
3353 goto done;
Christoph Lametere498be72005-09-09 13:03:32 -07003354
Andrew Mortona737b3e2006-03-22 00:08:11 -08003355must_grow:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003356 spin_unlock(&l3->list_lock);
Christoph Lameter3c517a62006-12-06 20:33:29 -08003357 x = cache_grow(cachep, flags | GFP_THISNODE, nodeid, NULL);
Christoph Lameter765c4502006-09-27 01:50:08 -07003358 if (x)
3359 goto retry;
Christoph Lametere498be72005-09-09 13:03:32 -07003360
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003361 return fallback_alloc(cachep, flags);
Christoph Lameter765c4502006-09-27 01:50:08 -07003362
Andrew Mortona737b3e2006-03-22 00:08:11 -08003363done:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003364 return obj;
Christoph Lametere498be72005-09-09 13:03:32 -07003365}
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003366
3367/**
3368 * kmem_cache_alloc_node - Allocate an object on the specified node
3369 * @cachep: The cache to allocate from.
3370 * @flags: See kmalloc().
3371 * @nodeid: node number of the target node.
3372 * @caller: return address of caller, used for debug information
3373 *
3374 * Identical to kmem_cache_alloc but it will allocate memory on the given
3375 * node, which can improve the performance for cpu bound structures.
3376 *
3377 * Fallback to other node is possible if __GFP_THISNODE is not set.
3378 */
3379static __always_inline void *
3380__cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid,
3381 void *caller)
3382{
3383 unsigned long save_flags;
3384 void *ptr;
3385
Akinobu Mita824ebef2007-05-06 14:49:58 -07003386 if (should_failslab(cachep, flags))
3387 return NULL;
3388
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003389 cache_alloc_debugcheck_before(cachep, flags);
3390 local_irq_save(save_flags);
3391
3392 if (unlikely(nodeid == -1))
3393 nodeid = numa_node_id();
3394
3395 if (unlikely(!cachep->nodelists[nodeid])) {
3396 /* Node not bootstrapped yet */
3397 ptr = fallback_alloc(cachep, flags);
3398 goto out;
3399 }
3400
3401 if (nodeid == numa_node_id()) {
3402 /*
3403 * Use the locally cached objects if possible.
3404 * However ____cache_alloc does not allow fallback
3405 * to other nodes. It may fail while we still have
3406 * objects on other nodes available.
3407 */
3408 ptr = ____cache_alloc(cachep, flags);
3409 if (ptr)
3410 goto out;
3411 }
3412 /* ___cache_alloc_node can fall back to other nodes */
3413 ptr = ____cache_alloc_node(cachep, flags, nodeid);
3414 out:
3415 local_irq_restore(save_flags);
3416 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, caller);
3417
3418 return ptr;
3419}
3420
3421static __always_inline void *
3422__do_cache_alloc(struct kmem_cache *cache, gfp_t flags)
3423{
3424 void *objp;
3425
3426 if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
3427 objp = alternate_node_alloc(cache, flags);
3428 if (objp)
3429 goto out;
3430 }
3431 objp = ____cache_alloc(cache, flags);
3432
3433 /*
3434 * We may just have run out of memory on the local node.
3435 * ____cache_alloc_node() knows how to locate memory on other nodes
3436 */
3437 if (!objp)
3438 objp = ____cache_alloc_node(cache, flags, numa_node_id());
3439
3440 out:
3441 return objp;
3442}
3443#else
3444
3445static __always_inline void *
3446__do_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
3447{
3448 return ____cache_alloc(cachep, flags);
3449}
3450
3451#endif /* CONFIG_NUMA */
3452
3453static __always_inline void *
3454__cache_alloc(struct kmem_cache *cachep, gfp_t flags, void *caller)
3455{
3456 unsigned long save_flags;
3457 void *objp;
3458
Akinobu Mita824ebef2007-05-06 14:49:58 -07003459 if (should_failslab(cachep, flags))
3460 return NULL;
3461
Pekka Enberg8c8cc2c2007-02-10 01:42:53 -08003462 cache_alloc_debugcheck_before(cachep, flags);
3463 local_irq_save(save_flags);
3464 objp = __do_cache_alloc(cachep, flags);
3465 local_irq_restore(save_flags);
3466 objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
3467 prefetchw(objp);
3468
3469 return objp;
3470}
Christoph Lametere498be72005-09-09 13:03:32 -07003471
3472/*
3473 * Caller needs to acquire correct kmem_list's list_lock
3474 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003475static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003476 int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477{
3478 int i;
Christoph Lametere498be72005-09-09 13:03:32 -07003479 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
3481 for (i = 0; i < nr_objects; i++) {
3482 void *objp = objpp[i];
3483 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003485 slabp = virt_to_slab(objp);
Christoph Lameterff694162005-09-22 21:44:02 -07003486 l3 = cachep->nodelists[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 list_del(&slabp->list);
Christoph Lameterff694162005-09-22 21:44:02 -07003488 check_spinlock_acquired_node(cachep, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 check_slabp(cachep, slabp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08003490 slab_put_obj(cachep, slabp, objp, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 STATS_DEC_ACTIVE(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003492 l3->free_objects++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 check_slabp(cachep, slabp);
3494
3495 /* fixup slab chains */
3496 if (slabp->inuse == 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07003497 if (l3->free_objects > l3->free_limit) {
3498 l3->free_objects -= cachep->num;
Ravikiran G Thirumalaie5ac9c52006-09-25 23:31:34 -07003499 /* No need to drop any previously held
3500 * lock here, even if we have a off-slab slab
3501 * descriptor it is guaranteed to come from
3502 * a different cache, refer to comments before
3503 * alloc_slabmgmt.
3504 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 slab_destroy(cachep, slabp);
3506 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07003507 list_add(&slabp->list, &l3->slabs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 }
3509 } else {
3510 /* Unconditionally move a slab to the end of the
3511 * partial list on free - maximum time for the
3512 * other objects to be freed, too.
3513 */
Christoph Lametere498be72005-09-09 13:03:32 -07003514 list_add_tail(&slabp->list, &l3->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 }
3516 }
3517}
3518
Pekka Enberg343e0d72006-02-01 03:05:50 -08003519static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520{
3521 int batchcount;
Christoph Lametere498be72005-09-09 13:03:32 -07003522 struct kmem_list3 *l3;
Christoph Lameterff694162005-09-22 21:44:02 -07003523 int node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
3525 batchcount = ac->batchcount;
3526#if DEBUG
3527 BUG_ON(!batchcount || batchcount > ac->avail);
3528#endif
3529 check_irq_off();
Christoph Lameterff694162005-09-22 21:44:02 -07003530 l3 = cachep->nodelists[node];
Ingo Molnar873623d2006-07-13 14:44:38 +02003531 spin_lock(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07003532 if (l3->shared) {
3533 struct array_cache *shared_array = l3->shared;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003534 int max = shared_array->limit - shared_array->avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 if (max) {
3536 if (batchcount > max)
3537 batchcount = max;
Christoph Lametere498be72005-09-09 13:03:32 -07003538 memcpy(&(shared_array->entry[shared_array->avail]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003539 ac->entry, sizeof(void *) * batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 shared_array->avail += batchcount;
3541 goto free_done;
3542 }
3543 }
3544
Christoph Lameterff694162005-09-22 21:44:02 -07003545 free_block(cachep, ac->entry, batchcount, node);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003546free_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547#if STATS
3548 {
3549 int i = 0;
3550 struct list_head *p;
3551
Christoph Lametere498be72005-09-09 13:03:32 -07003552 p = l3->slabs_free.next;
3553 while (p != &(l3->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 struct slab *slabp;
3555
3556 slabp = list_entry(p, struct slab, list);
3557 BUG_ON(slabp->inuse);
3558
3559 i++;
3560 p = p->next;
3561 }
3562 STATS_SET_FREEABLE(cachep, i);
3563 }
3564#endif
Christoph Lametere498be72005-09-09 13:03:32 -07003565 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 ac->avail -= batchcount;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003567 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568}
3569
3570/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08003571 * Release an obj back to its cache. If the obj has a constructed state, it must
3572 * be in this state _before_ it is released. Called with disabled ints.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 */
Ingo Molnar873623d2006-07-13 14:44:38 +02003574static inline void __cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575{
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003576 struct array_cache *ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577
3578 check_irq_off();
3579 objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));
3580
Siddha, Suresh B62918a02007-05-02 19:27:18 +02003581 if (use_alien_caches && cache_free_alien(cachep, objp))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07003582 return;
Christoph Lametere498be72005-09-09 13:03:32 -07003583
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 if (likely(ac->avail < ac->limit)) {
3585 STATS_INC_FREEHIT(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003586 ac->entry[ac->avail++] = objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587 return;
3588 } else {
3589 STATS_INC_FREEMISS(cachep);
3590 cache_flusharray(cachep, ac);
Christoph Lametere498be72005-09-09 13:03:32 -07003591 ac->entry[ac->avail++] = objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 }
3593}
3594
3595/**
3596 * kmem_cache_alloc - Allocate an object
3597 * @cachep: The cache to allocate from.
3598 * @flags: See kmalloc().
3599 *
3600 * Allocate an object from this cache. The flags are only relevant
3601 * if the cache has no available objects.
3602 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003603void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604{
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003605 return __cache_alloc(cachep, flags, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606}
3607EXPORT_SYMBOL(kmem_cache_alloc);
3608
3609/**
Rolf Eike Beerb8008b22006-07-30 03:04:04 -07003610 * kmem_cache_zalloc - Allocate an object. The memory is set to zero.
Pekka Enberga8c0f9a2006-03-25 03:06:42 -08003611 * @cache: The cache to allocate from.
3612 * @flags: See kmalloc().
3613 *
3614 * Allocate an object from this cache and set the allocated memory to zero.
3615 * The flags are only relevant if the cache has no available objects.
3616 */
3617void *kmem_cache_zalloc(struct kmem_cache *cache, gfp_t flags)
3618{
3619 void *ret = __cache_alloc(cache, flags, __builtin_return_address(0));
3620 if (ret)
3621 memset(ret, 0, obj_size(cache));
3622 return ret;
3623}
3624EXPORT_SYMBOL(kmem_cache_zalloc);
3625
3626/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 * kmem_ptr_validate - check if an untrusted pointer might
3628 * be a slab entry.
3629 * @cachep: the cache we're checking against
3630 * @ptr: pointer to validate
3631 *
3632 * This verifies that the untrusted pointer looks sane:
3633 * it is _not_ a guarantee that the pointer is actually
3634 * part of the slab cache in question, but it at least
3635 * validates that the pointer can be dereferenced and
3636 * looks half-way sane.
3637 *
3638 * Currently only used for dentry validation.
3639 */
Christoph Lameterb7f869a22006-12-22 01:06:44 -08003640int kmem_ptr_validate(struct kmem_cache *cachep, const void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003642 unsigned long addr = (unsigned long)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 unsigned long min_addr = PAGE_OFFSET;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003644 unsigned long align_mask = BYTES_PER_WORD - 1;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003645 unsigned long size = cachep->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 struct page *page;
3647
3648 if (unlikely(addr < min_addr))
3649 goto out;
3650 if (unlikely(addr > (unsigned long)high_memory - size))
3651 goto out;
3652 if (unlikely(addr & align_mask))
3653 goto out;
3654 if (unlikely(!kern_addr_valid(addr)))
3655 goto out;
3656 if (unlikely(!kern_addr_valid(addr + size - 1)))
3657 goto out;
3658 page = virt_to_page(ptr);
3659 if (unlikely(!PageSlab(page)))
3660 goto out;
Pekka Enberg065d41c2005-11-13 16:06:46 -08003661 if (unlikely(page_get_cache(page) != cachep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 goto out;
3663 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003664out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 return 0;
3666}
3667
3668#ifdef CONFIG_NUMA
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003669void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
3670{
3671 return __cache_alloc_node(cachep, flags, nodeid,
3672 __builtin_return_address(0));
3673}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003674EXPORT_SYMBOL(kmem_cache_alloc_node);
3675
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003676static __always_inline void *
3677__do_kmalloc_node(size_t size, gfp_t flags, int node, void *caller)
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003678{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003679 struct kmem_cache *cachep;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003680
3681 cachep = kmem_find_general_cachep(size, flags);
3682 if (unlikely(cachep == NULL))
3683 return NULL;
3684 return kmem_cache_alloc_node(cachep, flags, node);
3685}
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003686
3687#ifdef CONFIG_DEBUG_SLAB
3688void *__kmalloc_node(size_t size, gfp_t flags, int node)
3689{
3690 return __do_kmalloc_node(size, flags, node,
3691 __builtin_return_address(0));
3692}
Christoph Hellwigdbe5e692006-09-25 23:31:36 -07003693EXPORT_SYMBOL(__kmalloc_node);
Christoph Hellwig8b98c162006-12-06 20:32:30 -08003694
3695void *__kmalloc_node_track_caller(size_t size, gfp_t flags,
3696 int node, void *caller)
3697{
3698 return __do_kmalloc_node(size, flags, node, caller);
3699}
3700EXPORT_SYMBOL(__kmalloc_node_track_caller);
3701#else
3702void *__kmalloc_node(size_t size, gfp_t flags, int node)
3703{
3704 return __do_kmalloc_node(size, flags, node, NULL);
3705}
3706EXPORT_SYMBOL(__kmalloc_node);
3707#endif /* CONFIG_DEBUG_SLAB */
3708#endif /* CONFIG_NUMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709
3710/**
Paul Drynoff800590f2006-06-23 02:03:48 -07003711 * __do_kmalloc - allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 * @size: how many bytes of memory are required.
Paul Drynoff800590f2006-06-23 02:03:48 -07003713 * @flags: the type of memory to allocate (see kmalloc).
Randy Dunlap911851e2006-03-22 00:08:14 -08003714 * @caller: function caller for debug tracking of the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 */
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003716static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
3717 void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003719 struct kmem_cache *cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003721 /* If you want to save a few bytes .text space: replace
3722 * __ with kmem_.
3723 * Then kmalloc uses the uninlined functions instead of the inline
3724 * functions.
3725 */
3726 cachep = __find_general_cachep(size, flags);
Andrew Mortondbdb9042005-09-23 13:24:10 -07003727 if (unlikely(cachep == NULL))
3728 return NULL;
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003729 return __cache_alloc(cachep, flags, caller);
3730}
3731
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003732
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003733#ifdef CONFIG_DEBUG_SLAB
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003734void *__kmalloc(size_t size, gfp_t flags)
3735{
Al Viro871751e2006-03-25 03:06:39 -08003736 return __do_kmalloc(size, flags, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737}
3738EXPORT_SYMBOL(__kmalloc);
3739
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003740void *__kmalloc_track_caller(size_t size, gfp_t flags, void *caller)
3741{
3742 return __do_kmalloc(size, flags, caller);
3743}
3744EXPORT_SYMBOL(__kmalloc_track_caller);
Christoph Hellwig1d2c8ee2006-10-04 02:15:25 -07003745
3746#else
3747void *__kmalloc(size_t size, gfp_t flags)
3748{
3749 return __do_kmalloc(size, flags, NULL);
3750}
3751EXPORT_SYMBOL(__kmalloc);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003752#endif
3753
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754/**
Pekka Enbergfd76bab2007-05-06 14:48:40 -07003755 * krealloc - reallocate memory. The contents will remain unchanged.
Pekka Enbergfd76bab2007-05-06 14:48:40 -07003756 * @p: object to reallocate memory for.
3757 * @new_size: how many bytes of memory are required.
3758 * @flags: the type of memory to allocate.
3759 *
3760 * The contents of the object pointed to are preserved up to the
3761 * lesser of the new and old sizes. If @p is %NULL, krealloc()
3762 * behaves exactly like kmalloc(). If @size is 0 and @p is not a
3763 * %NULL pointer, the object pointed to is freed.
3764 */
3765void *krealloc(const void *p, size_t new_size, gfp_t flags)
3766{
3767 struct kmem_cache *cache, *new_cache;
3768 void *ret;
3769
3770 if (unlikely(!p))
3771 return kmalloc_track_caller(new_size, flags);
3772
3773 if (unlikely(!new_size)) {
3774 kfree(p);
3775 return NULL;
3776 }
3777
3778 cache = virt_to_cache(p);
3779 new_cache = __find_general_cachep(new_size, flags);
3780
3781 /*
3782 * If new size fits in the current cache, bail out.
3783 */
3784 if (likely(cache == new_cache))
3785 return (void *)p;
3786
3787 /*
3788 * We are on the slow-path here so do not use __cache_alloc
3789 * because it bloats kernel text.
3790 */
3791 ret = kmalloc_track_caller(new_size, flags);
3792 if (ret) {
3793 memcpy(ret, p, min(new_size, ksize(p)));
3794 kfree(p);
3795 }
3796 return ret;
3797}
3798EXPORT_SYMBOL(krealloc);
3799
3800/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003801 * kmem_cache_free - Deallocate an object
3802 * @cachep: The cache the allocation was from.
3803 * @objp: The previously allocated object.
3804 *
3805 * Free an object which was previously allocated from this
3806 * cache.
3807 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003808void kmem_cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809{
3810 unsigned long flags;
3811
Pekka Enbergddc2e812006-06-23 02:03:40 -07003812 BUG_ON(virt_to_cache(objp) != cachep);
3813
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 local_irq_save(flags);
Ingo Molnar898552c2007-02-10 01:44:57 -08003815 debug_check_no_locks_freed(objp, obj_size(cachep));
Ingo Molnar873623d2006-07-13 14:44:38 +02003816 __cache_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003817 local_irq_restore(flags);
3818}
3819EXPORT_SYMBOL(kmem_cache_free);
3820
3821/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 * kfree - free previously allocated memory
3823 * @objp: pointer returned by kmalloc.
3824 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07003825 * If @objp is NULL, no operation is performed.
3826 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 * Don't free memory not originally allocated by kmalloc()
3828 * or you will run into trouble.
3829 */
3830void kfree(const void *objp)
3831{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003832 struct kmem_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 unsigned long flags;
3834
3835 if (unlikely(!objp))
3836 return;
3837 local_irq_save(flags);
3838 kfree_debugcheck(objp);
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08003839 c = virt_to_cache(objp);
Ingo Molnarf9b84042006-06-27 02:54:49 -07003840 debug_check_no_locks_freed(objp, obj_size(c));
Ingo Molnar873623d2006-07-13 14:44:38 +02003841 __cache_free(c, (void *)objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 local_irq_restore(flags);
3843}
3844EXPORT_SYMBOL(kfree);
3845
Pekka Enberg343e0d72006-02-01 03:05:50 -08003846unsigned int kmem_cache_size(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003848 return obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849}
3850EXPORT_SYMBOL(kmem_cache_size);
3851
Pekka Enberg343e0d72006-02-01 03:05:50 -08003852const char *kmem_cache_name(struct kmem_cache *cachep)
Arnaldo Carvalho de Melo19449722005-06-18 22:46:19 -07003853{
3854 return cachep->name;
3855}
3856EXPORT_SYMBOL_GPL(kmem_cache_name);
3857
Christoph Lametere498be72005-09-09 13:03:32 -07003858/*
Christoph Lameter0718dc22006-03-25 03:06:47 -08003859 * This initializes kmem_list3 or resizes varioius caches for all nodes.
Christoph Lametere498be72005-09-09 13:03:32 -07003860 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003861static int alloc_kmemlist(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07003862{
3863 int node;
3864 struct kmem_list3 *l3;
Christoph Lametercafeb022006-03-25 03:06:46 -08003865 struct array_cache *new_shared;
Paul Menage3395ee02006-12-06 20:32:16 -08003866 struct array_cache **new_alien = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003867
3868 for_each_online_node(node) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003869
Paul Menage3395ee02006-12-06 20:32:16 -08003870 if (use_alien_caches) {
3871 new_alien = alloc_alien_cache(node, cachep->limit);
3872 if (!new_alien)
3873 goto fail;
3874 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003875
Eric Dumazet63109842007-05-06 14:49:28 -07003876 new_shared = NULL;
3877 if (cachep->shared) {
3878 new_shared = alloc_arraycache(node,
Christoph Lameter0718dc22006-03-25 03:06:47 -08003879 cachep->shared*cachep->batchcount,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003880 0xbaadf00d);
Eric Dumazet63109842007-05-06 14:49:28 -07003881 if (!new_shared) {
3882 free_alien_cache(new_alien);
3883 goto fail;
3884 }
Christoph Lameter0718dc22006-03-25 03:06:47 -08003885 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003886
Andrew Mortona737b3e2006-03-22 00:08:11 -08003887 l3 = cachep->nodelists[node];
3888 if (l3) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003889 struct array_cache *shared = l3->shared;
3890
Christoph Lametere498be72005-09-09 13:03:32 -07003891 spin_lock_irq(&l3->list_lock);
3892
Christoph Lametercafeb022006-03-25 03:06:46 -08003893 if (shared)
Christoph Lameter0718dc22006-03-25 03:06:47 -08003894 free_block(cachep, shared->entry,
3895 shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07003896
Christoph Lametercafeb022006-03-25 03:06:46 -08003897 l3->shared = new_shared;
3898 if (!l3->alien) {
Christoph Lametere498be72005-09-09 13:03:32 -07003899 l3->alien = new_alien;
3900 new_alien = NULL;
3901 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003902 l3->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003903 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07003904 spin_unlock_irq(&l3->list_lock);
Christoph Lametercafeb022006-03-25 03:06:46 -08003905 kfree(shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003906 free_alien_cache(new_alien);
3907 continue;
3908 }
Andrew Mortona737b3e2006-03-22 00:08:11 -08003909 l3 = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, node);
Christoph Lameter0718dc22006-03-25 03:06:47 -08003910 if (!l3) {
3911 free_alien_cache(new_alien);
3912 kfree(new_shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003913 goto fail;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003914 }
Christoph Lametere498be72005-09-09 13:03:32 -07003915
3916 kmem_list3_init(l3);
3917 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
Andrew Mortona737b3e2006-03-22 00:08:11 -08003918 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lametercafeb022006-03-25 03:06:46 -08003919 l3->shared = new_shared;
Christoph Lametere498be72005-09-09 13:03:32 -07003920 l3->alien = new_alien;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003921 l3->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003922 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07003923 cachep->nodelists[node] = l3;
3924 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003925 return 0;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003926
Andrew Mortona737b3e2006-03-22 00:08:11 -08003927fail:
Christoph Lameter0718dc22006-03-25 03:06:47 -08003928 if (!cachep->next.next) {
3929 /* Cache is not active yet. Roll back what we did */
3930 node--;
3931 while (node >= 0) {
3932 if (cachep->nodelists[node]) {
3933 l3 = cachep->nodelists[node];
3934
3935 kfree(l3->shared);
3936 free_alien_cache(l3->alien);
3937 kfree(l3);
3938 cachep->nodelists[node] = NULL;
3939 }
3940 node--;
3941 }
3942 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003943 return -ENOMEM;
Christoph Lametere498be72005-09-09 13:03:32 -07003944}
3945
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946struct ccupdate_struct {
Pekka Enberg343e0d72006-02-01 03:05:50 -08003947 struct kmem_cache *cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 struct array_cache *new[NR_CPUS];
3949};
3950
3951static void do_ccupdate_local(void *info)
3952{
Andrew Mortona737b3e2006-03-22 00:08:11 -08003953 struct ccupdate_struct *new = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954 struct array_cache *old;
3955
3956 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003957 old = cpu_cache_get(new->cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003958
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
3960 new->new[smp_processor_id()] = old;
3961}
3962
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -08003963/* Always called with the cache_chain_mutex held */
Andrew Mortona737b3e2006-03-22 00:08:11 -08003964static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
3965 int batchcount, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966{
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003967 struct ccupdate_struct *new;
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07003968 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003970 new = kzalloc(sizeof(*new), GFP_KERNEL);
3971 if (!new)
3972 return -ENOMEM;
3973
Christoph Lametere498be72005-09-09 13:03:32 -07003974 for_each_online_cpu(i) {
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003975 new->new[i] = alloc_arraycache(cpu_to_node(i), limit,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003976 batchcount);
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003977 if (!new->new[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003978 for (i--; i >= 0; i--)
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003979 kfree(new->new[i]);
3980 kfree(new);
Christoph Lametere498be72005-09-09 13:03:32 -07003981 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 }
3983 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003984 new->cachep = cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003986 on_each_cpu(do_ccupdate_local, (void *)new, 1, 1);
Christoph Lametere498be72005-09-09 13:03:32 -07003987
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988 check_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989 cachep->batchcount = batchcount;
3990 cachep->limit = limit;
Christoph Lametere498be72005-09-09 13:03:32 -07003991 cachep->shared = shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992
Christoph Lametere498be72005-09-09 13:03:32 -07003993 for_each_online_cpu(i) {
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07003994 struct array_cache *ccold = new->new[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 if (!ccold)
3996 continue;
Christoph Lametere498be72005-09-09 13:03:32 -07003997 spin_lock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
Christoph Lameterff694162005-09-22 21:44:02 -07003998 free_block(cachep, ccold->entry, ccold->avail, cpu_to_node(i));
Christoph Lametere498be72005-09-09 13:03:32 -07003999 spin_unlock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 kfree(ccold);
4001 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004002 kfree(new);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07004003 return alloc_kmemlist(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004}
4005
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -08004006/* Called with cache_chain_mutex held always */
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07004007static int enable_cpucache(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008{
4009 int err;
4010 int limit, shared;
4011
Andrew Mortona737b3e2006-03-22 00:08:11 -08004012 /*
4013 * The head array serves three purposes:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004014 * - create a LIFO ordering, i.e. return objects that are cache-warm
4015 * - reduce the number of spinlock operations.
Andrew Mortona737b3e2006-03-22 00:08:11 -08004016 * - reduce the number of linked list operations on the slab and
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 * bufctl chains: array operations are cheaper.
4018 * The numbers are guessed, we should auto-tune as described by
4019 * Bonwick.
4020 */
Manfred Spraul3dafccf2006-02-01 03:05:42 -08004021 if (cachep->buffer_size > 131072)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 limit = 1;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08004023 else if (cachep->buffer_size > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024 limit = 8;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08004025 else if (cachep->buffer_size > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 limit = 24;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08004027 else if (cachep->buffer_size > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 limit = 54;
4029 else
4030 limit = 120;
4031
Andrew Mortona737b3e2006-03-22 00:08:11 -08004032 /*
4033 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
Linus Torvalds1da177e2005-04-16 15:20:36 -07004034 * allocation behaviour: Most allocs on one cpu, most free operations
4035 * on another cpu. For these cases, an efficient object passing between
4036 * cpus is necessary. This is provided by a shared array. The array
4037 * replaces Bonwick's magazine layer.
4038 * On uniprocessor, it's functionally equivalent (but less efficient)
4039 * to a larger limit. Thus disabled by default.
4040 */
4041 shared = 0;
Eric Dumazet364fbb22007-05-06 14:49:27 -07004042 if (cachep->buffer_size <= PAGE_SIZE && num_possible_cpus() > 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 shared = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044
4045#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08004046 /*
4047 * With debugging enabled, large batchcount lead to excessively long
4048 * periods with disabled local interrupts. Limit the batchcount
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 */
4050 if (limit > 32)
4051 limit = 32;
4052#endif
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004053 err = do_tune_cpucache(cachep, limit, (limit + 1) / 2, shared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 if (err)
4055 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004056 cachep->name, -err);
Christoph Lameter2ed3a4e2006-09-25 23:31:38 -07004057 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058}
4059
Christoph Lameter1b552532006-03-22 00:09:07 -08004060/*
4061 * Drain an array if it contains any elements taking the l3 lock only if
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004062 * necessary. Note that the l3 listlock also protects the array_cache
4063 * if drain_array() is used on the shared array.
Christoph Lameter1b552532006-03-22 00:09:07 -08004064 */
4065void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
4066 struct array_cache *ac, int force, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067{
4068 int tofree;
4069
Christoph Lameter1b552532006-03-22 00:09:07 -08004070 if (!ac || !ac->avail)
4071 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 if (ac->touched && !force) {
4073 ac->touched = 0;
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004074 } else {
Christoph Lameter1b552532006-03-22 00:09:07 -08004075 spin_lock_irq(&l3->list_lock);
Christoph Lameterb18e7e62006-03-22 00:09:07 -08004076 if (ac->avail) {
4077 tofree = force ? ac->avail : (ac->limit + 4) / 5;
4078 if (tofree > ac->avail)
4079 tofree = (ac->avail + 1) / 2;
4080 free_block(cachep, ac->entry, tofree, node);
4081 ac->avail -= tofree;
4082 memmove(ac->entry, &(ac->entry[tofree]),
4083 sizeof(void *) * ac->avail);
4084 }
Christoph Lameter1b552532006-03-22 00:09:07 -08004085 spin_unlock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086 }
4087}
4088
4089/**
4090 * cache_reap - Reclaim memory from caches.
Randy Dunlap05fb6bf2007-02-28 20:12:13 -08004091 * @w: work descriptor
Linus Torvalds1da177e2005-04-16 15:20:36 -07004092 *
4093 * Called from workqueue/eventd every few seconds.
4094 * Purpose:
4095 * - clear the per-cpu caches for this CPU.
4096 * - return freeable pages to the main free memory pool.
4097 *
Andrew Mortona737b3e2006-03-22 00:08:11 -08004098 * If we cannot acquire the cache chain mutex then just give up - we'll try
4099 * again on the next iteration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004101static void cache_reap(struct work_struct *w)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102{
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004103 struct kmem_cache *searchp;
Christoph Lametere498be72005-09-09 13:03:32 -07004104 struct kmem_list3 *l3;
Christoph Lameteraab22072006-03-22 00:09:06 -08004105 int node = numa_node_id();
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004106 struct delayed_work *work =
4107 container_of(w, struct delayed_work, work);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004109 if (!mutex_trylock(&cache_chain_mutex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 /* Give up. Setup the next iteration. */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004111 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004113 list_for_each_entry(searchp, &cache_chain, next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 check_irq_on();
4115
Christoph Lameter35386e32006-03-22 00:09:05 -08004116 /*
4117 * We only take the l3 lock if absolutely necessary and we
4118 * have established with reasonable certainty that
4119 * we can do some work if the lock was obtained.
4120 */
Christoph Lameteraab22072006-03-22 00:09:06 -08004121 l3 = searchp->nodelists[node];
Christoph Lameter35386e32006-03-22 00:09:05 -08004122
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004123 reap_alien(searchp, l3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124
Christoph Lameteraab22072006-03-22 00:09:06 -08004125 drain_array(searchp, l3, cpu_cache_get(searchp), 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004126
Christoph Lameter35386e32006-03-22 00:09:05 -08004127 /*
4128 * These are racy checks but it does not matter
4129 * if we skip one check or scan twice.
4130 */
Christoph Lametere498be72005-09-09 13:03:32 -07004131 if (time_after(l3->next_reap, jiffies))
Christoph Lameter35386e32006-03-22 00:09:05 -08004132 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133
Christoph Lametere498be72005-09-09 13:03:32 -07004134 l3->next_reap = jiffies + REAPTIMEOUT_LIST3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135
Christoph Lameteraab22072006-03-22 00:09:06 -08004136 drain_array(searchp, l3, l3->shared, 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137
Christoph Lametered11d9e2006-06-30 01:55:45 -07004138 if (l3->free_touched)
Christoph Lametere498be72005-09-09 13:03:32 -07004139 l3->free_touched = 0;
Christoph Lametered11d9e2006-06-30 01:55:45 -07004140 else {
4141 int freed;
4142
4143 freed = drain_freelist(searchp, l3, (l3->free_limit +
4144 5 * searchp->num - 1) / (5 * searchp->num));
4145 STATS_ADD_REAPED(searchp, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 }
Christoph Lameter35386e32006-03-22 00:09:05 -08004147next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 cond_resched();
4149 }
4150 check_irq_on();
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004151 mutex_unlock(&cache_chain_mutex);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08004152 next_reap_node();
Christoph Lameter2244b952006-06-30 01:55:33 -07004153 refresh_cpu_vm_stats(smp_processor_id());
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004154out:
Andrew Mortona737b3e2006-03-22 00:08:11 -08004155 /* Set up the next iteration */
Christoph Lameter7c5cae32007-02-10 01:42:55 -08004156 schedule_delayed_work(work, round_jiffies_relative(REAPTIMEOUT_CPUC));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157}
4158
4159#ifdef CONFIG_PROC_FS
4160
Pekka Enberg85289f92006-01-08 01:00:36 -08004161static void print_slabinfo_header(struct seq_file *m)
4162{
4163 /*
4164 * Output format version, so at least we can change it
4165 * without _too_ many complaints.
4166 */
4167#if STATS
4168 seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
4169#else
4170 seq_puts(m, "slabinfo - version: 2.1\n");
4171#endif
4172 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
4173 "<objperslab> <pagesperslab>");
4174 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
4175 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
4176#if STATS
4177 seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004178 "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
Pekka Enberg85289f92006-01-08 01:00:36 -08004179 seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
4180#endif
4181 seq_putc(m, '\n');
4182}
4183
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184static void *s_start(struct seq_file *m, loff_t *pos)
4185{
4186 loff_t n = *pos;
4187 struct list_head *p;
4188
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004189 mutex_lock(&cache_chain_mutex);
Pekka Enberg85289f92006-01-08 01:00:36 -08004190 if (!n)
4191 print_slabinfo_header(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 p = cache_chain.next;
4193 while (n--) {
4194 p = p->next;
4195 if (p == &cache_chain)
4196 return NULL;
4197 }
Pekka Enberg343e0d72006-02-01 03:05:50 -08004198 return list_entry(p, struct kmem_cache, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199}
4200
4201static void *s_next(struct seq_file *m, void *p, loff_t *pos)
4202{
Pekka Enberg343e0d72006-02-01 03:05:50 -08004203 struct kmem_cache *cachep = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 ++*pos;
Andrew Mortona737b3e2006-03-22 00:08:11 -08004205 return cachep->next.next == &cache_chain ?
4206 NULL : list_entry(cachep->next.next, struct kmem_cache, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207}
4208
4209static void s_stop(struct seq_file *m, void *p)
4210{
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004211 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212}
4213
4214static int s_show(struct seq_file *m, void *p)
4215{
Pekka Enberg343e0d72006-02-01 03:05:50 -08004216 struct kmem_cache *cachep = p;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004217 struct slab *slabp;
4218 unsigned long active_objs;
4219 unsigned long num_objs;
4220 unsigned long active_slabs = 0;
4221 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004222 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 char *error = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07004224 int node;
4225 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 active_objs = 0;
4228 num_slabs = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07004229 for_each_online_node(node) {
4230 l3 = cachep->nodelists[node];
4231 if (!l3)
4232 continue;
4233
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004234 check_irq_on();
4235 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07004236
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004237 list_for_each_entry(slabp, &l3->slabs_full, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004238 if (slabp->inuse != cachep->num && !error)
4239 error = "slabs_full accounting error";
4240 active_objs += cachep->num;
4241 active_slabs++;
4242 }
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004243 list_for_each_entry(slabp, &l3->slabs_partial, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004244 if (slabp->inuse == cachep->num && !error)
4245 error = "slabs_partial inuse accounting error";
4246 if (!slabp->inuse && !error)
4247 error = "slabs_partial/inuse accounting error";
4248 active_objs += slabp->inuse;
4249 active_slabs++;
4250 }
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004251 list_for_each_entry(slabp, &l3->slabs_free, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07004252 if (slabp->inuse && !error)
4253 error = "slabs_free/inuse accounting error";
4254 num_slabs++;
4255 }
4256 free_objects += l3->free_objects;
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08004257 if (l3->shared)
4258 shared_avail += l3->shared->avail;
Christoph Lametere498be72005-09-09 13:03:32 -07004259
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08004260 spin_unlock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004262 num_slabs += active_slabs;
4263 num_objs = num_slabs * cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07004264 if (num_objs - active_objs != free_objects && !error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265 error = "free_objects accounting error";
4266
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004267 name = cachep->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 if (error)
4269 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
4270
4271 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
Manfred Spraul3dafccf2006-02-01 03:05:42 -08004272 name, active_objs, num_objs, cachep->buffer_size,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004273 cachep->num, (1 << cachep->gfporder));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 seq_printf(m, " : tunables %4u %4u %4u",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004275 cachep->limit, cachep->batchcount, cachep->shared);
Christoph Lametere498be72005-09-09 13:03:32 -07004276 seq_printf(m, " : slabdata %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004277 active_slabs, num_slabs, shared_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278#if STATS
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004279 { /* list3 stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280 unsigned long high = cachep->high_mark;
4281 unsigned long allocs = cachep->num_allocations;
4282 unsigned long grown = cachep->grown;
4283 unsigned long reaped = cachep->reaped;
4284 unsigned long errors = cachep->errors;
4285 unsigned long max_freeable = cachep->max_freeable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 unsigned long node_allocs = cachep->node_allocs;
Christoph Lametere498be72005-09-09 13:03:32 -07004287 unsigned long node_frees = cachep->node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004288 unsigned long overflows = cachep->node_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289
Christoph Lametere498be72005-09-09 13:03:32 -07004290 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu \
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004291 %4lu %4lu %4lu %4lu %4lu", allocs, high, grown,
Andrew Mortona737b3e2006-03-22 00:08:11 -08004292 reaped, errors, max_freeable, node_allocs,
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07004293 node_frees, overflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004294 }
4295 /* cpu stats */
4296 {
4297 unsigned long allochit = atomic_read(&cachep->allochit);
4298 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
4299 unsigned long freehit = atomic_read(&cachep->freehit);
4300 unsigned long freemiss = atomic_read(&cachep->freemiss);
4301
4302 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004303 allochit, allocmiss, freehit, freemiss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 }
4305#endif
4306 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 return 0;
4308}
4309
4310/*
4311 * slabinfo_op - iterator that generates /proc/slabinfo
4312 *
4313 * Output layout:
4314 * cache-name
4315 * num-active-objs
4316 * total-objs
4317 * object size
4318 * num-active-slabs
4319 * total-slabs
4320 * num-pages-per-slab
4321 * + further values on SMP and with statistics enabled
4322 */
4323
Helge Deller15ad7cd2006-12-06 20:40:36 -08004324const struct seq_operations slabinfo_op = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004325 .start = s_start,
4326 .next = s_next,
4327 .stop = s_stop,
4328 .show = s_show,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329};
4330
4331#define MAX_SLABINFO_WRITE 128
4332/**
4333 * slabinfo_write - Tuning for the slab allocator
4334 * @file: unused
4335 * @buffer: user buffer
4336 * @count: data length
4337 * @ppos: unused
4338 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004339ssize_t slabinfo_write(struct file *file, const char __user * buffer,
4340 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004342 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 int limit, batchcount, shared, res;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004344 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004345
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346 if (count > MAX_SLABINFO_WRITE)
4347 return -EINVAL;
4348 if (copy_from_user(&kbuf, buffer, count))
4349 return -EFAULT;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004350 kbuf[MAX_SLABINFO_WRITE] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07004351
4352 tmp = strchr(kbuf, ' ');
4353 if (!tmp)
4354 return -EINVAL;
4355 *tmp = '\0';
4356 tmp++;
4357 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
4358 return -EINVAL;
4359
4360 /* Find the cache in the chain of caches. */
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004361 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 res = -EINVAL;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004363 list_for_each_entry(cachep, &cache_chain, next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 if (!strcmp(cachep->name, kbuf)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08004365 if (limit < 1 || batchcount < 1 ||
4366 batchcount > limit || shared < 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07004367 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07004369 res = do_tune_cpucache(cachep, limit,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004370 batchcount, shared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 }
4372 break;
4373 }
4374 }
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004375 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 if (res >= 0)
4377 res = count;
4378 return res;
4379}
Al Viro871751e2006-03-25 03:06:39 -08004380
4381#ifdef CONFIG_DEBUG_SLAB_LEAK
4382
4383static void *leaks_start(struct seq_file *m, loff_t *pos)
4384{
4385 loff_t n = *pos;
4386 struct list_head *p;
4387
4388 mutex_lock(&cache_chain_mutex);
4389 p = cache_chain.next;
4390 while (n--) {
4391 p = p->next;
4392 if (p == &cache_chain)
4393 return NULL;
4394 }
4395 return list_entry(p, struct kmem_cache, next);
4396}
4397
4398static inline int add_caller(unsigned long *n, unsigned long v)
4399{
4400 unsigned long *p;
4401 int l;
4402 if (!v)
4403 return 1;
4404 l = n[1];
4405 p = n + 2;
4406 while (l) {
4407 int i = l/2;
4408 unsigned long *q = p + 2 * i;
4409 if (*q == v) {
4410 q[1]++;
4411 return 1;
4412 }
4413 if (*q > v) {
4414 l = i;
4415 } else {
4416 p = q + 2;
4417 l -= i + 1;
4418 }
4419 }
4420 if (++n[1] == n[0])
4421 return 0;
4422 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4423 p[0] = v;
4424 p[1] = 1;
4425 return 1;
4426}
4427
4428static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
4429{
4430 void *p;
4431 int i;
4432 if (n[0] == n[1])
4433 return;
4434 for (i = 0, p = s->s_mem; i < c->num; i++, p += c->buffer_size) {
4435 if (slab_bufctl(s)[i] != BUFCTL_ACTIVE)
4436 continue;
4437 if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
4438 return;
4439 }
4440}
4441
4442static void show_symbol(struct seq_file *m, unsigned long address)
4443{
4444#ifdef CONFIG_KALLSYMS
Al Viro871751e2006-03-25 03:06:39 -08004445 unsigned long offset, size;
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004446 char modname[MODULE_NAME_LEN + 1], name[KSYM_NAME_LEN + 1];
Al Viro871751e2006-03-25 03:06:39 -08004447
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004448 if (lookup_symbol_attrs(address, &size, &offset, modname, name) == 0) {
Al Viro871751e2006-03-25 03:06:39 -08004449 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
Alexey Dobriyana5c43da2007-05-08 00:28:47 -07004450 if (modname[0])
Al Viro871751e2006-03-25 03:06:39 -08004451 seq_printf(m, " [%s]", modname);
4452 return;
4453 }
4454#endif
4455 seq_printf(m, "%p", (void *)address);
4456}
4457
4458static int leaks_show(struct seq_file *m, void *p)
4459{
4460 struct kmem_cache *cachep = p;
Al Viro871751e2006-03-25 03:06:39 -08004461 struct slab *slabp;
4462 struct kmem_list3 *l3;
4463 const char *name;
4464 unsigned long *n = m->private;
4465 int node;
4466 int i;
4467
4468 if (!(cachep->flags & SLAB_STORE_USER))
4469 return 0;
4470 if (!(cachep->flags & SLAB_RED_ZONE))
4471 return 0;
4472
4473 /* OK, we can do it */
4474
4475 n[1] = 0;
4476
4477 for_each_online_node(node) {
4478 l3 = cachep->nodelists[node];
4479 if (!l3)
4480 continue;
4481
4482 check_irq_on();
4483 spin_lock_irq(&l3->list_lock);
4484
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004485 list_for_each_entry(slabp, &l3->slabs_full, list)
Al Viro871751e2006-03-25 03:06:39 -08004486 handle_slab(n, cachep, slabp);
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004487 list_for_each_entry(slabp, &l3->slabs_partial, list)
Al Viro871751e2006-03-25 03:06:39 -08004488 handle_slab(n, cachep, slabp);
Al Viro871751e2006-03-25 03:06:39 -08004489 spin_unlock_irq(&l3->list_lock);
4490 }
4491 name = cachep->name;
4492 if (n[0] == n[1]) {
4493 /* Increase the buffer size */
4494 mutex_unlock(&cache_chain_mutex);
4495 m->private = kzalloc(n[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
4496 if (!m->private) {
4497 /* Too bad, we are really out */
4498 m->private = n;
4499 mutex_lock(&cache_chain_mutex);
4500 return -ENOMEM;
4501 }
4502 *(unsigned long *)m->private = n[0] * 2;
4503 kfree(n);
4504 mutex_lock(&cache_chain_mutex);
4505 /* Now make sure this entry will be retried */
4506 m->count = m->size;
4507 return 0;
4508 }
4509 for (i = 0; i < n[1]; i++) {
4510 seq_printf(m, "%s: %lu ", name, n[2*i+3]);
4511 show_symbol(m, n[2*i+2]);
4512 seq_putc(m, '\n');
4513 }
Siddha, Suresh Bd2e7b7d2006-09-25 23:31:47 -07004514
Al Viro871751e2006-03-25 03:06:39 -08004515 return 0;
4516}
4517
Helge Deller15ad7cd2006-12-06 20:40:36 -08004518const struct seq_operations slabstats_op = {
Al Viro871751e2006-03-25 03:06:39 -08004519 .start = leaks_start,
4520 .next = s_next,
4521 .stop = s_stop,
4522 .show = leaks_show,
4523};
4524#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525#endif
4526
Manfred Spraul00e145b2005-09-03 15:55:07 -07004527/**
4528 * ksize - get the actual amount of memory allocated for a given object
4529 * @objp: Pointer to the object
4530 *
4531 * kmalloc may internally round up allocations and return more memory
4532 * than requested. ksize() can be used to determine the actual amount of
4533 * memory allocated. The caller may use this additional memory, even though
4534 * a smaller amount of memory was initially specified with the kmalloc call.
4535 * The caller must guarantee that objp points to a valid object previously
4536 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4537 * must not be freed during the duration of the call.
4538 */
Pekka Enbergfd76bab2007-05-06 14:48:40 -07004539size_t ksize(const void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540{
Manfred Spraul00e145b2005-09-03 15:55:07 -07004541 if (unlikely(objp == NULL))
4542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543
Pekka Enberg6ed5eb2212006-02-01 03:05:49 -08004544 return obj_size(virt_to_cache(objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545}