All of lore.kernel.org
 help / color / mirror / Atom feed
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>,
	 Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>
Cc: Marc Zyngier <maz@kernel.org>,
	Alexei Starovoitov <ast@kernel.org>,
	linux-perf-users@vger.kernel.org,  kvm@vger.kernel.org,
	bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
	 Ian Rogers <irogers@google.com>
Subject: [RFC PATCH 02/16] perf/core, bpf: Support BPF filtering of mmap_data events
Date: Tue,  1 Dec 2026 15:21:02 -0800	[thread overview]
Message-ID: <20261201232116.1988791-3-irogers@google.com> (raw)
In-Reply-To: <20261201232116.1988791-1-irogers@google.com>

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@google.com>
---
 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


  parent reply	other threads:[~2026-12-01 23:21 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 23:21 [RFC PATCH 00/16] perf, kvm: Add PERF_SAMPLE_HOST_IP for host-side guest symbolization Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 01/16] perf/core: Add PERF_SAMPLE_HOST_IP sample format Ian Rogers
2026-12-01 23:21 ` Ian Rogers [this message]
2026-12-01 23:21 ` [RFC PATCH 03/16] KVM: Add NMI-safe guest-to-host IP translation infrastructure Ian Rogers
2026-12-01 23:40   ` sashiko-bot
2026-12-02  8:29   ` Christian Borntraeger
2026-12-01 23:21 ` [RFC PATCH 04/16] KVM: x86: Implement kvm_arch_vcpu_get_ip_gpa() Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 05/16] KVM: arm64: " Ian Rogers
2026-12-01 23:41   ` sashiko-bot
2026-12-01 23:21 ` [RFC PATCH 06/16] KVM: riscv: " Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 07/16] KVM: loongarch: " Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 08/16] tools headers UAPI: Sync linux/perf_event.h with the kernel sources Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 09/16] perf tools: Add PERF_SAMPLE_HOST_IP parsing, synthesis, and record --host-ip Ian Rogers
2026-12-01 23:36   ` sashiko-bot
2026-12-01 23:21 ` [RFC PATCH 10/16] perf symbol: Support host IP resolution and guest fallback symbolization Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 11/16] perf report, script: Add host_ip, host_dso, and host_sym fields Ian Rogers
2026-12-01 23:41   ` sashiko-bot
2026-12-01 23:21 ` [RFC PATCH 12/16] perf record: Synthesize hypervisor file-backed data mappings for --host-ip Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 13/16] perf record: Filter runtime hypervisor data mmaps with BPF " Ian Rogers
2026-12-01 23:43   ` sashiko-bot
2026-12-01 23:21 ` [RFC PATCH 14/16] perf bpf-filter: Support host_ip in BPF sample filters Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 15/16] perf aslr: Anonymize sampled host_ip values Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 16/16] perf test: Add PERF_SAMPLE_HOST_IP symbolization, sorting, and ASLR tests Ian Rogers
2026-12-01 23:40   ` sashiko-bot
2026-12-01 23:43 ` [RFC PATCH 00/16] perf, kvm: Add PERF_SAMPLE_HOST_IP for host-side guest symbolization 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=20261201232116.1988791-3-irogers@google.com \
    --to=irogers@google.com \
    --cc=acme@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-perf-users@vger.kernel.org \
    --cc=maz@kernel.org \
    --cc=mingo@redhat.com \
    --cc=namhyung@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=seanjc@google.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.