Skip to contentSkip to conversation

KVM: x86: SVM: Mark VMCB_LBR dirty when L1 sets DebugCtl[LBR]

PATCH In review Jim Mattson 3 participants 7 messages Nov 1, 2025 – Nov 7, 2025

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

PATCH 1/1 KVM: x86: SVM: Mark VMCB_LBR dirty when L1 sets DebugCtl[LBR]

With the VMCB's LBR_VIRTUALIZATION_ENABLE bit set, the CPU will load the DebugCtl MSR from the VMCB's DBGCTL field at VMRUN. To ensure that it does not load a stale cached value, clear the VMCB's LBR clean bit when L1 is running and bit 0 (LBR) of the DBGCTL field is changed from 0 to 1. (Note that this is already handled correctly when L2 is running.)

There is no need to clear the clean bit in the other direction, because when the VMCB's DBGCTL.LBR is 0, the VMCB's LBR_VIRTUALIZATION_ENABLE bit will be clear, and the CPU will not consult the VMCB's DBGCTL field at VMRUN.

Fixes 1d5a1b5860ed ("KVM: x86: nSVM: correctly virtualize LBR msrs when L2 is running")
Reported-by [email protected]
---
 arch/x86/kvm/svm/svm.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kvm/svm/svm.c +2 -0
index 153c12dbf3eb..b4e5a0684f57 100644
@@ -816,6 +816,8 @@ void svm_enable_lbrv(struct kvm_vcpu *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);
+	else
+		vmcb_mark_dirty(svm->vmcb, VMCB_LBR);
 }
 
 static void svm_disable_lbrv(struct kvm_vcpu *vcpu)
Signature · 1 line
2.51.2.1006.ga50a493c49-goog
#1 lore raw
Quoted 11 lines · Jim Mattson

With the VMCB's LBR_VIRTUALIZATION_ENABLE bit set, the CPU will load the DebugCtl MSR from the VMCB's DBGCTL field at VMRUN. To ensure that it does not load a stale cached value, clear the VMCB's LBR clean bit when L1 is running and bit 0 (LBR) of the DBGCTL field is changed from 0 to 1. (Note that this is already handled correctly when L2 is running.)

There is no need to clear the clean bit in the other direction, because when the VMCB's DBGCTL.LBR is 0, the VMCB's LBR_VIRTUALIZATION_ENABLE bit will be clear, and the CPU will not consult the VMCB's DBGCTL field at VMRUN.

Is it worth the mental load of figuring out why we do it in svm_enable_lbrv() but not svm_disable_lbrv()?

Maybe we can at least document it in svm_disable_lbrv() with a comment?

Quoted 25 lines
Fixes 1d5a1b5860ed ("KVM: x86: nSVM: correctly virtualize LBR msrs when L2 is running")
Reported-by [email protected]
---
 arch/x86/kvm/svm/svm.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kvm/svm/svm.c +2 -0
index 153c12dbf3eb..b4e5a0684f57 100644
@@ -816,6 +816,8 @@ void svm_enable_lbrv(struct kvm_vcpu *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);
+	else
+		vmcb_mark_dirty(svm->vmcb, VMCB_LBR);
 }
 
 static void svm_disable_lbrv(struct kvm_vcpu *vcpu)
Signature · 1 line
2.51.2.1006.ga50a493c49-goog
#2 lore raw
Quoted 14 lines · Yosry Ahmed
Quoted 11 lines · Jim Mattson

With the VMCB's LBR_VIRTUALIZATION_ENABLE bit set, the CPU will load the DebugCtl MSR from the VMCB's DBGCTL field at VMRUN. To ensure that it does not load a stale cached value, clear the VMCB's LBR clean bit when L1 is running and bit 0 (LBR) of the DBGCTL field is changed from 0 to 1. (Note that this is already handled correctly when L2 is running.)

There is no need to clear the clean bit in the other direction, because when the VMCB's DBGCTL.LBR is 0, the VMCB's LBR_VIRTUALIZATION_ENABLE bit will be clear, and the CPU will not consult the VMCB's DBGCTL field at VMRUN.

Quoted 4 lines

Is it worth the mental load of figuring out why we do it in svm_enable_lbrv() but not svm_disable_lbrv()?

Maybe we can at least document it in svm_disable_lbrv() with a comment?

I'm happy to do it in svm_disable_lbrv() as well, just to reduce the cognitive load.

#3 lore raw
In reply to Jim Mattson
Quoted 25 lines · Jim Mattson

With the VMCB's LBR_VIRTUALIZATION_ENABLE bit set, the CPU will load the DebugCtl MSR from the VMCB's DBGCTL field at VMRUN. To ensure that it does not load a stale cached value, clear the VMCB's LBR clean bit when L1 is running and bit 0 (LBR) of the DBGCTL field is changed from 0 to 1. (Note that this is already handled correctly when L2 is running.)

There is no need to clear the clean bit in the other direction, because when the VMCB's DBGCTL.LBR is 0, the VMCB's LBR_VIRTUALIZATION_ENABLE bit will be clear, and the CPU will not consult the VMCB's DBGCTL field at VMRUN.

Fixes 1d5a1b5860ed ("KVM: x86: nSVM: correctly virtualize LBR msrs when L2 is running")
Reported-by [email protected]
---
 arch/x86/kvm/svm/svm.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kvm/svm/svm.c +0 -0
index 153c12dbf3eb..b4e5a0684f57 100644

arch/x86/kvm/svm/svm.c void svm_enable_lbrv(struct kvm_vcpu *vcpu) · line 823

Quoted 8 lines
arch/x86/kvm/svm/svm.c +2 -0
@@ -816,6 +816,8 @@ void svm_enable_lbrv(struct kvm_vcpu *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);
+	else
+		vmcb_mark_dirty(svm->vmcb, VMCB_LBR);
 }
 
 static void svm_disable_lbrv(struct kvm_vcpu *vcpu)

Hi Jim,
I am thinking, is it possible to add a test in KVM Unit Tests that covers this? Something where the stale cached value is loaded instead of the correct one, without your patch.

Best regards,
Shivansh

#4 lore raw
Quoted 34 lines · Shivansh Dhiman
Quoted 32 lines · Jim Mattson

With the VMCB's LBR_VIRTUALIZATION_ENABLE bit set, the CPU will load the DebugCtl MSR from the VMCB's DBGCTL field at VMRUN. To ensure that it does not load a stale cached value, clear the VMCB's LBR clean bit when L1 is running and bit 0 (LBR) of the DBGCTL field is changed from 0 to 1. (Note that this is already handled correctly when L2 is running.)

There is no need to clear the clean bit in the other direction, because when the VMCB's DBGCTL.LBR is 0, the VMCB's LBR_VIRTUALIZATION_ENABLE bit will be clear, and the CPU will not consult the VMCB's DBGCTL field at VMRUN.

Fixes 1d5a1b5860ed ("KVM: x86: nSVM: correctly virtualize LBR msrs when L2 is running")
Reported-by [email protected]
---
 arch/x86/kvm/svm/svm.c | 2 ++
 1 file changed, 2 insertions(+)
arch/x86/kvm/svm/svm.c +2 -0
index 153c12dbf3eb..b4e5a0684f57 100644
@@ -816,6 +816,8 @@ void svm_enable_lbrv(struct kvm_vcpu *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);
+     else
+             vmcb_mark_dirty(svm->vmcb, VMCB_LBR);
 }
 
Quoted 6 lines

arch/x86/kvm/svm/svm.c void svm_enable_lbrv(struct kvm_vcpu *vcpu) · line 823

Quoted 1 line
arch/x86/kvm/svm/svm.c +0 -0
 static void svm_disable_lbrv(struct kvm_vcpu *vcpu)

Hi Jim,
I am thinking, is it possible to add a test in KVM Unit Tests that covers this? Something where the stale cached value is loaded instead of the correct one, without your patch.

Though permitted by the architectural specification, I don't know if there is any hardware that caches the DBGCTL field when LBR virtualization is disabled.

Tested-by
#5 lore raw
Quoted 10 lines · Jim Mattson
Quoted 9 lines · Shivansh Dhiman
Quoted 7 lines · Jim Mattson

With the VMCB's LBR_VIRTUALIZATION_ENABLE bit set, the CPU will load the DebugCtl MSR from the VMCB's DBGCTL field at VMRUN. To ensure that it does not load a stale cached value, clear the VMCB's LBR clean bit when L1 is running and bit 0 (LBR) of the DBGCTL field is changed from 0 to 1. (Note that this is already handled correctly when L2 is running.)

Quoted 8 lines
Quoted 4 lines

Hi Jim,
I am thinking, is it possible to add a test in KVM Unit Tests that covers this? Something where the stale cached value is loaded instead of the correct one, without your patch.

Though permitted by the architectural specification, I don't know if there is any hardware that caches the DBGCTL field when LBR virtualization is disabled.

Alrighty.

-Shivansh

#6 lore raw

I'm going to rescind this patch. Yosry is going to handle the situation differently in his LBRV patch series.