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