From: Ian Rogers <irogers@google.com>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Namhyung Kim <namhyung@kernel.org>, Jiri Olsa <jolsa@kernel.org>,
Ian Rogers <irogers@google.com>,
Adrian Hunter <adrian.hunter@intel.com>,
James Clark <james.clark@linaro.org>
Cc: Howard Chu <howardchu95@gmail.com>,
Thomas Falcon <thomas.falcon@intel.com>,
Song Liu <songliubraving@fb.com>,
linux-perf-users@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v1 11/13] perf bpf-filter: Copy bpf_filters in evsel__clone() and close on evsel__close()
Date: Sun, 22 Nov 2026 23:19:11 -0800 [thread overview]
Message-ID: <20261123071913.653315-12-irogers@google.com> (raw)
In-Reply-To: <20261123071913.653315-1-irogers@google.com>
evsel__clone() copies evsel->filter for tracepoint/hardware filters, but
left evsel->bpf_filters empty on cloned evsels. When --for-each-cgroup
clones template evsels per cgroup, the cloned evsels lost their parsed
BPF filter expressions and ran unfiltered. Add perf_bpf_filter__copy() to
deep-copy the BPF filter expression list (including OR-group children,
which perf_bpf_filter__destroy() also leaked).
In addition, separate runtime skeleton/pinned-map teardown into
perf_bpf_filter__close() (without freeing the parsed filter expressions
or another evsel's pinned_filters entries) and call it in evsel__close()
so repeated runs (such as 'perf stat -r N') release the closed FDs' BPF
filter state and attach cleanly on subsequent runs.
Fixes: 56ec9457a4a2 ("perf bpf filter: Implement event sample filtering")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/bpf-filter.c | 57 ++++++++++++++++++++++++++++++++++--
tools/perf/util/bpf-filter.h | 10 +++++++
tools/perf/util/evlist.c | 2 ++
tools/perf/util/evsel.c | 4 +++
4 files changed, 70 insertions(+), 3 deletions(-)
diff --git a/tools/perf/util/bpf-filter.c b/tools/perf/util/bpf-filter.c
index 506414e5c407..affb75e7ff85 100644
--- a/tools/perf/util/bpf-filter.c
+++ b/tools/perf/util/bpf-filter.c
@@ -515,6 +515,8 @@ int perf_bpf_filter__prepare(struct evsel *evsel, struct target *target)
struct pinned_filter_idx *pfi, *tmp;
list_for_each_entry_safe(pfi, tmp, &pinned_filters, list) {
+ if (pfi->evsel != evsel)
+ continue;
destroy_idx_hash(pfi);
list_del(&pfi->list);
free(pfi);
@@ -524,22 +526,71 @@ int perf_bpf_filter__prepare(struct evsel *evsel, struct target *target)
return ret;
}
-int perf_bpf_filter__destroy(struct evsel *evsel)
+static void free_filter_expr_list(struct list_head *head)
{
struct perf_bpf_filter_expr *expr, *tmp;
- struct pinned_filter_idx *pfi, *pos;
+ struct perf_bpf_filter_expr *grp, *gtmp;
- list_for_each_entry_safe(expr, tmp, &evsel->bpf_filters, list) {
+ list_for_each_entry_safe(expr, tmp, head, list) {
+ list_for_each_entry_safe(grp, gtmp, &expr->groups, list) {
+ list_del(&grp->list);
+ free(grp);
+ }
list_del(&expr->list);
free(expr);
}
+}
+
+int perf_bpf_filter__copy(struct list_head *dst, const struct list_head *src)
+{
+ const struct perf_bpf_filter_expr *expr, *grp;
+
+ list_for_each_entry(expr, src, list) {
+ struct perf_bpf_filter_expr *copy;
+
+ copy = perf_bpf_filter_expr__new(expr->term, expr->part,
+ expr->op, expr->val);
+ if (!copy)
+ goto err;
+ list_add_tail(©->list, dst);
+
+ list_for_each_entry(grp, &expr->groups, list) {
+ struct perf_bpf_filter_expr *gcopy;
+
+ gcopy = perf_bpf_filter_expr__new(grp->term, grp->part,
+ grp->op, grp->val);
+ if (!gcopy)
+ goto err;
+ list_add_tail(&gcopy->list, ©->groups);
+ }
+ }
+ return 0;
+
+err:
+ free_filter_expr_list(dst);
+ return -ENOMEM;
+}
+
+void perf_bpf_filter__close(struct evsel *evsel)
+{
+ struct pinned_filter_idx *pfi, *pos;
+
sample_filter_bpf__destroy(evsel->bpf_skel);
+ evsel->bpf_skel = NULL;
list_for_each_entry_safe(pfi, pos, &pinned_filters, list) {
+ if (pfi->evsel != evsel)
+ continue;
destroy_idx_hash(pfi);
list_del(&pfi->list);
free(pfi);
}
+}
+
+int perf_bpf_filter__destroy(struct evsel *evsel)
+{
+ free_filter_expr_list(&evsel->bpf_filters);
+ perf_bpf_filter__close(evsel);
return 0;
}
diff --git a/tools/perf/util/bpf-filter.h b/tools/perf/util/bpf-filter.h
index 818c554b91b2..cf016174bc5b 100644
--- a/tools/perf/util/bpf-filter.h
+++ b/tools/perf/util/bpf-filter.h
@@ -28,7 +28,9 @@ struct perf_bpf_filter_expr *perf_bpf_filter_expr__new(enum perf_bpf_filter_term
enum perf_bpf_filter_op op,
unsigned long val);
int perf_bpf_filter__parse(struct list_head *expr_head, const char *str);
+int perf_bpf_filter__copy(struct list_head *dst, const struct list_head *src);
int perf_bpf_filter__prepare(struct evsel *evsel, struct target *target);
+void perf_bpf_filter__close(struct evsel *evsel);
int perf_bpf_filter__destroy(struct evsel *evsel);
u64 perf_bpf_filter__lost_count(struct evsel *evsel);
int perf_bpf_filter__pin(void);
@@ -45,11 +47,19 @@ static inline int perf_bpf_filter__parse(struct list_head *expr_head __maybe_unu
"\tPlease make sure to build with libbpf and BPF skeleton.\n");
return -EOPNOTSUPP;
}
+static inline int perf_bpf_filter__copy(struct list_head *dst __maybe_unused,
+ const struct list_head *src __maybe_unused)
+{
+ return 0;
+}
static inline int perf_bpf_filter__prepare(struct evsel *evsel __maybe_unused,
struct target *target __maybe_unused)
{
return -EOPNOTSUPP;
}
+static inline void perf_bpf_filter__close(struct evsel *evsel __maybe_unused)
+{
+}
static inline int perf_bpf_filter__destroy(struct evsel *evsel __maybe_unused)
{
return -EOPNOTSUPP;
diff --git a/tools/perf/util/evlist.c b/tools/perf/util/evlist.c
index 638d501a7531..5bcb5d0905ea 100644
--- a/tools/perf/util/evlist.c
+++ b/tools/perf/util/evlist.c
@@ -1708,6 +1708,8 @@ void evlist__close(struct evlist *evlist)
}
evlist__for_each_entry_reverse(evlist, evsel) {
+ if (!evsel__is_bpf(evsel))
+ perf_bpf_filter__close(evsel);
perf_evsel__free_fd(&evsel->core);
perf_evsel__free_id(&evsel->core);
}
diff --git a/tools/perf/util/evsel.c b/tools/perf/util/evsel.c
index e0f3ca79c9cc..f36c9b746c84 100644
--- a/tools/perf/util/evsel.c
+++ b/tools/perf/util/evsel.c
@@ -581,6 +581,8 @@ struct evsel *evsel__clone(struct evsel *orig)
if (evsel->filter == NULL)
goto out_err;
}
+ if (perf_bpf_filter__copy(&evsel->bpf_filters, &orig->bpf_filters) < 0)
+ goto out_err;
if (orig->metric_id) {
evsel->metric_id = strdup(orig->metric_id);
if (evsel->metric_id == NULL)
@@ -3261,6 +3263,8 @@ void evsel__close(struct evsel *evsel)
{
if (evsel__is_retire_lat(evsel))
evsel__tpebs_close(evsel);
+ if (!evsel__is_bpf(evsel))
+ perf_bpf_filter__close(evsel);
perf_evsel__close(&evsel->core);
perf_evsel__free_id(&evsel->core);
}
--
2.56.1.750.gf70dc682cf-goog
next prev parent reply other threads:[~2026-11-23 7:20 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 7:19 [PATCH v1 00/13] perf fixes: off-cpu, stat, tool PMU, bperf, and bpf-filter fixes Ian Rogers
2026-11-23 7:19 ` [PATCH v1 01/13] perf off-cpu: Bound callchain length in off_cpu_write() Ian Rogers
2026-11-23 7:19 ` [PATCH v1 02/13] perf off-cpu: Fix swapped PID and TID in direct off_cpu_dump() samples Ian Rogers
2026-11-23 7:19 ` [PATCH v1 03/13] libperf evlist: Recompute CPU map when an evsel goes system wide Ian Rogers
2026-11-23 7:19 ` [PATCH v1 04/13] perf stat: Give non-perf_event_open events unique IDs for stat record Ian Rogers
2026-11-23 7:37 ` sashiko-bot
2026-11-23 7:19 ` [PATCH v1 05/13] perf stat: Preserve per-evsel CPU maps across stat record and report Ian Rogers
2026-11-23 7:35 ` sashiko-bot
2026-11-23 7:19 ` [PATCH v1 06/13] perf pmu: Don't treat tool PMU as a kernel software PMU for event grouping Ian Rogers
2026-11-23 7:19 ` [PATCH v1 07/13] perf evlist: Enable tool PMU events when starting a workload Ian Rogers
2026-11-23 7:19 ` [PATCH v1 08/13] perf stat: Respect the enabled time of exited user_time/system_time events Ian Rogers
2026-11-23 7:19 ` [PATCH v1 09/13] perf stat: Fix --bpf-counters with non-bperf, hybrid, and mixed events Ian Rogers
2026-11-23 7:19 ` [PATCH v1 10/13] perf bpf-filter: Fix bpf_link leak by using PERF_EVENT_IOC_SET_BPF Ian Rogers
2026-11-23 7:19 ` Ian Rogers [this message]
2026-11-23 7:36 ` [PATCH v1 11/13] perf bpf-filter: Copy bpf_filters in evsel__clone() and close on evsel__close() sashiko-bot
2026-11-23 7:19 ` [PATCH v1 12/13] perf bpf-filter: Support BPF sample filters on tracepoint events Ian Rogers
2026-11-23 7:19 ` [PATCH v1 13/13] perf parse-events: Route non-field tracepoint filter terms to BPF filter Ian Rogers
2026-11-23 7:44 ` [PATCH v1 00/13] perf fixes: off-cpu, stat, tool PMU, bperf, and bpf-filter fixes sashiko-bot
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=20261123071913.653315-12-irogers@google.com \
--to=irogers@google.com \
--cc=acme@kernel.org \
--cc=adrian.hunter@intel.com \
--cc=howardchu95@gmail.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@redhat.com \
--cc=namhyung@kernel.org \
--cc=peterz@infradead.org \
--cc=songliubraving@fb.com \
--cc=thomas.falcon@intel.com \
/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