Kernel KVM virtualization development
 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 12/16] perf record: Synthesize hypervisor file-backed data mappings for --host-ip
Date: Tue,  1 Dec 2026 15:21:12 -0800	[thread overview]
Message-ID: <20261201232116.1988791-13-irogers@google.com> (raw)
In-Reply-To: <20261201232116.1988791-1-irogers@google.com>

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


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

Thread overview: 23+ 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-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 ` Ian Rogers [this message]
2026-12-01 23:21 ` [RFC PATCH 13/16] perf record: Filter runtime hypervisor data mmaps with BPF for --host-ip 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

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-13-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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox