Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [RFC PATCH 00/16] perf, kvm: Add PERF_SAMPLE_HOST_IP for host-side guest symbolization
@ 2026-12-01 23:21 Ian Rogers
  2026-12-01 23:21 ` [RFC PATCH 01/16] perf/core: Add PERF_SAMPLE_HOST_IP sample format Ian Rogers
                   ` (15 more replies)
  0 siblings, 16 replies; 23+ messages in thread
From: Ian Rogers @ 2026-12-01 23:21 UTC (permalink / raw)
  To: Peter Zijlstra, Ingo Molnar, Arnaldo Carvalho de Melo,
	Namhyung Kim, Sean Christopherson, Paolo Bonzini
  Cc: Marc Zyngier, Alexei Starovoitov, linux-perf-users, kvm, bpf,
	linux-kernel, Ian Rogers

This RFC proposes PERF_SAMPLE_HOST_IP along with BPF-filtered mmap_data
recording so host-side perf can symbolize KVM guest code directly from
host file-backed / DAX mappings without needing guest symbol state.

Note: This work is incomplete (for example, guest callchain / stack
trace support is missing, as described below). It is being posted early
as an RFC because combining host-translated guest IPs with DAX /
host-mapped guest binaries is a novel approach, and I would like
feedback on the overall idea, UAPI, and kernel/KVM/BPF design before
investing further.

Motivation: Host IPs + DAX / file-backed guest mappings
=======================================================

Profiling KVM guests from the host currently records guest virtual IPs
(PERF_SAMPLE_IP), which can only be symbolized if the host copies
/proc/kallsyms, /proc/modules, and userspace maps/binaries out of each
guest. That is difficult or impossible for opaque, ephemeral, or
multi-tenant VMs.

When guest executables, shared libraries, or kernel/rootfs images are
backed by host files mapped into the hypervisor address space (for
example via DAX / virtio-pmem or direct file-backed guest memory), the
host already knows which backing file and offset correspond to each
guest page. By translating the guest instruction pointer (GVA -> GPA ->
HVA in the hypervisor process) at PMI time into PERF_SAMPLE_HOST_IP and
recording the hypervisor's non-executable file-backed mappings, host
perf report/script can resolve guest samples to host DSOs, symbols, and
build-IDs with no guest cooperation.

Known gaps & open questions for discussion
==========================================

1. Missing guest callchain support:
   Perf currently bails out of perf_callchain_{kernel,user}() when
   perf_guest_state() is active on all architectures, so this RFC only
   translates the single sampled guest IP. Supporting host-IP
   translation for guest stack traces (where a per-sample on-stack page
   translation cache could amortize walks across frames in the same
   page) is left as future work.

2. No cross-PMI translation caching:
   Each PMI walks guest page tables fresh. Under hardware TDP
   (EPT/NPT/Stage-2), guest page-table updates, INVLPG/TLBI, and CR3
   loads do not trap to KVM, so caching GVA -> HVA across PMIs risks
   stale symbol attribution on guest remaps or THP splits.

3. UAPI for host IP sampling (Patch 1):
   Is PERF_SAMPLE_HOST_IP alongside PERF_SAMPLE_IP (placed before
   PERF_SAMPLE_AUX so AUX remains last in PERF_RECORD_SAMPLE) the right
   UAPI, or would maintainers prefer a different representation?

4. Filtering non-executable hypervisor mappings via attr.mmap_bpf
   (Patch 2):
   Hypervisors map guest memory without PROT_EXEC, so PERF_RECORD_MMAP
   misses DAX/file-backed guest images, while enabling attr.mmap_data
   system-wide floods the ring buffer with anonymous data mappings from
   every process. Patch 2 adds attr.mmap_bpf to run an attached
   BPF_PROG_TYPE_PERF_EVENT program (with access to the target
   vm_area_struct) as a per-event filter in perf_event_mmap_match().
   Because it attaches to a non-tracing event's own event->prog and is
   evaluated per sideband perf_event in perf_iterate_sb(), separate
   perf commands do not filter each other's mmap records. However,
   setting attr.mmap_data still incurs system-wide d_path() preparation
   in perf_event_mmap_event() before per-event filtering, and reusing
   event->prog means a single perf_event cannot carry both an overflow
   BPF filter and an mmap_bpf filter (perf record puts mmap_bpf on a
   dedicated dummy tracking event). Is BPF filtering of mmap_data the
   right mechanism here?

5. NMI-safe KVM guest-to-host IP translation (Patches 3-7):
   Patch 3 pairs synchronize_srcu_expedited(&kvm->srcu) with
   synchronize_rcu_expedited() in kvm_swap_active_memslots() for
   lockless NMI-safe memslot lookup and adds
   kvm_vcpu_read_guest_nofault() using copy_from_user_nofault().
   Patches 4-7 implement kvm_arch_vcpu_get_ip_gpa() for x86 (including
   nested EPT/NPT), arm64, riscv, and loongarch. Feedback on the KVM
   locking/RCU tradeoffs and per-arch walkers is especially welcome.

Series overview
===============

- Patch 01: Add PERF_SAMPLE_HOST_IP and optional ->get_host_ip() on
  perf_guest_info_callbacks.
- Patch 02: Add attr.mmap_bpf to allow BPF filtering of non-executable
  mmap_data events.
- Patch 03: Add common NMI-safe KVM guest-to-host IP translation
  infrastructure (kvm_vcpu_read_guest_nofault(), kvm_guest_get_host_ip()).
- Patch 04: Implement kvm_arch_vcpu_get_ip_gpa() on x86.
- Patch 05: Implement kvm_arch_vcpu_get_ip_gpa() on arm64.
- Patch 06: Implement kvm_arch_vcpu_get_ip_gpa() on riscv.
- Patch 07: Implement kvm_arch_vcpu_get_ip_gpa() on loongarch.
- Patch 08: Sync tools/include/uapi/linux/perf_event.h.
- Patch 09: Add PERF_SAMPLE_HOST_IP parsing, synthesis, build-id hit
  marking, perf record --host-ip, and sample-parsing test updates.
- Patch 10: Resolve sample->host_ip against machines->host and fall
  back to host symbolization for unresolved guest samples.
- Patch 11: Add host_ip, host_dso, and host_sym sort keys in perf
  report and output fields in perf script.
- Patch 12: Discover hypervisor processes via /proc and synthesize
  existing non-executable file-backed data mappings for --host-ip.
- Patch 13: Add the kvm_mmap_filter BPF skeleton to filter runtime
  non-executable hypervisor mappings via attr.mmap_bpf.
- Patch 14: Support host_ip in BPF sample filters (--filter) and test.
- Patch 15: Anonymize sampled host_ip values in aslr_tool.
- Patch 16: Add the host-ip-sym C unit test suite for fallback
  symbolization, sorting, and ASLR anonymization.

Prerequisites
=============

This series is based on torvalds/master (19cc08c1a6a6) plus two small
pre-existing fix series posted separately:
- perf/bpf: Fix BPF program inheritance and overflow races
  https://lore.kernel.org/linux-perf-users/20261201071653.827828-1-irogers@google.com/
- perf evsel: Add missing bounds checks in __evsel__parse_sample()
  https://lore.kernel.org/linux-perf-users/20261201071435.824674-1-irogers@google.com/

Development & testing notes
===========================

This prototype, tests, and changelogs were developed with an AI coding
assistant and reviewed with Sashiko.

Tested:
- Build-tested kernel/events/core.o and KVM across x86_64, arm64,
  riscv, loongarch, CONFIG_BPF_SYSCALL=n, and CONFIG_BPF_EVENTS=n.
- Build-tested tools/perf across intermediate split commits and at HEAD
  with BUILD_BPF_SKEL=1 EXTRA_CFLAGS=-DREFCNT_CHECKING=1 and
  BUILD_BPF_SKEL=0 NO_LIBBPF=1, passing
  'perf test "Sample parsing" "PERF_SAMPLE_HOST_IP"'.
- Runtime-tested in x86_64 QEMU/KVM (CONFIG_PROVE_LOCKING=y,
  CONFIG_PROVE_RCU=y): verified 2MB huge-page (797/797) and 4KB 4-level
  (800/800) guest IP-to-HVA translations, plus end-to-end
  'perf record --host-ip' + kvm_mmap_filter capturing a non-executable
  PROT_READ file-backed guest image and symbolizing guest execution in
  perf report/script with zero lockdep/RCU splats.
- Runtime-tested in arm64 QEMU/KVM (VHE EL2, CONFIG_PROVE_LOCKING=y,
  CONFIG_PROVE_RCU=y): verified MMU-off (154/154) and non-identity
  Stage-1 MMU-on (391/391) guest IP-to-HVA translations.

Not runtime-tested:
- riscv and loongarch KVM guest IP translation (compile-tested only).
- x86 nested EPT/NPT guest IP translation (compile-tested only).

Ian Rogers (16):
  perf/core: Add PERF_SAMPLE_HOST_IP sample format
  perf/core, bpf: Support BPF filtering of mmap_data events
  KVM: Add NMI-safe guest-to-host IP translation infrastructure
  KVM: x86: Implement kvm_arch_vcpu_get_ip_gpa()
  KVM: arm64: Implement kvm_arch_vcpu_get_ip_gpa()
  KVM: riscv: Implement kvm_arch_vcpu_get_ip_gpa()
  KVM: loongarch: Implement kvm_arch_vcpu_get_ip_gpa()
  tools headers UAPI: Sync linux/perf_event.h with the kernel sources
  perf tools: Add PERF_SAMPLE_HOST_IP parsing, synthesis, and record
    --host-ip
  perf symbol: Support host IP resolution and guest fallback
    symbolization
  perf report, script: Add host_ip, host_dso, and host_sym fields
  perf record: Synthesize hypervisor file-backed data mappings for
    --host-ip
  perf record: Filter runtime hypervisor data mmaps with BPF for
    --host-ip
  perf bpf-filter: Support host_ip in BPF sample filters
  perf aslr: Anonymize sampled host_ip values
  perf test: Add PERF_SAMPLE_HOST_IP symbolization, sorting, and ASLR
    tests

 arch/arm64/include/asm/kvm_nested.h           |   2 +
 arch/arm64/kvm/arm.c                          |  38 +
 arch/arm64/kvm/at.c                           |  39 +-
 arch/loongarch/kvm/vcpu.c                     | 104 +++
 arch/riscv/kvm/vcpu.c                         | 103 +++
 arch/x86/include/asm/kvm-x86-ops.h            |   1 +
 arch/x86/include/asm/kvm_host.h               |   7 +-
 arch/x86/kvm/mmu.h                            |  14 +-
 arch/x86/kvm/mmu/mmu.c                        |  53 +-
 arch/x86/kvm/mmu/paging_tmpl.h                |  86 +++
 arch/x86/kvm/svm/nested.c                     |  17 +-
 arch/x86/kvm/vmx/main.c                       |   9 +
 arch/x86/kvm/vmx/nested.c                     |   5 +-
 arch/x86/kvm/vmx/vmx.c                        |  16 +
 arch/x86/kvm/vmx/x86_ops.h                    |   1 +
 arch/x86/kvm/x86.c                            |  38 +
 include/linux/kvm_host.h                      |   3 +
 include/linux/perf_event.h                    |  12 +
 include/uapi/linux/perf_event.h               |   7 +-
 kernel/events/core.c                          | 111 ++-
 tools/include/uapi/linux/perf_event.h         |   7 +-
 tools/perf/Documentation/perf-record.txt      |   7 +
 tools/perf/Documentation/perf-report.txt      |   3 +
 tools/perf/Documentation/perf-script.txt      |   4 +
 tools/perf/builtin-record.c                   |  25 +-
 tools/perf/builtin-script.c                   |  48 ++
 tools/perf/tests/Build                        |   1 +
 tools/perf/tests/builtin-test.c               |   1 +
 tools/perf/tests/host-ip-sym.c                | 691 ++++++++++++++++++
 tools/perf/tests/sample-parsing.c             |   7 +-
 tools/perf/tests/shell/record_bpf_filter.sh   |   8 +
 tools/perf/tests/tests.h                      |   1 +
 tools/perf/util/Build                         |   3 +
 tools/perf/util/addr_location.c               |   7 +
 tools/perf/util/addr_location.h               |   4 +
 tools/perf/util/aslr.c                        |  32 +
 tools/perf/util/aslr.h                        |   3 +-
 tools/perf/util/bpf-filter.c                  |   3 +-
 tools/perf/util/bpf-filter.l                  |   1 +
 tools/perf/util/bpf_kvm_mmap.c                | 120 +++
 .../perf/util/bpf_skel/kvm_mmap_filter.bpf.c  |  79 ++
 tools/perf/util/bpf_skel/sample-filter.h      |   3 +-
 tools/perf/util/bpf_skel/sample_filter.bpf.c  |   9 +-
 tools/perf/util/bpf_skel/vmlinux/vmlinux.h    |  25 +
 tools/perf/util/build-id.c                    |  27 +
 tools/perf/util/event.c                       |  58 +-
 tools/perf/util/evsel.c                       |  22 +
 tools/perf/util/evsel.h                       |   1 +
 tools/perf/util/hist.c                        |  48 ++
 tools/perf/util/hist.h                        |   6 +
 tools/perf/util/kvm-mmap-filter.c             | 210 ++++++
 tools/perf/util/kvm-mmap-filter.h             |  46 ++
 tools/perf/util/perf_event_attr_fprintf.c     |   3 +-
 tools/perf/util/record.h                      |   1 +
 tools/perf/util/sample.h                      |   2 +
 tools/perf/util/session.c                     |   3 +
 tools/perf/util/sort.c                        | 114 +++
 tools/perf/util/sort.h                        |   4 +
 tools/perf/util/synthetic-events.c            |   8 +
 virt/kvm/kvm_main.c                           |  64 ++
 60 files changed, 2336 insertions(+), 39 deletions(-)
 create mode 100644 tools/perf/tests/host-ip-sym.c
 create mode 100644 tools/perf/util/bpf_kvm_mmap.c
 create mode 100644 tools/perf/util/bpf_skel/kvm_mmap_filter.bpf.c
 create mode 100644 tools/perf/util/kvm-mmap-filter.c
 create mode 100644 tools/perf/util/kvm-mmap-filter.h


base-commit: 19cc08c1a6a650f8e54c0461ff198be6448f425d
prerequisite-patch-id: 9d5da60cadbe4821cfe47b4ec47d6e2f11d635f1
prerequisite-patch-id: 974971a9ccbd0a396dcab1c1f587d8b807ef4abb
prerequisite-patch-id: 2b50af50ad0619ae3324a7a8932a68561ed55129
-- 
2.56.1.915.g69d5ec7298-goog


^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2026-12-01 23:43 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [RFC PATCH 12/16] perf record: Synthesize hypervisor file-backed data mappings for --host-ip Ian Rogers
2026-12-01 23:21 ` [RFC PATCH 13/16] perf record: Filter runtime hypervisor data mmaps with BPF " 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox