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