Skip to contentSkip to conversation

KVM: nSVM: Nested virtual LBR fix and cleanups

PATCH New Yosry Ahmed 2 participants 5 messages Dec 5

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

cover KVM: nSVM: Nested virtual LBR fix and cleanups

A minor fix for a missed virtual LBR state update during save+restore and a couple of cleanups on top.

Yosry Ahmed (3):
KVM: nSVM: Update virtual LBR state/intercepts on KVM_SET_NESTED_STATE
KVM: nSVM: Call svm_update_lbrv() outside nested_vmcb02_prepare_save()
KVM: nSVM: Explicitly initialize misc_ctl2 in vmcb02

 arch/x86/kvm/svm/nested.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

base-commit: 4c9f462c7094c480144c98a942af7e935fa87a6a

Signature · 1 line
2.56.1.987.g2ec0ab4260-goog
#1 lore raw

PATCH 1/3 KVM: nSVM: Update virtual LBR state/intercepts on KVM_SET_NESTED_STATE

KVM calls svm_update_lbrv() to check if virtual LBR (i.e. SVM_MISC2_ENABLE_V_LBR) needs to be enabled in the VMCB and update the intercepts accordingly on nested transitions, and when MSR_IA32_DEBUGCTLMSR is updated.

During live migration, if MSR_IA32_DEBUGCTLMSR is restored before nested state, and the vCPU is restored into guest mode, svm_update_lbrv() is not called again to recompute virtual LBR state after vmcb02 is updated (e.g. to account for L1 enabling/disabling virtual LBR).

Call svm_update_lbrv() after updating vmcb02 in the restore path to set up virtual LBR accordingly.

Fixes 8a4821412cf2 ("KVM: nSVM: Fix and simplify LBR virtualization handling with nested")
---
 arch/x86/kvm/svm/nested.c | 6 ++++++
 1 file changed, 6 insertions(+)
arch/x86/kvm/svm/nested.c +6 -0
index 81d768ec82419..c5a572b58be07 100644
@@ -2091,6 +2091,12 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
 
 	nested_vmcb02_prepare_control(svm);
 
+	/*
+	 * Update virtual LBR state after setting up vmcb02's control area in
+	 * case DEBUGCTLMSR was already restored.
+	 */
+	svm_update_lbrv(vcpu);
+
 	/*
 	 * Any previously restored state (e.g. KVM_SET_SREGS) would mark fields
 	 * dirty in vmcb01 instead of vmcb02, so mark all of vmcb02 dirty here.
Signature · 1 line
2.56.1.987.g2ec0ab4260-goog
#3 lore raw

PATCH 2/3 KVM: nSVM: Call svm_update_lbrv() outside nested_vmcb02_prepare_save()

In reply to Yosry Ahmed

svm_update_lbrv() must be called after both the save and control areas of vmcb02 are set up as it uses fields from both areas. In the nested VMRUN path, it is called at the end of nested_vmcb02_prepare_save(). Move it to its only caller, enter_svm_guest_mode(), to make it more obvious and consistent with svm_set_nested_state().

No functional change intended.

---
 arch/x86/kvm/svm/nested.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/svm/nested.c +1 -1
index c5a572b58be07..44afa4068e05b 100644
@@ -821,7 +821,6 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm)
 		svm_copy_lbrs(&vmcb02->save, &vmcb01->save);
 	}
 	vmcb_mark_dirty(vmcb02, VMCB_LBR);
-	svm_update_lbrv(&svm->vcpu);
 }
 
 static inline bool is_evtinj_soft(u32 evtinj)
@@ -1070,6 +1069,7 @@ int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa, bool from_vmrun)
 	svm_switch_vmcb(svm, &svm->nested.vmcb02);
 	nested_vmcb02_prepare_control(svm);
 	nested_vmcb02_prepare_save(svm);
+	svm_update_lbrv(vcpu);
 
 	ret = nested_svm_load_cr3(&svm->vcpu, svm->nested.save.cr3,
 				  nested_npt_enabled(svm), from_vmrun);
Signature · 1 line
2.56.1.987.g2ec0ab4260-goog
#4 lore raw

PATCH 3/3 KVM: nSVM: Explicitly initialize misc_ctl2 in vmcb02

In reply to Yosry Ahmed

misc_ctl2 is currently not fully initialized on nested VMRUN when preparing vmcb02. nested_vmcb02_prepare_control() sets ENABLE_V_VMLOAD_VMSAVE if L1 enables VLS (and is allowed to), but it never clears the bit. Instead, it relies on nested_vmcb02_recalc_intercepts() setting VMSAVE/VMLOAD intercepts otherwise, which renders enabling VLS ineffective.

The only other bit that KVM ever sets is ENABLE_V_LBR, and it relies on svm_update_lbrv() to explicitly set it or clear it after vmcb02 is set up.

Explicitly clear misc_ctl2 when preparing vmcb02 for better hygiene, and to harden against accidentally leaving bits set across different nested VMRUNs when they shouldn't be.

---
 arch/x86/kvm/svm/nested.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/svm/nested.c +1 -1
index 44afa4068e05b..c4ac245a49cf8 100644
@@ -990,7 +990,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
 	}
 
 	/* SVM_MISC2_ENABLE_V_LBR is controlled by svm_update_lbrv() */
-
+	vmcb02->control.misc_ctl2 = 0;
 	if (!nested_vmcb_needs_vls_intercept(svm))
 		vmcb02->control.misc_ctl2 |= SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE;
 
Signature · 1 line
2.56.1.987.g2ec0ab4260-goog
#2 lore raw

Re: [PATCH 1/3] KVM: nSVM: Update virtual LBR state/intercepts on KVM_SET_NESTED_STATE

In reply to Yosry Ahmed
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] KVM: nSVM: svm_set_nested_state() calls svm_update_lbrv() on SEV-ES vCPUs --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] KVM: nSVM: svm_set_nested_state() calls svm_update_lbrv() on SEV-ES vCPUs
--

commit 24710e2ee8b01dff28f27148759c38c44f7b5221
Author: Yosry Ahmed <[email protected]>

KVM: nSVM: Update virtual LBR state/intercepts on KVM_SET_NESTED_STATE

This commit calls svm_update_lbrv() in svm_set_nested_state() after preparing vmcb02's control area so virtual LBR state and intercepts are recomputed when MSR_IA32_DEBUGCTLMSR is restored before nested state.

arch/x86/kvm/svm/nested.c static int svm_set_nested_state(struct kvm_vcpu *vcpu, · line 2098

Quoted 13 lines
arch/x86/kvm/svm/nested.c +5 -0
index 81d768ec82419..c5a572b58be07 100644
@@ -2091,6 +2091,12 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
 
 	nested_vmcb02_prepare_control(svm);
 
+	/*
+	 * Update virtual LBR state after setting up vmcb02's control area in
+	 * case DEBUGCTLMSR was already restored.
+	 */
+	svm_update_lbrv(vcpu);

[Severity: High]
Can this svm_update_lbrv() call hit KVM_BUG_ON(is_sev_es_guest(vcpu), vcpu->kvm) and corrupt LBR MSR intercepts on SEV-ES/SEV-SNP vCPUs?

svm_set_nested_state() does not reject SEV-ES vCPUs, and svm_need_lbr_virtualization() does not check is_sev_es_guest(vcpu). Two KVM_SET_NESTED_STATE(KVM_STATE_NESTED_GUEST_MODE) calls (first with SVM_MISC2_ENABLE_V_LBR set, then cleared) reach __svm_disable_lbrv().

While commit "KVM: nSVM: Explicitly initialize misc_ctl2 in vmcb02" avoids that direct trigger, calling KVM_SET_NESTED_STATE after LAUNCH_UPDATE_VMSA with ctl->misc_ctl2 = 0 still runs svm_recalc_lbr_msr_intercepts() and re-enables intercepts for MSR_IA32_DEBUGCTLMSR and the LBR MSRs.

Leaving guest mode via KVM_SET_NESTED_STATE(0) then leaves MSR_IA32_DEBUGCTLMSR intercepted on vmcb01, bypassing sev_es_prevent_msr_access() so unprivileged userspace can call KVM_SET_MSRS(MSR_IA32_DEBUGCTLMSR, 0) to trigger KVM_BUG_ON().

Should svm_set_nested_state() reject SEV-ES guests, or should svm_need_lbr_virtualization() return true for is_sev_es_guest(vcpu)?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1