blob: 3936af3445427b6fa71e48dea318ce302b2b0900 [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
89#include <linux/config.h>
90#include <linux/slab.h>
91#include <linux/mm.h>
Randy Dunlapc9cf5522006-06-27 02:53:52 -070092#include <linux/poison.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#include <linux/swap.h>
94#include <linux/cache.h>
95#include <linux/interrupt.h>
96#include <linux/init.h>
97#include <linux/compiler.h>
Paul Jackson101a5002006-03-24 03:16:07 -080098#include <linux/cpuset.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070099#include <linux/seq_file.h>
100#include <linux/notifier.h>
101#include <linux/kallsyms.h>
102#include <linux/cpu.h>
103#include <linux/sysctl.h>
104#include <linux/module.h>
105#include <linux/rcupdate.h>
Paulo Marques543537b2005-06-23 00:09:02 -0700106#include <linux/string.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>
Ingo Molnare7eebaf2006-06-27 02:54:55 -0700110#include <linux/rtmutex.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112#include <asm/uaccess.h>
113#include <asm/cacheflush.h>
114#include <asm/tlbflush.h>
115#include <asm/page.h>
116
117/*
118 * DEBUG - 1 for kmem_cache_create() to honour; SLAB_DEBUG_INITIAL,
119 * SLAB_RED_ZONE & SLAB_POISON.
120 * 0 for faster, smaller code (especially in the critical paths).
121 *
122 * STATS - 1 to collect stats for /proc/slabinfo.
123 * 0 for faster, smaller code (especially in the critical paths).
124 *
125 * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible)
126 */
127
128#ifdef CONFIG_DEBUG_SLAB
129#define DEBUG 1
130#define STATS 1
131#define FORCED_DEBUG 1
132#else
133#define DEBUG 0
134#define STATS 0
135#define FORCED_DEBUG 0
136#endif
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138/* Shouldn't this be in a header file somewhere? */
139#define BYTES_PER_WORD sizeof(void *)
140
141#ifndef cache_line_size
142#define cache_line_size() L1_CACHE_BYTES
143#endif
144
145#ifndef ARCH_KMALLOC_MINALIGN
146/*
147 * Enforce a minimum alignment for the kmalloc caches.
148 * Usually, the kmalloc caches are cache_line_size() aligned, except when
149 * DEBUG and FORCED_DEBUG are enabled, then they are BYTES_PER_WORD aligned.
150 * Some archs want to perform DMA into kmalloc caches and need a guaranteed
151 * alignment larger than BYTES_PER_WORD. ARCH_KMALLOC_MINALIGN allows that.
152 * Note that this flag disables some debug features.
153 */
154#define ARCH_KMALLOC_MINALIGN 0
155#endif
156
157#ifndef ARCH_SLAB_MINALIGN
158/*
159 * Enforce a minimum alignment for all caches.
160 * Intended for archs that get misalignment faults even for BYTES_PER_WORD
161 * aligned buffers. Includes ARCH_KMALLOC_MINALIGN.
162 * If possible: Do not enable this flag for CONFIG_DEBUG_SLAB, it disables
163 * some debug features.
164 */
165#define ARCH_SLAB_MINALIGN 0
166#endif
167
168#ifndef ARCH_KMALLOC_FLAGS
169#define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN
170#endif
171
172/* Legal flag mask for kmem_cache_create(). */
173#if DEBUG
174# define CREATE_MASK (SLAB_DEBUG_INITIAL | SLAB_RED_ZONE | \
175 SLAB_POISON | SLAB_HWCACHE_ALIGN | \
Christoph Lameterac2b8982006-03-22 00:08:15 -0800176 SLAB_CACHE_DMA | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 SLAB_MUST_HWCACHE_ALIGN | SLAB_STORE_USER | \
178 SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
Paul Jackson101a5002006-03-24 03:16:07 -0800179 SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180#else
Christoph Lameterac2b8982006-03-22 00:08:15 -0800181# define CREATE_MASK (SLAB_HWCACHE_ALIGN | \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 SLAB_CACHE_DMA | SLAB_MUST_HWCACHE_ALIGN | \
183 SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \
Paul Jackson101a5002006-03-24 03:16:07 -0800184 SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185#endif
186
187/*
188 * kmem_bufctl_t:
189 *
190 * Bufctl's are used for linking objs within a slab
191 * linked offsets.
192 *
193 * This implementation relies on "struct page" for locating the cache &
194 * slab an object belongs to.
195 * This allows the bufctl structure to be small (one int), but limits
196 * the number of objects a slab (not a cache) can contain when off-slab
197 * bufctls are used. The limit is the size of the largest general cache
198 * that does not use off-slab slabs.
199 * For 32bit archs with 4 kB pages, is this 56.
200 * This is not serious, as it is only for large objects, when it is unwise
201 * to have too many per slab.
202 * Note: This limit can be raised by introducing a general cache whose size
203 * is less than 512 (PAGE_SIZE<<3), but greater than 256.
204 */
205
Kyle Moffettfa5b08d2005-09-03 15:55:03 -0700206typedef unsigned int kmem_bufctl_t;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207#define BUFCTL_END (((kmem_bufctl_t)(~0U))-0)
208#define BUFCTL_FREE (((kmem_bufctl_t)(~0U))-1)
Al Viro871751e2006-03-25 03:06:39 -0800209#define BUFCTL_ACTIVE (((kmem_bufctl_t)(~0U))-2)
210#define SLAB_LIMIT (((kmem_bufctl_t)(~0U))-3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212/*
213 * struct slab
214 *
215 * Manages the objs in a slab. Placed either at the beginning of mem allocated
216 * for a slab, or allocated from an general cache.
217 * Slabs are chained into three list: fully used, partial, fully free slabs.
218 */
219struct slab {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800220 struct list_head list;
221 unsigned long colouroff;
222 void *s_mem; /* including colour offset */
223 unsigned int inuse; /* num of objs active in slab */
224 kmem_bufctl_t free;
225 unsigned short nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226};
227
228/*
229 * struct slab_rcu
230 *
231 * slab_destroy on a SLAB_DESTROY_BY_RCU cache uses this structure to
232 * arrange for kmem_freepages to be called via RCU. This is useful if
233 * we need to approach a kernel structure obliquely, from its address
234 * obtained without the usual locking. We can lock the structure to
235 * stabilize it and check it's still at the given address, only if we
236 * can be sure that the memory has not been meanwhile reused for some
237 * other kind of object (which our subsystem's lock might corrupt).
238 *
239 * rcu_read_lock before reading the address, then rcu_read_unlock after
240 * taking the spinlock within the structure expected at that address.
241 *
242 * We assume struct slab_rcu can overlay struct slab when destroying.
243 */
244struct slab_rcu {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800245 struct rcu_head head;
Pekka Enberg343e0d72006-02-01 03:05:50 -0800246 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800247 void *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248};
249
250/*
251 * struct array_cache
252 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 * Purpose:
254 * - LIFO ordering, to hand out cache-warm objects from _alloc
255 * - reduce the number of linked list operations
256 * - reduce spinlock operations
257 *
258 * The limit is stored in the per-cpu structure to reduce the data cache
259 * footprint.
260 *
261 */
262struct array_cache {
263 unsigned int avail;
264 unsigned int limit;
265 unsigned int batchcount;
266 unsigned int touched;
Christoph Lametere498be72005-09-09 13:03:32 -0700267 spinlock_t lock;
Andrew Mortona737b3e2006-03-22 00:08:11 -0800268 void *entry[0]; /*
269 * Must have this definition in here for the proper
270 * alignment of array_cache. Also simplifies accessing
271 * the entries.
272 * [0] is for gcc 2.95. It should really be [].
273 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274};
275
Andrew Mortona737b3e2006-03-22 00:08:11 -0800276/*
277 * bootstrap: The caches do not work without cpuarrays anymore, but the
278 * cpuarrays are allocated from the generic caches...
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 */
280#define BOOT_CPUCACHE_ENTRIES 1
281struct arraycache_init {
282 struct array_cache cache;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800283 void *entries[BOOT_CPUCACHE_ENTRIES];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284};
285
286/*
Christoph Lametere498be72005-09-09 13:03:32 -0700287 * The slab lists for all objects.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 */
289struct kmem_list3 {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800290 struct list_head slabs_partial; /* partial list first, better asm code */
291 struct list_head slabs_full;
292 struct list_head slabs_free;
293 unsigned long free_objects;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800294 unsigned int free_limit;
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -0800295 unsigned int colour_next; /* Per-node cache coloring */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800296 spinlock_t list_lock;
297 struct array_cache *shared; /* shared per node */
298 struct array_cache **alien; /* on other nodes */
Christoph Lameter35386e32006-03-22 00:09:05 -0800299 unsigned long next_reap; /* updated without locking */
300 int free_touched; /* updated without locking */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301};
302
Christoph Lametere498be72005-09-09 13:03:32 -0700303/*
304 * Need this for bootstrapping a per node allocator.
305 */
306#define NUM_INIT_LISTS (2 * MAX_NUMNODES + 1)
307struct kmem_list3 __initdata initkmem_list3[NUM_INIT_LISTS];
308#define CACHE_CACHE 0
309#define SIZE_AC 1
310#define SIZE_L3 (1 + MAX_NUMNODES)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Christoph Lametered11d9e2006-06-30 01:55:45 -0700312static int drain_freelist(struct kmem_cache *cache,
313 struct kmem_list3 *l3, int tofree);
314static void free_block(struct kmem_cache *cachep, void **objpp, int len,
315 int node);
316static void enable_cpucache(struct kmem_cache *cachep);
317static void cache_reap(void *unused);
318
Christoph Lametere498be72005-09-09 13:03:32 -0700319/*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800320 * This function must be completely optimized away if a constant is passed to
321 * it. Mostly the same as what is in linux/slab.h except it returns an index.
Christoph Lametere498be72005-09-09 13:03:32 -0700322 */
Ivan Kokshaysky7243cc02005-09-22 21:43:58 -0700323static __always_inline int index_of(const size_t size)
Christoph Lametere498be72005-09-09 13:03:32 -0700324{
Steven Rostedt5ec8a842006-02-01 03:05:44 -0800325 extern void __bad_size(void);
326
Christoph Lametere498be72005-09-09 13:03:32 -0700327 if (__builtin_constant_p(size)) {
328 int i = 0;
329
330#define CACHE(x) \
331 if (size <=x) \
332 return i; \
333 else \
334 i++;
335#include "linux/kmalloc_sizes.h"
336#undef CACHE
Steven Rostedt5ec8a842006-02-01 03:05:44 -0800337 __bad_size();
Ivan Kokshaysky7243cc02005-09-22 21:43:58 -0700338 } else
Steven Rostedt5ec8a842006-02-01 03:05:44 -0800339 __bad_size();
Christoph Lametere498be72005-09-09 13:03:32 -0700340 return 0;
341}
342
Ingo Molnare0a42722006-06-23 02:03:46 -0700343static int slab_early_init = 1;
344
Christoph Lametere498be72005-09-09 13:03:32 -0700345#define INDEX_AC index_of(sizeof(struct arraycache_init))
346#define INDEX_L3 index_of(sizeof(struct kmem_list3))
347
Pekka Enberg5295a742006-02-01 03:05:48 -0800348static void kmem_list3_init(struct kmem_list3 *parent)
Christoph Lametere498be72005-09-09 13:03:32 -0700349{
350 INIT_LIST_HEAD(&parent->slabs_full);
351 INIT_LIST_HEAD(&parent->slabs_partial);
352 INIT_LIST_HEAD(&parent->slabs_free);
353 parent->shared = NULL;
354 parent->alien = NULL;
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -0800355 parent->colour_next = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700356 spin_lock_init(&parent->list_lock);
357 parent->free_objects = 0;
358 parent->free_touched = 0;
359}
360
Andrew Mortona737b3e2006-03-22 00:08:11 -0800361#define MAKE_LIST(cachep, listp, slab, nodeid) \
362 do { \
363 INIT_LIST_HEAD(listp); \
364 list_splice(&(cachep->nodelists[nodeid]->slab), listp); \
Christoph Lametere498be72005-09-09 13:03:32 -0700365 } while (0)
366
Andrew Mortona737b3e2006-03-22 00:08:11 -0800367#define MAKE_ALL_LISTS(cachep, ptr, nodeid) \
368 do { \
Christoph Lametere498be72005-09-09 13:03:32 -0700369 MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \
370 MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \
371 MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \
372 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374/*
Pekka Enberg343e0d72006-02-01 03:05:50 -0800375 * struct kmem_cache
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 *
377 * manages a cache.
378 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800379
Pekka J Enberg2109a2d2005-11-07 00:58:01 -0800380struct kmem_cache {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/* 1) per-cpu data, touched during every alloc/free */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800382 struct array_cache *array[NR_CPUS];
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800383/* 2) Cache tunables. Protected by cache_chain_mutex */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800384 unsigned int batchcount;
385 unsigned int limit;
386 unsigned int shared;
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800387
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800388 unsigned int buffer_size;
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800389/* 3) touched by every alloc & free from the backend */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800390 struct kmem_list3 *nodelists[MAX_NUMNODES];
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800391
Andrew Mortona737b3e2006-03-22 00:08:11 -0800392 unsigned int flags; /* constant flags */
393 unsigned int num; /* # of objs per slab */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800395/* 4) cache_grow/shrink */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 /* order of pgs per slab (2^n) */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800397 unsigned int gfporder;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 /* force GFP flags, e.g. GFP_DMA */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800400 gfp_t gfpflags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Andrew Mortona737b3e2006-03-22 00:08:11 -0800402 size_t colour; /* cache colouring range */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800403 unsigned int colour_off; /* colour offset */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800404 struct kmem_cache *slabp_cache;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800405 unsigned int slab_size;
Andrew Mortona737b3e2006-03-22 00:08:11 -0800406 unsigned int dflags; /* dynamic flags */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 /* constructor func */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800409 void (*ctor) (void *, struct kmem_cache *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /* de-constructor func */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800412 void (*dtor) (void *, struct kmem_cache *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800414/* 5) cache creation/removal */
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800415 const char *name;
416 struct list_head next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -0800418/* 6) statistics */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419#if STATS
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800420 unsigned long num_active;
421 unsigned long num_allocations;
422 unsigned long high_mark;
423 unsigned long grown;
424 unsigned long reaped;
425 unsigned long errors;
426 unsigned long max_freeable;
427 unsigned long node_allocs;
428 unsigned long node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700429 unsigned long node_overflow;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800430 atomic_t allochit;
431 atomic_t allocmiss;
432 atomic_t freehit;
433 atomic_t freemiss;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434#endif
435#if DEBUG
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800436 /*
437 * If debugging is enabled, then the allocator can add additional
438 * fields and/or padding to every object. buffer_size contains the total
439 * object size including these internal fields, the following two
440 * variables contain the offset to the user object and its size.
441 */
442 int obj_offset;
443 int obj_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444#endif
445};
446
447#define CFLGS_OFF_SLAB (0x80000000UL)
448#define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB)
449
450#define BATCHREFILL_LIMIT 16
Andrew Mortona737b3e2006-03-22 00:08:11 -0800451/*
452 * Optimization question: fewer reaps means less probability for unnessary
453 * cpucache drain/refill cycles.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 *
Adrian Bunkdc6f3f22005-11-08 16:44:08 +0100455 * OTOH the cpuarrays can contain lots of objects,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 * which could lock up otherwise freeable slabs.
457 */
458#define REAPTIMEOUT_CPUC (2*HZ)
459#define REAPTIMEOUT_LIST3 (4*HZ)
460
461#if STATS
462#define STATS_INC_ACTIVE(x) ((x)->num_active++)
463#define STATS_DEC_ACTIVE(x) ((x)->num_active--)
464#define STATS_INC_ALLOCED(x) ((x)->num_allocations++)
465#define STATS_INC_GROWN(x) ((x)->grown++)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700466#define STATS_ADD_REAPED(x,y) ((x)->reaped += (y))
Andrew Mortona737b3e2006-03-22 00:08:11 -0800467#define STATS_SET_HIGH(x) \
468 do { \
469 if ((x)->num_active > (x)->high_mark) \
470 (x)->high_mark = (x)->num_active; \
471 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472#define STATS_INC_ERR(x) ((x)->errors++)
473#define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++)
Christoph Lametere498be72005-09-09 13:03:32 -0700474#define STATS_INC_NODEFREES(x) ((x)->node_frees++)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700475#define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800476#define STATS_SET_FREEABLE(x, i) \
477 do { \
478 if ((x)->max_freeable < i) \
479 (x)->max_freeable = i; \
480 } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481#define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit)
482#define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss)
483#define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit)
484#define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss)
485#else
486#define STATS_INC_ACTIVE(x) do { } while (0)
487#define STATS_DEC_ACTIVE(x) do { } while (0)
488#define STATS_INC_ALLOCED(x) do { } while (0)
489#define STATS_INC_GROWN(x) do { } while (0)
Christoph Lametered11d9e2006-06-30 01:55:45 -0700490#define STATS_ADD_REAPED(x,y) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491#define STATS_SET_HIGH(x) do { } while (0)
492#define STATS_INC_ERR(x) do { } while (0)
493#define STATS_INC_NODEALLOCS(x) do { } while (0)
Christoph Lametere498be72005-09-09 13:03:32 -0700494#define STATS_INC_NODEFREES(x) do { } while (0)
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -0700495#define STATS_INC_ACOVERFLOW(x) do { } while (0)
Andrew Mortona737b3e2006-03-22 00:08:11 -0800496#define STATS_SET_FREEABLE(x, i) do { } while (0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497#define STATS_INC_ALLOCHIT(x) do { } while (0)
498#define STATS_INC_ALLOCMISS(x) do { } while (0)
499#define STATS_INC_FREEHIT(x) do { } while (0)
500#define STATS_INC_FREEMISS(x) do { } while (0)
501#endif
502
503#if DEBUG
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Andrew Mortona737b3e2006-03-22 00:08:11 -0800505/*
506 * memory layout of objects:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 * 0 : objp
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800508 * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 * the end of an object is aligned with the end of the real
510 * allocation. Catches writes behind the end of the allocation.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800511 * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 * redzone word.
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800513 * cachep->obj_offset: The real object.
514 * cachep->buffer_size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long]
Andrew Mortona737b3e2006-03-22 00:08:11 -0800515 * cachep->buffer_size - 1* BYTES_PER_WORD: last caller address
516 * [BYTES_PER_WORD long]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800518static int obj_offset(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800520 return cachep->obj_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Pekka Enberg343e0d72006-02-01 03:05:50 -0800523static int obj_size(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524{
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800525 return cachep->obj_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526}
527
Pekka Enberg343e0d72006-02-01 03:05:50 -0800528static unsigned long *dbg_redzone1(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
530 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800531 return (unsigned long*) (objp+obj_offset(cachep)-BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532}
533
Pekka Enberg343e0d72006-02-01 03:05:50 -0800534static unsigned long *dbg_redzone2(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 BUG_ON(!(cachep->flags & SLAB_RED_ZONE));
537 if (cachep->flags & SLAB_STORE_USER)
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800538 return (unsigned long *)(objp + cachep->buffer_size -
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800539 2 * BYTES_PER_WORD);
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800540 return (unsigned long *)(objp + cachep->buffer_size - BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Pekka Enberg343e0d72006-02-01 03:05:50 -0800543static void **dbg_userword(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 BUG_ON(!(cachep->flags & SLAB_STORE_USER));
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800546 return (void **)(objp + cachep->buffer_size - BYTES_PER_WORD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
548
549#else
550
Manfred Spraul3dafccf2006-02-01 03:05:42 -0800551#define obj_offset(x) 0
552#define obj_size(cachep) (cachep->buffer_size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553#define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long *)NULL;})
554#define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long *)NULL;})
555#define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;})
556
557#endif
558
559/*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800560 * Maximum size of an obj (in 2^order pages) and absolute limit for the gfp
561 * order.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 */
563#if defined(CONFIG_LARGE_ALLOCS)
564#define MAX_OBJ_ORDER 13 /* up to 32Mb */
565#define MAX_GFP_ORDER 13 /* up to 32Mb */
566#elif defined(CONFIG_MMU)
567#define MAX_OBJ_ORDER 5 /* 32 pages */
568#define MAX_GFP_ORDER 5 /* 32 pages */
569#else
570#define MAX_OBJ_ORDER 8 /* up to 1Mb */
571#define MAX_GFP_ORDER 8 /* up to 1Mb */
572#endif
573
574/*
575 * Do not go above this order unless 0 objects fit into the slab.
576 */
577#define BREAK_GFP_ORDER_HI 1
578#define BREAK_GFP_ORDER_LO 0
579static int slab_break_gfp_order = BREAK_GFP_ORDER_LO;
580
Andrew Mortona737b3e2006-03-22 00:08:11 -0800581/*
582 * Functions for storing/retrieving the cachep and or slab from the page
583 * allocator. These are used to find the slab an obj belongs to. With kfree(),
584 * these are used to find the cache which an obj belongs to.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 */
Pekka Enberg065d41c2005-11-13 16:06:46 -0800586static inline void page_set_cache(struct page *page, struct kmem_cache *cache)
587{
588 page->lru.next = (struct list_head *)cache;
589}
590
591static inline struct kmem_cache *page_get_cache(struct page *page)
592{
Nick Piggin84097512006-03-22 00:08:34 -0800593 if (unlikely(PageCompound(page)))
594 page = (struct page *)page_private(page);
Pekka Enbergddc2e812006-06-23 02:03:40 -0700595 BUG_ON(!PageSlab(page));
Pekka Enberg065d41c2005-11-13 16:06:46 -0800596 return (struct kmem_cache *)page->lru.next;
597}
598
599static inline void page_set_slab(struct page *page, struct slab *slab)
600{
601 page->lru.prev = (struct list_head *)slab;
602}
603
604static inline struct slab *page_get_slab(struct page *page)
605{
Nick Piggin84097512006-03-22 00:08:34 -0800606 if (unlikely(PageCompound(page)))
607 page = (struct page *)page_private(page);
Pekka Enbergddc2e812006-06-23 02:03:40 -0700608 BUG_ON(!PageSlab(page));
Pekka Enberg065d41c2005-11-13 16:06:46 -0800609 return (struct slab *)page->lru.prev;
610}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Pekka Enberg6ed5eb22006-02-01 03:05:49 -0800612static inline struct kmem_cache *virt_to_cache(const void *obj)
613{
614 struct page *page = virt_to_page(obj);
615 return page_get_cache(page);
616}
617
618static inline struct slab *virt_to_slab(const void *obj)
619{
620 struct page *page = virt_to_page(obj);
621 return page_get_slab(page);
622}
623
Pekka Enberg8fea4e92006-03-22 00:08:10 -0800624static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab,
625 unsigned int idx)
626{
627 return slab->s_mem + cache->buffer_size * idx;
628}
629
630static inline unsigned int obj_to_index(struct kmem_cache *cache,
631 struct slab *slab, void *obj)
632{
633 return (unsigned)(obj - slab->s_mem) / cache->buffer_size;
634}
635
Andrew Mortona737b3e2006-03-22 00:08:11 -0800636/*
637 * These are the default caches for kmalloc. Custom caches can have other sizes.
638 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639struct cache_sizes malloc_sizes[] = {
640#define CACHE(x) { .cs_size = (x) },
641#include <linux/kmalloc_sizes.h>
642 CACHE(ULONG_MAX)
643#undef CACHE
644};
645EXPORT_SYMBOL(malloc_sizes);
646
647/* Must match cache_sizes above. Out of line to keep cache footprint low. */
648struct cache_names {
649 char *name;
650 char *name_dma;
651};
652
653static struct cache_names __initdata cache_names[] = {
654#define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" },
655#include <linux/kmalloc_sizes.h>
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800656 {NULL,}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657#undef CACHE
658};
659
660static struct arraycache_init initarray_cache __initdata =
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800661 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662static struct arraycache_init initarray_generic =
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800663 { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
665/* internal cache of cache description objs */
Pekka Enberg343e0d72006-02-01 03:05:50 -0800666static struct kmem_cache cache_cache = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800667 .batchcount = 1,
668 .limit = BOOT_CPUCACHE_ENTRIES,
669 .shared = 1,
Pekka Enberg343e0d72006-02-01 03:05:50 -0800670 .buffer_size = sizeof(struct kmem_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800671 .name = "kmem_cache",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672#if DEBUG
Pekka Enberg343e0d72006-02-01 03:05:50 -0800673 .obj_size = sizeof(struct kmem_cache),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674#endif
675};
676
677/* Guard access to the cache-chain. */
Ingo Molnarfc0abb12006-01-18 17:42:33 -0800678static DEFINE_MUTEX(cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679static struct list_head cache_chain;
680
681/*
Andrew Mortona737b3e2006-03-22 00:08:11 -0800682 * vm_enough_memory() looks at this to determine how many slab-allocated pages
683 * are possibly freeable under pressure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 *
685 * SLAB_RECLAIM_ACCOUNT turns this on per-slab
686 */
687atomic_t slab_reclaim_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
689/*
690 * chicken and egg problem: delay the per-cpu array allocation
691 * until the general caches are up.
692 */
693static enum {
694 NONE,
Christoph Lametere498be72005-09-09 13:03:32 -0700695 PARTIAL_AC,
696 PARTIAL_L3,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 FULL
698} g_cpucache_up;
699
Mike Kravetz39d24e62006-05-15 09:44:13 -0700700/*
701 * used by boot code to determine if it can use slab based allocator
702 */
703int slab_is_available(void)
704{
705 return g_cpucache_up == FULL;
706}
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708static DEFINE_PER_CPU(struct work_struct, reap_work);
709
Pekka Enberg343e0d72006-02-01 03:05:50 -0800710static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
712 return cachep->array[smp_processor_id()];
713}
714
Andrew Mortona737b3e2006-03-22 00:08:11 -0800715static inline struct kmem_cache *__find_general_cachep(size_t size,
716 gfp_t gfpflags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 struct cache_sizes *csizep = malloc_sizes;
719
720#if DEBUG
721 /* This happens if someone tries to call
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800722 * kmem_cache_create(), or __kmalloc(), before
723 * the generic caches are initialized.
724 */
Alok Katariac7e43c72005-09-14 12:17:53 -0700725 BUG_ON(malloc_sizes[INDEX_AC].cs_cachep == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726#endif
727 while (size > csizep->cs_size)
728 csizep++;
729
730 /*
Martin Hicks0abf40c2005-09-03 15:54:54 -0700731 * Really subtle: The last entry with cs->cs_size==ULONG_MAX
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 * has cs_{dma,}cachep==NULL. Thus no special case
733 * for large kmalloc calls required.
734 */
735 if (unlikely(gfpflags & GFP_DMA))
736 return csizep->cs_dmacachep;
737 return csizep->cs_cachep;
738}
739
Pekka Enberg343e0d72006-02-01 03:05:50 -0800740struct kmem_cache *kmem_find_general_cachep(size_t size, gfp_t gfpflags)
Manfred Spraul97e2bde2005-05-01 08:58:38 -0700741{
742 return __find_general_cachep(size, gfpflags);
743}
744EXPORT_SYMBOL(kmem_find_general_cachep);
745
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800746static size_t slab_mgmt_size(size_t nr_objs, size_t align)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800748 return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align);
749}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Andrew Mortona737b3e2006-03-22 00:08:11 -0800751/*
752 * Calculate the number of objects and left-over bytes for a given buffer size.
753 */
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800754static void cache_estimate(unsigned long gfporder, size_t buffer_size,
755 size_t align, int flags, size_t *left_over,
756 unsigned int *num)
757{
758 int nr_objs;
759 size_t mgmt_size;
760 size_t slab_size = PAGE_SIZE << gfporder;
761
762 /*
763 * The slab management structure can be either off the slab or
764 * on it. For the latter case, the memory allocated for a
765 * slab is used for:
766 *
767 * - The struct slab
768 * - One kmem_bufctl_t for each object
769 * - Padding to respect alignment of @align
770 * - @buffer_size bytes for each object
771 *
772 * If the slab management structure is off the slab, then the
773 * alignment will already be calculated into the size. Because
774 * the slabs are all pages aligned, the objects will be at the
775 * correct alignment when allocated.
776 */
777 if (flags & CFLGS_OFF_SLAB) {
778 mgmt_size = 0;
779 nr_objs = slab_size / buffer_size;
780
781 if (nr_objs > SLAB_LIMIT)
782 nr_objs = SLAB_LIMIT;
783 } else {
784 /*
785 * Ignore padding for the initial guess. The padding
786 * is at most @align-1 bytes, and @buffer_size is at
787 * least @align. In the worst case, this result will
788 * be one greater than the number of objects that fit
789 * into the memory allocation when taking the padding
790 * into account.
791 */
792 nr_objs = (slab_size - sizeof(struct slab)) /
793 (buffer_size + sizeof(kmem_bufctl_t));
794
795 /*
796 * This calculated number will be either the right
797 * amount, or one greater than what we want.
798 */
799 if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size
800 > slab_size)
801 nr_objs--;
802
803 if (nr_objs > SLAB_LIMIT)
804 nr_objs = SLAB_LIMIT;
805
806 mgmt_size = slab_mgmt_size(nr_objs, align);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 }
Steven Rostedtfbaccac2006-02-01 03:05:45 -0800808 *num = nr_objs;
809 *left_over = slab_size - nr_objs*buffer_size - mgmt_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810}
811
812#define slab_error(cachep, msg) __slab_error(__FUNCTION__, cachep, msg)
813
Andrew Mortona737b3e2006-03-22 00:08:11 -0800814static void __slab_error(const char *function, struct kmem_cache *cachep,
815 char *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 printk(KERN_ERR "slab error in %s(): cache `%s': %s\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800818 function, cachep->name, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 dump_stack();
820}
821
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800822#ifdef CONFIG_NUMA
823/*
824 * Special reaping functions for NUMA systems called from cache_reap().
825 * These take care of doing round robin flushing of alien caches (containing
826 * objects freed on different nodes from which they were allocated) and the
827 * flushing of remote pcps by calling drain_node_pages.
828 */
829static DEFINE_PER_CPU(unsigned long, reap_node);
830
831static void init_reap_node(int cpu)
832{
833 int node;
834
835 node = next_node(cpu_to_node(cpu), node_online_map);
836 if (node == MAX_NUMNODES)
Paul Jackson442295c2006-03-22 00:09:11 -0800837 node = first_node(node_online_map);
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800838
839 __get_cpu_var(reap_node) = node;
840}
841
842static void next_reap_node(void)
843{
844 int node = __get_cpu_var(reap_node);
845
846 /*
847 * Also drain per cpu pages on remote zones
848 */
849 if (node != numa_node_id())
850 drain_node_pages(node);
851
852 node = next_node(node, node_online_map);
853 if (unlikely(node >= MAX_NUMNODES))
854 node = first_node(node_online_map);
855 __get_cpu_var(reap_node) = node;
856}
857
858#else
859#define init_reap_node(cpu) do { } while (0)
860#define next_reap_node(void) do { } while (0)
861#endif
862
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863/*
864 * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz
865 * via the workqueue/eventd.
866 * Add the CPU number into the expiration time to minimize the possibility of
867 * the CPUs getting into lockstep and contending for the global cache chain
868 * lock.
869 */
870static void __devinit start_cpu_timer(int cpu)
871{
872 struct work_struct *reap_work = &per_cpu(reap_work, cpu);
873
874 /*
875 * When this gets called from do_initcalls via cpucache_init(),
876 * init_workqueues() has already run, so keventd will be setup
877 * at that time.
878 */
879 if (keventd_up() && reap_work->func == NULL) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800880 init_reap_node(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 INIT_WORK(reap_work, cache_reap, NULL);
882 schedule_delayed_work_on(cpu, reap_work, HZ + 3 * cpu);
883 }
884}
885
Christoph Lametere498be72005-09-09 13:03:32 -0700886static struct array_cache *alloc_arraycache(int node, int entries,
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800887 int batchcount)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888{
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800889 int memsize = sizeof(void *) * entries + sizeof(struct array_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 struct array_cache *nc = NULL;
891
Christoph Lametere498be72005-09-09 13:03:32 -0700892 nc = kmalloc_node(memsize, GFP_KERNEL, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (nc) {
894 nc->avail = 0;
895 nc->limit = entries;
896 nc->batchcount = batchcount;
897 nc->touched = 0;
Christoph Lametere498be72005-09-09 13:03:32 -0700898 spin_lock_init(&nc->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900 return nc;
901}
902
Christoph Lameter3ded1752006-03-25 03:06:44 -0800903/*
904 * Transfer objects in one arraycache to another.
905 * Locking must be handled by the caller.
906 *
907 * Return the number of entries transferred.
908 */
909static int transfer_objects(struct array_cache *to,
910 struct array_cache *from, unsigned int max)
911{
912 /* Figure out how many entries to transfer */
913 int nr = min(min(from->avail, max), to->limit - to->avail);
914
915 if (!nr)
916 return 0;
917
918 memcpy(to->entry + to->avail, from->entry + from->avail -nr,
919 sizeof(void *) *nr);
920
921 from->avail -= nr;
922 to->avail += nr;
923 to->touched = 1;
924 return nr;
925}
926
Christoph Lametere498be72005-09-09 13:03:32 -0700927#ifdef CONFIG_NUMA
Pekka Enberg343e0d72006-02-01 03:05:50 -0800928static void *__cache_alloc_node(struct kmem_cache *, gfp_t, int);
Paul Jacksonc61afb12006-03-24 03:16:08 -0800929static void *alternate_node_alloc(struct kmem_cache *, gfp_t);
Christoph Lameterdc85da12006-01-18 17:42:36 -0800930
Pekka Enberg5295a742006-02-01 03:05:48 -0800931static struct array_cache **alloc_alien_cache(int node, int limit)
Christoph Lametere498be72005-09-09 13:03:32 -0700932{
933 struct array_cache **ac_ptr;
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800934 int memsize = sizeof(void *) * MAX_NUMNODES;
Christoph Lametere498be72005-09-09 13:03:32 -0700935 int i;
936
937 if (limit > 1)
938 limit = 12;
939 ac_ptr = kmalloc_node(memsize, GFP_KERNEL, node);
940 if (ac_ptr) {
941 for_each_node(i) {
942 if (i == node || !node_online(i)) {
943 ac_ptr[i] = NULL;
944 continue;
945 }
946 ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d);
947 if (!ac_ptr[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800948 for (i--; i <= 0; i--)
Christoph Lametere498be72005-09-09 13:03:32 -0700949 kfree(ac_ptr[i]);
950 kfree(ac_ptr);
951 return NULL;
952 }
953 }
954 }
955 return ac_ptr;
956}
957
Pekka Enberg5295a742006-02-01 03:05:48 -0800958static void free_alien_cache(struct array_cache **ac_ptr)
Christoph Lametere498be72005-09-09 13:03:32 -0700959{
960 int i;
961
962 if (!ac_ptr)
963 return;
Christoph Lametere498be72005-09-09 13:03:32 -0700964 for_each_node(i)
Pekka Enbergb28a02d2006-01-08 01:00:37 -0800965 kfree(ac_ptr[i]);
Christoph Lametere498be72005-09-09 13:03:32 -0700966 kfree(ac_ptr);
967}
968
Pekka Enberg343e0d72006-02-01 03:05:50 -0800969static void __drain_alien_cache(struct kmem_cache *cachep,
Pekka Enberg5295a742006-02-01 03:05:48 -0800970 struct array_cache *ac, int node)
Christoph Lametere498be72005-09-09 13:03:32 -0700971{
972 struct kmem_list3 *rl3 = cachep->nodelists[node];
973
974 if (ac->avail) {
975 spin_lock(&rl3->list_lock);
Christoph Lametere00946f2006-03-25 03:06:45 -0800976 /*
977 * Stuff objects into the remote nodes shared array first.
978 * That way we could avoid the overhead of putting the objects
979 * into the free lists and getting them back later.
980 */
shin, jacob693f7d32006-04-28 10:54:37 -0500981 if (rl3->shared)
982 transfer_objects(rl3->shared, ac, ac->limit);
Christoph Lametere00946f2006-03-25 03:06:45 -0800983
Christoph Lameterff694162005-09-22 21:44:02 -0700984 free_block(cachep, ac->entry, ac->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -0700985 ac->avail = 0;
986 spin_unlock(&rl3->list_lock);
987 }
988}
989
Christoph Lameter8fce4d82006-03-09 17:33:54 -0800990/*
991 * Called from cache_reap() to regularly drain alien caches round robin.
992 */
993static void reap_alien(struct kmem_cache *cachep, struct kmem_list3 *l3)
994{
995 int node = __get_cpu_var(reap_node);
996
997 if (l3->alien) {
998 struct array_cache *ac = l3->alien[node];
Christoph Lametere00946f2006-03-25 03:06:45 -0800999
1000 if (ac && ac->avail && spin_trylock_irq(&ac->lock)) {
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001001 __drain_alien_cache(cachep, ac, node);
1002 spin_unlock_irq(&ac->lock);
1003 }
1004 }
1005}
1006
Andrew Mortona737b3e2006-03-22 00:08:11 -08001007static void drain_alien_cache(struct kmem_cache *cachep,
1008 struct array_cache **alien)
Christoph Lametere498be72005-09-09 13:03:32 -07001009{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001010 int i = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07001011 struct array_cache *ac;
1012 unsigned long flags;
1013
1014 for_each_online_node(i) {
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001015 ac = alien[i];
Christoph Lametere498be72005-09-09 13:03:32 -07001016 if (ac) {
1017 spin_lock_irqsave(&ac->lock, flags);
1018 __drain_alien_cache(cachep, ac, i);
1019 spin_unlock_irqrestore(&ac->lock, flags);
1020 }
1021 }
1022}
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001023
1024static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
1025{
1026 struct slab *slabp = virt_to_slab(objp);
1027 int nodeid = slabp->nodeid;
1028 struct kmem_list3 *l3;
1029 struct array_cache *alien = NULL;
1030
1031 /*
1032 * Make sure we are not freeing a object from another node to the array
1033 * cache on this cpu.
1034 */
1035 if (likely(slabp->nodeid == numa_node_id()))
1036 return 0;
1037
1038 l3 = cachep->nodelists[numa_node_id()];
1039 STATS_INC_NODEFREES(cachep);
1040 if (l3->alien && l3->alien[nodeid]) {
1041 alien = l3->alien[nodeid];
1042 spin_lock(&alien->lock);
1043 if (unlikely(alien->avail == alien->limit)) {
1044 STATS_INC_ACOVERFLOW(cachep);
1045 __drain_alien_cache(cachep, alien, nodeid);
1046 }
1047 alien->entry[alien->avail++] = objp;
1048 spin_unlock(&alien->lock);
1049 } else {
1050 spin_lock(&(cachep->nodelists[nodeid])->list_lock);
1051 free_block(cachep, &objp, 1, nodeid);
1052 spin_unlock(&(cachep->nodelists[nodeid])->list_lock);
1053 }
1054 return 1;
1055}
1056
Christoph Lametere498be72005-09-09 13:03:32 -07001057#else
Linus Torvalds7a21ef62006-02-05 11:26:38 -08001058
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001059#define drain_alien_cache(cachep, alien) do { } while (0)
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001060#define reap_alien(cachep, l3) do { } while (0)
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001061
Linus Torvalds7a21ef62006-02-05 11:26:38 -08001062static inline struct array_cache **alloc_alien_cache(int node, int limit)
1063{
1064 return (struct array_cache **) 0x01020304ul;
1065}
1066
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001067static inline void free_alien_cache(struct array_cache **ac_ptr)
1068{
1069}
Linus Torvalds7a21ef62006-02-05 11:26:38 -08001070
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001071static inline int cache_free_alien(struct kmem_cache *cachep, void *objp)
1072{
1073 return 0;
1074}
1075
Christoph Lametere498be72005-09-09 13:03:32 -07001076#endif
1077
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -07001078static int __devinit cpuup_callback(struct notifier_block *nfb,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001079 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 long cpu = (long)hcpu;
Pekka Enberg343e0d72006-02-01 03:05:50 -08001082 struct kmem_cache *cachep;
Christoph Lametere498be72005-09-09 13:03:32 -07001083 struct kmem_list3 *l3 = NULL;
1084 int node = cpu_to_node(cpu);
1085 int memsize = sizeof(struct kmem_list3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 switch (action) {
1088 case CPU_UP_PREPARE:
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001089 mutex_lock(&cache_chain_mutex);
Andrew Mortona737b3e2006-03-22 00:08:11 -08001090 /*
1091 * We need to do this right in the beginning since
Christoph Lametere498be72005-09-09 13:03:32 -07001092 * alloc_arraycache's are going to use this list.
1093 * kmalloc_node allows us to add the slab to the right
1094 * kmem_list3 and not this cpu's kmem_list3
1095 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Christoph Lametere498be72005-09-09 13:03:32 -07001097 list_for_each_entry(cachep, &cache_chain, next) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08001098 /*
1099 * Set up the size64 kmemlist for cpu before we can
Christoph Lametere498be72005-09-09 13:03:32 -07001100 * begin anything. Make sure some other cpu on this
1101 * node has not already allocated this
1102 */
1103 if (!cachep->nodelists[node]) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08001104 l3 = kmalloc_node(memsize, GFP_KERNEL, node);
1105 if (!l3)
Christoph Lametere498be72005-09-09 13:03:32 -07001106 goto bad;
1107 kmem_list3_init(l3);
1108 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001109 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lametere498be72005-09-09 13:03:32 -07001110
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001111 /*
1112 * The l3s don't come and go as CPUs come and
1113 * go. cache_chain_mutex is sufficient
1114 * protection here.
1115 */
Christoph Lametere498be72005-09-09 13:03:32 -07001116 cachep->nodelists[node] = l3;
1117 }
1118
1119 spin_lock_irq(&cachep->nodelists[node]->list_lock);
1120 cachep->nodelists[node]->free_limit =
Andrew Mortona737b3e2006-03-22 00:08:11 -08001121 (1 + nr_cpus_node(node)) *
1122 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07001123 spin_unlock_irq(&cachep->nodelists[node]->list_lock);
1124 }
1125
Andrew Mortona737b3e2006-03-22 00:08:11 -08001126 /*
1127 * Now we can go ahead with allocating the shared arrays and
1128 * array caches
1129 */
Christoph Lametere498be72005-09-09 13:03:32 -07001130 list_for_each_entry(cachep, &cache_chain, next) {
Tobias Klausercd105df2006-01-08 01:00:59 -08001131 struct array_cache *nc;
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001132 struct array_cache *shared;
1133 struct array_cache **alien;
Tobias Klausercd105df2006-01-08 01:00:59 -08001134
Christoph Lametere498be72005-09-09 13:03:32 -07001135 nc = alloc_arraycache(node, cachep->limit,
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001136 cachep->batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 if (!nc)
1138 goto bad;
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001139 shared = alloc_arraycache(node,
1140 cachep->shared * cachep->batchcount,
1141 0xbaadf00d);
1142 if (!shared)
1143 goto bad;
Linus Torvalds7a21ef62006-02-05 11:26:38 -08001144
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001145 alien = alloc_alien_cache(node, cachep->limit);
1146 if (!alien)
1147 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 cachep->array[cpu] = nc;
Christoph Lametere498be72005-09-09 13:03:32 -07001149 l3 = cachep->nodelists[node];
1150 BUG_ON(!l3);
Christoph Lametere498be72005-09-09 13:03:32 -07001151
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001152 spin_lock_irq(&l3->list_lock);
1153 if (!l3->shared) {
1154 /*
1155 * We are serialised from CPU_DEAD or
1156 * CPU_UP_CANCELLED by the cpucontrol lock
1157 */
1158 l3->shared = shared;
1159 shared = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07001160 }
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001161#ifdef CONFIG_NUMA
1162 if (!l3->alien) {
1163 l3->alien = alien;
1164 alien = NULL;
1165 }
1166#endif
1167 spin_unlock_irq(&l3->list_lock);
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001168 kfree(shared);
1169 free_alien_cache(alien);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 }
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001171 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 break;
1173 case CPU_ONLINE:
1174 start_cpu_timer(cpu);
1175 break;
1176#ifdef CONFIG_HOTPLUG_CPU
1177 case CPU_DEAD:
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001178 /*
1179 * Even if all the cpus of a node are down, we don't free the
1180 * kmem_list3 of any cache. This to avoid a race between
1181 * cpu_down, and a kmalloc allocation from another cpu for
1182 * memory from the node of the cpu going down. The list3
1183 * structure is usually allocated from kmem_cache_create() and
1184 * gets destroyed at kmem_cache_destroy().
1185 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 /* fall thru */
1187 case CPU_UP_CANCELED:
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001188 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 list_for_each_entry(cachep, &cache_chain, next) {
1190 struct array_cache *nc;
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001191 struct array_cache *shared;
1192 struct array_cache **alien;
Christoph Lametere498be72005-09-09 13:03:32 -07001193 cpumask_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
Christoph Lametere498be72005-09-09 13:03:32 -07001195 mask = node_to_cpumask(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 /* cpu is dead; no one can alloc from it. */
1197 nc = cachep->array[cpu];
1198 cachep->array[cpu] = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07001199 l3 = cachep->nodelists[node];
1200
1201 if (!l3)
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001202 goto free_array_cache;
Christoph Lametere498be72005-09-09 13:03:32 -07001203
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08001204 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07001205
1206 /* Free limit for this kmem_list3 */
1207 l3->free_limit -= cachep->batchcount;
1208 if (nc)
Christoph Lameterff694162005-09-22 21:44:02 -07001209 free_block(cachep, nc->entry, nc->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001210
1211 if (!cpus_empty(mask)) {
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08001212 spin_unlock_irq(&l3->list_lock);
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001213 goto free_array_cache;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001214 }
Christoph Lametere498be72005-09-09 13:03:32 -07001215
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001216 shared = l3->shared;
1217 if (shared) {
Christoph Lametere498be72005-09-09 13:03:32 -07001218 free_block(cachep, l3->shared->entry,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001219 l3->shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001220 l3->shared = NULL;
1221 }
Christoph Lametere498be72005-09-09 13:03:32 -07001222
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001223 alien = l3->alien;
1224 l3->alien = NULL;
1225
1226 spin_unlock_irq(&l3->list_lock);
1227
1228 kfree(shared);
1229 if (alien) {
1230 drain_alien_cache(cachep, alien);
1231 free_alien_cache(alien);
Christoph Lametere498be72005-09-09 13:03:32 -07001232 }
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001233free_array_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 kfree(nc);
1235 }
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001236 /*
1237 * In the previous loop, all the objects were freed to
1238 * the respective cache's slabs, now we can go ahead and
1239 * shrink each nodelist to its limit.
1240 */
1241 list_for_each_entry(cachep, &cache_chain, next) {
1242 l3 = cachep->nodelists[node];
1243 if (!l3)
1244 continue;
Christoph Lametered11d9e2006-06-30 01:55:45 -07001245 drain_freelist(cachep, l3, l3->free_objects);
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001246 }
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001247 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 break;
1249#endif
1250 }
1251 return NOTIFY_OK;
Andrew Mortona737b3e2006-03-22 00:08:11 -08001252bad:
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001253 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return NOTIFY_BAD;
1255}
1256
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001257static struct notifier_block __cpuinitdata cpucache_notifier = {
1258 &cpuup_callback, NULL, 0
1259};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Christoph Lametere498be72005-09-09 13:03:32 -07001261/*
1262 * swap the static kmem_list3 with kmalloced memory
1263 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001264static void init_list(struct kmem_cache *cachep, struct kmem_list3 *list,
1265 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07001266{
1267 struct kmem_list3 *ptr;
1268
1269 BUG_ON(cachep->nodelists[nodeid] != list);
1270 ptr = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, nodeid);
1271 BUG_ON(!ptr);
1272
1273 local_irq_disable();
1274 memcpy(ptr, list, sizeof(struct kmem_list3));
1275 MAKE_ALL_LISTS(cachep, ptr, nodeid);
1276 cachep->nodelists[nodeid] = ptr;
1277 local_irq_enable();
1278}
1279
Andrew Mortona737b3e2006-03-22 00:08:11 -08001280/*
1281 * Initialisation. Called after the page allocator have been initialised and
1282 * before smp_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 */
1284void __init kmem_cache_init(void)
1285{
1286 size_t left_over;
1287 struct cache_sizes *sizes;
1288 struct cache_names *names;
Christoph Lametere498be72005-09-09 13:03:32 -07001289 int i;
Jack Steiner07ed76b2006-03-07 21:55:46 -08001290 int order;
Christoph Lametere498be72005-09-09 13:03:32 -07001291
1292 for (i = 0; i < NUM_INIT_LISTS; i++) {
1293 kmem_list3_init(&initkmem_list3[i]);
1294 if (i < MAX_NUMNODES)
1295 cache_cache.nodelists[i] = NULL;
1296 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297
1298 /*
1299 * Fragmentation resistance on low memory - only use bigger
1300 * page orders on machines with more than 32MB of memory.
1301 */
1302 if (num_physpages > (32 << 20) >> PAGE_SHIFT)
1303 slab_break_gfp_order = BREAK_GFP_ORDER_HI;
1304
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 /* Bootstrap is tricky, because several objects are allocated
1306 * from caches that do not exist yet:
Andrew Mortona737b3e2006-03-22 00:08:11 -08001307 * 1) initialize the cache_cache cache: it contains the struct
1308 * kmem_cache structures of all caches, except cache_cache itself:
1309 * cache_cache is statically allocated.
Christoph Lametere498be72005-09-09 13:03:32 -07001310 * Initially an __init data area is used for the head array and the
1311 * kmem_list3 structures, it's replaced with a kmalloc allocated
1312 * array at the end of the bootstrap.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 * 2) Create the first kmalloc cache.
Pekka Enberg343e0d72006-02-01 03:05:50 -08001314 * The struct kmem_cache for the new cache is allocated normally.
Christoph Lametere498be72005-09-09 13:03:32 -07001315 * An __init data area is used for the head array.
1316 * 3) Create the remaining kmalloc caches, with minimally sized
1317 * head arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 * 4) Replace the __init data head arrays for cache_cache and the first
1319 * kmalloc cache with kmalloc allocated arrays.
Christoph Lametere498be72005-09-09 13:03:32 -07001320 * 5) Replace the __init data for kmem_list3 for cache_cache and
1321 * the other cache's with kmalloc allocated memory.
1322 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 */
1324
1325 /* 1) create the cache_cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 INIT_LIST_HEAD(&cache_chain);
1327 list_add(&cache_cache.next, &cache_chain);
1328 cache_cache.colour_off = cache_line_size();
1329 cache_cache.array[smp_processor_id()] = &initarray_cache.cache;
Christoph Lametere498be72005-09-09 13:03:32 -07001330 cache_cache.nodelists[numa_node_id()] = &initkmem_list3[CACHE_CACHE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Andrew Mortona737b3e2006-03-22 00:08:11 -08001332 cache_cache.buffer_size = ALIGN(cache_cache.buffer_size,
1333 cache_line_size());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Jack Steiner07ed76b2006-03-07 21:55:46 -08001335 for (order = 0; order < MAX_ORDER; order++) {
1336 cache_estimate(order, cache_cache.buffer_size,
1337 cache_line_size(), 0, &left_over, &cache_cache.num);
1338 if (cache_cache.num)
1339 break;
1340 }
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02001341 BUG_ON(!cache_cache.num);
Jack Steiner07ed76b2006-03-07 21:55:46 -08001342 cache_cache.gfporder = order;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001343 cache_cache.colour = left_over / cache_cache.colour_off;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001344 cache_cache.slab_size = ALIGN(cache_cache.num * sizeof(kmem_bufctl_t) +
1345 sizeof(struct slab), cache_line_size());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 /* 2+3) create the kmalloc caches */
1348 sizes = malloc_sizes;
1349 names = cache_names;
1350
Andrew Mortona737b3e2006-03-22 00:08:11 -08001351 /*
1352 * Initialize the caches that provide memory for the array cache and the
1353 * kmem_list3 structures first. Without this, further allocations will
1354 * bug.
Christoph Lametere498be72005-09-09 13:03:32 -07001355 */
1356
1357 sizes[INDEX_AC].cs_cachep = kmem_cache_create(names[INDEX_AC].name,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001358 sizes[INDEX_AC].cs_size,
1359 ARCH_KMALLOC_MINALIGN,
1360 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
1361 NULL, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07001362
Andrew Mortona737b3e2006-03-22 00:08:11 -08001363 if (INDEX_AC != INDEX_L3) {
Christoph Lametere498be72005-09-09 13:03:32 -07001364 sizes[INDEX_L3].cs_cachep =
Andrew Mortona737b3e2006-03-22 00:08:11 -08001365 kmem_cache_create(names[INDEX_L3].name,
1366 sizes[INDEX_L3].cs_size,
1367 ARCH_KMALLOC_MINALIGN,
1368 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
1369 NULL, NULL);
1370 }
Christoph Lametere498be72005-09-09 13:03:32 -07001371
Ingo Molnare0a42722006-06-23 02:03:46 -07001372 slab_early_init = 0;
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 while (sizes->cs_size != ULONG_MAX) {
Christoph Lametere498be72005-09-09 13:03:32 -07001375 /*
1376 * For performance, all the general caches are L1 aligned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377 * This should be particularly beneficial on SMP boxes, as it
1378 * eliminates "false sharing".
1379 * Note for systems short on memory removing the alignment will
Christoph Lametere498be72005-09-09 13:03:32 -07001380 * allow tighter packing of the smaller caches.
1381 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001382 if (!sizes->cs_cachep) {
Christoph Lametere498be72005-09-09 13:03:32 -07001383 sizes->cs_cachep = kmem_cache_create(names->name,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001384 sizes->cs_size,
1385 ARCH_KMALLOC_MINALIGN,
1386 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
1387 NULL, NULL);
1388 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 sizes->cs_dmacachep = kmem_cache_create(names->name_dma,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001391 sizes->cs_size,
1392 ARCH_KMALLOC_MINALIGN,
1393 ARCH_KMALLOC_FLAGS|SLAB_CACHE_DMA|
1394 SLAB_PANIC,
1395 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 sizes++;
1397 names++;
1398 }
1399 /* 4) Replace the bootstrap head arrays */
1400 {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001401 void *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001402
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
Christoph Lametere498be72005-09-09 13:03:32 -07001404
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 local_irq_disable();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001406 BUG_ON(cpu_cache_get(&cache_cache) != &initarray_cache.cache);
1407 memcpy(ptr, cpu_cache_get(&cache_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001408 sizeof(struct arraycache_init));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 cache_cache.array[smp_processor_id()] = ptr;
1410 local_irq_enable();
Christoph Lametere498be72005-09-09 13:03:32 -07001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
Christoph Lametere498be72005-09-09 13:03:32 -07001413
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 local_irq_disable();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001415 BUG_ON(cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001416 != &initarray_generic.cache);
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001417 memcpy(ptr, cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001418 sizeof(struct arraycache_init));
Christoph Lametere498be72005-09-09 13:03:32 -07001419 malloc_sizes[INDEX_AC].cs_cachep->array[smp_processor_id()] =
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001420 ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 local_irq_enable();
1422 }
Christoph Lametere498be72005-09-09 13:03:32 -07001423 /* 5) Replace the bootstrap kmem_list3's */
1424 {
1425 int node;
1426 /* Replace the static kmem_list3 structures for the boot cpu */
1427 init_list(&cache_cache, &initkmem_list3[CACHE_CACHE],
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001428 numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429
Christoph Lametere498be72005-09-09 13:03:32 -07001430 for_each_online_node(node) {
1431 init_list(malloc_sizes[INDEX_AC].cs_cachep,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001432 &initkmem_list3[SIZE_AC + node], node);
Christoph Lametere498be72005-09-09 13:03:32 -07001433
1434 if (INDEX_AC != INDEX_L3) {
1435 init_list(malloc_sizes[INDEX_L3].cs_cachep,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001436 &initkmem_list3[SIZE_L3 + node],
1437 node);
Christoph Lametere498be72005-09-09 13:03:32 -07001438 }
1439 }
1440 }
1441
1442 /* 6) resize the head arrays to their final sizes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 {
Pekka Enberg343e0d72006-02-01 03:05:50 -08001444 struct kmem_cache *cachep;
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001445 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 list_for_each_entry(cachep, &cache_chain, next)
Andrew Mortona737b3e2006-03-22 00:08:11 -08001447 enable_cpucache(cachep);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001448 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 }
1450
1451 /* Done! */
1452 g_cpucache_up = FULL;
1453
Andrew Mortona737b3e2006-03-22 00:08:11 -08001454 /*
1455 * Register a cpu startup notifier callback that initializes
1456 * cpu_cache_get for all new cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 */
1458 register_cpu_notifier(&cpucache_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Andrew Mortona737b3e2006-03-22 00:08:11 -08001460 /*
1461 * The reap timers are started later, with a module init call: That part
1462 * of the kernel is not yet operational.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 */
1464}
1465
1466static int __init cpucache_init(void)
1467{
1468 int cpu;
1469
Andrew Mortona737b3e2006-03-22 00:08:11 -08001470 /*
1471 * Register the timers that return unneeded pages to the page allocator
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 */
Christoph Lametere498be72005-09-09 13:03:32 -07001473 for_each_online_cpu(cpu)
Andrew Mortona737b3e2006-03-22 00:08:11 -08001474 start_cpu_timer(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 return 0;
1476}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477__initcall(cpucache_init);
1478
1479/*
1480 * Interface to system's page allocator. No need to hold the cache-lock.
1481 *
1482 * If we requested dmaable memory, we will get it. Even if we
1483 * did not request dmaable memory, we might get it, but that
1484 * would be relatively rare and ignorable.
1485 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001486static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487{
1488 struct page *page;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001489 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 int i;
1491
Luke Yangd6fef9d2006-04-10 22:52:56 -07001492#ifndef CONFIG_MMU
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001493 /*
1494 * Nommu uses slab's for process anonymous memory allocations, and thus
1495 * requires __GFP_COMP to properly refcount higher order allocations
Luke Yangd6fef9d2006-04-10 22:52:56 -07001496 */
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001497 flags |= __GFP_COMP;
Luke Yangd6fef9d2006-04-10 22:52:56 -07001498#endif
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001499 flags |= cachep->gfpflags;
1500
1501 page = alloc_pages_node(nodeid, flags, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 if (!page)
1503 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001505 nr_pages = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001507 atomic_add(nr_pages, &slab_reclaim_pages);
Christoph Lameter9a865ff2006-06-30 01:55:38 -07001508 add_zone_page_state(page_zone(page), NR_SLAB, nr_pages);
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001509 for (i = 0; i < nr_pages; i++)
1510 __SetPageSlab(page + i);
1511 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512}
1513
1514/*
1515 * Interface to system's page release.
1516 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001517static void kmem_freepages(struct kmem_cache *cachep, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001519 unsigned long i = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 struct page *page = virt_to_page(addr);
1521 const unsigned long nr_freed = i;
1522
Christoph Lameter9a865ff2006-06-30 01:55:38 -07001523 sub_zone_page_state(page_zone(page), NR_SLAB, nr_freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 while (i--) {
Nick Pigginf205b2f2006-03-22 00:08:02 -08001525 BUG_ON(!PageSlab(page));
1526 __ClearPageSlab(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 page++;
1528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 if (current->reclaim_state)
1530 current->reclaim_state->reclaimed_slab += nr_freed;
1531 free_pages((unsigned long)addr, cachep->gfporder);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001532 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1533 atomic_sub(1 << cachep->gfporder, &slab_reclaim_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534}
1535
1536static void kmem_rcu_free(struct rcu_head *head)
1537{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001538 struct slab_rcu *slab_rcu = (struct slab_rcu *)head;
Pekka Enberg343e0d72006-02-01 03:05:50 -08001539 struct kmem_cache *cachep = slab_rcu->cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 kmem_freepages(cachep, slab_rcu->addr);
1542 if (OFF_SLAB(cachep))
1543 kmem_cache_free(cachep->slabp_cache, slab_rcu);
1544}
1545
1546#if DEBUG
1547
1548#ifdef CONFIG_DEBUG_PAGEALLOC
Pekka Enberg343e0d72006-02-01 03:05:50 -08001549static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001550 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001552 int size = obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001554 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001556 if (size < 5 * sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 return;
1558
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001559 *addr++ = 0x12345678;
1560 *addr++ = caller;
1561 *addr++ = smp_processor_id();
1562 size -= 3 * sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 {
1564 unsigned long *sptr = &caller;
1565 unsigned long svalue;
1566
1567 while (!kstack_end(sptr)) {
1568 svalue = *sptr++;
1569 if (kernel_text_address(svalue)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001570 *addr++ = svalue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 size -= sizeof(unsigned long);
1572 if (size <= sizeof(unsigned long))
1573 break;
1574 }
1575 }
1576
1577 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001578 *addr++ = 0x87654321;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579}
1580#endif
1581
Pekka Enberg343e0d72006-02-01 03:05:50 -08001582static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001584 int size = obj_size(cachep);
1585 addr = &((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
1587 memset(addr, val, size);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001588 *(unsigned char *)(addr + size - 1) = POISON_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589}
1590
1591static void dump_line(char *data, int offset, int limit)
1592{
1593 int i;
1594 printk(KERN_ERR "%03x:", offset);
Andrew Mortona737b3e2006-03-22 00:08:11 -08001595 for (i = 0; i < limit; i++)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001596 printk(" %02x", (unsigned char)data[offset + i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 printk("\n");
1598}
1599#endif
1600
1601#if DEBUG
1602
Pekka Enberg343e0d72006-02-01 03:05:50 -08001603static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604{
1605 int i, size;
1606 char *realobj;
1607
1608 if (cachep->flags & SLAB_RED_ZONE) {
1609 printk(KERN_ERR "Redzone: 0x%lx/0x%lx.\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001610 *dbg_redzone1(cachep, objp),
1611 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 }
1613
1614 if (cachep->flags & SLAB_STORE_USER) {
1615 printk(KERN_ERR "Last user: [<%p>]",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001616 *dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 print_symbol("(%s)",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001618 (unsigned long)*dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 printk("\n");
1620 }
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001621 realobj = (char *)objp + obj_offset(cachep);
1622 size = obj_size(cachep);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001623 for (i = 0; i < size && lines; i += 16, lines--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 int limit;
1625 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001626 if (i + limit > size)
1627 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 dump_line(realobj, i, limit);
1629 }
1630}
1631
Pekka Enberg343e0d72006-02-01 03:05:50 -08001632static void check_poison_obj(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633{
1634 char *realobj;
1635 int size, i;
1636 int lines = 0;
1637
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001638 realobj = (char *)objp + obj_offset(cachep);
1639 size = obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001641 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 char exp = POISON_FREE;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001643 if (i == size - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 exp = POISON_END;
1645 if (realobj[i] != exp) {
1646 int limit;
1647 /* Mismatch ! */
1648 /* Print header */
1649 if (lines == 0) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001650 printk(KERN_ERR
Andrew Mortona737b3e2006-03-22 00:08:11 -08001651 "Slab corruption: start=%p, len=%d\n",
1652 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 print_objinfo(cachep, objp, 0);
1654 }
1655 /* Hexdump the affected line */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001656 i = (i / 16) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001658 if (i + limit > size)
1659 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 dump_line(realobj, i, limit);
1661 i += 16;
1662 lines++;
1663 /* Limit to 5 lines */
1664 if (lines > 5)
1665 break;
1666 }
1667 }
1668 if (lines != 0) {
1669 /* Print some data about the neighboring objects, if they
1670 * exist:
1671 */
Pekka Enberg6ed5eb22006-02-01 03:05:49 -08001672 struct slab *slabp = virt_to_slab(objp);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001673 unsigned int objnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001675 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 if (objnr) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001677 objp = index_to_obj(cachep, slabp, objnr - 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001678 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001680 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 print_objinfo(cachep, objp, 2);
1682 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001683 if (objnr + 1 < cachep->num) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001684 objp = index_to_obj(cachep, slabp, objnr + 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001685 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001687 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688 print_objinfo(cachep, objp, 2);
1689 }
1690 }
1691}
1692#endif
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694#if DEBUG
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001695/**
Randy Dunlap911851e2006-03-22 00:08:14 -08001696 * slab_destroy_objs - destroy a slab and its objects
1697 * @cachep: cache pointer being destroyed
1698 * @slabp: slab pointer being destroyed
1699 *
1700 * Call the registered destructor for each object in a slab that is being
1701 * destroyed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001702 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001703static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001704{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 int i;
1706 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001707 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708
1709 if (cachep->flags & SLAB_POISON) {
1710#ifdef CONFIG_DEBUG_PAGEALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -08001711 if (cachep->buffer_size % PAGE_SIZE == 0 &&
1712 OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001713 kernel_map_pages(virt_to_page(objp),
Andrew Mortona737b3e2006-03-22 00:08:11 -08001714 cachep->buffer_size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 else
1716 check_poison_obj(cachep, objp);
1717#else
1718 check_poison_obj(cachep, objp);
1719#endif
1720 }
1721 if (cachep->flags & SLAB_RED_ZONE) {
1722 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
1723 slab_error(cachep, "start of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001724 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
1726 slab_error(cachep, "end of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001727 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 }
1729 if (cachep->dtor && !(cachep->flags & SLAB_POISON))
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001730 (cachep->dtor) (objp + obj_offset(cachep), cachep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001732}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733#else
Pekka Enberg343e0d72006-02-01 03:05:50 -08001734static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001735{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (cachep->dtor) {
1737 int i;
1738 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001739 void *objp = index_to_obj(cachep, slabp, i);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001740 (cachep->dtor) (objp, cachep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 }
1742 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001743}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744#endif
1745
Randy Dunlap911851e2006-03-22 00:08:14 -08001746/**
1747 * slab_destroy - destroy and release all objects in a slab
1748 * @cachep: cache pointer being destroyed
1749 * @slabp: slab pointer being destroyed
1750 *
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001751 * Destroy all the objs in a slab, and release the mem back to the system.
Andrew Mortona737b3e2006-03-22 00:08:11 -08001752 * Before calling the slab must have been unlinked from the cache. The
1753 * cache-lock is not held/needed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001754 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001755static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001756{
1757 void *addr = slabp->s_mem - slabp->colouroff;
1758
1759 slab_destroy_objs(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
1761 struct slab_rcu *slab_rcu;
1762
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001763 slab_rcu = (struct slab_rcu *)slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 slab_rcu->cachep = cachep;
1765 slab_rcu->addr = addr;
1766 call_rcu(&slab_rcu->head, kmem_rcu_free);
1767 } else {
1768 kmem_freepages(cachep, addr);
1769 if (OFF_SLAB(cachep))
1770 kmem_cache_free(cachep->slabp_cache, slabp);
1771 }
1772}
1773
Andrew Mortona737b3e2006-03-22 00:08:11 -08001774/*
1775 * For setting up all the kmem_list3s for cache whose buffer_size is same as
1776 * size of kmem_list3.
1777 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001778static void set_up_list3s(struct kmem_cache *cachep, int index)
Christoph Lametere498be72005-09-09 13:03:32 -07001779{
1780 int node;
1781
1782 for_each_online_node(node) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001783 cachep->nodelists[node] = &initkmem_list3[index + node];
Christoph Lametere498be72005-09-09 13:03:32 -07001784 cachep->nodelists[node]->next_reap = jiffies +
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001785 REAPTIMEOUT_LIST3 +
1786 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lametere498be72005-09-09 13:03:32 -07001787 }
1788}
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790/**
Randy.Dunlapa70773d2006-02-01 03:05:52 -08001791 * calculate_slab_order - calculate size (page order) of slabs
1792 * @cachep: pointer to the cache that is being created
1793 * @size: size of objects to be created in this cache.
1794 * @align: required alignment for the objects.
1795 * @flags: slab allocation flags
1796 *
1797 * Also calculates the number of objects per slab.
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001798 *
1799 * This could be made much more intelligent. For now, try to avoid using
1800 * high order pages for slabs. When the gfp() functions are more friendly
1801 * towards high-order requests, this should be changed.
1802 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001803static size_t calculate_slab_order(struct kmem_cache *cachep,
Randy Dunlapee13d782006-02-01 03:05:53 -08001804 size_t size, size_t align, unsigned long flags)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001805{
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02001806 unsigned long offslab_limit;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001807 size_t left_over = 0;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001808 int gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001809
Andrew Mortona737b3e2006-03-22 00:08:11 -08001810 for (gfporder = 0; gfporder <= MAX_GFP_ORDER; gfporder++) {
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001811 unsigned int num;
1812 size_t remainder;
1813
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001814 cache_estimate(gfporder, size, align, flags, &remainder, &num);
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001815 if (!num)
1816 continue;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001817
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02001818 if (flags & CFLGS_OFF_SLAB) {
1819 /*
1820 * Max number of objs-per-slab for caches which
1821 * use off-slab slabs. Needed to avoid a possible
1822 * looping condition in cache_grow().
1823 */
1824 offslab_limit = size - sizeof(struct slab);
1825 offslab_limit /= sizeof(kmem_bufctl_t);
1826
1827 if (num > offslab_limit)
1828 break;
1829 }
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001830
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001831 /* Found something acceptable - save it away */
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001832 cachep->num = num;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001833 cachep->gfporder = gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001834 left_over = remainder;
1835
1836 /*
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08001837 * A VFS-reclaimable slab tends to have most allocations
1838 * as GFP_NOFS and we really don't want to have to be allocating
1839 * higher-order pages when we are unable to shrink dcache.
1840 */
1841 if (flags & SLAB_RECLAIM_ACCOUNT)
1842 break;
1843
1844 /*
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001845 * Large number of objects is good, but very large slabs are
1846 * currently bad for the gfp()s.
1847 */
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001848 if (gfporder >= slab_break_gfp_order)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001849 break;
1850
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001851 /*
1852 * Acceptable internal fragmentation?
1853 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001854 if (left_over * 8 <= (PAGE_SIZE << gfporder))
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001855 break;
1856 }
1857 return left_over;
1858}
1859
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08001860static void setup_cpu_cache(struct kmem_cache *cachep)
1861{
1862 if (g_cpucache_up == FULL) {
1863 enable_cpucache(cachep);
1864 return;
1865 }
1866 if (g_cpucache_up == NONE) {
1867 /*
1868 * Note: the first kmem_cache_create must create the cache
1869 * that's used by kmalloc(24), otherwise the creation of
1870 * further caches will BUG().
1871 */
1872 cachep->array[smp_processor_id()] = &initarray_generic.cache;
1873
1874 /*
1875 * If the cache that's used by kmalloc(sizeof(kmem_list3)) is
1876 * the first cache, then we need to set up all its list3s,
1877 * otherwise the creation of further caches will BUG().
1878 */
1879 set_up_list3s(cachep, SIZE_AC);
1880 if (INDEX_AC == INDEX_L3)
1881 g_cpucache_up = PARTIAL_L3;
1882 else
1883 g_cpucache_up = PARTIAL_AC;
1884 } else {
1885 cachep->array[smp_processor_id()] =
1886 kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
1887
1888 if (g_cpucache_up == PARTIAL_AC) {
1889 set_up_list3s(cachep, SIZE_L3);
1890 g_cpucache_up = PARTIAL_L3;
1891 } else {
1892 int node;
1893 for_each_online_node(node) {
1894 cachep->nodelists[node] =
1895 kmalloc_node(sizeof(struct kmem_list3),
1896 GFP_KERNEL, node);
1897 BUG_ON(!cachep->nodelists[node]);
1898 kmem_list3_init(cachep->nodelists[node]);
1899 }
1900 }
1901 }
1902 cachep->nodelists[numa_node_id()]->next_reap =
1903 jiffies + REAPTIMEOUT_LIST3 +
1904 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1905
1906 cpu_cache_get(cachep)->avail = 0;
1907 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
1908 cpu_cache_get(cachep)->batchcount = 1;
1909 cpu_cache_get(cachep)->touched = 0;
1910 cachep->batchcount = 1;
1911 cachep->limit = BOOT_CPUCACHE_ENTRIES;
1912}
1913
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001914/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 * kmem_cache_create - Create a cache.
1916 * @name: A string which is used in /proc/slabinfo to identify this cache.
1917 * @size: The size of objects to be created in this cache.
1918 * @align: The required alignment for the objects.
1919 * @flags: SLAB flags
1920 * @ctor: A constructor for the objects.
1921 * @dtor: A destructor for the objects.
1922 *
1923 * Returns a ptr to the cache on success, NULL on failure.
1924 * Cannot be called within a int, but can be interrupted.
1925 * The @ctor is run when new pages are allocated by the cache
1926 * and the @dtor is run before the pages are handed back.
1927 *
1928 * @name must be valid until the cache is destroyed. This implies that
Andrew Mortona737b3e2006-03-22 00:08:11 -08001929 * the module calling this has to destroy the cache before getting unloaded.
1930 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 * The flags are
1932 *
1933 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
1934 * to catch references to uninitialised memory.
1935 *
1936 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
1937 * for buffer overruns.
1938 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
1940 * cacheline. This can be beneficial if you're counting cycles as closely
1941 * as davem.
1942 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001943struct kmem_cache *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944kmem_cache_create (const char *name, size_t size, size_t align,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001945 unsigned long flags,
1946 void (*ctor)(void*, struct kmem_cache *, unsigned long),
Pekka Enberg343e0d72006-02-01 03:05:50 -08001947 void (*dtor)(void*, struct kmem_cache *, unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
1949 size_t left_over, slab_size, ralign;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07001950 struct kmem_cache *cachep = NULL, *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
1952 /*
1953 * Sanity checks... these are all serious usage bugs.
1954 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001955 if (!name || in_interrupt() || (size < BYTES_PER_WORD) ||
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001956 (size > (1 << MAX_OBJ_ORDER) * PAGE_SIZE) || (dtor && !ctor)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08001957 printk(KERN_ERR "%s: Early error in slab %s\n", __FUNCTION__,
1958 name);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001959 BUG();
1960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Ravikiran G Thirumalaif0188f42006-02-10 01:51:13 -08001962 /*
1963 * Prevent CPUs from coming and going.
1964 * lock_cpu_hotplug() nests outside cache_chain_mutex
1965 */
1966 lock_cpu_hotplug();
1967
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001968 mutex_lock(&cache_chain_mutex);
Andrew Morton4f12bb42005-11-07 00:58:00 -08001969
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07001970 list_for_each_entry(pc, &cache_chain, next) {
Andrew Morton4f12bb42005-11-07 00:58:00 -08001971 mm_segment_t old_fs = get_fs();
1972 char tmp;
1973 int res;
1974
1975 /*
1976 * This happens when the module gets unloaded and doesn't
1977 * destroy its slab cache and no-one else reuses the vmalloc
1978 * area of the module. Print a warning.
1979 */
1980 set_fs(KERNEL_DS);
1981 res = __get_user(tmp, pc->name);
1982 set_fs(old_fs);
1983 if (res) {
1984 printk("SLAB: cache with size %d has lost its name\n",
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001985 pc->buffer_size);
Andrew Morton4f12bb42005-11-07 00:58:00 -08001986 continue;
1987 }
1988
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001989 if (!strcmp(pc->name, name)) {
Andrew Morton4f12bb42005-11-07 00:58:00 -08001990 printk("kmem_cache_create: duplicate cache %s\n", name);
1991 dump_stack();
1992 goto oops;
1993 }
1994 }
1995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996#if DEBUG
1997 WARN_ON(strchr(name, ' ')); /* It confuses parsers */
1998 if ((flags & SLAB_DEBUG_INITIAL) && !ctor) {
1999 /* No constructor, but inital state check requested */
2000 printk(KERN_ERR "%s: No con, but init state check "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002001 "requested - %s\n", __FUNCTION__, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 flags &= ~SLAB_DEBUG_INITIAL;
2003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004#if FORCED_DEBUG
2005 /*
2006 * Enable redzoning and last user accounting, except for caches with
2007 * large objects, if the increased size would increase the object size
2008 * above the next power of two: caches with object sizes just above a
2009 * power of two have a significant amount of internal fragmentation.
2010 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002011 if (size < 4096 || fls(size - 1) == fls(size-1 + 3 * BYTES_PER_WORD))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002012 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 if (!(flags & SLAB_DESTROY_BY_RCU))
2014 flags |= SLAB_POISON;
2015#endif
2016 if (flags & SLAB_DESTROY_BY_RCU)
2017 BUG_ON(flags & SLAB_POISON);
2018#endif
2019 if (flags & SLAB_DESTROY_BY_RCU)
2020 BUG_ON(dtor);
2021
2022 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002023 * Always checks flags, a caller might be expecting debug support which
2024 * isn't available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 */
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002026 BUG_ON(flags & ~CREATE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027
Andrew Mortona737b3e2006-03-22 00:08:11 -08002028 /*
2029 * Check that size is in terms of words. This is needed to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 * unaligned accesses for some archs when redzoning is used, and makes
2031 * sure any on-slab bufctl's are also correctly aligned.
2032 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002033 if (size & (BYTES_PER_WORD - 1)) {
2034 size += (BYTES_PER_WORD - 1);
2035 size &= ~(BYTES_PER_WORD - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
2037
Andrew Mortona737b3e2006-03-22 00:08:11 -08002038 /* calculate the final buffer alignment: */
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 /* 1) arch recommendation: can be overridden for debug */
2041 if (flags & SLAB_HWCACHE_ALIGN) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002042 /*
2043 * Default alignment: as specified by the arch code. Except if
2044 * an object is really small, then squeeze multiple objects into
2045 * one cacheline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 */
2047 ralign = cache_line_size();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002048 while (size <= ralign / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 ralign /= 2;
2050 } else {
2051 ralign = BYTES_PER_WORD;
2052 }
2053 /* 2) arch mandated alignment: disables debug if necessary */
2054 if (ralign < ARCH_SLAB_MINALIGN) {
2055 ralign = ARCH_SLAB_MINALIGN;
2056 if (ralign > BYTES_PER_WORD)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002057 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 }
2059 /* 3) caller mandated alignment: disables debug if necessary */
2060 if (ralign < align) {
2061 ralign = align;
2062 if (ralign > BYTES_PER_WORD)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002063 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
Andrew Mortona737b3e2006-03-22 00:08:11 -08002065 /*
2066 * 4) Store it. Note that the debug code below can reduce
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 * the alignment to BYTES_PER_WORD.
2068 */
2069 align = ralign;
2070
2071 /* Get cache's description obj. */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08002072 cachep = kmem_cache_zalloc(&cache_cache, SLAB_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 if (!cachep)
Andrew Morton4f12bb42005-11-07 00:58:00 -08002074 goto oops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075
2076#if DEBUG
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002077 cachep->obj_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
2079 if (flags & SLAB_RED_ZONE) {
2080 /* redzoning only works with word aligned caches */
2081 align = BYTES_PER_WORD;
2082
2083 /* add space for red zone words */
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002084 cachep->obj_offset += BYTES_PER_WORD;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002085 size += 2 * BYTES_PER_WORD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
2087 if (flags & SLAB_STORE_USER) {
2088 /* user store requires word alignment and
2089 * one word storage behind the end of the real
2090 * object.
2091 */
2092 align = BYTES_PER_WORD;
2093 size += BYTES_PER_WORD;
2094 }
2095#if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002096 if (size >= malloc_sizes[INDEX_L3 + 1].cs_size
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002097 && cachep->obj_size > cache_line_size() && size < PAGE_SIZE) {
2098 cachep->obj_offset += PAGE_SIZE - size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 size = PAGE_SIZE;
2100 }
2101#endif
2102#endif
2103
Ingo Molnare0a42722006-06-23 02:03:46 -07002104 /*
2105 * Determine if the slab management is 'on' or 'off' slab.
2106 * (bootstrapping cannot cope with offslab caches so don't do
2107 * it too early on.)
2108 */
2109 if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 /*
2111 * Size is large, assume best to place the slab management obj
2112 * off-slab (should allow better packing of objs).
2113 */
2114 flags |= CFLGS_OFF_SLAB;
2115
2116 size = ALIGN(size, align);
2117
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08002118 left_over = calculate_slab_order(cachep, size, align, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120 if (!cachep->num) {
2121 printk("kmem_cache_create: couldn't create cache %s.\n", name);
2122 kmem_cache_free(&cache_cache, cachep);
2123 cachep = NULL;
Andrew Morton4f12bb42005-11-07 00:58:00 -08002124 goto oops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002126 slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
2127 + sizeof(struct slab), align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 /*
2130 * If the slab has been placed off-slab, and we have enough space then
2131 * move it on-slab. This is at the expense of any extra colouring.
2132 */
2133 if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
2134 flags &= ~CFLGS_OFF_SLAB;
2135 left_over -= slab_size;
2136 }
2137
2138 if (flags & CFLGS_OFF_SLAB) {
2139 /* really off slab. No need for manual alignment */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002140 slab_size =
2141 cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
2143
2144 cachep->colour_off = cache_line_size();
2145 /* Offset must be a multiple of the alignment. */
2146 if (cachep->colour_off < align)
2147 cachep->colour_off = align;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002148 cachep->colour = left_over / cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 cachep->slab_size = slab_size;
2150 cachep->flags = flags;
2151 cachep->gfpflags = 0;
2152 if (flags & SLAB_CACHE_DMA)
2153 cachep->gfpflags |= GFP_DMA;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002154 cachep->buffer_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 if (flags & CFLGS_OFF_SLAB)
Victor Fuscob2d55072005-09-10 00:26:36 -07002157 cachep->slabp_cache = kmem_find_general_cachep(slab_size, 0u);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 cachep->ctor = ctor;
2159 cachep->dtor = dtor;
2160 cachep->name = name;
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002163 setup_cpu_cache(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 /* cache setup completed, link it into the list */
2166 list_add(&cachep->next, &cache_chain);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002167oops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 if (!cachep && (flags & SLAB_PANIC))
2169 panic("kmem_cache_create(): failed to create slab `%s'\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002170 name);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002171 mutex_unlock(&cache_chain_mutex);
Ravikiran G Thirumalaif0188f42006-02-10 01:51:13 -08002172 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 return cachep;
2174}
2175EXPORT_SYMBOL(kmem_cache_create);
2176
2177#if DEBUG
2178static void check_irq_off(void)
2179{
2180 BUG_ON(!irqs_disabled());
2181}
2182
2183static void check_irq_on(void)
2184{
2185 BUG_ON(irqs_disabled());
2186}
2187
Pekka Enberg343e0d72006-02-01 03:05:50 -08002188static void check_spinlock_acquired(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189{
2190#ifdef CONFIG_SMP
2191 check_irq_off();
Christoph Lametere498be72005-09-09 13:03:32 -07002192 assert_spin_locked(&cachep->nodelists[numa_node_id()]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193#endif
2194}
Christoph Lametere498be72005-09-09 13:03:32 -07002195
Pekka Enberg343e0d72006-02-01 03:05:50 -08002196static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07002197{
2198#ifdef CONFIG_SMP
2199 check_irq_off();
2200 assert_spin_locked(&cachep->nodelists[node]->list_lock);
2201#endif
2202}
2203
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204#else
2205#define check_irq_off() do { } while(0)
2206#define check_irq_on() do { } while(0)
2207#define check_spinlock_acquired(x) do { } while(0)
Christoph Lametere498be72005-09-09 13:03:32 -07002208#define check_spinlock_acquired_node(x, y) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209#endif
2210
Christoph Lameteraab22072006-03-22 00:09:06 -08002211static void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
2212 struct array_cache *ac,
2213 int force, int node);
2214
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215static void do_drain(void *arg)
2216{
Andrew Mortona737b3e2006-03-22 00:08:11 -08002217 struct kmem_cache *cachep = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 struct array_cache *ac;
Christoph Lameterff694162005-09-22 21:44:02 -07002219 int node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
2221 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002222 ac = cpu_cache_get(cachep);
Christoph Lameterff694162005-09-22 21:44:02 -07002223 spin_lock(&cachep->nodelists[node]->list_lock);
2224 free_block(cachep, ac->entry, ac->avail, node);
2225 spin_unlock(&cachep->nodelists[node]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 ac->avail = 0;
2227}
2228
Pekka Enberg343e0d72006-02-01 03:05:50 -08002229static void drain_cpu_caches(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230{
Christoph Lametere498be72005-09-09 13:03:32 -07002231 struct kmem_list3 *l3;
2232 int node;
2233
Andrew Mortona07fa392006-03-22 00:08:17 -08002234 on_each_cpu(do_drain, cachep, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 check_irq_on();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002236 for_each_online_node(node) {
Christoph Lametere498be72005-09-09 13:03:32 -07002237 l3 = cachep->nodelists[node];
Roland Dreiera4523a82006-05-15 11:41:00 -07002238 if (l3 && l3->alien)
2239 drain_alien_cache(cachep, l3->alien);
2240 }
2241
2242 for_each_online_node(node) {
2243 l3 = cachep->nodelists[node];
2244 if (l3)
Christoph Lameteraab22072006-03-22 00:09:06 -08002245 drain_array(cachep, l3, l3->shared, 1, node);
Christoph Lametere498be72005-09-09 13:03:32 -07002246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247}
2248
Christoph Lametered11d9e2006-06-30 01:55:45 -07002249/*
2250 * Remove slabs from the list of free slabs.
2251 * Specify the number of slabs to drain in tofree.
2252 *
2253 * Returns the actual number of slabs released.
2254 */
2255static int drain_freelist(struct kmem_cache *cache,
2256 struct kmem_list3 *l3, int tofree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257{
Christoph Lametered11d9e2006-06-30 01:55:45 -07002258 struct list_head *p;
2259 int nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Christoph Lametered11d9e2006-06-30 01:55:45 -07002262 nr_freed = 0;
2263 while (nr_freed < tofree && !list_empty(&l3->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Christoph Lametered11d9e2006-06-30 01:55:45 -07002265 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002266 p = l3->slabs_free.prev;
Christoph Lametered11d9e2006-06-30 01:55:45 -07002267 if (p == &l3->slabs_free) {
2268 spin_unlock_irq(&l3->list_lock);
2269 goto out;
2270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Christoph Lametered11d9e2006-06-30 01:55:45 -07002272 slabp = list_entry(p, struct slab, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273#if DEBUG
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002274 BUG_ON(slabp->inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275#endif
2276 list_del(&slabp->list);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002277 /*
2278 * Safe to drop the lock. The slab is no longer linked
2279 * to the cache.
2280 */
2281 l3->free_objects -= cache->num;
Christoph Lametere498be72005-09-09 13:03:32 -07002282 spin_unlock_irq(&l3->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002283 slab_destroy(cache, slabp);
2284 nr_freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 }
Christoph Lametered11d9e2006-06-30 01:55:45 -07002286out:
2287 return nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
2289
Pekka Enberg343e0d72006-02-01 03:05:50 -08002290static int __cache_shrink(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07002291{
2292 int ret = 0, i = 0;
2293 struct kmem_list3 *l3;
2294
2295 drain_cpu_caches(cachep);
2296
2297 check_irq_on();
2298 for_each_online_node(i) {
2299 l3 = cachep->nodelists[i];
Christoph Lametered11d9e2006-06-30 01:55:45 -07002300 if (!l3)
2301 continue;
2302
2303 drain_freelist(cachep, l3, l3->free_objects);
2304
2305 ret += !list_empty(&l3->slabs_full) ||
2306 !list_empty(&l3->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07002307 }
2308 return (ret ? 1 : 0);
2309}
2310
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311/**
2312 * kmem_cache_shrink - Shrink a cache.
2313 * @cachep: The cache to shrink.
2314 *
2315 * Releases as many slabs as possible for a cache.
2316 * To help debugging, a zero exit status indicates all slabs were released.
2317 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002318int kmem_cache_shrink(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002320 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 return __cache_shrink(cachep);
2323}
2324EXPORT_SYMBOL(kmem_cache_shrink);
2325
2326/**
2327 * kmem_cache_destroy - delete a cache
2328 * @cachep: the cache to destroy
2329 *
Pekka Enberg343e0d72006-02-01 03:05:50 -08002330 * Remove a struct kmem_cache object from the slab cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 * Returns 0 on success.
2332 *
2333 * It is expected this function will be called by a module when it is
2334 * unloaded. This will remove the cache completely, and avoid a duplicate
2335 * cache being allocated each time a module is loaded and unloaded, if the
2336 * module doesn't have persistent in-kernel storage across loads and unloads.
2337 *
2338 * The cache must be empty before calling this function.
2339 *
2340 * The caller must guarantee that noone will allocate memory from the cache
2341 * during the kmem_cache_destroy().
2342 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002343int kmem_cache_destroy(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344{
2345 int i;
Christoph Lametere498be72005-09-09 13:03:32 -07002346 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002348 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349
2350 /* Don't let CPUs to come and go */
2351 lock_cpu_hotplug();
2352
2353 /* Find the cache in the chain of caches. */
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002354 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 /*
2356 * the chain is never empty, cache_cache is never destroyed
2357 */
2358 list_del(&cachep->next);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002359 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
2361 if (__cache_shrink(cachep)) {
2362 slab_error(cachep, "Can't free all objects");
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002363 mutex_lock(&cache_chain_mutex);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002364 list_add(&cachep->next, &cache_chain);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002365 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 unlock_cpu_hotplug();
2367 return 1;
2368 }
2369
2370 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU))
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002371 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Christoph Lametere498be72005-09-09 13:03:32 -07002373 for_each_online_cpu(i)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002374 kfree(cachep->array[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
2376 /* NUMA: free the list3 structures */
Christoph Lametere498be72005-09-09 13:03:32 -07002377 for_each_online_node(i) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002378 l3 = cachep->nodelists[i];
2379 if (l3) {
Christoph Lametere498be72005-09-09 13:03:32 -07002380 kfree(l3->shared);
2381 free_alien_cache(l3->alien);
2382 kfree(l3);
2383 }
2384 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 kmem_cache_free(&cache_cache, cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 return 0;
2388}
2389EXPORT_SYMBOL(kmem_cache_destroy);
2390
2391/* Get the memory for a slab management obj. */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002392static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002393 int colour_off, gfp_t local_flags,
2394 int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
2396 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398 if (OFF_SLAB(cachep)) {
2399 /* Slab management obj is off-slab. */
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002400 slabp = kmem_cache_alloc_node(cachep->slabp_cache,
2401 local_flags, nodeid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 if (!slabp)
2403 return NULL;
2404 } else {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002405 slabp = objp + colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 colour_off += cachep->slab_size;
2407 }
2408 slabp->inuse = 0;
2409 slabp->colouroff = colour_off;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002410 slabp->s_mem = objp + colour_off;
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002411 slabp->nodeid = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 return slabp;
2413}
2414
2415static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
2416{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002417 return (kmem_bufctl_t *) (slabp + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418}
2419
Pekka Enberg343e0d72006-02-01 03:05:50 -08002420static void cache_init_objs(struct kmem_cache *cachep,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002421 struct slab *slabp, unsigned long ctor_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422{
2423 int i;
2424
2425 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002426 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427#if DEBUG
2428 /* need to poison the objs? */
2429 if (cachep->flags & SLAB_POISON)
2430 poison_obj(cachep, objp, POISON_FREE);
2431 if (cachep->flags & SLAB_STORE_USER)
2432 *dbg_userword(cachep, objp) = NULL;
2433
2434 if (cachep->flags & SLAB_RED_ZONE) {
2435 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2436 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2437 }
2438 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002439 * Constructors are not allowed to allocate memory from the same
2440 * cache which they are a constructor for. Otherwise, deadlock.
2441 * They must also be threaded.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 */
2443 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002444 cachep->ctor(objp + obj_offset(cachep), cachep,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002445 ctor_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
2447 if (cachep->flags & SLAB_RED_ZONE) {
2448 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2449 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002450 " end of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2452 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002453 " start of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 }
Andrew Mortona737b3e2006-03-22 00:08:11 -08002455 if ((cachep->buffer_size % PAGE_SIZE) == 0 &&
2456 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002457 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002458 cachep->buffer_size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459#else
2460 if (cachep->ctor)
2461 cachep->ctor(objp, cachep, ctor_flags);
2462#endif
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002463 slab_bufctl(slabp)[i] = i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002465 slab_bufctl(slabp)[i - 1] = BUFCTL_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 slabp->free = 0;
2467}
2468
Pekka Enberg343e0d72006-02-01 03:05:50 -08002469static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470{
Andrew Mortona737b3e2006-03-22 00:08:11 -08002471 if (flags & SLAB_DMA)
2472 BUG_ON(!(cachep->gfpflags & GFP_DMA));
2473 else
2474 BUG_ON(cachep->gfpflags & GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
Andrew Mortona737b3e2006-03-22 00:08:11 -08002477static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
2478 int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002479{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002480 void *objp = index_to_obj(cachep, slabp, slabp->free);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002481 kmem_bufctl_t next;
2482
2483 slabp->inuse++;
2484 next = slab_bufctl(slabp)[slabp->free];
2485#if DEBUG
2486 slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
2487 WARN_ON(slabp->nodeid != nodeid);
2488#endif
2489 slabp->free = next;
2490
2491 return objp;
2492}
2493
Andrew Mortona737b3e2006-03-22 00:08:11 -08002494static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
2495 void *objp, int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002496{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002497 unsigned int objnr = obj_to_index(cachep, slabp, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002498
2499#if DEBUG
2500 /* Verify that the slab belongs to the intended node */
2501 WARN_ON(slabp->nodeid != nodeid);
2502
Al Viro871751e2006-03-25 03:06:39 -08002503 if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) {
Matthew Dobson78d382d2006-02-01 03:05:47 -08002504 printk(KERN_ERR "slab: double free detected in cache "
Andrew Mortona737b3e2006-03-22 00:08:11 -08002505 "'%s', objp %p\n", cachep->name, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002506 BUG();
2507 }
2508#endif
2509 slab_bufctl(slabp)[objnr] = slabp->free;
2510 slabp->free = objnr;
2511 slabp->inuse--;
2512}
2513
Pekka Enberg47768742006-06-23 02:03:07 -07002514/*
2515 * Map pages beginning at addr to the given cache and slab. This is required
2516 * for the slab allocator to be able to lookup the cache and slab of a
2517 * virtual address for kfree, ksize, kmem_ptr_validate, and slab debugging.
2518 */
2519static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
2520 void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521{
Pekka Enberg47768742006-06-23 02:03:07 -07002522 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 struct page *page;
2524
Pekka Enberg47768742006-06-23 02:03:07 -07002525 page = virt_to_page(addr);
Nick Piggin84097512006-03-22 00:08:34 -08002526
Pekka Enberg47768742006-06-23 02:03:07 -07002527 nr_pages = 1;
Nick Piggin84097512006-03-22 00:08:34 -08002528 if (likely(!PageCompound(page)))
Pekka Enberg47768742006-06-23 02:03:07 -07002529 nr_pages <<= cache->gfporder;
2530
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 do {
Pekka Enberg47768742006-06-23 02:03:07 -07002532 page_set_cache(page, cache);
2533 page_set_slab(page, slab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 page++;
Pekka Enberg47768742006-06-23 02:03:07 -07002535 } while (--nr_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536}
2537
2538/*
2539 * Grow (by 1) the number of slabs within a cache. This is called by
2540 * kmem_cache_alloc() when there are no active objs left in a cache.
2541 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002542static int cache_grow(struct kmem_cache *cachep, gfp_t flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002544 struct slab *slabp;
2545 void *objp;
2546 size_t offset;
2547 gfp_t local_flags;
2548 unsigned long ctor_flags;
Christoph Lametere498be72005-09-09 13:03:32 -07002549 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550
Andrew Mortona737b3e2006-03-22 00:08:11 -08002551 /*
2552 * Be lazy and only check for valid flags here, keeping it out of the
2553 * critical path in kmem_cache_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 */
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002555 BUG_ON(flags & ~(SLAB_DMA | SLAB_LEVEL_MASK | SLAB_NO_GROW));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 if (flags & SLAB_NO_GROW)
2557 return 0;
2558
2559 ctor_flags = SLAB_CTOR_CONSTRUCTOR;
2560 local_flags = (flags & SLAB_LEVEL_MASK);
2561 if (!(local_flags & __GFP_WAIT))
2562 /*
2563 * Not allowed to sleep. Need to tell a constructor about
2564 * this - it might need to know...
2565 */
2566 ctor_flags |= SLAB_CTOR_ATOMIC;
2567
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002568 /* Take the l3 list lock to change the colour_next on this node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 check_irq_off();
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002570 l3 = cachep->nodelists[nodeid];
2571 spin_lock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572
2573 /* Get colour for the slab, and cal the next value. */
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002574 offset = l3->colour_next;
2575 l3->colour_next++;
2576 if (l3->colour_next >= cachep->colour)
2577 l3->colour_next = 0;
2578 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002580 offset *= cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581
2582 if (local_flags & __GFP_WAIT)
2583 local_irq_enable();
2584
2585 /*
2586 * The test for missing atomic flag is performed here, rather than
2587 * the more obvious place, simply to reduce the critical path length
2588 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2589 * will eventually be caught here (where it matters).
2590 */
2591 kmem_flagcheck(cachep, flags);
2592
Andrew Mortona737b3e2006-03-22 00:08:11 -08002593 /*
2594 * Get mem for the objs. Attempt to allocate a physical page from
2595 * 'nodeid'.
Christoph Lametere498be72005-09-09 13:03:32 -07002596 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002597 objp = kmem_getpages(cachep, flags, nodeid);
2598 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599 goto failed;
2600
2601 /* Get slab management. */
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002602 slabp = alloc_slabmgmt(cachep, objp, offset, local_flags, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002603 if (!slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 goto opps1;
2605
Christoph Lametere498be72005-09-09 13:03:32 -07002606 slabp->nodeid = nodeid;
Pekka Enberg47768742006-06-23 02:03:07 -07002607 slab_map_pages(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608
2609 cache_init_objs(cachep, slabp, ctor_flags);
2610
2611 if (local_flags & __GFP_WAIT)
2612 local_irq_disable();
2613 check_irq_off();
Christoph Lametere498be72005-09-09 13:03:32 -07002614 spin_lock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 /* Make slab active. */
Christoph Lametere498be72005-09-09 13:03:32 -07002617 list_add_tail(&slabp->list, &(l3->slabs_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 STATS_INC_GROWN(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07002619 l3->free_objects += cachep->num;
2620 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002622opps1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 kmem_freepages(cachep, objp);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002624failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 if (local_flags & __GFP_WAIT)
2626 local_irq_disable();
2627 return 0;
2628}
2629
2630#if DEBUG
2631
2632/*
2633 * Perform extra freeing checks:
2634 * - detect bad pointers.
2635 * - POISON/RED_ZONE checking
2636 * - destructor calls, for caches with POISON+dtor
2637 */
2638static void kfree_debugcheck(const void *objp)
2639{
2640 struct page *page;
2641
2642 if (!virt_addr_valid(objp)) {
2643 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002644 (unsigned long)objp);
2645 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 }
2647 page = virt_to_page(objp);
2648 if (!PageSlab(page)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002649 printk(KERN_ERR "kfree_debugcheck: bad ptr %lxh.\n",
2650 (unsigned long)objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 BUG();
2652 }
2653}
2654
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002655static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2656{
2657 unsigned long redzone1, redzone2;
2658
2659 redzone1 = *dbg_redzone1(cache, obj);
2660 redzone2 = *dbg_redzone2(cache, obj);
2661
2662 /*
2663 * Redzone is ok.
2664 */
2665 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2666 return;
2667
2668 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2669 slab_error(cache, "double free detected");
2670 else
2671 slab_error(cache, "memory outside object was overwritten");
2672
2673 printk(KERN_ERR "%p: redzone 1:0x%lx, redzone 2:0x%lx.\n",
2674 obj, redzone1, redzone2);
2675}
2676
Pekka Enberg343e0d72006-02-01 03:05:50 -08002677static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002678 void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679{
2680 struct page *page;
2681 unsigned int objnr;
2682 struct slab *slabp;
2683
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002684 objp -= obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 kfree_debugcheck(objp);
2686 page = virt_to_page(objp);
2687
Pekka Enberg065d41c2005-11-13 16:06:46 -08002688 slabp = page_get_slab(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
2690 if (cachep->flags & SLAB_RED_ZONE) {
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002691 verify_redzone_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2693 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2694 }
2695 if (cachep->flags & SLAB_STORE_USER)
2696 *dbg_userword(cachep, objp) = caller;
2697
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002698 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699
2700 BUG_ON(objnr >= cachep->num);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002701 BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702
2703 if (cachep->flags & SLAB_DEBUG_INITIAL) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002704 /*
2705 * Need to call the slab's constructor so the caller can
2706 * perform a verify of its state (debugging). Called without
2707 * the cache-lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 */
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002709 cachep->ctor(objp + obj_offset(cachep),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002710 cachep, SLAB_CTOR_CONSTRUCTOR | SLAB_CTOR_VERIFY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 }
2712 if (cachep->flags & SLAB_POISON && cachep->dtor) {
2713 /* we want to cache poison the object,
2714 * call the destruction callback
2715 */
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002716 cachep->dtor(objp + obj_offset(cachep), cachep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 }
Al Viro871751e2006-03-25 03:06:39 -08002718#ifdef CONFIG_DEBUG_SLAB_LEAK
2719 slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
2720#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002721 if (cachep->flags & SLAB_POISON) {
2722#ifdef CONFIG_DEBUG_PAGEALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -08002723 if ((cachep->buffer_size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 store_stackinfo(cachep, objp, (unsigned long)caller);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002725 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002726 cachep->buffer_size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 } else {
2728 poison_obj(cachep, objp, POISON_FREE);
2729 }
2730#else
2731 poison_obj(cachep, objp, POISON_FREE);
2732#endif
2733 }
2734 return objp;
2735}
2736
Pekka Enberg343e0d72006-02-01 03:05:50 -08002737static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738{
2739 kmem_bufctl_t i;
2740 int entries = 0;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 /* Check slab's freelist to see if this obj is there. */
2743 for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
2744 entries++;
2745 if (entries > cachep->num || i >= cachep->num)
2746 goto bad;
2747 }
2748 if (entries != cachep->num - slabp->inuse) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002749bad:
2750 printk(KERN_ERR "slab: Internal list corruption detected in "
2751 "cache '%s'(%d), slabp %p(%d). Hexdump:\n",
2752 cachep->name, cachep->num, slabp, slabp->inuse);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002753 for (i = 0;
Linus Torvalds264132b2006-03-06 12:10:07 -08002754 i < sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002755 i++) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002756 if (i % 16 == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 printk("\n%03x:", i);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002758 printk(" %02x", ((unsigned char *)slabp)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 }
2760 printk("\n");
2761 BUG();
2762 }
2763}
2764#else
2765#define kfree_debugcheck(x) do { } while(0)
2766#define cache_free_debugcheck(x,objp,z) (objp)
2767#define check_slabp(x,y) do { } while(0)
2768#endif
2769
Pekka Enberg343e0d72006-02-01 03:05:50 -08002770static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
2772 int batchcount;
2773 struct kmem_list3 *l3;
2774 struct array_cache *ac;
2775
2776 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002777 ac = cpu_cache_get(cachep);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002778retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 batchcount = ac->batchcount;
2780 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002781 /*
2782 * If there was little recent activity on this cache, then
2783 * perform only a partial refill. Otherwise we could generate
2784 * refill bouncing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 */
2786 batchcount = BATCHREFILL_LIMIT;
2787 }
Christoph Lametere498be72005-09-09 13:03:32 -07002788 l3 = cachep->nodelists[numa_node_id()];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Christoph Lametere498be72005-09-09 13:03:32 -07002790 BUG_ON(ac->avail > 0 || !l3);
2791 spin_lock(&l3->list_lock);
2792
Christoph Lameter3ded1752006-03-25 03:06:44 -08002793 /* See if we can refill from the shared array */
2794 if (l3->shared && transfer_objects(ac, l3->shared, batchcount))
2795 goto alloc_done;
2796
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 while (batchcount > 0) {
2798 struct list_head *entry;
2799 struct slab *slabp;
2800 /* Get slab alloc is to come from. */
2801 entry = l3->slabs_partial.next;
2802 if (entry == &l3->slabs_partial) {
2803 l3->free_touched = 1;
2804 entry = l3->slabs_free.next;
2805 if (entry == &l3->slabs_free)
2806 goto must_grow;
2807 }
2808
2809 slabp = list_entry(entry, struct slab, list);
2810 check_slabp(cachep, slabp);
2811 check_spinlock_acquired(cachep);
2812 while (slabp->inuse < cachep->num && batchcount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 STATS_INC_ALLOCED(cachep);
2814 STATS_INC_ACTIVE(cachep);
2815 STATS_SET_HIGH(cachep);
2816
Matthew Dobson78d382d2006-02-01 03:05:47 -08002817 ac->entry[ac->avail++] = slab_get_obj(cachep, slabp,
2818 numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 }
2820 check_slabp(cachep, slabp);
2821
2822 /* move slabp to correct slabp list: */
2823 list_del(&slabp->list);
2824 if (slabp->free == BUFCTL_END)
2825 list_add(&slabp->list, &l3->slabs_full);
2826 else
2827 list_add(&slabp->list, &l3->slabs_partial);
2828 }
2829
Andrew Mortona737b3e2006-03-22 00:08:11 -08002830must_grow:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 l3->free_objects -= ac->avail;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002832alloc_done:
Christoph Lametere498be72005-09-09 13:03:32 -07002833 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
2835 if (unlikely(!ac->avail)) {
2836 int x;
Christoph Lametere498be72005-09-09 13:03:32 -07002837 x = cache_grow(cachep, flags, numa_node_id());
2838
Andrew Mortona737b3e2006-03-22 00:08:11 -08002839 /* cache_grow can reenable interrupts, then ac could change. */
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002840 ac = cpu_cache_get(cachep);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002841 if (!x && ac->avail == 0) /* no objects in sight? abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 return NULL;
2843
Andrew Mortona737b3e2006-03-22 00:08:11 -08002844 if (!ac->avail) /* objects refilled by interrupt? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 goto retry;
2846 }
2847 ac->touched = 1;
Christoph Lametere498be72005-09-09 13:03:32 -07002848 return ac->entry[--ac->avail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849}
2850
Andrew Mortona737b3e2006-03-22 00:08:11 -08002851static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
2852 gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853{
2854 might_sleep_if(flags & __GFP_WAIT);
2855#if DEBUG
2856 kmem_flagcheck(cachep, flags);
2857#endif
2858}
2859
2860#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08002861static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
2862 gfp_t flags, void *objp, void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002864 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 return objp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002866 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867#ifdef CONFIG_DEBUG_PAGEALLOC
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002868 if ((cachep->buffer_size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002869 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002870 cachep->buffer_size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871 else
2872 check_poison_obj(cachep, objp);
2873#else
2874 check_poison_obj(cachep, objp);
2875#endif
2876 poison_obj(cachep, objp, POISON_INUSE);
2877 }
2878 if (cachep->flags & SLAB_STORE_USER)
2879 *dbg_userword(cachep, objp) = caller;
2880
2881 if (cachep->flags & SLAB_RED_ZONE) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002882 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
2883 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
2884 slab_error(cachep, "double free, or memory outside"
2885 " object was overwritten");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002886 printk(KERN_ERR
Andrew Mortona737b3e2006-03-22 00:08:11 -08002887 "%p: redzone 1:0x%lx, redzone 2:0x%lx\n",
2888 objp, *dbg_redzone1(cachep, objp),
2889 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 }
2891 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
2892 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
2893 }
Al Viro871751e2006-03-25 03:06:39 -08002894#ifdef CONFIG_DEBUG_SLAB_LEAK
2895 {
2896 struct slab *slabp;
2897 unsigned objnr;
2898
2899 slabp = page_get_slab(virt_to_page(objp));
2900 objnr = (unsigned)(objp - slabp->s_mem) / cachep->buffer_size;
2901 slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE;
2902 }
2903#endif
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002904 objp += obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905 if (cachep->ctor && cachep->flags & SLAB_POISON) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002906 unsigned long ctor_flags = SLAB_CTOR_CONSTRUCTOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907
2908 if (!(flags & __GFP_WAIT))
2909 ctor_flags |= SLAB_CTOR_ATOMIC;
2910
2911 cachep->ctor(objp, cachep, ctor_flags);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 return objp;
2914}
2915#else
2916#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
2917#endif
2918
Pekka Enberg343e0d72006-02-01 03:05:50 -08002919static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002921 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 struct array_cache *ac;
2923
Christoph Lameterdc85da12006-01-18 17:42:36 -08002924#ifdef CONFIG_NUMA
Paul Jacksonb2455392006-03-24 03:16:12 -08002925 if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
Paul Jacksonc61afb12006-03-24 03:16:08 -08002926 objp = alternate_node_alloc(cachep, flags);
2927 if (objp != NULL)
2928 return objp;
Paul Jackson101a5002006-03-24 03:16:07 -08002929 }
Christoph Lameterdc85da12006-01-18 17:42:36 -08002930#endif
2931
Alok N Kataria5c382302005-09-27 21:45:46 -07002932 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002933 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 if (likely(ac->avail)) {
2935 STATS_INC_ALLOCHIT(cachep);
2936 ac->touched = 1;
Christoph Lametere498be72005-09-09 13:03:32 -07002937 objp = ac->entry[--ac->avail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 } else {
2939 STATS_INC_ALLOCMISS(cachep);
2940 objp = cache_alloc_refill(cachep, flags);
2941 }
Alok N Kataria5c382302005-09-27 21:45:46 -07002942 return objp;
2943}
2944
Andrew Mortona737b3e2006-03-22 00:08:11 -08002945static __always_inline void *__cache_alloc(struct kmem_cache *cachep,
2946 gfp_t flags, void *caller)
Alok N Kataria5c382302005-09-27 21:45:46 -07002947{
2948 unsigned long save_flags;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002949 void *objp;
Alok N Kataria5c382302005-09-27 21:45:46 -07002950
2951 cache_alloc_debugcheck_before(cachep, flags);
2952
2953 local_irq_save(save_flags);
2954 objp = ____cache_alloc(cachep, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 local_irq_restore(save_flags);
Eric Dumazet34342e82005-09-03 15:55:06 -07002956 objp = cache_alloc_debugcheck_after(cachep, flags, objp,
Pekka Enberg7fd6b142006-02-01 03:05:52 -08002957 caller);
Eric Dumazet34342e82005-09-03 15:55:06 -07002958 prefetchw(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 return objp;
2960}
2961
Christoph Lametere498be72005-09-09 13:03:32 -07002962#ifdef CONFIG_NUMA
2963/*
Paul Jacksonb2455392006-03-24 03:16:12 -08002964 * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
Paul Jacksonc61afb12006-03-24 03:16:08 -08002965 *
2966 * If we are in_interrupt, then process context, including cpusets and
2967 * mempolicy, may not apply and should not be used for allocation policy.
2968 */
2969static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
2970{
2971 int nid_alloc, nid_here;
2972
2973 if (in_interrupt())
2974 return NULL;
2975 nid_alloc = nid_here = numa_node_id();
2976 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
2977 nid_alloc = cpuset_mem_spread_node();
2978 else if (current->mempolicy)
2979 nid_alloc = slab_node(current->mempolicy);
2980 if (nid_alloc != nid_here)
2981 return __cache_alloc_node(cachep, flags, nid_alloc);
2982 return NULL;
2983}
2984
2985/*
Christoph Lametere498be72005-09-09 13:03:32 -07002986 * A interface to enable slab creation on nodeid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002988static void *__cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
2989 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07002990{
2991 struct list_head *entry;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002992 struct slab *slabp;
2993 struct kmem_list3 *l3;
2994 void *obj;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002995 int x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002997 l3 = cachep->nodelists[nodeid];
2998 BUG_ON(!l3);
Christoph Lametere498be72005-09-09 13:03:32 -07002999
Andrew Mortona737b3e2006-03-22 00:08:11 -08003000retry:
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003001 check_irq_off();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003002 spin_lock(&l3->list_lock);
3003 entry = l3->slabs_partial.next;
3004 if (entry == &l3->slabs_partial) {
3005 l3->free_touched = 1;
3006 entry = l3->slabs_free.next;
3007 if (entry == &l3->slabs_free)
3008 goto must_grow;
3009 }
Christoph Lametere498be72005-09-09 13:03:32 -07003010
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003011 slabp = list_entry(entry, struct slab, list);
3012 check_spinlock_acquired_node(cachep, nodeid);
3013 check_slabp(cachep, slabp);
Christoph Lametere498be72005-09-09 13:03:32 -07003014
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003015 STATS_INC_NODEALLOCS(cachep);
3016 STATS_INC_ACTIVE(cachep);
3017 STATS_SET_HIGH(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003018
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003019 BUG_ON(slabp->inuse == cachep->num);
Christoph Lametere498be72005-09-09 13:03:32 -07003020
Matthew Dobson78d382d2006-02-01 03:05:47 -08003021 obj = slab_get_obj(cachep, slabp, nodeid);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003022 check_slabp(cachep, slabp);
3023 l3->free_objects--;
3024 /* move slabp to correct slabp list: */
3025 list_del(&slabp->list);
Christoph Lametere498be72005-09-09 13:03:32 -07003026
Andrew Mortona737b3e2006-03-22 00:08:11 -08003027 if (slabp->free == BUFCTL_END)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003028 list_add(&slabp->list, &l3->slabs_full);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003029 else
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003030 list_add(&slabp->list, &l3->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07003031
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003032 spin_unlock(&l3->list_lock);
3033 goto done;
Christoph Lametere498be72005-09-09 13:03:32 -07003034
Andrew Mortona737b3e2006-03-22 00:08:11 -08003035must_grow:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003036 spin_unlock(&l3->list_lock);
3037 x = cache_grow(cachep, flags, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07003038
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003039 if (!x)
3040 return NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003041
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003042 goto retry;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003043done:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003044 return obj;
Christoph Lametere498be72005-09-09 13:03:32 -07003045}
3046#endif
3047
3048/*
3049 * Caller needs to acquire correct kmem_list's list_lock
3050 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003051static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003052 int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053{
3054 int i;
Christoph Lametere498be72005-09-09 13:03:32 -07003055 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
3057 for (i = 0; i < nr_objects; i++) {
3058 void *objp = objpp[i];
3059 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
Pekka Enberg6ed5eb22006-02-01 03:05:49 -08003061 slabp = virt_to_slab(objp);
Christoph Lameterff694162005-09-22 21:44:02 -07003062 l3 = cachep->nodelists[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 list_del(&slabp->list);
Christoph Lameterff694162005-09-22 21:44:02 -07003064 check_spinlock_acquired_node(cachep, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 check_slabp(cachep, slabp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08003066 slab_put_obj(cachep, slabp, objp, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 STATS_DEC_ACTIVE(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003068 l3->free_objects++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 check_slabp(cachep, slabp);
3070
3071 /* fixup slab chains */
3072 if (slabp->inuse == 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07003073 if (l3->free_objects > l3->free_limit) {
3074 l3->free_objects -= cachep->num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 slab_destroy(cachep, slabp);
3076 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07003077 list_add(&slabp->list, &l3->slabs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 }
3079 } else {
3080 /* Unconditionally move a slab to the end of the
3081 * partial list on free - maximum time for the
3082 * other objects to be freed, too.
3083 */
Christoph Lametere498be72005-09-09 13:03:32 -07003084 list_add_tail(&slabp->list, &l3->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 }
3086 }
3087}
3088
Pekka Enberg343e0d72006-02-01 03:05:50 -08003089static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090{
3091 int batchcount;
Christoph Lametere498be72005-09-09 13:03:32 -07003092 struct kmem_list3 *l3;
Christoph Lameterff694162005-09-22 21:44:02 -07003093 int node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094
3095 batchcount = ac->batchcount;
3096#if DEBUG
3097 BUG_ON(!batchcount || batchcount > ac->avail);
3098#endif
3099 check_irq_off();
Christoph Lameterff694162005-09-22 21:44:02 -07003100 l3 = cachep->nodelists[node];
Christoph Lametere498be72005-09-09 13:03:32 -07003101 spin_lock(&l3->list_lock);
3102 if (l3->shared) {
3103 struct array_cache *shared_array = l3->shared;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003104 int max = shared_array->limit - shared_array->avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 if (max) {
3106 if (batchcount > max)
3107 batchcount = max;
Christoph Lametere498be72005-09-09 13:03:32 -07003108 memcpy(&(shared_array->entry[shared_array->avail]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003109 ac->entry, sizeof(void *) * batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 shared_array->avail += batchcount;
3111 goto free_done;
3112 }
3113 }
3114
Christoph Lameterff694162005-09-22 21:44:02 -07003115 free_block(cachep, ac->entry, batchcount, node);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003116free_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117#if STATS
3118 {
3119 int i = 0;
3120 struct list_head *p;
3121
Christoph Lametere498be72005-09-09 13:03:32 -07003122 p = l3->slabs_free.next;
3123 while (p != &(l3->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 struct slab *slabp;
3125
3126 slabp = list_entry(p, struct slab, list);
3127 BUG_ON(slabp->inuse);
3128
3129 i++;
3130 p = p->next;
3131 }
3132 STATS_SET_FREEABLE(cachep, i);
3133 }
3134#endif
Christoph Lametere498be72005-09-09 13:03:32 -07003135 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136 ac->avail -= batchcount;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003137 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138}
3139
3140/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08003141 * Release an obj back to its cache. If the obj has a constructed state, it must
3142 * be in this state _before_ it is released. Called with disabled ints.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003144static inline void __cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145{
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003146 struct array_cache *ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147
3148 check_irq_off();
3149 objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));
3150
Pekka Enberg729bd0b2006-06-23 02:03:05 -07003151 if (cache_free_alien(cachep, objp))
3152 return;
Christoph Lametere498be72005-09-09 13:03:32 -07003153
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 if (likely(ac->avail < ac->limit)) {
3155 STATS_INC_FREEHIT(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003156 ac->entry[ac->avail++] = objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 return;
3158 } else {
3159 STATS_INC_FREEMISS(cachep);
3160 cache_flusharray(cachep, ac);
Christoph Lametere498be72005-09-09 13:03:32 -07003161 ac->entry[ac->avail++] = objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 }
3163}
3164
3165/**
3166 * kmem_cache_alloc - Allocate an object
3167 * @cachep: The cache to allocate from.
3168 * @flags: See kmalloc().
3169 *
3170 * Allocate an object from this cache. The flags are only relevant
3171 * if the cache has no available objects.
3172 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003173void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174{
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003175 return __cache_alloc(cachep, flags, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176}
3177EXPORT_SYMBOL(kmem_cache_alloc);
3178
3179/**
Pekka Enberga8c0f9a2006-03-25 03:06:42 -08003180 * kmem_cache_alloc - Allocate an object. The memory is set to zero.
3181 * @cache: The cache to allocate from.
3182 * @flags: See kmalloc().
3183 *
3184 * Allocate an object from this cache and set the allocated memory to zero.
3185 * The flags are only relevant if the cache has no available objects.
3186 */
3187void *kmem_cache_zalloc(struct kmem_cache *cache, gfp_t flags)
3188{
3189 void *ret = __cache_alloc(cache, flags, __builtin_return_address(0));
3190 if (ret)
3191 memset(ret, 0, obj_size(cache));
3192 return ret;
3193}
3194EXPORT_SYMBOL(kmem_cache_zalloc);
3195
3196/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 * kmem_ptr_validate - check if an untrusted pointer might
3198 * be a slab entry.
3199 * @cachep: the cache we're checking against
3200 * @ptr: pointer to validate
3201 *
3202 * This verifies that the untrusted pointer looks sane:
3203 * it is _not_ a guarantee that the pointer is actually
3204 * part of the slab cache in question, but it at least
3205 * validates that the pointer can be dereferenced and
3206 * looks half-way sane.
3207 *
3208 * Currently only used for dentry validation.
3209 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003210int fastcall kmem_ptr_validate(struct kmem_cache *cachep, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003212 unsigned long addr = (unsigned long)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 unsigned long min_addr = PAGE_OFFSET;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003214 unsigned long align_mask = BYTES_PER_WORD - 1;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003215 unsigned long size = cachep->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 struct page *page;
3217
3218 if (unlikely(addr < min_addr))
3219 goto out;
3220 if (unlikely(addr > (unsigned long)high_memory - size))
3221 goto out;
3222 if (unlikely(addr & align_mask))
3223 goto out;
3224 if (unlikely(!kern_addr_valid(addr)))
3225 goto out;
3226 if (unlikely(!kern_addr_valid(addr + size - 1)))
3227 goto out;
3228 page = virt_to_page(ptr);
3229 if (unlikely(!PageSlab(page)))
3230 goto out;
Pekka Enberg065d41c2005-11-13 16:06:46 -08003231 if (unlikely(page_get_cache(page) != cachep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 goto out;
3233 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003234out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 return 0;
3236}
3237
3238#ifdef CONFIG_NUMA
3239/**
3240 * kmem_cache_alloc_node - Allocate an object on the specified node
3241 * @cachep: The cache to allocate from.
3242 * @flags: See kmalloc().
3243 * @nodeid: node number of the target node.
3244 *
3245 * Identical to kmem_cache_alloc, except that this function is slow
3246 * and can sleep. And it will allocate memory on the given node, which
3247 * can improve the performance for cpu bound structures.
Christoph Lametere498be72005-09-09 13:03:32 -07003248 * New and improved: it will now make sure that the object gets
3249 * put on the correct node list so that there is no false sharing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003251void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252{
Christoph Lametere498be72005-09-09 13:03:32 -07003253 unsigned long save_flags;
3254 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255
Christoph Lametere498be72005-09-09 13:03:32 -07003256 cache_alloc_debugcheck_before(cachep, flags);
3257 local_irq_save(save_flags);
Christoph Lameter18f820f2006-02-01 03:05:43 -08003258
3259 if (nodeid == -1 || nodeid == numa_node_id() ||
Andrew Mortona737b3e2006-03-22 00:08:11 -08003260 !cachep->nodelists[nodeid])
Alok N Kataria5c382302005-09-27 21:45:46 -07003261 ptr = ____cache_alloc(cachep, flags);
3262 else
3263 ptr = __cache_alloc_node(cachep, flags, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07003264 local_irq_restore(save_flags);
Christoph Lameter18f820f2006-02-01 03:05:43 -08003265
3266 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr,
3267 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Christoph Lametere498be72005-09-09 13:03:32 -07003269 return ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270}
3271EXPORT_SYMBOL(kmem_cache_alloc_node);
3272
Al Virodd0fc662005-10-07 07:46:04 +01003273void *kmalloc_node(size_t size, gfp_t flags, int node)
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003274{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003275 struct kmem_cache *cachep;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003276
3277 cachep = kmem_find_general_cachep(size, flags);
3278 if (unlikely(cachep == NULL))
3279 return NULL;
3280 return kmem_cache_alloc_node(cachep, flags, node);
3281}
3282EXPORT_SYMBOL(kmalloc_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283#endif
3284
3285/**
Paul Drynoff800590f2006-06-23 02:03:48 -07003286 * __do_kmalloc - allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 * @size: how many bytes of memory are required.
Paul Drynoff800590f2006-06-23 02:03:48 -07003288 * @flags: the type of memory to allocate (see kmalloc).
Randy Dunlap911851e2006-03-22 00:08:14 -08003289 * @caller: function caller for debug tracking of the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 */
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003291static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
3292 void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003294 struct kmem_cache *cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003295
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003296 /* If you want to save a few bytes .text space: replace
3297 * __ with kmem_.
3298 * Then kmalloc uses the uninlined functions instead of the inline
3299 * functions.
3300 */
3301 cachep = __find_general_cachep(size, flags);
Andrew Mortondbdb9042005-09-23 13:24:10 -07003302 if (unlikely(cachep == NULL))
3303 return NULL;
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003304 return __cache_alloc(cachep, flags, caller);
3305}
3306
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003307
3308void *__kmalloc(size_t size, gfp_t flags)
3309{
Al Viro871751e2006-03-25 03:06:39 -08003310#ifndef CONFIG_DEBUG_SLAB
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003311 return __do_kmalloc(size, flags, NULL);
Al Viro871751e2006-03-25 03:06:39 -08003312#else
3313 return __do_kmalloc(size, flags, __builtin_return_address(0));
3314#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316EXPORT_SYMBOL(__kmalloc);
3317
Al Viro871751e2006-03-25 03:06:39 -08003318#ifdef CONFIG_DEBUG_SLAB
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003319void *__kmalloc_track_caller(size_t size, gfp_t flags, void *caller)
3320{
3321 return __do_kmalloc(size, flags, caller);
3322}
3323EXPORT_SYMBOL(__kmalloc_track_caller);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003324#endif
3325
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326#ifdef CONFIG_SMP
3327/**
3328 * __alloc_percpu - allocate one copy of the object for every present
3329 * cpu in the system, zeroing them.
3330 * Objects should be dereferenced using the per_cpu_ptr macro only.
3331 *
3332 * @size: how many bytes of memory are required.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333 */
Pekka Enbergf9f75002006-01-08 01:00:33 -08003334void *__alloc_percpu(size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335{
3336 int i;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003337 struct percpu_data *pdata = kmalloc(sizeof(*pdata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338
3339 if (!pdata)
3340 return NULL;
3341
Christoph Lametere498be72005-09-09 13:03:32 -07003342 /*
3343 * Cannot use for_each_online_cpu since a cpu may come online
3344 * and we have no way of figuring out how to fix the array
3345 * that we have allocated then....
3346 */
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08003347 for_each_possible_cpu(i) {
Christoph Lametere498be72005-09-09 13:03:32 -07003348 int node = cpu_to_node(i);
3349
3350 if (node_online(node))
3351 pdata->ptrs[i] = kmalloc_node(size, GFP_KERNEL, node);
3352 else
3353 pdata->ptrs[i] = kmalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354
3355 if (!pdata->ptrs[i])
3356 goto unwind_oom;
3357 memset(pdata->ptrs[i], 0, size);
3358 }
3359
3360 /* Catch derefs w/o wrappers */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003361 return (void *)(~(unsigned long)pdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362
Andrew Mortona737b3e2006-03-22 00:08:11 -08003363unwind_oom:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 while (--i >= 0) {
3365 if (!cpu_possible(i))
3366 continue;
3367 kfree(pdata->ptrs[i]);
3368 }
3369 kfree(pdata);
3370 return NULL;
3371}
3372EXPORT_SYMBOL(__alloc_percpu);
3373#endif
3374
3375/**
3376 * kmem_cache_free - Deallocate an object
3377 * @cachep: The cache the allocation was from.
3378 * @objp: The previously allocated object.
3379 *
3380 * Free an object which was previously allocated from this
3381 * cache.
3382 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003383void kmem_cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384{
3385 unsigned long flags;
3386
Pekka Enbergddc2e812006-06-23 02:03:40 -07003387 BUG_ON(virt_to_cache(objp) != cachep);
3388
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 local_irq_save(flags);
3390 __cache_free(cachep, objp);
3391 local_irq_restore(flags);
3392}
3393EXPORT_SYMBOL(kmem_cache_free);
3394
3395/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 * kfree - free previously allocated memory
3397 * @objp: pointer returned by kmalloc.
3398 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07003399 * If @objp is NULL, no operation is performed.
3400 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 * Don't free memory not originally allocated by kmalloc()
3402 * or you will run into trouble.
3403 */
3404void kfree(const void *objp)
3405{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003406 struct kmem_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407 unsigned long flags;
3408
3409 if (unlikely(!objp))
3410 return;
3411 local_irq_save(flags);
3412 kfree_debugcheck(objp);
Pekka Enberg6ed5eb22006-02-01 03:05:49 -08003413 c = virt_to_cache(objp);
Ingo Molnarf9b84042006-06-27 02:54:49 -07003414 debug_check_no_locks_freed(objp, obj_size(c));
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003415 __cache_free(c, (void *)objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 local_irq_restore(flags);
3417}
3418EXPORT_SYMBOL(kfree);
3419
3420#ifdef CONFIG_SMP
3421/**
3422 * free_percpu - free previously allocated percpu memory
3423 * @objp: pointer returned by alloc_percpu.
3424 *
3425 * Don't free memory not originally allocated by alloc_percpu()
3426 * The complemented objp is to check for that.
3427 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003428void free_percpu(const void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429{
3430 int i;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003431 struct percpu_data *p = (struct percpu_data *)(~(unsigned long)objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432
Christoph Lametere498be72005-09-09 13:03:32 -07003433 /*
3434 * We allocate for all cpus so we cannot use for online cpu here.
3435 */
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08003436 for_each_possible_cpu(i)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003437 kfree(p->ptrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 kfree(p);
3439}
3440EXPORT_SYMBOL(free_percpu);
3441#endif
3442
Pekka Enberg343e0d72006-02-01 03:05:50 -08003443unsigned int kmem_cache_size(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003445 return obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446}
3447EXPORT_SYMBOL(kmem_cache_size);
3448
Pekka Enberg343e0d72006-02-01 03:05:50 -08003449const char *kmem_cache_name(struct kmem_cache *cachep)
Arnaldo Carvalho de Melo19449722005-06-18 22:46:19 -07003450{
3451 return cachep->name;
3452}
3453EXPORT_SYMBOL_GPL(kmem_cache_name);
3454
Christoph Lametere498be72005-09-09 13:03:32 -07003455/*
Christoph Lameter0718dc22006-03-25 03:06:47 -08003456 * This initializes kmem_list3 or resizes varioius caches for all nodes.
Christoph Lametere498be72005-09-09 13:03:32 -07003457 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003458static int alloc_kmemlist(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07003459{
3460 int node;
3461 struct kmem_list3 *l3;
Christoph Lametercafeb022006-03-25 03:06:46 -08003462 struct array_cache *new_shared;
3463 struct array_cache **new_alien;
Christoph Lametere498be72005-09-09 13:03:32 -07003464
3465 for_each_online_node(node) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003466
Andrew Mortona737b3e2006-03-22 00:08:11 -08003467 new_alien = alloc_alien_cache(node, cachep->limit);
3468 if (!new_alien)
Christoph Lametere498be72005-09-09 13:03:32 -07003469 goto fail;
Christoph Lametercafeb022006-03-25 03:06:46 -08003470
Christoph Lameter0718dc22006-03-25 03:06:47 -08003471 new_shared = alloc_arraycache(node,
3472 cachep->shared*cachep->batchcount,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003473 0xbaadf00d);
Christoph Lameter0718dc22006-03-25 03:06:47 -08003474 if (!new_shared) {
3475 free_alien_cache(new_alien);
Christoph Lametere498be72005-09-09 13:03:32 -07003476 goto fail;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003477 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003478
Andrew Mortona737b3e2006-03-22 00:08:11 -08003479 l3 = cachep->nodelists[node];
3480 if (l3) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003481 struct array_cache *shared = l3->shared;
3482
Christoph Lametere498be72005-09-09 13:03:32 -07003483 spin_lock_irq(&l3->list_lock);
3484
Christoph Lametercafeb022006-03-25 03:06:46 -08003485 if (shared)
Christoph Lameter0718dc22006-03-25 03:06:47 -08003486 free_block(cachep, shared->entry,
3487 shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07003488
Christoph Lametercafeb022006-03-25 03:06:46 -08003489 l3->shared = new_shared;
3490 if (!l3->alien) {
Christoph Lametere498be72005-09-09 13:03:32 -07003491 l3->alien = new_alien;
3492 new_alien = NULL;
3493 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003494 l3->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003495 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07003496 spin_unlock_irq(&l3->list_lock);
Christoph Lametercafeb022006-03-25 03:06:46 -08003497 kfree(shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003498 free_alien_cache(new_alien);
3499 continue;
3500 }
Andrew Mortona737b3e2006-03-22 00:08:11 -08003501 l3 = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, node);
Christoph Lameter0718dc22006-03-25 03:06:47 -08003502 if (!l3) {
3503 free_alien_cache(new_alien);
3504 kfree(new_shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003505 goto fail;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003506 }
Christoph Lametere498be72005-09-09 13:03:32 -07003507
3508 kmem_list3_init(l3);
3509 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
Andrew Mortona737b3e2006-03-22 00:08:11 -08003510 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lametercafeb022006-03-25 03:06:46 -08003511 l3->shared = new_shared;
Christoph Lametere498be72005-09-09 13:03:32 -07003512 l3->alien = new_alien;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003513 l3->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003514 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07003515 cachep->nodelists[node] = l3;
3516 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003517 return 0;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003518
Andrew Mortona737b3e2006-03-22 00:08:11 -08003519fail:
Christoph Lameter0718dc22006-03-25 03:06:47 -08003520 if (!cachep->next.next) {
3521 /* Cache is not active yet. Roll back what we did */
3522 node--;
3523 while (node >= 0) {
3524 if (cachep->nodelists[node]) {
3525 l3 = cachep->nodelists[node];
3526
3527 kfree(l3->shared);
3528 free_alien_cache(l3->alien);
3529 kfree(l3);
3530 cachep->nodelists[node] = NULL;
3531 }
3532 node--;
3533 }
3534 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003535 return -ENOMEM;
Christoph Lametere498be72005-09-09 13:03:32 -07003536}
3537
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538struct ccupdate_struct {
Pekka Enberg343e0d72006-02-01 03:05:50 -08003539 struct kmem_cache *cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 struct array_cache *new[NR_CPUS];
3541};
3542
3543static void do_ccupdate_local(void *info)
3544{
Andrew Mortona737b3e2006-03-22 00:08:11 -08003545 struct ccupdate_struct *new = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 struct array_cache *old;
3547
3548 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003549 old = cpu_cache_get(new->cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003550
Linus Torvalds1da177e2005-04-16 15:20:36 -07003551 new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
3552 new->new[smp_processor_id()] = old;
3553}
3554
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -08003555/* Always called with the cache_chain_mutex held */
Andrew Mortona737b3e2006-03-22 00:08:11 -08003556static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
3557 int batchcount, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558{
3559 struct ccupdate_struct new;
Christoph Lametere498be72005-09-09 13:03:32 -07003560 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003562 memset(&new.new, 0, sizeof(new.new));
Christoph Lametere498be72005-09-09 13:03:32 -07003563 for_each_online_cpu(i) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003564 new.new[i] = alloc_arraycache(cpu_to_node(i), limit,
3565 batchcount);
Christoph Lametere498be72005-09-09 13:03:32 -07003566 if (!new.new[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003567 for (i--; i >= 0; i--)
3568 kfree(new.new[i]);
Christoph Lametere498be72005-09-09 13:03:32 -07003569 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 }
3571 }
3572 new.cachep = cachep;
3573
Andrew Mortona07fa392006-03-22 00:08:17 -08003574 on_each_cpu(do_ccupdate_local, (void *)&new, 1, 1);
Christoph Lametere498be72005-09-09 13:03:32 -07003575
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 check_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 cachep->batchcount = batchcount;
3578 cachep->limit = limit;
Christoph Lametere498be72005-09-09 13:03:32 -07003579 cachep->shared = shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
Christoph Lametere498be72005-09-09 13:03:32 -07003581 for_each_online_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 struct array_cache *ccold = new.new[i];
3583 if (!ccold)
3584 continue;
Christoph Lametere498be72005-09-09 13:03:32 -07003585 spin_lock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
Christoph Lameterff694162005-09-22 21:44:02 -07003586 free_block(cachep, ccold->entry, ccold->avail, cpu_to_node(i));
Christoph Lametere498be72005-09-09 13:03:32 -07003587 spin_unlock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 kfree(ccold);
3589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590
Christoph Lametere498be72005-09-09 13:03:32 -07003591 err = alloc_kmemlist(cachep);
3592 if (err) {
3593 printk(KERN_ERR "alloc_kmemlist failed for %s, error %d.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003594 cachep->name, -err);
Christoph Lametere498be72005-09-09 13:03:32 -07003595 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003597 return 0;
3598}
3599
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -08003600/* Called with cache_chain_mutex held always */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003601static void enable_cpucache(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602{
3603 int err;
3604 int limit, shared;
3605
Andrew Mortona737b3e2006-03-22 00:08:11 -08003606 /*
3607 * The head array serves three purposes:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003608 * - create a LIFO ordering, i.e. return objects that are cache-warm
3609 * - reduce the number of spinlock operations.
Andrew Mortona737b3e2006-03-22 00:08:11 -08003610 * - reduce the number of linked list operations on the slab and
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 * bufctl chains: array operations are cheaper.
3612 * The numbers are guessed, we should auto-tune as described by
3613 * Bonwick.
3614 */
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003615 if (cachep->buffer_size > 131072)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 limit = 1;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003617 else if (cachep->buffer_size > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 limit = 8;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003619 else if (cachep->buffer_size > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 limit = 24;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003621 else if (cachep->buffer_size > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622 limit = 54;
3623 else
3624 limit = 120;
3625
Andrew Mortona737b3e2006-03-22 00:08:11 -08003626 /*
3627 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
Linus Torvalds1da177e2005-04-16 15:20:36 -07003628 * allocation behaviour: Most allocs on one cpu, most free operations
3629 * on another cpu. For these cases, an efficient object passing between
3630 * cpus is necessary. This is provided by a shared array. The array
3631 * replaces Bonwick's magazine layer.
3632 * On uniprocessor, it's functionally equivalent (but less efficient)
3633 * to a larger limit. Thus disabled by default.
3634 */
3635 shared = 0;
3636#ifdef CONFIG_SMP
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003637 if (cachep->buffer_size <= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 shared = 8;
3639#endif
3640
3641#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003642 /*
3643 * With debugging enabled, large batchcount lead to excessively long
3644 * periods with disabled local interrupts. Limit the batchcount
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 */
3646 if (limit > 32)
3647 limit = 32;
3648#endif
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003649 err = do_tune_cpucache(cachep, limit, (limit + 1) / 2, shared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650 if (err)
3651 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003652 cachep->name, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653}
3654
Christoph Lameter1b552532006-03-22 00:09:07 -08003655/*
3656 * Drain an array if it contains any elements taking the l3 lock only if
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003657 * necessary. Note that the l3 listlock also protects the array_cache
3658 * if drain_array() is used on the shared array.
Christoph Lameter1b552532006-03-22 00:09:07 -08003659 */
3660void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
3661 struct array_cache *ac, int force, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662{
3663 int tofree;
3664
Christoph Lameter1b552532006-03-22 00:09:07 -08003665 if (!ac || !ac->avail)
3666 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 if (ac->touched && !force) {
3668 ac->touched = 0;
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003669 } else {
Christoph Lameter1b552532006-03-22 00:09:07 -08003670 spin_lock_irq(&l3->list_lock);
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003671 if (ac->avail) {
3672 tofree = force ? ac->avail : (ac->limit + 4) / 5;
3673 if (tofree > ac->avail)
3674 tofree = (ac->avail + 1) / 2;
3675 free_block(cachep, ac->entry, tofree, node);
3676 ac->avail -= tofree;
3677 memmove(ac->entry, &(ac->entry[tofree]),
3678 sizeof(void *) * ac->avail);
3679 }
Christoph Lameter1b552532006-03-22 00:09:07 -08003680 spin_unlock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 }
3682}
3683
3684/**
3685 * cache_reap - Reclaim memory from caches.
Randy Dunlap1e5d5332005-11-07 01:01:06 -08003686 * @unused: unused parameter
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 *
3688 * Called from workqueue/eventd every few seconds.
3689 * Purpose:
3690 * - clear the per-cpu caches for this CPU.
3691 * - return freeable pages to the main free memory pool.
3692 *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003693 * If we cannot acquire the cache chain mutex then just give up - we'll try
3694 * again on the next iteration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 */
3696static void cache_reap(void *unused)
3697{
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07003698 struct kmem_cache *searchp;
Christoph Lametere498be72005-09-09 13:03:32 -07003699 struct kmem_list3 *l3;
Christoph Lameteraab22072006-03-22 00:09:06 -08003700 int node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003701
Ingo Molnarfc0abb12006-01-18 17:42:33 -08003702 if (!mutex_trylock(&cache_chain_mutex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 /* Give up. Setup the next iteration. */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003704 schedule_delayed_work(&__get_cpu_var(reap_work),
3705 REAPTIMEOUT_CPUC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 return;
3707 }
3708
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07003709 list_for_each_entry(searchp, &cache_chain, next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 check_irq_on();
3711
Christoph Lameter35386e32006-03-22 00:09:05 -08003712 /*
3713 * We only take the l3 lock if absolutely necessary and we
3714 * have established with reasonable certainty that
3715 * we can do some work if the lock was obtained.
3716 */
Christoph Lameteraab22072006-03-22 00:09:06 -08003717 l3 = searchp->nodelists[node];
Christoph Lameter35386e32006-03-22 00:09:05 -08003718
Christoph Lameter8fce4d82006-03-09 17:33:54 -08003719 reap_alien(searchp, l3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720
Christoph Lameteraab22072006-03-22 00:09:06 -08003721 drain_array(searchp, l3, cpu_cache_get(searchp), 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722
Christoph Lameter35386e32006-03-22 00:09:05 -08003723 /*
3724 * These are racy checks but it does not matter
3725 * if we skip one check or scan twice.
3726 */
Christoph Lametere498be72005-09-09 13:03:32 -07003727 if (time_after(l3->next_reap, jiffies))
Christoph Lameter35386e32006-03-22 00:09:05 -08003728 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729
Christoph Lametere498be72005-09-09 13:03:32 -07003730 l3->next_reap = jiffies + REAPTIMEOUT_LIST3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731
Christoph Lameteraab22072006-03-22 00:09:06 -08003732 drain_array(searchp, l3, l3->shared, 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733
Christoph Lametered11d9e2006-06-30 01:55:45 -07003734 if (l3->free_touched)
Christoph Lametere498be72005-09-09 13:03:32 -07003735 l3->free_touched = 0;
Christoph Lametered11d9e2006-06-30 01:55:45 -07003736 else {
3737 int freed;
3738
3739 freed = drain_freelist(searchp, l3, (l3->free_limit +
3740 5 * searchp->num - 1) / (5 * searchp->num));
3741 STATS_ADD_REAPED(searchp, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 }
Christoph Lameter35386e32006-03-22 00:09:05 -08003743next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 cond_resched();
3745 }
3746 check_irq_on();
Ingo Molnarfc0abb12006-01-18 17:42:33 -08003747 mutex_unlock(&cache_chain_mutex);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08003748 next_reap_node();
Christoph Lameter2244b952006-06-30 01:55:33 -07003749 refresh_cpu_vm_stats(smp_processor_id());
Andrew Mortona737b3e2006-03-22 00:08:11 -08003750 /* Set up the next iteration */
Manfred Spraulcd61ef62005-11-07 00:58:02 -08003751 schedule_delayed_work(&__get_cpu_var(reap_work), REAPTIMEOUT_CPUC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752}
3753
3754#ifdef CONFIG_PROC_FS
3755
Pekka Enberg85289f92006-01-08 01:00:36 -08003756static void print_slabinfo_header(struct seq_file *m)
3757{
3758 /*
3759 * Output format version, so at least we can change it
3760 * without _too_ many complaints.
3761 */
3762#if STATS
3763 seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
3764#else
3765 seq_puts(m, "slabinfo - version: 2.1\n");
3766#endif
3767 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
3768 "<objperslab> <pagesperslab>");
3769 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
3770 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
3771#if STATS
3772 seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07003773 "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
Pekka Enberg85289f92006-01-08 01:00:36 -08003774 seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
3775#endif
3776 seq_putc(m, '\n');
3777}
3778
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779static void *s_start(struct seq_file *m, loff_t *pos)
3780{
3781 loff_t n = *pos;
3782 struct list_head *p;
3783
Ingo Molnarfc0abb12006-01-18 17:42:33 -08003784 mutex_lock(&cache_chain_mutex);
Pekka Enberg85289f92006-01-08 01:00:36 -08003785 if (!n)
3786 print_slabinfo_header(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003787 p = cache_chain.next;
3788 while (n--) {
3789 p = p->next;
3790 if (p == &cache_chain)
3791 return NULL;
3792 }
Pekka Enberg343e0d72006-02-01 03:05:50 -08003793 return list_entry(p, struct kmem_cache, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794}
3795
3796static void *s_next(struct seq_file *m, void *p, loff_t *pos)
3797{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003798 struct kmem_cache *cachep = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 ++*pos;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003800 return cachep->next.next == &cache_chain ?
3801 NULL : list_entry(cachep->next.next, struct kmem_cache, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802}
3803
3804static void s_stop(struct seq_file *m, void *p)
3805{
Ingo Molnarfc0abb12006-01-18 17:42:33 -08003806 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807}
3808
3809static int s_show(struct seq_file *m, void *p)
3810{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003811 struct kmem_cache *cachep = p;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003812 struct slab *slabp;
3813 unsigned long active_objs;
3814 unsigned long num_objs;
3815 unsigned long active_slabs = 0;
3816 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07003817 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 char *error = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003819 int node;
3820 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 active_objs = 0;
3823 num_slabs = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07003824 for_each_online_node(node) {
3825 l3 = cachep->nodelists[node];
3826 if (!l3)
3827 continue;
3828
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003829 check_irq_on();
3830 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07003831
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07003832 list_for_each_entry(slabp, &l3->slabs_full, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07003833 if (slabp->inuse != cachep->num && !error)
3834 error = "slabs_full accounting error";
3835 active_objs += cachep->num;
3836 active_slabs++;
3837 }
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07003838 list_for_each_entry(slabp, &l3->slabs_partial, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07003839 if (slabp->inuse == cachep->num && !error)
3840 error = "slabs_partial inuse accounting error";
3841 if (!slabp->inuse && !error)
3842 error = "slabs_partial/inuse accounting error";
3843 active_objs += slabp->inuse;
3844 active_slabs++;
3845 }
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07003846 list_for_each_entry(slabp, &l3->slabs_free, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07003847 if (slabp->inuse && !error)
3848 error = "slabs_free/inuse accounting error";
3849 num_slabs++;
3850 }
3851 free_objects += l3->free_objects;
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08003852 if (l3->shared)
3853 shared_avail += l3->shared->avail;
Christoph Lametere498be72005-09-09 13:03:32 -07003854
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003855 spin_unlock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003857 num_slabs += active_slabs;
3858 num_objs = num_slabs * cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07003859 if (num_objs - active_objs != free_objects && !error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003860 error = "free_objects accounting error";
3861
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003862 name = cachep->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 if (error)
3864 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
3865
3866 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003867 name, active_objs, num_objs, cachep->buffer_size,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003868 cachep->num, (1 << cachep->gfporder));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 seq_printf(m, " : tunables %4u %4u %4u",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003870 cachep->limit, cachep->batchcount, cachep->shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003871 seq_printf(m, " : slabdata %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003872 active_slabs, num_slabs, shared_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873#if STATS
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003874 { /* list3 stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875 unsigned long high = cachep->high_mark;
3876 unsigned long allocs = cachep->num_allocations;
3877 unsigned long grown = cachep->grown;
3878 unsigned long reaped = cachep->reaped;
3879 unsigned long errors = cachep->errors;
3880 unsigned long max_freeable = cachep->max_freeable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 unsigned long node_allocs = cachep->node_allocs;
Christoph Lametere498be72005-09-09 13:03:32 -07003882 unsigned long node_frees = cachep->node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07003883 unsigned long overflows = cachep->node_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884
Christoph Lametere498be72005-09-09 13:03:32 -07003885 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu \
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07003886 %4lu %4lu %4lu %4lu %4lu", allocs, high, grown,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003887 reaped, errors, max_freeable, node_allocs,
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07003888 node_frees, overflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889 }
3890 /* cpu stats */
3891 {
3892 unsigned long allochit = atomic_read(&cachep->allochit);
3893 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
3894 unsigned long freehit = atomic_read(&cachep->freehit);
3895 unsigned long freemiss = atomic_read(&cachep->freemiss);
3896
3897 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003898 allochit, allocmiss, freehit, freemiss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 }
3900#endif
3901 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 return 0;
3903}
3904
3905/*
3906 * slabinfo_op - iterator that generates /proc/slabinfo
3907 *
3908 * Output layout:
3909 * cache-name
3910 * num-active-objs
3911 * total-objs
3912 * object size
3913 * num-active-slabs
3914 * total-slabs
3915 * num-pages-per-slab
3916 * + further values on SMP and with statistics enabled
3917 */
3918
3919struct seq_operations slabinfo_op = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003920 .start = s_start,
3921 .next = s_next,
3922 .stop = s_stop,
3923 .show = s_show,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924};
3925
3926#define MAX_SLABINFO_WRITE 128
3927/**
3928 * slabinfo_write - Tuning for the slab allocator
3929 * @file: unused
3930 * @buffer: user buffer
3931 * @count: data length
3932 * @ppos: unused
3933 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003934ssize_t slabinfo_write(struct file *file, const char __user * buffer,
3935 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003937 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 int limit, batchcount, shared, res;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07003939 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003940
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 if (count > MAX_SLABINFO_WRITE)
3942 return -EINVAL;
3943 if (copy_from_user(&kbuf, buffer, count))
3944 return -EFAULT;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003945 kbuf[MAX_SLABINFO_WRITE] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946
3947 tmp = strchr(kbuf, ' ');
3948 if (!tmp)
3949 return -EINVAL;
3950 *tmp = '\0';
3951 tmp++;
3952 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
3953 return -EINVAL;
3954
3955 /* Find the cache in the chain of caches. */
Ingo Molnarfc0abb12006-01-18 17:42:33 -08003956 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957 res = -EINVAL;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07003958 list_for_each_entry(cachep, &cache_chain, next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 if (!strcmp(cachep->name, kbuf)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003960 if (limit < 1 || batchcount < 1 ||
3961 batchcount > limit || shared < 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07003962 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003963 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07003964 res = do_tune_cpucache(cachep, limit,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003965 batchcount, shared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966 }
3967 break;
3968 }
3969 }
Ingo Molnarfc0abb12006-01-18 17:42:33 -08003970 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971 if (res >= 0)
3972 res = count;
3973 return res;
3974}
Al Viro871751e2006-03-25 03:06:39 -08003975
3976#ifdef CONFIG_DEBUG_SLAB_LEAK
3977
3978static void *leaks_start(struct seq_file *m, loff_t *pos)
3979{
3980 loff_t n = *pos;
3981 struct list_head *p;
3982
3983 mutex_lock(&cache_chain_mutex);
3984 p = cache_chain.next;
3985 while (n--) {
3986 p = p->next;
3987 if (p == &cache_chain)
3988 return NULL;
3989 }
3990 return list_entry(p, struct kmem_cache, next);
3991}
3992
3993static inline int add_caller(unsigned long *n, unsigned long v)
3994{
3995 unsigned long *p;
3996 int l;
3997 if (!v)
3998 return 1;
3999 l = n[1];
4000 p = n + 2;
4001 while (l) {
4002 int i = l/2;
4003 unsigned long *q = p + 2 * i;
4004 if (*q == v) {
4005 q[1]++;
4006 return 1;
4007 }
4008 if (*q > v) {
4009 l = i;
4010 } else {
4011 p = q + 2;
4012 l -= i + 1;
4013 }
4014 }
4015 if (++n[1] == n[0])
4016 return 0;
4017 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4018 p[0] = v;
4019 p[1] = 1;
4020 return 1;
4021}
4022
4023static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
4024{
4025 void *p;
4026 int i;
4027 if (n[0] == n[1])
4028 return;
4029 for (i = 0, p = s->s_mem; i < c->num; i++, p += c->buffer_size) {
4030 if (slab_bufctl(s)[i] != BUFCTL_ACTIVE)
4031 continue;
4032 if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
4033 return;
4034 }
4035}
4036
4037static void show_symbol(struct seq_file *m, unsigned long address)
4038{
4039#ifdef CONFIG_KALLSYMS
4040 char *modname;
4041 const char *name;
4042 unsigned long offset, size;
4043 char namebuf[KSYM_NAME_LEN+1];
4044
4045 name = kallsyms_lookup(address, &size, &offset, &modname, namebuf);
4046
4047 if (name) {
4048 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
4049 if (modname)
4050 seq_printf(m, " [%s]", modname);
4051 return;
4052 }
4053#endif
4054 seq_printf(m, "%p", (void *)address);
4055}
4056
4057static int leaks_show(struct seq_file *m, void *p)
4058{
4059 struct kmem_cache *cachep = p;
Al Viro871751e2006-03-25 03:06:39 -08004060 struct slab *slabp;
4061 struct kmem_list3 *l3;
4062 const char *name;
4063 unsigned long *n = m->private;
4064 int node;
4065 int i;
4066
4067 if (!(cachep->flags & SLAB_STORE_USER))
4068 return 0;
4069 if (!(cachep->flags & SLAB_RED_ZONE))
4070 return 0;
4071
4072 /* OK, we can do it */
4073
4074 n[1] = 0;
4075
4076 for_each_online_node(node) {
4077 l3 = cachep->nodelists[node];
4078 if (!l3)
4079 continue;
4080
4081 check_irq_on();
4082 spin_lock_irq(&l3->list_lock);
4083
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004084 list_for_each_entry(slabp, &l3->slabs_full, list)
Al Viro871751e2006-03-25 03:06:39 -08004085 handle_slab(n, cachep, slabp);
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004086 list_for_each_entry(slabp, &l3->slabs_partial, list)
Al Viro871751e2006-03-25 03:06:39 -08004087 handle_slab(n, cachep, slabp);
Al Viro871751e2006-03-25 03:06:39 -08004088 spin_unlock_irq(&l3->list_lock);
4089 }
4090 name = cachep->name;
4091 if (n[0] == n[1]) {
4092 /* Increase the buffer size */
4093 mutex_unlock(&cache_chain_mutex);
4094 m->private = kzalloc(n[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
4095 if (!m->private) {
4096 /* Too bad, we are really out */
4097 m->private = n;
4098 mutex_lock(&cache_chain_mutex);
4099 return -ENOMEM;
4100 }
4101 *(unsigned long *)m->private = n[0] * 2;
4102 kfree(n);
4103 mutex_lock(&cache_chain_mutex);
4104 /* Now make sure this entry will be retried */
4105 m->count = m->size;
4106 return 0;
4107 }
4108 for (i = 0; i < n[1]; i++) {
4109 seq_printf(m, "%s: %lu ", name, n[2*i+3]);
4110 show_symbol(m, n[2*i+2]);
4111 seq_putc(m, '\n');
4112 }
4113 return 0;
4114}
4115
4116struct seq_operations slabstats_op = {
4117 .start = leaks_start,
4118 .next = s_next,
4119 .stop = s_stop,
4120 .show = leaks_show,
4121};
4122#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123#endif
4124
Manfred Spraul00e145b2005-09-03 15:55:07 -07004125/**
4126 * ksize - get the actual amount of memory allocated for a given object
4127 * @objp: Pointer to the object
4128 *
4129 * kmalloc may internally round up allocations and return more memory
4130 * than requested. ksize() can be used to determine the actual amount of
4131 * memory allocated. The caller may use this additional memory, even though
4132 * a smaller amount of memory was initially specified with the kmalloc call.
4133 * The caller must guarantee that objp points to a valid object previously
4134 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4135 * must not be freed during the duration of the call.
4136 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137unsigned int ksize(const void *objp)
4138{
Manfred Spraul00e145b2005-09-03 15:55:07 -07004139 if (unlikely(objp == NULL))
4140 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141
Pekka Enberg6ed5eb22006-02-01 03:05:49 -08004142 return obj_size(virt_to_cache(objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143}