Re: [PATCH v2 08/25] KVM: VMX: Initialize VMCS FRED fields

From: Sean Christopherson
Date: Wed Jun 12 2024 - 17:41:43 EST


On Wed, Feb 07, 2024, Xin Li wrote:
> diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
> index d58ed2d3d379..b7b772183ee4 100644
> --- a/arch/x86/kvm/vmx/vmx.c
> +++ b/arch/x86/kvm/vmx/vmx.c
> @@ -1470,6 +1470,18 @@ void vmx_vcpu_load_vmcs(struct kvm_vcpu *vcpu, int cpu,
> (unsigned long)(cpu_entry_stack(cpu) + 1));
> }
>
> +#ifdef CONFIG_X86_64

Don't bother, practically no one cares about 32-bit KVM these days, and I highly
don't anyone that runs 32-bit KVM cares about the code footprint to this degree.

> + /* Per-CPU FRED MSRs */
> + if (kvm_cpu_cap_has(X86_FEATURE_FRED)) {
> + vmcs_write64(HOST_IA32_FRED_RSP1, read_msr(MSR_IA32_FRED_RSP1));
> + vmcs_write64(HOST_IA32_FRED_RSP2, read_msr(MSR_IA32_FRED_RSP2));
> + vmcs_write64(HOST_IA32_FRED_RSP3, read_msr(MSR_IA32_FRED_RSP3));
> + vmcs_write64(HOST_IA32_FRED_SSP1, read_msr(MSR_IA32_FRED_SSP1));
> + vmcs_write64(HOST_IA32_FRED_SSP2, read_msr(MSR_IA32_FRED_SSP2));
> + vmcs_write64(HOST_IA32_FRED_SSP3, read_msr(MSR_IA32_FRED_SSP3));

That's a lot of RDMSRs to eat on every task migration. How hard would it be to
add a per-CPU cache for each of these? Or is there a pre-existing way to get at
the info that's faster than RDMSR?

> + }
> +#endif
> +
> vmx->loaded_vmcs->cpu = cpu;
> }
> }
> @@ -4321,6 +4333,15 @@ void vmx_set_constant_host_state(struct vcpu_vmx *vmx)
> */
> vmcs_write16(HOST_DS_SELECTOR, 0);
> vmcs_write16(HOST_ES_SELECTOR, 0);
> +
> + /*
> + * FRED MSRs are per-cpu, however FRED CONFIG and STKLVLS MSRs
> + * are the same on all CPUs, thus they are initialized here.

Eh, just trim this to:

/* FRED CONFIG and STKLVLS are the same on all CPUs. */

> + */
> + if (kvm_cpu_cap_has(X86_FEATURE_FRED)) {
> + vmcs_write64(HOST_IA32_FRED_CONFIG, read_msr(MSR_IA32_FRED_CONFIG));
> + vmcs_write64(HOST_IA32_FRED_STKLVLS, read_msr(MSR_IA32_FRED_STKLVLS));
> + }
> #else
> vmcs_write16(HOST_DS_SELECTOR, __KERNEL_DS); /* 22.2.4 */
> vmcs_write16(HOST_ES_SELECTOR, __KERNEL_DS); /* 22.2.4 */
> @@ -4865,6 +4886,19 @@ static void __vmx_vcpu_reset(struct kvm_vcpu *vcpu)
> */
> vmx->pi_desc.nv = POSTED_INTR_VECTOR;
> vmx->pi_desc.sn = 1;
> +
> +#ifdef CONFIG_X86_64
> + if (kvm_cpu_cap_has(X86_FEATURE_FRED)) {
> + vmcs_write64(GUEST_IA32_FRED_CONFIG, 0);
> + vmcs_write64(GUEST_IA32_FRED_RSP1, 0);
> + vmcs_write64(GUEST_IA32_FRED_RSP2, 0);
> + vmcs_write64(GUEST_IA32_FRED_RSP3, 0);
> + vmcs_write64(GUEST_IA32_FRED_STKLVLS, 0);
> + vmcs_write64(GUEST_IA32_FRED_SSP1, 0);
> + vmcs_write64(GUEST_IA32_FRED_SSP2, 0);
> + vmcs_write64(GUEST_IA32_FRED_SSP3, 0);
> + }

Somewhat of a moot point, but this belongs in init_vmcs(), not __vmx_vcpu_reset().