linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] KVM: SVM: Clean up LBRv MSRs handling
@ 2023-06-07 20:35 Sean Christopherson
  2023-06-07 20:35 ` [PATCH 1/3] KVM: SVM: Fix dead KVM_BUG() code in LBR MSR virtualization Sean Christopherson
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Sean Christopherson @ 2023-06-07 20:35 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini
  Cc: kvm, linux-kernel, Michal Luczaj, Yuan Yao

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
-- 
2.41.0.162.gfafddb0af9-goog


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

* [PATCH 1/3] KVM: SVM: Fix dead KVM_BUG() code in LBR MSR virtualization
  2023-06-07 20:35 [PATCH 0/3] KVM: SVM: Clean up LBRv MSRs handling Sean Christopherson
@ 2023-06-07 20:35 ` Sean Christopherson
  2023-06-07 20:35 ` [PATCH 2/3] KVM: SVM: Clean up handling of LBR virtualization enabled Sean Christopherson
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Sean Christopherson @ 2023-06-07 20:35 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini
  Cc: kvm, linux-kernel, Michal Luczaj, Yuan Yao

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.

Reported-by: Michal Luczaj <mhal@rbox.co>
Cc: Yuan Yao <yuan.yao@intel.com>
Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/svm/svm.c | 45 +++++++++++++++---------------------------
 1 file changed, 16 insertions(+), 29 deletions(-)

diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index e265834fe859..b7d145571f75 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -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;
-- 
2.41.0.162.gfafddb0af9-goog


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

* [PATCH 2/3] KVM: SVM: Clean up handling of LBR virtualization enabled
  2023-06-07 20:35 [PATCH 0/3] KVM: SVM: Clean up LBRv MSRs handling Sean Christopherson
  2023-06-07 20:35 ` [PATCH 1/3] KVM: SVM: Fix dead KVM_BUG() code in LBR MSR virtualization Sean Christopherson
@ 2023-06-07 20:35 ` Sean Christopherson
  2023-06-07 20:35 ` [PATCH 3/3] KVM: SVM: Use svm_get_lbr_vmcb() helper to handle writes to DEBUGCTL Sean Christopherson
  2023-08-03  0:05 ` [PATCH 0/3] KVM: SVM: Clean up LBRv MSRs handling Sean Christopherson
  3 siblings, 0 replies; 5+ messages in thread
From: Sean Christopherson @ 2023-06-07 20:35 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini
  Cc: kvm, linux-kernel, Michal Luczaj, Yuan Yao

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.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/svm/svm.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index b7d145571f75..db97eca6c1ae 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -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;
-- 
2.41.0.162.gfafddb0af9-goog


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

* [PATCH 3/3] KVM: SVM: Use svm_get_lbr_vmcb() helper to handle writes to DEBUGCTL
  2023-06-07 20:35 [PATCH 0/3] KVM: SVM: Clean up LBRv MSRs handling Sean Christopherson
  2023-06-07 20:35 ` [PATCH 1/3] KVM: SVM: Fix dead KVM_BUG() code in LBR MSR virtualization Sean Christopherson
  2023-06-07 20:35 ` [PATCH 2/3] KVM: SVM: Clean up handling of LBR virtualization enabled Sean Christopherson
@ 2023-06-07 20:35 ` Sean Christopherson
  2023-08-03  0:05 ` [PATCH 0/3] KVM: SVM: Clean up LBRv MSRs handling Sean Christopherson
  3 siblings, 0 replies; 5+ messages in thread
From: Sean Christopherson @ 2023-06-07 20:35 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini
  Cc: kvm, linux-kernel, Michal Luczaj, Yuan Yao

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.

Signed-off-by: Sean Christopherson <seanjc@google.com>
---
 arch/x86/kvm/svm/svm.c | 7 +------
 1 file changed, 1 insertion(+), 6 deletions(-)

diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c
index db97eca6c1ae..b700de261ce8 100644
--- a/arch/x86/kvm/svm/svm.c
+++ b/arch/x86/kvm/svm/svm.c
@@ -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:
 		/*
-- 
2.41.0.162.gfafddb0af9-goog


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

* Re: [PATCH 0/3] KVM: SVM: Clean up LBRv MSRs handling
  2023-06-07 20:35 [PATCH 0/3] KVM: SVM: Clean up LBRv MSRs handling Sean Christopherson
                   ` (2 preceding siblings ...)
  2023-06-07 20:35 ` [PATCH 3/3] KVM: SVM: Use svm_get_lbr_vmcb() helper to handle writes to DEBUGCTL Sean Christopherson
@ 2023-08-03  0:05 ` Sean Christopherson
  3 siblings, 0 replies; 5+ messages in thread
From: Sean Christopherson @ 2023-08-03  0:05 UTC (permalink / raw)
  To: Sean Christopherson, Paolo Bonzini
  Cc: kvm, linux-kernel, Michal Luczaj, Yuan Yao

On Wed, 07 Jun 2023 13:35:16 -0700, Sean Christopherson wrote:
> 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

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

end of thread, other threads:[~2023-08-03  0:06 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-07 20:35 [PATCH 0/3] KVM: SVM: Clean up LBRv MSRs handling Sean Christopherson
2023-06-07 20:35 ` [PATCH 1/3] KVM: SVM: Fix dead KVM_BUG() code in LBR MSR virtualization Sean Christopherson
2023-06-07 20:35 ` [PATCH 2/3] KVM: SVM: Clean up handling of LBR virtualization enabled Sean Christopherson
2023-06-07 20:35 ` [PATCH 3/3] KVM: SVM: Use svm_get_lbr_vmcb() helper to handle writes to DEBUGCTL Sean Christopherson
2023-08-03  0:05 ` [PATCH 0/3] KVM: SVM: Clean up LBRv MSRs handling Sean Christopherson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).