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