Linux Perf Users
 help / color / mirror / Atom feed
From: Namhyung Kim <namhyung@kernel.org>
To: Arnaldo Carvalho de Melo <acme@kernel.org>,
	Ian Rogers <irogers@google.com>,
	James Clark <james.clark@linaro.org>
Cc: Jiri Olsa <jolsa@kernel.org>,
	Adrian Hunter <adrian.hunter@intel.com>,
	Peter Zijlstra <peterz@infradead.org>,
	Ingo Molnar <mingo@kernel.org>,
	LKML <linux-kernel@vger.kernel.org>,
	linux-perf-users@vger.kernel.org
Subject: [PATCH] perf bpf-filter: Fix memory leak on bpf_link
Date: Mon, 19 Oct 2026 22:32:26 -0700	[thread overview]
Message-ID: <20261020053226.55936-1-namhyung@kernel.org> (raw)

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


             reply	other threads:[~2026-10-20  5:32 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-20  5:32 Namhyung Kim [this message]
2026-10-20  8:33 ` [PATCH] perf bpf-filter: Fix memory leak on bpf_link sashiko-bot
2026-10-20 15:47 ` Ian Rogers

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261020053226.55936-1-namhyung@kernel.org \
    --to=namhyung@kernel.org \
    --cc=acme@kernel.org \
    --cc=adrian.hunter@intel.com \
    --cc=irogers@google.com \
    --cc=james.clark@linaro.org \
    --cc=jolsa@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox