blob: 85c2e03098a7fcf60f9c2f250b0af5a72a0330cf [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
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001024static inline int cache_free_alien(struct kmem_cache *cachep, void *objp,
1025 int nesting)
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001026{
1027 struct slab *slabp = virt_to_slab(objp);
1028 int nodeid = slabp->nodeid;
1029 struct kmem_list3 *l3;
1030 struct array_cache *alien = NULL;
1031
1032 /*
1033 * Make sure we are not freeing a object from another node to the array
1034 * cache on this cpu.
1035 */
1036 if (likely(slabp->nodeid == numa_node_id()))
1037 return 0;
1038
1039 l3 = cachep->nodelists[numa_node_id()];
1040 STATS_INC_NODEFREES(cachep);
1041 if (l3->alien && l3->alien[nodeid]) {
1042 alien = l3->alien[nodeid];
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001043 spin_lock_nested(&alien->lock, nesting);
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001044 if (unlikely(alien->avail == alien->limit)) {
1045 STATS_INC_ACOVERFLOW(cachep);
1046 __drain_alien_cache(cachep, alien, nodeid);
1047 }
1048 alien->entry[alien->avail++] = objp;
1049 spin_unlock(&alien->lock);
1050 } else {
1051 spin_lock(&(cachep->nodelists[nodeid])->list_lock);
1052 free_block(cachep, &objp, 1, nodeid);
1053 spin_unlock(&(cachep->nodelists[nodeid])->list_lock);
1054 }
1055 return 1;
1056}
1057
Christoph Lametere498be72005-09-09 13:03:32 -07001058#else
Linus Torvalds7a21ef62006-02-05 11:26:38 -08001059
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001060#define drain_alien_cache(cachep, alien) do { } while (0)
Christoph Lameter8fce4d82006-03-09 17:33:54 -08001061#define reap_alien(cachep, l3) do { } while (0)
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001062
Linus Torvalds7a21ef62006-02-05 11:26:38 -08001063static inline struct array_cache **alloc_alien_cache(int node, int limit)
1064{
1065 return (struct array_cache **) 0x01020304ul;
1066}
1067
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001068static inline void free_alien_cache(struct array_cache **ac_ptr)
1069{
1070}
Linus Torvalds7a21ef62006-02-05 11:26:38 -08001071
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001072static inline int cache_free_alien(struct kmem_cache *cachep, void *objp,
1073 int nesting)
Pekka Enberg729bd0b2006-06-23 02:03:05 -07001074{
1075 return 0;
1076}
1077
Christoph Lametere498be72005-09-09 13:03:32 -07001078#endif
1079
Chandra Seetharaman9c7b2162006-06-27 02:54:07 -07001080static int __devinit cpuup_callback(struct notifier_block *nfb,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001081 unsigned long action, void *hcpu)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082{
1083 long cpu = (long)hcpu;
Pekka Enberg343e0d72006-02-01 03:05:50 -08001084 struct kmem_cache *cachep;
Christoph Lametere498be72005-09-09 13:03:32 -07001085 struct kmem_list3 *l3 = NULL;
1086 int node = cpu_to_node(cpu);
1087 int memsize = sizeof(struct kmem_list3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 switch (action) {
1090 case CPU_UP_PREPARE:
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001091 mutex_lock(&cache_chain_mutex);
Andrew Mortona737b3e2006-03-22 00:08:11 -08001092 /*
1093 * We need to do this right in the beginning since
Christoph Lametere498be72005-09-09 13:03:32 -07001094 * alloc_arraycache's are going to use this list.
1095 * kmalloc_node allows us to add the slab to the right
1096 * kmem_list3 and not this cpu's kmem_list3
1097 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
Christoph Lametere498be72005-09-09 13:03:32 -07001099 list_for_each_entry(cachep, &cache_chain, next) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08001100 /*
1101 * Set up the size64 kmemlist for cpu before we can
Christoph Lametere498be72005-09-09 13:03:32 -07001102 * begin anything. Make sure some other cpu on this
1103 * node has not already allocated this
1104 */
1105 if (!cachep->nodelists[node]) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08001106 l3 = kmalloc_node(memsize, GFP_KERNEL, node);
1107 if (!l3)
Christoph Lametere498be72005-09-09 13:03:32 -07001108 goto bad;
1109 kmem_list3_init(l3);
1110 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001111 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lametere498be72005-09-09 13:03:32 -07001112
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001113 /*
1114 * The l3s don't come and go as CPUs come and
1115 * go. cache_chain_mutex is sufficient
1116 * protection here.
1117 */
Christoph Lametere498be72005-09-09 13:03:32 -07001118 cachep->nodelists[node] = l3;
1119 }
1120
1121 spin_lock_irq(&cachep->nodelists[node]->list_lock);
1122 cachep->nodelists[node]->free_limit =
Andrew Mortona737b3e2006-03-22 00:08:11 -08001123 (1 + nr_cpus_node(node)) *
1124 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07001125 spin_unlock_irq(&cachep->nodelists[node]->list_lock);
1126 }
1127
Andrew Mortona737b3e2006-03-22 00:08:11 -08001128 /*
1129 * Now we can go ahead with allocating the shared arrays and
1130 * array caches
1131 */
Christoph Lametere498be72005-09-09 13:03:32 -07001132 list_for_each_entry(cachep, &cache_chain, next) {
Tobias Klausercd105df2006-01-08 01:00:59 -08001133 struct array_cache *nc;
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001134 struct array_cache *shared;
1135 struct array_cache **alien;
Tobias Klausercd105df2006-01-08 01:00:59 -08001136
Christoph Lametere498be72005-09-09 13:03:32 -07001137 nc = alloc_arraycache(node, cachep->limit,
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001138 cachep->batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (!nc)
1140 goto bad;
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001141 shared = alloc_arraycache(node,
1142 cachep->shared * cachep->batchcount,
1143 0xbaadf00d);
1144 if (!shared)
1145 goto bad;
Linus Torvalds7a21ef62006-02-05 11:26:38 -08001146
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001147 alien = alloc_alien_cache(node, cachep->limit);
1148 if (!alien)
1149 goto bad;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 cachep->array[cpu] = nc;
Christoph Lametere498be72005-09-09 13:03:32 -07001151 l3 = cachep->nodelists[node];
1152 BUG_ON(!l3);
Christoph Lametere498be72005-09-09 13:03:32 -07001153
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001154 spin_lock_irq(&l3->list_lock);
1155 if (!l3->shared) {
1156 /*
1157 * We are serialised from CPU_DEAD or
1158 * CPU_UP_CANCELLED by the cpucontrol lock
1159 */
1160 l3->shared = shared;
1161 shared = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07001162 }
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001163#ifdef CONFIG_NUMA
1164 if (!l3->alien) {
1165 l3->alien = alien;
1166 alien = NULL;
1167 }
1168#endif
1169 spin_unlock_irq(&l3->list_lock);
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001170 kfree(shared);
1171 free_alien_cache(alien);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 }
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001173 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 break;
1175 case CPU_ONLINE:
1176 start_cpu_timer(cpu);
1177 break;
1178#ifdef CONFIG_HOTPLUG_CPU
1179 case CPU_DEAD:
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001180 /*
1181 * Even if all the cpus of a node are down, we don't free the
1182 * kmem_list3 of any cache. This to avoid a race between
1183 * cpu_down, and a kmalloc allocation from another cpu for
1184 * memory from the node of the cpu going down. The list3
1185 * structure is usually allocated from kmem_cache_create() and
1186 * gets destroyed at kmem_cache_destroy().
1187 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 /* fall thru */
1189 case CPU_UP_CANCELED:
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001190 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 list_for_each_entry(cachep, &cache_chain, next) {
1192 struct array_cache *nc;
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001193 struct array_cache *shared;
1194 struct array_cache **alien;
Christoph Lametere498be72005-09-09 13:03:32 -07001195 cpumask_t mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Christoph Lametere498be72005-09-09 13:03:32 -07001197 mask = node_to_cpumask(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 /* cpu is dead; no one can alloc from it. */
1199 nc = cachep->array[cpu];
1200 cachep->array[cpu] = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07001201 l3 = cachep->nodelists[node];
1202
1203 if (!l3)
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001204 goto free_array_cache;
Christoph Lametere498be72005-09-09 13:03:32 -07001205
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08001206 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07001207
1208 /* Free limit for this kmem_list3 */
1209 l3->free_limit -= cachep->batchcount;
1210 if (nc)
Christoph Lameterff694162005-09-22 21:44:02 -07001211 free_block(cachep, nc->entry, nc->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001212
1213 if (!cpus_empty(mask)) {
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08001214 spin_unlock_irq(&l3->list_lock);
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001215 goto free_array_cache;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001216 }
Christoph Lametere498be72005-09-09 13:03:32 -07001217
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001218 shared = l3->shared;
1219 if (shared) {
Christoph Lametere498be72005-09-09 13:03:32 -07001220 free_block(cachep, l3->shared->entry,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001221 l3->shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07001222 l3->shared = NULL;
1223 }
Christoph Lametere498be72005-09-09 13:03:32 -07001224
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001225 alien = l3->alien;
1226 l3->alien = NULL;
1227
1228 spin_unlock_irq(&l3->list_lock);
1229
1230 kfree(shared);
1231 if (alien) {
1232 drain_alien_cache(cachep, alien);
1233 free_alien_cache(alien);
Christoph Lametere498be72005-09-09 13:03:32 -07001234 }
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001235free_array_cache:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 kfree(nc);
1237 }
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001238 /*
1239 * In the previous loop, all the objects were freed to
1240 * the respective cache's slabs, now we can go ahead and
1241 * shrink each nodelist to its limit.
1242 */
1243 list_for_each_entry(cachep, &cache_chain, next) {
1244 l3 = cachep->nodelists[node];
1245 if (!l3)
1246 continue;
Christoph Lametered11d9e2006-06-30 01:55:45 -07001247 drain_freelist(cachep, l3, l3->free_objects);
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08001248 }
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001249 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 break;
1251#endif
1252 }
1253 return NOTIFY_OK;
Andrew Mortona737b3e2006-03-22 00:08:11 -08001254bad:
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001255 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return NOTIFY_BAD;
1257}
1258
Chandra Seetharaman74b85f32006-06-27 02:54:09 -07001259static struct notifier_block __cpuinitdata cpucache_notifier = {
1260 &cpuup_callback, NULL, 0
1261};
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Christoph Lametere498be72005-09-09 13:03:32 -07001263/*
1264 * swap the static kmem_list3 with kmalloced memory
1265 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001266static void init_list(struct kmem_cache *cachep, struct kmem_list3 *list,
1267 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07001268{
1269 struct kmem_list3 *ptr;
1270
1271 BUG_ON(cachep->nodelists[nodeid] != list);
1272 ptr = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, nodeid);
1273 BUG_ON(!ptr);
1274
1275 local_irq_disable();
1276 memcpy(ptr, list, sizeof(struct kmem_list3));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001277 /*
1278 * Do not assume that spinlocks can be initialized via memcpy:
1279 */
1280 spin_lock_init(&ptr->list_lock);
1281
Christoph Lametere498be72005-09-09 13:03:32 -07001282 MAKE_ALL_LISTS(cachep, ptr, nodeid);
1283 cachep->nodelists[nodeid] = ptr;
1284 local_irq_enable();
1285}
1286
Andrew Mortona737b3e2006-03-22 00:08:11 -08001287/*
1288 * Initialisation. Called after the page allocator have been initialised and
1289 * before smp_init().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 */
1291void __init kmem_cache_init(void)
1292{
1293 size_t left_over;
1294 struct cache_sizes *sizes;
1295 struct cache_names *names;
Christoph Lametere498be72005-09-09 13:03:32 -07001296 int i;
Jack Steiner07ed76b2006-03-07 21:55:46 -08001297 int order;
Christoph Lametere498be72005-09-09 13:03:32 -07001298
1299 for (i = 0; i < NUM_INIT_LISTS; i++) {
1300 kmem_list3_init(&initkmem_list3[i]);
1301 if (i < MAX_NUMNODES)
1302 cache_cache.nodelists[i] = NULL;
1303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 /*
1306 * Fragmentation resistance on low memory - only use bigger
1307 * page orders on machines with more than 32MB of memory.
1308 */
1309 if (num_physpages > (32 << 20) >> PAGE_SHIFT)
1310 slab_break_gfp_order = BREAK_GFP_ORDER_HI;
1311
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 /* Bootstrap is tricky, because several objects are allocated
1313 * from caches that do not exist yet:
Andrew Mortona737b3e2006-03-22 00:08:11 -08001314 * 1) initialize the cache_cache cache: it contains the struct
1315 * kmem_cache structures of all caches, except cache_cache itself:
1316 * cache_cache is statically allocated.
Christoph Lametere498be72005-09-09 13:03:32 -07001317 * Initially an __init data area is used for the head array and the
1318 * kmem_list3 structures, it's replaced with a kmalloc allocated
1319 * array at the end of the bootstrap.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 * 2) Create the first kmalloc cache.
Pekka Enberg343e0d72006-02-01 03:05:50 -08001321 * The struct kmem_cache for the new cache is allocated normally.
Christoph Lametere498be72005-09-09 13:03:32 -07001322 * An __init data area is used for the head array.
1323 * 3) Create the remaining kmalloc caches, with minimally sized
1324 * head arrays.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 * 4) Replace the __init data head arrays for cache_cache and the first
1326 * kmalloc cache with kmalloc allocated arrays.
Christoph Lametere498be72005-09-09 13:03:32 -07001327 * 5) Replace the __init data for kmem_list3 for cache_cache and
1328 * the other cache's with kmalloc allocated memory.
1329 * 6) Resize the head arrays of the kmalloc caches to their final sizes.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 */
1331
1332 /* 1) create the cache_cache */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 INIT_LIST_HEAD(&cache_chain);
1334 list_add(&cache_cache.next, &cache_chain);
1335 cache_cache.colour_off = cache_line_size();
1336 cache_cache.array[smp_processor_id()] = &initarray_cache.cache;
Christoph Lametere498be72005-09-09 13:03:32 -07001337 cache_cache.nodelists[numa_node_id()] = &initkmem_list3[CACHE_CACHE];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338
Andrew Mortona737b3e2006-03-22 00:08:11 -08001339 cache_cache.buffer_size = ALIGN(cache_cache.buffer_size,
1340 cache_line_size());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341
Jack Steiner07ed76b2006-03-07 21:55:46 -08001342 for (order = 0; order < MAX_ORDER; order++) {
1343 cache_estimate(order, cache_cache.buffer_size,
1344 cache_line_size(), 0, &left_over, &cache_cache.num);
1345 if (cache_cache.num)
1346 break;
1347 }
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02001348 BUG_ON(!cache_cache.num);
Jack Steiner07ed76b2006-03-07 21:55:46 -08001349 cache_cache.gfporder = order;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001350 cache_cache.colour = left_over / cache_cache.colour_off;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001351 cache_cache.slab_size = ALIGN(cache_cache.num * sizeof(kmem_bufctl_t) +
1352 sizeof(struct slab), cache_line_size());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353
1354 /* 2+3) create the kmalloc caches */
1355 sizes = malloc_sizes;
1356 names = cache_names;
1357
Andrew Mortona737b3e2006-03-22 00:08:11 -08001358 /*
1359 * Initialize the caches that provide memory for the array cache and the
1360 * kmem_list3 structures first. Without this, further allocations will
1361 * bug.
Christoph Lametere498be72005-09-09 13:03:32 -07001362 */
1363
1364 sizes[INDEX_AC].cs_cachep = kmem_cache_create(names[INDEX_AC].name,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001365 sizes[INDEX_AC].cs_size,
1366 ARCH_KMALLOC_MINALIGN,
1367 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
1368 NULL, NULL);
Christoph Lametere498be72005-09-09 13:03:32 -07001369
Andrew Mortona737b3e2006-03-22 00:08:11 -08001370 if (INDEX_AC != INDEX_L3) {
Christoph Lametere498be72005-09-09 13:03:32 -07001371 sizes[INDEX_L3].cs_cachep =
Andrew Mortona737b3e2006-03-22 00:08:11 -08001372 kmem_cache_create(names[INDEX_L3].name,
1373 sizes[INDEX_L3].cs_size,
1374 ARCH_KMALLOC_MINALIGN,
1375 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
1376 NULL, NULL);
1377 }
Christoph Lametere498be72005-09-09 13:03:32 -07001378
Ingo Molnare0a42722006-06-23 02:03:46 -07001379 slab_early_init = 0;
1380
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 while (sizes->cs_size != ULONG_MAX) {
Christoph Lametere498be72005-09-09 13:03:32 -07001382 /*
1383 * For performance, all the general caches are L1 aligned.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384 * This should be particularly beneficial on SMP boxes, as it
1385 * eliminates "false sharing".
1386 * Note for systems short on memory removing the alignment will
Christoph Lametere498be72005-09-09 13:03:32 -07001387 * allow tighter packing of the smaller caches.
1388 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001389 if (!sizes->cs_cachep) {
Christoph Lametere498be72005-09-09 13:03:32 -07001390 sizes->cs_cachep = kmem_cache_create(names->name,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001391 sizes->cs_size,
1392 ARCH_KMALLOC_MINALIGN,
1393 ARCH_KMALLOC_FLAGS|SLAB_PANIC,
1394 NULL, NULL);
1395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 sizes->cs_dmacachep = kmem_cache_create(names->name_dma,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001398 sizes->cs_size,
1399 ARCH_KMALLOC_MINALIGN,
1400 ARCH_KMALLOC_FLAGS|SLAB_CACHE_DMA|
1401 SLAB_PANIC,
1402 NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 sizes++;
1404 names++;
1405 }
1406 /* 4) Replace the bootstrap head arrays */
1407 {
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001408 struct array_cache *ptr;
Christoph Lametere498be72005-09-09 13:03:32 -07001409
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
Christoph Lametere498be72005-09-09 13:03:32 -07001411
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 local_irq_disable();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001413 BUG_ON(cpu_cache_get(&cache_cache) != &initarray_cache.cache);
1414 memcpy(ptr, cpu_cache_get(&cache_cache),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001415 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001416 /*
1417 * Do not assume that spinlocks can be initialized via memcpy:
1418 */
1419 spin_lock_init(&ptr->lock);
1420
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 cache_cache.array[smp_processor_id()] = ptr;
1422 local_irq_enable();
Christoph Lametere498be72005-09-09 13:03:32 -07001423
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
Christoph Lametere498be72005-09-09 13:03:32 -07001425
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 local_irq_disable();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001427 BUG_ON(cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001428 != &initarray_generic.cache);
Pekka Enberg9a2dba42006-02-01 03:05:49 -08001429 memcpy(ptr, cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001430 sizeof(struct arraycache_init));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001431 /*
1432 * Do not assume that spinlocks can be initialized via memcpy:
1433 */
1434 spin_lock_init(&ptr->lock);
1435
Christoph Lametere498be72005-09-09 13:03:32 -07001436 malloc_sizes[INDEX_AC].cs_cachep->array[smp_processor_id()] =
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001437 ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 local_irq_enable();
1439 }
Christoph Lametere498be72005-09-09 13:03:32 -07001440 /* 5) Replace the bootstrap kmem_list3's */
1441 {
1442 int node;
1443 /* Replace the static kmem_list3 structures for the boot cpu */
1444 init_list(&cache_cache, &initkmem_list3[CACHE_CACHE],
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001445 numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446
Christoph Lametere498be72005-09-09 13:03:32 -07001447 for_each_online_node(node) {
1448 init_list(malloc_sizes[INDEX_AC].cs_cachep,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001449 &initkmem_list3[SIZE_AC + node], node);
Christoph Lametere498be72005-09-09 13:03:32 -07001450
1451 if (INDEX_AC != INDEX_L3) {
1452 init_list(malloc_sizes[INDEX_L3].cs_cachep,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001453 &initkmem_list3[SIZE_L3 + node],
1454 node);
Christoph Lametere498be72005-09-09 13:03:32 -07001455 }
1456 }
1457 }
1458
1459 /* 6) resize the head arrays to their final sizes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 {
Pekka Enberg343e0d72006-02-01 03:05:50 -08001461 struct kmem_cache *cachep;
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001462 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 list_for_each_entry(cachep, &cache_chain, next)
Andrew Mortona737b3e2006-03-22 00:08:11 -08001464 enable_cpucache(cachep);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001465 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467
1468 /* Done! */
1469 g_cpucache_up = FULL;
1470
Andrew Mortona737b3e2006-03-22 00:08:11 -08001471 /*
1472 * Register a cpu startup notifier callback that initializes
1473 * cpu_cache_get for all new cpus
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 */
1475 register_cpu_notifier(&cpucache_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476
Andrew Mortona737b3e2006-03-22 00:08:11 -08001477 /*
1478 * The reap timers are started later, with a module init call: That part
1479 * of the kernel is not yet operational.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 */
1481}
1482
1483static int __init cpucache_init(void)
1484{
1485 int cpu;
1486
Andrew Mortona737b3e2006-03-22 00:08:11 -08001487 /*
1488 * Register the timers that return unneeded pages to the page allocator
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 */
Christoph Lametere498be72005-09-09 13:03:32 -07001490 for_each_online_cpu(cpu)
Andrew Mortona737b3e2006-03-22 00:08:11 -08001491 start_cpu_timer(cpu);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return 0;
1493}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494__initcall(cpucache_init);
1495
1496/*
1497 * Interface to system's page allocator. No need to hold the cache-lock.
1498 *
1499 * If we requested dmaable memory, we will get it. Even if we
1500 * did not request dmaable memory, we might get it, but that
1501 * would be relatively rare and ignorable.
1502 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001503static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504{
1505 struct page *page;
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001506 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 int i;
1508
Luke Yangd6fef9d2006-04-10 22:52:56 -07001509#ifndef CONFIG_MMU
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001510 /*
1511 * Nommu uses slab's for process anonymous memory allocations, and thus
1512 * requires __GFP_COMP to properly refcount higher order allocations
Luke Yangd6fef9d2006-04-10 22:52:56 -07001513 */
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001514 flags |= __GFP_COMP;
Luke Yangd6fef9d2006-04-10 22:52:56 -07001515#endif
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001516 flags |= cachep->gfpflags;
1517
1518 page = alloc_pages_node(nodeid, flags, cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 if (!page)
1520 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001522 nr_pages = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001524 atomic_add(nr_pages, &slab_reclaim_pages);
Christoph Lameter9a865ff2006-06-30 01:55:38 -07001525 add_zone_page_state(page_zone(page), NR_SLAB, nr_pages);
Christoph Hellwige1b6aa62006-06-23 02:03:17 -07001526 for (i = 0; i < nr_pages; i++)
1527 __SetPageSlab(page + i);
1528 return page_address(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
1531/*
1532 * Interface to system's page release.
1533 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001534static void kmem_freepages(struct kmem_cache *cachep, void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001536 unsigned long i = (1 << cachep->gfporder);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 struct page *page = virt_to_page(addr);
1538 const unsigned long nr_freed = i;
1539
Christoph Lameter9a865ff2006-06-30 01:55:38 -07001540 sub_zone_page_state(page_zone(page), NR_SLAB, nr_freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 while (i--) {
Nick Pigginf205b2f2006-03-22 00:08:02 -08001542 BUG_ON(!PageSlab(page));
1543 __ClearPageSlab(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 page++;
1545 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 if (current->reclaim_state)
1547 current->reclaim_state->reclaimed_slab += nr_freed;
1548 free_pages((unsigned long)addr, cachep->gfporder);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001549 if (cachep->flags & SLAB_RECLAIM_ACCOUNT)
1550 atomic_sub(1 << cachep->gfporder, &slab_reclaim_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
1553static void kmem_rcu_free(struct rcu_head *head)
1554{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001555 struct slab_rcu *slab_rcu = (struct slab_rcu *)head;
Pekka Enberg343e0d72006-02-01 03:05:50 -08001556 struct kmem_cache *cachep = slab_rcu->cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
1558 kmem_freepages(cachep, slab_rcu->addr);
1559 if (OFF_SLAB(cachep))
1560 kmem_cache_free(cachep->slabp_cache, slab_rcu);
1561}
1562
1563#if DEBUG
1564
1565#ifdef CONFIG_DEBUG_PAGEALLOC
Pekka Enberg343e0d72006-02-01 03:05:50 -08001566static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001567 unsigned long caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001569 int size = obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001571 addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001573 if (size < 5 * sizeof(unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 return;
1575
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001576 *addr++ = 0x12345678;
1577 *addr++ = caller;
1578 *addr++ = smp_processor_id();
1579 size -= 3 * sizeof(unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 {
1581 unsigned long *sptr = &caller;
1582 unsigned long svalue;
1583
1584 while (!kstack_end(sptr)) {
1585 svalue = *sptr++;
1586 if (kernel_text_address(svalue)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001587 *addr++ = svalue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 size -= sizeof(unsigned long);
1589 if (size <= sizeof(unsigned long))
1590 break;
1591 }
1592 }
1593
1594 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001595 *addr++ = 0x87654321;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596}
1597#endif
1598
Pekka Enberg343e0d72006-02-01 03:05:50 -08001599static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001601 int size = obj_size(cachep);
1602 addr = &((char *)addr)[obj_offset(cachep)];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
1604 memset(addr, val, size);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001605 *(unsigned char *)(addr + size - 1) = POISON_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606}
1607
1608static void dump_line(char *data, int offset, int limit)
1609{
1610 int i;
1611 printk(KERN_ERR "%03x:", offset);
Andrew Mortona737b3e2006-03-22 00:08:11 -08001612 for (i = 0; i < limit; i++)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001613 printk(" %02x", (unsigned char)data[offset + i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 printk("\n");
1615}
1616#endif
1617
1618#if DEBUG
1619
Pekka Enberg343e0d72006-02-01 03:05:50 -08001620static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621{
1622 int i, size;
1623 char *realobj;
1624
1625 if (cachep->flags & SLAB_RED_ZONE) {
1626 printk(KERN_ERR "Redzone: 0x%lx/0x%lx.\n",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001627 *dbg_redzone1(cachep, objp),
1628 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
1630
1631 if (cachep->flags & SLAB_STORE_USER) {
1632 printk(KERN_ERR "Last user: [<%p>]",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001633 *dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634 print_symbol("(%s)",
Andrew Mortona737b3e2006-03-22 00:08:11 -08001635 (unsigned long)*dbg_userword(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 printk("\n");
1637 }
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001638 realobj = (char *)objp + obj_offset(cachep);
1639 size = obj_size(cachep);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001640 for (i = 0; i < size && lines; i += 16, lines--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 int limit;
1642 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001643 if (i + limit > size)
1644 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 dump_line(realobj, i, limit);
1646 }
1647}
1648
Pekka Enberg343e0d72006-02-01 03:05:50 -08001649static void check_poison_obj(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
1651 char *realobj;
1652 int size, i;
1653 int lines = 0;
1654
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001655 realobj = (char *)objp + obj_offset(cachep);
1656 size = obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001658 for (i = 0; i < size; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 char exp = POISON_FREE;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001660 if (i == size - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 exp = POISON_END;
1662 if (realobj[i] != exp) {
1663 int limit;
1664 /* Mismatch ! */
1665 /* Print header */
1666 if (lines == 0) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001667 printk(KERN_ERR
Andrew Mortona737b3e2006-03-22 00:08:11 -08001668 "Slab corruption: start=%p, len=%d\n",
1669 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 print_objinfo(cachep, objp, 0);
1671 }
1672 /* Hexdump the affected line */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001673 i = (i / 16) * 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 limit = 16;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001675 if (i + limit > size)
1676 limit = size - i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 dump_line(realobj, i, limit);
1678 i += 16;
1679 lines++;
1680 /* Limit to 5 lines */
1681 if (lines > 5)
1682 break;
1683 }
1684 }
1685 if (lines != 0) {
1686 /* Print some data about the neighboring objects, if they
1687 * exist:
1688 */
Pekka Enberg6ed5eb22006-02-01 03:05:49 -08001689 struct slab *slabp = virt_to_slab(objp);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001690 unsigned int objnr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001692 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 if (objnr) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001694 objp = index_to_obj(cachep, slabp, objnr - 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001695 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 printk(KERN_ERR "Prev obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001697 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 print_objinfo(cachep, objp, 2);
1699 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001700 if (objnr + 1 < cachep->num) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001701 objp = index_to_obj(cachep, slabp, objnr + 1);
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001702 realobj = (char *)objp + obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 printk(KERN_ERR "Next obj: start=%p, len=%d\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001704 realobj, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 print_objinfo(cachep, objp, 2);
1706 }
1707 }
1708}
1709#endif
1710
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711#if DEBUG
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001712/**
Randy Dunlap911851e2006-03-22 00:08:14 -08001713 * slab_destroy_objs - destroy a slab and its objects
1714 * @cachep: cache pointer being destroyed
1715 * @slabp: slab pointer being destroyed
1716 *
1717 * Call the registered destructor for each object in a slab that is being
1718 * destroyed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001719 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001720static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001721{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 int i;
1723 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001724 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725
1726 if (cachep->flags & SLAB_POISON) {
1727#ifdef CONFIG_DEBUG_PAGEALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -08001728 if (cachep->buffer_size % PAGE_SIZE == 0 &&
1729 OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001730 kernel_map_pages(virt_to_page(objp),
Andrew Mortona737b3e2006-03-22 00:08:11 -08001731 cachep->buffer_size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 else
1733 check_poison_obj(cachep, objp);
1734#else
1735 check_poison_obj(cachep, objp);
1736#endif
1737 }
1738 if (cachep->flags & SLAB_RED_ZONE) {
1739 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
1740 slab_error(cachep, "start of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001741 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
1743 slab_error(cachep, "end of a freed object "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001744 "was overwritten");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746 if (cachep->dtor && !(cachep->flags & SLAB_POISON))
Manfred Spraul3dafccf2006-02-01 03:05:42 -08001747 (cachep->dtor) (objp + obj_offset(cachep), cachep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001749}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750#else
Pekka Enberg343e0d72006-02-01 03:05:50 -08001751static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001752{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 if (cachep->dtor) {
1754 int i;
1755 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08001756 void *objp = index_to_obj(cachep, slabp, i);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001757 (cachep->dtor) (objp, cachep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 }
1759 }
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001760}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761#endif
1762
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001763static void __cache_free(struct kmem_cache *cachep, void *objp, int nesting);
1764
Randy Dunlap911851e2006-03-22 00:08:14 -08001765/**
1766 * slab_destroy - destroy and release all objects in a slab
1767 * @cachep: cache pointer being destroyed
1768 * @slabp: slab pointer being destroyed
1769 *
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001770 * Destroy all the objs in a slab, and release the mem back to the system.
Andrew Mortona737b3e2006-03-22 00:08:11 -08001771 * Before calling the slab must have been unlinked from the cache. The
1772 * cache-lock is not held/needed.
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001773 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001774static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp)
Matthew Dobson12dd36f2006-02-01 03:05:46 -08001775{
1776 void *addr = slabp->s_mem - slabp->colouroff;
1777
1778 slab_destroy_objs(cachep, slabp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) {
1780 struct slab_rcu *slab_rcu;
1781
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001782 slab_rcu = (struct slab_rcu *)slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 slab_rcu->cachep = cachep;
1784 slab_rcu->addr = addr;
1785 call_rcu(&slab_rcu->head, kmem_rcu_free);
1786 } else {
1787 kmem_freepages(cachep, addr);
Ingo Molnar2b2d5492006-07-03 00:25:28 -07001788 if (OFF_SLAB(cachep)) {
1789 unsigned long flags;
1790
1791 /*
1792 * lockdep: we may nest inside an already held
1793 * ac->lock, so pass in a nesting flag:
1794 */
1795 local_irq_save(flags);
1796 __cache_free(cachep->slabp_cache, slabp, 1);
1797 local_irq_restore(flags);
1798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
1800}
1801
Andrew Mortona737b3e2006-03-22 00:08:11 -08001802/*
1803 * For setting up all the kmem_list3s for cache whose buffer_size is same as
1804 * size of kmem_list3.
1805 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001806static void set_up_list3s(struct kmem_cache *cachep, int index)
Christoph Lametere498be72005-09-09 13:03:32 -07001807{
1808 int node;
1809
1810 for_each_online_node(node) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001811 cachep->nodelists[node] = &initkmem_list3[index + node];
Christoph Lametere498be72005-09-09 13:03:32 -07001812 cachep->nodelists[node]->next_reap = jiffies +
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001813 REAPTIMEOUT_LIST3 +
1814 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lametere498be72005-09-09 13:03:32 -07001815 }
1816}
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818/**
Randy.Dunlapa70773d2006-02-01 03:05:52 -08001819 * calculate_slab_order - calculate size (page order) of slabs
1820 * @cachep: pointer to the cache that is being created
1821 * @size: size of objects to be created in this cache.
1822 * @align: required alignment for the objects.
1823 * @flags: slab allocation flags
1824 *
1825 * Also calculates the number of objects per slab.
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001826 *
1827 * This could be made much more intelligent. For now, try to avoid using
1828 * high order pages for slabs. When the gfp() functions are more friendly
1829 * towards high-order requests, this should be changed.
1830 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001831static size_t calculate_slab_order(struct kmem_cache *cachep,
Randy Dunlapee13d782006-02-01 03:05:53 -08001832 size_t size, size_t align, unsigned long flags)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001833{
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02001834 unsigned long offslab_limit;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001835 size_t left_over = 0;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001836 int gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001837
Andrew Mortona737b3e2006-03-22 00:08:11 -08001838 for (gfporder = 0; gfporder <= MAX_GFP_ORDER; gfporder++) {
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001839 unsigned int num;
1840 size_t remainder;
1841
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001842 cache_estimate(gfporder, size, align, flags, &remainder, &num);
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001843 if (!num)
1844 continue;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001845
Ingo Molnarb1ab41c2006-06-02 15:44:58 +02001846 if (flags & CFLGS_OFF_SLAB) {
1847 /*
1848 * Max number of objs-per-slab for caches which
1849 * use off-slab slabs. Needed to avoid a possible
1850 * looping condition in cache_grow().
1851 */
1852 offslab_limit = size - sizeof(struct slab);
1853 offslab_limit /= sizeof(kmem_bufctl_t);
1854
1855 if (num > offslab_limit)
1856 break;
1857 }
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001858
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001859 /* Found something acceptable - save it away */
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001860 cachep->num = num;
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001861 cachep->gfporder = gfporder;
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001862 left_over = remainder;
1863
1864 /*
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08001865 * A VFS-reclaimable slab tends to have most allocations
1866 * as GFP_NOFS and we really don't want to have to be allocating
1867 * higher-order pages when we are unable to shrink dcache.
1868 */
1869 if (flags & SLAB_RECLAIM_ACCOUNT)
1870 break;
1871
1872 /*
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001873 * Large number of objects is good, but very large slabs are
1874 * currently bad for the gfp()s.
1875 */
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001876 if (gfporder >= slab_break_gfp_order)
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001877 break;
1878
Linus Torvalds9888e6f2006-03-06 17:44:43 -08001879 /*
1880 * Acceptable internal fragmentation?
1881 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001882 if (left_over * 8 <= (PAGE_SIZE << gfporder))
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001883 break;
1884 }
1885 return left_over;
1886}
1887
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08001888static void setup_cpu_cache(struct kmem_cache *cachep)
1889{
1890 if (g_cpucache_up == FULL) {
1891 enable_cpucache(cachep);
1892 return;
1893 }
1894 if (g_cpucache_up == NONE) {
1895 /*
1896 * Note: the first kmem_cache_create must create the cache
1897 * that's used by kmalloc(24), otherwise the creation of
1898 * further caches will BUG().
1899 */
1900 cachep->array[smp_processor_id()] = &initarray_generic.cache;
1901
1902 /*
1903 * If the cache that's used by kmalloc(sizeof(kmem_list3)) is
1904 * the first cache, then we need to set up all its list3s,
1905 * otherwise the creation of further caches will BUG().
1906 */
1907 set_up_list3s(cachep, SIZE_AC);
1908 if (INDEX_AC == INDEX_L3)
1909 g_cpucache_up = PARTIAL_L3;
1910 else
1911 g_cpucache_up = PARTIAL_AC;
1912 } else {
1913 cachep->array[smp_processor_id()] =
1914 kmalloc(sizeof(struct arraycache_init), GFP_KERNEL);
1915
1916 if (g_cpucache_up == PARTIAL_AC) {
1917 set_up_list3s(cachep, SIZE_L3);
1918 g_cpucache_up = PARTIAL_L3;
1919 } else {
1920 int node;
1921 for_each_online_node(node) {
1922 cachep->nodelists[node] =
1923 kmalloc_node(sizeof(struct kmem_list3),
1924 GFP_KERNEL, node);
1925 BUG_ON(!cachep->nodelists[node]);
1926 kmem_list3_init(cachep->nodelists[node]);
1927 }
1928 }
1929 }
1930 cachep->nodelists[numa_node_id()]->next_reap =
1931 jiffies + REAPTIMEOUT_LIST3 +
1932 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
1933
1934 cpu_cache_get(cachep)->avail = 0;
1935 cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES;
1936 cpu_cache_get(cachep)->batchcount = 1;
1937 cpu_cache_get(cachep)->touched = 0;
1938 cachep->batchcount = 1;
1939 cachep->limit = BOOT_CPUCACHE_ENTRIES;
1940}
1941
Pekka Enberg4d268eb2006-01-08 01:00:36 -08001942/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 * kmem_cache_create - Create a cache.
1944 * @name: A string which is used in /proc/slabinfo to identify this cache.
1945 * @size: The size of objects to be created in this cache.
1946 * @align: The required alignment for the objects.
1947 * @flags: SLAB flags
1948 * @ctor: A constructor for the objects.
1949 * @dtor: A destructor for the objects.
1950 *
1951 * Returns a ptr to the cache on success, NULL on failure.
1952 * Cannot be called within a int, but can be interrupted.
1953 * The @ctor is run when new pages are allocated by the cache
1954 * and the @dtor is run before the pages are handed back.
1955 *
1956 * @name must be valid until the cache is destroyed. This implies that
Andrew Mortona737b3e2006-03-22 00:08:11 -08001957 * the module calling this has to destroy the cache before getting unloaded.
1958 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 * The flags are
1960 *
1961 * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5)
1962 * to catch references to uninitialised memory.
1963 *
1964 * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check
1965 * for buffer overruns.
1966 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware
1968 * cacheline. This can be beneficial if you're counting cycles as closely
1969 * as davem.
1970 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08001971struct kmem_cache *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972kmem_cache_create (const char *name, size_t size, size_t align,
Andrew Mortona737b3e2006-03-22 00:08:11 -08001973 unsigned long flags,
1974 void (*ctor)(void*, struct kmem_cache *, unsigned long),
Pekka Enberg343e0d72006-02-01 03:05:50 -08001975 void (*dtor)(void*, struct kmem_cache *, unsigned long))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976{
1977 size_t left_over, slab_size, ralign;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07001978 struct kmem_cache *cachep = NULL, *pc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
1980 /*
1981 * Sanity checks... these are all serious usage bugs.
1982 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08001983 if (!name || in_interrupt() || (size < BYTES_PER_WORD) ||
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001984 (size > (1 << MAX_OBJ_ORDER) * PAGE_SIZE) || (dtor && !ctor)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08001985 printk(KERN_ERR "%s: Early error in slab %s\n", __FUNCTION__,
1986 name);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08001987 BUG();
1988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Ravikiran G Thirumalaif0188f42006-02-10 01:51:13 -08001990 /*
1991 * Prevent CPUs from coming and going.
1992 * lock_cpu_hotplug() nests outside cache_chain_mutex
1993 */
1994 lock_cpu_hotplug();
1995
Ingo Molnarfc0abb12006-01-18 17:42:33 -08001996 mutex_lock(&cache_chain_mutex);
Andrew Morton4f12bb42005-11-07 00:58:00 -08001997
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07001998 list_for_each_entry(pc, &cache_chain, next) {
Andrew Morton4f12bb42005-11-07 00:58:00 -08001999 mm_segment_t old_fs = get_fs();
2000 char tmp;
2001 int res;
2002
2003 /*
2004 * This happens when the module gets unloaded and doesn't
2005 * destroy its slab cache and no-one else reuses the vmalloc
2006 * area of the module. Print a warning.
2007 */
2008 set_fs(KERNEL_DS);
2009 res = __get_user(tmp, pc->name);
2010 set_fs(old_fs);
2011 if (res) {
2012 printk("SLAB: cache with size %d has lost its name\n",
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002013 pc->buffer_size);
Andrew Morton4f12bb42005-11-07 00:58:00 -08002014 continue;
2015 }
2016
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002017 if (!strcmp(pc->name, name)) {
Andrew Morton4f12bb42005-11-07 00:58:00 -08002018 printk("kmem_cache_create: duplicate cache %s\n", name);
2019 dump_stack();
2020 goto oops;
2021 }
2022 }
2023
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024#if DEBUG
2025 WARN_ON(strchr(name, ' ')); /* It confuses parsers */
2026 if ((flags & SLAB_DEBUG_INITIAL) && !ctor) {
2027 /* No constructor, but inital state check requested */
2028 printk(KERN_ERR "%s: No con, but init state check "
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002029 "requested - %s\n", __FUNCTION__, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 flags &= ~SLAB_DEBUG_INITIAL;
2031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032#if FORCED_DEBUG
2033 /*
2034 * Enable redzoning and last user accounting, except for caches with
2035 * large objects, if the increased size would increase the object size
2036 * above the next power of two: caches with object sizes just above a
2037 * power of two have a significant amount of internal fragmentation.
2038 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002039 if (size < 4096 || fls(size - 1) == fls(size-1 + 3 * BYTES_PER_WORD))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002040 flags |= SLAB_RED_ZONE | SLAB_STORE_USER;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 if (!(flags & SLAB_DESTROY_BY_RCU))
2042 flags |= SLAB_POISON;
2043#endif
2044 if (flags & SLAB_DESTROY_BY_RCU)
2045 BUG_ON(flags & SLAB_POISON);
2046#endif
2047 if (flags & SLAB_DESTROY_BY_RCU)
2048 BUG_ON(dtor);
2049
2050 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002051 * Always checks flags, a caller might be expecting debug support which
2052 * isn't available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 */
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002054 BUG_ON(flags & ~CREATE_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055
Andrew Mortona737b3e2006-03-22 00:08:11 -08002056 /*
2057 * Check that size is in terms of words. This is needed to avoid
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 * unaligned accesses for some archs when redzoning is used, and makes
2059 * sure any on-slab bufctl's are also correctly aligned.
2060 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002061 if (size & (BYTES_PER_WORD - 1)) {
2062 size += (BYTES_PER_WORD - 1);
2063 size &= ~(BYTES_PER_WORD - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 }
2065
Andrew Mortona737b3e2006-03-22 00:08:11 -08002066 /* calculate the final buffer alignment: */
2067
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 /* 1) arch recommendation: can be overridden for debug */
2069 if (flags & SLAB_HWCACHE_ALIGN) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002070 /*
2071 * Default alignment: as specified by the arch code. Except if
2072 * an object is really small, then squeeze multiple objects into
2073 * one cacheline.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074 */
2075 ralign = cache_line_size();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002076 while (size <= ralign / 2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 ralign /= 2;
2078 } else {
2079 ralign = BYTES_PER_WORD;
2080 }
2081 /* 2) arch mandated alignment: disables debug if necessary */
2082 if (ralign < ARCH_SLAB_MINALIGN) {
2083 ralign = ARCH_SLAB_MINALIGN;
2084 if (ralign > BYTES_PER_WORD)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002085 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 }
2087 /* 3) caller mandated alignment: disables debug if necessary */
2088 if (ralign < align) {
2089 ralign = align;
2090 if (ralign > BYTES_PER_WORD)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002091 flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 }
Andrew Mortona737b3e2006-03-22 00:08:11 -08002093 /*
2094 * 4) Store it. Note that the debug code below can reduce
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 * the alignment to BYTES_PER_WORD.
2096 */
2097 align = ralign;
2098
2099 /* Get cache's description obj. */
Pekka Enbergc5e3b832006-03-25 03:06:43 -08002100 cachep = kmem_cache_zalloc(&cache_cache, SLAB_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (!cachep)
Andrew Morton4f12bb42005-11-07 00:58:00 -08002102 goto oops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104#if DEBUG
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002105 cachep->obj_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
2107 if (flags & SLAB_RED_ZONE) {
2108 /* redzoning only works with word aligned caches */
2109 align = BYTES_PER_WORD;
2110
2111 /* add space for red zone words */
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002112 cachep->obj_offset += BYTES_PER_WORD;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002113 size += 2 * BYTES_PER_WORD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 }
2115 if (flags & SLAB_STORE_USER) {
2116 /* user store requires word alignment and
2117 * one word storage behind the end of the real
2118 * object.
2119 */
2120 align = BYTES_PER_WORD;
2121 size += BYTES_PER_WORD;
2122 }
2123#if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002124 if (size >= malloc_sizes[INDEX_L3 + 1].cs_size
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002125 && cachep->obj_size > cache_line_size() && size < PAGE_SIZE) {
2126 cachep->obj_offset += PAGE_SIZE - size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 size = PAGE_SIZE;
2128 }
2129#endif
2130#endif
2131
Ingo Molnare0a42722006-06-23 02:03:46 -07002132 /*
2133 * Determine if the slab management is 'on' or 'off' slab.
2134 * (bootstrapping cannot cope with offslab caches so don't do
2135 * it too early on.)
2136 */
2137 if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 /*
2139 * Size is large, assume best to place the slab management obj
2140 * off-slab (should allow better packing of objs).
2141 */
2142 flags |= CFLGS_OFF_SLAB;
2143
2144 size = ALIGN(size, align);
2145
Linus Torvaldsf78bb8a2006-03-08 10:33:05 -08002146 left_over = calculate_slab_order(cachep, size, align, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
2148 if (!cachep->num) {
2149 printk("kmem_cache_create: couldn't create cache %s.\n", name);
2150 kmem_cache_free(&cache_cache, cachep);
2151 cachep = NULL;
Andrew Morton4f12bb42005-11-07 00:58:00 -08002152 goto oops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002154 slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t)
2155 + sizeof(struct slab), align);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156
2157 /*
2158 * If the slab has been placed off-slab, and we have enough space then
2159 * move it on-slab. This is at the expense of any extra colouring.
2160 */
2161 if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) {
2162 flags &= ~CFLGS_OFF_SLAB;
2163 left_over -= slab_size;
2164 }
2165
2166 if (flags & CFLGS_OFF_SLAB) {
2167 /* really off slab. No need for manual alignment */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002168 slab_size =
2169 cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 }
2171
2172 cachep->colour_off = cache_line_size();
2173 /* Offset must be a multiple of the alignment. */
2174 if (cachep->colour_off < align)
2175 cachep->colour_off = align;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002176 cachep->colour = left_over / cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 cachep->slab_size = slab_size;
2178 cachep->flags = flags;
2179 cachep->gfpflags = 0;
2180 if (flags & SLAB_CACHE_DMA)
2181 cachep->gfpflags |= GFP_DMA;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002182 cachep->buffer_size = size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
2184 if (flags & CFLGS_OFF_SLAB)
Victor Fuscob2d55072005-09-10 00:26:36 -07002185 cachep->slabp_cache = kmem_find_general_cachep(slab_size, 0u);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 cachep->ctor = ctor;
2187 cachep->dtor = dtor;
2188 cachep->name = name;
2189
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
Pekka Enbergf30cf7d2006-03-22 00:08:11 -08002191 setup_cpu_cache(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 /* cache setup completed, link it into the list */
2194 list_add(&cachep->next, &cache_chain);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002195oops:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 if (!cachep && (flags & SLAB_PANIC))
2197 panic("kmem_cache_create(): failed to create slab `%s'\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002198 name);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002199 mutex_unlock(&cache_chain_mutex);
Ravikiran G Thirumalaif0188f42006-02-10 01:51:13 -08002200 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 return cachep;
2202}
2203EXPORT_SYMBOL(kmem_cache_create);
2204
2205#if DEBUG
2206static void check_irq_off(void)
2207{
2208 BUG_ON(!irqs_disabled());
2209}
2210
2211static void check_irq_on(void)
2212{
2213 BUG_ON(irqs_disabled());
2214}
2215
Pekka Enberg343e0d72006-02-01 03:05:50 -08002216static void check_spinlock_acquired(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
2218#ifdef CONFIG_SMP
2219 check_irq_off();
Christoph Lametere498be72005-09-09 13:03:32 -07002220 assert_spin_locked(&cachep->nodelists[numa_node_id()]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221#endif
2222}
Christoph Lametere498be72005-09-09 13:03:32 -07002223
Pekka Enberg343e0d72006-02-01 03:05:50 -08002224static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node)
Christoph Lametere498be72005-09-09 13:03:32 -07002225{
2226#ifdef CONFIG_SMP
2227 check_irq_off();
2228 assert_spin_locked(&cachep->nodelists[node]->list_lock);
2229#endif
2230}
2231
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232#else
2233#define check_irq_off() do { } while(0)
2234#define check_irq_on() do { } while(0)
2235#define check_spinlock_acquired(x) do { } while(0)
Christoph Lametere498be72005-09-09 13:03:32 -07002236#define check_spinlock_acquired_node(x, y) do { } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237#endif
2238
Christoph Lameteraab22072006-03-22 00:09:06 -08002239static void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
2240 struct array_cache *ac,
2241 int force, int node);
2242
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243static void do_drain(void *arg)
2244{
Andrew Mortona737b3e2006-03-22 00:08:11 -08002245 struct kmem_cache *cachep = arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 struct array_cache *ac;
Christoph Lameterff694162005-09-22 21:44:02 -07002247 int node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
2249 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002250 ac = cpu_cache_get(cachep);
Christoph Lameterff694162005-09-22 21:44:02 -07002251 spin_lock(&cachep->nodelists[node]->list_lock);
2252 free_block(cachep, ac->entry, ac->avail, node);
2253 spin_unlock(&cachep->nodelists[node]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 ac->avail = 0;
2255}
2256
Pekka Enberg343e0d72006-02-01 03:05:50 -08002257static void drain_cpu_caches(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258{
Christoph Lametere498be72005-09-09 13:03:32 -07002259 struct kmem_list3 *l3;
2260 int node;
2261
Andrew Mortona07fa392006-03-22 00:08:17 -08002262 on_each_cpu(do_drain, cachep, 1, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 check_irq_on();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002264 for_each_online_node(node) {
Christoph Lametere498be72005-09-09 13:03:32 -07002265 l3 = cachep->nodelists[node];
Roland Dreiera4523a82006-05-15 11:41:00 -07002266 if (l3 && l3->alien)
2267 drain_alien_cache(cachep, l3->alien);
2268 }
2269
2270 for_each_online_node(node) {
2271 l3 = cachep->nodelists[node];
2272 if (l3)
Christoph Lameteraab22072006-03-22 00:09:06 -08002273 drain_array(cachep, l3, l3->shared, 1, node);
Christoph Lametere498be72005-09-09 13:03:32 -07002274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275}
2276
Christoph Lametered11d9e2006-06-30 01:55:45 -07002277/*
2278 * Remove slabs from the list of free slabs.
2279 * Specify the number of slabs to drain in tofree.
2280 *
2281 * Returns the actual number of slabs released.
2282 */
2283static int drain_freelist(struct kmem_cache *cache,
2284 struct kmem_list3 *l3, int tofree)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285{
Christoph Lametered11d9e2006-06-30 01:55:45 -07002286 struct list_head *p;
2287 int nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289
Christoph Lametered11d9e2006-06-30 01:55:45 -07002290 nr_freed = 0;
2291 while (nr_freed < tofree && !list_empty(&l3->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
Christoph Lametered11d9e2006-06-30 01:55:45 -07002293 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07002294 p = l3->slabs_free.prev;
Christoph Lametered11d9e2006-06-30 01:55:45 -07002295 if (p == &l3->slabs_free) {
2296 spin_unlock_irq(&l3->list_lock);
2297 goto out;
2298 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Christoph Lametered11d9e2006-06-30 01:55:45 -07002300 slabp = list_entry(p, struct slab, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301#if DEBUG
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002302 BUG_ON(slabp->inuse);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303#endif
2304 list_del(&slabp->list);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002305 /*
2306 * Safe to drop the lock. The slab is no longer linked
2307 * to the cache.
2308 */
2309 l3->free_objects -= cache->num;
Christoph Lametere498be72005-09-09 13:03:32 -07002310 spin_unlock_irq(&l3->list_lock);
Christoph Lametered11d9e2006-06-30 01:55:45 -07002311 slab_destroy(cache, slabp);
2312 nr_freed++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 }
Christoph Lametered11d9e2006-06-30 01:55:45 -07002314out:
2315 return nr_freed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316}
2317
Pekka Enberg343e0d72006-02-01 03:05:50 -08002318static int __cache_shrink(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07002319{
2320 int ret = 0, i = 0;
2321 struct kmem_list3 *l3;
2322
2323 drain_cpu_caches(cachep);
2324
2325 check_irq_on();
2326 for_each_online_node(i) {
2327 l3 = cachep->nodelists[i];
Christoph Lametered11d9e2006-06-30 01:55:45 -07002328 if (!l3)
2329 continue;
2330
2331 drain_freelist(cachep, l3, l3->free_objects);
2332
2333 ret += !list_empty(&l3->slabs_full) ||
2334 !list_empty(&l3->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07002335 }
2336 return (ret ? 1 : 0);
2337}
2338
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339/**
2340 * kmem_cache_shrink - Shrink a cache.
2341 * @cachep: The cache to shrink.
2342 *
2343 * Releases as many slabs as possible for a cache.
2344 * To help debugging, a zero exit status indicates all slabs were released.
2345 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002346int kmem_cache_shrink(struct kmem_cache *cachep)
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 return __cache_shrink(cachep);
2351}
2352EXPORT_SYMBOL(kmem_cache_shrink);
2353
2354/**
2355 * kmem_cache_destroy - delete a cache
2356 * @cachep: the cache to destroy
2357 *
Pekka Enberg343e0d72006-02-01 03:05:50 -08002358 * Remove a struct kmem_cache object from the slab cache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 * Returns 0 on success.
2360 *
2361 * It is expected this function will be called by a module when it is
2362 * unloaded. This will remove the cache completely, and avoid a duplicate
2363 * cache being allocated each time a module is loaded and unloaded, if the
2364 * module doesn't have persistent in-kernel storage across loads and unloads.
2365 *
2366 * The cache must be empty before calling this function.
2367 *
2368 * The caller must guarantee that noone will allocate memory from the cache
2369 * during the kmem_cache_destroy().
2370 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002371int kmem_cache_destroy(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372{
2373 int i;
Christoph Lametere498be72005-09-09 13:03:32 -07002374 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002376 BUG_ON(!cachep || in_interrupt());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
2378 /* Don't let CPUs to come and go */
2379 lock_cpu_hotplug();
2380
2381 /* Find the cache in the chain of caches. */
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002382 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 /*
2384 * the chain is never empty, cache_cache is never destroyed
2385 */
2386 list_del(&cachep->next);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002387 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389 if (__cache_shrink(cachep)) {
2390 slab_error(cachep, "Can't free all objects");
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002391 mutex_lock(&cache_chain_mutex);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002392 list_add(&cachep->next, &cache_chain);
Ingo Molnarfc0abb12006-01-18 17:42:33 -08002393 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 unlock_cpu_hotplug();
2395 return 1;
2396 }
2397
2398 if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU))
Paul E. McKenneyfbd568a3e2005-05-01 08:59:04 -07002399 synchronize_rcu();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400
Christoph Lametere498be72005-09-09 13:03:32 -07002401 for_each_online_cpu(i)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002402 kfree(cachep->array[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
2404 /* NUMA: free the list3 structures */
Christoph Lametere498be72005-09-09 13:03:32 -07002405 for_each_online_node(i) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002406 l3 = cachep->nodelists[i];
2407 if (l3) {
Christoph Lametere498be72005-09-09 13:03:32 -07002408 kfree(l3->shared);
2409 free_alien_cache(l3->alien);
2410 kfree(l3);
2411 }
2412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 kmem_cache_free(&cache_cache, cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 unlock_cpu_hotplug();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415 return 0;
2416}
2417EXPORT_SYMBOL(kmem_cache_destroy);
2418
2419/* Get the memory for a slab management obj. */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002420static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp,
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002421 int colour_off, gfp_t local_flags,
2422 int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423{
2424 struct slab *slabp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002425
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 if (OFF_SLAB(cachep)) {
2427 /* Slab management obj is off-slab. */
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002428 slabp = kmem_cache_alloc_node(cachep->slabp_cache,
2429 local_flags, nodeid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 if (!slabp)
2431 return NULL;
2432 } else {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002433 slabp = objp + colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 colour_off += cachep->slab_size;
2435 }
2436 slabp->inuse = 0;
2437 slabp->colouroff = colour_off;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002438 slabp->s_mem = objp + colour_off;
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002439 slabp->nodeid = nodeid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 return slabp;
2441}
2442
2443static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp)
2444{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002445 return (kmem_bufctl_t *) (slabp + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446}
2447
Pekka Enberg343e0d72006-02-01 03:05:50 -08002448static void cache_init_objs(struct kmem_cache *cachep,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002449 struct slab *slabp, unsigned long ctor_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450{
2451 int i;
2452
2453 for (i = 0; i < cachep->num; i++) {
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002454 void *objp = index_to_obj(cachep, slabp, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455#if DEBUG
2456 /* need to poison the objs? */
2457 if (cachep->flags & SLAB_POISON)
2458 poison_obj(cachep, objp, POISON_FREE);
2459 if (cachep->flags & SLAB_STORE_USER)
2460 *dbg_userword(cachep, objp) = NULL;
2461
2462 if (cachep->flags & SLAB_RED_ZONE) {
2463 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2464 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2465 }
2466 /*
Andrew Mortona737b3e2006-03-22 00:08:11 -08002467 * Constructors are not allowed to allocate memory from the same
2468 * cache which they are a constructor for. Otherwise, deadlock.
2469 * They must also be threaded.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 */
2471 if (cachep->ctor && !(cachep->flags & SLAB_POISON))
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002472 cachep->ctor(objp + obj_offset(cachep), cachep,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002473 ctor_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
2475 if (cachep->flags & SLAB_RED_ZONE) {
2476 if (*dbg_redzone2(cachep, objp) != RED_INACTIVE)
2477 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002478 " end of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE)
2480 slab_error(cachep, "constructor overwrote the"
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002481 " start of an object");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 }
Andrew Mortona737b3e2006-03-22 00:08:11 -08002483 if ((cachep->buffer_size % PAGE_SIZE) == 0 &&
2484 OFF_SLAB(cachep) && cachep->flags & SLAB_POISON)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002485 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002486 cachep->buffer_size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487#else
2488 if (cachep->ctor)
2489 cachep->ctor(objp, cachep, ctor_flags);
2490#endif
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002491 slab_bufctl(slabp)[i] = i + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002493 slab_bufctl(slabp)[i - 1] = BUFCTL_END;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 slabp->free = 0;
2495}
2496
Pekka Enberg343e0d72006-02-01 03:05:50 -08002497static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498{
Andrew Mortona737b3e2006-03-22 00:08:11 -08002499 if (flags & SLAB_DMA)
2500 BUG_ON(!(cachep->gfpflags & GFP_DMA));
2501 else
2502 BUG_ON(cachep->gfpflags & GFP_DMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
Andrew Mortona737b3e2006-03-22 00:08:11 -08002505static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp,
2506 int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002507{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002508 void *objp = index_to_obj(cachep, slabp, slabp->free);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002509 kmem_bufctl_t next;
2510
2511 slabp->inuse++;
2512 next = slab_bufctl(slabp)[slabp->free];
2513#if DEBUG
2514 slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE;
2515 WARN_ON(slabp->nodeid != nodeid);
2516#endif
2517 slabp->free = next;
2518
2519 return objp;
2520}
2521
Andrew Mortona737b3e2006-03-22 00:08:11 -08002522static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp,
2523 void *objp, int nodeid)
Matthew Dobson78d382d2006-02-01 03:05:47 -08002524{
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002525 unsigned int objnr = obj_to_index(cachep, slabp, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002526
2527#if DEBUG
2528 /* Verify that the slab belongs to the intended node */
2529 WARN_ON(slabp->nodeid != nodeid);
2530
Al Viro871751e2006-03-25 03:06:39 -08002531 if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) {
Matthew Dobson78d382d2006-02-01 03:05:47 -08002532 printk(KERN_ERR "slab: double free detected in cache "
Andrew Mortona737b3e2006-03-22 00:08:11 -08002533 "'%s', objp %p\n", cachep->name, objp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08002534 BUG();
2535 }
2536#endif
2537 slab_bufctl(slabp)[objnr] = slabp->free;
2538 slabp->free = objnr;
2539 slabp->inuse--;
2540}
2541
Pekka Enberg47768742006-06-23 02:03:07 -07002542/*
2543 * Map pages beginning at addr to the given cache and slab. This is required
2544 * for the slab allocator to be able to lookup the cache and slab of a
2545 * virtual address for kfree, ksize, kmem_ptr_validate, and slab debugging.
2546 */
2547static void slab_map_pages(struct kmem_cache *cache, struct slab *slab,
2548 void *addr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549{
Pekka Enberg47768742006-06-23 02:03:07 -07002550 int nr_pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 struct page *page;
2552
Pekka Enberg47768742006-06-23 02:03:07 -07002553 page = virt_to_page(addr);
Nick Piggin84097512006-03-22 00:08:34 -08002554
Pekka Enberg47768742006-06-23 02:03:07 -07002555 nr_pages = 1;
Nick Piggin84097512006-03-22 00:08:34 -08002556 if (likely(!PageCompound(page)))
Pekka Enberg47768742006-06-23 02:03:07 -07002557 nr_pages <<= cache->gfporder;
2558
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 do {
Pekka Enberg47768742006-06-23 02:03:07 -07002560 page_set_cache(page, cache);
2561 page_set_slab(page, slab);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 page++;
Pekka Enberg47768742006-06-23 02:03:07 -07002563 } while (--nr_pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564}
2565
2566/*
2567 * Grow (by 1) the number of slabs within a cache. This is called by
2568 * kmem_cache_alloc() when there are no active objs left in a cache.
2569 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08002570static int cache_grow(struct kmem_cache *cachep, gfp_t flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002572 struct slab *slabp;
2573 void *objp;
2574 size_t offset;
2575 gfp_t local_flags;
2576 unsigned long ctor_flags;
Christoph Lametere498be72005-09-09 13:03:32 -07002577 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578
Andrew Mortona737b3e2006-03-22 00:08:11 -08002579 /*
2580 * Be lazy and only check for valid flags here, keeping it out of the
2581 * critical path in kmem_cache_alloc().
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582 */
Eric Sesterhenn40094fa2006-04-02 13:49:25 +02002583 BUG_ON(flags & ~(SLAB_DMA | SLAB_LEVEL_MASK | SLAB_NO_GROW));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 if (flags & SLAB_NO_GROW)
2585 return 0;
2586
2587 ctor_flags = SLAB_CTOR_CONSTRUCTOR;
2588 local_flags = (flags & SLAB_LEVEL_MASK);
2589 if (!(local_flags & __GFP_WAIT))
2590 /*
2591 * Not allowed to sleep. Need to tell a constructor about
2592 * this - it might need to know...
2593 */
2594 ctor_flags |= SLAB_CTOR_ATOMIC;
2595
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002596 /* Take the l3 list lock to change the colour_next on this node */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597 check_irq_off();
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002598 l3 = cachep->nodelists[nodeid];
2599 spin_lock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600
2601 /* Get colour for the slab, and cal the next value. */
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002602 offset = l3->colour_next;
2603 l3->colour_next++;
2604 if (l3->colour_next >= cachep->colour)
2605 l3->colour_next = 0;
2606 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607
Ravikiran G Thirumalai2e1217c2006-02-04 23:27:56 -08002608 offset *= cachep->colour_off;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
2610 if (local_flags & __GFP_WAIT)
2611 local_irq_enable();
2612
2613 /*
2614 * The test for missing atomic flag is performed here, rather than
2615 * the more obvious place, simply to reduce the critical path length
2616 * in kmem_cache_alloc(). If a caller is seriously mis-behaving they
2617 * will eventually be caught here (where it matters).
2618 */
2619 kmem_flagcheck(cachep, flags);
2620
Andrew Mortona737b3e2006-03-22 00:08:11 -08002621 /*
2622 * Get mem for the objs. Attempt to allocate a physical page from
2623 * 'nodeid'.
Christoph Lametere498be72005-09-09 13:03:32 -07002624 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08002625 objp = kmem_getpages(cachep, flags, nodeid);
2626 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 goto failed;
2628
2629 /* Get slab management. */
Ravikiran G Thirumalai5b74ada2006-04-10 22:52:53 -07002630 slabp = alloc_slabmgmt(cachep, objp, offset, local_flags, nodeid);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002631 if (!slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 goto opps1;
2633
Christoph Lametere498be72005-09-09 13:03:32 -07002634 slabp->nodeid = nodeid;
Pekka Enberg47768742006-06-23 02:03:07 -07002635 slab_map_pages(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
2637 cache_init_objs(cachep, slabp, ctor_flags);
2638
2639 if (local_flags & __GFP_WAIT)
2640 local_irq_disable();
2641 check_irq_off();
Christoph Lametere498be72005-09-09 13:03:32 -07002642 spin_lock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
2644 /* Make slab active. */
Christoph Lametere498be72005-09-09 13:03:32 -07002645 list_add_tail(&slabp->list, &(l3->slabs_free));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 STATS_INC_GROWN(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07002647 l3->free_objects += cachep->num;
2648 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002650opps1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651 kmem_freepages(cachep, objp);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002652failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 if (local_flags & __GFP_WAIT)
2654 local_irq_disable();
2655 return 0;
2656}
2657
2658#if DEBUG
2659
2660/*
2661 * Perform extra freeing checks:
2662 * - detect bad pointers.
2663 * - POISON/RED_ZONE checking
2664 * - destructor calls, for caches with POISON+dtor
2665 */
2666static void kfree_debugcheck(const void *objp)
2667{
2668 struct page *page;
2669
2670 if (!virt_addr_valid(objp)) {
2671 printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002672 (unsigned long)objp);
2673 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 }
2675 page = virt_to_page(objp);
2676 if (!PageSlab(page)) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002677 printk(KERN_ERR "kfree_debugcheck: bad ptr %lxh.\n",
2678 (unsigned long)objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 BUG();
2680 }
2681}
2682
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002683static inline void verify_redzone_free(struct kmem_cache *cache, void *obj)
2684{
2685 unsigned long redzone1, redzone2;
2686
2687 redzone1 = *dbg_redzone1(cache, obj);
2688 redzone2 = *dbg_redzone2(cache, obj);
2689
2690 /*
2691 * Redzone is ok.
2692 */
2693 if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE)
2694 return;
2695
2696 if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE)
2697 slab_error(cache, "double free detected");
2698 else
2699 slab_error(cache, "memory outside object was overwritten");
2700
2701 printk(KERN_ERR "%p: redzone 1:0x%lx, redzone 2:0x%lx.\n",
2702 obj, redzone1, redzone2);
2703}
2704
Pekka Enberg343e0d72006-02-01 03:05:50 -08002705static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002706 void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707{
2708 struct page *page;
2709 unsigned int objnr;
2710 struct slab *slabp;
2711
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002712 objp -= obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 kfree_debugcheck(objp);
2714 page = virt_to_page(objp);
2715
Pekka Enberg065d41c2005-11-13 16:06:46 -08002716 slabp = page_get_slab(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718 if (cachep->flags & SLAB_RED_ZONE) {
Pekka Enberg58ce1fd2006-06-23 02:03:24 -07002719 verify_redzone_free(cachep, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 *dbg_redzone1(cachep, objp) = RED_INACTIVE;
2721 *dbg_redzone2(cachep, objp) = RED_INACTIVE;
2722 }
2723 if (cachep->flags & SLAB_STORE_USER)
2724 *dbg_userword(cachep, objp) = caller;
2725
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002726 objnr = obj_to_index(cachep, slabp, objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727
2728 BUG_ON(objnr >= cachep->num);
Pekka Enberg8fea4e92006-03-22 00:08:10 -08002729 BUG_ON(objp != index_to_obj(cachep, slabp, objnr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730
2731 if (cachep->flags & SLAB_DEBUG_INITIAL) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002732 /*
2733 * Need to call the slab's constructor so the caller can
2734 * perform a verify of its state (debugging). Called without
2735 * the cache-lock held.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 */
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002737 cachep->ctor(objp + obj_offset(cachep),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002738 cachep, SLAB_CTOR_CONSTRUCTOR | SLAB_CTOR_VERIFY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 }
2740 if (cachep->flags & SLAB_POISON && cachep->dtor) {
2741 /* we want to cache poison the object,
2742 * call the destruction callback
2743 */
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002744 cachep->dtor(objp + obj_offset(cachep), cachep, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745 }
Al Viro871751e2006-03-25 03:06:39 -08002746#ifdef CONFIG_DEBUG_SLAB_LEAK
2747 slab_bufctl(slabp)[objnr] = BUFCTL_FREE;
2748#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 if (cachep->flags & SLAB_POISON) {
2750#ifdef CONFIG_DEBUG_PAGEALLOC
Andrew Mortona737b3e2006-03-22 00:08:11 -08002751 if ((cachep->buffer_size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 store_stackinfo(cachep, objp, (unsigned long)caller);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002753 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002754 cachep->buffer_size / PAGE_SIZE, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755 } else {
2756 poison_obj(cachep, objp, POISON_FREE);
2757 }
2758#else
2759 poison_obj(cachep, objp, POISON_FREE);
2760#endif
2761 }
2762 return objp;
2763}
2764
Pekka Enberg343e0d72006-02-01 03:05:50 -08002765static void check_slabp(struct kmem_cache *cachep, struct slab *slabp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
2767 kmem_bufctl_t i;
2768 int entries = 0;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 /* Check slab's freelist to see if this obj is there. */
2771 for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) {
2772 entries++;
2773 if (entries > cachep->num || i >= cachep->num)
2774 goto bad;
2775 }
2776 if (entries != cachep->num - slabp->inuse) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002777bad:
2778 printk(KERN_ERR "slab: Internal list corruption detected in "
2779 "cache '%s'(%d), slabp %p(%d). Hexdump:\n",
2780 cachep->name, cachep->num, slabp, slabp->inuse);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002781 for (i = 0;
Linus Torvalds264132b2006-03-06 12:10:07 -08002782 i < sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002783 i++) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002784 if (i % 16 == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 printk("\n%03x:", i);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002786 printk(" %02x", ((unsigned char *)slabp)[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 }
2788 printk("\n");
2789 BUG();
2790 }
2791}
2792#else
2793#define kfree_debugcheck(x) do { } while(0)
2794#define cache_free_debugcheck(x,objp,z) (objp)
2795#define check_slabp(x,y) do { } while(0)
2796#endif
2797
Pekka Enberg343e0d72006-02-01 03:05:50 -08002798static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
2800 int batchcount;
2801 struct kmem_list3 *l3;
2802 struct array_cache *ac;
2803
2804 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002805 ac = cpu_cache_get(cachep);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002806retry:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 batchcount = ac->batchcount;
2808 if (!ac->touched && batchcount > BATCHREFILL_LIMIT) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002809 /*
2810 * If there was little recent activity on this cache, then
2811 * perform only a partial refill. Otherwise we could generate
2812 * refill bouncing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 */
2814 batchcount = BATCHREFILL_LIMIT;
2815 }
Christoph Lametere498be72005-09-09 13:03:32 -07002816 l3 = cachep->nodelists[numa_node_id()];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817
Christoph Lametere498be72005-09-09 13:03:32 -07002818 BUG_ON(ac->avail > 0 || !l3);
2819 spin_lock(&l3->list_lock);
2820
Christoph Lameter3ded1752006-03-25 03:06:44 -08002821 /* See if we can refill from the shared array */
2822 if (l3->shared && transfer_objects(ac, l3->shared, batchcount))
2823 goto alloc_done;
2824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 while (batchcount > 0) {
2826 struct list_head *entry;
2827 struct slab *slabp;
2828 /* Get slab alloc is to come from. */
2829 entry = l3->slabs_partial.next;
2830 if (entry == &l3->slabs_partial) {
2831 l3->free_touched = 1;
2832 entry = l3->slabs_free.next;
2833 if (entry == &l3->slabs_free)
2834 goto must_grow;
2835 }
2836
2837 slabp = list_entry(entry, struct slab, list);
2838 check_slabp(cachep, slabp);
2839 check_spinlock_acquired(cachep);
2840 while (slabp->inuse < cachep->num && batchcount--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 STATS_INC_ALLOCED(cachep);
2842 STATS_INC_ACTIVE(cachep);
2843 STATS_SET_HIGH(cachep);
2844
Matthew Dobson78d382d2006-02-01 03:05:47 -08002845 ac->entry[ac->avail++] = slab_get_obj(cachep, slabp,
2846 numa_node_id());
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 }
2848 check_slabp(cachep, slabp);
2849
2850 /* move slabp to correct slabp list: */
2851 list_del(&slabp->list);
2852 if (slabp->free == BUFCTL_END)
2853 list_add(&slabp->list, &l3->slabs_full);
2854 else
2855 list_add(&slabp->list, &l3->slabs_partial);
2856 }
2857
Andrew Mortona737b3e2006-03-22 00:08:11 -08002858must_grow:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 l3->free_objects -= ac->avail;
Andrew Mortona737b3e2006-03-22 00:08:11 -08002860alloc_done:
Christoph Lametere498be72005-09-09 13:03:32 -07002861 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862
2863 if (unlikely(!ac->avail)) {
2864 int x;
Christoph Lametere498be72005-09-09 13:03:32 -07002865 x = cache_grow(cachep, flags, numa_node_id());
2866
Andrew Mortona737b3e2006-03-22 00:08:11 -08002867 /* cache_grow can reenable interrupts, then ac could change. */
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002868 ac = cpu_cache_get(cachep);
Andrew Mortona737b3e2006-03-22 00:08:11 -08002869 if (!x && ac->avail == 0) /* no objects in sight? abort */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 return NULL;
2871
Andrew Mortona737b3e2006-03-22 00:08:11 -08002872 if (!ac->avail) /* objects refilled by interrupt? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 goto retry;
2874 }
2875 ac->touched = 1;
Christoph Lametere498be72005-09-09 13:03:32 -07002876 return ac->entry[--ac->avail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877}
2878
Andrew Mortona737b3e2006-03-22 00:08:11 -08002879static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep,
2880 gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881{
2882 might_sleep_if(flags & __GFP_WAIT);
2883#if DEBUG
2884 kmem_flagcheck(cachep, flags);
2885#endif
2886}
2887
2888#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08002889static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep,
2890 gfp_t flags, void *objp, void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002892 if (!objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893 return objp;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002894 if (cachep->flags & SLAB_POISON) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895#ifdef CONFIG_DEBUG_PAGEALLOC
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002896 if ((cachep->buffer_size % PAGE_SIZE) == 0 && OFF_SLAB(cachep))
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002897 kernel_map_pages(virt_to_page(objp),
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002898 cachep->buffer_size / PAGE_SIZE, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 else
2900 check_poison_obj(cachep, objp);
2901#else
2902 check_poison_obj(cachep, objp);
2903#endif
2904 poison_obj(cachep, objp, POISON_INUSE);
2905 }
2906 if (cachep->flags & SLAB_STORE_USER)
2907 *dbg_userword(cachep, objp) = caller;
2908
2909 if (cachep->flags & SLAB_RED_ZONE) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08002910 if (*dbg_redzone1(cachep, objp) != RED_INACTIVE ||
2911 *dbg_redzone2(cachep, objp) != RED_INACTIVE) {
2912 slab_error(cachep, "double free, or memory outside"
2913 " object was overwritten");
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002914 printk(KERN_ERR
Andrew Mortona737b3e2006-03-22 00:08:11 -08002915 "%p: redzone 1:0x%lx, redzone 2:0x%lx\n",
2916 objp, *dbg_redzone1(cachep, objp),
2917 *dbg_redzone2(cachep, objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 }
2919 *dbg_redzone1(cachep, objp) = RED_ACTIVE;
2920 *dbg_redzone2(cachep, objp) = RED_ACTIVE;
2921 }
Al Viro871751e2006-03-25 03:06:39 -08002922#ifdef CONFIG_DEBUG_SLAB_LEAK
2923 {
2924 struct slab *slabp;
2925 unsigned objnr;
2926
2927 slabp = page_get_slab(virt_to_page(objp));
2928 objnr = (unsigned)(objp - slabp->s_mem) / cachep->buffer_size;
2929 slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE;
2930 }
2931#endif
Manfred Spraul3dafccf2006-02-01 03:05:42 -08002932 objp += obj_offset(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 if (cachep->ctor && cachep->flags & SLAB_POISON) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002934 unsigned long ctor_flags = SLAB_CTOR_CONSTRUCTOR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935
2936 if (!(flags & __GFP_WAIT))
2937 ctor_flags |= SLAB_CTOR_ATOMIC;
2938
2939 cachep->ctor(objp, cachep, ctor_flags);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return objp;
2942}
2943#else
2944#define cache_alloc_debugcheck_after(a,b,objp,d) (objp)
2945#endif
2946
Pekka Enberg343e0d72006-02-01 03:05:50 -08002947static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002949 void *objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 struct array_cache *ac;
2951
Christoph Lameterdc85da12006-01-18 17:42:36 -08002952#ifdef CONFIG_NUMA
Paul Jacksonb2455392006-03-24 03:16:12 -08002953 if (unlikely(current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) {
Paul Jacksonc61afb12006-03-24 03:16:08 -08002954 objp = alternate_node_alloc(cachep, flags);
2955 if (objp != NULL)
2956 return objp;
Paul Jackson101a5002006-03-24 03:16:07 -08002957 }
Christoph Lameterdc85da12006-01-18 17:42:36 -08002958#endif
2959
Alok N Kataria5c382302005-09-27 21:45:46 -07002960 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08002961 ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 if (likely(ac->avail)) {
2963 STATS_INC_ALLOCHIT(cachep);
2964 ac->touched = 1;
Christoph Lametere498be72005-09-09 13:03:32 -07002965 objp = ac->entry[--ac->avail];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 } else {
2967 STATS_INC_ALLOCMISS(cachep);
2968 objp = cache_alloc_refill(cachep, flags);
2969 }
Alok N Kataria5c382302005-09-27 21:45:46 -07002970 return objp;
2971}
2972
Andrew Mortona737b3e2006-03-22 00:08:11 -08002973static __always_inline void *__cache_alloc(struct kmem_cache *cachep,
2974 gfp_t flags, void *caller)
Alok N Kataria5c382302005-09-27 21:45:46 -07002975{
2976 unsigned long save_flags;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08002977 void *objp;
Alok N Kataria5c382302005-09-27 21:45:46 -07002978
2979 cache_alloc_debugcheck_before(cachep, flags);
2980
2981 local_irq_save(save_flags);
2982 objp = ____cache_alloc(cachep, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 local_irq_restore(save_flags);
Eric Dumazet34342e82005-09-03 15:55:06 -07002984 objp = cache_alloc_debugcheck_after(cachep, flags, objp,
Pekka Enberg7fd6b142006-02-01 03:05:52 -08002985 caller);
Eric Dumazet34342e82005-09-03 15:55:06 -07002986 prefetchw(objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 return objp;
2988}
2989
Christoph Lametere498be72005-09-09 13:03:32 -07002990#ifdef CONFIG_NUMA
2991/*
Paul Jacksonb2455392006-03-24 03:16:12 -08002992 * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY.
Paul Jacksonc61afb12006-03-24 03:16:08 -08002993 *
2994 * If we are in_interrupt, then process context, including cpusets and
2995 * mempolicy, may not apply and should not be used for allocation policy.
2996 */
2997static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags)
2998{
2999 int nid_alloc, nid_here;
3000
3001 if (in_interrupt())
3002 return NULL;
3003 nid_alloc = nid_here = numa_node_id();
3004 if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD))
3005 nid_alloc = cpuset_mem_spread_node();
3006 else if (current->mempolicy)
3007 nid_alloc = slab_node(current->mempolicy);
3008 if (nid_alloc != nid_here)
3009 return __cache_alloc_node(cachep, flags, nid_alloc);
3010 return NULL;
3011}
3012
3013/*
Christoph Lametere498be72005-09-09 13:03:32 -07003014 * A interface to enable slab creation on nodeid
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 */
Andrew Mortona737b3e2006-03-22 00:08:11 -08003016static void *__cache_alloc_node(struct kmem_cache *cachep, gfp_t flags,
3017 int nodeid)
Christoph Lametere498be72005-09-09 13:03:32 -07003018{
3019 struct list_head *entry;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003020 struct slab *slabp;
3021 struct kmem_list3 *l3;
3022 void *obj;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003023 int x;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003025 l3 = cachep->nodelists[nodeid];
3026 BUG_ON(!l3);
Christoph Lametere498be72005-09-09 13:03:32 -07003027
Andrew Mortona737b3e2006-03-22 00:08:11 -08003028retry:
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003029 check_irq_off();
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003030 spin_lock(&l3->list_lock);
3031 entry = l3->slabs_partial.next;
3032 if (entry == &l3->slabs_partial) {
3033 l3->free_touched = 1;
3034 entry = l3->slabs_free.next;
3035 if (entry == &l3->slabs_free)
3036 goto must_grow;
3037 }
Christoph Lametere498be72005-09-09 13:03:32 -07003038
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003039 slabp = list_entry(entry, struct slab, list);
3040 check_spinlock_acquired_node(cachep, nodeid);
3041 check_slabp(cachep, slabp);
Christoph Lametere498be72005-09-09 13:03:32 -07003042
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003043 STATS_INC_NODEALLOCS(cachep);
3044 STATS_INC_ACTIVE(cachep);
3045 STATS_SET_HIGH(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003046
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003047 BUG_ON(slabp->inuse == cachep->num);
Christoph Lametere498be72005-09-09 13:03:32 -07003048
Matthew Dobson78d382d2006-02-01 03:05:47 -08003049 obj = slab_get_obj(cachep, slabp, nodeid);
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003050 check_slabp(cachep, slabp);
3051 l3->free_objects--;
3052 /* move slabp to correct slabp list: */
3053 list_del(&slabp->list);
Christoph Lametere498be72005-09-09 13:03:32 -07003054
Andrew Mortona737b3e2006-03-22 00:08:11 -08003055 if (slabp->free == BUFCTL_END)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003056 list_add(&slabp->list, &l3->slabs_full);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003057 else
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003058 list_add(&slabp->list, &l3->slabs_partial);
Christoph Lametere498be72005-09-09 13:03:32 -07003059
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003060 spin_unlock(&l3->list_lock);
3061 goto done;
Christoph Lametere498be72005-09-09 13:03:32 -07003062
Andrew Mortona737b3e2006-03-22 00:08:11 -08003063must_grow:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003064 spin_unlock(&l3->list_lock);
3065 x = cache_grow(cachep, flags, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07003066
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003067 if (!x)
3068 return NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003069
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003070 goto retry;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003071done:
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003072 return obj;
Christoph Lametere498be72005-09-09 13:03:32 -07003073}
3074#endif
3075
3076/*
3077 * Caller needs to acquire correct kmem_list's list_lock
3078 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003079static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003080 int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081{
3082 int i;
Christoph Lametere498be72005-09-09 13:03:32 -07003083 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
3085 for (i = 0; i < nr_objects; i++) {
3086 void *objp = objpp[i];
3087 struct slab *slabp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088
Pekka Enberg6ed5eb22006-02-01 03:05:49 -08003089 slabp = virt_to_slab(objp);
Christoph Lameterff694162005-09-22 21:44:02 -07003090 l3 = cachep->nodelists[node];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 list_del(&slabp->list);
Christoph Lameterff694162005-09-22 21:44:02 -07003092 check_spinlock_acquired_node(cachep, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093 check_slabp(cachep, slabp);
Matthew Dobson78d382d2006-02-01 03:05:47 -08003094 slab_put_obj(cachep, slabp, objp, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 STATS_DEC_ACTIVE(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003096 l3->free_objects++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 check_slabp(cachep, slabp);
3098
3099 /* fixup slab chains */
3100 if (slabp->inuse == 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07003101 if (l3->free_objects > l3->free_limit) {
3102 l3->free_objects -= cachep->num;
Ingo Molnar2b2d5492006-07-03 00:25:28 -07003103 /*
3104 * It is safe to drop the lock. The slab is
3105 * no longer linked to the cache. cachep
3106 * cannot disappear - we are using it and
3107 * all destruction of caches must be
3108 * serialized properly by the user.
3109 */
3110 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 slab_destroy(cachep, slabp);
Ingo Molnar2b2d5492006-07-03 00:25:28 -07003112 spin_lock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07003114 list_add(&slabp->list, &l3->slabs_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 }
3116 } else {
3117 /* Unconditionally move a slab to the end of the
3118 * partial list on free - maximum time for the
3119 * other objects to be freed, too.
3120 */
Christoph Lametere498be72005-09-09 13:03:32 -07003121 list_add_tail(&slabp->list, &l3->slabs_partial);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 }
3123 }
3124}
3125
Pekka Enberg343e0d72006-02-01 03:05:50 -08003126static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127{
3128 int batchcount;
Christoph Lametere498be72005-09-09 13:03:32 -07003129 struct kmem_list3 *l3;
Christoph Lameterff694162005-09-22 21:44:02 -07003130 int node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131
3132 batchcount = ac->batchcount;
3133#if DEBUG
3134 BUG_ON(!batchcount || batchcount > ac->avail);
3135#endif
3136 check_irq_off();
Christoph Lameterff694162005-09-22 21:44:02 -07003137 l3 = cachep->nodelists[node];
Ingo Molnar2b2d5492006-07-03 00:25:28 -07003138 spin_lock_nested(&l3->list_lock, SINGLE_DEPTH_NESTING);
Christoph Lametere498be72005-09-09 13:03:32 -07003139 if (l3->shared) {
3140 struct array_cache *shared_array = l3->shared;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003141 int max = shared_array->limit - shared_array->avail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 if (max) {
3143 if (batchcount > max)
3144 batchcount = max;
Christoph Lametere498be72005-09-09 13:03:32 -07003145 memcpy(&(shared_array->entry[shared_array->avail]),
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003146 ac->entry, sizeof(void *) * batchcount);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 shared_array->avail += batchcount;
3148 goto free_done;
3149 }
3150 }
3151
Christoph Lameterff694162005-09-22 21:44:02 -07003152 free_block(cachep, ac->entry, batchcount, node);
Andrew Mortona737b3e2006-03-22 00:08:11 -08003153free_done:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154#if STATS
3155 {
3156 int i = 0;
3157 struct list_head *p;
3158
Christoph Lametere498be72005-09-09 13:03:32 -07003159 p = l3->slabs_free.next;
3160 while (p != &(l3->slabs_free)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 struct slab *slabp;
3162
3163 slabp = list_entry(p, struct slab, list);
3164 BUG_ON(slabp->inuse);
3165
3166 i++;
3167 p = p->next;
3168 }
3169 STATS_SET_FREEABLE(cachep, i);
3170 }
3171#endif
Christoph Lametere498be72005-09-09 13:03:32 -07003172 spin_unlock(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 ac->avail -= batchcount;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003174 memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175}
3176
3177/*
Andrew Mortona737b3e2006-03-22 00:08:11 -08003178 * Release an obj back to its cache. If the obj has a constructed state, it must
3179 * be in this state _before_ it is released. Called with disabled ints.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 */
Ingo Molnar2b2d5492006-07-03 00:25:28 -07003181static void __cache_free(struct kmem_cache *cachep, void *objp, int nesting)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182{
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003183 struct array_cache *ac = cpu_cache_get(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
3185 check_irq_off();
3186 objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0));
3187
Ingo Molnar2b2d5492006-07-03 00:25:28 -07003188 if (cache_free_alien(cachep, objp, nesting))
Pekka Enberg729bd0b2006-06-23 02:03:05 -07003189 return;
Christoph Lametere498be72005-09-09 13:03:32 -07003190
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 if (likely(ac->avail < ac->limit)) {
3192 STATS_INC_FREEHIT(cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003193 ac->entry[ac->avail++] = objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 return;
3195 } else {
3196 STATS_INC_FREEMISS(cachep);
3197 cache_flusharray(cachep, ac);
Christoph Lametere498be72005-09-09 13:03:32 -07003198 ac->entry[ac->avail++] = objp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 }
3200}
3201
3202/**
3203 * kmem_cache_alloc - Allocate an object
3204 * @cachep: The cache to allocate from.
3205 * @flags: See kmalloc().
3206 *
3207 * Allocate an object from this cache. The flags are only relevant
3208 * if the cache has no available objects.
3209 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003210void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211{
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003212 return __cache_alloc(cachep, flags, __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213}
3214EXPORT_SYMBOL(kmem_cache_alloc);
3215
3216/**
Pekka Enberga8c0f9a2006-03-25 03:06:42 -08003217 * kmem_cache_alloc - Allocate an object. The memory is set to zero.
3218 * @cache: The cache to allocate from.
3219 * @flags: See kmalloc().
3220 *
3221 * Allocate an object from this cache and set the allocated memory to zero.
3222 * The flags are only relevant if the cache has no available objects.
3223 */
3224void *kmem_cache_zalloc(struct kmem_cache *cache, gfp_t flags)
3225{
3226 void *ret = __cache_alloc(cache, flags, __builtin_return_address(0));
3227 if (ret)
3228 memset(ret, 0, obj_size(cache));
3229 return ret;
3230}
3231EXPORT_SYMBOL(kmem_cache_zalloc);
3232
3233/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 * kmem_ptr_validate - check if an untrusted pointer might
3235 * be a slab entry.
3236 * @cachep: the cache we're checking against
3237 * @ptr: pointer to validate
3238 *
3239 * This verifies that the untrusted pointer looks sane:
3240 * it is _not_ a guarantee that the pointer is actually
3241 * part of the slab cache in question, but it at least
3242 * validates that the pointer can be dereferenced and
3243 * looks half-way sane.
3244 *
3245 * Currently only used for dentry validation.
3246 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003247int fastcall kmem_ptr_validate(struct kmem_cache *cachep, void *ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003249 unsigned long addr = (unsigned long)ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 unsigned long min_addr = PAGE_OFFSET;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003251 unsigned long align_mask = BYTES_PER_WORD - 1;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003252 unsigned long size = cachep->buffer_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 struct page *page;
3254
3255 if (unlikely(addr < min_addr))
3256 goto out;
3257 if (unlikely(addr > (unsigned long)high_memory - size))
3258 goto out;
3259 if (unlikely(addr & align_mask))
3260 goto out;
3261 if (unlikely(!kern_addr_valid(addr)))
3262 goto out;
3263 if (unlikely(!kern_addr_valid(addr + size - 1)))
3264 goto out;
3265 page = virt_to_page(ptr);
3266 if (unlikely(!PageSlab(page)))
3267 goto out;
Pekka Enberg065d41c2005-11-13 16:06:46 -08003268 if (unlikely(page_get_cache(page) != cachep))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 goto out;
3270 return 1;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003271out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003272 return 0;
3273}
3274
3275#ifdef CONFIG_NUMA
3276/**
3277 * kmem_cache_alloc_node - Allocate an object on the specified node
3278 * @cachep: The cache to allocate from.
3279 * @flags: See kmalloc().
3280 * @nodeid: node number of the target node.
3281 *
3282 * Identical to kmem_cache_alloc, except that this function is slow
3283 * and can sleep. And it will allocate memory on the given node, which
3284 * can improve the performance for cpu bound structures.
Christoph Lametere498be72005-09-09 13:03:32 -07003285 * New and improved: it will now make sure that the object gets
3286 * put on the correct node list so that there is no false sharing.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003288void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289{
Christoph Lametere498be72005-09-09 13:03:32 -07003290 unsigned long save_flags;
3291 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
Christoph Lametere498be72005-09-09 13:03:32 -07003293 cache_alloc_debugcheck_before(cachep, flags);
3294 local_irq_save(save_flags);
Christoph Lameter18f820f2006-02-01 03:05:43 -08003295
3296 if (nodeid == -1 || nodeid == numa_node_id() ||
Andrew Mortona737b3e2006-03-22 00:08:11 -08003297 !cachep->nodelists[nodeid])
Alok N Kataria5c382302005-09-27 21:45:46 -07003298 ptr = ____cache_alloc(cachep, flags);
3299 else
3300 ptr = __cache_alloc_node(cachep, flags, nodeid);
Christoph Lametere498be72005-09-09 13:03:32 -07003301 local_irq_restore(save_flags);
Christoph Lameter18f820f2006-02-01 03:05:43 -08003302
3303 ptr = cache_alloc_debugcheck_after(cachep, flags, ptr,
3304 __builtin_return_address(0));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305
Christoph Lametere498be72005-09-09 13:03:32 -07003306 return ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307}
3308EXPORT_SYMBOL(kmem_cache_alloc_node);
3309
Al Virodd0fc662005-10-07 07:46:04 +01003310void *kmalloc_node(size_t size, gfp_t flags, int node)
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003311{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003312 struct kmem_cache *cachep;
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003313
3314 cachep = kmem_find_general_cachep(size, flags);
3315 if (unlikely(cachep == NULL))
3316 return NULL;
3317 return kmem_cache_alloc_node(cachep, flags, node);
3318}
3319EXPORT_SYMBOL(kmalloc_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003320#endif
3321
3322/**
Paul Drynoff800590f2006-06-23 02:03:48 -07003323 * __do_kmalloc - allocate memory
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 * @size: how many bytes of memory are required.
Paul Drynoff800590f2006-06-23 02:03:48 -07003325 * @flags: the type of memory to allocate (see kmalloc).
Randy Dunlap911851e2006-03-22 00:08:14 -08003326 * @caller: function caller for debug tracking of the caller
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 */
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003328static __always_inline void *__do_kmalloc(size_t size, gfp_t flags,
3329 void *caller)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003331 struct kmem_cache *cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
Manfred Spraul97e2bde2005-05-01 08:58:38 -07003333 /* If you want to save a few bytes .text space: replace
3334 * __ with kmem_.
3335 * Then kmalloc uses the uninlined functions instead of the inline
3336 * functions.
3337 */
3338 cachep = __find_general_cachep(size, flags);
Andrew Mortondbdb9042005-09-23 13:24:10 -07003339 if (unlikely(cachep == NULL))
3340 return NULL;
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003341 return __cache_alloc(cachep, flags, caller);
3342}
3343
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003344
3345void *__kmalloc(size_t size, gfp_t flags)
3346{
Al Viro871751e2006-03-25 03:06:39 -08003347#ifndef CONFIG_DEBUG_SLAB
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003348 return __do_kmalloc(size, flags, NULL);
Al Viro871751e2006-03-25 03:06:39 -08003349#else
3350 return __do_kmalloc(size, flags, __builtin_return_address(0));
3351#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352}
3353EXPORT_SYMBOL(__kmalloc);
3354
Al Viro871751e2006-03-25 03:06:39 -08003355#ifdef CONFIG_DEBUG_SLAB
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003356void *__kmalloc_track_caller(size_t size, gfp_t flags, void *caller)
3357{
3358 return __do_kmalloc(size, flags, caller);
3359}
3360EXPORT_SYMBOL(__kmalloc_track_caller);
Pekka Enberg7fd6b142006-02-01 03:05:52 -08003361#endif
3362
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363#ifdef CONFIG_SMP
3364/**
3365 * __alloc_percpu - allocate one copy of the object for every present
3366 * cpu in the system, zeroing them.
3367 * Objects should be dereferenced using the per_cpu_ptr macro only.
3368 *
3369 * @size: how many bytes of memory are required.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370 */
Pekka Enbergf9f75002006-01-08 01:00:33 -08003371void *__alloc_percpu(size_t size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372{
3373 int i;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003374 struct percpu_data *pdata = kmalloc(sizeof(*pdata), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375
3376 if (!pdata)
3377 return NULL;
3378
Christoph Lametere498be72005-09-09 13:03:32 -07003379 /*
3380 * Cannot use for_each_online_cpu since a cpu may come online
3381 * and we have no way of figuring out how to fix the array
3382 * that we have allocated then....
3383 */
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08003384 for_each_possible_cpu(i) {
Christoph Lametere498be72005-09-09 13:03:32 -07003385 int node = cpu_to_node(i);
3386
3387 if (node_online(node))
3388 pdata->ptrs[i] = kmalloc_node(size, GFP_KERNEL, node);
3389 else
3390 pdata->ptrs[i] = kmalloc(size, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
3392 if (!pdata->ptrs[i])
3393 goto unwind_oom;
3394 memset(pdata->ptrs[i], 0, size);
3395 }
3396
3397 /* Catch derefs w/o wrappers */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003398 return (void *)(~(unsigned long)pdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003399
Andrew Mortona737b3e2006-03-22 00:08:11 -08003400unwind_oom:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 while (--i >= 0) {
3402 if (!cpu_possible(i))
3403 continue;
3404 kfree(pdata->ptrs[i]);
3405 }
3406 kfree(pdata);
3407 return NULL;
3408}
3409EXPORT_SYMBOL(__alloc_percpu);
3410#endif
3411
3412/**
3413 * kmem_cache_free - Deallocate an object
3414 * @cachep: The cache the allocation was from.
3415 * @objp: The previously allocated object.
3416 *
3417 * Free an object which was previously allocated from this
3418 * cache.
3419 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003420void kmem_cache_free(struct kmem_cache *cachep, void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421{
3422 unsigned long flags;
3423
Pekka Enbergddc2e812006-06-23 02:03:40 -07003424 BUG_ON(virt_to_cache(objp) != cachep);
3425
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426 local_irq_save(flags);
Ingo Molnar2b2d5492006-07-03 00:25:28 -07003427 __cache_free(cachep, objp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 local_irq_restore(flags);
3429}
3430EXPORT_SYMBOL(kmem_cache_free);
3431
3432/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 * kfree - free previously allocated memory
3434 * @objp: pointer returned by kmalloc.
3435 *
Pekka Enberg80e93ef2005-09-09 13:10:16 -07003436 * If @objp is NULL, no operation is performed.
3437 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 * Don't free memory not originally allocated by kmalloc()
3439 * or you will run into trouble.
3440 */
3441void kfree(const void *objp)
3442{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003443 struct kmem_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444 unsigned long flags;
3445
3446 if (unlikely(!objp))
3447 return;
3448 local_irq_save(flags);
3449 kfree_debugcheck(objp);
Pekka Enberg6ed5eb22006-02-01 03:05:49 -08003450 c = virt_to_cache(objp);
Ingo Molnarf9b84042006-06-27 02:54:49 -07003451 debug_check_no_locks_freed(objp, obj_size(c));
Ingo Molnar2b2d5492006-07-03 00:25:28 -07003452 __cache_free(c, (void *)objp, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453 local_irq_restore(flags);
3454}
3455EXPORT_SYMBOL(kfree);
3456
3457#ifdef CONFIG_SMP
3458/**
3459 * free_percpu - free previously allocated percpu memory
3460 * @objp: pointer returned by alloc_percpu.
3461 *
3462 * Don't free memory not originally allocated by alloc_percpu()
3463 * The complemented objp is to check for that.
3464 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003465void free_percpu(const void *objp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466{
3467 int i;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003468 struct percpu_data *p = (struct percpu_data *)(~(unsigned long)objp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Christoph Lametere498be72005-09-09 13:03:32 -07003470 /*
3471 * We allocate for all cpus so we cannot use for online cpu here.
3472 */
KAMEZAWA Hiroyuki0a945022006-03-28 01:56:37 -08003473 for_each_possible_cpu(i)
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003474 kfree(p->ptrs[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003475 kfree(p);
3476}
3477EXPORT_SYMBOL(free_percpu);
3478#endif
3479
Pekka Enberg343e0d72006-02-01 03:05:50 -08003480unsigned int kmem_cache_size(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481{
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003482 return obj_size(cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483}
3484EXPORT_SYMBOL(kmem_cache_size);
3485
Pekka Enberg343e0d72006-02-01 03:05:50 -08003486const char *kmem_cache_name(struct kmem_cache *cachep)
Arnaldo Carvalho de Melo19449722005-06-18 22:46:19 -07003487{
3488 return cachep->name;
3489}
3490EXPORT_SYMBOL_GPL(kmem_cache_name);
3491
Christoph Lametere498be72005-09-09 13:03:32 -07003492/*
Christoph Lameter0718dc22006-03-25 03:06:47 -08003493 * This initializes kmem_list3 or resizes varioius caches for all nodes.
Christoph Lametere498be72005-09-09 13:03:32 -07003494 */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003495static int alloc_kmemlist(struct kmem_cache *cachep)
Christoph Lametere498be72005-09-09 13:03:32 -07003496{
3497 int node;
3498 struct kmem_list3 *l3;
Christoph Lametercafeb022006-03-25 03:06:46 -08003499 struct array_cache *new_shared;
3500 struct array_cache **new_alien;
Christoph Lametere498be72005-09-09 13:03:32 -07003501
3502 for_each_online_node(node) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003503
Andrew Mortona737b3e2006-03-22 00:08:11 -08003504 new_alien = alloc_alien_cache(node, cachep->limit);
3505 if (!new_alien)
Christoph Lametere498be72005-09-09 13:03:32 -07003506 goto fail;
Christoph Lametercafeb022006-03-25 03:06:46 -08003507
Christoph Lameter0718dc22006-03-25 03:06:47 -08003508 new_shared = alloc_arraycache(node,
3509 cachep->shared*cachep->batchcount,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003510 0xbaadf00d);
Christoph Lameter0718dc22006-03-25 03:06:47 -08003511 if (!new_shared) {
3512 free_alien_cache(new_alien);
Christoph Lametere498be72005-09-09 13:03:32 -07003513 goto fail;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003514 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003515
Andrew Mortona737b3e2006-03-22 00:08:11 -08003516 l3 = cachep->nodelists[node];
3517 if (l3) {
Christoph Lametercafeb022006-03-25 03:06:46 -08003518 struct array_cache *shared = l3->shared;
3519
Christoph Lametere498be72005-09-09 13:03:32 -07003520 spin_lock_irq(&l3->list_lock);
3521
Christoph Lametercafeb022006-03-25 03:06:46 -08003522 if (shared)
Christoph Lameter0718dc22006-03-25 03:06:47 -08003523 free_block(cachep, shared->entry,
3524 shared->avail, node);
Christoph Lametere498be72005-09-09 13:03:32 -07003525
Christoph Lametercafeb022006-03-25 03:06:46 -08003526 l3->shared = new_shared;
3527 if (!l3->alien) {
Christoph Lametere498be72005-09-09 13:03:32 -07003528 l3->alien = new_alien;
3529 new_alien = NULL;
3530 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003531 l3->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003532 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07003533 spin_unlock_irq(&l3->list_lock);
Christoph Lametercafeb022006-03-25 03:06:46 -08003534 kfree(shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003535 free_alien_cache(new_alien);
3536 continue;
3537 }
Andrew Mortona737b3e2006-03-22 00:08:11 -08003538 l3 = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, node);
Christoph Lameter0718dc22006-03-25 03:06:47 -08003539 if (!l3) {
3540 free_alien_cache(new_alien);
3541 kfree(new_shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003542 goto fail;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003543 }
Christoph Lametere498be72005-09-09 13:03:32 -07003544
3545 kmem_list3_init(l3);
3546 l3->next_reap = jiffies + REAPTIMEOUT_LIST3 +
Andrew Mortona737b3e2006-03-22 00:08:11 -08003547 ((unsigned long)cachep) % REAPTIMEOUT_LIST3;
Christoph Lametercafeb022006-03-25 03:06:46 -08003548 l3->shared = new_shared;
Christoph Lametere498be72005-09-09 13:03:32 -07003549 l3->alien = new_alien;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003550 l3->free_limit = (1 + nr_cpus_node(node)) *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003551 cachep->batchcount + cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07003552 cachep->nodelists[node] = l3;
3553 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003554 return 0;
Christoph Lameter0718dc22006-03-25 03:06:47 -08003555
Andrew Mortona737b3e2006-03-22 00:08:11 -08003556fail:
Christoph Lameter0718dc22006-03-25 03:06:47 -08003557 if (!cachep->next.next) {
3558 /* Cache is not active yet. Roll back what we did */
3559 node--;
3560 while (node >= 0) {
3561 if (cachep->nodelists[node]) {
3562 l3 = cachep->nodelists[node];
3563
3564 kfree(l3->shared);
3565 free_alien_cache(l3->alien);
3566 kfree(l3);
3567 cachep->nodelists[node] = NULL;
3568 }
3569 node--;
3570 }
3571 }
Christoph Lametercafeb022006-03-25 03:06:46 -08003572 return -ENOMEM;
Christoph Lametere498be72005-09-09 13:03:32 -07003573}
3574
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575struct ccupdate_struct {
Pekka Enberg343e0d72006-02-01 03:05:50 -08003576 struct kmem_cache *cachep;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 struct array_cache *new[NR_CPUS];
3578};
3579
3580static void do_ccupdate_local(void *info)
3581{
Andrew Mortona737b3e2006-03-22 00:08:11 -08003582 struct ccupdate_struct *new = info;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 struct array_cache *old;
3584
3585 check_irq_off();
Pekka Enberg9a2dba42006-02-01 03:05:49 -08003586 old = cpu_cache_get(new->cachep);
Christoph Lametere498be72005-09-09 13:03:32 -07003587
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588 new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()];
3589 new->new[smp_processor_id()] = old;
3590}
3591
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -08003592/* Always called with the cache_chain_mutex held */
Andrew Mortona737b3e2006-03-22 00:08:11 -08003593static int do_tune_cpucache(struct kmem_cache *cachep, int limit,
3594 int batchcount, int shared)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003595{
3596 struct ccupdate_struct new;
Christoph Lametere498be72005-09-09 13:03:32 -07003597 int i, err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003599 memset(&new.new, 0, sizeof(new.new));
Christoph Lametere498be72005-09-09 13:03:32 -07003600 for_each_online_cpu(i) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003601 new.new[i] = alloc_arraycache(cpu_to_node(i), limit,
3602 batchcount);
Christoph Lametere498be72005-09-09 13:03:32 -07003603 if (!new.new[i]) {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003604 for (i--; i >= 0; i--)
3605 kfree(new.new[i]);
Christoph Lametere498be72005-09-09 13:03:32 -07003606 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 }
3608 }
3609 new.cachep = cachep;
3610
Andrew Mortona07fa392006-03-22 00:08:17 -08003611 on_each_cpu(do_ccupdate_local, (void *)&new, 1, 1);
Christoph Lametere498be72005-09-09 13:03:32 -07003612
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613 check_irq_on();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 cachep->batchcount = batchcount;
3615 cachep->limit = limit;
Christoph Lametere498be72005-09-09 13:03:32 -07003616 cachep->shared = shared;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Christoph Lametere498be72005-09-09 13:03:32 -07003618 for_each_online_cpu(i) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 struct array_cache *ccold = new.new[i];
3620 if (!ccold)
3621 continue;
Christoph Lametere498be72005-09-09 13:03:32 -07003622 spin_lock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
Christoph Lameterff694162005-09-22 21:44:02 -07003623 free_block(cachep, ccold->entry, ccold->avail, cpu_to_node(i));
Christoph Lametere498be72005-09-09 13:03:32 -07003624 spin_unlock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 kfree(ccold);
3626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
Christoph Lametere498be72005-09-09 13:03:32 -07003628 err = alloc_kmemlist(cachep);
3629 if (err) {
3630 printk(KERN_ERR "alloc_kmemlist failed for %s, error %d.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003631 cachep->name, -err);
Christoph Lametere498be72005-09-09 13:03:32 -07003632 BUG();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 return 0;
3635}
3636
Ravikiran G Thirumalaib5d8ca72006-03-22 00:08:12 -08003637/* Called with cache_chain_mutex held always */
Pekka Enberg343e0d72006-02-01 03:05:50 -08003638static void enable_cpucache(struct kmem_cache *cachep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639{
3640 int err;
3641 int limit, shared;
3642
Andrew Mortona737b3e2006-03-22 00:08:11 -08003643 /*
3644 * The head array serves three purposes:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 * - create a LIFO ordering, i.e. return objects that are cache-warm
3646 * - reduce the number of spinlock operations.
Andrew Mortona737b3e2006-03-22 00:08:11 -08003647 * - reduce the number of linked list operations on the slab and
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 * bufctl chains: array operations are cheaper.
3649 * The numbers are guessed, we should auto-tune as described by
3650 * Bonwick.
3651 */
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003652 if (cachep->buffer_size > 131072)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653 limit = 1;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003654 else if (cachep->buffer_size > PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655 limit = 8;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003656 else if (cachep->buffer_size > 1024)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657 limit = 24;
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003658 else if (cachep->buffer_size > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659 limit = 54;
3660 else
3661 limit = 120;
3662
Andrew Mortona737b3e2006-03-22 00:08:11 -08003663 /*
3664 * CPU bound tasks (e.g. network routing) can exhibit cpu bound
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 * allocation behaviour: Most allocs on one cpu, most free operations
3666 * on another cpu. For these cases, an efficient object passing between
3667 * cpus is necessary. This is provided by a shared array. The array
3668 * replaces Bonwick's magazine layer.
3669 * On uniprocessor, it's functionally equivalent (but less efficient)
3670 * to a larger limit. Thus disabled by default.
3671 */
3672 shared = 0;
3673#ifdef CONFIG_SMP
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003674 if (cachep->buffer_size <= PAGE_SIZE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 shared = 8;
3676#endif
3677
3678#if DEBUG
Andrew Mortona737b3e2006-03-22 00:08:11 -08003679 /*
3680 * With debugging enabled, large batchcount lead to excessively long
3681 * periods with disabled local interrupts. Limit the batchcount
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 */
3683 if (limit > 32)
3684 limit = 32;
3685#endif
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003686 err = do_tune_cpucache(cachep, limit, (limit + 1) / 2, shared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003687 if (err)
3688 printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003689 cachep->name, -err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690}
3691
Christoph Lameter1b552532006-03-22 00:09:07 -08003692/*
3693 * Drain an array if it contains any elements taking the l3 lock only if
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003694 * necessary. Note that the l3 listlock also protects the array_cache
3695 * if drain_array() is used on the shared array.
Christoph Lameter1b552532006-03-22 00:09:07 -08003696 */
3697void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3,
3698 struct array_cache *ac, int force, int node)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699{
3700 int tofree;
3701
Christoph Lameter1b552532006-03-22 00:09:07 -08003702 if (!ac || !ac->avail)
3703 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003704 if (ac->touched && !force) {
3705 ac->touched = 0;
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003706 } else {
Christoph Lameter1b552532006-03-22 00:09:07 -08003707 spin_lock_irq(&l3->list_lock);
Christoph Lameterb18e7e62006-03-22 00:09:07 -08003708 if (ac->avail) {
3709 tofree = force ? ac->avail : (ac->limit + 4) / 5;
3710 if (tofree > ac->avail)
3711 tofree = (ac->avail + 1) / 2;
3712 free_block(cachep, ac->entry, tofree, node);
3713 ac->avail -= tofree;
3714 memmove(ac->entry, &(ac->entry[tofree]),
3715 sizeof(void *) * ac->avail);
3716 }
Christoph Lameter1b552532006-03-22 00:09:07 -08003717 spin_unlock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 }
3719}
3720
3721/**
3722 * cache_reap - Reclaim memory from caches.
Randy Dunlap1e5d5332005-11-07 01:01:06 -08003723 * @unused: unused parameter
Linus Torvalds1da177e2005-04-16 15:20:36 -07003724 *
3725 * Called from workqueue/eventd every few seconds.
3726 * Purpose:
3727 * - clear the per-cpu caches for this CPU.
3728 * - return freeable pages to the main free memory pool.
3729 *
Andrew Mortona737b3e2006-03-22 00:08:11 -08003730 * If we cannot acquire the cache chain mutex then just give up - we'll try
3731 * again on the next iteration.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 */
3733static void cache_reap(void *unused)
3734{
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07003735 struct kmem_cache *searchp;
Christoph Lametere498be72005-09-09 13:03:32 -07003736 struct kmem_list3 *l3;
Christoph Lameteraab22072006-03-22 00:09:06 -08003737 int node = numa_node_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738
Ingo Molnarfc0abb12006-01-18 17:42:33 -08003739 if (!mutex_trylock(&cache_chain_mutex)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 /* Give up. Setup the next iteration. */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003741 schedule_delayed_work(&__get_cpu_var(reap_work),
3742 REAPTIMEOUT_CPUC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 return;
3744 }
3745
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07003746 list_for_each_entry(searchp, &cache_chain, next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747 check_irq_on();
3748
Christoph Lameter35386e32006-03-22 00:09:05 -08003749 /*
3750 * We only take the l3 lock if absolutely necessary and we
3751 * have established with reasonable certainty that
3752 * we can do some work if the lock was obtained.
3753 */
Christoph Lameteraab22072006-03-22 00:09:06 -08003754 l3 = searchp->nodelists[node];
Christoph Lameter35386e32006-03-22 00:09:05 -08003755
Christoph Lameter8fce4d82006-03-09 17:33:54 -08003756 reap_alien(searchp, l3);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
Christoph Lameteraab22072006-03-22 00:09:06 -08003758 drain_array(searchp, l3, cpu_cache_get(searchp), 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759
Christoph Lameter35386e32006-03-22 00:09:05 -08003760 /*
3761 * These are racy checks but it does not matter
3762 * if we skip one check or scan twice.
3763 */
Christoph Lametere498be72005-09-09 13:03:32 -07003764 if (time_after(l3->next_reap, jiffies))
Christoph Lameter35386e32006-03-22 00:09:05 -08003765 goto next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766
Christoph Lametere498be72005-09-09 13:03:32 -07003767 l3->next_reap = jiffies + REAPTIMEOUT_LIST3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768
Christoph Lameteraab22072006-03-22 00:09:06 -08003769 drain_array(searchp, l3, l3->shared, 0, node);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770
Christoph Lametered11d9e2006-06-30 01:55:45 -07003771 if (l3->free_touched)
Christoph Lametere498be72005-09-09 13:03:32 -07003772 l3->free_touched = 0;
Christoph Lametered11d9e2006-06-30 01:55:45 -07003773 else {
3774 int freed;
3775
3776 freed = drain_freelist(searchp, l3, (l3->free_limit +
3777 5 * searchp->num - 1) / (5 * searchp->num));
3778 STATS_ADD_REAPED(searchp, freed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 }
Christoph Lameter35386e32006-03-22 00:09:05 -08003780next:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003781 cond_resched();
3782 }
3783 check_irq_on();
Ingo Molnarfc0abb12006-01-18 17:42:33 -08003784 mutex_unlock(&cache_chain_mutex);
Christoph Lameter8fce4d82006-03-09 17:33:54 -08003785 next_reap_node();
Christoph Lameter2244b952006-06-30 01:55:33 -07003786 refresh_cpu_vm_stats(smp_processor_id());
Andrew Mortona737b3e2006-03-22 00:08:11 -08003787 /* Set up the next iteration */
Manfred Spraulcd61ef62005-11-07 00:58:02 -08003788 schedule_delayed_work(&__get_cpu_var(reap_work), REAPTIMEOUT_CPUC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789}
3790
3791#ifdef CONFIG_PROC_FS
3792
Pekka Enberg85289f92006-01-08 01:00:36 -08003793static void print_slabinfo_header(struct seq_file *m)
3794{
3795 /*
3796 * Output format version, so at least we can change it
3797 * without _too_ many complaints.
3798 */
3799#if STATS
3800 seq_puts(m, "slabinfo - version: 2.1 (statistics)\n");
3801#else
3802 seq_puts(m, "slabinfo - version: 2.1\n");
3803#endif
3804 seq_puts(m, "# name <active_objs> <num_objs> <objsize> "
3805 "<objperslab> <pagesperslab>");
3806 seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>");
3807 seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>");
3808#if STATS
3809 seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> "
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07003810 "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>");
Pekka Enberg85289f92006-01-08 01:00:36 -08003811 seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>");
3812#endif
3813 seq_putc(m, '\n');
3814}
3815
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816static void *s_start(struct seq_file *m, loff_t *pos)
3817{
3818 loff_t n = *pos;
3819 struct list_head *p;
3820
Ingo Molnarfc0abb12006-01-18 17:42:33 -08003821 mutex_lock(&cache_chain_mutex);
Pekka Enberg85289f92006-01-08 01:00:36 -08003822 if (!n)
3823 print_slabinfo_header(m);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824 p = cache_chain.next;
3825 while (n--) {
3826 p = p->next;
3827 if (p == &cache_chain)
3828 return NULL;
3829 }
Pekka Enberg343e0d72006-02-01 03:05:50 -08003830 return list_entry(p, struct kmem_cache, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831}
3832
3833static void *s_next(struct seq_file *m, void *p, loff_t *pos)
3834{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003835 struct kmem_cache *cachep = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003836 ++*pos;
Andrew Mortona737b3e2006-03-22 00:08:11 -08003837 return cachep->next.next == &cache_chain ?
3838 NULL : list_entry(cachep->next.next, struct kmem_cache, next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839}
3840
3841static void s_stop(struct seq_file *m, void *p)
3842{
Ingo Molnarfc0abb12006-01-18 17:42:33 -08003843 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844}
3845
3846static int s_show(struct seq_file *m, void *p)
3847{
Pekka Enberg343e0d72006-02-01 03:05:50 -08003848 struct kmem_cache *cachep = p;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003849 struct slab *slabp;
3850 unsigned long active_objs;
3851 unsigned long num_objs;
3852 unsigned long active_slabs = 0;
3853 unsigned long num_slabs, free_objects = 0, shared_avail = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07003854 const char *name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003855 char *error = NULL;
Christoph Lametere498be72005-09-09 13:03:32 -07003856 int node;
3857 struct kmem_list3 *l3;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003858
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 active_objs = 0;
3860 num_slabs = 0;
Christoph Lametere498be72005-09-09 13:03:32 -07003861 for_each_online_node(node) {
3862 l3 = cachep->nodelists[node];
3863 if (!l3)
3864 continue;
3865
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003866 check_irq_on();
3867 spin_lock_irq(&l3->list_lock);
Christoph Lametere498be72005-09-09 13:03:32 -07003868
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07003869 list_for_each_entry(slabp, &l3->slabs_full, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07003870 if (slabp->inuse != cachep->num && !error)
3871 error = "slabs_full accounting error";
3872 active_objs += cachep->num;
3873 active_slabs++;
3874 }
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07003875 list_for_each_entry(slabp, &l3->slabs_partial, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07003876 if (slabp->inuse == cachep->num && !error)
3877 error = "slabs_partial inuse accounting error";
3878 if (!slabp->inuse && !error)
3879 error = "slabs_partial/inuse accounting error";
3880 active_objs += slabp->inuse;
3881 active_slabs++;
3882 }
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07003883 list_for_each_entry(slabp, &l3->slabs_free, list) {
Christoph Lametere498be72005-09-09 13:03:32 -07003884 if (slabp->inuse && !error)
3885 error = "slabs_free/inuse accounting error";
3886 num_slabs++;
3887 }
3888 free_objects += l3->free_objects;
Ravikiran G Thirumalai4484ebf2006-02-04 23:27:59 -08003889 if (l3->shared)
3890 shared_avail += l3->shared->avail;
Christoph Lametere498be72005-09-09 13:03:32 -07003891
Ravikiran G Thirumalaica3b9b92006-02-04 23:27:58 -08003892 spin_unlock_irq(&l3->list_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003893 }
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003894 num_slabs += active_slabs;
3895 num_objs = num_slabs * cachep->num;
Christoph Lametere498be72005-09-09 13:03:32 -07003896 if (num_objs - active_objs != free_objects && !error)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003897 error = "free_objects accounting error";
3898
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003899 name = cachep->name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 if (error)
3901 printk(KERN_ERR "slab: cache %s error: %s\n", name, error);
3902
3903 seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d",
Manfred Spraul3dafccf2006-02-01 03:05:42 -08003904 name, active_objs, num_objs, cachep->buffer_size,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003905 cachep->num, (1 << cachep->gfporder));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 seq_printf(m, " : tunables %4u %4u %4u",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003907 cachep->limit, cachep->batchcount, cachep->shared);
Christoph Lametere498be72005-09-09 13:03:32 -07003908 seq_printf(m, " : slabdata %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003909 active_slabs, num_slabs, shared_avail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003910#if STATS
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003911 { /* list3 stats */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 unsigned long high = cachep->high_mark;
3913 unsigned long allocs = cachep->num_allocations;
3914 unsigned long grown = cachep->grown;
3915 unsigned long reaped = cachep->reaped;
3916 unsigned long errors = cachep->errors;
3917 unsigned long max_freeable = cachep->max_freeable;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 unsigned long node_allocs = cachep->node_allocs;
Christoph Lametere498be72005-09-09 13:03:32 -07003919 unsigned long node_frees = cachep->node_frees;
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07003920 unsigned long overflows = cachep->node_overflow;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921
Christoph Lametere498be72005-09-09 13:03:32 -07003922 seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu \
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07003923 %4lu %4lu %4lu %4lu %4lu", allocs, high, grown,
Andrew Mortona737b3e2006-03-22 00:08:11 -08003924 reaped, errors, max_freeable, node_allocs,
Ravikiran G Thirumalaifb7faf32006-04-10 22:52:54 -07003925 node_frees, overflows);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926 }
3927 /* cpu stats */
3928 {
3929 unsigned long allochit = atomic_read(&cachep->allochit);
3930 unsigned long allocmiss = atomic_read(&cachep->allocmiss);
3931 unsigned long freehit = atomic_read(&cachep->freehit);
3932 unsigned long freemiss = atomic_read(&cachep->freemiss);
3933
3934 seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu",
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003935 allochit, allocmiss, freehit, freemiss);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 }
3937#endif
3938 seq_putc(m, '\n');
Linus Torvalds1da177e2005-04-16 15:20:36 -07003939 return 0;
3940}
3941
3942/*
3943 * slabinfo_op - iterator that generates /proc/slabinfo
3944 *
3945 * Output layout:
3946 * cache-name
3947 * num-active-objs
3948 * total-objs
3949 * object size
3950 * num-active-slabs
3951 * total-slabs
3952 * num-pages-per-slab
3953 * + further values on SMP and with statistics enabled
3954 */
3955
3956struct seq_operations slabinfo_op = {
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003957 .start = s_start,
3958 .next = s_next,
3959 .stop = s_stop,
3960 .show = s_show,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961};
3962
3963#define MAX_SLABINFO_WRITE 128
3964/**
3965 * slabinfo_write - Tuning for the slab allocator
3966 * @file: unused
3967 * @buffer: user buffer
3968 * @count: data length
3969 * @ppos: unused
3970 */
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003971ssize_t slabinfo_write(struct file *file, const char __user * buffer,
3972 size_t count, loff_t *ppos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973{
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003974 char kbuf[MAX_SLABINFO_WRITE + 1], *tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975 int limit, batchcount, shared, res;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07003976 struct kmem_cache *cachep;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003977
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978 if (count > MAX_SLABINFO_WRITE)
3979 return -EINVAL;
3980 if (copy_from_user(&kbuf, buffer, count))
3981 return -EFAULT;
Pekka Enbergb28a02d2006-01-08 01:00:37 -08003982 kbuf[MAX_SLABINFO_WRITE] = '\0';
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983
3984 tmp = strchr(kbuf, ' ');
3985 if (!tmp)
3986 return -EINVAL;
3987 *tmp = '\0';
3988 tmp++;
3989 if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3)
3990 return -EINVAL;
3991
3992 /* Find the cache in the chain of caches. */
Ingo Molnarfc0abb12006-01-18 17:42:33 -08003993 mutex_lock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994 res = -EINVAL;
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07003995 list_for_each_entry(cachep, &cache_chain, next) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996 if (!strcmp(cachep->name, kbuf)) {
Andrew Mortona737b3e2006-03-22 00:08:11 -08003997 if (limit < 1 || batchcount < 1 ||
3998 batchcount > limit || shared < 0) {
Christoph Lametere498be72005-09-09 13:03:32 -07003999 res = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000 } else {
Christoph Lametere498be72005-09-09 13:03:32 -07004001 res = do_tune_cpucache(cachep, limit,
Pekka Enbergb28a02d2006-01-08 01:00:37 -08004002 batchcount, shared);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 }
4004 break;
4005 }
4006 }
Ingo Molnarfc0abb12006-01-18 17:42:33 -08004007 mutex_unlock(&cache_chain_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 if (res >= 0)
4009 res = count;
4010 return res;
4011}
Al Viro871751e2006-03-25 03:06:39 -08004012
4013#ifdef CONFIG_DEBUG_SLAB_LEAK
4014
4015static void *leaks_start(struct seq_file *m, loff_t *pos)
4016{
4017 loff_t n = *pos;
4018 struct list_head *p;
4019
4020 mutex_lock(&cache_chain_mutex);
4021 p = cache_chain.next;
4022 while (n--) {
4023 p = p->next;
4024 if (p == &cache_chain)
4025 return NULL;
4026 }
4027 return list_entry(p, struct kmem_cache, next);
4028}
4029
4030static inline int add_caller(unsigned long *n, unsigned long v)
4031{
4032 unsigned long *p;
4033 int l;
4034 if (!v)
4035 return 1;
4036 l = n[1];
4037 p = n + 2;
4038 while (l) {
4039 int i = l/2;
4040 unsigned long *q = p + 2 * i;
4041 if (*q == v) {
4042 q[1]++;
4043 return 1;
4044 }
4045 if (*q > v) {
4046 l = i;
4047 } else {
4048 p = q + 2;
4049 l -= i + 1;
4050 }
4051 }
4052 if (++n[1] == n[0])
4053 return 0;
4054 memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n));
4055 p[0] = v;
4056 p[1] = 1;
4057 return 1;
4058}
4059
4060static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s)
4061{
4062 void *p;
4063 int i;
4064 if (n[0] == n[1])
4065 return;
4066 for (i = 0, p = s->s_mem; i < c->num; i++, p += c->buffer_size) {
4067 if (slab_bufctl(s)[i] != BUFCTL_ACTIVE)
4068 continue;
4069 if (!add_caller(n, (unsigned long)*dbg_userword(c, p)))
4070 return;
4071 }
4072}
4073
4074static void show_symbol(struct seq_file *m, unsigned long address)
4075{
4076#ifdef CONFIG_KALLSYMS
4077 char *modname;
4078 const char *name;
4079 unsigned long offset, size;
4080 char namebuf[KSYM_NAME_LEN+1];
4081
4082 name = kallsyms_lookup(address, &size, &offset, &modname, namebuf);
4083
4084 if (name) {
4085 seq_printf(m, "%s+%#lx/%#lx", name, offset, size);
4086 if (modname)
4087 seq_printf(m, " [%s]", modname);
4088 return;
4089 }
4090#endif
4091 seq_printf(m, "%p", (void *)address);
4092}
4093
4094static int leaks_show(struct seq_file *m, void *p)
4095{
4096 struct kmem_cache *cachep = p;
Al Viro871751e2006-03-25 03:06:39 -08004097 struct slab *slabp;
4098 struct kmem_list3 *l3;
4099 const char *name;
4100 unsigned long *n = m->private;
4101 int node;
4102 int i;
4103
4104 if (!(cachep->flags & SLAB_STORE_USER))
4105 return 0;
4106 if (!(cachep->flags & SLAB_RED_ZONE))
4107 return 0;
4108
4109 /* OK, we can do it */
4110
4111 n[1] = 0;
4112
4113 for_each_online_node(node) {
4114 l3 = cachep->nodelists[node];
4115 if (!l3)
4116 continue;
4117
4118 check_irq_on();
4119 spin_lock_irq(&l3->list_lock);
4120
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004121 list_for_each_entry(slabp, &l3->slabs_full, list)
Al Viro871751e2006-03-25 03:06:39 -08004122 handle_slab(n, cachep, slabp);
Christoph Hellwig7a7c3812006-06-23 02:03:17 -07004123 list_for_each_entry(slabp, &l3->slabs_partial, list)
Al Viro871751e2006-03-25 03:06:39 -08004124 handle_slab(n, cachep, slabp);
Al Viro871751e2006-03-25 03:06:39 -08004125 spin_unlock_irq(&l3->list_lock);
4126 }
4127 name = cachep->name;
4128 if (n[0] == n[1]) {
4129 /* Increase the buffer size */
4130 mutex_unlock(&cache_chain_mutex);
4131 m->private = kzalloc(n[0] * 4 * sizeof(unsigned long), GFP_KERNEL);
4132 if (!m->private) {
4133 /* Too bad, we are really out */
4134 m->private = n;
4135 mutex_lock(&cache_chain_mutex);
4136 return -ENOMEM;
4137 }
4138 *(unsigned long *)m->private = n[0] * 2;
4139 kfree(n);
4140 mutex_lock(&cache_chain_mutex);
4141 /* Now make sure this entry will be retried */
4142 m->count = m->size;
4143 return 0;
4144 }
4145 for (i = 0; i < n[1]; i++) {
4146 seq_printf(m, "%s: %lu ", name, n[2*i+3]);
4147 show_symbol(m, n[2*i+2]);
4148 seq_putc(m, '\n');
4149 }
4150 return 0;
4151}
4152
4153struct seq_operations slabstats_op = {
4154 .start = leaks_start,
4155 .next = s_next,
4156 .stop = s_stop,
4157 .show = leaks_show,
4158};
4159#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160#endif
4161
Manfred Spraul00e145b2005-09-03 15:55:07 -07004162/**
4163 * ksize - get the actual amount of memory allocated for a given object
4164 * @objp: Pointer to the object
4165 *
4166 * kmalloc may internally round up allocations and return more memory
4167 * than requested. ksize() can be used to determine the actual amount of
4168 * memory allocated. The caller may use this additional memory, even though
4169 * a smaller amount of memory was initially specified with the kmalloc call.
4170 * The caller must guarantee that objp points to a valid object previously
4171 * allocated with either kmalloc() or kmem_cache_alloc(). The object
4172 * must not be freed during the duration of the call.
4173 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174unsigned int ksize(const void *objp)
4175{
Manfred Spraul00e145b2005-09-03 15:55:07 -07004176 if (unlikely(objp == NULL))
4177 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178
Pekka Enberg6ed5eb22006-02-01 03:05:49 -08004179 return obj_size(virt_to_cache(objp));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180}