Skip to content

Commit fbe5e5f

Browse files
yosrym93bonzini
authored andcommitted
KVM: nSVM: Always recalculate LBR MSR intercepts in svm_update_lbrv()
svm_update_lbrv() is called when MSR_IA32_DEBUGCTLMSR is updated, and on nested transitions where LBRV is used. It checks whether LBRV enablement needs to be changed in the current VMCB, and if it does, it also recalculate intercepts to LBR MSRs. However, there are cases where intercepts need to be updated even when LBRV enablement doesn't. Example scenario: - L1 has MSR_IA32_DEBUGCTLMSR cleared. - L1 runs L2 without LBR_CTL_ENABLE (no LBRV). - L2 sets DEBUGCTLMSR_LBR in MSR_IA32_DEBUGCTLMSR, svm_update_lbrv() sets LBR_CTL_ENABLE in VMCB02 and disables intercepts to LBR MSRs. - L2 exits to L1, svm_update_lbrv() is not called on this transition. - L1 clears MSR_IA32_DEBUGCTLMSR, svm_update_lbrv() finds that LBR_CTL_ENABLE is already cleared in VMCB01 and does nothing. - Intercepts remain disabled, L1 reads to LBR MSRs read the host MSRs. Fix it by always recalculating intercepts in svm_update_lbrv(). Fixes: 1d5a1b5 ("KVM: x86: nSVM: correctly virtualize LBR msrs when L2 is running") Cc: stable@vger.kernel.org Signed-off-by: Yosry Ahmed <yosry.ahmed@linux.dev> Link: https://patch.msgid.link/20251108004524.1600006-3-yosry.ahmed@linux.dev Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent dc55b3c commit fbe5e5f

1 file changed

Lines changed: 19 additions & 10 deletions

File tree

arch/x86/kvm/svm/svm.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -806,25 +806,29 @@ void svm_copy_lbrs(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
806806
vmcb_mark_dirty(to_vmcb, VMCB_LBR);
807807
}
808808

809-
void svm_enable_lbrv(struct kvm_vcpu *vcpu)
809+
static void __svm_enable_lbrv(struct kvm_vcpu *vcpu)
810810
{
811811
struct vcpu_svm *svm = to_svm(vcpu);
812812

813813
svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
814-
svm_recalc_lbr_msr_intercepts(vcpu);
815814

816815
/* Move the LBR msrs to the vmcb02 so that the guest can see them. */
817816
if (is_guest_mode(vcpu))
818817
svm_copy_lbrs(svm->vmcb, svm->vmcb01.ptr);
819818
}
820819

821-
static void svm_disable_lbrv(struct kvm_vcpu *vcpu)
820+
void svm_enable_lbrv(struct kvm_vcpu *vcpu)
821+
{
822+
__svm_enable_lbrv(vcpu);
823+
svm_recalc_lbr_msr_intercepts(vcpu);
824+
}
825+
826+
static void __svm_disable_lbrv(struct kvm_vcpu *vcpu)
822827
{
823828
struct vcpu_svm *svm = to_svm(vcpu);
824829

825830
KVM_BUG_ON(sev_es_guest(vcpu->kvm), vcpu->kvm);
826831
svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
827-
svm_recalc_lbr_msr_intercepts(vcpu);
828832

829833
/*
830834
* Move the LBR msrs back to the vmcb01 to avoid copying them
@@ -853,13 +857,18 @@ void svm_update_lbrv(struct kvm_vcpu *vcpu)
853857
(is_guest_mode(vcpu) && guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) &&
854858
(svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK));
855859

856-
if (enable_lbrv == current_enable_lbrv)
857-
return;
860+
if (enable_lbrv && !current_enable_lbrv)
861+
__svm_enable_lbrv(vcpu);
862+
else if (!enable_lbrv && current_enable_lbrv)
863+
__svm_disable_lbrv(vcpu);
858864

859-
if (enable_lbrv)
860-
svm_enable_lbrv(vcpu);
861-
else
862-
svm_disable_lbrv(vcpu);
865+
/*
866+
* During nested transitions, it is possible that the current VMCB has
867+
* LBR_CTL set, but the previous LBR_CTL had it cleared (or vice versa).
868+
* In this case, even though LBR_CTL does not need an update, intercepts
869+
* do, so always recalculate the intercepts here.
870+
*/
871+
svm_recalc_lbr_msr_intercepts(vcpu);
863872
}
864873

865874
void disable_nmi_singlestep(struct vcpu_svm *svm)

0 commit comments

Comments
 (0)