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 03/16] KVM: Add NMI-safe guest-to-host IP translation infrastructure
Date: Tue, 1 Dec 2026 15:21:03 -0800 [thread overview]
Message-ID: <20261201232116.1988791-4-irogers@google.com> (raw)
In-Reply-To: <20261201232116.1988791-1-irogers@google.com>
Wire up kvm_guest_get_host_ip() in common KVM code and provide the
NMI-safe primitives architectures need to implement
kvm_arch_vcpu_get_ip_gpa() for PERF_SAMPLE_HOST_IP.
Because perf PMIs run in NMI or hardirq-disabled context and cannot
sleep or mix SRCU flavors on kvm->srcu:
- Pair synchronize_srcu_expedited(&kvm->srcu) with
synchronize_rcu_expedited() in kvm_swap_active_memslots() and add an
RCU-protected, read-only memslot lookup helper
(kvm_vcpu_gfn_to_hva_nofault()) so NMI callers never touch kvm->srcu
or last_used_slot caches.
- Add kvm_vcpu_read_guest_nofault() using copy_from_user_nofault() so
arch guest page-table reads are non-faulting, NMI-safe, and support
KVM_MEM_READONLY memslots.
- Add a __weak kvm_arch_vcpu_get_ip_gpa() default returning INVALID_GPA
and wire kvm_guest_get_host_ip() into kvm_guest_cbs.get_host_ip.
Signed-off-by: Ian Rogers <irogers@google.com>
---
include/linux/kvm_host.h | 3 ++
virt/kvm/kvm_main.c | 64 ++++++++++++++++++++++++++++++++++++++++
2 files changed, 67 insertions(+)
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h
index fe300a22ab0f..c3e5366d01ea 100644
--- a/include/linux/kvm_host.h
+++ b/include/linux/kvm_host.h
@@ -1468,6 +1468,8 @@ DEFINE_VCPU_MAP_CLASS(_readonly);
unsigned long kvm_vcpu_gfn_to_hva(struct kvm_vcpu *vcpu, gfn_t gfn);
unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, bool *writable);
+int kvm_vcpu_read_guest_nofault(struct kvm_vcpu *vcpu, gpa_t gpa, void *data,
+ unsigned long len);
int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data, int offset,
int len);
int kvm_vcpu_read_guest_atomic(struct kvm_vcpu *vcpu, gpa_t gpa, void *data,
@@ -1821,6 +1823,7 @@ static inline bool kvm_arch_intc_initialized(struct kvm *kvm)
#ifdef CONFIG_GUEST_PERF_EVENTS
unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu);
+gpa_t kvm_arch_vcpu_get_ip_gpa(struct kvm_vcpu *vcpu);
void __kvm_register_perf_callbacks(unsigned int (*pt_intr_handler)(void),
void (*mediated_pmi_handler)(void));
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 83e727bf6452..7935d4a346a3 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -1658,6 +1658,7 @@ static void kvm_swap_active_memslots(struct kvm *kvm, int as_id)
mutex_unlock(&kvm->slots_arch_lock);
synchronize_srcu_expedited(&kvm->srcu);
+ synchronize_rcu_expedited();
/*
* Increment the new memslot generation a second time, dropping the
@@ -2880,6 +2881,41 @@ unsigned long kvm_vcpu_gfn_to_hva_prot(struct kvm_vcpu *vcpu, gfn_t gfn, bool *w
return gfn_to_hva_memslot_prot(slot, gfn, writable);
}
+static unsigned long kvm_vcpu_gfn_to_hva_nofault(struct kvm_vcpu *vcpu,
+ gfn_t gfn)
+{
+ int as_id = kvm_arch_vcpu_memslots_id(vcpu);
+ struct kvm_memslots *slots;
+ struct kvm_memory_slot *slot;
+
+ guard(rcu)();
+ as_id = array_index_nospec(as_id, KVM_MAX_NR_ADDRESS_SPACES);
+ slots = rcu_dereference(vcpu->kvm->memslots[as_id]);
+ slot = search_memslots(slots, gfn, false);
+ return __gfn_to_hva_many(slot, gfn, NULL, false);
+}
+
+int kvm_vcpu_read_guest_nofault(struct kvm_vcpu *vcpu, gpa_t gpa, void *data,
+ unsigned long len)
+{
+ gfn_t gfn = gpa >> PAGE_SHIFT;
+ int offset = offset_in_page(gpa);
+ unsigned long addr;
+
+ if (WARN_ON_ONCE(offset + len > PAGE_SIZE))
+ return -EINVAL;
+
+ addr = kvm_vcpu_gfn_to_hva_nofault(vcpu, gfn);
+ if (kvm_is_error_hva(addr))
+ return -EFAULT;
+
+ if (copy_from_user_nofault(data, (const void __user *)(addr + offset), len))
+ return -EFAULT;
+
+ return 0;
+}
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_vcpu_read_guest_nofault);
+
static bool kvm_is_ad_tracked_page(struct page *page)
{
/*
@@ -6578,9 +6614,37 @@ static unsigned long kvm_guest_get_ip(void)
return kvm_arch_vcpu_get_ip(vcpu);
}
+gpa_t __weak kvm_arch_vcpu_get_ip_gpa(struct kvm_vcpu *vcpu)
+{
+ return INVALID_GPA;
+}
+
+static unsigned long kvm_guest_get_host_ip(void)
+{
+ struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
+ unsigned long hva = 0;
+ gpa_t gpa;
+
+ /* Retrieving the IP must be guarded by a call to kvm_guest_state(). */
+ if (WARN_ON_ONCE(!kvm_arch_pmi_in_guest(vcpu)))
+ return 0;
+
+ gpa = kvm_arch_vcpu_get_ip_gpa(vcpu);
+ if (gpa != INVALID_GPA) {
+ hva = kvm_vcpu_gfn_to_hva_nofault(vcpu, gpa_to_gfn(gpa));
+ if (kvm_is_error_hva(hva))
+ hva = 0;
+ else
+ hva += offset_in_page(gpa);
+ }
+
+ return hva;
+}
+
static struct perf_guest_info_callbacks kvm_guest_cbs = {
.state = kvm_guest_state,
.get_ip = kvm_guest_get_ip,
+ .get_host_ip = kvm_guest_get_host_ip,
.handle_intel_pt_intr = NULL,
.handle_mediated_pmi = NULL,
};
--
2.56.1.915.g69d5ec7298-goog
next prev parent reply other threads:[~2026-12-01 23:21 UTC|newest]
Thread overview: 17+ 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 ` Ian Rogers [this message]
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: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: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: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: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
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-4-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
all inboxes | Powered by JetHome®