Skip to content

Commit c3d6a72

Browse files
committed
KVM: VMX: Dedup code for adding MSR to VMCS's auto list
Add a helper to add an MSR to a VMCS's "auto" list to deduplicate the code in add_atomic_switch_msr(), and so that the functionality can be used in the future for managing the MSR auto-store list. No functional change intended. Reviewed-by: Dapeng Mi <dapeng1.mi@linux.intel.com> Tested-by: Manali Shukla <manali.shukla@amd.com> Link: https://patch.msgid.link/20251206001720.468579-43-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 2239d13 commit c3d6a72

1 file changed

Lines changed: 19 additions & 22 deletions

File tree

arch/x86/kvm/vmx/vmx.c

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,12 +1093,28 @@ static __always_inline void add_atomic_switch_msr_special(struct vcpu_vmx *vmx,
10931093
vm_exit_controls_setbit(vmx, exit);
10941094
}
10951095

1096+
static void vmx_add_auto_msr(struct vmx_msrs *m, u32 msr, u64 value,
1097+
unsigned long vmcs_count_field, struct kvm *kvm)
1098+
{
1099+
int i;
1100+
1101+
i = vmx_find_loadstore_msr_slot(m, msr);
1102+
if (i < 0) {
1103+
if (KVM_BUG_ON(m->nr == MAX_NR_LOADSTORE_MSRS, kvm))
1104+
return;
1105+
1106+
i = m->nr++;
1107+
m->val[i].index = msr;
1108+
vmcs_write32(vmcs_count_field, m->nr);
1109+
}
1110+
m->val[i].value = value;
1111+
}
1112+
10961113
static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
10971114
u64 guest_val, u64 host_val)
10981115
{
10991116
struct msr_autoload *m = &vmx->msr_autoload;
11001117
struct kvm *kvm = vmx->vcpu.kvm;
1101-
int i;
11021118

11031119
switch (msr) {
11041120
case MSR_EFER:
@@ -1132,27 +1148,8 @@ static void add_atomic_switch_msr(struct vcpu_vmx *vmx, unsigned msr,
11321148
wrmsrq(MSR_IA32_PEBS_ENABLE, 0);
11331149
}
11341150

1135-
i = vmx_find_loadstore_msr_slot(&m->guest, msr);
1136-
if (i < 0) {
1137-
if (KVM_BUG_ON(m->guest.nr == MAX_NR_LOADSTORE_MSRS, kvm))
1138-
return;
1139-
1140-
i = m->guest.nr++;
1141-
m->guest.val[i].index = msr;
1142-
vmcs_write32(VM_ENTRY_MSR_LOAD_COUNT, m->guest.nr);
1143-
}
1144-
m->guest.val[i].value = guest_val;
1145-
1146-
i = vmx_find_loadstore_msr_slot(&m->host, msr);
1147-
if (i < 0) {
1148-
if (KVM_BUG_ON(m->host.nr == MAX_NR_LOADSTORE_MSRS, kvm))
1149-
return;
1150-
1151-
i = m->host.nr++;
1152-
m->host.val[i].index = msr;
1153-
vmcs_write32(VM_EXIT_MSR_LOAD_COUNT, m->host.nr);
1154-
}
1155-
m->host.val[i].value = host_val;
1151+
vmx_add_auto_msr(&m->guest, msr, guest_val, VM_ENTRY_MSR_LOAD_COUNT, kvm);
1152+
vmx_add_auto_msr(&m->guest, msr, host_val, VM_EXIT_MSR_LOAD_COUNT, kvm);
11561153
}
11571154

11581155
static bool update_transition_efer(struct vcpu_vmx *vmx)

0 commit comments

Comments
 (0)