]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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 | * | |
a737b3e2 | 53 | * The c_cpuarray may not be read with enabled local interrupts - |
1da177e4 LT |
54 | * it's changed with a smp_call_function(). |
55 | * | |
56 | * SMP synchronization: | |
57 | * constructors and destructors are called without any locking. | |
343e0d7a | 58 | * Several members in struct kmem_cache and struct slab never change, they |
1da177e4 LT |
59 | * 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 | |
fc0abb14 | 71 | * The global cache-chain is protected by the mutex 'cache_chain_mutex'. |
1da177e4 LT |
72 | * 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 | * | |
e498be7d CL |
78 | * 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. | |
1da177e4 LT |
87 | */ |
88 | ||
1da177e4 LT |
89 | #include <linux/slab.h> |
90 | #include <linux/mm.h> | |
c9cf5528 | 91 | #include <linux/poison.h> |
1da177e4 LT |
92 | #include <linux/swap.h> |
93 | #include <linux/cache.h> | |
94 | #include <linux/interrupt.h> | |
95 | #include <linux/init.h> | |
96 | #include <linux/compiler.h> | |
101a5001 | 97 | #include <linux/cpuset.h> |
1da177e4 LT |
98 | #include <linux/seq_file.h> |
99 | #include <linux/notifier.h> | |
100 | #include <linux/kallsyms.h> | |
101 | #include <linux/cpu.h> | |
102 | #include <linux/sysctl.h> | |
103 | #include <linux/module.h> | |
104 | #include <linux/rcupdate.h> | |
543537bd | 105 | #include <linux/string.h> |
e498be7d | 106 | #include <linux/nodemask.h> |
dc85da15 | 107 | #include <linux/mempolicy.h> |
fc0abb14 | 108 | #include <linux/mutex.h> |
e7eebaf6 | 109 | #include <linux/rtmutex.h> |
1da177e4 LT |
110 | |
111 | #include <asm/uaccess.h> | |
112 | #include <asm/cacheflush.h> | |
113 | #include <asm/tlbflush.h> | |
114 | #include <asm/page.h> | |
115 | ||
116 | /* | |
117 | * DEBUG - 1 for kmem_cache_create() to honour; SLAB_DEBUG_INITIAL, | |
118 | * SLAB_RED_ZONE & SLAB_POISON. | |
119 | * 0 for faster, smaller code (especially in the critical paths). | |
120 | * | |
121 | * STATS - 1 to collect stats for /proc/slabinfo. | |
122 | * 0 for faster, smaller code (especially in the critical paths). | |
123 | * | |
124 | * FORCED_DEBUG - 1 enables SLAB_RED_ZONE and SLAB_POISON (if possible) | |
125 | */ | |
126 | ||
127 | #ifdef CONFIG_DEBUG_SLAB | |
128 | #define DEBUG 1 | |
129 | #define STATS 1 | |
130 | #define FORCED_DEBUG 1 | |
131 | #else | |
132 | #define DEBUG 0 | |
133 | #define STATS 0 | |
134 | #define FORCED_DEBUG 0 | |
135 | #endif | |
136 | ||
1da177e4 LT |
137 | /* Shouldn't this be in a header file somewhere? */ |
138 | #define BYTES_PER_WORD sizeof(void *) | |
139 | ||
140 | #ifndef cache_line_size | |
141 | #define cache_line_size() L1_CACHE_BYTES | |
142 | #endif | |
143 | ||
144 | #ifndef ARCH_KMALLOC_MINALIGN | |
145 | /* | |
146 | * Enforce a minimum alignment for the kmalloc caches. | |
147 | * Usually, the kmalloc caches are cache_line_size() aligned, except when | |
148 | * DEBUG and FORCED_DEBUG are enabled, then they are BYTES_PER_WORD aligned. | |
149 | * Some archs want to perform DMA into kmalloc caches and need a guaranteed | |
150 | * alignment larger than BYTES_PER_WORD. ARCH_KMALLOC_MINALIGN allows that. | |
151 | * Note that this flag disables some debug features. | |
152 | */ | |
153 | #define ARCH_KMALLOC_MINALIGN 0 | |
154 | #endif | |
155 | ||
156 | #ifndef ARCH_SLAB_MINALIGN | |
157 | /* | |
158 | * Enforce a minimum alignment for all caches. | |
159 | * Intended for archs that get misalignment faults even for BYTES_PER_WORD | |
160 | * aligned buffers. Includes ARCH_KMALLOC_MINALIGN. | |
161 | * If possible: Do not enable this flag for CONFIG_DEBUG_SLAB, it disables | |
162 | * some debug features. | |
163 | */ | |
164 | #define ARCH_SLAB_MINALIGN 0 | |
165 | #endif | |
166 | ||
167 | #ifndef ARCH_KMALLOC_FLAGS | |
168 | #define ARCH_KMALLOC_FLAGS SLAB_HWCACHE_ALIGN | |
169 | #endif | |
170 | ||
171 | /* Legal flag mask for kmem_cache_create(). */ | |
172 | #if DEBUG | |
173 | # define CREATE_MASK (SLAB_DEBUG_INITIAL | SLAB_RED_ZONE | \ | |
174 | SLAB_POISON | SLAB_HWCACHE_ALIGN | \ | |
ac2b898c | 175 | SLAB_CACHE_DMA | \ |
1da177e4 LT |
176 | SLAB_MUST_HWCACHE_ALIGN | SLAB_STORE_USER | \ |
177 | SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \ | |
101a5001 | 178 | SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD) |
1da177e4 | 179 | #else |
ac2b898c | 180 | # define CREATE_MASK (SLAB_HWCACHE_ALIGN | \ |
1da177e4 LT |
181 | SLAB_CACHE_DMA | SLAB_MUST_HWCACHE_ALIGN | \ |
182 | SLAB_RECLAIM_ACCOUNT | SLAB_PANIC | \ | |
101a5001 | 183 | SLAB_DESTROY_BY_RCU | SLAB_MEM_SPREAD) |
1da177e4 LT |
184 | #endif |
185 | ||
186 | /* | |
187 | * kmem_bufctl_t: | |
188 | * | |
189 | * Bufctl's are used for linking objs within a slab | |
190 | * linked offsets. | |
191 | * | |
192 | * This implementation relies on "struct page" for locating the cache & | |
193 | * slab an object belongs to. | |
194 | * This allows the bufctl structure to be small (one int), but limits | |
195 | * the number of objects a slab (not a cache) can contain when off-slab | |
196 | * bufctls are used. The limit is the size of the largest general cache | |
197 | * that does not use off-slab slabs. | |
198 | * For 32bit archs with 4 kB pages, is this 56. | |
199 | * This is not serious, as it is only for large objects, when it is unwise | |
200 | * to have too many per slab. | |
201 | * Note: This limit can be raised by introducing a general cache whose size | |
202 | * is less than 512 (PAGE_SIZE<<3), but greater than 256. | |
203 | */ | |
204 | ||
fa5b08d5 | 205 | typedef unsigned int kmem_bufctl_t; |
1da177e4 LT |
206 | #define BUFCTL_END (((kmem_bufctl_t)(~0U))-0) |
207 | #define BUFCTL_FREE (((kmem_bufctl_t)(~0U))-1) | |
871751e2 AV |
208 | #define BUFCTL_ACTIVE (((kmem_bufctl_t)(~0U))-2) |
209 | #define SLAB_LIMIT (((kmem_bufctl_t)(~0U))-3) | |
1da177e4 | 210 | |
1da177e4 LT |
211 | /* |
212 | * struct slab | |
213 | * | |
214 | * Manages the objs in a slab. Placed either at the beginning of mem allocated | |
215 | * for a slab, or allocated from an general cache. | |
216 | * Slabs are chained into three list: fully used, partial, fully free slabs. | |
217 | */ | |
218 | struct slab { | |
b28a02de PE |
219 | struct list_head list; |
220 | unsigned long colouroff; | |
221 | void *s_mem; /* including colour offset */ | |
222 | unsigned int inuse; /* num of objs active in slab */ | |
223 | kmem_bufctl_t free; | |
224 | unsigned short nodeid; | |
1da177e4 LT |
225 | }; |
226 | ||
227 | /* | |
228 | * struct slab_rcu | |
229 | * | |
230 | * slab_destroy on a SLAB_DESTROY_BY_RCU cache uses this structure to | |
231 | * arrange for kmem_freepages to be called via RCU. This is useful if | |
232 | * we need to approach a kernel structure obliquely, from its address | |
233 | * obtained without the usual locking. We can lock the structure to | |
234 | * stabilize it and check it's still at the given address, only if we | |
235 | * can be sure that the memory has not been meanwhile reused for some | |
236 | * other kind of object (which our subsystem's lock might corrupt). | |
237 | * | |
238 | * rcu_read_lock before reading the address, then rcu_read_unlock after | |
239 | * taking the spinlock within the structure expected at that address. | |
240 | * | |
241 | * We assume struct slab_rcu can overlay struct slab when destroying. | |
242 | */ | |
243 | struct slab_rcu { | |
b28a02de | 244 | struct rcu_head head; |
343e0d7a | 245 | struct kmem_cache *cachep; |
b28a02de | 246 | void *addr; |
1da177e4 LT |
247 | }; |
248 | ||
249 | /* | |
250 | * struct array_cache | |
251 | * | |
1da177e4 LT |
252 | * Purpose: |
253 | * - LIFO ordering, to hand out cache-warm objects from _alloc | |
254 | * - reduce the number of linked list operations | |
255 | * - reduce spinlock operations | |
256 | * | |
257 | * The limit is stored in the per-cpu structure to reduce the data cache | |
258 | * footprint. | |
259 | * | |
260 | */ | |
261 | struct array_cache { | |
262 | unsigned int avail; | |
263 | unsigned int limit; | |
264 | unsigned int batchcount; | |
265 | unsigned int touched; | |
e498be7d | 266 | spinlock_t lock; |
a737b3e2 AM |
267 | void *entry[0]; /* |
268 | * Must have this definition in here for the proper | |
269 | * alignment of array_cache. Also simplifies accessing | |
270 | * the entries. | |
271 | * [0] is for gcc 2.95. It should really be []. | |
272 | */ | |
1da177e4 LT |
273 | }; |
274 | ||
a737b3e2 AM |
275 | /* |
276 | * bootstrap: The caches do not work without cpuarrays anymore, but the | |
277 | * cpuarrays are allocated from the generic caches... | |
1da177e4 LT |
278 | */ |
279 | #define BOOT_CPUCACHE_ENTRIES 1 | |
280 | struct arraycache_init { | |
281 | struct array_cache cache; | |
b28a02de | 282 | void *entries[BOOT_CPUCACHE_ENTRIES]; |
1da177e4 LT |
283 | }; |
284 | ||
285 | /* | |
e498be7d | 286 | * The slab lists for all objects. |
1da177e4 LT |
287 | */ |
288 | struct kmem_list3 { | |
b28a02de PE |
289 | struct list_head slabs_partial; /* partial list first, better asm code */ |
290 | struct list_head slabs_full; | |
291 | struct list_head slabs_free; | |
292 | unsigned long free_objects; | |
b28a02de | 293 | unsigned int free_limit; |
2e1217cf | 294 | unsigned int colour_next; /* Per-node cache coloring */ |
b28a02de PE |
295 | spinlock_t list_lock; |
296 | struct array_cache *shared; /* shared per node */ | |
297 | struct array_cache **alien; /* on other nodes */ | |
35386e3b CL |
298 | unsigned long next_reap; /* updated without locking */ |
299 | int free_touched; /* updated without locking */ | |
1da177e4 LT |
300 | }; |
301 | ||
e498be7d CL |
302 | /* |
303 | * Need this for bootstrapping a per node allocator. | |
304 | */ | |
305 | #define NUM_INIT_LISTS (2 * MAX_NUMNODES + 1) | |
306 | struct kmem_list3 __initdata initkmem_list3[NUM_INIT_LISTS]; | |
307 | #define CACHE_CACHE 0 | |
308 | #define SIZE_AC 1 | |
309 | #define SIZE_L3 (1 + MAX_NUMNODES) | |
310 | ||
ed11d9eb CL |
311 | static int drain_freelist(struct kmem_cache *cache, |
312 | struct kmem_list3 *l3, int tofree); | |
313 | static void free_block(struct kmem_cache *cachep, void **objpp, int len, | |
314 | int node); | |
2ed3a4ef | 315 | static int enable_cpucache(struct kmem_cache *cachep); |
65f27f38 | 316 | static void cache_reap(struct work_struct *unused); |
ed11d9eb | 317 | |
e498be7d | 318 | /* |
a737b3e2 AM |
319 | * This function must be completely optimized away if a constant is passed to |
320 | * it. Mostly the same as what is in linux/slab.h except it returns an index. | |
e498be7d | 321 | */ |
7243cc05 | 322 | static __always_inline int index_of(const size_t size) |
e498be7d | 323 | { |
5ec8a847 SR |
324 | extern void __bad_size(void); |
325 | ||
e498be7d CL |
326 | if (__builtin_constant_p(size)) { |
327 | int i = 0; | |
328 | ||
329 | #define CACHE(x) \ | |
330 | if (size <=x) \ | |
331 | return i; \ | |
332 | else \ | |
333 | i++; | |
334 | #include "linux/kmalloc_sizes.h" | |
335 | #undef CACHE | |
5ec8a847 | 336 | __bad_size(); |
7243cc05 | 337 | } else |
5ec8a847 | 338 | __bad_size(); |
e498be7d CL |
339 | return 0; |
340 | } | |
341 | ||
e0a42726 IM |
342 | static int slab_early_init = 1; |
343 | ||
e498be7d CL |
344 | #define INDEX_AC index_of(sizeof(struct arraycache_init)) |
345 | #define INDEX_L3 index_of(sizeof(struct kmem_list3)) | |
1da177e4 | 346 | |
5295a74c | 347 | static void kmem_list3_init(struct kmem_list3 *parent) |
e498be7d CL |
348 | { |
349 | INIT_LIST_HEAD(&parent->slabs_full); | |
350 | INIT_LIST_HEAD(&parent->slabs_partial); | |
351 | INIT_LIST_HEAD(&parent->slabs_free); | |
352 | parent->shared = NULL; | |
353 | parent->alien = NULL; | |
2e1217cf | 354 | parent->colour_next = 0; |
e498be7d CL |
355 | spin_lock_init(&parent->list_lock); |
356 | parent->free_objects = 0; | |
357 | parent->free_touched = 0; | |
358 | } | |
359 | ||
a737b3e2 AM |
360 | #define MAKE_LIST(cachep, listp, slab, nodeid) \ |
361 | do { \ | |
362 | INIT_LIST_HEAD(listp); \ | |
363 | list_splice(&(cachep->nodelists[nodeid]->slab), listp); \ | |
e498be7d CL |
364 | } while (0) |
365 | ||
a737b3e2 AM |
366 | #define MAKE_ALL_LISTS(cachep, ptr, nodeid) \ |
367 | do { \ | |
e498be7d CL |
368 | MAKE_LIST((cachep), (&(ptr)->slabs_full), slabs_full, nodeid); \ |
369 | MAKE_LIST((cachep), (&(ptr)->slabs_partial), slabs_partial, nodeid); \ | |
370 | MAKE_LIST((cachep), (&(ptr)->slabs_free), slabs_free, nodeid); \ | |
371 | } while (0) | |
1da177e4 LT |
372 | |
373 | /* | |
343e0d7a | 374 | * struct kmem_cache |
1da177e4 LT |
375 | * |
376 | * manages a cache. | |
377 | */ | |
b28a02de | 378 | |
2109a2d1 | 379 | struct kmem_cache { |
1da177e4 | 380 | /* 1) per-cpu data, touched during every alloc/free */ |
b28a02de | 381 | struct array_cache *array[NR_CPUS]; |
b5d8ca7c | 382 | /* 2) Cache tunables. Protected by cache_chain_mutex */ |
b28a02de PE |
383 | unsigned int batchcount; |
384 | unsigned int limit; | |
385 | unsigned int shared; | |
b5d8ca7c | 386 | |
3dafccf2 | 387 | unsigned int buffer_size; |
b5d8ca7c | 388 | /* 3) touched by every alloc & free from the backend */ |
b28a02de | 389 | struct kmem_list3 *nodelists[MAX_NUMNODES]; |
b5d8ca7c | 390 | |
a737b3e2 AM |
391 | unsigned int flags; /* constant flags */ |
392 | unsigned int num; /* # of objs per slab */ | |
1da177e4 | 393 | |
b5d8ca7c | 394 | /* 4) cache_grow/shrink */ |
1da177e4 | 395 | /* order of pgs per slab (2^n) */ |
b28a02de | 396 | unsigned int gfporder; |
1da177e4 LT |
397 | |
398 | /* force GFP flags, e.g. GFP_DMA */ | |
b28a02de | 399 | gfp_t gfpflags; |
1da177e4 | 400 | |
a737b3e2 | 401 | size_t colour; /* cache colouring range */ |
b28a02de | 402 | unsigned int colour_off; /* colour offset */ |
343e0d7a | 403 | struct kmem_cache *slabp_cache; |
b28a02de | 404 | unsigned int slab_size; |
a737b3e2 | 405 | unsigned int dflags; /* dynamic flags */ |
1da177e4 LT |
406 | |
407 | /* constructor func */ | |
343e0d7a | 408 | void (*ctor) (void *, struct kmem_cache *, unsigned long); |
1da177e4 LT |
409 | |
410 | /* de-constructor func */ | |
343e0d7a | 411 | void (*dtor) (void *, struct kmem_cache *, unsigned long); |
1da177e4 | 412 | |
b5d8ca7c | 413 | /* 5) cache creation/removal */ |
b28a02de PE |
414 | const char *name; |
415 | struct list_head next; | |
1da177e4 | 416 | |
b5d8ca7c | 417 | /* 6) statistics */ |
1da177e4 | 418 | #if STATS |
b28a02de PE |
419 | unsigned long num_active; |
420 | unsigned long num_allocations; | |
421 | unsigned long high_mark; | |
422 | unsigned long grown; | |
423 | unsigned long reaped; | |
424 | unsigned long errors; | |
425 | unsigned long max_freeable; | |
426 | unsigned long node_allocs; | |
427 | unsigned long node_frees; | |
fb7faf33 | 428 | unsigned long node_overflow; |
b28a02de PE |
429 | atomic_t allochit; |
430 | atomic_t allocmiss; | |
431 | atomic_t freehit; | |
432 | atomic_t freemiss; | |
1da177e4 LT |
433 | #endif |
434 | #if DEBUG | |
3dafccf2 MS |
435 | /* |
436 | * If debugging is enabled, then the allocator can add additional | |
437 | * fields and/or padding to every object. buffer_size contains the total | |
438 | * object size including these internal fields, the following two | |
439 | * variables contain the offset to the user object and its size. | |
440 | */ | |
441 | int obj_offset; | |
442 | int obj_size; | |
1da177e4 LT |
443 | #endif |
444 | }; | |
445 | ||
446 | #define CFLGS_OFF_SLAB (0x80000000UL) | |
447 | #define OFF_SLAB(x) ((x)->flags & CFLGS_OFF_SLAB) | |
448 | ||
449 | #define BATCHREFILL_LIMIT 16 | |
a737b3e2 AM |
450 | /* |
451 | * Optimization question: fewer reaps means less probability for unnessary | |
452 | * cpucache drain/refill cycles. | |
1da177e4 | 453 | * |
dc6f3f27 | 454 | * OTOH the cpuarrays can contain lots of objects, |
1da177e4 LT |
455 | * which could lock up otherwise freeable slabs. |
456 | */ | |
457 | #define REAPTIMEOUT_CPUC (2*HZ) | |
458 | #define REAPTIMEOUT_LIST3 (4*HZ) | |
459 | ||
460 | #if STATS | |
461 | #define STATS_INC_ACTIVE(x) ((x)->num_active++) | |
462 | #define STATS_DEC_ACTIVE(x) ((x)->num_active--) | |
463 | #define STATS_INC_ALLOCED(x) ((x)->num_allocations++) | |
464 | #define STATS_INC_GROWN(x) ((x)->grown++) | |
ed11d9eb | 465 | #define STATS_ADD_REAPED(x,y) ((x)->reaped += (y)) |
a737b3e2 AM |
466 | #define STATS_SET_HIGH(x) \ |
467 | do { \ | |
468 | if ((x)->num_active > (x)->high_mark) \ | |
469 | (x)->high_mark = (x)->num_active; \ | |
470 | } while (0) | |
1da177e4 LT |
471 | #define STATS_INC_ERR(x) ((x)->errors++) |
472 | #define STATS_INC_NODEALLOCS(x) ((x)->node_allocs++) | |
e498be7d | 473 | #define STATS_INC_NODEFREES(x) ((x)->node_frees++) |
fb7faf33 | 474 | #define STATS_INC_ACOVERFLOW(x) ((x)->node_overflow++) |
a737b3e2 AM |
475 | #define STATS_SET_FREEABLE(x, i) \ |
476 | do { \ | |
477 | if ((x)->max_freeable < i) \ | |
478 | (x)->max_freeable = i; \ | |
479 | } while (0) | |
1da177e4 LT |
480 | #define STATS_INC_ALLOCHIT(x) atomic_inc(&(x)->allochit) |
481 | #define STATS_INC_ALLOCMISS(x) atomic_inc(&(x)->allocmiss) | |
482 | #define STATS_INC_FREEHIT(x) atomic_inc(&(x)->freehit) | |
483 | #define STATS_INC_FREEMISS(x) atomic_inc(&(x)->freemiss) | |
484 | #else | |
485 | #define STATS_INC_ACTIVE(x) do { } while (0) | |
486 | #define STATS_DEC_ACTIVE(x) do { } while (0) | |
487 | #define STATS_INC_ALLOCED(x) do { } while (0) | |
488 | #define STATS_INC_GROWN(x) do { } while (0) | |
ed11d9eb | 489 | #define STATS_ADD_REAPED(x,y) do { } while (0) |
1da177e4 LT |
490 | #define STATS_SET_HIGH(x) do { } while (0) |
491 | #define STATS_INC_ERR(x) do { } while (0) | |
492 | #define STATS_INC_NODEALLOCS(x) do { } while (0) | |
e498be7d | 493 | #define STATS_INC_NODEFREES(x) do { } while (0) |
fb7faf33 | 494 | #define STATS_INC_ACOVERFLOW(x) do { } while (0) |
a737b3e2 | 495 | #define STATS_SET_FREEABLE(x, i) do { } while (0) |
1da177e4 LT |
496 | #define STATS_INC_ALLOCHIT(x) do { } while (0) |
497 | #define STATS_INC_ALLOCMISS(x) do { } while (0) | |
498 | #define STATS_INC_FREEHIT(x) do { } while (0) | |
499 | #define STATS_INC_FREEMISS(x) do { } while (0) | |
500 | #endif | |
501 | ||
502 | #if DEBUG | |
1da177e4 | 503 | |
a737b3e2 AM |
504 | /* |
505 | * memory layout of objects: | |
1da177e4 | 506 | * 0 : objp |
3dafccf2 | 507 | * 0 .. cachep->obj_offset - BYTES_PER_WORD - 1: padding. This ensures that |
1da177e4 LT |
508 | * the end of an object is aligned with the end of the real |
509 | * allocation. Catches writes behind the end of the allocation. | |
3dafccf2 | 510 | * cachep->obj_offset - BYTES_PER_WORD .. cachep->obj_offset - 1: |
1da177e4 | 511 | * redzone word. |
3dafccf2 MS |
512 | * cachep->obj_offset: The real object. |
513 | * cachep->buffer_size - 2* BYTES_PER_WORD: redzone word [BYTES_PER_WORD long] | |
a737b3e2 AM |
514 | * cachep->buffer_size - 1* BYTES_PER_WORD: last caller address |
515 | * [BYTES_PER_WORD long] | |
1da177e4 | 516 | */ |
343e0d7a | 517 | static int obj_offset(struct kmem_cache *cachep) |
1da177e4 | 518 | { |
3dafccf2 | 519 | return cachep->obj_offset; |
1da177e4 LT |
520 | } |
521 | ||
343e0d7a | 522 | static int obj_size(struct kmem_cache *cachep) |
1da177e4 | 523 | { |
3dafccf2 | 524 | return cachep->obj_size; |
1da177e4 LT |
525 | } |
526 | ||
343e0d7a | 527 | static unsigned long *dbg_redzone1(struct kmem_cache *cachep, void *objp) |
1da177e4 LT |
528 | { |
529 | BUG_ON(!(cachep->flags & SLAB_RED_ZONE)); | |
3dafccf2 | 530 | return (unsigned long*) (objp+obj_offset(cachep)-BYTES_PER_WORD); |
1da177e4 LT |
531 | } |
532 | ||
343e0d7a | 533 | static unsigned long *dbg_redzone2(struct kmem_cache *cachep, void *objp) |
1da177e4 LT |
534 | { |
535 | BUG_ON(!(cachep->flags & SLAB_RED_ZONE)); | |
536 | if (cachep->flags & SLAB_STORE_USER) | |
3dafccf2 | 537 | return (unsigned long *)(objp + cachep->buffer_size - |
b28a02de | 538 | 2 * BYTES_PER_WORD); |
3dafccf2 | 539 | return (unsigned long *)(objp + cachep->buffer_size - BYTES_PER_WORD); |
1da177e4 LT |
540 | } |
541 | ||
343e0d7a | 542 | static void **dbg_userword(struct kmem_cache *cachep, void *objp) |
1da177e4 LT |
543 | { |
544 | BUG_ON(!(cachep->flags & SLAB_STORE_USER)); | |
3dafccf2 | 545 | return (void **)(objp + cachep->buffer_size - BYTES_PER_WORD); |
1da177e4 LT |
546 | } |
547 | ||
548 | #else | |
549 | ||
3dafccf2 MS |
550 | #define obj_offset(x) 0 |
551 | #define obj_size(cachep) (cachep->buffer_size) | |
1da177e4 LT |
552 | #define dbg_redzone1(cachep, objp) ({BUG(); (unsigned long *)NULL;}) |
553 | #define dbg_redzone2(cachep, objp) ({BUG(); (unsigned long *)NULL;}) | |
554 | #define dbg_userword(cachep, objp) ({BUG(); (void **)NULL;}) | |
555 | ||
556 | #endif | |
557 | ||
558 | /* | |
a737b3e2 AM |
559 | * Maximum size of an obj (in 2^order pages) and absolute limit for the gfp |
560 | * order. | |
1da177e4 LT |
561 | */ |
562 | #if defined(CONFIG_LARGE_ALLOCS) | |
563 | #define MAX_OBJ_ORDER 13 /* up to 32Mb */ | |
564 | #define MAX_GFP_ORDER 13 /* up to 32Mb */ | |
565 | #elif defined(CONFIG_MMU) | |
566 | #define MAX_OBJ_ORDER 5 /* 32 pages */ | |
567 | #define MAX_GFP_ORDER 5 /* 32 pages */ | |
568 | #else | |
569 | #define MAX_OBJ_ORDER 8 /* up to 1Mb */ | |
570 | #define MAX_GFP_ORDER 8 /* up to 1Mb */ | |
571 | #endif | |
572 | ||
573 | /* | |
574 | * Do not go above this order unless 0 objects fit into the slab. | |
575 | */ | |
576 | #define BREAK_GFP_ORDER_HI 1 | |
577 | #define BREAK_GFP_ORDER_LO 0 | |
578 | static int slab_break_gfp_order = BREAK_GFP_ORDER_LO; | |
579 | ||
a737b3e2 AM |
580 | /* |
581 | * Functions for storing/retrieving the cachep and or slab from the page | |
582 | * allocator. These are used to find the slab an obj belongs to. With kfree(), | |
583 | * these are used to find the cache which an obj belongs to. | |
1da177e4 | 584 | */ |
065d41cb PE |
585 | static inline void page_set_cache(struct page *page, struct kmem_cache *cache) |
586 | { | |
587 | page->lru.next = (struct list_head *)cache; | |
588 | } | |
589 | ||
590 | static inline struct kmem_cache *page_get_cache(struct page *page) | |
591 | { | |
84097518 NP |
592 | if (unlikely(PageCompound(page))) |
593 | page = (struct page *)page_private(page); | |
ddc2e812 | 594 | BUG_ON(!PageSlab(page)); |
065d41cb PE |
595 | return (struct kmem_cache *)page->lru.next; |
596 | } | |
597 | ||
598 | static inline void page_set_slab(struct page *page, struct slab *slab) | |
599 | { | |
600 | page->lru.prev = (struct list_head *)slab; | |
601 | } | |
602 | ||
603 | static inline struct slab *page_get_slab(struct page *page) | |
604 | { | |
84097518 NP |
605 | if (unlikely(PageCompound(page))) |
606 | page = (struct page *)page_private(page); | |
ddc2e812 | 607 | BUG_ON(!PageSlab(page)); |
065d41cb PE |
608 | return (struct slab *)page->lru.prev; |
609 | } | |
1da177e4 | 610 | |
6ed5eb22 PE |
611 | static inline struct kmem_cache *virt_to_cache(const void *obj) |
612 | { | |
613 | struct page *page = virt_to_page(obj); | |
614 | return page_get_cache(page); | |
615 | } | |
616 | ||
617 | static inline struct slab *virt_to_slab(const void *obj) | |
618 | { | |
619 | struct page *page = virt_to_page(obj); | |
620 | return page_get_slab(page); | |
621 | } | |
622 | ||
8fea4e96 PE |
623 | static inline void *index_to_obj(struct kmem_cache *cache, struct slab *slab, |
624 | unsigned int idx) | |
625 | { | |
626 | return slab->s_mem + cache->buffer_size * idx; | |
627 | } | |
628 | ||
629 | static inline unsigned int obj_to_index(struct kmem_cache *cache, | |
630 | struct slab *slab, void *obj) | |
631 | { | |
632 | return (unsigned)(obj - slab->s_mem) / cache->buffer_size; | |
633 | } | |
634 | ||
a737b3e2 AM |
635 | /* |
636 | * These are the default caches for kmalloc. Custom caches can have other sizes. | |
637 | */ | |
1da177e4 LT |
638 | struct cache_sizes malloc_sizes[] = { |
639 | #define CACHE(x) { .cs_size = (x) }, | |
640 | #include <linux/kmalloc_sizes.h> | |
641 | CACHE(ULONG_MAX) | |
642 | #undef CACHE | |
643 | }; | |
644 | EXPORT_SYMBOL(malloc_sizes); | |
645 | ||
646 | /* Must match cache_sizes above. Out of line to keep cache footprint low. */ | |
647 | struct cache_names { | |
648 | char *name; | |
649 | char *name_dma; | |
650 | }; | |
651 | ||
652 | static struct cache_names __initdata cache_names[] = { | |
653 | #define CACHE(x) { .name = "size-" #x, .name_dma = "size-" #x "(DMA)" }, | |
654 | #include <linux/kmalloc_sizes.h> | |
b28a02de | 655 | {NULL,} |
1da177e4 LT |
656 | #undef CACHE |
657 | }; | |
658 | ||
659 | static struct arraycache_init initarray_cache __initdata = | |
b28a02de | 660 | { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} }; |
1da177e4 | 661 | static struct arraycache_init initarray_generic = |
b28a02de | 662 | { {0, BOOT_CPUCACHE_ENTRIES, 1, 0} }; |
1da177e4 LT |
663 | |
664 | /* internal cache of cache description objs */ | |
343e0d7a | 665 | static struct kmem_cache cache_cache = { |
b28a02de PE |
666 | .batchcount = 1, |
667 | .limit = BOOT_CPUCACHE_ENTRIES, | |
668 | .shared = 1, | |
343e0d7a | 669 | .buffer_size = sizeof(struct kmem_cache), |
b28a02de | 670 | .name = "kmem_cache", |
1da177e4 | 671 | #if DEBUG |
343e0d7a | 672 | .obj_size = sizeof(struct kmem_cache), |
1da177e4 LT |
673 | #endif |
674 | }; | |
675 | ||
056c6241 RT |
676 | #define BAD_ALIEN_MAGIC 0x01020304ul |
677 | ||
f1aaee53 AV |
678 | #ifdef CONFIG_LOCKDEP |
679 | ||
680 | /* | |
681 | * Slab sometimes uses the kmalloc slabs to store the slab headers | |
682 | * for other slabs "off slab". | |
683 | * The locking for this is tricky in that it nests within the locks | |
684 | * of all other slabs in a few places; to deal with this special | |
685 | * locking we put on-slab caches into a separate lock-class. | |
056c6241 RT |
686 | * |
687 | * We set lock class for alien array caches which are up during init. | |
688 | * The lock annotation will be lost if all cpus of a node goes down and | |
689 | * then comes back up during hotplug | |
f1aaee53 | 690 | */ |
056c6241 RT |
691 | static struct lock_class_key on_slab_l3_key; |
692 | static struct lock_class_key on_slab_alc_key; | |
693 | ||
694 | static inline void init_lock_keys(void) | |
f1aaee53 | 695 | |
f1aaee53 AV |
696 | { |
697 | int q; | |
056c6241 RT |
698 | struct cache_sizes *s = malloc_sizes; |
699 | ||
700 | while (s->cs_size != ULONG_MAX) { | |
701 | for_each_node(q) { | |
702 | struct array_cache **alc; | |
703 | int r; | |
704 | struct kmem_list3 *l3 = s->cs_cachep->nodelists[q]; | |
705 | if (!l3 || OFF_SLAB(s->cs_cachep)) | |
706 | continue; | |
707 | lockdep_set_class(&l3->list_lock, &on_slab_l3_key); | |
708 | alc = l3->alien; | |
709 | /* | |
710 | * FIXME: This check for BAD_ALIEN_MAGIC | |
711 | * should go away when common slab code is taught to | |
712 | * work even without alien caches. | |
713 | * Currently, non NUMA code returns BAD_ALIEN_MAGIC | |
714 | * for alloc_alien_cache, | |
715 | */ | |
716 | if (!alc || (unsigned long)alc == BAD_ALIEN_MAGIC) | |
717 | continue; | |
718 | for_each_node(r) { | |
719 | if (alc[r]) | |
720 | lockdep_set_class(&alc[r]->lock, | |
721 | &on_slab_alc_key); | |
722 | } | |
723 | } | |
724 | s++; | |
f1aaee53 AV |
725 | } |
726 | } | |
f1aaee53 | 727 | #else |
056c6241 | 728 | static inline void init_lock_keys(void) |
f1aaee53 AV |
729 | { |
730 | } | |
731 | #endif | |
732 | ||
8f5be20b RT |
733 | /* |
734 | * 1. Guard access to the cache-chain. | |
735 | * 2. Protect sanity of cpu_online_map against cpu hotplug events | |
736 | */ | |
fc0abb14 | 737 | static DEFINE_MUTEX(cache_chain_mutex); |
1da177e4 LT |
738 | static struct list_head cache_chain; |
739 | ||
1da177e4 LT |
740 | /* |
741 | * chicken and egg problem: delay the per-cpu array allocation | |
742 | * until the general caches are up. | |
743 | */ | |
744 | static enum { | |
745 | NONE, | |
e498be7d CL |
746 | PARTIAL_AC, |
747 | PARTIAL_L3, | |
1da177e4 LT |
748 | FULL |
749 | } g_cpucache_up; | |
750 | ||
39d24e64 MK |
751 | /* |
752 | * used by boot code to determine if it can use slab based allocator | |
753 | */ | |
754 | int slab_is_available(void) | |
755 | { | |
756 | return g_cpucache_up == FULL; | |
757 | } | |
758 | ||
52bad64d | 759 | static DEFINE_PER_CPU(struct delayed_work, reap_work); |
1da177e4 | 760 | |
343e0d7a | 761 | static inline struct array_cache *cpu_cache_get(struct kmem_cache *cachep) |
1da177e4 LT |
762 | { |
763 | return cachep->array[smp_processor_id()]; | |
764 | } | |
765 | ||
a737b3e2 AM |
766 | static inline struct kmem_cache *__find_general_cachep(size_t size, |
767 | gfp_t gfpflags) | |
1da177e4 LT |
768 | { |
769 | struct cache_sizes *csizep = malloc_sizes; | |
770 | ||
771 | #if DEBUG | |
772 | /* This happens if someone tries to call | |
b28a02de PE |
773 | * kmem_cache_create(), or __kmalloc(), before |
774 | * the generic caches are initialized. | |
775 | */ | |
c7e43c78 | 776 | BUG_ON(malloc_sizes[INDEX_AC].cs_cachep == NULL); |
1da177e4 LT |
777 | #endif |
778 | while (size > csizep->cs_size) | |
779 | csizep++; | |
780 | ||
781 | /* | |
0abf40c1 | 782 | * Really subtle: The last entry with cs->cs_size==ULONG_MAX |
1da177e4 LT |
783 | * has cs_{dma,}cachep==NULL. Thus no special case |
784 | * for large kmalloc calls required. | |
785 | */ | |
786 | if (unlikely(gfpflags & GFP_DMA)) | |
787 | return csizep->cs_dmacachep; | |
788 | return csizep->cs_cachep; | |
789 | } | |
790 | ||
b221385b | 791 | static struct kmem_cache *kmem_find_general_cachep(size_t size, gfp_t gfpflags) |
97e2bde4 MS |
792 | { |
793 | return __find_general_cachep(size, gfpflags); | |
794 | } | |
97e2bde4 | 795 | |
fbaccacf | 796 | static size_t slab_mgmt_size(size_t nr_objs, size_t align) |
1da177e4 | 797 | { |
fbaccacf SR |
798 | return ALIGN(sizeof(struct slab)+nr_objs*sizeof(kmem_bufctl_t), align); |
799 | } | |
1da177e4 | 800 | |
a737b3e2 AM |
801 | /* |
802 | * Calculate the number of objects and left-over bytes for a given buffer size. | |
803 | */ | |
fbaccacf SR |
804 | static void cache_estimate(unsigned long gfporder, size_t buffer_size, |
805 | size_t align, int flags, size_t *left_over, | |
806 | unsigned int *num) | |
807 | { | |
808 | int nr_objs; | |
809 | size_t mgmt_size; | |
810 | size_t slab_size = PAGE_SIZE << gfporder; | |
1da177e4 | 811 | |
fbaccacf SR |
812 | /* |
813 | * The slab management structure can be either off the slab or | |
814 | * on it. For the latter case, the memory allocated for a | |
815 | * slab is used for: | |
816 | * | |
817 | * - The struct slab | |
818 | * - One kmem_bufctl_t for each object | |
819 | * - Padding to respect alignment of @align | |
820 | * - @buffer_size bytes for each object | |
821 | * | |
822 | * If the slab management structure is off the slab, then the | |
823 | * alignment will already be calculated into the size. Because | |
824 | * the slabs are all pages aligned, the objects will be at the | |
825 | * correct alignment when allocated. | |
826 | */ | |
827 | if (flags & CFLGS_OFF_SLAB) { | |
828 | mgmt_size = 0; | |
829 | nr_objs = slab_size / buffer_size; | |
830 | ||
831 | if (nr_objs > SLAB_LIMIT) | |
832 | nr_objs = SLAB_LIMIT; | |
833 | } else { | |
834 | /* | |
835 | * Ignore padding for the initial guess. The padding | |
836 | * is at most @align-1 bytes, and @buffer_size is at | |
837 | * least @align. In the worst case, this result will | |
838 | * be one greater than the number of objects that fit | |
839 | * into the memory allocation when taking the padding | |
840 | * into account. | |
841 | */ | |
842 | nr_objs = (slab_size - sizeof(struct slab)) / | |
843 | (buffer_size + sizeof(kmem_bufctl_t)); | |
844 | ||
845 | /* | |
846 | * This calculated number will be either the right | |
847 | * amount, or one greater than what we want. | |
848 | */ | |
849 | if (slab_mgmt_size(nr_objs, align) + nr_objs*buffer_size | |
850 | > slab_size) | |
851 | nr_objs--; | |
852 | ||
853 | if (nr_objs > SLAB_LIMIT) | |
854 | nr_objs = SLAB_LIMIT; | |
855 | ||
856 | mgmt_size = slab_mgmt_size(nr_objs, align); | |
857 | } | |
858 | *num = nr_objs; | |
859 | *left_over = slab_size - nr_objs*buffer_size - mgmt_size; | |
1da177e4 LT |
860 | } |
861 | ||
862 | #define slab_error(cachep, msg) __slab_error(__FUNCTION__, cachep, msg) | |
863 | ||
a737b3e2 AM |
864 | static void __slab_error(const char *function, struct kmem_cache *cachep, |
865 | char *msg) | |
1da177e4 LT |
866 | { |
867 | printk(KERN_ERR "slab error in %s(): cache `%s': %s\n", | |
b28a02de | 868 | function, cachep->name, msg); |
1da177e4 LT |
869 | dump_stack(); |
870 | } | |
871 | ||
8fce4d8e CL |
872 | #ifdef CONFIG_NUMA |
873 | /* | |
874 | * Special reaping functions for NUMA systems called from cache_reap(). | |
875 | * These take care of doing round robin flushing of alien caches (containing | |
876 | * objects freed on different nodes from which they were allocated) and the | |
877 | * flushing of remote pcps by calling drain_node_pages. | |
878 | */ | |
879 | static DEFINE_PER_CPU(unsigned long, reap_node); | |
880 | ||
881 | static void init_reap_node(int cpu) | |
882 | { | |
883 | int node; | |
884 | ||
885 | node = next_node(cpu_to_node(cpu), node_online_map); | |
886 | if (node == MAX_NUMNODES) | |
442295c9 | 887 | node = first_node(node_online_map); |
8fce4d8e | 888 | |
7f6b8876 | 889 | per_cpu(reap_node, cpu) = node; |
8fce4d8e CL |
890 | } |
891 | ||
892 | static void next_reap_node(void) | |
893 | { | |
894 | int node = __get_cpu_var(reap_node); | |
895 | ||
896 | /* | |
897 | * Also drain per cpu pages on remote zones | |
898 | */ | |
899 | if (node != numa_node_id()) | |
900 | drain_node_pages(node); | |
901 | ||
902 | node = next_node(node, node_online_map); | |
903 | if (unlikely(node >= MAX_NUMNODES)) | |
904 | node = first_node(node_online_map); | |
905 | __get_cpu_var(reap_node) = node; | |
906 | } | |
907 | ||
908 | #else | |
909 | #define init_reap_node(cpu) do { } while (0) | |
910 | #define next_reap_node(void) do { } while (0) | |
911 | #endif | |
912 | ||
1da177e4 LT |
913 | /* |
914 | * Initiate the reap timer running on the target CPU. We run at around 1 to 2Hz | |
915 | * via the workqueue/eventd. | |
916 | * Add the CPU number into the expiration time to minimize the possibility of | |
917 | * the CPUs getting into lockstep and contending for the global cache chain | |
918 | * lock. | |
919 | */ | |
920 | static void __devinit start_cpu_timer(int cpu) | |
921 | { | |
52bad64d | 922 | struct delayed_work *reap_work = &per_cpu(reap_work, cpu); |
1da177e4 LT |
923 | |
924 | /* | |
925 | * When this gets called from do_initcalls via cpucache_init(), | |
926 | * init_workqueues() has already run, so keventd will be setup | |
927 | * at that time. | |
928 | */ | |
52bad64d | 929 | if (keventd_up() && reap_work->work.func == NULL) { |
8fce4d8e | 930 | init_reap_node(cpu); |
65f27f38 | 931 | INIT_DELAYED_WORK(reap_work, cache_reap); |
1da177e4 LT |
932 | schedule_delayed_work_on(cpu, reap_work, HZ + 3 * cpu); |
933 | } | |
934 | } | |
935 | ||
e498be7d | 936 | static struct array_cache *alloc_arraycache(int node, int entries, |
b28a02de | 937 | int batchcount) |
1da177e4 | 938 | { |
b28a02de | 939 | int memsize = sizeof(void *) * entries + sizeof(struct array_cache); |
1da177e4 LT |
940 | struct array_cache *nc = NULL; |
941 | ||
e498be7d | 942 | nc = kmalloc_node(memsize, GFP_KERNEL, node); |
1da177e4 LT |
943 | if (nc) { |
944 | nc->avail = 0; | |
945 | nc->limit = entries; | |
946 | nc->batchcount = batchcount; | |
947 | nc->touched = 0; | |
e498be7d | 948 | spin_lock_init(&nc->lock); |
1da177e4 LT |
949 | } |
950 | return nc; | |
951 | } | |
952 | ||
3ded175a CL |
953 | /* |
954 | * Transfer objects in one arraycache to another. | |
955 | * Locking must be handled by the caller. | |
956 | * | |
957 | * Return the number of entries transferred. | |
958 | */ | |
959 | static int transfer_objects(struct array_cache *to, | |
960 | struct array_cache *from, unsigned int max) | |
961 | { | |
962 | /* Figure out how many entries to transfer */ | |
963 | int nr = min(min(from->avail, max), to->limit - to->avail); | |
964 | ||
965 | if (!nr) | |
966 | return 0; | |
967 | ||
968 | memcpy(to->entry + to->avail, from->entry + from->avail -nr, | |
969 | sizeof(void *) *nr); | |
970 | ||
971 | from->avail -= nr; | |
972 | to->avail += nr; | |
973 | to->touched = 1; | |
974 | return nr; | |
975 | } | |
976 | ||
765c4507 CL |
977 | #ifndef CONFIG_NUMA |
978 | ||
979 | #define drain_alien_cache(cachep, alien) do { } while (0) | |
980 | #define reap_alien(cachep, l3) do { } while (0) | |
981 | ||
982 | static inline struct array_cache **alloc_alien_cache(int node, int limit) | |
983 | { | |
984 | return (struct array_cache **)BAD_ALIEN_MAGIC; | |
985 | } | |
986 | ||
987 | static inline void free_alien_cache(struct array_cache **ac_ptr) | |
988 | { | |
989 | } | |
990 | ||
991 | static inline int cache_free_alien(struct kmem_cache *cachep, void *objp) | |
992 | { | |
993 | return 0; | |
994 | } | |
995 | ||
996 | static inline void *alternate_node_alloc(struct kmem_cache *cachep, | |
997 | gfp_t flags) | |
998 | { | |
999 | return NULL; | |
1000 | } | |
1001 | ||
1002 | static inline void *__cache_alloc_node(struct kmem_cache *cachep, | |
1003 | gfp_t flags, int nodeid) | |
1004 | { | |
1005 | return NULL; | |
1006 | } | |
1007 | ||
1008 | #else /* CONFIG_NUMA */ | |
1009 | ||
343e0d7a | 1010 | static void *__cache_alloc_node(struct kmem_cache *, gfp_t, int); |
c61afb18 | 1011 | static void *alternate_node_alloc(struct kmem_cache *, gfp_t); |
dc85da15 | 1012 | |
5295a74c | 1013 | static struct array_cache **alloc_alien_cache(int node, int limit) |
e498be7d CL |
1014 | { |
1015 | struct array_cache **ac_ptr; | |
b28a02de | 1016 | int memsize = sizeof(void *) * MAX_NUMNODES; |
e498be7d CL |
1017 | int i; |
1018 | ||
1019 | if (limit > 1) | |
1020 | limit = 12; | |
1021 | ac_ptr = kmalloc_node(memsize, GFP_KERNEL, node); | |
1022 | if (ac_ptr) { | |
1023 | for_each_node(i) { | |
1024 | if (i == node || !node_online(i)) { | |
1025 | ac_ptr[i] = NULL; | |
1026 | continue; | |
1027 | } | |
1028 | ac_ptr[i] = alloc_arraycache(node, limit, 0xbaadf00d); | |
1029 | if (!ac_ptr[i]) { | |
b28a02de | 1030 | for (i--; i <= 0; i--) |
e498be7d CL |
1031 | kfree(ac_ptr[i]); |
1032 | kfree(ac_ptr); | |
1033 | return NULL; | |
1034 | } | |
1035 | } | |
1036 | } | |
1037 | return ac_ptr; | |
1038 | } | |
1039 | ||
5295a74c | 1040 | static void free_alien_cache(struct array_cache **ac_ptr) |
e498be7d CL |
1041 | { |
1042 | int i; | |
1043 | ||
1044 | if (!ac_ptr) | |
1045 | return; | |
e498be7d | 1046 | for_each_node(i) |
b28a02de | 1047 | kfree(ac_ptr[i]); |
e498be7d CL |
1048 | kfree(ac_ptr); |
1049 | } | |
1050 | ||
343e0d7a | 1051 | static void __drain_alien_cache(struct kmem_cache *cachep, |
5295a74c | 1052 | struct array_cache *ac, int node) |
e498be7d CL |
1053 | { |
1054 | struct kmem_list3 *rl3 = cachep->nodelists[node]; | |
1055 | ||
1056 | if (ac->avail) { | |
1057 | spin_lock(&rl3->list_lock); | |
e00946fe CL |
1058 | /* |
1059 | * Stuff objects into the remote nodes shared array first. | |
1060 | * That way we could avoid the overhead of putting the objects | |
1061 | * into the free lists and getting them back later. | |
1062 | */ | |
693f7d36 JS |
1063 | if (rl3->shared) |
1064 | transfer_objects(rl3->shared, ac, ac->limit); | |
e00946fe | 1065 | |
ff69416e | 1066 | free_block(cachep, ac->entry, ac->avail, node); |
e498be7d CL |
1067 | ac->avail = 0; |
1068 | spin_unlock(&rl3->list_lock); | |
1069 | } | |
1070 | } | |
1071 | ||
8fce4d8e CL |
1072 | /* |
1073 | * Called from cache_reap() to regularly drain alien caches round robin. | |
1074 | */ | |
1075 | static void reap_alien(struct kmem_cache *cachep, struct kmem_list3 *l3) | |
1076 | { | |
1077 | int node = __get_cpu_var(reap_node); | |
1078 | ||
1079 | if (l3->alien) { | |
1080 | struct array_cache *ac = l3->alien[node]; | |
e00946fe CL |
1081 | |
1082 | if (ac && ac->avail && spin_trylock_irq(&ac->lock)) { | |
8fce4d8e CL |
1083 | __drain_alien_cache(cachep, ac, node); |
1084 | spin_unlock_irq(&ac->lock); | |
1085 | } | |
1086 | } | |
1087 | } | |
1088 | ||
a737b3e2 AM |
1089 | static void drain_alien_cache(struct kmem_cache *cachep, |
1090 | struct array_cache **alien) | |
e498be7d | 1091 | { |
b28a02de | 1092 | int i = 0; |
e498be7d CL |
1093 | struct array_cache *ac; |
1094 | unsigned long flags; | |
1095 | ||
1096 | for_each_online_node(i) { | |
4484ebf1 | 1097 | ac = alien[i]; |
e498be7d CL |
1098 | if (ac) { |
1099 | spin_lock_irqsave(&ac->lock, flags); | |
1100 | __drain_alien_cache(cachep, ac, i); | |
1101 | spin_unlock_irqrestore(&ac->lock, flags); | |
1102 | } | |
1103 | } | |
1104 | } | |
729bd0b7 | 1105 | |
873623df | 1106 | static inline int cache_free_alien(struct kmem_cache *cachep, void *objp) |
729bd0b7 PE |
1107 | { |
1108 | struct slab *slabp = virt_to_slab(objp); | |
1109 | int nodeid = slabp->nodeid; | |
1110 | struct kmem_list3 *l3; | |
1111 | struct array_cache *alien = NULL; | |
1ca4cb24 PE |
1112 | int node; |
1113 | ||
1114 | node = numa_node_id(); | |
729bd0b7 PE |
1115 | |
1116 | /* | |
1117 | * Make sure we are not freeing a object from another node to the array | |
1118 | * cache on this cpu. | |
1119 | */ | |
1ca4cb24 | 1120 | if (likely(slabp->nodeid == node)) |
729bd0b7 PE |
1121 | return 0; |
1122 | ||
1ca4cb24 | 1123 | l3 = cachep->nodelists[node]; |
729bd0b7 PE |
1124 | STATS_INC_NODEFREES(cachep); |
1125 | if (l3->alien && l3->alien[nodeid]) { | |
1126 | alien = l3->alien[nodeid]; | |
873623df | 1127 | spin_lock(&alien->lock); |
729bd0b7 PE |
1128 | if (unlikely(alien->avail == alien->limit)) { |
1129 | STATS_INC_ACOVERFLOW(cachep); | |
1130 | __drain_alien_cache(cachep, alien, nodeid); | |
1131 | } | |
1132 | alien->entry[alien->avail++] = objp; | |
1133 | spin_unlock(&alien->lock); | |
1134 | } else { | |
1135 | spin_lock(&(cachep->nodelists[nodeid])->list_lock); | |
1136 | free_block(cachep, &objp, 1, nodeid); | |
1137 | spin_unlock(&(cachep->nodelists[nodeid])->list_lock); | |
1138 | } | |
1139 | return 1; | |
1140 | } | |
e498be7d CL |
1141 | #endif |
1142 | ||
8c78f307 | 1143 | static int __cpuinit cpuup_callback(struct notifier_block *nfb, |
b28a02de | 1144 | unsigned long action, void *hcpu) |
1da177e4 LT |
1145 | { |
1146 | long cpu = (long)hcpu; | |
343e0d7a | 1147 | struct kmem_cache *cachep; |
e498be7d CL |
1148 | struct kmem_list3 *l3 = NULL; |
1149 | int node = cpu_to_node(cpu); | |
1150 | int memsize = sizeof(struct kmem_list3); | |
1da177e4 LT |
1151 | |
1152 | switch (action) { | |
1153 | case CPU_UP_PREPARE: | |
fc0abb14 | 1154 | mutex_lock(&cache_chain_mutex); |
a737b3e2 AM |
1155 | /* |
1156 | * We need to do this right in the beginning since | |
e498be7d CL |
1157 | * alloc_arraycache's are going to use this list. |
1158 | * kmalloc_node allows us to add the slab to the right | |
1159 | * kmem_list3 and not this cpu's kmem_list3 | |
1160 | */ | |
1161 | ||
1da177e4 | 1162 | list_for_each_entry(cachep, &cache_chain, next) { |
a737b3e2 AM |
1163 | /* |
1164 | * Set up the size64 kmemlist for cpu before we can | |
e498be7d CL |
1165 | * begin anything. Make sure some other cpu on this |
1166 | * node has not already allocated this | |
1167 | */ | |
1168 | if (!cachep->nodelists[node]) { | |
a737b3e2 AM |
1169 | l3 = kmalloc_node(memsize, GFP_KERNEL, node); |
1170 | if (!l3) | |
e498be7d CL |
1171 | goto bad; |
1172 | kmem_list3_init(l3); | |
1173 | l3->next_reap = jiffies + REAPTIMEOUT_LIST3 + | |
b28a02de | 1174 | ((unsigned long)cachep) % REAPTIMEOUT_LIST3; |
e498be7d | 1175 | |
4484ebf1 RT |
1176 | /* |
1177 | * The l3s don't come and go as CPUs come and | |
1178 | * go. cache_chain_mutex is sufficient | |
1179 | * protection here. | |
1180 | */ | |
e498be7d CL |
1181 | cachep->nodelists[node] = l3; |
1182 | } | |
1da177e4 | 1183 | |
e498be7d CL |
1184 | spin_lock_irq(&cachep->nodelists[node]->list_lock); |
1185 | cachep->nodelists[node]->free_limit = | |
a737b3e2 AM |
1186 | (1 + nr_cpus_node(node)) * |
1187 | cachep->batchcount + cachep->num; | |
e498be7d CL |
1188 | spin_unlock_irq(&cachep->nodelists[node]->list_lock); |
1189 | } | |
1190 | ||
a737b3e2 AM |
1191 | /* |
1192 | * Now we can go ahead with allocating the shared arrays and | |
1193 | * array caches | |
1194 | */ | |
e498be7d | 1195 | list_for_each_entry(cachep, &cache_chain, next) { |
cd105df4 | 1196 | struct array_cache *nc; |
4484ebf1 RT |
1197 | struct array_cache *shared; |
1198 | struct array_cache **alien; | |
cd105df4 | 1199 | |
e498be7d | 1200 | nc = alloc_arraycache(node, cachep->limit, |
4484ebf1 | 1201 | cachep->batchcount); |
1da177e4 LT |
1202 | if (!nc) |
1203 | goto bad; | |
4484ebf1 RT |
1204 | shared = alloc_arraycache(node, |
1205 | cachep->shared * cachep->batchcount, | |
1206 | 0xbaadf00d); | |
1207 | if (!shared) | |
1208 | goto bad; | |
7a21ef6f | 1209 | |
4484ebf1 RT |
1210 | alien = alloc_alien_cache(node, cachep->limit); |
1211 | if (!alien) | |
1212 | goto bad; | |
1da177e4 | 1213 | cachep->array[cpu] = nc; |
e498be7d CL |
1214 | l3 = cachep->nodelists[node]; |
1215 | BUG_ON(!l3); | |
e498be7d | 1216 | |
4484ebf1 RT |
1217 | spin_lock_irq(&l3->list_lock); |
1218 | if (!l3->shared) { | |
1219 | /* | |
1220 | * We are serialised from CPU_DEAD or | |
1221 | * CPU_UP_CANCELLED by the cpucontrol lock | |
1222 | */ | |
1223 | l3->shared = shared; | |
1224 | shared = NULL; | |
e498be7d | 1225 | } |
4484ebf1 RT |
1226 | #ifdef CONFIG_NUMA |
1227 | if (!l3->alien) { | |
1228 | l3->alien = alien; | |
1229 | alien = NULL; | |
1230 | } | |
1231 | #endif | |
1232 | spin_unlock_irq(&l3->list_lock); | |
4484ebf1 RT |
1233 | kfree(shared); |
1234 | free_alien_cache(alien); | |
1da177e4 | 1235 | } |
1da177e4 LT |
1236 | break; |
1237 | case CPU_ONLINE: | |
8f5be20b | 1238 | mutex_unlock(&cache_chain_mutex); |
1da177e4 LT |
1239 | start_cpu_timer(cpu); |
1240 | break; | |
1241 | #ifdef CONFIG_HOTPLUG_CPU | |
8f5be20b RT |
1242 | case CPU_DOWN_PREPARE: |
1243 | mutex_lock(&cache_chain_mutex); | |
1244 | break; | |
1245 | case CPU_DOWN_FAILED: | |
1246 | mutex_unlock(&cache_chain_mutex); | |
1247 | break; | |
1da177e4 | 1248 | case CPU_DEAD: |
4484ebf1 RT |
1249 | /* |
1250 | * Even if all the cpus of a node are down, we don't free the | |
1251 | * kmem_list3 of any cache. This to avoid a race between | |
1252 | * cpu_down, and a kmalloc allocation from another cpu for | |
1253 | * memory from the node of the cpu going down. The list3 | |
1254 | * structure is usually allocated from kmem_cache_create() and | |
1255 | * gets destroyed at kmem_cache_destroy(). | |
1256 | */ | |
1da177e4 | 1257 | /* fall thru */ |
8f5be20b | 1258 | #endif |
1da177e4 | 1259 | case CPU_UP_CANCELED: |
1da177e4 LT |
1260 | list_for_each_entry(cachep, &cache_chain, next) { |
1261 | struct array_cache *nc; | |
4484ebf1 RT |
1262 | struct array_cache *shared; |
1263 | struct array_cache **alien; | |
e498be7d | 1264 | cpumask_t mask; |
1da177e4 | 1265 | |
e498be7d | 1266 | mask = node_to_cpumask(node); |
1da177e4 LT |
1267 | /* cpu is dead; no one can alloc from it. */ |
1268 | nc = cachep->array[cpu]; | |
1269 | cachep->array[cpu] = NULL; | |
e498be7d CL |
1270 | l3 = cachep->nodelists[node]; |
1271 | ||
1272 | if (!l3) | |
4484ebf1 | 1273 | goto free_array_cache; |
e498be7d | 1274 | |
ca3b9b91 | 1275 | spin_lock_irq(&l3->list_lock); |
e498be7d CL |
1276 | |
1277 | /* Free limit for this kmem_list3 */ | |
1278 | l3->free_limit -= cachep->batchcount; | |
1279 | if (nc) | |
ff69416e | 1280 | free_block(cachep, nc->entry, nc->avail, node); |
e498be7d CL |
1281 | |
1282 | if (!cpus_empty(mask)) { | |
ca3b9b91 | 1283 | spin_unlock_irq(&l3->list_lock); |
4484ebf1 | 1284 | goto free_array_cache; |
b28a02de | 1285 | } |
e498be7d | 1286 | |
4484ebf1 RT |
1287 | shared = l3->shared; |
1288 | if (shared) { | |
e498be7d | 1289 | free_block(cachep, l3->shared->entry, |
b28a02de | 1290 | l3->shared->avail, node); |
e498be7d CL |
1291 | l3->shared = NULL; |
1292 | } | |
e498be7d | 1293 | |
4484ebf1 RT |
1294 | alien = l3->alien; |
1295 | l3->alien = NULL; | |
1296 | ||
1297 | spin_unlock_irq(&l3->list_lock); | |
1298 | ||
1299 | kfree(shared); | |
1300 | if (alien) { | |
1301 | drain_alien_cache(cachep, alien); | |
1302 | free_alien_cache(alien); | |
e498be7d | 1303 | } |
4484ebf1 | 1304 | free_array_cache: |
1da177e4 LT |
1305 | kfree(nc); |
1306 | } | |
4484ebf1 RT |
1307 | /* |
1308 | * In the previous loop, all the objects were freed to | |
1309 | * the respective cache's slabs, now we can go ahead and | |
1310 | * shrink each nodelist to its limit. | |
1311 | */ | |
1312 | list_for_each_entry(cachep, &cache_chain, next) { | |
1313 | l3 = cachep->nodelists[node]; | |
1314 | if (!l3) | |
1315 | continue; | |
ed11d9eb | 1316 | drain_freelist(cachep, l3, l3->free_objects); |
4484ebf1 | 1317 | } |
fc0abb14 | 1318 | mutex_unlock(&cache_chain_mutex); |
1da177e4 | 1319 | break; |
1da177e4 LT |
1320 | } |
1321 | return NOTIFY_OK; | |
a737b3e2 | 1322 | bad: |
1da177e4 LT |
1323 | return NOTIFY_BAD; |
1324 | } | |
1325 | ||
74b85f37 CS |
1326 | static struct notifier_block __cpuinitdata cpucache_notifier = { |
1327 | &cpuup_callback, NULL, 0 | |
1328 | }; | |
1da177e4 | 1329 | |
e498be7d CL |
1330 | /* |
1331 | * swap the static kmem_list3 with kmalloced memory | |
1332 | */ | |
a737b3e2 AM |
1333 | static void init_list(struct kmem_cache *cachep, struct kmem_list3 *list, |
1334 | int nodeid) | |
e498be7d CL |
1335 | { |
1336 | struct kmem_list3 *ptr; | |
1337 | ||
e498be7d CL |
1338 | ptr = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, nodeid); |
1339 | BUG_ON(!ptr); | |
1340 | ||
1341 | local_irq_disable(); | |
1342 | memcpy(ptr, list, sizeof(struct kmem_list3)); | |
2b2d5493 IM |
1343 | /* |
1344 | * Do not assume that spinlocks can be initialized via memcpy: | |
1345 | */ | |
1346 | spin_lock_init(&ptr->list_lock); | |
1347 | ||
e498be7d CL |
1348 | MAKE_ALL_LISTS(cachep, ptr, nodeid); |
1349 | cachep->nodelists[nodeid] = ptr; | |
1350 | local_irq_enable(); | |
1351 | } | |
1352 | ||
a737b3e2 AM |
1353 | /* |
1354 | * Initialisation. Called after the page allocator have been initialised and | |
1355 | * before smp_init(). | |
1da177e4 LT |
1356 | */ |
1357 | void __init kmem_cache_init(void) | |
1358 | { | |
1359 | size_t left_over; | |
1360 | struct cache_sizes *sizes; | |
1361 | struct cache_names *names; | |
e498be7d | 1362 | int i; |
07ed76b2 | 1363 | int order; |
1ca4cb24 | 1364 | int node; |
e498be7d CL |
1365 | |
1366 | for (i = 0; i < NUM_INIT_LISTS; i++) { | |
1367 | kmem_list3_init(&initkmem_list3[i]); | |
1368 | if (i < MAX_NUMNODES) | |
1369 | cache_cache.nodelists[i] = NULL; | |
1370 | } | |
1da177e4 LT |
1371 | |
1372 | /* | |
1373 | * Fragmentation resistance on low memory - only use bigger | |
1374 | * page orders on machines with more than 32MB of memory. | |
1375 | */ | |
1376 | if (num_physpages > (32 << 20) >> PAGE_SHIFT) | |
1377 | slab_break_gfp_order = BREAK_GFP_ORDER_HI; | |
1378 | ||
1da177e4 LT |
1379 | /* Bootstrap is tricky, because several objects are allocated |
1380 | * from caches that do not exist yet: | |
a737b3e2 AM |
1381 | * 1) initialize the cache_cache cache: it contains the struct |
1382 | * kmem_cache structures of all caches, except cache_cache itself: | |
1383 | * cache_cache is statically allocated. | |
e498be7d CL |
1384 | * Initially an __init data area is used for the head array and the |
1385 | * kmem_list3 structures, it's replaced with a kmalloc allocated | |
1386 | * array at the end of the bootstrap. | |
1da177e4 | 1387 | * 2) Create the first kmalloc cache. |
343e0d7a | 1388 | * The struct kmem_cache for the new cache is allocated normally. |
e498be7d CL |
1389 | * An __init data area is used for the head array. |
1390 | * 3) Create the remaining kmalloc caches, with minimally sized | |
1391 | * head arrays. | |
1da177e4 LT |
1392 | * 4) Replace the __init data head arrays for cache_cache and the first |
1393 | * kmalloc cache with kmalloc allocated arrays. | |
e498be7d CL |
1394 | * 5) Replace the __init data for kmem_list3 for cache_cache and |
1395 | * the other cache's with kmalloc allocated memory. | |
1396 | * 6) Resize the head arrays of the kmalloc caches to their final sizes. | |
1da177e4 LT |
1397 | */ |
1398 | ||
1ca4cb24 PE |
1399 | node = numa_node_id(); |
1400 | ||
1da177e4 | 1401 | /* 1) create the cache_cache */ |
1da177e4 LT |
1402 | INIT_LIST_HEAD(&cache_chain); |
1403 | list_add(&cache_cache.next, &cache_chain); | |
1404 | cache_cache.colour_off = cache_line_size(); | |
1405 | cache_cache.array[smp_processor_id()] = &initarray_cache.cache; | |
1ca4cb24 | 1406 | cache_cache.nodelists[node] = &initkmem_list3[CACHE_CACHE]; |
1da177e4 | 1407 | |
a737b3e2 AM |
1408 | cache_cache.buffer_size = ALIGN(cache_cache.buffer_size, |
1409 | cache_line_size()); | |
1da177e4 | 1410 | |
07ed76b2 JS |
1411 | for (order = 0; order < MAX_ORDER; order++) { |
1412 | cache_estimate(order, cache_cache.buffer_size, | |
1413 | cache_line_size(), 0, &left_over, &cache_cache.num); | |
1414 | if (cache_cache.num) | |
1415 | break; | |
1416 | } | |
40094fa6 | 1417 | BUG_ON(!cache_cache.num); |
07ed76b2 | 1418 | cache_cache.gfporder = order; |
b28a02de | 1419 | cache_cache.colour = left_over / cache_cache.colour_off; |
b28a02de PE |
1420 | cache_cache.slab_size = ALIGN(cache_cache.num * sizeof(kmem_bufctl_t) + |
1421 | sizeof(struct slab), cache_line_size()); | |
1da177e4 LT |
1422 | |
1423 | /* 2+3) create the kmalloc caches */ | |
1424 | sizes = malloc_sizes; | |
1425 | names = cache_names; | |
1426 | ||
a737b3e2 AM |
1427 | /* |
1428 | * Initialize the caches that provide memory for the array cache and the | |
1429 | * kmem_list3 structures first. Without this, further allocations will | |
1430 | * bug. | |
e498be7d CL |
1431 | */ |
1432 | ||
1433 | sizes[INDEX_AC].cs_cachep = kmem_cache_create(names[INDEX_AC].name, | |
a737b3e2 AM |
1434 | sizes[INDEX_AC].cs_size, |
1435 | ARCH_KMALLOC_MINALIGN, | |
1436 | ARCH_KMALLOC_FLAGS|SLAB_PANIC, | |
1437 | NULL, NULL); | |
e498be7d | 1438 | |
a737b3e2 | 1439 | if (INDEX_AC != INDEX_L3) { |
e498be7d | 1440 | sizes[INDEX_L3].cs_cachep = |
a737b3e2 AM |
1441 | kmem_cache_create(names[INDEX_L3].name, |
1442 | sizes[INDEX_L3].cs_size, | |
1443 | ARCH_KMALLOC_MINALIGN, | |
1444 | ARCH_KMALLOC_FLAGS|SLAB_PANIC, | |
1445 | NULL, NULL); | |
1446 | } | |
e498be7d | 1447 | |
e0a42726 IM |
1448 | slab_early_init = 0; |
1449 | ||
1da177e4 | 1450 | while (sizes->cs_size != ULONG_MAX) { |
e498be7d CL |
1451 | /* |
1452 | * For performance, all the general caches are L1 aligned. | |
1da177e4 LT |
1453 | * This should be particularly beneficial on SMP boxes, as it |
1454 | * eliminates "false sharing". | |
1455 | * Note for systems short on memory removing the alignment will | |
e498be7d CL |
1456 | * allow tighter packing of the smaller caches. |
1457 | */ | |
a737b3e2 | 1458 | if (!sizes->cs_cachep) { |
e498be7d | 1459 | sizes->cs_cachep = kmem_cache_create(names->name, |
a737b3e2 AM |
1460 | sizes->cs_size, |
1461 | ARCH_KMALLOC_MINALIGN, | |
1462 | ARCH_KMALLOC_FLAGS|SLAB_PANIC, | |
1463 | NULL, NULL); | |
1464 | } | |
1da177e4 | 1465 | |
1da177e4 | 1466 | sizes->cs_dmacachep = kmem_cache_create(names->name_dma, |
a737b3e2 AM |
1467 | sizes->cs_size, |
1468 | ARCH_KMALLOC_MINALIGN, | |
1469 | ARCH_KMALLOC_FLAGS|SLAB_CACHE_DMA| | |
1470 | SLAB_PANIC, | |
1471 | NULL, NULL); | |
1da177e4 LT |
1472 | sizes++; |
1473 | names++; | |
1474 | } | |
1475 | /* 4) Replace the bootstrap head arrays */ | |
1476 | { | |
2b2d5493 | 1477 | struct array_cache *ptr; |
e498be7d | 1478 | |
1da177e4 | 1479 | ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL); |
e498be7d | 1480 | |
1da177e4 | 1481 | local_irq_disable(); |
9a2dba4b PE |
1482 | BUG_ON(cpu_cache_get(&cache_cache) != &initarray_cache.cache); |
1483 | memcpy(ptr, cpu_cache_get(&cache_cache), | |
b28a02de | 1484 | sizeof(struct arraycache_init)); |
2b2d5493 IM |
1485 | /* |
1486 | * Do not assume that spinlocks can be initialized via memcpy: | |
1487 | */ | |
1488 | spin_lock_init(&ptr->lock); | |
1489 | ||
1da177e4 LT |
1490 | cache_cache.array[smp_processor_id()] = ptr; |
1491 | local_irq_enable(); | |
e498be7d | 1492 | |
1da177e4 | 1493 | ptr = kmalloc(sizeof(struct arraycache_init), GFP_KERNEL); |
e498be7d | 1494 | |
1da177e4 | 1495 | local_irq_disable(); |
9a2dba4b | 1496 | BUG_ON(cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep) |
b28a02de | 1497 | != &initarray_generic.cache); |
9a2dba4b | 1498 | memcpy(ptr, cpu_cache_get(malloc_sizes[INDEX_AC].cs_cachep), |
b28a02de | 1499 | sizeof(struct arraycache_init)); |
2b2d5493 IM |
1500 | /* |
1501 | * Do not assume that spinlocks can be initialized via memcpy: | |
1502 | */ | |
1503 | spin_lock_init(&ptr->lock); | |
1504 | ||
e498be7d | 1505 | malloc_sizes[INDEX_AC].cs_cachep->array[smp_processor_id()] = |
b28a02de | 1506 | ptr; |
1da177e4 LT |
1507 | local_irq_enable(); |
1508 | } | |
e498be7d CL |
1509 | /* 5) Replace the bootstrap kmem_list3's */ |
1510 | { | |
1ca4cb24 PE |
1511 | int nid; |
1512 | ||
e498be7d | 1513 | /* Replace the static kmem_list3 structures for the boot cpu */ |
1ca4cb24 | 1514 | init_list(&cache_cache, &initkmem_list3[CACHE_CACHE], node); |
e498be7d | 1515 | |
1ca4cb24 | 1516 | for_each_online_node(nid) { |
e498be7d | 1517 | init_list(malloc_sizes[INDEX_AC].cs_cachep, |
1ca4cb24 | 1518 | &initkmem_list3[SIZE_AC + nid], nid); |
e498be7d CL |
1519 | |
1520 | if (INDEX_AC != INDEX_L3) { | |
1521 | init_list(malloc_sizes[INDEX_L3].cs_cachep, | |
1ca4cb24 | 1522 | &initkmem_list3[SIZE_L3 + nid], nid); |
e498be7d CL |
1523 | } |
1524 | } | |
1525 | } | |
1da177e4 | 1526 | |
e498be7d | 1527 | /* 6) resize the head arrays to their final sizes */ |
1da177e4 | 1528 | { |
343e0d7a | 1529 | struct kmem_cache *cachep; |
fc0abb14 | 1530 | mutex_lock(&cache_chain_mutex); |
1da177e4 | 1531 | list_for_each_entry(cachep, &cache_chain, next) |
2ed3a4ef CL |
1532 | if (enable_cpucache(cachep)) |
1533 | BUG(); | |
fc0abb14 | 1534 | mutex_unlock(&cache_chain_mutex); |
1da177e4 LT |
1535 | } |
1536 | ||
056c6241 RT |
1537 | /* Annotate slab for lockdep -- annotate the malloc caches */ |
1538 | init_lock_keys(); | |
1539 | ||
1540 | ||
1da177e4 LT |
1541 | /* Done! */ |
1542 | g_cpucache_up = FULL; | |
1543 | ||
a737b3e2 AM |
1544 | /* |
1545 | * Register a cpu startup notifier callback that initializes | |
1546 | * cpu_cache_get for all new cpus | |
1da177e4 LT |
1547 | */ |
1548 | register_cpu_notifier(&cpucache_notifier); | |
1da177e4 | 1549 | |
a737b3e2 AM |
1550 | /* |
1551 | * The reap timers are started later, with a module init call: That part | |
1552 | * of the kernel is not yet operational. | |
1da177e4 LT |
1553 | */ |
1554 | } | |
1555 | ||
1556 | static int __init cpucache_init(void) | |
1557 | { | |
1558 | int cpu; | |
1559 | ||
a737b3e2 AM |
1560 | /* |
1561 | * Register the timers that return unneeded pages to the page allocator | |
1da177e4 | 1562 | */ |
e498be7d | 1563 | for_each_online_cpu(cpu) |
a737b3e2 | 1564 | start_cpu_timer(cpu); |
1da177e4 LT |
1565 | return 0; |
1566 | } | |
1da177e4 LT |
1567 | __initcall(cpucache_init); |
1568 | ||
1569 | /* | |
1570 | * Interface to system's page allocator. No need to hold the cache-lock. | |
1571 | * | |
1572 | * If we requested dmaable memory, we will get it. Even if we | |
1573 | * did not request dmaable memory, we might get it, but that | |
1574 | * would be relatively rare and ignorable. | |
1575 | */ | |
343e0d7a | 1576 | static void *kmem_getpages(struct kmem_cache *cachep, gfp_t flags, int nodeid) |
1da177e4 LT |
1577 | { |
1578 | struct page *page; | |
e1b6aa6f | 1579 | int nr_pages; |
1da177e4 LT |
1580 | int i; |
1581 | ||
d6fef9da | 1582 | #ifndef CONFIG_MMU |
e1b6aa6f CH |
1583 | /* |
1584 | * Nommu uses slab's for process anonymous memory allocations, and thus | |
1585 | * requires __GFP_COMP to properly refcount higher order allocations | |
d6fef9da | 1586 | */ |
e1b6aa6f | 1587 | flags |= __GFP_COMP; |
d6fef9da | 1588 | #endif |
765c4507 CL |
1589 | |
1590 | /* | |
1591 | * Under NUMA we want memory on the indicated node. We will handle | |
1592 | * the needed fallback ourselves since we want to serve from our | |
1593 | * per node object lists first for other nodes. | |
1594 | */ | |
1595 | flags |= cachep->gfpflags | GFP_THISNODE; | |
e1b6aa6f CH |
1596 | |
1597 | page = alloc_pages_node(nodeid, flags, cachep->gfporder); | |
1da177e4 LT |
1598 | if (!page) |
1599 | return NULL; | |
1da177e4 | 1600 | |
e1b6aa6f | 1601 | nr_pages = (1 << cachep->gfporder); |
1da177e4 | 1602 | if (cachep->flags & SLAB_RECLAIM_ACCOUNT) |
972d1a7b CL |
1603 | add_zone_page_state(page_zone(page), |
1604 | NR_SLAB_RECLAIMABLE, nr_pages); | |
1605 | else | |
1606 | add_zone_page_state(page_zone(page), | |
1607 | NR_SLAB_UNRECLAIMABLE, nr_pages); | |
e1b6aa6f CH |
1608 | for (i = 0; i < nr_pages; i++) |
1609 | __SetPageSlab(page + i); | |
1610 | return page_address(page); | |
1da177e4 LT |
1611 | } |
1612 | ||
1613 | /* | |
1614 | * Interface to system's page release. | |
1615 | */ | |
343e0d7a | 1616 | static void kmem_freepages(struct kmem_cache *cachep, void *addr) |
1da177e4 | 1617 | { |
b28a02de | 1618 | unsigned long i = (1 << cachep->gfporder); |
1da177e4 LT |
1619 | struct page *page = virt_to_page(addr); |
1620 | const unsigned long nr_freed = i; | |
1621 | ||
972d1a7b CL |
1622 | if (cachep->flags & SLAB_RECLAIM_ACCOUNT) |
1623 | sub_zone_page_state(page_zone(page), | |
1624 | NR_SLAB_RECLAIMABLE, nr_freed); | |
1625 | else | |
1626 | sub_zone_page_state(page_zone(page), | |
1627 | NR_SLAB_UNRECLAIMABLE, nr_freed); | |
1da177e4 | 1628 | while (i--) { |
f205b2fe NP |
1629 | BUG_ON(!PageSlab(page)); |
1630 | __ClearPageSlab(page); | |
1da177e4 LT |
1631 | page++; |
1632 | } | |
1da177e4 LT |
1633 | if (current->reclaim_state) |
1634 | current->reclaim_state->reclaimed_slab += nr_freed; | |
1635 | free_pages((unsigned long)addr, cachep->gfporder); | |
1da177e4 LT |
1636 | } |
1637 | ||
1638 | static void kmem_rcu_free(struct rcu_head *head) | |
1639 | { | |
b28a02de | 1640 | struct slab_rcu *slab_rcu = (struct slab_rcu *)head; |
343e0d7a | 1641 | struct kmem_cache *cachep = slab_rcu->cachep; |
1da177e4 LT |
1642 | |
1643 | kmem_freepages(cachep, slab_rcu->addr); | |
1644 | if (OFF_SLAB(cachep)) | |
1645 | kmem_cache_free(cachep->slabp_cache, slab_rcu); | |
1646 | } | |
1647 | ||
1648 | #if DEBUG | |
1649 | ||
1650 | #ifdef CONFIG_DEBUG_PAGEALLOC | |
343e0d7a | 1651 | static void store_stackinfo(struct kmem_cache *cachep, unsigned long *addr, |
b28a02de | 1652 | unsigned long caller) |
1da177e4 | 1653 | { |
3dafccf2 | 1654 | int size = obj_size(cachep); |
1da177e4 | 1655 | |
3dafccf2 | 1656 | addr = (unsigned long *)&((char *)addr)[obj_offset(cachep)]; |
1da177e4 | 1657 | |
b28a02de | 1658 | if (size < 5 * sizeof(unsigned long)) |
1da177e4 LT |
1659 | return; |
1660 | ||
b28a02de PE |
1661 | *addr++ = 0x12345678; |
1662 | *addr++ = caller; | |
1663 | *addr++ = smp_processor_id(); | |
1664 | size -= 3 * sizeof(unsigned long); | |
1da177e4 LT |
1665 | { |
1666 | unsigned long *sptr = &caller; | |
1667 | unsigned long svalue; | |
1668 | ||
1669 | while (!kstack_end(sptr)) { | |
1670 | svalue = *sptr++; | |
1671 | if (kernel_text_address(svalue)) { | |
b28a02de | 1672 | *addr++ = svalue; |
1da177e4 LT |
1673 | size -= sizeof(unsigned long); |
1674 | if (size <= sizeof(unsigned long)) | |
1675 | break; | |
1676 | } | |
1677 | } | |
1678 | ||
1679 | } | |
b28a02de | 1680 | *addr++ = 0x87654321; |
1da177e4 LT |
1681 | } |
1682 | #endif | |
1683 | ||
343e0d7a | 1684 | static void poison_obj(struct kmem_cache *cachep, void *addr, unsigned char val) |
1da177e4 | 1685 | { |
3dafccf2 MS |
1686 | int size = obj_size(cachep); |
1687 | addr = &((char *)addr)[obj_offset(cachep)]; | |
1da177e4 LT |
1688 | |
1689 | memset(addr, val, size); | |
b28a02de | 1690 | *(unsigned char *)(addr + size - 1) = POISON_END; |
1da177e4 LT |
1691 | } |
1692 | ||
1693 | static void dump_line(char *data, int offset, int limit) | |
1694 | { | |
1695 | int i; | |
aa83aa40 DJ |
1696 | unsigned char error = 0; |
1697 | int bad_count = 0; | |
1698 | ||
1da177e4 | 1699 | printk(KERN_ERR "%03x:", offset); |
aa83aa40 DJ |
1700 | for (i = 0; i < limit; i++) { |
1701 | if (data[offset + i] != POISON_FREE) { | |
1702 | error = data[offset + i]; | |
1703 | bad_count++; | |
1704 | } | |
b28a02de | 1705 | printk(" %02x", (unsigned char)data[offset + i]); |
aa83aa40 | 1706 | } |
1da177e4 | 1707 | printk("\n"); |
aa83aa40 DJ |
1708 | |
1709 | if (bad_count == 1) { | |
1710 | error ^= POISON_FREE; | |
1711 | if (!(error & (error - 1))) { | |
1712 | printk(KERN_ERR "Single bit error detected. Probably " | |
1713 | "bad RAM.\n"); | |
1714 | #ifdef CONFIG_X86 | |
1715 | printk(KERN_ERR "Run memtest86+ or a similar memory " | |
1716 | "test tool.\n"); | |
1717 | #else | |
1718 | printk(KERN_ERR "Run a memory test tool.\n"); | |
1719 | #endif | |
1720 | } | |
1721 | } | |
1da177e4 LT |
1722 | } |
1723 | #endif | |
1724 | ||
1725 | #if DEBUG | |
1726 | ||
343e0d7a | 1727 | static void print_objinfo(struct kmem_cache *cachep, void *objp, int lines) |
1da177e4 LT |
1728 | { |
1729 | int i, size; | |
1730 | char *realobj; | |
1731 | ||
1732 | if (cachep->flags & SLAB_RED_ZONE) { | |
1733 | printk(KERN_ERR "Redzone: 0x%lx/0x%lx.\n", | |
a737b3e2 AM |
1734 | *dbg_redzone1(cachep, objp), |
1735 | *dbg_redzone2(cachep, objp)); | |
1da177e4 LT |
1736 | } |
1737 | ||
1738 | if (cachep->flags & SLAB_STORE_USER) { | |
1739 | printk(KERN_ERR "Last user: [<%p>]", | |
a737b3e2 | 1740 | *dbg_userword(cachep, objp)); |
1da177e4 | 1741 | print_symbol("(%s)", |
a737b3e2 | 1742 | (unsigned long)*dbg_userword(cachep, objp)); |
1da177e4 LT |
1743 | printk("\n"); |
1744 | } | |
3dafccf2 MS |
1745 | realobj = (char *)objp + obj_offset(cachep); |
1746 | size = obj_size(cachep); | |
b28a02de | 1747 | for (i = 0; i < size && lines; i += 16, lines--) { |
1da177e4 LT |
1748 | int limit; |
1749 | limit = 16; | |
b28a02de PE |
1750 | if (i + limit > size) |
1751 | limit = size - i; | |
1da177e4 LT |
1752 | dump_line(realobj, i, limit); |
1753 | } | |
1754 | } | |
1755 | ||
343e0d7a | 1756 | static void check_poison_obj(struct kmem_cache *cachep, void *objp) |
1da177e4 LT |
1757 | { |
1758 | char *realobj; | |
1759 | int size, i; | |
1760 | int lines = 0; | |
1761 | ||
3dafccf2 MS |
1762 | realobj = (char *)objp + obj_offset(cachep); |
1763 | size = obj_size(cachep); | |
1da177e4 | 1764 | |
b28a02de | 1765 | for (i = 0; i < size; i++) { |
1da177e4 | 1766 | char exp = POISON_FREE; |
b28a02de | 1767 | if (i == size - 1) |
1da177e4 LT |
1768 | exp = POISON_END; |
1769 | if (realobj[i] != exp) { | |
1770 | int limit; | |
1771 | /* Mismatch ! */ | |
1772 | /* Print header */ | |
1773 | if (lines == 0) { | |
b28a02de | 1774 | printk(KERN_ERR |
a737b3e2 AM |
1775 | "Slab corruption: start=%p, len=%d\n", |
1776 | realobj, size); | |
1da177e4 LT |
1777 | print_objinfo(cachep, objp, 0); |
1778 | } | |
1779 | /* Hexdump the affected line */ | |
b28a02de | 1780 | i = (i / 16) * 16; |
1da177e4 | 1781 | limit = 16; |
b28a02de PE |
1782 | if (i + limit > size) |
1783 | limit = size - i; | |
1da177e4 LT |
1784 | dump_line(realobj, i, limit); |
1785 | i += 16; | |
1786 | lines++; | |
1787 | /* Limit to 5 lines */ | |
1788 | if (lines > 5) | |
1789 | break; | |
1790 | } | |
1791 | } | |
1792 | if (lines != 0) { | |
1793 | /* Print some data about the neighboring objects, if they | |
1794 | * exist: | |
1795 | */ | |
6ed5eb22 | 1796 | struct slab *slabp = virt_to_slab(objp); |
8fea4e96 | 1797 | unsigned int objnr; |
1da177e4 | 1798 | |
8fea4e96 | 1799 | objnr = obj_to_index(cachep, slabp, objp); |
1da177e4 | 1800 | if (objnr) { |
8fea4e96 | 1801 | objp = index_to_obj(cachep, slabp, objnr - 1); |
3dafccf2 | 1802 | realobj = (char *)objp + obj_offset(cachep); |
1da177e4 | 1803 | printk(KERN_ERR "Prev obj: start=%p, len=%d\n", |
b28a02de | 1804 | realobj, size); |
1da177e4 LT |
1805 | print_objinfo(cachep, objp, 2); |
1806 | } | |
b28a02de | 1807 | if (objnr + 1 < cachep->num) { |
8fea4e96 | 1808 | objp = index_to_obj(cachep, slabp, objnr + 1); |
3dafccf2 | 1809 | realobj = (char *)objp + obj_offset(cachep); |
1da177e4 | 1810 | printk(KERN_ERR "Next obj: start=%p, len=%d\n", |
b28a02de | 1811 | realobj, size); |
1da177e4 LT |
1812 | print_objinfo(cachep, objp, 2); |
1813 | } | |
1814 | } | |
1815 | } | |
1816 | #endif | |
1817 | ||
12dd36fa MD |
1818 | #if DEBUG |
1819 | /** | |
911851e6 RD |
1820 | * slab_destroy_objs - destroy a slab and its objects |
1821 | * @cachep: cache pointer being destroyed | |
1822 | * @slabp: slab pointer being destroyed | |
1823 | * | |
1824 | * Call the registered destructor for each object in a slab that is being | |
1825 | * destroyed. | |
1da177e4 | 1826 | */ |
343e0d7a | 1827 | static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp) |
1da177e4 | 1828 | { |
1da177e4 LT |
1829 | int i; |
1830 | for (i = 0; i < cachep->num; i++) { | |
8fea4e96 | 1831 | void *objp = index_to_obj(cachep, slabp, i); |
1da177e4 LT |
1832 | |
1833 | if (cachep->flags & SLAB_POISON) { | |
1834 | #ifdef CONFIG_DEBUG_PAGEALLOC | |
a737b3e2 AM |
1835 | if (cachep->buffer_size % PAGE_SIZE == 0 && |
1836 | OFF_SLAB(cachep)) | |
b28a02de | 1837 | kernel_map_pages(virt_to_page(objp), |
a737b3e2 | 1838 | cachep->buffer_size / PAGE_SIZE, 1); |
1da177e4 LT |
1839 | else |
1840 | check_poison_obj(cachep, objp); | |
1841 | #else | |
1842 | check_poison_obj(cachep, objp); | |
1843 | #endif | |
1844 | } | |
1845 | if (cachep->flags & SLAB_RED_ZONE) { | |
1846 | if (*dbg_redzone1(cachep, objp) != RED_INACTIVE) | |
1847 | slab_error(cachep, "start of a freed object " | |
b28a02de | 1848 | "was overwritten"); |
1da177e4 LT |
1849 | if (*dbg_redzone2(cachep, objp) != RED_INACTIVE) |
1850 | slab_error(cachep, "end of a freed object " | |
b28a02de | 1851 | "was overwritten"); |
1da177e4 LT |
1852 | } |
1853 | if (cachep->dtor && !(cachep->flags & SLAB_POISON)) | |
3dafccf2 | 1854 | (cachep->dtor) (objp + obj_offset(cachep), cachep, 0); |
1da177e4 | 1855 | } |
12dd36fa | 1856 | } |
1da177e4 | 1857 | #else |
343e0d7a | 1858 | static void slab_destroy_objs(struct kmem_cache *cachep, struct slab *slabp) |
12dd36fa | 1859 | { |
1da177e4 LT |
1860 | if (cachep->dtor) { |
1861 | int i; | |
1862 | for (i = 0; i < cachep->num; i++) { | |
8fea4e96 | 1863 | void *objp = index_to_obj(cachep, slabp, i); |
b28a02de | 1864 | (cachep->dtor) (objp, cachep, 0); |
1da177e4 LT |
1865 | } |
1866 | } | |
12dd36fa | 1867 | } |
1da177e4 LT |
1868 | #endif |
1869 | ||
911851e6 RD |
1870 | /** |
1871 | * slab_destroy - destroy and release all objects in a slab | |
1872 | * @cachep: cache pointer being destroyed | |
1873 | * @slabp: slab pointer being destroyed | |
1874 | * | |
12dd36fa | 1875 | * Destroy all the objs in a slab, and release the mem back to the system. |
a737b3e2 AM |
1876 | * Before calling the slab must have been unlinked from the cache. The |
1877 | * cache-lock is not held/needed. | |
12dd36fa | 1878 | */ |
343e0d7a | 1879 | static void slab_destroy(struct kmem_cache *cachep, struct slab *slabp) |
12dd36fa MD |
1880 | { |
1881 | void *addr = slabp->s_mem - slabp->colouroff; | |
1882 | ||
1883 | slab_destroy_objs(cachep, slabp); | |
1da177e4 LT |
1884 | if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) { |
1885 | struct slab_rcu *slab_rcu; | |
1886 | ||
b28a02de | 1887 | slab_rcu = (struct slab_rcu *)slabp; |
1da177e4 LT |
1888 | slab_rcu->cachep = cachep; |
1889 | slab_rcu->addr = addr; | |
1890 | call_rcu(&slab_rcu->head, kmem_rcu_free); | |
1891 | } else { | |
1892 | kmem_freepages(cachep, addr); | |
873623df IM |
1893 | if (OFF_SLAB(cachep)) |
1894 | kmem_cache_free(cachep->slabp_cache, slabp); | |
1da177e4 LT |
1895 | } |
1896 | } | |
1897 | ||
a737b3e2 AM |
1898 | /* |
1899 | * For setting up all the kmem_list3s for cache whose buffer_size is same as | |
1900 | * size of kmem_list3. | |
1901 | */ | |
343e0d7a | 1902 | static void set_up_list3s(struct kmem_cache *cachep, int index) |
e498be7d CL |
1903 | { |
1904 | int node; | |
1905 | ||
1906 | for_each_online_node(node) { | |
b28a02de | 1907 | cachep->nodelists[node] = &initkmem_list3[index + node]; |
e498be7d | 1908 | cachep->nodelists[node]->next_reap = jiffies + |
b28a02de PE |
1909 | REAPTIMEOUT_LIST3 + |
1910 | ((unsigned long)cachep) % REAPTIMEOUT_LIST3; | |
e498be7d CL |
1911 | } |
1912 | } | |
1913 | ||
117f6eb1 CL |
1914 | static void __kmem_cache_destroy(struct kmem_cache *cachep) |
1915 | { | |
1916 | int i; | |
1917 | struct kmem_list3 *l3; | |
1918 | ||
1919 | for_each_online_cpu(i) | |
1920 | kfree(cachep->array[i]); | |
1921 | ||
1922 | /* NUMA: free the list3 structures */ | |
1923 | for_each_online_node(i) { | |
1924 | l3 = cachep->nodelists[i]; | |
1925 | if (l3) { | |
1926 | kfree(l3->shared); | |
1927 | free_alien_cache(l3->alien); | |
1928 | kfree(l3); | |
1929 | } | |
1930 | } | |
1931 | kmem_cache_free(&cache_cache, cachep); | |
1932 | } | |
1933 | ||
1934 | ||
4d268eba | 1935 | /** |
a70773dd RD |
1936 | * calculate_slab_order - calculate size (page order) of slabs |
1937 | * @cachep: pointer to the cache that is being created | |
1938 | * @size: size of objects to be created in this cache. | |
1939 | * @align: required alignment for the objects. | |
1940 | * @flags: slab allocation flags | |
1941 | * | |
1942 | * Also calculates the number of objects per slab. | |
4d268eba PE |
1943 | * |
1944 | * This could be made much more intelligent. For now, try to avoid using | |
1945 | * high order pages for slabs. When the gfp() functions are more friendly | |
1946 | * towards high-order requests, this should be changed. | |
1947 | */ | |
a737b3e2 | 1948 | static size_t calculate_slab_order(struct kmem_cache *cachep, |
ee13d785 | 1949 | size_t size, size_t align, unsigned long flags) |
4d268eba | 1950 | { |
b1ab41c4 | 1951 | unsigned long offslab_limit; |
4d268eba | 1952 | size_t left_over = 0; |
9888e6fa | 1953 | int gfporder; |
4d268eba | 1954 | |
a737b3e2 | 1955 | for (gfporder = 0; gfporder <= MAX_GFP_ORDER; gfporder++) { |
4d268eba PE |
1956 | unsigned int num; |
1957 | size_t remainder; | |
1958 | ||
9888e6fa | 1959 | cache_estimate(gfporder, size, align, flags, &remainder, &num); |
4d268eba PE |
1960 | if (!num) |
1961 | continue; | |
9888e6fa | 1962 | |
b1ab41c4 IM |
1963 | if (flags & CFLGS_OFF_SLAB) { |
1964 | /* | |
1965 | * Max number of objs-per-slab for caches which | |
1966 | * use off-slab slabs. Needed to avoid a possible | |
1967 | * looping condition in cache_grow(). | |
1968 | */ | |
1969 | offslab_limit = size - sizeof(struct slab); | |
1970 | offslab_limit /= sizeof(kmem_bufctl_t); | |
1971 | ||
1972 | if (num > offslab_limit) | |
1973 | break; | |
1974 | } | |
4d268eba | 1975 | |
9888e6fa | 1976 | /* Found something acceptable - save it away */ |
4d268eba | 1977 | cachep->num = num; |
9888e6fa | 1978 | cachep->gfporder = gfporder; |
4d268eba PE |
1979 | left_over = remainder; |
1980 | ||
f78bb8ad LT |
1981 | /* |
1982 | * A VFS-reclaimable slab tends to have most allocations | |
1983 | * as GFP_NOFS and we really don't want to have to be allocating | |
1984 | * higher-order pages when we are unable to shrink dcache. | |
1985 | */ | |
1986 | if (flags & SLAB_RECLAIM_ACCOUNT) | |
1987 | break; | |
1988 | ||
4d268eba PE |
1989 | /* |
1990 | * Large number of objects is good, but very large slabs are | |
1991 | * currently bad for the gfp()s. | |
1992 | */ | |
9888e6fa | 1993 | if (gfporder >= slab_break_gfp_order) |
4d268eba PE |
1994 | break; |
1995 | ||
9888e6fa LT |
1996 | /* |
1997 | * Acceptable internal fragmentation? | |
1998 | */ | |
a737b3e2 | 1999 | if (left_over * 8 <= (PAGE_SIZE << gfporder)) |
4d268eba PE |
2000 | break; |
2001 | } | |
2002 | return left_over; | |
2003 | } | |
2004 | ||
2ed3a4ef | 2005 | static int setup_cpu_cache(struct kmem_cache *cachep) |
f30cf7d1 | 2006 | { |
2ed3a4ef CL |
2007 | if (g_cpucache_up == FULL) |
2008 | return enable_cpucache(cachep); | |
2009 | ||
f30cf7d1 PE |
2010 | if (g_cpucache_up == NONE) { |
2011 | /* | |
2012 | * Note: the first kmem_cache_create must create the cache | |
2013 | * that's used by kmalloc(24), otherwise the creation of | |
2014 | * further caches will BUG(). | |
2015 | */ | |
2016 | cachep->array[smp_processor_id()] = &initarray_generic.cache; | |
2017 | ||
2018 | /* | |
2019 | * If the cache that's used by kmalloc(sizeof(kmem_list3)) is | |
2020 | * the first cache, then we need to set up all its list3s, | |
2021 | * otherwise the creation of further caches will BUG(). | |
2022 | */ | |
2023 | set_up_list3s(cachep, SIZE_AC); | |
2024 | if (INDEX_AC == INDEX_L3) | |
2025 | g_cpucache_up = PARTIAL_L3; | |
2026 | else | |
2027 | g_cpucache_up = PARTIAL_AC; | |
2028 | } else { | |
2029 | cachep->array[smp_processor_id()] = | |
2030 | kmalloc(sizeof(struct arraycache_init), GFP_KERNEL); | |
2031 | ||
2032 | if (g_cpucache_up == PARTIAL_AC) { | |
2033 | set_up_list3s(cachep, SIZE_L3); | |
2034 | g_cpucache_up = PARTIAL_L3; | |
2035 | } else { | |
2036 | int node; | |
2037 | for_each_online_node(node) { | |
2038 | cachep->nodelists[node] = | |
2039 | kmalloc_node(sizeof(struct kmem_list3), | |
2040 | GFP_KERNEL, node); | |
2041 | BUG_ON(!cachep->nodelists[node]); | |
2042 | kmem_list3_init(cachep->nodelists[node]); | |
2043 | } | |
2044 | } | |
2045 | } | |
2046 | cachep->nodelists[numa_node_id()]->next_reap = | |
2047 | jiffies + REAPTIMEOUT_LIST3 + | |
2048 | ((unsigned long)cachep) % REAPTIMEOUT_LIST3; | |
2049 | ||
2050 | cpu_cache_get(cachep)->avail = 0; | |
2051 | cpu_cache_get(cachep)->limit = BOOT_CPUCACHE_ENTRIES; | |
2052 | cpu_cache_get(cachep)->batchcount = 1; | |
2053 | cpu_cache_get(cachep)->touched = 0; | |
2054 | cachep->batchcount = 1; | |
2055 | cachep->limit = BOOT_CPUCACHE_ENTRIES; | |
2ed3a4ef | 2056 | return 0; |
f30cf7d1 PE |
2057 | } |
2058 | ||
1da177e4 LT |
2059 | /** |
2060 | * kmem_cache_create - Create a cache. | |
2061 | * @name: A string which is used in /proc/slabinfo to identify this cache. | |
2062 | * @size: The size of objects to be created in this cache. | |
2063 | * @align: The required alignment for the objects. | |
2064 | * @flags: SLAB flags | |
2065 | * @ctor: A constructor for the objects. | |
2066 | * @dtor: A destructor for the objects. | |
2067 | * | |
2068 | * Returns a ptr to the cache on success, NULL on failure. | |
2069 | * Cannot be called within a int, but can be interrupted. | |
2070 | * The @ctor is run when new pages are allocated by the cache | |
2071 | * and the @dtor is run before the pages are handed back. | |
2072 | * | |
2073 | * @name must be valid until the cache is destroyed. This implies that | |
a737b3e2 AM |
2074 | * the module calling this has to destroy the cache before getting unloaded. |
2075 | * | |
1da177e4 LT |
2076 | * The flags are |
2077 | * | |
2078 | * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5) | |
2079 | * to catch references to uninitialised memory. | |
2080 | * | |
2081 | * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check | |
2082 | * for buffer overruns. | |
2083 | * | |
1da177e4 LT |
2084 | * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware |
2085 | * cacheline. This can be beneficial if you're counting cycles as closely | |
2086 | * as davem. | |
2087 | */ | |
343e0d7a | 2088 | struct kmem_cache * |
1da177e4 | 2089 | kmem_cache_create (const char *name, size_t size, size_t align, |
a737b3e2 AM |
2090 | unsigned long flags, |
2091 | void (*ctor)(void*, struct kmem_cache *, unsigned long), | |
343e0d7a | 2092 | void (*dtor)(void*, struct kmem_cache *, unsigned long)) |
1da177e4 LT |
2093 | { |
2094 | size_t left_over, slab_size, ralign; | |
7a7c381d | 2095 | struct kmem_cache *cachep = NULL, *pc; |
1da177e4 LT |
2096 | |
2097 | /* | |
2098 | * Sanity checks... these are all serious usage bugs. | |
2099 | */ | |
a737b3e2 | 2100 | if (!name || in_interrupt() || (size < BYTES_PER_WORD) || |
b28a02de | 2101 | (size > (1 << MAX_OBJ_ORDER) * PAGE_SIZE) || (dtor && !ctor)) { |
a737b3e2 AM |
2102 | printk(KERN_ERR "%s: Early error in slab %s\n", __FUNCTION__, |
2103 | name); | |
b28a02de PE |
2104 | BUG(); |
2105 | } | |
1da177e4 | 2106 | |
f0188f47 | 2107 | /* |
8f5be20b RT |
2108 | * We use cache_chain_mutex to ensure a consistent view of |
2109 | * cpu_online_map as well. Please see cpuup_callback | |
f0188f47 | 2110 | */ |
fc0abb14 | 2111 | mutex_lock(&cache_chain_mutex); |
4f12bb4f | 2112 | |
7a7c381d | 2113 | list_for_each_entry(pc, &cache_chain, next) { |
4f12bb4f AM |
2114 | mm_segment_t old_fs = get_fs(); |
2115 | char tmp; | |
2116 | int res; | |
2117 | ||
2118 | /* | |
2119 | * This happens when the module gets unloaded and doesn't | |
2120 | * destroy its slab cache and no-one else reuses the vmalloc | |
2121 | * area of the module. Print a warning. | |
2122 | */ | |
2123 | set_fs(KERNEL_DS); | |
2124 | res = __get_user(tmp, pc->name); | |
2125 | set_fs(old_fs); | |
2126 | if (res) { | |
2127 | printk("SLAB: cache with size %d has lost its name\n", | |
3dafccf2 | 2128 | pc->buffer_size); |
4f12bb4f AM |
2129 | continue; |
2130 | } | |
2131 | ||
b28a02de | 2132 | if (!strcmp(pc->name, name)) { |
4f12bb4f AM |
2133 | printk("kmem_cache_create: duplicate cache %s\n", name); |
2134 | dump_stack(); | |
2135 | goto oops; | |
2136 | } | |
2137 | } | |
2138 | ||
1da177e4 LT |
2139 | #if DEBUG |
2140 | WARN_ON(strchr(name, ' ')); /* It confuses parsers */ | |
2141 | if ((flags & SLAB_DEBUG_INITIAL) && !ctor) { | |
2142 | /* No constructor, but inital state check requested */ | |
2143 | printk(KERN_ERR "%s: No con, but init state check " | |
b28a02de | 2144 | "requested - %s\n", __FUNCTION__, name); |
1da177e4 LT |
2145 | flags &= ~SLAB_DEBUG_INITIAL; |
2146 | } | |
1da177e4 LT |
2147 | #if FORCED_DEBUG |
2148 | /* | |
2149 | * Enable redzoning and last user accounting, except for caches with | |
2150 | * large objects, if the increased size would increase the object size | |
2151 | * above the next power of two: caches with object sizes just above a | |
2152 | * power of two have a significant amount of internal fragmentation. | |
2153 | */ | |
a737b3e2 | 2154 | if (size < 4096 || fls(size - 1) == fls(size-1 + 3 * BYTES_PER_WORD)) |
b28a02de | 2155 | flags |= SLAB_RED_ZONE | SLAB_STORE_USER; |
1da177e4 LT |
2156 | if (!(flags & SLAB_DESTROY_BY_RCU)) |
2157 | flags |= SLAB_POISON; | |
2158 | #endif | |
2159 | if (flags & SLAB_DESTROY_BY_RCU) | |
2160 | BUG_ON(flags & SLAB_POISON); | |
2161 | #endif | |
2162 | if (flags & SLAB_DESTROY_BY_RCU) | |
2163 | BUG_ON(dtor); | |
2164 | ||
2165 | /* | |
a737b3e2 AM |
2166 | * Always checks flags, a caller might be expecting debug support which |
2167 | * isn't available. | |
1da177e4 | 2168 | */ |
40094fa6 | 2169 | BUG_ON(flags & ~CREATE_MASK); |
1da177e4 | 2170 | |
a737b3e2 AM |
2171 | /* |
2172 | * Check that size is in terms of words. This is needed to avoid | |
1da177e4 LT |
2173 | * unaligned accesses for some archs when redzoning is used, and makes |
2174 | * sure any on-slab bufctl's are also correctly aligned. | |
2175 | */ | |
b28a02de PE |
2176 | if (size & (BYTES_PER_WORD - 1)) { |
2177 | size += (BYTES_PER_WORD - 1); | |
2178 | size &= ~(BYTES_PER_WORD - 1); | |
1da177e4 LT |
2179 | } |
2180 | ||
a737b3e2 AM |
2181 | /* calculate the final buffer alignment: */ |
2182 | ||
1da177e4 LT |
2183 | /* 1) arch recommendation: can be overridden for debug */ |
2184 | if (flags & SLAB_HWCACHE_ALIGN) { | |
a737b3e2 AM |
2185 | /* |
2186 | * Default alignment: as specified by the arch code. Except if | |
2187 | * an object is really small, then squeeze multiple objects into | |
2188 | * one cacheline. | |
1da177e4 LT |
2189 | */ |
2190 | ralign = cache_line_size(); | |
b28a02de | 2191 | while (size <= ralign / 2) |
1da177e4 LT |
2192 | ralign /= 2; |
2193 | } else { | |
2194 | ralign = BYTES_PER_WORD; | |
2195 | } | |
ca5f9703 PE |
2196 | |
2197 | /* | |
2198 | * Redzoning and user store require word alignment. Note this will be | |
2199 | * overridden by architecture or caller mandated alignment if either | |
2200 | * is greater than BYTES_PER_WORD. | |
2201 | */ | |
2202 | if (flags & SLAB_RED_ZONE || flags & SLAB_STORE_USER) | |
2203 | ralign = BYTES_PER_WORD; | |
2204 | ||
a44b56d3 | 2205 | /* 2) arch mandated alignment */ |
1da177e4 LT |
2206 | if (ralign < ARCH_SLAB_MINALIGN) { |
2207 | ralign = ARCH_SLAB_MINALIGN; | |
1da177e4 | 2208 | } |
a44b56d3 | 2209 | /* 3) caller mandated alignment */ |
1da177e4 LT |
2210 | if (ralign < align) { |
2211 | ralign = align; | |
1da177e4 | 2212 | } |
a44b56d3 KH |
2213 | /* disable debug if necessary */ |
2214 | if (ralign > BYTES_PER_WORD) | |
2215 | flags &= ~(SLAB_RED_ZONE | SLAB_STORE_USER); | |
a737b3e2 | 2216 | /* |
ca5f9703 | 2217 | * 4) Store it. |
1da177e4 LT |
2218 | */ |
2219 | align = ralign; | |
2220 | ||
2221 | /* Get cache's description obj. */ | |
c5e3b83e | 2222 | cachep = kmem_cache_zalloc(&cache_cache, SLAB_KERNEL); |
1da177e4 | 2223 | if (!cachep) |
4f12bb4f | 2224 | goto oops; |
1da177e4 LT |
2225 | |
2226 | #if DEBUG | |
3dafccf2 | 2227 | cachep->obj_size = size; |
1da177e4 | 2228 | |
ca5f9703 PE |
2229 | /* |
2230 | * Both debugging options require word-alignment which is calculated | |
2231 | * into align above. | |
2232 | */ | |
1da177e4 | 2233 | if (flags & SLAB_RED_ZONE) { |
1da177e4 | 2234 | /* add space for red zone words */ |
3dafccf2 | 2235 | cachep->obj_offset += BYTES_PER_WORD; |
b28a02de | 2236 | size += 2 * BYTES_PER_WORD; |
1da177e4 LT |
2237 | } |
2238 | if (flags & SLAB_STORE_USER) { | |
ca5f9703 PE |
2239 | /* user store requires one word storage behind the end of |
2240 | * the real object. | |
1da177e4 | 2241 | */ |
1da177e4 LT |
2242 | size += BYTES_PER_WORD; |
2243 | } | |
2244 | #if FORCED_DEBUG && defined(CONFIG_DEBUG_PAGEALLOC) | |
b28a02de | 2245 | if (size >= malloc_sizes[INDEX_L3 + 1].cs_size |
3dafccf2 MS |
2246 | && cachep->obj_size > cache_line_size() && size < PAGE_SIZE) { |
2247 | cachep->obj_offset += PAGE_SIZE - size; | |
1da177e4 LT |
2248 | size = PAGE_SIZE; |
2249 | } | |
2250 | #endif | |
2251 | #endif | |
2252 | ||
e0a42726 IM |
2253 | /* |
2254 | * Determine if the slab management is 'on' or 'off' slab. | |
2255 | * (bootstrapping cannot cope with offslab caches so don't do | |
2256 | * it too early on.) | |
2257 | */ | |
2258 | if ((size >= (PAGE_SIZE >> 3)) && !slab_early_init) | |
1da177e4 LT |
2259 | /* |
2260 | * Size is large, assume best to place the slab management obj | |
2261 | * off-slab (should allow better packing of objs). | |
2262 | */ | |
2263 | flags |= CFLGS_OFF_SLAB; | |
2264 | ||
2265 | size = ALIGN(size, align); | |
2266 | ||
f78bb8ad | 2267 | left_over = calculate_slab_order(cachep, size, align, flags); |
1da177e4 LT |
2268 | |
2269 | if (!cachep->num) { | |
2270 | printk("kmem_cache_create: couldn't create cache %s.\n", name); | |
2271 | kmem_cache_free(&cache_cache, cachep); | |
2272 | cachep = NULL; | |
4f12bb4f | 2273 | goto oops; |
1da177e4 | 2274 | } |
b28a02de PE |
2275 | slab_size = ALIGN(cachep->num * sizeof(kmem_bufctl_t) |
2276 | + sizeof(struct slab), align); | |
1da177e4 LT |
2277 | |
2278 | /* | |
2279 | * If the slab has been placed off-slab, and we have enough space then | |
2280 | * move it on-slab. This is at the expense of any extra colouring. | |
2281 | */ | |
2282 | if (flags & CFLGS_OFF_SLAB && left_over >= slab_size) { | |
2283 | flags &= ~CFLGS_OFF_SLAB; | |
2284 | left_over -= slab_size; | |
2285 | } | |
2286 | ||
2287 | if (flags & CFLGS_OFF_SLAB) { | |
2288 | /* really off slab. No need for manual alignment */ | |
b28a02de PE |
2289 | slab_size = |
2290 | cachep->num * sizeof(kmem_bufctl_t) + sizeof(struct slab); | |
1da177e4 LT |
2291 | } |
2292 | ||
2293 | cachep->colour_off = cache_line_size(); | |
2294 | /* Offset must be a multiple of the alignment. */ | |
2295 | if (cachep->colour_off < align) | |
2296 | cachep->colour_off = align; | |
b28a02de | 2297 | cachep->colour = left_over / cachep->colour_off; |
1da177e4 LT |
2298 | cachep->slab_size = slab_size; |
2299 | cachep->flags = flags; | |
2300 | cachep->gfpflags = 0; | |
2301 | if (flags & SLAB_CACHE_DMA) | |
2302 | cachep->gfpflags |= GFP_DMA; | |
3dafccf2 | 2303 | cachep->buffer_size = size; |
1da177e4 | 2304 | |
e5ac9c5a | 2305 | if (flags & CFLGS_OFF_SLAB) { |
b2d55073 | 2306 | cachep->slabp_cache = kmem_find_general_cachep(slab_size, 0u); |
e5ac9c5a RT |
2307 | /* |
2308 | * This is a possibility for one of the malloc_sizes caches. | |
2309 | * But since we go off slab only for object size greater than | |
2310 | * PAGE_SIZE/8, and malloc_sizes gets created in ascending order, | |
2311 | * this should not happen at all. | |
2312 | * But leave a BUG_ON for some lucky dude. | |
2313 | */ | |
2314 | BUG_ON(!cachep->slabp_cache); | |
2315 | } | |
1da177e4 LT |
2316 | cachep->ctor = ctor; |
2317 | cachep->dtor = dtor; | |
2318 | cachep->name = name; | |
2319 | ||
2ed3a4ef CL |
2320 | if (setup_cpu_cache(cachep)) { |
2321 | __kmem_cache_destroy(cachep); | |
2322 | cachep = NULL; | |
2323 | goto oops; | |
2324 | } | |
1da177e4 | 2325 | |
1da177e4 LT |
2326 | /* cache setup completed, link it into the list */ |
2327 | list_add(&cachep->next, &cache_chain); | |
a737b3e2 | 2328 | oops: |
1da177e4 LT |
2329 | if (!cachep && (flags & SLAB_PANIC)) |
2330 | panic("kmem_cache_create(): failed to create slab `%s'\n", | |
b28a02de | 2331 | name); |
fc0abb14 | 2332 | mutex_unlock(&cache_chain_mutex); |
1da177e4 LT |
2333 | return cachep; |
2334 | } | |
2335 | EXPORT_SYMBOL(kmem_cache_create); | |
2336 | ||
2337 | #if DEBUG | |
2338 | static void check_irq_off(void) | |
2339 | { | |
2340 | BUG_ON(!irqs_disabled()); | |
2341 | } | |
2342 | ||
2343 | static void check_irq_on(void) | |
2344 | { | |
2345 | BUG_ON(irqs_disabled()); | |
2346 | } | |
2347 | ||
343e0d7a | 2348 | static void check_spinlock_acquired(struct kmem_cache *cachep) |
1da177e4 LT |
2349 | { |
2350 | #ifdef CONFIG_SMP | |
2351 | check_irq_off(); | |
e498be7d | 2352 | assert_spin_locked(&cachep->nodelists[numa_node_id()]->list_lock); |
1da177e4 LT |
2353 | #endif |
2354 | } | |
e498be7d | 2355 | |
343e0d7a | 2356 | static void check_spinlock_acquired_node(struct kmem_cache *cachep, int node) |
e498be7d CL |
2357 | { |
2358 | #ifdef CONFIG_SMP | |
2359 | check_irq_off(); | |
2360 | assert_spin_locked(&cachep->nodelists[node]->list_lock); | |
2361 | #endif | |
2362 | } | |
2363 | ||
1da177e4 LT |
2364 | #else |
2365 | #define check_irq_off() do { } while(0) | |
2366 | #define check_irq_on() do { } while(0) | |
2367 | #define check_spinlock_acquired(x) do { } while(0) | |
e498be7d | 2368 | #define check_spinlock_acquired_node(x, y) do { } while(0) |
1da177e4 LT |
2369 | #endif |
2370 | ||
aab2207c CL |
2371 | static void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3, |
2372 | struct array_cache *ac, | |
2373 | int force, int node); | |
2374 | ||
1da177e4 LT |
2375 | static void do_drain(void *arg) |
2376 | { | |
a737b3e2 | 2377 | struct kmem_cache *cachep = arg; |
1da177e4 | 2378 | struct array_cache *ac; |
ff69416e | 2379 | int node = numa_node_id(); |
1da177e4 LT |
2380 | |
2381 | check_irq_off(); | |
9a2dba4b | 2382 | ac = cpu_cache_get(cachep); |
ff69416e CL |
2383 | spin_lock(&cachep->nodelists[node]->list_lock); |
2384 | free_block(cachep, ac->entry, ac->avail, node); | |
2385 | spin_unlock(&cachep->nodelists[node]->list_lock); | |
1da177e4 LT |
2386 | ac->avail = 0; |
2387 | } | |
2388 | ||
343e0d7a | 2389 | static void drain_cpu_caches(struct kmem_cache *cachep) |
1da177e4 | 2390 | { |
e498be7d CL |
2391 | struct kmem_list3 *l3; |
2392 | int node; | |
2393 | ||
a07fa394 | 2394 | on_each_cpu(do_drain, cachep, 1, 1); |
1da177e4 | 2395 | check_irq_on(); |
b28a02de | 2396 | for_each_online_node(node) { |
e498be7d | 2397 | l3 = cachep->nodelists[node]; |
a4523a8b RD |
2398 | if (l3 && l3->alien) |
2399 | drain_alien_cache(cachep, l3->alien); | |
2400 | } | |
2401 | ||
2402 | for_each_online_node(node) { | |
2403 | l3 = cachep->nodelists[node]; | |
2404 | if (l3) | |
aab2207c | 2405 | drain_array(cachep, l3, l3->shared, 1, node); |
e498be7d | 2406 | } |
1da177e4 LT |
2407 | } |
2408 | ||
ed11d9eb CL |
2409 | /* |
2410 | * Remove slabs from the list of free slabs. | |
2411 | * Specify the number of slabs to drain in tofree. | |
2412 | * | |
2413 | * Returns the actual number of slabs released. | |
2414 | */ | |
2415 | static int drain_freelist(struct kmem_cache *cache, | |
2416 | struct kmem_list3 *l3, int tofree) | |
1da177e4 | 2417 | { |
ed11d9eb CL |
2418 | struct list_head *p; |
2419 | int nr_freed; | |
1da177e4 | 2420 | struct slab *slabp; |
1da177e4 | 2421 | |
ed11d9eb CL |
2422 | nr_freed = 0; |
2423 | while (nr_freed < tofree && !list_empty(&l3->slabs_free)) { | |
1da177e4 | 2424 | |
ed11d9eb | 2425 | spin_lock_irq(&l3->list_lock); |
e498be7d | 2426 | p = l3->slabs_free.prev; |
ed11d9eb CL |
2427 | if (p == &l3->slabs_free) { |
2428 | spin_unlock_irq(&l3->list_lock); | |
2429 | goto out; | |
2430 | } | |
1da177e4 | 2431 | |
ed11d9eb | 2432 | slabp = list_entry(p, struct slab, list); |
1da177e4 | 2433 | #if DEBUG |
40094fa6 | 2434 | BUG_ON(slabp->inuse); |
1da177e4 LT |
2435 | #endif |
2436 | list_del(&slabp->list); | |
ed11d9eb CL |
2437 | /* |
2438 | * Safe to drop the lock. The slab is no longer linked | |
2439 | * to the cache. | |
2440 | */ | |
2441 | l3->free_objects -= cache->num; | |
e498be7d | 2442 | spin_unlock_irq(&l3->list_lock); |
ed11d9eb CL |
2443 | slab_destroy(cache, slabp); |
2444 | nr_freed++; | |
1da177e4 | 2445 | } |
ed11d9eb CL |
2446 | out: |
2447 | return nr_freed; | |
1da177e4 LT |
2448 | } |
2449 | ||
8f5be20b | 2450 | /* Called with cache_chain_mutex held to protect against cpu hotplug */ |
343e0d7a | 2451 | static int __cache_shrink(struct kmem_cache *cachep) |
e498be7d CL |
2452 | { |
2453 | int ret = 0, i = 0; | |
2454 | struct kmem_list3 *l3; | |
2455 | ||
2456 | drain_cpu_caches(cachep); | |
2457 | ||
2458 | check_irq_on(); | |
2459 | for_each_online_node(i) { | |
2460 | l3 = cachep->nodelists[i]; | |
ed11d9eb CL |
2461 | if (!l3) |
2462 | continue; | |
2463 | ||
2464 | drain_freelist(cachep, l3, l3->free_objects); | |
2465 | ||
2466 | ret += !list_empty(&l3->slabs_full) || | |
2467 | !list_empty(&l3->slabs_partial); | |
e498be7d CL |
2468 | } |
2469 | return (ret ? 1 : 0); | |
2470 | } | |
2471 | ||
1da177e4 LT |
2472 | /** |
2473 | * kmem_cache_shrink - Shrink a cache. | |
2474 | * @cachep: The cache to shrink. | |
2475 | * | |
2476 | * Releases as many slabs as possible for a cache. | |
2477 | * To help debugging, a zero exit status indicates all slabs were released. | |
2478 | */ | |
343e0d7a | 2479 | int kmem_cache_shrink(struct kmem_cache *cachep) |
1da177e4 | 2480 | { |
8f5be20b | 2481 | int ret; |
40094fa6 | 2482 | BUG_ON(!cachep || in_interrupt()); |
1da177e4 | 2483 | |
8f5be20b RT |
2484 | mutex_lock(&cache_chain_mutex); |
2485 | ret = __cache_shrink(cachep); | |
2486 | mutex_unlock(&cache_chain_mutex); | |
2487 | return ret; | |
1da177e4 LT |
2488 | } |
2489 | EXPORT_SYMBOL(kmem_cache_shrink); | |
2490 | ||
2491 | /** | |
2492 | * kmem_cache_destroy - delete a cache | |
2493 | * @cachep: the cache to destroy | |
2494 | * | |
343e0d7a | 2495 | * Remove a struct kmem_cache object from the slab cache. |
1da177e4 LT |
2496 | * |
2497 | * It is expected this function will be called by a module when it is | |
2498 | * unloaded. This will remove the cache completely, and avoid a duplicate | |
2499 | * cache being allocated each time a module is loaded and unloaded, if the | |
2500 | * module doesn't have persistent in-kernel storage across loads and unloads. | |
2501 | * | |
2502 | * The cache must be empty before calling this function. | |
2503 | * | |
2504 | * The caller must guarantee that noone will allocate memory from the cache | |
2505 | * during the kmem_cache_destroy(). | |
2506 | */ | |
133d205a | 2507 | void kmem_cache_destroy(struct kmem_cache *cachep) |
1da177e4 | 2508 | { |
40094fa6 | 2509 | BUG_ON(!cachep || in_interrupt()); |
1da177e4 | 2510 | |
1da177e4 | 2511 | /* Find the cache in the chain of caches. */ |
fc0abb14 | 2512 | mutex_lock(&cache_chain_mutex); |
1da177e4 LT |
2513 | /* |
2514 | * the chain is never empty, cache_cache is never destroyed | |
2515 | */ | |
2516 | list_del(&cachep->next); | |
1da177e4 LT |
2517 | if (__cache_shrink(cachep)) { |
2518 | slab_error(cachep, "Can't free all objects"); | |
b28a02de | 2519 | list_add(&cachep->next, &cache_chain); |
fc0abb14 | 2520 | mutex_unlock(&cache_chain_mutex); |
133d205a | 2521 | return; |
1da177e4 LT |
2522 | } |
2523 | ||
2524 | if (unlikely(cachep->flags & SLAB_DESTROY_BY_RCU)) | |
fbd568a3 | 2525 | synchronize_rcu(); |
1da177e4 | 2526 | |
117f6eb1 | 2527 | __kmem_cache_destroy(cachep); |
8f5be20b | 2528 | mutex_unlock(&cache_chain_mutex); |
1da177e4 LT |
2529 | } |
2530 | EXPORT_SYMBOL(kmem_cache_destroy); | |
2531 | ||
e5ac9c5a RT |
2532 | /* |
2533 | * Get the memory for a slab management obj. | |
2534 | * For a slab cache when the slab descriptor is off-slab, slab descriptors | |
2535 | * always come from malloc_sizes caches. The slab descriptor cannot | |
2536 | * come from the same cache which is getting created because, | |
2537 | * when we are searching for an appropriate cache for these | |
2538 | * descriptors in kmem_cache_create, we search through the malloc_sizes array. | |
2539 | * If we are creating a malloc_sizes cache here it would not be visible to | |
2540 | * kmem_find_general_cachep till the initialization is complete. | |
2541 | * Hence we cannot have slabp_cache same as the original cache. | |
2542 | */ | |
343e0d7a | 2543 | static struct slab *alloc_slabmgmt(struct kmem_cache *cachep, void *objp, |
5b74ada7 RT |
2544 | int colour_off, gfp_t local_flags, |
2545 | int nodeid) | |
1da177e4 LT |
2546 | { |
2547 | struct slab *slabp; | |
b28a02de | 2548 | |
1da177e4 LT |
2549 | if (OFF_SLAB(cachep)) { |
2550 | /* Slab management obj is off-slab. */ | |
5b74ada7 RT |
2551 | slabp = kmem_cache_alloc_node(cachep->slabp_cache, |
2552 | local_flags, nodeid); | |
1da177e4 LT |
2553 | if (!slabp) |
2554 | return NULL; | |
2555 | } else { | |
b28a02de | 2556 | slabp = objp + colour_off; |
1da177e4 LT |
2557 | colour_off += cachep->slab_size; |
2558 | } | |
2559 | slabp->inuse = 0; | |
2560 | slabp->colouroff = colour_off; | |
b28a02de | 2561 | slabp->s_mem = objp + colour_off; |
5b74ada7 | 2562 | slabp->nodeid = nodeid; |
1da177e4 LT |
2563 | return slabp; |
2564 | } | |
2565 | ||
2566 | static inline kmem_bufctl_t *slab_bufctl(struct slab *slabp) | |
2567 | { | |
b28a02de | 2568 | return (kmem_bufctl_t *) (slabp + 1); |
1da177e4 LT |
2569 | } |
2570 | ||
343e0d7a | 2571 | static void cache_init_objs(struct kmem_cache *cachep, |
b28a02de | 2572 | struct slab *slabp, unsigned long ctor_flags) |
1da177e4 LT |
2573 | { |
2574 | int i; | |
2575 | ||
2576 | for (i = 0; i < cachep->num; i++) { | |
8fea4e96 | 2577 | void *objp = index_to_obj(cachep, slabp, i); |
1da177e4 LT |
2578 | #if DEBUG |
2579 | /* need to poison the objs? */ | |
2580 | if (cachep->flags & SLAB_POISON) | |
2581 | poison_obj(cachep, objp, POISON_FREE); | |
2582 | if (cachep->flags & SLAB_STORE_USER) | |
2583 | *dbg_userword(cachep, objp) = NULL; | |
2584 | ||
2585 | if (cachep->flags & SLAB_RED_ZONE) { | |
2586 | *dbg_redzone1(cachep, objp) = RED_INACTIVE; | |
2587 | *dbg_redzone2(cachep, objp) = RED_INACTIVE; | |
2588 | } | |
2589 | /* | |
a737b3e2 AM |
2590 | * Constructors are not allowed to allocate memory from the same |
2591 | * cache which they are a constructor for. Otherwise, deadlock. | |
2592 | * They must also be threaded. | |
1da177e4 LT |
2593 | */ |
2594 | if (cachep->ctor && !(cachep->flags & SLAB_POISON)) | |
3dafccf2 | 2595 | cachep->ctor(objp + obj_offset(cachep), cachep, |
b28a02de | 2596 | ctor_flags); |
1da177e4 LT |
2597 | |
2598 | if (cachep->flags & SLAB_RED_ZONE) { | |
2599 | if (*dbg_redzone2(cachep, objp) != RED_INACTIVE) | |
2600 | slab_error(cachep, "constructor overwrote the" | |
b28a02de | 2601 | " end of an object"); |
1da177e4 LT |
2602 | if (*dbg_redzone1(cachep, objp) != RED_INACTIVE) |
2603 | slab_error(cachep, "constructor overwrote the" | |
b28a02de | 2604 | " start of an object"); |
1da177e4 | 2605 | } |
a737b3e2 AM |
2606 | if ((cachep->buffer_size % PAGE_SIZE) == 0 && |
2607 | OFF_SLAB(cachep) && cachep->flags & SLAB_POISON) | |
b28a02de | 2608 | kernel_map_pages(virt_to_page(objp), |
3dafccf2 | 2609 | cachep->buffer_size / PAGE_SIZE, 0); |
1da177e4 LT |
2610 | #else |
2611 | if (cachep->ctor) | |
2612 | cachep->ctor(objp, cachep, ctor_flags); | |
2613 | #endif | |
b28a02de | 2614 | slab_bufctl(slabp)[i] = i + 1; |
1da177e4 | 2615 | } |
b28a02de | 2616 | slab_bufctl(slabp)[i - 1] = BUFCTL_END; |
1da177e4 LT |
2617 | slabp->free = 0; |
2618 | } | |
2619 | ||
343e0d7a | 2620 | static void kmem_flagcheck(struct kmem_cache *cachep, gfp_t flags) |
1da177e4 | 2621 | { |
a737b3e2 AM |
2622 | if (flags & SLAB_DMA) |
2623 | BUG_ON(!(cachep->gfpflags & GFP_DMA)); | |
2624 | else | |
2625 | BUG_ON(cachep->gfpflags & GFP_DMA); | |
1da177e4 LT |
2626 | } |
2627 | ||
a737b3e2 AM |
2628 | static void *slab_get_obj(struct kmem_cache *cachep, struct slab *slabp, |
2629 | int nodeid) | |
78d382d7 | 2630 | { |
8fea4e96 | 2631 | void *objp = index_to_obj(cachep, slabp, slabp->free); |
78d382d7 MD |
2632 | kmem_bufctl_t next; |
2633 | ||
2634 | slabp->inuse++; | |
2635 | next = slab_bufctl(slabp)[slabp->free]; | |
2636 | #if DEBUG | |
2637 | slab_bufctl(slabp)[slabp->free] = BUFCTL_FREE; | |
2638 | WARN_ON(slabp->nodeid != nodeid); | |
2639 | #endif | |
2640 | slabp->free = next; | |
2641 | ||
2642 | return objp; | |
2643 | } | |
2644 | ||
a737b3e2 AM |
2645 | static void slab_put_obj(struct kmem_cache *cachep, struct slab *slabp, |
2646 | void *objp, int nodeid) | |
78d382d7 | 2647 | { |
8fea4e96 | 2648 | unsigned int objnr = obj_to_index(cachep, slabp, objp); |
78d382d7 MD |
2649 | |
2650 | #if DEBUG | |
2651 | /* Verify that the slab belongs to the intended node */ | |
2652 | WARN_ON(slabp->nodeid != nodeid); | |
2653 | ||
871751e2 | 2654 | if (slab_bufctl(slabp)[objnr] + 1 <= SLAB_LIMIT + 1) { |
78d382d7 | 2655 | printk(KERN_ERR "slab: double free detected in cache " |
a737b3e2 | 2656 | "'%s', objp %p\n", cachep->name, objp); |
78d382d7 MD |
2657 | BUG(); |
2658 | } | |
2659 | #endif | |
2660 | slab_bufctl(slabp)[objnr] = slabp->free; | |
2661 | slabp->free = objnr; | |
2662 | slabp->inuse--; | |
2663 | } | |
2664 | ||
4776874f PE |
2665 | /* |
2666 | * Map pages beginning at addr to the given cache and slab. This is required | |
2667 | * for the slab allocator to be able to lookup the cache and slab of a | |
2668 | * virtual address for kfree, ksize, kmem_ptr_validate, and slab debugging. | |
2669 | */ | |
2670 | static void slab_map_pages(struct kmem_cache *cache, struct slab *slab, | |
2671 | void *addr) | |
1da177e4 | 2672 | { |
4776874f | 2673 | int nr_pages; |
1da177e4 LT |
2674 | struct page *page; |
2675 | ||
4776874f | 2676 | page = virt_to_page(addr); |
84097518 | 2677 | |
4776874f | 2678 | nr_pages = 1; |
84097518 | 2679 | if (likely(!PageCompound(page))) |
4776874f PE |
2680 | nr_pages <<= cache->gfporder; |
2681 | ||
1da177e4 | 2682 | do { |
4776874f PE |
2683 | page_set_cache(page, cache); |
2684 | page_set_slab(page, slab); | |
1da177e4 | 2685 | page++; |
4776874f | 2686 | } while (--nr_pages); |
1da177e4 LT |
2687 | } |
2688 | ||
2689 | /* | |
2690 | * Grow (by 1) the number of slabs within a cache. This is called by | |
2691 | * kmem_cache_alloc() when there are no active objs left in a cache. | |
2692 | */ | |
343e0d7a | 2693 | static int cache_grow(struct kmem_cache *cachep, gfp_t flags, int nodeid) |
1da177e4 | 2694 | { |
b28a02de PE |
2695 | struct slab *slabp; |
2696 | void *objp; | |
2697 | size_t offset; | |
2698 | gfp_t local_flags; | |
2699 | unsigned long ctor_flags; | |
e498be7d | 2700 | struct kmem_list3 *l3; |
1da177e4 | 2701 | |
a737b3e2 AM |
2702 | /* |
2703 | * Be lazy and only check for valid flags here, keeping it out of the | |
2704 | * critical path in kmem_cache_alloc(). | |
1da177e4 | 2705 | */ |
40094fa6 | 2706 | BUG_ON(flags & ~(SLAB_DMA | SLAB_LEVEL_MASK | SLAB_NO_GROW)); |
1da177e4 LT |
2707 | if (flags & SLAB_NO_GROW) |
2708 | return 0; | |
2709 | ||
2710 | ctor_flags = SLAB_CTOR_CONSTRUCTOR; | |
2711 | local_flags = (flags & SLAB_LEVEL_MASK); | |
2712 | if (!(local_flags & __GFP_WAIT)) | |
2713 | /* | |
2714 | * Not allowed to sleep. Need to tell a constructor about | |
2715 | * this - it might need to know... | |
2716 | */ | |
2717 | ctor_flags |= SLAB_CTOR_ATOMIC; | |
2718 | ||
2e1217cf | 2719 | /* Take the l3 list lock to change the colour_next on this node */ |
1da177e4 | 2720 | check_irq_off(); |
2e1217cf RT |
2721 | l3 = cachep->nodelists[nodeid]; |
2722 | spin_lock(&l3->list_lock); | |
1da177e4 LT |
2723 | |
2724 | /* Get colour for the slab, and cal the next value. */ | |
2e1217cf RT |
2725 | offset = l3->colour_next; |
2726 | l3->colour_next++; | |
2727 | if (l3->colour_next >= cachep->colour) | |
2728 | l3->colour_next = 0; | |
2729 | spin_unlock(&l3->list_lock); | |
1da177e4 | 2730 | |
2e1217cf | 2731 | offset *= cachep->colour_off; |
1da177e4 LT |
2732 | |
2733 | if (local_flags & __GFP_WAIT) | |
2734 | local_irq_enable(); | |
2735 | ||
2736 | /* | |
2737 | * The test for missing atomic flag is performed here, rather than | |
2738 | * the more obvious place, simply to reduce the critical path length | |
2739 | * in kmem_cache_alloc(). If a caller is seriously mis-behaving they | |
2740 | * will eventually be caught here (where it matters). | |
2741 | */ | |
2742 | kmem_flagcheck(cachep, flags); | |
2743 | ||
a737b3e2 AM |
2744 | /* |
2745 | * Get mem for the objs. Attempt to allocate a physical page from | |
2746 | * 'nodeid'. | |
e498be7d | 2747 | */ |
a737b3e2 AM |
2748 | objp = kmem_getpages(cachep, flags, nodeid); |
2749 | if (!objp) | |
1da177e4 LT |
2750 | goto failed; |
2751 | ||
2752 | /* Get slab management. */ | |
5b74ada7 | 2753 | slabp = alloc_slabmgmt(cachep, objp, offset, local_flags, nodeid); |
a737b3e2 | 2754 | if (!slabp) |
1da177e4 LT |
2755 | goto opps1; |
2756 | ||
e498be7d | 2757 | slabp->nodeid = nodeid; |
4776874f | 2758 | slab_map_pages(cachep, slabp, objp); |
1da177e4 LT |
2759 | |
2760 | cache_init_objs(cachep, slabp, ctor_flags); | |
2761 | ||
2762 | if (local_flags & __GFP_WAIT) | |
2763 | local_irq_disable(); | |
2764 | check_irq_off(); | |
e498be7d | 2765 | spin_lock(&l3->list_lock); |
1da177e4 LT |
2766 | |
2767 | /* Make slab active. */ | |
e498be7d | 2768 | list_add_tail(&slabp->list, &(l3->slabs_free)); |
1da177e4 | 2769 | STATS_INC_GROWN(cachep); |
e498be7d CL |
2770 | l3->free_objects += cachep->num; |
2771 | spin_unlock(&l3->list_lock); | |
1da177e4 | 2772 | return 1; |
a737b3e2 | 2773 | opps1: |
1da177e4 | 2774 | kmem_freepages(cachep, objp); |
a737b3e2 | 2775 | failed: |
1da177e4 LT |
2776 | if (local_flags & __GFP_WAIT) |
2777 | local_irq_disable(); | |
2778 | return 0; | |
2779 | } | |
2780 | ||
2781 | #if DEBUG | |
2782 | ||
2783 | /* | |
2784 | * Perform extra freeing checks: | |
2785 | * - detect bad pointers. | |
2786 | * - POISON/RED_ZONE checking | |
2787 | * - destructor calls, for caches with POISON+dtor | |
2788 | */ | |
2789 | static void kfree_debugcheck(const void *objp) | |
2790 | { | |
2791 | struct page *page; | |
2792 | ||
2793 | if (!virt_addr_valid(objp)) { | |
2794 | printk(KERN_ERR "kfree_debugcheck: out of range ptr %lxh.\n", | |
b28a02de PE |
2795 | (unsigned long)objp); |
2796 | BUG(); | |
1da177e4 LT |
2797 | } |
2798 | page = virt_to_page(objp); | |
2799 | if (!PageSlab(page)) { | |
b28a02de PE |
2800 | printk(KERN_ERR "kfree_debugcheck: bad ptr %lxh.\n", |
2801 | (unsigned long)objp); | |
1da177e4 LT |
2802 | BUG(); |
2803 | } | |
2804 | } | |
2805 | ||
58ce1fd5 PE |
2806 | static inline void verify_redzone_free(struct kmem_cache *cache, void *obj) |
2807 | { | |
2808 | unsigned long redzone1, redzone2; | |
2809 | ||
2810 | redzone1 = *dbg_redzone1(cache, obj); | |
2811 | redzone2 = *dbg_redzone2(cache, obj); | |
2812 | ||
2813 | /* | |
2814 | * Redzone is ok. | |
2815 | */ | |
2816 | if (redzone1 == RED_ACTIVE && redzone2 == RED_ACTIVE) | |
2817 | return; | |
2818 | ||
2819 | if (redzone1 == RED_INACTIVE && redzone2 == RED_INACTIVE) | |
2820 | slab_error(cache, "double free detected"); | |
2821 | else | |
2822 | slab_error(cache, "memory outside object was overwritten"); | |
2823 | ||
2824 | printk(KERN_ERR "%p: redzone 1:0x%lx, redzone 2:0x%lx.\n", | |
2825 | obj, redzone1, redzone2); | |
2826 | } | |
2827 | ||
343e0d7a | 2828 | static void *cache_free_debugcheck(struct kmem_cache *cachep, void *objp, |
b28a02de | 2829 | void *caller) |
1da177e4 LT |
2830 | { |
2831 | struct page *page; | |
2832 | unsigned int objnr; | |
2833 | struct slab *slabp; | |
2834 | ||
3dafccf2 | 2835 | objp -= obj_offset(cachep); |
1da177e4 LT |
2836 | kfree_debugcheck(objp); |
2837 | page = virt_to_page(objp); | |
2838 | ||
065d41cb | 2839 | slabp = page_get_slab(page); |
1da177e4 LT |
2840 | |
2841 | if (cachep->flags & SLAB_RED_ZONE) { | |
58ce1fd5 | 2842 | verify_redzone_free(cachep, objp); |
1da177e4 LT |
2843 | *dbg_redzone1(cachep, objp) = RED_INACTIVE; |
2844 | *dbg_redzone2(cachep, objp) = RED_INACTIVE; | |
2845 | } | |
2846 | if (cachep->flags & SLAB_STORE_USER) | |
2847 | *dbg_userword(cachep, objp) = caller; | |
2848 | ||
8fea4e96 | 2849 | objnr = obj_to_index(cachep, slabp, objp); |
1da177e4 LT |
2850 | |
2851 | BUG_ON(objnr >= cachep->num); | |
8fea4e96 | 2852 | BUG_ON(objp != index_to_obj(cachep, slabp, objnr)); |
1da177e4 LT |
2853 | |
2854 | if (cachep->flags & SLAB_DEBUG_INITIAL) { | |
a737b3e2 AM |
2855 | /* |
2856 | * Need to call the slab's constructor so the caller can | |
2857 | * perform a verify of its state (debugging). Called without | |
2858 | * the cache-lock held. | |
1da177e4 | 2859 | */ |
3dafccf2 | 2860 | cachep->ctor(objp + obj_offset(cachep), |
b28a02de | 2861 | cachep, SLAB_CTOR_CONSTRUCTOR | SLAB_CTOR_VERIFY); |
1da177e4 LT |
2862 | } |
2863 | if (cachep->flags & SLAB_POISON && cachep->dtor) { | |
2864 | /* we want to cache poison the object, | |
2865 | * call the destruction callback | |
2866 | */ | |
3dafccf2 | 2867 | cachep->dtor(objp + obj_offset(cachep), cachep, 0); |
1da177e4 | 2868 | } |
871751e2 AV |
2869 | #ifdef CONFIG_DEBUG_SLAB_LEAK |
2870 | slab_bufctl(slabp)[objnr] = BUFCTL_FREE; | |
2871 | #endif | |
1da177e4 LT |
2872 | if (cachep->flags & SLAB_POISON) { |
2873 | #ifdef CONFIG_DEBUG_PAGEALLOC | |
a737b3e2 | 2874 | if ((cachep->buffer_size % PAGE_SIZE)==0 && OFF_SLAB(cachep)) { |
1da177e4 | 2875 | store_stackinfo(cachep, objp, (unsigned long)caller); |
b28a02de | 2876 | kernel_map_pages(virt_to_page(objp), |
3dafccf2 | 2877 | cachep->buffer_size / PAGE_SIZE, 0); |
1da177e4 LT |
2878 | } else { |
2879 | poison_obj(cachep, objp, POISON_FREE); | |
2880 | } | |
2881 | #else | |
2882 | poison_obj(cachep, objp, POISON_FREE); | |
2883 | #endif | |
2884 | } | |
2885 | return objp; | |
2886 | } | |
2887 | ||
343e0d7a | 2888 | static void check_slabp(struct kmem_cache *cachep, struct slab *slabp) |
1da177e4 LT |
2889 | { |
2890 | kmem_bufctl_t i; | |
2891 | int entries = 0; | |
b28a02de | 2892 | |
1da177e4 LT |
2893 | /* Check slab's freelist to see if this obj is there. */ |
2894 | for (i = slabp->free; i != BUFCTL_END; i = slab_bufctl(slabp)[i]) { | |
2895 | entries++; | |
2896 | if (entries > cachep->num || i >= cachep->num) | |
2897 | goto bad; | |
2898 | } | |
2899 | if (entries != cachep->num - slabp->inuse) { | |
a737b3e2 AM |
2900 | bad: |
2901 | printk(KERN_ERR "slab: Internal list corruption detected in " | |
2902 | "cache '%s'(%d), slabp %p(%d). Hexdump:\n", | |
2903 | cachep->name, cachep->num, slabp, slabp->inuse); | |
b28a02de | 2904 | for (i = 0; |
264132bc | 2905 | i < sizeof(*slabp) + cachep->num * sizeof(kmem_bufctl_t); |
b28a02de | 2906 | i++) { |
a737b3e2 | 2907 | if (i % 16 == 0) |
1da177e4 | 2908 | printk("\n%03x:", i); |
b28a02de | 2909 | printk(" %02x", ((unsigned char *)slabp)[i]); |
1da177e4 LT |
2910 | } |
2911 | printk("\n"); | |
2912 | BUG(); | |
2913 | } | |
2914 | } | |
2915 | #else | |
2916 | #define kfree_debugcheck(x) do { } while(0) | |
2917 | #define cache_free_debugcheck(x,objp,z) (objp) | |
2918 | #define check_slabp(x,y) do { } while(0) | |
2919 | #endif | |
2920 | ||
343e0d7a | 2921 | static void *cache_alloc_refill(struct kmem_cache *cachep, gfp_t flags) |
1da177e4 LT |
2922 | { |
2923 | int batchcount; | |
2924 | struct kmem_list3 *l3; | |
2925 | struct array_cache *ac; | |
1ca4cb24 PE |
2926 | int node; |
2927 | ||
2928 | node = numa_node_id(); | |
1da177e4 LT |
2929 | |
2930 | check_irq_off(); | |
9a2dba4b | 2931 | ac = cpu_cache_get(cachep); |
a737b3e2 | 2932 | retry: |
1da177e4 LT |
2933 | batchcount = ac->batchcount; |
2934 | if (!ac->touched && batchcount > BATCHREFILL_LIMIT) { | |
a737b3e2 AM |
2935 | /* |
2936 | * If there was little recent activity on this cache, then | |
2937 | * perform only a partial refill. Otherwise we could generate | |
2938 | * refill bouncing. | |
1da177e4 LT |
2939 | */ |
2940 | batchcount = BATCHREFILL_LIMIT; | |
2941 | } | |
1ca4cb24 | 2942 | l3 = cachep->nodelists[node]; |
e498be7d CL |
2943 | |
2944 | BUG_ON(ac->avail > 0 || !l3); | |
2945 | spin_lock(&l3->list_lock); | |
1da177e4 | 2946 | |
3ded175a CL |
2947 | /* See if we can refill from the shared array */ |
2948 | if (l3->shared && transfer_objects(ac, l3->shared, batchcount)) | |
2949 | goto alloc_done; | |
2950 | ||
1da177e4 LT |
2951 | while (batchcount > 0) { |
2952 | struct list_head *entry; | |
2953 | struct slab *slabp; | |
2954 | /* Get slab alloc is to come from. */ | |
2955 | entry = l3->slabs_partial.next; | |
2956 | if (entry == &l3->slabs_partial) { | |
2957 | l3->free_touched = 1; | |
2958 | entry = l3->slabs_free.next; | |
2959 | if (entry == &l3->slabs_free) | |
2960 | goto must_grow; | |
2961 | } | |
2962 | ||
2963 | slabp = list_entry(entry, struct slab, list); | |
2964 | check_slabp(cachep, slabp); | |
2965 | check_spinlock_acquired(cachep); | |
2966 | while (slabp->inuse < cachep->num && batchcount--) { | |
1da177e4 LT |
2967 | STATS_INC_ALLOCED(cachep); |
2968 | STATS_INC_ACTIVE(cachep); | |
2969 | STATS_SET_HIGH(cachep); | |
2970 | ||
78d382d7 | 2971 | ac->entry[ac->avail++] = slab_get_obj(cachep, slabp, |
1ca4cb24 | 2972 | node); |
1da177e4 LT |
2973 | } |
2974 | check_slabp(cachep, slabp); | |
2975 | ||
2976 | /* move slabp to correct slabp list: */ | |
2977 | list_del(&slabp->list); | |
2978 | if (slabp->free == BUFCTL_END) | |
2979 | list_add(&slabp->list, &l3->slabs_full); | |
2980 | else | |
2981 | list_add(&slabp->list, &l3->slabs_partial); | |
2982 | } | |
2983 | ||
a737b3e2 | 2984 | must_grow: |
1da177e4 | 2985 | l3->free_objects -= ac->avail; |
a737b3e2 | 2986 | alloc_done: |
e498be7d | 2987 | spin_unlock(&l3->list_lock); |
1da177e4 LT |
2988 | |
2989 | if (unlikely(!ac->avail)) { | |
2990 | int x; | |
1ca4cb24 | 2991 | x = cache_grow(cachep, flags, node); |
e498be7d | 2992 | |
a737b3e2 | 2993 | /* cache_grow can reenable interrupts, then ac could change. */ |
9a2dba4b | 2994 | ac = cpu_cache_get(cachep); |
a737b3e2 | 2995 | if (!x && ac->avail == 0) /* no objects in sight? abort */ |
1da177e4 LT |
2996 | return NULL; |
2997 | ||
a737b3e2 | 2998 | if (!ac->avail) /* objects refilled by interrupt? */ |
1da177e4 LT |
2999 | goto retry; |
3000 | } | |
3001 | ac->touched = 1; | |
e498be7d | 3002 | return ac->entry[--ac->avail]; |
1da177e4 LT |
3003 | } |
3004 | ||
a737b3e2 AM |
3005 | static inline void cache_alloc_debugcheck_before(struct kmem_cache *cachep, |
3006 | gfp_t flags) | |
1da177e4 LT |
3007 | { |
3008 | might_sleep_if(flags & __GFP_WAIT); | |
3009 | #if DEBUG | |
3010 | kmem_flagcheck(cachep, flags); | |
3011 | #endif | |
3012 | } | |
3013 | ||
3014 | #if DEBUG | |
a737b3e2 AM |
3015 | static void *cache_alloc_debugcheck_after(struct kmem_cache *cachep, |
3016 | gfp_t flags, void *objp, void *caller) | |
1da177e4 | 3017 | { |
b28a02de | 3018 | if (!objp) |
1da177e4 | 3019 | return objp; |
b28a02de | 3020 | if (cachep->flags & SLAB_POISON) { |
1da177e4 | 3021 | #ifdef CONFIG_DEBUG_PAGEALLOC |
3dafccf2 | 3022 | if ((cachep->buffer_size % PAGE_SIZE) == 0 && OFF_SLAB(cachep)) |
b28a02de | 3023 | kernel_map_pages(virt_to_page(objp), |
3dafccf2 | 3024 | cachep->buffer_size / PAGE_SIZE, 1); |
1da177e4 LT |
3025 | else |
3026 | check_poison_obj(cachep, objp); | |
3027 | #else | |
3028 | check_poison_obj(cachep, objp); | |
3029 | #endif | |
3030 | poison_obj(cachep, objp, POISON_INUSE); | |
3031 | } | |
3032 | if (cachep->flags & SLAB_STORE_USER) | |
3033 | *dbg_userword(cachep, objp) = caller; | |
3034 | ||
3035 | if (cachep->flags & SLAB_RED_ZONE) { | |
a737b3e2 AM |
3036 | if (*dbg_redzone1(cachep, objp) != RED_INACTIVE || |
3037 | *dbg_redzone2(cachep, objp) != RED_INACTIVE) { | |
3038 | slab_error(cachep, "double free, or memory outside" | |
3039 | " object was overwritten"); | |
b28a02de | 3040 | printk(KERN_ERR |
a737b3e2 AM |
3041 | "%p: redzone 1:0x%lx, redzone 2:0x%lx\n", |
3042 | objp, *dbg_redzone1(cachep, objp), | |
3043 | *dbg_redzone2(cachep, objp)); | |
1da177e4 LT |
3044 | } |
3045 | *dbg_redzone1(cachep, objp) = RED_ACTIVE; | |
3046 | *dbg_redzone2(cachep, objp) = RED_ACTIVE; | |
3047 | } | |
871751e2 AV |
3048 | #ifdef CONFIG_DEBUG_SLAB_LEAK |
3049 | { | |
3050 | struct slab *slabp; | |
3051 | unsigned objnr; | |
3052 | ||
3053 | slabp = page_get_slab(virt_to_page(objp)); | |
3054 | objnr = (unsigned)(objp - slabp->s_mem) / cachep->buffer_size; | |
3055 | slab_bufctl(slabp)[objnr] = BUFCTL_ACTIVE; | |
3056 | } | |
3057 | #endif | |
3dafccf2 | 3058 | objp += obj_offset(cachep); |
1da177e4 | 3059 | if (cachep->ctor && cachep->flags & SLAB_POISON) { |
b28a02de | 3060 | unsigned long ctor_flags = SLAB_CTOR_CONSTRUCTOR; |
1da177e4 LT |
3061 | |
3062 | if (!(flags & __GFP_WAIT)) | |
3063 | ctor_flags |= SLAB_CTOR_ATOMIC; | |
3064 | ||
3065 | cachep->ctor(objp, cachep, ctor_flags); | |
b28a02de | 3066 | } |
a44b56d3 KH |
3067 | #if ARCH_SLAB_MINALIGN |
3068 | if ((u32)objp & (ARCH_SLAB_MINALIGN-1)) { | |
3069 | printk(KERN_ERR "0x%p: not aligned to ARCH_SLAB_MINALIGN=%d\n", | |
3070 | objp, ARCH_SLAB_MINALIGN); | |
3071 | } | |
3072 | #endif | |
1da177e4 LT |
3073 | return objp; |
3074 | } | |
3075 | #else | |
3076 | #define cache_alloc_debugcheck_after(a,b,objp,d) (objp) | |
3077 | #endif | |
3078 | ||
343e0d7a | 3079 | static inline void *____cache_alloc(struct kmem_cache *cachep, gfp_t flags) |
1da177e4 | 3080 | { |
b28a02de | 3081 | void *objp; |
1da177e4 LT |
3082 | struct array_cache *ac; |
3083 | ||
5c382300 | 3084 | check_irq_off(); |
9a2dba4b | 3085 | ac = cpu_cache_get(cachep); |
1da177e4 LT |
3086 | if (likely(ac->avail)) { |
3087 | STATS_INC_ALLOCHIT(cachep); | |
3088 | ac->touched = 1; | |
e498be7d | 3089 | objp = ac->entry[--ac->avail]; |
1da177e4 LT |
3090 | } else { |
3091 | STATS_INC_ALLOCMISS(cachep); | |
3092 | objp = cache_alloc_refill(cachep, flags); | |
3093 | } | |
5c382300 AK |
3094 | return objp; |
3095 | } | |
3096 | ||
a737b3e2 AM |
3097 | static __always_inline void *__cache_alloc(struct kmem_cache *cachep, |
3098 | gfp_t flags, void *caller) | |
5c382300 AK |
3099 | { |
3100 | unsigned long save_flags; | |
de3083ec | 3101 | void *objp = NULL; |
5c382300 AK |
3102 | |
3103 | cache_alloc_debugcheck_before(cachep, flags); | |
3104 | ||
3105 | local_irq_save(save_flags); | |
de3083ec | 3106 | |
765c4507 CL |
3107 | if (unlikely(NUMA_BUILD && |
3108 | current->flags & (PF_SPREAD_SLAB | PF_MEMPOLICY))) | |
de3083ec | 3109 | objp = alternate_node_alloc(cachep, flags); |
de3083ec CL |
3110 | |
3111 | if (!objp) | |
3112 | objp = ____cache_alloc(cachep, flags); | |
765c4507 CL |
3113 | /* |
3114 | * We may just have run out of memory on the local node. | |
3115 | * __cache_alloc_node() knows how to locate memory on other nodes | |
3116 | */ | |
3117 | if (NUMA_BUILD && !objp) | |
3118 | objp = __cache_alloc_node(cachep, flags, numa_node_id()); | |
1da177e4 | 3119 | local_irq_restore(save_flags); |
34342e86 | 3120 | objp = cache_alloc_debugcheck_after(cachep, flags, objp, |
7fd6b141 | 3121 | caller); |
34342e86 | 3122 | prefetchw(objp); |
1da177e4 LT |
3123 | return objp; |
3124 | } | |
3125 | ||
e498be7d | 3126 | #ifdef CONFIG_NUMA |
c61afb18 | 3127 | /* |
b2455396 | 3128 | * Try allocating on another node if PF_SPREAD_SLAB|PF_MEMPOLICY. |
c61afb18 PJ |
3129 | * |
3130 | * If we are in_interrupt, then process context, including cpusets and | |
3131 | * mempolicy, may not apply and should not be used for allocation policy. | |
3132 | */ | |
3133 | static void *alternate_node_alloc(struct kmem_cache *cachep, gfp_t flags) | |
3134 | { | |
3135 | int nid_alloc, nid_here; | |
3136 | ||
765c4507 | 3137 | if (in_interrupt() || (flags & __GFP_THISNODE)) |
c61afb18 PJ |
3138 | return NULL; |
3139 | nid_alloc = nid_here = numa_node_id(); | |
3140 | if (cpuset_do_slab_mem_spread() && (cachep->flags & SLAB_MEM_SPREAD)) | |
3141 | nid_alloc = cpuset_mem_spread_node(); | |
3142 | else if (current->mempolicy) | |
3143 | nid_alloc = slab_node(current->mempolicy); | |
3144 | if (nid_alloc != nid_here) | |
3145 | return __cache_alloc_node(cachep, flags, nid_alloc); | |
3146 | return NULL; | |
3147 | } | |
3148 | ||
765c4507 CL |
3149 | /* |
3150 | * Fallback function if there was no memory available and no objects on a | |
3151 | * certain node and we are allowed to fall back. We mimick the behavior of | |
3152 | * the page allocator. We fall back according to a zonelist determined by | |
3153 | * the policy layer while obeying cpuset constraints. | |
3154 | */ | |
3155 | void *fallback_alloc(struct kmem_cache *cache, gfp_t flags) | |
3156 | { | |
3157 | struct zonelist *zonelist = &NODE_DATA(slab_node(current->mempolicy)) | |
3158 | ->node_zonelists[gfp_zone(flags)]; | |
3159 | struct zone **z; | |
3160 | void *obj = NULL; | |
3161 | ||
aedb0eb1 CL |
3162 | for (z = zonelist->zones; *z && !obj; z++) { |
3163 | int nid = zone_to_nid(*z); | |
3164 | ||
765c4507 | 3165 | if (zone_idx(*z) <= ZONE_NORMAL && |
aedb0eb1 CL |
3166 | cpuset_zone_allowed(*z, flags) && |
3167 | cache->nodelists[nid]) | |
765c4507 | 3168 | obj = __cache_alloc_node(cache, |
aedb0eb1 CL |
3169 | flags | __GFP_THISNODE, nid); |
3170 | } | |
765c4507 CL |
3171 | return obj; |
3172 | } | |
3173 | ||
e498be7d CL |
3174 | /* |
3175 | * A interface to enable slab creation on nodeid | |
1da177e4 | 3176 | */ |
a737b3e2 AM |
3177 | static void *__cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, |
3178 | int nodeid) | |
e498be7d CL |
3179 | { |
3180 | struct list_head *entry; | |
b28a02de PE |
3181 | struct slab *slabp; |
3182 | struct kmem_list3 *l3; | |
3183 | void *obj; | |
b28a02de PE |
3184 | int x; |
3185 | ||
3186 | l3 = cachep->nodelists[nodeid]; | |
3187 | BUG_ON(!l3); | |
3188 | ||
a737b3e2 | 3189 | retry: |
ca3b9b91 | 3190 | check_irq_off(); |
b28a02de PE |
3191 | spin_lock(&l3->list_lock); |
3192 | entry = l3->slabs_partial.next; | |
3193 | if (entry == &l3->slabs_partial) { | |
3194 | l3->free_touched = 1; | |
3195 | entry = l3->slabs_free.next; | |
3196 | if (entry == &l3->slabs_free) | |
3197 | goto must_grow; | |
3198 | } | |
3199 | ||
3200 | slabp = list_entry(entry, struct slab, list); | |
3201 | check_spinlock_acquired_node(cachep, nodeid); | |
3202 | check_slabp(cachep, slabp); | |
3203 | ||
3204 | STATS_INC_NODEALLOCS(cachep); | |
3205 | STATS_INC_ACTIVE(cachep); | |
3206 | STATS_SET_HIGH(cachep); | |
3207 | ||
3208 | BUG_ON(slabp->inuse == cachep->num); | |
3209 | ||
78d382d7 | 3210 | obj = slab_get_obj(cachep, slabp, nodeid); |
b28a02de PE |
3211 | check_slabp(cachep, slabp); |
3212 | l3->free_objects--; | |
3213 | /* move slabp to correct slabp list: */ | |
3214 | list_del(&slabp->list); | |
3215 | ||
a737b3e2 | 3216 | if (slabp->free == BUFCTL_END) |
b28a02de | 3217 | list_add(&slabp->list, &l3->slabs_full); |
a737b3e2 | 3218 | else |
b28a02de | 3219 | list_add(&slabp->list, &l3->slabs_partial); |
e498be7d | 3220 | |
b28a02de PE |
3221 | spin_unlock(&l3->list_lock); |
3222 | goto done; | |
e498be7d | 3223 | |
a737b3e2 | 3224 | must_grow: |
b28a02de PE |
3225 | spin_unlock(&l3->list_lock); |
3226 | x = cache_grow(cachep, flags, nodeid); | |
765c4507 CL |
3227 | if (x) |
3228 | goto retry; | |
1da177e4 | 3229 | |
765c4507 CL |
3230 | if (!(flags & __GFP_THISNODE)) |
3231 | /* Unable to grow the cache. Fall back to other nodes. */ | |
3232 | return fallback_alloc(cachep, flags); | |
3233 | ||
3234 | return NULL; | |
e498be7d | 3235 | |
a737b3e2 | 3236 | done: |
b28a02de | 3237 | return obj; |
e498be7d CL |
3238 | } |
3239 | #endif | |
3240 | ||
3241 | /* | |
3242 | * Caller needs to acquire correct kmem_list's list_lock | |
3243 | */ | |
343e0d7a | 3244 | static void free_block(struct kmem_cache *cachep, void **objpp, int nr_objects, |
b28a02de | 3245 | int node) |
1da177e4 LT |
3246 | { |
3247 | int i; | |
e498be7d | 3248 | struct kmem_list3 *l3; |
1da177e4 LT |
3249 | |
3250 | for (i = 0; i < nr_objects; i++) { | |
3251 | void *objp = objpp[i]; | |
3252 | struct slab *slabp; | |
1da177e4 | 3253 | |
6ed5eb22 | 3254 | slabp = virt_to_slab(objp); |
ff69416e | 3255 | l3 = cachep->nodelists[node]; |
1da177e4 | 3256 | list_del(&slabp->list); |
ff69416e | 3257 | check_spinlock_acquired_node(cachep, node); |
1da177e4 | 3258 | check_slabp(cachep, slabp); |
78d382d7 | 3259 | slab_put_obj(cachep, slabp, objp, node); |
1da177e4 | 3260 | STATS_DEC_ACTIVE(cachep); |
e498be7d | 3261 | l3->free_objects++; |
1da177e4 LT |
3262 | check_slabp(cachep, slabp); |
3263 | ||
3264 | /* fixup slab chains */ | |
3265 | if (slabp->inuse == 0) { | |
e498be7d CL |
3266 | if (l3->free_objects > l3->free_limit) { |
3267 | l3->free_objects -= cachep->num; | |
e5ac9c5a RT |
3268 | /* No need to drop any previously held |
3269 | * lock here, even if we have a off-slab slab | |
3270 | * descriptor it is guaranteed to come from | |
3271 | * a different cache, refer to comments before | |
3272 | * alloc_slabmgmt. | |
3273 | */ | |
1da177e4 LT |
3274 | slab_destroy(cachep, slabp); |
3275 | } else { | |
e498be7d | 3276 | list_add(&slabp->list, &l3->slabs_free); |
1da177e4 LT |
3277 | } |
3278 | } else { | |
3279 | /* Unconditionally move a slab to the end of the | |
3280 | * partial list on free - maximum time for the | |
3281 | * other objects to be freed, too. | |
3282 | */ | |
e498be7d | 3283 | list_add_tail(&slabp->list, &l3->slabs_partial); |
1da177e4 LT |
3284 | } |
3285 | } | |
3286 | } | |
3287 | ||
343e0d7a | 3288 | static void cache_flusharray(struct kmem_cache *cachep, struct array_cache *ac) |
1da177e4 LT |
3289 | { |
3290 | int batchcount; | |
e498be7d | 3291 | struct kmem_list3 *l3; |
ff69416e | 3292 | int node = numa_node_id(); |
1da177e4 LT |
3293 | |
3294 | batchcount = ac->batchcount; | |
3295 | #if DEBUG | |
3296 | BUG_ON(!batchcount || batchcount > ac->avail); | |
3297 | #endif | |
3298 | check_irq_off(); | |
ff69416e | 3299 | l3 = cachep->nodelists[node]; |
873623df | 3300 | spin_lock(&l3->list_lock); |
e498be7d CL |
3301 | if (l3->shared) { |
3302 | struct array_cache *shared_array = l3->shared; | |
b28a02de | 3303 | int max = shared_array->limit - shared_array->avail; |
1da177e4 LT |
3304 | if (max) { |
3305 | if (batchcount > max) | |
3306 | batchcount = max; | |
e498be7d | 3307 | memcpy(&(shared_array->entry[shared_array->avail]), |
b28a02de | 3308 | ac->entry, sizeof(void *) * batchcount); |
1da177e4 LT |
3309 | shared_array->avail += batchcount; |
3310 | goto free_done; | |
3311 | } | |
3312 | } | |
3313 | ||
ff69416e | 3314 | free_block(cachep, ac->entry, batchcount, node); |
a737b3e2 | 3315 | free_done: |
1da177e4 LT |
3316 | #if STATS |
3317 | { | |
3318 | int i = 0; | |
3319 | struct list_head *p; | |
3320 | ||
e498be7d CL |
3321 | p = l3->slabs_free.next; |
3322 | while (p != &(l3->slabs_free)) { | |
1da177e4 LT |
3323 | struct slab *slabp; |
3324 | ||
3325 | slabp = list_entry(p, struct slab, list); | |
3326 | BUG_ON(slabp->inuse); | |
3327 | ||
3328 | i++; | |
3329 | p = p->next; | |
3330 | } | |
3331 | STATS_SET_FREEABLE(cachep, i); | |
3332 | } | |
3333 | #endif | |
e498be7d | 3334 | spin_unlock(&l3->list_lock); |
1da177e4 | 3335 | ac->avail -= batchcount; |
a737b3e2 | 3336 | memmove(ac->entry, &(ac->entry[batchcount]), sizeof(void *)*ac->avail); |
1da177e4 LT |
3337 | } |
3338 | ||
3339 | /* | |
a737b3e2 AM |
3340 | * Release an obj back to its cache. If the obj has a constructed state, it must |
3341 | * be in this state _before_ it is released. Called with disabled ints. | |
1da177e4 | 3342 | */ |
873623df | 3343 | static inline void __cache_free(struct kmem_cache *cachep, void *objp) |
1da177e4 | 3344 | { |
9a2dba4b | 3345 | struct array_cache *ac = cpu_cache_get(cachep); |
1da177e4 LT |
3346 | |
3347 | check_irq_off(); | |
3348 | objp = cache_free_debugcheck(cachep, objp, __builtin_return_address(0)); | |
3349 | ||
873623df | 3350 | if (cache_free_alien(cachep, objp)) |
729bd0b7 PE |
3351 | return; |
3352 | ||
1da177e4 LT |
3353 | if (likely(ac->avail < ac->limit)) { |
3354 | STATS_INC_FREEHIT(cachep); | |
e498be7d | 3355 | ac->entry[ac->avail++] = objp; |
1da177e4 LT |
3356 | return; |
3357 | } else { | |
3358 | STATS_INC_FREEMISS(cachep); | |
3359 | cache_flusharray(cachep, ac); | |
e498be7d | 3360 | ac->entry[ac->avail++] = objp; |
1da177e4 LT |
3361 | } |
3362 | } | |
3363 | ||
3364 | /** | |
3365 | * kmem_cache_alloc - Allocate an object | |
3366 | * @cachep: The cache to allocate from. | |
3367 | * @flags: See kmalloc(). | |
3368 | * | |
3369 | * Allocate an object from this cache. The flags are only relevant | |
3370 | * if the cache has no available objects. | |
3371 | */ | |
343e0d7a | 3372 | void *kmem_cache_alloc(struct kmem_cache *cachep, gfp_t flags) |
1da177e4 | 3373 | { |
7fd6b141 | 3374 | return __cache_alloc(cachep, flags, __builtin_return_address(0)); |
1da177e4 LT |
3375 | } |
3376 | EXPORT_SYMBOL(kmem_cache_alloc); | |
3377 | ||
a8c0f9a4 | 3378 | /** |
b8008b2b | 3379 | * kmem_cache_zalloc - Allocate an object. The memory is set to zero. |
a8c0f9a4 PE |
3380 | * @cache: The cache to allocate from. |
3381 | * @flags: See kmalloc(). | |
3382 | * | |
3383 | * Allocate an object from this cache and set the allocated memory to zero. | |
3384 | * The flags are only relevant if the cache has no available objects. | |
3385 | */ | |
3386 | void *kmem_cache_zalloc(struct kmem_cache *cache, gfp_t flags) | |
3387 | { | |
3388 | void *ret = __cache_alloc(cache, flags, __builtin_return_address(0)); | |
3389 | if (ret) | |
3390 | memset(ret, 0, obj_size(cache)); | |
3391 | return ret; | |
3392 | } | |
3393 | EXPORT_SYMBOL(kmem_cache_zalloc); | |
3394 | ||
1da177e4 LT |
3395 | /** |
3396 | * kmem_ptr_validate - check if an untrusted pointer might | |
3397 | * be a slab entry. | |
3398 | * @cachep: the cache we're checking against | |
3399 | * @ptr: pointer to validate | |
3400 | * | |
3401 | * This verifies that the untrusted pointer looks sane: | |
3402 | * it is _not_ a guarantee that the pointer is actually | |
3403 | * part of the slab cache in question, but it at least | |
3404 | * validates that the pointer can be dereferenced and | |
3405 | * looks half-way sane. | |
3406 | * | |
3407 | * Currently only used for dentry validation. | |
3408 | */ | |
343e0d7a | 3409 | int fastcall kmem_ptr_validate(struct kmem_cache *cachep, void *ptr) |
1da177e4 | 3410 | { |
b28a02de | 3411 | unsigned long addr = (unsigned long)ptr; |
1da177e4 | 3412 | unsigned long min_addr = PAGE_OFFSET; |
b28a02de | 3413 | unsigned long align_mask = BYTES_PER_WORD - 1; |
3dafccf2 | 3414 | unsigned long size = cachep->buffer_size; |
1da177e4 LT |
3415 | struct page *page; |
3416 | ||
3417 | if (unlikely(addr < min_addr)) | |
3418 | goto out; | |
3419 | if (unlikely(addr > (unsigned long)high_memory - size)) | |
3420 | goto out; | |
3421 | if (unlikely(addr & align_mask)) | |
3422 | goto out; | |
3423 | if (unlikely(!kern_addr_valid(addr))) | |
3424 | goto out; | |
3425 | if (unlikely(!kern_addr_valid(addr + size - 1))) | |
3426 | goto out; | |
3427 | page = virt_to_page(ptr); | |
3428 | if (unlikely(!PageSlab(page))) | |
3429 | goto out; | |
065d41cb | 3430 | if (unlikely(page_get_cache(page) != cachep)) |
1da177e4 LT |
3431 | goto out; |
3432 | return 1; | |
a737b3e2 | 3433 | out: |
1da177e4 LT |
3434 | return 0; |
3435 | } | |
3436 | ||
3437 | #ifdef CONFIG_NUMA | |
3438 | /** | |
3439 | * kmem_cache_alloc_node - Allocate an object on the specified node | |
3440 | * @cachep: The cache to allocate from. | |
3441 | * @flags: See kmalloc(). | |
3442 | * @nodeid: node number of the target node. | |
3443 | * | |
3444 | * Identical to kmem_cache_alloc, except that this function is slow | |
3445 | * and can sleep. And it will allocate memory on the given node, which | |
3446 | * can improve the performance for cpu bound structures. | |
e498be7d CL |
3447 | * New and improved: it will now make sure that the object gets |
3448 | * put on the correct node list so that there is no false sharing. | |
1da177e4 | 3449 | */ |
343e0d7a | 3450 | void *kmem_cache_alloc_node(struct kmem_cache *cachep, gfp_t flags, int nodeid) |
1da177e4 | 3451 | { |
e498be7d CL |
3452 | unsigned long save_flags; |
3453 | void *ptr; | |
1da177e4 | 3454 | |
e498be7d CL |
3455 | cache_alloc_debugcheck_before(cachep, flags); |
3456 | local_irq_save(save_flags); | |
18f820f6 CL |
3457 | |
3458 | if (nodeid == -1 || nodeid == numa_node_id() || | |
a737b3e2 | 3459 | !cachep->nodelists[nodeid]) |
5c382300 AK |
3460 | ptr = ____cache_alloc(cachep, flags); |
3461 | else | |
3462 | ptr = __cache_alloc_node(cachep, flags, nodeid); | |
e498be7d | 3463 | local_irq_restore(save_flags); |
18f820f6 CL |
3464 | |
3465 | ptr = cache_alloc_debugcheck_after(cachep, flags, ptr, | |
3466 | __builtin_return_address(0)); | |
1da177e4 | 3467 | |
e498be7d | 3468 | return ptr; |
1da177e4 LT |
3469 | } |
3470 | EXPORT_SYMBOL(kmem_cache_alloc_node); | |
3471 | ||
dbe5e69d | 3472 | void *__kmalloc_node(size_t size, gfp_t flags, int node) |
97e2bde4 | 3473 | { |
343e0d7a | 3474 | struct kmem_cache *cachep; |
97e2bde4 MS |
3475 | |
3476 | cachep = kmem_find_general_cachep(size, flags); | |
3477 | if (unlikely(cachep == NULL)) | |
3478 | return NULL; | |
3479 | return kmem_cache_alloc_node(cachep, flags, node); | |
3480 | } | |
dbe5e69d | 3481 | EXPORT_SYMBOL(__kmalloc_node); |
1da177e4 LT |
3482 | #endif |
3483 | ||
3484 | /** | |
800590f5 | 3485 | * __do_kmalloc - allocate memory |
1da177e4 | 3486 | * @size: how many bytes of memory are required. |
800590f5 | 3487 | * @flags: the type of memory to allocate (see kmalloc). |
911851e6 | 3488 | * @caller: function caller for debug tracking of the caller |
1da177e4 | 3489 | */ |
7fd6b141 PE |
3490 | static __always_inline void *__do_kmalloc(size_t size, gfp_t flags, |
3491 | void *caller) | |
1da177e4 | 3492 | { |
343e0d7a | 3493 | struct kmem_cache *cachep; |
1da177e4 | 3494 | |
97e2bde4 MS |
3495 | /* If you want to save a few bytes .text space: replace |
3496 | * __ with kmem_. | |
3497 | * Then kmalloc uses the uninlined functions instead of the inline | |
3498 | * functions. | |
3499 | */ | |
3500 | cachep = __find_general_cachep(size, flags); | |
dbdb9045 AM |
3501 | if (unlikely(cachep == NULL)) |
3502 | return NULL; | |
7fd6b141 PE |
3503 | return __cache_alloc(cachep, flags, caller); |
3504 | } | |
3505 | ||
7fd6b141 | 3506 | |
1d2c8eea | 3507 | #ifdef CONFIG_DEBUG_SLAB |
7fd6b141 PE |
3508 | void *__kmalloc(size_t size, gfp_t flags) |
3509 | { | |
871751e2 | 3510 | return __do_kmalloc(size, flags, __builtin_return_address(0)); |
1da177e4 LT |
3511 | } |
3512 | EXPORT_SYMBOL(__kmalloc); | |
3513 | ||
7fd6b141 PE |
3514 | void *__kmalloc_track_caller(size_t size, gfp_t flags, void *caller) |
3515 | { | |
3516 | return __do_kmalloc(size, flags, caller); | |
3517 | } | |
3518 | EXPORT_SYMBOL(__kmalloc_track_caller); | |
1d2c8eea CH |
3519 | |
3520 | #else | |
3521 | void *__kmalloc(size_t size, gfp_t flags) | |
3522 | { | |
3523 | return __do_kmalloc(size, flags, NULL); | |
3524 | } | |
3525 | EXPORT_SYMBOL(__kmalloc); | |
7fd6b141 PE |
3526 | #endif |
3527 | ||
1da177e4 LT |
3528 | /** |
3529 | * kmem_cache_free - Deallocate an object | |
3530 | * @cachep: The cache the allocation was from. | |
3531 | * @objp: The previously allocated object. | |
3532 | * | |
3533 | * Free an object which was previously allocated from this | |
3534 | * cache. | |
3535 | */ | |
343e0d7a | 3536 | void kmem_cache_free(struct kmem_cache *cachep, void *objp) |
1da177e4 LT |
3537 | { |
3538 | unsigned long flags; | |
3539 | ||
ddc2e812 PE |
3540 | BUG_ON(virt_to_cache(objp) != cachep); |
3541 | ||
1da177e4 | 3542 | local_irq_save(flags); |
873623df | 3543 | __cache_free(cachep, objp); |
1da177e4 LT |
3544 | local_irq_restore(flags); |
3545 | } | |
3546 | EXPORT_SYMBOL(kmem_cache_free); | |
3547 | ||
1da177e4 LT |
3548 | /** |
3549 | * kfree - free previously allocated memory | |
3550 | * @objp: pointer returned by kmalloc. | |
3551 | * | |
80e93eff PE |
3552 | * If @objp is NULL, no operation is performed. |
3553 | * | |
1da177e4 LT |
3554 | * Don't free memory not originally allocated by kmalloc() |
3555 | * or you will run into trouble. | |
3556 | */ | |
3557 | void kfree(const void *objp) | |
3558 | { | |
343e0d7a | 3559 | struct kmem_cache *c; |
1da177e4 LT |
3560 | unsigned long flags; |
3561 | ||
3562 | if (unlikely(!objp)) | |
3563 | return; | |
3564 | local_irq_save(flags); | |
3565 | kfree_debugcheck(objp); | |
6ed5eb22 | 3566 | c = virt_to_cache(objp); |
f9b8404c | 3567 | debug_check_no_locks_freed(objp, obj_size(c)); |
873623df | 3568 | __cache_free(c, (void *)objp); |
1da177e4 LT |
3569 | local_irq_restore(flags); |
3570 | } | |
3571 | EXPORT_SYMBOL(kfree); | |
3572 | ||
343e0d7a | 3573 | unsigned int kmem_cache_size(struct kmem_cache *cachep) |
1da177e4 | 3574 | { |
3dafccf2 | 3575 | return obj_size(cachep); |
1da177e4 LT |
3576 | } |
3577 | EXPORT_SYMBOL(kmem_cache_size); | |
3578 | ||
343e0d7a | 3579 | const char *kmem_cache_name(struct kmem_cache *cachep) |
1944972d ACM |
3580 | { |
3581 | return cachep->name; | |
3582 | } | |
3583 | EXPORT_SYMBOL_GPL(kmem_cache_name); | |
3584 | ||
e498be7d | 3585 | /* |
0718dc2a | 3586 | * This initializes kmem_list3 or resizes varioius caches for all nodes. |
e498be7d | 3587 | */ |
343e0d7a | 3588 | static int alloc_kmemlist(struct kmem_cache *cachep) |
e498be7d CL |
3589 | { |
3590 | int node; | |
3591 | struct kmem_list3 *l3; | |
cafeb02e CL |
3592 | struct array_cache *new_shared; |
3593 | struct array_cache **new_alien; | |
e498be7d CL |
3594 | |
3595 | for_each_online_node(node) { | |
cafeb02e | 3596 | |
a737b3e2 AM |
3597 | new_alien = alloc_alien_cache(node, cachep->limit); |
3598 | if (!new_alien) | |
e498be7d | 3599 | goto fail; |
cafeb02e | 3600 | |
0718dc2a CL |
3601 | new_shared = alloc_arraycache(node, |
3602 | cachep->shared*cachep->batchcount, | |
a737b3e2 | 3603 | 0xbaadf00d); |
0718dc2a CL |
3604 | if (!new_shared) { |
3605 | free_alien_cache(new_alien); | |
e498be7d | 3606 | goto fail; |
0718dc2a | 3607 | } |
cafeb02e | 3608 | |
a737b3e2 AM |
3609 | l3 = cachep->nodelists[node]; |
3610 | if (l3) { | |
cafeb02e CL |
3611 | struct array_cache *shared = l3->shared; |
3612 | ||
e498be7d CL |
3613 | spin_lock_irq(&l3->list_lock); |
3614 | ||
cafeb02e | 3615 | if (shared) |
0718dc2a CL |
3616 | free_block(cachep, shared->entry, |
3617 | shared->avail, node); | |
e498be7d | 3618 | |
cafeb02e CL |
3619 | l3->shared = new_shared; |
3620 | if (!l3->alien) { | |
e498be7d CL |
3621 | l3->alien = new_alien; |
3622 | new_alien = NULL; | |
3623 | } | |
b28a02de | 3624 | l3->free_limit = (1 + nr_cpus_node(node)) * |
a737b3e2 | 3625 | cachep->batchcount + cachep->num; |
e498be7d | 3626 | spin_unlock_irq(&l3->list_lock); |
cafeb02e | 3627 | kfree(shared); |
e498be7d CL |
3628 | free_alien_cache(new_alien); |
3629 | continue; | |
3630 | } | |
a737b3e2 | 3631 | l3 = kmalloc_node(sizeof(struct kmem_list3), GFP_KERNEL, node); |
0718dc2a CL |
3632 | if (!l3) { |
3633 | free_alien_cache(new_alien); | |
3634 | kfree(new_shared); | |
e498be7d | 3635 | goto fail; |
0718dc2a | 3636 | } |
e498be7d CL |
3637 | |
3638 | kmem_list3_init(l3); | |
3639 | l3->next_reap = jiffies + REAPTIMEOUT_LIST3 + | |
a737b3e2 | 3640 | ((unsigned long)cachep) % REAPTIMEOUT_LIST3; |
cafeb02e | 3641 | l3->shared = new_shared; |
e498be7d | 3642 | l3->alien = new_alien; |
b28a02de | 3643 | l3->free_limit = (1 + nr_cpus_node(node)) * |
a737b3e2 | 3644 | cachep->batchcount + cachep->num; |
e498be7d CL |
3645 | cachep->nodelists[node] = l3; |
3646 | } | |
cafeb02e | 3647 | return 0; |
0718dc2a | 3648 | |
a737b3e2 | 3649 | fail: |
0718dc2a CL |
3650 | if (!cachep->next.next) { |
3651 | /* Cache is not active yet. Roll back what we did */ | |
3652 | node--; | |
3653 | while (node >= 0) { | |
3654 | if (cachep->nodelists[node]) { | |
3655 | l3 = cachep->nodelists[node]; | |
3656 | ||
3657 | kfree(l3->shared); | |
3658 | free_alien_cache(l3->alien); | |
3659 | kfree(l3); | |
3660 | cachep->nodelists[node] = NULL; | |
3661 | } | |
3662 | node--; | |
3663 | } | |
3664 | } | |
cafeb02e | 3665 | return -ENOMEM; |
e498be7d CL |
3666 | } |
3667 | ||
1da177e4 | 3668 | struct ccupdate_struct { |
343e0d7a | 3669 | struct kmem_cache *cachep; |
1da177e4 LT |
3670 | struct array_cache *new[NR_CPUS]; |
3671 | }; | |
3672 | ||
3673 | static void do_ccupdate_local(void *info) | |
3674 | { | |
a737b3e2 | 3675 | struct ccupdate_struct *new = info; |
1da177e4 LT |
3676 | struct array_cache *old; |
3677 | ||
3678 | check_irq_off(); | |
9a2dba4b | 3679 | old = cpu_cache_get(new->cachep); |
e498be7d | 3680 | |
1da177e4 LT |
3681 | new->cachep->array[smp_processor_id()] = new->new[smp_processor_id()]; |
3682 | new->new[smp_processor_id()] = old; | |
3683 | } | |
3684 | ||
b5d8ca7c | 3685 | /* Always called with the cache_chain_mutex held */ |
a737b3e2 AM |
3686 | static int do_tune_cpucache(struct kmem_cache *cachep, int limit, |
3687 | int batchcount, int shared) | |
1da177e4 | 3688 | { |
d2e7b7d0 | 3689 | struct ccupdate_struct *new; |
2ed3a4ef | 3690 | int i; |
1da177e4 | 3691 | |
d2e7b7d0 SS |
3692 | new = kzalloc(sizeof(*new), GFP_KERNEL); |
3693 | if (!new) | |
3694 | return -ENOMEM; | |
3695 | ||
e498be7d | 3696 | for_each_online_cpu(i) { |
d2e7b7d0 | 3697 | new->new[i] = alloc_arraycache(cpu_to_node(i), limit, |
a737b3e2 | 3698 | batchcount); |
d2e7b7d0 | 3699 | if (!new->new[i]) { |
b28a02de | 3700 | for (i--; i >= 0; i--) |
d2e7b7d0 SS |
3701 | kfree(new->new[i]); |
3702 | kfree(new); | |
e498be7d | 3703 | return -ENOMEM; |
1da177e4 LT |
3704 | } |
3705 | } | |
d2e7b7d0 | 3706 | new->cachep = cachep; |
1da177e4 | 3707 | |
d2e7b7d0 | 3708 | on_each_cpu(do_ccupdate_local, (void *)new, 1, 1); |
e498be7d | 3709 | |
1da177e4 | 3710 | check_irq_on(); |
1da177e4 LT |
3711 | cachep->batchcount = batchcount; |
3712 | cachep->limit = limit; | |
e498be7d | 3713 | cachep->shared = shared; |
1da177e4 | 3714 | |
e498be7d | 3715 | for_each_online_cpu(i) { |
d2e7b7d0 | 3716 | struct array_cache *ccold = new->new[i]; |
1da177e4 LT |
3717 | if (!ccold) |
3718 | continue; | |
e498be7d | 3719 | spin_lock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock); |
ff69416e | 3720 | free_block(cachep, ccold->entry, ccold->avail, cpu_to_node(i)); |
e498be7d | 3721 | spin_unlock_irq(&cachep->nodelists[cpu_to_node(i)]->list_lock); |
1da177e4 LT |
3722 | kfree(ccold); |
3723 | } | |
d2e7b7d0 | 3724 | kfree(new); |
2ed3a4ef | 3725 | return alloc_kmemlist(cachep); |
1da177e4 LT |
3726 | } |
3727 | ||
b5d8ca7c | 3728 | /* Called with cache_chain_mutex held always */ |
2ed3a4ef | 3729 | static int enable_cpucache(struct kmem_cache *cachep) |
1da177e4 LT |
3730 | { |
3731 | int err; | |
3732 | int limit, shared; | |
3733 | ||
a737b3e2 AM |
3734 | /* |
3735 | * The head array serves three purposes: | |
1da177e4 LT |
3736 | * - create a LIFO ordering, i.e. return objects that are cache-warm |
3737 | * - reduce the number of spinlock operations. | |
a737b3e2 | 3738 | * - reduce the number of linked list operations on the slab and |
1da177e4 LT |
3739 | * bufctl chains: array operations are cheaper. |
3740 | * The numbers are guessed, we should auto-tune as described by | |
3741 | * Bonwick. | |
3742 | */ | |
3dafccf2 | 3743 | if (cachep->buffer_size > 131072) |
1da177e4 | 3744 | limit = 1; |
3dafccf2 | 3745 | else if (cachep->buffer_size > PAGE_SIZE) |
1da177e4 | 3746 | limit = 8; |
3dafccf2 | 3747 | else if (cachep->buffer_size > 1024) |
1da177e4 | 3748 | limit = 24; |
3dafccf2 | 3749 | else if (cachep->buffer_size > 256) |
1da177e4 LT |
3750 | limit = 54; |
3751 | else | |
3752 | limit = 120; | |
3753 | ||
a737b3e2 AM |
3754 | /* |
3755 | * CPU bound tasks (e.g. network routing) can exhibit cpu bound | |
1da177e4 LT |
3756 | * allocation behaviour: Most allocs on one cpu, most free operations |
3757 | * on another cpu. For these cases, an efficient object passing between | |
3758 | * cpus is necessary. This is provided by a shared array. The array | |
3759 | * replaces Bonwick's magazine layer. | |
3760 | * On uniprocessor, it's functionally equivalent (but less efficient) | |
3761 | * to a larger limit. Thus disabled by default. | |
3762 | */ | |
3763 | shared = 0; | |
3764 | #ifdef CONFIG_SMP | |
3dafccf2 | 3765 | if (cachep->buffer_size <= PAGE_SIZE) |
1da177e4 LT |
3766 | shared = 8; |
3767 | #endif | |
3768 | ||
3769 | #if DEBUG | |
a737b3e2 AM |
3770 | /* |
3771 | * With debugging enabled, large batchcount lead to excessively long | |
3772 | * periods with disabled local interrupts. Limit the batchcount | |
1da177e4 LT |
3773 | */ |
3774 | if (limit > 32) | |
3775 | limit = 32; | |
3776 | #endif | |
b28a02de | 3777 | err = do_tune_cpucache(cachep, limit, (limit + 1) / 2, shared); |
1da177e4 LT |
3778 | if (err) |
3779 | printk(KERN_ERR "enable_cpucache failed for %s, error %d.\n", | |
b28a02de | 3780 | cachep->name, -err); |
2ed3a4ef | 3781 | return err; |
1da177e4 LT |
3782 | } |
3783 | ||
1b55253a CL |
3784 | /* |
3785 | * Drain an array if it contains any elements taking the l3 lock only if | |
b18e7e65 CL |
3786 | * necessary. Note that the l3 listlock also protects the array_cache |
3787 | * if drain_array() is used on the shared array. | |
1b55253a CL |
3788 | */ |
3789 | void drain_array(struct kmem_cache *cachep, struct kmem_list3 *l3, | |
3790 | struct array_cache *ac, int force, int node) | |
1da177e4 LT |
3791 | { |
3792 | int tofree; | |
3793 | ||
1b55253a CL |
3794 | if (!ac || !ac->avail) |
3795 | return; | |
1da177e4 LT |
3796 | if (ac->touched && !force) { |
3797 | ac->touched = 0; | |
b18e7e65 | 3798 | } else { |
1b55253a | 3799 | spin_lock_irq(&l3->list_lock); |
b18e7e65 CL |
3800 | if (ac->avail) { |
3801 | tofree = force ? ac->avail : (ac->limit + 4) / 5; | |
3802 | if (tofree > ac->avail) | |
3803 | tofree = (ac->avail + 1) / 2; | |
3804 | free_block(cachep, ac->entry, tofree, node); | |
3805 | ac->avail -= tofree; | |
3806 | memmove(ac->entry, &(ac->entry[tofree]), | |
3807 | sizeof(void *) * ac->avail); | |
3808 | } | |
1b55253a | 3809 | spin_unlock_irq(&l3->list_lock); |
1da177e4 LT |
3810 | } |
3811 | } | |
3812 | ||
3813 | /** | |
3814 | * cache_reap - Reclaim memory from caches. | |
1e5d5331 | 3815 | * @unused: unused parameter |
1da177e4 LT |
3816 | * |
3817 | * Called from workqueue/eventd every few seconds. | |
3818 | * Purpose: | |
3819 | * - clear the per-cpu caches for this CPU. | |
3820 | * - return freeable pages to the main free memory pool. | |
3821 | * | |
a737b3e2 AM |
3822 | * If we cannot acquire the cache chain mutex then just give up - we'll try |
3823 | * again on the next iteration. | |
1da177e4 | 3824 | */ |
65f27f38 | 3825 | static void cache_reap(struct work_struct *unused) |
1da177e4 | 3826 | { |
7a7c381d | 3827 | struct kmem_cache *searchp; |
e498be7d | 3828 | struct kmem_list3 *l3; |
aab2207c | 3829 | int node = numa_node_id(); |
1da177e4 | 3830 | |
fc0abb14 | 3831 | if (!mutex_trylock(&cache_chain_mutex)) { |
1da177e4 | 3832 | /* Give up. Setup the next iteration. */ |
b28a02de PE |
3833 | schedule_delayed_work(&__get_cpu_var(reap_work), |
3834 | REAPTIMEOUT_CPUC); | |
1da177e4 LT |
3835 | return; |
3836 | } | |
3837 | ||
7a7c381d | 3838 | list_for_each_entry(searchp, &cache_chain, next) { |
1da177e4 LT |
3839 | check_irq_on(); |
3840 | ||
35386e3b CL |
3841 | /* |
3842 | * We only take the l3 lock if absolutely necessary and we | |
3843 | * have established with reasonable certainty that | |
3844 | * we can do some work if the lock was obtained. | |
3845 | */ | |
aab2207c | 3846 | l3 = searchp->nodelists[node]; |
35386e3b | 3847 | |
8fce4d8e | 3848 | reap_alien(searchp, l3); |
1da177e4 | 3849 | |
aab2207c | 3850 | drain_array(searchp, l3, cpu_cache_get(searchp), 0, node); |
1da177e4 | 3851 | |
35386e3b CL |
3852 | /* |
3853 | * These are racy checks but it does not matter | |
3854 | * if we skip one check or scan twice. | |
3855 | */ | |
e498be7d | 3856 | if (time_after(l3->next_reap, jiffies)) |
35386e3b | 3857 | goto next; |
1da177e4 | 3858 | |
e498be7d | 3859 | l3->next_reap = jiffies + REAPTIMEOUT_LIST3; |
1da177e4 | 3860 | |
aab2207c | 3861 | drain_array(searchp, l3, l3->shared, 0, node); |
1da177e4 | 3862 | |
ed11d9eb | 3863 | if (l3->free_touched) |
e498be7d | 3864 | l3->free_touched = 0; |
ed11d9eb CL |
3865 | else { |
3866 | int freed; | |
1da177e4 | 3867 | |
ed11d9eb CL |
3868 | freed = drain_freelist(searchp, l3, (l3->free_limit + |
3869 | 5 * searchp->num - 1) / (5 * searchp->num)); | |
3870 | STATS_ADD_REAPED(searchp, freed); | |
3871 | } | |
35386e3b | 3872 | next: |
1da177e4 LT |
3873 | cond_resched(); |
3874 | } | |
3875 | check_irq_on(); | |
fc0abb14 | 3876 | mutex_unlock(&cache_chain_mutex); |
8fce4d8e | 3877 | next_reap_node(); |
2244b95a | 3878 | refresh_cpu_vm_stats(smp_processor_id()); |
a737b3e2 | 3879 | /* Set up the next iteration */ |
cd61ef62 | 3880 | schedule_delayed_work(&__get_cpu_var(reap_work), REAPTIMEOUT_CPUC); |
1da177e4 LT |
3881 | } |
3882 | ||
3883 | #ifdef CONFIG_PROC_FS | |
3884 | ||
85289f98 | 3885 | static void print_slabinfo_header(struct seq_file *m) |
1da177e4 | 3886 | { |
85289f98 PE |
3887 | /* |
3888 | * Output format version, so at least we can change it | |
3889 | * without _too_ many complaints. | |
3890 | */ | |
1da177e4 | 3891 | #if STATS |
85289f98 | 3892 | seq_puts(m, "slabinfo - version: 2.1 (statistics)\n"); |
1da177e4 | 3893 | #else |
85289f98 | 3894 | seq_puts(m, "slabinfo - version: 2.1\n"); |
1da177e4 | 3895 | #endif |
85289f98 PE |
3896 | seq_puts(m, "# name <active_objs> <num_objs> <objsize> " |
3897 | "<objperslab> <pagesperslab>"); | |
3898 | seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>"); | |
3899 | seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>"); | |
1da177e4 | 3900 | #if STATS |
85289f98 | 3901 | seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> " |
fb7faf33 | 3902 | "<error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>"); |
85289f98 | 3903 | seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>"); |
1da177e4 | 3904 | #endif |
85289f98 PE |
3905 | seq_putc(m, '\n'); |
3906 | } | |
3907 | ||
3908 | static void *s_start(struct seq_file *m, loff_t *pos) | |
3909 | { | |
3910 | loff_t n = *pos; | |
3911 | struct list_head *p; | |
3912 | ||
fc0abb14 | 3913 | mutex_lock(&cache_chain_mutex); |
85289f98 PE |
3914 | if (!n) |
3915 | print_slabinfo_header(m); | |
1da177e4 LT |
3916 | p = cache_chain.next; |
3917 | while (n--) { | |
3918 | p = p->next; | |
3919 | if (p == &cache_chain) | |
3920 | return NULL; | |
3921 | } | |
343e0d7a | 3922 | return list_entry(p, struct kmem_cache, next); |
1da177e4 LT |
3923 | } |
3924 | ||
3925 | static void *s_next(struct seq_file *m, void *p, loff_t *pos) | |
3926 | { | |
343e0d7a | 3927 | struct kmem_cache *cachep = p; |
1da177e4 | 3928 | ++*pos; |
a737b3e2 AM |
3929 | return cachep->next.next == &cache_chain ? |
3930 | NULL : list_entry(cachep->next.next, struct kmem_cache, next); | |
1da177e4 LT |
3931 | } |
3932 | ||
3933 | static void s_stop(struct seq_file *m, void *p) | |
3934 | { | |
fc0abb14 | 3935 | mutex_unlock(&cache_chain_mutex); |
1da177e4 LT |
3936 | } |
3937 | ||
3938 | static int s_show(struct seq_file *m, void *p) | |
3939 | { | |
343e0d7a | 3940 | struct kmem_cache *cachep = p; |
b28a02de PE |
3941 | struct slab *slabp; |
3942 | unsigned long active_objs; | |
3943 | unsigned long num_objs; | |
3944 | unsigned long active_slabs = 0; | |
3945 | unsigned long num_slabs, free_objects = 0, shared_avail = 0; | |
e498be7d | 3946 | const char *name; |
1da177e4 | 3947 | char *error = NULL; |
e498be7d CL |
3948 | int node; |
3949 | struct kmem_list3 *l3; | |
1da177e4 | 3950 | |
1da177e4 LT |
3951 | active_objs = 0; |
3952 | num_slabs = 0; | |
e498be7d CL |
3953 | for_each_online_node(node) { |
3954 | l3 = cachep->nodelists[node]; | |
3955 | if (!l3) | |
3956 | continue; | |
3957 | ||
ca3b9b91 RT |
3958 | check_irq_on(); |
3959 | spin_lock_irq(&l3->list_lock); | |
e498be7d | 3960 | |
7a7c381d | 3961 | list_for_each_entry(slabp, &l3->slabs_full, list) { |
e498be7d CL |
3962 | if (slabp->inuse != cachep->num && !error) |
3963 | error = "slabs_full accounting error"; | |
3964 | active_objs += cachep->num; | |
3965 | active_slabs++; | |
3966 | } | |
7a7c381d | 3967 | list_for_each_entry(slabp, &l3->slabs_partial, list) { |
e498be7d CL |
3968 | if (slabp->inuse == cachep->num && !error) |
3969 | error = "slabs_partial inuse accounting error"; | |
3970 | if (!slabp->inuse && !error) | |
3971 | error = "slabs_partial/inuse accounting error"; | |
3972 | active_objs += slabp->inuse; | |
3973 | active_slabs++; | |
3974 | } | |
7a7c381d | 3975 | list_for_each_entry(slabp, &l3->slabs_free, list) { |
e498be7d CL |
3976 | if (slabp->inuse && !error) |
3977 | error = "slabs_free/inuse accounting error"; | |
3978 | num_slabs++; | |
3979 | } | |
3980 | free_objects += l3->free_objects; | |
4484ebf1 RT |
3981 | if (l3->shared) |
3982 | shared_avail += l3->shared->avail; | |
e498be7d | 3983 | |
ca3b9b91 | 3984 | spin_unlock_irq(&l3->list_lock); |
1da177e4 | 3985 | } |
b28a02de PE |
3986 | num_slabs += active_slabs; |
3987 | num_objs = num_slabs * cachep->num; | |
e498be7d | 3988 | if (num_objs - active_objs != free_objects && !error) |
1da177e4 LT |
3989 | error = "free_objects accounting error"; |
3990 | ||
b28a02de | 3991 | name = cachep->name; |
1da177e4 LT |
3992 | if (error) |
3993 | printk(KERN_ERR "slab: cache %s error: %s\n", name, error); | |
3994 | ||
3995 | seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", | |
3dafccf2 | 3996 | name, active_objs, num_objs, cachep->buffer_size, |
b28a02de | 3997 | cachep->num, (1 << cachep->gfporder)); |
1da177e4 | 3998 | seq_printf(m, " : tunables %4u %4u %4u", |
b28a02de | 3999 | cachep->limit, cachep->batchcount, cachep->shared); |
e498be7d | 4000 | seq_printf(m, " : slabdata %6lu %6lu %6lu", |
b28a02de | 4001 | active_slabs, num_slabs, shared_avail); |
1da177e4 | 4002 | #if STATS |
b28a02de | 4003 | { /* list3 stats */ |
1da177e4 LT |
4004 | unsigned long high = cachep->high_mark; |
4005 | unsigned long allocs = cachep->num_allocations; | |
4006 | unsigned long grown = cachep->grown; | |
4007 | unsigned long reaped = cachep->reaped; | |
4008 | unsigned long errors = cachep->errors; | |
4009 | unsigned long max_freeable = cachep->max_freeable; | |
1da177e4 | 4010 | unsigned long node_allocs = cachep->node_allocs; |
e498be7d | 4011 | unsigned long node_frees = cachep->node_frees; |
fb7faf33 | 4012 | unsigned long overflows = cachep->node_overflow; |
1da177e4 | 4013 | |
e498be7d | 4014 | seq_printf(m, " : globalstat %7lu %6lu %5lu %4lu \ |
fb7faf33 | 4015 | %4lu %4lu %4lu %4lu %4lu", allocs, high, grown, |
a737b3e2 | 4016 | reaped, errors, max_freeable, node_allocs, |
fb7faf33 | 4017 | node_frees, overflows); |
1da177e4 LT |
4018 | } |
4019 | /* cpu stats */ | |
4020 | { | |
4021 | unsigned long allochit = atomic_read(&cachep->allochit); | |
4022 | unsigned long allocmiss = atomic_read(&cachep->allocmiss); | |
4023 | unsigned long freehit = atomic_read(&cachep->freehit); | |
4024 | unsigned long freemiss = atomic_read(&cachep->freemiss); | |
4025 | ||
4026 | seq_printf(m, " : cpustat %6lu %6lu %6lu %6lu", | |
b28a02de | 4027 | allochit, allocmiss, freehit, freemiss); |
1da177e4 LT |
4028 | } |
4029 | #endif | |
4030 | seq_putc(m, '\n'); | |
1da177e4 LT |
4031 | return 0; |
4032 | } | |
4033 | ||
4034 | /* | |
4035 | * slabinfo_op - iterator that generates /proc/slabinfo | |
4036 | * | |
4037 | * Output layout: | |
4038 | * cache-name | |
4039 | * num-active-objs | |
4040 | * total-objs | |
4041 | * object size | |
4042 | * num-active-slabs | |
4043 | * total-slabs | |
4044 | * num-pages-per-slab | |
4045 | * + further values on SMP and with statistics enabled | |
4046 | */ | |
4047 | ||
4048 | struct seq_operations slabinfo_op = { | |
b28a02de PE |
4049 | .start = s_start, |
4050 | .next = s_next, | |
4051 | .stop = s_stop, | |
4052 | .show = s_show, | |
1da177e4 LT |
4053 | }; |
4054 | ||
4055 | #define MAX_SLABINFO_WRITE 128 | |
4056 | /** | |
4057 | * slabinfo_write - Tuning for the slab allocator | |
4058 | * @file: unused | |
4059 | * @buffer: user buffer | |
4060 | * @count: data length | |
4061 | * @ppos: unused | |
4062 | */ | |
b28a02de PE |
4063 | ssize_t slabinfo_write(struct file *file, const char __user * buffer, |
4064 | size_t count, loff_t *ppos) | |
1da177e4 | 4065 | { |
b28a02de | 4066 | char kbuf[MAX_SLABINFO_WRITE + 1], *tmp; |
1da177e4 | 4067 | int limit, batchcount, shared, res; |
7a7c381d | 4068 | struct kmem_cache *cachep; |
b28a02de | 4069 | |
1da177e4 LT |
4070 | if (count > MAX_SLABINFO_WRITE) |
4071 | return -EINVAL; | |
4072 | if (copy_from_user(&kbuf, buffer, count)) | |
4073 | return -EFAULT; | |
b28a02de | 4074 | kbuf[MAX_SLABINFO_WRITE] = '\0'; |
1da177e4 LT |
4075 | |
4076 | tmp = strchr(kbuf, ' '); | |
4077 | if (!tmp) | |
4078 | return -EINVAL; | |
4079 | *tmp = '\0'; | |
4080 | tmp++; | |
4081 | if (sscanf(tmp, " %d %d %d", &limit, &batchcount, &shared) != 3) | |
4082 | return -EINVAL; | |
4083 | ||
4084 | /* Find the cache in the chain of caches. */ | |
fc0abb14 | 4085 | mutex_lock(&cache_chain_mutex); |
1da177e4 | 4086 | res = -EINVAL; |
7a7c381d | 4087 | list_for_each_entry(cachep, &cache_chain, next) { |
1da177e4 | 4088 | if (!strcmp(cachep->name, kbuf)) { |
a737b3e2 AM |
4089 | if (limit < 1 || batchcount < 1 || |
4090 | batchcount > limit || shared < 0) { | |
e498be7d | 4091 | res = 0; |
1da177e4 | 4092 | } else { |
e498be7d | 4093 | res = do_tune_cpucache(cachep, limit, |
b28a02de | 4094 | batchcount, shared); |
1da177e4 LT |
4095 | } |
4096 | break; | |
4097 | } | |
4098 | } | |
fc0abb14 | 4099 | mutex_unlock(&cache_chain_mutex); |
1da177e4 LT |
4100 | if (res >= 0) |
4101 | res = count; | |
4102 | return res; | |
4103 | } | |
871751e2 AV |
4104 | |
4105 | #ifdef CONFIG_DEBUG_SLAB_LEAK | |
4106 | ||
4107 | static void *leaks_start(struct seq_file *m, loff_t *pos) | |
4108 | { | |
4109 | loff_t n = *pos; | |
4110 | struct list_head *p; | |
4111 | ||
4112 | mutex_lock(&cache_chain_mutex); | |
4113 | p = cache_chain.next; | |
4114 | while (n--) { | |
4115 | p = p->next; | |
4116 | if (p == &cache_chain) | |
4117 | return NULL; | |
4118 | } | |
4119 | return list_entry(p, struct kmem_cache, next); | |
4120 | } | |
4121 | ||
4122 | static inline int add_caller(unsigned long *n, unsigned long v) | |
4123 | { | |
4124 | unsigned long *p; | |
4125 | int l; | |
4126 | if (!v) | |
4127 | return 1; | |
4128 | l = n[1]; | |
4129 | p = n + 2; | |
4130 | while (l) { | |
4131 | int i = l/2; | |
4132 | unsigned long *q = p + 2 * i; | |
4133 | if (*q == v) { | |
4134 | q[1]++; | |
4135 | return 1; | |
4136 | } | |
4137 | if (*q > v) { | |
4138 | l = i; | |
4139 | } else { | |
4140 | p = q + 2; | |
4141 | l -= i + 1; | |
4142 | } | |
4143 | } | |
4144 | if (++n[1] == n[0]) | |
4145 | return 0; | |
4146 | memmove(p + 2, p, n[1] * 2 * sizeof(unsigned long) - ((void *)p - (void *)n)); | |
4147 | p[0] = v; | |
4148 | p[1] = 1; | |
4149 | return 1; | |
4150 | } | |
4151 | ||
4152 | static void handle_slab(unsigned long *n, struct kmem_cache *c, struct slab *s) | |
4153 | { | |
4154 | void *p; | |
4155 | int i; | |
4156 | if (n[0] == n[1]) | |
4157 | return; | |
4158 | for (i = 0, p = s->s_mem; i < c->num; i++, p += c->buffer_size) { | |
4159 | if (slab_bufctl(s)[i] != BUFCTL_ACTIVE) | |
4160 | continue; | |
4161 | if (!add_caller(n, (unsigned long)*dbg_userword(c, p))) | |
4162 | return; | |
4163 | } | |
4164 | } | |
4165 | ||
4166 | static void show_symbol(struct seq_file *m, unsigned long address) | |
4167 | { | |
4168 | #ifdef CONFIG_KALLSYMS | |
4169 | char *modname; | |
4170 | const char *name; | |
4171 | unsigned long offset, size; | |
4172 | char namebuf[KSYM_NAME_LEN+1]; | |
4173 | ||
4174 | name = kallsyms_lookup(address, &size, &offset, &modname, namebuf); | |
4175 | ||
4176 | if (name) { | |
4177 | seq_printf(m, "%s+%#lx/%#lx", name, offset, size); | |
4178 | if (modname) | |
4179 | seq_printf(m, " [%s]", modname); | |
4180 | return; | |
4181 | } | |
4182 | #endif | |
4183 | seq_printf(m, "%p", (void *)address); | |
4184 | } | |
4185 | ||
4186 | static int leaks_show(struct seq_file *m, void *p) | |
4187 | { | |
4188 | struct kmem_cache *cachep = p; | |
871751e2 AV |
4189 | struct slab *slabp; |
4190 | struct kmem_list3 *l3; | |
4191 | const char *name; | |
4192 | unsigned long *n = m->private; | |
4193 | int node; | |
4194 | int i; | |
4195 | ||
4196 | if (!(cachep->flags & SLAB_STORE_USER)) | |
4197 | return 0; | |
4198 | if (!(cachep->flags & SLAB_RED_ZONE)) | |
4199 | return 0; | |
4200 | ||
4201 | /* OK, we can do it */ | |
4202 | ||
4203 | n[1] = 0; | |
4204 | ||
4205 | for_each_online_node(node) { | |
4206 | l3 = cachep->nodelists[node]; | |
4207 | if (!l3) | |
4208 | continue; | |
4209 | ||
4210 | check_irq_on(); | |
4211 | spin_lock_irq(&l3->list_lock); | |
4212 | ||
7a7c381d | 4213 | list_for_each_entry(slabp, &l3->slabs_full, list) |
871751e2 | 4214 | handle_slab(n, cachep, slabp); |
7a7c381d | 4215 | list_for_each_entry(slabp, &l3->slabs_partial, list) |
871751e2 | 4216 | handle_slab(n, cachep, slabp); |
871751e2 AV |
4217 | spin_unlock_irq(&l3->list_lock); |
4218 | } | |
4219 | name = cachep->name; | |
4220 | if (n[0] == n[1]) { | |
4221 | /* Increase the buffer size */ | |
4222 | mutex_unlock(&cache_chain_mutex); | |
4223 | m->private = kzalloc(n[0] * 4 * sizeof(unsigned long), GFP_KERNEL); | |
4224 | if (!m->private) { | |
4225 | /* Too bad, we are really out */ | |
4226 | m->private = n; | |
4227 | mutex_lock(&cache_chain_mutex); | |
4228 | return -ENOMEM; | |
4229 | } | |
4230 | *(unsigned long *)m->private = n[0] * 2; | |
4231 | kfree(n); | |
4232 | mutex_lock(&cache_chain_mutex); | |
4233 | /* Now make sure this entry will be retried */ | |
4234 | m->count = m->size; | |
4235 | return 0; | |
4236 | } | |
4237 | for (i = 0; i < n[1]; i++) { | |
4238 | seq_printf(m, "%s: %lu ", name, n[2*i+3]); | |
4239 | show_symbol(m, n[2*i+2]); | |
4240 | seq_putc(m, '\n'); | |
4241 | } | |
d2e7b7d0 | 4242 | |
871751e2 AV |
4243 | return 0; |
4244 | } | |
4245 | ||
4246 | struct seq_operations slabstats_op = { | |
4247 | .start = leaks_start, | |
4248 | .next = s_next, | |
4249 | .stop = s_stop, | |
4250 | .show = leaks_show, | |
4251 | }; | |
4252 | #endif | |
1da177e4 LT |
4253 | #endif |
4254 | ||
00e145b6 MS |
4255 | /** |
4256 | * ksize - get the actual amount of memory allocated for a given object | |
4257 | * @objp: Pointer to the object | |
4258 | * | |
4259 | * kmalloc may internally round up allocations and return more memory | |
4260 | * than requested. ksize() can be used to determine the actual amount of | |
4261 | * memory allocated. The caller may use this additional memory, even though | |
4262 | * a smaller amount of memory was initially specified with the kmalloc call. | |
4263 | * The caller must guarantee that objp points to a valid object previously | |
4264 | * allocated with either kmalloc() or kmem_cache_alloc(). The object | |
4265 | * must not be freed during the duration of the call. | |
4266 | */ | |
1da177e4 LT |
4267 | unsigned int ksize(const void *objp) |
4268 | { | |
00e145b6 MS |
4269 | if (unlikely(objp == NULL)) |
4270 | return 0; | |
1da177e4 | 4271 | |
6ed5eb22 | 4272 | return obj_size(virt_to_cache(objp)); |
1da177e4 | 4273 | } |