Skip to contentSkip to conversation

KVM: SVM: Clear VMCB save area instead of entire VMCB on shutdown intercept

PATCH Applied Shivansh Dhiman 2 participants 3 messages Oct 8 – Nov 15

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 KVM: SVM: Clear VMCB save area instead of entire VMCB on shutdown intercept

The AMD APM previously stated that after an intercepted shutdown, the entire VMCB state is undefined. In practice, only the save area is undefined and most of the control area remains valid. Revision 3.45 of the APM now makes this explicit:

"After an intercepted shutdown, the VMCB control area is valid (with
the exception of offsets 60h, 61h, and 68h) and the VMCB state save area is undefined."

KVM zeroes the entire VMCB before INITing the vCPU based on the old wording, discarding control area state that hardware preserves.

Clear only the save area and the three undefined control area fields (int_ctl[15:0] and int_state) in line with the updated APM.

Also explicitly clear EVENTINJ. Hardware clears it on #VMEXIT, but only if VMRUN fully entered the guest. Clearing it here should be unnecessary, since a shutdown can only happen if the guest actually ran. Do so anyway, as there is no reason to carry a stale event across the vCPU reset, and the cost is negligible.

---

Changes in v2:
* Explicitly clear EVENTINJ and EVENTINJ_ERR (Sean Christopherson).
* Rebased on kvm-x86/next.

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

---
 arch/x86/kvm/svm/svm.c | 27 ++++++++++++++++++++-------
 1 file changed, 20 insertions(+), 7 deletions(-)
arch/x86/kvm/svm/svm.c +20 -7
index 136069dfcd44..09ca864e5a38 100644
@@ -2213,18 +2213,31 @@ static int shutdown_interception(struct kvm_vcpu *vcpu)
 
 
 	/*
-	 * VMCB is undefined after a SHUTDOWN intercept.  INIT the vCPU to put
-	 * the VMCB in a known good state.  Unfortuately, 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
-	 * in response to shutdown.
+	 * The VMCB save area is undefined after a SHUTDOWN intercept, as are
+	 * control area offsets 60h, 61h, and 68h (int_ctl[15:0] and int_state).
+	 * INIT the vCPU to put 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 in response to shutdown.
 	 *
 	 * The VM save area for SEV-ES guests has already been encrypted so it
 	 * cannot be reinitialized, i.e. synthesizing INIT is futile.
 	 */
 	if (!is_sev_es_guest(vcpu)) {
-		clear_page(svm->vmcb);
+		struct vmcb_control_area *control = &svm->vmcb->control;
+
+		memset(&svm->vmcb->save, 0, sizeof(svm->vmcb->save));
+		control->int_ctl &= ~GENMASK(15, 0);
+		control->int_state = 0;
+
+		/*
+		 * EVENTINJ is cleared on #VMEXIT, but only if VMRUN fully
+		 * entered the guest.  Manually clear the fields even though it
+		 * should be redundant, as there is no downside to doing so.
+		 */
+		control->event_inj = 0;
+		control->event_inj_err = 0;
 #ifdef CONFIG_KVM_SMM
 		if (is_smm(vcpu))
 			kvm_smm_changed(vcpu, false);

base-commit: 6bd2905303c58679e303941b7d8ae8c074cd95ce

Signature · 1 line
2.43.0
maintainer Applied
#1 lore raw
Quoted 10 lines · Shivansh Dhiman

The AMD APM previously stated that after an intercepted shutdown, the entire VMCB state is undefined. In practice, only the save area is undefined and most of the control area remains valid. Revision 3.45 of the APM now makes this explicit:

"After an intercepted shutdown, the VMCB control area is valid (with
the exception of offsets 60h, 61h, and 68h) and the VMCB state save area is undefined."

[...]

Applied to kvm-x86 fixes, thanks!

[1/1] KVM: SVM: Clear VMCB save area instead of entire VMCB on shutdown intercept

      https://github.com/kvm-x86/linux/commit/bdd05706f146

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

#2 lore raw
Quoted 6 lines · Sean Christopherson
Quoted 5 lines · Shivansh Dhiman

The AMD APM previously stated that after an intercepted shutdown, the entire VMCB state is undefined. In practice, only the save area is undefined and most of the control area remains valid. Revision 3.45 of the APM now makes this explicit:

Quoted 7 lines
Quoted 5 lines

"After an intercepted shutdown, the VMCB control area is valid (with
the exception of offsets 60h, 61h, and 68h) and the VMCB state save area is undefined."

[...]

Applied to kvm-x86 fixes, thanks!

Thank you Sean for merging this patch. :D

Quoted 6 lines

[1/1] KVM: SVM: Clear VMCB save area instead of entire VMCB on shutdown intercept

      https://github.com/kvm-x86/linux/commit/bdd05706f146

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