Skip to content

Commit 1438d34

Browse files
surenbaghdasaryanakpm00
authored andcommitted
lib: add memory allocations report in show_mem()
Include allocations in show_mem reports. Link: https://lkml.kernel.org/r/20240321163705.3067592-33-surenb@google.com Signed-off-by: Kent Overstreet <kent.overstreet@linux.dev> Signed-off-by: Suren Baghdasaryan <surenb@google.com> Reviewed-by: Vlastimil Babka <vbabka@suse.cz> Tested-by: Kees Cook <keescook@chromium.org> Cc: Alexander Viro <viro@zeniv.linux.org.uk> Cc: Alex Gaynor <alex.gaynor@gmail.com> Cc: Alice Ryhl <aliceryhl@google.com> Cc: Andreas Hindborg <a.hindborg@samsung.com> Cc: Benno Lossin <benno.lossin@proton.me> Cc: "Björn Roy Baron" <bjorn3_gh@protonmail.com> Cc: Boqun Feng <boqun.feng@gmail.com> Cc: Christoph Lameter <cl@linux.com> Cc: Dennis Zhou <dennis@kernel.org> Cc: Gary Guo <gary@garyguo.net> Cc: Miguel Ojeda <ojeda@kernel.org> Cc: Pasha Tatashin <pasha.tatashin@soleen.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Tejun Heo <tj@kernel.org> Cc: Wedson Almeida Filho <wedsonaf@gmail.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 9e54dd8 commit 1438d34

File tree

5 files changed

+77
-0
lines changed

5 files changed

+77
-0
lines changed

include/linux/alloc_tag.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ struct alloc_tag {
3131

3232
#ifdef CONFIG_MEM_ALLOC_PROFILING
3333

34+
struct codetag_bytes {
35+
struct codetag *ct;
36+
s64 bytes;
37+
};
38+
39+
size_t alloc_tag_top_users(struct codetag_bytes *tags, size_t count, bool can_sleep);
40+
3441
static inline struct alloc_tag *ct_to_alloc_tag(struct codetag *ct)
3542
{
3643
return container_of(ct, struct alloc_tag, ct);

include/linux/codetag.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ struct codetag_iterator {
6161
}
6262

6363
void codetag_lock_module_list(struct codetag_type *cttype, bool lock);
64+
bool codetag_trylock_module_list(struct codetag_type *cttype);
6465
struct codetag_iterator codetag_get_ct_iter(struct codetag_type *cttype);
6566
struct codetag *codetag_next_ct(struct codetag_iterator *iter);
6667

lib/alloc_tag.c

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,44 @@ static const struct seq_operations allocinfo_seq_op = {
8989
.show = allocinfo_show,
9090
};
9191

92+
size_t alloc_tag_top_users(struct codetag_bytes *tags, size_t count, bool can_sleep)
93+
{
94+
struct codetag_iterator iter;
95+
struct codetag *ct;
96+
struct codetag_bytes n;
97+
unsigned int i, nr = 0;
98+
99+
if (can_sleep)
100+
codetag_lock_module_list(alloc_tag_cttype, true);
101+
else if (!codetag_trylock_module_list(alloc_tag_cttype))
102+
return 0;
103+
104+
iter = codetag_get_ct_iter(alloc_tag_cttype);
105+
while ((ct = codetag_next_ct(&iter))) {
106+
struct alloc_tag_counters counter = alloc_tag_read(ct_to_alloc_tag(ct));
107+
108+
n.ct = ct;
109+
n.bytes = counter.bytes;
110+
111+
for (i = 0; i < nr; i++)
112+
if (n.bytes > tags[i].bytes)
113+
break;
114+
115+
if (i < count) {
116+
nr -= nr == count;
117+
memmove(&tags[i + 1],
118+
&tags[i],
119+
sizeof(tags[0]) * (nr - i));
120+
nr++;
121+
tags[i] = n;
122+
}
123+
}
124+
125+
codetag_lock_module_list(alloc_tag_cttype, false);
126+
127+
return nr;
128+
}
129+
92130
static void __init procfs_init(void)
93131
{
94132
proc_create_seq("allocinfo", 0444, NULL, &allocinfo_seq_op);

lib/codetag.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ void codetag_lock_module_list(struct codetag_type *cttype, bool lock)
3636
up_read(&cttype->mod_lock);
3737
}
3838

39+
bool codetag_trylock_module_list(struct codetag_type *cttype)
40+
{
41+
return down_read_trylock(&cttype->mod_lock) != 0;
42+
}
43+
3944
struct codetag_iterator codetag_get_ct_iter(struct codetag_type *cttype)
4045
{
4146
struct codetag_iterator iter = {

mm/show_mem.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,4 +423,30 @@ void __show_mem(unsigned int filter, nodemask_t *nodemask, int max_zone_idx)
423423
#ifdef CONFIG_MEMORY_FAILURE
424424
printk("%lu pages hwpoisoned\n", atomic_long_read(&num_poisoned_pages));
425425
#endif
426+
#ifdef CONFIG_MEM_ALLOC_PROFILING
427+
{
428+
struct codetag_bytes tags[10];
429+
size_t i, nr;
430+
431+
nr = alloc_tag_top_users(tags, ARRAY_SIZE(tags), false);
432+
if (nr) {
433+
pr_notice("Memory allocations:\n");
434+
for (i = 0; i < nr; i++) {
435+
struct codetag *ct = tags[i].ct;
436+
struct alloc_tag *tag = ct_to_alloc_tag(ct);
437+
struct alloc_tag_counters counter = alloc_tag_read(tag);
438+
439+
/* Same as alloc_tag_to_text() but w/o intermediate buffer */
440+
if (ct->modname)
441+
pr_notice("%12lli %8llu %s:%u [%s] func:%s\n",
442+
counter.bytes, counter.calls, ct->filename,
443+
ct->lineno, ct->modname, ct->function);
444+
else
445+
pr_notice("%12lli %8llu %s:%u func:%s\n",
446+
counter.bytes, counter.calls, ct->filename,
447+
ct->lineno, ct->function);
448+
}
449+
}
450+
}
451+
#endif
426452
}

0 commit comments

Comments
 (0)