[RFC PATCH 02/16] perf/core, bpf: Support BPF filtering of mmap_data events
From: Ian Rogers
Date: Tue Dec 01 2026 - 18:21:52 EST
Add attr.mmap_bpf to struct perf_event_attr and expose the target
struct vm_area_struct *vma in struct bpf_perf_event_data_kern.
When attr.mmap_data and attr.mmap_bpf are both set on an event,
perf_event_mmap_match() invokes the attached BPF_PROG_TYPE_PERF_EVENT
program before emitting non-executable PERF_RECORD_MMAP/MMAP2 records.
This allows tools such as perf record --host-ip to capture file-backed
mappings in hypervisor processes without flooding the ring buffer with
anonymous data mappings across the system.
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
include/linux/perf_event.h | 3 ++
include/uapi/linux/perf_event.h | 3 +-
kernel/events/core.c | 87 +++++++++++++++++++++++++++++++--
3 files changed, 88 insertions(+), 5 deletions(-)
diff --git a/include/linux/perf_event.h b/include/linux/perf_event.h
index 8ce5383e4b0a..c31e1bf278ec 100644
--- a/include/linux/perf_event.h
+++ b/include/linux/perf_event.h
@@ -1167,10 +1167,13 @@ struct perf_output_handle {
int page;
};
+struct vm_area_struct;
+
struct bpf_perf_event_data_kern {
bpf_user_pt_regs_t *regs;
struct perf_sample_data *data;
struct perf_event *event;
+ struct vm_area_struct *vma;
};
#ifdef CONFIG_CGROUP_PERF
diff --git a/include/uapi/linux/perf_event.h b/include/uapi/linux/perf_event.h
index 75ed8e968d30..146df3041c31 100644
--- a/include/uapi/linux/perf_event.h
+++ b/include/uapi/linux/perf_event.h
@@ -469,7 +469,8 @@ struct perf_event_attr {
sigtrap : 1, /* send synchronous SIGTRAP on event */
defer_callchain: 1, /* request PERF_RECORD_CALLCHAIN_DEFERRED records */
defer_output : 1, /* output PERF_RECORD_CALLCHAIN_DEFERRED records */
- __reserved_1 : 24;
+ mmap_bpf : 1, /* filter mmap_data events with BPF prog */
+ __reserved_1 : 23;
union {
__u32 wakeup_events; /* wake up every n events */
diff --git a/kernel/events/core.c b/kernel/events/core.c
index 6da956ceead0..d32aaf141c9e 100644
--- a/kernel/events/core.c
+++ b/kernel/events/core.c
@@ -9880,6 +9880,9 @@ struct perf_mmap_event {
} event_id;
};
+static int bpf_mmap_handler(struct perf_event *event,
+ struct vm_area_struct *vma);
+
static int perf_event_mmap_match(struct perf_event *event,
void *data)
{
@@ -9887,8 +9890,16 @@ static int perf_event_mmap_match(struct perf_event *event,
struct vm_area_struct *vma = mmap_event->vma;
int executable = vma->vm_flags & VM_EXEC;
- return (!executable && event->attr.mmap_data) ||
- (executable && (event->attr.mmap || event->attr.mmap2));
+ if (executable)
+ return event->attr.mmap || event->attr.mmap2;
+
+ if (!event->attr.mmap_data)
+ return 0;
+
+ if (event->attr.mmap_bpf && !bpf_mmap_handler(event, vma))
+ return 0;
+
+ return 1;
}
static void perf_event_mmap_output(struct perf_event *event,
@@ -10878,6 +10889,58 @@ static int bpf_overflow_handler(struct perf_event *event,
return ret;
}
+static int bpf_mmap_handler(struct perf_event *event,
+ struct vm_area_struct *vma)
+{
+ struct pt_regs caller_regs = {};
+ struct pt_regs *regs = &caller_regs;
+ struct perf_sample_data data = {
+ .addr = vma->vm_start,
+ .period = vma->vm_end - vma->vm_start,
+ .sample_flags = PERF_SAMPLE_ADDR | PERF_SAMPLE_PERIOD,
+ };
+ struct bpf_perf_event_data_kern ctx = {
+ .data = &data,
+ .event = event,
+ .vma = vma,
+ };
+ struct bpf_prog *prog;
+ int ret = 0;
+
+ perf_fetch_caller_regs(regs);
+ ctx.regs = perf_arch_bpf_user_pt_regs(regs);
+
+ preempt_disable();
+ if (unlikely(__this_cpu_inc_return(bpf_prog_active) != 1))
+ goto out;
+ rcu_read_lock();
+ /* Pairs with smp_store_release() in perf_event_set_bpf_handler() */
+ prog = smp_load_acquire(&event->prog);
+ if (prog && prog->type == BPF_PROG_TYPE_PERF_EVENT) {
+ if (prog->call_get_stack &&
+ (event->attr.sample_type & PERF_SAMPLE_CALLCHAIN)) {
+ data.callchain = get_perf_callchain(regs,
+ !event->attr.exclude_callchain_kernel,
+ !event->attr.exclude_callchain_user &&
+ is_user_task(current) && current->mm,
+ event->attr.sample_max_stack,
+ event->ctx->task &&
+ event->ctx->task != current,
+ true, 0);
+ if (!data.callchain)
+ data.callchain = &__empty_callchain;
+ data.sample_flags |= PERF_SAMPLE_CALLCHAIN;
+ }
+ ret = bpf_prog_run(prog, &ctx);
+ }
+ rcu_read_unlock();
+out:
+ __this_cpu_dec(bpf_prog_active);
+ preempt_enable();
+
+ return ret;
+}
+
static inline int perf_event_set_bpf_handler(struct perf_event *event,
struct bpf_prog *prog,
u64 bpf_cookie)
@@ -10934,6 +10997,12 @@ static inline int bpf_overflow_handler(struct perf_event *event,
return 1;
}
+static inline int bpf_mmap_handler(struct perf_event *event,
+ struct vm_area_struct *vma)
+{
+ return 1;
+}
+
static inline int perf_event_set_bpf_handler(struct perf_event *event,
struct bpf_prog *prog,
u64 bpf_cookie)
@@ -10978,8 +11047,8 @@ static int __perf_event_overflow(struct perf_event *event,
if (event->attr.aux_pause)
perf_event_aux_pause(event->aux_event, true);
- if (!perf_event_is_tracing(event) && READ_ONCE(event->prog) &&
- !bpf_overflow_handler(event, data, regs))
+ if (!event->attr.mmap_bpf && !perf_event_is_tracing(event) &&
+ READ_ONCE(event->prog) && !bpf_overflow_handler(event, data, regs))
goto out;
/*
@@ -11917,6 +11986,9 @@ static int __perf_event_set_bpf_prog(struct perf_event *event,
if (!perf_event_is_tracing(event))
return perf_event_set_bpf_handler(event, prog, bpf_cookie);
+ if (event->attr.mmap_bpf)
+ return -EINVAL;
+
is_kprobe = event->tp_event->flags & TRACE_EVENT_FL_KPROBE;
is_uprobe = event->tp_event->flags & TRACE_EVENT_FL_UPROBE;
is_tracepoint = event->tp_event->flags & TRACE_EVENT_FL_TRACEPOINT;
@@ -13958,6 +14030,13 @@ static int perf_copy_attr(struct perf_event_attr __user *uattr,
(attr->sample_type & PERF_SAMPLE_WEIGHT_STRUCT))
return -EINVAL;
+#if !defined(CONFIG_BPF_SYSCALL) || !defined(CONFIG_EVENT_TRACING)
+ if (attr->mmap_bpf)
+ return -EOPNOTSUPP;
+#endif
+ if (attr->mmap_bpf && !attr->mmap_data)
+ return -EINVAL;
+
if (!attr->inherit && attr->inherit_thread)
return -EINVAL;
--
2.56.1.915.g69d5ec7298-goog