2022-04-05 01:46:54

by Vlastimil Babka

[permalink] [raw]
Subject: [PATCH v3 4/6] mm/slub: distinguish and print stack traces in debugfs files

From: Oliver Glitta <[email protected]>

Aggregate objects in slub cache by unique stack trace in addition to
caller address when producing contents of debugfs files alloc_traces and
free_traces in debugfs. Also add the stack traces to the debugfs output.
This makes it much more useful to e.g. debug memory leaks.

Signed-off-by: Oliver Glitta <[email protected]>
Signed-off-by: Vlastimil Babka <[email protected]>
Reviewed-and-tested-by: Hyeonggon Yoo <[email protected]>
---
mm/slub.c | 28 ++++++++++++++++++++++++++--
1 file changed, 26 insertions(+), 2 deletions(-)

diff --git a/mm/slub.c b/mm/slub.c
index 98c1450c23f0..f2e550e1adf0 100644
--- a/mm/slub.c
+++ b/mm/slub.c
@@ -5064,6 +5064,7 @@ EXPORT_SYMBOL(validate_slab_cache);
*/

struct location {
+ depot_stack_handle_t handle;
unsigned long count;
unsigned long addr;
long long sum_time;
@@ -5116,9 +5117,13 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
{
long start, end, pos;
struct location *l;
- unsigned long caddr;
+ unsigned long caddr, chandle;
unsigned long age = jiffies - track->when;
+ depot_stack_handle_t handle = 0;

+#ifdef CONFIG_STACKDEPOT
+ handle = READ_ONCE(track->handle);
+#endif
start = -1;
end = t->count;

@@ -5133,7 +5138,8 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
break;

caddr = t->loc[pos].addr;
- if (track->addr == caddr) {
+ chandle = t->loc[pos].handle;
+ if ((track->addr == caddr) && (handle == chandle)) {

l = &t->loc[pos];
l->count++;
@@ -5158,6 +5164,8 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,

if (track->addr < caddr)
end = pos;
+ else if (track->addr == caddr && handle < chandle)
+ end = pos;
else
start = pos;
}
@@ -5180,6 +5188,7 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
l->max_time = age;
l->min_pid = track->pid;
l->max_pid = track->pid;
+ l->handle = handle;
cpumask_clear(to_cpumask(l->cpus));
cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
nodes_clear(l->nodes);
@@ -6089,6 +6098,21 @@ static int slab_debugfs_show(struct seq_file *seq, void *v)
seq_printf(seq, " nodes=%*pbl",
nodemask_pr_args(&l->nodes));

+#ifdef CONFIG_STACKDEPOT
+ {
+ depot_stack_handle_t handle;
+ unsigned long *entries;
+ unsigned int nr_entries, j;
+
+ handle = READ_ONCE(l->handle);
+ if (handle) {
+ nr_entries = stack_depot_fetch(handle, &entries);
+ seq_puts(seq, "\n");
+ for (j = 0; j < nr_entries; j++)
+ seq_printf(seq, " %pS\n", (void *)entries[j]);
+ }
+ }
+#endif
seq_puts(seq, "\n");
}

--
2.35.1


2022-04-06 13:43:57

by David Rientjes

[permalink] [raw]
Subject: Re: [PATCH v3 4/6] mm/slub: distinguish and print stack traces in debugfs files

On Mon, 4 Apr 2022, Vlastimil Babka wrote:

> From: Oliver Glitta <[email protected]>
>
> Aggregate objects in slub cache by unique stack trace in addition to
> caller address when producing contents of debugfs files alloc_traces and
> free_traces in debugfs. Also add the stack traces to the debugfs output.
> This makes it much more useful to e.g. debug memory leaks.
>
> Signed-off-by: Oliver Glitta <[email protected]>
> Signed-off-by: Vlastimil Babka <[email protected]>
> Reviewed-and-tested-by: Hyeonggon Yoo <[email protected]>
> ---
> mm/slub.c | 28 ++++++++++++++++++++++++++--
> 1 file changed, 26 insertions(+), 2 deletions(-)
>
> diff --git a/mm/slub.c b/mm/slub.c
> index 98c1450c23f0..f2e550e1adf0 100644
> --- a/mm/slub.c
> +++ b/mm/slub.c
> @@ -5064,6 +5064,7 @@ EXPORT_SYMBOL(validate_slab_cache);
> */
>
> struct location {
> + depot_stack_handle_t handle;
> unsigned long count;
> unsigned long addr;
> long long sum_time;
> @@ -5116,9 +5117,13 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
> {
> long start, end, pos;
> struct location *l;
> - unsigned long caddr;
> + unsigned long caddr, chandle;
> unsigned long age = jiffies - track->when;
> + depot_stack_handle_t handle = 0;
>
> +#ifdef CONFIG_STACKDEPOT
> + handle = READ_ONCE(track->handle);
> +#endif
> start = -1;
> end = t->count;
>
> @@ -5133,7 +5138,8 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
> break;
>
> caddr = t->loc[pos].addr;
> - if (track->addr == caddr) {
> + chandle = t->loc[pos].handle;
> + if ((track->addr == caddr) && (handle == chandle)) {
>
> l = &t->loc[pos];
> l->count++;
> @@ -5158,6 +5164,8 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
>
> if (track->addr < caddr)
> end = pos;
> + else if (track->addr == caddr && handle < chandle)
> + end = pos;
> else
> start = pos;
> }

Does this need to properly handle the case where handle == NULL?

> @@ -5180,6 +5188,7 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
> l->max_time = age;
> l->min_pid = track->pid;
> l->max_pid = track->pid;
> + l->handle = handle;
> cpumask_clear(to_cpumask(l->cpus));
> cpumask_set_cpu(track->cpu, to_cpumask(l->cpus));
> nodes_clear(l->nodes);
> @@ -6089,6 +6098,21 @@ static int slab_debugfs_show(struct seq_file *seq, void *v)
> seq_printf(seq, " nodes=%*pbl",
> nodemask_pr_args(&l->nodes));
>
> +#ifdef CONFIG_STACKDEPOT
> + {
> + depot_stack_handle_t handle;
> + unsigned long *entries;
> + unsigned int nr_entries, j;
> +
> + handle = READ_ONCE(l->handle);
> + if (handle) {
> + nr_entries = stack_depot_fetch(handle, &entries);
> + seq_puts(seq, "\n");
> + for (j = 0; j < nr_entries; j++)
> + seq_printf(seq, " %pS\n", (void *)entries[j]);
> + }
> + }
> +#endif
> seq_puts(seq, "\n");
> }
>
> --
> 2.35.1
>
>

2022-04-06 15:40:36

by Vlastimil Babka

[permalink] [raw]
Subject: Re: [PATCH v3 4/6] mm/slub: distinguish and print stack traces in debugfs files

On 4/5/22 23:40, David Rientjes wrote:
>> @@ -5116,9 +5117,13 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
>> {
>> long start, end, pos;
>> struct location *l;
>> - unsigned long caddr;
>> + unsigned long caddr, chandle;
>> unsigned long age = jiffies - track->when;
>> + depot_stack_handle_t handle = 0;
>>
>> +#ifdef CONFIG_STACKDEPOT
>> + handle = READ_ONCE(track->handle);
>> +#endif
>> start = -1;
>> end = t->count;
>>
>> @@ -5133,7 +5138,8 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
>> break;
>>
>> caddr = t->loc[pos].addr;
>> - if (track->addr == caddr) {
>> + chandle = t->loc[pos].handle;
>> + if ((track->addr == caddr) && (handle == chandle)) {
>>
>> l = &t->loc[pos];
>> l->count++;
>> @@ -5158,6 +5164,8 @@ static int add_location(struct loc_track *t, struct kmem_cache *s,
>>
>> if (track->addr < caddr)
>> end = pos;
>> + else if (track->addr == caddr && handle < chandle)
>> + end = pos;
>> else
>> start = pos;
>> }
>
> Does this need to properly handle the case where handle == NULL?

Hm I can't think of how much more properly is possible. If objects have same
track->addr (which is the immediate caller) and also same NULL handle, they
will be counted together. I think it's the best we can do?