2023-12-07 08:21:41

by Yicong Yang

[permalink] [raw]
Subject: [PATCH 0/2] Perf fix two memory leakage

From: Yicong Yang <[email protected]>

ASan reports two memory leakage in the raw trace dump mode `perf report -D`.
One is in the framework and can be reproduced when using hisi_ptt and
arm_spe, which is fixed in Patch 1/2. Another is in the hisi_ptt parser
and fixed in Patch 2/2.

Yicong Yang (2):
perf header: Fix one memory leakage in
perf_event__fprintf_event_update()
perf hisi-ptt: Fix one memory leakage in
hisi_ptt_process_auxtrace_event()

tools/perf/util/header.c | 5 +++--
tools/perf/util/hisi-ptt.c | 1 +
2 files changed, 4 insertions(+), 2 deletions(-)

--
2.24.0


2023-12-07 08:21:43

by Yicong Yang

[permalink] [raw]
Subject: [PATCH 1/2] perf header: Fix one memory leakage in perf_event__fprintf_event_update()

From: Yicong Yang <[email protected]>

When dump the raw trace by `perf report -D` ASan reports a memory
leakage in perf_event__fprintf_event_update(). It shows that we
allocated a temporary cpumap for dumping the CPUs but doesn't
release it and it's not used elsewhere. Fix this by free the
cpumap after the dumping.

Fixes: c853f9394b7b ("perf tools: Add perf_event__fprintf_event_update function")
Cc: Jiri Olsa <[email protected]>
Signed-off-by: Yicong Yang <[email protected]>
---
tools/perf/util/header.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
index e86b9439ffee..7190f39ccd13 100644
--- a/tools/perf/util/header.c
+++ b/tools/perf/util/header.c
@@ -4369,9 +4369,10 @@ size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
ret += fprintf(fp, "... ");

map = cpu_map__new_data(&ev->cpus.cpus);
- if (map)
+ if (map) {
ret += cpu_map__fprintf(map, fp);
- else
+ perf_cpu_map__put(map);
+ } else
ret += fprintf(fp, "failed to get cpus\n");
break;
default:
--
2.24.0

2023-12-07 08:22:15

by Yicong Yang

[permalink] [raw]
Subject: [PATCH 2/2] perf hisi-ptt: Fix one memory leakage in hisi_ptt_process_auxtrace_event()

From: Yicong Yang <[email protected]>

ASan complains a memory leakage in hisi_ptt_process_auxtrace_event()
that the data buffer is not freed. Since currently we only support
the raw dump trace mode, the data buffer is used only within this
function. So fix this by freeing the data buffer before going out.

Fixes: 5e91e57e6809 ("perf auxtrace arm64: Add support for parsing HiSilicon PCIe Trace packet")
Signed-off-by: Yicong Yang <[email protected]>
---
tools/perf/util/hisi-ptt.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/tools/perf/util/hisi-ptt.c b/tools/perf/util/hisi-ptt.c
index 43bd1ca62d58..52d0ce302ca0 100644
--- a/tools/perf/util/hisi-ptt.c
+++ b/tools/perf/util/hisi-ptt.c
@@ -123,6 +123,7 @@ static int hisi_ptt_process_auxtrace_event(struct perf_session *session,
if (dump_trace)
hisi_ptt_dump_event(ptt, data, size);

+ free(data);
return 0;
}

--
2.24.0

2023-12-07 18:45:33

by Ian Rogers

[permalink] [raw]
Subject: Re: [PATCH 0/2] Perf fix two memory leakage

On Thu, Dec 7, 2023 at 12:20 AM Yicong Yang <[email protected]> wrote:
>
> From: Yicong Yang <[email protected]>
>
> ASan reports two memory leakage in the raw trace dump mode `perf report -D`.
> One is in the framework and can be reproduced when using hisi_ptt and
> arm_spe, which is fixed in Patch 1/2. Another is in the hisi_ptt parser
> and fixed in Patch 2/2.
>
> Yicong Yang (2):
> perf header: Fix one memory leakage in
> perf_event__fprintf_event_update()
> perf hisi-ptt: Fix one memory leakage in
> hisi_ptt_process_auxtrace_event()
>
> tools/perf/util/header.c | 5 +++--
> tools/perf/util/hisi-ptt.c | 1 +
> 2 files changed, 4 insertions(+), 2 deletions(-)

The series:

Reviewed-by: Ian Rogers <[email protected]>

Thanks,
Ian

> --
> 2.24.0
>

2023-12-13 01:18:06

by Namhyung Kim

[permalink] [raw]
Subject: Re: [PATCH 1/2] perf header: Fix one memory leakage in perf_event__fprintf_event_update()

Hello,

On Thu, Dec 7, 2023 at 12:20 AM Yicong Yang <[email protected]> wrote:
>
> From: Yicong Yang <[email protected]>
>
> When dump the raw trace by `perf report -D` ASan reports a memory
> leakage in perf_event__fprintf_event_update(). It shows that we
> allocated a temporary cpumap for dumping the CPUs but doesn't
> release it and it's not used elsewhere. Fix this by free the
> cpumap after the dumping.
>
> Fixes: c853f9394b7b ("perf tools: Add perf_event__fprintf_event_update function")
> Cc: Jiri Olsa <[email protected]>
> Signed-off-by: Yicong Yang <[email protected]>

Acked-by: Namhyung Kim <[email protected]>

Thanks,
Namhyung


> ---
> tools/perf/util/header.c | 5 +++--
> 1 file changed, 3 insertions(+), 2 deletions(-)
>
> diff --git a/tools/perf/util/header.c b/tools/perf/util/header.c
> index e86b9439ffee..7190f39ccd13 100644
> --- a/tools/perf/util/header.c
> +++ b/tools/perf/util/header.c
> @@ -4369,9 +4369,10 @@ size_t perf_event__fprintf_event_update(union perf_event *event, FILE *fp)
> ret += fprintf(fp, "... ");
>
> map = cpu_map__new_data(&ev->cpus.cpus);
> - if (map)
> + if (map) {
> ret += cpu_map__fprintf(map, fp);
> - else
> + perf_cpu_map__put(map);
> + } else
> ret += fprintf(fp, "failed to get cpus\n");
> break;
> default:
> --
> 2.24.0
>
>

2023-12-13 01:21:02

by Namhyung Kim

[permalink] [raw]
Subject: Re: [PATCH 2/2] perf hisi-ptt: Fix one memory leakage in hisi_ptt_process_auxtrace_event()

On Thu, Dec 7, 2023 at 12:20 AM Yicong Yang <[email protected]> wrote:
>
> From: Yicong Yang <[email protected]>
>
> ASan complains a memory leakage in hisi_ptt_process_auxtrace_event()
> that the data buffer is not freed. Since currently we only support
> the raw dump trace mode, the data buffer is used only within this
> function. So fix this by freeing the data buffer before going out.
>
> Fixes: 5e91e57e6809 ("perf auxtrace arm64: Add support for parsing HiSilicon PCIe Trace packet")
> Signed-off-by: Yicong Yang <[email protected]>

Acked-by: Namhyung Kim <[email protected]>

Thanks,
Namhyung

> ---
> tools/perf/util/hisi-ptt.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/tools/perf/util/hisi-ptt.c b/tools/perf/util/hisi-ptt.c
> index 43bd1ca62d58..52d0ce302ca0 100644
> --- a/tools/perf/util/hisi-ptt.c
> +++ b/tools/perf/util/hisi-ptt.c
> @@ -123,6 +123,7 @@ static int hisi_ptt_process_auxtrace_event(struct perf_session *session,
> if (dump_trace)
> hisi_ptt_dump_event(ptt, data, size);
>
> + free(data);
> return 0;
> }
>
> --
> 2.24.0
>
>