Skip to contentSkip to conversation

KVM: SVM: Clean up LBRv MSRs handling

PATCH Applied Sean Christopherson 1 participant 5 messages Jun 7, 2023 – Aug 3, 2023

Subsystems (3)
lore
Share & tools
lore

Sean Christopherson

author maintainer
#0 lore raw

cover KVM: SVM: Clean up LBRv MSRs handling

Eliminate dead KVM_BUG() code in SVM's LBR MSRs virtualization by refactoring the code to completely remove any need for a KVM_BUG(), and clean up a few others pieces of related code.

Sean Christopherson (3):
KVM: SVM: Fix dead KVM_BUG() code in LBR MSR virtualization
KVM: SVM: Clean up handling of LBR virtualization enabled
KVM: SVM: Use svm_get_lbr_vmcb() helper to handle writes to DEBUGCTL

 arch/x86/kvm/svm/svm.c | 63 ++++++++++++++----------------------------
 1 file changed, 20 insertions(+), 43 deletions(-)

base-commit: 24ff4c08e5bbdd7399d45f940f10fed030dfadda

Signature · 1 line
2.41.0.162.gfafddb0af9-goog
author maintainer
#1 lore raw

PATCH 1/3 KVM: SVM: Fix dead KVM_BUG() code in LBR MSR virtualization

Refactor KVM's handling of LBR MSRs on SVM to avoid a second layer of case statements, and thus eliminate a dead KVM_BUG() call, which (a) will never be hit in the current code base and (b) if a future commit breaks things, will never fire as KVM passes "false" instead "true" or '1' for the KVM_BUG() condition.

---
 arch/x86/kvm/svm/svm.c | 45 +++++++++++++++---------------------------
 1 file changed, 16 insertions(+), 29 deletions(-)
arch/x86/kvm/svm/svm.c +16 -29
index e265834fe859..b7d145571f75 100644
@@ -947,43 +947,22 @@ static void svm_disable_lbrv(struct kvm_vcpu *vcpu)
 		svm_copy_lbrs(svm->vmcb01.ptr, svm->vmcb);
 }
 
-static int svm_get_lbr_msr(struct vcpu_svm *svm, u32 index)
+static struct vmcb *svm_get_lbr_vmcb(struct vcpu_svm *svm)
 {
 	/*
-	 * If the LBR virtualization is disabled, the LBR msrs are always
-	 * kept in the vmcb01 to avoid copying them on nested guest entries.
-	 *
-	 * If nested, and the LBR virtualization is enabled/disabled, the msrs
-	 * are moved between the vmcb01 and vmcb02 as needed.
+	 * If LBR virtualization is disabled, the LBR MSRs are always kept in
+	 * vmcb01.  If LBR virtualization is enabled and L1 is running VMs of
+	 * its own, the MSRs are moved between vmcb01 and vmcb02 as needed.
 	 */
-	struct vmcb *vmcb =
-		(svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK) ?
-			svm->vmcb : svm->vmcb01.ptr;
-
-	switch (index) {
-	case MSR_IA32_DEBUGCTLMSR:
-		return vmcb->save.dbgctl;
-	case MSR_IA32_LASTBRANCHFROMIP:
-		return vmcb->save.br_from;
-	case MSR_IA32_LASTBRANCHTOIP:
-		return vmcb->save.br_to;
-	case MSR_IA32_LASTINTFROMIP:
-		return vmcb->save.last_excp_from;
-	case MSR_IA32_LASTINTTOIP:
-		return vmcb->save.last_excp_to;
-	default:
-		KVM_BUG(false, svm->vcpu.kvm,
-			"%s: Unknown MSR 0x%x", __func__, index);
-		return 0;
-	}
+	return svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK ? svm->vmcb :
+								   svm->vmcb01.ptr;
 }
 
 void svm_update_lbrv(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
 
-	bool enable_lbrv = svm_get_lbr_msr(svm, MSR_IA32_DEBUGCTLMSR) &
-					   DEBUGCTLMSR_LBR;
+	bool enable_lbrv = svm_get_lbr_vmcb(svm)->save.dbgctl & DEBUGCTLMSR_LBR;
 
 	bool current_enable_lbrv = !!(svm->vmcb->control.virt_ext &
 				      LBR_CTL_ENABLE_MASK);
@@ -2795,11 +2774,19 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		msr_info->data = svm->tsc_aux;
 		break;
 	case MSR_IA32_DEBUGCTLMSR:
+		msr_info->data = svm_get_lbr_vmcb(svm)->save.dbgctl;
+		break;
 	case MSR_IA32_LASTBRANCHFROMIP:
+		msr_info->data = svm_get_lbr_vmcb(svm)->save.br_from;
+		break;
 	case MSR_IA32_LASTBRANCHTOIP:
+		msr_info->data = svm_get_lbr_vmcb(svm)->save.br_to;
+		break;
 	case MSR_IA32_LASTINTFROMIP:
+		msr_info->data = svm_get_lbr_vmcb(svm)->save.last_excp_from;
+		break;
 	case MSR_IA32_LASTINTTOIP:
-		msr_info->data = svm_get_lbr_msr(svm, msr_info->index);
+		msr_info->data = svm_get_lbr_vmcb(svm)->save.last_excp_to;
 		break;
 	case MSR_VM_HSAVE_PA:
 		msr_info->data = svm->nested.hsave_msr;
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
author maintainer
#2 lore raw

PATCH 2/3 KVM: SVM: Clean up handling of LBR virtualization enabled

In reply to Sean Christopherson

Clean up the enable_lbrv computation in svm_update_lbrv() to consolidate the logic for computing enable_lbrv into a single statement, and to remove the coding style violations (lack of curly braces on nested if).

No functional change intended.

---
 arch/x86/kvm/svm/svm.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)
arch/x86/kvm/svm/svm.c +4 -9
index b7d145571f75..db97eca6c1ae 100644
@@ -961,15 +961,10 @@ static struct vmcb *svm_get_lbr_vmcb(struct vcpu_svm *svm)
 void svm_update_lbrv(struct kvm_vcpu *vcpu)
 {
 	struct vcpu_svm *svm = to_svm(vcpu);
-
-	bool enable_lbrv = svm_get_lbr_vmcb(svm)->save.dbgctl & DEBUGCTLMSR_LBR;
-
-	bool current_enable_lbrv = !!(svm->vmcb->control.virt_ext &
-				      LBR_CTL_ENABLE_MASK);
-
-	if (unlikely(is_guest_mode(vcpu) && svm->lbrv_enabled))
-		if (unlikely(svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK))
-			enable_lbrv = true;
+	bool current_enable_lbrv = svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK;
+	bool enable_lbrv = (svm_get_lbr_vmcb(svm)->save.dbgctl & DEBUGCTLMSR_LBR) ||
+			   (is_guest_mode(vcpu) && svm->lbrv_enabled &&
+			    (svm->nested.ctl.virt_ext & LBR_CTL_ENABLE_MASK));
 
 	if (enable_lbrv == current_enable_lbrv)
 		return;
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
author maintainer
#3 lore raw

PATCH 3/3 KVM: SVM: Use svm_get_lbr_vmcb() helper to handle writes to DEBUGCTL

In reply to Sean Christopherson

Use the recently introduced svm_get_lbr_vmcb() instead an open coded equivalent to retrieve the target VMCB when emulating writes to MSR_IA32_DEBUGCTLMSR.

No functional change intended.

---
 arch/x86/kvm/svm/svm.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)
arch/x86/kvm/svm/svm.c +1 -6
index db97eca6c1ae..b700de261ce8 100644
@@ -3012,13 +3012,8 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr)
 		if (data & DEBUGCTL_RESERVED_BITS)
 			return 1;
 
-		if (svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK)
-			svm->vmcb->save.dbgctl = data;
-		else
-			svm->vmcb01.ptr->save.dbgctl = data;
-
+		svm_get_lbr_vmcb(svm)->save.dbgctl = data;
 		svm_update_lbrv(vcpu);
-
 		break;
 	case MSR_VM_HSAVE_PA:
 		/*
Signature · 1 line
2.41.0.162.gfafddb0af9-goog
author maintainer Applied
#4 lore raw
In reply to Sean Christopherson
Quoted 10 lines · Sean Christopherson

Eliminate dead KVM_BUG() code in SVM's LBR MSRs virtualization by refactoring the code to completely remove any need for a KVM_BUG(), and clean up a few others pieces of related code.

Sean Christopherson (3):
KVM: SVM: Fix dead KVM_BUG() code in LBR MSR virtualization
KVM: SVM: Clean up handling of LBR virtualization enabled
KVM: SVM: Use svm_get_lbr_vmcb() helper to handle writes to DEBUGCTL

[...]

Applied to kvm-x86 svm, thanks!

[1/3] KVM: SVM: Fix dead KVM_BUG() code in LBR MSR virtualization

      https://github.com/kvm-x86/linux/commit/d518f8cc10af
[2/3] KVM: SVM: Clean up handling of LBR virtualization enabled
      https://github.com/kvm-x86/linux/commit/41dfb5f13ed9
[3/3] KVM: SVM: Use svm_get_lbr_vmcb() helper to handle writes to DEBUGCTL
      https://github.com/kvm-x86/linux/commit/a85cd52d7205

--
https://github.com/kvm-x86/linux/tree/next
https://github.com/kvm-x86/linux/tree/fixes