1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/alloc_tag.h>
3 #include <linux/execmem.h>
4 #include <linux/fs.h>
5 #include <linux/gfp.h>
6 #include <linux/kallsyms.h>
7 #include <linux/module.h>
8 #include <linux/page_ext.h>
9 #include <linux/proc_fs.h>
10 #include <linux/seq_buf.h>
11 #include <linux/seq_file.h>
12 #include <linux/vmalloc.h>
13
14 #define ALLOCINFO_FILE_NAME "allocinfo"
15 #define MODULE_ALLOC_TAG_VMAP_SIZE (100000UL * sizeof(struct alloc_tag))
16 #define SECTION_START(NAME) (CODETAG_SECTION_START_PREFIX NAME)
17 #define SECTION_STOP(NAME) (CODETAG_SECTION_STOP_PREFIX NAME)
18
19 #ifdef CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT
20 static bool mem_profiling_support = true;
21 #else
22 static bool mem_profiling_support;
23 #endif
24
25 static struct codetag_type *alloc_tag_cttype;
26
27 DEFINE_PER_CPU(struct alloc_tag_counters, _shared_alloc_tag);
28 EXPORT_SYMBOL(_shared_alloc_tag);
29
30 DEFINE_STATIC_KEY_MAYBE(CONFIG_MEM_ALLOC_PROFILING_ENABLED_BY_DEFAULT,
31 mem_alloc_profiling_key);
32 EXPORT_SYMBOL(mem_alloc_profiling_key);
33
34 DEFINE_STATIC_KEY_FALSE(mem_profiling_compressed);
35
36 struct alloc_tag_kernel_section kernel_tags = { NULL, 0 };
37 unsigned long alloc_tag_ref_mask;
38 int alloc_tag_ref_offs;
39
40 struct allocinfo_private {
41 struct codetag_iterator iter;
42 bool print_header;
43 };
44
allocinfo_start(struct seq_file * m,loff_t * pos)45 static void *allocinfo_start(struct seq_file *m, loff_t *pos)
46 {
47 struct allocinfo_private *priv;
48 struct codetag *ct;
49 loff_t node = *pos;
50
51 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
52 m->private = priv;
53 if (!priv)
54 return NULL;
55
56 priv->print_header = (node == 0);
57 codetag_lock_module_list(alloc_tag_cttype, true);
58 priv->iter = codetag_get_ct_iter(alloc_tag_cttype);
59 while ((ct = codetag_next_ct(&priv->iter)) != NULL && node)
60 node--;
61
62 return ct ? priv : NULL;
63 }
64
allocinfo_next(struct seq_file * m,void * arg,loff_t * pos)65 static void *allocinfo_next(struct seq_file *m, void *arg, loff_t *pos)
66 {
67 struct allocinfo_private *priv = (struct allocinfo_private *)arg;
68 struct codetag *ct = codetag_next_ct(&priv->iter);
69
70 (*pos)++;
71 if (!ct)
72 return NULL;
73
74 return priv;
75 }
76
allocinfo_stop(struct seq_file * m,void * arg)77 static void allocinfo_stop(struct seq_file *m, void *arg)
78 {
79 struct allocinfo_private *priv = (struct allocinfo_private *)m->private;
80
81 if (priv) {
82 codetag_lock_module_list(alloc_tag_cttype, false);
83 kfree(priv);
84 }
85 }
86
print_allocinfo_header(struct seq_buf * buf)87 static void print_allocinfo_header(struct seq_buf *buf)
88 {
89 /* Output format version, so we can change it. */
90 seq_buf_printf(buf, "allocinfo - version: 1.0\n");
91 seq_buf_printf(buf, "# <size> <calls> <tag info>\n");
92 }
93
alloc_tag_to_text(struct seq_buf * out,struct codetag * ct)94 static void alloc_tag_to_text(struct seq_buf *out, struct codetag *ct)
95 {
96 struct alloc_tag *tag = ct_to_alloc_tag(ct);
97 struct alloc_tag_counters counter = alloc_tag_read(tag);
98 s64 bytes = counter.bytes;
99
100 seq_buf_printf(out, "%12lli %8llu ", bytes, counter.calls);
101 codetag_to_text(out, ct);
102 seq_buf_putc(out, ' ');
103 seq_buf_putc(out, '\n');
104 }
105
allocinfo_show(struct seq_file * m,void * arg)106 static int allocinfo_show(struct seq_file *m, void *arg)
107 {
108 struct allocinfo_private *priv = (struct allocinfo_private *)arg;
109 char *bufp;
110 size_t n = seq_get_buf(m, &bufp);
111 struct seq_buf buf;
112
113 seq_buf_init(&buf, bufp, n);
114 if (priv->print_header) {
115 print_allocinfo_header(&buf);
116 priv->print_header = false;
117 }
118 alloc_tag_to_text(&buf, priv->iter.ct);
119 seq_commit(m, seq_buf_used(&buf));
120 return 0;
121 }
122
123 static const struct seq_operations allocinfo_seq_op = {
124 .start = allocinfo_start,
125 .next = allocinfo_next,
126 .stop = allocinfo_stop,
127 .show = allocinfo_show,
128 };
129
alloc_tag_top_users(struct codetag_bytes * tags,size_t count,bool can_sleep)130 size_t alloc_tag_top_users(struct codetag_bytes *tags, size_t count, bool can_sleep)
131 {
132 struct codetag_iterator iter;
133 struct codetag *ct;
134 struct codetag_bytes n;
135 unsigned int i, nr = 0;
136
137 if (IS_ERR_OR_NULL(alloc_tag_cttype))
138 return 0;
139
140 if (can_sleep)
141 codetag_lock_module_list(alloc_tag_cttype, true);
142 else if (!codetag_trylock_module_list(alloc_tag_cttype))
143 return 0;
144
145 iter = codetag_get_ct_iter(alloc_tag_cttype);
146 while ((ct = codetag_next_ct(&iter))) {
147 struct alloc_tag_counters counter = alloc_tag_read(ct_to_alloc_tag(ct));
148
149 n.ct = ct;
150 n.bytes = counter.bytes;
151
152 for (i = 0; i < nr; i++)
153 if (n.bytes > tags[i].bytes)
154 break;
155
156 if (i < count) {
157 nr -= nr == count;
158 memmove(&tags[i + 1],
159 &tags[i],
160 sizeof(tags[0]) * (nr - i));
161 nr++;
162 tags[i] = n;
163 }
164 }
165
166 codetag_lock_module_list(alloc_tag_cttype, false);
167
168 return nr;
169 }
170
pgalloc_tag_split(struct folio * folio,int old_order,int new_order)171 void pgalloc_tag_split(struct folio *folio, int old_order, int new_order)
172 {
173 int i;
174 struct alloc_tag *tag;
175 unsigned int nr_pages = 1 << new_order;
176
177 if (!mem_alloc_profiling_enabled())
178 return;
179
180 tag = __pgalloc_tag_get(&folio->page);
181 if (!tag)
182 return;
183
184 for (i = nr_pages; i < (1 << old_order); i += nr_pages) {
185 union pgtag_ref_handle handle;
186 union codetag_ref ref;
187
188 if (get_page_tag_ref(folio_page(folio, i), &ref, &handle)) {
189 /* Set new reference to point to the original tag */
190 alloc_tag_ref_set(&ref, tag);
191 update_page_tag_ref(handle, &ref);
192 put_page_tag_ref(handle);
193 }
194 }
195 }
196
pgalloc_tag_swap(struct folio * new,struct folio * old)197 void pgalloc_tag_swap(struct folio *new, struct folio *old)
198 {
199 union pgtag_ref_handle handle_old, handle_new;
200 union codetag_ref ref_old, ref_new;
201 struct alloc_tag *tag_old, *tag_new;
202
203 if (!mem_alloc_profiling_enabled())
204 return;
205
206 tag_old = __pgalloc_tag_get(&old->page);
207 if (!tag_old)
208 return;
209 tag_new = __pgalloc_tag_get(&new->page);
210 if (!tag_new)
211 return;
212
213 if (!get_page_tag_ref(&old->page, &ref_old, &handle_old))
214 return;
215 if (!get_page_tag_ref(&new->page, &ref_new, &handle_new)) {
216 put_page_tag_ref(handle_old);
217 return;
218 }
219
220 /*
221 * Clear tag references to avoid debug warning when using
222 * __alloc_tag_ref_set() with non-empty reference.
223 */
224 set_codetag_empty(&ref_old);
225 set_codetag_empty(&ref_new);
226
227 /* swap tags */
228 __alloc_tag_ref_set(&ref_old, tag_new);
229 update_page_tag_ref(handle_old, &ref_old);
230 __alloc_tag_ref_set(&ref_new, tag_old);
231 update_page_tag_ref(handle_new, &ref_new);
232
233 put_page_tag_ref(handle_old);
234 put_page_tag_ref(handle_new);
235 }
236
shutdown_mem_profiling(bool remove_file)237 static void shutdown_mem_profiling(bool remove_file)
238 {
239 if (mem_alloc_profiling_enabled())
240 static_branch_disable(&mem_alloc_profiling_key);
241
242 if (!mem_profiling_support)
243 return;
244
245 if (remove_file)
246 remove_proc_entry(ALLOCINFO_FILE_NAME, NULL);
247 mem_profiling_support = false;
248 }
249
procfs_init(void)250 static void __init procfs_init(void)
251 {
252 if (!mem_profiling_support)
253 return;
254
255 if (!proc_create_seq(ALLOCINFO_FILE_NAME, 0400, NULL, &allocinfo_seq_op)) {
256 pr_err("Failed to create %s file\n", ALLOCINFO_FILE_NAME);
257 shutdown_mem_profiling(false);
258 }
259 }
260
alloc_tag_sec_init(void)261 void __init alloc_tag_sec_init(void)
262 {
263 struct alloc_tag *last_codetag;
264
265 if (!mem_profiling_support)
266 return;
267
268 if (!static_key_enabled(&mem_profiling_compressed))
269 return;
270
271 kernel_tags.first_tag = (struct alloc_tag *)kallsyms_lookup_name(
272 SECTION_START(ALLOC_TAG_SECTION_NAME));
273 last_codetag = (struct alloc_tag *)kallsyms_lookup_name(
274 SECTION_STOP(ALLOC_TAG_SECTION_NAME));
275 kernel_tags.count = last_codetag - kernel_tags.first_tag;
276
277 /* Check if kernel tags fit into page flags */
278 if (kernel_tags.count > (1UL << NR_UNUSED_PAGEFLAG_BITS)) {
279 shutdown_mem_profiling(false); /* allocinfo file does not exist yet */
280 pr_err("%lu allocation tags cannot be references using %d available page flag bits. Memory allocation profiling is disabled!\n",
281 kernel_tags.count, NR_UNUSED_PAGEFLAG_BITS);
282 return;
283 }
284
285 alloc_tag_ref_offs = (LRU_REFS_PGOFF - NR_UNUSED_PAGEFLAG_BITS);
286 alloc_tag_ref_mask = ((1UL << NR_UNUSED_PAGEFLAG_BITS) - 1);
287 pr_debug("Memory allocation profiling compression is using %d page flag bits!\n",
288 NR_UNUSED_PAGEFLAG_BITS);
289 }
290
291 #ifdef CONFIG_MODULES
292
293 static struct maple_tree mod_area_mt = MTREE_INIT(mod_area_mt, MT_FLAGS_ALLOC_RANGE);
294 static struct vm_struct *vm_module_tags;
295 /* A dummy object used to indicate an unloaded module */
296 static struct module unloaded_mod;
297 /* A dummy object used to indicate a module prepended area */
298 static struct module prepend_mod;
299
300 struct alloc_tag_module_section module_tags;
301
alloc_tag_align(unsigned long val)302 static inline unsigned long alloc_tag_align(unsigned long val)
303 {
304 if (!static_key_enabled(&mem_profiling_compressed)) {
305 /* No alignment requirements when we are not indexing the tags */
306 return val;
307 }
308
309 if (val % sizeof(struct alloc_tag) == 0)
310 return val;
311 return ((val / sizeof(struct alloc_tag)) + 1) * sizeof(struct alloc_tag);
312 }
313
ensure_alignment(unsigned long align,unsigned int * prepend)314 static bool ensure_alignment(unsigned long align, unsigned int *prepend)
315 {
316 if (!static_key_enabled(&mem_profiling_compressed)) {
317 /* No alignment requirements when we are not indexing the tags */
318 return true;
319 }
320
321 /*
322 * If alloc_tag size is not a multiple of required alignment, tag
323 * indexing does not work.
324 */
325 if (!IS_ALIGNED(sizeof(struct alloc_tag), align))
326 return false;
327
328 /* Ensure prepend consumes multiple of alloc_tag-sized blocks */
329 if (*prepend)
330 *prepend = alloc_tag_align(*prepend);
331
332 return true;
333 }
334
tags_addressable(void)335 static inline bool tags_addressable(void)
336 {
337 unsigned long tag_idx_count;
338
339 if (!static_key_enabled(&mem_profiling_compressed))
340 return true; /* with page_ext tags are always addressable */
341
342 tag_idx_count = CODETAG_ID_FIRST + kernel_tags.count +
343 module_tags.size / sizeof(struct alloc_tag);
344
345 return tag_idx_count < (1UL << NR_UNUSED_PAGEFLAG_BITS);
346 }
347
needs_section_mem(struct module * mod,unsigned long size)348 static bool needs_section_mem(struct module *mod, unsigned long size)
349 {
350 if (!mem_profiling_support)
351 return false;
352
353 return size >= sizeof(struct alloc_tag);
354 }
355
find_used_tag(struct alloc_tag * from,struct alloc_tag * to)356 static struct alloc_tag *find_used_tag(struct alloc_tag *from, struct alloc_tag *to)
357 {
358 while (from <= to) {
359 struct alloc_tag_counters counter;
360
361 counter = alloc_tag_read(from);
362 if (counter.bytes)
363 return from;
364 from++;
365 }
366
367 return NULL;
368 }
369
370 /* Called with mod_area_mt locked */
clean_unused_module_areas_locked(void)371 static void clean_unused_module_areas_locked(void)
372 {
373 MA_STATE(mas, &mod_area_mt, 0, module_tags.size);
374 struct module *val;
375
376 mas_for_each(&mas, val, module_tags.size) {
377 if (val != &unloaded_mod)
378 continue;
379
380 /* Release area if all tags are unused */
381 if (!find_used_tag((struct alloc_tag *)(module_tags.start_addr + mas.index),
382 (struct alloc_tag *)(module_tags.start_addr + mas.last)))
383 mas_erase(&mas);
384 }
385 }
386
387 /* Called with mod_area_mt locked */
find_aligned_area(struct ma_state * mas,unsigned long section_size,unsigned long size,unsigned int prepend,unsigned long align)388 static bool find_aligned_area(struct ma_state *mas, unsigned long section_size,
389 unsigned long size, unsigned int prepend, unsigned long align)
390 {
391 bool cleanup_done = false;
392
393 repeat:
394 /* Try finding exact size and hope the start is aligned */
395 if (!mas_empty_area(mas, 0, section_size - 1, prepend + size)) {
396 if (IS_ALIGNED(mas->index + prepend, align))
397 return true;
398
399 /* Try finding larger area to align later */
400 mas_reset(mas);
401 if (!mas_empty_area(mas, 0, section_size - 1,
402 size + prepend + align - 1))
403 return true;
404 }
405
406 /* No free area, try cleanup stale data and repeat the search once */
407 if (!cleanup_done) {
408 clean_unused_module_areas_locked();
409 cleanup_done = true;
410 mas_reset(mas);
411 goto repeat;
412 }
413
414 return false;
415 }
416
vm_module_tags_populate(void)417 static int vm_module_tags_populate(void)
418 {
419 unsigned long phys_end = ALIGN_DOWN(module_tags.start_addr, PAGE_SIZE) +
420 (vm_module_tags->nr_pages << PAGE_SHIFT);
421 unsigned long new_end = module_tags.start_addr + module_tags.size;
422
423 if (phys_end < new_end) {
424 struct page **next_page = vm_module_tags->pages + vm_module_tags->nr_pages;
425 unsigned long old_shadow_end = ALIGN(phys_end, MODULE_ALIGN);
426 unsigned long new_shadow_end = ALIGN(new_end, MODULE_ALIGN);
427 unsigned long more_pages;
428 unsigned long nr = 0;
429
430 more_pages = ALIGN(new_end - phys_end, PAGE_SIZE) >> PAGE_SHIFT;
431 while (nr < more_pages) {
432 unsigned long allocated;
433
434 allocated = alloc_pages_bulk_array_node(GFP_KERNEL | __GFP_NOWARN,
435 NUMA_NO_NODE, more_pages - nr, next_page + nr);
436
437 if (!allocated)
438 break;
439 nr += allocated;
440 }
441
442 if (nr < more_pages ||
443 vmap_pages_range(phys_end, phys_end + (nr << PAGE_SHIFT), PAGE_KERNEL,
444 next_page, PAGE_SHIFT) < 0) {
445 /* Clean up and error out */
446 for (int i = 0; i < nr; i++)
447 __free_page(next_page[i]);
448 return -ENOMEM;
449 }
450
451 vm_module_tags->nr_pages += nr;
452
453 /*
454 * Kasan allocates 1 byte of shadow for every 8 bytes of data.
455 * When kasan_alloc_module_shadow allocates shadow memory,
456 * its unit of allocation is a page.
457 * Therefore, here we need to align to MODULE_ALIGN.
458 */
459 if (old_shadow_end < new_shadow_end)
460 kasan_alloc_module_shadow((void *)old_shadow_end,
461 new_shadow_end - old_shadow_end,
462 GFP_KERNEL);
463 }
464
465 /*
466 * Mark the pages as accessible, now that they are mapped.
467 * With hardware tag-based KASAN, marking is skipped for
468 * non-VM_ALLOC mappings, see __kasan_unpoison_vmalloc().
469 */
470 kasan_unpoison_vmalloc((void *)module_tags.start_addr,
471 new_end - module_tags.start_addr,
472 KASAN_VMALLOC_PROT_NORMAL);
473
474 return 0;
475 }
476
reserve_module_tags(struct module * mod,unsigned long size,unsigned int prepend,unsigned long align)477 static void *reserve_module_tags(struct module *mod, unsigned long size,
478 unsigned int prepend, unsigned long align)
479 {
480 unsigned long section_size = module_tags.end_addr - module_tags.start_addr;
481 MA_STATE(mas, &mod_area_mt, 0, section_size - 1);
482 unsigned long offset;
483 void *ret = NULL;
484
485 /* If no tags return error */
486 if (size < sizeof(struct alloc_tag))
487 return ERR_PTR(-EINVAL);
488
489 /*
490 * align is always power of 2, so we can use IS_ALIGNED and ALIGN.
491 * align 0 or 1 means no alignment, to simplify set to 1.
492 */
493 if (!align)
494 align = 1;
495
496 if (!ensure_alignment(align, &prepend)) {
497 shutdown_mem_profiling(true);
498 pr_err("%s: alignment %lu is incompatible with allocation tag indexing. Memory allocation profiling is disabled!\n",
499 mod->name, align);
500 return ERR_PTR(-EINVAL);
501 }
502
503 mas_lock(&mas);
504 if (!find_aligned_area(&mas, section_size, size, prepend, align)) {
505 ret = ERR_PTR(-ENOMEM);
506 goto unlock;
507 }
508
509 /* Mark found area as reserved */
510 offset = mas.index;
511 offset += prepend;
512 offset = ALIGN(offset, align);
513 if (offset != mas.index) {
514 unsigned long pad_start = mas.index;
515
516 mas.last = offset - 1;
517 mas_store(&mas, &prepend_mod);
518 if (mas_is_err(&mas)) {
519 ret = ERR_PTR(xa_err(mas.node));
520 goto unlock;
521 }
522 mas.index = offset;
523 mas.last = offset + size - 1;
524 mas_store(&mas, mod);
525 if (mas_is_err(&mas)) {
526 mas.index = pad_start;
527 mas_erase(&mas);
528 ret = ERR_PTR(xa_err(mas.node));
529 }
530 } else {
531 mas.last = offset + size - 1;
532 mas_store(&mas, mod);
533 if (mas_is_err(&mas))
534 ret = ERR_PTR(xa_err(mas.node));
535 }
536 unlock:
537 mas_unlock(&mas);
538
539 if (IS_ERR(ret))
540 return ret;
541
542 if (module_tags.size < offset + size) {
543 int grow_res;
544
545 module_tags.size = offset + size;
546 if (mem_alloc_profiling_enabled() && !tags_addressable()) {
547 shutdown_mem_profiling(true);
548 pr_warn("With module %s there are too many tags to fit in %d page flag bits. Memory allocation profiling is disabled!\n",
549 mod->name, NR_UNUSED_PAGEFLAG_BITS);
550 }
551
552 grow_res = vm_module_tags_populate();
553 if (grow_res) {
554 shutdown_mem_profiling(true);
555 pr_err("Failed to allocate memory for allocation tags in the module %s. Memory allocation profiling is disabled!\n",
556 mod->name);
557 return ERR_PTR(grow_res);
558 }
559 }
560
561 return (struct alloc_tag *)(module_tags.start_addr + offset);
562 }
563
release_module_tags(struct module * mod,bool used)564 static void release_module_tags(struct module *mod, bool used)
565 {
566 MA_STATE(mas, &mod_area_mt, module_tags.size, module_tags.size);
567 struct alloc_tag *tag;
568 struct module *val;
569
570 mas_lock(&mas);
571 mas_for_each_rev(&mas, val, 0)
572 if (val == mod)
573 break;
574
575 if (!val) /* module not found */
576 goto out;
577
578 if (!used)
579 goto release_area;
580
581 /* Find out if the area is used */
582 tag = find_used_tag((struct alloc_tag *)(module_tags.start_addr + mas.index),
583 (struct alloc_tag *)(module_tags.start_addr + mas.last));
584 if (tag) {
585 struct alloc_tag_counters counter = alloc_tag_read(tag);
586
587 pr_info("%s:%u module %s func:%s has %llu allocated at module unload\n",
588 tag->ct.filename, tag->ct.lineno, tag->ct.modname,
589 tag->ct.function, counter.bytes);
590 } else {
591 used = false;
592 }
593 release_area:
594 mas_store(&mas, used ? &unloaded_mod : NULL);
595 val = mas_prev_range(&mas, 0);
596 if (val == &prepend_mod)
597 mas_store(&mas, NULL);
598 out:
599 mas_unlock(&mas);
600 }
601
replace_module(struct module * mod,struct module * new_mod)602 static void replace_module(struct module *mod, struct module *new_mod)
603 {
604 MA_STATE(mas, &mod_area_mt, 0, module_tags.size);
605 struct module *val;
606
607 mas_lock(&mas);
608 mas_for_each(&mas, val, module_tags.size) {
609 if (val != mod)
610 continue;
611
612 mas_store_gfp(&mas, new_mod, GFP_KERNEL);
613 break;
614 }
615 mas_unlock(&mas);
616 }
617
alloc_mod_tags_mem(void)618 static int __init alloc_mod_tags_mem(void)
619 {
620 /* Map space to copy allocation tags */
621 vm_module_tags = execmem_vmap(MODULE_ALLOC_TAG_VMAP_SIZE);
622 if (!vm_module_tags) {
623 pr_err("Failed to map %lu bytes for module allocation tags\n",
624 MODULE_ALLOC_TAG_VMAP_SIZE);
625 module_tags.start_addr = 0;
626 return -ENOMEM;
627 }
628
629 vm_module_tags->pages = kmalloc_array(get_vm_area_size(vm_module_tags) >> PAGE_SHIFT,
630 sizeof(struct page *), GFP_KERNEL | __GFP_ZERO);
631 if (!vm_module_tags->pages) {
632 free_vm_area(vm_module_tags);
633 return -ENOMEM;
634 }
635
636 module_tags.start_addr = (unsigned long)vm_module_tags->addr;
637 module_tags.end_addr = module_tags.start_addr + MODULE_ALLOC_TAG_VMAP_SIZE;
638 /* Ensure the base is alloc_tag aligned when required for indexing */
639 module_tags.start_addr = alloc_tag_align(module_tags.start_addr);
640
641 return 0;
642 }
643
free_mod_tags_mem(void)644 static void __init free_mod_tags_mem(void)
645 {
646 int i;
647
648 module_tags.start_addr = 0;
649 for (i = 0; i < vm_module_tags->nr_pages; i++)
650 __free_page(vm_module_tags->pages[i]);
651 kfree(vm_module_tags->pages);
652 free_vm_area(vm_module_tags);
653 }
654
655 #else /* CONFIG_MODULES */
656
alloc_mod_tags_mem(void)657 static inline int alloc_mod_tags_mem(void) { return 0; }
free_mod_tags_mem(void)658 static inline void free_mod_tags_mem(void) {}
659
660 #endif /* CONFIG_MODULES */
661
662 /* See: Documentation/mm/allocation-profiling.rst */
setup_early_mem_profiling(char * str)663 static int __init setup_early_mem_profiling(char *str)
664 {
665 bool compressed = false;
666 bool enable;
667
668 if (!str || !str[0])
669 return -EINVAL;
670
671 if (!strncmp(str, "never", 5)) {
672 enable = false;
673 mem_profiling_support = false;
674 pr_info("Memory allocation profiling is disabled!\n");
675 } else {
676 char *token = strsep(&str, ",");
677
678 if (kstrtobool(token, &enable))
679 return -EINVAL;
680
681 if (str) {
682
683 if (strcmp(str, "compressed"))
684 return -EINVAL;
685
686 compressed = true;
687 }
688 mem_profiling_support = true;
689 pr_info("Memory allocation profiling is enabled %s compression and is turned %s!\n",
690 compressed ? "with" : "without", enable ? "on" : "off");
691 }
692
693 if (enable != mem_alloc_profiling_enabled()) {
694 if (enable)
695 static_branch_enable(&mem_alloc_profiling_key);
696 else
697 static_branch_disable(&mem_alloc_profiling_key);
698 }
699 if (compressed != static_key_enabled(&mem_profiling_compressed)) {
700 if (compressed)
701 static_branch_enable(&mem_profiling_compressed);
702 else
703 static_branch_disable(&mem_profiling_compressed);
704 }
705
706 return 0;
707 }
708 early_param("sysctl.vm.mem_profiling", setup_early_mem_profiling);
709
need_page_alloc_tagging(void)710 static __init bool need_page_alloc_tagging(void)
711 {
712 if (static_key_enabled(&mem_profiling_compressed))
713 return false;
714
715 return mem_profiling_support;
716 }
717
init_page_alloc_tagging(void)718 static __init void init_page_alloc_tagging(void)
719 {
720 }
721
722 struct page_ext_operations page_alloc_tagging_ops = {
723 .size = sizeof(union codetag_ref),
724 .need = need_page_alloc_tagging,
725 .init = init_page_alloc_tagging,
726 };
727 EXPORT_SYMBOL(page_alloc_tagging_ops);
728
729 #ifdef CONFIG_SYSCTL
730 static struct ctl_table memory_allocation_profiling_sysctls[] = {
731 {
732 .procname = "mem_profiling",
733 .data = &mem_alloc_profiling_key,
734 #ifdef CONFIG_MEM_ALLOC_PROFILING_DEBUG
735 .mode = 0444,
736 #else
737 .mode = 0644,
738 #endif
739 .proc_handler = proc_do_static_key,
740 },
741 };
742
sysctl_init(void)743 static void __init sysctl_init(void)
744 {
745 if (!mem_profiling_support)
746 memory_allocation_profiling_sysctls[0].mode = 0444;
747
748 register_sysctl_init("vm", memory_allocation_profiling_sysctls);
749 }
750 #else /* CONFIG_SYSCTL */
sysctl_init(void)751 static inline void sysctl_init(void) {}
752 #endif /* CONFIG_SYSCTL */
753
alloc_tag_init(void)754 static int __init alloc_tag_init(void)
755 {
756 const struct codetag_type_desc desc = {
757 .section = ALLOC_TAG_SECTION_NAME,
758 .tag_size = sizeof(struct alloc_tag),
759 #ifdef CONFIG_MODULES
760 .needs_section_mem = needs_section_mem,
761 .alloc_section_mem = reserve_module_tags,
762 .free_section_mem = release_module_tags,
763 .module_replaced = replace_module,
764 #endif
765 };
766 int res;
767
768 res = alloc_mod_tags_mem();
769 if (res)
770 return res;
771
772 alloc_tag_cttype = codetag_register_type(&desc);
773 if (IS_ERR(alloc_tag_cttype)) {
774 free_mod_tags_mem();
775 return PTR_ERR(alloc_tag_cttype);
776 }
777
778 sysctl_init();
779 procfs_init();
780
781 return 0;
782 }
783 module_init(alloc_tag_init);
784