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