Re: [RFC PATCH 2/3] x86/cpufeatures: Combine word 11 and 12 into new scattered features word 11

From: Sean Christopherson
Date: Fri Jun 14 2019 - 10:44:00 EST


On Fri, Jun 14, 2019 at 04:21:39PM +0200, Borislav Petkov wrote:
> On Fri, Jun 14, 2019 at 07:14:24AM -0700, Sean Christopherson wrote:
> > This is wrong. KVM isn't complaining about shuffling the order of feature
> > words, it's complaining that code is trying to do a reverse CPUID lookup
> > to a feature that isn't in the reverse_cpuid table. Filtering out
> > checks dynamically is just hiding bugs.
>
> No no, reverse_cpuid is hardcoding our feature leafs. This is wrong as
> we want to be able to change those. And reverse_cpuid[] should be able
> to handle that.
>
> KVM is complaining because he removed one leaf. He adds it later in
> patch 3 as a Linux-defined leaf.

Yes, because removing that leaf breaks 'enum cpuid_leafs'. Patch 3/3
"fixes" it by re-inserting a leaf, which causes 'enum cpuid_leafs' to
align with the CPU features.

For example, this assertion also fails:

diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 5b0e9d869ce5..c273b99702d0 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -823,6 +823,7 @@ void get_cpu_cap(struct cpuinfo_x86 *c)
c->x86_capability[CPUID_7_0_EBX] = ebx;
c->x86_capability[CPUID_7_ECX] = ecx;
c->x86_capability[CPUID_7_EDX] = edx;
+ BUILD_BUG_ON(CPUID_7_EDX != X86_FEATURE_ARCH_CAPABILITIES/32);
}

/* Extended state features: level 0x0000000d */

In function âx86_feature_cpuidâ,
inlined from âguest_cpuid_get_registerâ at arch/x86/kvm/cpuid.h:71:25,
inlined from âguest_cpuid_hasâ at arch/x86/kvm/cpuid.h:100:6,
inlined from âkvm_get_msr_commonâ at arch/x86/kvm/x86.c:2824:8:
include/linux/compiler.h:345:38: error: call to â__compiletime_assert_62â declared with attribute error: BUILD_BUG_ON failed: x86_leaf >= ARRAY_SIZE(reverse_cpuid)
_compiletime_assert(condition, msg, __compiletime_assert_, __LINE__)


But this assertion passes because its word is 10, i.e. below the 11/12
words that are getting mucked with.

diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 5b0e9d869ce5..aada9d2fa4df 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -830,6 +830,7 @@ void get_cpu_cap(struct cpuinfo_x86 *c)
cpuid_count(0x0000000d, 1, &eax, &ebx, &ecx, &edx);

c->x86_capability[CPUID_D_1_EAX] = eax;
+ BUILD_BUG_ON(CPUID_D_1_EAX != X86_FEATURE_XSAVES/32);
}

/* AMD-defined flags: level 0x80000001 */


> All that doesn't matter for KVM - if KVM wants to do reverse lookup,
> then it should handle Linux-defined leafs just fine.

KVM can't handle Linux-defined leafs without extra tricks, which is why
I removed get_scattered_cpuid_leaf() or whatever it was called.