Re: [RFC PATCH 03/16] KVM: Add NMI-safe guest-to-host IP translation infrastructure

From: Christian Borntraeger

Date: Wed Dec 02 2026 - 03:30:53 EST




Am 02.12.26 um 00:21 schrieb Ian Rogers:
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@xxxxxxxxxx>
---
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();

Probably not critical but maybe limit that to architectures that implement CONFIG_GUEST_PERF_EVENTS.



cc Hendrik. We might want to look at GUEST_PERF_EVENTSfor s390 in general. Right now this
perf_arch_instruction_pointer() returns the _guest_ PSW address when the interrupt hit (or via hw sample)
If we decide to change that, we could re-use the existing gaccess DAT walker to also implement
this here for s390.

This might also be relevant for our arm on s390.