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 13/16] perf record: Filter runtime hypervisor data mmaps with BPF for --host-ip
Date: Tue, 1 Dec 2026 15:21:13 -0800 [thread overview]
Message-ID: <20261201232116.1988791-14-irogers@google.com> (raw)
In-Reply-To: <20261201232116.1988791-1-irogers@google.com>
To capture non-executable file-backed mappings created after recording
starts (including newly launched hypervisor processes) without flooding
the perf ring buffer with system-wide anonymous data mappings, add the
kvm_mmap_filter BPF skeleton and attach it via attr.mmap_bpf to a
dedicated tracking dummy event in perf record --host-ip.
The BPF program matches linked regular-file VMAs belonging to either
discovered hypervisor PIDs or processes with active MMU notifier
subscriptions (such as KVM), and gracefully falls back if the running
kernel does not support attr.mmap_bpf.
Signed-off-by: Ian Rogers <irogers@google.com>
---
tools/perf/builtin-record.c | 20 ++-
tools/perf/util/Build | 2 +
tools/perf/util/bpf_kvm_mmap.c | 120 ++++++++++++++++++
.../perf/util/bpf_skel/kvm_mmap_filter.bpf.c | 79 ++++++++++++
tools/perf/util/bpf_skel/vmlinux/vmlinux.h | 24 ++++
tools/perf/util/kvm-mmap-filter.h | 30 +++++
6 files changed, 274 insertions(+), 1 deletion(-)
create mode 100644 tools/perf/util/bpf_kvm_mmap.c
create mode 100644 tools/perf/util/bpf_skel/kvm_mmap_filter.bpf.c
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 5e11ccda8c6d..1807ac3d8cc8 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -937,7 +937,7 @@ static int record__config_tracking_events(struct record *rec)
* delay of waiting or event synthesis.
*/
if (opts->target.initial_delay || target__has_cpu(&opts->target) ||
- perf_pmus__num_core_pmus() > 1) {
+ perf_pmus__num_core_pmus() > 1 || opts->sample_host_ip) {
/*
* User space tasks can migrate between CPUs, so when tracing
* selected CPUs, sideband for all CPUs is still needed.
@@ -949,6 +949,13 @@ static int record__config_tracking_events(struct record *rec)
if (!evsel)
return -ENOMEM;
+ if (opts->sample_host_ip && !list_empty(&evsel->bpf_filters)) {
+ evsel = evlist__add_aux_dummy(evlist, system_wide);
+ if (!evsel)
+ return -ENOMEM;
+ evlist__set_tracking_event(evlist, evsel);
+ }
+
/*
* Enable the tracking event when the process is forked for
* initial_delay, immediately for system wide.
@@ -958,6 +965,9 @@ static int record__config_tracking_events(struct record *rec)
evsel->core.attr.enable_on_exec = 1;
else
evsel->immediate = 1;
+
+ if (opts->sample_host_ip && !opts->record_data_mmap)
+ perf_kvm_mmap_filter__prepare(evsel);
}
return 0;
@@ -1378,6 +1388,8 @@ static int record__open(struct record *rec)
if (evsel__open(pos, pos->core.cpus, pos->core.threads) < 0) {
bool report_error = true;
+ if (perf_kvm_mmap_filter__fallback(pos, errno, opts->record_data_mmap))
+ goto try_again;
if (evsel__fallback(pos, &opts->target, errno, msg, sizeof(msg))) {
if (verbose > 0)
ui__warning("%s\n", msg);
@@ -1474,6 +1486,11 @@ static int record__open(struct record *rec)
goto out;
}
+ if (perf_kvm_mmap_filter__apply()) {
+ rc = -1;
+ goto out;
+ }
+
rc = record__mmap(rec);
if (rc)
goto out;
@@ -4431,6 +4448,7 @@ int cmd_record(int argc, const char **argv)
err = __cmd_record(&record, argc, argv);
out:
+ perf_kvm_mmap_filter__destroy();
record__free_thread_masks(rec, rec->nr_threads);
rec->nr_threads = 0;
symbol__exit();
diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index fd34879d45ba..e48edc11eb21 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -175,6 +175,7 @@ perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_counter.o
perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_counter_cgroup.o
perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_ftrace.o
perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_off_cpu.o
+perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf_kvm_mmap.o
perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf-filter.o
perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf-filter-flex.o
perf-util-$(CONFIG_PERF_BPF_SKEL) += bpf-filter-bison.o
@@ -451,6 +452,7 @@ $(OUTPUT)util/bpf_ftrace.o: $(SKEL_OUT)/func_latency.skel.h
$(OUTPUT)util/bpf-filter.o: $(SKEL_OUT)/sample_filter.skel.h
$(OUTPUT)util/bpf_kwork_top.o: $(SKEL_OUT)/kwork_top.skel.h
$(OUTPUT)util/bpf_off_cpu.o: $(SKEL_OUT)/off_cpu.skel.h
+$(OUTPUT)util/bpf_kvm_mmap.o: $(SKEL_OUT)/kvm_mmap_filter.skel.h
$(OUTPUT)util/bpf-trace-summary.o: $(SKEL_OUT)/syscall_summary.skel.h
$(OUTPUT)util/bpf_counter_cgroup.o: $(SKEL_OUT)/bperf_cgroup.skel.h
$(OUTPUT)util/bpf_trace_augment.o: $(SKEL_OUT)/augmented_raw_syscalls.skel.h
diff --git a/tools/perf/util/bpf_kvm_mmap.c b/tools/perf/util/bpf_kvm_mmap.c
new file mode 100644
index 000000000000..ca96edc86777
--- /dev/null
+++ b/tools/perf/util/bpf_kvm_mmap.c
@@ -0,0 +1,120 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "util/kvm-mmap-filter.h"
+
+#include <errno.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+
+#include <bpf/bpf.h>
+#include <bpf/libbpf.h>
+#include <internal/xyarray.h>
+#include <linux/perf_event.h>
+
+#include "util/bpf-utils.h"
+#include "util/cap.h"
+#include "util/debug.h"
+#include "util/evsel.h"
+#include "util/intlist.h"
+
+#include "bpf_skel/kvm_mmap_filter.skel.h"
+
+#define FD(e, x, y) (*(int *)xyarray__entry((e)->core.fd, x, y))
+
+static struct kvm_mmap_filter_bpf *skel;
+static struct evsel *filter_evsel;
+
+int perf_kvm_mmap_filter__prepare(struct evsel *evsel)
+{
+ struct intlist *pids;
+ struct int_node *pos;
+ int map_fd;
+
+ if (!evsel || skel)
+ return 0;
+
+ if (!perf_cap__capable(CAP_BPF) && !perf_cap__capable(CAP_SYS_ADMIN)) {
+ pr_debug("Insufficient capabilities to load kvm_mmap_filter BPF skeleton\n");
+ return 0;
+ }
+
+ skel = kvm_mmap_filter_bpf__open_and_load();
+ if (!skel) {
+ pr_debug("Failed to load kvm_mmap_filter BPF skeleton; runtime hypervisor data mmaps will not be filtered via BPF\n");
+ return 0;
+ }
+
+ map_fd = bpf_map__fd(skel->maps.hypervisor_pids);
+ pids = intlist__new(NULL);
+ if (pids) {
+ if (perf_kvm_mmap_filter__discover_pids(pids) == 0) {
+ intlist__for_each_entry(pos, pids) {
+ __u32 tgid = (__u32)pos->i;
+ __u8 one = 1;
+
+ bpf_map_update_elem(map_fd, &tgid, &one, BPF_ANY);
+ }
+ }
+ intlist__delete(pids);
+ }
+
+ filter_evsel = evsel;
+ evsel->core.attr.mmap_data = 1;
+ evsel->core.attr.mmap_bpf = 1;
+ return 0;
+}
+
+bool perf_kvm_mmap_filter__fallback(struct evsel *evsel, int err, bool keep_mmap_data)
+{
+ if (!skel || evsel != filter_evsel || !evsel->core.attr.mmap_bpf)
+ return false;
+
+ if (err != EINVAL && err != EOPNOTSUPP)
+ return false;
+
+ pr_debug("Kernel does not support attr.mmap_bpf, disabling runtime BPF hypervisor mmap filter\n");
+ evsel->core.attr.mmap_bpf = 0;
+ if (!keep_mmap_data)
+ evsel->core.attr.mmap_data = 0;
+
+ perf_kvm_mmap_filter__destroy();
+ return true;
+}
+
+int perf_kvm_mmap_filter__apply(void)
+{
+ int prog_fd;
+
+ if (!skel || !filter_evsel || !filter_evsel->core.fd)
+ return 0;
+
+ prog_fd = bpf_program__fd(skel->progs.perf_kvm_mmap_filter);
+ if (prog_fd < 0)
+ return -EINVAL;
+
+ for (int x = 0; x < xyarray__max_x(filter_evsel->core.fd); x++) {
+ for (int y = 0; y < xyarray__max_y(filter_evsel->core.fd); y++) {
+ int fd = FD(filter_evsel, x, y);
+
+ if (fd < 0)
+ continue;
+
+ if (ioctl(fd, PERF_EVENT_IOC_SET_BPF, prog_fd) < 0) {
+ int ret = -errno;
+
+ pr_err("Failed to attach hypervisor mmap BPF filter: %m\n");
+ return ret;
+ }
+ }
+ }
+
+ return 0;
+}
+
+void perf_kvm_mmap_filter__destroy(void)
+{
+ kvm_mmap_filter_bpf__destroy(skel);
+ skel = NULL;
+ filter_evsel = NULL;
+}
diff --git a/tools/perf/util/bpf_skel/kvm_mmap_filter.bpf.c b/tools/perf/util/bpf_skel/kvm_mmap_filter.bpf.c
new file mode 100644
index 000000000000..3f089412fa59
--- /dev/null
+++ b/tools/perf/util/bpf_skel/kvm_mmap_filter.bpf.c
@@ -0,0 +1,79 @@
+// SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_core_read.h>
+
+#ifndef S_IFMT
+#define S_IFMT 00170000
+#define S_IFREG 0100000
+#endif
+
+#define MAX_HYPERVISOR_PIDS 4096
+
+struct {
+ __uint(type, BPF_MAP_TYPE_HASH);
+ __type(key, __u32);
+ __type(value, __u8);
+ __uint(max_entries, MAX_HYPERVISOR_PIDS);
+} hypervisor_pids SEC(".maps");
+
+void *bpf_cast_to_kern_ctx(void *) __ksym;
+
+/*
+ * Filter non-executable mmap_data events so only linked regular-file mappings
+ * (such as direct-access guest images and firmware ROMs) in hypervisor
+ * processes are emitted into the perf ring buffer.
+ */
+SEC("perf_event")
+int perf_kvm_mmap_filter(void *ctx)
+{
+ struct bpf_perf_event_data_kern *kctx;
+ struct vm_area_struct *vma;
+ struct file *file;
+ struct inode *inode;
+ struct mm_struct *mm;
+ unsigned int nlink;
+ unsigned short mode;
+ __u32 tgid;
+
+ kctx = bpf_cast_to_kern_ctx(ctx);
+ if (!bpf_core_field_exists(kctx->vma))
+ return 0;
+
+ vma = kctx->vma;
+ if (!vma)
+ return 0;
+
+ file = BPF_CORE_READ(vma, vm_file);
+ if (!file)
+ return 0;
+
+ inode = BPF_CORE_READ(file, f_inode);
+ if (!inode)
+ return 0;
+
+ mode = BPF_CORE_READ(inode, i_mode);
+ if ((mode & S_IFMT) != S_IFREG)
+ return 0;
+
+ nlink = BPF_CORE_READ(inode, __i_nlink);
+ if (nlink == 0)
+ return 0;
+
+ tgid = bpf_get_current_pid_tgid() >> 32;
+ if (bpf_map_lookup_elem(&hypervisor_pids, &tgid))
+ return 1;
+
+ mm = BPF_CORE_READ(vma, vm_mm);
+ if (mm && bpf_core_field_exists(mm->notifier_subscriptions) &&
+ BPF_CORE_READ(mm, notifier_subscriptions) != NULL) {
+ __u8 one = 1;
+
+ bpf_map_update_elem(&hypervisor_pids, &tgid, &one, BPF_ANY);
+ return 1;
+ }
+
+ return 0;
+}
+
+char LICENSE[] SEC("license") = "Dual BSD/GPL";
diff --git a/tools/perf/util/bpf_skel/vmlinux/vmlinux.h b/tools/perf/util/bpf_skel/vmlinux/vmlinux.h
index 94e42b022ab1..7d07c565762c 100644
--- a/tools/perf/util/bpf_skel/vmlinux/vmlinux.h
+++ b/tools/perf/util/bpf_skel/vmlinux/vmlinux.h
@@ -101,8 +101,31 @@ struct css_set {
struct cgroup *dfl_cgrp;
} __attribute__((preserve_access_index));
+struct mmu_notifier_subscriptions;
+
struct mm_struct {
struct rw_semaphore mmap_lock;
+ struct mmu_notifier_subscriptions *notifier_subscriptions;
+} __attribute__((preserve_access_index));
+
+struct inode {
+ unsigned short i_mode;
+ union {
+ const unsigned int i_nlink;
+ unsigned int __i_nlink;
+ };
+} __attribute__((preserve_access_index));
+
+struct file {
+ struct inode *f_inode;
+} __attribute__((preserve_access_index));
+
+struct vm_area_struct {
+ struct mm_struct *vm_mm;
+ union {
+ const unsigned long vm_flags;
+ };
+ struct file *vm_file;
} __attribute__((preserve_access_index));
struct task_struct {
@@ -193,6 +216,7 @@ struct perf_event {
struct bpf_perf_event_data_kern {
struct perf_sample_data *data;
struct perf_event *event;
+ struct vm_area_struct *vma;
} __attribute__((preserve_access_index));
/*
diff --git a/tools/perf/util/kvm-mmap-filter.h b/tools/perf/util/kvm-mmap-filter.h
index c269a862f7b2..56b49442567d 100644
--- a/tools/perf/util/kvm-mmap-filter.h
+++ b/tools/perf/util/kvm-mmap-filter.h
@@ -2,8 +2,10 @@
#ifndef PERF_UTIL_KVM_MMAP_FILTER_H
#define PERF_UTIL_KVM_MMAP_FILTER_H
+#include <stdbool.h>
#include "util/synthetic-events.h"
+struct evsel;
struct intlist;
struct machine;
struct perf_tool;
@@ -13,4 +15,32 @@ int perf_kvm_mmap_filter__synthesize_maps(const struct perf_tool *tool,
struct machine *machine,
perf_event__handler_t process);
+#ifdef HAVE_BPF_SKEL
+int perf_kvm_mmap_filter__prepare(struct evsel *evsel);
+bool perf_kvm_mmap_filter__fallback(struct evsel *evsel, int err, bool keep_mmap_data);
+int perf_kvm_mmap_filter__apply(void);
+void perf_kvm_mmap_filter__destroy(void);
+#else
+static inline int perf_kvm_mmap_filter__prepare(struct evsel *evsel __maybe_unused)
+{
+ return 0;
+}
+
+static inline bool perf_kvm_mmap_filter__fallback(struct evsel *evsel __maybe_unused,
+ int err __maybe_unused,
+ bool keep_mmap_data __maybe_unused)
+{
+ return false;
+}
+
+static inline int perf_kvm_mmap_filter__apply(void)
+{
+ return 0;
+}
+
+static inline void perf_kvm_mmap_filter__destroy(void)
+{
+}
+#endif /* HAVE_BPF_SKEL */
+
#endif /* PERF_UTIL_KVM_MMAP_FILTER_H */
--
2.56.1.915.g69d5ec7298-goog
next prev 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 ` [RFC PATCH 02/16] perf/core, bpf: Support BPF filtering of mmap_data events Ian Rogers
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 ` Ian Rogers [this message]
2026-12-01 23:43 ` [RFC PATCH 13/16] perf record: Filter runtime hypervisor data mmaps with BPF " 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-14-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.