Skip to contentSkip to conversation

KVM: TDX: Syntehsize SHUTDOWN on unhandled EPT Violation

PATCH Applied Sean Christopherson 4 participants 20 messages Sep 30 – Nov 13

Subsystems (4)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

v2 cover KVM: TDX: Syntehsize SHUTDOWN on unhandled EPT Violation

Signal SHUTDOWN instead of -EIO if the guest accesses an unaccepted page and has disabled EPT Violation #VEs on such accesses. Returning -EIO is all but guaranteed to mislead the VMM into thinking KVM (or the VMM) messed up, and will likely result in the VM being terminated instead of rebooted.

v2:
- Return SHUTDOWN directly, via a new helper. [Rick]
- Ratelimit the error message. [Sashiko, (and past me, amusingly)]

v1: https://lore.kernel.org/all/[email protected]

Sean Christopherson (3):
KVM: x86: Add a helper to prepare vcpu->run for a SHUTDOWN exit to
userspace
KVM: TDX: Synthesize SHUTDOWN instead of returning -EIO on unhandled
EPT violation
KVM: TDX: Ratelimit the "EPT Violation on pending acceptance page"
warning

 arch/x86/kvm/svm/svm.c   |  3 +--
 arch/x86/kvm/vmx/tdx.c   | 11 +++++------
 arch/x86/kvm/vmx/vmx.c   |  3 +--
 arch/x86/kvm/x86.c       |  3 +--
 include/linux/kvm_host.h |  8 ++++++++
 5 files changed, 16 insertions(+), 12 deletions(-)

base-commit: a0bc8e1d7a82bb143b8f8f44ae3a01938f37c2ea

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author maintainer
#1 lore raw

PATCH v2 1/3 KVM: x86: Add a helper to prepare vcpu->run for a SHUTDOWN exit to userspace

Add kvm_prepare_shutdown_exit() to dedup the paths that exit to userspace with KVM_EXIT_SHUTDOWN, all of which happen to be x86 (presumably other architectures use KVM_EXIT_SYSTEM_EVENT?).

Note, this adds a clearing of mmio_needed for SVM's shutdown_interception(). Not clearing mmio_needed in that case is all but guaranteed to be benign; KVM should never enter the guest with outstanding emulated MMIO operations (the equivalent VMX and TDX paths likely copy+pasted the code from KVM_REQ_TRIPLE_FAULT, which does need to clear mmio_needed as KVM can synthesize a triple fault shutdown in the middle of instruction emulation).

---
 arch/x86/kvm/svm/svm.c   | 3 +--
 arch/x86/kvm/vmx/tdx.c   | 3 +--
 arch/x86/kvm/vmx/vmx.c   | 3 +--
 arch/x86/kvm/x86.c       | 3 +--
 include/linux/kvm_host.h | 8 ++++++++
 5 files changed, 12 insertions(+), 8 deletions(-)
arch/x86/kvm/svm/svm.c +1 -2
index 0eb1623052c1..d2feb57d774f 100644
@@ -2190,7 +2190,6 @@ static int mc_interception(struct kvm_vcpu *vcpu)
 
 static int shutdown_interception(struct kvm_vcpu *vcpu)
 {
-	struct kvm_run *kvm_run = vcpu->run;
 	struct vcpu_svm *svm = to_svm(vcpu);
 
 
@@ -2214,7 +2213,7 @@ static int shutdown_interception(struct kvm_vcpu *vcpu)
 		kvm_vcpu_reset(vcpu, true);
 	}
 
-	kvm_run->exit_reason = KVM_EXIT_SHUTDOWN;
+	kvm_prepare_shutdown_exit(vcpu);
 	return 0;
 }
 
arch/x86/kvm/vmx/tdx.c +1 -2
index ee1d412b0ee6..29d4751f37fb 100644
@@ -2092,8 +2092,7 @@ int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)
 
 	switch (exit_reason.basic) {
 	case EXIT_REASON_TRIPLE_FAULT:
-		vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
-		vcpu->mmio_needed = 0;
+		kvm_prepare_shutdown_exit(vcpu);
 		return 0;
 	case EXIT_REASON_EXCEPTION_NMI:
 		return tdx_handle_exception_nmi(vcpu);
arch/x86/kvm/vmx/vmx.c +1 -2
index dc635145eeaf..35db7197c586 100644
@@ -5588,8 +5588,7 @@ static __always_inline int handle_external_interrupt(struct kvm_vcpu *vcpu)
 
 static int handle_triple_fault(struct kvm_vcpu *vcpu)
 {
-	vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
-	vcpu->mmio_needed = 0;
+	kvm_prepare_shutdown_exit(vcpu);
 	return 0;
 }
 
arch/x86/kvm/x86.c +1 -2
index cf3fcdfd8ad2..cffd1b6ef789 100644
@@ -8139,8 +8139,7 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 				kvm_nested_call(triple_fault)(vcpu);
 
 			if (kvm_check_request(KVM_REQ_TRIPLE_FAULT, vcpu)) {
-				vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
-				vcpu->mmio_needed = 0;
+				kvm_prepare_shutdown_exit(vcpu);
 				r = 0;
 				goto out;
 			}
include/linux/kvm_host.h +8 -0
index a836756baebb..02ca5d5d83b5 100644
@@ -2522,6 +2522,14 @@ static inline void kvm_account_pgtable_pages(void *virt, int nr)
 /* Max number of entries allowed for each kvm dirty ring */
 #define  KVM_DIRTY_RING_MAX_ENTRIES  65536
 
+static inline void kvm_prepare_shutdown_exit(struct kvm_vcpu *vcpu)
+{
+	vcpu->run->exit_reason = KVM_EXIT_SHUTDOWN;
+#ifdef CONFIG_HAS_IOMEM
+	vcpu->mmio_needed = 0;
+#endif
+}
+
 static inline void kvm_prepare_memory_fault_exit(struct kvm_vcpu *vcpu,
 						 gpa_t gpa, gpa_t size,
 						 bool is_write, bool is_exec,
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Reviewed-by on v2 1/3
#2 lore raw

Re: [PATCH v2 1/3] KVM: x86: Add a helper to prepare vcpu->run for a SHUTDOWN exit to userspace

Quoted 11 lines · Sean Christopherson

Add kvm_prepare_shutdown_exit() to dedup the paths that exit to userspace with KVM_EXIT_SHUTDOWN, all of which happen to be x86 (presumably other architectures use KVM_EXIT_SYSTEM_EVENT?).

Note, this adds a clearing of mmio_needed for SVM's shutdown_interception(). Not clearing mmio_needed in that case is all but guaranteed to be benign; KVM should never enter the guest with outstanding emulated MMIO operations (the equivalent VMX and TDX paths likely copy+pasted the code from KVM_REQ_TRIPLE_FAULT, which does need to clear mmio_needed as KVM can synthesize a triple fault shutdown in the middle of instruction emulation).

Quoted 2 lines
author maintainer
#3 lore raw

PATCH v2 2/3 KVM: TDX: Synthesize SHUTDOWN instead of returning -EIO on unhandled EPT violation

In reply to Sean Christopherson

Exit to userspace with KVM_EXIT_SHUTDOWN instead of returning -EIO from KVM_RUN if KVM encounters an EPT Violation due to a guest access to a pending page. Returning -EIO implies KVM is buggy, and most VMMs will respond by completely terminating the VM, versus rebooting the VM in response to KVM_EXIT_SHUTDOWN. I.e. give the VMM the option of trying to keep the VM (from the end user's perspective) alive.

Ideally, KVM would probably exit with KVM_EXIT_MEMORY_FAULT, but KVM would need to extend run->memory_fault so that userspace knows the fault can't be handled. This scenario specifically occurs when the guest has deliberately disabled #VEs on unaccepted memory for security purposes, i.e. the guest literally disabled the mechanism that tells it it screwed up. But, because this is fatal, and the whole point is to NOT try to fixup the fault, jumping through hoops to return MEMORY_FAULT instead of SHUTDOWN doesn't make a whole lot of sense.

Don't bother bouncing through KVM_REQ_TRIPLE_FAULT as tdx_handle_ept_violation() is a top-level exit handler, i.e. there is no need to worry about failing to actually exit to userspace.

Fixes e6a85781f783 ("KVM: TDX: Detect unexpected SEPT violations due to pending SPTEs")
Cc James Houghton <[email protected]>
Cc Xiaoyao Li <[email protected]>
Cc Rick Edgecombe <[email protected]>
Cc Yan Zhao <[email protected]>
Cc Binbin Wu <[email protected]>
Cc Ackerley Tng <[email protected]>
Cc Vishal Annapurve <[email protected]>
---
 arch/x86/kvm/vmx/tdx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/tdx.c +2 -2
index 29d4751f37fb..e3723f1222fc 100644
@@ -1939,8 +1939,8 @@ static int tdx_handle_ept_violation(struct kvm_vcpu *vcpu)
 		if (tdx_is_sept_violation_unexpected_pending(vcpu)) {
 			pr_warn("Guest access before accepting 0x%llx on vCPU %d\n",
 				gpa, vcpu->vcpu_id);
-			kvm_vm_dead(vcpu->kvm);
-			return -EIO;
+			kvm_prepare_shutdown_exit(vcpu);
+			return 0;
 		}
 		/*
 		 * Always treat SEPT violations as write faults.  Ignore the
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Reviewed-by on v2 2/3
#4 lore raw

Re: [PATCH v2 2/3] KVM: TDX: Synthesize SHUTDOWN instead of returning -EIO on unhandled EPT violation

Quoted 20 lines · Sean Christopherson

Exit to userspace with KVM_EXIT_SHUTDOWN instead of returning -EIO from KVM_RUN if KVM encounters an EPT Violation due to a guest access to a pending page. Returning -EIO implies KVM is buggy, and most VMMs will respond by completely terminating the VM, versus rebooting the VM in response to KVM_EXIT_SHUTDOWN. I.e. give the VMM the option of trying to keep the VM (from the end user's perspective) alive.

Ideally, KVM would probably exit with KVM_EXIT_MEMORY_FAULT, but KVM would need to extend run->memory_fault so that userspace knows the fault can't be handled. This scenario specifically occurs when the guest has deliberately disabled #VEs on unaccepted memory for security purposes, i.e. the guest literally disabled the mechanism that tells it it screwed up. But, because this is fatal, and the whole point is to NOT try to fixup the fault, jumping through hoops to return MEMORY_FAULT instead of SHUTDOWN doesn't make a whole lot of sense.

Don't bother bouncing through KVM_REQ_TRIPLE_FAULT as tdx_handle_ept_violation() is a top-level exit handler, i.e. there is no need to worry about failing to actually exit to userspace.

Quoted 10 lines
Fixes e6a85781f783 ("KVM: TDX: Detect unexpected SEPT violations due to pending SPTEs")
Cc James Houghton <[email protected]>
Cc Xiaoyao Li <[email protected]>
Cc Rick Edgecombe <[email protected]>
Cc Yan Zhao <[email protected]>
Cc Binbin Wu <[email protected]>
Cc Ackerley Tng <[email protected]>
Cc Vishal Annapurve <[email protected]>
Quoted 19 lines
---
 arch/x86/kvm/vmx/tdx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/tdx.c +2 -2
index 29d4751f37fb..e3723f1222fc 100644
@@ -1939,8 +1939,8 @@ static int tdx_handle_ept_violation(struct kvm_vcpu *vcpu)
 		if (tdx_is_sept_violation_unexpected_pending(vcpu)) {
 			pr_warn("Guest access before accepting 0x%llx on vCPU %d\n",
 				gpa, vcpu->vcpu_id);
-			kvm_vm_dead(vcpu->kvm);
-			return -EIO;
+			kvm_prepare_shutdown_exit(vcpu);
+			return 0;
 		}
 		/*
 		 * Always treat SEPT violations as write faults.  Ignore the
#5 lore raw

Re: [PATCH v2 2/3] KVM: TDX: Synthesize SHUTDOWN instead of returning -EIO on unhandled EPT violation

In reply to Sean Christopherson
Quoted 39 lines · Sean Christopherson

Exit to userspace with KVM_EXIT_SHUTDOWN instead of returning -EIO from KVM_RUN if KVM encounters an EPT Violation due to a guest access to a pending page. Returning -EIO implies KVM is buggy, and most VMMs will respond by completely terminating the VM, versus rebooting the VM in response to KVM_EXIT_SHUTDOWN. I.e. give the VMM the option of trying to keep the VM (from the end user's perspective) alive.

Ideally, KVM would probably exit with KVM_EXIT_MEMORY_FAULT, but KVM would need to extend run->memory_fault so that userspace knows the fault can't be handled. This scenario specifically occurs when the guest has deliberately disabled #VEs on unaccepted memory for security purposes, i.e. the guest literally disabled the mechanism that tells it it screwed up. But, because this is fatal, and the whole point is to NOT try to fixup the fault, jumping through hoops to return MEMORY_FAULT instead of SHUTDOWN doesn't make a whole lot of sense.

Don't bother bouncing through KVM_REQ_TRIPLE_FAULT as tdx_handle_ept_violation() is a top-level exit handler, i.e. there is no need to worry about failing to actually exit to userspace.

Fixes e6a85781f783 ("KVM: TDX: Detect unexpected SEPT violations due to pending SPTEs")
Cc James Houghton <[email protected]>
Cc Xiaoyao Li <[email protected]>
Cc Rick Edgecombe <[email protected]>
Cc Yan Zhao <[email protected]>
Cc Binbin Wu <[email protected]>
Cc Ackerley Tng <[email protected]>
Cc Vishal Annapurve <[email protected]>
---
 arch/x86/kvm/vmx/tdx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/tdx.c +0 -0
index 29d4751f37fb..e3723f1222fc 100644

arch/x86/kvm/vmx/tdx.c static int tdx_handle_ept_violation(struct kvm_vcpu *vcpu) · line 1942

Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +1 -2
@@ -1939,8 +1939,8 @@ static int tdx_handle_ept_violation(struct kvm_vcpu *vcpu)
 		if (tdx_is_sept_violation_unexpected_pending(vcpu)) {
 			pr_warn("Guest access before accepting 0x%llx on vCPU %d\n",
 				gpa, vcpu->vcpu_id);
-			kvm_vm_dead(vcpu->kvm);
-			return -EIO;
+			kvm_prepare_shutdown_exit(vcpu);

Would it be better to exit with the reason KVM_EXIT_SYSTEM_EVENT and type KVM_SYSTEM_EVENT_SHUTDOWN or KVM_SYSTEM_EVENT_TDX_FATAL?
By defining a specific flag (e.g., KVM_SYSTEM_EVENT_FLAG_TDX_UNEXPECTED_PENDING), we can stuff necessary info like gpa, vcpu->vcpu_id into data, so both pr_warn() and pr_warn_ratelimited() can be dropped.

Quoted 7 lines
arch/x86/kvm/vmx/tdx.c +1 -0
+			return 0;
 		}
 		/*
 		 * Always treat SEPT violations as write faults.  Ignore the
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
author maintainer
#6 lore raw

Re: [PATCH v2 2/3] KVM: TDX: Synthesize SHUTDOWN instead of returning -EIO on unhandled EPT violation

Quoted 35 lines · Yan Zhao
Quoted 34 lines · Sean Christopherson

Exit to userspace with KVM_EXIT_SHUTDOWN instead of returning -EIO from KVM_RUN if KVM encounters an EPT Violation due to a guest access to a pending page. Returning -EIO implies KVM is buggy, and most VMMs will respond by completely terminating the VM, versus rebooting the VM in response to KVM_EXIT_SHUTDOWN. I.e. give the VMM the option of trying to keep the VM (from the end user's perspective) alive.

Ideally, KVM would probably exit with KVM_EXIT_MEMORY_FAULT, but KVM would need to extend run->memory_fault so that userspace knows the fault can't be handled. This scenario specifically occurs when the guest has deliberately disabled #VEs on unaccepted memory for security purposes, i.e. the guest literally disabled the mechanism that tells it it screwed up. But, because this is fatal, and the whole point is to NOT try to fixup the fault, jumping through hoops to return MEMORY_FAULT instead of SHUTDOWN doesn't make a whole lot of sense.

Don't bother bouncing through KVM_REQ_TRIPLE_FAULT as tdx_handle_ept_violation() is a top-level exit handler, i.e. there is no need to worry about failing to actually exit to userspace.

Fixes e6a85781f783 ("KVM: TDX: Detect unexpected SEPT violations due to pending SPTEs")
Cc James Houghton <[email protected]>
Cc Xiaoyao Li <[email protected]>
Cc Rick Edgecombe <[email protected]>
Cc Yan Zhao <[email protected]>
Cc Binbin Wu <[email protected]>
Cc Ackerley Tng <[email protected]>
Cc Vishal Annapurve <[email protected]>
---
 arch/x86/kvm/vmx/tdx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 13 lines

arch/x86/kvm/vmx/tdx.c static int tdx_handle_ept_violation(struct kvm_vcpu *vcpu) · line 1942

Quoted 11 lines
arch/x86/kvm/vmx/tdx.c +1 -2
index 29d4751f37fb..e3723f1222fc 100644
@@ -1939,8 +1939,8 @@ static int tdx_handle_ept_violation(struct kvm_vcpu *vcpu)
 		if (tdx_is_sept_violation_unexpected_pending(vcpu)) {
 			pr_warn("Guest access before accepting 0x%llx on vCPU %d\n",
 				gpa, vcpu->vcpu_id);
-			kvm_vm_dead(vcpu->kvm);
-			return -EIO;
+			kvm_prepare_shutdown_exit(vcpu);

Would it be better to exit with the reason KVM_EXIT_SYSTEM_EVENT and type KVM_SYSTEM_EVENT_SHUTDOWN or KVM_SYSTEM_EVENT_TDX_FATAL?

No. KVM_SYSTEM_EVENT_SHUTDOWN is an explicit call from the guest to do a clean shutdown (used by arm64 and RISC-V). KVM_SYSTEM_EVENT_TDX_FATAL similarly exists to relay explicit calls from the guest. Ditto for KVM_SYSTEM_EVENT_CRASH and KVM_SYSTEM_EVENT_SEV_TERM (which obviously is a poor fit for TDX, just pointing out that it's another guest-initiated event).

KVM_EXIT_SHUTDOWN isn't a perfect fit, but IMO it's the closest approximation, e.g. it's a rough equivalent of what happens if a #VE couldn't be handled because the stack is invalid. And more importantly, because we're retroactively fixing code, KVM_EXIT_SHUTDOWN has the best chance of being handled appropriately by all VMMs.

Quoted 3 lines

By defining a specific flag (e.g., KVM_SYSTEM_EVENT_FLAG_TDX_UNEXPECTED_PENDING), we can stuff necessary info like gpa, vcpu->vcpu_id into data, so both pr_warn() and pr_warn_ratelimited() can be dropped.

Well, IMO, the printk can be dropped no matter what. :-) But I don't care _that_ much so I'm ok keeping the ratelimited version.

vcpu_id is redundant, the exit is tied to a specific vCPU. And as I said elsewhere, practically speaking the GPA is all but guaranteed to be useless outside of interactive debug sessions.

author maintainer
#7 lore raw

PATCH v2 3/3 KVM: TDX: Ratelimit the "EPT Violation on pending acceptance page" warning

In reply to Sean Christopherson

Ratelimit TDX's kernel logging when a guest accesses an unaccepted page and has opted to disable EPT Violation #VEs for unaccepted accesses, as the pr_warn() is trivial for a misbehaving userspace to trigger.

Fixes e6a85781f783 ("KVM: TDX: Detect unexpected SEPT violations due to pending SPTEs")
---
 arch/x86/kvm/vmx/tdx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/tdx.c +2 -2
index e3723f1222fc..4cac43299851 100644
@@ -1937,8 +1937,8 @@ static int tdx_handle_ept_violation(struct kvm_vcpu *vcpu)
 
 	if (vt_is_tdx_private_gpa(vcpu->kvm, gpa)) {
 		if (tdx_is_sept_violation_unexpected_pending(vcpu)) {
-			pr_warn("Guest access before accepting 0x%llx on vCPU %d\n",
-				gpa, vcpu->vcpu_id);
+			pr_warn_ratelimited("Guest access before accepting 0x%llx on vCPU %d\n",
+					    gpa, vcpu->vcpu_id);
 			kvm_prepare_shutdown_exit(vcpu);
 			return 0;
 		}
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
Reviewed-by on v2 3/3
#8 lore raw

Re: [PATCH v2 3/3] KVM: TDX: Ratelimit the "EPT Violation on pending acceptance page" warning

Quoted 9 lines · Sean Christopherson

Ratelimit TDX's kernel logging when a guest accesses an unaccepted page and has opted to disable EPT Violation #VEs for unaccepted accesses, as the pr_warn() is trivial for a misbehaving userspace to trigger.

Fixes e6a85781f783 ("KVM: TDX: Detect unexpected SEPT violations due to pending SPTEs")

Nit: should this patch be put before patch 2, to avoid a bisect window?

Otherwise,

Quoted 19 lines
---
 arch/x86/kvm/vmx/tdx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/tdx.c +2 -2
index e3723f1222fc..4cac43299851 100644
@@ -1937,8 +1937,8 @@ static int tdx_handle_ept_violation(struct kvm_vcpu *vcpu)
 
 	if (vt_is_tdx_private_gpa(vcpu->kvm, gpa)) {
 		if (tdx_is_sept_violation_unexpected_pending(vcpu)) {
-			pr_warn("Guest access before accepting 0x%llx on vCPU %d\n",
-				gpa, vcpu->vcpu_id);
+			pr_warn_ratelimited("Guest access before accepting 0x%llx on vCPU %d\n",
+					    gpa, vcpu->vcpu_id);
 			kvm_prepare_shutdown_exit(vcpu);
 			return 0;
 		}
#9 lore raw

Re: [PATCH v2 3/3] KVM: TDX: Ratelimit the "EPT Violation on pending acceptance page" warning

In reply to Sean Christopherson
Quoted 3 lines · Sean Christopherson

Ratelimit TDX's kernel logging when a guest accesses an unaccepted page and has opted to disable EPT Violation #VEs for unaccepted accesses, as the pr_warn() is trivial for a misbehaving userspace to trigger.

Since the pr_warn() is previously accompanied by a kvm_vm_dead(), which kicks off all vCPUs and disallows re-entry, a misbehaving guest should not be able to trigger pr_warn() more than once.
Or are you concerned about the pr_warn() being triggered by multiple misbehaving guests? But would it be better to print for each guest?

Quoted 27 lines
Fixes e6a85781f783 ("KVM: TDX: Detect unexpected SEPT violations due to pending SPTEs")
---
 arch/x86/kvm/vmx/tdx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/tdx.c +2 -2
index e3723f1222fc..4cac43299851 100644
@@ -1937,8 +1937,8 @@ static int tdx_handle_ept_violation(struct kvm_vcpu *vcpu)
 
 	if (vt_is_tdx_private_gpa(vcpu->kvm, gpa)) {
 		if (tdx_is_sept_violation_unexpected_pending(vcpu)) {
-			pr_warn("Guest access before accepting 0x%llx on vCPU %d\n",
-				gpa, vcpu->vcpu_id);
+			pr_warn_ratelimited("Guest access before accepting 0x%llx on vCPU %d\n",
+					    gpa, vcpu->vcpu_id);
 			kvm_prepare_shutdown_exit(vcpu);
 			return 0;
 		}
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
#10 lore raw

Re: [PATCH v2 3/3] KVM: TDX: Ratelimit the "EPT Violation on pending acceptance page" warning

Quoted 9 lines · Yan Zhao
Quoted 3 lines · Sean Christopherson

Ratelimit TDX's kernel logging when a guest accesses an unaccepted page and has opted to disable EPT Violation #VEs for unaccepted accesses, as the pr_warn() is trivial for a misbehaving userspace to trigger.

Since the pr_warn() is previously accompanied by a kvm_vm_dead(), which kicks off all vCPUs and disallows re-entry, a misbehaving guest should not be able to trigger pr_warn() more than once.
Or are you concerned about the pr_warn() being triggered by multiple misbehaving guests? But would it be better to print for each guest?

Sashiko reported the issue due to the change introduced in the patch 2.

Will this change allow a malicious or buggy userspace VMM to flood the host kernel log?

By replacing kvm_vm_dead() with KVM_REQ_TRIPLE_FAULT and returning 1, KVM will clear the request during vcpu_enter_guest() and exit to userspace with KVM_EXIT_SHUTDOWN. If the VMM ignores this exit and repeatedly calls KVM_RUN without resetting the vCPU, KVM will immediately re-enter the guest.

In this case, is the Fixes tag pointing to e6a85781f783 needed?

Quoted 29 lines
Quoted 28 lines
Fixes e6a85781f783 ("KVM: TDX: Detect unexpected SEPT violations due to pending SPTEs")
---
 arch/x86/kvm/vmx/tdx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/tdx.c +2 -2
index e3723f1222fc..4cac43299851 100644
@@ -1937,8 +1937,8 @@ static int tdx_handle_ept_violation(struct kvm_vcpu *vcpu)
 
 	if (vt_is_tdx_private_gpa(vcpu->kvm, gpa)) {
 		if (tdx_is_sept_violation_unexpected_pending(vcpu)) {
-			pr_warn("Guest access before accepting 0x%llx on vCPU %d\n",
-				gpa, vcpu->vcpu_id);
+			pr_warn_ratelimited("Guest access before accepting 0x%llx on vCPU %d\n",
+					    gpa, vcpu->vcpu_id);
 			kvm_prepare_shutdown_exit(vcpu);
 			return 0;
 		}
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
#11 lore raw

Re: [PATCH v2 3/3] KVM: TDX: Ratelimit the "EPT Violation on pending acceptance page" warning

Quoted 16 lines · Binbin Wu
Quoted 9 lines · Yan Zhao
Quoted 3 lines · Sean Christopherson

Ratelimit TDX's kernel logging when a guest accesses an unaccepted page and has opted to disable EPT Violation #VEs for unaccepted accesses, as the pr_warn() is trivial for a misbehaving userspace to trigger.

Since the pr_warn() is previously accompanied by a kvm_vm_dead(), which kicks off all vCPUs and disallows re-entry, a misbehaving guest should not be able to trigger pr_warn() more than once.
Or are you concerned about the pr_warn() being triggered by multiple misbehaving guests? But would it be better to print for each guest?

Sashiko reported the issue due to the change introduced in the patch 2.

Will this change allow a malicious or buggy userspace VMM to flood the host kernel log?

Quoted 6 lines

By replacing kvm_vm_dead() with KVM_REQ_TRIPLE_FAULT and returning 1, KVM will clear the request during vcpu_enter_guest() and exit to userspace with KVM_EXIT_SHUTDOWN. If the VMM ignores this exit and repeatedly calls KVM_RUN without resetting the vCPU, KVM will immediately re-enter the guest.

In this case, is the Fixes tag pointing to e6a85781f783 needed?

Upon further reflection, in an extreme scenario where 100 vCPUs access the unaccepted page simultaneously, pr_warn() could theoretically be invoked for 100 times.
So pr_warn_ratelimited() is still the better choice.

So, Reviewed-by: Yan Zhao <[email protected]>

Quoted 29 lines
Quoted 28 lines
Quoted 27 lines
Fixes e6a85781f783 ("KVM: TDX: Detect unexpected SEPT violations due to pending SPTEs")
---
 arch/x86/kvm/vmx/tdx.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/tdx.c +2 -2
index e3723f1222fc..4cac43299851 100644
@@ -1937,8 +1937,8 @@ static int tdx_handle_ept_violation(struct kvm_vcpu *vcpu)
 
 	if (vt_is_tdx_private_gpa(vcpu->kvm, gpa)) {
 		if (tdx_is_sept_violation_unexpected_pending(vcpu)) {
-			pr_warn("Guest access before accepting 0x%llx on vCPU %d\n",
-				gpa, vcpu->vcpu_id);
+			pr_warn_ratelimited("Guest access before accepting 0x%llx on vCPU %d\n",
+					    gpa, vcpu->vcpu_id);
 			kvm_prepare_shutdown_exit(vcpu);
 			return 0;
 		}
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
#12 lore raw
In reply to Sean Christopherson
Quoted 4 lines · Sean Christopherson

Signal SHUTDOWN instead of -EIO if the guest accesses an unaccepted page and has disabled EPT Violation #VEs on such accesses.  Returning -EIO is all but guaranteed to mislead the VMM into thinking KVM (or the VMM) messed up, and will likely result in the VM being terminated instead of rebooted.

I'm pretty sure Yan had a test for this path, and I'd love to see it actually exercised. What is the urgency on getting this fix upstream? Can we wait a week?

author maintainer
#13 lore raw
Quoted 8 lines · Rick P Edgecombe
Quoted 4 lines · Sean Christopherson

Signal SHUTDOWN instead of -EIO if the guest accesses an unaccepted page and has disabled EPT Violation #VEs on such accesses.  Returning -EIO is all but guaranteed to mislead the VMM into thinking KVM (or the VMM) messed up, and will likely result in the VM being terminated instead of rebooted.

I'm pretty sure Yan had a test for this path, and I'd love to see it actually exercised. What is the urgency on getting this fix upstream? Can we wait a week?

Absolutely. It can probably wait a month and no one would care. IIRC, this got hit by someone (internal to Google) deliberately crashing a guest kernel and doing funky things with kexec. It showed up on my radar purely because our automated madness alerted on the resulting assertion (on -EIO) in the VMM.

#14 lore raw
Quoted 6 lines · Sean Christopherson
Quoted 5 lines · Rick P Edgecombe
Quoted 4 lines · Sean Christopherson

Signal SHUTDOWN instead of -EIO if the guest accesses an unaccepted page and has disabled EPT Violation #VEs on such accesses.  Returning -EIO is all but guaranteed to mislead the VMM into thinking KVM (or the VMM) messed up, and will likely result in the VM being terminated instead of rebooted.

Hmm, it's by design in my previous implementation to terminate a VM when the guest accesses an unaccepted page and has disabled EPT Violation #VEs on such accesses, according to the TDX module spec:

"This happens if the TD is configured to TD-exit (instead of a #VE) on an EPT
violation due to accessing a PENDING page. It normally indicates an error condition; the host VMM may decide to tear the TD down."

So, for the initial implementation, we chose to invoke kvm_vm_dead() and print out the exact reason as a hint to the system admin. (Previously, reboot was also not supported for a TDX guest).

Returning -EIO was based on the following considerations:
- vcpu_enter_guest() returns -EIO when kvm_test_request(KVM_REQ_VM_DEAD, vcpu) is true.
- A request to handle a specific EPT violation is rejected by the firmware (the TDX module).

Quoted 2 lines
Quoted 2 lines

I'm pretty sure Yan had a test for this path, and I'd love to see it actually exercised. What is the urgency on getting this fix upstream? Can we wait a week?

I triggered the EPT violation in the kexec path, and successfully had the TD
- killed with error msg: "qemu-system-x86_64: cpus are not resettable, terminating" on an old QEMU, or
- rebooted on a new QEMU with the msg printed:
"qemu-system-x86_64: info: virtual machine state has been rebuilt with new
guest file handle".

The TDX selftests we are using do not handle KVM_EXIT_SHUTDOWN, so if such an error occurs, it's silently ignored by the TDX selftests even with msg "kvm_intel: Guest access before accepting 0x8000c000 on vCPU 1" printed in dmesg.

Quoted 4 lines

Absolutely. It can probably wait a month and no one would care. IIRC, this got hit by someone (internal to Google) deliberately crashing a guest kernel and doing funky things with kexec. It showed up on my radar purely because our automated madness alerted on the resulting assertion (on -EIO) in the VMM.

Though I didn't realize that returning -EIO would mislead the VMM into thinking KVM (or the VMM) messed up, such an error could also be introduced by a VMM bug? e.g., VMM removes an accepted page without any notification to the TD configured with TDX_TD_ATTRIBUTES_SEPT_VE_DISABLE bit.

author maintainer
#15 lore raw
Quoted 11 lines · Yan Zhao
Quoted 6 lines · Sean Christopherson
Quoted 5 lines · Rick P Edgecombe
Quoted 4 lines · Sean Christopherson

Signal SHUTDOWN instead of -EIO if the guest accesses an unaccepted page and has disabled EPT Violation #VEs on such accesses.  Returning -EIO is all but guaranteed to mislead the VMM into thinking KVM (or the VMM) messed up, and will likely result in the VM being terminated instead of rebooted.

Hmm, it's by design in my previous implementation to terminate a VM when the guest accesses an unaccepted page and has disabled EPT Violation #VEs on such accesses, according to the TDX module spec:

Quoted 6 lines

"This happens if the TD is configured to TD-exit (instead of a #VE) on an EPT
violation due to accessing a PENDING page. It normally indicates an error condition; the host VMM may decide to tear the TD down."

So, for the initial implementation, we chose to invoke kvm_vm_dead() and print out the exact reason as a hint to the system admin.

kvm_vm_dead() should only be used when KVM *needs* to prevent userspace from running the VM.

Quoted 1 line

(Previously, reboot was also not supported for a TDX guest).

But QEMU isn't KVM. While QEMU is the de facto reference VMM, it's important to keep in mind that it's not the only VMM (and that's not just about Google's VMM, there are plenty of other non-QEMU VMMs that are used in production environments).

Quoted 15 lines

Returning -EIO was based on the following considerations:
- vcpu_enter_guest() returns -EIO when kvm_test_request(KVM_REQ_VM_DEAD, vcpu) is true.
- A request to handle a specific EPT violation is rejected by the firmware (the TDX module).

Quoted 2 lines
Quoted 2 lines

I'm pretty sure Yan had a test for this path, and I'd love to see it actually exercised. What is the urgency on getting this fix upstream? Can we wait a week?

I triggered the EPT violation in the kexec path, and successfully had the TD
- killed with error msg: "qemu-system-x86_64: cpus are not resettable, terminating" on an old QEMU, or
- rebooted on a new QEMU with the msg printed:
"qemu-system-x86_64: info: virtual machine state has been rebuilt with new
guest file handle".

Quoted 3 lines

The TDX selftests we are using do not handle KVM_EXIT_SHUTDOWN, so if such an error occurs, it's silently ignored by the TDX selftests even with msg "kvm_intel: Guest access before accepting 0x8000c000 on vCPU 1" printed in dmesg.

If TDX selftests don't fail due to an unexpected KVM_EXIT_SHUTDOWN, then that needs to be fixed irrespective of this change.

Quoted 6 lines
Quoted 4 lines

Absolutely. It can probably wait a month and no one would care. IIRC, this got hit by someone (internal to Google) deliberately crashing a guest kernel and doing funky things with kexec. It showed up on my radar purely because our automated madness alerted on the resulting assertion (on -EIO) in the VMM.

Though I didn't realize that returning -EIO would mislead the VMM into thinking KVM (or the VMM) messed up, such an error could also be introduced by a VMM bug?

Yes, but that holds true for literally every guest failure. It's like saying that all observed errors could be due to a CPU bug, e.g. because the CPU corrupted state or because a bit flipped in DRAM. It's technically true, but in practice the vast majority of failures (outside of pre-production hardware) are software errors, and so absent evidence that a hardware/VMM/KVM bug is at play, KVM shouldn't assume anything. I.e. as above, unless KVM needs to protect itself, KVM should aways try to provide semantics that are rooted in the CPU architecture.

E.g. an analogous failure would for non-TDX guests would be if the host zeroed a page that happened to be necessary to handle exceptions in the guest, and as a result the guest hit a triple fault shutdown. KVM doesn't return -EIO instead of KVM_EXIT_SHUTDOWN just because the failure could have been introduced by a kernel or VMM bug.

Quoted 2 lines

e.g., VMM removes an accepted page without any notification to the TD configured with TDX_TD_ATTRIBUTES_SEPT_VE_DISABLE bit.

#16 lore raw
Quoted 16 lines · Sean Christopherson
Quoted 15 lines · Yan Zhao
Quoted 6 lines · Sean Christopherson
Quoted 5 lines · Rick P Edgecombe
Quoted 4 lines · Sean Christopherson

Signal SHUTDOWN instead of -EIO if the guest accesses an unaccepted page and has disabled EPT Violation #VEs on such accesses.  Returning -EIO is all but guaranteed to mislead the VMM into thinking KVM (or the VMM) messed up, and will likely result in the VM being terminated instead of rebooted.

Hmm, it's by design in my previous implementation to terminate a VM when the guest accesses an unaccepted page and has disabled EPT Violation #VEs on such accesses, according to the TDX module spec:

"This happens if the TD is configured to TD-exit (instead of a #VE) on an EPT
violation due to accessing a PENDING page. It normally indicates an error condition; the host VMM may decide to tear the TD down."

Quoted 5 lines
Quoted 2 lines

So, for the initial implementation, we chose to invoke kvm_vm_dead() and print out the exact reason as a hint to the system admin.

kvm_vm_dead() should only be used when KVM *needs* to prevent userspace from running the VM.

Ok. But my previous intention was to kill the VM regardless of whether the userspace handles KVM_EXIT_SHUTDOWN or not.
Did I use it correctly?
I'm asking because you added the Fixes tag and "Cc: [email protected]" for patches 1/2 as well.

Quoted 5 lines
Quoted 1 line

(Previously, reboot was also not supported for a TDX guest).

But QEMU isn't KVM. While QEMU is the de facto reference VMM, it's important to keep in mind that it's not the only VMM (and that's not just about Google's VMM, there are plenty of other non-QEMU VMMs that are used in production environments).

Hmm. That was the reason for me to choose forcefully killing the VM inside KVM, since for the initial implementation, I didn't want to bother updating every VMM :)

Quoted 15 lines
Quoted 15 lines

Returning -EIO was based on the following considerations:
- vcpu_enter_guest() returns -EIO when kvm_test_request(KVM_REQ_VM_DEAD, vcpu) is true.
- A request to handle a specific EPT violation is rejected by the firmware (the TDX module).

Quoted 2 lines
Quoted 2 lines

I'm pretty sure Yan had a test for this path, and I'd love to see it actually exercised. What is the urgency on getting this fix upstream? Can we wait a week?

I triggered the EPT violation in the kexec path, and successfully had the TD
- killed with error msg: "qemu-system-x86_64: cpus are not resettable, terminating" on an old QEMU, or
- rebooted on a new QEMU with the msg printed:
"qemu-system-x86_64: info: virtual machine state has been rebuilt with new
guest file handle".

Quoted 6 lines
Quoted 3 lines

The TDX selftests we are using do not handle KVM_EXIT_SHUTDOWN, so if such an error occurs, it's silently ignored by the TDX selftests even with msg "kvm_intel: Guest access before accepting 0x8000c000 on vCPU 1" printed in dmesg.

If TDX selftests don't fail due to an unexpected KVM_EXIT_SHUTDOWN, then that needs to be fixed irrespective of this change.

Ok.

Quoted 15 lines
Quoted 6 lines
Quoted 4 lines

Absolutely. It can probably wait a month and no one would care. IIRC, this got hit by someone (internal to Google) deliberately crashing a guest kernel and doing funky things with kexec. It showed up on my radar purely because our automated madness alerted on the resulting assertion (on -EIO) in the VMM.

Though I didn't realize that returning -EIO would mislead the VMM into thinking KVM (or the VMM) messed up, such an error could also be introduced by a VMM bug?

Yes, but that holds true for literally every guest failure. It's like saying that all observed errors could be due to a CPU bug, e.g. because the CPU corrupted state or because a bit flipped in DRAM. It's technically true, but in practice the vast majority of failures (outside of pre-production hardware) are software errors, and so absent evidence that a hardware/VMM/KVM bug is at play, KVM shouldn't assume anything. I.e. as above, unless KVM needs to protect itself, KVM should aways try to provide semantics that are rooted in the CPU architecture.

Quoted 5 lines

E.g. an analogous failure would for non-TDX guests would be if the host zeroed a page that happened to be necessary to handle exceptions in the guest, and as a result the guest hit a triple fault shutdown. KVM doesn't return -EIO instead of KVM_EXIT_SHUTDOWN just because the failure could have been introduced by a kernel or VMM bug.

Makes sense. Thanks for the explanation!

Quoted 2 lines
Quoted 2 lines

e.g., VMM removes an accepted page without any notification to the TD configured with TDX_TD_ATTRIBUTES_SEPT_VE_DISABLE bit.

author maintainer
#17 lore raw
Quoted 17 lines · Yan Zhao
Quoted 16 lines · Sean Christopherson
Quoted 15 lines · Yan Zhao
Quoted 6 lines · Sean Christopherson
Quoted 5 lines · Rick P Edgecombe
Quoted 4 lines · Sean Christopherson

Signal SHUTDOWN instead of -EIO if the guest accesses an unaccepted page and has disabled EPT Violation #VEs on such accesses.  Returning -EIO is all but guaranteed to mislead the VMM into thinking KVM (or the VMM) messed up, and will likely result in the VM being terminated instead of rebooted.

Hmm, it's by design in my previous implementation to terminate a VM when the guest accesses an unaccepted page and has disabled EPT Violation #VEs on such accesses, according to the TDX module spec:

"This happens if the TD is configured to TD-exit (instead of a #VE) on an EPT
violation due to accessing a PENDING page. It normally indicates an error condition; the host VMM may decide to tear the TD down."

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

So, for the initial implementation, we chose to invoke kvm_vm_dead() and print out the exact reason as a hint to the system admin.

kvm_vm_dead() should only be used when KVM *needs* to prevent userspace from running the VM.

Ok. But my previous intention was to kill the VM regardless of whether the userspace handles KVM_EXIT_SHUTDOWN or not.
Did I use it correctly?

No. Returning -EIO kills not only what KVM sees as the VM, it often kills what the end user, i.e. the customer in our case, see as the VM.

Quoted 11 lines

I'm asking because you added the Fixes tag and "Cc: [email protected]" for patches 1/2 as well.

Quoted 5 lines
Quoted 1 line

(Previously, reboot was also not supported for a TDX guest).

But QEMU isn't KVM. While QEMU is the de facto reference VMM, it's important to keep in mind that it's not the only VMM (and that's not just about Google's VMM, there are plenty of other non-QEMU VMMs that are used in production environments).

Hmm. That was the reason for me to choose forcefully killing the VM inside KVM, since for the initial implementation, I didn't want to bother updating every VMM :)

Why would any VMM need to be updated? VMMs already need to handle KVM_EXIT_SHUTDOWN for TDX VMs.

#18 lore raw
Quoted 21 lines · Sean Christopherson
Quoted 20 lines · Yan Zhao
Quoted 19 lines · Sean Christopherson
Quoted 17 lines · Yan Zhao
Quoted 6 lines · Sean Christopherson
Quoted 5 lines · Rick P Edgecombe
Quoted 4 lines · Sean Christopherson

Signal SHUTDOWN instead of -EIO if the guest accesses an unaccepted page and has disabled EPT Violation #VEs on such accesses.  Returning -EIO is all but guaranteed to mislead the VMM into thinking KVM (or the VMM) messed up, and will likely result in the VM being terminated instead of rebooted.

Hmm, it's by design in my previous implementation to terminate a VM when the guest accesses an unaccepted page and has disabled EPT Violation #VEs on such accesses, according to the TDX module spec:

"This happens if the TD is configured to TD-exit (instead of a #VE) on an EPT
violation due to accessing a PENDING page. It normally indicates an error condition; the host VMM may decide to tear the TD down."

So, for the initial implementation, we chose to invoke kvm_vm_dead() and print out the exact reason as a hint to the system admin.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

kvm_vm_dead() should only be used when KVM *needs* to prevent userspace from running the VM.

Ok. But my previous intention was to kill the VM regardless of whether the userspace handles KVM_EXIT_SHUTDOWN or not.
Did I use it correctly?

No. Returning -EIO kills not only what KVM sees as the VM, it often kills what the end user, i.e. the customer in our case, see as the VM.

Ah. I see.
I didn't realize it was seen a bug. I thought it was something we could improve later by returning KVM_EXIT* instead.

Quoted 8 lines
Quoted 8 lines

I'm asking because you added the Fixes tag and "Cc: [email protected]" for patches 1/2 as well.

Quoted 5 lines
Quoted 1 line

(Previously, reboot was also not supported for a TDX guest).

But QEMU isn't KVM. While QEMU is the de facto reference VMM, it's important to keep in mind that it's not the only VMM (and that's not just about Google's VMM, there are plenty of other non-QEMU VMMs that are used in production environments).

Quoted 6 lines
Quoted 3 lines

Hmm. That was the reason for me to choose forcefully killing the VM inside KVM, since for the initial implementation, I didn't want to bother updating every VMM :)

Why would any VMM need to be updated? VMMs already need to handle KVM_EXIT_SHUTDOWN for TDX VMs.

Hmm. I knew QEMU handles KVM_EXIT_SHUTDOWN, but I wasn't sure if every VMM does. So, a brute-force way was chosen :)

author maintainer Applied
#19 lore raw
In reply to Sean Christopherson
Quoted 10 lines · Sean Christopherson

Signal SHUTDOWN instead of -EIO if the guest accesses an unaccepted page and has disabled EPT Violation #VEs on such accesses. Returning -EIO is all but guaranteed to mislead the VMM into thinking KVM (or the VMM) messed up, and will likely result in the VM being terminated instead of rebooted.

v2:
- Return SHUTDOWN directly, via a new helper. [Rick]
- Ratelimit the error message. [Sashiko, (and past me, amusingly)]

[...]

Applied to kvm-x86 vmx. I moved the ratelimit patch first, as Yan suggested (ignore the ordering below). Thanks!

[1/3] KVM: x86: Add a helper to prepare vcpu->run for a SHUTDOWN exit to userspace

      https://github.com/kvm-x86/linux/commit/def30ba0b6c3
[2/3] KVM: TDX: Synthesize SHUTDOWN instead of returning -EIO on unhandled EPT violation
      https://github.com/kvm-x86/linux/commit/a1b89df10ed8
[3/3] KVM: TDX: Ratelimit the "EPT Violation on pending acceptance page" warning
      https://github.com/kvm-x86/linux/commit/0b1431a34f1a

--
https://github.com/kvm-x86/linux/tree/next