mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] perf bpf-filter: Fix memory leak on bpf_link
@ 2026-10-20  5:32 Namhyung Kim
  0 siblings, 0 replies; only message in thread
From: Namhyung Kim @ 2026-10-20  5:32 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo, Ian Rogers, James Clark
  Cc: Jiri Olsa, Adrian Hunter, Peter Zijlstra, Ingo Molnar, LKML,
	linux-perf-users

Attaching BPF program returns bpf_link but it leaked like following.

  ==51328==ERROR: LeakSanitizer: detected memory leaks

  Direct leak of 1568 byte(s) in 28 object(s) allocated from:
      #0 0x7ffbe76ef24f in calloc (/lib64/libasan.so.8+0xef24f) (BuildId: 61e8165ca90f7a4f06b73f064a938c76deaae079)
      #1 0x000000625df7 in bpf_program__attach_perf_event_opts (tools/perf/perf+0x625df7) (BuildId: 5e6309fda155e58708e92f1c76d44cadd585d0eb)
      #2 0x000000beddc0 in perf_bpf_filter__prepare util/bpf-filter.c:507
      #3 0x0000008f3def in evlist__apply_filters util/evlist.c:1421
      #4 0x00000044bbcf in record__open  tools/perf/builtin-record.c:1469
      #5 0x000000453f9e in __cmd_record  tools/perf/builtin-record.c:2594
      #6 0x00000045dadb in cmd_record  tools/perf/builtin-record.c:4427
      #7 0x0000005df56b in run_builtin  tools/perf/perf.c:383
      #8 0x0000005dfa0e in handle_internal_command  tools/perf/perf.c:436
      #9 0x0000005dfddc in run_argv  tools/perf/perf.c:480
      #10 0x0000005e0333 in main  tools/perf/perf.c:581
      #11 0x7ffbdd450680 in __libc_start_call_main (/lib64/libc.so.6+0x3680) (BuildId: 85075e2ae2e13352a1413d41c327dacf6461526c)
      #12 0x7ffbdd450797 in __libc_start_main@GLIBC_2.2.5 (/lib64/libc.so.6+0x3797) (BuildId: 85075e2ae2e13352a1413d41c327dacf6461526c)
      #13 0x000000409c54 in _start (tools/perf/perf+0x409c54) (BuildId: 5e6309fda155e58708e92f1c76d44cadd585d0eb)

  SUMMARY: AddressSanitizer: 1568 byte(s) leaked in 28 allocation(s).

Let's add an xyarray of bpf_link for the filters and release them at the
end to fix the leaks.

Fixes: 56ec9457a4a20c5e ("perf bpf filter: Implement event sample filtering")
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/bpf-filter.c | 32 ++++++++++++++++++++++++++++++++
 tools/perf/util/evsel.h      |  1 +
 2 files changed, 33 insertions(+)

diff --git a/tools/perf/util/bpf-filter.c b/tools/perf/util/bpf-filter.c
index 72e83ac1d6a4b075..2fec6004b1aabc1e 100644
--- a/tools/perf/util/bpf-filter.c
+++ b/tools/perf/util/bpf-filter.c
@@ -67,6 +67,7 @@
 #include "bpf_skel/sample_filter.skel.h"
 
 #define FD(e, x, y) (*(int *)xyarray__entry(e->core.fd, x, y))
+#define LINK(e, x, y) (*(struct bpf_link **)xyarray__entry(e->bpf_links, x, y))
 
 #define __PERF_SAMPLE_TYPE(tt, st, opt)	{ tt, #st, opt }
 #define PERF_SAMPLE_TYPE(_st, opt)	__PERF_SAMPLE_TYPE(PBF_TERM_##_st, PERF_SAMPLE_##_st, opt)
@@ -501,6 +502,15 @@ int perf_bpf_filter__prepare(struct evsel *evsel, struct target *target)
 		goto err;
 	}
 
+	evsel->bpf_links = xyarray__new(xyarray__max_x(evsel->core.fd),
+					xyarray__max_y(evsel->core.fd),
+					sizeof(link));
+	if (evsel->bpf_links == NULL) {
+		ret = -errno;
+		pr_err("Failed to allocate bpf links\n");
+		goto err;
+	}
+
 	prog = skel->progs.perf_sample_filter;
 	for (x = 0; x < xyarray__max_x(evsel->core.fd); x++) {
 		for (y = 0; y < xyarray__max_y(evsel->core.fd); y++) {
@@ -511,6 +521,8 @@ int perf_bpf_filter__prepare(struct evsel *evsel, struct target *target)
 				ret = PTR_ERR(link);
 				goto err;
 			}
+
+			LINK(evsel, x, y) = link;
 		}
 	}
 	free(entry);
@@ -528,6 +540,15 @@ int perf_bpf_filter__prepare(struct evsel *evsel, struct target *target)
 			free(pfi);
 		}
 	}
+	if (evsel->bpf_links) {
+		for (x = 0; x < xyarray__max_x(evsel->bpf_links); x++) {
+			for (y = 0; y < xyarray__max_y(evsel->bpf_links); y++) {
+				bpf_link__destroy(LINK(evsel, x, y));
+			}
+		}
+		xyarray__delete(evsel->bpf_links);
+		evsel->bpf_links = NULL;
+	}
 	sample_filter_bpf__destroy(skel);
 	return ret;
 }
@@ -541,6 +562,17 @@ int perf_bpf_filter__destroy(struct evsel *evsel)
 		list_del(&expr->list);
 		free(expr);
 	}
+
+	if (evsel->bpf_links) {
+		for (int x = 0; x < xyarray__max_x(evsel->bpf_links); x++) {
+			for (int y = 0; y < xyarray__max_y(evsel->bpf_links); y++) {
+				bpf_link__destroy(LINK(evsel, x, y));
+			}
+		}
+		xyarray__delete(evsel->bpf_links);
+		evsel->bpf_links = NULL;
+	}
+
 	sample_filter_bpf__destroy(evsel->bpf_skel);
 
 	list_for_each_entry_safe(pfi, pos, &pinned_filters, list) {
diff --git a/tools/perf/util/evsel.h b/tools/perf/util/evsel.h
index 53f3641b66e18674..c4639e6aeac2ffce 100644
--- a/tools/perf/util/evsel.h
+++ b/tools/perf/util/evsel.h
@@ -174,6 +174,7 @@ struct evsel {
 
 	struct list_head	bpf_counter_list; /* for perf-stat -b */
 	struct list_head	bpf_filters; /* for perf-record --filter */
+	struct xyarray		*bpf_links;
 
 	/* for perf-stat --use-bpf */
 	int			bperf_leader_prog_fd;
-- 
2.56.0


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-10-20  5:32 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-10-20  5:32 [PATCH] perf bpf-filter: Fix memory leak on bpf_link Namhyung Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®