Skip to content

Commit 6bee6fc

Browse files
yosrym93gregkh
authored andcommitted
KVM: SVM: Fix redundant updates of LBR MSR intercepts
commit 3fa05f9 upstream. Don't update the LBR MSR intercept bitmaps if they're already up-to-date, as unconditionally updating the intercepts forces KVM to recalculate the MSR bitmaps for vmcb02 on every nested VMRUN. The redundant updates are functionally okay; however, they neuter an optimization in Hyper-V nested virtualization enlightenments and this manifests as a self-test failure. In particular, Hyper-V lets L1 mark "nested enlightenments" as clean, i.e. tell KVM that no changes were made to the MSR bitmap since the last VMRUN. The hyperv_svm_test KVM selftest intentionally changes the MSR bitmap "without telling KVM about it" to verify that KVM honors the clean hint, correctly fails because KVM notices the changed bitmap anyway: ==== Test Assertion Failure ==== x86/hyperv_svm_test.c:120: vmcb->control.exit_code == 0x081 pid=193558 tid=193558 errno=4 - Interrupted system call 1 0x0000000000411361: assert_on_unhandled_exception at processor.c:659 2 0x0000000000406186: _vcpu_run at kvm_util.c:1699 3 (inlined by) vcpu_run at kvm_util.c:1710 4 0x0000000000401f2a: main at hyperv_svm_test.c:175 5 0x000000000041d0d3: __libc_start_call_main at libc-start.o:? 6 0x000000000041f27c: __libc_start_main_impl at ??:? 7 0x00000000004021a0: _start at ??:? vmcb->control.exit_code == SVM_EXIT_VMMCALL Do *not* fix this by skipping svm_hv_vmcb_dirty_nested_enlightenments() when svm_set_intercept_for_msr() performs a no-op change. changes to the L0 MSR interception bitmap are only triggered by full CPUID updates and MSR filter updates, both of which should be rare. Changing svm_set_intercept_for_msr() risks hiding unintended pessimizations like this one, and is actually more complex than this change. Fixes: fbe5e5f ("KVM: nSVM: Always recalculate LBR MSR intercepts in svm_update_lbrv()") Cc: stable@vger.kernel.org Signed-off-by: Yosry Ahmed <yosry.ahmed@linux.dev> Link: https://patch.msgid.link/20251112013017.1836863-1-yosry.ahmed@linux.dev [Rewritten commit message based on mailing list discussion. - Paolo] Reviewed-by: Sean Christopherson <seanjc@google.com> Tested-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Yosry Ahmed <yosry.ahmed@linux.dev> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 76b0fef commit 6bee6fc

2 files changed

Lines changed: 7 additions & 0 deletions

File tree

arch/x86/kvm/svm/svm.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,9 @@ static void svm_recalc_lbr_msr_intercepts(struct kvm_vcpu *vcpu)
10001000
struct vcpu_svm *svm = to_svm(vcpu);
10011001
bool intercept = !(svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK);
10021002

1003+
if (intercept == svm->lbr_msrs_intercepted)
1004+
return;
1005+
10031006
set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHFROMIP,
10041007
!intercept, !intercept);
10051008
set_msr_interception(vcpu, svm->msrpm, MSR_IA32_LASTBRANCHTOIP,
@@ -1012,6 +1015,8 @@ static void svm_recalc_lbr_msr_intercepts(struct kvm_vcpu *vcpu)
10121015
if (sev_es_guest(vcpu->kvm))
10131016
set_msr_interception(vcpu, svm->msrpm, MSR_IA32_DEBUGCTLMSR,
10141017
!intercept, !intercept);
1018+
1019+
svm->lbr_msrs_intercepted = intercept;
10151020
}
10161021

10171022
static void __svm_enable_lbrv(struct kvm_vcpu *vcpu)
@@ -1450,6 +1455,7 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu)
14501455
}
14511456

14521457
svm->x2avic_msrs_intercepted = true;
1458+
svm->lbr_msrs_intercepted = true;
14531459

14541460
svm->vmcb01.ptr = page_address(vmcb01_page);
14551461
svm->vmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) << PAGE_SHIFT);

arch/x86/kvm/svm/svm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ struct vcpu_svm {
324324
bool guest_state_loaded;
325325

326326
bool x2avic_msrs_intercepted;
327+
bool lbr_msrs_intercepted;
327328

328329
/* Guest GIF value, used when vGIF is not enabled */
329330
bool guest_gif;

0 commit comments

Comments
 (0)