[RFC v1 01/16] perf kwork: Fix incorrect and missing free atom in work_push_atom()

Yang Jihong posted 16 patches 2 years, 4 months ago
[RFC v1 01/16] perf kwork: Fix incorrect and missing free atom in work_push_atom()
Posted by Yang Jihong 2 years, 4 months ago
1. Atoms are managed in page mode and should be released using atom_free()
   instead of free().
2. When the event does not match, the atom needs to free.

Fixes: f98919ec4fcc ("perf kwork: Implement 'report' subcommand")
Signed-off-by: Yang Jihong <yangjihong1@huawei.com>
---
 tools/perf/builtin-kwork.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/tools/perf/builtin-kwork.c b/tools/perf/builtin-kwork.c
index 14bf7a8429e7..73b5dc099a8a 100644
--- a/tools/perf/builtin-kwork.c
+++ b/tools/perf/builtin-kwork.c
@@ -406,12 +406,14 @@ static int work_push_atom(struct perf_kwork *kwork,
 
 	work = work_findnew(&class->work_root, &key, &kwork->cmp_id);
 	if (work == NULL) {
-		free(atom);
+		atom_free(atom);
 		return -1;
 	}
 
-	if (!profile_event_match(kwork, work, sample))
+	if (!profile_event_match(kwork, work, sample)) {
+		atom_free(atom);
 		return 0;
+	}
 
 	if (dst_type < KWORK_TRACE_MAX) {
 		dst_atom = list_last_entry_or_null(&work->atom_list[dst_type],
-- 
2.30.GIT
Re: [RFC v1 01/16] perf kwork: Fix incorrect and missing free atom in work_push_atom()
Posted by Ian Rogers 2 years, 3 months ago
On Sat, Aug 12, 2023 at 1:52 AM Yang Jihong <yangjihong1@huawei.com> wrote:
>
> 1. Atoms are managed in page mode and should be released using atom_free()
>    instead of free().
> 2. When the event does not match, the atom needs to free.
>
> Fixes: f98919ec4fcc ("perf kwork: Implement 'report' subcommand")
> Signed-off-by: Yang Jihong <yangjihong1@huawei.com>

Reviewed-by: Ian Rogers <irogers@google.com>

> ---
>  tools/perf/builtin-kwork.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/tools/perf/builtin-kwork.c b/tools/perf/builtin-kwork.c
> index 14bf7a8429e7..73b5dc099a8a 100644
> --- a/tools/perf/builtin-kwork.c
> +++ b/tools/perf/builtin-kwork.c
> @@ -406,12 +406,14 @@ static int work_push_atom(struct perf_kwork *kwork,
>
>         work = work_findnew(&class->work_root, &key, &kwork->cmp_id);
>         if (work == NULL) {
> -               free(atom);
> +               atom_free(atom);

Presumably this is fixing a memory-leak. It would be nice if kwork had
perf tests, then our builds with -fsanitize=address would highlight
this kind of issue. Here is a build command I use for this:
make -C tools/perf O=/tmp/perf DEBUG=1 EXTRA_CFLAGS="-O0 -g
-fno-omit-frame-pointer -fsanitize=address" BUILD_BPF_SKEL=1
NO_LIBTRACEEVENT=1

Thanks,
Ian

>                 return -1;
>         }
>
> -       if (!profile_event_match(kwork, work, sample))
> +       if (!profile_event_match(kwork, work, sample)) {
> +               atom_free(atom);
>                 return 0;
> +       }
>
>         if (dst_type < KWORK_TRACE_MAX) {
>                 dst_atom = list_last_entry_or_null(&work->atom_list[dst_type],
> --
> 2.30.GIT
>
Re: [RFC v1 01/16] perf kwork: Fix incorrect and missing free atom in work_push_atom()
Posted by Arnaldo Carvalho de Melo 2 years, 3 months ago
Em Sun, Sep 03, 2023 at 09:05:06PM -0700, Ian Rogers escreveu:
> On Sat, Aug 12, 2023 at 1:52 AM Yang Jihong <yangjihong1@huawei.com> wrote:
> >
> > 1. Atoms are managed in page mode and should be released using atom_free()
> >    instead of free().
> > 2. When the event does not match, the atom needs to free.
> >
> > Fixes: f98919ec4fcc ("perf kwork: Implement 'report' subcommand")
> > Signed-off-by: Yang Jihong <yangjihong1@huawei.com>
> 
> Reviewed-by: Ian Rogers <irogers@google.com>

Thanks, applied locally, will perform some tests and later today
probably push publicly, great work!

- Arnaldo

 
> > ---
> >  tools/perf/builtin-kwork.c | 6 ++++--
> >  1 file changed, 4 insertions(+), 2 deletions(-)
> >
> > diff --git a/tools/perf/builtin-kwork.c b/tools/perf/builtin-kwork.c
> > index 14bf7a8429e7..73b5dc099a8a 100644
> > --- a/tools/perf/builtin-kwork.c
> > +++ b/tools/perf/builtin-kwork.c
> > @@ -406,12 +406,14 @@ static int work_push_atom(struct perf_kwork *kwork,
> >
> >         work = work_findnew(&class->work_root, &key, &kwork->cmp_id);
> >         if (work == NULL) {
> > -               free(atom);
> > +               atom_free(atom);
> 
> Presumably this is fixing a memory-leak. It would be nice if kwork had
> perf tests, then our builds with -fsanitize=address would highlight
> this kind of issue. Here is a build command I use for this:
> make -C tools/perf O=/tmp/perf DEBUG=1 EXTRA_CFLAGS="-O0 -g
> -fno-omit-frame-pointer -fsanitize=address" BUILD_BPF_SKEL=1
> NO_LIBTRACEEVENT=1
> 
> Thanks,
> Ian
> 
> >                 return -1;
> >         }
> >
> > -       if (!profile_event_match(kwork, work, sample))
> > +       if (!profile_event_match(kwork, work, sample)) {
> > +               atom_free(atom);
> >                 return 0;
> > +       }
> >
> >         if (dst_type < KWORK_TRACE_MAX) {
> >                 dst_atom = list_last_entry_or_null(&work->atom_list[dst_type],
> > --
> > 2.30.GIT
> >

-- 

- Arnaldo
Re: [RFC v1 01/16] perf kwork: Fix incorrect and missing free atom in work_push_atom()
Posted by Yang Jihong 2 years, 3 months ago
Hello,

On 2023/9/4 12:05, Ian Rogers wrote:
> On Sat, Aug 12, 2023 at 1:52 AM Yang Jihong <yangjihong1@huawei.com> wrote:
>>
>> 1. Atoms are managed in page mode and should be released using atom_free()
>>     instead of free().
>> 2. When the event does not match, the atom needs to free.
>>
>> Fixes: f98919ec4fcc ("perf kwork: Implement 'report' subcommand")
>> Signed-off-by: Yang Jihong <yangjihong1@huawei.com>
> 
> Reviewed-by: Ian Rogers <irogers@google.com>
> 
Thanks for review-by tag.
>> ---
>>   tools/perf/builtin-kwork.c | 6 ++++--
>>   1 file changed, 4 insertions(+), 2 deletions(-)
>>
>> diff --git a/tools/perf/builtin-kwork.c b/tools/perf/builtin-kwork.c
>> index 14bf7a8429e7..73b5dc099a8a 100644
>> --- a/tools/perf/builtin-kwork.c
>> +++ b/tools/perf/builtin-kwork.c
>> @@ -406,12 +406,14 @@ static int work_push_atom(struct perf_kwork *kwork,
>>
>>          work = work_findnew(&class->work_root, &key, &kwork->cmp_id);
>>          if (work == NULL) {
>> -               free(atom);
>> +               atom_free(atom);
> 
> Presumably this is fixing a memory-leak. It would be nice if kwork had
> perf tests, then our builds with -fsanitize=address would highlight
> this kind of issue. Here is a build command I use for this:
> make -C tools/perf O=/tmp/perf DEBUG=1 EXTRA_CFLAGS="-O0 -g
> -fno-omit-frame-pointer -fsanitize=address" BUILD_BPF_SKEL=1
> NO_LIBTRACEEVENT=1
> 
Okay, I'm going to add the kwork test in perf tests.

Thanks,
Yang