Kernel KVM virtualization development
 help / color / mirror / Atom feed
* [PATCH 0/3] KVM: nSVM: Nested virtual LBR fix and cleanups
@ 2026-12-05  1:11 Yosry Ahmed
  2026-12-05  1:11 ` [PATCH 1/3] KVM: nSVM: Update virtual LBR state/intercepts on KVM_SET_NESTED_STATE Yosry Ahmed
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Yosry Ahmed @ 2026-12-05  1:11 UTC (permalink / raw)
  To: Sean Christopherson; +Cc: Paolo Bonzini, kvm, linux-kernel, Yosry Ahmed

A minor fix for a missed virtual LBR state update during save+restore
and a couple of cleanups on top.

Yosry Ahmed (3):
  KVM: nSVM: Update virtual LBR state/intercepts on KVM_SET_NESTED_STATE
  KVM: nSVM: Call svm_update_lbrv() outside nested_vmcb02_prepare_save()
  KVM: nSVM: Explicitly initialize misc_ctl2 in vmcb02

 arch/x86/kvm/svm/nested.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)


base-commit: 4c9f462c7094c480144c98a942af7e935fa87a6a
-- 
2.56.1.987.g2ec0ab4260-goog


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/3] KVM: nSVM: Update virtual LBR state/intercepts on KVM_SET_NESTED_STATE
  2026-12-05  1:11 [PATCH 0/3] KVM: nSVM: Nested virtual LBR fix and cleanups Yosry Ahmed
@ 2026-12-05  1:11 ` Yosry Ahmed
  2026-12-05  1:11 ` [PATCH 2/3] KVM: nSVM: Call svm_update_lbrv() outside nested_vmcb02_prepare_save() Yosry Ahmed
  2026-12-05  1:11 ` [PATCH 3/3] KVM: nSVM: Explicitly initialize misc_ctl2 in vmcb02 Yosry Ahmed
  2 siblings, 0 replies; 4+ messages in thread
From: Yosry Ahmed @ 2026-12-05  1:11 UTC (permalink / raw)
  To: Sean Christopherson
  Cc: Paolo Bonzini, kvm, linux-kernel, Yosry Ahmed, stable,
	Ulfar Erlingsson

KVM calls svm_update_lbrv() to check if virtual LBR (i.e.
SVM_MISC2_ENABLE_V_LBR) needs to be enabled in the VMCB and update the
intercepts accordingly on nested transitions, and when
MSR_IA32_DEBUGCTLMSR is updated.

During live migration, if MSR_IA32_DEBUGCTLMSR is restored before nested
state, and the vCPU is restored into guest mode, svm_update_lbrv() is
not called again to recompute virtual LBR state after vmcb02 is updated
(e.g. to account for L1 enabling/disabling virtual LBR).

Call svm_update_lbrv() after updating vmcb02 in the restore path to
set up virtual LBR accordingly.

Fixes: 8a4821412cf2 ("KVM: nSVM: Fix and simplify LBR virtualization handling with nested")
Cc: stable@vger.kernel.org
Reported-by: Ulfar Erlingsson <ulfar@google.com>
Reported-by: Sean Christopherson <seanjc@google.com>
Closes: https://lore.kernel.org/kvm/atpGGKVAL3bAnmnk@google.com/
Signed-off-by: Yosry Ahmed <yosry@kernel.org>
---
 arch/x86/kvm/svm/nested.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
index 81d768ec82419..c5a572b58be07 100644
--- a/arch/x86/kvm/svm/nested.c
+++ b/arch/x86/kvm/svm/nested.c
@@ -2091,6 +2091,12 @@ static int svm_set_nested_state(struct kvm_vcpu *vcpu,
 
 	nested_vmcb02_prepare_control(svm);
 
+	/*
+	 * Update virtual LBR state after setting up vmcb02's control area in
+	 * case DEBUGCTLMSR was already restored.
+	 */
+	svm_update_lbrv(vcpu);
+
 	/*
 	 * Any previously restored state (e.g. KVM_SET_SREGS) would mark fields
 	 * dirty in vmcb01 instead of vmcb02, so mark all of vmcb02 dirty here.
-- 
2.56.1.987.g2ec0ab4260-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/3] KVM: nSVM: Call svm_update_lbrv() outside nested_vmcb02_prepare_save()
  2026-12-05  1:11 [PATCH 0/3] KVM: nSVM: Nested virtual LBR fix and cleanups Yosry Ahmed
  2026-12-05  1:11 ` [PATCH 1/3] KVM: nSVM: Update virtual LBR state/intercepts on KVM_SET_NESTED_STATE Yosry Ahmed
@ 2026-12-05  1:11 ` Yosry Ahmed
  2026-12-05  1:11 ` [PATCH 3/3] KVM: nSVM: Explicitly initialize misc_ctl2 in vmcb02 Yosry Ahmed
  2 siblings, 0 replies; 4+ messages in thread
From: Yosry Ahmed @ 2026-12-05  1:11 UTC (permalink / raw)
  To: Sean Christopherson; +Cc: Paolo Bonzini, kvm, linux-kernel, Yosry Ahmed

svm_update_lbrv() must be called after both the save and control areas
of vmcb02 are set up as it uses fields from both areas. In the nested
VMRUN path, it is called at the end of nested_vmcb02_prepare_save().
Move it to its only caller, enter_svm_guest_mode(), to make it more
obvious and consistent with svm_set_nested_state().

No functional change intended.

Signed-off-by: Yosry Ahmed <yosry@kernel.org>
---
 arch/x86/kvm/svm/nested.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
index c5a572b58be07..44afa4068e05b 100644
--- a/arch/x86/kvm/svm/nested.c
+++ b/arch/x86/kvm/svm/nested.c
@@ -821,7 +821,6 @@ static void nested_vmcb02_prepare_save(struct vcpu_svm *svm)
 		svm_copy_lbrs(&vmcb02->save, &vmcb01->save);
 	}
 	vmcb_mark_dirty(vmcb02, VMCB_LBR);
-	svm_update_lbrv(&svm->vcpu);
 }
 
 static inline bool is_evtinj_soft(u32 evtinj)
@@ -1070,6 +1069,7 @@ int enter_svm_guest_mode(struct kvm_vcpu *vcpu, u64 vmcb12_gpa, bool from_vmrun)
 	svm_switch_vmcb(svm, &svm->nested.vmcb02);
 	nested_vmcb02_prepare_control(svm);
 	nested_vmcb02_prepare_save(svm);
+	svm_update_lbrv(vcpu);
 
 	ret = nested_svm_load_cr3(&svm->vcpu, svm->nested.save.cr3,
 				  nested_npt_enabled(svm), from_vmrun);
-- 
2.56.1.987.g2ec0ab4260-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 3/3] KVM: nSVM: Explicitly initialize misc_ctl2 in vmcb02
  2026-12-05  1:11 [PATCH 0/3] KVM: nSVM: Nested virtual LBR fix and cleanups Yosry Ahmed
  2026-12-05  1:11 ` [PATCH 1/3] KVM: nSVM: Update virtual LBR state/intercepts on KVM_SET_NESTED_STATE Yosry Ahmed
  2026-12-05  1:11 ` [PATCH 2/3] KVM: nSVM: Call svm_update_lbrv() outside nested_vmcb02_prepare_save() Yosry Ahmed
@ 2026-12-05  1:11 ` Yosry Ahmed
  2 siblings, 0 replies; 4+ messages in thread
From: Yosry Ahmed @ 2026-12-05  1:11 UTC (permalink / raw)
  To: Sean Christopherson; +Cc: Paolo Bonzini, kvm, linux-kernel, Yosry Ahmed

misc_ctl2 is currently not fully initialized on nested VMRUN when
preparing vmcb02. nested_vmcb02_prepare_control() sets
ENABLE_V_VMLOAD_VMSAVE if L1 enables VLS (and is allowed to), but it
never clears the bit. Instead, it relies on
nested_vmcb02_recalc_intercepts() setting VMSAVE/VMLOAD intercepts
otherwise, which renders enabling VLS ineffective.

The only other bit that KVM ever sets is ENABLE_V_LBR, and it relies on
svm_update_lbrv() to explicitly set it or clear it after vmcb02 is set
up.

Explicitly clear misc_ctl2 when preparing vmcb02 for better hygiene, and
to harden against accidentally leaving bits set across different nested
VMRUNs when they shouldn't be.

Suggested-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Yosry Ahmed <yosry@kernel.org>
---
 arch/x86/kvm/svm/nested.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kvm/svm/nested.c b/arch/x86/kvm/svm/nested.c
index 44afa4068e05b..c4ac245a49cf8 100644
--- a/arch/x86/kvm/svm/nested.c
+++ b/arch/x86/kvm/svm/nested.c
@@ -990,7 +990,7 @@ static void nested_vmcb02_prepare_control(struct vcpu_svm *svm)
 	}
 
 	/* SVM_MISC2_ENABLE_V_LBR is controlled by svm_update_lbrv() */
-
+	vmcb02->control.misc_ctl2 = 0;
 	if (!nested_vmcb_needs_vls_intercept(svm))
 		vmcb02->control.misc_ctl2 |= SVM_MISC2_ENABLE_V_VMLOAD_VMSAVE;
 
-- 
2.56.1.987.g2ec0ab4260-goog


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-12-05  1:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05  1:11 [PATCH 0/3] KVM: nSVM: Nested virtual LBR fix and cleanups Yosry Ahmed
2026-12-05  1:11 ` [PATCH 1/3] KVM: nSVM: Update virtual LBR state/intercepts on KVM_SET_NESTED_STATE Yosry Ahmed
2026-12-05  1:11 ` [PATCH 2/3] KVM: nSVM: Call svm_update_lbrv() outside nested_vmcb02_prepare_save() Yosry Ahmed
2026-12-05  1:11 ` [PATCH 3/3] KVM: nSVM: Explicitly initialize misc_ctl2 in vmcb02 Yosry Ahmed

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox