[PATCH v1] perf tool x86: Fix perf_env memory leak

Ian Rogers posted 1 patch 2 years, 7 months ago
tools/perf/arch/x86/util/env.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH v1] perf tool x86: Fix perf_env memory leak
Posted by Ian Rogers 2 years, 7 months ago
Found by leak sanitizer:
```
==1632594==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 21 byte(s) in 1 object(s) allocated from:
    #0 0x7f2953a7077b in __interceptor_strdup ../../../../src/libsanitizer/asan/asan_interceptors.cpp:439
    #1 0x556701d6fbbf in perf_env__read_cpuid util/env.c:369
    #2 0x556701d70589 in perf_env__cpuid util/env.c:465
    #3 0x55670204bba2 in x86__is_amd_cpu arch/x86/util/env.c:14
    #4 0x5567020487a2 in arch__post_evsel_config arch/x86/util/evsel.c:83
    #5 0x556701d8f78b in evsel__config util/evsel.c:1366
    #6 0x556701ef5872 in evlist__config util/record.c:108
    #7 0x556701cd6bcd in test__PERF_RECORD tests/perf-record.c:112
    #8 0x556701cacd07 in run_test tests/builtin-test.c:236
    #9 0x556701cacfac in test_and_print tests/builtin-test.c:265
    #10 0x556701cadddb in __cmd_test tests/builtin-test.c:402
    #11 0x556701caf2aa in cmd_test tests/builtin-test.c:559
    #12 0x556701d3b557 in run_builtin tools/perf/perf.c:323
    #13 0x556701d3bac8 in handle_internal_command tools/perf/perf.c:377
    #14 0x556701d3be90 in run_argv tools/perf/perf.c:421
    #15 0x556701d3c3f8 in main tools/perf/perf.c:537
    #16 0x7f2952a46189 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58

SUMMARY: AddressSanitizer: 21 byte(s) leaked in 1 allocation(s).
```

Fixes: daea405f5f06 ("perf tool x86: Consolidate is_amd check into single function")
Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/perf/arch/x86/util/env.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/perf/arch/x86/util/env.c b/tools/perf/arch/x86/util/env.c
index 33b87f8ac1cc..3e537ffb1353 100644
--- a/tools/perf/arch/x86/util/env.c
+++ b/tools/perf/arch/x86/util/env.c
@@ -13,7 +13,7 @@ bool x86__is_amd_cpu(void)
 
 	perf_env__cpuid(&env);
 	is_amd = env.cpuid && strstarts(env.cpuid, "AuthenticAMD") ? 1 : -1;
-
+	perf_env__exit(&env);
 ret:
 	return is_amd >= 1 ? true : false;
 }
-- 
2.41.0.162.gfafddb0af9-goog
Re: [PATCH v1] perf tool x86: Fix perf_env memory leak
Posted by Arnaldo Carvalho de Melo 2 years, 7 months ago
Em Tue, Jun 13, 2023 at 04:54:16PM -0700, Ian Rogers escreveu:
> Found by leak sanitizer:
> ```
> ==1632594==ERROR: LeakSanitizer: detected memory leaks
> 
> Direct leak of 21 byte(s) in 1 object(s) allocated from:
>     #0 0x7f2953a7077b in __interceptor_strdup ../../../../src/libsanitizer/asan/asan_interceptors.cpp:439
>     #1 0x556701d6fbbf in perf_env__read_cpuid util/env.c:369
>     #2 0x556701d70589 in perf_env__cpuid util/env.c:465
>     #3 0x55670204bba2 in x86__is_amd_cpu arch/x86/util/env.c:14
>     #4 0x5567020487a2 in arch__post_evsel_config arch/x86/util/evsel.c:83
>     #5 0x556701d8f78b in evsel__config util/evsel.c:1366
>     #6 0x556701ef5872 in evlist__config util/record.c:108
>     #7 0x556701cd6bcd in test__PERF_RECORD tests/perf-record.c:112
>     #8 0x556701cacd07 in run_test tests/builtin-test.c:236
>     #9 0x556701cacfac in test_and_print tests/builtin-test.c:265
>     #10 0x556701cadddb in __cmd_test tests/builtin-test.c:402
>     #11 0x556701caf2aa in cmd_test tests/builtin-test.c:559
>     #12 0x556701d3b557 in run_builtin tools/perf/perf.c:323
>     #13 0x556701d3bac8 in handle_internal_command tools/perf/perf.c:377
>     #14 0x556701d3be90 in run_argv tools/perf/perf.c:421
>     #15 0x556701d3c3f8 in main tools/perf/perf.c:537
>     #16 0x7f2952a46189 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
> 
> SUMMARY: AddressSanitizer: 21 byte(s) leaked in 1 allocation(s).
> ```
> 
> Fixes: daea405f5f06 ("perf tool x86: Consolidate is_amd check into single function")

Its a fix, yes, but it is not for this daea405f5f06 patch, he just moved the
perf_mem_is_amd_cpu(), that was leaky, to a different file, the fixes is
for  for:

f7b58cbdb3ff36eb ("perf mem/c2c: Add load store event mappings for
AMD").

Right?

I'm fixing the fixes tag and applying the patch, thanks.

- Arnaldo

> Signed-off-by: Ian Rogers <irogers@google.com>
> ---
>  tools/perf/arch/x86/util/env.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/tools/perf/arch/x86/util/env.c b/tools/perf/arch/x86/util/env.c
> index 33b87f8ac1cc..3e537ffb1353 100644
> --- a/tools/perf/arch/x86/util/env.c
> +++ b/tools/perf/arch/x86/util/env.c
> @@ -13,7 +13,7 @@ bool x86__is_amd_cpu(void)
>  
>  	perf_env__cpuid(&env);
>  	is_amd = env.cpuid && strstarts(env.cpuid, "AuthenticAMD") ? 1 : -1;
> -
> +	perf_env__exit(&env);
>  ret:
>  	return is_amd >= 1 ? true : false;
>  }
> -- 
> 2.41.0.162.gfafddb0af9-goog
> 

-- 

- Arnaldo
Re: [PATCH v1] perf tool x86: Fix perf_env memory leak
Posted by Ravi Bangoria 2 years, 7 months ago
On 14-Jun-23 6:36 AM, Arnaldo Carvalho de Melo wrote:
> Em Tue, Jun 13, 2023 at 04:54:16PM -0700, Ian Rogers escreveu:
>> Found by leak sanitizer:
>> ```
>> ==1632594==ERROR: LeakSanitizer: detected memory leaks
>>
>> Direct leak of 21 byte(s) in 1 object(s) allocated from:
>>     #0 0x7f2953a7077b in __interceptor_strdup ../../../../src/libsanitizer/asan/asan_interceptors.cpp:439
>>     #1 0x556701d6fbbf in perf_env__read_cpuid util/env.c:369
>>     #2 0x556701d70589 in perf_env__cpuid util/env.c:465
>>     #3 0x55670204bba2 in x86__is_amd_cpu arch/x86/util/env.c:14
>>     #4 0x5567020487a2 in arch__post_evsel_config arch/x86/util/evsel.c:83
>>     #5 0x556701d8f78b in evsel__config util/evsel.c:1366
>>     #6 0x556701ef5872 in evlist__config util/record.c:108
>>     #7 0x556701cd6bcd in test__PERF_RECORD tests/perf-record.c:112
>>     #8 0x556701cacd07 in run_test tests/builtin-test.c:236
>>     #9 0x556701cacfac in test_and_print tests/builtin-test.c:265
>>     #10 0x556701cadddb in __cmd_test tests/builtin-test.c:402
>>     #11 0x556701caf2aa in cmd_test tests/builtin-test.c:559
>>     #12 0x556701d3b557 in run_builtin tools/perf/perf.c:323
>>     #13 0x556701d3bac8 in handle_internal_command tools/perf/perf.c:377
>>     #14 0x556701d3be90 in run_argv tools/perf/perf.c:421
>>     #15 0x556701d3c3f8 in main tools/perf/perf.c:537
>>     #16 0x7f2952a46189 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
>>
>> SUMMARY: AddressSanitizer: 21 byte(s) leaked in 1 allocation(s).
>> ```
>>
>> Fixes: daea405f5f06 ("perf tool x86: Consolidate is_amd check into single function")
> 
> Its a fix, yes, but it is not for this daea405f5f06 patch, he just moved the
> perf_mem_is_amd_cpu(), that was leaky, to a different file, the fixes is
> for  for:
> 
> f7b58cbdb3ff36eb ("perf mem/c2c: Add load store event mappings for
> AMD").
> 
> Right?

Yeah that's correct "Fixes" commit. Thanks for the fix Ian!

Ravi
Re: [PATCH v1] perf tool x86: Fix perf_env memory leak
Posted by Arnaldo Carvalho de Melo 2 years, 7 months ago
Em Wed, Jun 14, 2023 at 08:52:43AM +0530, Ravi Bangoria escreveu:
> On 14-Jun-23 6:36 AM, Arnaldo Carvalho de Melo wrote:
> > Em Tue, Jun 13, 2023 at 04:54:16PM -0700, Ian Rogers escreveu:
> >> Found by leak sanitizer:
> >> ```
> >> ==1632594==ERROR: LeakSanitizer: detected memory leaks
> >>
> >> Direct leak of 21 byte(s) in 1 object(s) allocated from:
> >>     #0 0x7f2953a7077b in __interceptor_strdup ../../../../src/libsanitizer/asan/asan_interceptors.cpp:439
> >>     #1 0x556701d6fbbf in perf_env__read_cpuid util/env.c:369
> >>     #2 0x556701d70589 in perf_env__cpuid util/env.c:465
> >>     #3 0x55670204bba2 in x86__is_amd_cpu arch/x86/util/env.c:14
> >>     #4 0x5567020487a2 in arch__post_evsel_config arch/x86/util/evsel.c:83
> >>     #5 0x556701d8f78b in evsel__config util/evsel.c:1366
> >>     #6 0x556701ef5872 in evlist__config util/record.c:108
> >>     #7 0x556701cd6bcd in test__PERF_RECORD tests/perf-record.c:112
> >>     #8 0x556701cacd07 in run_test tests/builtin-test.c:236
> >>     #9 0x556701cacfac in test_and_print tests/builtin-test.c:265
> >>     #10 0x556701cadddb in __cmd_test tests/builtin-test.c:402
> >>     #11 0x556701caf2aa in cmd_test tests/builtin-test.c:559
> >>     #12 0x556701d3b557 in run_builtin tools/perf/perf.c:323
> >>     #13 0x556701d3bac8 in handle_internal_command tools/perf/perf.c:377
> >>     #14 0x556701d3be90 in run_argv tools/perf/perf.c:421
> >>     #15 0x556701d3c3f8 in main tools/perf/perf.c:537
> >>     #16 0x7f2952a46189 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
> >>
> >> SUMMARY: AddressSanitizer: 21 byte(s) leaked in 1 allocation(s).
> >> ```
> >>
> >> Fixes: daea405f5f06 ("perf tool x86: Consolidate is_amd check into single function")
> > 
> > Its a fix, yes, but it is not for this daea405f5f06 patch, he just moved the
> > perf_mem_is_amd_cpu(), that was leaky, to a different file, the fixes is
> > for  for:
> > 
> > f7b58cbdb3ff36eb ("perf mem/c2c: Add load store event mappings for
> > AMD").
> > 
> > Right?
> 
> Yeah that's correct "Fixes" commit. Thanks for the fix Ian!

Thanks, I'm taking that as an:

Acked-by: Ravi Bangoria <ravi.bangoria@amd.com>

Ok?

- Arnaldo
Re: [PATCH v1] perf tool x86: Fix perf_env memory leak
Posted by Ravi Bangoria 2 years, 7 months ago
On 14-Jun-23 7:12 PM, Arnaldo Carvalho de Melo wrote:
> Em Wed, Jun 14, 2023 at 08:52:43AM +0530, Ravi Bangoria escreveu:
>> On 14-Jun-23 6:36 AM, Arnaldo Carvalho de Melo wrote:
>>> Em Tue, Jun 13, 2023 at 04:54:16PM -0700, Ian Rogers escreveu:
>>>> Found by leak sanitizer:
>>>> ```
>>>> ==1632594==ERROR: LeakSanitizer: detected memory leaks
>>>>
>>>> Direct leak of 21 byte(s) in 1 object(s) allocated from:
>>>>     #0 0x7f2953a7077b in __interceptor_strdup ../../../../src/libsanitizer/asan/asan_interceptors.cpp:439
>>>>     #1 0x556701d6fbbf in perf_env__read_cpuid util/env.c:369
>>>>     #2 0x556701d70589 in perf_env__cpuid util/env.c:465
>>>>     #3 0x55670204bba2 in x86__is_amd_cpu arch/x86/util/env.c:14
>>>>     #4 0x5567020487a2 in arch__post_evsel_config arch/x86/util/evsel.c:83
>>>>     #5 0x556701d8f78b in evsel__config util/evsel.c:1366
>>>>     #6 0x556701ef5872 in evlist__config util/record.c:108
>>>>     #7 0x556701cd6bcd in test__PERF_RECORD tests/perf-record.c:112
>>>>     #8 0x556701cacd07 in run_test tests/builtin-test.c:236
>>>>     #9 0x556701cacfac in test_and_print tests/builtin-test.c:265
>>>>     #10 0x556701cadddb in __cmd_test tests/builtin-test.c:402
>>>>     #11 0x556701caf2aa in cmd_test tests/builtin-test.c:559
>>>>     #12 0x556701d3b557 in run_builtin tools/perf/perf.c:323
>>>>     #13 0x556701d3bac8 in handle_internal_command tools/perf/perf.c:377
>>>>     #14 0x556701d3be90 in run_argv tools/perf/perf.c:421
>>>>     #15 0x556701d3c3f8 in main tools/perf/perf.c:537
>>>>     #16 0x7f2952a46189 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
>>>>
>>>> SUMMARY: AddressSanitizer: 21 byte(s) leaked in 1 allocation(s).
>>>> ```
>>>>
>>>> Fixes: daea405f5f06 ("perf tool x86: Consolidate is_amd check into single function")
>>>
>>> Its a fix, yes, but it is not for this daea405f5f06 patch, he just moved the
>>> perf_mem_is_amd_cpu(), that was leaky, to a different file, the fixes is
>>> for  for:
>>>
>>> f7b58cbdb3ff36eb ("perf mem/c2c: Add load store event mappings for
>>> AMD").
>>>
>>> Right?
>>
>> Yeah that's correct "Fixes" commit. Thanks for the fix Ian!
> 
> Thanks, I'm taking that as an:
> 
> Acked-by: Ravi Bangoria <ravi.bangoria@amd.com>
> 
> Ok?

Yup.

Thanks,
Ravi
Re: [PATCH v1] perf tool x86: Fix perf_env memory leak
Posted by Ian Rogers 2 years, 7 months ago
On Tue, Jun 13, 2023 at 8:24 PM Ravi Bangoria <ravi.bangoria@amd.com> wrote:
>
> On 14-Jun-23 6:36 AM, Arnaldo Carvalho de Melo wrote:
> > Em Tue, Jun 13, 2023 at 04:54:16PM -0700, Ian Rogers escreveu:
> >> Found by leak sanitizer:
> >> ```
> >> ==1632594==ERROR: LeakSanitizer: detected memory leaks
> >>
> >> Direct leak of 21 byte(s) in 1 object(s) allocated from:
> >>     #0 0x7f2953a7077b in __interceptor_strdup ../../../../src/libsanitizer/asan/asan_interceptors.cpp:439
> >>     #1 0x556701d6fbbf in perf_env__read_cpuid util/env.c:369
> >>     #2 0x556701d70589 in perf_env__cpuid util/env.c:465
> >>     #3 0x55670204bba2 in x86__is_amd_cpu arch/x86/util/env.c:14
> >>     #4 0x5567020487a2 in arch__post_evsel_config arch/x86/util/evsel.c:83
> >>     #5 0x556701d8f78b in evsel__config util/evsel.c:1366
> >>     #6 0x556701ef5872 in evlist__config util/record.c:108
> >>     #7 0x556701cd6bcd in test__PERF_RECORD tests/perf-record.c:112
> >>     #8 0x556701cacd07 in run_test tests/builtin-test.c:236
> >>     #9 0x556701cacfac in test_and_print tests/builtin-test.c:265
> >>     #10 0x556701cadddb in __cmd_test tests/builtin-test.c:402
> >>     #11 0x556701caf2aa in cmd_test tests/builtin-test.c:559
> >>     #12 0x556701d3b557 in run_builtin tools/perf/perf.c:323
> >>     #13 0x556701d3bac8 in handle_internal_command tools/perf/perf.c:377
> >>     #14 0x556701d3be90 in run_argv tools/perf/perf.c:421
> >>     #15 0x556701d3c3f8 in main tools/perf/perf.c:537
> >>     #16 0x7f2952a46189 in __libc_start_call_main ../sysdeps/nptl/libc_start_call_main.h:58
> >>
> >> SUMMARY: AddressSanitizer: 21 byte(s) leaked in 1 allocation(s).
> >> ```
> >>
> >> Fixes: daea405f5f06 ("perf tool x86: Consolidate is_amd check into single function")
> >
> > Its a fix, yes, but it is not for this daea405f5f06 patch, he just moved the
> > perf_mem_is_amd_cpu(), that was leaky, to a different file, the fixes is
> > for  for:
> >
> > f7b58cbdb3ff36eb ("perf mem/c2c: Add load store event mappings for
> > AMD").
> >
> > Right?
>
> Yeah that's correct "Fixes" commit. Thanks for the fix Ian!
>
> Ravi

Thanks Arnaldo and Ravi. No worries on the fix, if you build with
EXTRA_CFLAGS="-fsanitize=address" then you should find "perf test" is
now passing unless a memory leak has been introduced (address
sanitizer coming with leak sanitizer).  Of course there could be
memory leaks specific to AMD so please check it out :-)

Thanks,
Ian