Skip to contentSkip to conversation

KVM: x86: Fixes for KVM_INTEL_PROVE_VE

PATCH Applied Sean Christopherson 4 participants 23 messages May 18, 2024 – May 23, 2024

Subsystems (3)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

cover KVM: x86: Fixes for KVM_INTEL_PROVE_VE

Fixes and debug help for prove #VE support. I'm not in love with the sanity check implementation, but I also don't love the idea of plumbing in @kvm to the low level SPTE helpers.

Not super well tested, but I wanted to get this posted asap in case someone wants to debug the unexpected #VEs we're seeing.

Note, Isaku's patch needs his SoB.

Isaku Yamahata (1):
KVM: x86/mmu: Use SHADOW_NONPRESENT_VALUE for atomic zap in TDP MMU

Sean Christopherson (8):
KVM: nVMX: Initialize #VE info page for vmcs02 when proving #VE
support
KVM: nVMX: Always handle #VEs in L0 (never forward #VEs from L2 to L1)
KVM: x86/mmu: Add sanity checks that KVM doesn't create EPT #VE SPTEs
KVM: VMX: Dump VMCS on unexpected #VE
KVM: x86/mmu: Print SPTEs on unexpected #VE
KVM: VMX: Don't kill the VM on an unexpected #VE
KVM: VMX: Enumerate EPT Violation #VE support in /proc/cpuinfo
KVM: x86: Disable KVM_INTEL_PROVE_VE by default

 arch/x86/include/asm/kvm_host.h    |  1 +
 arch/x86/include/asm/vmxfeatures.h |  2 +-
 arch/x86/kvm/Kconfig               |  6 ++--
 arch/x86/kvm/mmu/mmu.c             | 45 ++++++++++++++++++++++++------
 arch/x86/kvm/mmu/spte.h            |  9 ++++++
 arch/x86/kvm/mmu/tdp_iter.h        |  2 ++
 arch/x86/kvm/mmu/tdp_mmu.c         |  2 +-
 arch/x86/kvm/vmx/nested.c          |  5 ++++
 arch/x86/kvm/vmx/vmx.c             | 11 ++++++--
 9 files changed, 67 insertions(+), 16 deletions(-)

base-commit: 4aad0b1893a141f114ba40ed509066f3c9bc24b0

Signature · 1 line
2.45.0.215.g3402c0e53f-goog
author maintainer
#1 lore raw

PATCH 1/9 KVM: x86/mmu: Use SHADOW_NONPRESENT_VALUE for atomic zap in TDP MMU

From: Isaku Yamahata <[email protected]>

Use SHADOW_NONPRESENT_VALUE when zapping TDP MMU SPTEs with mmu_lock held for read, tdp_mmu_zap_spte_atomic() was simply missed during the initial development.

Fixes 7f01cab84928 ("KVM: x86/mmu: Allow non-zero value for non-present SPTE and removed SPTE")
[sean: write changelog]
---
 arch/x86/kvm/mmu/tdp_mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/tdp_mmu.c +1 -1
index 1259dd63defc..36539c1b36cd 100644
@@ -626,7 +626,7 @@ static inline int tdp_mmu_zap_spte_atomic(struct kvm *kvm,
 	 * SPTEs.
 	 */
 	handle_changed_spte(kvm, iter->as_id, iter->gfn, iter->old_spte,
-			    0, iter->level, true);
+			    SHADOW_NONPRESENT_VALUE, iter->level, true);
 
 	return 0;
 }
Signature · 1 line
2.45.0.215.g3402c0e53f-goog
Reviewed-by on v1 1/9
#2 lore raw

Re: [PATCH 1/9] KVM: x86/mmu: Use SHADOW_NONPRESENT_VALUE for atomic zap in TDP MMU

Quoted 19 lines · Sean Christopherson

From: Isaku Yamahata <[email protected]>

Use SHADOW_NONPRESENT_VALUE when zapping TDP MMU SPTEs with mmu_lock held for read, tdp_mmu_zap_spte_atomic() was simply missed during the initial development.

Fixes 7f01cab84928 ("KVM: x86/mmu: Allow non-zero value for non-present SPTE and removed SPTE")
[sean: write changelog]
---
 arch/x86/kvm/mmu/tdp_mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/tdp_mmu.c +0 -0
index 1259dd63defc..36539c1b36cd 100644

arch/x86/kvm/mmu/tdp_mmu.c static inline int tdp_mmu_zap_spte_atomic(struct kvm *kvm, · line 632

Quoted 8 lines
arch/x86/kvm/mmu/tdp_mmu.c +1 -1
@@ -626,7 +626,7 @@ static inline int tdp_mmu_zap_spte_atomic(struct kvm *kvm,
 	 * SPTEs.
 	 */
 	handle_changed_spte(kvm, iter->as_id, iter->gfn, iter->old_spte,
-			    0, iter->level, true);
+			    SHADOW_NONPRESENT_VALUE, iter->level, true);
 
 	return 0;
 }
#3 lore raw

Re: [PATCH 1/9] KVM: x86/mmu: Use SHADOW_NONPRESENT_VALUE for atomic zap in TDP MMU

In reply to Sean Christopherson
Quoted 29 lines · Sean Christopherson

From: Isaku Yamahata <[email protected]>

Use SHADOW_NONPRESENT_VALUE when zapping TDP MMU SPTEs with mmu_lock held for read, tdp_mmu_zap_spte_atomic() was simply missed during the initial development.

Fixes 7f01cab84928 ("KVM: x86/mmu: Allow non-zero value for non-present SPTE and removed SPTE")
[sean: write changelog]
---
 arch/x86/kvm/mmu/tdp_mmu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/mmu/tdp_mmu.c +1 -1
index 1259dd63defc..36539c1b36cd 100644
@@ -626,7 +626,7 @@ static inline int tdp_mmu_zap_spte_atomic(struct kvm *kvm,
 	 * SPTEs.
 	 */
 	handle_changed_spte(kvm, iter->as_id, iter->gfn, iter->old_spte,
-			    0, iter->level, true);
+			    SHADOW_NONPRESENT_VALUE, iter->level, true);
 
 	return 0;
 }
Signature · 1 line
2.45.0.215.g3402c0e53f-goog
Signature · 1 line
Isaku Yamahata <[email protected]>
author maintainer
#4 lore raw

PATCH 2/9 KVM: nVMX: Initialize #VE info page for vmcs02 when proving #VE support

In reply to Sean Christopherson

Point vmcs02.VE_INFORMATION_ADDRESS at the vCPU's #VE info page when initializing vmcs02, otherwise KVM will run L2 with EPT Violation #VE enabled and a VE info address pointing at pfn 0.

Fixes 8131cf5b4fd8 ("KVM: VMX: Introduce test mode related to EPT violation VE")
---
 arch/x86/kvm/vmx/nested.c | 3 +++
 1 file changed, 3 insertions(+)
arch/x86/kvm/vmx/nested.c +3 -0
index d5b832126e34..6798fadaa335 100644
@@ -2242,6 +2242,9 @@ static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
 		vmcs_write64(EPT_POINTER,
 			     construct_eptp(&vmx->vcpu, 0, PT64_ROOT_4LEVEL));
 
+	if (vmx->ve_info)
+		vmcs_write64(VE_INFORMATION_ADDRESS, __pa(vmx->ve_info));
+
 	/* All VMFUNCs are currently emulated through L0 vmexits.  */
 	if (cpu_has_vmx_vmfunc())
 		vmcs_write64(VM_FUNCTION_CONTROL, 0);
Signature · 1 line
2.45.0.215.g3402c0e53f-goog
#5 lore raw

Re: [PATCH 2/9] KVM: nVMX: Initialize #VE info page for vmcs02 when proving #VE support

Quoted 3 lines · Sean Christopherson

Point vmcs02.VE_INFORMATION_ADDRESS at the vCPU's #VE info page when initializing vmcs02, otherwise KVM will run L2 with EPT Violation #VE enabled and a VE info address pointing at pfn 0.

How about we just clear EPT_VIOLATION_VE bit in 2nd_exec_control unconditionally for vmcs02? Your next patch says:

"
Always handle #VEs, e.g. due to prove EPT Violation #VE failures, in L0, as KVM does not expose any #VE capabilities to L1, i.e. any and all #VEs are KVM's responsibility.
"

Quoted 21 lines
Fixes 8131cf5b4fd8 ("KVM: VMX: Introduce test mode related to EPT violation VE")
---
  arch/x86/kvm/vmx/nested.c | 3 +++
  1 file changed, 3 insertions(+)
arch/x86/kvm/vmx/nested.c +3 -0
index d5b832126e34..6798fadaa335 100644
@@ -2242,6 +2242,9 @@ static void prepare_vmcs02_constant_state(struct vcpu_vmx *vmx)
  		vmcs_write64(EPT_POINTER,
  			     construct_eptp(&vmx->vcpu, 0, PT64_ROOT_4LEVEL));
 
+	if (vmx->ve_info)
+		vmcs_write64(VE_INFORMATION_ADDRESS, __pa(vmx->ve_info));
+
  	/* All VMFUNCs are currently emulated through L0 vmexits.  */
  	if (cpu_has_vmx_vmfunc())
  		vmcs_write64(VM_FUNCTION_CONTROL, 0);
author maintainer
#6 lore raw

Re: [PATCH 2/9] KVM: nVMX: Initialize #VE info page for vmcs02 when proving #VE support

Quoted 7 lines · Kai Huang
Quoted 3 lines · Sean Christopherson

Point vmcs02.VE_INFORMATION_ADDRESS at the vCPU's #VE info page when initializing vmcs02, otherwise KVM will run L2 with EPT Violation #VE enabled and a VE info address pointing at pfn 0.

How about we just clear EPT_VIOLATION_VE bit in 2nd_exec_control unconditionally for vmcs02?

Because then KVM wouldn't get any EPT Violation #VE coverage for L2, and as evidence by the KVM-Unit-Test failure, running L2 with EPT Violation #VEs enabled provides unique coverage. Doing so definitely provides coverage beyond what is strictly needed for TDX, but it's just as easy to set the VE info page in vmcs02 as it is so clear EPT_VIOLATION_VE, so why not.

Quoted 7 lines

Your next patch says:

"
Always handle #VEs, e.g. due to prove EPT Violation #VE failures, in L0, as KVM does not expose any #VE capabilities to L1, i.e. any and all #VEs are KVM's responsibility.
"

I don't see how that's relevant to whether or not KVM enables EPT Violation #VEs while L2 is running. That patch simply routes all #VEs to L0, it doesn't affect whether or not it's safe to enable EPT Violation #VEs for L2.

#7 lore raw

Re: [PATCH 2/9] KVM: nVMX: Initialize #VE info page for vmcs02 when proving #VE support

Quoted 23 lines · Sean Christopherson
Quoted 7 lines · Kai Huang
Quoted 3 lines · Sean Christopherson

Point vmcs02.VE_INFORMATION_ADDRESS at the vCPU's #VE info page when initializing vmcs02, otherwise KVM will run L2 with EPT Violation #VE enabled and a VE info address pointing at pfn 0.

How about we just clear EPT_VIOLATION_VE bit in 2nd_exec_control unconditionally for vmcs02?

Because then KVM wouldn't get any EPT Violation #VE coverage for L2, and as evidence by the KVM-Unit-Test failure, running L2 with EPT Violation #VEs enabled provides unique coverage. Doing so definitely provides coverage beyond what is strictly needed for TDX, but it's just as easy to set the VE info page in vmcs02 as it is so clear EPT_VIOLATION_VE, so why not.

Quoted 7 lines

Your next patch says:

"
Always handle #VEs, e.g. due to prove EPT Violation #VE failures, in L0, as KVM does not expose any #VE capabilities to L1, i.e. any and all #VEs are KVM's responsibility.
"

Quoted 3 lines

I don't see how that's relevant to whether or not KVM enables EPT Violation #VEs while L2 is running. That patch simply routes all #VEs to L0, it doesn't affect whether or not it's safe to enable EPT Violation #VEs for L2.

My logic is, if #VE exit cannot possibly happen for L2, then we don't need to deal whether to route #VE exits to L1. :-)

Well, actually I think conceptually, it kinda makes sense to route #VE exits to L1:

L1 should never enable #VE related bits so L1 is certainly not expecting to see #VE from L2. But how to act should be depending on L1's logic? E.g., it can choose to ignore, or just kill the L2 etc?

Unconditionally disable #VE in vmcs02 can avoid such issue because it's just not possible for L2 to have the #VE exit.

author maintainer
#8 lore raw

Re: [PATCH 2/9] KVM: nVMX: Initialize #VE info page for vmcs02 when proving #VE support

Quoted 28 lines · Kai Huang
Quoted 23 lines · Sean Christopherson
Quoted 7 lines · Kai Huang
Quoted 3 lines · Sean Christopherson

Point vmcs02.VE_INFORMATION_ADDRESS at the vCPU's #VE info page when initializing vmcs02, otherwise KVM will run L2 with EPT Violation #VE enabled and a VE info address pointing at pfn 0.

How about we just clear EPT_VIOLATION_VE bit in 2nd_exec_control unconditionally for vmcs02?

Because then KVM wouldn't get any EPT Violation #VE coverage for L2, and as evidence by the KVM-Unit-Test failure, running L2 with EPT Violation #VEs enabled provides unique coverage. Doing so definitely provides coverage beyond what is strictly needed for TDX, but it's just as easy to set the VE info page in vmcs02 as it is so clear EPT_VIOLATION_VE, so why not.

Quoted 7 lines

Your next patch says:

"
Always handle #VEs, e.g. due to prove EPT Violation #VE failures, in L0, as KVM does not expose any #VE capabilities to L1, i.e. any and all #VEs are KVM's responsibility.
"

Quoted 3 lines

I don't see how that's relevant to whether or not KVM enables EPT Violation #VEs while L2 is running. That patch simply routes all #VEs to L0, it doesn't affect whether or not it's safe to enable EPT Violation #VEs for L2.

Quoted 7 lines

My logic is, if #VE exit cannot possibly happen for L2, then we don't need to deal whether to route #VE exits to L1. :-)

Well, actually I think conceptually, it kinda makes sense to route #VE exits to L1:

L1 should never enable #VE related bits so L1 is certainly not expecting to

Not "should never", "can never". If L1 attempts to enable EPT_VIOLATION_VE, then VM-Enter will VM-Fail.

Quoted 2 lines

see #VE from L2. But how to act should be depending on L1's logic? E.g., it can choose to ignore, or just kill the L2 etc?

No. Architecturally, from L1's perspective, a #VE VM-Exit _cannot_ occur in L2. L1 can inject a #VE into L2, but a #VE cannot be generated by the CPU and thus cannot cause a VM-Exit.

Quoted 2 lines

Unconditionally disable #VE in vmcs02 can avoid such issue because it's just not possible for L2 to have the #VE exit.

Sure, but by that argument we could just avoid all nested VMX issues by never enabling anything for L2.

If there's an argument to be made for disabling EPT_VIOLATION_VE in vmcs02, it's that the potential maintenance cost of keeping nEPT, nVMX, and the shadow MMU healthy outweighs the benefits. I.e. we don't have a use case for enabling EPT_VIOLATION_VE while L2 is running, so why validate it?

If whatever bug the KUT EPT found ends up being a KVM bug that specifically only affects nVMX, then it'd be worth revisiting whether or not it's worth enabling EPT_VIOLATION_VE in vmcs02. But that's a rather big "if" at this point.

#9 lore raw

Re: [PATCH 2/9] KVM: nVMX: Initialize #VE info page for vmcs02 when proving #VE support

Quoted 40 lines · Sean Christopherson
Quoted 28 lines · Kai Huang
Quoted 23 lines · Sean Christopherson
Quoted 7 lines · Kai Huang
Quoted 3 lines · Sean Christopherson

Point vmcs02.VE_INFORMATION_ADDRESS at the vCPU's #VE info page when initializing vmcs02, otherwise KVM will run L2 with EPT Violation #VE enabled and a VE info address pointing at pfn 0.

How about we just clear EPT_VIOLATION_VE bit in 2nd_exec_control unconditionally for vmcs02?

Because then KVM wouldn't get any EPT Violation #VE coverage for L2, and as evidence by the KVM-Unit-Test failure, running L2 with EPT Violation #VEs enabled provides unique coverage. Doing so definitely provides coverage beyond what is strictly needed for TDX, but it's just as easy to set the VE info page in vmcs02 as it is so clear EPT_VIOLATION_VE, so why not.

Quoted 7 lines

Your next patch says:

"
Always handle #VEs, e.g. due to prove EPT Violation #VE failures, in L0, as KVM does not expose any #VE capabilities to L1, i.e. any and all #VEs are KVM's responsibility.
"

Quoted 3 lines

I don't see how that's relevant to whether or not KVM enables EPT Violation #VEs while L2 is running. That patch simply routes all #VEs to L0, it doesn't affect whether or not it's safe to enable EPT Violation #VEs for L2.

Quoted 7 lines

My logic is, if #VE exit cannot possibly happen for L2, then we don't need to deal whether to route #VE exits to L1. :-)

Well, actually I think conceptually, it kinda makes sense to route #VE exits to L1:

L1 should never enable #VE related bits so L1 is certainly not expecting to

Not "should never", "can never". If L1 attempts to enable EPT_VIOLATION_VE, then VM-Enter will VM-Fail.

Quoted 6 lines
Quoted 2 lines

see #VE from L2. But how to act should be depending on L1's logic? E.g., it can choose to ignore, or just kill the L2 etc?

No. Architecturally, from L1's perspective, a #VE VM-Exit _cannot_ occur in L2. L1 can inject a #VE into L2, but a #VE cannot be generated by the CPU and thus cannot cause a VM-Exit.

OK. The point is not to argue about L1 how to handle, but whether we should inject to L1 -- L1 can do whatever it believes legal/sane.

But I understand the purpose is to test/validate, so it's fine for L0 to handle, and by handle it eventually means we want to just dump that #VE exit.

But now L0 always handles #VE exits from L2, and AFAICT L0 will just kill the L1, until the patch:

KVM: VMX: Don't kill the VM on an unexpected #VE

lands.

So looks that patch at least should be done first. Otherwise it doesn't make a lot sense to kill L1 for #VE exits from L2.

Quoted 11 lines
Quoted 3 lines

Unconditionally disable #VE in vmcs02 can avoid such issue because it's just not possible for L2 to have the #VE exit.

Sure, but by that argument we could just avoid all nested VMX issues by never enabling anything for L2.

If there's an argument to be made for disabling EPT_VIOLATION_VE in vmcs02, it's that the potential maintenance cost of keeping nEPT, nVMX, and the shadow MMU healthy outweighs the benefits. I.e. we don't have a use case for enabling EPT_VIOLATION_VE while L2 is running, so why validate it?

Yeah. I am not sure the purpose of validating #VE exits from L2.

Quoted 4 lines

If whatever bug the KUT EPT found ends up being a KVM bug that specifically only affects nVMX, then it'd be worth revisiting whether or not it's worth enabling EPT_VIOLATION_VE in vmcs02. But that's a rather big "if" at this point.

OK.

author maintainer
#10 lore raw

Re: [PATCH 2/9] KVM: nVMX: Initialize #VE info page for vmcs02 when proving #VE support

Quoted 9 lines · Kai Huang

But now L0 always handles #VE exits from L2, and AFAICT L0 will just kill the L1, until the patch:

KVM: VMX: Don't kill the VM on an unexpected #VE

lands.

So looks that patch at least should be done first. Otherwise it doesn't make a lot sense to kill L1 for #VE exits from L2.

I have no objection to changing the order.

author maintainer
#11 lore raw

PATCH 3/9 KVM: nVMX: Always handle #VEs in L0 (never forward #VEs from L2 to L1)

In reply to Sean Christopherson

Always handle #VEs, e.g. due to prove EPT Violation #VE failures, in L0, as KVM does not expose any #VE capabilities to L1, i.e. any and all #VEs are KVM's responsibility.

Fixes 8131cf5b4fd8 ("KVM: VMX: Introduce test mode related to EPT violation VE")
---
 arch/x86/kvm/vmx/nested.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kvm/vmx/nested.c +2 -0
index 6798fadaa335..643935a0f70a 100644
@@ -6233,6 +6233,8 @@ static bool nested_vmx_l0_wants_exit(struct kvm_vcpu *vcpu,
 		else if (is_alignment_check(intr_info) &&
 			 !vmx_guest_inject_ac(vcpu))
 			return true;
+		else if (is_ve_fault(intr_info))
+			return true;
 		return false;
 	case EXIT_REASON_EXTERNAL_INTERRUPT:
 		return true;
Signature · 1 line
2.45.0.215.g3402c0e53f-goog
author maintainer
#12 lore raw

PATCH 4/9 KVM: x86/mmu: Add sanity checks that KVM doesn't create EPT #VE SPTEs

In reply to Sean Christopherson

Assert that KVM doesn't set a SPTE to a value that could trigger an EPT Violation #VE on a non-MMIO SPTE, e.g. to help detect bugs even without KVM_INTEL_PROVE_VE enabled, and to help debug actual #VE failures.

Note, this will run afoul of TDX support, which needs to reflect emulated MMIO accesses into the guest as #VEs (which was the whole point of adding EPT Violation #VE support in KVM). The obvious fix for that is to exempt MMIO SPTEs, but that's annoyingly difficult now that is_mmio_spte() relies on a per-VM value. However, resolving that conundrum is a future problem, whereas getting KVM_INTEL_PROVE_VE healthy is a current problem.

---
 arch/x86/kvm/mmu/mmu.c      | 3 +++
 arch/x86/kvm/mmu/spte.h     | 9 +++++++++
 arch/x86/kvm/mmu/tdp_iter.h | 2 ++
 3 files changed, 14 insertions(+)
arch/x86/kvm/mmu/mmu.c +3 -0
index 5095fb46713e..d2af077d8b34 100644
@@ -336,16 +336,19 @@ static int is_cpuid_PSE36(void)
 #ifdef CONFIG_X86_64
 static void __set_spte(u64 *sptep, u64 spte)
 {
+	KVM_MMU_WARN_ON(is_ept_ve_possible(spte));
 	WRITE_ONCE(*sptep, spte);
 }
 
 static void __update_clear_spte_fast(u64 *sptep, u64 spte)
 {
+	KVM_MMU_WARN_ON(is_ept_ve_possible(spte));
 	WRITE_ONCE(*sptep, spte);
 }
 
 static u64 __update_clear_spte_slow(u64 *sptep, u64 spte)
 {
+	KVM_MMU_WARN_ON(is_ept_ve_possible(spte));
 	return xchg(sptep, spte);
 }
 
arch/x86/kvm/mmu/spte.h +9 -0
index 5dd5405fa07a..52fa004a1fbc 100644
@@ -3,6 +3,8 @@
 #ifndef KVM_X86_MMU_SPTE_H
 #define KVM_X86_MMU_SPTE_H
 
+#include <asm/vmx.h>
+
 #include "mmu.h"
 #include "mmu_internal.h"
 
@@ -276,6 +278,13 @@ static inline bool is_shadow_present_pte(u64 pte)
 	return !!(pte & SPTE_MMU_PRESENT_MASK);
 }
 
+static inline bool is_ept_ve_possible(u64 spte)
+{
+	return (shadow_present_mask & VMX_EPT_SUPPRESS_VE_BIT) &&
+	       !(spte & VMX_EPT_SUPPRESS_VE_BIT) &&
+	       (spte & VMX_EPT_RWX_MASK) != VMX_EPT_MISCONFIG_WX_VALUE;
+}
+
 /*
  * Returns true if A/D bits are supported in hardware and are enabled by KVM.
  * When enabled, KVM uses A/D bits for all non-nested MMUs.  Because L1 can
arch/x86/kvm/mmu/tdp_iter.h +2 -0
index fae559559a80..2880fd392e0c 100644
@@ -21,11 +21,13 @@ static inline u64 kvm_tdp_mmu_read_spte(tdp_ptep_t sptep)
 
 static inline u64 kvm_tdp_mmu_write_spte_atomic(tdp_ptep_t sptep, u64 new_spte)
 {
+	KVM_MMU_WARN_ON(is_ept_ve_possible(new_spte));
 	return xchg(rcu_dereference(sptep), new_spte);
 }
 
 static inline void __kvm_tdp_mmu_write_spte(tdp_ptep_t sptep, u64 new_spte)
 {
+	KVM_MMU_WARN_ON(is_ept_ve_possible(new_spte));
 	WRITE_ONCE(*rcu_dereference(sptep), new_spte);
 }
 
Signature · 1 line
2.45.0.215.g3402c0e53f-goog
author maintainer
#13 lore raw

PATCH 5/9 KVM: VMX: Dump VMCS on unexpected #VE

In reply to Sean Christopherson

Dump the VMCS on an unexpected #VE, otherwise it's practically impossible to figure out why the #VE occurred.

---
 arch/x86/kvm/vmx/vmx.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/vmx.c +3 -1
index 51b2cd13250a..0c68643d982b 100644
@@ -5217,8 +5217,10 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)
 	if (is_invalid_opcode(intr_info))
 		return handle_ud(vcpu);
 
-	if (KVM_BUG_ON(is_ve_fault(intr_info), vcpu->kvm))
+	if (KVM_BUG_ON(is_ve_fault(intr_info), vcpu->kvm)) {
+		dump_vmcs(vcpu);
 		return -EIO;
+	}
 
 	error_code = 0;
 	if (intr_info & INTR_INFO_DELIVER_CODE_MASK)
Signature · 1 line
2.45.0.215.g3402c0e53f-goog
author maintainer
#14 lore raw

PATCH 6/9 KVM: x86/mmu: Print SPTEs on unexpected #VE

In reply to Sean Christopherson

Print the SPTEs that correspond to the faulting GPA on an unexpected EPT Violation #VE to help the user debug failures, e.g. to pinpoint which SPTE didn't have SUPPRESS_VE set.

Opportunistically assert that the underlying exit reason was indeed an EPT Violation, as the CPU has *really* gone off the rails if a #VE occurs due to a completely unexpected exit reason.

---
 arch/x86/include/asm/kvm_host.h |  1 +
 arch/x86/kvm/mmu/mmu.c          | 42 ++++++++++++++++++++++++++-------
 arch/x86/kvm/vmx/vmx.c          |  5 ++++
 3 files changed, 39 insertions(+), 9 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
index aabf1648a56a..9bb2e164c523 100644
@@ -2159,6 +2159,7 @@ int kvm_emulate_hypercall(struct kvm_vcpu *vcpu);
 
 int kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 error_code,
 		       void *insn, int insn_len);
+void kvm_mmu_print_sptes(struct kvm_vcpu *vcpu, gpa_t gpa, const char *msg);
 void kvm_mmu_invlpg(struct kvm_vcpu *vcpu, gva_t gva);
 void kvm_mmu_invalidate_addr(struct kvm_vcpu *vcpu, struct kvm_mmu *mmu,
 			     u64 addr, unsigned long roots);
arch/x86/kvm/mmu/mmu.c +33 -9

60 lines not on this page · raw message

arch/x86/kvm/vmx/vmx.c +5 -0
index 0c68643d982b..2a3fce61c785 100644
@@ -5218,7 +5218,12 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)
 		return handle_ud(vcpu);
 
 	if (KVM_BUG_ON(is_ve_fault(intr_info), vcpu->kvm)) {
+		struct vmx_ve_information *ve_info = vmx->ve_info;
+
+		WARN_ONCE(ve_info->exit_reason != EXIT_REASON_EPT_VIOLATION,
+			  "Unexpected #VE on VM-Exit reason 0x%x", ve_info->exit_reason);
 		dump_vmcs(vcpu);
+		kvm_mmu_print_sptes(vcpu, ve_info->guest_physical_address, "#VE");
 		return -EIO;
 	}
 
Signature · 1 line
2.45.0.215.g3402c0e53f-goog
author maintainer
#15 lore raw

PATCH 7/9 KVM: VMX: Don't kill the VM on an unexpected #VE

In reply to Sean Christopherson

Don't terminiate the VM on an unexpected #VE, as it's extremely unlikely the #VE is fatal to the guest, and even less likely that it presents a danger to the host. Simply resume the guest on "failure", as the #VE info page's BUSY field will prevent converting any more EPT Violations to #VEs for the vCPU (at least, that's what the BUSY field is supposed to do).

---
 arch/x86/kvm/vmx/vmx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/vmx.c +2 -2
index 2a3fce61c785..58832aae2248 100644
@@ -5217,14 +5217,14 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)
 	if (is_invalid_opcode(intr_info))
 		return handle_ud(vcpu);
 
-	if (KVM_BUG_ON(is_ve_fault(intr_info), vcpu->kvm)) {
+	if (WARN_ON_ONCE(is_ve_fault(intr_info))) {
 		struct vmx_ve_information *ve_info = vmx->ve_info;
 
 		WARN_ONCE(ve_info->exit_reason != EXIT_REASON_EPT_VIOLATION,
 			  "Unexpected #VE on VM-Exit reason 0x%x", ve_info->exit_reason);
 		dump_vmcs(vcpu);
 		kvm_mmu_print_sptes(vcpu, ve_info->guest_physical_address, "#VE");
-		return -EIO;
+		return 1;
 	}
 
 	error_code = 0;
Signature · 1 line
2.45.0.215.g3402c0e53f-goog
author maintainer
#16 lore raw

PATCH 8/9 KVM: VMX: Enumerate EPT Violation #VE support in /proc/cpuinfo

In reply to Sean Christopherson

Don't suppress printing EPT_VIOLATION_VE in /proc/cpuinfo, knowing whether or not KVM_INTEL_PROVE_VE actually does anything is extremely valuable. A privileged user can get at the information by reading the raw MSR, but the whole point of the VMX flags is to avoid needing to glean information from raw MSR reads.

---
 arch/x86/include/asm/vmxfeatures.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/include/asm/vmxfeatures.h +1 -1
index 266daf5b5b84..695f36664889 100644
@@ -77,7 +77,7 @@
 #define VMX_FEATURE_ENCLS_EXITING	( 2*32+ 15) /* "" VM-Exit on ENCLS (leaf dependent) */
 #define VMX_FEATURE_RDSEED_EXITING	( 2*32+ 16) /* "" VM-Exit on RDSEED */
 #define VMX_FEATURE_PAGE_MOD_LOGGING	( 2*32+ 17) /* "pml" Log dirty pages into buffer */
-#define VMX_FEATURE_EPT_VIOLATION_VE	( 2*32+ 18) /* "" Conditionally reflect EPT violations as #VE exceptions */
+#define VMX_FEATURE_EPT_VIOLATION_VE	( 2*32+ 18) /* Conditionally reflect EPT violations as #VE exceptions */
 #define VMX_FEATURE_PT_CONCEAL_VMX	( 2*32+ 19) /* "" Suppress VMX indicators in Processor Trace */
 #define VMX_FEATURE_XSAVES		( 2*32+ 20) /* "" Enable XSAVES and XRSTORS in guest */
 #define VMX_FEATURE_MODE_BASED_EPT_EXEC	( 2*32+ 22) /* "ept_mode_based_exec" Enable separate EPT EXEC bits for supervisor vs. user */
Signature · 1 line
2.45.0.215.g3402c0e53f-goog
author maintainer
#17 lore raw

PATCH 9/9 KVM: x86: Disable KVM_INTEL_PROVE_VE by default

In reply to Sean Christopherson

Disable KVM's "prove #VE" support by default, as it provides no functional value, and even its sanity checking benefits are relatively limited. I.e. it should be fully opt-in even on debug kernels, especially since EPT Violation #VE suppression appears to be buggy on some CPUs.

Opportunistically add a line in the help text to make it abundantly clear that KVM_INTEL_PROVE_VE should never be enabled in a production environment.

---
 arch/x86/kvm/Kconfig | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/Kconfig +3 -3
index 2a7f69abcac3..3468efc4be55 100644
@@ -97,15 +97,15 @@ config KVM_INTEL
 
 config KVM_INTEL_PROVE_VE
         bool "Check that guests do not receive #VE exceptions"
-        default KVM_PROVE_MMU || DEBUG_KERNEL
-        depends on KVM_INTEL
+        depends on KVM_INTEL && DEBUG_KERNEL && EXPERT
         help
-
           Checks that KVM's page table management code will not incorrectly
           let guests receive a virtualization exception.  Virtualization
           exceptions will be trapped by the hypervisor rather than injected
           in the guest.
 
+          This should never be enabled in a production environment.
+
           If unsure, say N.
 
 config X86_SGX_KVM
Signature · 1 line
2.45.0.215.g3402c0e53f-goog
maintainer
#18 lore raw

Re: [PATCH 9/9] KVM: x86: Disable KVM_INTEL_PROVE_VE by default

Quoted 4 lines · Sean Christopherson

Disable KVM's "prove #VE" support by default, as it provides no functional value, and even its sanity checking benefits are relatively limited. I.e. it should be fully opt-in even on debug kernels, especially since EPT Violation #VE suppression appears to be buggy on some CPUs.

More #VE trapping than #VE suppression.

I wouldn't go so far as making it *depend* on DEBUG_KERNEL. EXPERT plus the scary help message is good enough.

What about this:

arch/x86/kvm/Kconfig +3 -2
index b6831e17ec31..2864608c7016 100644
@@ -97,14 +97,15 @@ config KVM_INTEL
 
 config KVM_INTEL_PROVE_VE
         bool "Check that guests do not receive #VE exceptions"
-        depends on KVM_INTEL && DEBUG_KERNEL && EXPERT
+        depends on KVM_INTEL && EXPERT
         help
           Checks that KVM's page table management code will not incorrectly
           let guests receive a virtualization exception.  Virtualization
           exceptions will be trapped by the hypervisor rather than injected
           in the guest.
 
-          This should never be enabled in a production environment.
+          Note that #VE trapping appears to be buggy on some CPUs.
+          This should never be enabled in a production environment!
 
           If unsure, say N.
 

Paolo

Quoted 36 lines

Opportunistically add a line in the help text to make it abundantly clear that KVM_INTEL_PROVE_VE should never be enabled in a production environment.

---
 arch/x86/kvm/Kconfig | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kvm/Kconfig +3 -4
index 2a7f69abcac3..3468efc4be55 100644
@@ -97,15 +97,15 @@ config KVM_INTEL
 
 config KVM_INTEL_PROVE_VE
         bool "Check that guests do not receive #VE exceptions"
-        default KVM_PROVE_MMU || DEBUG_KERNEL
-        depends on KVM_INTEL
+        depends on KVM_INTEL && DEBUG_KERNEL && EXPERT
         help
-
           Checks that KVM's page table management code will not incorrectly
           let guests receive a virtualization exception.  Virtualization
           exceptions will be trapped by the hypervisor rather than injected
           in the guest.
 
+          This should never be enabled in a production environment.
+
           If unsure, say N.
 
 config X86_SGX_KVM
--

2.45.0.215.g3402c0e53f-goog

author maintainer
#19 lore raw

Re: [PATCH 9/9] KVM: x86: Disable KVM_INTEL_PROVE_VE by default

Quoted 10 lines · Paolo Bonzini
Quoted 4 lines · Sean Christopherson

Disable KVM's "prove #VE" support by default, as it provides no functional value, and even its sanity checking benefits are relatively limited. I.e. it should be fully opt-in even on debug kernels, especially since EPT Violation #VE suppression appears to be buggy on some CPUs.

More #VE trapping than #VE suppression.

I wouldn't go so far as making it *depend* on DEBUG_KERNEL. EXPERT plus the scary help message is good enough.

Works for me.

Quoted 11 lines

What about this:

arch/x86/kvm/Kconfig +0 -0
index b6831e17ec31..2864608c7016 100644
@@ -97,14 +97,15 @@ config KVM_INTEL
 
 config KVM_INTEL_PROVE_VE
         bool "Check that guests do not receive #VE exceptions"

arch/x86/kvm/Kconfig config KVM_INTEL · line 107

Quoted 10 lines
arch/x86/kvm/Kconfig +2 -2
-        depends on KVM_INTEL && DEBUG_KERNEL && EXPERT
+        depends on KVM_INTEL && EXPERT
         help
           Checks that KVM's page table management code will not incorrectly
           let guests receive a virtualization exception.  Virtualization
           exceptions will be trapped by the hypervisor rather than injected
           in the guest.
 
-          This should never be enabled in a production environment.
+          Note that #VE trapping appears to be buggy on some CPUs.

I see where you're coming from, but I don't think "trapping" is much better, e.g. it suggests there's something broken with the interception of #VEs. Ah, the entire help text is weird.

This?

config KVM_INTEL_PROVE_VE
bool "Verify guests do not receive unexpected EPT Violation #VEs" depends on KVM_INTEL && EXPERT
help
Enable EPT Violation #VEs (when supported) for all VMs, to verify that KVM's EPT management code will not incorrectly result in a #VE (KVM is supposed to supress #VEs by default). Unexpected #VEs will be intercepted by KVM and will trigger a WARN, but are otherwise transparent to the guest.

Note, EPT Violation #VE support appears to be buggy on some CPUs.

This should never be enabled in a production environment!

If unsure, say N.

maintainer
#20 lore raw

Re: [PATCH 9/9] KVM: x86: Disable KVM_INTEL_PROVE_VE by default

Quoted 6 lines · Sean Christopherson

arch/x86/kvm/Kconfig config KVM_INTEL · line 107

Quoted 2 lines
arch/x86/kvm/Kconfig +1 -1
-          This should never be enabled in a production environment.
+          Note that #VE trapping appears to be buggy on some CPUs.

I see where you're coming from, but I don't think "trapping" is much better, e.g. it suggests there's something broken with the interception of #VEs. Ah, the entire help text is weird.

Yeah, I didn't want to say #VE is broken altogether - interception is where we saw issues, and #VE is used in production as far as I know (not just by TDX; at least Xen and maybe Hyper-V use it for anti-malware purposes?).

Maybe "Note: there appear to be bugs in some CPUs that will trigger the WARN, in particular with eptad=0 and/or nested virtualization" covers all bases.

Paolo

Quoted 19 lines

This?

config KVM_INTEL_PROVE_VE
bool "Verify guests do not receive unexpected EPT Violation #VEs" depends on KVM_INTEL && EXPERT
help
Enable EPT Violation #VEs (when supported) for all VMs, to verify that KVM's EPT management code will not incorrectly result in a #VE (KVM is supposed to supress #VEs by default). Unexpected #VEs will be intercepted by KVM and will trigger a WARN, but are otherwise transparent to the guest.

Note, EPT Violation #VE support appears to be buggy on some CPUs.

This should never be enabled in a production environment!

If unsure, say N.

author maintainer
#21 lore raw

Re: [PATCH 9/9] KVM: x86: Disable KVM_INTEL_PROVE_VE by default

Quoted 9 lines · Paolo Bonzini
Quoted 6 lines · Sean Christopherson

arch/x86/kvm/Kconfig config KVM_INTEL · line 107

Quoted 2 lines
arch/x86/kvm/Kconfig +1 -1
-          This should never be enabled in a production environment.
+          Note that #VE trapping appears to be buggy on some CPUs.

I see where you're coming from, but I don't think "trapping" is much better, e.g. it suggests there's something broken with the interception of #VEs. Ah, the entire help text is weird.

Yeah, I didn't want to say #VE is broken altogether -

Ah, yeah, good call. The #VE isn't broken per se, just spurious/unexpected.

Quoted 1 line

interception is where we saw issues,

It's not an issue with interception, disabling #VE intercepts results in the #VE being delivered to the guest.

Test suite: ept_access_test_not_present
PTE[4] @ 109fff8 = 9fed0007
PTE[3] @ 9fed0ff0 = 9fed1007
PTE[2] @ 9fed1000 = 9fed2007
VA PTE @ 9fed2000 = 8000000007
Created EPT @ 9feca008 = 11d2007
Created EPT @ 11d2000 = 11d3007
Created EPT @ 11d3000 = 11d4007
L1 hva = 40000000, hpa = 40000000, L2 gva = ffffffff80000000, gpa = 8000000000 Unhandled exception 8 #DF at ip 0000000000410d39
error_code=0000 rflags=00010097 cs=00000008
rax=ffffffff80000000 rcx=0000000000000000 rdx=0000000000000000 rbx=0000000000000000 rbp=000000009fec6fe0 rsi=0000000000000000 rdi=0000000000000000
r8=0000000000000000 r9=0000000000000000 r10=0000000000000000 r11=0000000000000000
r12=ffffffff80000008 r13=0000000000000000 r14=0000000000000000 r15=0000000000000000 cr0=0000000080010031 cr2=0000000000000000 cr3=000000000109f000 cr4=0000000000002020 cr8=0000000000000000

	STACK: @410d39 40144a 4002dd
Quoted 2 lines

and #VE is used in production as far as I know (not just by TDX; at least Xen and maybe Hyper-V use it for anti-malware purposes?).

Hmm, maybe a spurious #VE is benign? Or it really is limited to A/D bits being disabled? Not that us speculating is going to change anything :-)

Quoted 3 lines

Maybe "Note: there appear to be bugs in some CPUs that will trigger the WARN, in particular with eptad=0 and/or nested virtualization" covers all bases.

Works for me. Maybe tweak it slightly to explain why the WARN is triggered?

Note, some CPUs appear to generate spurious EPT Violations #VEs that trigger KVM's WARN, in particular with eptad=0 and/or nested virtualization.