# \[PATCH\] perf env: fix memory leak: free bpf\_prog\_info\_linear

- Subsystems: PERFORMANCE EVENTS SUBSYSTEM, THE REST
- Maintainers: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo, Namhyung Kim, Linus Torvalds
- Status: LANDED (67069a1f0fe5 · 2021-06-04 13:26)
- Landed in mainline as 67069a1f0fe5 on 2021-06-04.
- List: netdev
- 3 messages, 3 authors
- Page: https://ratatoskr.run/netdev/2021/06/1400526/t

## \[PATCH\] perf env: fix memory leak: free bpf\_prog\_info\_linear

- From: Riccardo Mancini \<hidden\>
- Date: 2021-06-02 22:41:41 
- Message: https://ratatoskr.run/netdev/2021/06/1400526.md

```text
ASan reported a memory leak caused by info_linear not being
deallocated. The info_linear was allocated during
perf_event__synthesize_one_bpf_prog.
This patch adds the corresponding free() when bpf_prog_info_node
is freed in perf_env__purge_bpf.

$ sudo ./perf record -- sleep 5
[ perf record: Woken up 1 times to write data ]
[ perf record: Captured and wrote 0.025 MB perf.data (8 samples) ]

=================================================================
==297735==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 7688 byte(s) in 19 object(s) allocated from:
    #0 0x4f420f in malloc (/home/user/linux/tools/perf/perf+0x4f420f)
    #1 0xc06a74 in bpf_program__get_prog_info_linear /home/user/linux/tools/lib/bpf/libbpf.c:11113:16
    #2 0xb426fe in perf_event__synthesize_one_bpf_prog /home/user/linux/tools/perf/util/bpf-event.c:191:16
    #3 0xb42008 in perf_event__synthesize_bpf_events /home/user/linux/tools/perf/util/bpf-event.c:410:9
    #4 0x594596 in record__synthesize /home/user/linux/tools/perf/builtin-record.c:1490:8
    #5 0x58c9ac in __cmd_record /home/user/linux/tools/perf/builtin-record.c:1798:8
    #6 0x58990b in cmd_record /home/user/linux/tools/perf/builtin-record.c:2901:8
    #7 0x7b2a20 in run_builtin /home/user/linux/tools/perf/perf.c:313:11
    #8 0x7b12ff in handle_internal_command /home/user/linux/tools/perf/perf.c:365:8
    #9 0x7b2583 in run_argv /home/user/linux/tools/perf/perf.c:409:2
    #10 0x7b0d79 in main /home/user/linux/tools/perf/perf.c:539:3
    #11 0x7fa357ef6b74 in __libc_start_main /usr/src/debug/glibc-2.33-8.fc34.x86_64/csu/../csu/libc-start.c:332:16

Signed-off-by: Riccardo Mancini <redacted>
---
 tools/perf/util/env.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c
index 9130f6fad8d54..bc5e4f294e9e9 100644
--- a/tools/perf/util/env.c
+++ b/tools/perf/util/env.c
@@ -144,6 +144,7 @@ static void perf_env__purge_bpf(struct perf_env *env)
 		node = rb_entry(next, struct bpf_prog_info_node, rb_node);
 		next = rb_next(&node->rb_node);
 		rb_erase(&node->rb_node, root);
+		free(node->info_linear);
 		free(node);
 	}
 
-- 
2.31.1

```

## Re: \[PATCH\] perf env: fix memory leak: free bpf\_prog\_info\_linear

- From: Ian Rogers \<irogers@google.com\>
- Date: 2021-06-04 04:16:58 
- Message: https://ratatoskr.run/netdev/2021/06/1402100.md

```text
On Wed, Jun 2, 2021 at 3:41 PM Riccardo Mancini [ref] wrote:
>
> ASan reported a memory leak caused by info_linear not being
> deallocated. The info_linear was allocated during
> perf_event__synthesize_one_bpf_prog.
> This patch adds the corresponding free() when bpf_prog_info_node
> is freed in perf_env__purge_bpf.
>
> $ sudo ./perf record -- sleep 5
> [ perf record: Woken up 1 times to write data ]
> [ perf record: Captured and wrote 0.025 MB perf.data (8 samples) ]
>
> =================================================================
> ==297735==ERROR: LeakSanitizer: detected memory leaks
>
> Direct leak of 7688 byte(s) in 19 object(s) allocated from:
>     #0 0x4f420f in malloc (/home/user/linux/tools/perf/perf+0x4f420f)
>     #1 0xc06a74 in bpf_program__get_prog_info_linear /home/user/linux/tools/lib/bpf/libbpf.c:11113:16
>     #2 0xb426fe in perf_event__synthesize_one_bpf_prog /home/user/linux/tools/perf/util/bpf-event.c:191:16
>     #3 0xb42008 in perf_event__synthesize_bpf_events /home/user/linux/tools/perf/util/bpf-event.c:410:9
>     #4 0x594596 in record__synthesize /home/user/linux/tools/perf/builtin-record.c:1490:8
>     #5 0x58c9ac in __cmd_record /home/user/linux/tools/perf/builtin-record.c:1798:8
>     #6 0x58990b in cmd_record /home/user/linux/tools/perf/builtin-record.c:2901:8
>     #7 0x7b2a20 in run_builtin /home/user/linux/tools/perf/perf.c:313:11
>     #8 0x7b12ff in handle_internal_command /home/user/linux/tools/perf/perf.c:365:8
>     #9 0x7b2583 in run_argv /home/user/linux/tools/perf/perf.c:409:2
>     #10 0x7b0d79 in main /home/user/linux/tools/perf/perf.c:539:3
>     #11 0x7fa357ef6b74 in __libc_start_main /usr/src/debug/glibc-2.33-8.fc34.x86_64/csu/../csu/libc-start.c:332:16
>
> Signed-off-by: Riccardo Mancini <redacted>

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

Thanks,
Ian

> ---
>  tools/perf/util/env.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/tools/perf/util/env.c b/tools/perf/util/env.c
> index 9130f6fad8d54..bc5e4f294e9e9 100644
> --- a/tools/perf/util/env.c
> +++ b/tools/perf/util/env.c
> @@ -144,6 +144,7 @@ static void perf_env__purge_bpf(struct perf_env *env)
>                 node = rb_entry(next, struct bpf_prog_info_node, rb_node);
>                 next = rb_next(&node->rb_node);
>                 rb_erase(&node->rb_node, root);
> +               free(node->info_linear);
>                 free(node);
>         }
>
> --
> 2.31.1
>
```

## Re: \[PATCH\] perf env: fix memory leak: free bpf\_prog\_info\_linear

- From: Arnaldo Carvalho de Melo \<acme@kernel.org\>
- Date: 2021-06-04 13:09:08 
- Message: https://ratatoskr.run/netdev/2021/06/1402614.md

```text
Em Thu, Jun 03, 2021 at 09:15:32PM -0700, Ian Rogers escreveu:
> On Wed, Jun 2, 2021 at 3:41 PM Riccardo Mancini [ref] wrote:
> >
> > ASan reported a memory leak caused by info_linear not being
> > deallocated. The info_linear was allocated during
> > perf_event__synthesize_one_bpf_prog.
> > This patch adds the corresponding free() when bpf_prog_info_node
> > is freed in perf_env__purge_bpf.
> >
> > $ sudo ./perf record -- sleep 5
> > [ perf record: Woken up 1 times to write data ]
> > [ perf record: Captured and wrote 0.025 MB perf.data (8 samples) ]
> >
> > =================================================================
> > ==297735==ERROR: LeakSanitizer: detected memory leaks
> >
> > Direct leak of 7688 byte(s) in 19 object(s) allocated from:
> >     #0 0x4f420f in malloc (/home/user/linux/tools/perf/perf+0x4f420f)
> >     #1 0xc06a74 in bpf_program__get_prog_info_linear /home/user/linux/tools/lib/bpf/libbpf.c:11113:16
> >     #2 0xb426fe in perf_event__synthesize_one_bpf_prog /home/user/linux/tools/perf/util/bpf-event.c:191:16
> >     #3 0xb42008 in perf_event__synthesize_bpf_events /home/user/linux/tools/perf/util/bpf-event.c:410:9
> >     #4 0x594596 in record__synthesize /home/user/linux/tools/perf/builtin-record.c:1490:8
> >     #5 0x58c9ac in __cmd_record /home/user/linux/tools/perf/builtin-record.c:1798:8
> >     #6 0x58990b in cmd_record /home/user/linux/tools/perf/builtin-record.c:2901:8
> >     #7 0x7b2a20 in run_builtin /home/user/linux/tools/perf/perf.c:313:11
> >     #8 0x7b12ff in handle_internal_command /home/user/linux/tools/perf/perf.c:365:8
> >     #9 0x7b2583 in run_argv /home/user/linux/tools/perf/perf.c:409:2
> >     #10 0x7b0d79 in main /home/user/linux/tools/perf/perf.c:539:3
> >     #11 0x7fa357ef6b74 in __libc_start_main /usr/src/debug/glibc-2.33-8.fc34.x86_64/csu/../csu/libc-start.c:332:16
> >
> > Signed-off-by: Riccardo Mancini <redacted>
> 
> Acked-by: Ian Rogers <irogers@google.com>

Thanks, applied.

- Arnaldo

```
