]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
KVM: SVM: Fix redundant updates of LBR MSR intercepts
authorYosry Ahmed <yosry.ahmed@linux.dev>
Wed, 12 Nov 2025 01:30:17 +0000 (01:30 +0000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 1 Dec 2025 10:45:35 +0000 (11:45 +0100)
commit 3fa05f96fc08dff5e846c2cc283a249c1bf029a1 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: fbe5e5f030c2 ("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: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
arch/x86/kvm/svm/svm.c
arch/x86/kvm/svm/svm.h

index 66db728fae3ef19f2eb8e2fee40f42e901149116..30c143cc6bf418384b667a27e9ac3d7a4c6f075b 100644 (file)
@@ -713,7 +713,11 @@ void *svm_alloc_permissions_map(unsigned long size, gfp_t gfp_mask)
 
 static void svm_recalc_lbr_msr_intercepts(struct kvm_vcpu *vcpu)
 {
-       bool intercept = !(to_svm(vcpu)->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK);
+       struct vcpu_svm *svm = to_svm(vcpu);
+       bool intercept = !(svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK);
+
+       if (intercept == svm->lbr_msrs_intercepted)
+               return;
 
        svm_set_intercept_for_msr(vcpu, MSR_IA32_LASTBRANCHFROMIP, MSR_TYPE_RW, intercept);
        svm_set_intercept_for_msr(vcpu, MSR_IA32_LASTBRANCHTOIP, MSR_TYPE_RW, intercept);
@@ -722,6 +726,8 @@ static void svm_recalc_lbr_msr_intercepts(struct kvm_vcpu *vcpu)
 
        if (sev_es_guest(vcpu->kvm))
                svm_set_intercept_for_msr(vcpu, MSR_IA32_DEBUGCTLMSR, MSR_TYPE_RW, intercept);
+
+       svm->lbr_msrs_intercepted = intercept;
 }
 
 void svm_set_x2apic_msr_interception(struct vcpu_svm *svm, bool intercept)
@@ -1278,6 +1284,7 @@ static int svm_vcpu_create(struct kvm_vcpu *vcpu)
        }
 
        svm->x2avic_msrs_intercepted = true;
+       svm->lbr_msrs_intercepted = true;
 
        svm->vmcb01.ptr = page_address(vmcb01_page);
        svm->vmcb01.pa = __sme_set(page_to_pfn(vmcb01_page) << PAGE_SHIFT);
index 04371aa8c8f282ffb716db2e540741948d55b54d..8771362c85fdb5aaf5bac9f3566c18836adf1221 100644 (file)
@@ -334,6 +334,7 @@ struct vcpu_svm {
        bool guest_state_loaded;
 
        bool x2avic_msrs_intercepted;
+       bool lbr_msrs_intercepted;
 
        /* Guest GIF value, used when vGIF is not enabled */
        bool guest_gif;