]>
Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
039363f3 CL |
2 | /* |
3 | * Slab allocator functions that are independent of the allocator strategy | |
4 | * | |
5 | * (C) 2012 Christoph Lameter <[email protected]> | |
6 | */ | |
7 | #include <linux/slab.h> | |
8 | ||
9 | #include <linux/mm.h> | |
10 | #include <linux/poison.h> | |
11 | #include <linux/interrupt.h> | |
12 | #include <linux/memory.h> | |
13 | #include <linux/compiler.h> | |
14 | #include <linux/module.h> | |
20cea968 CL |
15 | #include <linux/cpu.h> |
16 | #include <linux/uaccess.h> | |
b7454ad3 GC |
17 | #include <linux/seq_file.h> |
18 | #include <linux/proc_fs.h> | |
039363f3 CL |
19 | #include <asm/cacheflush.h> |
20 | #include <asm/tlbflush.h> | |
21 | #include <asm/page.h> | |
2633d7a0 | 22 | #include <linux/memcontrol.h> |
928cec9c AR |
23 | |
24 | #define CREATE_TRACE_POINTS | |
f1b6eb6e | 25 | #include <trace/events/kmem.h> |
039363f3 | 26 | |
97d06609 CL |
27 | #include "slab.h" |
28 | ||
29 | enum slab_state slab_state; | |
18004c5d CL |
30 | LIST_HEAD(slab_caches); |
31 | DEFINE_MUTEX(slab_mutex); | |
9b030cb8 | 32 | struct kmem_cache *kmem_cache; |
97d06609 | 33 | |
657dc2f9 TH |
34 | static LIST_HEAD(slab_caches_to_rcu_destroy); |
35 | static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work); | |
36 | static DECLARE_WORK(slab_caches_to_rcu_destroy_work, | |
37 | slab_caches_to_rcu_destroy_workfn); | |
38 | ||
423c929c JK |
39 | /* |
40 | * Set of flags that will prevent slab merging | |
41 | */ | |
42 | #define SLAB_NEVER_MERGE (SLAB_RED_ZONE | SLAB_POISON | SLAB_STORE_USER | \ | |
5f0d5a3a | 43 | SLAB_TRACE | SLAB_TYPESAFE_BY_RCU | SLAB_NOLEAKTRACE | \ |
7ed2f9e6 | 44 | SLAB_FAILSLAB | SLAB_KASAN) |
423c929c | 45 | |
230e9fc2 | 46 | #define SLAB_MERGE_SAME (SLAB_RECLAIM_ACCOUNT | SLAB_CACHE_DMA | \ |
75f296d9 | 47 | SLAB_ACCOUNT) |
423c929c JK |
48 | |
49 | /* | |
50 | * Merge control. If this is set then no merging of slab caches will occur. | |
423c929c | 51 | */ |
7660a6fd | 52 | static bool slab_nomerge = !IS_ENABLED(CONFIG_SLAB_MERGE_DEFAULT); |
423c929c JK |
53 | |
54 | static int __init setup_slab_nomerge(char *str) | |
55 | { | |
7660a6fd | 56 | slab_nomerge = true; |
423c929c JK |
57 | return 1; |
58 | } | |
59 | ||
60 | #ifdef CONFIG_SLUB | |
61 | __setup_param("slub_nomerge", slub_nomerge, setup_slab_nomerge, 0); | |
62 | #endif | |
63 | ||
64 | __setup("slab_nomerge", setup_slab_nomerge); | |
65 | ||
07f361b2 JK |
66 | /* |
67 | * Determine the size of a slab object | |
68 | */ | |
69 | unsigned int kmem_cache_size(struct kmem_cache *s) | |
70 | { | |
71 | return s->object_size; | |
72 | } | |
73 | EXPORT_SYMBOL(kmem_cache_size); | |
74 | ||
77be4b13 | 75 | #ifdef CONFIG_DEBUG_VM |
794b1248 | 76 | static int kmem_cache_sanity_check(const char *name, size_t size) |
039363f3 CL |
77 | { |
78 | struct kmem_cache *s = NULL; | |
79 | ||
039363f3 CL |
80 | if (!name || in_interrupt() || size < sizeof(void *) || |
81 | size > KMALLOC_MAX_SIZE) { | |
77be4b13 SK |
82 | pr_err("kmem_cache_create(%s) integrity check failed\n", name); |
83 | return -EINVAL; | |
039363f3 | 84 | } |
b920536a | 85 | |
20cea968 CL |
86 | list_for_each_entry(s, &slab_caches, list) { |
87 | char tmp; | |
88 | int res; | |
89 | ||
90 | /* | |
91 | * This happens when the module gets unloaded and doesn't | |
92 | * destroy its slab cache and no-one else reuses the vmalloc | |
93 | * area of the module. Print a warning. | |
94 | */ | |
95 | res = probe_kernel_address(s->name, tmp); | |
96 | if (res) { | |
77be4b13 | 97 | pr_err("Slab cache with size %d has lost its name\n", |
20cea968 CL |
98 | s->object_size); |
99 | continue; | |
100 | } | |
20cea968 CL |
101 | } |
102 | ||
103 | WARN_ON(strchr(name, ' ')); /* It confuses parsers */ | |
77be4b13 SK |
104 | return 0; |
105 | } | |
106 | #else | |
794b1248 | 107 | static inline int kmem_cache_sanity_check(const char *name, size_t size) |
77be4b13 SK |
108 | { |
109 | return 0; | |
110 | } | |
20cea968 CL |
111 | #endif |
112 | ||
484748f0 CL |
113 | void __kmem_cache_free_bulk(struct kmem_cache *s, size_t nr, void **p) |
114 | { | |
115 | size_t i; | |
116 | ||
ca257195 JDB |
117 | for (i = 0; i < nr; i++) { |
118 | if (s) | |
119 | kmem_cache_free(s, p[i]); | |
120 | else | |
121 | kfree(p[i]); | |
122 | } | |
484748f0 CL |
123 | } |
124 | ||
865762a8 | 125 | int __kmem_cache_alloc_bulk(struct kmem_cache *s, gfp_t flags, size_t nr, |
484748f0 CL |
126 | void **p) |
127 | { | |
128 | size_t i; | |
129 | ||
130 | for (i = 0; i < nr; i++) { | |
131 | void *x = p[i] = kmem_cache_alloc(s, flags); | |
132 | if (!x) { | |
133 | __kmem_cache_free_bulk(s, i, p); | |
865762a8 | 134 | return 0; |
484748f0 CL |
135 | } |
136 | } | |
865762a8 | 137 | return i; |
484748f0 CL |
138 | } |
139 | ||
127424c8 | 140 | #if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB) |
510ded33 TH |
141 | |
142 | LIST_HEAD(slab_root_caches); | |
143 | ||
f7ce3190 | 144 | void slab_init_memcg_params(struct kmem_cache *s) |
33a690c4 | 145 | { |
9eeadc8b | 146 | s->memcg_params.root_cache = NULL; |
f7ce3190 | 147 | RCU_INIT_POINTER(s->memcg_params.memcg_caches, NULL); |
9eeadc8b | 148 | INIT_LIST_HEAD(&s->memcg_params.children); |
f7ce3190 VD |
149 | } |
150 | ||
151 | static int init_memcg_params(struct kmem_cache *s, | |
152 | struct mem_cgroup *memcg, struct kmem_cache *root_cache) | |
153 | { | |
154 | struct memcg_cache_array *arr; | |
33a690c4 | 155 | |
9eeadc8b | 156 | if (root_cache) { |
f7ce3190 | 157 | s->memcg_params.root_cache = root_cache; |
9eeadc8b TH |
158 | s->memcg_params.memcg = memcg; |
159 | INIT_LIST_HEAD(&s->memcg_params.children_node); | |
bc2791f8 | 160 | INIT_LIST_HEAD(&s->memcg_params.kmem_caches_node); |
33a690c4 | 161 | return 0; |
f7ce3190 | 162 | } |
33a690c4 | 163 | |
f7ce3190 | 164 | slab_init_memcg_params(s); |
33a690c4 | 165 | |
f7ce3190 VD |
166 | if (!memcg_nr_cache_ids) |
167 | return 0; | |
33a690c4 | 168 | |
f80c7dab JW |
169 | arr = kvzalloc(sizeof(struct memcg_cache_array) + |
170 | memcg_nr_cache_ids * sizeof(void *), | |
171 | GFP_KERNEL); | |
f7ce3190 VD |
172 | if (!arr) |
173 | return -ENOMEM; | |
33a690c4 | 174 | |
f7ce3190 | 175 | RCU_INIT_POINTER(s->memcg_params.memcg_caches, arr); |
33a690c4 VD |
176 | return 0; |
177 | } | |
178 | ||
f7ce3190 | 179 | static void destroy_memcg_params(struct kmem_cache *s) |
33a690c4 | 180 | { |
f7ce3190 | 181 | if (is_root_cache(s)) |
f80c7dab JW |
182 | kvfree(rcu_access_pointer(s->memcg_params.memcg_caches)); |
183 | } | |
184 | ||
185 | static void free_memcg_params(struct rcu_head *rcu) | |
186 | { | |
187 | struct memcg_cache_array *old; | |
188 | ||
189 | old = container_of(rcu, struct memcg_cache_array, rcu); | |
190 | kvfree(old); | |
33a690c4 VD |
191 | } |
192 | ||
f7ce3190 | 193 | static int update_memcg_params(struct kmem_cache *s, int new_array_size) |
6f817f4c | 194 | { |
f7ce3190 | 195 | struct memcg_cache_array *old, *new; |
6f817f4c | 196 | |
f80c7dab JW |
197 | new = kvzalloc(sizeof(struct memcg_cache_array) + |
198 | new_array_size * sizeof(void *), GFP_KERNEL); | |
f7ce3190 | 199 | if (!new) |
6f817f4c VD |
200 | return -ENOMEM; |
201 | ||
f7ce3190 VD |
202 | old = rcu_dereference_protected(s->memcg_params.memcg_caches, |
203 | lockdep_is_held(&slab_mutex)); | |
204 | if (old) | |
205 | memcpy(new->entries, old->entries, | |
206 | memcg_nr_cache_ids * sizeof(void *)); | |
6f817f4c | 207 | |
f7ce3190 VD |
208 | rcu_assign_pointer(s->memcg_params.memcg_caches, new); |
209 | if (old) | |
f80c7dab | 210 | call_rcu(&old->rcu, free_memcg_params); |
6f817f4c VD |
211 | return 0; |
212 | } | |
213 | ||
55007d84 GC |
214 | int memcg_update_all_caches(int num_memcgs) |
215 | { | |
216 | struct kmem_cache *s; | |
217 | int ret = 0; | |
55007d84 | 218 | |
05257a1a | 219 | mutex_lock(&slab_mutex); |
510ded33 | 220 | list_for_each_entry(s, &slab_root_caches, root_caches_node) { |
f7ce3190 | 221 | ret = update_memcg_params(s, num_memcgs); |
55007d84 | 222 | /* |
55007d84 GC |
223 | * Instead of freeing the memory, we'll just leave the caches |
224 | * up to this point in an updated state. | |
225 | */ | |
226 | if (ret) | |
05257a1a | 227 | break; |
55007d84 | 228 | } |
55007d84 GC |
229 | mutex_unlock(&slab_mutex); |
230 | return ret; | |
231 | } | |
657dc2f9 | 232 | |
510ded33 | 233 | void memcg_link_cache(struct kmem_cache *s) |
657dc2f9 | 234 | { |
510ded33 TH |
235 | if (is_root_cache(s)) { |
236 | list_add(&s->root_caches_node, &slab_root_caches); | |
237 | } else { | |
238 | list_add(&s->memcg_params.children_node, | |
239 | &s->memcg_params.root_cache->memcg_params.children); | |
240 | list_add(&s->memcg_params.kmem_caches_node, | |
241 | &s->memcg_params.memcg->kmem_caches); | |
242 | } | |
243 | } | |
244 | ||
245 | static void memcg_unlink_cache(struct kmem_cache *s) | |
246 | { | |
247 | if (is_root_cache(s)) { | |
248 | list_del(&s->root_caches_node); | |
249 | } else { | |
250 | list_del(&s->memcg_params.children_node); | |
251 | list_del(&s->memcg_params.kmem_caches_node); | |
252 | } | |
657dc2f9 | 253 | } |
33a690c4 | 254 | #else |
f7ce3190 VD |
255 | static inline int init_memcg_params(struct kmem_cache *s, |
256 | struct mem_cgroup *memcg, struct kmem_cache *root_cache) | |
33a690c4 VD |
257 | { |
258 | return 0; | |
259 | } | |
260 | ||
f7ce3190 | 261 | static inline void destroy_memcg_params(struct kmem_cache *s) |
33a690c4 VD |
262 | { |
263 | } | |
657dc2f9 | 264 | |
510ded33 | 265 | static inline void memcg_unlink_cache(struct kmem_cache *s) |
657dc2f9 TH |
266 | { |
267 | } | |
127424c8 | 268 | #endif /* CONFIG_MEMCG && !CONFIG_SLOB */ |
55007d84 | 269 | |
423c929c JK |
270 | /* |
271 | * Find a mergeable slab cache | |
272 | */ | |
273 | int slab_unmergeable(struct kmem_cache *s) | |
274 | { | |
275 | if (slab_nomerge || (s->flags & SLAB_NEVER_MERGE)) | |
276 | return 1; | |
277 | ||
278 | if (!is_root_cache(s)) | |
279 | return 1; | |
280 | ||
281 | if (s->ctor) | |
282 | return 1; | |
283 | ||
8eb8284b DW |
284 | if (s->usersize) |
285 | return 1; | |
286 | ||
423c929c JK |
287 | /* |
288 | * We may have set a slab to be unmergeable during bootstrap. | |
289 | */ | |
290 | if (s->refcount < 0) | |
291 | return 1; | |
292 | ||
293 | return 0; | |
294 | } | |
295 | ||
296 | struct kmem_cache *find_mergeable(size_t size, size_t align, | |
d50112ed | 297 | slab_flags_t flags, const char *name, void (*ctor)(void *)) |
423c929c JK |
298 | { |
299 | struct kmem_cache *s; | |
300 | ||
c6e28895 | 301 | if (slab_nomerge) |
423c929c JK |
302 | return NULL; |
303 | ||
304 | if (ctor) | |
305 | return NULL; | |
306 | ||
307 | size = ALIGN(size, sizeof(void *)); | |
308 | align = calculate_alignment(flags, align, size); | |
309 | size = ALIGN(size, align); | |
310 | flags = kmem_cache_flags(size, flags, name, NULL); | |
311 | ||
c6e28895 GM |
312 | if (flags & SLAB_NEVER_MERGE) |
313 | return NULL; | |
314 | ||
510ded33 | 315 | list_for_each_entry_reverse(s, &slab_root_caches, root_caches_node) { |
423c929c JK |
316 | if (slab_unmergeable(s)) |
317 | continue; | |
318 | ||
319 | if (size > s->size) | |
320 | continue; | |
321 | ||
322 | if ((flags & SLAB_MERGE_SAME) != (s->flags & SLAB_MERGE_SAME)) | |
323 | continue; | |
324 | /* | |
325 | * Check if alignment is compatible. | |
326 | * Courtesy of Adrian Drzewiecki | |
327 | */ | |
328 | if ((s->size & ~(align - 1)) != s->size) | |
329 | continue; | |
330 | ||
331 | if (s->size - size >= sizeof(void *)) | |
332 | continue; | |
333 | ||
95069ac8 JK |
334 | if (IS_ENABLED(CONFIG_SLAB) && align && |
335 | (align > s->align || s->align % align)) | |
336 | continue; | |
337 | ||
423c929c JK |
338 | return s; |
339 | } | |
340 | return NULL; | |
341 | } | |
342 | ||
45906855 CL |
343 | /* |
344 | * Figure out what the alignment of the objects will be given a set of | |
345 | * flags, a user specified alignment and the size of the objects. | |
346 | */ | |
d50112ed | 347 | unsigned long calculate_alignment(slab_flags_t flags, |
45906855 CL |
348 | unsigned long align, unsigned long size) |
349 | { | |
350 | /* | |
351 | * If the user wants hardware cache aligned objects then follow that | |
352 | * suggestion if the object is sufficiently large. | |
353 | * | |
354 | * The hardware cache alignment cannot override the specified | |
355 | * alignment though. If that is greater then use it. | |
356 | */ | |
357 | if (flags & SLAB_HWCACHE_ALIGN) { | |
358 | unsigned long ralign = cache_line_size(); | |
359 | while (size <= ralign / 2) | |
360 | ralign /= 2; | |
361 | align = max(align, ralign); | |
362 | } | |
363 | ||
364 | if (align < ARCH_SLAB_MINALIGN) | |
365 | align = ARCH_SLAB_MINALIGN; | |
366 | ||
367 | return ALIGN(align, sizeof(void *)); | |
368 | } | |
369 | ||
c9a77a79 VD |
370 | static struct kmem_cache *create_cache(const char *name, |
371 | size_t object_size, size_t size, size_t align, | |
8eb8284b DW |
372 | slab_flags_t flags, size_t useroffset, |
373 | size_t usersize, void (*ctor)(void *), | |
c9a77a79 | 374 | struct mem_cgroup *memcg, struct kmem_cache *root_cache) |
794b1248 VD |
375 | { |
376 | struct kmem_cache *s; | |
377 | int err; | |
378 | ||
8eb8284b DW |
379 | if (WARN_ON(useroffset + usersize > object_size)) |
380 | useroffset = usersize = 0; | |
381 | ||
794b1248 VD |
382 | err = -ENOMEM; |
383 | s = kmem_cache_zalloc(kmem_cache, GFP_KERNEL); | |
384 | if (!s) | |
385 | goto out; | |
386 | ||
387 | s->name = name; | |
388 | s->object_size = object_size; | |
389 | s->size = size; | |
390 | s->align = align; | |
391 | s->ctor = ctor; | |
8eb8284b DW |
392 | s->useroffset = useroffset; |
393 | s->usersize = usersize; | |
794b1248 | 394 | |
f7ce3190 | 395 | err = init_memcg_params(s, memcg, root_cache); |
794b1248 VD |
396 | if (err) |
397 | goto out_free_cache; | |
398 | ||
399 | err = __kmem_cache_create(s, flags); | |
400 | if (err) | |
401 | goto out_free_cache; | |
402 | ||
403 | s->refcount = 1; | |
404 | list_add(&s->list, &slab_caches); | |
510ded33 | 405 | memcg_link_cache(s); |
794b1248 VD |
406 | out: |
407 | if (err) | |
408 | return ERR_PTR(err); | |
409 | return s; | |
410 | ||
411 | out_free_cache: | |
f7ce3190 | 412 | destroy_memcg_params(s); |
7c4da061 | 413 | kmem_cache_free(kmem_cache, s); |
794b1248 VD |
414 | goto out; |
415 | } | |
45906855 | 416 | |
77be4b13 | 417 | /* |
8eb8284b | 418 | * kmem_cache_create_usercopy - Create a cache. |
77be4b13 SK |
419 | * @name: A string which is used in /proc/slabinfo to identify this cache. |
420 | * @size: The size of objects to be created in this cache. | |
421 | * @align: The required alignment for the objects. | |
422 | * @flags: SLAB flags | |
8eb8284b DW |
423 | * @useroffset: Usercopy region offset |
424 | * @usersize: Usercopy region size | |
77be4b13 SK |
425 | * @ctor: A constructor for the objects. |
426 | * | |
427 | * Returns a ptr to the cache on success, NULL on failure. | |
428 | * Cannot be called within a interrupt, but can be interrupted. | |
429 | * The @ctor is run when new pages are allocated by the cache. | |
430 | * | |
431 | * The flags are | |
432 | * | |
433 | * %SLAB_POISON - Poison the slab with a known test pattern (a5a5a5a5) | |
434 | * to catch references to uninitialised memory. | |
435 | * | |
436 | * %SLAB_RED_ZONE - Insert `Red' zones around the allocated memory to check | |
437 | * for buffer overruns. | |
438 | * | |
439 | * %SLAB_HWCACHE_ALIGN - Align the objects in this cache to a hardware | |
440 | * cacheline. This can be beneficial if you're counting cycles as closely | |
441 | * as davem. | |
442 | */ | |
2633d7a0 | 443 | struct kmem_cache * |
8eb8284b DW |
444 | kmem_cache_create_usercopy(const char *name, size_t size, size_t align, |
445 | slab_flags_t flags, size_t useroffset, size_t usersize, | |
446 | void (*ctor)(void *)) | |
77be4b13 | 447 | { |
40911a79 | 448 | struct kmem_cache *s = NULL; |
3dec16ea | 449 | const char *cache_name; |
3965fc36 | 450 | int err; |
039363f3 | 451 | |
77be4b13 | 452 | get_online_cpus(); |
03afc0e2 | 453 | get_online_mems(); |
05257a1a | 454 | memcg_get_cache_ids(); |
03afc0e2 | 455 | |
77be4b13 | 456 | mutex_lock(&slab_mutex); |
686d550d | 457 | |
794b1248 | 458 | err = kmem_cache_sanity_check(name, size); |
3aa24f51 | 459 | if (err) { |
3965fc36 | 460 | goto out_unlock; |
3aa24f51 | 461 | } |
686d550d | 462 | |
e70954fd TG |
463 | /* Refuse requests with allocator specific flags */ |
464 | if (flags & ~SLAB_FLAGS_PERMITTED) { | |
465 | err = -EINVAL; | |
466 | goto out_unlock; | |
467 | } | |
468 | ||
d8843922 GC |
469 | /* |
470 | * Some allocators will constraint the set of valid flags to a subset | |
471 | * of all flags. We expect them to define CACHE_CREATE_MASK in this | |
472 | * case, and we'll just provide them with a sanitized version of the | |
473 | * passed flags. | |
474 | */ | |
475 | flags &= CACHE_CREATE_MASK; | |
686d550d | 476 | |
8eb8284b DW |
477 | /* Fail closed on bad usersize of useroffset values. */ |
478 | if (WARN_ON(!usersize && useroffset) || | |
479 | WARN_ON(size < usersize || size - usersize < useroffset)) | |
480 | usersize = useroffset = 0; | |
481 | ||
482 | if (!usersize) | |
483 | s = __kmem_cache_alias(name, size, align, flags, ctor); | |
794b1248 | 484 | if (s) |
3965fc36 | 485 | goto out_unlock; |
2633d7a0 | 486 | |
3dec16ea | 487 | cache_name = kstrdup_const(name, GFP_KERNEL); |
794b1248 VD |
488 | if (!cache_name) { |
489 | err = -ENOMEM; | |
490 | goto out_unlock; | |
491 | } | |
7c9adf5a | 492 | |
c9a77a79 VD |
493 | s = create_cache(cache_name, size, size, |
494 | calculate_alignment(flags, align, size), | |
8eb8284b | 495 | flags, useroffset, usersize, ctor, NULL, NULL); |
794b1248 VD |
496 | if (IS_ERR(s)) { |
497 | err = PTR_ERR(s); | |
3dec16ea | 498 | kfree_const(cache_name); |
794b1248 | 499 | } |
3965fc36 VD |
500 | |
501 | out_unlock: | |
20cea968 | 502 | mutex_unlock(&slab_mutex); |
03afc0e2 | 503 | |
05257a1a | 504 | memcg_put_cache_ids(); |
03afc0e2 | 505 | put_online_mems(); |
20cea968 CL |
506 | put_online_cpus(); |
507 | ||
ba3253c7 | 508 | if (err) { |
686d550d CL |
509 | if (flags & SLAB_PANIC) |
510 | panic("kmem_cache_create: Failed to create slab '%s'. Error %d\n", | |
511 | name, err); | |
512 | else { | |
1170532b | 513 | pr_warn("kmem_cache_create(%s) failed with error %d\n", |
686d550d CL |
514 | name, err); |
515 | dump_stack(); | |
516 | } | |
686d550d CL |
517 | return NULL; |
518 | } | |
039363f3 CL |
519 | return s; |
520 | } | |
8eb8284b DW |
521 | EXPORT_SYMBOL(kmem_cache_create_usercopy); |
522 | ||
523 | struct kmem_cache * | |
524 | kmem_cache_create(const char *name, size_t size, size_t align, | |
525 | slab_flags_t flags, void (*ctor)(void *)) | |
526 | { | |
527 | return kmem_cache_create_usercopy(name, size, align, flags, 0, size, | |
528 | ctor); | |
529 | } | |
794b1248 | 530 | EXPORT_SYMBOL(kmem_cache_create); |
2633d7a0 | 531 | |
657dc2f9 | 532 | static void slab_caches_to_rcu_destroy_workfn(struct work_struct *work) |
d5b3cf71 | 533 | { |
657dc2f9 TH |
534 | LIST_HEAD(to_destroy); |
535 | struct kmem_cache *s, *s2; | |
d5b3cf71 | 536 | |
657dc2f9 | 537 | /* |
5f0d5a3a | 538 | * On destruction, SLAB_TYPESAFE_BY_RCU kmem_caches are put on the |
657dc2f9 TH |
539 | * @slab_caches_to_rcu_destroy list. The slab pages are freed |
540 | * through RCU and and the associated kmem_cache are dereferenced | |
541 | * while freeing the pages, so the kmem_caches should be freed only | |
542 | * after the pending RCU operations are finished. As rcu_barrier() | |
543 | * is a pretty slow operation, we batch all pending destructions | |
544 | * asynchronously. | |
545 | */ | |
546 | mutex_lock(&slab_mutex); | |
547 | list_splice_init(&slab_caches_to_rcu_destroy, &to_destroy); | |
548 | mutex_unlock(&slab_mutex); | |
d5b3cf71 | 549 | |
657dc2f9 TH |
550 | if (list_empty(&to_destroy)) |
551 | return; | |
552 | ||
553 | rcu_barrier(); | |
554 | ||
555 | list_for_each_entry_safe(s, s2, &to_destroy, list) { | |
556 | #ifdef SLAB_SUPPORTS_SYSFS | |
557 | sysfs_slab_release(s); | |
558 | #else | |
559 | slab_kmem_cache_release(s); | |
560 | #endif | |
561 | } | |
d5b3cf71 VD |
562 | } |
563 | ||
657dc2f9 | 564 | static int shutdown_cache(struct kmem_cache *s) |
d5b3cf71 | 565 | { |
f9fa1d91 GT |
566 | /* free asan quarantined objects */ |
567 | kasan_cache_shutdown(s); | |
568 | ||
657dc2f9 TH |
569 | if (__kmem_cache_shutdown(s) != 0) |
570 | return -EBUSY; | |
d5b3cf71 | 571 | |
510ded33 | 572 | memcg_unlink_cache(s); |
657dc2f9 | 573 | list_del(&s->list); |
d5b3cf71 | 574 | |
5f0d5a3a | 575 | if (s->flags & SLAB_TYPESAFE_BY_RCU) { |
657dc2f9 TH |
576 | list_add_tail(&s->list, &slab_caches_to_rcu_destroy); |
577 | schedule_work(&slab_caches_to_rcu_destroy_work); | |
578 | } else { | |
d5b3cf71 | 579 | #ifdef SLAB_SUPPORTS_SYSFS |
bf5eb3de | 580 | sysfs_slab_release(s); |
d5b3cf71 VD |
581 | #else |
582 | slab_kmem_cache_release(s); | |
583 | #endif | |
584 | } | |
657dc2f9 TH |
585 | |
586 | return 0; | |
d5b3cf71 VD |
587 | } |
588 | ||
127424c8 | 589 | #if defined(CONFIG_MEMCG) && !defined(CONFIG_SLOB) |
794b1248 | 590 | /* |
776ed0f0 | 591 | * memcg_create_kmem_cache - Create a cache for a memory cgroup. |
794b1248 VD |
592 | * @memcg: The memory cgroup the new cache is for. |
593 | * @root_cache: The parent of the new cache. | |
594 | * | |
595 | * This function attempts to create a kmem cache that will serve allocation | |
596 | * requests going from @memcg to @root_cache. The new cache inherits properties | |
597 | * from its parent. | |
598 | */ | |
d5b3cf71 VD |
599 | void memcg_create_kmem_cache(struct mem_cgroup *memcg, |
600 | struct kmem_cache *root_cache) | |
2633d7a0 | 601 | { |
3e0350a3 | 602 | static char memcg_name_buf[NAME_MAX + 1]; /* protected by slab_mutex */ |
33398cf2 | 603 | struct cgroup_subsys_state *css = &memcg->css; |
f7ce3190 | 604 | struct memcg_cache_array *arr; |
bd673145 | 605 | struct kmem_cache *s = NULL; |
794b1248 | 606 | char *cache_name; |
f7ce3190 | 607 | int idx; |
794b1248 VD |
608 | |
609 | get_online_cpus(); | |
03afc0e2 VD |
610 | get_online_mems(); |
611 | ||
794b1248 VD |
612 | mutex_lock(&slab_mutex); |
613 | ||
2a4db7eb | 614 | /* |
567e9ab2 | 615 | * The memory cgroup could have been offlined while the cache |
2a4db7eb VD |
616 | * creation work was pending. |
617 | */ | |
b6ecd2de | 618 | if (memcg->kmem_state != KMEM_ONLINE) |
2a4db7eb VD |
619 | goto out_unlock; |
620 | ||
f7ce3190 VD |
621 | idx = memcg_cache_id(memcg); |
622 | arr = rcu_dereference_protected(root_cache->memcg_params.memcg_caches, | |
623 | lockdep_is_held(&slab_mutex)); | |
624 | ||
d5b3cf71 VD |
625 | /* |
626 | * Since per-memcg caches are created asynchronously on first | |
627 | * allocation (see memcg_kmem_get_cache()), several threads can try to | |
628 | * create the same cache, but only one of them may succeed. | |
629 | */ | |
f7ce3190 | 630 | if (arr->entries[idx]) |
d5b3cf71 VD |
631 | goto out_unlock; |
632 | ||
f1008365 | 633 | cgroup_name(css->cgroup, memcg_name_buf, sizeof(memcg_name_buf)); |
73f576c0 JW |
634 | cache_name = kasprintf(GFP_KERNEL, "%s(%llu:%s)", root_cache->name, |
635 | css->serial_nr, memcg_name_buf); | |
794b1248 VD |
636 | if (!cache_name) |
637 | goto out_unlock; | |
638 | ||
c9a77a79 VD |
639 | s = create_cache(cache_name, root_cache->object_size, |
640 | root_cache->size, root_cache->align, | |
f773e36d | 641 | root_cache->flags & CACHE_CREATE_MASK, |
8eb8284b | 642 | root_cache->useroffset, root_cache->usersize, |
f773e36d | 643 | root_cache->ctor, memcg, root_cache); |
d5b3cf71 VD |
644 | /* |
645 | * If we could not create a memcg cache, do not complain, because | |
646 | * that's not critical at all as we can always proceed with the root | |
647 | * cache. | |
648 | */ | |
bd673145 | 649 | if (IS_ERR(s)) { |
794b1248 | 650 | kfree(cache_name); |
d5b3cf71 | 651 | goto out_unlock; |
bd673145 | 652 | } |
794b1248 | 653 | |
d5b3cf71 VD |
654 | /* |
655 | * Since readers won't lock (see cache_from_memcg_idx()), we need a | |
656 | * barrier here to ensure nobody will see the kmem_cache partially | |
657 | * initialized. | |
658 | */ | |
659 | smp_wmb(); | |
f7ce3190 | 660 | arr->entries[idx] = s; |
d5b3cf71 | 661 | |
794b1248 VD |
662 | out_unlock: |
663 | mutex_unlock(&slab_mutex); | |
03afc0e2 VD |
664 | |
665 | put_online_mems(); | |
794b1248 | 666 | put_online_cpus(); |
2633d7a0 | 667 | } |
b8529907 | 668 | |
01fb58bc TH |
669 | static void kmemcg_deactivate_workfn(struct work_struct *work) |
670 | { | |
671 | struct kmem_cache *s = container_of(work, struct kmem_cache, | |
672 | memcg_params.deact_work); | |
673 | ||
674 | get_online_cpus(); | |
675 | get_online_mems(); | |
676 | ||
677 | mutex_lock(&slab_mutex); | |
678 | ||
679 | s->memcg_params.deact_fn(s); | |
680 | ||
681 | mutex_unlock(&slab_mutex); | |
682 | ||
683 | put_online_mems(); | |
684 | put_online_cpus(); | |
685 | ||
686 | /* done, put the ref from slab_deactivate_memcg_cache_rcu_sched() */ | |
687 | css_put(&s->memcg_params.memcg->css); | |
688 | } | |
689 | ||
690 | static void kmemcg_deactivate_rcufn(struct rcu_head *head) | |
691 | { | |
692 | struct kmem_cache *s = container_of(head, struct kmem_cache, | |
693 | memcg_params.deact_rcu_head); | |
694 | ||
695 | /* | |
696 | * We need to grab blocking locks. Bounce to ->deact_work. The | |
697 | * work item shares the space with the RCU head and can't be | |
698 | * initialized eariler. | |
699 | */ | |
700 | INIT_WORK(&s->memcg_params.deact_work, kmemcg_deactivate_workfn); | |
17cc4dfe | 701 | queue_work(memcg_kmem_cache_wq, &s->memcg_params.deact_work); |
01fb58bc TH |
702 | } |
703 | ||
704 | /** | |
705 | * slab_deactivate_memcg_cache_rcu_sched - schedule deactivation after a | |
706 | * sched RCU grace period | |
707 | * @s: target kmem_cache | |
708 | * @deact_fn: deactivation function to call | |
709 | * | |
710 | * Schedule @deact_fn to be invoked with online cpus, mems and slab_mutex | |
711 | * held after a sched RCU grace period. The slab is guaranteed to stay | |
712 | * alive until @deact_fn is finished. This is to be used from | |
713 | * __kmemcg_cache_deactivate(). | |
714 | */ | |
715 | void slab_deactivate_memcg_cache_rcu_sched(struct kmem_cache *s, | |
716 | void (*deact_fn)(struct kmem_cache *)) | |
717 | { | |
718 | if (WARN_ON_ONCE(is_root_cache(s)) || | |
719 | WARN_ON_ONCE(s->memcg_params.deact_fn)) | |
720 | return; | |
721 | ||
722 | /* pin memcg so that @s doesn't get destroyed in the middle */ | |
723 | css_get(&s->memcg_params.memcg->css); | |
724 | ||
725 | s->memcg_params.deact_fn = deact_fn; | |
726 | call_rcu_sched(&s->memcg_params.deact_rcu_head, kmemcg_deactivate_rcufn); | |
727 | } | |
728 | ||
2a4db7eb VD |
729 | void memcg_deactivate_kmem_caches(struct mem_cgroup *memcg) |
730 | { | |
731 | int idx; | |
732 | struct memcg_cache_array *arr; | |
d6e0b7fa | 733 | struct kmem_cache *s, *c; |
2a4db7eb VD |
734 | |
735 | idx = memcg_cache_id(memcg); | |
736 | ||
d6e0b7fa VD |
737 | get_online_cpus(); |
738 | get_online_mems(); | |
739 | ||
2a4db7eb | 740 | mutex_lock(&slab_mutex); |
510ded33 | 741 | list_for_each_entry(s, &slab_root_caches, root_caches_node) { |
2a4db7eb VD |
742 | arr = rcu_dereference_protected(s->memcg_params.memcg_caches, |
743 | lockdep_is_held(&slab_mutex)); | |
d6e0b7fa VD |
744 | c = arr->entries[idx]; |
745 | if (!c) | |
746 | continue; | |
747 | ||
c9fc5864 | 748 | __kmemcg_cache_deactivate(c); |
2a4db7eb VD |
749 | arr->entries[idx] = NULL; |
750 | } | |
751 | mutex_unlock(&slab_mutex); | |
d6e0b7fa VD |
752 | |
753 | put_online_mems(); | |
754 | put_online_cpus(); | |
2a4db7eb VD |
755 | } |
756 | ||
d5b3cf71 | 757 | void memcg_destroy_kmem_caches(struct mem_cgroup *memcg) |
b8529907 | 758 | { |
d5b3cf71 | 759 | struct kmem_cache *s, *s2; |
b8529907 | 760 | |
d5b3cf71 VD |
761 | get_online_cpus(); |
762 | get_online_mems(); | |
b8529907 | 763 | |
b8529907 | 764 | mutex_lock(&slab_mutex); |
bc2791f8 TH |
765 | list_for_each_entry_safe(s, s2, &memcg->kmem_caches, |
766 | memcg_params.kmem_caches_node) { | |
d5b3cf71 VD |
767 | /* |
768 | * The cgroup is about to be freed and therefore has no charges | |
769 | * left. Hence, all its caches must be empty by now. | |
770 | */ | |
657dc2f9 | 771 | BUG_ON(shutdown_cache(s)); |
d5b3cf71 VD |
772 | } |
773 | mutex_unlock(&slab_mutex); | |
b8529907 | 774 | |
d5b3cf71 VD |
775 | put_online_mems(); |
776 | put_online_cpus(); | |
b8529907 | 777 | } |
d60fdcc9 | 778 | |
657dc2f9 | 779 | static int shutdown_memcg_caches(struct kmem_cache *s) |
d60fdcc9 VD |
780 | { |
781 | struct memcg_cache_array *arr; | |
782 | struct kmem_cache *c, *c2; | |
783 | LIST_HEAD(busy); | |
784 | int i; | |
785 | ||
786 | BUG_ON(!is_root_cache(s)); | |
787 | ||
788 | /* | |
789 | * First, shutdown active caches, i.e. caches that belong to online | |
790 | * memory cgroups. | |
791 | */ | |
792 | arr = rcu_dereference_protected(s->memcg_params.memcg_caches, | |
793 | lockdep_is_held(&slab_mutex)); | |
794 | for_each_memcg_cache_index(i) { | |
795 | c = arr->entries[i]; | |
796 | if (!c) | |
797 | continue; | |
657dc2f9 | 798 | if (shutdown_cache(c)) |
d60fdcc9 VD |
799 | /* |
800 | * The cache still has objects. Move it to a temporary | |
801 | * list so as not to try to destroy it for a second | |
802 | * time while iterating over inactive caches below. | |
803 | */ | |
9eeadc8b | 804 | list_move(&c->memcg_params.children_node, &busy); |
d60fdcc9 VD |
805 | else |
806 | /* | |
807 | * The cache is empty and will be destroyed soon. Clear | |
808 | * the pointer to it in the memcg_caches array so that | |
809 | * it will never be accessed even if the root cache | |
810 | * stays alive. | |
811 | */ | |
812 | arr->entries[i] = NULL; | |
813 | } | |
814 | ||
815 | /* | |
816 | * Second, shutdown all caches left from memory cgroups that are now | |
817 | * offline. | |
818 | */ | |
9eeadc8b TH |
819 | list_for_each_entry_safe(c, c2, &s->memcg_params.children, |
820 | memcg_params.children_node) | |
657dc2f9 | 821 | shutdown_cache(c); |
d60fdcc9 | 822 | |
9eeadc8b | 823 | list_splice(&busy, &s->memcg_params.children); |
d60fdcc9 VD |
824 | |
825 | /* | |
826 | * A cache being destroyed must be empty. In particular, this means | |
827 | * that all per memcg caches attached to it must be empty too. | |
828 | */ | |
9eeadc8b | 829 | if (!list_empty(&s->memcg_params.children)) |
d60fdcc9 VD |
830 | return -EBUSY; |
831 | return 0; | |
832 | } | |
833 | #else | |
657dc2f9 | 834 | static inline int shutdown_memcg_caches(struct kmem_cache *s) |
d60fdcc9 VD |
835 | { |
836 | return 0; | |
837 | } | |
127424c8 | 838 | #endif /* CONFIG_MEMCG && !CONFIG_SLOB */ |
97d06609 | 839 | |
41a21285 CL |
840 | void slab_kmem_cache_release(struct kmem_cache *s) |
841 | { | |
52b4b950 | 842 | __kmem_cache_release(s); |
f7ce3190 | 843 | destroy_memcg_params(s); |
3dec16ea | 844 | kfree_const(s->name); |
41a21285 CL |
845 | kmem_cache_free(kmem_cache, s); |
846 | } | |
847 | ||
945cf2b6 CL |
848 | void kmem_cache_destroy(struct kmem_cache *s) |
849 | { | |
d60fdcc9 | 850 | int err; |
d5b3cf71 | 851 | |
3942d299 SS |
852 | if (unlikely(!s)) |
853 | return; | |
854 | ||
945cf2b6 | 855 | get_online_cpus(); |
03afc0e2 VD |
856 | get_online_mems(); |
857 | ||
945cf2b6 | 858 | mutex_lock(&slab_mutex); |
b8529907 | 859 | |
945cf2b6 | 860 | s->refcount--; |
b8529907 VD |
861 | if (s->refcount) |
862 | goto out_unlock; | |
863 | ||
657dc2f9 | 864 | err = shutdown_memcg_caches(s); |
d60fdcc9 | 865 | if (!err) |
657dc2f9 | 866 | err = shutdown_cache(s); |
b8529907 | 867 | |
cd918c55 | 868 | if (err) { |
756a025f JP |
869 | pr_err("kmem_cache_destroy %s: Slab cache still has objects\n", |
870 | s->name); | |
cd918c55 VD |
871 | dump_stack(); |
872 | } | |
b8529907 VD |
873 | out_unlock: |
874 | mutex_unlock(&slab_mutex); | |
d5b3cf71 | 875 | |
03afc0e2 | 876 | put_online_mems(); |
945cf2b6 CL |
877 | put_online_cpus(); |
878 | } | |
879 | EXPORT_SYMBOL(kmem_cache_destroy); | |
880 | ||
03afc0e2 VD |
881 | /** |
882 | * kmem_cache_shrink - Shrink a cache. | |
883 | * @cachep: The cache to shrink. | |
884 | * | |
885 | * Releases as many slabs as possible for a cache. | |
886 | * To help debugging, a zero exit status indicates all slabs were released. | |
887 | */ | |
888 | int kmem_cache_shrink(struct kmem_cache *cachep) | |
889 | { | |
890 | int ret; | |
891 | ||
892 | get_online_cpus(); | |
893 | get_online_mems(); | |
55834c59 | 894 | kasan_cache_shrink(cachep); |
c9fc5864 | 895 | ret = __kmem_cache_shrink(cachep); |
03afc0e2 VD |
896 | put_online_mems(); |
897 | put_online_cpus(); | |
898 | return ret; | |
899 | } | |
900 | EXPORT_SYMBOL(kmem_cache_shrink); | |
901 | ||
fda90124 | 902 | bool slab_is_available(void) |
97d06609 CL |
903 | { |
904 | return slab_state >= UP; | |
905 | } | |
b7454ad3 | 906 | |
45530c44 CL |
907 | #ifndef CONFIG_SLOB |
908 | /* Create a cache during boot when no slab services are available yet */ | |
909 | void __init create_boot_cache(struct kmem_cache *s, const char *name, size_t size, | |
8eb8284b | 910 | slab_flags_t flags, size_t useroffset, size_t usersize) |
45530c44 CL |
911 | { |
912 | int err; | |
913 | ||
914 | s->name = name; | |
915 | s->size = s->object_size = size; | |
45906855 | 916 | s->align = calculate_alignment(flags, ARCH_KMALLOC_MINALIGN, size); |
8eb8284b DW |
917 | s->useroffset = useroffset; |
918 | s->usersize = usersize; | |
f7ce3190 VD |
919 | |
920 | slab_init_memcg_params(s); | |
921 | ||
45530c44 CL |
922 | err = __kmem_cache_create(s, flags); |
923 | ||
924 | if (err) | |
31ba7346 | 925 | panic("Creation of kmalloc slab %s size=%zu failed. Reason %d\n", |
45530c44 CL |
926 | name, size, err); |
927 | ||
928 | s->refcount = -1; /* Exempt from merging for now */ | |
929 | } | |
930 | ||
931 | struct kmem_cache *__init create_kmalloc_cache(const char *name, size_t size, | |
d50112ed | 932 | slab_flags_t flags) |
45530c44 CL |
933 | { |
934 | struct kmem_cache *s = kmem_cache_zalloc(kmem_cache, GFP_NOWAIT); | |
935 | ||
936 | if (!s) | |
937 | panic("Out of memory when creating slab %s\n", name); | |
938 | ||
8eb8284b | 939 | create_boot_cache(s, name, size, flags, 0, size); |
45530c44 | 940 | list_add(&s->list, &slab_caches); |
510ded33 | 941 | memcg_link_cache(s); |
45530c44 CL |
942 | s->refcount = 1; |
943 | return s; | |
944 | } | |
945 | ||
9425c58e CL |
946 | struct kmem_cache *kmalloc_caches[KMALLOC_SHIFT_HIGH + 1]; |
947 | EXPORT_SYMBOL(kmalloc_caches); | |
948 | ||
949 | #ifdef CONFIG_ZONE_DMA | |
950 | struct kmem_cache *kmalloc_dma_caches[KMALLOC_SHIFT_HIGH + 1]; | |
951 | EXPORT_SYMBOL(kmalloc_dma_caches); | |
952 | #endif | |
953 | ||
2c59dd65 CL |
954 | /* |
955 | * Conversion table for small slabs sizes / 8 to the index in the | |
956 | * kmalloc array. This is necessary for slabs < 192 since we have non power | |
957 | * of two cache sizes there. The size of larger slabs can be determined using | |
958 | * fls. | |
959 | */ | |
960 | static s8 size_index[24] = { | |
961 | 3, /* 8 */ | |
962 | 4, /* 16 */ | |
963 | 5, /* 24 */ | |
964 | 5, /* 32 */ | |
965 | 6, /* 40 */ | |
966 | 6, /* 48 */ | |
967 | 6, /* 56 */ | |
968 | 6, /* 64 */ | |
969 | 1, /* 72 */ | |
970 | 1, /* 80 */ | |
971 | 1, /* 88 */ | |
972 | 1, /* 96 */ | |
973 | 7, /* 104 */ | |
974 | 7, /* 112 */ | |
975 | 7, /* 120 */ | |
976 | 7, /* 128 */ | |
977 | 2, /* 136 */ | |
978 | 2, /* 144 */ | |
979 | 2, /* 152 */ | |
980 | 2, /* 160 */ | |
981 | 2, /* 168 */ | |
982 | 2, /* 176 */ | |
983 | 2, /* 184 */ | |
984 | 2 /* 192 */ | |
985 | }; | |
986 | ||
987 | static inline int size_index_elem(size_t bytes) | |
988 | { | |
989 | return (bytes - 1) / 8; | |
990 | } | |
991 | ||
992 | /* | |
993 | * Find the kmem_cache structure that serves a given size of | |
994 | * allocation | |
995 | */ | |
996 | struct kmem_cache *kmalloc_slab(size_t size, gfp_t flags) | |
997 | { | |
998 | int index; | |
999 | ||
9de1bc87 | 1000 | if (unlikely(size > KMALLOC_MAX_SIZE)) { |
907985f4 | 1001 | WARN_ON_ONCE(!(flags & __GFP_NOWARN)); |
6286ae97 | 1002 | return NULL; |
907985f4 | 1003 | } |
6286ae97 | 1004 | |
2c59dd65 CL |
1005 | if (size <= 192) { |
1006 | if (!size) | |
1007 | return ZERO_SIZE_PTR; | |
1008 | ||
1009 | index = size_index[size_index_elem(size)]; | |
1010 | } else | |
1011 | index = fls(size - 1); | |
1012 | ||
1013 | #ifdef CONFIG_ZONE_DMA | |
b1e05416 | 1014 | if (unlikely((flags & GFP_DMA))) |
2c59dd65 CL |
1015 | return kmalloc_dma_caches[index]; |
1016 | ||
1017 | #endif | |
1018 | return kmalloc_caches[index]; | |
1019 | } | |
1020 | ||
4066c33d GG |
1021 | /* |
1022 | * kmalloc_info[] is to make slub_debug=,kmalloc-xx option work at boot time. | |
1023 | * kmalloc_index() supports up to 2^26=64MB, so the final entry of the table is | |
1024 | * kmalloc-67108864. | |
1025 | */ | |
af3b5f87 | 1026 | const struct kmalloc_info_struct kmalloc_info[] __initconst = { |
4066c33d GG |
1027 | {NULL, 0}, {"kmalloc-96", 96}, |
1028 | {"kmalloc-192", 192}, {"kmalloc-8", 8}, | |
1029 | {"kmalloc-16", 16}, {"kmalloc-32", 32}, | |
1030 | {"kmalloc-64", 64}, {"kmalloc-128", 128}, | |
1031 | {"kmalloc-256", 256}, {"kmalloc-512", 512}, | |
1032 | {"kmalloc-1024", 1024}, {"kmalloc-2048", 2048}, | |
1033 | {"kmalloc-4096", 4096}, {"kmalloc-8192", 8192}, | |
1034 | {"kmalloc-16384", 16384}, {"kmalloc-32768", 32768}, | |
1035 | {"kmalloc-65536", 65536}, {"kmalloc-131072", 131072}, | |
1036 | {"kmalloc-262144", 262144}, {"kmalloc-524288", 524288}, | |
1037 | {"kmalloc-1048576", 1048576}, {"kmalloc-2097152", 2097152}, | |
1038 | {"kmalloc-4194304", 4194304}, {"kmalloc-8388608", 8388608}, | |
1039 | {"kmalloc-16777216", 16777216}, {"kmalloc-33554432", 33554432}, | |
1040 | {"kmalloc-67108864", 67108864} | |
1041 | }; | |
1042 | ||
f97d5f63 | 1043 | /* |
34cc6990 DS |
1044 | * Patch up the size_index table if we have strange large alignment |
1045 | * requirements for the kmalloc array. This is only the case for | |
1046 | * MIPS it seems. The standard arches will not generate any code here. | |
1047 | * | |
1048 | * Largest permitted alignment is 256 bytes due to the way we | |
1049 | * handle the index determination for the smaller caches. | |
1050 | * | |
1051 | * Make sure that nothing crazy happens if someone starts tinkering | |
1052 | * around with ARCH_KMALLOC_MINALIGN | |
f97d5f63 | 1053 | */ |
34cc6990 | 1054 | void __init setup_kmalloc_cache_index_table(void) |
f97d5f63 CL |
1055 | { |
1056 | int i; | |
1057 | ||
2c59dd65 CL |
1058 | BUILD_BUG_ON(KMALLOC_MIN_SIZE > 256 || |
1059 | (KMALLOC_MIN_SIZE & (KMALLOC_MIN_SIZE - 1))); | |
1060 | ||
1061 | for (i = 8; i < KMALLOC_MIN_SIZE; i += 8) { | |
1062 | int elem = size_index_elem(i); | |
1063 | ||
1064 | if (elem >= ARRAY_SIZE(size_index)) | |
1065 | break; | |
1066 | size_index[elem] = KMALLOC_SHIFT_LOW; | |
1067 | } | |
1068 | ||
1069 | if (KMALLOC_MIN_SIZE >= 64) { | |
1070 | /* | |
1071 | * The 96 byte size cache is not used if the alignment | |
1072 | * is 64 byte. | |
1073 | */ | |
1074 | for (i = 64 + 8; i <= 96; i += 8) | |
1075 | size_index[size_index_elem(i)] = 7; | |
1076 | ||
1077 | } | |
1078 | ||
1079 | if (KMALLOC_MIN_SIZE >= 128) { | |
1080 | /* | |
1081 | * The 192 byte sized cache is not used if the alignment | |
1082 | * is 128 byte. Redirect kmalloc to use the 256 byte cache | |
1083 | * instead. | |
1084 | */ | |
1085 | for (i = 128 + 8; i <= 192; i += 8) | |
1086 | size_index[size_index_elem(i)] = 8; | |
1087 | } | |
34cc6990 DS |
1088 | } |
1089 | ||
d50112ed | 1090 | static void __init new_kmalloc_cache(int idx, slab_flags_t flags) |
a9730fca CL |
1091 | { |
1092 | kmalloc_caches[idx] = create_kmalloc_cache(kmalloc_info[idx].name, | |
1093 | kmalloc_info[idx].size, flags); | |
1094 | } | |
1095 | ||
34cc6990 DS |
1096 | /* |
1097 | * Create the kmalloc array. Some of the regular kmalloc arrays | |
1098 | * may already have been created because they were needed to | |
1099 | * enable allocations for slab creation. | |
1100 | */ | |
d50112ed | 1101 | void __init create_kmalloc_caches(slab_flags_t flags) |
34cc6990 DS |
1102 | { |
1103 | int i; | |
1104 | ||
a9730fca CL |
1105 | for (i = KMALLOC_SHIFT_LOW; i <= KMALLOC_SHIFT_HIGH; i++) { |
1106 | if (!kmalloc_caches[i]) | |
1107 | new_kmalloc_cache(i, flags); | |
f97d5f63 | 1108 | |
956e46ef | 1109 | /* |
a9730fca CL |
1110 | * Caches that are not of the two-to-the-power-of size. |
1111 | * These have to be created immediately after the | |
1112 | * earlier power of two caches | |
956e46ef | 1113 | */ |
a9730fca CL |
1114 | if (KMALLOC_MIN_SIZE <= 32 && !kmalloc_caches[1] && i == 6) |
1115 | new_kmalloc_cache(1, flags); | |
1116 | if (KMALLOC_MIN_SIZE <= 64 && !kmalloc_caches[2] && i == 7) | |
1117 | new_kmalloc_cache(2, flags); | |
8a965b3b CL |
1118 | } |
1119 | ||
f97d5f63 CL |
1120 | /* Kmalloc array is now usable */ |
1121 | slab_state = UP; | |
1122 | ||
f97d5f63 CL |
1123 | #ifdef CONFIG_ZONE_DMA |
1124 | for (i = 0; i <= KMALLOC_SHIFT_HIGH; i++) { | |
1125 | struct kmem_cache *s = kmalloc_caches[i]; | |
1126 | ||
1127 | if (s) { | |
1128 | int size = kmalloc_size(i); | |
1129 | char *n = kasprintf(GFP_NOWAIT, | |
1130 | "dma-kmalloc-%d", size); | |
1131 | ||
1132 | BUG_ON(!n); | |
1133 | kmalloc_dma_caches[i] = create_kmalloc_cache(n, | |
1134 | size, SLAB_CACHE_DMA | flags); | |
1135 | } | |
1136 | } | |
1137 | #endif | |
1138 | } | |
45530c44 CL |
1139 | #endif /* !CONFIG_SLOB */ |
1140 | ||
cea371f4 VD |
1141 | /* |
1142 | * To avoid unnecessary overhead, we pass through large allocation requests | |
1143 | * directly to the page allocator. We use __GFP_COMP, because we will need to | |
1144 | * know the allocation order to free the pages properly in kfree. | |
1145 | */ | |
52383431 VD |
1146 | void *kmalloc_order(size_t size, gfp_t flags, unsigned int order) |
1147 | { | |
1148 | void *ret; | |
1149 | struct page *page; | |
1150 | ||
1151 | flags |= __GFP_COMP; | |
4949148a | 1152 | page = alloc_pages(flags, order); |
52383431 VD |
1153 | ret = page ? page_address(page) : NULL; |
1154 | kmemleak_alloc(ret, size, 1, flags); | |
505f5dcb | 1155 | kasan_kmalloc_large(ret, size, flags); |
52383431 VD |
1156 | return ret; |
1157 | } | |
1158 | EXPORT_SYMBOL(kmalloc_order); | |
1159 | ||
f1b6eb6e CL |
1160 | #ifdef CONFIG_TRACING |
1161 | void *kmalloc_order_trace(size_t size, gfp_t flags, unsigned int order) | |
1162 | { | |
1163 | void *ret = kmalloc_order(size, flags, order); | |
1164 | trace_kmalloc(_RET_IP_, ret, size, PAGE_SIZE << order, flags); | |
1165 | return ret; | |
1166 | } | |
1167 | EXPORT_SYMBOL(kmalloc_order_trace); | |
1168 | #endif | |
45530c44 | 1169 | |
7c00fce9 TG |
1170 | #ifdef CONFIG_SLAB_FREELIST_RANDOM |
1171 | /* Randomize a generic freelist */ | |
1172 | static void freelist_randomize(struct rnd_state *state, unsigned int *list, | |
1173 | size_t count) | |
1174 | { | |
1175 | size_t i; | |
1176 | unsigned int rand; | |
1177 | ||
1178 | for (i = 0; i < count; i++) | |
1179 | list[i] = i; | |
1180 | ||
1181 | /* Fisher-Yates shuffle */ | |
1182 | for (i = count - 1; i > 0; i--) { | |
1183 | rand = prandom_u32_state(state); | |
1184 | rand %= (i + 1); | |
1185 | swap(list[i], list[rand]); | |
1186 | } | |
1187 | } | |
1188 | ||
1189 | /* Create a random sequence per cache */ | |
1190 | int cache_random_seq_create(struct kmem_cache *cachep, unsigned int count, | |
1191 | gfp_t gfp) | |
1192 | { | |
1193 | struct rnd_state state; | |
1194 | ||
1195 | if (count < 2 || cachep->random_seq) | |
1196 | return 0; | |
1197 | ||
1198 | cachep->random_seq = kcalloc(count, sizeof(unsigned int), gfp); | |
1199 | if (!cachep->random_seq) | |
1200 | return -ENOMEM; | |
1201 | ||
1202 | /* Get best entropy at this stage of boot */ | |
1203 | prandom_seed_state(&state, get_random_long()); | |
1204 | ||
1205 | freelist_randomize(&state, cachep->random_seq, count); | |
1206 | return 0; | |
1207 | } | |
1208 | ||
1209 | /* Destroy the per-cache random freelist sequence */ | |
1210 | void cache_random_seq_destroy(struct kmem_cache *cachep) | |
1211 | { | |
1212 | kfree(cachep->random_seq); | |
1213 | cachep->random_seq = NULL; | |
1214 | } | |
1215 | #endif /* CONFIG_SLAB_FREELIST_RANDOM */ | |
1216 | ||
5b365771 | 1217 | #if defined(CONFIG_SLAB) || defined(CONFIG_SLUB_DEBUG) |
e9b4db2b WL |
1218 | #ifdef CONFIG_SLAB |
1219 | #define SLABINFO_RIGHTS (S_IWUSR | S_IRUSR) | |
1220 | #else | |
1221 | #define SLABINFO_RIGHTS S_IRUSR | |
1222 | #endif | |
1223 | ||
b047501c | 1224 | static void print_slabinfo_header(struct seq_file *m) |
bcee6e2a GC |
1225 | { |
1226 | /* | |
1227 | * Output format version, so at least we can change it | |
1228 | * without _too_ many complaints. | |
1229 | */ | |
1230 | #ifdef CONFIG_DEBUG_SLAB | |
1231 | seq_puts(m, "slabinfo - version: 2.1 (statistics)\n"); | |
1232 | #else | |
1233 | seq_puts(m, "slabinfo - version: 2.1\n"); | |
1234 | #endif | |
756a025f | 1235 | seq_puts(m, "# name <active_objs> <num_objs> <objsize> <objperslab> <pagesperslab>"); |
bcee6e2a GC |
1236 | seq_puts(m, " : tunables <limit> <batchcount> <sharedfactor>"); |
1237 | seq_puts(m, " : slabdata <active_slabs> <num_slabs> <sharedavail>"); | |
1238 | #ifdef CONFIG_DEBUG_SLAB | |
756a025f | 1239 | seq_puts(m, " : globalstat <listallocs> <maxobjs> <grown> <reaped> <error> <maxfreeable> <nodeallocs> <remotefrees> <alienoverflow>"); |
bcee6e2a GC |
1240 | seq_puts(m, " : cpustat <allochit> <allocmiss> <freehit> <freemiss>"); |
1241 | #endif | |
1242 | seq_putc(m, '\n'); | |
1243 | } | |
1244 | ||
1df3b26f | 1245 | void *slab_start(struct seq_file *m, loff_t *pos) |
b7454ad3 | 1246 | { |
b7454ad3 | 1247 | mutex_lock(&slab_mutex); |
510ded33 | 1248 | return seq_list_start(&slab_root_caches, *pos); |
b7454ad3 GC |
1249 | } |
1250 | ||
276a2439 | 1251 | void *slab_next(struct seq_file *m, void *p, loff_t *pos) |
b7454ad3 | 1252 | { |
510ded33 | 1253 | return seq_list_next(p, &slab_root_caches, pos); |
b7454ad3 GC |
1254 | } |
1255 | ||
276a2439 | 1256 | void slab_stop(struct seq_file *m, void *p) |
b7454ad3 GC |
1257 | { |
1258 | mutex_unlock(&slab_mutex); | |
1259 | } | |
1260 | ||
749c5415 GC |
1261 | static void |
1262 | memcg_accumulate_slabinfo(struct kmem_cache *s, struct slabinfo *info) | |
1263 | { | |
1264 | struct kmem_cache *c; | |
1265 | struct slabinfo sinfo; | |
749c5415 GC |
1266 | |
1267 | if (!is_root_cache(s)) | |
1268 | return; | |
1269 | ||
426589f5 | 1270 | for_each_memcg_cache(c, s) { |
749c5415 GC |
1271 | memset(&sinfo, 0, sizeof(sinfo)); |
1272 | get_slabinfo(c, &sinfo); | |
1273 | ||
1274 | info->active_slabs += sinfo.active_slabs; | |
1275 | info->num_slabs += sinfo.num_slabs; | |
1276 | info->shared_avail += sinfo.shared_avail; | |
1277 | info->active_objs += sinfo.active_objs; | |
1278 | info->num_objs += sinfo.num_objs; | |
1279 | } | |
1280 | } | |
1281 | ||
b047501c | 1282 | static void cache_show(struct kmem_cache *s, struct seq_file *m) |
b7454ad3 | 1283 | { |
0d7561c6 GC |
1284 | struct slabinfo sinfo; |
1285 | ||
1286 | memset(&sinfo, 0, sizeof(sinfo)); | |
1287 | get_slabinfo(s, &sinfo); | |
1288 | ||
749c5415 GC |
1289 | memcg_accumulate_slabinfo(s, &sinfo); |
1290 | ||
0d7561c6 | 1291 | seq_printf(m, "%-17s %6lu %6lu %6u %4u %4d", |
749c5415 | 1292 | cache_name(s), sinfo.active_objs, sinfo.num_objs, s->size, |
0d7561c6 GC |
1293 | sinfo.objects_per_slab, (1 << sinfo.cache_order)); |
1294 | ||
1295 | seq_printf(m, " : tunables %4u %4u %4u", | |
1296 | sinfo.limit, sinfo.batchcount, sinfo.shared); | |
1297 | seq_printf(m, " : slabdata %6lu %6lu %6lu", | |
1298 | sinfo.active_slabs, sinfo.num_slabs, sinfo.shared_avail); | |
1299 | slabinfo_show_stats(m, s); | |
1300 | seq_putc(m, '\n'); | |
b7454ad3 GC |
1301 | } |
1302 | ||
1df3b26f | 1303 | static int slab_show(struct seq_file *m, void *p) |
749c5415 | 1304 | { |
510ded33 | 1305 | struct kmem_cache *s = list_entry(p, struct kmem_cache, root_caches_node); |
749c5415 | 1306 | |
510ded33 | 1307 | if (p == slab_root_caches.next) |
1df3b26f | 1308 | print_slabinfo_header(m); |
510ded33 | 1309 | cache_show(s, m); |
b047501c VD |
1310 | return 0; |
1311 | } | |
1312 | ||
852d8be0 YS |
1313 | void dump_unreclaimable_slab(void) |
1314 | { | |
1315 | struct kmem_cache *s, *s2; | |
1316 | struct slabinfo sinfo; | |
1317 | ||
1318 | /* | |
1319 | * Here acquiring slab_mutex is risky since we don't prefer to get | |
1320 | * sleep in oom path. But, without mutex hold, it may introduce a | |
1321 | * risk of crash. | |
1322 | * Use mutex_trylock to protect the list traverse, dump nothing | |
1323 | * without acquiring the mutex. | |
1324 | */ | |
1325 | if (!mutex_trylock(&slab_mutex)) { | |
1326 | pr_warn("excessive unreclaimable slab but cannot dump stats\n"); | |
1327 | return; | |
1328 | } | |
1329 | ||
1330 | pr_info("Unreclaimable slab info:\n"); | |
1331 | pr_info("Name Used Total\n"); | |
1332 | ||
1333 | list_for_each_entry_safe(s, s2, &slab_caches, list) { | |
1334 | if (!is_root_cache(s) || (s->flags & SLAB_RECLAIM_ACCOUNT)) | |
1335 | continue; | |
1336 | ||
1337 | get_slabinfo(s, &sinfo); | |
1338 | ||
1339 | if (sinfo.num_objs > 0) | |
1340 | pr_info("%-17s %10luKB %10luKB\n", cache_name(s), | |
1341 | (sinfo.active_objs * s->size) / 1024, | |
1342 | (sinfo.num_objs * s->size) / 1024); | |
1343 | } | |
1344 | mutex_unlock(&slab_mutex); | |
1345 | } | |
1346 | ||
5b365771 | 1347 | #if defined(CONFIG_MEMCG) |
bc2791f8 TH |
1348 | void *memcg_slab_start(struct seq_file *m, loff_t *pos) |
1349 | { | |
1350 | struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); | |
1351 | ||
1352 | mutex_lock(&slab_mutex); | |
1353 | return seq_list_start(&memcg->kmem_caches, *pos); | |
1354 | } | |
1355 | ||
1356 | void *memcg_slab_next(struct seq_file *m, void *p, loff_t *pos) | |
1357 | { | |
1358 | struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); | |
1359 | ||
1360 | return seq_list_next(p, &memcg->kmem_caches, pos); | |
1361 | } | |
1362 | ||
1363 | void memcg_slab_stop(struct seq_file *m, void *p) | |
1364 | { | |
1365 | mutex_unlock(&slab_mutex); | |
1366 | } | |
1367 | ||
b047501c VD |
1368 | int memcg_slab_show(struct seq_file *m, void *p) |
1369 | { | |
bc2791f8 TH |
1370 | struct kmem_cache *s = list_entry(p, struct kmem_cache, |
1371 | memcg_params.kmem_caches_node); | |
b047501c VD |
1372 | struct mem_cgroup *memcg = mem_cgroup_from_css(seq_css(m)); |
1373 | ||
bc2791f8 | 1374 | if (p == memcg->kmem_caches.next) |
b047501c | 1375 | print_slabinfo_header(m); |
bc2791f8 | 1376 | cache_show(s, m); |
b047501c | 1377 | return 0; |
749c5415 | 1378 | } |
b047501c | 1379 | #endif |
749c5415 | 1380 | |
b7454ad3 GC |
1381 | /* |
1382 | * slabinfo_op - iterator that generates /proc/slabinfo | |
1383 | * | |
1384 | * Output layout: | |
1385 | * cache-name | |
1386 | * num-active-objs | |
1387 | * total-objs | |
1388 | * object size | |
1389 | * num-active-slabs | |
1390 | * total-slabs | |
1391 | * num-pages-per-slab | |
1392 | * + further values on SMP and with statistics enabled | |
1393 | */ | |
1394 | static const struct seq_operations slabinfo_op = { | |
1df3b26f | 1395 | .start = slab_start, |
276a2439 WL |
1396 | .next = slab_next, |
1397 | .stop = slab_stop, | |
1df3b26f | 1398 | .show = slab_show, |
b7454ad3 GC |
1399 | }; |
1400 | ||
1401 | static int slabinfo_open(struct inode *inode, struct file *file) | |
1402 | { | |
1403 | return seq_open(file, &slabinfo_op); | |
1404 | } | |
1405 | ||
1406 | static const struct file_operations proc_slabinfo_operations = { | |
1407 | .open = slabinfo_open, | |
1408 | .read = seq_read, | |
1409 | .write = slabinfo_write, | |
1410 | .llseek = seq_lseek, | |
1411 | .release = seq_release, | |
1412 | }; | |
1413 | ||
1414 | static int __init slab_proc_init(void) | |
1415 | { | |
e9b4db2b WL |
1416 | proc_create("slabinfo", SLABINFO_RIGHTS, NULL, |
1417 | &proc_slabinfo_operations); | |
b7454ad3 GC |
1418 | return 0; |
1419 | } | |
1420 | module_init(slab_proc_init); | |
5b365771 | 1421 | #endif /* CONFIG_SLAB || CONFIG_SLUB_DEBUG */ |
928cec9c AR |
1422 | |
1423 | static __always_inline void *__do_krealloc(const void *p, size_t new_size, | |
1424 | gfp_t flags) | |
1425 | { | |
1426 | void *ret; | |
1427 | size_t ks = 0; | |
1428 | ||
1429 | if (p) | |
1430 | ks = ksize(p); | |
1431 | ||
0316bec2 | 1432 | if (ks >= new_size) { |
505f5dcb | 1433 | kasan_krealloc((void *)p, new_size, flags); |
928cec9c | 1434 | return (void *)p; |
0316bec2 | 1435 | } |
928cec9c AR |
1436 | |
1437 | ret = kmalloc_track_caller(new_size, flags); | |
1438 | if (ret && p) | |
1439 | memcpy(ret, p, ks); | |
1440 | ||
1441 | return ret; | |
1442 | } | |
1443 | ||
1444 | /** | |
1445 | * __krealloc - like krealloc() but don't free @p. | |
1446 | * @p: object to reallocate memory for. | |
1447 | * @new_size: how many bytes of memory are required. | |
1448 | * @flags: the type of memory to allocate. | |
1449 | * | |
1450 | * This function is like krealloc() except it never frees the originally | |
1451 | * allocated buffer. Use this if you don't want to free the buffer immediately | |
1452 | * like, for example, with RCU. | |
1453 | */ | |
1454 | void *__krealloc(const void *p, size_t new_size, gfp_t flags) | |
1455 | { | |
1456 | if (unlikely(!new_size)) | |
1457 | return ZERO_SIZE_PTR; | |
1458 | ||
1459 | return __do_krealloc(p, new_size, flags); | |
1460 | ||
1461 | } | |
1462 | EXPORT_SYMBOL(__krealloc); | |
1463 | ||
1464 | /** | |
1465 | * krealloc - reallocate memory. The contents will remain unchanged. | |
1466 | * @p: object to reallocate memory for. | |
1467 | * @new_size: how many bytes of memory are required. | |
1468 | * @flags: the type of memory to allocate. | |
1469 | * | |
1470 | * The contents of the object pointed to are preserved up to the | |
1471 | * lesser of the new and old sizes. If @p is %NULL, krealloc() | |
1472 | * behaves exactly like kmalloc(). If @new_size is 0 and @p is not a | |
1473 | * %NULL pointer, the object pointed to is freed. | |
1474 | */ | |
1475 | void *krealloc(const void *p, size_t new_size, gfp_t flags) | |
1476 | { | |
1477 | void *ret; | |
1478 | ||
1479 | if (unlikely(!new_size)) { | |
1480 | kfree(p); | |
1481 | return ZERO_SIZE_PTR; | |
1482 | } | |
1483 | ||
1484 | ret = __do_krealloc(p, new_size, flags); | |
1485 | if (ret && p != ret) | |
1486 | kfree(p); | |
1487 | ||
1488 | return ret; | |
1489 | } | |
1490 | EXPORT_SYMBOL(krealloc); | |
1491 | ||
1492 | /** | |
1493 | * kzfree - like kfree but zero memory | |
1494 | * @p: object to free memory of | |
1495 | * | |
1496 | * The memory of the object @p points to is zeroed before freed. | |
1497 | * If @p is %NULL, kzfree() does nothing. | |
1498 | * | |
1499 | * Note: this function zeroes the whole allocated buffer which can be a good | |
1500 | * deal bigger than the requested buffer size passed to kmalloc(). So be | |
1501 | * careful when using this function in performance sensitive code. | |
1502 | */ | |
1503 | void kzfree(const void *p) | |
1504 | { | |
1505 | size_t ks; | |
1506 | void *mem = (void *)p; | |
1507 | ||
1508 | if (unlikely(ZERO_OR_NULL_PTR(mem))) | |
1509 | return; | |
1510 | ks = ksize(mem); | |
1511 | memset(mem, 0, ks); | |
1512 | kfree(mem); | |
1513 | } | |
1514 | EXPORT_SYMBOL(kzfree); | |
1515 | ||
1516 | /* Tracepoints definitions. */ | |
1517 | EXPORT_TRACEPOINT_SYMBOL(kmalloc); | |
1518 | EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc); | |
1519 | EXPORT_TRACEPOINT_SYMBOL(kmalloc_node); | |
1520 | EXPORT_TRACEPOINT_SYMBOL(kmem_cache_alloc_node); | |
1521 | EXPORT_TRACEPOINT_SYMBOL(kfree); | |
1522 | EXPORT_TRACEPOINT_SYMBOL(kmem_cache_free); |