[RFC PATCH 13/16] perf record: Filter runtime hypervisor data mmaps with BPF for --host-ip

From: Ian Rogers

Date: Tue Dec 01 2026 - 18:22:51 EST


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@xxxxxxxxxx>
---
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