[RFC PATCH 12/16] perf record: Synthesize hypervisor file-backed data mappings for --host-ip
From: Ian Rogers
Date: Tue Dec 01 2026 - 18:22:47 EST
Hypervisors map guest images (such as DAX / virtio-pmem or direct
file-backed guest memory) without PROT_EXEC, so normal executable
PERF_RECORD_MMAP synthesis misses them.
Add /proc hypervisor process discovery (using stack-allocated io_dir
with openat()/readlinkat() to check for /dev/kvm or anon_inode:kvm-vm*
descriptors) and synthesize non-executable linked regular-file MMAP2
records for existing hypervisor processes during perf record --host-ip
(when --data-mmap is not already enabled).
Signed-off-by: Ian Rogers <irogers@xxxxxxxxxx>
---
tools/perf/builtin-record.c | 3 +
tools/perf/util/Build | 1 +
tools/perf/util/kvm-mmap-filter.c | 210 ++++++++++++++++++++++++++++++
tools/perf/util/kvm-mmap-filter.h | 16 +++
4 files changed, 230 insertions(+)
create mode 100644 tools/perf/util/kvm-mmap-filter.c
create mode 100644 tools/perf/util/kvm-mmap-filter.h
diff --git a/tools/perf/builtin-record.c b/tools/perf/builtin-record.c
index 4ff929191211..5e11ccda8c6d 100644
--- a/tools/perf/builtin-record.c
+++ b/tools/perf/builtin-record.c
@@ -52,6 +52,7 @@
#include "util/clockid.h"
#include "util/off_cpu.h"
#include "util/bpf-filter.h"
+#include "util/kvm-mmap-filter.h"
#include "util/strbuf.h"
#include "asm/bug.h"
#include "perf.h"
@@ -2221,6 +2222,8 @@ static int record__synthesize(struct record *rec, bool tail)
evlist__core(rec->evlist)->threads,
f, needs_mmap, opts->record_data_mmap,
rec->opts.nr_threads_synthesize);
+ if (!err && needs_mmap && opts->sample_host_ip && !opts->record_data_mmap)
+ err = perf_kvm_mmap_filter__synthesize_maps(tool, machine, f);
}
if (rec->opts.nr_threads_synthesize > 1) {
diff --git a/tools/perf/util/Build b/tools/perf/util/Build
index 2c1f880c4c47..fd34879d45ba 100644
--- a/tools/perf/util/Build
+++ b/tools/perf/util/Build
@@ -130,6 +130,7 @@ perf-util-y += iostat.o
perf-util-y += stream.o
perf-util-$(CONFIG_LIBTRACEEVENT) += kvm-stat.o
perf-util-y += kvm-stat-arch/
+perf-util-y += kvm-mmap-filter.o
perf-util-y += lock-contention.o
perf-util-y += auxtrace.o
perf-util-y += intel-pt-decoder/
diff --git a/tools/perf/util/kvm-mmap-filter.c b/tools/perf/util/kvm-mmap-filter.c
new file mode 100644
index 000000000000..daf29359035b
--- /dev/null
+++ b/tools/perf/util/kvm-mmap-filter.c
@@ -0,0 +1,210 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "util/kvm-mmap-filter.h"
+
+#include <ctype.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <api/fs/fs.h>
+#include <api/io_dir.h>
+#include <linux/kernel.h>
+#include <linux/perf_event.h>
+#include <linux/zalloc.h>
+
+#include "util/debug.h"
+#include "util/event.h"
+#include "util/intlist.h"
+#include "util/machine.h"
+#include "util/tool.h"
+
+static bool pid_has_kvm_fd(int proc_dirfd, const char *pid_name)
+{
+ char fd_subpath[32];
+ struct io_dirent64 *de;
+ struct io_dir fd_dir;
+ bool found = false;
+ int fd_dirfd;
+
+ if (snprintf(fd_subpath, sizeof(fd_subpath), "%s/fd", pid_name) >=
+ (int)sizeof(fd_subpath))
+ return false;
+
+ fd_dirfd = openat(proc_dirfd, fd_subpath,
+ O_CLOEXEC | O_DIRECTORY | O_RDONLY);
+ if (fd_dirfd < 0)
+ return false;
+
+ io_dir__init(&fd_dir, fd_dirfd);
+ while ((de = io_dir__readdir(&fd_dir)) != NULL) {
+ char target[32];
+ ssize_t len;
+
+ if (!isdigit((unsigned char)de->d_name[0]))
+ continue;
+
+ len = readlinkat(fd_dir.dirfd, de->d_name,
+ target, sizeof(target) - 1);
+ if (len <= 0)
+ continue;
+ target[len] = '\0';
+
+ if (!strcmp(target, "/dev/kvm") ||
+ !strncmp(target, "anon_inode:kvm-vm", 17)) {
+ found = true;
+ break;
+ }
+ }
+
+ close(fd_dirfd);
+ return found;
+}
+
+int perf_kvm_mmap_filter__discover_pids(struct intlist *pids)
+{
+ const char *proc_root = procfs__mountpoint();
+ struct io_dirent64 *de;
+ struct io_dir proc_dir;
+ int proc_dirfd;
+
+ if (!proc_root || !*proc_root)
+ proc_root = "/proc";
+
+ proc_dirfd = open(proc_root, O_CLOEXEC | O_DIRECTORY | O_RDONLY);
+ if (proc_dirfd < 0)
+ return -errno;
+
+ io_dir__init(&proc_dir, proc_dirfd);
+ while ((de = io_dir__readdir(&proc_dir)) != NULL) {
+ char *endptr = NULL;
+ long pid;
+
+ if (!isdigit((unsigned char)de->d_name[0]))
+ continue;
+
+ pid = strtol(de->d_name, &endptr, 10);
+ if (!endptr || *endptr != '\0' || pid <= 0 || pid > INT_MAX)
+ continue;
+
+ if (pid_has_kvm_fd(proc_dir.dirfd, de->d_name)) {
+ int err = intlist__add(pids, (int)pid);
+
+ if (err && err != -EEXIST) {
+ close(proc_dirfd);
+ return err;
+ }
+ }
+ }
+
+ close(proc_dirfd);
+ return 0;
+}
+
+struct kvm_mmap_synth_ctx {
+ struct perf_tool tool;
+ const struct perf_tool *orig_tool;
+ perf_event__handler_t orig_process;
+};
+
+static bool is_linked_regular_file_mmap(const char *filename)
+{
+ struct stat st;
+
+ if (!filename || filename[0] != '/' || filename[1] == '/')
+ return false;
+ if (!strncmp(filename, "/memfd:", 7) || !strncmp(filename, "/SYSV", 5))
+ return false;
+ if (stat(filename, &st) != 0)
+ return false;
+
+ return S_ISREG(st.st_mode) && st.st_nlink > 0;
+}
+
+static int kvm_mmap_synth_cb(const struct perf_tool *tool,
+ union perf_event *event,
+ struct perf_sample *sample,
+ struct machine *machine)
+{
+ const struct kvm_mmap_synth_ctx *ctx =
+ container_of(tool, struct kvm_mmap_synth_ctx, tool);
+ const char *filename;
+
+ /*
+ * Executable mappings are already synthesized by normal thread
+ * synthesis; only emit non-executable linked regular-file mappings.
+ */
+ if (!(event->header.misc & PERF_RECORD_MISC_MMAP_DATA))
+ return 0;
+
+ if (event->header.type == PERF_RECORD_MMAP2)
+ filename = event->mmap2.filename;
+ else if (event->header.type == PERF_RECORD_MMAP)
+ filename = event->mmap.filename;
+ else
+ return 0;
+
+ if (!is_linked_regular_file_mmap(filename))
+ return 0;
+
+ return ctx->orig_process(ctx->orig_tool, event, sample, machine);
+}
+
+int perf_kvm_mmap_filter__synthesize_maps(const struct perf_tool *tool,
+ struct machine *machine,
+ perf_event__handler_t process)
+{
+ struct kvm_mmap_synth_ctx ctx = {
+ .orig_tool = tool,
+ .orig_process = process,
+ };
+ struct intlist *pids;
+ struct int_node *pos;
+ union perf_event *event;
+ int err = 0;
+
+ pids = intlist__new(NULL);
+ if (!pids)
+ return -ENOMEM;
+
+ err = perf_kvm_mmap_filter__discover_pids(pids);
+ if (err || intlist__empty(pids)) {
+ intlist__delete(pids);
+ return 0;
+ }
+
+ if (tool)
+ ctx.tool = *tool;
+ else
+ perf_tool__init(&ctx.tool, /*ordered_events=*/false);
+
+ event = zalloc(sizeof(event->mmap2) + machine->id_hdr_size);
+ if (!event) {
+ intlist__delete(pids);
+ return -ENOMEM;
+ }
+
+ intlist__for_each_entry(pos, pids) {
+ pid_t tgid = (pid_t)pos->i;
+
+ err = perf_event__synthesize_mmap_events(&ctx.tool, event,
+ tgid, tgid,
+ kvm_mmap_synth_cb,
+ machine,
+ /*mmap_data=*/true);
+ if (err)
+ pr_debug("Failed to synthesize hypervisor data mmaps for pid %d\n",
+ (int)tgid);
+ err = 0;
+ }
+
+ free(event);
+ intlist__delete(pids);
+ return err;
+}
diff --git a/tools/perf/util/kvm-mmap-filter.h b/tools/perf/util/kvm-mmap-filter.h
new file mode 100644
index 000000000000..c269a862f7b2
--- /dev/null
+++ b/tools/perf/util/kvm-mmap-filter.h
@@ -0,0 +1,16 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef PERF_UTIL_KVM_MMAP_FILTER_H
+#define PERF_UTIL_KVM_MMAP_FILTER_H
+
+#include "util/synthetic-events.h"
+
+struct intlist;
+struct machine;
+struct perf_tool;
+
+int perf_kvm_mmap_filter__discover_pids(struct intlist *pids);
+int perf_kvm_mmap_filter__synthesize_maps(const struct perf_tool *tool,
+ struct machine *machine,
+ perf_event__handler_t process);
+
+#endif /* PERF_UTIL_KVM_MMAP_FILTER_H */
--
2.56.1.915.g69d5ec7298-goog