Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.71 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796167315; b=EkOaaaROPaBGCRdEc6k9EL3q/Bkpiodlv6BbVdx4MvwThxMXnDlca7aomJXcOZyMuCkE pqjDEfbKui/QTkoNPzhCOR1bJ6VI/OJeNrMpkWwpKP5bESoWA7hh/kP3eCjYXfsMxVMEd vWtxamnxjKP3fWmd1rTcI7Xkw+TDyFsb344WtEBK+GnToT76De9+mTHn7Eu2+JK9EiEp8 ukDE8CjU+Zw5Wy8uy2UAMF2AJSpPy+V+ow7uPd/OqJEiVIrR/V767aQEjeQmQ7mhKgHtt 31zSxiRxF4oopK2vtQf6SRKeq6ju1OtbNY5fhGqzw9RCQiDY6bRqCRb/BrK9B5Rj4rg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796167315; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Received:Date:In-Reply-To: Mime-Version:References:X-Mailer:Message-ID:Subject:From:To:Cc: Content-Type; bh=fy7C1PPsILqt7pK2l0MarybA2eU4Ww4m9bl46QEQbPM=; b=MWN2viqne9EJUxzLwizXZ/O/7YW13D73lUVAhscNmv74xJ47a4nFylhAgsKYxYPiD2lY hk0GilzgpejbXBlzgrcqQpQn90jLitUPsYBrR8TZvr9zPhpK7e8T1TLFFkdG8E+7UAktc i/bWmPudVmb4pgKaJM+8tysbz4L7t3mFrlHsNfzgNoZG7TjXglCtya8mI5FgMy8vErgMk SfPacQ2SHaZlWUJOSjEl2gjDQgbCLKF4W/RbZ6LHpiieOBXcpNRgbk86foxDdArqrUopj ZPxpnAJ90n9JcbXT6w8eAkKPpOon5rgPn8hgApmR+LTkdmIxbPW0kBLpEc7DUuQ8nWw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=google.com; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com; dkim=pass header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=dv5Dvvy0; arc=none smtp.remote-ip=209.85.210.71 Received: from mail-ot1-f71.google.com (mail-ot1-f71.google.com [209.85.210.71]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 44B6760A6F6B for ; Tue, 1 Dec 2026 23:21:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 44B6760A6F6B Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--irogers.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.a=rsa-sha256 header.s=20260707 header.b=dv5Dvvy0 Received: by mail-ot1-f71.google.com with SMTP id 46e09a7af769-8628955a085so12056514a34.3 for ; Tue, 01 Dec 2026 15:21:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20260707; t=1796167314; x=1796772114; darn=vger.kernel.org; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=fy7C1PPsILqt7pK2l0MarybA2eU4Ww4m9bl46QEQbPM=; b=dv5Dvvy0F00o4ejDbuSJ3g8vK/2KMVOkbSI+nq0uuXg8jLk0nRnVFzNSp2v2w6WZKY KNc9RhNUVqsGwmfX/t4qLigyDpBYJ5wAKFRddGSRk0w8+/TimjN+kiN9A+dIAVvIyXWk a/cwWhuc/CWXIRKzfOD1DN8vPFx+8v55RKpLwVskf/DjbBZmajUWUVJWpoj0F9ZgHJjo qDFxHfnKK5JzcMYFyO4r3gb8jzuGguDSBgYDhi7OqckGwmJphXoKvFtsODF4Ie24THZC PoXcfELa1E0T1VmCL10A3DLmoV4eji5aQU9/Q49PSsm+DtcpoFX2C3WlKX5kkr6NdUlE 2M1A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796167314; x=1796772114; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=fy7C1PPsILqt7pK2l0MarybA2eU4Ww4m9bl46QEQbPM=; b=BVar1rTBiHHdzFozJI4pLidvvY5mowhc1ZqQLn33D2LbNliI43ZlNNUoqbl3nE5Tdk 4uhXaoBEgq372S1PYX67A2UmbZXfxL+z7wpB+oe9pTAky6rUKX5hlCzrpnFWFD+UKQBb bBRDooKU70wV8VthW/8Va4ZXKPWfNl6clDRIvDz13UmYjNZwfiLvSX/53bo4EfcUUdb2 zzhBTBlpqtJeKJ9JNoNGzT/wq23fHM6rcvIqu9Wx1nDvJe0bvEL5tXpUYLeqyl/fERTL bc9u6d65I/kF5/lb7FKZxyDbx/HUDJ3D0tJ7rYEZqJ9Y8eSSlgOYxVy1wpvMcLWmRFrL izVw== X-Forwarded-Encrypted: i=1; AL+hErGD0tsIExYm6Z5AsAfgEVgsqmkTHJ5qMgjMs0bGbH1WQ8cJanLEZZHrC5Npi4ImZ6uuazg=@vger.kernel.org X-Gm-Message-State: APuU4Z8pytltb7DKJRZkonm5HOrDhsExr+mUDjeD9OgJPBNEKzFk7nx6 K+dHZLosI9YPlIRm2sxBgVRJradn9X1zz/4rzGCyNndVDQjoLScy/MeVB6w5rxhlm0bX6/GIg6t Fjnh0FKR0mw== X-Received: from ooi25.prod.google.com ([2002:a05:6820:4df9:b0:715:1fca:76c]) (user=irogers job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:2488:b0:71a:f390:ae4d with SMTP id 006d021491bc7-71b3958da33mr24458574eaf.75.1796167313882; Tue, 01 Dec 2026 15:21:53 -0800 (PST) Date: Tue, 1 Dec 2026 15:21:12 -0800 In-Reply-To: <20261201232116.1988791-1-irogers@google.com> X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list Mime-Version: 1.0 References: <20261201232116.1988791-1-irogers@google.com> X-Mailer: git-send-email 2.56.1.915.g69d5ec7298-goog Message-ID: <20261201232116.1988791-13-irogers@google.com> Subject: [RFC PATCH 12/16] perf record: Synthesize hypervisor file-backed data mappings for --host-ip From: Ian Rogers To: Peter Zijlstra , Ingo Molnar , Arnaldo Carvalho de Melo , Namhyung Kim , Sean Christopherson , Paolo Bonzini Cc: Marc Zyngier , Alexei Starovoitov , linux-perf-users@vger.kernel.org, kvm@vger.kernel.org, bpf@vger.kernel.org, linux-kernel@vger.kernel.org, Ian Rogers Content-Type: text/plain; charset="UTF-8" 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 --- 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 +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#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