diff options
author | Kent Overstreet <kent.overstreet@linux.dev> | 2023-03-13 15:40:16 -0400 |
---|---|---|
committer | Kent Overstreet <kent.overstreet@linux.dev> | 2023-03-19 19:55:03 -0400 |
commit | 8178f49de019e05c1b17fdfae846d06dc559031e (patch) | |
tree | ad97f14042d9fb35042f02f0f29920d8304cd2fa | |
parent | b1381646c50c379f0a380967d66d90eb79078417 (diff) |
fixup! lib: add allocation tagging support for memory allocation profilingcodetags_v1
- fix the warning on module unload
- add a show_mem() hook
Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev>
-rw-r--r-- | include/linux/alloc_tag.h | 2 | ||||
-rw-r--r-- | lib/alloc_tag.c | 50 | ||||
-rw-r--r-- | lib/show_mem.c | 13 |
3 files changed, 59 insertions, 6 deletions
diff --git a/include/linux/alloc_tag.h b/include/linux/alloc_tag.h index d7551b0b386c..27badfa7993b 100644 --- a/include/linux/alloc_tag.h +++ b/include/linux/alloc_tag.h @@ -24,6 +24,8 @@ struct alloc_tag { #ifdef CONFIG_MEM_ALLOC_PROFILING +void alloc_tags_show_mem_report(struct seq_buf *s); + static inline struct alloc_tag *ctc_to_alloc_tag(struct codetag_with_ctx *ctc) { return container_of(ctc, struct alloc_tag, ctc); diff --git a/lib/alloc_tag.c b/lib/alloc_tag.c index adf8b5da18db..b40684e12416 100644 --- a/lib/alloc_tag.c +++ b/lib/alloc_tag.c @@ -13,6 +13,8 @@ #define STACK_BUF_SIZE 1024 +static struct codetag_type *alloc_tag_cttype; + DEFINE_STATIC_KEY_TRUE(mem_alloc_profiling_key); /* @@ -133,6 +135,43 @@ static ssize_t allocations_file_read(struct file *file, char __user *ubuf, return err ? : buf.ret; } +void alloc_tags_show_mem_report(struct seq_buf *s) +{ + struct codetag_iterator iter; + struct codetag *ct; + struct { + struct codetag *tag; + size_t bytes; + } tags[10], n; + unsigned i, nr = 0; + + codetag_init_iter(&iter, alloc_tag_cttype); + + codetag_lock_module_list(alloc_tag_cttype, true); + while ((ct = codetag_next_ct(&iter))) { + n.tag = ct; + n.bytes = lazy_percpu_counter_read(&ct_to_alloc_tag(ct)->bytes_allocated); + + for (i = 0; i < nr; i++) + if (n.bytes > tags[i].bytes) + break; + + if (i < ARRAY_SIZE(tags)) { + nr -= nr == ARRAY_SIZE(tags); + memmove(&tags[i + 1], + &tags[i], + sizeof(tags[0]) * (nr - i)); + nr++; + tags[i] = n; + } + } + + for (i = 0; i < nr; i++) + alloc_tag_to_text(s, tags[i].tag); + + codetag_lock_module_list(alloc_tag_cttype, false); +} + static const struct file_operations allocations_file_ops = { .owner = THIS_MODULE, .open = allocations_file_open, @@ -376,7 +415,7 @@ static void alloc_tag_module_unload(struct codetag_type *cttype, struct codetag_ size_t bytes = lazy_percpu_counter_read(&tag->bytes_allocated); if (!WARN(bytes, "%s:%u module %s func:%s has %zu allocated at module unload", - ct->filename, ct->lineno, ct->modname, ct->function)) + ct->filename, ct->lineno, ct->modname, ct->function, bytes)) lazy_percpu_counter_exit(&tag->bytes_allocated); } } @@ -399,7 +438,6 @@ EXPORT_SYMBOL(page_alloc_tagging_ops); static int __init alloc_tag_init(void) { - struct codetag_type *cttype; const struct codetag_type_desc desc = { .section = "alloc_tags", .tag_size = sizeof(struct alloc_tag), @@ -407,10 +445,10 @@ static int __init alloc_tag_init(void) .free_ctx = alloc_tag_ops_free_ctx, }; - cttype = codetag_register_type(&desc); - if (IS_ERR_OR_NULL(cttype)) - return PTR_ERR(cttype); + alloc_tag_cttype = codetag_register_type(&desc); + if (IS_ERR_OR_NULL(alloc_tag_cttype)) + return PTR_ERR(alloc_tag_cttype); - return dbgfs_init(cttype); + return dbgfs_init(alloc_tag_cttype); } module_init(alloc_tag_init); diff --git a/lib/show_mem.c b/lib/show_mem.c index 0d7585cde2a6..382dc57db82a 100644 --- a/lib/show_mem.c +++ b/lib/show_mem.c @@ -7,6 +7,7 @@ #include <linux/mm.h> #include <linux/cma.h> +#include <linux/seq_buf.h> void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) { @@ -41,4 +42,16 @@ void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx) #ifdef CONFIG_MEMORY_FAILURE printk("%lu pages hwpoisoned\n", atomic_long_read(&num_poisoned_pages)); #endif + if (IS_ENABLED(CONFIG_MEM_ALLOC_PROFILING)) { + struct seq_buf s; + char *buf = kmalloc(4096, GFP_ATOMIC); + + if (buf) { + printk("Memory allocations:\n"); + seq_buf_init(&s, buf, 4096); + alloc_tags_show_mem_report(&s); + printk("%s", buf); + kfree(buf); + } + } } |