[PATCH] x86/fpu/xstate: clear XSAVE features if DISABLED_MASK set

From: Jon Kohler
Date: Tue May 30 2023 - 16:02:59 EST


Respect DISABLED_MASK when clearing XSAVE features, such that features
that are disabled do not appear in the xfeatures mask.

This is important for kvm_load_{guest|host}_xsave_state, which look
at host_xcr0 and will do an expensive xsetbv when the guest and host
do not match.

A prime example if CONFIG_X86_INTEL_MEMORY_PROTECTION_KEYS is disabled,
the guest OS will not see PKU masked; however, the guest will incur
xsetbv since the host mask will never match the guest, even though
DISABLED_MASK16 has DISABLE_PKU set.

Signed-off-by: Jon Kohler <jon@xxxxxxxxxxx>
CC: kvm@xxxxxxxxxxxxxxx
CC: Sean Christopherson <seanjc@xxxxxxxxxx>
CC: Paolo Bonzini <pbonzini@xxxxxxxxxx>
---
arch/x86/kernel/fpu/xstate.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c
index 0bab497c9436..211ef82b53e3 100644
--- a/arch/x86/kernel/fpu/xstate.c
+++ b/arch/x86/kernel/fpu/xstate.c
@@ -798,7 +798,8 @@ void __init fpu__init_system_xstate(unsigned int legacy_size)
unsigned short cid = xsave_cpuid_features[i];

/* Careful: X86_FEATURE_FPU is 0! */
- if ((i != XFEATURE_FP && !cid) || !boot_cpu_has(cid))
+ if ((i != XFEATURE_FP && !cid) || !boot_cpu_has(cid) ||
+ DISABLED_MASK_BIT_SET(cid))
fpu_kernel_cfg.max_features &= ~BIT_ULL(i);
}

--
2.30.1 (Apple Git-130)