Re: [PATCH v2 19/49] KVM: x86: Add a macro to init CPUID features that ignore host kernel support
From: Maxim Levitsky
Date: Wed Jul 24 2024 - 13:41:01 EST
On Mon, 2024-07-08 at 15:36 -0700, Sean Christopherson wrote:
> On Thu, Jul 04, 2024, Maxim Levitsky wrote:
> > On Fri, 2024-05-17 at 10:38 -0700, Sean Christopherson wrote:
> > > +/*
> > > + * Raw Feature - For features that KVM supports based purely on raw host CPUID,
> > > + * i.e. that KVM virtualizes even if the host kernel doesn't use the feature.
> > > + * Simply force set the feature in KVM's capabilities, raw CPUID support will
> > > + * be factored in by kvm_cpu_cap_mask().
> > > + */
> > > +#define RAW_F(name) \
> > > +({ \
> > > + kvm_cpu_cap_set(X86_FEATURE_##name); \
> > > + F(name); \
> > > +})
> > > +
> > > /*
> > > * Magic value used by KVM when querying userspace-provided CPUID entries and
> > > * doesn't care about the CPIUD index because the index of the function in
> > > @@ -682,15 +694,12 @@ void kvm_set_cpu_caps(void)
> > > F(AVX512VL));
> > >
> > > kvm_cpu_cap_mask(CPUID_7_ECX,
> > > - F(AVX512VBMI) | F(LA57) | F(PKU) | 0 /*OSPKE*/ | F(RDPID) |
> > > + F(AVX512VBMI) | RAW_F(LA57) | F(PKU) | 0 /*OSPKE*/ | F(RDPID) |
> > > F(AVX512_VPOPCNTDQ) | F(UMIP) | F(AVX512_VBMI2) | F(GFNI) |
> > > F(VAES) | F(VPCLMULQDQ) | F(AVX512_VNNI) | F(AVX512_BITALG) |
> > > F(CLDEMOTE) | F(MOVDIRI) | F(MOVDIR64B) | 0 /*WAITPKG*/ |
> > > F(SGX_LC) | F(BUS_LOCK_DETECT)
> > > );
> > > - /* Set LA57 based on hardware capability. */
> > > - if (cpuid_ecx(7) & F(LA57))
> > > - kvm_cpu_cap_set(X86_FEATURE_LA57);
> > >
> > > /*
> > > * PKU not yet implemented for shadow paging and requires OSPKE
> >
> > Putting a function call into a macro which evaluates into a bitmask is somewhat misleading,
> > but let it be...
>
> And weird. Rather than abuse kvm_cpu_cap_set(), what about adding another variable
> scoped to kvm_cpu_cap_init()?
>
> diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
> index 0e64a6332052..b8bc8713a0ec 100644
> --- a/arch/x86/kvm/cpuid.c
> +++ b/arch/x86/kvm/cpuid.c
> @@ -87,12 +87,10 @@ u32 xstate_required_size(u64 xstate_bv, bool compacted)
> /*
> * Raw Feature - For features that KVM supports based purely on raw host CPUID,
> * i.e. that KVM virtualizes even if the host kernel doesn't use the feature.
> - * Simply force set the feature in KVM's capabilities, raw CPUID support will
> - * be factored in by __kvm_cpu_cap_mask().
> */
> #define RAW_F(name) \
> ({ \
> - kvm_cpu_cap_set(X86_FEATURE_##name); \
> + kvm_cpu_cap_passthrough |= F(name); \
> F(name); \
> })
>
> @@ -737,6 +735,7 @@ do { \
> do { \
> const struct cpuid_reg cpuid = x86_feature_cpuid(leaf * 32); \
> const u32 __maybe_unused kvm_cpu_cap_init_in_progress = leaf; \
> + u32 kvm_cpu_cap_passthrough = 0; \
> u32 kvm_cpu_cap_emulated = 0; \
> u32 kvm_cpu_cap_synthesized = 0; \
> \
> @@ -745,6 +744,7 @@ do { \
> else \
> kvm_cpu_caps[leaf] = (mask); \
> \
> + kvm_cpu_caps[leaf] |= kvm_cpu_cap_passthrough; \
> kvm_cpu_caps[leaf] &= (raw_cpuid_get(cpuid) | \
> kvm_cpu_cap_synthesized); \
> kvm_cpu_caps[leaf] |= kvm_cpu_cap_emulated; \
>
I agree, this is better.
Best regards,
Maxim Levitsky