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