BPF List
 help / color / mirror / Atom feed
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 04/16] KVM: x86: Implement kvm_arch_vcpu_get_ip_gpa()
Date: Tue,  1 Dec 2026 15:21:04 -0800	[thread overview]
Message-ID: <20261201232116.1988791-5-irogers@google.com> (raw)
In-Reply-To: <20261201232116.1988791-1-irogers@google.com>

Implement NMI-safe guest virtual-to-physical instruction pointer
translation on x86 via kvm_arch_vcpu_get_ip_gpa() so PERF_SAMPLE_HOST_IP
can translate guest RIP into the backing host virtual address:

- Add gva_to_gpa_nofault and get_pdptr_nofault callbacks for nonpaging,
  32-bit, PAE/64-bit/LA57, and nested EPT/NPT page walks that read guest
  page tables via kvm_vcpu_read_guest_nofault(), never sleep, never
  mutate guest A/D bits, and validate PFERR_FETCH_MASK permissions.
- Temporarily restore host PKRU while reading guest page tables via
  copy_from_user_nofault() if an NMI PMI fires while guest PKRU is
  loaded, and translate kvm_get_linear_rip() with the guest's current
  CPL.

Signed-off-by: Ian Rogers <irogers@google.com>
---
 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 +++++++++++++
 11 files changed, 240 insertions(+), 7 deletions(-)

diff --git a/arch/x86/include/asm/kvm-x86-ops.h b/arch/x86/include/asm/kvm-x86-ops.h
index 4dde4e3e608a..ca9d1e37306c 100644
--- a/arch/x86/include/asm/kvm-x86-ops.h
+++ b/arch/x86/include/asm/kvm-x86-ops.h
@@ -52,6 +52,7 @@ KVM_X86_OP(set_gdt)
 KVM_X86_OP(sync_dirty_debug_regs)
 KVM_X86_OP(set_dr7)
 KVM_X86_OP(cache_reg)
+KVM_X86_OP_OPTIONAL(read_pdptr_nofault)
 KVM_X86_OP(get_rflags)
 KVM_X86_OP(set_rflags)
 KVM_X86_OP(get_if_flag)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 1ee4b127004d..44c492e6b15f 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -487,12 +487,16 @@ struct kvm_page_fault;
 struct kvm_pagewalk {
 	unsigned long (*get_guest_pgd)(struct kvm_vcpu *vcpu);
 	u64 (*get_pdptr)(struct kvm_vcpu *vcpu, int index);
+	int (*get_pdptr_nofault)(struct kvm_vcpu *vcpu, int index, u64 *pdptr);
 	void (*inject_page_fault)(struct kvm_vcpu *vcpu,
 				  struct x86_exception *fault,
 				  bool from_hardware);
 	gpa_t (*gva_to_gpa)(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,
 			    gpa_t gva_or_gpa, u64 access,
 			    struct x86_exception *exception);
+	gpa_t (*gva_to_gpa_nofault)(struct kvm_vcpu *vcpu,
+				    struct kvm_pagewalk *w,
+				    gpa_t gva_or_gpa, u64 access);
 
 	union kvm_cpu_role cpu_role;
 	struct kvm_page_format fmt;
@@ -1565,6 +1569,7 @@ struct kvm_x86_ops {
 	void (*sync_dirty_debug_regs)(struct kvm_vcpu *vcpu);
 	void (*set_dr7)(struct kvm_vcpu *vcpu, unsigned long value);
 	void (*cache_reg)(struct kvm_vcpu *vcpu, enum kvm_reg reg);
+	int (*read_pdptr_nofault)(struct kvm_vcpu *vcpu, int index, u64 *pdptr);
 	unsigned long (*get_rflags)(struct kvm_vcpu *vcpu);
 	void (*set_rflags)(struct kvm_vcpu *vcpu, unsigned long rflags);
 	bool (*get_if_flag)(struct kvm_vcpu *vcpu);
@@ -1774,7 +1779,7 @@ struct kvm_x86_nested_ops {
 	gpa_t (*translate_nested_gpa)(struct kvm_vcpu *vcpu, gpa_t gpa,
 				      u64 access,
 				      struct x86_exception *exception,
-				      u64 pte_access);
+				      u64 pte_access, bool nofault);
 
 	int (*enable_evmcs)(struct kvm_vcpu *vcpu,
 			    uint16_t *vmcs_version);
diff --git a/arch/x86/kvm/mmu.h b/arch/x86/kvm/mmu.h
index b2a295a9f084..4636e020c922 100644
--- a/arch/x86/kvm/mmu.h
+++ b/arch/x86/kvm/mmu.h
@@ -394,7 +394,19 @@ static inline gpa_t kvm_translate_gpa(struct kvm_vcpu *vcpu,
 	if (!mmu_is_nested(vcpu) || w == &vcpu->arch.ngpa_walk)
 		return gpa;
 	return kvm_nested_call(translate_nested_gpa)(vcpu, gpa, access,
-						     exception, pte_access);
+						     exception, pte_access,
+						     false);
+}
+
+static inline gpa_t kvm_translate_gpa_nofault(struct kvm_vcpu *vcpu,
+					      struct kvm_pagewalk *w,
+					      gpa_t gpa, u64 access,
+					      u64 pte_access)
+{
+	if (!mmu_is_nested(vcpu) || w == &vcpu->arch.ngpa_walk)
+		return gpa;
+	return kvm_nested_call(translate_nested_gpa)(vcpu, gpa, access,
+						     NULL, pte_access, true);
 }
 
 static inline bool kvm_has_mirrored_tdp(const struct kvm *kvm)
diff --git a/arch/x86/kvm/mmu/mmu.c b/arch/x86/kvm/mmu/mmu.c
index b122061b1fab..c92ba1555b2f 100644
--- a/arch/x86/kvm/mmu/mmu.c
+++ b/arch/x86/kvm/mmu/mmu.c
@@ -4487,6 +4487,15 @@ static gpa_t nonpaging_gva_to_gpa(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,
 				 exception, ACC_ALL);
 }
 
+static gpa_t nonpaging_gva_to_gpa_nofault(struct kvm_vcpu *vcpu,
+					  struct kvm_pagewalk *w,
+					  gpa_t vaddr, u64 access)
+{
+	return kvm_translate_gpa_nofault(vcpu, w, vaddr,
+					 access | PFERR_GUEST_FINAL_MASK,
+					 ACC_ALL);
+}
+
 static bool mmio_info_in_cache(struct kvm_vcpu *vcpu, u64 addr, bool direct)
 {
 	/*
@@ -5566,6 +5575,8 @@ static void __reset_rsvds_bits_mask(struct kvm_page_format *fmt,
 						   rsvd_bits(1, 2);	/* PDPTE */
 		fmt->rsvd_bits_mask[0][1] = high_bits_rsvd;	/* PDE */
 		fmt->rsvd_bits_mask[0][0] = high_bits_rsvd;	/* PTE */
+		fmt->rsvd_bits_mask[1][2] =
+			fmt->rsvd_bits_mask[0][2];
 		fmt->rsvd_bits_mask[1][1] = high_bits_rsvd |
 						   rsvd_bits(13, 20);	/* large page */
 		fmt->rsvd_bits_mask[1][0] =
@@ -6156,6 +6167,36 @@ static void init_kvm_shadow_mmu(struct kvm_vcpu *vcpu,
 	shadow_mmu_init_context(vcpu, context, root_role);
 }
 
+static int kvm_pdptr_read_nofault(struct kvm_vcpu *vcpu, int index, u64 *pdptr)
+{
+	struct kvm_pagewalk *w = &vcpu->arch.gva_walk;
+	gpa_t pdpt_gpa;
+
+	if (!tdp_enabled) {
+		*pdptr = vcpu->arch.pdptrs[index];
+		return 0;
+	}
+
+	if (kvm_x86_ops.read_pdptr_nofault)
+		return kvm_x86_call(read_pdptr_nofault)(vcpu, index, pdptr);
+
+	/*
+	 * Under NPT, SVM hardware does not cache architectural PDPTRs in the
+	 * VMCB and instead fetches PAE PDPTEs directly from guest CR3.
+	 */
+	pdpt_gpa = (kvm_read_cr3(vcpu) & GENMASK_ULL(31, 5)) +
+		   (unsigned int)index * sizeof(u64);
+	pdpt_gpa = kvm_translate_gpa_nofault(vcpu, w, pdpt_gpa,
+					     PFERR_WRITE_MASK |
+					     PFERR_USER_MASK |
+					     PFERR_GUEST_PAGE_MASK, 0);
+	if (pdpt_gpa == INVALID_GPA)
+		return -EFAULT;
+
+	return kvm_vcpu_read_guest_nofault(vcpu, pdpt_gpa, pdptr,
+					   sizeof(*pdptr));
+}
+
 static void init_kvm_page_walk(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,
 			       union kvm_cpu_role cpu_role)
 {
@@ -6165,14 +6206,19 @@ static void init_kvm_page_walk(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,
 	w->cpu_role.as_u64   = cpu_role.as_u64;
 	w->inject_page_fault = kvm_inject_page_fault;
 	w->get_pdptr         = kvm_pdptr_read;
+	w->get_pdptr_nofault = kvm_pdptr_read_nofault;
 	w->get_guest_pgd     = get_guest_cr3;
 
-	if (!is_cr0_pg(w))
+	if (!is_cr0_pg(w)) {
 		w->gva_to_gpa = nonpaging_gva_to_gpa;
-	else if (is_cr4_pae(w))
+		w->gva_to_gpa_nofault = nonpaging_gva_to_gpa_nofault;
+	} else if (is_cr4_pae(w)) {
 		w->gva_to_gpa = paging64_gva_to_gpa;
-	else
+		w->gva_to_gpa_nofault = paging64_gva_to_gpa_nofault;
+	} else {
 		w->gva_to_gpa = paging32_gva_to_gpa;
+		w->gva_to_gpa_nofault = paging32_gva_to_gpa_nofault;
+	}
 
 	reset_guest_paging_metadata(vcpu, w);
 }
@@ -6251,6 +6297,7 @@ void kvm_init_shadow_ept_mmu(struct kvm_vcpu *vcpu, bool execonly,
 
 		context->page_fault = ept_page_fault;
 		ngpa_walk->gva_to_gpa = ept_gva_to_gpa;
+		ngpa_walk->gva_to_gpa_nofault = ept_gva_to_gpa_nofault;
 		context->sync_spte = ept_sync_spte;
 
 		update_permission_bitmask(ngpa_walk, true, true);
diff --git a/arch/x86/kvm/mmu/paging_tmpl.h b/arch/x86/kvm/mmu/paging_tmpl.h
index 939a10fc1110..02753a1042b9 100644
--- a/arch/x86/kvm/mmu/paging_tmpl.h
+++ b/arch/x86/kvm/mmu/paging_tmpl.h
@@ -933,6 +933,92 @@ static gpa_t FNAME(gva_to_gpa)(struct kvm_vcpu *vcpu, struct kvm_pagewalk *w,
 	return gpa;
 }
 
+static gpa_t FNAME(gva_to_gpa_nofault)(struct kvm_vcpu *vcpu,
+				       struct kvm_pagewalk *w,
+				       gpa_t addr, u64 access)
+{
+	int level = w->cpu_role.base.level;
+	pt_element_t pte = kvm_mmu_get_guest_pgd(vcpu, w);
+	u64 nested_access = (PT_HAVE_ACCESSED_DIRTY(w) ? PFERR_WRITE_MASK : 0) |
+			    PFERR_USER_MASK;
+	u64 walk_nx_mask = 0;
+	u64 pt_access, pte_access = ~0;
+	unsigned int pte_pkey, walker_pte_access;
+	gpa_t real_gpa;
+	gfn_t gfn;
+
+#ifndef CONFIG_X86_64
+	if ((addr >> 32) && w == &vcpu->arch.gva_walk)
+		return INVALID_GPA;
+#endif
+
+#if PTTYPE == 64
+	walk_nx_mask = 1ULL << PT64_NX_SHIFT;
+	if (level == PT32E_ROOT_LEVEL) {
+		if (!w->get_pdptr_nofault ||
+		    w->get_pdptr_nofault(vcpu, (addr >> 30) & 3, &pte))
+			return INVALID_GPA;
+		if (!FNAME(is_present_gpte)(w, pte) ||
+		    FNAME(is_rsvd_bits_set)(&w->fmt, pte, PT32E_ROOT_LEVEL))
+			return INVALID_GPA;
+		--level;
+	}
+#endif
+	if (unlikely((is_long_mode(vcpu) && !is_pae(vcpu)) ||
+		     level > PT_MAX_FULL_LEVELS || level < 1))
+		return INVALID_GPA;
+
+	++level;
+	do {
+		gfn_t table_gfn;
+		int offset;
+
+		pt_access = pte_access;
+		--level;
+		if (unlikely(level < 1))
+			return INVALID_GPA;
+
+		table_gfn = gpte_to_gfn(pte);
+		offset = PT_INDEX(addr, level) * sizeof(pt_element_t);
+
+		real_gpa = kvm_translate_gpa_nofault(vcpu, w,
+						     gfn_to_gpa(table_gfn),
+						     nested_access |
+						     PFERR_GUEST_PAGE_MASK, 0);
+		if (unlikely(real_gpa == INVALID_GPA))
+			return INVALID_GPA;
+
+		if (unlikely(kvm_vcpu_read_guest_nofault(vcpu,
+							 real_gpa + offset,
+							 &pte, sizeof(pte))))
+			return INVALID_GPA;
+
+		pte_access = pt_access & (pte ^ walk_nx_mask);
+		if (unlikely(!FNAME(is_present_gpte)(w, pte) ||
+			     FNAME(is_rsvd_bits_set)(&w->fmt, pte, level)))
+			return INVALID_GPA;
+	} while (!FNAME(is_last_gpte)(w, level, pte));
+
+	pte_pkey = FNAME(gpte_pkeys)(vcpu, pte);
+	walker_pte_access = FNAME(gpte_access)(pte_access ^ walk_nx_mask);
+	if (unlikely(permission_fault(vcpu, w, walker_pte_access, pte_pkey, access)))
+		return INVALID_GPA;
+
+	gfn = gpte_to_gfn_lvl(pte, level);
+	gfn += (addr & PT_LVL_OFFSET_MASK(level)) >> PAGE_SHIFT;
+#if PTTYPE == 32
+	if (level > PG_LEVEL_4K && is_cpuid_PSE36())
+		gfn += pse36_gfn_delta(pte);
+#endif
+	real_gpa = kvm_translate_gpa_nofault(vcpu, w, gfn_to_gpa(gfn),
+					     access | PFERR_GUEST_FINAL_MASK,
+					     walker_pte_access);
+	if (real_gpa == INVALID_GPA)
+		return INVALID_GPA;
+
+	return real_gpa | (addr & ~PAGE_MASK);
+}
+
 /*
  * Using the information in sp->shadowed_translation (kvm_mmu_page_get_gfn()) is
  * safe because SPTEs are protected by mmu_notifiers and memslot generations, so
diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
index 07554089ec05..989912a2c190 100644
--- a/arch/x86/kvm/svm/nested.c
+++ b/arch/x86/kvm/svm/nested.c
@@ -89,6 +89,17 @@ static u64 nested_svm_get_tdp_pdptr(struct kvm_vcpu *vcpu, int index)
 	return pdpte;
 }
 
+static int nested_svm_get_tdp_pdptr_nofault(struct kvm_vcpu *vcpu, int index,
+					    u64 *pdpte)
+{
+	struct vcpu_svm *svm = to_svm(vcpu);
+	u64 cr3 = svm->nested.ctl.nested_cr3;
+
+	return kvm_vcpu_read_guest_nofault(vcpu,
+					   (cr3 & ~0x1fULL) + index * 8,
+					   pdpte, sizeof(*pdpte));
+}
+
 static unsigned long nested_svm_get_tdp_cr3(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
@@ -116,6 +127,7 @@ static void nested_svm_init_mmu_context(struct kvm_vcpu *vcpu)
 
 	vcpu->arch.ngpa_walk.get_guest_pgd     = nested_svm_get_tdp_cr3;
 	vcpu->arch.ngpa_walk.get_pdptr         = nested_svm_get_tdp_pdptr;
+	vcpu->arch.ngpa_walk.get_pdptr_nofault = nested_svm_get_tdp_pdptr_nofault;
 	vcpu->arch.ngpa_walk.inject_page_fault = nested_svm_inject_npf_exit;
 }
 
@@ -2163,7 +2175,7 @@ static bool svm_get_nested_state_pages(struct kvm_vcpu *vcpu)
 static gpa_t svm_translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa,
 				      u64 access,
 				      struct x86_exception *exception,
-				      u64 pte_access)
+				      u64 pte_access, bool nofault)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
 	struct kvm_pagewalk *w = &vcpu->arch.ngpa_walk;
@@ -2175,6 +2187,9 @@ static gpa_t svm_translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa,
 	if (!(svm->nested.ctl.misc_ctl & SVM_MISC_ENABLE_GMET))
 		access |= PFERR_USER_MASK;
 
+	if (nofault)
+		return w->gva_to_gpa_nofault(vcpu, w, gpa, access);
+
 	return w->gva_to_gpa(vcpu, w, gpa, access, exception);
 }
 
diff --git a/arch/x86/kvm/vmx/main.c b/arch/x86/kvm/vmx/main.c
index d75170a6199e..9ec117ceeea7 100644
--- a/arch/x86/kvm/vmx/main.c
+++ b/arch/x86/kvm/vmx/main.c
@@ -495,6 +495,14 @@ static void vt_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg)
 	vmx_cache_reg(vcpu, reg);
 }
 
+static int vt_read_pdptr_nofault(struct kvm_vcpu *vcpu, int index, u64 *pdptr)
+{
+	if (WARN_ON_ONCE(is_td_vcpu(vcpu)))
+		return -EINVAL;
+
+	return vmx_read_pdptr_nofault(vcpu, index, pdptr);
+}
+
 static unsigned long vt_get_rflags(struct kvm_vcpu *vcpu)
 {
 	if (is_td_vcpu(vcpu))
@@ -1050,6 +1058,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = {
 	.set_dr7 = vt_op(set_dr7),
 	.sync_dirty_debug_regs = vt_op(sync_dirty_debug_regs),
 	.cache_reg = vt_op(cache_reg),
+	.read_pdptr_nofault = vt_op(read_pdptr_nofault),
 	.get_rflags = vt_op(get_rflags),
 	.set_rflags = vt_op(set_rflags),
 	.get_if_flag = vt_op(get_if_flag),
diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index cd9465f4ee34..773f8f1be629 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -7495,7 +7495,7 @@ __init int nested_vmx_hardware_setup(int (*exit_handlers[])(struct kvm_vcpu *))
 static gpa_t vmx_translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa,
 				      u64 access,
 				      struct x86_exception *exception,
-				      u64 pte_access)
+				      u64 pte_access, bool nofault)
 {
 	struct kvm_pagewalk *w = &vcpu->arch.ngpa_walk;
 
@@ -7510,6 +7510,9 @@ static gpa_t vmx_translate_nested_gpa(struct kvm_vcpu *vcpu, gpa_t gpa,
 	if ((pte_access & ACC_USER_MASK) && (access & PFERR_GUEST_FINAL_MASK))
 		access |= PFERR_USER_MASK;
 
+	if (nofault)
+		return w->gva_to_gpa_nofault(vcpu, w, gpa, access);
+
 	return w->gva_to_gpa(vcpu, w, gpa, access, exception);
 }
 
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index af52568b551b..944d86091968 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -3436,6 +3436,22 @@ void ept_save_pdptrs(struct kvm_vcpu *vcpu)
 	kvm_register_mark_available(vcpu, VCPU_REG_PDPTR);
 }
 
+int vmx_read_pdptr_nofault(struct kvm_vcpu *vcpu, int index, u64 *pdptr)
+{
+	static const u16 guest_pdptr_fields[] = {
+		GUEST_PDPTR0, GUEST_PDPTR1, GUEST_PDPTR2, GUEST_PDPTR3,
+	};
+
+	if (WARN_ON_ONCE((unsigned int)index >= ARRAY_SIZE(guest_pdptr_fields)))
+		return -EINVAL;
+
+	if (enable_ept)
+		*pdptr = vmcs_read64(guest_pdptr_fields[index]);
+	else
+		*pdptr = vcpu->arch.pdptrs[index];
+	return 0;
+}
+
 #define CR3_EXITING_BITS (CPU_BASED_CR3_LOAD_EXITING | \
 			  CPU_BASED_CR3_STORE_EXITING)
 
diff --git a/arch/x86/kvm/vmx/x86_ops.h b/arch/x86/kvm/vmx/x86_ops.h
index cd72684d075d..21450e3dc94c 100644
--- a/arch/x86/kvm/vmx/x86_ops.h
+++ b/arch/x86/kvm/vmx/x86_ops.h
@@ -76,6 +76,7 @@ void vmx_set_gdt(struct kvm_vcpu *vcpu, struct desc_ptr *dt);
 void vmx_set_dr7(struct kvm_vcpu *vcpu, unsigned long val);
 void vmx_sync_dirty_debug_regs(struct kvm_vcpu *vcpu);
 void vmx_cache_reg(struct kvm_vcpu *vcpu, enum kvm_reg reg);
+int vmx_read_pdptr_nofault(struct kvm_vcpu *vcpu, int index, u64 *pdptr);
 unsigned long vmx_get_rflags(struct kvm_vcpu *vcpu);
 void vmx_set_rflags(struct kvm_vcpu *vcpu, unsigned long rflags);
 bool vmx_get_if_flag(struct kvm_vcpu *vcpu);
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 0549c0b48901..7533a22c2b4b 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -10482,6 +10482,44 @@ unsigned long kvm_arch_vcpu_get_ip(struct kvm_vcpu *vcpu)
 	return kvm_rip_read(vcpu);
 }
 
+gpa_t kvm_arch_vcpu_get_ip_gpa(struct kvm_vcpu *vcpu)
+{
+	struct kvm_pagewalk *gva_walk = &vcpu->arch.gva_walk;
+	unsigned long gva;
+	u32 saved_pkru;
+	bool restore_pkru = false;
+	u64 access;
+	gpa_t gpa;
+
+	WARN_ON_ONCE(!kvm_arch_pmi_in_guest(vcpu));
+
+	if (vcpu->arch.guest_state_protected || !gva_walk->gva_to_gpa_nofault)
+		return INVALID_GPA;
+
+	/*
+	 * An NMI PMI can fire inside vcpu_run() while guest PKRU is loaded.
+	 * Temporarily switch back to host PKRU so copy_from_user_nofault()
+	 * reads host VMAs using the host protection-key permissions.
+	 */
+	saved_pkru = read_pkru();
+	if (saved_pkru != vcpu->arch.host_pkru) {
+		write_pkru(vcpu->arch.host_pkru);
+		restore_pkru = true;
+	}
+
+	gva = kvm_get_linear_rip(vcpu);
+	access = PFERR_FETCH_MASK;
+	if (kvm_x86_call(get_cpl)(vcpu) == 3)
+		access |= PFERR_USER_MASK;
+
+	gpa = gva_walk->gva_to_gpa_nofault(vcpu, gva_walk, gva, access);
+
+	if (restore_pkru)
+		write_pkru(saved_pkru);
+
+	return gpa;
+}
+
 static inline u32 kvm_async_pf_hash_fn(gfn_t gfn)
 {
 	BUILD_BUG_ON(!is_power_of_2(ASYNC_PF_PER_VCPU));
-- 
2.56.1.915.g69d5ec7298-goog


  parent reply	other threads:[~2026-12-01 23:21 UTC|newest]

Thread overview: 23+ 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 ` [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 ` Ian Rogers [this message]
2026-12-01 23:21 ` [RFC PATCH 05/16] KVM: arm64: Implement kvm_arch_vcpu_get_ip_gpa() 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

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-5-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