[PATCH 6/6] KVM: x86: Set KVM LASS based on hardware capability

From: Zeng Guang
Date: Thu Apr 20 2023 - 10:16:45 EST


Host kernel may clear LASS capability in boot_cpu_data.x86_capability
besides explicitly using clearcpuid parameter. That will cause guest
not being able to manage LASS independently. So set KVM LASS directly
based on hardware capability to eliminate the dependency.

Add new helper functions to facilitate getting result of CPUID sub-leaf.

Signed-off-by: Zeng Guang <guang.zeng@xxxxxxxxx>
---
arch/x86/include/asm/cpuid.h | 36 ++++++++++++++++++++++++++++++++++++
arch/x86/kvm/cpuid.c | 4 ++++
2 files changed, 40 insertions(+)

diff --git a/arch/x86/include/asm/cpuid.h b/arch/x86/include/asm/cpuid.h
index 9bee3e7bf973..a25dd00b7c0a 100644
--- a/arch/x86/include/asm/cpuid.h
+++ b/arch/x86/include/asm/cpuid.h
@@ -127,6 +127,42 @@ static inline unsigned int cpuid_edx(unsigned int op)
return edx;
}

+static inline unsigned int cpuid_count_eax(unsigned int op, int count)
+{
+ unsigned int eax, ebx, ecx, edx;
+
+ cpuid_count(op, count, &eax, &ebx, &ecx, &edx);
+
+ return eax;
+}
+
+static inline unsigned int cpuid_count_ebx(unsigned int op, int count)
+{
+ unsigned int eax, ebx, ecx, edx;
+
+ cpuid_count(op, count, &eax, &ebx, &ecx, &edx);
+
+ return ebx;
+}
+
+static inline unsigned int cpuid_count_ecx(unsigned int op, int count)
+{
+ unsigned int eax, ebx, ecx, edx;
+
+ cpuid_count(op, count, &eax, &ebx, &ecx, &edx);
+
+ return ecx;
+}
+
+static inline unsigned int cpuid_count_edx(unsigned int op, int count)
+{
+ unsigned int eax, ebx, ecx, edx;
+
+ cpuid_count(op, count, &eax, &ebx, &ecx, &edx);
+
+ return edx;
+}
+
static __always_inline bool cpuid_function_is_indexed(u32 function)
{
switch (function) {
diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index 5facb8037140..e99b99ebe1fe 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -667,6 +667,10 @@ void kvm_set_cpu_caps(void)
F(AMX_FP16) | F(AVX_IFMA)
);

+ /* Set LASS based on hardware capability */
+ if (cpuid_count_eax(7, 1) & F(LASS))
+ kvm_cpu_cap_set(X86_FEATURE_LASS);
+
kvm_cpu_cap_init_kvm_defined(CPUID_7_1_EDX,
F(AVX_VNNI_INT8) | F(AVX_NE_CONVERT) | F(PREFETCHITI)
);
--
2.27.0