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