Skip to contentSkip to conversation

KVM: SVM: LBR virtualization fixes

PATCH In review Yosry Ahmed 3 participants 16 messages Nov 8, 2025 – Nov 11, 2025

Subsystems (5)
Stable branch x86 KVM Kselftest +1 more subsystems
lore
Share & tools
lore
#0 lore raw

cover KVM: SVM: LBR virtualization fixes

This series fixes multiple problems with LBR virtualization, including a fun problem that leads to L1 reading the host's LBR MSRs. It also considerably simplifies the code.

The series has a selftest in the end that verifies that save/restore work correctly. I will send a couple of new kvm-unit-tests separately that exercise the bugs fixed by patches 2 & 3.

Yosry Ahmed (6):
KVM: SVM: Mark VMCB_LBR dirty when MSR_IA32_DEBUGCTLMSR is updated
KVM: nSVM: Always recalculate LBR MSR intercepts in svm_update_lbrv()
KVM: nSVM: Fix and simplify LBR virtualization handling with nested
KVM: SVM: Switch svm_copy_lbrs() to a macro
KVM: SVM: Add missing save/restore handling of LBR MSRs
KVM: selftests: Add a test for LBR save/restore (ft. nested)

 arch/x86/kvm/svm/nested.c                     |  31 ++--
 arch/x86/kvm/svm/svm.c                        |  98 ++++++-----
 arch/x86/kvm/svm/svm.h                        |  10 +-
 arch/x86/kvm/x86.c                            |   3 +
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/include/x86/processor.h     |   5 +
 .../selftests/kvm/x86/svm_lbr_nested_state.c  | 155 ++++++++++++++++++
 7 files changed, 236 insertions(+), 67 deletions(-)
 create mode 100644 tools/testing/selftests/kvm/x86/svm_lbr_nested_state.c
Signature · 1 line
2.51.2.1041.gc1ab5b90ca-goog
#1 lore raw

PATCH 1/6 KVM: SVM: Mark VMCB_LBR dirty when MSR_IA32_DEBUGCTLMSR is updated

Clear the VMCB_LBR clean bit when MSR_IA32_DEBUGCTLMSR is updated, as the only valid bit is DEBUGCTLMSR_LBR.

The history is complicated, it was correctly cleared for L1 before commit 1d5a1b5860ed ("KVM: x86: nSVM: correctly virtualize LBR msrs when L2 is running"), then the latter relied on svm_update_lbrv() to clear it, but it only did so for L2. Go back to clearing it directly in svm_set_msr().

Fixes 1d5a1b5860ed ("KVM: x86: nSVM: correctly virtualize LBR msrs when L2 is running")
Reported-by [email protected]
---
 arch/x86/kvm/svm/svm.c | 4 ++++
 1 file changed, 4 insertions(+)
arch/x86/kvm/svm/svm.c +4 -0
index 55bd7aa5cd743..d25c56b30b4e2 100644
@@ -3009,7 +3009,11 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
 		if (data & DEBUGCTL_RESERVED_BITS)
 			return 1;
 
+		if (svm_get_lbr_vmcb(svm)->save.dbgctl == data)
+			break;
+
 		svm_get_lbr_vmcb(svm)->save.dbgctl = data;
+		vmcb_mark_dirty(svm->vmcb, VMCB_LBR);
 		svm_update_lbrv(vcpu);
 		break;
 	case MSR_VM_HSAVE_PA:
Signature · 1 line
2.51.2.1041.gc1ab5b90ca-goog
maintainer
#2 lore raw

Re: [PATCH 1/6] KVM: SVM: Mark VMCB_LBR dirty when MSR_IA32_DEBUGCTLMSR is updated

Quoted 8 lines · Yosry Ahmed

Clear the VMCB_LBR clean bit when MSR_IA32_DEBUGCTLMSR is updated, as the only valid bit is DEBUGCTLMSR_LBR.

The history is complicated, it was correctly cleared for L1 before commit 1d5a1b5860ed ("KVM: x86: nSVM: correctly virtualize LBR msrs when L2 is running"), then the latter relied on svm_update_lbrv() to clear it, but it only did so for L2. Go back to clearing it directly in svm_set_msr().

Slightly more accurate:

The APM lists the DbgCtlMsr field as being tracked by the VMCB_LBR clean bit. Always clear the bit when MSR_IA32_DEBUGCTLMSR is updated.

The history is complicated, it was correctly cleared for L1 before commit 1d5a1b5860ed ("KVM: x86: nSVM: correctly virtualize LBR msrs when L2 is running"). At that point svm_set_msr() started to rely on svm_update_lbrv() to clear the bit, but when nested virtualization is enabled the latter does not always clear it even if MSR_IA32_DEBUGCTLMSR changed. Go back to clearing it directly in svm_set_msr().

Paolo

Quoted 26 lines
Fixes 1d5a1b5860ed ("KVM: x86: nSVM: correctly virtualize LBR msrs when L2 is running")
Reported-by [email protected]
---
  arch/x86/kvm/svm/svm.c | 4 ++++
  1 file changed, 4 insertions(+)
arch/x86/kvm/svm/svm.c +4 -0
index 55bd7aa5cd743..d25c56b30b4e2 100644
@@ -3009,7 +3009,11 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
  		if (data & DEBUGCTL_RESERVED_BITS)
  			return 1;
 
+		if (svm_get_lbr_vmcb(svm)->save.dbgctl == data)
+			break;
+
  		svm_get_lbr_vmcb(svm)->save.dbgctl = data;
+		vmcb_mark_dirty(svm->vmcb, VMCB_LBR);
  		svm_update_lbrv(vcpu);
  		break;
  	case MSR_VM_HSAVE_PA:
#3 lore raw

PATCH 2/6 KVM: nSVM: Always recalculate LBR MSR intercepts in svm_update_lbrv()

In reply to Yosry Ahmed

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 1d5a1b5860ed ("KVM: x86: nSVM: correctly virtualize LBR msrs when L2 is running")
---
 arch/x86/kvm/svm/svm.c | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)
arch/x86/kvm/svm/svm.c +19 -10
index d25c56b30b4e2..26ab75ecf1c67 100644
@@ -806,25 +806,29 @@ void svm_copy_lbrs(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
 	vmcb_mark_dirty(to_vmcb, VMCB_LBR);
 }
 
-void svm_enable_lbrv(struct kvm_vcpu *vcpu)
+static void __svm_enable_lbrv(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
 
 	svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
-	svm_recalc_lbr_msr_intercepts(vcpu);
 
 	/* Move the LBR msrs to the vmcb02 so that the guest can see them. */
 	if (is_guest_mode(vcpu))
 		svm_copy_lbrs(svm->vmcb, svm->vmcb01.ptr);
 }
 
-static void svm_disable_lbrv(struct kvm_vcpu *vcpu)
+void svm_enable_lbrv(struct kvm_vcpu *vcpu)
+{
+	__svm_enable_lbrv(vcpu);
+	svm_recalc_lbr_msr_intercepts(vcpu);
+}
+
+static void __svm_disable_lbrv(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
 
 	KVM_BUG_ON(sev_es_guest(vcpu->kvm), vcpu->kvm);
 	svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
-	svm_recalc_lbr_msr_intercepts(vcpu);
 
 	/*
 	 * Move the LBR msrs back to the vmcb01 to avoid copying them
@@ -853,13 +857,18 @@ void svm_update_lbrv(struct kvm_vcpu *vcpu)
 			    (is_guest_mode(vcpu) && guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) &&
 			    (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK));
 
-	if (enable_lbrv == current_enable_lbrv)
-		return;
+	if (enable_lbrv && !current_enable_lbrv)
+		__svm_enable_lbrv(vcpu);
+	else if (!enable_lbrv && current_enable_lbrv)
+		__svm_disable_lbrv(vcpu);
 
-	if (enable_lbrv)
-		svm_enable_lbrv(vcpu);
-	else
-		svm_disable_lbrv(vcpu);
+	/*
+	 * During nested transitions, it is possible that the current VMCB has
+	 * LBR_CTL set, but the previous LBR_CTL had it cleared (or vice versa).
+	 * In this case, even though LBR_CTL does not need an update, intercepts
+	 * do, so always recalculate the intercepts here.
+	 */
+	svm_recalc_lbr_msr_intercepts(vcpu);
 }
 
 void disable_nmi_singlestep(struct vcpu_svm *svm)
Signature · 1 line
2.51.2.1041.gc1ab5b90ca-goog
#4 lore raw

Re: [PATCH 2/6] KVM: nSVM: Always recalculate LBR MSR intercepts in svm_update_lbrv()

Quoted 16 lines · Yosry Ahmed

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.

Quoted 1 line

Fix it by always recalculating intercepts in svm_update_lbrv().

This actually breaks hyperv_svm_test, because svm_update_lbrv() is called on every nested transition, calling svm_recalc_lbr_msr_intercepts() -> svm_set_intercept_for_msr() and setting svm->nested.force_msr_bitmap_recalc to true.

This breaks the hyperv optimization in nested_svm_vmrun_msrpm() AFAICT.

I think there are two ways to fix this:
- Add another bool to svm->nested to track LBR intercepts, and only call svm_set_intercept_for_msr() if the intercepts need to be updated.

- Update svm_set_intercept_for_msr() itself to do nothing if the intercepts do not need to be changed, which is more clutter but applies to other callers as well so could shave cycles elsewhere (see below).

Sean, Paolo, any preferences?

Here's what updating svm_set_intercept_for_msr() looks like:

arch/x86/kvm/svm/svm.c +18 -6
index 2fbb0b88c6a3e..b1afafc8b37c0 100644
@@ -664,24 +664,36 @@ void svm_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type, bool se
 {
        struct vcpu_svm *svm = to_svm(vcpu);
        void *msrpm = svm->msrpm;
+       bool recalc = false;
+       bool already_set;
 
        /* Don't disable interception for MSRs userspace wants to handle. */
        if (type & MSR_TYPE_R) {
-               if (!set && kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ))
+               already_set = svm_test_msr_bitmap_read(msrpm, msr);
+               if (!set && already_set && kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ)) {
                        svm_clear_msr_bitmap_read(msrpm, msr);
-               else
+                       recalc = true;
+               } else if (set && !already_set) {
                        svm_set_msr_bitmap_read(msrpm, msr);
+                       recalc = true;
+               }
        }
 
        if (type & MSR_TYPE_W) {
-               if (!set && kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE))
+               already_set = svm_test_msr_bitmap_write(msrpm, msr);
+               if (!set && already_set && kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE)) {
                        svm_clear_msr_bitmap_write(msrpm, msr);
-               else
+                       recalc = true;
+               } else if (set && !already_set) {
                        svm_set_msr_bitmap_write(msrpm, msr);
+                       recalc = true;
+               }
        }
 
-       svm_hv_vmcb_dirty_nested_enlightenments(vcpu);
-       svm->nested.force_msr_bitmap_recalc = true;
+       if (recalc) {
+               svm_hv_vmcb_dirty_nested_enlightenments(vcpu);
+               svm->nested.force_msr_bitmap_recalc = true;
+       }
 }
 
 void *svm_alloc_permissions_map(unsigned long size, gfp_t gfp_mask)
Quoted 75 lines
Fixes 1d5a1b5860ed ("KVM: x86: nSVM: correctly virtualize LBR msrs when L2 is running")
---
 arch/x86/kvm/svm/svm.c | 29 +++++++++++++++++++----------
 1 file changed, 19 insertions(+), 10 deletions(-)
arch/x86/kvm/svm/svm.c +19 -10
index d25c56b30b4e2..26ab75ecf1c67 100644
@@ -806,25 +806,29 @@ void svm_copy_lbrs(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
 	vmcb_mark_dirty(to_vmcb, VMCB_LBR);
 }
 
-void svm_enable_lbrv(struct kvm_vcpu *vcpu)
+static void __svm_enable_lbrv(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
 
 	svm->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
-	svm_recalc_lbr_msr_intercepts(vcpu);
 
 	/* Move the LBR msrs to the vmcb02 so that the guest can see them. */
 	if (is_guest_mode(vcpu))
 		svm_copy_lbrs(svm->vmcb, svm->vmcb01.ptr);
 }
 
-static void svm_disable_lbrv(struct kvm_vcpu *vcpu)
+void svm_enable_lbrv(struct kvm_vcpu *vcpu)
+{
+	__svm_enable_lbrv(vcpu);
+	svm_recalc_lbr_msr_intercepts(vcpu);
+}
+
+static void __svm_disable_lbrv(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
 
 	KVM_BUG_ON(sev_es_guest(vcpu->kvm), vcpu->kvm);
 	svm->vmcb->control.virt_ext &= ~LBR_CTL_ENABLE_MASK;
-	svm_recalc_lbr_msr_intercepts(vcpu);
 
 	/*
 	 * Move the LBR msrs back to the vmcb01 to avoid copying them
@@ -853,13 +857,18 @@ void svm_update_lbrv(struct kvm_vcpu *vcpu)
 			    (is_guest_mode(vcpu) && guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) &&
 			    (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK));
 
-	if (enable_lbrv == current_enable_lbrv)
-		return;
+	if (enable_lbrv && !current_enable_lbrv)
+		__svm_enable_lbrv(vcpu);
+	else if (!enable_lbrv && current_enable_lbrv)
+		__svm_disable_lbrv(vcpu);
 
-	if (enable_lbrv)
-		svm_enable_lbrv(vcpu);
-	else
-		svm_disable_lbrv(vcpu);
+	/*
+	 * During nested transitions, it is possible that the current VMCB has
+	 * LBR_CTL set, but the previous LBR_CTL had it cleared (or vice versa).
+	 * In this case, even though LBR_CTL does not need an update, intercepts
+	 * do, so always recalculate the intercepts here.
+	 */
+	svm_recalc_lbr_msr_intercepts(vcpu);
 }
 
 void disable_nmi_singlestep(struct vcpu_svm *svm)
Signature · 1 line
2.51.2.1041.gc1ab5b90ca-goog
#5 lore raw

Re: [PATCH 2/6] KVM: nSVM: Always recalculate LBR MSR intercepts in svm_update_lbrv()

Quoted 30 lines · Yosry Ahmed
Quoted 16 lines · Yosry Ahmed

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.

Quoted 1 line

Fix it by always recalculating intercepts in svm_update_lbrv().

This actually breaks hyperv_svm_test, because svm_update_lbrv() is called on every nested transition, calling svm_recalc_lbr_msr_intercepts() -> svm_set_intercept_for_msr() and setting svm->nested.force_msr_bitmap_recalc to true.

This breaks the hyperv optimization in nested_svm_vmrun_msrpm() AFAICT.

I think there are two ways to fix this:
- Add another bool to svm->nested to track LBR intercepts, and only call svm_set_intercept_for_msr() if the intercepts need to be updated.

Quoted 8 lines

- Update svm_set_intercept_for_msr() itself to do nothing if the intercepts do not need to be changed, which is more clutter but applies to other callers as well so could shave cycles elsewhere (see below).

Sean, Paolo, any preferences?

Here's what updating svm_set_intercept_for_msr() looks like:

and that diff breaks userspace_msr_exit_test :)

Here's an actually tested diff:

arch/x86/kvm/svm/svm.c +20 -6
index 2fbb0b88c6a3e..88717429ba9d5 100644
@@ -664,24 +664,38 @@ void svm_set_intercept_for_msr(struct kvm_vcpu *vcpu, u32 msr, int type, bool se
 {
        struct vcpu_svm *svm = to_svm(vcpu);
        void *msrpm = svm->msrpm;
+       bool recalc = false;
+       bool already_set;
 
        /* Don't disable interception for MSRs userspace wants to handle. */
        if (type & MSR_TYPE_R) {
-               if (!set && kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ))
+               set = set || !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_READ);
+               already_set = svm_test_msr_bitmap_read(msrpm, msr);
+
+               if (!set && already_set)
                        svm_clear_msr_bitmap_read(msrpm, msr);
-               else
+               else if (set && !already_set)
                        svm_set_msr_bitmap_read(msrpm, msr);
+
+               recalc |= (set != already_set);
        }
 
        if (type & MSR_TYPE_W) {
-               if (!set && kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE))
+               set = set || !kvm_msr_allowed(vcpu, msr, KVM_MSR_FILTER_WRITE);
+               already_set = svm_test_msr_bitmap_write(msrpm, msr);
+
+               if (!set && already_set)
                        svm_clear_msr_bitmap_write(msrpm, msr);
-               else
+               else if (set && !already_set)
                        svm_set_msr_bitmap_write(msrpm, msr);
+
+               recalc |= (set != already_set);
        }
 
-       svm_hv_vmcb_dirty_nested_enlightenments(vcpu);
-       svm->nested.force_msr_bitmap_recalc = true;
+       if (recalc) {
+               svm_hv_vmcb_dirty_nested_enlightenments(vcpu);
+               svm->nested.force_msr_bitmap_recalc = true;
+       }
 }
 
 void *svm_alloc_permissions_map(unsigned long size, gfp_t gfp_mask)

---

For the record, I don't want to just use svm_test_msr_bitmap_*() in svm_update_lbrv() because there is no direct equivalent in older kernels as far as I can tell, so a backport would be completely inapplicable.

#6 lore raw

Re: [PATCH 2/6] KVM: nSVM: Always recalculate LBR MSR intercepts in svm_update_lbrv()

Quoted 31 lines · Yosry Ahmed
Quoted 30 lines · Yosry Ahmed
Quoted 16 lines · Yosry Ahmed

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.

Quoted 1 line

Fix it by always recalculating intercepts in svm_update_lbrv().

This actually breaks hyperv_svm_test, because svm_update_lbrv() is called on every nested transition, calling svm_recalc_lbr_msr_intercepts() -> svm_set_intercept_for_msr() and setting svm->nested.force_msr_bitmap_recalc to true.

This breaks the hyperv optimization in nested_svm_vmrun_msrpm() AFAICT.

I think there are two ways to fix this:
- Add another bool to svm->nested to track LBR intercepts, and only call svm_set_intercept_for_msr() if the intercepts need to be updated.

Quoted 8 lines
Quoted 8 lines

- Update svm_set_intercept_for_msr() itself to do nothing if the intercepts do not need to be changed, which is more clutter but applies to other callers as well so could shave cycles elsewhere (see below).

Sean, Paolo, any preferences?

Here's what updating svm_set_intercept_for_msr() looks like:

I am *very* strongly opposed to modifying svm_set_intercept_for_msr() to deal with whatever mess LBRs has created. Whatever the problem is (I haven't read through all of this yet), it needs to be fixed in the LBR code, not in svm_set_intercept_for_msr().

Recalculating MSR intercepts is supposed to done only as needed, I don't want to encourage lazy code that works by optimizing paths that should be rare.

#7 lore raw

PATCH 3/6 KVM: nSVM: Fix and simplify LBR virtualization handling with nested

In reply to Yosry Ahmed

The current scheme for handling LBRV when nested is used is very complicated, especially when L1 does not enable LBRV (i.e. does not set LBR_CTL_ENABLE_MASK).

To avoid copying LBRs between VMCB01 and VMCB02 on every nested transition, the current implementation switches between using VMCB01 or VMCB02 as the source of truth for the LBRs while L2 is running. If L2 enables LBR, VMCB02 is used as the source of truth. When L2 disables LBR, the LBRs are copied to VMCB01 and VMCB01 is used as the source of truth. This introduces significant complexity, and incorrect behavior in some cases.

For example, on a nested #VMEXIT, the LBRs are only copied from VMCB02 to VMCB01 if LBRV is enabled in VMCB01. This is because L2's writes to MSR_IA32_DEBUGCTLMSR to enable LBR are intercepted and propagated to VMCB01 instead of VMCB02. However, LBRV is only enabled in VMCB02 when L2 is running.

This means that if L2 enables LBR and exits to L1, the LBRs will not be propagated from VMCB02 to VMCB01, because LBRV is disabled in VMCB01.

There is no meaningful difference in CPUID rate in L2 when copying LBRs on every nested transition vs. the current approach, so do the simple and correct thing and always copy LBRs between VMCB01 and VMCB02 on nested transitions (when LBRV is disabled by L1). Drop the conditional LBRs copying in __svm_{enable/disable}_lbrv() as it is now unnecessary.

VMCB02 becomes the only source of truth for LBRs when L2 is running, regardless of LBRV being enabled by L1, drop svm_get_lbr_vmcb() and use svm->vmcb directly in its place.

Fixes 1d5a1b5860ed ("KVM: x86: nSVM: correctly virtualize LBR msrs when L2 is running")
---
 arch/x86/kvm/svm/nested.c | 20 ++++++-----------
 arch/x86/kvm/svm/svm.c    | 46 +++++++++------------------------------
 2 files changed, 17 insertions(+), 49 deletions(-)
arch/x86/kvm/svm/nested.c +7 -13
index 71664d54d8b2a..c81005b245222 100644
@@ -678,11 +678,10 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12
 		 */
 		svm_copy_lbrs(vmcb02, vmcb12);
 		vmcb02->save.dbgctl &= ~DEBUGCTL_RESERVED_BITS;
-		svm_update_lbrv(&svm->vcpu);
-
-	} else if (unlikely(vmcb01->control.virt_ext & LBR_CTL_ENABLE_MASK)) {
+	} else {
 		svm_copy_lbrs(vmcb02, vmcb01);
 	}
+	svm_update_lbrv(&svm->vcpu);
 }
 
 static inline bool is_evtinj_soft(u32 evtinj)
@@ -835,11 +834,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm,
 			svm->soft_int_next_rip = vmcb12_rip;
 	}
 
-	vmcb02->control.virt_ext            = vmcb01->control.virt_ext &
-					      LBR_CTL_ENABLE_MASK;
-	if (guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV))
-		vmcb02->control.virt_ext  |=
-			(svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK);
+	/* LBR_CTL_ENABLE_MASK is controlled by svm_update_lbrv() */
 
 	if (!nested_vmcb_needs_vls_intercept(svm))
 		vmcb02->control.virt_ext |= VIRTUAL_VMLOAD_VMSAVE_ENABLE_MASK;
@@ -1191,13 +1186,12 @@ int nested_svm_vmexit(struct vcpu_svm *svm)
 		kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
 
 	if (unlikely(guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) &&
-		     (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) {
+		     (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK)))
 		svm_copy_lbrs(vmcb12, vmcb02);
-		svm_update_lbrv(vcpu);
-	} else if (unlikely(vmcb01->control.virt_ext & LBR_CTL_ENABLE_MASK)) {
+	else
 		svm_copy_lbrs(vmcb01, vmcb02);
-		svm_update_lbrv(vcpu);
-	}
+
+	svm_update_lbrv(vcpu);
 
 	if (vnmi) {
 		if (vmcb02->control.int_ctl & V_NMI_BLOCKING_MASK)
arch/x86/kvm/svm/svm.c +10 -36

87 lines not on this page · raw message

Signature · 1 line
2.51.2.1041.gc1ab5b90ca-goog
#8 lore raw

PATCH 4/6 KVM: SVM: Switch svm_copy_lbrs() to a macro

In reply to Yosry Ahmed

In preparation for using svm_copy_lbrs() with 'struct vmcb_save_area' without a containing 'struct vmcb', and later even 'struct vmcb_save_area_cached', make it a macro. Pull the call to vmcb_mark_dirty() out to the callers.

Macros are generally not preferred compared to functions, mainly due to type-safety. However, in this case it seems like having a simple macro copying a few fields is better than copy-pasting the same 5 lines of code in different places.

On the bright side, pulling vmcb_mark_dirty() calls to the callers makes it clear that in one case, vmcb_mark_dirty() was being called on VMCB12. It is not architecturally defined for the CPU to clear arbitrary clean bits, and it is not needed, so drop that one call.

Technically fixes the non-architectural behavior of setting the dirty bit on VMCB12.

Fixes d20c796ca370 ("KVM: x86: nSVM: implement nested LBR virtualization")
---
 arch/x86/kvm/svm/nested.c | 16 ++++++++++------
 arch/x86/kvm/svm/svm.c    | 11 -----------
 arch/x86/kvm/svm/svm.h    | 10 +++++++++-
 3 files changed, 19 insertions(+), 18 deletions(-)
arch/x86/kvm/svm/nested.c +10 -6
index c81005b245222..e7861392f2fcd 100644
@@ -676,10 +676,12 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12
 		 * Reserved bits of DEBUGCTL are ignored.  Be consistent with
 		 * svm_set_msr's definition of reserved bits.
 		 */
-		svm_copy_lbrs(vmcb02, vmcb12);
+		svm_copy_lbrs(&vmcb02->save, &vmcb12->save);
+		vmcb_mark_dirty(vmcb02, VMCB_LBR);
 		vmcb02->save.dbgctl &= ~DEBUGCTL_RESERVED_BITS;
 	} else {
-		svm_copy_lbrs(vmcb02, vmcb01);
+		svm_copy_lbrs(&vmcb02->save, &vmcb01->save);
+		vmcb_mark_dirty(vmcb02, VMCB_LBR);
 	}
 	svm_update_lbrv(&svm->vcpu);
 }
@@ -1186,10 +1188,12 @@ int nested_svm_vmexit(struct vcpu_svm *svm)
 		kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
 
 	if (unlikely(guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) &&
-		     (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK)))
-		svm_copy_lbrs(vmcb12, vmcb02);
-	else
-		svm_copy_lbrs(vmcb01, vmcb02);
+		     (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) {
+		svm_copy_lbrs(&vmcb12->save, &vmcb02->save);
+	} else {
+		svm_copy_lbrs(&vmcb01->save, &vmcb02->save);
+		vmcb_mark_dirty(vmcb01, VMCB_LBR);
+	}
 
 	svm_update_lbrv(vcpu);
 
arch/x86/kvm/svm/svm.c +0 -11
index fc42bcdbb5200..9eb112f0e61f0 100644
@@ -795,17 +795,6 @@ static void svm_recalc_msr_intercepts(struct kvm_vcpu *vcpu)
 	 */
 }
 
-void svm_copy_lbrs(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
-{
-	to_vmcb->save.dbgctl		= from_vmcb->save.dbgctl;
-	to_vmcb->save.br_from		= from_vmcb->save.br_from;
-	to_vmcb->save.br_to		= from_vmcb->save.br_to;
-	to_vmcb->save.last_excp_from	= from_vmcb->save.last_excp_from;
-	to_vmcb->save.last_excp_to	= from_vmcb->save.last_excp_to;
-
-	vmcb_mark_dirty(to_vmcb, VMCB_LBR);
-}
-
 static void __svm_enable_lbrv(struct kvm_vcpu *vcpu)
 {
 	to_svm(vcpu)->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
arch/x86/kvm/svm/svm.h +9 -1
index c2acaa49ee1c5..e510c8183bd87 100644
@@ -687,8 +687,16 @@ static inline void *svm_vcpu_alloc_msrpm(void)
 	return svm_alloc_permissions_map(MSRPM_SIZE, GFP_KERNEL_ACCOUNT);
 }
 
+#define svm_copy_lbrs(to, from)					\
+({								\
+	(to)->dbgctl		= (from)->dbgctl;		\
+	(to)->br_from		= (from)->br_from;		\
+	(to)->br_to		= (from)->br_to;		\
+	(to)->last_excp_from	= (from)->last_excp_from;	\
+	(to)->last_excp_to	= (from)->last_excp_to;		\
+})
+
 void svm_vcpu_free_msrpm(void *msrpm);
-void svm_copy_lbrs(struct vmcb *to_vmcb, struct vmcb *from_vmcb);
 void svm_enable_lbrv(struct kvm_vcpu *vcpu);
 void svm_update_lbrv(struct kvm_vcpu *vcpu);
 
Signature · 1 line
2.51.2.1041.gc1ab5b90ca-goog
maintainer
#9 lore raw

Re: [PATCH 4/6] KVM: SVM: Switch svm_copy_lbrs() to a macro

Quoted 4 lines · Yosry Ahmed

In preparation for using svm_copy_lbrs() with 'struct vmcb_save_area' without a containing 'struct vmcb', and later even 'struct vmcb_save_area_cached', make it a macro. Pull the call to vmcb_mark_dirty() out to the callers.

The changes to use `struct vmcb_save_area_cached' are not included in this series, so they are irrelevant.

Since I've applied patches 1-3, which fix the worst bugs, there are two ways to handle the rest:

* keep the function instead of the macro, while making it take a struct vmcb_save_area (and therefore pulling vmcb_mark_dirty() to the callers and fixing the bug you mention below).

* you resubmit with the changes to use struct vmcb_save_area_cached, so that the commit message makes more sense.

Thanks,

Paolo

Quoted 93 lines

Macros are generally not preferred compared to functions, mainly due to type-safety. However, in this case it seems like having a simple macro copying a few fields is better than copy-pasting the same 5 lines of code in different places.

On the bright side, pulling vmcb_mark_dirty() calls to the callers makes it clear that in one case, vmcb_mark_dirty() was being called on VMCB12. It is not architecturally defined for the CPU to clear arbitrary clean bits, and it is not needed, so drop that one call.

Technically fixes the non-architectural behavior of setting the dirty bit on VMCB12.

Fixes d20c796ca370 ("KVM: x86: nSVM: implement nested LBR virtualization")
---
  arch/x86/kvm/svm/nested.c | 16 ++++++++++------
  arch/x86/kvm/svm/svm.c    | 11 -----------
  arch/x86/kvm/svm/svm.h    | 10 +++++++++-
  3 files changed, 19 insertions(+), 18 deletions(-)
arch/x86/kvm/svm/nested.c +10 -6
index c81005b245222..e7861392f2fcd 100644
@@ -676,10 +676,12 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12
  		 * Reserved bits of DEBUGCTL are ignored.  Be consistent with
  		 * svm_set_msr's definition of reserved bits.
  		 */
-		svm_copy_lbrs(vmcb02, vmcb12);
+		svm_copy_lbrs(&vmcb02->save, &vmcb12->save);
+		vmcb_mark_dirty(vmcb02, VMCB_LBR);
  		vmcb02->save.dbgctl &= ~DEBUGCTL_RESERVED_BITS;
  	} else {
-		svm_copy_lbrs(vmcb02, vmcb01);
+		svm_copy_lbrs(&vmcb02->save, &vmcb01->save);
+		vmcb_mark_dirty(vmcb02, VMCB_LBR);
  	}
  	svm_update_lbrv(&svm->vcpu);
  }
@@ -1186,10 +1188,12 @@ int nested_svm_vmexit(struct vcpu_svm *svm)
  		kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
 
  	if (unlikely(guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) &&
-		     (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK)))
-		svm_copy_lbrs(vmcb12, vmcb02);
-	else
-		svm_copy_lbrs(vmcb01, vmcb02);
+		     (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) {
+		svm_copy_lbrs(&vmcb12->save, &vmcb02->save);
+	} else {
+		svm_copy_lbrs(&vmcb01->save, &vmcb02->save);
+		vmcb_mark_dirty(vmcb01, VMCB_LBR);
+	}
 
  	svm_update_lbrv(vcpu);
 
arch/x86/kvm/svm/svm.c +0 -11
index fc42bcdbb5200..9eb112f0e61f0 100644
@@ -795,17 +795,6 @@ static void svm_recalc_msr_intercepts(struct kvm_vcpu *vcpu)
  	 */
  }
 
-void svm_copy_lbrs(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
-{
-	to_vmcb->save.dbgctl		= from_vmcb->save.dbgctl;
-	to_vmcb->save.br_from		= from_vmcb->save.br_from;
-	to_vmcb->save.br_to		= from_vmcb->save.br_to;
-	to_vmcb->save.last_excp_from	= from_vmcb->save.last_excp_from;
-	to_vmcb->save.last_excp_to	= from_vmcb->save.last_excp_to;
-
-	vmcb_mark_dirty(to_vmcb, VMCB_LBR);
-}
-
  static void __svm_enable_lbrv(struct kvm_vcpu *vcpu)
  {
  	to_svm(vcpu)->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
arch/x86/kvm/svm/svm.h +4 -0
index c2acaa49ee1c5..e510c8183bd87 100644
@@ -687,8 +687,16 @@ static inline void *svm_vcpu_alloc_msrpm(void)
  	return svm_alloc_permissions_map(MSRPM_SIZE, GFP_KERNEL_ACCOUNT);
  }
 
+#define svm_copy_lbrs(to, from)					\
+({								\
+	(to)->dbgctl		= (from)->dbgctl;		\
+	(to)->br_from		= (from)->br_from;		\

arch/x86/kvm/svm/svm.h static inline void *svm_vcpu_alloc_msrpm(void) · line 702

Quoted 10 lines
arch/x86/kvm/svm/svm.h +5 -1
+	(to)->br_to		= (from)->br_to;		\
+	(to)->last_excp_from	= (from)->last_excp_from;	\
+	(to)->last_excp_to	= (from)->last_excp_to;		\
+})
+
  void svm_vcpu_free_msrpm(void *msrpm);
-void svm_copy_lbrs(struct vmcb *to_vmcb, struct vmcb *from_vmcb);
  void svm_enable_lbrv(struct kvm_vcpu *vcpu);
  void svm_update_lbrv(struct kvm_vcpu *vcpu);
 

Since

#10 lore raw

Re: [PATCH 4/6] KVM: SVM: Switch svm_copy_lbrs() to a macro

Quoted 12 lines · Paolo Bonzini
Quoted 4 lines · Yosry Ahmed

In preparation for using svm_copy_lbrs() with 'struct vmcb_save_area' without a containing 'struct vmcb', and later even 'struct vmcb_save_area_cached', make it a macro. Pull the call to vmcb_mark_dirty() out to the callers.

The changes to use `struct vmcb_save_area_cached' are not included in this series, so they are irrelevant.

Since I've applied patches 1-3, which fix the worst bugs, there are two ways to handle the rest:

Quoted 6 lines

* keep the function instead of the macro, while making it take a struct vmcb_save_area (and therefore pulling vmcb_mark_dirty() to the callers and fixing the bug you mention below).

* you resubmit with the changes to use struct vmcb_save_area_cached, so that the commit message makes more sense.

I can include patches 4-6 with the respin of the series [1] that has the changes to use struct vmcb_save_area_cached. That series origianlly had the patch to switch svm_copy_lbrs() to a macro, but I moved it here to use for the save/restore patch. I was planning to rebase [1] on top of this series anyway.

There is a hiccup though, I assumed everything would go through Sean's tree so I planned to respin [1] on top of this series. Otherwise, they will conflict. With the first 3 patches in your tree, I am not sure how that would work.

I can respin [1] on top of Sean's kvm-x86/next or kvm-x86/svm, but it will conflict with the patches you picked up eventually, and I already have them locally on top of the LBR fixes so it seems like wasted effort.

Sean, Paolo, how do you want to handle this?

[1]https://lore.kernel.org/kvm/[email protected]/

Quoted 104 lines

Thanks,

Paolo

Quoted 87 lines

Macros are generally not preferred compared to functions, mainly due to type-safety. However, in this case it seems like having a simple macro copying a few fields is better than copy-pasting the same 5 lines of code in different places.

On the bright side, pulling vmcb_mark_dirty() calls to the callers makes it clear that in one case, vmcb_mark_dirty() was being called on VMCB12. It is not architecturally defined for the CPU to clear arbitrary clean bits, and it is not needed, so drop that one call.

Technically fixes the non-architectural behavior of setting the dirty bit on VMCB12.

Fixes d20c796ca370 ("KVM: x86: nSVM: implement nested LBR virtualization")
---
  arch/x86/kvm/svm/nested.c | 16 ++++++++++------
  arch/x86/kvm/svm/svm.c    | 11 -----------
  arch/x86/kvm/svm/svm.h    | 10 +++++++++-
  3 files changed, 19 insertions(+), 18 deletions(-)
arch/x86/kvm/svm/nested.c +10 -6
index c81005b245222..e7861392f2fcd 100644
@@ -676,10 +676,12 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm, struct vmcb *vmcb12
  		 * Reserved bits of DEBUGCTL are ignored.  Be consistent with
  		 * svm_set_msr's definition of reserved bits.
  		 */
-		svm_copy_lbrs(vmcb02, vmcb12);
+		svm_copy_lbrs(&vmcb02->save, &vmcb12->save);
+		vmcb_mark_dirty(vmcb02, VMCB_LBR);
  		vmcb02->save.dbgctl &= ~DEBUGCTL_RESERVED_BITS;
  	} else {
-		svm_copy_lbrs(vmcb02, vmcb01);
+		svm_copy_lbrs(&vmcb02->save, &vmcb01->save);
+		vmcb_mark_dirty(vmcb02, VMCB_LBR);
  	}
  	svm_update_lbrv(&svm->vcpu);
  }
@@ -1186,10 +1188,12 @@ int nested_svm_vmexit(struct vcpu_svm *svm)
  		kvm_make_request(KVM_REQ_EVENT, &svm->vcpu);
  	if (unlikely(guest_cpu_cap_has(vcpu, X86_FEATURE_LBRV) &&
-		     (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK)))
-		svm_copy_lbrs(vmcb12, vmcb02);
-	else
-		svm_copy_lbrs(vmcb01, vmcb02);
+		     (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))) {
+		svm_copy_lbrs(&vmcb12->save, &vmcb02->save);
+	} else {
+		svm_copy_lbrs(&vmcb01->save, &vmcb02->save);
+		vmcb_mark_dirty(vmcb01, VMCB_LBR);
+	}
  	svm_update_lbrv(vcpu);
arch/x86/kvm/svm/svm.c +0 -11
index fc42bcdbb5200..9eb112f0e61f0 100644
@@ -795,17 +795,6 @@ static void svm_recalc_msr_intercepts(struct kvm_vcpu *vcpu)
  	 */
  }
-void svm_copy_lbrs(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
-{
-	to_vmcb->save.dbgctl		= from_vmcb->save.dbgctl;
-	to_vmcb->save.br_from		= from_vmcb->save.br_from;
-	to_vmcb->save.br_to		= from_vmcb->save.br_to;
-	to_vmcb->save.last_excp_from	= from_vmcb->save.last_excp_from;
-	to_vmcb->save.last_excp_to	= from_vmcb->save.last_excp_to;
-
-	vmcb_mark_dirty(to_vmcb, VMCB_LBR);
-}
-
  static void __svm_enable_lbrv(struct kvm_vcpu *vcpu)
  {
  	to_svm(vcpu)->vmcb->control.virt_ext |= LBR_CTL_ENABLE_MASK;
arch/x86/kvm/svm/svm.h +3 -0
index c2acaa49ee1c5..e510c8183bd87 100644
@@ -687,8 +687,16 @@ static inline void *svm_vcpu_alloc_msrpm(void)
  	return svm_alloc_permissions_map(MSRPM_SIZE, GFP_KERNEL_ACCOUNT);
  }
+#define svm_copy_lbrs(to, from)					\
+({								\
+	(to)->dbgctl		= (from)->dbgctl;		\

arch/x86/kvm/svm/svm.h static inline void *svm_vcpu_alloc_msrpm(void) · line 700

Quoted 10 lines
arch/x86/kvm/svm/svm.h +6 -1
+	(to)->br_from		= (from)->br_from;		\
+	(to)->br_to		= (from)->br_to;		\
+	(to)->last_excp_from	= (from)->last_excp_from;	\
+	(to)->last_excp_to	= (from)->last_excp_to;		\
+})
+
  void svm_vcpu_free_msrpm(void *msrpm);
-void svm_copy_lbrs(struct vmcb *to_vmcb, struct vmcb *from_vmcb);
  void svm_enable_lbrv(struct kvm_vcpu *vcpu);
  void svm_update_lbrv(struct kvm_vcpu *vcpu);

Since

#11 lore raw

Re: [PATCH 4/6] KVM: SVM: Switch svm_copy_lbrs() to a macro

Quoted 31 lines · Yosry Ahmed
Quoted 12 lines · Paolo Bonzini
Quoted 4 lines · Yosry Ahmed

In preparation for using svm_copy_lbrs() with 'struct vmcb_save_area' without a containing 'struct vmcb', and later even 'struct vmcb_save_area_cached', make it a macro. Pull the call to vmcb_mark_dirty() out to the callers.

The changes to use `struct vmcb_save_area_cached' are not included in this series, so they are irrelevant.

Since I've applied patches 1-3, which fix the worst bugs, there are two ways to handle the rest:

Quoted 6 lines

* keep the function instead of the macro, while making it take a struct vmcb_save_area (and therefore pulling vmcb_mark_dirty() to the callers and fixing the bug you mention below).

* you resubmit with the changes to use struct vmcb_save_area_cached, so that the commit message makes more sense.

I can include patches 4-6 with the respin of the series [1] that has the changes to use struct vmcb_save_area_cached. That series origianlly had the patch to switch svm_copy_lbrs() to a macro, but I moved it here to use for the save/restore patch. I was planning to rebase [1] on top of this series anyway.

There is a hiccup though, I assumed everything would go through Sean's tree so I planned to respin [1] on top of this series. Otherwise, they will conflict. With the first 3 patches in your tree, I am not sure how that would work.

Quoted 6 lines

I can respin [1] on top of Sean's kvm-x86/next or kvm-x86/svm, but it will conflict with the patches you picked up eventually, and I already have them locally on top of the LBR fixes so it seems like wasted effort.

Sean, Paolo, how do you want to handle this?

Base your patches on kvm/master, assuming there's nothing in kvm-x86/svm that you need. I can create a one-off branch, e.g. kvm-x86/lbrv, based on kvm/master or 6.18-rc6.

#12 lore raw

PATCH 5/6 KVM: SVM: Add missing save/restore handling of LBR MSRs

In reply to Yosry Ahmed

MSR_IA32_DEBUGCTLMSR and LBR MSRs are currently not enumerated by KVM_GET_MSR_INDEX_LIST, and LBR MSRs cannot be set with KVM_SET_MSRS. So save/restore is completely broken.

Fix it by adding the MSRs to msrs_to_save_base, and allowing writes to LBR MSRs from userspace only (as they are read-only MSRs). Additionally, to correctly restore L1's LBRs while L2 is running, make sure the LBRs are copied from the captured VMCB01 save area in svm_copy_vmrun_state().

Fixes 24e09cbf480a ("KVM: SVM: enable LBR virtualization")
---
 arch/x86/kvm/svm/nested.c |  3 +++
 arch/x86/kvm/svm/svm.c    | 20 ++++++++++++++++++++
 arch/x86/kvm/x86.c        |  3 +++
 3 files changed, 26 insertions(+)
arch/x86/kvm/svm/nested.c +3 -0
index e7861392f2fcd..955de5d90f48e 100644
@@ -1057,6 +1057,9 @@ void svm_copy_vmrun_state(struct vmcb_save_area *to_save,
 		to_save->isst_addr = from_save->isst_addr;
 		to_save->ssp = from_save->ssp;
 	}
+
+	if (lbrv)
+		svm_copy_lbrs(to_save, from_save);
 }
 
 void svm_copy_vmloadsave_state(struct vmcb *to_vmcb, struct vmcb *from_vmcb)
arch/x86/kvm/svm/svm.c +20 -0
index 9eb112f0e61f0..45faf892a0431 100644
@@ -2988,6 +2988,26 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
 		vmcb_mark_dirty(svm->vmcb, VMCB_LBR);
 		svm_update_lbrv(vcpu);
 		break;
+	case MSR_IA32_LASTBRANCHFROMIP:
+		if (!msr->host_initiated)
+			return 1;
+		svm->vmcb->save.br_from = data;
+		break;
+	case MSR_IA32_LASTBRANCHTOIP:
+		if (!msr->host_initiated)
+			return 1;
+		svm->vmcb->save.br_to = data;
+		break;
+	case MSR_IA32_LASTINTFROMIP:
+		if (!msr->host_initiated)
+			return 1;
+		svm->vmcb->save.last_excp_from = data;
+		break;
+	case MSR_IA32_LASTINTTOIP:
+		if (!msr->host_initiated)
+			return 1;
+		svm->vmcb->save.last_excp_to = data;
+		break;
 	case MSR_VM_HSAVE_PA:
 		/*
 		 * Old kernels did not validate the value written to
arch/x86/kvm/x86.c +3 -0
index 9c2e28028c2b5..1d36a0f782c49 100644
@@ -348,6 +348,9 @@ static const u32 msrs_to_save_base[] = {
 	MSR_IA32_U_CET, MSR_IA32_S_CET,
 	MSR_IA32_PL0_SSP, MSR_IA32_PL1_SSP, MSR_IA32_PL2_SSP,
 	MSR_IA32_PL3_SSP, MSR_IA32_INT_SSP_TAB,
+	MSR_IA32_DEBUGCTLMSR,
+	MSR_IA32_LASTBRANCHFROMIP, MSR_IA32_LASTBRANCHTOIP,
+	MSR_IA32_LASTINTFROMIP, MSR_IA32_LASTINTTOIP,
 };
 
 static const u32 msrs_to_save_pmu[] = {
Signature · 1 line
2.51.2.1041.gc1ab5b90ca-goog
#13 lore raw

Re: [PATCH 5/6] KVM: SVM: Add missing save/restore handling of LBR MSRs

Quoted 5 lines · Yosry Ahmed

MSR_IA32_DEBUGCTLMSR and LBR MSRs are currently not enumerated by KVM_GET_MSR_INDEX_LIST, and LBR MSRs cannot be set with KVM_SET_MSRS. So save/restore is completely broken.

Quoted 8 lines

Fix it by adding the MSRs to msrs_to_save_base, and allowing writes to LBR MSRs from userspace only (as they are read-only MSRs). Additionally, to correctly restore L1's LBRs while L2 is running, make sure the LBRs are copied from the captured VMCB01 save area in svm_copy_vmrun_state().

Fixes 24e09cbf480a ("KVM: SVM: enable LBR virtualization")
Quoted 1 line

[..]

#14 lore raw

PATCH 6/6 KVM: selftests: Add a test for LBR save/restore (ft. nested)

In reply to Yosry Ahmed

Add a selftest exercising save/restore with usage of LBRs in both L1 and L2, and making sure all LBRs remain intact.

---
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/include/x86/processor.h     |   5 +
 .../selftests/kvm/x86/svm_lbr_nested_state.c  | 155 ++++++++++++++++++
 3 files changed, 161 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/x86/svm_lbr_nested_state.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 148d427ff24be..9a19554ffd3c1 100644
@@ -105,6 +105,7 @@ TEST_GEN_PROGS_x86 += x86/svm_vmcall_test
 TEST_GEN_PROGS_x86 += x86/svm_int_ctl_test
 TEST_GEN_PROGS_x86 += x86/svm_nested_shutdown_test
 TEST_GEN_PROGS_x86 += x86/svm_nested_soft_inject_test
+TEST_GEN_PROGS_x86 += x86/svm_lbr_nested_state
 TEST_GEN_PROGS_x86 += x86/tsc_scaling_sync
 TEST_GEN_PROGS_x86 += x86/sync_regs_test
 TEST_GEN_PROGS_x86 += x86/ucna_injection_test
tools/testing/selftests/kvm/include/x86/processor.h +5 -0
index 51cd84b9ca664..aee4b83c47b19 100644
@@ -1367,6 +1367,11 @@ static inline bool kvm_is_ignore_msrs(void)
 	return get_kvm_param_bool("ignore_msrs");
 }
 
+static inline bool kvm_is_lbrv_enabled(void)
+{
+	return !!get_kvm_amd_param_integer("lbrv");
+}
+
 uint64_t *__vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr,
 				    int *level);
 uint64_t *vm_get_page_table_entry(struct kvm_vm *vm, uint64_t vaddr);
tools/testing/selftests/kvm/x86/svm_lbr_nested_state.c new file +155 -0

155 lines not on this page · raw message

Signature · 1 line
2.51.2.1041.gc1ab5b90ca-goog
maintainer
#15 lore raw
In reply to Yosry Ahmed
Quoted 7 lines · Yosry Ahmed

This series fixes multiple problems with LBR virtualization, including a fun problem that leads to L1 reading the host's LBR MSRs. It also considerably simplifies the code.

The series has a selftest in the end that verifies that save/restore work correctly. I will send a couple of new kvm-unit-tests separately that exercise the bugs fixed by patches 2 & 3.

Thanks, I've applied patches 1-3 for now.

Save/restore has been broken for 17 years so it can wait the next release anyway. :)

Paolo

Quoted 19 lines

Yosry Ahmed (6):
KVM: SVM: Mark VMCB_LBR dirty when MSR_IA32_DEBUGCTLMSR is updated
KVM: nSVM: Always recalculate LBR MSR intercepts in svm_update_lbrv()
KVM: nSVM: Fix and simplify LBR virtualization handling with nested
KVM: SVM: Switch svm_copy_lbrs() to a macro
KVM: SVM: Add missing save/restore handling of LBR MSRs
KVM: selftests: Add a test for LBR save/restore (ft. nested)

  arch/x86/kvm/svm/nested.c                     |  31 ++--
  arch/x86/kvm/svm/svm.c                        |  98 ++++++-----
  arch/x86/kvm/svm/svm.h                        |  10 +-
  arch/x86/kvm/x86.c                            |   3 +
  tools/testing/selftests/kvm/Makefile.kvm      |   1 +
  .../selftests/kvm/include/x86/processor.h     |   5 +
  .../selftests/kvm/x86/svm_lbr_nested_state.c  | 155 ++++++++++++++++++
  7 files changed, 236 insertions(+), 67 deletions(-)

create mode 100644 tools/testing/selftests/kvm/x86/svm_lbr_nested_state.c