Skip to contentSkip to conversation

KVM: SEV: Update SEV-ES shutdown intercepts with more metadata

PATCH Superseded Peter Gonda 2 participants 3 messages Sep 5, 2023

Subsystems (3)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 KVM: SEV: Update SEV-ES shutdown intercepts with more metadata

Currently if an SEV-ES VM shuts down userspace sees KVM_RUN struct with only the INVALID_ARGUMENT. This is a very limited amount of information to debug the situation. Instead KVM can return a KVM_SYSTEM_EVENT_SEV_TERM to alert userspace the VM is shutting down and is not usable any further. This latter point can be enforced using the kvm_vm_dead() functionality.

Cc Paolo Bonzini <[email protected]>
Cc Sean Christopherson <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>

---

I am not sure if this is the right path forward maybe just returning KVM_EXIT_SHUTDOWN is better. But the current behavior is very unhelpful.

---

 arch/x86/kvm/svm/svm.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
arch/x86/kvm/svm/svm.c +7 -3
index 956726d867aa..547526616d60 100644
@@ -2131,12 +2131,16 @@ static int shutdown_interception(struct kvm_vcpu *vcpu)
 	 * The VM save area has already been encrypted so it
 	 * cannot be reinitialized - just terminate.
 	 */
-	if (sev_es_guest(vcpu->kvm))
-		return -EINVAL;
+	if (sev_es_guest(vcpu->kvm)) {
+		vcpu->run->exit_reason = KVM_EXIT_SYSTEM_EVENT;
+		vcpu->run->system_event.type = KVM_SYSTEM_EVENT_SEV_TERM;
+		kvm_vm_dead(vcpu->kvm);
+		return 0;
+	}
 
 	/*
 	 * VMCB is undefined after a SHUTDOWN intercept.  INIT the vCPU to put
-	 * the VMCB in a known good state.  Unfortuately, KVM doesn't have
+	 * the VMCB in a known good state.  Unfortunately, KVM doesn't have
 	 * KVM_MP_STATE_SHUTDOWN and can't add it without potentially breaking
 	 * userspace.  At a platform view, INIT is acceptable behavior as
 	 * there exist bare metal platforms that automatically INIT the CPU
Signature · 1 line
2.42.0.283.g2d96d420d3-goog
#1 lore raw
Quoted 6 lines · Peter Gonda

Currently if an SEV-ES VM shuts down userspace sees KVM_RUN struct with only the INVALID_ARGUMENT. This is a very limited amount of information to debug the situation. Instead KVM can return a KVM_SYSTEM_EVENT_SEV_TERM to alert userspace the VM is shutting down and is not usable any further. This latter point can be enforced using the kvm_vm_dead() functionality.

Add the kvm_vm_dead() thing in a separate patch. If we want to actually harden KVM against consuming a garbage VMSA then we do need to mark the VM dead, but on the other hand that will block _all_ KVM ioctls(), which will make debug even harder.

Quoted 10 lines
Cc Paolo Bonzini <[email protected]>
Cc Sean Christopherson <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Quoted 4 lines

---

I am not sure if this is the right path forward maybe just returning KVM_EXIT_SHUTDOWN is better. But the current behavior is very unhelpful.

Ya, KVM_EXIT_SHUTDOWN is better, we should leave KVM_SYSTEM_EVENT_SEV_TERM to explicit "requests" from the guest.

#2 lore raw
Quoted 9 lines · Sean Christopherson
Quoted 6 lines · Peter Gonda

Currently if an SEV-ES VM shuts down userspace sees KVM_RUN struct with only the INVALID_ARGUMENT. This is a very limited amount of information to debug the situation. Instead KVM can return a KVM_SYSTEM_EVENT_SEV_TERM to alert userspace the VM is shutting down and is not usable any further. This latter point can be enforced using the kvm_vm_dead() functionality.

Quoted 4 lines

Add the kvm_vm_dead() thing in a separate patch. If we want to actually harden KVM against consuming a garbage VMSA then we do need to mark the VM dead, but on the other hand that will block _all_ KVM ioctls(), which will make debug even harder.

Will do. Do we have better functionality for just blocking running the vCPU?

Quoted 11 lines
Quoted 11 lines
Cc Paolo Bonzini <[email protected]>
Cc Sean Christopherson <[email protected]>
Cc Tom Lendacky <[email protected]>
Cc Joerg Roedel <[email protected]>
Cc Borislav Petkov <[email protected]>
Quoted 7 lines
Quoted 4 lines

---

I am not sure if this is the right path forward maybe just returning KVM_EXIT_SHUTDOWN is better. But the current behavior is very unhelpful.

Ya, KVM_EXIT_SHUTDOWN is better, we should leave KVM_SYSTEM_EVENT_SEV_TERM to explicit "requests" from the guest.

Sounds good to me. I'll send a V2 that just updates to KVM_EXIT_SHUTDOWN.