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 12/13] perf bpf-filter: Support BPF sample filters on tracepoint events
Date: Sun, 22 Nov 2026 23:19:12 -0800 [thread overview]
Message-ID: <20261123071913.653315-13-irogers@google.com> (raw)
In-Reply-To: <20261123071913.653315-1-irogers@google.com>
Commit 466db4275edd ("perf parse-events: Add parse_uid_filter helper")
updated set_filter() to route non-tracepoint filter terms (such as uid
and gid) on PERF_TYPE_TRACEPOINT events to evsel->bpf_filters instead
of the ftrace tracepoint predicate filter.
However, in the kernel's __perf_event_set_bpf_prog()
(kernel/events/core.c), attaching a BPF program to a perf_event fd
branches on perf_event_is_tracing(event):
- Non-tracing events (hardware PMU, software events, breakpoints) call
perf_event_set_bpf_handler(), which requires BPF_PROG_TYPE_PERF_EVENT
(SEC("perf_event")).
- Tracepoint events (event->pmu == &perf_tracepoint) reject
BPF_PROG_TYPE_PERF_EVENT with -EINVAL and instead require
BPF_PROG_TYPE_TRACEPOINT (SEC("tracepoint")), which is attached to
event->tp_event->prog_array and executed in
perf_trace_run_bpf_submit() before perf_tp_event(), dropping the
sample if the BPF program returns 0. Furthermore, the
bpf_cast_to_kern_ctx() kfunc is only permitted in
BPF_PROG_TYPE_PERF_EVENT programs.
Add SEC("tracepoint") int perf_tp_sample_filter(void *ctx) in
sample_filter.bpf.c to evaluate context-independent filter terms (uid,
gid, tid, cpu, time, cgroup) using BPF helpers without calling
bpf_cast_to_kern_ctx(), guarding bpf_get_current_cgroup_id() with CO-RE
for !CONFIG_CGROUPS kernels. When preparing a BPF filter on a
PERF_TYPE_TRACEPOINT evsel, autoload perf_tp_sample_filter instead of
perf_sample_filter and attach it once to the shared trace_event_call.
Check CAP_SYS_ADMIN || (CAP_BPF && CAP_PERFMON) when deciding whether
the caller can load BPF filter programs directly.
Because perf_bpf_filter__pin() and perf_bpf_filter__unpin() only load
perf_sample_filter and disable autoload on perf_tp_sample_filter,
introduce pin_filter_object() and unpin_filter_object() to pin/unpin the
maps and only the loaded perf_sample_filter program.
Fixes: 466db4275edd ("perf parse-events: Add parse_uid_filter helper")
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/util/bpf-filter.c | 158 ++++++++++++++++---
tools/perf/util/bpf_skel/sample_filter.bpf.c | 101 ++++++++++++
2 files changed, 238 insertions(+), 21 deletions(-)
diff --git a/tools/perf/util/bpf-filter.c b/tools/perf/util/bpf-filter.c
index affb75e7ff85..0a07429642be 100644
--- a/tools/perf/util/bpf-filter.c
+++ b/tools/perf/util/bpf-filter.c
@@ -41,8 +41,10 @@
* it will be reported as lost samples.
*/
#include <errno.h>
+#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
+#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
@@ -120,19 +122,36 @@ static const struct perf_sample_info *get_sample_info(enum perf_bpf_filter_term
return NULL;
}
+static bool can_load_bpf_filter(void)
+{
+ return perf_cap__capable(CAP_SYS_ADMIN) ||
+ (perf_cap__capable(CAP_BPF) && perf_cap__capable(CAP_PERFMON));
+}
+
static int check_sample_flags(struct evsel *evsel, struct perf_bpf_filter_expr *expr)
{
const struct perf_sample_info *info;
- if (expr->term >= PBF_TERM_SAMPLE_START && expr->term <= PBF_TERM_SAMPLE_END &&
- (evsel->core.attr.sample_type & (1 << (expr->term - PBF_TERM_SAMPLE_START))))
- return 0;
-
if (expr->term == PBF_TERM_UID || expr->term == PBF_TERM_GID) {
/* Not dependent on the sample_type as computed from a BPF helper. */
return 0;
}
+ if (evsel->core.attr.type == PERF_TYPE_TRACEPOINT) {
+ if (expr->term == PBF_TERM_TID || expr->term == PBF_TERM_CPU ||
+ expr->term == PBF_TERM_TIME || expr->term == PBF_TERM_CGROUP)
+ return 0;
+ if (expr->op != PBF_OP_GROUP_BEGIN) {
+ pr_err("Error: %s tracepoint event does not support BPF sample term %d\n",
+ evsel__name(evsel), expr->term);
+ return -1;
+ }
+ }
+
+ if (expr->term >= PBF_TERM_SAMPLE_START && expr->term <= PBF_TERM_SAMPLE_END &&
+ (evsel->core.attr.sample_type & (1 << (expr->term - PBF_TERM_SAMPLE_START))))
+ return 0;
+
if (expr->op == PBF_OP_GROUP_BEGIN) {
struct perf_bpf_filter_expr *group;
@@ -426,6 +445,11 @@ int perf_bpf_filter__prepare(struct evsel *evsel, struct target *target)
struct sample_filter_bpf *skel = NULL;
struct perf_bpf_filter_entry *entry;
bool needs_idx_hash = !target__has_cpu(target);
+ bool is_tracepoint = (evsel->core.attr.type == PERF_TYPE_TRACEPOINT);
+ bool can_load = can_load_bpf_filter();
+
+ if (evsel->bpf_skel)
+ return 0;
entry = calloc(MAX_FILTERS, sizeof(*entry));
if (entry == NULL)
@@ -437,7 +461,13 @@ int perf_bpf_filter__prepare(struct evsel *evsel, struct target *target)
goto err;
}
- if (needs_idx_hash && geteuid() != 0) {
+ if (is_tracepoint && !can_load) {
+ pr_err("Error: Tracepoint BPF filter requires CAP_BPF and CAP_PERFMON (or CAP_SYS_ADMIN)\n");
+ ret = -EPERM;
+ goto err;
+ }
+
+ if (needs_idx_hash && !can_load) {
int zero = 0;
/* The filters map is shared among other processes */
@@ -477,8 +507,17 @@ int perf_bpf_filter__prepare(struct evsel *evsel, struct target *target)
return 0;
}
- skel = sample_filter_bpf__open_and_load();
+ skel = sample_filter_bpf__open();
if (!skel) {
+ ret = -errno;
+ pr_err("Failed to open perf sample-filter BPF skeleton\n");
+ goto err;
+ }
+
+ bpf_program__set_autoload(skel->progs.perf_sample_filter, !is_tracepoint);
+ bpf_program__set_autoload(skel->progs.perf_tp_sample_filter, is_tracepoint);
+
+ if (sample_filter_bpf__load(skel) < 0) {
ret = -errno;
pr_err("Failed to load perf sample-filter BPF skeleton\n");
goto err;
@@ -494,14 +533,32 @@ int perf_bpf_filter__prepare(struct evsel *evsel, struct target *target)
goto err;
}
- fd = bpf_program__fd(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++) {
- ret = ioctl(FD(evsel, x, y), PERF_EVENT_IOC_SET_BPF, fd);
- if (ret < 0) {
- ret = -errno;
- pr_err("Failed to attach perf sample-filter program\n");
- goto err;
+ if (is_tracepoint) {
+ /*
+ * For PERF_TYPE_TRACEPOINT, PERF_EVENT_IOC_SET_BPF attaches the
+ * BPF program to event->tp_event->prog_array on the shared
+ * trace_event_call, which perf_trace_run_bpf_submit() executes
+ * for all CPUs and tasks before iterating the per-CPU perf
+ * events. Attach once on the first FD so tp_event->prog_array
+ * does not get duplicate copies for each per-CPU FD.
+ */
+ fd = bpf_program__fd(skel->progs.perf_tp_sample_filter);
+ ret = ioctl(FD(evsel, 0, 0), PERF_EVENT_IOC_SET_BPF, fd);
+ if (ret < 0) {
+ ret = -errno;
+ pr_err("Failed to attach perf tracepoint sample-filter program\n");
+ goto err;
+ }
+ } else {
+ fd = bpf_program__fd(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++) {
+ ret = ioctl(FD(evsel, x, y), PERF_EVENT_IOC_SET_BPF, fd);
+ if (ret < 0) {
+ ret = -errno;
+ pr_err("Failed to attach perf sample-filter program\n");
+ goto err;
+ }
}
}
}
@@ -649,7 +706,7 @@ static bool check_bpf_filter_capable(void)
{
int fd;
- if (perf_cap__capable(CAP_BPF))
+ if (can_load_bpf_filter())
return true;
/* Check if root already pinned the filter programs and maps */
@@ -659,7 +716,7 @@ static bool check_bpf_filter_capable(void)
return true;
}
- pr_err("Error: BPF filter only works for users with the CAP_BPF capability!\n"
+ pr_err("Error: BPF filter requires CAP_BPF and CAP_PERFMON (or CAP_SYS_ADMIN)!\n"
"\tPlease run 'perf record --setup-filter pin' as root first.\n");
return false;
@@ -684,6 +741,55 @@ int perf_bpf_filter__parse(struct list_head *expr_head, const char *str)
return ret;
}
+/*
+ * perf_bpf_filter__pin() and perf_bpf_filter__unpin() only load
+ * perf_sample_filter and disable autoload on other programs in the skeleton
+ * (such as perf_tp_sample_filter). While libbpf's bpf_object__pin_maps() skips
+ * maps that were not created, bpf_object__pin_programs() and
+ * bpf_object__unpin_programs() iterate over every program in skel->obj without
+ * checking whether autoload was enabled and fail with -EINVAL when a program
+ * was not loaded. Pin and unpin the maps and the loaded perf_sample_filter
+ * program explicitly instead of calling bpf_object__pin() / bpf_object__unpin().
+ */
+static int pin_filter_object(struct sample_filter_bpf *skel, const char *path)
+{
+ char *prog_path = NULL;
+ int ret;
+
+ ret = bpf_object__pin_maps(skel->obj, path);
+ if (ret < 0)
+ return ret;
+
+ if (asprintf(&prog_path, "%s/perf_sample_filter", path) < 0) {
+ bpf_object__unpin_maps(skel->obj, path);
+ return -ENOMEM;
+ }
+
+ ret = bpf_program__pin(skel->progs.perf_sample_filter, prog_path);
+ if (ret < 0)
+ bpf_object__unpin_maps(skel->obj, path);
+
+ free(prog_path);
+ return ret;
+}
+
+static int unpin_filter_object(struct sample_filter_bpf *skel, const char *path)
+{
+ char *prog_path = NULL;
+ int ret, map_ret;
+
+ if (asprintf(&prog_path, "%s/perf_sample_filter", path) < 0) {
+ bpf_object__unpin_maps(skel->obj, path);
+ return -ENOMEM;
+ }
+
+ ret = bpf_program__unpin(skel->progs.perf_sample_filter, prog_path);
+ free(prog_path);
+
+ map_ret = bpf_object__unpin_maps(skel->obj, path);
+ return ret < 0 ? ret : map_ret;
+}
+
int perf_bpf_filter__pin(void)
{
struct sample_filter_bpf *skel;
@@ -698,6 +804,7 @@ int perf_bpf_filter__pin(void)
}
/* pinned program will use pid-hash */
+ bpf_program__set_autoload(skel->progs.perf_tp_sample_filter, false);
bpf_map__set_max_entries(skel->maps.filters, MAX_FILTERS);
bpf_map__set_max_entries(skel->maps.event_hash, MAX_EVT_HASH);
bpf_map__set_max_entries(skel->maps.idx_hash, MAX_IDX_HASH);
@@ -717,7 +824,7 @@ int perf_bpf_filter__pin(void)
goto err;
}
- ret = bpf_object__pin(skel->obj, path);
+ ret = pin_filter_object(skel, path);
if (ret < 0) {
pr_err("Failed to pin BPF filter objects\n");
goto err;
@@ -726,8 +833,8 @@ int perf_bpf_filter__pin(void)
/* setup access permissions for the pinned objects */
dir_fd = open(path, O_PATH);
if (dir_fd < 0) {
- bpf_object__unpin(skel->obj, path);
- ret = dir_fd;
+ ret = -errno;
+ unpin_filter_object(skel, path);
goto err;
}
@@ -770,6 +877,8 @@ int perf_bpf_filter__pin(void)
err_close:
close(dir_fd);
+ if (ret < 0)
+ unpin_filter_object(skel, path);
err:
free(path);
@@ -783,13 +892,20 @@ int perf_bpf_filter__unpin(void)
char *path = NULL;
int ret = -1;
- skel = sample_filter_bpf__open_and_load();
+ skel = sample_filter_bpf__open();
if (!skel) {
ret = -errno;
pr_err("Failed to open perf sample-filter BPF skeleton\n");
goto err;
}
+ bpf_program__set_autoload(skel->progs.perf_tp_sample_filter, false);
+ if (sample_filter_bpf__load(skel) < 0) {
+ ret = -errno;
+ pr_err("Failed to load perf sample-filter BPF skeleton\n");
+ goto err;
+ }
+
if (asprintf(&path, "%s/fs/bpf/%s", sysfs__mountpoint(),
PERF_BPF_FILTER_PIN_PATH) < 0) {
ret = -errno;
@@ -797,7 +913,7 @@ int perf_bpf_filter__unpin(void)
goto err;
}
- ret = bpf_object__unpin(skel->obj, path);
+ ret = unpin_filter_object(skel, path);
err:
free(path);
diff --git a/tools/perf/util/bpf_skel/sample_filter.bpf.c b/tools/perf/util/bpf_skel/sample_filter.bpf.c
index 9ddac8e5cd78..ac6309728e14 100644
--- a/tools/perf/util/bpf_skel/sample_filter.bpf.c
+++ b/tools/perf/util/bpf_skel/sample_filter.bpf.c
@@ -206,6 +206,30 @@ static inline __u64 perf_get_sample(struct bpf_perf_event_data_kern *kctx,
return 0;
}
+static __always_inline __u64 perf_get_common_sample(struct perf_bpf_filter_entry *entry)
+{
+ switch (entry->term) {
+ case PBF_TERM_TID:
+ if (entry->part)
+ return bpf_get_current_pid_tgid() >> 32;
+ return (__u32)bpf_get_current_pid_tgid();
+ case PBF_TERM_CPU:
+ return bpf_get_smp_processor_id();
+ case PBF_TERM_TIME:
+ return bpf_ktime_get_ns();
+ case PBF_TERM_CGROUP:
+ if (bpf_core_field_exists(struct task_struct, cgroups))
+ return bpf_get_current_cgroup_id();
+ return 0;
+ case PBF_TERM_UID:
+ return bpf_get_current_uid_gid() & 0xFFFFFFFF;
+ case PBF_TERM_GID:
+ return bpf_get_current_uid_gid() >> 32;
+ default:
+ return 0;
+ }
+}
+
#define CHECK_RESULT(data, op, val) \
if (!(data op val)) { \
if (!in_group) \
@@ -309,4 +333,81 @@ int perf_sample_filter(void *ctx)
return 0;
}
+/*
+ * BPF program to be called for tracepoint perf events.
+ *
+ * For PERF_TYPE_TRACEPOINT events, the kernel's __perf_event_set_bpf_prog()
+ * rejects BPF_PROG_TYPE_PERF_EVENT and only accepts BPF_PROG_TYPE_TRACEPOINT,
+ * attaching the program to the shared trace_event_call->prog_array executed
+ * by perf_trace_run_bpf_submit() prior to perf_tp_event(). Because returning 0
+ * filters the event at trace_event_call->prog_array (affecting the
+ * trace_event_call globally while attached), tracepoint BPF filters are never
+ * pinned for unprivileged use and require CAP_BPF + CAP_PERFMON or
+ * CAP_SYS_ADMIN.
+ */
+SEC("tracepoint")
+int perf_tp_sample_filter(void *ctx)
+{
+ struct perf_bpf_filter_entry *entry;
+ __u64 sample_data;
+ int in_group = 0;
+ int group_result = 0;
+ int i, k = 0;
+ int *losts;
+
+ (void)ctx;
+ entry = bpf_map_lookup_elem(&filters, &k);
+ if (entry == NULL)
+ goto drop;
+
+ for (i = 0; i < MAX_FILTERS; i++) {
+ sample_data = perf_get_common_sample(&entry[i]);
+
+ switch (entry[i].op) {
+ case PBF_OP_EQ:
+ CHECK_RESULT(sample_data, ==, entry[i].value)
+ break;
+ case PBF_OP_NEQ:
+ CHECK_RESULT(sample_data, !=, entry[i].value)
+ break;
+ case PBF_OP_GT:
+ CHECK_RESULT(sample_data, >, entry[i].value)
+ break;
+ case PBF_OP_GE:
+ CHECK_RESULT(sample_data, >=, entry[i].value)
+ break;
+ case PBF_OP_LT:
+ CHECK_RESULT(sample_data, <, entry[i].value)
+ break;
+ case PBF_OP_LE:
+ CHECK_RESULT(sample_data, <=, entry[i].value)
+ break;
+ case PBF_OP_AND:
+ CHECK_RESULT(sample_data, &, entry[i].value)
+ break;
+ case PBF_OP_GROUP_BEGIN:
+ in_group = 1;
+ group_result = 0;
+ break;
+ case PBF_OP_GROUP_END:
+ if (group_result == 0)
+ goto drop;
+ in_group = 0;
+ break;
+ case PBF_OP_DONE:
+ return 1;
+ default:
+ break;
+ }
+ }
+ return 1;
+
+drop:
+ losts = bpf_map_lookup_elem(&dropped, &k);
+ if (losts != NULL)
+ __sync_fetch_and_add(losts, 1);
+
+ return 0;
+}
+
char LICENSE[] SEC("license") = "Dual BSD/GPL";
--
2.56.1.750.gf70dc682cf-goog
next prev parent reply other threads:[~2026-11-23 7:20 UTC|newest]
Thread overview: 23+ 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 17:12 ` Athira Rajeev
2026-11-24 4:23 ` Ian Rogers
2026-11-24 7:24 ` Athira Rajeev
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 23:12 ` TAKANO Mitsuhiro
2026-11-24 0:16 ` Namhyung Kim
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 ` [PATCH v1 11/13] perf bpf-filter: Copy bpf_filters in evsel__clone() and close on evsel__close() Ian Rogers
2026-11-23 7:36 ` sashiko-bot
2026-11-23 7:19 ` Ian Rogers [this message]
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-13-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.