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