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