Skip to content

RFC perf, kvm: Add PERF_SAMPLE_HOST_IP for host-side guest symbolization

Ian Rogers bpf kvm linux-perf-users lkml 1 version In review

Get series with b4
b4 am [email protected]

Versions newest first

  1. v1 RFC Latest16 patches 1 replyIn review

Patches in v1 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/16perf/core: Add PERF_SAMPLE_HOST_IP sample formatinclude/linux/perf_event.h include/uapi/linux/perf_event.h kernel/events/core.c+36 -10 replies
2/16perf/core, bpf: Support BPF filtering of mmap_data eventsinclude/linux/perf_event.h include/uapi/linux/perf_event.h kernel/events/core.c+88 -50 replies
3/16KVM: Add NMI-safe guest-to-host IP translation infrastructureinclude/linux/kvm_host.h virt/kvm/kvm_main.c+67 -01 reply
4/16KVM: x86: Implement kvm_arch_vcpu_get_ip_gpa()arch/x86/include/asm/kvm-x86-ops.h arch/x86/include/asm/kvm_host.h arch/x86/kvm/mmu.h +8 more+240 -70 replies
5/16KVM: arm64: Implement kvm_arch_vcpu_get_ip_gpa()arch/arm64/include/asm/kvm_nested.h arch/arm64/kvm/arm.c arch/arm64/kvm/at.c+65 -140 replies
6/16KVM: riscv: Implement kvm_arch_vcpu_get_ip_gpa()arch/riscv/kvm/vcpu.c+103 -00 replies
7/16KVM: loongarch: Implement kvm_arch_vcpu_get_ip_gpa()arch/loongarch/kvm/vcpu.c+104 -00 replies
8/16tools headers UAPI: Sync linux/perf_event.h with the kernel sourcestools/include/uapi/linux/perf_event.h+5 -20 replies
9/16perf tools: Add PERF_SAMPLE_HOST_IP parsing, synthesis, and record --host-iptools/perf/Documentation/perf-record.txt tools/perf/builtin-record.c tools/perf/tests/sample-parsing.c +8 more+81 -20 replies
10/16perf symbol: Support host IP resolution and guest fallback symbolizationtools/perf/util/addr_location.c tools/perf/util/addr_location.h tools/perf/util/event.c+67 -20 replies
11/16perf report, script: Add host_ip, host_dso, and host_sym fieldstools/perf/Documentation/perf-report.txt tools/perf/Documentation/perf-script.txt tools/perf/builtin-script.c +4 more+227 -00 replies
12/16perf record: Synthesize hypervisor file-backed data mappings for --host-iptools/perf/builtin-record.c tools/perf/util/Build tools/perf/util/kvm-mmap-filter.c +1 more+230 -00 replies
13/16perf record: Filter runtime hypervisor data mmaps with BPF for --host-iptools/perf/builtin-record.c tools/perf/util/Build tools/perf/util/bpf_kvm_mmap.c +3 more+274 -10 replies
14/16perf bpf-filter: Support host_ip in BPF sample filterstools/perf/tests/shell/record_bpf_filter.sh tools/perf/util/bpf-filter.c tools/perf/util/bpf-filter.l +3 more+21 -40 replies
15/16perf aslr: Anonymize sampled host_ip valuestools/perf/util/aslr.c tools/perf/util/aslr.h+34 -10 replies
16/16perf test: Add PERF_SAMPLE_HOST_IP symbolization, sorting, and ASLR teststools/perf/tests/Build tools/perf/tests/builtin-test.c tools/perf/tests/host-ip-sym.c +1 more+694 -00 replies

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.

Read the v1 thread

Cover letter [RFC PATCH 00/16] perf, kvm: Add PERF_SAMPLE_HOST_IP for host-side guest symbolization

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/[email protected]/
- perf evsel: Add missing bounds checks in __evsel__parse_sample()
https://lore.kernel.org/linux-perf-users/[email protected]/

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

Signature · 1 line
2.56.1.915.g69d5ec7298-goog