Skip to content

Commit d518f8c

Browse files
committed
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. Reported-by: Michal Luczaj <mhal@rbox.co> Cc: Yuan Yao <yuan.yao@intel.com> Link: https://lore.kernel.org/r/20230607203519.1570167-2-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent a6bb570 commit d518f8c

File tree

1 file changed

+16
-29
lines changed

1 file changed

+16
-29
lines changed

arch/x86/kvm/svm/svm.c

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -980,43 +980,22 @@ static void svm_disable_lbrv(struct kvm_vcpu *vcpu)
980980
svm_copy_lbrs(svm->vmcb01.ptr, svm->vmcb);
981981
}
982982

983-
static int svm_get_lbr_msr(struct vcpu_svm *svm, u32 index)
983+
static struct vmcb *svm_get_lbr_vmcb(struct vcpu_svm *svm)
984984
{
985985
/*
986-
* If the LBR virtualization is disabled, the LBR msrs are always
987-
* kept in the vmcb01 to avoid copying them on nested guest entries.
988-
*
989-
* If nested, and the LBR virtualization is enabled/disabled, the msrs
990-
* are moved between the vmcb01 and vmcb02 as needed.
986+
* If LBR virtualization is disabled, the LBR MSRs are always kept in
987+
* vmcb01. If LBR virtualization is enabled and L1 is running VMs of
988+
* its own, the MSRs are moved between vmcb01 and vmcb02 as needed.
991989
*/
992-
struct vmcb *vmcb =
993-
(svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK) ?
994-
svm->vmcb : svm->vmcb01.ptr;
995-
996-
switch (index) {
997-
case MSR_IA32_DEBUGCTLMSR:
998-
return vmcb->save.dbgctl;
999-
case MSR_IA32_LASTBRANCHFROMIP:
1000-
return vmcb->save.br_from;
1001-
case MSR_IA32_LASTBRANCHTOIP:
1002-
return vmcb->save.br_to;
1003-
case MSR_IA32_LASTINTFROMIP:
1004-
return vmcb->save.last_excp_from;
1005-
case MSR_IA32_LASTINTTOIP:
1006-
return vmcb->save.last_excp_to;
1007-
default:
1008-
KVM_BUG(false, svm->vcpu.kvm,
1009-
"%s: Unknown MSR 0x%x", __func__, index);
1010-
return 0;
1011-
}
990+
return svm->vmcb->control.virt_ext & LBR_CTL_ENABLE_MASK ? svm->vmcb :
991+
svm->vmcb01.ptr;
1012992
}
1013993

1014994
void svm_update_lbrv(struct kvm_vcpu *vcpu)
1015995
{
1016996
struct vcpu_svm *svm = to_svm(vcpu);
1017997

1018-
bool enable_lbrv = svm_get_lbr_msr(svm, MSR_IA32_DEBUGCTLMSR) &
1019-
DEBUGCTLMSR_LBR;
998+
bool enable_lbrv = svm_get_lbr_vmcb(svm)->save.dbgctl & DEBUGCTLMSR_LBR;
1020999

10211000
bool current_enable_lbrv = !!(svm->vmcb->control.virt_ext &
10221001
LBR_CTL_ENABLE_MASK);
@@ -2835,11 +2814,19 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
28352814
msr_info->data = svm->tsc_aux;
28362815
break;
28372816
case MSR_IA32_DEBUGCTLMSR:
2817+
msr_info->data = svm_get_lbr_vmcb(svm)->save.dbgctl;
2818+
break;
28382819
case MSR_IA32_LASTBRANCHFROMIP:
2820+
msr_info->data = svm_get_lbr_vmcb(svm)->save.br_from;
2821+
break;
28392822
case MSR_IA32_LASTBRANCHTOIP:
2823+
msr_info->data = svm_get_lbr_vmcb(svm)->save.br_to;
2824+
break;
28402825
case MSR_IA32_LASTINTFROMIP:
2826+
msr_info->data = svm_get_lbr_vmcb(svm)->save.last_excp_from;
2827+
break;
28412828
case MSR_IA32_LASTINTTOIP:
2842-
msr_info->data = svm_get_lbr_msr(svm, msr_info->index);
2829+
msr_info->data = svm_get_lbr_vmcb(svm)->save.last_excp_to;
28432830
break;
28442831
case MSR_VM_HSAVE_PA:
28452832
msr_info->data = svm->nested.hsave_msr;

0 commit comments

Comments
 (0)