On Fri, 2024-05-17 at 10:39 -0700, Sean Christopherson wrote: > Merge kvm_cpu_cap_init() and kvm_cpu_cap_init_kvm_defined() into a single > helper. The only advantage of separating the two was to make it somewhat > obvious that KVM directly initializes the KVM-defined words, whereas using > a common helper will allow for hardening both kernel- and KVM-defined > CPUID words without needing copy+paste. > > No functional change intended. > > Signed-off-by: Sean Christopherson <seanjc@xxxxxxxxxx> > --- > arch/x86/kvm/cpuid.c | 44 +++++++++++++++----------------------------- > 1 file changed, 15 insertions(+), 29 deletions(-) > > diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c > index f2bd2f5c4ea3..8efffd48cdf1 100644 > --- a/arch/x86/kvm/cpuid.c > +++ b/arch/x86/kvm/cpuid.c > @@ -622,37 +622,23 @@ static __always_inline u32 raw_cpuid_get(struct cpuid_reg cpuid) > return *__cpuid_entry_get_reg(&entry, cpuid.reg); > } > > -/* Mask kvm_cpu_caps for @leaf with the raw CPUID capabilities of this CPU. */ > -static __always_inline void __kvm_cpu_cap_mask(unsigned int leaf) > +static __always_inline void kvm_cpu_cap_init(u32 leaf, u32 mask) > { > const struct cpuid_reg cpuid = x86_feature_cpuid(leaf * 32); > > - reverse_cpuid_check(leaf); > + /* > + * For kernel-defined leafs, mask the boot CPU's pre-populated value. > + * For KVM-defined leafs, explicitly set the leaf, as KVM is the one > + * and only authority. > + */ > + if (leaf < NCAPINTS) > + kvm_cpu_caps[leaf] &= mask; > + else > + kvm_cpu_caps[leaf] = mask; Hi, I have an idea, how about we just initialize the kvm only leafs to 0xFFFFFFFF and then treat them exactly in the same way as kernel regular leafs? Then the user won't have to figure out (assuming that the user doesn't read the comment, who does?) why we use mask as init value. But if you prefer to leave it this way, I won't object either. Best regards, Maxim Levitsky > > kvm_cpu_caps[leaf] &= raw_cpuid_get(cpuid); > } > > -static __always_inline > -void kvm_cpu_cap_init_kvm_defined(enum kvm_only_cpuid_leafs leaf, u32 mask) > -{ > - /* Use kvm_cpu_cap_init for leafs that aren't KVM-only. */ > - BUILD_BUG_ON(leaf < NCAPINTS); > - > - kvm_cpu_caps[leaf] = mask; > - > - __kvm_cpu_cap_mask(leaf); > -} > - > -static __always_inline void kvm_cpu_cap_init(enum cpuid_leafs leaf, u32 mask) > -{ > - /* Use kvm_cpu_cap_init_kvm_defined for KVM-only leafs. */ > - BUILD_BUG_ON(leaf >= NCAPINTS); > - > - kvm_cpu_caps[leaf] &= mask; > - > - __kvm_cpu_cap_mask(leaf); > -} > - > void kvm_set_cpu_caps(void) > { > memset(kvm_cpu_caps, 0, sizeof(kvm_cpu_caps)); > @@ -740,12 +726,12 @@ void kvm_set_cpu_caps(void) > F(AMX_FP16) | F(AVX_IFMA) | F(LAM) > ); > > - kvm_cpu_cap_init_kvm_defined(CPUID_7_1_EDX, > + kvm_cpu_cap_init(CPUID_7_1_EDX, > F(AVX_VNNI_INT8) | F(AVX_NE_CONVERT) | F(PREFETCHITI) | > F(AMX_COMPLEX) > ); > > - kvm_cpu_cap_init_kvm_defined(CPUID_7_2_EDX, > + kvm_cpu_cap_init(CPUID_7_2_EDX, > F(INTEL_PSFD) | F(IPRED_CTRL) | F(RRSBA_CTRL) | F(DDPD_U) | > F(BHI_CTRL) | F(MCDT_NO) > ); > @@ -755,7 +741,7 @@ void kvm_set_cpu_caps(void) > X86_64_F(XFD) > ); > > - kvm_cpu_cap_init_kvm_defined(CPUID_12_EAX, > + kvm_cpu_cap_init(CPUID_12_EAX, > SF(SGX1) | SF(SGX2) | SF(SGX_EDECCSSA) > ); > > @@ -781,7 +767,7 @@ void kvm_set_cpu_caps(void) > if (!tdp_enabled && IS_ENABLED(CONFIG_X86_64)) > kvm_cpu_cap_set(X86_FEATURE_GBPAGES); > > - kvm_cpu_cap_init_kvm_defined(CPUID_8000_0007_EDX, > + kvm_cpu_cap_init(CPUID_8000_0007_EDX, > SF(CONSTANT_TSC) > ); > > @@ -835,7 +821,7 @@ void kvm_set_cpu_caps(void) > kvm_cpu_cap_check_and_set(X86_FEATURE_IBPB_BRTYPE); > kvm_cpu_cap_check_and_set(X86_FEATURE_SRSO_NO); > > - kvm_cpu_cap_init_kvm_defined(CPUID_8000_0022_EAX, > + kvm_cpu_cap_init(CPUID_8000_0022_EAX, > F(PERFMON_V2) > ); >